@imqueue/rpc 3.0.0 → 3.1.1
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 +16 -14
- package/src/IMQClient.js +64 -4
- package/src/cache/RedisCache.d.ts +6 -6
- package/src/decorators/expose.js +57 -23
- package/tsconfig.json +2 -0
- package/.github/workflows/build.yml +0 -72
- package/.oxfmtrc.json +0 -9
- package/.oxlintrc.json +0 -37
- package/debug.d.ts +0 -1
- package/scripts/strip-comment-coverage.mjs +0 -464
- package/typedoc.json +0 -19
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@imqueue/rpc",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.1",
|
|
4
4
|
"description": "RPC-like client-service implementation over messaging queue",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"redis",
|
|
@@ -20,19 +20,17 @@
|
|
|
20
20
|
"lint": "oxlint",
|
|
21
21
|
"format": "oxfmt \"index.ts\" \"debug.ts\" \"src/**/*.ts\" \"test/**/*.ts\"",
|
|
22
22
|
"format:check": "oxfmt --check \"index.ts\" \"debug.ts\" \"src/**/*.ts\" \"test/**/*.ts\"",
|
|
23
|
-
"test": "npm run build && node --test --test-timeout=15000 $(find test -name '*.spec.js')",
|
|
24
|
-
"test-coverage": "npm run build && node --enable-source-maps --test --experimental-test-coverage --test-timeout=15000 $(find test -name '*.spec.js')",
|
|
25
|
-
"test-lcov": "npm run build && mkdir -p coverage && node --enable-source-maps --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage/lcov.info --test-timeout=15000 $(find test -name '*.spec.js'); node scripts/strip-comment-coverage.mjs coverage/lcov.info",
|
|
23
|
+
"test": "npm run build && node --experimental-test-module-mocks --test --test-timeout=15000 $(find test -name '*.spec.js')",
|
|
24
|
+
"test-coverage": "npm run build && node --experimental-test-module-mocks --enable-source-maps --test --experimental-test-coverage --test-timeout=15000 $(find test -name '*.spec.js')",
|
|
25
|
+
"test-lcov": "npm run build && mkdir -p coverage && node --experimental-test-module-mocks --enable-source-maps --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage/lcov.info --test-timeout=15000 $(find test -name '*.spec.js'); node scripts/strip-comment-coverage.mjs coverage/lcov.info",
|
|
26
26
|
"test-coverage-html": "npm run test-lcov; genhtml coverage/lcov.info --output-directory coverage/html --ignore-errors inconsistent,corrupt,format,mismatch && echo \"Coverage report: file://$(pwd)/coverage/html/index.html\"",
|
|
27
27
|
"test-dev": "npm run test && npm run clean-js && npm run clean-typedefs && npm run clean-maps",
|
|
28
28
|
"clean-typedefs": "find . -name '*.d.ts' -not -wholename '*node_modules*' -not -wholename '*generator*' -type f -delete",
|
|
29
29
|
"clean-maps": "find . -name '*.js.map' -not -wholename '*node_modules*' -not -wholename '*generator*' -type f -delete",
|
|
30
30
|
"clean-js": "find . -name '*.js' -not -wholename '*node_modules*' -not -wholename '*generator*' -type f -delete",
|
|
31
31
|
"clean-tests": "rm -rf .nyc_output coverage",
|
|
32
|
-
"clean-doc": "rm -rf docs",
|
|
33
32
|
"clean-benchmark": "rm -rf benchmark-result",
|
|
34
|
-
"clean": "npm run clean-tests && npm run clean-typedefs && npm run clean-maps && npm run clean-js && npm run clean-
|
|
35
|
-
"doc": "typedoc && echo \"Docs generated: file://$(pwd)/docs/index.html\""
|
|
33
|
+
"clean": "npm run clean-tests && npm run clean-typedefs && npm run clean-maps && npm run clean-js && npm run clean-benchmark"
|
|
36
34
|
},
|
|
37
35
|
"repository": {
|
|
38
36
|
"type": "git",
|
|
@@ -45,17 +43,21 @@
|
|
|
45
43
|
"author": "imqueue.com <support@imqueue.com> (https://imqueue.com)",
|
|
46
44
|
"license": "GPL-3.0-only",
|
|
47
45
|
"dependencies": {
|
|
48
|
-
"@imqueue/core": "^3.
|
|
49
|
-
"
|
|
46
|
+
"@imqueue/core": "^3.1.0",
|
|
47
|
+
"acorn": "^8.17.0",
|
|
48
|
+
"typescript": "^7.0.2"
|
|
50
49
|
},
|
|
51
50
|
"devDependencies": {
|
|
52
|
-
"@types/mock-require": "^3.0.0",
|
|
53
51
|
"@types/node": "^24.9.1",
|
|
54
|
-
"mock-require": "^3.0.3",
|
|
55
52
|
"oxfmt": "0.57.0",
|
|
56
|
-
"oxlint": "1.72.0"
|
|
57
|
-
"typedoc": "^0.28.20"
|
|
53
|
+
"oxlint": "1.72.0"
|
|
58
54
|
},
|
|
59
55
|
"main": "index.js",
|
|
60
|
-
"types": "index.d.ts"
|
|
56
|
+
"types": "index.d.ts",
|
|
57
|
+
"exports": {
|
|
58
|
+
".": {
|
|
59
|
+
"types": "./index.d.ts",
|
|
60
|
+
"default": "./index.js"
|
|
61
|
+
}
|
|
62
|
+
}
|
|
61
63
|
}
|
package/src/IMQClient.js
CHANGED
|
@@ -96,13 +96,15 @@ exports.imqCallRejector = imqCallRejector;
|
|
|
96
96
|
const core_1 = __importStar(require("@imqueue/core"));
|
|
97
97
|
const _1 = require(".");
|
|
98
98
|
const helpers_1 = require("./helpers");
|
|
99
|
-
const typescript_1 = require("typescript");
|
|
100
99
|
const node_events_1 = require("node:events");
|
|
101
100
|
const node_vm_1 = require("node:vm");
|
|
101
|
+
const node_child_process_1 = require("node:child_process");
|
|
102
|
+
const node_fs_1 = require("node:fs");
|
|
103
|
+
const node_os_1 = require("node:os");
|
|
104
|
+
const node_path_1 = require("node:path");
|
|
102
105
|
// Loaded via require (not a static import) so that consumers which type-check
|
|
103
106
|
// this source through a file: link do not need `resolveJsonModule` enabled.
|
|
104
|
-
const tsOptions = require('../tsconfig.json')
|
|
105
|
-
.compilerOptions;
|
|
107
|
+
const tsOptions = require('../tsconfig.json').compilerOptions;
|
|
106
108
|
const RX_SEMICOLON = /;+$/g;
|
|
107
109
|
/**
|
|
108
110
|
* Class IMQClient - base abstract class for service clients.
|
|
@@ -652,6 +654,64 @@ function toComment(typedef, promised = false) {
|
|
|
652
654
|
.map((line, lineNum) => (lineNum ? ' * ' : '') + line)
|
|
653
655
|
.join('\n');
|
|
654
656
|
}
|
|
657
|
+
/**
|
|
658
|
+
* Transpiles generated client TypeScript source to CommonJS JavaScript.
|
|
659
|
+
*
|
|
660
|
+
* TypeScript 7 (native port) removed the in-process `transpile()`/`transpileModule()`
|
|
661
|
+
* API; its `typescript/unstable/*` replacement is not a stable runtime contract.
|
|
662
|
+
* Instead this shells out to the bundled `tsc` CLI in transform-only mode
|
|
663
|
+
* (`noCheck`, `noEmitOnError:false`) — the same "emit regardless of type or
|
|
664
|
+
* module-resolution errors" behaviour the old single-file transpile provided,
|
|
665
|
+
* built on the stable CLI rather than the unstable programmatic API.
|
|
666
|
+
*
|
|
667
|
+
* @param {string} src - generated client source
|
|
668
|
+
* @returns {string} - emitted CommonJS JavaScript
|
|
669
|
+
*/
|
|
670
|
+
function transpileClient(src) {
|
|
671
|
+
const dir = (0, node_fs_1.mkdtempSync)((0, node_path_1.join)((0, node_os_1.tmpdir)(), 'imq-client-'));
|
|
672
|
+
try {
|
|
673
|
+
const tsFile = (0, node_path_1.join)(dir, '__client__.ts');
|
|
674
|
+
const jsFile = (0, node_path_1.join)(dir, '__client__.js');
|
|
675
|
+
const cfgFile = (0, node_path_1.join)(dir, 'tsconfig.json');
|
|
676
|
+
(0, node_fs_1.writeFileSync)(tsFile, src);
|
|
677
|
+
(0, node_fs_1.writeFileSync)(cfgFile, JSON.stringify({
|
|
678
|
+
compilerOptions: {
|
|
679
|
+
...tsOptions,
|
|
680
|
+
// transform-only: always emit, ignoring type and
|
|
681
|
+
// module-resolution errors (the generated client references
|
|
682
|
+
// `@imqueue/rpc` types that need not resolve in this temp
|
|
683
|
+
// dir)
|
|
684
|
+
noCheck: true,
|
|
685
|
+
noEmitOnError: false,
|
|
686
|
+
skipLibCheck: true,
|
|
687
|
+
declaration: false,
|
|
688
|
+
declarationMap: false,
|
|
689
|
+
sourceMap: false,
|
|
690
|
+
inlineSources: false,
|
|
691
|
+
types: [],
|
|
692
|
+
rootDir: dir,
|
|
693
|
+
outDir: dir,
|
|
694
|
+
},
|
|
695
|
+
files: ['__client__.ts'],
|
|
696
|
+
}));
|
|
697
|
+
// resolve the compiler entry via the package root (its `exports` map
|
|
698
|
+
// blocks `typescript/lib/*`, so build a raw path that Node runs
|
|
699
|
+
// directly)
|
|
700
|
+
const tscJs = (0, node_path_1.join)((0, node_path_1.dirname)(require.resolve('typescript/package.json')), 'lib', 'tsc.js');
|
|
701
|
+
const result = (0, node_child_process_1.spawnSync)(process.execPath, [tscJs, '-p', cfgFile], {
|
|
702
|
+
encoding: 'utf8',
|
|
703
|
+
});
|
|
704
|
+
if (!(0, node_fs_1.existsSync)(jsFile)) {
|
|
705
|
+
throw new Error('IMQClient: client transpilation produced no output' +
|
|
706
|
+
(result.stdout ? `\n${result.stdout}` : '') +
|
|
707
|
+
(result.stderr ? `\n${result.stderr}` : ''));
|
|
708
|
+
}
|
|
709
|
+
return (0, node_fs_1.readFileSync)(jsFile, 'utf8');
|
|
710
|
+
}
|
|
711
|
+
finally {
|
|
712
|
+
(0, node_fs_1.rmSync)(dir, { recursive: true, force: true });
|
|
713
|
+
}
|
|
714
|
+
}
|
|
655
715
|
/**
|
|
656
716
|
* Compiles client source code and returns loaded module
|
|
657
717
|
*
|
|
@@ -664,7 +724,7 @@ async function compile(name, src, options) {
|
|
|
664
724
|
const path = options.path;
|
|
665
725
|
const srcFile = `${path}/${name}.ts`;
|
|
666
726
|
const jsFile = `${path}/${name}.js`;
|
|
667
|
-
const js = (
|
|
727
|
+
const js = transpileClient(src);
|
|
668
728
|
if (options.write) {
|
|
669
729
|
if (!(await (0, helpers_1.fileExists)(path))) {
|
|
670
730
|
await (0, helpers_1.mkdir)(path);
|
|
@@ -27,7 +27,11 @@ export interface IRedisCacheOptions extends Partial<IMQOptions> {
|
|
|
27
27
|
conn?: IRedisClient;
|
|
28
28
|
}
|
|
29
29
|
export declare const DEFAULT_REDIS_CACHE_OPTIONS: {
|
|
30
|
-
|
|
30
|
+
id?: string;
|
|
31
|
+
host?: string | undefined;
|
|
32
|
+
port?: number | undefined;
|
|
33
|
+
username?: string;
|
|
34
|
+
password?: string;
|
|
31
35
|
cleanup: boolean;
|
|
32
36
|
cleanupFilter: string;
|
|
33
37
|
vendor?: string;
|
|
@@ -41,11 +45,7 @@ export declare const DEFAULT_REDIS_CACHE_OPTIONS: {
|
|
|
41
45
|
handleSignals?: boolean;
|
|
42
46
|
verbose?: boolean;
|
|
43
47
|
verboseExtended?: boolean;
|
|
44
|
-
|
|
45
|
-
host?: string | undefined;
|
|
46
|
-
port?: number | undefined;
|
|
47
|
-
username?: string | undefined;
|
|
48
|
-
password?: string | undefined;
|
|
48
|
+
prefix: string;
|
|
49
49
|
};
|
|
50
50
|
export declare const REDIS_CLIENT_INIT_ERROR = "Redis client is not initialized!";
|
|
51
51
|
/**
|
package/src/decorators/expose.js
CHANGED
|
@@ -24,7 +24,7 @@ exports.expose = expose;
|
|
|
24
24
|
* purchase a proprietary commercial license. Please contact us at
|
|
25
25
|
* <support@imqueue.com> to get commercial licensing options.
|
|
26
26
|
*/
|
|
27
|
-
const
|
|
27
|
+
const acorn_1 = require("acorn");
|
|
28
28
|
const __1 = require("..");
|
|
29
29
|
const descriptions = {};
|
|
30
30
|
const RX_ARG_NAMES = /^(\S+)([=\s]+.*?)?$/;
|
|
@@ -114,45 +114,79 @@ function parseComment(src) {
|
|
|
114
114
|
return data;
|
|
115
115
|
}
|
|
116
116
|
/**
|
|
117
|
-
* Finds and parses methods and their comment blocks for a given class
|
|
117
|
+
* Finds and parses methods and their comment blocks for a given class.
|
|
118
|
+
*
|
|
119
|
+
* The class source (from `Function.prototype.toString`) is always compiled
|
|
120
|
+
* JavaScript, so it is parsed with acorn; a method's doc is the last block
|
|
121
|
+
* comment between the previous class member (or the class body start) and
|
|
122
|
+
* the method itself, i.e. the last block comment in its leading trivia.
|
|
118
123
|
*
|
|
119
124
|
* @param {string} name - class name
|
|
120
125
|
* @param {string} src - class source code
|
|
121
126
|
*/
|
|
122
127
|
function parseDescriptions(name, src) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
128
|
+
const comments = [];
|
|
129
|
+
const options = {
|
|
130
|
+
// 'latest' so we can parse whatever modern syntax the configured
|
|
131
|
+
// compilation target emits into the class source we introspect
|
|
132
|
+
ecmaVersion: 'latest',
|
|
133
|
+
allowReserved: true,
|
|
134
|
+
onComment: comments,
|
|
135
|
+
};
|
|
127
136
|
// the local `inherits` is a placeholder only: the public description's
|
|
128
137
|
// `inherits` is derived from the runtime prototype chain in
|
|
129
|
-
// buildMethodDescription(), never from the parsed source
|
|
138
|
+
// buildMethodDescription(), never from the parsed source (standard
|
|
139
|
+
// decorators compile the heritage clause to a helper like `_classSuper`,
|
|
140
|
+
// so the source name is unusable)
|
|
130
141
|
descriptions[name] = {
|
|
131
142
|
inherits: 'Function',
|
|
132
143
|
};
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
144
|
+
let body;
|
|
145
|
+
try {
|
|
146
|
+
body = (0, acorn_1.parse)(src, options).body;
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
// tolerate unparseable sources (e.g. bound or native functions) the
|
|
150
|
+
// same way the previous error-tolerant parsers did
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
for (const node of body) {
|
|
154
|
+
if (!(node &&
|
|
155
|
+
node.type === 'ClassDeclaration' &&
|
|
156
|
+
node.id &&
|
|
157
|
+
node.id.name === name &&
|
|
158
|
+
node.body &&
|
|
159
|
+
node.body.type === 'ClassBody')) {
|
|
137
160
|
continue;
|
|
138
161
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
162
|
+
const members = node.body.body;
|
|
163
|
+
for (let i = 0; i < members.length; i++) {
|
|
164
|
+
const member = members[i];
|
|
165
|
+
// plain (non-accessor, non-constructor) methods with literal
|
|
166
|
+
// identifier names only — same filter the previous
|
|
167
|
+
// implementations applied
|
|
168
|
+
if (member.type !== 'MethodDefinition' ||
|
|
169
|
+
member.kind !== 'method' ||
|
|
170
|
+
member.computed ||
|
|
171
|
+
!member.key ||
|
|
172
|
+
member.key.type !== 'Identifier') {
|
|
143
173
|
continue;
|
|
144
174
|
}
|
|
145
|
-
// the
|
|
146
|
-
//
|
|
147
|
-
//
|
|
148
|
-
const
|
|
175
|
+
// the doc block must lie after the previous member of any kind
|
|
176
|
+
// (so comments inside earlier bodies and field initializers can
|
|
177
|
+
// never match) and before the method itself
|
|
178
|
+
const lowerBound = i
|
|
179
|
+
? members[i - 1].end
|
|
180
|
+
: node.body.start + 1;
|
|
181
|
+
const blocks = comments.filter(comment => comment.type === 'Block' &&
|
|
182
|
+
comment.start >= lowerBound &&
|
|
183
|
+
comment.end <= member.start);
|
|
149
184
|
if (!blocks.length) {
|
|
150
185
|
continue;
|
|
151
186
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
comment: parseComment(src.slice(block.pos + 2, block.end - 2)),
|
|
187
|
+
descriptions[name][member.key.name] = {
|
|
188
|
+
// acorn comment values exclude the /* and */ delimiters
|
|
189
|
+
comment: parseComment(blocks[blocks.length - 1].value),
|
|
156
190
|
};
|
|
157
191
|
}
|
|
158
192
|
}
|
package/tsconfig.json
CHANGED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
name: Build
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
branches: [master]
|
|
6
|
-
tags: ['*']
|
|
7
|
-
pull_request:
|
|
8
|
-
|
|
9
|
-
permissions:
|
|
10
|
-
contents: read
|
|
11
|
-
|
|
12
|
-
concurrency:
|
|
13
|
-
group: ${{ github.workflow }}-${{ github.ref }}
|
|
14
|
-
cancel-in-progress: true
|
|
15
|
-
|
|
16
|
-
jobs:
|
|
17
|
-
quality:
|
|
18
|
-
name: Lint & format
|
|
19
|
-
runs-on: ubuntu-latest
|
|
20
|
-
timeout-minutes: 10
|
|
21
|
-
steps:
|
|
22
|
-
- name: Checkout repository
|
|
23
|
-
uses: actions/checkout@v4
|
|
24
|
-
|
|
25
|
-
- name: Use Node.js
|
|
26
|
-
uses: actions/setup-node@v4
|
|
27
|
-
with:
|
|
28
|
-
node-version: 24.x
|
|
29
|
-
cache: npm
|
|
30
|
-
|
|
31
|
-
- name: Install dependencies
|
|
32
|
-
run: npm ci
|
|
33
|
-
|
|
34
|
-
- name: Check formatting
|
|
35
|
-
run: npm run format:check
|
|
36
|
-
|
|
37
|
-
- name: Lint
|
|
38
|
-
run: npm run lint
|
|
39
|
-
|
|
40
|
-
test:
|
|
41
|
-
name: Test (Node ${{ matrix.node-version }})
|
|
42
|
-
runs-on: ubuntu-latest
|
|
43
|
-
timeout-minutes: 15
|
|
44
|
-
strategy:
|
|
45
|
-
fail-fast: false
|
|
46
|
-
matrix:
|
|
47
|
-
node-version: [22.x, 24.x]
|
|
48
|
-
services:
|
|
49
|
-
redis:
|
|
50
|
-
image: redis:7
|
|
51
|
-
ports:
|
|
52
|
-
- 6379:6379
|
|
53
|
-
options: >-
|
|
54
|
-
--health-cmd "redis-cli ping"
|
|
55
|
-
--health-interval 10s
|
|
56
|
-
--health-timeout 5s
|
|
57
|
-
--health-retries 5
|
|
58
|
-
steps:
|
|
59
|
-
- name: Checkout repository
|
|
60
|
-
uses: actions/checkout@v4
|
|
61
|
-
|
|
62
|
-
- name: Use Node.js ${{ matrix.node-version }}
|
|
63
|
-
uses: actions/setup-node@v4
|
|
64
|
-
with:
|
|
65
|
-
node-version: ${{ matrix.node-version }}
|
|
66
|
-
cache: npm
|
|
67
|
-
|
|
68
|
-
- name: Install dependencies
|
|
69
|
-
run: npm ci
|
|
70
|
-
|
|
71
|
-
- name: Run tests
|
|
72
|
-
run: npm test
|
package/.oxfmtrc.json
DELETED
package/.oxlintrc.json
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "./node_modules/oxlint/configuration_schema.json",
|
|
3
|
-
"categories": {
|
|
4
|
-
"correctness": "error"
|
|
5
|
-
},
|
|
6
|
-
"rules": {
|
|
7
|
-
"no-debugger": "error",
|
|
8
|
-
"no-console": ["warn", { "allow": ["warn", "error", "info"] }],
|
|
9
|
-
"no-unused-vars": "warn",
|
|
10
|
-
"no-unused-expressions": [
|
|
11
|
-
"error",
|
|
12
|
-
{ "allowShortCircuit": true, "allowTernary": true }
|
|
13
|
-
]
|
|
14
|
-
},
|
|
15
|
-
"overrides": [
|
|
16
|
-
{
|
|
17
|
-
"files": ["test/**"],
|
|
18
|
-
"rules": {
|
|
19
|
-
"no-unused-expressions": "off",
|
|
20
|
-
"no-async-promise-executor": "off",
|
|
21
|
-
"no-useless-catch": "off",
|
|
22
|
-
"unicorn/no-new-array": "off",
|
|
23
|
-
"unicorn/prefer-string-starts-ends-with": "off",
|
|
24
|
-
"unicorn/no-unnecessary-await": "off",
|
|
25
|
-
"no-unused-vars": "off"
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
],
|
|
29
|
-
"ignorePatterns": [
|
|
30
|
-
"**/*.js",
|
|
31
|
-
"**/*.d.ts",
|
|
32
|
-
"docs/",
|
|
33
|
-
"coverage/",
|
|
34
|
-
".nyc_output/",
|
|
35
|
-
"node_modules/"
|
|
36
|
-
]
|
|
37
|
-
}
|
package/debug.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,464 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Coverage post-processor
|
|
3
|
-
*
|
|
4
|
-
* I'm Queue Software Project
|
|
5
|
-
* Copyright (C) 2025 imqueue.com <support@imqueue.com>
|
|
6
|
-
*
|
|
7
|
-
* This program is free software: you can redistribute it and/or modify
|
|
8
|
-
* it under the terms of the GNU General Public License as published by
|
|
9
|
-
* the Free Software Foundation, either version 3 of the License, or
|
|
10
|
-
* (at your option) any later version.
|
|
11
|
-
*
|
|
12
|
-
* This program is distributed in the hope that it will be useful,
|
|
13
|
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15
|
-
* GNU General Public License for more details.
|
|
16
|
-
*
|
|
17
|
-
* You should have received a copy of the GNU General Public License
|
|
18
|
-
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
19
|
-
*
|
|
20
|
-
* If you want to use this code in a closed source (commercial) project, you can
|
|
21
|
-
* purchase a proprietary commercial license. Please contact us at
|
|
22
|
-
* <support@imqueue.com> to get commercial licensing options.
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Node's built-in coverage (`--experimental-test-coverage --enable-source-maps`)
|
|
27
|
-
* reports every non-code line of a `.ts` source as uncovered, because comment
|
|
28
|
-
* and blank lines have no source-map mappings and therefore fall outside every
|
|
29
|
-
* covered V8 byte-range. That paints the license header and each JSDoc block
|
|
30
|
-
* red in the HTML report and pushes per-file line% far below reality.
|
|
31
|
-
*
|
|
32
|
-
* Node's reporter also emits records that the (strict) genhtml LCOV 2.x tool
|
|
33
|
-
* rejects — function entries with `undefined` line numbers or synthetic,
|
|
34
|
-
* duplicated names (`<instance_members_initializer>`, and decorated methods all
|
|
35
|
-
* mis-named `has`/`get`), plus `BRDA:undefined,...` branch records. Left in,
|
|
36
|
-
* they produce a wall of warnings and a fatal "unexpected category" error.
|
|
37
|
-
*
|
|
38
|
-
* This script rewrites an lcov file in place: it drops the `DA`/`BRDA` records
|
|
39
|
-
* for lines that contain no executable code (comments and blank lines), drops
|
|
40
|
-
* all function records and malformed branch records, then recomputes the
|
|
41
|
-
* `LF`/`LH`/`BRF`/`BRH` totals. It uses the TypeScript scanner (already a dev
|
|
42
|
-
* dependency) to locate comments reliably — comment markers inside strings,
|
|
43
|
-
* template literals and regexes are not misdetected. The result renders in
|
|
44
|
-
* genhtml with accurate line + branch coverage and no warnings or errors.
|
|
45
|
-
*/
|
|
46
|
-
import { readFileSync, writeFileSync, existsSync } from 'node:fs';
|
|
47
|
-
import ts from 'typescript';
|
|
48
|
-
|
|
49
|
-
const lcovPath = process.argv[2] || 'coverage/lcov.info';
|
|
50
|
-
|
|
51
|
-
if (!existsSync(lcovPath)) {
|
|
52
|
-
console.warn(`strip-comment-coverage: ${lcovPath} not found, skipping`);
|
|
53
|
-
process.exit(0);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Returns the set of 1-based line numbers in the given source that hold no
|
|
58
|
-
* executable code — i.e. blank lines and lines whose only content is a comment.
|
|
59
|
-
*
|
|
60
|
-
* @param {string} text
|
|
61
|
-
* @returns {Set<number>}
|
|
62
|
-
*/
|
|
63
|
-
function nonCodeLines(text) {
|
|
64
|
-
const commentChars = new Uint8Array(text.length);
|
|
65
|
-
// Collect every comment range from the parsed tree. A standalone
|
|
66
|
-
// ts.createScanner is unreliable here — without parser context it can
|
|
67
|
-
// mis-scan a stray `/` as a regex literal and swallow the comments inside
|
|
68
|
-
// it — so walk all tokens and take their leading/trailing comment trivia,
|
|
69
|
-
// which every comment in the file belongs to.
|
|
70
|
-
const sf = ts.createSourceFile('x.ts', text, ts.ScriptTarget.Latest, true);
|
|
71
|
-
const seen = new Set();
|
|
72
|
-
const mark = pos => {
|
|
73
|
-
for (const get of [
|
|
74
|
-
ts.getLeadingCommentRanges,
|
|
75
|
-
ts.getTrailingCommentRanges,
|
|
76
|
-
]) {
|
|
77
|
-
for (const range of get(text, pos) || []) {
|
|
78
|
-
const key = `${range.pos}:${range.end}`;
|
|
79
|
-
|
|
80
|
-
if (seen.has(key)) {
|
|
81
|
-
continue;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
seen.add(key);
|
|
85
|
-
|
|
86
|
-
for (let i = range.pos; i < range.end; i++) {
|
|
87
|
-
commentChars[i] = 1;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
const visit = node => {
|
|
93
|
-
mark(node.getFullStart());
|
|
94
|
-
node.getChildren(sf).forEach(visit);
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
visit(sf);
|
|
98
|
-
|
|
99
|
-
const lines = text.split('\n');
|
|
100
|
-
const result = new Set();
|
|
101
|
-
let pos = 0;
|
|
102
|
-
|
|
103
|
-
for (let ln = 0; ln < lines.length; ln++) {
|
|
104
|
-
const line = lines[ln];
|
|
105
|
-
let hasCode = false;
|
|
106
|
-
|
|
107
|
-
for (let i = 0; i < line.length; i++) {
|
|
108
|
-
const ch = line[i];
|
|
109
|
-
|
|
110
|
-
if (ch === ' ' || ch === '\t' || ch === '\r') {
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (!commentChars[pos + i]) {
|
|
115
|
-
hasCode = true;
|
|
116
|
-
break;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (!hasCode) {
|
|
121
|
-
result.add(ln + 1);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
pos += line.length + 1; // account for the split '\n'
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return result;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Returns the set of 1-based lines that carry no coverable executable code of
|
|
132
|
-
* their own yet Node's source-mapped coverage mis-reports as uncovered
|
|
133
|
-
* (the mapping lands outside the covered byte-range, especially under
|
|
134
|
-
* `nodenext` output). These are:
|
|
135
|
-
* - imports (compile to a `require` that always runs on load);
|
|
136
|
-
* - module-scope variable declarations (`const`/`let`/`var`) — the whole
|
|
137
|
-
* statement if its initializer has no function body, otherwise just the
|
|
138
|
-
* declaration line so real bodies keep their coverage;
|
|
139
|
-
* - class declaration headers and type-only property fields (no initializer).
|
|
140
|
-
* Method/accessor/constructor bodies are never touched, so genuinely unreached
|
|
141
|
-
* code still shows up.
|
|
142
|
-
*
|
|
143
|
-
* @param {string} text
|
|
144
|
-
* @returns {Set<number>}
|
|
145
|
-
*/
|
|
146
|
-
function importLines(text) {
|
|
147
|
-
const sf = ts.createSourceFile(
|
|
148
|
-
'x.ts',
|
|
149
|
-
text,
|
|
150
|
-
ts.ScriptTarget.Latest,
|
|
151
|
-
false,
|
|
152
|
-
);
|
|
153
|
-
const result = new Set();
|
|
154
|
-
const lineAt = pos => sf.getLineAndCharacterOfPosition(pos).line;
|
|
155
|
-
const addRange = (start, end) => {
|
|
156
|
-
for (let line = lineAt(start); line <= lineAt(end); line++) {
|
|
157
|
-
result.add(line + 1);
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
const hasFunction = node => {
|
|
161
|
-
let found = false;
|
|
162
|
-
const walk = n => {
|
|
163
|
-
if (found) {
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (
|
|
168
|
-
ts.isFunctionExpression(n) ||
|
|
169
|
-
ts.isArrowFunction(n) ||
|
|
170
|
-
ts.isFunctionDeclaration(n) ||
|
|
171
|
-
ts.isMethodDeclaration(n)
|
|
172
|
-
) {
|
|
173
|
-
found = true;
|
|
174
|
-
|
|
175
|
-
return;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
n.forEachChild(walk);
|
|
179
|
-
};
|
|
180
|
-
|
|
181
|
-
walk(node);
|
|
182
|
-
|
|
183
|
-
return found;
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
for (const stmt of sf.statements) {
|
|
187
|
-
if (
|
|
188
|
-
ts.isImportDeclaration(stmt) ||
|
|
189
|
-
ts.isImportEqualsDeclaration(stmt)
|
|
190
|
-
) {
|
|
191
|
-
addRange(stmt.getStart(sf), stmt.getEnd());
|
|
192
|
-
} else if (ts.isVariableStatement(stmt)) {
|
|
193
|
-
if (hasFunction(stmt)) {
|
|
194
|
-
result.add(lineAt(stmt.getStart(sf)) + 1);
|
|
195
|
-
} else {
|
|
196
|
-
addRange(stmt.getStart(sf), stmt.getEnd());
|
|
197
|
-
}
|
|
198
|
-
} else if (ts.isClassDeclaration(stmt)) {
|
|
199
|
-
// class header (`class X extends Y {`): everything up to the first
|
|
200
|
-
// member, or the whole thing for an empty class
|
|
201
|
-
const headerEnd = stmt.members.length
|
|
202
|
-
? stmt.members[0].getStart(sf)
|
|
203
|
-
: stmt.getEnd();
|
|
204
|
-
|
|
205
|
-
addRange(stmt.getStart(sf), headerEnd);
|
|
206
|
-
// the closing brace line emits no coverable code either
|
|
207
|
-
result.add(lineAt(stmt.getEnd()) + 1);
|
|
208
|
-
|
|
209
|
-
// type-only fields (definite-assignment / uninitialized) emit no JS
|
|
210
|
-
for (const member of stmt.members) {
|
|
211
|
-
if (
|
|
212
|
-
ts.isPropertyDeclaration(member) &&
|
|
213
|
-
!member.initializer
|
|
214
|
-
) {
|
|
215
|
-
addRange(member.getStart(sf), member.getEnd());
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
return result;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const B64 =
|
|
225
|
-
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
226
|
-
const B64MAP = new Map([...B64].map((c, i) => [c, i]));
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Decodes one base64 VLQ source-map segment into its integer fields.
|
|
230
|
-
*
|
|
231
|
-
* @param {string} seg
|
|
232
|
-
* @returns {number[]}
|
|
233
|
-
*/
|
|
234
|
-
function decodeVLQ(seg) {
|
|
235
|
-
const out = [];
|
|
236
|
-
let shift = 0;
|
|
237
|
-
let value = 0;
|
|
238
|
-
|
|
239
|
-
for (const ch of seg) {
|
|
240
|
-
const digit = B64MAP.get(ch);
|
|
241
|
-
|
|
242
|
-
if (digit === undefined) {
|
|
243
|
-
break;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
value += (digit & 31) << shift;
|
|
247
|
-
|
|
248
|
-
if (digit & 32) {
|
|
249
|
-
shift += 5;
|
|
250
|
-
} else {
|
|
251
|
-
out.push(value & 1 ? -(value >> 1) : value >> 1);
|
|
252
|
-
value = 0;
|
|
253
|
-
shift = 0;
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
return out;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Returns the set of 1-based source lines that produced generated JavaScript,
|
|
262
|
-
* read from the file's `.js.map`. These are exactly the coverable lines — every
|
|
263
|
-
* other line (comments, blanks, type-only declarations, interfaces) emits no
|
|
264
|
-
* code and can never be executed. Returns null when no source map is present.
|
|
265
|
-
*
|
|
266
|
-
* @param {string} sourceFile - path of the .ts source (e.g. src/RedisQueue.ts)
|
|
267
|
-
* @returns {Set<number> | null}
|
|
268
|
-
*/
|
|
269
|
-
function emittedLines(sourceFile) {
|
|
270
|
-
const mapPath = sourceFile.replace(/\.ts$/, '.js.map');
|
|
271
|
-
|
|
272
|
-
if (!existsSync(mapPath)) {
|
|
273
|
-
return null;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
let map;
|
|
277
|
-
|
|
278
|
-
try {
|
|
279
|
-
map = JSON.parse(readFileSync(mapPath, 'utf8'));
|
|
280
|
-
} catch {
|
|
281
|
-
return null;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
if (typeof map.mappings !== 'string') {
|
|
285
|
-
return null;
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
const lines = new Set();
|
|
289
|
-
let srcIndex = 0;
|
|
290
|
-
let srcLine = 0;
|
|
291
|
-
|
|
292
|
-
for (const group of map.mappings.split(';')) {
|
|
293
|
-
for (const seg of group.split(',')) {
|
|
294
|
-
if (!seg) {
|
|
295
|
-
continue;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
const fields = decodeVLQ(seg);
|
|
299
|
-
|
|
300
|
-
// [genCol, srcIndex, srcLine, srcCol, nameIndex]; <4 fields = no
|
|
301
|
-
// source position for this segment
|
|
302
|
-
if (fields.length >= 4) {
|
|
303
|
-
srcIndex += fields[1];
|
|
304
|
-
srcLine += fields[2];
|
|
305
|
-
|
|
306
|
-
if (srcIndex === 0) {
|
|
307
|
-
lines.add(srcLine + 1); // map lines are 0-based
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
return lines;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
const coverableCache = new Map();
|
|
317
|
-
|
|
318
|
-
/**
|
|
319
|
-
* Resolves how to filter a source file's line records:
|
|
320
|
-
* - `{ mode: 'keep', set }` — keep only lines in `set` (source-map driven);
|
|
321
|
-
* - `{ mode: 'drop', set }` — drop lines in `set` (comment-scanner fallback).
|
|
322
|
-
*
|
|
323
|
-
* @param {string} sourceFile
|
|
324
|
-
* @returns {{ mode: 'keep' | 'drop', set: Set<number> }}
|
|
325
|
-
*/
|
|
326
|
-
function coverableInfo(sourceFile) {
|
|
327
|
-
if (coverableCache.has(sourceFile)) {
|
|
328
|
-
return coverableCache.get(sourceFile);
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
const emitted = sourceFile ? emittedLines(sourceFile) : null;
|
|
332
|
-
let info;
|
|
333
|
-
|
|
334
|
-
if (emitted && existsSync(sourceFile)) {
|
|
335
|
-
// A line is coverable only if it emitted JS (has a source-map mapping)
|
|
336
|
-
// AND is not a comment/blank AND is not an import statement. The comment
|
|
337
|
-
// check is required because `removeComments: false` keeps license/JSDoc
|
|
338
|
-
// comments in the output, so they carry mappings too; the mapping check
|
|
339
|
-
// drops type-only lines (interfaces, type aliases) which emit nothing;
|
|
340
|
-
// the import check drops the mis-mapped import artifact.
|
|
341
|
-
const text = readFileSync(sourceFile, 'utf8');
|
|
342
|
-
const nonCode = nonCodeLines(text);
|
|
343
|
-
const imports = importLines(text);
|
|
344
|
-
const keep = new Set(
|
|
345
|
-
[...emitted].filter(
|
|
346
|
-
line => !nonCode.has(line) && !imports.has(line),
|
|
347
|
-
),
|
|
348
|
-
);
|
|
349
|
-
info = { mode: 'keep', set: keep };
|
|
350
|
-
} else if (sourceFile && existsSync(sourceFile)) {
|
|
351
|
-
const text = readFileSync(sourceFile, 'utf8');
|
|
352
|
-
const skip = nonCodeLines(text);
|
|
353
|
-
|
|
354
|
-
for (const line of importLines(text)) {
|
|
355
|
-
skip.add(line);
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
info = { mode: 'drop', set: skip };
|
|
359
|
-
} else {
|
|
360
|
-
info = { mode: 'drop', set: new Set() };
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
coverableCache.set(sourceFile, info);
|
|
364
|
-
|
|
365
|
-
return info;
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
let strippedLines = 0;
|
|
369
|
-
|
|
370
|
-
/**
|
|
371
|
-
* Rewrites a single lcov record so that genhtml accepts it without warnings or
|
|
372
|
-
* errors. It:
|
|
373
|
-
* - drops DA/BRDA entries on non-code (comment/blank) lines;
|
|
374
|
-
* - drops all function records (FN/FNDA/FNF/FNH) — Node's source-mapped
|
|
375
|
-
* function detection is unreliable here (undefined line numbers, mis-named
|
|
376
|
-
* and duplicated entries), which genhtml rejects, so line + branch coverage
|
|
377
|
-
* is kept instead;
|
|
378
|
-
* - drops malformed branch records such as `BRDA:undefined,...`;
|
|
379
|
-
* - recomputes the LF/LH/BRF/BRH totals.
|
|
380
|
-
*
|
|
381
|
-
* @param {string[]} recordLines - record body, excluding the end_of_record line
|
|
382
|
-
* @returns {string}
|
|
383
|
-
*/
|
|
384
|
-
function processRecord(recordLines) {
|
|
385
|
-
const sfLine = recordLines.find(line => line.startsWith('SF:'));
|
|
386
|
-
const sourceFile = sfLine ? sfLine.slice(3).trim() : '';
|
|
387
|
-
const info = coverableInfo(sourceFile);
|
|
388
|
-
|
|
389
|
-
// true when a source line produces no executable code and must be dropped
|
|
390
|
-
const drop = n => (info.mode === 'keep' ? !info.set.has(n) : info.set.has(n));
|
|
391
|
-
|
|
392
|
-
let lf = 0;
|
|
393
|
-
let lh = 0;
|
|
394
|
-
let brf = 0;
|
|
395
|
-
let brh = 0;
|
|
396
|
-
|
|
397
|
-
const kept = recordLines.filter(line => {
|
|
398
|
-
// drop all function records — unreliable under source maps
|
|
399
|
-
if (/^(FN:|FNDA:|FNF:|FNH:)/.test(line)) {
|
|
400
|
-
return false;
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
const da = line.match(/^DA:(\d+),(\d+)/);
|
|
404
|
-
|
|
405
|
-
if (da) {
|
|
406
|
-
if (drop(+da[1])) {
|
|
407
|
-
strippedLines++;
|
|
408
|
-
|
|
409
|
-
return false;
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
lf++;
|
|
413
|
-
|
|
414
|
-
if (+da[2] > 0) {
|
|
415
|
-
lh++;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
return true;
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
if (line.startsWith('BRDA:')) {
|
|
422
|
-
const brda = line.match(/^BRDA:(\d+),\d+,\d+,(\d+|-)$/);
|
|
423
|
-
|
|
424
|
-
// drop malformed (e.g. BRDA:undefined,...) or non-code branches
|
|
425
|
-
if (!brda || drop(+brda[1])) {
|
|
426
|
-
return false;
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
brf++;
|
|
430
|
-
|
|
431
|
-
if (brda[2] !== '-' && +brda[2] > 0) {
|
|
432
|
-
brh++;
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
return true;
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
// drop the old totals — they are recomputed below
|
|
439
|
-
return !/^(LF|LH|BRF|BRH):/.test(line);
|
|
440
|
-
});
|
|
441
|
-
|
|
442
|
-
return (
|
|
443
|
-
`${kept.join('\n')}\n` +
|
|
444
|
-
`LF:${lf}\nLH:${lh}\nBRF:${brf}\nBRH:${brh}\nend_of_record\n`
|
|
445
|
-
);
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
const output = [];
|
|
449
|
-
let record = [];
|
|
450
|
-
|
|
451
|
-
for (const line of readFileSync(lcovPath, 'utf8').split('\n')) {
|
|
452
|
-
if (line === 'end_of_record') {
|
|
453
|
-
output.push(processRecord(record));
|
|
454
|
-
record = [];
|
|
455
|
-
} else if (line !== '') {
|
|
456
|
-
record.push(line);
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
writeFileSync(lcovPath, output.join(''));
|
|
461
|
-
console.info(
|
|
462
|
-
`strip-comment-coverage: removed ${strippedLines} comment/blank line ` +
|
|
463
|
-
`entries from ${lcovPath}`,
|
|
464
|
-
);
|
package/typedoc.json
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://typedoc.org/schema.json",
|
|
3
|
-
"name": "@imqueue/rpc",
|
|
4
|
-
"entryPoints": ["."],
|
|
5
|
-
"entryPointStrategy": "expand",
|
|
6
|
-
"exclude": [
|
|
7
|
-
"**/+(debug|test|node_modules|docs|coverage|benchmark|.nyc_output)/**/*",
|
|
8
|
-
"**/*.d.ts"
|
|
9
|
-
],
|
|
10
|
-
"excludePrivate": true,
|
|
11
|
-
"excludeExternals": true,
|
|
12
|
-
"hideGenerator": true,
|
|
13
|
-
"externalSymbolLinkMappings": {
|
|
14
|
-
"@types/node": {
|
|
15
|
-
"EventEmitter.defaultMaxListeners": "https://nodejs.org/api/events.html#eventsdefaultmaxlisteners"
|
|
16
|
-
}
|
|
17
|
-
},
|
|
18
|
-
"out": "docs"
|
|
19
|
-
}
|