Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 707x 707x 707x 707x 707x 707x 707x 707x 707x 707x 707x 707x 707x 707x 707x 707x 707x 707x 707x 707x 707x 707x 707x 707x 707x 707x 707x 827x 827x 827x 827x 827x 827x 827x 827x 827x 827x 827x 827x 827x 827x 827x 827x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 3333x 3333x 3333x 3333x 3333x 3333x 3333x 3333x 1x 1x 1x 90x 90x 90x 90x 90x 90x 3333x 3333x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 15x 8x 15x 15x 15x 15x 15x 15x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 1204x 2298x 2298x 2298x 2298x 2298x 2298x 2298x 2298x 2298x 2298x 2298x 2298x 1204x 2298x 1094x 1094x 1094x 1094x 1094x 1094x 1094x 1094x 1094x 33x 33x 33x 33x 33x 33x 33x 33x 1061x 1061x 1061x 1061x 1061x 1061x 1061x 1061x 1061x 1061x 1061x 1061x 1094x 1028x 1028x 706x 322x 322x 1028x 1028x 1028x 2961x 2961x 2961x 2961x 2961x 2961x 2961x 2961x 2961x 2640x 2961x 2961x 1028x 1028x 1028x 321x 321x 321x 321x 321x 51x 321x 321x 321x 321x 321x 321x 321x 307x 307x 307x 307x 307x 307x 134x 173x 173x 173x 173x 173x 173x 173x 173x 173x 173x 173x 173x 173x 173x 173x 173x 307x 307x 307x 307x 307x 307x 173x 173x 173x 173x 321x 321x 321x 321x 1028x 1028x 707x 707x 707x 707x 707x 707x 707x 707x 707x 1028x 1028x 1028x 1061x 1061x 2298x 2298x 1204x 1204x 1204x | import {
kind,
type Adapter,
type IAdapterFactory,
type IApiSchema,
type IConfigRuntime,
type IErrorFactory,
type ILib,
type IMeta,
type IModuleConfig,
type IObjectSchema,
} from '@feasibleone/blong/types';
import merge from 'ut-function.merge';
import createPort from './AdapterBase.ts';
import ConfigRuntime from './ConfigRuntime.ts';
import createHandlerProxy from './handlerProxy.ts';
import { methodId } from './lib.ts';
import type { IPort } from './Port.ts';
/**
* Create the handler closure that is pushed into `where.methods[]`.
*
* This closure is invoked later by `Registry._createHandlers()` to assemble the
* layerApi object, load libraries, and process handler/validation/api/model items.
*/
function createHandlerClosure(
others: any[], // eslint-disable-line @typescript-eslint/no-explicit-any
moduleName: string,
moduleConfigSlice: unknown,
name: string,
namespace: string,
source: string,
target: {result: {error: unknown; schema: unknown; vocabulary?: object[]}; error?: unknown},
apiSchema: IApiSchema | undefined,
configRuntime: IConfigRuntime | undefined,
attachSignals?: () => Promise<
| ((
port: object,
vocabularies: readonly object[],
policy?: Record<string, object>,
) => void)
| undefined
>,
): (params: {
remote: (methodName: string) => () => unknown;
lib: ILib;
local: object;
literals: unknown[];
port: Adapter;
attachCheckpoint?: (meta: IMeta) => void;
}) => Promise<void> {
return async function ({
remote,
lib,
local,
literals,
port,
attachCheckpoint,
...rest
}: {
remote: (methodName: string) => () => unknown;
lib: ILib;
local: object;
literals: unknown[];
port: Adapter;
attachCheckpoint?: (meta: IMeta) => void;
}) {
// A live view, not a copy: the module slice the loader hands over is the
// runtime's own config proxy, so re-merging it after a reload is what
// lets a handler read a reloaded value without the layer being assembled
// again (T-159).
const mergedConfig = ConfigRuntime.liveLayerConfig(
() => ConfigRuntime.mergeLayerConfig(moduleConfigSlice, port?.config?.[namespace]),
() => configRuntime?.version ?? 0,
);
// The declared vocabulary, attached to the port its handlers run on (`@@T-712`). This is the
// first place that holds both halves: the declarations the layer shipped, and the port —
// whose bound logger is where a record is written. A port whose layer declared nothing gets
// no `signal`, and `this.signal?.x?.()` in its code is then a no-op rather than an error; such
// a port also never resolves the attacher, so the module behind it is not loaded at all.
// A layer that only declares — `lukaot/` has no port and no handlers — has nowhere to attach
// the proxy: the realm hands its array to every layer's result, and the port that answers those
// names is an adapter or orchestrator beside it (`@@T-718`).
if (target.result.vocabulary?.length && attachSignals && port) {
const attach = await attachSignals();
// The realm's own `lukaot.policy` block, read from the config this layer was handed: an
// operator raises one identifier to `debug` or switches one off there, and nothing in the
// layer's source changes. Read structurally so this file keeps its distance from lukaot's
// types — it is on the browser loader's path.
const policy = (mergedConfig as {lukaot?: {policy?: Record<string, object>}})?.lukaot
?.policy;
attach?.(port, target.result.vocabulary, policy);
// A `library()` function has no port, so the same vocabulary reaches it through the lib it
// is handed: `lib.signal.x()` and `this.signal?.x?.()` are then one mechanism, not two.
const vocabulary = (port as {signal?: unknown})?.signal;
if (vocabulary) (lib as unknown as Record<string, unknown>).signal = vocabulary;
}
const layerApi = {
...rest,
config: mergedConfig,
lib: new Proxy(lib, {
get(target: ILib, functionName: string) {
const rec = target as unknown as Record<string, unknown>;
let fn: () => unknown;
// A member the library *has* and the runtime leaves out is absent, not pending:
// `checkpoint` is `undefined` outside a recording mode by design, and reading it
// has to answer `undefined` so `lib.checkpoint?.(…)` costs the optional chain and
// nothing else. Only a key the library has never seen is deferred, because a later
// layer may merge it in (`@@T-718`).
if (Object.hasOwn(rec, functionName)) return rec[functionName];
return (
rec[functionName] ??
function (...params: unknown[]) {
fn ||= rec[functionName] as () => unknown;
if (!fn)
throw new Error(
`Lib property '${functionName.toString()}' not found. Available properties are: ${Object.keys(
rec,
).sort()}`,
);
return fn.apply(port, params as []);
}
);
},
}),
handler: createHandlerProxy(
local,
port,
remote,
attachCheckpoint,
lib,
mergedConfig,
String(moduleName ?? '').replace(/[^A-Za-z0-9.]/g, ''),
),
errors: target.result.error,
schema: target.result.schema,
};
for (let what of others) {
// process the injected library functions first @@doc:patterns/handler#injected-library-functions
switch (`${typeof what}:${kind(what)}`) {
case 'object:lib':
merge(lib, what);
break;
case 'function:lib':
what = await what(layerApi);
if (typeof what === 'function')
(lib as unknown as Record<string, unknown>)[what.name] = what;
else merge(lib, what);
}
}
for (let what of others) {
const kindOfWhat = kind(what);
switch (`${typeof what}:${kindOfWhat}`) {
case 'object:handler':
case 'object:validation':
merge(local, what);
break;
/**
* The declared vocabulary (`@@T-711` `@@T-712`).
*
* A declaration is a plain object carrying the framework's kind symbol, and it is
* *not* a handler, a lib or a schema — it is the list of names the layer's own code may
* call, so the only thing to do with it is register it: the error half with the error
* factory (so `errors.<name>` answers the declared message and status rather than a
* generated default), and the whole vocabulary on the layer's result, where the port
* reads it to build the `signal` proxy a handler calls.
*/
case 'object:vocabulary': {
const declaration = what as {
declared?: {error?: Record<string, unknown>};
signal?: Record<string, unknown>;
};
if (declaration.declared?.error && target.error) {
(target.error as (map: object) => unknown)(declaration.declared.error);
}
const vocabulary = target.result as {vocabulary?: object[]};
(vocabulary.vocabulary ??= []).push(declaration);
break;
}
/**
* The legacy `error/error.ts` map, which has no kind symbol at all.
*
* It arrives here as a bare `object:` — the shape this switch had no case for, so for
* as long as the folder has existed its declared messages and status codes were
* dropped and `errors.<name>` answered a generated default. It is registered now, by
* the same call the vocabulary uses, because a realm that has not moved to a
* declaration must keep the errors it declared (`@@T-711`).
*/
case 'object:':
if (name === 'error' && target.error) {
(target.error as (map: object) => unknown)(what as object);
break;
}
break;
case 'function:api':
merge(local, await apiSchema!.schema(what(layerApi), source));
break;
case 'object:schema':
if (target.result.schema)
lib.mergeWithSymbols(target.result.schema as object, {
[moduleName]: what as object,
});
break;
case 'function:schema': {
configRuntime?.enterConfig();
try {
what = await what(layerApi);
} finally {
configRuntime?.exitConfig();
}
if (typeof what === 'object' && what !== null && target.result.schema)
lib.mergeWithSymbols(target.result.schema, {[moduleName]: what});
break;
}
case 'function:handler':
case 'function:validation':
case 'function:model':
case 'function:fixture':
configRuntime?.enterConfig();
try {
what = await what(layerApi);
} finally {
configRuntime?.exitConfig();
}
const created = await (
port as unknown as {createHandlers?: (opts: unknown) => Promise<unknown>}
)?.createHandlers?.({
handlers: typeof what === 'function' ? [what] : what,
layerApi,
kind: kindOfWhat,
});
// Each handler is stored under TWO keys, mirroring `attachHandlers`
// in `adapter/schema/knex/schemaTable.ts`. The normalised one
// (`methodId`: dots stripped, letters lower-cased) is what
// `findHandler` resolves; the original-case one is what
// `super.<name>` looks up, and only that spelling is reachable there
// because JavaScript property lookup is case-sensitive. A group's
// `local` is an ancestor of the object literal a handler returns,
// which is how a handler delegates to a handler group attached
// *before* it — `core` is an early child for exactly this reason.
//
// Validations keep the normalised key alone: they are collected by
// iterating `local` (`Registry._validations`), and a second spelling
// of every schema would register a second name for it that
// `methodParts` cannot split.
const forSuper = kindOfWhat !== 'validation';
if (typeof what === 'function') {
(local as Record<string, unknown>)[methodId(what.name)] = what;
if (forSuper) (local as Record<string, unknown>)[what.name] = what;
} else {
literals.push(what);
if (forSuper) merge(local, what);
merge(local, methodId(what));
}
Object.assign(local, methodId(created));
}
}
};
}
export default function layerProxy(
errors: IErrorFactory | undefined,
objectSchema: IObjectSchema | undefined,
apiSchema: IApiSchema | undefined,
port: (() => void) | undefined,
moduleConfig: {
pkg: IModuleConfig['pkg'];
base: string;
name: string;
configNames?: string[];
} & {
[name: string]: object;
},
configRuntime?: IConfigRuntime,
/**
* Resolve the attacher that turns a layer's declarations into the `signal` proxy.
*
* A *resolver*, not the function, and injected rather than imported: this module is on the browser
* loader's path too, and `signals.ts` reaches lukaot's root — which reaches pino and
* `node:async_hooks`. Resolving it lazily is also what keeps a process that declares nothing from
* loading the module at all, which is every process today.
*/
attachSignals?: () => Promise<
| ((
port: object,
vocabularies: readonly object[],
policy?: Record<string, object>,
) => void)
| undefined
>,
): {result: {error: unknown; schema: unknown; vocabulary?: object[]}; feature: unknown} {
return new Proxy(
{
error: errors?.register.bind(errors),
result: {error: errors?.get(), schema: objectSchema},
feature() {},
},
{
get(
target: {
error: unknown;
result: {error: unknown; schema: unknown};
feature: unknown;
},
name: string,
receiver: unknown,
) {
switch (name) {
case 'utPort':
return port;
case 'registerErrors':
return target.error;
case 'result':
return target.result;
default:
return (fn: unknown, namespace: string, source: string) => {
// A declared vocabulary is registered *now*, not when the layer's methods
// are, because it needs neither the port nor the closure: it is a list of
// names that belongs to the layer's result and to the process-wide error
// factory. Deferring it was incidental, and it left a realm whose layer
// declares nothing but a vocabulary with no registration at all — the
// closure carrying it is only run while a layer's methods are registered,
// and `lukaot/` has no methods of its own (`@@T-717`).
if (kind(fn as never) === 'vocabulary') {
const declaration = fn as {
declared?: {error?: Record<string, unknown>};
};
if (declaration.declared?.error && target.error) {
(target.error as (map: object) => unknown)(
declaration.declared.error,
);
}
const collected = target.result as {vocabulary?: object[]};
(collected.vocabulary ??= []).push(fn as object);
return;
}
type Where = {
methods: unknown[];
port?: unknown;
source?: string;
config?: unknown;
};
const resultRecord = target.result as Record<string, Where>;
const where: Where = (resultRecord[name] ??= {methods: [], source});
const targetRec = target as unknown as Record<
string,
((fn: unknown) => void) | undefined
>;
if (targetRec[name]) merge(where, targetRec[name]!(fn));
else {
const fnArr: unknown[] = Array.isArray(fn)
? fn
: fn != null
? [fn]
: [];
const [ports, others] = fnArr.reduce(
(prev: [unknown[], unknown[]], item: unknown) => {
if (
(port &&
(item as {prototype?: unknown}).prototype instanceof
port) ||
['adapter', 'orchestrator'].includes(
kind(item as unknown as Parameters<typeof kind>[0]),
)
)
prev[0].push(item);
else prev[1].push(item);
return prev;
},
[[], []] as [unknown[], unknown[]],
) as [unknown[], unknown[]];
ports.forEach(what => {
if (
what &&
port &&
(what as {prototype?: unknown}).prototype instanceof port
) {
where.port = async (
{
id,
...portApi
}: Parameters<IAdapterFactory>[0] & {id: string},
configOverride: object,
) => {
const config = {
...moduleConfig?.[name],
id,
pkg: moduleConfig.pkg,
};
const port = new (what as unknown as IPort)({
...portApi,
config: configOverride
? merge({}, config, configOverride)
: config,
configBase: moduleConfig.base,
});
await (
port as unknown as {init: () => Promise<void>}
).init();
return port;
};
(where.port as unknown as {config: unknown}).config =
moduleConfig?.[name];
} else if (
what &&
['adapter', 'orchestrator'].includes(
kind(what as unknown as Parameters<typeof kind>[0]),
)
) {
where.port = async (
api: Parameters<IAdapterFactory>[0] & {id: string},
configOverride: object,
) => {
const {id} = api;
if (!id)
return (what as IAdapterFactory)(
api as Parameters<IAdapterFactory>[0],
);
// Assign `handlers` directly onto the api object rather
// than creating a spread copy. AdapterBase stores
// `_api = api`, and Registry.ts sets
// `api.attachHandlers = fn` *after* this factory
// returns. Keeping the same object reference ensures
// that assignment is visible to `_api.attachHandlers`
// when `start()` is called later.
(api as unknown as Record<string, unknown>).handlers =
what;
const port = await createPort(
api as unknown as Parameters<typeof createPort>[0],
moduleConfig.base,
moduleConfig.configNames,
);
const config = {
...moduleConfig?.[name],
id,
pkg: moduleConfig.pkg,
};
await port.init(
configOverride
? merge({}, config, configOverride)
: config,
);
return port;
};
(where.port as unknown as {config: unknown}).config =
moduleConfig?.[name];
}
});
if (others.length)
where.methods.push(
createHandlerClosure(
others as unknown[],
moduleConfig.name,
moduleConfig[name],
name,
namespace,
source,
target,
apiSchema,
configRuntime,
attachSignals,
),
);
}
return receiver;
};
}
},
},
);
}
|