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 | 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 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 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 1x 1x 1x 1x 199x 199x 199x 144x 144x 144x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 110x 110x 110x 110x 110x 55x 55x 110x 110x 110x 110x 110x 55x 55x 347x 347x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 1x 1x 46x 46x 46x 46x 46x 46x 10x 10x 10x 46x 424x 424x 424x 424x 424x 447x 424x 424x 83x 6x 6x 6x 6x 83x 77x 77x 77x 83x 424x 46x 1x 1x 10x 6x 10x 10x 10x 10x 10x 10x 10x 10x 6x 10x 10x 1x 1x 55x 55x 55x 55x 55x 55x 55x 55x 55x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 36x 36x 77x 77x 77x 34x 77x 77x 76x 76x 76x 76x 77x 77x 77x 77x 77x 36x 56x 55x 1x 1x 1x 1x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 8x 8x 8x 8x 25x 25x 83x 9x 9x 9x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 1x 1x 11x 11x 11x 11x 1x 1x 11x 11x 11x 11x 10x 11x 11x 11x 11x 11x 11x 11x 11x 11x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 1x 1x 10x 10x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import type {
Adapter,
GatewaySchema,
Handlers,
IAdapterFactory,
IAdapterRegistry,
IApiSchema,
IErrorFactory,
IGateway,
ILocal,
ILog,
IMeta,
IPlatformApi,
IRegistry,
IRemote,
IRpcServer,
} from '@feasibleone/blong/types';
import {Internal} from '@feasibleone/blong/types';
import nodeAssert from 'node:assert';
import PQueue from 'p-queue';
import {Type} from 'typebox';
import {monotonicFactory} from 'ulidx';
import merge from 'ut-function.merge';
import {v4 as uuid4, v7 as uuid7} from 'uuid';
import yaml from 'yaml';
import {createAttachCheckpoint} from './checkpoint.ts';
import {methodId, methodParts} from './lib.ts';
import type {IResolution} from './Resolution.ts';
import type {IWatch} from './Watch.ts';
type MatchMethodsCallback = (
name: string,
local: {config?: object; namespace?: string | string[]},
literals: object[],
) => void;
const API: RegExp = /\.validation$|\.api$|^validation$|^api$/;
const ulid: ReturnType<typeof monotonicFactory> = monotonicFactory();
interface IConfig {
api?: Record<string, {source: string; def: {namespace: Record<string, string | string[]>}}>;
checkpointMode?: 'test' | 'debug' | 'production';
/**
* Map of namespace → true for methods that should be registered in
* `Local` only (in-process dispatch) and skipped in `RpcServer`
* (inter-process dispatch). When running as a monolith with
* `canSkipSocket: true`, local-only methods avoid unnecessary RPC
* registration overhead.
*/
localOnly?: Record<string, boolean>;
}
export default class Registry extends Internal implements IRegistry {
public modules: Map<string | symbol, IRegistry[]> = new Map();
public ports: Map<string, IAdapterRegistry> = new Map();
public methods: Map<string, Handlers> = new Map();
#reload: PQueue = new PQueue({concurrency: 1});
#ports: Map<string, Adapter> = new Map();
#error: IErrorFactory;
#portAttachments: Map<
string,
[
{
port: object;
target: {importedMap?: Map<string, object>};
parent: object;
pointer: object;
},
]
> = new Map();
#validations: Record<string, GatewaySchema> = {};
#resolution: IResolution;
#rpcServer: IRpcServer;
#remote: IRemote;
#gateway: IGateway;
#local: ILocal;
#watch: IWatch;
#log: ILog;
#apiSchema: IApiSchema;
#platform: IPlatformApi;
#config: IConfig = {};
#attachCheckpoint?: (meta: IMeta) => void;
public constructor(
config: object,
{
log,
error,
rpcServer,
remote,
gateway,
local,
resolution,
watch,
apiSchema,
platform,
}: {
log?: ILog;
error?: IErrorFactory;
rpcServer?: IRpcServer;
remote?: IRemote;
gateway?: IGateway;
local?: ILocal;
resolution?: IResolution;
watch?: IWatch;
apiSchema?: IApiSchema;
platform?: IPlatformApi;
},
) {
super({log});
this.merge(this.#config, config);
this.#resolution = resolution!;
this.#rpcServer = rpcServer!;
this.#error = error!;
this.#remote = remote!;
this.#gateway = gateway!;
this.#local = local!;
this.#log = log!;
this.#watch = watch!;
this.#apiSchema = apiSchema!;
this.#platform = platform!;
this.#attachCheckpoint = createAttachCheckpoint(
this.#config.checkpointMode ?? 'production',
);
this.#rpcServer?.setAttachCheckpoint?.(this.#attachCheckpoint);
}
public getPort(id: string): Adapter | undefined {
return this.#ports.get(id);
}
public async createPort(id: string): Promise<Adapter | undefined> {
const port = this.ports.get(id);
await this.#ports.get(id)?.stop();
if (port!.config === false) {
this.#ports.delete(id);
return;
}
const api: Parameters<IAdapterFactory>[0] = {
id,
type: Type,
adapter: id => this.ports.get(id),
utError: {
register: this.#error.register.bind(this.#error),
defineError: this.#error.define.bind(this.#error),
getError: this.#error.get.bind(this.#error),
fetchErrors: this.#error.fetch.bind(this.#error),
},
errors: this.#error,
gateway: this.#gateway,
remote: this.#remote,
rpc: this.#rpcServer,
local: this.#local,
registry: this,
register: (methods, namespace, port, pkg) => {
if (!this.#config.localOnly?.[namespace]) {
this.#rpcServer?.register(methods, namespace, true, pkg);
}
this.#local?.register(methods, namespace, true, pkg);
},
unregister: (methods, namespace) => {
if (!this.#config.localOnly?.[namespace]) {
this.#rpcServer?.unregister(methods, namespace, true);
}
this.#local?.unregister(methods, namespace);
},
subscribe: (methods, namespace, port, pkg) => {
if (!this.#config.localOnly?.[namespace]) {
this.#rpcServer?.register(methods, namespace, false, pkg);
}
this.#local?.register(methods, namespace, false, pkg);
},
unsubscribe: (methods, namespace) => {
if (!this.#config.localOnly?.[namespace]) {
this.#rpcServer?.unregister(methods, namespace, false);
}
this.#local?.unregister(methods, namespace);
},
getPath(method: string) {
return method.match(/^[^[#?]*/)?.[0] || method;
},
methodId,
importMethod: (methodName, options) => this.#remote.remote(methodName, options),
dispatch: (...params) => this.#remote.dispatch(...params),
attachHandlers: undefined!,
createLog: (level, bindings) => this.#log?.logger(level, bindings) || {},
attachCheckpoint: this.#attachCheckpoint,
};
const result = (await port!(api)) as unknown as Adapter;
this.#ports.set(id, result);
api.attachHandlers = this._attachHandlers(result);
return result as Adapter;
}
private async _matchMethods(
mode: 'extend' | 'merge',
patterns: (string | RegExp)[] | string | RegExp,
port: object | MatchMethodsCallback | undefined,
callback?: MatchMethodsCallback,
): Promise<void> {
if (typeof port === 'function' && !callback) {
callback = port as MatchMethodsCallback;
port = undefined;
}
for (const [name, value] of this.methods.entries()) {
if (
([] as (string | RegExp)[])
.concat(patterns)
.some(
pattern =>
(pattern instanceof RegExp && pattern.test(name)) || pattern === name,
)
) {
if (mode === 'merge') {
for (const item of value) {
const {local, literals} = await this._createHandlers([item], port);
callback!(name, local, literals);
}
} else {
const {local, literals} = await this._createHandlers(value, port);
callback!(name, local, literals);
}
}
}
}
private async _validations(): Promise<Record<string, GatewaySchema>> {
await this._matchMethods('merge', API, (name, local) => {
Object.entries(local).forEach(([name, validation]) => {
const schema =
typeof validation === 'function'
? (validation as () => GatewaySchema)()
: typeof validation === 'object'
? validation
: {};
if (typeof validation === 'function') name = validation.name;
const prev = this.#validations[methodParts(name)];
if (prev) merge(prev, schema);
else this.#validations[methodParts(name)] = schema as GatewaySchema;
});
});
return this.#validations;
}
private _attachHandlers(port: object): (
target: {
importedMap?: Map<string, object>;
imported: object;
config: {namespace?: string | string[]};
},
patterns: (string | RegExp)[] | string | RegExp,
adapter?: boolean,
) => unknown {
return (
target: {
importedMap?: Map<string, object>;
imported: object;
config: {
namespace?: string | string[];
};
},
patterns: (string | RegExp)[] | string | RegExp,
adapter?: boolean,
) => {
target.imported = {};
Object.setPrototypeOf(target.imported, target);
if (patterns && (!Array.isArray(patterns) || patterns.length)) {
target.importedMap = new Map(); // preserve patterns order
return this._matchMethods('extend', patterns, port, (name, local, literals) => {
const ports = this.#portAttachments.get(name);
const info = {port, target, parent: target.imported, pointer: {}};
if (ports) ports.push(info);
else this.#portAttachments.set(name, [info]);
target.importedMap!.set(name, local);
if (adapter) {
if (local.config) merge(target.config, local.config);
if (local.namespace)
target.config.namespace = target.config.namespace
? ([] as string[]).concat(target.config.namespace, local.namespace)
: local.namespace;
}
literals.forEach(literal => Object.setPrototypeOf(literal, target.imported));
Object.setPrototypeOf(local, info.parent);
Object.setPrototypeOf(info.pointer, local);
target.imported = info.pointer;
});
}
};
}
public async replaceHandlers(id: string, handlers: Handlers): Promise<void> {
const attachments = this.#portAttachments.get(id);
if (attachments) {
for (const {port, target, parent, pointer} of attachments) {
const {local, literals} = await this._createHandlers(handlers, port);
target.importedMap!.set(id, local);
literals.forEach(literal => Object.setPrototypeOf(literal, parent));
Object.setPrototypeOf(local, parent);
Object.setPrototypeOf(pointer, local);
}
}
if (API.test(id)) {
await this.#reload.add(async () => {
this.methods.set(id, handlers);
this.#validations = {};
this.#gateway?.route(await this._validations(), {name: '', version: ''});
await this.#gateway?.start();
});
}
}
private async _createHandlers(
handlers: Handlers,
port: object | undefined,
): Promise<{local: object; literals: object[]}> {
const attachCheckpoint = this.#attachCheckpoint;
function isSafeKey(key: string): boolean {
return key !== '__proto__' && key !== 'constructor' && key !== 'prototype';
}
const lib = {
type: Type,
error: this.#error.register.bind(this.#error),
merge,
ulid,
uuid4,
uuid7,
yaml,
assert: attachCheckpoint ? nodeAssert : undefined,
rename: (object: object, value: string) =>
Object.defineProperty<unknown>(object, 'name', {value}),
group:
(name: string, config?: {autoSnapshot?: boolean; mask?: string[]}) =>
(steps: unknown[]) => {
Object.defineProperty(steps, 'name', {
value: name,
enumerable: false,
writable: true,
});
if (config?.autoSnapshot !== undefined)
Object.defineProperty(steps, 'autoSnapshot', {
value: config.autoSnapshot,
enumerable: false,
writable: true,
});
if (config?.mask !== undefined)
Object.defineProperty(steps, 'mask', {
value: config.mask,
enumerable: false,
writable: true,
});
return steps;
},
checkpoint: (name: string, ...markers: string[]) => {
const arr: string[] = markers.length > 0 ? [...markers] : ['*'];
return Object.assign(arr, {name});
},
timing: this.#platform.timing,
setProperty(obj: Record<string, unknown>, path: string, value: unknown): void {
if (!path) return;
const parts = path.split('.');
let current = obj;
for (let i = 0; i < parts.length - 1; i++) {
const part = parts[i];
if (!isSafeKey(part)) return;
if (current[part] == null || typeof current[part] !== 'object') {
current[part] = {};
}
current = current[part] as Record<string, unknown>;
}
const lastPart = parts[parts.length - 1];
if (isSafeKey(lastPart)) {
current[lastPart] = value;
}
},
};
const local = {};
const literals: object[] = [];
for (const fn of handlers) {
await fn({
lib,
local,
literals,
gateway: this.#gateway,
remote: (name: string) => this.#remote.remote(methodParts(name)),
port,
attachCheckpoint,
apiSchema: this.#apiSchema,
});
}
return {local, literals};
}
public async init(): Promise<IRegistry> {
for (const [id, {source, def}] of Object.entries(this.#config.api || {}))
await this.loadApi(id, def, source);
return this;
}
public async start(configOverride: object): Promise<IRegistry> {
for (const id of Array.from(this.ports.keys())) await this.createPort(id);
for (const port of this.#ports.values()) await port.start(configOverride);
for (const port of this.#ports.values()) await port.ready();
this.#gateway?.route(await this._validations(), {name: '', version: ''});
await this.#resolution?.start();
await this.#rpcServer?.start();
await this.#remote?.start();
await this.#gateway?.start();
await this.#watch?.start(this, this.#remote, configOverride);
return this;
// console.dir(this.ports.entries());
// console.dir(this.#validations);
// console.dir(created);
}
public async connected(): Promise<boolean> {
return (
await Promise.all(
Array.from(this.#ports.values())
.reverse()
.map(port => port.connected?.() ?? port.isConnected),
)
).every(item => item);
}
public async test(framework: unknown): Promise<void> {
return await this.#watch.test(framework);
}
public async stop(): Promise<IRegistry> {
await this.#watch?.stop();
await this.#gateway?.stop();
await this.#remote?.stop();
await this.#rpcServer?.stop();
await this.#resolution?.stop();
for (const port of this.#ports.values()) await port.stop();
return this;
}
public async loadApi(
id: string,
def: {
namespace: Record<string, string | string[]>;
},
source: string,
): Promise<void> {
const api = await this.#apiSchema.schema(def, source);
this.methods.set(id, [({local}) => merge(local, api)]);
}
}
|