@nxtedition/lib 23.17.19 → 24.0.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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/lib",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "24.0.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Robert Nagy <robert.nagy@boffins.se>",
|
|
6
6
|
"type": "module",
|
|
@@ -70,7 +70,8 @@
|
|
|
70
70
|
"@elastic/transport": "^8.9.3",
|
|
71
71
|
"@nxtedition/nxt-undici": "^6.4.0",
|
|
72
72
|
"@smithy/node-http-handler": "^4.1.1",
|
|
73
|
-
"
|
|
73
|
+
"acorn": "^8.15.0",
|
|
74
|
+
"astring": "^1.9.0",
|
|
74
75
|
"date-fns": "^4.1.0",
|
|
75
76
|
"diff": "5.2.0",
|
|
76
77
|
"fast-querystring": "^1.1.2",
|
|
@@ -4,8 +4,8 @@ import getNxtpressionsCompiler from './nextpressions.js'
|
|
|
4
4
|
import getJavascriptCompiler from './javascript.js'
|
|
5
5
|
import JSON5 from 'json5'
|
|
6
6
|
import objectHash from 'object-hash'
|
|
7
|
-
import { makeWeakCache } from '../../weakCache.js'
|
|
8
7
|
import firstValueFrom from '../../rxjs/firstValueFrom.js'
|
|
8
|
+
import { LRUCache } from 'lru-cache'
|
|
9
9
|
|
|
10
10
|
export function makeTemplateCompiler({ ds, proxify, logger, platform }) {
|
|
11
11
|
const compiler = {
|
|
@@ -210,20 +210,35 @@ export function makeTemplateCompiler({ ds, proxify, logger, platform }) {
|
|
|
210
210
|
return typeof val === 'string' && val.indexOf('{{') !== -1
|
|
211
211
|
}
|
|
212
212
|
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
213
|
+
const objCache = new WeakMap()
|
|
214
|
+
const strCache = new LRUCache({ max: 8192 })
|
|
215
|
+
|
|
216
|
+
const _compileTemplateCache = (template) => {
|
|
217
|
+
if (fp.isPlainObject(template)) {
|
|
218
|
+
let resolver = objCache.get(template)
|
|
219
|
+
if (resolver == null) {
|
|
220
|
+
resolver = compileObjectTemplate(template)
|
|
221
|
+
objCache.set(template, resolver)
|
|
222
|
+
}
|
|
223
|
+
return resolver
|
|
224
|
+
} else if (Array.isArray(template)) {
|
|
225
|
+
let resolver = objCache.get(template)
|
|
226
|
+
if (resolver == null) {
|
|
227
|
+
resolver = compileArrayTemplate(template)
|
|
228
|
+
objCache.set(template, resolver)
|
|
223
229
|
}
|
|
224
|
-
|
|
225
|
-
(template
|
|
226
|
-
|
|
230
|
+
return resolver
|
|
231
|
+
} else if (isTemplate(template)) {
|
|
232
|
+
let resolver = strCache.get(template)
|
|
233
|
+
if (resolver == null) {
|
|
234
|
+
resolver = compileStringTemplate(template)
|
|
235
|
+
strCache.set(template, resolver)
|
|
236
|
+
}
|
|
237
|
+
return resolver
|
|
238
|
+
} else {
|
|
239
|
+
return null
|
|
240
|
+
}
|
|
241
|
+
}
|
|
227
242
|
|
|
228
243
|
function _compileTemplate(template) {
|
|
229
244
|
const hash = hashTemplate(template)
|
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
import _init from '@swc/wasm-web'
|
|
2
1
|
import { makeTemplateCompiler as commonMakeTemplateCompiler } from './index-common.js'
|
|
3
2
|
|
|
4
|
-
export async function init() {
|
|
5
|
-
await _init()
|
|
6
|
-
}
|
|
7
|
-
|
|
8
3
|
export function makeTemplateCompiler({ ds, proxify, logger = console }) {
|
|
9
4
|
return commonMakeTemplateCompiler({
|
|
10
5
|
ds,
|
package/util/template/index.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import { createRequire } from 'node:module'
|
|
2
|
-
import { readFileSync } from 'node:fs'
|
|
3
|
-
import { initSync } from '@swc/wasm-web'
|
|
4
1
|
import { makeTemplateCompiler as commonMakeTemplateCompiler } from './index-common.js'
|
|
5
2
|
import { hashSync } from 'hasha'
|
|
6
3
|
import { request } from '@nxtedition/nxt-undici'
|
|
@@ -8,12 +5,6 @@ import { doYield } from '../../yield.js'
|
|
|
8
5
|
|
|
9
6
|
export * from './index-common.js'
|
|
10
7
|
|
|
11
|
-
const require = createRequire(import.meta.url)
|
|
12
|
-
const wasmPath = require.resolve('@swc/wasm-web').replace('wasm.js', 'wasm_bg.wasm')
|
|
13
|
-
const wasmBuffer = readFileSync(wasmPath)
|
|
14
|
-
|
|
15
|
-
initSync({ module: wasmBuffer })
|
|
16
|
-
|
|
17
8
|
export function makeTemplateCompiler({
|
|
18
9
|
logger = globalThis.__nxt_lib_app?.logger,
|
|
19
10
|
dispatcher,
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { parse } from 'acorn'
|
|
2
|
+
import { generate } from 'astring'
|
|
2
3
|
|
|
3
4
|
export default function (code) {
|
|
4
|
-
const ast =
|
|
5
|
+
const ast = parse(code, { ecmaVersion: 'latest' })
|
|
5
6
|
|
|
6
7
|
const { body } = ast
|
|
7
8
|
|
|
@@ -13,10 +14,9 @@ export default function (code) {
|
|
|
13
14
|
type: 'ReturnStatement',
|
|
14
15
|
argument:
|
|
15
16
|
lastStatement.type === 'ExpressionStatement' ? lastStatement.expression : lastStatement,
|
|
16
|
-
span: lastStatement.span,
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
return
|
|
21
|
+
return generate(ast)
|
|
22
22
|
}
|
|
@@ -1,20 +1,11 @@
|
|
|
1
1
|
import { test } from 'node:test'
|
|
2
2
|
import assert from 'node:assert'
|
|
3
3
|
import transform from './transform.js'
|
|
4
|
-
import { createRequire } from 'node:module'
|
|
5
|
-
import { readFileSync } from 'node:fs'
|
|
6
|
-
import { initSync } from '@swc/wasm-web'
|
|
7
|
-
|
|
8
|
-
const require = createRequire(import.meta.url)
|
|
9
|
-
const wasmPath = require.resolve('@swc/wasm-web').replace('wasm.js', 'wasm_bg.wasm')
|
|
10
|
-
const wasmBuffer = readFileSync(wasmPath)
|
|
11
|
-
|
|
12
|
-
initSync({ module: wasmBuffer })
|
|
13
4
|
|
|
14
5
|
test('transform', function () {
|
|
15
6
|
assert.strictEqual(transform('const x = 2; x;'), 'const x = 2;\nreturn x;\n')
|
|
16
|
-
assert.strictEqual(transform('[1,2,3];'), 'return [
|
|
17
|
-
assert.strictEqual(transform('[1,2,3]'), 'return [
|
|
7
|
+
assert.strictEqual(transform('[1,2,3];'), 'return [1, 2, 3];\n')
|
|
8
|
+
assert.strictEqual(transform('[1,2,3]'), 'return [1, 2, 3];\n')
|
|
18
9
|
assert.strictEqual(transform('x\n? \n2 \n: \n3'), `return x ? 2 : 3;\n`)
|
|
19
10
|
// assert.strictEqual(transform('return x\n? \n2 \n: \n3'), `return x ? 2 : 3;\n`)
|
|
20
11
|
// assert.strictEqual(transform('return\n x\n? \n2 \n: \n3'), `return x ? 2 : 3;\n`)
|