-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
359 lines (315 loc) · 11.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/**
* @typedef { import("xmlrpc").Client } XMLRPCClient
*/
/**
* @typedef { import("streamx").Transform } Transform
*/
// dependencies
const os = require('os')
const { Transform } = require('streamx')
const log = require('fancy-log')
const PluginError = require('plugin-error')
const assign = require('lodash.assign')
const Path = require('path')
const { connect, getMimeType, defineMimeTypes, readOptionsFromEnv } = require('@existdb/node-exist')
/**
* @typedef {Object} GulpExistConnectionOptions
* @prop {string} [host] database host, default: "localhost"
* @prop {string} [port] database port, default: "8080"
* @prop {boolean} [secure] use HTTPS? default: false
* @prop {string} [path] path to XMLRPC, default: "/exist/xmlrpc"
* @prop {user:string, pass:string} [basic_auth] database user credentials, default: "guest/guest"
*/
/**
* NOTE: gulp-exist will still default to HTTP!
*
* But if your existdb instance has a proper certificate,
* you can now switch to HTTPS.
* Set "secure" to true and the "port" to 8443 (the port
* configured to serve HTTPS may differ in your installation)
* @type {GulpExistConnectionOptions}
*/
const defaultRPCoptions = {
host: 'localhost',
port: '8443',
secure: true,
path: '/exist/xmlrpc',
basic_auth: {
user: 'guest',
pass: 'guest'
}
}
/**
* @typedef {string} UnixPermission unix style permission string
*
*/
/**
* @typedef {Object} GulpExistUploadOptions
* @prop {boolean} [html5AsBinary] override mimetype for invalid HTML, default: false
* @prop {string} target collection to write to, default: ""
* @prop {Object<string,UnixPermission>} [permissions] mapping of filename to unix style permission string
*/
/**
* @type {GulpExistUploadOptions}
*/
const defaultUploadOptions = {
html5AsBinary: false,
target: '',
permissions: null
}
/**
* @typedef {Object} GulpExistQueryOptions
* @prop {boolean} [printXqlResults] default: true
* @prop {"xml"|"json"|string} xqlOutputExt the file extension the results are written to
* @prop {Object} queryParams query parameters passed to eXist-db
*/
/**
* @type {GulpExistQueryOptions}
*/
const defaultQueryOptions = {
printXqlResults: true,
xqlOutputExt: 'xml',
queryParams: {}
}
const isWin = os.platform() === 'win32'
function isSaxParserError (error) {
return error && error.faultString && /SAXParseException/.test(error.faultString)
}
function normalizePath (path) {
return isWin ? Path.normalize(path).replace(/\\/g, '/') : Path.normalize(path)
}
function createCollection (client, collection) {
const normalizedCollectionPath = normalizePath(collection)
log('Creating collection "' + normalizedCollectionPath + '"...')
return client.collections.create(normalizedCollectionPath)
}
/**
* upload a file to the connected database
* missing collections will be created
*
* @param {XMLRPCClient} client
* @param {GulpExistUploadOptions} options
* @return {Transform} store files from stream
*/
function dest (client, options) {
const conf = assign({}, defaultUploadOptions, options)
return new Transform({
transform (file, callback) {
if (file.isStream()) {
return this.emit('error', new PluginError('gulp-exist', 'Streaming not supported'))
}
if (file.isDirectory()) {
return createCollection(client, normalizePath(conf.target + '/' + file.relative))
.then(_ => callback())
.catch(e => callback(e))
}
if (file.isNull()) {
return callback()
}
const remotePath = normalizePath(conf.target + '/' + file.relative)
const folder = file.relative.substring(0, file.relative.length - file.basename.length)
const collection = Path.normalize(conf.target) + '/' + folder
// create target collection if neccessary
return client.collections.describe(collection)
.then(null, function (e) {
if (e.faultString) {
log(`collection ${collection} not found`)
return createCollection(client, collection)
}
// server may be down, unreachable or misconfigured
return Promise.reject(e)
})
// then upload file
.then(function (result) {
log('Storing "' + file.base + file.relative + '" as (' + getMimeType(file.path) + ')...')
return client.documents.upload(file.contents)
})
// parse file on server
.then(function (result) {
return client.documents.parseLocal(result, remotePath, { mimetype: getMimeType(file.path) })
})
// handle re-upload as octet stream if parsing failed and html5AsBinary is set
.then(null, function (error) {
if (isSaxParserError(error) && conf.html5AsBinary && file.extname === '.html') {
log(file.relative + ' is not well-formed XML, storing as binary...')
return client.documents.upload(file.contents)
.then(function (result) {
return client.documents.parseLocal(result, remotePath, { mimetype: 'application/octet-stream' })
})
} else {
throw error
}
})
// Then override permissions if specified in options
.then(function (result) {
if (conf.permissions && file.relative in conf.permissions) {
log('Setting permissions for "' + normalizePath(file.relative) + '" (' + conf.permissions[file.relative] + ')...')
return client.resources.setPermissions(remotePath, conf.permissions[file.relative])
}
})
// Print result and proceed to next file
.then(function (result) {
log(' ✔ ︎' + remotePath + ' stored')
return callback(null, file)
})
.catch(function (error) {
let errorMessage
if (isSaxParserError(error)) {
// Failed to invoke method parseLocal in class org.exist.xmlrpc.RpcConnection: org.xml.sax.SAXException:
errorMessage = error.faultString.split('\n')[0].substring(102)
} else {
errorMessage = error.message
}
log.error(' ✖ ' + remotePath + ' was not stored. Reason:', errorMessage)
return callback(error)
})
}
})
}
/**
* upload and execute an xquery script
* save the results to a file
* appends the date of execution
* and the expected file extension
*
* @param {XMLRPCClient} client
* @param {GulpExistQueryOptions} options
* @return {Transform} upload and execute files from stream
*/
function query (client, options) {
const conf = assign({}, defaultQueryOptions, options)
return new Transform({
transform (file, callback) {
if (file.isStream()) {
return callback(new PluginError('gulp-exist', 'Streaming not supported'))
}
if (file.isDirectory() || file.isNull()) {
callback()
return
}
log('Running XQuery on server: ' + file.relative)
client.queries.readAll(file.contents, conf.queryParams)
.then(function (result) {
const resultBuffer = Buffer.concat(result.pages)
if (conf.printXqlResults) {
log(resultBuffer.toString())
}
file.extname = `.${new Date().toJSON()}.${conf.xqlOutputExt}`
file.contents = resultBuffer
return callback(null, file)
})
.catch(function (error) {
return callback(new PluginError('gulp-exist', 'Error running XQuery ' + file.relative + ':\n' + error))
})
}
})
}
/**
* check if a file exists in the database and if the local file is newer
*
* @param {XMLRPCClient} client
* @param {GulpExistUploadOptions} options
* @return {Transform} filter files from stream that are older
*/
function newer (client, options) {
const conf = assign({}, defaultUploadOptions, options)
return new Transform({
transform (file, callback) {
if (file.isDirectory()) {
const collection = normalizePath(conf.target + '/' + file.relative)
client.collections.describe(collection)
.then(function () {
callback(null)
}, function () {
callback(null, file)
})
return
}
client.resources.describe(normalizePath(conf.target + '/' + file.relative))
.then(function (resourceInfo) {
const newer = !Object.prototype.hasOwnProperty.call(resourceInfo, 'modified') || (Date.parse(file.stat.mtime) > Date.parse(resourceInfo.modified))
callback(null, newer ? file : null)
})
.catch(function (e) {
callback(e)
})
}
})
}
/**
* @typedef {Object} GulpExistInstallationOptions
* @prop {string} [packageUri] deprecated
* @prop {string} [customPackageRepoUrl]
*/
/**
* Install a XAR package in the database
*
* @param {XMLRPCClient} client database client
* @param {GulpExistInstallationOptions} options installation options
* @return {Transform} install XAR from vinyl file stream
*/
function install (client, options) {
const customPackageRepoUrl = options && options.customPackageRepoUrl ? options.customPackageRepoUrl : null
return new Transform({
transform (file, callback) {
const xarName = file.basename
if (file.isStream()) { return callback(new PluginError('gulp-exist', 'Streaming not supported')) }
if (file.isDirectory()) { return callback(new PluginError('gulp-exist', `Source "${xarName}" is a directory`)) }
if (file.isNull()) { return callback(new PluginError('gulp-exist', `Source "${xarName}" is null`)) }
if (file.extname !== '.xar') { return callback(new PluginError('gulp-exist', `Source "${xarName}" is not a XAR package`)) }
log(`Uploading ${xarName} (${file.contents.length} bytes)`)
client.app.upload(file.contents, xarName)
.then(response => {
if (!response.success) {
return callback(new PluginError('gulp-exist', `XAR was not uploaded: ${response.error}`))
}
log(`Install ${xarName}`)
return client.app.install(xarName, customPackageRepoUrl)
})
.then(response => {
if (!response.success) {
return callback(new PluginError('gulp-exist', `XAR Installation failed: ${response.error}`))
}
if (response.result.update) {
log('Application was updated')
return callback(null, response)
}
log('Application was installed')
callback(null, response)
})
.catch(error => {
callback(new PluginError('gulp-exist', `XAR Installation failed: ${error}`))
})
}
})
}
/**
* @typedef {Object} GulpExist
* @prop {(options:GulpExistUploadOptions) => Transform} dest
* @prop {(options:GulpExistQueryOptions) => Transform} query
* @prop {(options:GulpExistUploadOptions) => Transform} newer
* @prop {(options:GulpExistInstallationOptions) => Transform} install
*/
/**
* create database client and bind methods to it
*
* @param {GulpExistConnectionOptions} options
* @return {GulpExist} bound methods
*/
function createClient (options) {
// TODO sanity checks
const _options = assign({}, defaultRPCoptions, options)
const client = connect(_options)
return {
dest: dest.bind(null, client),
query: query.bind(null, client),
newer: newer.bind(null, client),
install: install.bind(null, client)
}
}
module.exports = {
createClient,
defineMimeTypes,
getMimeType,
readOptionsFromEnv
}