@nxtedition/lib 14.0.5 → 14.0.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "14.0.5",
3
+ "version": "14.0.7",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "files": [
package/serializers.js CHANGED
@@ -173,6 +173,8 @@ function errSerializer(err) {
173
173
  if (key !== 'cause' && !Object.prototype.hasOwnProperty.call(val, seen)) {
174
174
  _err[key] = errSerializer(val)
175
175
  }
176
+ } else if (key === 'data' && typeof val !== 'string') {
177
+ _err[key] = JSON.stringify(val)
176
178
  } else {
177
179
  _err[key] = val
178
180
  }
package/trace.js CHANGED
@@ -44,7 +44,7 @@ module.exports = function ({
44
44
  traceData = ''
45
45
 
46
46
  if (pending > limit / 4) {
47
- logger.warn('throttling')
47
+ logger.warn('throttling tracing')
48
48
  if (pending > limit) {
49
49
  sleep(1000)
50
50
  } else if (pending > limit / 2) {
@@ -4,6 +4,8 @@ const fp = require('lodash/fp')
4
4
  const getNxtpressionsCompiler = require('./nextpressions')
5
5
  const getJavascriptCompiler = require('./javascript')
6
6
  const JSON5 = require('json5')
7
+ const objectHash = require('object-hash')
8
+ const weakCache = require('../../weakCache')
7
9
 
8
10
  module.exports = (options) => {
9
11
  const compilers = {
@@ -11,71 +13,7 @@ module.exports = (options) => {
11
13
  js: getJavascriptCompiler(options),
12
14
  }
13
15
 
14
- const compileArrayTemplate = (arr, args$) => {
15
- if (!fp.isArray(arr)) {
16
- throw new Error('invalid argument')
17
- }
18
-
19
- let resolvers
20
- let indices
21
-
22
- for (let i = 0; i < arr.length; i++) {
23
- const resolver = compileTemplate(arr[i], args$)
24
- if (resolver) {
25
- resolvers ??= []
26
- resolvers.push(resolver)
27
- indices ??= []
28
- indices.push(i)
29
- }
30
- }
31
-
32
- return resolvers
33
- ? rxjs.combineLatest(resolvers).pipe(
34
- rx.map((values) => {
35
- const ret = [...arr]
36
- for (let n = 0; n < values.length; n++) {
37
- ret[indices[n]] = values[n]
38
- }
39
- return ret
40
- })
41
- )
42
- : null
43
- }
44
-
45
- const compileObjectTemplate = (obj, args$) => {
46
- if (!fp.isPlainObject(obj)) {
47
- throw new Error('invalid argument')
48
- }
49
-
50
- let resolvers
51
- let indices
52
-
53
- const keys = Object.keys(obj)
54
-
55
- for (let i = 0; i < keys.length; i++) {
56
- const resolver = compileTemplate(obj[keys[i]], args$)
57
- if (resolver) {
58
- resolvers ??= []
59
- resolvers.push(resolver)
60
- indices ??= []
61
- indices.push(keys[i])
62
- }
63
- }
64
-
65
- return resolvers
66
- ? rxjs.combineLatest(resolvers).pipe(
67
- rx.map((values) => {
68
- const ret = { ...obj }
69
- for (let n = 0; n < values.length; n++) {
70
- ret[indices[n]] = values[n]
71
- }
72
- return ret
73
- })
74
- )
75
- : null
76
- }
77
-
78
- const inner = function inner(str) {
16
+ function inner(str) {
79
17
  const templateStart = str.lastIndexOf('{{')
80
18
  if (templateStart === -1) {
81
19
  return null
@@ -104,34 +42,165 @@ module.exports = (options) => {
104
42
  }
105
43
  }
106
44
 
107
- function compileStringTemplate(str, args$) {
108
- if (!fp.isString(str)) {
109
- throw new Error('invalid argument')
45
+ const hashTemplate = (template, prefix) => {
46
+ if (fp.isPlainObject(template)) {
47
+ let hashes
48
+ for (const key of Object.keys(template)) {
49
+ const hash = hashTemplate(template[key], key)
50
+ if (hash) {
51
+ hashes ??= []
52
+ hashes.push(hash)
53
+ }
54
+ }
55
+ return hashes ? objectHash([prefix, hashes]) : ''
56
+ } else if (fp.isArray(template)) {
57
+ let hashes
58
+ for (let idx = 0; idx < template.length; idx++) {
59
+ const hash = hashTemplate(template[idx], idx)
60
+ if (hash) {
61
+ hashes ??= []
62
+ hashes.push(hash)
63
+ }
64
+ }
65
+ return hashes ? objectHash([prefix, hashes]) : ''
66
+ } else if (isTemplate(template)) {
67
+ return objectHash([prefix, template])
68
+ } else {
69
+ return ''
110
70
  }
71
+ }
111
72
 
112
- const match = inner(str)
113
- if (!match) {
114
- return null
73
+ const _compileArrayTemplate = weakCache(
74
+ (arr, hash) => {
75
+ if (!fp.isArray(arr)) {
76
+ throw new Error('invalid argument')
77
+ }
78
+
79
+ let resolvers
80
+ let indices
81
+
82
+ for (let i = 0; i < arr.length; i++) {
83
+ const resolver = compileTemplate(arr[i])
84
+ if (resolver) {
85
+ resolvers ??= []
86
+ resolvers.push(resolver)
87
+ indices ??= []
88
+ indices.push(i)
89
+ }
90
+ }
91
+
92
+ return resolvers
93
+ ? (args$) =>
94
+ rxjs.combineLatest(resolvers.map((resolver) => resolver(args$))).pipe(
95
+ rx.map((values) => {
96
+ const ret = [...arr]
97
+ for (let n = 0; n < values.length; n++) {
98
+ ret[indices[n]] = values[n]
99
+ }
100
+ return ret
101
+ })
102
+ )
103
+ : null
104
+ },
105
+ (arr, hash) => hash ?? hashTemplate(arr)
106
+ )
107
+
108
+ const compileArrayTemplate = (arr) => {
109
+ if (!fp.isArray(arr)) {
110
+ throw new Error('invalid argument')
115
111
  }
112
+ const hash = hashTemplate(arr)
113
+ return hash ? _compileArrayTemplate(arr, hash) : null
114
+ }
116
115
 
117
- const { pre, type, body, post } = match
116
+ const _compileObjectTemplate = weakCache(
117
+ (obj) => {
118
+ if (!fp.isPlainObject(obj)) {
119
+ throw new Error('invalid argument')
120
+ }
121
+
122
+ let resolvers
123
+ let indices
124
+
125
+ const keys = Object.keys(obj)
126
+
127
+ for (let i = 0; i < keys.length; i++) {
128
+ const resolver = compileTemplate(obj[keys[i]])
129
+ if (resolver) {
130
+ resolvers ??= []
131
+ resolvers.push(resolver)
132
+ indices ??= []
133
+ indices.push(keys[i])
134
+ }
135
+ }
118
136
 
119
- const compileExpression = compilers[type]
120
- if (!compileExpression) {
121
- throw new Error('unknown expression type: ' + type)
137
+ return resolvers
138
+ ? (args$) =>
139
+ rxjs.combineLatest(resolvers.map((resolver) => resolver(args$))).pipe(
140
+ rx.map((values) => {
141
+ const ret = { ...obj }
142
+ for (let n = 0; n < values.length; n++) {
143
+ ret[indices[n]] = values[n]
144
+ }
145
+ return ret
146
+ })
147
+ )
148
+ : null
149
+ },
150
+ (obj, hash) => hash ?? hashTemplate(obj)
151
+ )
152
+
153
+ const compileObjectTemplate = (obj) => {
154
+ if (!fp.isPlainObject(obj)) {
155
+ throw new Error('invalid argument')
122
156
  }
123
157
 
124
- const expr = compileExpression(body)
158
+ const hash = hashTemplate(obj)
159
+ return hash ? _compileObjectTemplate(obj, hash) : null
160
+ }
161
+
162
+ const _compileStringTemplate = weakCache(
163
+ (str, hash) => {
164
+ if (!fp.isString(str)) {
165
+ throw new Error('invalid argument')
166
+ }
167
+
168
+ const match = inner(str)
169
+ if (!match) {
170
+ return null
171
+ }
172
+
173
+ const { pre, type, body, post } = match
174
+
175
+ const compileExpression = compilers[type]
176
+ if (!compileExpression) {
177
+ throw new Error('unknown expression type: ' + type)
178
+ }
179
+
180
+ const expr = compileExpression(body)
125
181
 
126
- if (!pre && !post) {
127
- return expr(args$)
182
+ if (!pre && !post) {
183
+ return (args$) => expr(args$)
184
+ }
185
+
186
+ return (args$) =>
187
+ expr(args$).pipe(
188
+ rx.switchMap((body) => {
189
+ const str = `${pre}${stringify(body, type !== 'js')}${post}`
190
+ return compileStringTemplate(str)?.(args$) ?? rxjs.of(str)
191
+ })
192
+ )
193
+ },
194
+ (str, hash) => hash ?? hashTemplate(str)
195
+ )
196
+
197
+ const compileStringTemplate = (obj) => {
198
+ if (!fp.isString(obj)) {
199
+ throw new Error('invalid argument')
128
200
  }
129
201
 
130
- return expr(args$).pipe(
131
- rx.switchMap((body) =>
132
- compileStringTemplate(`${pre}${stringify(body, type !== 'js')}${post}`, args$)
133
- )
134
- )
202
+ const hash = hashTemplate(obj)
203
+ return hash ? _compileStringTemplate(obj, hash) : null
135
204
  }
136
205
 
137
206
  function stringify(value, escape) {
@@ -149,13 +218,13 @@ module.exports = (options) => {
149
218
  return typeof val === 'string' && val.indexOf('{{') !== -1
150
219
  }
151
220
 
152
- function compileTemplate(template, args$) {
221
+ function compileTemplate(template, args$ = null) {
153
222
  if (fp.isPlainObject(template)) {
154
- return compileObjectTemplate(template, args$)
223
+ return compileObjectTemplate(template)
155
224
  } else if (fp.isArray(template)) {
156
- return compileArrayTemplate(template, args$)
157
- } else if (fp.isString(template)) {
158
- return compileStringTemplate(template, args$)
225
+ return compileArrayTemplate(template)
226
+ } else if (isTemplate(template)) {
227
+ return compileStringTemplate(template)
159
228
  } else {
160
229
  return null
161
230
  }
@@ -166,12 +235,8 @@ module.exports = (options) => {
166
235
  }
167
236
 
168
237
  function onResolveTemplate(template, args$) {
169
- if (fp.isString(template) && template.lastIndexOf('{{') === -1) {
170
- return rxjs.of(template)
171
- }
172
-
173
238
  try {
174
- return compileTemplate(template, args$) ?? rxjs.of(template)
239
+ return compileTemplate(template)?.(args$) ?? rxjs.of(template)
175
240
  } catch (err) {
176
241
  return rxjs.throwError(() => err)
177
242
  }