@jsreport/jsreport-core 3.1.1 → 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.
@@ -6,7 +6,7 @@ exports.getDefaultTempDirectory = () => {
6
6
  }
7
7
 
8
8
  exports.getDefaultRootDirectory = () => {
9
- return path.join(__dirname, '../../../../')
9
+ return path.join(__dirname, '../../../../../')
10
10
  }
11
11
 
12
12
  exports.getDefaultLoadConfig = () => {
@@ -435,10 +435,6 @@ class MainReporter extends Reporter {
435
435
  }
436
436
  }
437
437
 
438
- generateRequestId () {
439
- return generateRequestId()
440
- }
441
-
442
438
  registerWorkersManagerFactory (workersManagerFactory) {
443
439
  this._workersManagerFactory = workersManagerFactory
444
440
  }
@@ -459,12 +455,12 @@ class MainReporter extends Reporter {
459
455
  clearInterval(this._reaperTimerRef)
460
456
  }
461
457
 
458
+ await this.closeListeners.fire()
459
+
462
460
  if (this._workersManager) {
463
461
  await this._workersManager.close()
464
462
  }
465
463
 
466
- await this.closeListeners.fire()
467
-
468
464
  if (this.documentStore) {
469
465
  await this.documentStore.close()
470
466
  }
@@ -6,6 +6,7 @@ const Folders = require('./folders')
6
6
  const createOrExtendError = require('./createError')
7
7
  const tempFilesHandler = require('./tempFilesHandler')
8
8
  const encryption = require('./encryption')
9
+ const generateRequestId = require('../shared/generateRequestId')
9
10
 
10
11
  class Reporter extends EventEmitter {
11
12
  constructor (options) {
@@ -46,6 +47,10 @@ class Reporter extends EventEmitter {
46
47
  return createOrExtendError(message, options)
47
48
  }
48
49
 
50
+ generateRequestId () {
51
+ return generateRequestId()
52
+ }
53
+
49
54
  /**
50
55
  * Ensures that the jsreport auto-cleanup temp directory (options.tempAutoCleanupDirectory) exists by doing a mkdir call
51
56
  *
@@ -10,32 +10,51 @@ const { nanoid } = require('nanoid')
10
10
 
11
11
  module.exports = (reporter) => {
12
12
  const cache = LRU(reporter.options.sandbox.cache || { max: 100 })
13
+
13
14
  reporter.templatingEngines = { cache }
15
+
14
16
  const executionFnParsedParamsMap = new Map()
15
17
 
18
+ const templatingEnginesEvaluate = async (mainCall, { engine, content, helpers, data }, { entity, entitySet }, req) => {
19
+ const engineImpl = reporter.extensionsManager.engines.find((e) => e.name === engine)
20
+
21
+ if (!engine) {
22
+ throw reporter.createError(`Engine '${engine}' not found. If this is a custom engine make sure it's properly installed from npm`, {
23
+ statusCode: 400
24
+ })
25
+ }
26
+
27
+ if (mainCall) {
28
+ executionFnParsedParamsMap.set(req.context.id, new Map())
29
+ }
30
+
31
+ try {
32
+ const res = await executeEngine({
33
+ engine: engineImpl,
34
+ content,
35
+ helpers,
36
+ systemHelpers: req.context.systemHelpers,
37
+ data
38
+ }, { handleErrors: false, entity, entitySet }, req)
39
+
40
+ return res.content
41
+ } finally {
42
+ if (mainCall) {
43
+ executionFnParsedParamsMap.delete(req.context.id)
44
+ }
45
+ }
46
+ }
47
+
48
+ reporter.templatingEngines.evaluate = (executionInfo, entityInfo, req) => templatingEnginesEvaluate(true, executionInfo, entityInfo, req)
49
+
16
50
  reporter.extendProxy((proxy, req, {
17
51
  runInSandbox,
18
52
  context,
19
53
  getTopLevelFunctions
20
54
  }) => {
21
55
  proxy.templatingEngines = {
22
- evaluate: async ({ engine, content, helpers, data }, { entity, entitySet }) => {
23
- const engineImpl = reporter.extensionsManager.engines.find((e) => e.name === engine)
24
-
25
- if (!engine) {
26
- throw reporter.createError(`Engine '${engine}' not found. If this is a custom engine make sure it's properly installed from npm`, {
27
- statusCode: 400
28
- })
29
- }
30
-
31
- const res = await executeEngine({
32
- engine: engineImpl,
33
- content,
34
- helpers,
35
- systemHelpers: req.context.systemHelpers,
36
- data
37
- }, { handleErrors: false, entity, entitySet }, req)
38
- return res.content
56
+ evaluate: async (executionInfo, entityInfo) => {
57
+ return templatingEnginesEvaluate(false, executionInfo, entityInfo, req)
39
58
  }
40
59
  }
41
60
  })
@@ -65,12 +84,13 @@ module.exports = (reporter) => {
65
84
 
66
85
  async function executeEngine ({ engine, content, helpers, systemHelpers, data }, { handleErrors, entity, entitySet }, req) {
67
86
  let entityPath
87
+
68
88
  if (entity._id) {
69
89
  entityPath = await reporter.folders.resolveEntityPath(entity, entitySet, req)
70
90
  entityPath = entityPath.substring(0, entityPath.lastIndexOf('/'))
71
91
  }
72
92
 
73
- const joinedHelpers = systemHelpers + '\n' + helpers
93
+ const joinedHelpers = systemHelpers + '\n' + (helpers || '')
74
94
  const executionFnParsedParamsKey = `entity:${entity.shortid || 'anonymous'}:helpers:${joinedHelpers}`
75
95
 
76
96
  const executionFn = async ({ require, console, topLevelFunctions }) => {
@@ -3,11 +3,13 @@ const path = require('path')
3
3
 
4
4
  module.exports = (reporter) => {
5
5
  let helpersScript
6
- reporter.beforeRenderListeners.add('core-helpers', async (req) => {
7
- if (!helpersScript) {
8
- helpersScript = await fs.readFile(path.join(__dirname, '../../static/helpers.js'), 'utf8')
9
- }
10
- req.context.systemHelpers += helpersScript + '\n'
6
+
7
+ reporter.registerHelpersListeners.add('core-helpers', (req) => {
8
+ return helpersScript
9
+ })
10
+
11
+ reporter.initializeListeners.add('core-helpers', async () => {
12
+ helpersScript = await fs.readFile(path.join(__dirname, '../../static/helpers.js'), 'utf8')
11
13
  })
12
14
 
13
15
  reporter.extendProxy((proxy, req, { safeRequire }) => {
@@ -54,6 +54,14 @@ module.exports = (reporter) => {
54
54
  })
55
55
  }
56
56
 
57
+ let helpersResults = await reporter.registerHelpersListeners.fire(request)
58
+
59
+ helpersResults = helpersResults.filter((result) => {
60
+ return result != null
61
+ })
62
+
63
+ request.context.systemHelpers = helpersResults.join('\n')
64
+
57
65
  const engineProfilerEvent = reporter.profiler.emit({
58
66
  type: 'operationStart',
59
67
  subtype: 'engine',
@@ -113,7 +121,6 @@ module.exports = (reporter) => {
113
121
 
114
122
  return async (req, parentReq) => {
115
123
  const request = Request(req, parentReq)
116
- request.context.systemHelpers = ''
117
124
  const response = { meta: {} }
118
125
  let renderStartProfilerEvent
119
126
  try {
@@ -132,8 +139,11 @@ module.exports = (reporter) => {
132
139
 
133
140
  request.context.reportCounter = ++reportCounter
134
141
  request.context.startTimestamp = new Date().getTime()
142
+ request.context.systemHelpers = ''
135
143
 
136
- reporter.requestModulesCache.set(request.context.id, Object.create(null))
144
+ if (parentReq == null) {
145
+ reporter.requestModulesCache.set(request.context.rootId, Object.create(null))
146
+ }
137
147
 
138
148
  reporter.logger.info(`Starting rendering request ${request.context.reportCounter} (user: ${(request.context.user ? request.context.user.username : 'null')})`, request)
139
149
 
@@ -195,7 +205,9 @@ module.exports = (reporter) => {
195
205
 
196
206
  throw e
197
207
  } finally {
198
- reporter.requestModulesCache.delete(request.context.id)
208
+ if (parentReq == null) {
209
+ reporter.requestModulesCache.delete(request.context.rootId)
210
+ }
199
211
  }
200
212
  }
201
213
  }
@@ -26,6 +26,7 @@ class WorkerReporter extends Reporter {
26
26
  this._workerActions = new Map()
27
27
  this._registerRenderAction()
28
28
 
29
+ this.registerHelpersListeners = this.createListenerCollection('registerHelpers')
29
30
  this.afterTemplatingEnginesExecutedListeners = this.createListenerCollection('afterTemplatingEnginesExecuted')
30
31
  this.validateRenderListeners = this.createListenerCollection('validateRender')
31
32
 
@@ -34,7 +34,7 @@ module.exports = (reporter) => {
34
34
  formatError: (error, moduleName) => {
35
35
  error.message += ` To be able to require custom modules you need to add to configuration { "allowLocalFilesAccess": true } or enable just specific module using { sandbox: { allowedModules": ["${moduleName}"] }`
36
36
  },
37
- modulesCache: reporter.requestModulesCache.get(req.context.id),
37
+ modulesCache: reporter.requestModulesCache.get(req.context.rootId),
38
38
  globalModules: reporter.options.sandbox.nativeModules || [],
39
39
  allowedModules: reporter.options.sandbox.allowedModules,
40
40
  propertiesConfig,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsreport/jsreport-core",
3
- "version": "3.1.1",
3
+ "version": "3.1.2-test.1",
4
4
  "description": "javascript based business reporting",
5
5
  "keywords": [
6
6
  "report",