@bleedingdev/modern-js-code-tools 3.9.0-ultramodern.1 → 3.9.0-ultramodern.3

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.
@@ -0,0 +1,335 @@
1
+ import "node:module";
2
+ import node_fs from "node:fs";
3
+ import node_path from "node:path";
4
+ import { parse } from "@babel/parser";
5
+ import traverse, { Hub, NodePath } from "@babel/traverse";
6
+ import * as __rspack_external__babel_types_6e8337dd from "@babel/types";
7
+ const edge = '@modern-js/plugin-bff/effect-edge';
8
+ const sharedRuntime = /^@[^/]+\/shared-contracts\/server\/effect-bff-runtime$/u;
9
+ const failure = 'Generated API entries must export defineEffectBff(...) or the server-only shared Effect BFF assembly helper with an explicitly composed handler Layer and an unshadowed executable root; entries must implement handlers through HttpApiBuilder.group.';
10
+ const invalidSources = new WeakSet();
11
+ function invalidSource(message) {
12
+ const error = new Error(message);
13
+ invalidSources.add(error);
14
+ return error;
15
+ }
16
+ class SourceValidationHub extends Hub {
17
+ buildError(_node, message) {
18
+ return invalidSource(message);
19
+ }
20
+ }
21
+ function createEffectApiImportResolver(filename) {
22
+ const directory = node_fs.realpathSync(node_path.dirname(filename));
23
+ const owner = node_fs.realpathSync(node_path.resolve(directory, '..'));
24
+ const resolve = (from)=>(specifier)=>{
25
+ if (!specifier.startsWith('.')) return;
26
+ const target = node_path.resolve(node_path.dirname(from), specifier);
27
+ if (!target.startsWith(`${owner}${node_path.sep}`)) return;
28
+ for (const candidate of [
29
+ target,
30
+ `${target}.ts`,
31
+ `${target}.mts`
32
+ ])try {
33
+ const real = node_fs.realpathSync(candidate);
34
+ if (!real.startsWith(`${owner}${node_path.sep}`) || node_fs.statSync(real).size > 1000000) return;
35
+ return {
36
+ id: real,
37
+ source: node_fs.readFileSync(real, 'utf8'),
38
+ resolveImport: resolve(real)
39
+ };
40
+ } catch {}
41
+ };
42
+ return resolve(node_path.join(directory, node_path.basename(filename)));
43
+ }
44
+ function strictEffectRuntimeTopologyViolation(source, resolveImport) {
45
+ const modules = new Map();
46
+ const owners = new Map();
47
+ const paths = new Map();
48
+ const mutated = new Set();
49
+ const load = (input)=>{
50
+ const existing = modules.get(input.id);
51
+ if (existing) return existing;
52
+ if (modules.size >= 64 || input.source.length > 1000000) throw invalidSource('API source budget exceeded');
53
+ let file;
54
+ try {
55
+ file = parse(input.source, {
56
+ sourceType: 'module',
57
+ sourceFilename: input.id,
58
+ plugins: [
59
+ "typescript"
60
+ ]
61
+ });
62
+ } catch (cause) {
63
+ if ('object' == typeof cause && null !== cause && 'code' in cause && 'BABEL_PARSER_SYNTAX_ERROR' === cause.code && 'reasonCode' in cause && 'string' == typeof cause.reasonCode) throw invalidSource('Invalid API syntax');
64
+ throw cause;
65
+ }
66
+ const module = {
67
+ ...input,
68
+ file
69
+ };
70
+ modules.set(input.id, module);
71
+ const program = NodePath.get({
72
+ hub: new SourceValidationHub(),
73
+ parentPath: void 0,
74
+ parent: file,
75
+ container: file,
76
+ key: 'program'
77
+ });
78
+ program.setContext();
79
+ traverse(file, {
80
+ enter (nodePath) {
81
+ paths.set(nodePath.node, nodePath);
82
+ owners.set(nodePath.node, module);
83
+ if (nodePath.isAssignmentExpression() || nodePath.isUpdateExpression() || nodePath.isUnaryExpression({
84
+ operator: 'delete'
85
+ })) {
86
+ let target = nodePath.isAssignmentExpression() ? nodePath.node.left : nodePath.node.argument;
87
+ while(__rspack_external__babel_types_6e8337dd.isMemberExpression(target))target = target.object;
88
+ if (__rspack_external__babel_types_6e8337dd.isIdentifier(target)) {
89
+ const binding = nodePath.scope.getBinding(target.name);
90
+ if (binding) mutated.add(binding);
91
+ }
92
+ }
93
+ if (nodePath.isTSModuleDeclaration() && __rspack_external__babel_types_6e8337dd.isIdentifier(nodePath.node.id)) {
94
+ const binding = nodePath.scope.getBinding(nodePath.node.id.name);
95
+ if (binding) mutated.add(binding);
96
+ }
97
+ }
98
+ }, program.scope, void 0, program);
99
+ return module;
100
+ };
101
+ try {
102
+ const entry = load({
103
+ id: '/entry.ts',
104
+ source,
105
+ resolveImport
106
+ });
107
+ const unwrap = (node)=>{
108
+ while(__rspack_external__babel_types_6e8337dd.isParenthesizedExpression(node) || __rspack_external__babel_types_6e8337dd.isTSSatisfiesExpression(node) || __rspack_external__babel_types_6e8337dd.isTSAsExpression(node) || __rspack_external__babel_types_6e8337dd.isTSTypeAssertion(node) || __rspack_external__babel_types_6e8337dd.isTSNonNullExpression(node))node = node.expression;
109
+ return node;
110
+ };
111
+ const binding = (node)=>{
112
+ node = unwrap(node);
113
+ if (!__rspack_external__babel_types_6e8337dd.isIdentifier(node)) return;
114
+ const nodePath = paths.get(node);
115
+ if (!nodePath) throw new Error('Missing API lexical scope');
116
+ const value = nodePath.scope.getBinding(node.name);
117
+ return value?.constant && !mutated.has(value) ? value : void 0;
118
+ };
119
+ const declaration = (node)=>binding(node)?.path.node;
120
+ const imported = (node)=>{
121
+ const value = binding(node);
122
+ const decl = value?.path.node;
123
+ if (!decl || !__rspack_external__babel_types_6e8337dd.isImportSpecifier(decl) || 'type' === decl.importKind) return;
124
+ const statement = value.path.parent;
125
+ if (!__rspack_external__babel_types_6e8337dd.isImportDeclaration(statement) || 'type' === statement.importKind) return;
126
+ return {
127
+ name: __rspack_external__babel_types_6e8337dd.isIdentifier(decl.imported) ? decl.imported.name : decl.imported.value,
128
+ specifier: statement.source.value
129
+ };
130
+ };
131
+ const native = (node, name, sources = [
132
+ edge
133
+ ])=>{
134
+ const value = imported(node);
135
+ return value?.name === name && sources.includes(value.specifier);
136
+ };
137
+ const method = (node, namespace, member, sources = [
138
+ edge
139
+ ])=>{
140
+ node = unwrap(node);
141
+ return __rspack_external__babel_types_6e8337dd.isMemberExpression(node) && !node.computed && __rspack_external__babel_types_6e8337dd.isIdentifier(node.property, {
142
+ name: member
143
+ }) && native(node.object, namespace, sources);
144
+ };
145
+ const initialized = (node)=>{
146
+ const value = binding(node);
147
+ return value?.kind === 'const' && __rspack_external__babel_types_6e8337dd.isVariableDeclarator(value.path.node) ? value.path.node.init ?? void 0 : void 0;
148
+ };
149
+ const moduleFor = (node)=>{
150
+ const value = imported(node);
151
+ if (!value) return;
152
+ const input = owners.get(node)?.resolveImport?.(value.specifier);
153
+ return input ? {
154
+ module: load(input),
155
+ name: value.name
156
+ } : void 0;
157
+ };
158
+ const exported = (module, name)=>{
159
+ for (const statement of module.file.program.body)if (__rspack_external__babel_types_6e8337dd.isExportNamedDeclaration(statement) && 'type' !== statement.exportKind) {
160
+ if (__rspack_external__babel_types_6e8337dd.isVariableDeclaration(statement.declaration)) {
161
+ for (const decl of statement.declaration.declarations)if (__rspack_external__babel_types_6e8337dd.isIdentifier(decl.id, {
162
+ name
163
+ })) return decl.id;
164
+ }
165
+ if (!statement.source) {
166
+ const specifier = statement.specifiers.find((element)=>__rspack_external__babel_types_6e8337dd.isExportSpecifier(element) && 'type' !== element.exportKind && (__rspack_external__babel_types_6e8337dd.isIdentifier(element.exported) ? element.exported.name : element.exported.value) === name);
167
+ if (__rspack_external__babel_types_6e8337dd.isExportSpecifier(specifier)) return specifier.local;
168
+ }
169
+ }
170
+ };
171
+ const externalValue = (node)=>{
172
+ const value = moduleFor(node);
173
+ return value ? exported(value.module, value.name) : void 0;
174
+ };
175
+ const apiIdentity = (node, seen = new Set())=>{
176
+ node = unwrap(node);
177
+ if (seen.has(node)) return;
178
+ seen.add(node);
179
+ const value = imported(node);
180
+ if (value) {
181
+ if (!value.specifier.startsWith('.')) return;
182
+ const module = moduleFor(node);
183
+ const exportedValue = module && exported(module.module, module.name);
184
+ if (exportedValue) return apiIdentity(exportedValue, seen);
185
+ if (module && /(?:^|\/)shared\/(?:api|rpc)\.ts$/u.test(value.specifier)) return `${module.module.id}:import:${value.name}`;
186
+ if (!owners.get(node)?.resolveImport && /(?:^|\/)shared\/(?:api|rpc)\.ts$/u.test(value.specifier)) return `${value.specifier}:${value.name}`;
187
+ return;
188
+ }
189
+ const init = initialized(node);
190
+ if (init && __rspack_external__babel_types_6e8337dd.isIdentifier(unwrap(init))) return apiIdentity(init, seen);
191
+ const valueBinding = binding(node);
192
+ return valueBinding ? `${owners.get(valueBinding.path.node)?.id}:${valueBinding.identifier.start}` : void 0;
193
+ };
194
+ const properties = (node)=>{
195
+ node = unwrap(node);
196
+ if (!__rspack_external__babel_types_6e8337dd.isObjectExpression(node)) return;
197
+ const result = new Map();
198
+ for (const prop of node.properties){
199
+ if (!__rspack_external__babel_types_6e8337dd.isObjectProperty(prop) || prop.computed || !__rspack_external__babel_types_6e8337dd.isIdentifier(prop.key) || result.has(prop.key.name)) return;
200
+ result.set(prop.key.name, prop.value);
201
+ }
202
+ return result;
203
+ };
204
+ const active = new Set();
205
+ const guarded = (node, check)=>{
206
+ node = unwrap(node);
207
+ if (active.has(node) || active.size > 128) return false;
208
+ active.add(node);
209
+ try {
210
+ return check(node);
211
+ } finally{
212
+ active.delete(node);
213
+ }
214
+ };
215
+ const layerSources = [
216
+ edge,
217
+ 'effect'
218
+ ];
219
+ const pipe = (node, check)=>__rspack_external__babel_types_6e8337dd.isMemberExpression(node.callee) && !node.callee.computed && __rspack_external__babel_types_6e8337dd.isIdentifier(node.callee.property, {
220
+ name: 'pipe'
221
+ }) && check(node.callee.object) && node.arguments.every((argument)=>method(argument, 'Layer', 'orDie', layerSources) || __rspack_external__babel_types_6e8337dd.isCallExpression(argument) && method(argument.callee, 'Layer', 'provide', layerSources) && argument.arguments.length > 0);
222
+ const handled = (node)=>{
223
+ node = unwrap(node);
224
+ if (!__rspack_external__babel_types_6e8337dd.isArrowFunctionExpression(node) && !__rspack_external__babel_types_6e8337dd.isFunctionExpression(node)) return false;
225
+ const parameter = node.params[0];
226
+ if (!__rspack_external__babel_types_6e8337dd.isIdentifier(parameter)) return false;
227
+ const body = __rspack_external__babel_types_6e8337dd.isBlockStatement(node.body) ? 1 === node.body.body.length && __rspack_external__babel_types_6e8337dd.isReturnStatement(node.body.body[0]) ? node.body.body[0].argument : void 0 : node.body;
228
+ const chain = (value, count = 0)=>{
229
+ value = unwrap(value);
230
+ if (__rspack_external__babel_types_6e8337dd.isIdentifier(value)) return count > 0 && declaration(value) === parameter;
231
+ return __rspack_external__babel_types_6e8337dd.isCallExpression(value) && __rspack_external__babel_types_6e8337dd.isMemberExpression(value.callee) && !value.callee.computed && __rspack_external__babel_types_6e8337dd.isIdentifier(value.callee.property) && [
232
+ 'handle',
233
+ 'handleRaw'
234
+ ].includes(value.callee.property.name) && 2 === value.arguments.length && chain(value.callee.object, count + 1);
235
+ };
236
+ return !!body && chain(body);
237
+ };
238
+ const handlers = (node, api)=>guarded(node, (node)=>{
239
+ const init = initialized(node) ?? externalValue(node);
240
+ if (init) return handlers(init, api);
241
+ if (!__rspack_external__babel_types_6e8337dd.isCallExpression(node)) return false;
242
+ if (pipe(node, (value)=>handlers(value, api))) return true;
243
+ if (method(node.callee, 'Layer', 'mergeAll', layerSources)) return node.arguments.length > 0 && node.arguments.every((argument)=>handlers(argument, api));
244
+ return method(node.callee, 'HttpApiBuilder', 'group') && 3 === node.arguments.length && apiIdentity(node.arguments[0]) === api && handled(node.arguments[2]);
245
+ });
246
+ const transport = (node)=>guarded(node, (node)=>{
247
+ const init = initialized(node);
248
+ if (init) return transport(init);
249
+ if (!__rspack_external__babel_types_6e8337dd.isCallExpression(node)) return method(node, 'Layer', 'empty', layerSources);
250
+ return pipe(node, transport) || method(node.callee, 'HttpRouter', 'cors') || method(node.callee, 'Layer', 'mergeAll', layerSources) && node.arguments.length > 0 && node.arguments.every(transport);
251
+ });
252
+ const directLayer = (node, api)=>guarded(node, (node)=>{
253
+ const init = initialized(node);
254
+ if (init) return directLayer(init, api);
255
+ if (!__rspack_external__babel_types_6e8337dd.isCallExpression(node) || !__rspack_external__babel_types_6e8337dd.isMemberExpression(node.callee) || node.callee.computed || !__rspack_external__babel_types_6e8337dd.isIdentifier(node.callee.property, {
256
+ name: 'pipe'
257
+ })) return false;
258
+ const base = unwrap(node.callee.object);
259
+ return __rspack_external__babel_types_6e8337dd.isCallExpression(base) && method(base.callee, 'HttpApiBuilder', 'layer') && 1 === base.arguments.length && apiIdentity(base.arguments[0]) === api && node.arguments.length > 0 && node.arguments.every((argument)=>__rspack_external__babel_types_6e8337dd.isCallExpression(argument) && method(argument.callee, 'Layer', 'provide', layerSources) && 1 === argument.arguments.length && handlers(argument.arguments[0], api));
260
+ });
261
+ const rpc = (values)=>{
262
+ const api = values.get('api');
263
+ const apiLayer = values.get('layer');
264
+ const apiInit = api && (initialized(api) ?? unwrap(api));
265
+ if (!apiInit || !__rspack_external__babel_types_6e8337dd.isCallExpression(apiInit) || !method(apiInit.callee, 'HttpApi', 'make') || 1 !== apiInit.arguments.length || !apiLayer || !transport(apiLayer)) return false;
266
+ const config = values.get('rpc');
267
+ const fields = config && properties(config);
268
+ const route = fields?.get('path');
269
+ const serialization = fields?.get('serialization');
270
+ if (!__rspack_external__babel_types_6e8337dd.isStringLiteral(route, {
271
+ value: '/rpc'
272
+ }) || !__rspack_external__babel_types_6e8337dd.isStringLiteral(serialization, {
273
+ value: 'json'
274
+ })) return false;
275
+ const group = fields?.get('group');
276
+ const layer = fields?.get('layer');
277
+ const value = group && imported(group);
278
+ if (!group || !layer || value?.specifier !== '../shared/rpc.ts') return false;
279
+ const groupValue = externalValue(group);
280
+ const groupInit = groupValue && initialized(groupValue);
281
+ if (!groupInit || !__rspack_external__babel_types_6e8337dd.isCallExpression(groupInit) || !method(groupInit.callee, 'RpcGroup', 'make', [
282
+ 'effect/unstable/rpc',
283
+ '@modern-js/plugin-bff/effect-client'
284
+ ])) return false;
285
+ const layerInit = initialized(layer) ?? unwrap(layer);
286
+ if (!__rspack_external__babel_types_6e8337dd.isCallExpression(layerInit) || !__rspack_external__babel_types_6e8337dd.isMemberExpression(layerInit.callee) || layerInit.callee.computed || !__rspack_external__babel_types_6e8337dd.isIdentifier(layerInit.callee.property, {
287
+ name: 'toLayer'
288
+ }) || apiIdentity(layerInit.callee.object) !== apiIdentity(group) || 1 !== layerInit.arguments.length) return false;
289
+ const implementation = unwrap(layerInit.arguments[0]);
290
+ if (!__rspack_external__babel_types_6e8337dd.isCallExpression(implementation) || !__rspack_external__babel_types_6e8337dd.isMemberExpression(implementation.callee) || implementation.callee.computed || !__rspack_external__babel_types_6e8337dd.isIdentifier(implementation.callee.property, {
291
+ name: 'of'
292
+ }) || apiIdentity(implementation.callee.object) !== apiIdentity(group) || 1 !== implementation.arguments.length) return false;
293
+ const entries = properties(implementation.arguments[0]);
294
+ return !!entries?.size && [
295
+ ...entries.values()
296
+ ].every((value)=>__rspack_external__babel_types_6e8337dd.isArrowFunctionExpression(value) || __rspack_external__babel_types_6e8337dd.isFunctionExpression(value));
297
+ };
298
+ const runtime = (node)=>guarded(node, (node)=>{
299
+ const init = initialized(node);
300
+ if (init) return runtime(init);
301
+ if (!__rspack_external__babel_types_6e8337dd.isCallExpression(node)) return false;
302
+ const value = imported(node.callee);
303
+ if (value?.name === 'defineEffectBff' && value.specifier === edge || value?.name === 'assembleEffectBffRuntime' && sharedRuntime.test(value.specifier)) {
304
+ if (1 !== node.arguments.length) return false;
305
+ const values = properties(node.arguments[0]);
306
+ if (!values) return false;
307
+ if ('defineEffectBff' === value.name && values.has('rpc')) return rpc(values);
308
+ const api = values.get('api');
309
+ const apiBinding = api && imported(api);
310
+ if (!api || apiBinding?.specifier !== '../shared/api.ts') return false;
311
+ const identity = apiIdentity(api);
312
+ if (!identity) return false;
313
+ const layer = values.get('defineEffectBff' === value.name ? 'layer' : 'handlers');
314
+ const extra = values.get('transport');
315
+ return void 0 !== layer && ('defineEffectBff' === value.name ? directLayer(layer, identity) : handlers(layer, identity)) && (!extra || transport(extra));
316
+ }
317
+ const decl = declaration(node.callee);
318
+ const factory = decl && __rspack_external__babel_types_6e8337dd.isFunctionDeclaration(decl) ? decl : initialized(node.callee);
319
+ if (!factory || !__rspack_external__babel_types_6e8337dd.isFunctionDeclaration(factory) && !__rspack_external__babel_types_6e8337dd.isArrowFunctionExpression(factory) && !__rspack_external__babel_types_6e8337dd.isFunctionExpression(factory) || factory.params.length || node.arguments.length || !factory.body) return false;
320
+ if (!__rspack_external__babel_types_6e8337dd.isBlockStatement(factory.body)) return runtime(factory.body);
321
+ const statements = factory.body.body;
322
+ const last = statements.at(-1);
323
+ return statements.slice(0, -1).every((statement)=>__rspack_external__babel_types_6e8337dd.isVariableDeclaration(statement)) && !!last && __rspack_external__babel_types_6e8337dd.isReturnStatement(last) && !!last.argument && runtime(last.argument);
324
+ });
325
+ const root = entry.file.program.body.find((statement)=>__rspack_external__babel_types_6e8337dd.isExportDefaultDeclaration(statement));
326
+ return root && runtime(root.declaration) ? void 0 : failure;
327
+ } catch (cause) {
328
+ if ('object' == typeof cause && null !== cause && invalidSources.has(cause)) return failure;
329
+ const message = 'object' == typeof cause && null !== cause && 'message' in cause && 'string' == typeof cause.message ? cause.message : String(cause);
330
+ throw new Error(`Strict Effect API analysis failed: ${message}`, {
331
+ cause
332
+ });
333
+ }
334
+ }
335
+ export { createEffectApiImportResolver, strictEffectRuntimeTopologyViolation };
@@ -0,0 +1,10 @@
1
+ /** A bounded, owner-local source resolver; never executes application modules. */
2
+ export interface EffectApiSource {
3
+ readonly id: string;
4
+ readonly source: string;
5
+ readonly resolveImport?: (specifier: string) => EffectApiSource | undefined;
6
+ }
7
+ export type EffectApiImportResolver = (specifier: string) => EffectApiSource | undefined;
8
+ export declare function createEffectApiImportResolver(filename: string): EffectApiImportResolver;
9
+ /** Proves binding identity and an exported composition, not the occurrence of names. */
10
+ export declare function strictEffectRuntimeTopologyViolation(source: string, resolveImport?: EffectApiImportResolver): string | undefined;
package/package.json CHANGED
@@ -23,7 +23,7 @@
23
23
  "engines": {
24
24
  "node": ">=20"
25
25
  },
26
- "version": "3.9.0-ultramodern.1",
26
+ "version": "3.9.0-ultramodern.3",
27
27
  "types": "./dist/types/index.d.ts",
28
28
  "main": "./dist/esm-node/index.js",
29
29
  "typesVersions": {
@@ -45,6 +45,11 @@
45
45
  },
46
46
  "default": "./dist/esm-node/index.js"
47
47
  },
48
+ "./strict-effect-runtime": {
49
+ "types": "./dist/types/strict-effect-runtime.d.ts",
50
+ "import": "./dist/esm-node/strict-effect-runtime.js",
51
+ "require": "./dist/cjs/strict-effect-runtime.cjs"
52
+ },
48
53
  "./oxlint-plugin": {
49
54
  "types": "./dist/types/oxlint-plugin.d.ts",
50
55
  "node": {
@@ -58,12 +63,15 @@
58
63
  "dist"
59
64
  ],
60
65
  "dependencies": {
61
- "oxlint": "1.80.0"
66
+ "@babel/parser": "^8.0.4",
67
+ "@babel/traverse": "^8.0.4",
68
+ "@babel/types": "^8.0.4",
69
+ "oxlint": "1.81.0"
62
70
  },
63
71
  "devDependencies": {
64
72
  "@rslib/core": "1.0.0",
65
73
  "@scripts/rstest-config": "2.66.0",
66
- "@types/node": "^26.2.0",
74
+ "@types/node": "^26.4.1",
67
75
  "@typescript/native-preview": "7.0.0-dev.20260707.2"
68
76
  },
69
77
  "sideEffects": false,