@nxtedition/lib 14.0.6 → 14.0.8

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.6",
3
+ "version": "14.0.8",
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,7 +13,36 @@ module.exports = (options) => {
11
13
  js: getJavascriptCompiler(options),
12
14
  }
13
15
 
14
- const compileArrayTemplate = (arr, args$) => {
16
+ function inner(str) {
17
+ const templateStart = str.lastIndexOf('{{')
18
+ if (templateStart === -1) {
19
+ return null
20
+ }
21
+ let bodyStart = templateStart + 2
22
+
23
+ let templateEnd = str.indexOf('}}', templateStart + 2)
24
+ if (templateEnd === -1) {
25
+ return null
26
+ }
27
+ const bodyEnd = templateEnd
28
+ templateEnd += 2
29
+
30
+ let type = 'nxt'
31
+ if (str[bodyStart] === '#') {
32
+ type = str.slice(bodyStart + 1).match(/^([a-z]*)/)[1]
33
+ bodyStart += type.length + 1
34
+ }
35
+
36
+ return {
37
+ input: str,
38
+ pre: str.slice(0, templateStart),
39
+ type,
40
+ body: str.slice(bodyStart, bodyEnd),
41
+ post: str.slice(templateEnd),
42
+ }
43
+ }
44
+
45
+ function compileArrayTemplate(arr) {
15
46
  if (!fp.isArray(arr)) {
16
47
  throw new Error('invalid argument')
17
48
  }
@@ -20,7 +51,7 @@ module.exports = (options) => {
20
51
  let indices
21
52
 
22
53
  for (let i = 0; i < arr.length; i++) {
23
- const resolver = compileTemplate(arr[i], args$)
54
+ const resolver = _compileTemplate(arr[i])
24
55
  if (resolver) {
25
56
  resolvers ??= []
26
57
  resolvers.push(resolver)
@@ -30,19 +61,20 @@ module.exports = (options) => {
30
61
  }
31
62
 
32
63
  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
- )
64
+ ? (args$) =>
65
+ rxjs.combineLatest(resolvers.map((resolver) => resolver(args$))).pipe(
66
+ rx.map((values) => {
67
+ const ret = [...arr]
68
+ for (let n = 0; n < values.length; n++) {
69
+ ret[indices[n]] = values[n]
70
+ }
71
+ return ret
72
+ })
73
+ )
42
74
  : null
43
75
  }
44
76
 
45
- const compileObjectTemplate = (obj, args$) => {
77
+ function compileObjectTemplate(obj) {
46
78
  if (!fp.isPlainObject(obj)) {
47
79
  throw new Error('invalid argument')
48
80
  }
@@ -53,7 +85,7 @@ module.exports = (options) => {
53
85
  const keys = Object.keys(obj)
54
86
 
55
87
  for (let i = 0; i < keys.length; i++) {
56
- const resolver = compileTemplate(obj[keys[i]], args$)
88
+ const resolver = _compileTemplate(obj[keys[i]])
57
89
  if (resolver) {
58
90
  resolvers ??= []
59
91
  resolvers.push(resolver)
@@ -63,48 +95,20 @@ module.exports = (options) => {
63
95
  }
64
96
 
65
97
  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
- )
98
+ ? (args$) =>
99
+ rxjs.combineLatest(resolvers.map((resolver) => resolver(args$))).pipe(
100
+ rx.map((values) => {
101
+ const ret = { ...obj }
102
+ for (let n = 0; n < values.length; n++) {
103
+ ret[indices[n]] = values[n]
104
+ }
105
+ return ret
106
+ })
107
+ )
75
108
  : null
76
109
  }
77
110
 
78
- const inner = function inner(str) {
79
- const templateStart = str.lastIndexOf('{{')
80
- if (templateStart === -1) {
81
- return null
82
- }
83
- let bodyStart = templateStart + 2
84
-
85
- let templateEnd = str.indexOf('}}', templateStart + 2)
86
- if (templateEnd === -1) {
87
- return null
88
- }
89
- const bodyEnd = templateEnd
90
- templateEnd += 2
91
-
92
- let type = 'nxt'
93
- if (str[bodyStart] === '#') {
94
- type = str.slice(bodyStart + 1).match(/^([a-z]*)/)[1]
95
- bodyStart += type.length + 1
96
- }
97
-
98
- return {
99
- input: str,
100
- pre: str.slice(0, templateStart),
101
- type,
102
- body: str.slice(bodyStart, bodyEnd),
103
- post: str.slice(templateEnd),
104
- }
105
- }
106
-
107
- function compileStringTemplate(str, args$) {
111
+ function compileStringTemplate(str) {
108
112
  if (!fp.isString(str)) {
109
113
  throw new Error('invalid argument')
110
114
  }
@@ -124,15 +128,16 @@ module.exports = (options) => {
124
128
  const expr = compileExpression(body)
125
129
 
126
130
  if (!pre && !post) {
127
- return expr(args$)
131
+ return (args$) => expr(args$)
128
132
  }
129
133
 
130
- return expr(args$).pipe(
131
- rx.switchMap((body) => {
132
- const str = `${pre}${stringify(body, type !== 'js')}${post}`
133
- return compileStringTemplate(str, args$) ?? rxjs.of(str)
134
- })
135
- )
134
+ return (args$) =>
135
+ expr(args$).pipe(
136
+ rx.switchMap((body) => {
137
+ const str = `${pre}${stringify(body, type !== 'js')}${post}`
138
+ return compileStringTemplate(str)?.(args$) ?? rxjs.of(str)
139
+ })
140
+ )
136
141
  }
137
142
 
138
143
  function stringify(value, escape) {
@@ -150,29 +155,58 @@ module.exports = (options) => {
150
155
  return typeof val === 'string' && val.indexOf('{{') !== -1
151
156
  }
152
157
 
153
- function compileTemplate(template, args$) {
158
+ function hashTemplate(template, prefix) {
154
159
  if (fp.isPlainObject(template)) {
155
- return compileObjectTemplate(template, args$)
160
+ let hashes
161
+ for (const key of Object.keys(template)) {
162
+ const hash = hashTemplate(template[key], key)
163
+ if (hash) {
164
+ hashes ??= []
165
+ hashes.push(hash)
166
+ }
167
+ }
168
+ return hashes ? objectHash([prefix, hashes]) : ''
156
169
  } else if (fp.isArray(template)) {
157
- return compileArrayTemplate(template, args$)
158
- } else if (fp.isString(template)) {
159
- return compileStringTemplate(template, args$)
170
+ let hashes
171
+ for (let idx = 0; idx < template.length; idx++) {
172
+ const hash = hashTemplate(template[idx], idx)
173
+ if (hash) {
174
+ hashes ??= []
175
+ hashes.push(hash)
176
+ }
177
+ }
178
+ return hashes ? objectHash([prefix, hashes]) : ''
179
+ } else if (isTemplate(template)) {
180
+ return objectHash([prefix, template])
181
+ } else {
182
+ return ''
183
+ }
184
+ }
185
+
186
+ function _compileTemplate(template) {
187
+ if (fp.isPlainObject(template)) {
188
+ return compileObjectTemplate(template)
189
+ } else if (fp.isArray(template)) {
190
+ return compileArrayTemplate(template)
191
+ } else if (isTemplate(template)) {
192
+ return compileStringTemplate(template)
160
193
  } else {
161
194
  return null
162
195
  }
163
196
  }
164
197
 
198
+ const compileTemplate = weakCache(
199
+ (template) => _compileTemplate(template),
200
+ (template) => hashTemplate(template)
201
+ )
202
+
165
203
  async function resolveTemplate(template, args$) {
166
204
  return rxjs.firstValueFrom(onResolveTemplate(template, args$))
167
205
  }
168
206
 
169
207
  function onResolveTemplate(template, args$) {
170
- if (fp.isString(template) && template.lastIndexOf('{{') === -1) {
171
- return rxjs.of(template)
172
- }
173
-
174
208
  try {
175
- return compileTemplate(template, args$) ?? rxjs.of(template)
209
+ return compileTemplate(template)?.(args$) ?? rxjs.of(template)
176
210
  } catch (err) {
177
211
  return rxjs.throwError(() => err)
178
212
  }