@mionjs/router 0.8.3-alpha.0 → 0.8.4

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.
Files changed (52) hide show
  1. package/.dist/cjs/index.cjs +2 -0
  2. package/.dist/cjs/index.cjs.map +1 -1
  3. package/.dist/cjs/src/defaultRoutes.cjs.map +1 -1
  4. package/.dist/cjs/src/lib/aotEmitter.cjs +7 -3
  5. package/.dist/cjs/src/lib/aotEmitter.cjs.map +1 -1
  6. package/.dist/cjs/src/lib/aotEmitter.d.ts +6 -0
  7. package/.dist/cjs/src/lib/reflection.cjs +51 -19
  8. package/.dist/cjs/src/lib/reflection.cjs.map +1 -1
  9. package/.dist/cjs/src/lib/reflection.d.ts +3 -1
  10. package/.dist/cjs/src/lib/remoteMethods.cjs +26 -10
  11. package/.dist/cjs/src/lib/remoteMethods.cjs.map +1 -1
  12. package/.dist/cjs/src/lib/test/aotEmitter-test-router.cjs.map +1 -1
  13. package/.dist/cjs/src/router.cjs +61 -8
  14. package/.dist/cjs/src/router.cjs.map +1 -1
  15. package/.dist/cjs/src/router.d.ts +2 -0
  16. package/.dist/cjs/src/routes/client.routes.cjs +12 -12
  17. package/.dist/cjs/src/routes/client.routes.cjs.map +1 -1
  18. package/.dist/cjs/src/routes/client.routes.d.ts +5 -2
  19. package/.dist/cjs/src/routes/mion.routes.d.ts +0 -1
  20. package/.dist/cjs/src/routesFlow.cjs +3 -3
  21. package/.dist/cjs/src/routesFlow.cjs.map +1 -1
  22. package/.dist/cjs/src/types/general.cjs +1 -1
  23. package/.dist/cjs/src/types/general.d.ts +1 -0
  24. package/.dist/cjs/src/types/remoteMethods.cjs +3 -3
  25. package/.dist/cjs/src/types/remoteMethods.cjs.map +1 -1
  26. package/.dist/cjs/src/types/remoteMethods.d.ts +3 -3
  27. package/.dist/esm/index.js +3 -1
  28. package/.dist/esm/src/defaultRoutes.js.map +1 -1
  29. package/.dist/esm/src/lib/aotEmitter.d.ts +6 -0
  30. package/.dist/esm/src/lib/aotEmitter.js +8 -4
  31. package/.dist/esm/src/lib/aotEmitter.js.map +1 -1
  32. package/.dist/esm/src/lib/reflection.d.ts +3 -1
  33. package/.dist/esm/src/lib/reflection.js +53 -21
  34. package/.dist/esm/src/lib/reflection.js.map +1 -1
  35. package/.dist/esm/src/lib/remoteMethods.js +26 -10
  36. package/.dist/esm/src/lib/remoteMethods.js.map +1 -1
  37. package/.dist/esm/src/lib/test/aotEmitter-test-router.js.map +1 -1
  38. package/.dist/esm/src/router.d.ts +2 -0
  39. package/.dist/esm/src/router.js +64 -11
  40. package/.dist/esm/src/router.js.map +1 -1
  41. package/.dist/esm/src/routes/client.routes.d.ts +5 -2
  42. package/.dist/esm/src/routes/client.routes.js +15 -15
  43. package/.dist/esm/src/routes/client.routes.js.map +1 -1
  44. package/.dist/esm/src/routes/mion.routes.d.ts +0 -1
  45. package/.dist/esm/src/routesFlow.js +1 -1
  46. package/.dist/esm/src/routesFlow.js.map +1 -1
  47. package/.dist/esm/src/types/general.d.ts +1 -0
  48. package/.dist/esm/src/types/general.js +1 -1
  49. package/.dist/esm/src/types/remoteMethods.d.ts +3 -3
  50. package/.dist/esm/src/types/remoteMethods.js +3 -3
  51. package/.dist/esm/src/types/remoteMethods.js.map +1 -1
  52. package/package.json +4 -4
@@ -22,10 +22,12 @@ let complexity = 0;
22
22
  let routerOptions = { ...src_constants.DEFAULT_ROUTE_OPTIONS };
23
23
  let isRouterInitialized = false;
24
24
  let allExecutablesIds;
25
+ let platformConfig;
25
26
  const defaultStartMiddleFns = {
26
27
  mionDeserializeRequest: src_routes_serializer_routes.serializerMiddleFns.mionDeserializeRequest
27
28
  };
28
29
  const defaultEndMiddleFns = {
30
+ ...src_routes_client_routes.mionClientMiddleFns,
29
31
  mionSerializeResponse: src_routes_serializer_routes.serializerMiddleFns.mionSerializeResponse
30
32
  };
31
33
  let startMiddleFnsDef = { ...defaultStartMiddleFns };
@@ -41,6 +43,18 @@ const geMiddleFnsSize = () => middleFnsById.size;
41
43
  const getComplexity = () => complexity;
42
44
  const getRouterOptions = () => routerOptions;
43
45
  const getAnyExecutable = (id) => routesById.get(id) || middleFnsById.get(id) || rawMiddleFnsById.get(id);
46
+ function setPlatformConfig(config) {
47
+ platformConfig = config;
48
+ if (core.isMionAOTEmitMode() && typeof process.send === "function") {
49
+ const routerConfig = Object.fromEntries(Object.entries(routerOptions).filter(([, v]) => typeof v !== "function"));
50
+ try {
51
+ process.send({ type: "mion-platform-ready", routerConfig, platformConfig: config });
52
+ } catch (err) {
53
+ console.error("[mion] Failed to send platform-ready IPC:", err);
54
+ }
55
+ }
56
+ }
57
+ const getPlatformConfig = () => platformConfig;
44
58
  const resetRouter = () => {
45
59
  flatRouter.clear();
46
60
  middleFnsById.clear();
@@ -56,6 +70,7 @@ const resetRouter = () => {
56
70
  exports.endMiddleFns = [];
57
71
  isRouterInitialized = false;
58
72
  allExecutablesIds = void 0;
73
+ platformConfig = void 0;
59
74
  src_lib_remoteMethods.resetRemoteMethodsMetadata();
60
75
  src_lib_methodsCache.resetPersistedMethods();
61
76
  core.resetRoutesCache();
@@ -85,7 +100,9 @@ async function registerRoutes(routes) {
85
100
  if (!isRouterInitialized) throw new Error("initRouter should be called first");
86
101
  exports.startMiddleFns = await getExecutablesFromMiddleFnsCollection(startMiddleFnsDef);
87
102
  exports.endMiddleFns = await getExecutablesFromMiddleFnsCollection(endMiddleFnsDef);
88
- await recursiveFlatRoutes(routes);
103
+ const binaryMiddlewares = /* @__PURE__ */ new Set();
104
+ await recursiveFlatRoutes(routes, [], [], [], binaryMiddlewares, 0);
105
+ if (binaryMiddlewares.size > 0) await compileBinaryForMiddleware(binaryMiddlewares);
89
106
  if (shouldFullGenerateSpec()) {
90
107
  return src_lib_remoteMethods.getPublicApi(routes);
91
108
  }
@@ -153,7 +170,7 @@ async function emitAOTCaches() {
153
170
  const aotEmitter = await Promise.resolve().then(() => require("./lib/aotEmitter.cjs"));
154
171
  return aotEmitter.emitAOTCaches();
155
172
  }
156
- async function recursiveFlatRoutes(routes, currentPointer = [], preMiddleFns = [], postMiddleFns = [], nestLevel = 0) {
173
+ async function recursiveFlatRoutes(routes, currentPointer = [], preMiddleFns = [], postMiddleFns = [], binaryMiddlewares = /* @__PURE__ */ new Set(), nestLevel = 0) {
157
174
  if (nestLevel > src_constants.MAX_ROUTE_NESTING)
158
175
  throw new Error("Too many nested routes, you can only nest routes ${MAX_ROUTE_NESTING} levels");
159
176
  const entries = Object.entries(routes);
@@ -192,11 +209,12 @@ async function recursiveFlatRoutes(routes, currentPointer = [], preMiddleFns = [
192
209
  const itemType = typeof item;
193
210
  throw new Error(`Invalid route: ${joinPath(...newPointer)}. Type <${itemType}> is not a valid route.`);
194
211
  }
195
- minus1Props = await recursiveCreateExecutionChainAsync(
212
+ minus1Props = await recursiveCreateExecutionChain(
196
213
  routeEntry,
197
214
  newPointer,
198
215
  preMiddleFns,
199
216
  postMiddleFns,
217
+ binaryMiddlewares,
200
218
  nestLevel,
201
219
  index,
202
220
  entries,
@@ -205,7 +223,7 @@ async function recursiveFlatRoutes(routes, currentPointer = [], preMiddleFns = [
205
223
  complexity++;
206
224
  }
207
225
  }
208
- async function recursiveCreateExecutionChainAsync(routeEntry, currentPointer, preMiddleFns, postMiddleFns, nestLevel, index, routeKeyedEntries, minus1Props) {
226
+ async function recursiveCreateExecutionChain(routeEntry, currentPointer, preMiddleFns, postMiddleFns, binaryMiddlewares, nestLevel, index, routeKeyedEntries, minus1Props) {
209
227
  const minus1 = getEntry(index - 1, routeKeyedEntries);
210
228
  const plus1 = getEntry(index + 1, routeKeyedEntries);
211
229
  const props = getRouteEntryProperties(minus1, routeEntry, plus1);
@@ -243,12 +261,20 @@ async function recursiveCreateExecutionChainAsync(routeEntry, currentPointer, pr
243
261
  const middleFnIds = getPublicMiddleFnIds(methods);
244
262
  if (middleFnIds.length) routeMethod.middleFnIds = middleFnIds;
245
263
  flatRouter.set(path, executionChain);
264
+ if (routeMethod.options.serializer === "binary") {
265
+ for (const method of methods) {
266
+ if (method.type === core.HandlerType.middleFn || method.type === core.HandlerType.headersMiddleFn) {
267
+ binaryMiddlewares.add(method.id);
268
+ }
269
+ }
270
+ }
246
271
  } else if (!isExec) {
247
272
  await recursiveFlatRoutes(
248
273
  routeEntry.routes,
249
274
  routeEntry.pathPointer,
250
275
  [...preMiddleFns, ...props.preLevelMiddleFns],
251
276
  [...props.postLevelMiddleFns, ...postMiddleFns],
277
+ binaryMiddlewares,
252
278
  nestLevel + 1
253
279
  );
254
280
  }
@@ -268,7 +294,14 @@ async function getExecutableFromMiddleFn(middleFn, middleFnPointer, nestLevel) {
268
294
  if (compiledMethod) {
269
295
  executable = compiledMethod;
270
296
  } else {
271
- const reflectionData = await src_lib_reflection.getHandlerReflection(middleFn.handler, middleFnId, routerOptions, isHeader);
297
+ const reflectionData = await src_lib_reflection.getHandlerReflection(
298
+ middleFn.handler,
299
+ middleFnId,
300
+ routerOptions,
301
+ middleFn.options ?? {},
302
+ isHeader,
303
+ middleFn.options?.strictTypes
304
+ );
272
305
  executable = {
273
306
  id: middleFnId,
274
307
  type: isHeader ? core.HandlerType.headersMiddleFn : core.HandlerType.middleFn,
@@ -280,7 +313,8 @@ async function getExecutableFromMiddleFn(middleFn, middleFnPointer, nestLevel) {
280
313
  runOnError: !!middleFn.options?.runOnError,
281
314
  validateParams: middleFn.options?.validateParams ?? true,
282
315
  validateReturn: middleFn.options?.validateReturn ?? false,
283
- description: middleFn.options?.description
316
+ description: middleFn.options?.description,
317
+ strictTypes: middleFn.options?.strictTypes ?? routerOptions.strictTypes
284
318
  }
285
319
  };
286
320
  src_lib_methodsCache.addToPersistedMethods(middleFnId, executable);
@@ -312,6 +346,12 @@ async function getExecutableFromRawMiddleFn(middleFn, middleFnPointer, nestLevel
312
346
  core.routesCache.setMethodJitFns(middleFnId, executable);
313
347
  return executable;
314
348
  }
349
+ async function compileBinaryForMiddleware(binaryMiddlewareIds) {
350
+ for (const id of binaryMiddlewareIds) {
351
+ const method = middleFnsById.get(id);
352
+ if (method) await src_lib_reflection.ensureBinaryJitFns(method);
353
+ }
354
+ }
315
355
  async function getExecutableFromRoute(route, routePointer, nestLevel) {
316
356
  const routeId = core.getRouterItemId(routePointer);
317
357
  const existing = routesById.get(routeId);
@@ -321,7 +361,15 @@ async function getExecutableFromRoute(route, routePointer, nestLevel) {
321
361
  if (compiledMethod) {
322
362
  executable = compiledMethod;
323
363
  } else {
324
- const reflectionData = await src_lib_reflection.getHandlerReflection(route.handler, routeId, routerOptions);
364
+ const resolvedRouteOptions = { ...route.options, serializer: route.options?.serializer ?? routerOptions.serializer };
365
+ const reflectionData = await src_lib_reflection.getHandlerReflection(
366
+ route.handler,
367
+ routeId,
368
+ routerOptions,
369
+ resolvedRouteOptions,
370
+ false,
371
+ route.options?.strictTypes
372
+ );
325
373
  executable = {
326
374
  id: routeId,
327
375
  type: core.HandlerType.route,
@@ -335,7 +383,8 @@ async function getExecutableFromRoute(route, routePointer, nestLevel) {
335
383
  validateReturn: route.options?.validateReturn ?? false,
336
384
  description: route.options?.description,
337
385
  serializer: route.options?.serializer ?? routerOptions.serializer,
338
- isMutation: route.options?.isMutation
386
+ isMutation: route.options?.isMutation,
387
+ strictTypes: route.options?.strictTypes ?? routerOptions.strictTypes
339
388
  }
340
389
  };
341
390
  src_lib_methodsCache.addToPersistedMethods(routeId, executable);
@@ -394,6 +443,8 @@ function getSerializerCodeFromMode(mode) {
394
443
  return core.SerializerModes.binary;
395
444
  case "stringifyJson":
396
445
  return core.SerializerModes.stringifyJson;
446
+ case "optimistic":
447
+ return core.SerializerModes.stringifyJson;
397
448
  case "json":
398
449
  default:
399
450
  return core.SerializerModes.json;
@@ -413,6 +464,7 @@ exports.getExecutableFromMiddleFn = getExecutableFromMiddleFn;
413
464
  exports.getExecutableFromRawMiddleFn = getExecutableFromRawMiddleFn;
414
465
  exports.getExecutableFromRoute = getExecutableFromRoute;
415
466
  exports.getMiddleFnExecutable = getMiddleFnExecutable;
467
+ exports.getPlatformConfig = getPlatformConfig;
416
468
  exports.getRouteEntries = getRouteEntries;
417
469
  exports.getRouteExecutable = getRouteExecutable;
418
470
  exports.getRouteExecutableFromPath = getRouteExecutableFromPath;
@@ -425,5 +477,6 @@ exports.isPrivateDefinition = isPrivateDefinition;
425
477
  exports.isPrivateExecutable = isPrivateExecutable;
426
478
  exports.registerRoutes = registerRoutes;
427
479
  exports.resetRouter = resetRouter;
480
+ exports.setPlatformConfig = setPlatformConfig;
428
481
  exports.shouldFullGenerateSpec = shouldFullGenerateSpec;
429
482
  //# sourceMappingURL=router.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"router.cjs","sources":["../../../src/router.ts"],"sourcesContent":["/* ########\n * 2022 mion\n * Author: Ma-jerez\n * License: MIT\n * The software is provided \"as is\", without warranty of any kind.\n * ######## */\n\n/** Lightweight path join for error messages (avoids Node's 'path' module for edge compatibility) */\nimport type {Route, RouterOptions, Routes, RouterEntry} from './types/general.ts';\nimport type {\n RemoteMethod,\n MethodsExecutionChain,\n RawMethod,\n HeadersMethod,\n MiddleFnMethod,\n RouteMethod,\n} from './types/remoteMethods.ts';\nimport type {PublicApi, PrivateDef, MiddleFnsCollection} from './types/publicMethods.ts';\nimport type {HeadersMiddleFnDef, MiddleFnDef, RawMiddleFnDef} from './types/definitions.ts';\nimport {DEFAULT_ROUTE_OPTIONS, MAX_ROUTE_NESTING, WORKFLOW_KEY} from './constants.ts';\nimport {\n isRawMiddleFnDef,\n isHeadersMiddleFnDef,\n isExecutable,\n isMiddleFnDef,\n isRoute,\n isRoutes,\n isAnyMiddleFnDef,\n isPublicExecutable,\n} from './types/guards.ts';\nimport {\n HandlerType,\n SerializerModes,\n SerializerCode,\n SerializerMode,\n isTestEnv,\n isMionCompileMode,\n isMionAOTEmitMode,\n resetRoutesCache,\n} from '@mionjs/core';\nimport {getRawMethodReflection, getHandlerReflection} from './lib/reflection.ts';\nimport {serializerMiddleFns} from './routes/serializer.routes.ts';\nimport {getRouterItemId, getRoutePath, getENV, MION_ROUTES, routesCache} from '@mionjs/core';\nimport {setErrorOptions} from '@mionjs/core';\nimport {getPublicApi, resetRemoteMethodsMetadata} from './lib/remoteMethods.ts';\nimport {addToPersistedMethods, getPersistedMethod, resetPersistedMethods} from './lib/methodsCache.ts';\nimport {mionClientRoutes} from './routes/client.routes.ts';\nimport {mionErrorsRoutes} from './routes/errors.routes.ts';\nimport {clearRoutesFlowCache} from './routesFlow.ts';\nimport {clearContextPool} from './callContext.ts';\n\ntype RouterKeyEntryList = [string, RouterEntry][];\ntype RoutesWithId = {\n pathPointer: string[];\n routes: Routes;\n};\n\n// ############# PRIVATE STATE #############\n\nconst mionInternalRoutes = Object.values(MION_ROUTES) as string[];\nconst flatRouter: Map<string, MethodsExecutionChain> = new Map(); // Main Router\nconst middleFnsById: Map<string, MiddleFnMethod | HeadersMethod | RawMethod> = new Map();\nconst routesById: Map<string, RouteMethod> = new Map();\nconst rawMiddleFnsById: Map<string, RawMethod> = new Map();\nconst middleFnNames: Set<string> = new Set();\nconst routeNames: Set<string> = new Set();\nlet complexity = 0;\nlet routerOptions: RouterOptions = {...DEFAULT_ROUTE_OPTIONS};\nlet isRouterInitialized = false;\nlet allExecutablesIds: string[] | undefined;\n\n/** Global middleFns to be run before and after any other middleFns or routes set using `registerRoutes` */\nconst defaultStartMiddleFns = {\n mionDeserializeRequest: serializerMiddleFns.mionDeserializeRequest,\n};\nconst defaultEndMiddleFns = {\n mionSerializeResponse: serializerMiddleFns.mionSerializeResponse,\n};\nlet startMiddleFnsDef: MiddleFnsCollection = {...defaultStartMiddleFns};\nlet endMiddleFnsDef: MiddleFnsCollection = {...defaultEndMiddleFns};\nexport let startMiddleFns: RemoteMethod[] = [];\nexport let endMiddleFns: RemoteMethod[] = [];\n\n// ############# PUBLIC METHODS #############\n\nexport const getRouteExecutionChain = (path: string) => flatRouter.get(path);\nexport const getRouteEntries = () => flatRouter.entries();\nexport const geRoutesSize = () => flatRouter.size;\nexport const getRouteExecutable = (id: string) => routesById.get(id);\nexport const getMiddleFnExecutable = (id: string) => middleFnsById.get(id);\nexport const geMiddleFnsSize = () => middleFnsById.size;\nexport const getComplexity = () => complexity;\nexport const getRouterOptions = <Opts extends RouterOptions>(): Readonly<Opts> => routerOptions as Opts;\nexport const getAnyExecutable = (id: string) => routesById.get(id) || middleFnsById.get(id) || rawMiddleFnsById.get(id);\n\nexport const resetRouter = () => {\n flatRouter.clear();\n middleFnsById.clear();\n routesById.clear();\n rawMiddleFnsById.clear();\n middleFnNames.clear();\n routeNames.clear();\n complexity = 0;\n routerOptions = {...DEFAULT_ROUTE_OPTIONS};\n startMiddleFnsDef = {...defaultStartMiddleFns};\n endMiddleFnsDef = {...defaultEndMiddleFns};\n startMiddleFns = [];\n endMiddleFns = [];\n isRouterInitialized = false;\n allExecutablesIds = undefined;\n resetRemoteMethodsMetadata();\n resetPersistedMethods();\n resetRoutesCache();\n clearContextPool();\n clearRoutesFlowCache();\n // Note: We intentionally do NOT call resetJitFnCaches() here because:\n // 1. JIT function caches are global and should persist across router resets\n // 2. The serializableClassRegistry (cleared by resetJitFnCaches) is needed for\n // serialization/deserialization of classes like RpcError\n // resetJitFnCaches() should only be called in specific test scenarios that need\n // to test AOT cache loading behavior\n};\n\n// simpler router initialization\nexport async function initMionRouter<R extends Routes>(routes: R, opts?: Partial<RouterOptions>): Promise<PublicApi<R>> {\n await initRouter(opts);\n const publicApi = await registerRoutes(routes);\n // Emit AOT caches once after ALL routes (error, client, user) are registered\n await emitAOTCaches();\n return publicApi;\n}\n\n/**\n * Initializes the Router.\n * @param application\n * @param contextDataFactory a factory function that returns an object to be shared in the `callContext.shared`\n * @param routerOptions\n * @returns\n */\nexport async function initRouter(opts?: Partial<RouterOptions>): Promise<Readonly<RouterOptions>> {\n if (isRouterInitialized) throw new Error('Router has already been initialized');\n routerOptions = {...routerOptions, ...opts};\n validateSharedDataFactory(routerOptions);\n Object.freeze(routerOptions);\n setErrorOptions(routerOptions);\n if (routerOptions.aot) await loadAOTCaches();\n isRouterInitialized = true;\n await registerRoutes({...mionErrorsRoutes});\n if (!routerOptions.skipClientRoutes) await registerRoutes({...mionClientRoutes});\n if (!isTestEnv()) console.log('mion router initialized', {routerOptions});\n return routerOptions;\n}\n\nexport async function registerRoutes<R extends Routes>(routes: R): Promise<PublicApi<R>> {\n if (!isRouterInitialized) throw new Error('initRouter should be called first');\n startMiddleFns = await getExecutablesFromMiddleFnsCollection(startMiddleFnsDef);\n endMiddleFns = await getExecutablesFromMiddleFnsCollection(endMiddleFnsDef);\n await recursiveFlatRoutes(routes);\n if (shouldFullGenerateSpec()) {\n return getPublicApi(routes);\n }\n return {} as PublicApi<R>;\n}\n\n/** Add middleFns at the start af the ExecutionChain, adds them before any other existing start middleFns by default */\nexport function addStartMiddleFns(middleFnsDef: MiddleFnsCollection, appendBeforeExisting = true) {\n if (isRouterInitialized) throw new Error('Can not add start middleFns after the router has been initialized');\n if (appendBeforeExisting) {\n startMiddleFnsDef = {...middleFnsDef, ...startMiddleFnsDef};\n return;\n }\n startMiddleFnsDef = {...startMiddleFnsDef, ...middleFnsDef};\n}\n\n/** Add middleFns at the end af the ExecutionChain, adds them after any other existing end middleFns by default */\nexport function addEndMiddleFns(middleFnsDef: MiddleFnsCollection, prependAfterExisting = true) {\n if (isRouterInitialized) throw new Error('Can not add end middleFns after the router has been initialized');\n if (prependAfterExisting) {\n endMiddleFnsDef = {...endMiddleFnsDef, ...middleFnsDef};\n return;\n }\n endMiddleFnsDef = {...middleFnsDef, ...endMiddleFnsDef};\n}\n\nexport function isPrivateDefinition(entry: RouterEntry, id: string): entry is PrivateDef {\n if (isRoute(entry)) return false;\n if (isRawMiddleFnDef(entry)) return true;\n try {\n const executable = getMiddleFnExecutable(id) || getRouteExecutable(id);\n if (!executable)\n throw new Error(`Route or MiddleFn ${id} not found. Please check you have called router.registerRoutes first.`);\n return isPrivateExecutable(executable);\n } catch {\n // error thrown because entry is a Routes object and does not have any handler\n return false;\n }\n}\n\nexport function isPrivateExecutable(executable: RemoteMethod): boolean {\n if (executable.type === HandlerType.rawMiddleFn) return true;\n if (executable.type === HandlerType.route) return false;\n const hasPublicParams = !!executable.paramNames?.length;\n const hasHeaderParams = !!(executable as HeadersMethod).headersParam?.headerNames?.length;\n return !hasPublicParams && !hasHeaderParams && !executable.hasReturnData;\n}\n\nexport function getTotalExecutables(): number {\n return routesById.size + middleFnsById.size + rawMiddleFnsById.size;\n}\n\nexport function getAllExecutablesIds(): string[] {\n if (allExecutablesIds) return allExecutablesIds;\n allExecutablesIds = [...routesById.keys(), ...middleFnsById.keys(), ...rawMiddleFnsById.keys()];\n return allExecutablesIds;\n}\n\n// used by codegen\nexport function shouldFullGenerateSpec(): boolean {\n return routerOptions.getPublicRoutesData || getENV('GENERATE_ROUTER_SPEC') === 'true' || isMionCompileMode();\n}\n\nexport function getRouteExecutableFromPath(path: string): RouteMethod {\n const executionChain = flatRouter.get(path);\n if (!executionChain) {\n // Return the not-found route executable\n return getAnyExecutable(MION_ROUTES.notFound) as RouteMethod;\n }\n return executionChain.methods[executionChain.routeIndex] as RouteMethod;\n}\n\n// ############# PRIVATE METHODS #############\n\nasync function loadAOTCaches() {\n const loader = await import('./aot/aotCacheLoader.ts');\n return loader.loadRouterAOTCaches();\n}\n\nasync function emitAOTCaches() {\n if (!isMionAOTEmitMode()) return;\n // Dynamic import resolves relative to this source file.\n // This only runs via vite-node (MION_COMPILE=true), which always resolves from source.\n const aotEmitter = await import('./lib/aotEmitter.ts');\n return aotEmitter.emitAOTCaches();\n}\n\n/**\n * Optimized algorithm to flatten the routes object into a list of Executable objects.\n * @param routes\n * @param currentPointer current pointer in the routes object i.e. ['users', 'get']\n * @param preMiddleFns middleFns one level up preceding current pointer\n * @param postMiddleFns middleFns one level up following the current pointer\n * @param nestLevel\n */\nasync function recursiveFlatRoutes(\n routes: Routes,\n currentPointer: string[] = [],\n preMiddleFns: RemoteMethod[] = [],\n postMiddleFns: RemoteMethod[] = [],\n nestLevel = 0\n) {\n if (nestLevel > MAX_ROUTE_NESTING)\n throw new Error('Too many nested routes, you can only nest routes ${MAX_ROUTE_NESTING} levels');\n\n const entries = Object.entries(routes);\n if (entries.length === 0)\n throw new Error(\n `Invalid route: ${currentPointer.length ? joinPath(...currentPointer) : '*'}. Can Not define empty routes`\n );\n\n let minus1Props: ReturnType<typeof getRouteEntryProperties> | null = null;\n for (let index = 0; index < entries.length; index++) {\n const [key, item] = entries[index];\n // create the executable items\n const newPointer = [...currentPointer, key];\n let routeEntry: RemoteMethod | RoutesWithId;\n if (typeof key !== 'string' || !isNaN(key as any))\n throw new Error(`Invalid route: ${joinPath(...newPointer)}. Numeric route names are not allowed`);\n if (key.includes(',')) throw new Error(`Invalid route: ${joinPath(...newPointer)}. Route names cannot contain commas.`);\n if (key === WORKFLOW_KEY)\n throw new Error(`Invalid route: ${joinPath(...newPointer)}. '${WORKFLOW_KEY}' is a reserved mion route name.`);\n\n // generates a middleFn\n if (isAnyMiddleFnDef(item)) {\n routeEntry = await getExecutableFromAnyMiddleFn(item, newPointer, nestLevel);\n if (middleFnNames.has(routeEntry.id))\n throw new Error(\n `Invalid middleFn: ${joinPath(...newPointer)}. Naming collision, Naming collision, duplicated middleFn.`\n );\n middleFnNames.add(routeEntry.id);\n }\n\n // generates a route\n else if (isRoute(item)) {\n routeEntry = await getExecutableFromRoute(item, newPointer, nestLevel);\n if (routeNames.has(routeEntry.id))\n throw new Error(`Invalid route: ${joinPath(...newPointer)}. Naming collision, duplicated route`);\n routeNames.add(routeEntry.id);\n }\n\n // generates structure required to go one level down\n else if (isRoutes(item)) {\n routeEntry = {\n pathPointer: newPointer,\n routes: item,\n };\n }\n\n // throws an error if the route is invalid\n else {\n const itemType = typeof item;\n throw new Error(`Invalid route: ${joinPath(...newPointer)}. Type <${itemType}> is not a valid route.`);\n }\n\n // recurse into sublevels\n minus1Props = await recursiveCreateExecutionChainAsync(\n routeEntry,\n newPointer,\n preMiddleFns,\n postMiddleFns,\n nestLevel,\n index,\n entries,\n minus1Props\n );\n\n complexity++;\n }\n}\n\nasync function recursiveCreateExecutionChainAsync(\n routeEntry: RemoteMethod | RoutesWithId,\n currentPointer: string[],\n preMiddleFns: RemoteMethod[],\n postMiddleFns: RemoteMethod[],\n nestLevel: number,\n index: number,\n routeKeyedEntries: RouterKeyEntryList,\n minus1Props: ReturnType<typeof getRouteEntryProperties> | null\n) {\n const minus1 = getEntry(index - 1, routeKeyedEntries);\n const plus1 = getEntry(index + 1, routeKeyedEntries);\n const props = getRouteEntryProperties(minus1, routeEntry, plus1);\n\n if (props.isBetweenRoutes && minus1Props) {\n props.preLevelMiddleFns = minus1Props.preLevelMiddleFns;\n props.postLevelMiddleFns = minus1Props.postLevelMiddleFns;\n } else {\n for (let i = 0; i < routeKeyedEntries.length; i++) {\n const [k, entry] = routeKeyedEntries[i];\n complexity++;\n if (!isAnyMiddleFnDef(entry)) continue;\n const newPointer = [...currentPointer.slice(0, -1), k];\n const executable = await getExecutableFromAnyMiddleFn(entry, newPointer, nestLevel);\n if (i < index) props.preLevelMiddleFns.push(executable);\n if (i > index) props.postLevelMiddleFns.push(executable);\n }\n }\n const isExec = isExecutable(routeEntry);\n\n if (isExec && props.isRoute) {\n const path = getRoutePath(routeEntry.pointer, routerOptions);\n const routeMethod = routeEntry as RouteMethod;\n const levelMethods = [\n ...preMiddleFns,\n ...props.preLevelMiddleFns,\n routeEntry,\n ...props.postLevelMiddleFns,\n ...postMiddleFns,\n ];\n const methods = [...startMiddleFns, ...levelMethods, ...endMiddleFns];\n const executionChain: MethodsExecutionChain = {\n routeIndex: startMiddleFns.length + preMiddleFns.length + props.preLevelMiddleFns.length,\n methods,\n serializer: getSerializerCodeFromMode(routeMethod.options.serializer),\n };\n const middleFnIds = getPublicMiddleFnIds(methods);\n // add middleware functions deps, so can be serialized with the router\n if (middleFnIds.length) routeMethod.middleFnIds = middleFnIds;\n flatRouter.set(path, executionChain);\n } else if (!isExec) {\n await recursiveFlatRoutes(\n routeEntry.routes,\n routeEntry.pathPointer,\n [...preMiddleFns, ...props.preLevelMiddleFns],\n [...props.postLevelMiddleFns, ...postMiddleFns],\n nestLevel + 1\n );\n }\n\n return props;\n}\n\nasync function getExecutableFromAnyMiddleFn(\n middleFn: MiddleFnDef | HeadersMiddleFnDef | RawMiddleFnDef,\n middleFnPointer: string[],\n nestLevel: number\n) {\n if (isRawMiddleFnDef(middleFn)) return getExecutableFromRawMiddleFn(middleFn, middleFnPointer, nestLevel);\n return getExecutableFromMiddleFn(middleFn, middleFnPointer, nestLevel);\n}\n\nexport async function getExecutableFromMiddleFn(\n middleFn: MiddleFnDef | HeadersMiddleFnDef,\n middleFnPointer: string[],\n nestLevel: number\n): Promise<MiddleFnMethod | HeadersMethod> {\n const isHeader = isHeadersMiddleFnDef(middleFn);\n // todo fix header id should be same as any other one and then maybe map from id to header name\n const middleFnId = getRouterItemId(middleFnPointer);\n const existing = middleFnsById.get(middleFnId);\n if (existing) return existing as MiddleFnMethod;\n\n type MixedMiddleFn = (Omit<MiddleFnMethod, 'type'> | Omit<HeadersMethod, 'type'>) & {\n type: typeof HandlerType.middleFn | typeof HandlerType.headersMiddleFn;\n };\n\n const compiledMethod = getPersistedMethod(middleFnId, middleFn.handler);\n let executable: MixedMiddleFn;\n if (compiledMethod) {\n executable = compiledMethod as MixedMiddleFn;\n } else {\n const reflectionData = await getHandlerReflection(middleFn.handler, middleFnId, routerOptions, isHeader);\n executable = {\n id: middleFnId,\n type: isHeader ? HandlerType.headersMiddleFn : HandlerType.middleFn,\n nestLevel,\n handler: middleFn.handler,\n pointer: middleFnPointer,\n ...reflectionData,\n options: {\n runOnError: !!middleFn.options?.runOnError,\n validateParams: middleFn.options?.validateParams ?? true,\n validateReturn: middleFn.options?.validateReturn ?? false,\n description: middleFn.options?.description,\n },\n };\n addToPersistedMethods(middleFnId, executable);\n }\n\n middleFnsById.set(middleFnId, executable as any);\n routesCache.setMethodJitFns(middleFnId, executable as any);\n return executable as any;\n}\n\nexport async function getExecutableFromRawMiddleFn(\n middleFn: RawMiddleFnDef,\n middleFnPointer: string[],\n nestLevel: number\n): Promise<RawMethod> {\n const middleFnId = getRouterItemId(middleFnPointer);\n const existing = rawMiddleFnsById.get(middleFnId);\n if (existing) return existing as RawMethod;\n const reflectionData = await getRawMethodReflection(middleFn.handler, middleFnId, routerOptions);\n const executable: RawMethod = {\n id: middleFnId,\n type: HandlerType.rawMiddleFn,\n nestLevel,\n handler: middleFn.handler,\n pointer: middleFnPointer,\n ...reflectionData,\n options: {\n runOnError: !!middleFn.options?.runOnError,\n validateParams: false,\n validateReturn: false,\n description: middleFn.options?.description,\n },\n };\n rawMiddleFnsById.set(middleFnId, executable);\n routesCache.setMethodJitFns(middleFnId, executable as any);\n return executable;\n}\n\nexport async function getExecutableFromRoute(route: Route, routePointer: string[], nestLevel: number): Promise<RouteMethod> {\n const routeId = getRouterItemId(routePointer);\n const existing = routesById.get(routeId);\n if (existing) return existing as RouteMethod;\n\n const compiledMethod = getPersistedMethod(routeId, route.handler);\n let executable: RouteMethod;\n if (compiledMethod) {\n executable = compiledMethod as RouteMethod;\n } else {\n const reflectionData = await getHandlerReflection(route.handler, routeId, routerOptions);\n executable = {\n id: routeId,\n type: HandlerType.route,\n nestLevel,\n handler: route.handler,\n pointer: routePointer,\n ...reflectionData,\n options: {\n runOnError: false,\n validateParams: route.options?.validateParams ?? true,\n validateReturn: route.options?.validateReturn ?? false,\n description: route.options?.description,\n serializer: route.options?.serializer ?? routerOptions.serializer,\n isMutation: route.options?.isMutation,\n },\n };\n addToPersistedMethods(routeId, executable);\n }\n routesById.set(routeId, executable);\n routesCache.setMethodJitFns(routeId, executable as any);\n return executable;\n}\n\n/** Returns IDs of public middleware methods from the execution chain, excluding internal mion routes. */\nfunction getPublicMiddleFnIds(methods: RemoteMethod[]): string[] {\n const ids = methods\n .filter((exec) => isPublicExecutable(exec))\n .map((exec) => getRouterItemId(exec.pointer))\n .filter((mfId) => {\n if (mionInternalRoutes.includes(mfId)) return false;\n const exec = getMiddleFnExecutable(mfId);\n return exec && isPublicExecutable(exec);\n });\n return ids;\n}\n\nfunction getEntry(index: number, keyEntryList: RouterKeyEntryList) {\n return keyEntryList[index]?.[1];\n}\n\nfunction getRouteEntryProperties(\n minus1: RouterEntry | undefined,\n zero: RemoteMethod | RoutesWithId,\n plus1: RouterEntry | undefined\n) {\n const minus1IsRoute = minus1 && isRoute(minus1);\n const zeroIsRoute = (zero as RemoteMethod).type === HandlerType.route;\n const plus1IsRoute = plus1 && isRoute(plus1);\n\n const isExec = !!(zero as RemoteMethod).handler;\n\n return {\n isBetweenRoutes: minus1IsRoute && zeroIsRoute && plus1IsRoute,\n isExecutable: isExec,\n isRoute: zeroIsRoute,\n preLevelMiddleFns: [] as RemoteMethod[],\n postLevelMiddleFns: [] as RemoteMethod[],\n };\n}\n\nasync function getExecutablesFromMiddleFnsCollection(\n middleFnsDef: MiddleFnsCollection\n): Promise<(RawMethod | MiddleFnMethod | HeadersMethod)[]> {\n const results: (RawMethod | MiddleFnMethod | HeadersMethod)[] = [];\n for (const [key, middleFn] of Object.entries(middleFnsDef)) {\n if (isRawMiddleFnDef(middleFn)) {\n results.push(await getExecutableFromRawMiddleFn(middleFn, [key], 0));\n } else if (isHeadersMiddleFnDef(middleFn) || isMiddleFnDef(middleFn)) {\n results.push(await getExecutableFromMiddleFn(middleFn, [key], 0));\n } else {\n throw new Error(`Invalid middleFn: ${key}. Invalid middleFn definition`);\n }\n }\n return results;\n}\n\n/**\n * Validates that a contextDataFactory returns a valid context data object.\n * @param contextDataFactory The factory function to validate\n * @throws Error if the factory doesn't return a plain object with at least one property\n */\nfunction validateSharedDataFactory(opts?: Partial<RouterOptions>): void {\n if (!opts?.contextDataFactory) return;\n const testSharedData = opts.contextDataFactory();\n if (\n typeof testSharedData !== 'object' ||\n Array.isArray(testSharedData) ||\n testSharedData === null ||\n Object.keys(testSharedData).length === 0\n ) {\n throw new Error('contextDataFactory must return a plain object with at least one property');\n }\n}\n\n/** Maps serializer mode string to response body type code */\nfunction getSerializerCodeFromMode(mode: SerializerMode | undefined): SerializerCode {\n switch (mode) {\n case 'binary':\n return SerializerModes.binary;\n case 'stringifyJson':\n return SerializerModes.stringifyJson;\n case 'json':\n default:\n return SerializerModes.json;\n }\n}\n\n/** Path replacement as is not available in edge runtime */\nfunction joinPath(...parts: string[]): string {\n return parts.filter(Boolean).join('/');\n}\n"],"names":["MION_ROUTES","DEFAULT_ROUTE_OPTIONS","serializerMiddleFns","startMiddleFns","endMiddleFns","resetRemoteMethodsMetadata","resetPersistedMethods","resetRoutesCache","clearContextPool","clearRoutesFlowCache","setErrorOptions","mionErrorsRoutes","mionClientRoutes","isTestEnv","getPublicApi","isRoute","isRawMiddleFnDef","HandlerType","getENV","isMionCompileMode","isMionAOTEmitMode","MAX_ROUTE_NESTING","WORKFLOW_KEY","isAnyMiddleFnDef","isRoutes","isExecutable","getRoutePath","isHeadersMiddleFnDef","getRouterItemId","getPersistedMethod","getHandlerReflection","addToPersistedMethods","routesCache","getRawMethodReflection","isPublicExecutable","isMiddleFnDef","SerializerModes"],"mappings":";;;;;;;;;;;;;AA2DA,MAAM,qBAAqB,OAAO,OAAOA,gBAAW;AACpD,MAAM,iCAAqD,IAAA;AAC3D,MAAM,oCAA6E,IAAA;AACnF,MAAM,iCAA2C,IAAA;AACjD,MAAM,uCAA+C,IAAA;AACrD,MAAM,oCAAiC,IAAA;AACvC,MAAM,iCAA8B,IAAA;AACpC,IAAI,aAAa;AACjB,IAAI,gBAA+B,EAAC,GAAGC,oCAAA;AACvC,IAAI,sBAAsB;AAC1B,IAAI;AAGJ,MAAM,wBAAwB;AAAA,EAC1B,wBAAwBC,6BAAAA,oBAAoB;AAChD;AACA,MAAM,sBAAsB;AAAA,EACxB,uBAAuBA,6BAAAA,oBAAoB;AAC/C;AACA,IAAI,oBAAyC,EAAC,GAAG,sBAAA;AACjD,IAAI,kBAAuC,EAAC,GAAG,oBAAA;AACpCC,QAAAA,iBAAiC,CAAA;AACjCC,QAAAA,eAA+B,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MIT\n * The software is provided \"as is\", without warranty of any kind.\n * ######## */\n\n/** Lightweight path join for error messages (avoids Node's 'path' module for edge compatibility) */\nimport type {Route, RouterOptions, Routes, RouterEntry} from './types/general.ts';\nimport type {\n RemoteMethod,\n MethodsExecutionChain,\n RawMethod,\n HeadersMethod,\n MiddleFnMethod,\n RouteMethod,\n} from './types/remoteMethods.ts';\nimport type {PublicApi, PrivateDef, MiddleFnsCollection} from './types/publicMethods.ts';\nimport type {HeadersMiddleFnDef, MiddleFnDef, RawMiddleFnDef} from './types/definitions.ts';\nimport {DEFAULT_ROUTE_OPTIONS, MAX_ROUTE_NESTING, WORKFLOW_KEY} from './constants.ts';\nimport {\n isRawMiddleFnDef,\n isHeadersMiddleFnDef,\n isExecutable,\n isMiddleFnDef,\n isRoute,\n isRoutes,\n isAnyMiddleFnDef,\n isPublicExecutable,\n} from './types/guards.ts';\nimport {\n HandlerType,\n SerializerModes,\n SerializerCode,\n SerializerMode,\n isTestEnv,\n isMionCompileMode,\n isMionAOTEmitMode,\n resetRoutesCache,\n} from '@mionjs/core';\nimport {getRawMethodReflection, getHandlerReflection, ensureBinaryJitFns} from './lib/reflection.ts';\nimport {serializerMiddleFns} from './routes/serializer.routes.ts';\nimport {getRouterItemId, getRoutePath, getENV, MION_ROUTES, routesCache} from '@mionjs/core';\nimport {setErrorOptions} from '@mionjs/core';\nimport {getPublicApi, resetRemoteMethodsMetadata} from './lib/remoteMethods.ts';\nimport {addToPersistedMethods, getPersistedMethod, resetPersistedMethods} from './lib/methodsCache.ts';\nimport {mionClientRoutes, mionClientMiddleFns} from './routes/client.routes.ts';\nimport {mionErrorsRoutes} from './routes/errors.routes.ts';\nimport {clearRoutesFlowCache} from './routesFlow.ts';\nimport {clearContextPool} from './callContext.ts';\n\ntype RouterKeyEntryList = [string, RouterEntry][];\ntype RoutesWithId = {\n pathPointer: string[];\n routes: Routes;\n};\n\n// ############# PRIVATE STATE #############\n\nconst mionInternalRoutes = Object.values(MION_ROUTES) as string[];\nconst flatRouter: Map<string, MethodsExecutionChain> = new Map(); // Main Router\nconst middleFnsById: Map<string, MiddleFnMethod | HeadersMethod | RawMethod> = new Map();\nconst routesById: Map<string, RouteMethod> = new Map();\nconst rawMiddleFnsById: Map<string, RawMethod> = new Map();\nconst middleFnNames: Set<string> = new Set();\nconst routeNames: Set<string> = new Set();\nlet complexity = 0;\nlet routerOptions: RouterOptions = {...DEFAULT_ROUTE_OPTIONS};\nlet isRouterInitialized = false;\nlet allExecutablesIds: string[] | undefined;\nlet platformConfig: Record<string, unknown> | undefined;\n\n/** Global middleFns to be run before and after any other middleFns or routes set using `registerRoutes` */\nconst defaultStartMiddleFns = {\n mionDeserializeRequest: serializerMiddleFns.mionDeserializeRequest,\n};\nconst defaultEndMiddleFns = {\n ...mionClientMiddleFns,\n mionSerializeResponse: serializerMiddleFns.mionSerializeResponse,\n};\nlet startMiddleFnsDef: MiddleFnsCollection = {...defaultStartMiddleFns};\nlet endMiddleFnsDef: MiddleFnsCollection = {...defaultEndMiddleFns};\nexport let startMiddleFns: RemoteMethod[] = [];\nexport let endMiddleFns: RemoteMethod[] = [];\n\n// ############# PUBLIC METHODS #############\n\nexport const getRouteExecutionChain = (path: string) => flatRouter.get(path);\nexport const getRouteEntries = () => flatRouter.entries();\nexport const geRoutesSize = () => flatRouter.size;\nexport const getRouteExecutable = (id: string) => routesById.get(id);\nexport const getMiddleFnExecutable = (id: string) => middleFnsById.get(id);\nexport const geMiddleFnsSize = () => middleFnsById.size;\nexport const getComplexity = () => complexity;\nexport const getRouterOptions = <Opts extends RouterOptions>(): Readonly<Opts> => routerOptions as Opts;\nexport const getAnyExecutable = (id: string) => routesById.get(id) || middleFnsById.get(id) || rawMiddleFnsById.get(id);\n\n/** Sets platform adapter config and notifies the parent process (Vite plugin) that the server is ready.\n * Called automatically by platform adapters. Sends an IPC message containing both the\n * serializable router config and the platform adapter config. */\nexport function setPlatformConfig(config: Record<string, unknown>): void {\n platformConfig = config;\n if (isMionAOTEmitMode() && typeof process.send === 'function') {\n const routerConfig = Object.fromEntries(Object.entries(routerOptions).filter(([, v]) => typeof v !== 'function'));\n try {\n process.send({type: 'mion-platform-ready', routerConfig, platformConfig: config});\n } catch (err) {\n console.error('[mion] Failed to send platform-ready IPC:', err);\n }\n }\n}\n\n/** Returns the platform adapter config set by setPlatformConfig(). */\nexport const getPlatformConfig = (): Readonly<Record<string, unknown>> | undefined => platformConfig;\n\nexport const resetRouter = () => {\n flatRouter.clear();\n middleFnsById.clear();\n routesById.clear();\n rawMiddleFnsById.clear();\n middleFnNames.clear();\n routeNames.clear();\n complexity = 0;\n routerOptions = {...DEFAULT_ROUTE_OPTIONS};\n startMiddleFnsDef = {...defaultStartMiddleFns};\n endMiddleFnsDef = {...defaultEndMiddleFns};\n startMiddleFns = [];\n endMiddleFns = [];\n isRouterInitialized = false;\n allExecutablesIds = undefined;\n platformConfig = undefined;\n resetRemoteMethodsMetadata();\n resetPersistedMethods();\n resetRoutesCache();\n clearContextPool();\n clearRoutesFlowCache();\n // Note: We intentionally do NOT call resetJitFnCaches() here because:\n // 1. JIT function caches are global and should persist across router resets\n // 2. The serializableClassRegistry (cleared by resetJitFnCaches) is needed for\n // serialization/deserialization of classes like RpcError\n // resetJitFnCaches() should only be called in specific test scenarios that need\n // to test AOT cache loading behavior\n};\n\n// simpler router initialization\nexport async function initMionRouter<R extends Routes>(routes: R, opts?: Partial<RouterOptions>): Promise<PublicApi<R>> {\n await initRouter(opts);\n const publicApi = await registerRoutes(routes);\n // Emit AOT caches once after ALL routes (error, client, user) are registered\n await emitAOTCaches();\n return publicApi;\n}\n\n/**\n * Initializes the Router.\n * @param application\n * @param contextDataFactory a factory function that returns an object to be shared in the `callContext.shared`\n * @param routerOptions\n * @returns\n */\nexport async function initRouter(opts?: Partial<RouterOptions>): Promise<Readonly<RouterOptions>> {\n if (isRouterInitialized) throw new Error('Router has already been initialized');\n routerOptions = {...routerOptions, ...opts};\n validateSharedDataFactory(routerOptions);\n Object.freeze(routerOptions);\n setErrorOptions(routerOptions);\n if (routerOptions.aot) await loadAOTCaches();\n isRouterInitialized = true;\n await registerRoutes({...mionErrorsRoutes});\n if (!routerOptions.skipClientRoutes) await registerRoutes({...mionClientRoutes});\n if (!isTestEnv()) console.log('mion router initialized', {routerOptions});\n return routerOptions;\n}\n\nexport async function registerRoutes<R extends Routes>(routes: R): Promise<PublicApi<R>> {\n if (!isRouterInitialized) throw new Error('initRouter should be called first');\n startMiddleFns = await getExecutablesFromMiddleFnsCollection(startMiddleFnsDef);\n endMiddleFns = await getExecutablesFromMiddleFnsCollection(endMiddleFnsDef);\n const binaryMiddlewares = new Set<string>();\n await recursiveFlatRoutes(routes, [], [], [], binaryMiddlewares, 0);\n if (binaryMiddlewares.size > 0) await compileBinaryForMiddleware(binaryMiddlewares);\n if (shouldFullGenerateSpec()) {\n return getPublicApi(routes);\n }\n return {} as PublicApi<R>;\n}\n\n/** Add middleFns at the start af the ExecutionChain, adds them before any other existing start middleFns by default */\nexport function addStartMiddleFns(middleFnsDef: MiddleFnsCollection, appendBeforeExisting = true) {\n if (isRouterInitialized) throw new Error('Can not add start middleFns after the router has been initialized');\n if (appendBeforeExisting) {\n startMiddleFnsDef = {...middleFnsDef, ...startMiddleFnsDef};\n return;\n }\n startMiddleFnsDef = {...startMiddleFnsDef, ...middleFnsDef};\n}\n\n/** Add middleFns at the end af the ExecutionChain, adds them after any other existing end middleFns by default */\nexport function addEndMiddleFns(middleFnsDef: MiddleFnsCollection, prependAfterExisting = true) {\n if (isRouterInitialized) throw new Error('Can not add end middleFns after the router has been initialized');\n if (prependAfterExisting) {\n endMiddleFnsDef = {...endMiddleFnsDef, ...middleFnsDef};\n return;\n }\n endMiddleFnsDef = {...middleFnsDef, ...endMiddleFnsDef};\n}\n\nexport function isPrivateDefinition(entry: RouterEntry, id: string): entry is PrivateDef {\n if (isRoute(entry)) return false;\n if (isRawMiddleFnDef(entry)) return true;\n try {\n const executable = getMiddleFnExecutable(id) || getRouteExecutable(id);\n if (!executable)\n throw new Error(`Route or MiddleFn ${id} not found. Please check you have called router.registerRoutes first.`);\n return isPrivateExecutable(executable);\n } catch {\n // error thrown because entry is a Routes object and does not have any handler\n return false;\n }\n}\n\nexport function isPrivateExecutable(executable: RemoteMethod): boolean {\n if (executable.type === HandlerType.rawMiddleFn) return true;\n if (executable.type === HandlerType.route) return false;\n const hasPublicParams = !!executable.paramNames?.length;\n const hasHeaderParams = !!(executable as HeadersMethod).headersParam?.headerNames?.length;\n return !hasPublicParams && !hasHeaderParams && !executable.hasReturnData;\n}\n\nexport function getTotalExecutables(): number {\n return routesById.size + middleFnsById.size + rawMiddleFnsById.size;\n}\n\nexport function getAllExecutablesIds(): string[] {\n if (allExecutablesIds) return allExecutablesIds;\n allExecutablesIds = [...routesById.keys(), ...middleFnsById.keys(), ...rawMiddleFnsById.keys()];\n return allExecutablesIds;\n}\n\n// used by codegen\nexport function shouldFullGenerateSpec(): boolean {\n return routerOptions.getPublicRoutesData || getENV('GENERATE_ROUTER_SPEC') === 'true' || isMionCompileMode();\n}\n\nexport function getRouteExecutableFromPath(path: string): RouteMethod {\n const executionChain = flatRouter.get(path);\n if (!executionChain) {\n // Return the not-found route executable\n return getAnyExecutable(MION_ROUTES.notFound) as RouteMethod;\n }\n return executionChain.methods[executionChain.routeIndex] as RouteMethod;\n}\n\n// ############# PRIVATE METHODS #############\n\nasync function loadAOTCaches() {\n const loader = await import('./aot/aotCacheLoader.ts');\n return loader.loadRouterAOTCaches();\n}\n\nasync function emitAOTCaches() {\n if (!isMionAOTEmitMode()) return;\n // Dynamic import resolves relative to this source file.\n // This only runs via vite-node (MION_COMPILE=buildOnly|childProcess), which always resolves from source.\n const aotEmitter = await import('./lib/aotEmitter.ts');\n return aotEmitter.emitAOTCaches();\n}\n\n/**\n * Optimized algorithm to flatten the routes object into a list of Executable objects.\n * @param routes\n * @param currentPointer current pointer in the routes object i.e. ['users', 'get']\n * @param preMiddleFns middleFns one level up preceding current pointer\n * @param postMiddleFns middleFns one level up following the current pointer\n * @param nestLevel\n */\nasync function recursiveFlatRoutes(\n routes: Routes,\n currentPointer: string[] = [],\n preMiddleFns: RemoteMethod[] = [],\n postMiddleFns: RemoteMethod[] = [],\n binaryMiddlewares: Set<string> = new Set(),\n nestLevel = 0\n) {\n if (nestLevel > MAX_ROUTE_NESTING)\n throw new Error('Too many nested routes, you can only nest routes ${MAX_ROUTE_NESTING} levels');\n\n const entries = Object.entries(routes);\n if (entries.length === 0)\n throw new Error(\n `Invalid route: ${currentPointer.length ? joinPath(...currentPointer) : '*'}. Can Not define empty routes`\n );\n\n let minus1Props: ReturnType<typeof getRouteEntryProperties> | null = null;\n for (let index = 0; index < entries.length; index++) {\n const [key, item] = entries[index];\n // create the executable items\n const newPointer = [...currentPointer, key];\n let routeEntry: RemoteMethod | RoutesWithId;\n if (typeof key !== 'string' || !isNaN(key as any))\n throw new Error(`Invalid route: ${joinPath(...newPointer)}. Numeric route names are not allowed`);\n if (key.includes(',')) throw new Error(`Invalid route: ${joinPath(...newPointer)}. Route names cannot contain commas.`);\n if (key === WORKFLOW_KEY)\n throw new Error(`Invalid route: ${joinPath(...newPointer)}. '${WORKFLOW_KEY}' is a reserved mion route name.`);\n\n // generates a middleFn\n if (isAnyMiddleFnDef(item)) {\n routeEntry = await getExecutableFromAnyMiddleFn(item, newPointer, nestLevel);\n if (middleFnNames.has(routeEntry.id))\n throw new Error(\n `Invalid middleFn: ${joinPath(...newPointer)}. Naming collision, Naming collision, duplicated middleFn.`\n );\n middleFnNames.add(routeEntry.id);\n }\n\n // generates a route\n else if (isRoute(item)) {\n routeEntry = await getExecutableFromRoute(item, newPointer, nestLevel);\n if (routeNames.has(routeEntry.id))\n throw new Error(`Invalid route: ${joinPath(...newPointer)}. Naming collision, duplicated route`);\n routeNames.add(routeEntry.id);\n }\n\n // generates structure required to go one level down\n else if (isRoutes(item)) {\n routeEntry = {\n pathPointer: newPointer,\n routes: item,\n };\n }\n\n // throws an error if the route is invalid\n else {\n const itemType = typeof item;\n throw new Error(`Invalid route: ${joinPath(...newPointer)}. Type <${itemType}> is not a valid route.`);\n }\n\n // recurse into sublevels\n minus1Props = await recursiveCreateExecutionChain(\n routeEntry,\n newPointer,\n preMiddleFns,\n postMiddleFns,\n binaryMiddlewares,\n nestLevel,\n index,\n entries,\n minus1Props\n );\n\n complexity++;\n }\n}\n\nasync function recursiveCreateExecutionChain(\n routeEntry: RemoteMethod | RoutesWithId,\n currentPointer: string[],\n preMiddleFns: RemoteMethod[],\n postMiddleFns: RemoteMethod[],\n binaryMiddlewares: Set<string>,\n nestLevel: number,\n index: number,\n routeKeyedEntries: RouterKeyEntryList,\n minus1Props: ReturnType<typeof getRouteEntryProperties> | null\n) {\n const minus1 = getEntry(index - 1, routeKeyedEntries);\n const plus1 = getEntry(index + 1, routeKeyedEntries);\n const props = getRouteEntryProperties(minus1, routeEntry, plus1);\n\n if (props.isBetweenRoutes && minus1Props) {\n props.preLevelMiddleFns = minus1Props.preLevelMiddleFns;\n props.postLevelMiddleFns = minus1Props.postLevelMiddleFns;\n } else {\n for (let i = 0; i < routeKeyedEntries.length; i++) {\n const [k, entry] = routeKeyedEntries[i];\n complexity++;\n if (!isAnyMiddleFnDef(entry)) continue;\n const newPointer = [...currentPointer.slice(0, -1), k];\n const executable = await getExecutableFromAnyMiddleFn(entry, newPointer, nestLevel);\n if (i < index) props.preLevelMiddleFns.push(executable);\n if (i > index) props.postLevelMiddleFns.push(executable);\n }\n }\n const isExec = isExecutable(routeEntry);\n\n if (isExec && props.isRoute) {\n const path = getRoutePath(routeEntry.pointer, routerOptions);\n const routeMethod = routeEntry as RouteMethod;\n const levelMethods = [\n ...preMiddleFns,\n ...props.preLevelMiddleFns,\n routeEntry,\n ...props.postLevelMiddleFns,\n ...postMiddleFns,\n ];\n const methods = [...startMiddleFns, ...levelMethods, ...endMiddleFns];\n const executionChain: MethodsExecutionChain = {\n routeIndex: startMiddleFns.length + preMiddleFns.length + props.preLevelMiddleFns.length,\n methods,\n serializer: getSerializerCodeFromMode(routeMethod.options.serializer),\n };\n const middleFnIds = getPublicMiddleFnIds(methods);\n // add middleware functions deps, so can be serialized with the router\n if (middleFnIds.length) routeMethod.middleFnIds = middleFnIds;\n flatRouter.set(path, executionChain);\n // Collect middleware that needs binary JIT functions for retroactive compilation\n if (routeMethod.options.serializer === 'binary') {\n for (const method of methods) {\n if (method.type === HandlerType.middleFn || method.type === HandlerType.headersMiddleFn) {\n binaryMiddlewares.add(method.id);\n }\n }\n }\n } else if (!isExec) {\n await recursiveFlatRoutes(\n routeEntry.routes,\n routeEntry.pathPointer,\n [...preMiddleFns, ...props.preLevelMiddleFns],\n [...props.postLevelMiddleFns, ...postMiddleFns],\n binaryMiddlewares,\n nestLevel + 1\n );\n }\n\n return props;\n}\n\nasync function getExecutableFromAnyMiddleFn(\n middleFn: MiddleFnDef | HeadersMiddleFnDef | RawMiddleFnDef,\n middleFnPointer: string[],\n nestLevel: number\n) {\n if (isRawMiddleFnDef(middleFn)) return getExecutableFromRawMiddleFn(middleFn, middleFnPointer, nestLevel);\n return getExecutableFromMiddleFn(middleFn, middleFnPointer, nestLevel);\n}\n\nexport async function getExecutableFromMiddleFn(\n middleFn: MiddleFnDef | HeadersMiddleFnDef,\n middleFnPointer: string[],\n nestLevel: number\n): Promise<MiddleFnMethod | HeadersMethod> {\n const isHeader = isHeadersMiddleFnDef(middleFn);\n // todo fix header id should be same as any other one and then maybe map from id to header name\n const middleFnId = getRouterItemId(middleFnPointer);\n const existing = middleFnsById.get(middleFnId);\n if (existing) return existing as MiddleFnMethod;\n\n type MixedMiddleFn = (Omit<MiddleFnMethod, 'type'> | Omit<HeadersMethod, 'type'>) & {\n type: typeof HandlerType.middleFn | typeof HandlerType.headersMiddleFn;\n };\n\n const compiledMethod = getPersistedMethod(middleFnId, middleFn.handler);\n let executable: MixedMiddleFn;\n if (compiledMethod) {\n executable = compiledMethod as MixedMiddleFn;\n } else {\n const reflectionData = await getHandlerReflection(\n middleFn.handler,\n middleFnId,\n routerOptions,\n middleFn.options ?? {},\n isHeader,\n middleFn.options?.strictTypes\n );\n executable = {\n id: middleFnId,\n type: isHeader ? HandlerType.headersMiddleFn : HandlerType.middleFn,\n nestLevel,\n handler: middleFn.handler,\n pointer: middleFnPointer,\n ...reflectionData,\n options: {\n runOnError: !!middleFn.options?.runOnError,\n validateParams: middleFn.options?.validateParams ?? true,\n validateReturn: middleFn.options?.validateReturn ?? false,\n description: middleFn.options?.description,\n strictTypes: middleFn.options?.strictTypes ?? routerOptions.strictTypes,\n },\n };\n addToPersistedMethods(middleFnId, executable);\n }\n\n middleFnsById.set(middleFnId, executable as any);\n routesCache.setMethodJitFns(middleFnId, executable as any);\n return executable as any;\n}\n\nexport async function getExecutableFromRawMiddleFn(\n middleFn: RawMiddleFnDef,\n middleFnPointer: string[],\n nestLevel: number\n): Promise<RawMethod> {\n const middleFnId = getRouterItemId(middleFnPointer);\n const existing = rawMiddleFnsById.get(middleFnId);\n if (existing) return existing as RawMethod;\n const reflectionData = await getRawMethodReflection(middleFn.handler, middleFnId, routerOptions);\n const executable: RawMethod = {\n id: middleFnId,\n type: HandlerType.rawMiddleFn,\n nestLevel,\n handler: middleFn.handler,\n pointer: middleFnPointer,\n ...reflectionData,\n options: {\n runOnError: !!middleFn.options?.runOnError,\n validateParams: false,\n validateReturn: false,\n description: middleFn.options?.description,\n },\n };\n rawMiddleFnsById.set(middleFnId, executable);\n routesCache.setMethodJitFns(middleFnId, executable as any);\n return executable;\n}\n\n/** Retroactively compiles binary JIT functions for middleware in the path of binary routes */\nasync function compileBinaryForMiddleware(binaryMiddlewareIds: Set<string>): Promise<void> {\n for (const id of binaryMiddlewareIds) {\n const method = middleFnsById.get(id);\n if (method) await ensureBinaryJitFns(method as MiddleFnMethod);\n }\n}\n\nexport async function getExecutableFromRoute(route: Route, routePointer: string[], nestLevel: number): Promise<RouteMethod> {\n const routeId = getRouterItemId(routePointer);\n const existing = routesById.get(routeId);\n if (existing) return existing as RouteMethod;\n\n const compiledMethod = getPersistedMethod(routeId, route.handler);\n let executable: RouteMethod;\n if (compiledMethod) {\n executable = compiledMethod as RouteMethod;\n } else {\n const resolvedRouteOptions = {...route.options, serializer: route.options?.serializer ?? routerOptions.serializer};\n const reflectionData = await getHandlerReflection(\n route.handler,\n routeId,\n routerOptions,\n resolvedRouteOptions,\n false,\n route.options?.strictTypes\n );\n executable = {\n id: routeId,\n type: HandlerType.route,\n nestLevel,\n handler: route.handler,\n pointer: routePointer,\n ...reflectionData,\n options: {\n runOnError: false,\n validateParams: route.options?.validateParams ?? true,\n validateReturn: route.options?.validateReturn ?? false,\n description: route.options?.description,\n serializer: route.options?.serializer ?? routerOptions.serializer,\n isMutation: route.options?.isMutation,\n strictTypes: route.options?.strictTypes ?? routerOptions.strictTypes,\n },\n };\n addToPersistedMethods(routeId, executable);\n }\n routesById.set(routeId, executable);\n routesCache.setMethodJitFns(routeId, executable as any);\n return executable;\n}\n\n/** Returns IDs of public middleware methods from the execution chain, excluding internal mion routes. */\nfunction getPublicMiddleFnIds(methods: RemoteMethod[]): string[] {\n const ids = methods\n .filter((exec) => isPublicExecutable(exec))\n .map((exec) => getRouterItemId(exec.pointer))\n .filter((mfId) => {\n if (mionInternalRoutes.includes(mfId)) return false;\n const exec = getMiddleFnExecutable(mfId);\n return exec && isPublicExecutable(exec);\n });\n return ids;\n}\n\nfunction getEntry(index: number, keyEntryList: RouterKeyEntryList) {\n return keyEntryList[index]?.[1];\n}\n\nfunction getRouteEntryProperties(\n minus1: RouterEntry | undefined,\n zero: RemoteMethod | RoutesWithId,\n plus1: RouterEntry | undefined\n) {\n const minus1IsRoute = minus1 && isRoute(minus1);\n const zeroIsRoute = (zero as RemoteMethod).type === HandlerType.route;\n const plus1IsRoute = plus1 && isRoute(plus1);\n\n const isExec = !!(zero as RemoteMethod).handler;\n\n return {\n isBetweenRoutes: minus1IsRoute && zeroIsRoute && plus1IsRoute,\n isExecutable: isExec,\n isRoute: zeroIsRoute,\n preLevelMiddleFns: [] as RemoteMethod[],\n postLevelMiddleFns: [] as RemoteMethod[],\n };\n}\n\nasync function getExecutablesFromMiddleFnsCollection(\n middleFnsDef: MiddleFnsCollection\n): Promise<(RawMethod | MiddleFnMethod | HeadersMethod)[]> {\n const results: (RawMethod | MiddleFnMethod | HeadersMethod)[] = [];\n for (const [key, middleFn] of Object.entries(middleFnsDef)) {\n if (isRawMiddleFnDef(middleFn)) {\n results.push(await getExecutableFromRawMiddleFn(middleFn, [key], 0));\n } else if (isHeadersMiddleFnDef(middleFn) || isMiddleFnDef(middleFn)) {\n results.push(await getExecutableFromMiddleFn(middleFn, [key], 0));\n } else {\n throw new Error(`Invalid middleFn: ${key}. Invalid middleFn definition`);\n }\n }\n return results;\n}\n\n/**\n * Validates that a contextDataFactory returns a valid context data object.\n * @param contextDataFactory The factory function to validate\n * @throws Error if the factory doesn't return a plain object with at least one property\n */\nfunction validateSharedDataFactory(opts?: Partial<RouterOptions>): void {\n if (!opts?.contextDataFactory) return;\n const testSharedData = opts.contextDataFactory();\n if (\n typeof testSharedData !== 'object' ||\n Array.isArray(testSharedData) ||\n testSharedData === null ||\n Object.keys(testSharedData).length === 0\n ) {\n throw new Error('contextDataFactory must return a plain object with at least one property');\n }\n}\n\n/** Maps serializer mode string to response body type code */\nfunction getSerializerCodeFromMode(mode: SerializerMode | undefined): SerializerCode {\n switch (mode) {\n case 'binary':\n return SerializerModes.binary;\n case 'stringifyJson':\n return SerializerModes.stringifyJson;\n case 'optimistic':\n return SerializerModes.stringifyJson;\n case 'json':\n default:\n return SerializerModes.json;\n }\n}\n\n/** Path replacement as is not available in edge runtime */\nfunction joinPath(...parts: string[]): string {\n return parts.filter(Boolean).join('/');\n}\n"],"names":["MION_ROUTES","DEFAULT_ROUTE_OPTIONS","serializerMiddleFns","mionClientMiddleFns","startMiddleFns","endMiddleFns","isMionAOTEmitMode","resetRemoteMethodsMetadata","resetPersistedMethods","resetRoutesCache","clearContextPool","clearRoutesFlowCache","setErrorOptions","mionErrorsRoutes","mionClientRoutes","isTestEnv","getPublicApi","isRoute","isRawMiddleFnDef","HandlerType","getENV","isMionCompileMode","MAX_ROUTE_NESTING","WORKFLOW_KEY","isAnyMiddleFnDef","isRoutes","isExecutable","getRoutePath","isHeadersMiddleFnDef","getRouterItemId","getPersistedMethod","getHandlerReflection","addToPersistedMethods","routesCache","getRawMethodReflection","ensureBinaryJitFns","isPublicExecutable","isMiddleFnDef","SerializerModes"],"mappings":";;;;;;;;;;;;;AA2DA,MAAM,qBAAqB,OAAO,OAAOA,gBAAW;AACpD,MAAM,iCAAqD,IAAA;AAC3D,MAAM,oCAA6E,IAAA;AACnF,MAAM,iCAA2C,IAAA;AACjD,MAAM,uCAA+C,IAAA;AACrD,MAAM,oCAAiC,IAAA;AACvC,MAAM,iCAA8B,IAAA;AACpC,IAAI,aAAa;AACjB,IAAI,gBAA+B,EAAC,GAAGC,oCAAA;AACvC,IAAI,sBAAsB;AAC1B,IAAI;AACJ,IAAI;AAGJ,MAAM,wBAAwB;AAAA,EAC1B,wBAAwBC,6BAAAA,oBAAoB;AAChD;AACA,MAAM,sBAAsB;AAAA,EACxB,GAAGC,yBAAAA;AAAAA,EACH,uBAAuBD,6BAAAA,oBAAoB;AAC/C;AACA,IAAI,oBAAyC,EAAC,GAAG,sBAAA;AACjD,IAAI,kBAAuC,EAAC,GAAG,oBAAA;AACpCE,QAAAA,iBAAiC,CAAA;AACjCC,QAAAA,eAA+B,CAAA;AAInC,MAAM,yBAAyB,CAAC,SAAiB,WAAW,IAAI,IAAI;AACpE,MAAM,kBAAkB,MAAM,WAAW,QAAA;AACzC,MAAM,eAAe,MAAM,WAAW;AACtC,MAAM,qBAAqB,CAAC,OAAe,WAAW,IAAI,EAAE;AAC5D,MAAM,wBAAwB,CAAC,OAAe,cAAc,IAAI,EAAE;AAClE,MAAM,kBAAkB,MAAM,cAAc;AAC5C,MAAM,gBAAgB,MAAM;AAC5B,MAAM,mBAAmB,MAAkD;AAC3E,MAAM,mBAAmB,CAAC,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,GAAG,GAAG,CAAC,CAAC;AAAA,IACvE,WAAWU,iBAAAA,qBAAqB,QAAQ,KAAKS,iBAAAA,cAAc,QAAQ,GAAG;AAClE,cAAQ,KAAK,MAAM,0BAA0B,UAAU,CAAC,GAAG,GAAG,CAAC,CAAC;AAAA,IACpE,OAAO;AACH,YAAM,IAAI,MAAM,qBAAqB,GAAG,+BAA+B;AAAA,IAC3E;AAAA,EACJ;AACA,SAAO;AACX;AAOA,SAAS,0BAA0B,MAAqC;AACpE,MAAI,CAAC,MAAM,mBAAoB;AAC/B,QAAM,iBAAiB,KAAK,mBAAA;AAC5B,MACI,OAAO,mBAAmB,YAC1B,MAAM,QAAQ,cAAc,KAC5B,mBAAmB,QACnB,OAAO,KAAK,cAAc,EAAE,WAAW,GACzC;AACE,UAAM,IAAI,MAAM,0EAA0E;AAAA,EAC9F;AACJ;AAGA,SAAS,0BAA0B,MAAkD;AACjF,UAAQ,MAAA;AAAA,IACJ,KAAK;AACD,aAAOC,KAAAA,gBAAgB;AAAA,IAC3B,KAAK;AACD,aAAOA,KAAAA,gBAAgB;AAAA,IAC3B,KAAK;AACD,aAAOA,KAAAA,gBAAgB;AAAA,IAC3B,KAAK;AAAA,IACL;AACI,aAAOA,KAAAA,gBAAgB;AAAA,EAAA;AAEnC;AAGA,SAAS,YAAY,OAAyB;AAC1C,SAAO,MAAM,OAAO,OAAO,EAAE,KAAK,GAAG;AACzC;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -13,6 +13,8 @@ export declare const geMiddleFnsSize: () => number;
13
13
  export declare const getComplexity: () => number;
14
14
  export declare const getRouterOptions: <Opts extends RouterOptions>() => Readonly<Opts>;
15
15
  export declare const getAnyExecutable: (id: string) => RouteMethod<any> | MiddleFnMethod<any> | HeadersMethod<any> | RawMethod<any> | undefined;
16
+ export declare function setPlatformConfig(config: Record<string, unknown>): void;
17
+ export declare const getPlatformConfig: () => Readonly<Record<string, unknown>> | undefined;
16
18
  export declare const resetRouter: () => void;
17
19
  export declare function initMionRouter<R extends Routes>(routes: R, opts?: Partial<RouterOptions>): Promise<PublicApi<R>>;
18
20
  export declare function initRouter(opts?: Partial<RouterOptions>): Promise<Readonly<RouterOptions>>;
@@ -5,6 +5,7 @@ const src_router = require("../router.cjs");
5
5
  const src_lib_handlers = require("../lib/handlers.cjs");
6
6
  const src_types_general = require("../types/general.cjs");
7
7
  const src_lib_remoteMethods = require("../lib/remoteMethods.cjs");
8
+ const src_types_context = require("../types/context.cjs");
8
9
  function __assignType(fn, args) {
9
10
  fn.__type = args;
10
11
  return fn;
@@ -34,17 +35,13 @@ function mionGetRemoteMethodsDataById(ctx, methodsIds, getAllRemoteMethods) {
34
35
  return resp;
35
36
  }
36
37
  mionGetRemoteMethodsDataById.__type = ["ctx", "methodsIds", "getAllRemoteMethods", () => core.__ΩSerializableMethodsData, "rpc-metadata-not-found", () => core.RpcError, "mionGetRemoteMethodsDataById", `P"2!&F2")2#8Pn$P.%7&J/'`];
37
- function mionGetRemoteMethodsDataByPath(ctx, path, getAllRemoteMethods) {
38
- const executables = src_router.getRouteExecutionChain(path);
39
- if (!executables)
40
- return new core.RpcError({
41
- type: "rpc-metadata-not-found",
42
- publicMessage: `Route ${path} not found`
43
- });
44
- const privateExecutables = executables.methods.filter(__assignType((e) => !src_router.isPrivateExecutable(e), ["e", "", 'P"2!"/"']));
45
- return mionGetRemoteMethodsDataById(ctx, privateExecutables.map(__assignType((e) => e.id, ["e", "", 'P"2!"/"'])), getAllRemoteMethods);
38
+ function mionMethodsMetadata(ctx, methodsIds, getAllRemoteMethods) {
39
+ if (!methodsIds || methodsIds.length === 0)
40
+ return;
41
+ ctx.response.serializer = core.SerializerModes.stringifyJson;
42
+ return mionGetRemoteMethodsDataById(ctx, methodsIds, getAllRemoteMethods);
46
43
  }
47
- mionGetRemoteMethodsDataByPath.__type = ["ctx", "path", "getAllRemoteMethods", () => core.__ΩSerializableMethodsData, "rpc-metadata-not-found", () => core.RpcError, "mionGetRemoteMethodsDataByPath", `P"2!&2")2#8Pn$P.%7&J/'`];
44
+ mionMethodsMetadata.__type = [() => src_types_context.__ΩCallContext, "ctx", "methodsIds", "getAllRemoteMethods", () => core.__ΩSerializableMethodsData, "rpc-metadata-not-found", () => core.RpcError, "mionMethodsMetadata", `Pn!2"&F2#8)2$8Pn%P.&7'$J/(`];
48
45
  function addRequiredRemoteMethodsToResponse(id, resp, errorData) {
49
46
  const { methods, deps, purFnDeps } = resp;
50
47
  if (methods[id])
@@ -64,13 +61,16 @@ function addRequiredRemoteMethodsToResponse(id, resp, errorData) {
64
61
  src_lib_remoteMethods.serializeMethodDeps(method, deps, purFnDeps);
65
62
  }
66
63
  addRequiredRemoteMethodsToResponse.__type = ["id", () => core.__ΩSerializableMethodsData, "resp", () => core.__ΩAnyObject, "errorData", "addRequiredRemoteMethodsToResponse", 'P&2!n"2#n$2%$/&'];
64
+ const mionClientMiddleFns = {
65
+ [core.MION_ROUTES.methodsMetadata]: src_lib_handlers.middleFn(mionMethodsMetadata, { runOnError: true })
66
+ };
67
67
  const mionClientRoutes = {
68
68
  // Client routes always use stringifyJson serialization to avoid mutating data as is cached
69
69
  // These routes are used by the client to fetch metadata and must work regardless of router's default serialization
70
- [core.MION_ROUTES.methodsMetadataById]: src_lib_handlers.route(mionGetRemoteMethodsDataById, { serializer: "stringifyJson" }),
71
- [core.MION_ROUTES.methodsMetadataByPath]: src_lib_handlers.route(mionGetRemoteMethodsDataByPath, { serializer: "stringifyJson" })
70
+ [core.MION_ROUTES.methodsMetadataById]: src_lib_handlers.route(mionGetRemoteMethodsDataById, { serializer: "stringifyJson" })
72
71
  };
73
72
  exports.__ΩClientRouteOptions = __ΩClientRouteOptions;
74
73
  exports.defaultClientRouteOptions = defaultClientRouteOptions;
74
+ exports.mionClientMiddleFns = mionClientMiddleFns;
75
75
  exports.mionClientRoutes = mionClientRoutes;
76
76
  //# sourceMappingURL=client.routes.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"client.routes.cjs","sources":["../../../../src/routes/client.routes.ts"],"sourcesContent":["/* ########\n * 2023 mion\n * Author: Ma-jerez\n * License: MIT\n * The software is provided \"as is\", without warranty of any kind.\n * ######## */\n\nimport {AnyObject, RpcError, MION_ROUTES, SerializableMethodsData} from '@mionjs/core';\nimport {\n getMiddleFnExecutable,\n getRouteExecutable,\n isPrivateExecutable,\n getRouteExecutionChain,\n getRouterOptions,\n getTotalExecutables,\n getAllExecutablesIds,\n getAnyExecutable,\n} from '../router.ts';\nimport {route} from '../lib/handlers.ts';\nimport {RouterOptions, Routes} from '../types/general.ts';\nimport {getSerializableMethod, serializeMethodDeps} from '../lib/remoteMethods.ts';\nimport {RemoteMethod} from '../types/remoteMethods.ts';\n\nexport interface ClientRouteOptions extends RouterOptions {\n getAllRemoteMethodsMaxNumber?: number;\n}\n\nexport const defaultClientRouteOptions = {\n getAllRemoteMethodsMaxNumber: 100,\n};\n\n// Internal mion routes that should not be exposed to clients\nconst mionInternalRoutes = Object.values(MION_ROUTES) as string[];\n\n/**\n * Returns the metadata for the given method ids.\n * If getAllRemoteMethods is true, all public methods and middleFns are returned.\n * @mion:route\n */\nfunction mionGetRemoteMethodsDataById(\n ctx,\n methodsIds: string[],\n getAllRemoteMethods?: boolean\n): SerializableMethodsData | RpcError<'rpc-metadata-not-found'> {\n const resp: SerializableMethodsData = {\n methods: {},\n deps: {},\n purFnDeps: {},\n };\n const errorData = {};\n const maxMethods =\n getRouterOptions<ClientRouteOptions>().getAllRemoteMethodsMaxNumber ||\n defaultClientRouteOptions.getAllRemoteMethodsMaxNumber;\n const shouldReturnAll = getAllRemoteMethods && getTotalExecutables() <= maxMethods;\n const idsToReturn = shouldReturnAll\n ? getAllExecutablesIds().filter(\n (id) => !mionInternalRoutes.includes(id) && !isPrivateExecutable(getAnyExecutable(id) as RemoteMethod)\n )\n : methodsIds;\n idsToReturn.forEach((id) => addRequiredRemoteMethodsToResponse(id, resp, errorData));\n\n if (Object.keys(errorData).length)\n return new RpcError({\n type: 'rpc-metadata-not-found',\n publicMessage: 'Errors getting Remote Methods Metadata',\n errorData,\n });\n return resp;\n}\n\n/**\n * Returns the metadata for the given route path.\n * This include all middleFns in the ExecutionChain of the route.\n * If getAllRemoteMethods is true, all public methods and middleFns are returned.\n * @mion:route\n */\nfunction mionGetRemoteMethodsDataByPath(\n ctx,\n path: string,\n getAllRemoteMethods?: boolean\n): SerializableMethodsData | RpcError<'rpc-metadata-not-found'> {\n const executables = getRouteExecutionChain(path);\n if (!executables)\n return new RpcError({\n type: 'rpc-metadata-not-found',\n publicMessage: `Route ${path} not found`,\n });\n const privateExecutables = executables.methods.filter((e) => !isPrivateExecutable(e));\n return mionGetRemoteMethodsDataById(\n ctx,\n privateExecutables.map((e) => e.id),\n getAllRemoteMethods\n );\n}\n\nfunction addRequiredRemoteMethodsToResponse(id: string, resp: SerializableMethodsData, errorData: AnyObject): void {\n const {methods, deps, purFnDeps} = resp;\n if (methods[id]) return;\n if (mionInternalRoutes.includes(id)) return;\n const executable = getMiddleFnExecutable(id) || getRouteExecutable(id);\n if (!executable) {\n errorData[id] = `Remote Method ${id} not found`;\n return;\n }\n if (isPrivateExecutable(executable)) return;\n const method = getSerializableMethod(executable as RemoteMethod);\n methods[id] = method;\n method.middleFnIds?.forEach((middleFnId) => addRequiredRemoteMethodsToResponse(middleFnId, resp, errorData));\n serializeMethodDeps(method, deps, purFnDeps);\n}\n\nexport const mionClientRoutes = {\n // Client routes always use stringifyJson serialization to avoid mutating data as is cached\n // These routes are used by the client to fetch metadata and must work regardless of router's default serialization\n [MION_ROUTES.methodsMetadataById]: route(mionGetRemoteMethodsDataById, {serializer: 'stringifyJson'}),\n [MION_ROUTES.methodsMetadataByPath]: route(mionGetRemoteMethodsDataByPath, {serializer: 'stringifyJson'}),\n} as const satisfies Routes;\n"],"names":["MION_ROUTES","getRouterOptions","getTotalExecutables","getAllExecutablesIds","isPrivateExecutable","getAnyExecutable","RpcError","getRouteExecutionChain","getMiddleFnExecutable","getRouteExecutable","getSerializableMethod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S,MAAM,UAAA,IAAa;AACnC,MAAI,QAAQ,EAAE;AAAG;AACjB,MAAI,mBAAmB,SAAS,EAAE;AAAG;AACrC,QAAM,aAAaI,WAAAA,sBAAsB,EAAE,KAAKC,WAAAA,mBAAmB,EAAE;AACrE,MAAI,CAAC,YAAY;AACb,cAAU,EAAE,IAAI,iBAAiB,EAAE;AACnC;AAAA,EACJ;AACA,MAAIL,WAAAA,oBAAoB,UAAU;AAAG;AACrC,QAAM,SAASM,sBAAAA,sBAAsB,UAA0B;AAC/D,UAAQ,EAAE,IAAI;AACd,SAAO,aAAa,QAAO,aAAC,CAAC,eAAe,mCAAmC,YAAY,MAAM,SAAS;AAC1GC,4CAAoB,QAAQ,MAAM,SAAS;AAC/C;;AAEO,MAAM,mBAAmB;AAAA;AAAA;AAAA,EAG5B,CAACX,KAAAA,YAAY,mBAAmB,GAAGY,iBAAAA,MAAM,8BAA8B,EAAC,YAAY,iBAAgB;AAAA,EACpG,CAACZ,KAAAA,YAAY,qBAAqB,GAAGY,iBAAAA,MAAM,gCAAgC,EAAC,YAAY,gBAAA,CAAgB;;;;;"}
1
+ {"version":3,"file":"client.routes.cjs","sources":["../../../../src/routes/client.routes.ts"],"sourcesContent":["/* ########\n * 2023 mion\n * Author: Ma-jerez\n * License: MIT\n * The software is provided \"as is\", without warranty of any kind.\n * ######## */\n\nimport {AnyObject, Mutable, RpcError, MION_ROUTES, SerializableMethodsData, SerializerModes} from '@mionjs/core';\nimport {\n getMiddleFnExecutable,\n getRouteExecutable,\n isPrivateExecutable,\n getRouterOptions,\n getTotalExecutables,\n getAllExecutablesIds,\n getAnyExecutable,\n} from '../router.ts';\nimport {middleFn, route} from '../lib/handlers.ts';\nimport {RouterOptions, Routes} from '../types/general.ts';\nimport {MiddleFnsCollection} from '../types/publicMethods.ts';\nimport {getSerializableMethod, serializeMethodDeps} from '../lib/remoteMethods.ts';\nimport {RemoteMethod} from '../types/remoteMethods.ts';\nimport {CallContext, MionResponse} from '../types/context.ts';\n\nexport interface ClientRouteOptions extends RouterOptions {\n getAllRemoteMethodsMaxNumber?: number;\n}\n\nexport const defaultClientRouteOptions = {\n getAllRemoteMethodsMaxNumber: 100,\n};\n\n// Internal mion routes that should not be exposed to clients\nconst mionInternalRoutes = Object.values(MION_ROUTES) as string[];\n\n/**\n * Returns the metadata for the given method ids.\n * If getAllRemoteMethods is true, all public methods and middleFns are returned.\n * @mion:route\n */\nfunction mionGetRemoteMethodsDataById(\n ctx,\n methodsIds: string[],\n getAllRemoteMethods?: boolean\n): SerializableMethodsData | RpcError<'rpc-metadata-not-found'> {\n const resp: SerializableMethodsData = {\n methods: {},\n deps: {},\n purFnDeps: {},\n };\n const errorData = {};\n const maxMethods =\n getRouterOptions<ClientRouteOptions>().getAllRemoteMethodsMaxNumber ||\n defaultClientRouteOptions.getAllRemoteMethodsMaxNumber;\n const shouldReturnAll = getAllRemoteMethods && getTotalExecutables() <= maxMethods;\n const idsToReturn = shouldReturnAll\n ? getAllExecutablesIds().filter(\n (id) => !mionInternalRoutes.includes(id) && !isPrivateExecutable(getAnyExecutable(id) as RemoteMethod)\n )\n : methodsIds;\n idsToReturn.forEach((id) => addRequiredRemoteMethodsToResponse(id, resp, errorData));\n\n if (Object.keys(errorData).length)\n return new RpcError({\n type: 'rpc-metadata-not-found',\n publicMessage: 'Errors getting Remote Methods Metadata',\n errorData,\n });\n return resp;\n}\n\n/** Middleware wrapper: delegates to mionGetRemoteMethodsDataById when params are provided */\nfunction mionMethodsMetadata(\n ctx: CallContext,\n methodsIds?: string[],\n getAllRemoteMethods?: boolean\n): SerializableMethodsData | RpcError<'rpc-metadata-not-found'> | void {\n if (!methodsIds || methodsIds.length === 0) return;\n // Force JSON serialization so optimistic client can parse the response\n (ctx.response as Mutable<MionResponse>).serializer = SerializerModes.stringifyJson;\n return mionGetRemoteMethodsDataById(ctx, methodsIds, getAllRemoteMethods);\n}\n\nfunction addRequiredRemoteMethodsToResponse(id: string, resp: SerializableMethodsData, errorData: AnyObject): void {\n const {methods, deps, purFnDeps} = resp;\n if (methods[id]) return;\n if (mionInternalRoutes.includes(id)) return;\n const executable = getMiddleFnExecutable(id) || getRouteExecutable(id);\n if (!executable) {\n errorData[id] = `Remote Method ${id} not found`;\n return;\n }\n if (isPrivateExecutable(executable)) return;\n const method = getSerializableMethod(executable as RemoteMethod);\n methods[id] = method;\n method.middleFnIds?.forEach((middleFnId) => addRequiredRemoteMethodsToResponse(middleFnId, resp, errorData));\n serializeMethodDeps(method, deps, purFnDeps);\n}\n\nexport const mionClientMiddleFns = {\n [MION_ROUTES.methodsMetadata]: middleFn(mionMethodsMetadata, {runOnError: true}),\n} as const satisfies MiddleFnsCollection;\n\nexport const mionClientRoutes = {\n // Client routes always use stringifyJson serialization to avoid mutating data as is cached\n // These routes are used by the client to fetch metadata and must work regardless of router's default serialization\n [MION_ROUTES.methodsMetadataById]: route(mionGetRemoteMethodsDataById, {serializer: 'stringifyJson'}),\n} as const satisfies Routes;\n"],"names":["MION_ROUTES","getRouterOptions","getTotalExecutables","getAllExecutablesIds","isPrivateExecutable","getAnyExecutable","RpcError","SerializerModes","getMiddleFnExecutable","getRouteExecutable","getSerializableMethod","serializeMethodDeps","middleFn","route"],"mappings":";;;;;;;;;;;;;AA4BO,MAAM,4BAA4B;AAAA,EACrC,8BAA8B;;AAIlC,MAAM,qBAAqB,OAAO,OAAOA,gBAAW;AAOpD,SAAS,6BACL,KACA,YACA,qBAA6B;AAE7B,QAAM,OAAgC;AAAA,IAClC,SAAS,CAAA;AAAA,IACT,MAAM,CAAA;AAAA,IACN,WAAW,CAAA;AAAA,EAAA;AAEf,QAAM,YAAY,CAAA;AAClB,QAAM,cACFC,WAAAA,4DAAAA,WAAAA,iBAAA,GAAuC,gCACvC,0BAA0B;AAC9B,QAAM,kBAAkB,uBAAuBC,WAAAA,oBAAA,KAAyB;AACxE,QAAM,cAAc,kBACdC,WAAAA,uBAAuB,OAAM,aACzB,CAAC,OAAO,CAAC,mBAAmB,SAAS,EAAE,K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Y,gBAAA,CAAgB;;;;;;"}
@@ -1,5 +1,6 @@
1
1
  import { RpcError, SerializableMethodsData } from '@mionjs/core';
2
2
  import { RouterOptions } from '../types/general.ts';
3
+ import { CallContext } from '../types/context.ts';
3
4
  export interface ClientRouteOptions extends RouterOptions {
4
5
  getAllRemoteMethodsMaxNumber?: number;
5
6
  }
@@ -7,10 +8,12 @@ export declare const defaultClientRouteOptions: {
7
8
  getAllRemoteMethodsMaxNumber: number;
8
9
  };
9
10
  declare function mionGetRemoteMethodsDataById(ctx: any, methodsIds: string[], getAllRemoteMethods?: boolean): SerializableMethodsData | RpcError<'rpc-metadata-not-found'>;
10
- declare function mionGetRemoteMethodsDataByPath(ctx: any, path: string, getAllRemoteMethods?: boolean): SerializableMethodsData | RpcError<'rpc-metadata-not-found'>;
11
+ declare function mionMethodsMetadata(ctx: CallContext, methodsIds?: string[], getAllRemoteMethods?: boolean): SerializableMethodsData | RpcError<'rpc-metadata-not-found'> | void;
12
+ export declare const mionClientMiddleFns: {
13
+ readonly "mion@methodsMetadata": import('../types/definitions.ts').MiddleFnDef<typeof mionMethodsMetadata>;
14
+ };
11
15
  export declare const mionClientRoutes: {
12
16
  readonly "mion@methodsMetadataById": import('../types/definitions.ts').RouteDef<typeof mionGetRemoteMethodsDataById>;
13
- readonly "mion@methodsMetadataByPath": import('../types/definitions.ts').RouteDef<typeof mionGetRemoteMethodsDataByPath>;
14
17
  };
15
18
  export {};
16
19
  export declare type __ΩClientRouteOptions = any[];
@@ -4,7 +4,6 @@ export declare const mionRoutes: {
4
4
  readonly "mion@notFound": import('../types/definitions.ts').RouteDef<(ctx: import('../types/context.ts').CallContext) => import('@mionjs/core/.dist/esm/index.js').RpcError<"route-not-found">>;
5
5
  readonly "mion@platformError": import('../types/definitions.ts').RouteDef<(_ctx: import('../types/context.ts').CallContext) => import('@mionjs/core/.dist/esm/index.js').RpcError<string>>;
6
6
  readonly "mion@methodsMetadataById": import('../types/definitions.ts').RouteDef<(ctx: any, methodsIds: string[], getAllRemoteMethods?: boolean) => import('@mionjs/core/.dist/esm/index.js').SerializableMethodsData | import('@mionjs/core/.dist/esm/index.js').RpcError<"rpc-metadata-not-found">>;
7
- readonly "mion@methodsMetadataByPath": import('../types/definitions.ts').RouteDef<(ctx: any, path: string, getAllRemoteMethods?: boolean) => import('@mionjs/core/.dist/esm/index.js').SerializableMethodsData | import('@mionjs/core/.dist/esm/index.js').RpcError<"rpc-metadata-not-found">>;
8
7
  };
9
8
  export type MionRoutes = PublicApi<typeof mionRoutes>;
10
9
  export declare type __ΩMionRoutes = any[];
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const core = require("@mionjs/core");
4
- const virtual_mionServerPureFns = require("virtual:mion-server-pure-fns");
4
+ const serverPureFns = require("@mionjs/core/server-pure-fns");
5
5
  const src_router = require("./router.cjs");
6
6
  const src_types_general = require("./types/general.cjs");
7
7
  const src_types_remoteMethods = require("./types/remoteMethods.cjs");
@@ -156,7 +156,7 @@ function insertMappingMethods(middleMethods, mappings) {
156
156
  errorData: { mapping }
157
157
  });
158
158
  }
159
- if (!virtual_mionServerPureFns.serverPureFnsCache[core.PURE_SERVER_FN_NAMESPACE]?.[mapping.bodyHash]?.fn) {
159
+ if (!serverPureFns.serverPureFnsCache[core.PURE_SERVER_FN_NAMESPACE]?.[mapping.bodyHash]?.fn) {
160
160
  throw new core.RpcError({
161
161
  statusCode: core.StatusCodes.UNEXPECTED_ERROR,
162
162
  type: "routesFlow-mapping-missing-pure-fn",
@@ -201,7 +201,7 @@ createMappingMethod.__type = ["RoutesFlowMapping", "mapping", () => src_types_re
201
201
  function createMappingHandler(mapping) {
202
202
  return __assignType((ctx) => {
203
203
  const sourceOutput = ctx.response.body[mapping.fromId];
204
- const entry = virtual_mionServerPureFns.serverPureFnsCache[core.PURE_SERVER_FN_NAMESPACE]?.[mapping.bodyHash];
204
+ const entry = serverPureFns.serverPureFnsCache[core.PURE_SERVER_FN_NAMESPACE]?.[mapping.bodyHash];
205
205
  if (!entry?.fn) {
206
206
  throw new core.RpcError({
207
207
  statusCode: core.StatusCodes.UNEXPECTED_ERROR,