@imqueue/rpc 3.2.1 → 3.2.2
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/package.json +1 -1
- package/src/decorators/expose.d.ts +18 -0
- package/src/decorators/expose.js +182 -9
package/package.json
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts method JSDoc blocks from original source text of a given class.
|
|
3
|
+
*
|
|
4
|
+
* The text may be TypeScript (type annotations, generics, modifiers,
|
|
5
|
+
* decorators), which acorn cannot parse — so extraction is textual: the
|
|
6
|
+
* search is narrowed to the class region (from the class declaration to the
|
|
7
|
+
* next class declaration or EOF), and each doc block is attributed to the
|
|
8
|
+
* method whose declaration head immediately follows it. When several blocks
|
|
9
|
+
* precede a method, the closest one wins, matching the runtime parser.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} src - source file text (TypeScript or JavaScript)
|
|
12
|
+
* @param {string} className - class to extract method docs for
|
|
13
|
+
* @return {{ [method: string]: string }} - map of method name to raw JSDoc
|
|
14
|
+
* block body (delimiters excluded, as acorn reports comment values)
|
|
15
|
+
*/
|
|
16
|
+
export declare function parseSourceComments(src: string, className: string): {
|
|
17
|
+
[method: string]: string;
|
|
18
|
+
};
|
|
1
19
|
/**
|
|
2
20
|
* Expose decorator factory. Applied to a service method, it registers that
|
|
3
21
|
* method in the RPC service description. (Complex argument/return types are
|
package/src/decorators/expose.js
CHANGED
|
@@ -21,6 +21,8 @@
|
|
|
21
21
|
* purchase a proprietary commercial license. Please contact us at
|
|
22
22
|
* <support@imqueue.com> to get commercial licensing options.
|
|
23
23
|
*/
|
|
24
|
+
import { readFileSync } from 'fs';
|
|
25
|
+
import { fileURLToPath } from 'url';
|
|
24
26
|
import { parse } from 'acorn';
|
|
25
27
|
import { IMQRPCDescription, } from '../index.js';
|
|
26
28
|
const descriptions = {};
|
|
@@ -29,10 +31,23 @@ const RX_COMMA_SPLIT = /\s*,\s*/;
|
|
|
29
31
|
const RX_MULTILINE_CLEANUP = /\*?\n +\* ?/g;
|
|
30
32
|
const RX_DESCRIPTION = /^([\s\S]*?)@/;
|
|
31
33
|
const RX_TAG = /(@[^@]+)/g;
|
|
32
|
-
const RX_TYPE = /\{([\s\S]+)\}/;
|
|
33
34
|
const RX_LI_CLEANUP = /^\s*-\s*/;
|
|
34
35
|
const RX_SPACE_SPLIT = / /;
|
|
35
36
|
const RX_OPTIONAL = /^\[(.*?)\]$/;
|
|
37
|
+
// member head that may follow a doc block in original (possibly TypeScript)
|
|
38
|
+
// source: optional decorators and modifiers, then the method name right
|
|
39
|
+
// before its (possibly generic) argument list
|
|
40
|
+
const RX_SOURCE_METHOD = /^\s*(?:@[\w$.]+(?:\([^)]*\))?\s*)*(?:(?:public|protected|private|static|override|abstract|async)\s+)*([\w$]+)\s*(?:<[^>]*>)?\s*\(/;
|
|
41
|
+
const RX_JSDOC_BLOCK = /\/\*\*([\s\S]*?)\*\//g;
|
|
42
|
+
// path of this very module — used to skip own frames during caller capture
|
|
43
|
+
const OWN_PATH = (() => {
|
|
44
|
+
try {
|
|
45
|
+
return fileURLToPath(import.meta.url);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return '';
|
|
49
|
+
}
|
|
50
|
+
})();
|
|
36
51
|
/**
|
|
37
52
|
* Lookup and returns a list of argument names for a given function
|
|
38
53
|
*
|
|
@@ -47,6 +62,37 @@ function argumentNames(fn) {
|
|
|
47
62
|
.map(arg => arg.trim().replace(RX_ARG_NAMES, '$1'))
|
|
48
63
|
.filter(arg => arg);
|
|
49
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Extracts a leading JSDoc `{type}` from a tag definition. Braces are matched
|
|
67
|
+
* by depth, so object-literal types (e.g. `{{ x: number }}`) are captured whole
|
|
68
|
+
* and a later `}` in the description cannot extend the captured type. The type,
|
|
69
|
+
* if present, must be the leading token of the definition.
|
|
70
|
+
*
|
|
71
|
+
* @param {string} tagDef - tag definition text following the tag name
|
|
72
|
+
* @return {{ tsType: string, rest: string } | null} - captured type and the
|
|
73
|
+
* remaining definition (name + description), or null when there is no type
|
|
74
|
+
*/
|
|
75
|
+
function extractType(tagDef) {
|
|
76
|
+
const start = tagDef.indexOf('{');
|
|
77
|
+
// a type is only a type when it leads the definition; a `{` preceded by
|
|
78
|
+
// any non-whitespace belongs to the description, not to a type
|
|
79
|
+
if (start === -1 || tagDef.slice(0, start).trim() !== '') {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
let depth = 0;
|
|
83
|
+
for (let i = start; i < tagDef.length; i++) {
|
|
84
|
+
if (tagDef[i] === '{') {
|
|
85
|
+
depth++;
|
|
86
|
+
}
|
|
87
|
+
else if (tagDef[i] === '}' && --depth === 0) {
|
|
88
|
+
return {
|
|
89
|
+
tsType: tagDef.slice(start + 1, i),
|
|
90
|
+
rest: tagDef.slice(i + 1).trim(),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return null; // unbalanced braces — treat as having no parseable type
|
|
95
|
+
}
|
|
50
96
|
/**
|
|
51
97
|
* Parses given multi-line comment block
|
|
52
98
|
*
|
|
@@ -73,13 +119,12 @@ function parseComment(src) {
|
|
|
73
119
|
let parts = tag.split(RX_SPACE_SPLIT);
|
|
74
120
|
let tagName = parts.shift();
|
|
75
121
|
let tagDef = parts.join(' ');
|
|
76
|
-
let
|
|
122
|
+
let typeInfo = extractType(tagDef);
|
|
77
123
|
let tsType = '', name = '', description = '', type = '';
|
|
78
124
|
let isOptional = false;
|
|
79
|
-
if (
|
|
80
|
-
tsType =
|
|
81
|
-
|
|
82
|
-
parts = tagDef.split(/ /);
|
|
125
|
+
if (typeInfo) {
|
|
126
|
+
tsType = typeInfo.tsType;
|
|
127
|
+
parts = typeInfo.rest.split(/ /);
|
|
83
128
|
}
|
|
84
129
|
name = (parts.shift() || '').replace(RX_LI_CLEANUP, '');
|
|
85
130
|
description = parts.join(' ').replace(RX_LI_CLEANUP, '');
|
|
@@ -188,6 +233,124 @@ function parseDescriptions(name, src) {
|
|
|
188
233
|
}
|
|
189
234
|
}
|
|
190
235
|
}
|
|
236
|
+
/**
|
|
237
|
+
* Extracts method JSDoc blocks from original source text of a given class.
|
|
238
|
+
*
|
|
239
|
+
* The text may be TypeScript (type annotations, generics, modifiers,
|
|
240
|
+
* decorators), which acorn cannot parse — so extraction is textual: the
|
|
241
|
+
* search is narrowed to the class region (from the class declaration to the
|
|
242
|
+
* next class declaration or EOF), and each doc block is attributed to the
|
|
243
|
+
* method whose declaration head immediately follows it. When several blocks
|
|
244
|
+
* precede a method, the closest one wins, matching the runtime parser.
|
|
245
|
+
*
|
|
246
|
+
* @param {string} src - source file text (TypeScript or JavaScript)
|
|
247
|
+
* @param {string} className - class to extract method docs for
|
|
248
|
+
* @return {{ [method: string]: string }} - map of method name to raw JSDoc
|
|
249
|
+
* block body (delimiters excluded, as acorn reports comment values)
|
|
250
|
+
*/
|
|
251
|
+
export function parseSourceComments(src, className) {
|
|
252
|
+
const comments = {};
|
|
253
|
+
const classRx = new RegExp(`\\bclass\\s+${className}\\b`);
|
|
254
|
+
const classMatch = classRx.exec(src);
|
|
255
|
+
if (!classMatch) {
|
|
256
|
+
return comments;
|
|
257
|
+
}
|
|
258
|
+
const start = classMatch.index + classMatch[0].length;
|
|
259
|
+
const nextClass = /\bclass\s+[\w$]+/g;
|
|
260
|
+
nextClass.lastIndex = start;
|
|
261
|
+
const next = nextClass.exec(src);
|
|
262
|
+
const region = src.slice(start, next ? next.index : src.length);
|
|
263
|
+
let match;
|
|
264
|
+
RX_JSDOC_BLOCK.lastIndex = 0;
|
|
265
|
+
while ((match = RX_JSDOC_BLOCK.exec(region))) {
|
|
266
|
+
const head = region
|
|
267
|
+
.slice(match.index + match[0].length)
|
|
268
|
+
.match(RX_SOURCE_METHOD);
|
|
269
|
+
if (head && head[1] !== 'constructor') {
|
|
270
|
+
comments[head[1]] = match[1];
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return comments;
|
|
274
|
+
}
|
|
275
|
+
// per-path cache of source file reads used by the dev-mode fallback;
|
|
276
|
+
// null marks an unreadable path so it is only attempted once
|
|
277
|
+
const sourceFileCache = {};
|
|
278
|
+
/**
|
|
279
|
+
* Reads and caches a source file, returning null when unreadable.
|
|
280
|
+
*
|
|
281
|
+
* @param {string} path - absolute source file path
|
|
282
|
+
* @return {string | null}
|
|
283
|
+
*/
|
|
284
|
+
function readSourceFile(path) {
|
|
285
|
+
if (!(path in sourceFileCache)) {
|
|
286
|
+
try {
|
|
287
|
+
sourceFileCache[path] = readFileSync(path, 'utf8');
|
|
288
|
+
}
|
|
289
|
+
catch {
|
|
290
|
+
sourceFileCache[path] = null;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return sourceFileCache[path];
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Captures the file path of the code that invoked the expose() factory —
|
|
297
|
+
* physically the module defining the decorated class. Own and internal
|
|
298
|
+
* frames are skipped. Returns an empty string when no frame is usable
|
|
299
|
+
* (e.g. bundled builds without source maps).
|
|
300
|
+
*
|
|
301
|
+
* @return {string}
|
|
302
|
+
*/
|
|
303
|
+
function captureCallerPath() {
|
|
304
|
+
const stack = new Error().stack || '';
|
|
305
|
+
for (const line of stack.split('\n').slice(1)) {
|
|
306
|
+
const match = line.match(/\(?((?:file:\/\/)?[^()\s]+?\.[cm]?[jt]sx?)(?::\d+){0,2}\)?$/);
|
|
307
|
+
if (!match) {
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
let path = match[1];
|
|
311
|
+
if (path.startsWith('file://')) {
|
|
312
|
+
try {
|
|
313
|
+
path = fileURLToPath(path);
|
|
314
|
+
}
|
|
315
|
+
catch {
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
if (path === OWN_PATH ||
|
|
320
|
+
path.startsWith('node:') ||
|
|
321
|
+
path.includes('node_modules')) {
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
return path;
|
|
325
|
+
}
|
|
326
|
+
return '';
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Dev-mode fallback: when the runtime class source yields no JSDoc (a
|
|
330
|
+
* comment-stripping transpiler like tsx/esbuild), re-extract method docs
|
|
331
|
+
* from the defining source file on disk. Runtime source always wins when
|
|
332
|
+
* it carries any JSDoc; extraction misses leave methods at their current
|
|
333
|
+
* (untyped) state, so behavior never degrades below the status quo.
|
|
334
|
+
*
|
|
335
|
+
* @param {string} className - class to describe
|
|
336
|
+
* @param {string} sourcePath - captured defining file path
|
|
337
|
+
*/
|
|
338
|
+
function applySourceFallback(className, sourcePath) {
|
|
339
|
+
const hasJsdoc = Object.keys(descriptions[className] || {}).some(key => key !== 'inherits');
|
|
340
|
+
if (hasJsdoc || !sourcePath) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
const src = readSourceFile(sourcePath);
|
|
344
|
+
if (!src) {
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
const comments = parseSourceComments(src, className);
|
|
348
|
+
for (const method of Object.keys(comments)) {
|
|
349
|
+
descriptions[className][method] = {
|
|
350
|
+
comment: parseComment(comments[method]),
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
}
|
|
191
354
|
/**
|
|
192
355
|
* Helper function to make easy descriptions parts extractions
|
|
193
356
|
*
|
|
@@ -217,12 +380,18 @@ function get(prop, className, method, defaults) {
|
|
|
217
380
|
* @param {Function} ctor - class that declares the method
|
|
218
381
|
* @param {string} methodName - exposed method name
|
|
219
382
|
* @param {(...args: any[]) => any} fn - the method implementation
|
|
383
|
+
* @param {string} [sourcePath] - defining file path captured at decoration
|
|
220
384
|
*/
|
|
221
|
-
function buildMethodDescription(ctor, methodName, fn) {
|
|
385
|
+
function buildMethodDescription(ctor, methodName, fn, sourcePath) {
|
|
222
386
|
const className = ctor.name;
|
|
223
387
|
const argNames = argumentNames(fn);
|
|
224
388
|
if (!descriptions[className]) {
|
|
225
389
|
parseDescriptions(className, ctor.toString());
|
|
390
|
+
// dev-mode: a comment-stripping transpiler leaves no JSDoc in the
|
|
391
|
+
// runtime source — recover method docs from the file on disk
|
|
392
|
+
if (sourcePath) {
|
|
393
|
+
applySourceFallback(className, sourcePath);
|
|
394
|
+
}
|
|
226
395
|
}
|
|
227
396
|
const args = get('params', className, methodName, []);
|
|
228
397
|
const ret = get('returns', className, methodName, {
|
|
@@ -275,6 +444,10 @@ function buildMethodDescription(ctor, methodName, fn) {
|
|
|
275
444
|
* @return {(value: any, context: ClassMethodDecoratorContext) => void}
|
|
276
445
|
*/
|
|
277
446
|
export function expose() {
|
|
447
|
+
// the factory executes at class-definition time inside the defining
|
|
448
|
+
// module, so the caller frame points at the service source file — used
|
|
449
|
+
// as the dev-mode fallback when runtime comments are stripped
|
|
450
|
+
const sourcePath = captureCallerPath();
|
|
278
451
|
// Dual-mode: standard (TC39) invocations pass a context object with a
|
|
279
452
|
// `kind` property; legacy ones pass (target, propertyKey, descriptor).
|
|
280
453
|
return function exposeDecorator(target, context, descriptor) {
|
|
@@ -295,13 +468,13 @@ export function expose() {
|
|
|
295
468
|
const ctor = proto
|
|
296
469
|
? proto.constructor
|
|
297
470
|
: this.constructor;
|
|
298
|
-
buildMethodDescription(ctor, methodName, value);
|
|
471
|
+
buildMethodDescription(ctor, methodName, value, sourcePath);
|
|
299
472
|
});
|
|
300
473
|
return;
|
|
301
474
|
}
|
|
302
475
|
// legacy: `target` is the prototype, so its constructor is the class
|
|
303
476
|
// that declares the method — available immediately at decoration time
|
|
304
|
-
buildMethodDescription(target.constructor, String(context), descriptor.value);
|
|
477
|
+
buildMethodDescription(target.constructor, String(context), descriptor.value, sourcePath);
|
|
305
478
|
return descriptor;
|
|
306
479
|
};
|
|
307
480
|
}
|