apcore-js 0.4.0 → 0.5.0
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.
- package/CHANGELOG.md +32 -1
- package/package.json +1 -1
- package/src/async-task.ts +267 -0
- package/src/cancel.ts +32 -0
- package/src/context.ts +83 -1
- package/src/errors.ts +4 -0
- package/src/executor.ts +18 -0
- package/src/extensions.ts +265 -0
- package/src/index.ts +17 -3
- package/src/middleware/manager.ts +1 -1
- package/src/observability/tracing.ts +69 -5
- package/src/registry/index.ts +1 -0
- package/src/registry/registry.ts +224 -4
- package/src/trace-context.ts +102 -0
- package/tests/async-task.test.ts +335 -0
- package/tests/observability/test-tracing.test.ts +173 -1
- package/tests/registry/test-registry.test.ts +389 -0
- package/tests/test-cancel.test.ts +71 -0
- package/tests/test-context.test.ts +115 -0
- package/tests/test-extensions.test.ts +310 -0
- package/tests/test-trace-context.test.ts +251 -0
package/src/registry/registry.ts
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
* Central module registry for discovering, registering, and querying modules.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { watch as fsWatch, accessSync } from 'node:fs';
|
|
6
|
+
import { resolve, join, basename, extname } from 'node:path';
|
|
6
7
|
import type { Config } from '../config.js';
|
|
7
8
|
import { InvalidInputError, ModuleNotFoundError } from '../errors.js';
|
|
8
9
|
import type { ModuleAnnotations, ModuleExample } from '../module.js';
|
|
@@ -27,6 +28,30 @@ export const REGISTRY_EVENTS = Object.freeze({
|
|
|
27
28
|
*/
|
|
28
29
|
export const MODULE_ID_PATTERN = /^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)*$/;
|
|
29
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Maximum allowed length for a module ID.
|
|
33
|
+
*/
|
|
34
|
+
export const MAX_MODULE_ID_LENGTH = 128;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Reserved words that cannot appear as any segment of a module ID.
|
|
38
|
+
*/
|
|
39
|
+
export const RESERVED_WORDS = new Set(['system', 'internal', 'core', 'apcore', 'plugin', 'schema', 'acl']);
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Interface for custom module discovery.
|
|
43
|
+
*/
|
|
44
|
+
export interface Discoverer {
|
|
45
|
+
discover(roots: string[]): Array<{ moduleId: string; module: unknown }> | Promise<Array<{ moduleId: string; module: unknown }>>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Interface for custom module validation.
|
|
50
|
+
*/
|
|
51
|
+
export interface ModuleValidator {
|
|
52
|
+
validate(module: unknown): string[] | Promise<string[]>;
|
|
53
|
+
}
|
|
54
|
+
|
|
30
55
|
type EventCallback = (moduleId: string, module: unknown) => void;
|
|
31
56
|
|
|
32
57
|
export class Registry {
|
|
@@ -40,6 +65,10 @@ export class Registry {
|
|
|
40
65
|
private _idMap: Record<string, Record<string, unknown>> = {};
|
|
41
66
|
private _schemaCache: Map<string, Record<string, unknown>> = new Map();
|
|
42
67
|
private _config: Config | null;
|
|
68
|
+
private _watchers?: Array<{ close(): void }>;
|
|
69
|
+
private _debounceTimers?: Map<string, number>;
|
|
70
|
+
private _customDiscoverer: Discoverer | null = null;
|
|
71
|
+
private _customValidator: ModuleValidator | null = null;
|
|
43
72
|
|
|
44
73
|
constructor(options?: {
|
|
45
74
|
config?: Config | null;
|
|
@@ -76,13 +105,58 @@ export class Registry {
|
|
|
76
105
|
}
|
|
77
106
|
}
|
|
78
107
|
|
|
108
|
+
setDiscoverer(discoverer: Discoverer): void {
|
|
109
|
+
this._customDiscoverer = discoverer;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
setValidator(validator: ModuleValidator): void {
|
|
113
|
+
this._customValidator = validator;
|
|
114
|
+
}
|
|
115
|
+
|
|
79
116
|
async discover(): Promise<number> {
|
|
117
|
+
if (this._customDiscoverer !== null) {
|
|
118
|
+
return this._discoverCustom();
|
|
119
|
+
}
|
|
120
|
+
return this._discoverDefault();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private async _discoverCustom(): Promise<number> {
|
|
124
|
+
const rootPaths = this._extensionRoots.map((r) => r['root'] as string);
|
|
125
|
+
const customModules = await this._customDiscoverer!.discover(rootPaths);
|
|
126
|
+
|
|
127
|
+
let count = 0;
|
|
128
|
+
for (const entry of customModules) {
|
|
129
|
+
const { moduleId, module: mod } = entry;
|
|
130
|
+
|
|
131
|
+
// Apply custom validator if set
|
|
132
|
+
if (this._customValidator !== null) {
|
|
133
|
+
const errors = await this._customValidator.validate(mod);
|
|
134
|
+
if (errors.length > 0) {
|
|
135
|
+
console.warn(
|
|
136
|
+
`[apcore:registry] Custom validator rejected module '${moduleId}': ${errors.join('; ')}`,
|
|
137
|
+
);
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
this.register(moduleId, mod);
|
|
144
|
+
count++;
|
|
145
|
+
} catch (e) {
|
|
146
|
+
console.warn(`[apcore:registry] Failed to register custom-discovered module '${moduleId}':`, e);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return count;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private async _discoverDefault(): Promise<number> {
|
|
80
154
|
const discovered = this._scanRoots();
|
|
81
155
|
this._applyIdMapOverrides(discovered);
|
|
82
156
|
|
|
83
157
|
const rawMetadata = this._loadAllMetadata(discovered);
|
|
84
158
|
const resolvedModules = await this._resolveAllEntryPoints(discovered, rawMetadata);
|
|
85
|
-
const validModules = this._validateAll(resolvedModules);
|
|
159
|
+
const validModules = await this._validateAll(resolvedModules);
|
|
86
160
|
const loadOrder = this._resolveLoadOrder(validModules, rawMetadata);
|
|
87
161
|
|
|
88
162
|
return this._registerInOrder(loadOrder, validModules, rawMetadata);
|
|
@@ -152,10 +226,15 @@ export class Registry {
|
|
|
152
226
|
return resolvedModules;
|
|
153
227
|
}
|
|
154
228
|
|
|
155
|
-
private _validateAll(resolvedModules: Map<string, unknown>): Map<string, unknown
|
|
229
|
+
private async _validateAll(resolvedModules: Map<string, unknown>): Promise<Map<string, unknown>> {
|
|
156
230
|
const validModules = new Map<string, unknown>();
|
|
157
231
|
for (const [modId, mod] of resolvedModules) {
|
|
158
|
-
if (
|
|
232
|
+
if (this._customValidator !== null) {
|
|
233
|
+
const errors = await this._customValidator.validate(mod);
|
|
234
|
+
if (errors.length === 0) {
|
|
235
|
+
validModules.set(modId, mod);
|
|
236
|
+
}
|
|
237
|
+
} else if (validateModule(mod).length === 0) {
|
|
159
238
|
validModules.set(modId, mod);
|
|
160
239
|
}
|
|
161
240
|
}
|
|
@@ -217,6 +296,16 @@ export class Registry {
|
|
|
217
296
|
);
|
|
218
297
|
}
|
|
219
298
|
|
|
299
|
+
const parts = moduleId.split('.');
|
|
300
|
+
for (const part of parts) {
|
|
301
|
+
if (RESERVED_WORDS.has(part)) {
|
|
302
|
+
throw new InvalidInputError(`Module ID contains reserved word: '${part}'`);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
if (moduleId.length > MAX_MODULE_ID_LENGTH) {
|
|
306
|
+
throw new InvalidInputError(`Module ID exceeds maximum length of ${MAX_MODULE_ID_LENGTH}: ${moduleId.length}`);
|
|
307
|
+
}
|
|
308
|
+
|
|
220
309
|
if (this._modules.has(moduleId)) {
|
|
221
310
|
throw new InvalidInputError(`Module already exists: ${moduleId}`);
|
|
222
311
|
}
|
|
@@ -333,6 +422,49 @@ export class Registry {
|
|
|
333
422
|
};
|
|
334
423
|
}
|
|
335
424
|
|
|
425
|
+
describe(moduleId: string): string {
|
|
426
|
+
const module = this.get(moduleId);
|
|
427
|
+
if (module === null) {
|
|
428
|
+
throw new ModuleNotFoundError(moduleId);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// Check for custom describe method
|
|
432
|
+
const modObj = module as Record<string, unknown>;
|
|
433
|
+
if (typeof modObj['describe'] === 'function') {
|
|
434
|
+
return (modObj['describe'] as () => string)();
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// Auto-generate from descriptor
|
|
438
|
+
const descriptor = this.getDefinition(moduleId);
|
|
439
|
+
if (descriptor === null) {
|
|
440
|
+
return `Module: ${moduleId}\n\nNo description available.`;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
const lines: string[] = [`# ${descriptor.moduleId}`];
|
|
444
|
+
if (descriptor.description) {
|
|
445
|
+
lines.push(`\n${descriptor.description}`);
|
|
446
|
+
}
|
|
447
|
+
if (descriptor.tags.length > 0) {
|
|
448
|
+
lines.push(`\n**Tags:** ${descriptor.tags.join(', ')}`);
|
|
449
|
+
}
|
|
450
|
+
const props = descriptor.inputSchema['properties'] as Record<string, Record<string, unknown>> | undefined;
|
|
451
|
+
if (props && Object.keys(props).length > 0) {
|
|
452
|
+
lines.push('\n**Parameters:**');
|
|
453
|
+
const requiredFields = (descriptor.inputSchema['required'] as string[]) ?? [];
|
|
454
|
+
for (const [param, schema] of Object.entries(props)) {
|
|
455
|
+
const paramType = (schema['type'] as string) ?? 'any';
|
|
456
|
+
const paramDesc = (schema['description'] as string) ?? '';
|
|
457
|
+
const isRequired = requiredFields.includes(param);
|
|
458
|
+
const reqMarker = isRequired ? ' (required)' : '';
|
|
459
|
+
lines.push(`- \`${param}\` (${paramType})${reqMarker}: ${paramDesc}`);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
if (descriptor.documentation) {
|
|
463
|
+
lines.push(`\n**Documentation:**\n${descriptor.documentation}`);
|
|
464
|
+
}
|
|
465
|
+
return lines.join('\n');
|
|
466
|
+
}
|
|
467
|
+
|
|
336
468
|
on(event: string, callback: EventCallback): void {
|
|
337
469
|
const validEvents = Object.values(REGISTRY_EVENTS) as string[];
|
|
338
470
|
if (!validEvents.includes(event)) {
|
|
@@ -354,6 +486,94 @@ export class Registry {
|
|
|
354
486
|
}
|
|
355
487
|
}
|
|
356
488
|
|
|
489
|
+
watch(): void {
|
|
490
|
+
if (this._watchers && this._watchers.length > 0) {
|
|
491
|
+
return; // Already watching
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
this._watchers = [];
|
|
495
|
+
this._debounceTimers = new Map<string, number>();
|
|
496
|
+
|
|
497
|
+
for (const root of this._extensionRoots) {
|
|
498
|
+
const rootPath = typeof root === "string" ? root : (root as Record<string, unknown>).root as string;
|
|
499
|
+
if (!rootPath) continue;
|
|
500
|
+
|
|
501
|
+
try {
|
|
502
|
+
const watcher = fsWatch(rootPath, { recursive: true }, (eventType: string, filename: string | null) => {
|
|
503
|
+
if (!filename) return;
|
|
504
|
+
if (!filename.endsWith(".ts") && !filename.endsWith(".js")) return;
|
|
505
|
+
|
|
506
|
+
const fullPath = join(rootPath, filename);
|
|
507
|
+
const now = Date.now();
|
|
508
|
+
const last = this._debounceTimers?.get(fullPath) ?? 0;
|
|
509
|
+
if (now - last < 300) return;
|
|
510
|
+
this._debounceTimers?.set(fullPath, now);
|
|
511
|
+
|
|
512
|
+
if (eventType === "rename") {
|
|
513
|
+
// Could be create or delete
|
|
514
|
+
try {
|
|
515
|
+
accessSync(fullPath);
|
|
516
|
+
this._handleFileChange(fullPath);
|
|
517
|
+
} catch {
|
|
518
|
+
this._handleFileDeletion(fullPath);
|
|
519
|
+
}
|
|
520
|
+
} else {
|
|
521
|
+
this._handleFileChange(fullPath);
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
this._watchers.push(watcher);
|
|
525
|
+
} catch {
|
|
526
|
+
// Skip directories that don't exist
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
unwatch(): void {
|
|
532
|
+
if (this._watchers) {
|
|
533
|
+
for (const watcher of this._watchers) {
|
|
534
|
+
watcher.close();
|
|
535
|
+
}
|
|
536
|
+
this._watchers = [];
|
|
537
|
+
}
|
|
538
|
+
this._debounceTimers = undefined;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
private _handleFileChange(filePath: string): void {
|
|
542
|
+
const moduleId = this._pathToModuleId(filePath);
|
|
543
|
+
|
|
544
|
+
if (moduleId && this.has(moduleId)) {
|
|
545
|
+
const oldModule = this.get(moduleId) as Record<string, unknown> | null;
|
|
546
|
+
if (oldModule && typeof oldModule.onUnload === "function") {
|
|
547
|
+
try { oldModule.onUnload(); } catch { /* ignore */ }
|
|
548
|
+
}
|
|
549
|
+
this.unregister(moduleId);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
// Re-import is complex in ES modules - emit event for user to handle
|
|
553
|
+
this._triggerEvent("register", moduleId ?? basename(filePath, extname(filePath)), null);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
private _handleFileDeletion(path: string): void {
|
|
557
|
+
const moduleId = this._pathToModuleId(path);
|
|
558
|
+
if (moduleId && this.has(moduleId)) {
|
|
559
|
+
const module = this.get(moduleId) as Record<string, unknown> | null;
|
|
560
|
+
if (module && typeof module.onUnload === "function") {
|
|
561
|
+
try { module.onUnload(); } catch { /* ignore */ }
|
|
562
|
+
}
|
|
563
|
+
this.unregister(moduleId);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
private _pathToModuleId(filePath: string): string | null {
|
|
568
|
+
const base = basename(filePath, extname(filePath));
|
|
569
|
+
for (const mid of this.moduleIds) {
|
|
570
|
+
if (mid.endsWith(base) || mid === base) {
|
|
571
|
+
return mid;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
return null;
|
|
575
|
+
}
|
|
576
|
+
|
|
357
577
|
clearCache(): void {
|
|
358
578
|
this._schemaCache.clear();
|
|
359
579
|
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* W3C Trace Context support: TraceParent parsing and TraceContext injection/extraction.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { randomBytes } from 'node:crypto';
|
|
6
|
+
import type { Context } from './context.js';
|
|
7
|
+
import type { Span } from './observability/tracing.js';
|
|
8
|
+
|
|
9
|
+
const TRACEPARENT_RE = /^([0-9a-f]{2})-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})$/;
|
|
10
|
+
|
|
11
|
+
export interface TraceParent {
|
|
12
|
+
readonly version: string; // "00"
|
|
13
|
+
readonly traceId: string; // 32 lowercase hex chars
|
|
14
|
+
readonly parentId: string; // 16 lowercase hex chars
|
|
15
|
+
readonly traceFlags: string; // "01" (sampled) or "00"
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class TraceContext {
|
|
19
|
+
/**
|
|
20
|
+
* Build a traceparent header dict from an apcore Context.
|
|
21
|
+
*
|
|
22
|
+
* Converts `context.traceId` (UUID with dashes) to the 32-hex
|
|
23
|
+
* format required by the W3C traceparent spec. Uses the last span's
|
|
24
|
+
* `spanId` from the tracing stack if available, otherwise generates
|
|
25
|
+
* a random 16-hex parent id.
|
|
26
|
+
*/
|
|
27
|
+
static inject(context: Context): Record<string, string> {
|
|
28
|
+
const traceIdHex = context.traceId.replace(/-/g, '');
|
|
29
|
+
|
|
30
|
+
const spansStack = context.data['_tracing_spans'] as Span[] | undefined;
|
|
31
|
+
let parentId: string;
|
|
32
|
+
if (spansStack && spansStack.length > 0) {
|
|
33
|
+
parentId = spansStack[spansStack.length - 1].spanId;
|
|
34
|
+
} else {
|
|
35
|
+
parentId = randomBytes(8).toString('hex');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const traceparent = `00-${traceIdHex}-${parentId}-01`;
|
|
39
|
+
return { traceparent };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Parse the `traceparent` header from the given headers object.
|
|
44
|
+
*
|
|
45
|
+
* Returns `null` if the header is missing or malformed.
|
|
46
|
+
*/
|
|
47
|
+
static extract(headers: Record<string, string>): TraceParent | null {
|
|
48
|
+
const raw = headers['traceparent'];
|
|
49
|
+
if (raw === undefined) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
const match = TRACEPARENT_RE.exec(raw.trim().toLowerCase());
|
|
53
|
+
if (match === null) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
const version = match[1];
|
|
57
|
+
const traceId = match[2];
|
|
58
|
+
const parentId = match[3];
|
|
59
|
+
if (version === 'ff') {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
if (traceId === '0'.repeat(32) || parentId === '0'.repeat(16)) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
return Object.freeze({
|
|
66
|
+
version,
|
|
67
|
+
traceId,
|
|
68
|
+
parentId,
|
|
69
|
+
traceFlags: match[4],
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Strictly parse a traceparent string, throwing on invalid format.
|
|
75
|
+
*
|
|
76
|
+
* @throws {Error} If the traceparent does not match the expected
|
|
77
|
+
* `00-<32 hex>-<16 hex>-<2 hex>` format.
|
|
78
|
+
*/
|
|
79
|
+
static fromTraceparent(traceparent: string): TraceParent {
|
|
80
|
+
const match = TRACEPARENT_RE.exec(traceparent.trim().toLowerCase());
|
|
81
|
+
if (match === null) {
|
|
82
|
+
throw new Error(
|
|
83
|
+
`Malformed traceparent: ${JSON.stringify(traceparent.slice(0, 100))}. Expected format: 00-<32 hex>-<16 hex>-<2 hex>`,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
const version = match[1];
|
|
87
|
+
const traceId = match[2];
|
|
88
|
+
const parentId = match[3];
|
|
89
|
+
if (version === 'ff') {
|
|
90
|
+
throw new Error('Invalid traceparent: version ff is not allowed');
|
|
91
|
+
}
|
|
92
|
+
if (traceId === '0'.repeat(32) || parentId === '0'.repeat(16)) {
|
|
93
|
+
throw new Error('Invalid traceparent: all-zero trace_id or parent_id');
|
|
94
|
+
}
|
|
95
|
+
return Object.freeze({
|
|
96
|
+
version,
|
|
97
|
+
traceId,
|
|
98
|
+
parentId,
|
|
99
|
+
traceFlags: match[4],
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|