Skip to content

Commit d9cbc9d

Browse files
author
ehennum
committed
Merge branch 'develop-151' into develop
2 parents 5a5075c + 25f12f8 commit d9cbc9d

File tree

743 files changed

+17333
-764
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

743 files changed

+17333
-764
lines changed

.npmignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
config-optic/
2+
examples/
3+
etc/
4+
test-basic/
5+
test-basic-proxy/
6+
test-complete/
7+
gulpfile.js
8+
.jshintrc
9+
jsdoc.json

gulpfile.js

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,19 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
const path = require('path');
1617
const gulp = require('gulp');
1718
const jshint = require('gulp-jshint');
1819
const mocha = require('gulp-mocha');
1920
const jsdoc = require('gulp-jsdoc3');
2021

22+
const { parallel, series } = gulp;
23+
24+
const marklogic = require('./');
25+
const proxy = require('./lib/proxy-generator.js');
26+
const testproxy = require('./test-basic-proxy/testGenerator.js');
27+
const testconfig = require('./etc/test-config.js');
28+
const basicloader = require('./lib/basic-loader.js');
2129

2230
function lint() {
2331
return gulp.src('lib/*')
@@ -36,13 +44,128 @@ function test() {
3644
}
3745

3846
function doc() {
39-
// TODO: clear the directory first
47+
// TODO: clear the directory first - maybe by following this recipe:
48+
// https://github.com/gulpjs/gulp/blob/master/docs/recipes/delete-files-folder.md
4049
const config = require('./jsdoc.json');
4150
return gulp.src(['./lib/*.js', 'README.md'])
4251
.pipe(jsdoc(config));
4352
}
4453

54+
let testModulesClient = null;
55+
function getTestModulesClient() {
56+
if (testModulesClient === null) {
57+
const connectionParams = {
58+
host: testconfig.restEvaluatorConnection.host,
59+
port: testconfig.restEvaluatorConnection.port,
60+
user: testconfig.restEvaluatorConnection.user,
61+
password: testconfig.restEvaluatorConnection.password,
62+
authType: testconfig.restEvaluatorConnection.authType,
63+
database: 'unittest-nodeapi-modules'
64+
};
65+
testModulesClient = marklogic.createDatabaseClient(connectionParams);
66+
}
67+
return testModulesClient;
68+
}
69+
function getTestDocumentPermissions() {
70+
return [
71+
{'role-name':'rest-reader', capabilities:['read', 'execute']},
72+
{'role-name':'rest-writer', capabilities:['update']}
73+
];
74+
}
75+
76+
function loadProxyTestInspector(callback) {
77+
const testdir = path.resolve('test-basic-proxy');
78+
const modulesdir = path.resolve(testdir, 'ml-modules');
79+
const filePath = path.resolve(modulesdir, 'testInspector.sjs');
80+
const databaseClient = getTestModulesClient();
81+
const documentDescriptor = {
82+
uri: '/dbf/test/testInspector.sjs',
83+
contentType: 'application/vnd.marklogic-javascript',
84+
permissions: getTestDocumentPermissions()
85+
};
86+
basicloader.loadFile(callback, {
87+
filePath: filePath,
88+
databaseClient: databaseClient,
89+
documentDescriptor: documentDescriptor
90+
});
91+
}
92+
93+
function loadProxyTestData(callback) {
94+
const testdir = path.resolve('test-basic-proxy');
95+
const filePath = path.resolve(testdir, 'testdef.json');
96+
const databaseClient = marklogic.createDatabaseClient(testconfig.restWriterConnection);
97+
const documentDescriptor = {
98+
uri: '/dbf/test.json',
99+
contentType: 'application/json'
100+
};
101+
basicloader.loadFile(callback, {
102+
filePath: filePath,
103+
databaseClient: databaseClient,
104+
documentDescriptor: documentDescriptor
105+
});
106+
}
107+
108+
function loadProxyTestCases(callback) {
109+
const databaseClient = getTestModulesClient();
110+
const documentMetadata = {
111+
collections: ['/dbf/test/cases'],
112+
permissions: getTestDocumentPermissions()
113+
};
114+
const uriPrefix = '/dbf/test/';
115+
const uriStartDepth = 4;
116+
return gulp.src([
117+
'test-basic-proxy/ml-modules/*/*/service.json',
118+
'test-basic-proxy/ml-modules/*/*/*.api',
119+
'test-basic-proxy/ml-modules/*/*/*.sjs',
120+
'test-basic-proxy/ml-modules/*/*/*.mjs',
121+
'test-basic-proxy/ml-modules/*/*/*.xqy'
122+
])
123+
.pipe(basicloader.loadFileStream({
124+
databaseClient: databaseClient,
125+
documentMetadata: documentMetadata,
126+
uriPrefix: uriPrefix,
127+
uriStartDepth: uriStartDepth
128+
}));
129+
}
130+
function positiveProxyTests() {
131+
return gulp.src('test-basic-proxy/ml-modules/positive/*')
132+
.pipe(proxy.generate())
133+
.pipe(gulp.dest('test-basic-proxy/lib/positive/'));
134+
}
135+
function negativeProxyTests() {
136+
return gulp.src('test-basic-proxy/ml-modules/negative/*')
137+
.pipe(proxy.generate())
138+
.pipe(gulp.dest('test-basic-proxy/lib/negative/'));
139+
}
140+
function generatedProxyTests() {
141+
return gulp.src('test-basic-proxy/ml-modules/generated/*')
142+
.pipe(testproxy.generate())
143+
.pipe(gulp.dest('test-basic-proxy/lib/generated/'));
144+
}
145+
function runProxyTests() {
146+
return gulp.src(['test-basic-proxy/lib/*/*Test.js'])
147+
.pipe(mocha({
148+
reporter: 'spec',
149+
globals: {
150+
should: require('chai')
151+
}
152+
}));
153+
}
154+
function proxyDocTests() {
155+
return gulp.src(['test-basic-proxy/lib/positive/DescribedBundle.js'])
156+
.pipe(jsdoc({opts:{destination:'test-basic-proxy/doc'}}));
157+
}
158+
45159
exports.doc = doc;
46160
exports.lint = lint;
161+
exports.loadProxyTests = parallel(loadProxyTestInspector, loadProxyTestData, loadProxyTestCases);
162+
exports.generateProxyTests = parallel(positiveProxyTests, negativeProxyTests, generatedProxyTests);
163+
exports.generateProxyDocTests = proxyDocTests;
164+
exports.runProxyTests = runProxyTests;
165+
exports.proxyTests = series(
166+
parallel(loadProxyTestInspector, loadProxyTestData, loadProxyTestCases),
167+
parallel(positiveProxyTests, negativeProxyTests, generatedProxyTests),
168+
proxyDocTests,
169+
runProxyTests);
47170
exports.test = test;
48171
exports.default = lint;

lib/basic-loader.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright 2019 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
'use strict';
17+
18+
const fs = require('fs');
19+
const path = require('path');
20+
21+
const {Writable} = require('stream');
22+
23+
const Vinyl = require('./optional.js').library('vinyl');
24+
25+
function getDatabaseClient(loader, options) {
26+
if (options === void 0 || options == null) {
27+
throw new Error(`${loader} requires options`);
28+
}
29+
30+
const databaseClient = options.databaseClient;
31+
if (databaseClient === void 0 || databaseClient == null) {
32+
throw new Error(`${loader} requires databaseClient option`);
33+
}
34+
35+
return databaseClient;
36+
}
37+
38+
function loadFile(callback, options) {
39+
const databaseClient = getDatabaseClient('loadFile', options);
40+
41+
const filePath = options.filePath;
42+
if (filePath === void 0 || filePath == null) {
43+
throw new Error('loadFile requires the filePath for the source file');
44+
}
45+
46+
const documentDescriptor = options.documentDescriptor;
47+
if (documentDescriptor === void 0 || documentDescriptor == null) {
48+
throw new Error(`loadFile requires fileDescriptor option`);
49+
}
50+
51+
fs.readFile(filePath, 'utf8', (err, data) => {
52+
if (err) {
53+
callback(err);
54+
return;
55+
}
56+
documentDescriptor.content = data;
57+
databaseClient.documents.write(documentDescriptor)
58+
.result(output => callback(), callback);
59+
});
60+
}
61+
62+
function loadFileStream(options) {
63+
const databaseClient = getDatabaseClient('loadFileStream', options);
64+
65+
const documentMetadata = options.documentMetadata;
66+
if (documentMetadata === void 0 || documentMetadata == null) {
67+
throw new Error(`loadFileStream requires documentMetadata option`);
68+
}
69+
70+
let uriPrefix = options.uriPrefix;
71+
if (uriPrefix === void 0 || uriPrefix == null) {
72+
uriPrefix = '/';
73+
} else if (!uriPrefix.endsWith('/')) {
74+
uriPrefix += '/';
75+
}
76+
77+
let uriStartDepth = options.uriStartDepth;
78+
if (uriStartDepth === void 0 || uriStartDepth == null) {
79+
uriStartDepth = 2;
80+
}
81+
uriStartDepth += path.resolve().split(path.sep).length - 1;
82+
83+
const bufferMax = 100;
84+
const batchSeed = [documentMetadata];
85+
const fileBuffer = new Array(bufferMax);
86+
let bufferNext = 0;
87+
return new Writable({
88+
objectMode: true,
89+
write(file, encoding, callback) {
90+
if (!Vinyl.isVinyl(file) || file.isDirectory() || file.path === void 0 || !(file.path.length > 0)) {
91+
console.trace('not Vinyl file: '+file);
92+
callback();
93+
return;
94+
}
95+
fileBuffer[bufferNext++] = {
96+
uri: uriPrefix + file.path.split(path.sep).slice(uriStartDepth).join('/'),
97+
content: file.contents
98+
};
99+
if (bufferNext < bufferMax) {
100+
callback();
101+
return;
102+
}
103+
databaseClient.documents.write(batchSeed.concat(fileBuffer))
104+
.result(output => callback(), callback);
105+
bufferNext = 0;
106+
},
107+
final(callback) {
108+
if (bufferNext > 0) {
109+
databaseClient.documents.write(batchSeed.concat(fileBuffer.slice(0, bufferNext)))
110+
.result(output => callback(), callback);
111+
bufferNext = 0;
112+
} else {
113+
callback();
114+
}
115+
}
116+
});
117+
}
118+
119+
module.exports = {
120+
loadFile: loadFile,
121+
loadFileStream: loadFileStream
122+
};

0 commit comments

Comments
 (0)