|
1 | | -require = require("esm")(module/*, options*/) |
2 | | -module.exports = require("../../runtime/middleware") |
| 1 | +/** |
| 2 | + * CommonJS implementation of the node middleware helpers. |
| 3 | + * |
| 4 | + * This used to proxy the ESM implementation in `runtime/middleware.js` via the |
| 5 | + * unmaintained `esm` package. Keeping a CJS copy here avoids loaders and works |
| 6 | + * on Node 22+, while still supporting older Node versions. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * Node middleware |
| 11 | + * @description Walks through the nodes of a tree |
| 12 | + * @example middleware = createNodeMiddleware(payload => {payload.file.name = 'hello'})(treePayload)) |
| 13 | + * @param {(payload: any) => any} fn |
| 14 | + */ |
| 15 | +function createNodeMiddleware(fn) { |
| 16 | + /** |
| 17 | + * NodeMiddleware payload receiver |
| 18 | + * @param {TreePayload} payload |
| 19 | + */ |
| 20 | + const inner = async function execute(payload) { |
| 21 | + return await nodeMiddleware(fn, { |
| 22 | + file: payload.tree, |
| 23 | + state: { treePayload: payload }, |
| 24 | + scope: {}, |
| 25 | + }) |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * NodeMiddleware sync payload receiver |
| 30 | + * @param {TreePayload} payload |
| 31 | + */ |
| 32 | + inner.sync = function executeSync(payload) { |
| 33 | + return nodeMiddlewareSync(fn, { |
| 34 | + file: payload.tree, |
| 35 | + state: { treePayload: payload }, |
| 36 | + scope: {}, |
| 37 | + }) |
| 38 | + } |
| 39 | + |
| 40 | + return inner |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Node walker |
| 45 | + * @param {(payload: any) => any} fn function to be called for each file |
| 46 | + * @param {any=} payload |
| 47 | + */ |
| 48 | +async function nodeMiddleware(fn, payload) { |
| 49 | + const _file = await fn(payload) |
| 50 | + if (_file === false) return false |
| 51 | + const file = _file || payload.file |
| 52 | + |
| 53 | + if (file.children) { |
| 54 | + const children = await Promise.all( |
| 55 | + file.children.map(async _child => |
| 56 | + nodeMiddleware(fn, { |
| 57 | + state: payload.state, |
| 58 | + scope: clone(payload.scope || {}), |
| 59 | + parent: payload.file, |
| 60 | + file: await _child, |
| 61 | + }) |
| 62 | + ) |
| 63 | + ) |
| 64 | + file.children = children.filter(Boolean) |
| 65 | + } |
| 66 | + |
| 67 | + return file |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Node walker (sync version) |
| 72 | + * @param {(payload: any) => any} fn function to be called for each file |
| 73 | + * @param {any=} payload |
| 74 | + */ |
| 75 | +function nodeMiddlewareSync(fn, payload) { |
| 76 | + const _file = fn(payload) |
| 77 | + if (_file === false) return false |
| 78 | + |
| 79 | + const file = _file || payload.file |
| 80 | + |
| 81 | + if (file.children) { |
| 82 | + const children = file.children.map(_child => |
| 83 | + nodeMiddlewareSync(fn, { |
| 84 | + state: payload.state, |
| 85 | + scope: clone(payload.scope || {}), |
| 86 | + parent: payload.file, |
| 87 | + file: _child, |
| 88 | + }) |
| 89 | + ) |
| 90 | + file.children = children.filter(Boolean) |
| 91 | + } |
| 92 | + |
| 93 | + return file |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Clone with JSON |
| 98 | + * @param {any} obj |
| 99 | + */ |
| 100 | +function clone(obj) { |
| 101 | + return JSON.parse(JSON.stringify(obj)) |
| 102 | +} |
| 103 | + |
| 104 | +module.exports = { |
| 105 | + nodeMiddleware, |
| 106 | + nodeMiddlewareSync, |
| 107 | + createNodeMiddleware, |
| 108 | +} |
0 commit comments