@jsreport/jsreport-core 3.0.0 → 3.1.2-test.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +166 -166
- package/README.md +298 -284
- package/index.js +29 -27
- package/lib/main/blobStorage/blobStorage.js +52 -47
- package/lib/main/blobStorage/inMemoryProvider.js +27 -27
- package/lib/main/blobStorage/mainActions.js +24 -24
- package/lib/main/createDefaultLoggerFormat.js +17 -17
- package/lib/main/defaults.js +14 -14
- package/lib/main/extensions/discover.js +20 -20
- package/lib/main/extensions/extensionsManager.js +264 -265
- package/lib/main/extensions/fileUtils.js +56 -55
- package/lib/main/extensions/findVersion.js +49 -53
- package/lib/main/extensions/locationCache.js +103 -97
- package/lib/main/extensions/sorter.js +10 -10
- package/lib/main/extensions/validateMinimalVersion.js +50 -50
- package/lib/main/folders/cascadeFolderRemove.js +25 -25
- package/lib/main/folders/getEntitiesInFolder.js +53 -53
- package/lib/main/folders/index.js +42 -42
- package/lib/main/folders/moveBetweenFolders.js +354 -354
- package/lib/main/folders/validateDuplicatedName.js +107 -107
- package/lib/main/folders/validateReservedName.js +53 -53
- package/lib/main/logger.js +244 -244
- package/lib/main/migration/resourcesToAssets.js +230 -210
- package/lib/main/migration/xlsxTemplatesToAssets.js +128 -118
- package/lib/main/monitoring.js +91 -91
- package/lib/main/optionsLoad.js +237 -237
- package/lib/main/optionsSchema.js +237 -237
- package/lib/main/profiler.js +2 -1
- package/lib/main/reporter.js +575 -578
- package/lib/main/schemaValidator.js +252 -252
- package/lib/main/settings.js +154 -154
- package/lib/main/store/checkDuplicatedId.js +27 -27
- package/lib/main/store/collection.js +329 -329
- package/lib/main/store/documentStore.js +469 -469
- package/lib/main/store/mainActions.js +28 -28
- package/lib/main/store/memoryStoreProvider.js +99 -99
- package/lib/main/store/queue.js +48 -48
- package/lib/main/store/referenceUtils.js +251 -251
- package/lib/main/store/setupValidateId.js +43 -43
- package/lib/main/store/setupValidateShortid.js +71 -71
- package/lib/main/store/transaction.js +69 -69
- package/lib/main/store/typeUtils.js +180 -180
- package/lib/main/templates.js +34 -34
- package/lib/main/validateEntityName.js +62 -62
- package/lib/shared/createError.js +36 -36
- package/lib/shared/encryption.js +114 -114
- package/lib/shared/folders/index.js +11 -11
- package/lib/shared/folders/normalizeEntityPath.js +15 -15
- package/lib/shared/folders/resolveEntityFromPath.js +88 -88
- package/lib/shared/folders/resolveEntityPath.js +46 -46
- package/lib/shared/folders/resolveFolderFromPath.js +38 -38
- package/lib/shared/generateRequestId.js +4 -4
- package/lib/shared/listenerCollection.js +169 -0
- package/lib/shared/normalizeMetaFromLogs.js +30 -30
- package/lib/shared/reporter.js +128 -123
- package/lib/shared/request.js +64 -64
- package/lib/shared/tempFilesHandler.js +81 -81
- package/lib/shared/templates.js +82 -82
- package/lib/static/helpers.js +33 -33
- package/lib/worker/blobStorage.js +34 -34
- package/lib/worker/defaultProxyExtend.js +46 -46
- package/lib/worker/documentStore.js +49 -49
- package/lib/worker/extensionsManager.js +17 -17
- package/lib/worker/logger.js +48 -48
- package/lib/worker/render/diff.js +138 -138
- package/lib/worker/render/executeEngine.js +227 -190
- package/lib/worker/render/htmlRecipe.js +10 -10
- package/lib/worker/render/moduleHelper.js +45 -43
- package/lib/worker/render/noneEngine.js +12 -12
- package/lib/worker/render/profiler.js +158 -158
- package/lib/worker/render/render.js +213 -209
- package/lib/worker/render/resolveReferences.js +60 -60
- package/lib/worker/reporter.js +192 -187
- package/lib/worker/sandbox/runInSandbox.js +13 -4
- package/lib/worker/sandbox/safeSandbox.js +828 -822
- package/lib/worker/templates.js +78 -78
- package/lib/worker/workerHandler.js +54 -54
- package/package.json +92 -92
- package/test/blobStorage/common.js +21 -21
- package/test/store/common.js +1449 -1449
package/lib/shared/reporter.js
CHANGED
|
@@ -1,123 +1,128 @@
|
|
|
1
|
-
const EventEmitter = require('events')
|
|
2
|
-
const
|
|
3
|
-
const Request = require('./request')
|
|
4
|
-
const Templates = require('./templates')
|
|
5
|
-
const Folders = require('./folders')
|
|
6
|
-
const createOrExtendError = require('./createError')
|
|
7
|
-
const tempFilesHandler = require('./tempFilesHandler')
|
|
8
|
-
const encryption = require('./encryption')
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
this.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
this.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
this.
|
|
31
|
-
this.
|
|
32
|
-
this.
|
|
33
|
-
this.
|
|
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
|
-
this.
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
1
|
+
const EventEmitter = require('events')
|
|
2
|
+
const createListenerCollection = require('./listenerCollection')
|
|
3
|
+
const Request = require('./request')
|
|
4
|
+
const Templates = require('./templates')
|
|
5
|
+
const Folders = require('./folders')
|
|
6
|
+
const createOrExtendError = require('./createError')
|
|
7
|
+
const tempFilesHandler = require('./tempFilesHandler')
|
|
8
|
+
const encryption = require('./encryption')
|
|
9
|
+
const generateRequestId = require('../shared/generateRequestId')
|
|
10
|
+
|
|
11
|
+
class Reporter extends EventEmitter {
|
|
12
|
+
constructor (options) {
|
|
13
|
+
super()
|
|
14
|
+
|
|
15
|
+
this.options = options || {}
|
|
16
|
+
this.Request = Request
|
|
17
|
+
|
|
18
|
+
// since `reporter` instance will be used for other extensions,
|
|
19
|
+
// it will quickly reach the limit of `10` listeners,
|
|
20
|
+
// we increase the limit to Infinity for now,
|
|
21
|
+
// later we should probably design
|
|
22
|
+
// a way to detect possible memory leaks from extensions
|
|
23
|
+
this.setMaxListeners(Infinity)
|
|
24
|
+
|
|
25
|
+
const coreVersion = require('../../package.json').version
|
|
26
|
+
|
|
27
|
+
this.version = coreVersion
|
|
28
|
+
this.coreVersion = coreVersion
|
|
29
|
+
|
|
30
|
+
this.initializeListeners = this.createListenerCollection('initialize')
|
|
31
|
+
this.afterRenderListeners = this.createListenerCollection('afterRender')
|
|
32
|
+
this.renderErrorListeners = this.createListenerCollection('renderError')
|
|
33
|
+
this.beforeRenderListeners = this.createListenerCollection('beforeRender')
|
|
34
|
+
this.closeListeners = this.createListenerCollection('close')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
createListenerCollection (name) {
|
|
38
|
+
return createListenerCollection(name)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Creates a custom error or extends an existing one
|
|
43
|
+
*
|
|
44
|
+
* @public
|
|
45
|
+
*/
|
|
46
|
+
createError (message, options = {}) {
|
|
47
|
+
return createOrExtendError(message, options)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
generateRequestId () {
|
|
51
|
+
return generateRequestId()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Ensures that the jsreport auto-cleanup temp directory (options.tempAutoCleanupDirectory) exists by doing a mkdir call
|
|
56
|
+
*
|
|
57
|
+
* @public
|
|
58
|
+
*/
|
|
59
|
+
async ensureTempDirectoryExists () {
|
|
60
|
+
if (this.options.tempAutoCleanupDirectory == null) {
|
|
61
|
+
throw new Error('Can not use ensureTempDirectoryExists when tempAutoCleanupDirectory option is not initialized, make sure to initialize jsreport first using jsreport.init()')
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return tempFilesHandler.ensureTempDirectoryExists(this.options.tempAutoCleanupDirectory)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Reads a file from the jsreport auto-cleanup temp directory (options.tempAutoCleanupDirectory)
|
|
69
|
+
*
|
|
70
|
+
* @public
|
|
71
|
+
*/
|
|
72
|
+
async readTempFile (filename, opts) {
|
|
73
|
+
if (this.options.tempAutoCleanupDirectory == null) {
|
|
74
|
+
throw new Error('Can not use readTempFile when tempAutoCleanupDirectory option is not initialized, make sure to initialize jsreport first using jsreport.init()')
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return tempFilesHandler.readTempFile(this.options.tempAutoCleanupDirectory, filename, opts)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Creates a file into the jsreport auto-cleanup temp directory (options.tempAutoCleanupDirectory)
|
|
82
|
+
* ensuring that the directory always exists
|
|
83
|
+
*
|
|
84
|
+
* @public
|
|
85
|
+
*/
|
|
86
|
+
async writeTempFile (filenameFn, content, opts) {
|
|
87
|
+
if (this.options.tempAutoCleanupDirectory == null) {
|
|
88
|
+
throw new Error('Can not use writeTempFile when tempAutoCleanupDirectory option is not initialized, make sure to initialize jsreport first using jsreport.init()')
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return tempFilesHandler.writeTempFile(this.options.tempAutoCleanupDirectory, filenameFn, content, opts)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Reads a file as stream from the jsreport auto-cleanup temp directory (options.tempAutoCleanupDirectory)
|
|
96
|
+
*
|
|
97
|
+
* @public
|
|
98
|
+
*/
|
|
99
|
+
async readTempFileStream (filename, opts) {
|
|
100
|
+
if (this.options.tempAutoCleanupDirectory == null) {
|
|
101
|
+
throw new Error('Can not use readTempFileStream when tempAutoCleanupDirectory option is not initialized, make sure to initialize jsreport first using jsreport.init()')
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return tempFilesHandler.readTempFileStream(this.options.tempAutoCleanupDirectory, filename, opts)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Creates a file as stream into the jsreport auto-cleanup temp directory (options.tempAutoCleanupDirectory)
|
|
109
|
+
* ensuring that the directory always exists
|
|
110
|
+
*
|
|
111
|
+
* @public
|
|
112
|
+
*/
|
|
113
|
+
async writeTempFileStream (filenameFn, opts) {
|
|
114
|
+
if (this.options.tempAutoCleanupDirectory == null) {
|
|
115
|
+
throw new Error('Can not use writeTempFileStream when tempAutoCleanupDirectory option is not initialized, make sure to initialize jsreport first using jsreport.init()')
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return tempFilesHandler.writeTempFileStream(this.options.tempAutoCleanupDirectory, filenameFn, opts)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async init () {
|
|
122
|
+
this.templates = Templates(this)
|
|
123
|
+
this.folders = Folders(this)
|
|
124
|
+
this.encryption = encryption(this)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
module.exports = Reporter
|
package/lib/shared/request.js
CHANGED
|
@@ -1,64 +1,64 @@
|
|
|
1
|
-
const extend = require('node.extend.without.arrays')
|
|
2
|
-
const omit = require('lodash.omit')
|
|
3
|
-
|
|
4
|
-
module.exports = (obj, parent) => {
|
|
5
|
-
if (parent && !parent.__isJsreportRequest__) {
|
|
6
|
-
throw new Error('Invalid parent request passed')
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const request = Object.create({}, {
|
|
10
|
-
__isJsreportRequest__: {
|
|
11
|
-
value: true,
|
|
12
|
-
writable: false,
|
|
13
|
-
configurable: false,
|
|
14
|
-
enumerable: false
|
|
15
|
-
}
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
request.template = extend(true, {}, obj.template)
|
|
19
|
-
|
|
20
|
-
if (parent) {
|
|
21
|
-
request.context = Object.assign({}, request.context, omit(parent.context, ['id', 'logs', 'systemHelpers']))
|
|
22
|
-
request.context.isChildRequest = true
|
|
23
|
-
request.options = Object.assign({}, request.options, parent.options)
|
|
24
|
-
|
|
25
|
-
if (parent.data) {
|
|
26
|
-
const dataInput = normalizeJSONData(parent.data)
|
|
27
|
-
request.data = Object.assign(Array.isArray(dataInput) ? [] : {}, dataInput)
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
request.options = extend(true, {}, request.options, obj.options)
|
|
32
|
-
request.context = extend(true, {}, request.context, obj.context)
|
|
33
|
-
request.context.shared = extend(true, {}, request.context.shared)
|
|
34
|
-
|
|
35
|
-
if (obj.data) {
|
|
36
|
-
const dataInput = normalizeJSONData(obj.data)
|
|
37
|
-
request.data = Object.assign(Array.isArray(dataInput) ? [] : {}, request.data, dataInput)
|
|
38
|
-
|
|
39
|
-
// don't override value if it was already set by caller
|
|
40
|
-
if (obj.context == null || obj.context.originalInputDataIsEmpty == null) {
|
|
41
|
-
request.context.originalInputDataIsEmpty = false
|
|
42
|
-
}
|
|
43
|
-
} else if (!parent) {
|
|
44
|
-
// don't override value if it was already set by caller
|
|
45
|
-
if (obj.context == null || obj.context.originalInputDataIsEmpty == null) {
|
|
46
|
-
request.context.originalInputDataIsEmpty = true
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// initialize data if it is empty
|
|
51
|
-
if (!request.data) {
|
|
52
|
-
request.data = {}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return request
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function normalizeJSONData (data) {
|
|
59
|
-
if (typeof data === 'string') {
|
|
60
|
-
return JSON.parse(data)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return data
|
|
64
|
-
}
|
|
1
|
+
const extend = require('node.extend.without.arrays')
|
|
2
|
+
const omit = require('lodash.omit')
|
|
3
|
+
|
|
4
|
+
module.exports = (obj, parent) => {
|
|
5
|
+
if (parent && !parent.__isJsreportRequest__) {
|
|
6
|
+
throw new Error('Invalid parent request passed')
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const request = Object.create({}, {
|
|
10
|
+
__isJsreportRequest__: {
|
|
11
|
+
value: true,
|
|
12
|
+
writable: false,
|
|
13
|
+
configurable: false,
|
|
14
|
+
enumerable: false
|
|
15
|
+
}
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
request.template = extend(true, {}, obj.template)
|
|
19
|
+
|
|
20
|
+
if (parent) {
|
|
21
|
+
request.context = Object.assign({}, request.context, omit(parent.context, ['id', 'logs', 'systemHelpers']))
|
|
22
|
+
request.context.isChildRequest = true
|
|
23
|
+
request.options = Object.assign({}, request.options, parent.options)
|
|
24
|
+
|
|
25
|
+
if (parent.data) {
|
|
26
|
+
const dataInput = normalizeJSONData(parent.data)
|
|
27
|
+
request.data = Object.assign(Array.isArray(dataInput) ? [] : {}, dataInput)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
request.options = extend(true, {}, request.options, obj.options)
|
|
32
|
+
request.context = extend(true, {}, request.context, obj.context)
|
|
33
|
+
request.context.shared = extend(true, {}, request.context.shared)
|
|
34
|
+
|
|
35
|
+
if (obj.data) {
|
|
36
|
+
const dataInput = normalizeJSONData(obj.data)
|
|
37
|
+
request.data = Object.assign(Array.isArray(dataInput) ? [] : {}, request.data, dataInput)
|
|
38
|
+
|
|
39
|
+
// don't override value if it was already set by caller
|
|
40
|
+
if (obj.context == null || obj.context.originalInputDataIsEmpty == null) {
|
|
41
|
+
request.context.originalInputDataIsEmpty = false
|
|
42
|
+
}
|
|
43
|
+
} else if (!parent) {
|
|
44
|
+
// don't override value if it was already set by caller
|
|
45
|
+
if (obj.context == null || obj.context.originalInputDataIsEmpty == null) {
|
|
46
|
+
request.context.originalInputDataIsEmpty = true
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// initialize data if it is empty
|
|
51
|
+
if (!request.data) {
|
|
52
|
+
request.data = {}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return request
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function normalizeJSONData (data) {
|
|
59
|
+
if (typeof data === 'string') {
|
|
60
|
+
return JSON.parse(data)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return data
|
|
64
|
+
}
|
|
@@ -1,81 +1,81 @@
|
|
|
1
|
-
const path = require('path')
|
|
2
|
-
const fs = require('fs')
|
|
3
|
-
const fsAsync = require('fs/promises')
|
|
4
|
-
const { v4: uuidv4 } = require('uuid')
|
|
5
|
-
|
|
6
|
-
module.exports.ensureTempDirectoryExists = async function (tempDirectory) {
|
|
7
|
-
await fsAsync.mkdir(tempDirectory, {
|
|
8
|
-
recursive: true
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
return {
|
|
12
|
-
directoryPath: tempDirectory
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
module.exports.readTempFile = async function readTempFile (tempDirectory, filename, opts = {}) {
|
|
17
|
-
const pathToFile = path.join(tempDirectory, filename)
|
|
18
|
-
|
|
19
|
-
const content = await fsAsync.readFile(pathToFile, opts)
|
|
20
|
-
|
|
21
|
-
return {
|
|
22
|
-
pathToFile,
|
|
23
|
-
filename,
|
|
24
|
-
content
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
module.exports.readTempFileStream = async function readTempFileStream (tempDirectory, filename, opts = {}) {
|
|
29
|
-
const pathToFile = path.join(tempDirectory, filename)
|
|
30
|
-
|
|
31
|
-
return new Promise((resolve) => {
|
|
32
|
-
const stream = fs.createReadStream(pathToFile, opts)
|
|
33
|
-
|
|
34
|
-
resolve({
|
|
35
|
-
pathToFile,
|
|
36
|
-
filename,
|
|
37
|
-
stream
|
|
38
|
-
})
|
|
39
|
-
})
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
module.exports.writeTempFile = async function writeTempFile (tempDirectory, filenameFn, content, opts = {}) {
|
|
43
|
-
return writeFile(tempDirectory, filenameFn, content, opts)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
module.exports.writeTempFileStream = async function writeTempFileStream (tempDirectory, filenameFn, opts = {}) {
|
|
47
|
-
return writeFile(tempDirectory, filenameFn, undefined, opts, true)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
async function writeFile (tempDirectory, filenameFn, content, opts, asStream = false) {
|
|
51
|
-
const filename = filenameFn(uuidv4())
|
|
52
|
-
|
|
53
|
-
if (filename == null || filename === '') {
|
|
54
|
-
throw new Error('No valid filename was returned from filenameFn')
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const pathToFile = path.join(tempDirectory, filename)
|
|
58
|
-
|
|
59
|
-
await fsAsync.mkdir(tempDirectory, {
|
|
60
|
-
recursive: true
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
if (asStream === true) {
|
|
64
|
-
return new Promise((resolve) => {
|
|
65
|
-
const stream = fs.createWriteStream(pathToFile, opts)
|
|
66
|
-
|
|
67
|
-
resolve({
|
|
68
|
-
pathToFile,
|
|
69
|
-
filename,
|
|
70
|
-
stream
|
|
71
|
-
})
|
|
72
|
-
})
|
|
73
|
-
} else {
|
|
74
|
-
await fsAsync.writeFile(pathToFile, content, opts)
|
|
75
|
-
|
|
76
|
-
return {
|
|
77
|
-
pathToFile,
|
|
78
|
-
filename
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
1
|
+
const path = require('path')
|
|
2
|
+
const fs = require('fs')
|
|
3
|
+
const fsAsync = require('fs/promises')
|
|
4
|
+
const { v4: uuidv4 } = require('uuid')
|
|
5
|
+
|
|
6
|
+
module.exports.ensureTempDirectoryExists = async function (tempDirectory) {
|
|
7
|
+
await fsAsync.mkdir(tempDirectory, {
|
|
8
|
+
recursive: true
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
return {
|
|
12
|
+
directoryPath: tempDirectory
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports.readTempFile = async function readTempFile (tempDirectory, filename, opts = {}) {
|
|
17
|
+
const pathToFile = path.join(tempDirectory, filename)
|
|
18
|
+
|
|
19
|
+
const content = await fsAsync.readFile(pathToFile, opts)
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
pathToFile,
|
|
23
|
+
filename,
|
|
24
|
+
content
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
module.exports.readTempFileStream = async function readTempFileStream (tempDirectory, filename, opts = {}) {
|
|
29
|
+
const pathToFile = path.join(tempDirectory, filename)
|
|
30
|
+
|
|
31
|
+
return new Promise((resolve) => {
|
|
32
|
+
const stream = fs.createReadStream(pathToFile, opts)
|
|
33
|
+
|
|
34
|
+
resolve({
|
|
35
|
+
pathToFile,
|
|
36
|
+
filename,
|
|
37
|
+
stream
|
|
38
|
+
})
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
module.exports.writeTempFile = async function writeTempFile (tempDirectory, filenameFn, content, opts = {}) {
|
|
43
|
+
return writeFile(tempDirectory, filenameFn, content, opts)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
module.exports.writeTempFileStream = async function writeTempFileStream (tempDirectory, filenameFn, opts = {}) {
|
|
47
|
+
return writeFile(tempDirectory, filenameFn, undefined, opts, true)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function writeFile (tempDirectory, filenameFn, content, opts, asStream = false) {
|
|
51
|
+
const filename = filenameFn(uuidv4())
|
|
52
|
+
|
|
53
|
+
if (filename == null || filename === '') {
|
|
54
|
+
throw new Error('No valid filename was returned from filenameFn')
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const pathToFile = path.join(tempDirectory, filename)
|
|
58
|
+
|
|
59
|
+
await fsAsync.mkdir(tempDirectory, {
|
|
60
|
+
recursive: true
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
if (asStream === true) {
|
|
64
|
+
return new Promise((resolve) => {
|
|
65
|
+
const stream = fs.createWriteStream(pathToFile, opts)
|
|
66
|
+
|
|
67
|
+
resolve({
|
|
68
|
+
pathToFile,
|
|
69
|
+
filename,
|
|
70
|
+
stream
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
} else {
|
|
74
|
+
await fsAsync.writeFile(pathToFile, content, opts)
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
pathToFile,
|
|
78
|
+
filename
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|