@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 +1 -1
- package/serializers.js +2 -0
- package/trace.js +1 -1
- package/util/template/index.js +103 -69
package/package.json
CHANGED
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
package/util/template/index.js
CHANGED
|
@@ -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
|
-
|
|
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 =
|
|
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
|
-
?
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
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 =
|
|
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
|
-
?
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
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
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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
|
|
158
|
+
function hashTemplate(template, prefix) {
|
|
154
159
|
if (fp.isPlainObject(template)) {
|
|
155
|
-
|
|
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
|
-
|
|
158
|
-
|
|
159
|
-
|
|
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
|
|
209
|
+
return compileTemplate(template)?.(args$) ?? rxjs.of(template)
|
|
176
210
|
} catch (err) {
|
|
177
211
|
return rxjs.throwError(() => err)
|
|
178
212
|
}
|