@source-repo/rpc-cli 4.4.0 → 4.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/dist/index.js +40 -2
- package/dist/index.js.map +1 -1
- package/dist/mcp.d.ts.map +1 -1
- package/dist/mcp.js +11 -3
- package/dist/mcp.js.map +1 -1
- package/dist/strip.d.ts +25 -0
- package/dist/strip.d.ts.map +1 -0
- package/dist/strip.js +124 -0
- package/dist/strip.js.map +1 -0
- package/dist/web/app.js.map +1 -1
- package/package.json +2 -2
package/dist/strip.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { Project, SyntaxKind } from 'ts-morph';
|
|
2
|
+
/**
|
|
3
|
+
* The `{ ... }` a decorator carried, or the empty options a bare `@rpc` means. Collapsed onto one
|
|
4
|
+
* line, because the mark is inserted on the class's closing-brace line and must not add newlines.
|
|
5
|
+
*/
|
|
6
|
+
const optionsTextOf = (decorator) => {
|
|
7
|
+
if (!decorator.isDecoratorFactory())
|
|
8
|
+
return '{}';
|
|
9
|
+
const [options] = decorator.getArguments();
|
|
10
|
+
return options?.getText().replace(/\s*\n\s*/g, ' ') ?? '{}';
|
|
11
|
+
};
|
|
12
|
+
/** A method name as an object key: bare when it is a usable identifier, quoted when not. */
|
|
13
|
+
const asKey = (name) => (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name) ? name : JSON.stringify(name));
|
|
14
|
+
export const stripSource = (source, fileName = 'script.ts') => {
|
|
15
|
+
const project = new Project({ useInMemoryFileSystem: true, compilerOptions: { allowJs: false } });
|
|
16
|
+
const file = project.createSourceFile(fileName, source);
|
|
17
|
+
const problems = [];
|
|
18
|
+
/** [start, end) spans to blank out. */
|
|
19
|
+
const spans = [];
|
|
20
|
+
/**
|
|
21
|
+
* Mark calls inserted at the class's closing brace, not appended at the end of the file: a
|
|
22
|
+
* script exposes its class somewhere below the declaration, and marks that ran after the whole
|
|
23
|
+
* file would run after the expose they exist to inform. On the brace's own line they run the
|
|
24
|
+
* statement after the class exists and add no newline, so nothing below moves.
|
|
25
|
+
*/
|
|
26
|
+
const insertions = [];
|
|
27
|
+
let needsNamespace = false;
|
|
28
|
+
let needsMethods = false;
|
|
29
|
+
const handled = new Set();
|
|
30
|
+
const stripClass = (declaration) => {
|
|
31
|
+
const className = declaration.getName();
|
|
32
|
+
const where = className ?? '(anonymous class)';
|
|
33
|
+
const marks = [];
|
|
34
|
+
for (const decorator of declaration.getDecorators()) {
|
|
35
|
+
if (decorator.getName() !== 'rpcNamespace')
|
|
36
|
+
continue;
|
|
37
|
+
handled.add(decorator);
|
|
38
|
+
if (!className) {
|
|
39
|
+
problems.push({ where, reason: 'an anonymous class cannot be re-marked by name - give it one' });
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
const argumentsText = decorator
|
|
43
|
+
.getArguments()
|
|
44
|
+
.map((argument) => argument.getText().replace(/\s*\n\s*/g, ' '))
|
|
45
|
+
.join(', ');
|
|
46
|
+
spans.push([decorator.getStart(), decorator.getEnd()]);
|
|
47
|
+
marks.push(`__rpcNamespace(${className}, ${argumentsText})`);
|
|
48
|
+
needsNamespace = true;
|
|
49
|
+
}
|
|
50
|
+
const methods = [];
|
|
51
|
+
for (const method of declaration.getMethods()) {
|
|
52
|
+
for (const decorator of method.getDecorators()) {
|
|
53
|
+
if (decorator.getName() !== 'rpc')
|
|
54
|
+
continue;
|
|
55
|
+
handled.add(decorator);
|
|
56
|
+
if (!className) {
|
|
57
|
+
problems.push({ where: `${where}.${method.getName()}`, reason: 'an anonymous class cannot be re-marked by name - give it one' });
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (method.isStatic()) {
|
|
61
|
+
// The decorator would have thrown at runtime; stripping must not launder it.
|
|
62
|
+
problems.push({ where: `${className}.${method.getName()}`, reason: '@rpc: static methods cannot be exposed' });
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
spans.push([decorator.getStart(), decorator.getEnd()]);
|
|
66
|
+
methods.push(`${asKey(method.getName())}: ${optionsTextOf(decorator)}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (methods.length && className) {
|
|
70
|
+
marks.push(`__rpcMethods(${className}, { ${methods.join(', ')} })`);
|
|
71
|
+
needsMethods = true;
|
|
72
|
+
}
|
|
73
|
+
if (marks.length)
|
|
74
|
+
insertions.push({ at: declaration.getEnd(), text: `; ${marks.join('; ')}` });
|
|
75
|
+
};
|
|
76
|
+
// Only classes that are statements of the file itself: a class inside a function would need
|
|
77
|
+
// its marks inside that function, and appending them at top level would reference a name that
|
|
78
|
+
// does not exist there. Reported below when such a class carries our decorators.
|
|
79
|
+
for (const declaration of file.getClasses())
|
|
80
|
+
stripClass(declaration);
|
|
81
|
+
// Every decorator in the file has to be one we replaced: an unknown one has a runtime effect
|
|
82
|
+
// this cannot reproduce, and one on a nested class would lose its mark silently.
|
|
83
|
+
for (const decorator of file.getDescendantsOfKind(SyntaxKind.Decorator)) {
|
|
84
|
+
if (handled.has(decorator))
|
|
85
|
+
continue;
|
|
86
|
+
const name = decorator.getName();
|
|
87
|
+
problems.push({
|
|
88
|
+
where: decorator.getFirstAncestorByKind(SyntaxKind.ClassDeclaration)?.getName() ?? name,
|
|
89
|
+
reason: name === 'rpc' || name === 'rpcNamespace'
|
|
90
|
+
? `@${name} sits on something strip cannot re-mark from the top level - only methods of classes declared as statements of the file are handled`
|
|
91
|
+
: `@${name} is not a decorator this library defines, so stripping it would change what the program means`
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
if (problems.length)
|
|
95
|
+
return { output: source, stripped: 0, problems };
|
|
96
|
+
if (!spans.length)
|
|
97
|
+
return { output: source, stripped: 0, problems: [] };
|
|
98
|
+
// Blank the decorator spans in place, keeping every newline, so nothing below them moves.
|
|
99
|
+
const characters = [...source];
|
|
100
|
+
for (const [start, end] of spans)
|
|
101
|
+
for (let index = start; index < end; index++)
|
|
102
|
+
if (characters[index] !== '\n')
|
|
103
|
+
characters[index] = ' ';
|
|
104
|
+
let body = characters.join('');
|
|
105
|
+
// Highest offset first, so each splice leaves the earlier offsets true.
|
|
106
|
+
for (const { at, text } of [...insertions].sort((a, b) => b.at - a.at))
|
|
107
|
+
body = `${body.slice(0, at)}${text}${body.slice(at)}`;
|
|
108
|
+
const imports = [];
|
|
109
|
+
if (needsNamespace)
|
|
110
|
+
imports.push('declareRpcNamespace as __rpcNamespace');
|
|
111
|
+
if (needsMethods)
|
|
112
|
+
imports.push('exposeMethods as __rpcMethods');
|
|
113
|
+
// The import sits at the end, and legally so: ESM hoists import bindings before any module
|
|
114
|
+
// code runs, so the marks on each class's closing-brace line already see these names.
|
|
115
|
+
const appended = [
|
|
116
|
+
'',
|
|
117
|
+
'// Added by source-rpc strip: the decorators above are blanked out - Node cannot run them -',
|
|
118
|
+
'// and re-said as runtime marks on the closing line of each class. Line numbers are unchanged.',
|
|
119
|
+
`import { ${imports.join(', ')} } from '@source-repo/rpc'`,
|
|
120
|
+
''
|
|
121
|
+
].join('\n');
|
|
122
|
+
return { output: `${body}${body.endsWith('\n') ? '' : '\n'}${appended}`, stripped: spans.length, problems: [] };
|
|
123
|
+
};
|
|
124
|
+
//# sourceMappingURL=strip.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strip.js","sourceRoot":"","sources":["../src/strip.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,UAAU,EAAyC,MAAM,UAAU,CAAA;AA2BrF;;;GAGG;AACH,MAAM,aAAa,GAAG,CAAC,SAAoB,EAAE,EAAE;IAC3C,IAAI,CAAC,SAAS,CAAC,kBAAkB,EAAE;QAAE,OAAO,IAAI,CAAA;IAChD,MAAM,CAAC,OAAO,CAAC,GAAG,SAAS,CAAC,YAAY,EAAE,CAAA;IAC1C,OAAO,OAAO,EAAE,OAAO,EAAE,CAAC,OAAO,CAAC,WAAW,EAAE,GAAG,CAAC,IAAI,IAAI,CAAA;AAC/D,CAAC,CA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