rpc-express-toolkit 4.4.0 → 4.4.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/README.md +74 -0
- package/docs/COMPATIBILITY.md +28 -1
- package/package.json +12 -4
- package/src/index.d.ts +248 -251
- package/src/index.js +3 -0
- package/src/safe.d.ts +79 -51
- package/src/safe.js +14 -0
package/README.md
CHANGED
|
@@ -28,6 +28,7 @@ yarn add express rpc-express-toolkit
|
|
|
28
28
|
```
|
|
29
29
|
|
|
30
30
|
Requirements:
|
|
31
|
+
|
|
31
32
|
- Node.js 18+ (uses `globalThis.fetch`)
|
|
32
33
|
- Express 4.21+ or Express 5.x
|
|
33
34
|
|
|
@@ -35,6 +36,79 @@ Requirements:
|
|
|
35
36
|
|
|
36
37
|
`rpc-express-toolkit` is tested with Node.js 18, 20, 22, and 24 across Express 4 and Express 5. See [Compatibility](docs/COMPATIBILITY.md) for the current runtime matrix.
|
|
37
38
|
|
|
39
|
+
### TypeScript
|
|
40
|
+
|
|
41
|
+
The package supports TypeScript ESM/NodeNext and CommonJS consumers. Both forms are tested with `strict: true`, `skipLibCheck: false`, and `esModuleInterop: false` against the tarball produced by `npm pack`.
|
|
42
|
+
|
|
43
|
+
Install the TypeScript and Express declarations used by these examples:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm install --save-dev typescript @types/express @types/node
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
ESM/NodeNext (`package.json` contains `"type": "module"`):
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import express from 'express';
|
|
53
|
+
import RpcEndpoint, {
|
|
54
|
+
RpcEndpoint as NamedRpcEndpoint,
|
|
55
|
+
RpcClient,
|
|
56
|
+
type RpcEndpointOptions,
|
|
57
|
+
} from 'rpc-express-toolkit';
|
|
58
|
+
import {
|
|
59
|
+
RpcSafeClient,
|
|
60
|
+
RpcSafeEndpoint,
|
|
61
|
+
} from 'rpc-express-toolkit/safe';
|
|
62
|
+
|
|
63
|
+
const app = express();
|
|
64
|
+
app.use(express.json());
|
|
65
|
+
|
|
66
|
+
const options: RpcEndpointOptions = { safeEnabled: false };
|
|
67
|
+
const rpc = new RpcEndpoint(app, {}, options);
|
|
68
|
+
const namedRpc = new NamedRpcEndpoint(app, {}, '/named');
|
|
69
|
+
const client = new RpcClient('http://localhost:3000/api');
|
|
70
|
+
const safeRpc = new RpcSafeEndpoint(app, {}, '/safe');
|
|
71
|
+
const safeClient = new RpcSafeClient('http://localhost:3000/safe');
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Use these compiler options for the ESM example:
|
|
75
|
+
|
|
76
|
+
```json
|
|
77
|
+
{
|
|
78
|
+
"compilerOptions": {
|
|
79
|
+
"module": "NodeNext",
|
|
80
|
+
"moduleResolution": "NodeNext",
|
|
81
|
+
"strict": true,
|
|
82
|
+
"skipLibCheck": false,
|
|
83
|
+
"esModuleInterop": false,
|
|
84
|
+
"ignoreDeprecations": "6.0"
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
`ignoreDeprecations` only acknowledges TypeScript 6's deprecation notice for
|
|
90
|
+
the explicitly tested `esModuleInterop: false` setting.
|
|
91
|
+
|
|
92
|
+
CommonJS TypeScript (`.cts` with NodeNext or Node16 resolution):
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import express = require('express');
|
|
96
|
+
import RpcEndpoint = require('rpc-express-toolkit');
|
|
97
|
+
import Safe = require('rpc-express-toolkit/safe');
|
|
98
|
+
|
|
99
|
+
const app = express();
|
|
100
|
+
app.use(express.json());
|
|
101
|
+
|
|
102
|
+
const options: RpcEndpoint.RpcEndpointOptions = { safeEnabled: false };
|
|
103
|
+
const rpc = new RpcEndpoint(app, {}, options);
|
|
104
|
+
const namedRpc = new RpcEndpoint.RpcEndpoint(app, {}, '/named');
|
|
105
|
+
const client = new RpcEndpoint.RpcClient('http://localhost:3000/api');
|
|
106
|
+
const safeRpc = new Safe.RpcSafeEndpoint(app, {}, '/safe');
|
|
107
|
+
const safeClient = new Safe.RpcSafeClient('http://localhost:3000/safe');
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
The root import remains the `RpcEndpoint` CommonJS default while also exposing its named API. The `/safe` subpath exposes the safe classes and the root utilities it re-exports at runtime.
|
|
111
|
+
|
|
38
112
|
### Server
|
|
39
113
|
|
|
40
114
|
```javascript
|
package/docs/COMPATIBILITY.md
CHANGED
|
@@ -15,6 +15,33 @@
|
|
|
15
15
|
|
|
16
16
|
The GitHub Actions matrix runs the test suite across Node.js 18, 20, 22, and 24 with both Express 4 and Express 5.
|
|
17
17
|
|
|
18
|
+
## Module And TypeScript Compatibility
|
|
19
|
+
|
|
20
|
+
The published package remains CommonJS at runtime and supports both CommonJS and Node.js ESM consumers. Its TypeScript declarations model the CommonJS default export and the named properties exposed by the root and `/safe` entrypoints.
|
|
21
|
+
|
|
22
|
+
| Consumer | Module setup | Verified imports | Validation |
|
|
23
|
+
| --- | --- | --- | --- |
|
|
24
|
+
| TypeScript ESM | `"type": "module"` with `module` and `moduleResolution` set to `NodeNext` | Root default and named imports, public root types, and named `/safe` imports | TypeScript 6, `strict: true`, `skipLibCheck: false`, `esModuleInterop: false` |
|
|
25
|
+
| TypeScript CommonJS | `.cts` with NodeNext resolution | `import = require()` for the root and `/safe`, including namespace properties | TypeScript 6, `strict: true`, `skipLibCheck: false`, `esModuleInterop: false` |
|
|
26
|
+
| Node.js ESM | `.mjs` | Root default and named imports plus named `/safe` imports | Runtime smoke test |
|
|
27
|
+
| Node.js CommonJS | `.cjs` | `require()` for the root and `/safe`, including root default identity | Runtime smoke test |
|
|
28
|
+
|
|
29
|
+
The TypeScript ESM consumer covers `RpcEndpoint` as both the default and a named import, `RpcClient`, `RpcEndpointOptions`, `RpcSafeEndpoint`, and `RpcSafeClient`. The CommonJS consumer covers the constructable root export, `RpcEndpoint.RpcClient`, `Safe.RpcSafeEndpoint`, and `Safe.RpcSafeClient`.
|
|
30
|
+
|
|
31
|
+
TypeScript applications using Express should install `@types/express` and
|
|
32
|
+
`@types/node`; the package test installs these declarations in its isolated
|
|
33
|
+
consumer.
|
|
34
|
+
|
|
35
|
+
The TypeScript 6 fixtures set `ignoreDeprecations: "6.0"` solely to
|
|
36
|
+
acknowledge the compiler's deprecation notice for the deliberately explicit
|
|
37
|
+
`esModuleInterop: false` test setting.
|
|
38
|
+
|
|
39
|
+
### Packaged Consumer Validation
|
|
40
|
+
|
|
41
|
+
`npm run package-test` builds a tarball with `npm pack`, installs that tarball into isolated consumer fixtures, and runs the full matrix above. This verifies the published `exports` map, included files, declaration resolution, module extensions, and runtime format instead of importing the repository sources directly.
|
|
42
|
+
|
|
43
|
+
Package validation runs in CI and as part of `prepublishOnly`, so declaration or packaging regressions block publication.
|
|
44
|
+
|
|
18
45
|
## Express Dependency
|
|
19
46
|
|
|
20
47
|
Express is declared as a peer dependency:
|
|
@@ -34,4 +61,4 @@ The Express compatibility tests cover:
|
|
|
34
61
|
- Safe Mode endpoints;
|
|
35
62
|
- introspection reporting of the active Express runtime.
|
|
36
63
|
|
|
37
|
-
Broader RPC behavior is covered by the main test suite and is expected to run identically on Express 4 and Express 5.
|
|
64
|
+
Broader RPC behavior is covered by the main test suite and is expected to run identically on Express 4 and Express 5. Module and declaration compatibility is covered separately by the packaged consumer validation.
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rpc-express-toolkit",
|
|
3
|
-
"version": "4.4.
|
|
3
|
+
"version": "4.4.2",
|
|
4
4
|
"description": "JSON-RPC 2.0 toolkit for Express.js with simplified APIs, structured logging, middleware, schema validation, batch support, and optional Safe Mode.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"types": "src/index.d.ts",
|
|
7
|
+
"type": "commonjs",
|
|
7
8
|
"exports": {
|
|
8
9
|
".": {
|
|
9
10
|
"types": "./src/index.d.ts",
|
|
@@ -31,7 +32,9 @@
|
|
|
31
32
|
"lint:fix": "eslint src/ examples/ --fix",
|
|
32
33
|
"clean": "node -e \"require(\u0027fs\u0027).existsSync(\u0027dist\u0027) \u0026\u0026 require(\u0027fs\u0027).rmSync(\u0027dist\u0027, {recursive: true})\"",
|
|
33
34
|
"build": "node -e \"require.resolve(\u0027rpc-toolkit-js-client\u0027)\"",
|
|
34
|
-
"
|
|
35
|
+
"typecheck": "node test/package/runner.js --types-only",
|
|
36
|
+
"package-test": "node test/package/runner.js",
|
|
37
|
+
"prepublishOnly": "npm run build \u0026\u0026 npm run package-test",
|
|
35
38
|
"precommit": "npm run lint \u0026\u0026 npm test"
|
|
36
39
|
},
|
|
37
40
|
"repository": {
|
|
@@ -77,6 +80,9 @@
|
|
|
77
80
|
"node": "\u003e=18.0.0"
|
|
78
81
|
},
|
|
79
82
|
"devDependencies": {
|
|
83
|
+
"@arethetypeswrong/cli": "^0.18.5",
|
|
84
|
+
"@types/express": "^5.0.6",
|
|
85
|
+
"@types/node": "^18.19.0",
|
|
80
86
|
"eslint": "^8.57.1",
|
|
81
87
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
82
88
|
"eslint-config-prettier": "^9.1.2",
|
|
@@ -87,8 +93,10 @@
|
|
|
87
93
|
"jest": "^29.7.0",
|
|
88
94
|
"lint-staged": "^13.0.0",
|
|
89
95
|
"prettier": "^3.6.2",
|
|
96
|
+
"publint": "^0.3.22",
|
|
90
97
|
"supertest": "^6.3.3",
|
|
91
|
-
"terser": "^5.43.1"
|
|
98
|
+
"terser": "^5.43.1",
|
|
99
|
+
"typescript": "~6.0.3"
|
|
92
100
|
},
|
|
93
101
|
"peerDependencies": {
|
|
94
102
|
"express": "^4.21.2 || ^5.0.0"
|
|
@@ -97,7 +105,7 @@
|
|
|
97
105
|
"ajv": "^8.12.0",
|
|
98
106
|
"ajv-formats": "^2.1.1",
|
|
99
107
|
"nested-error-stacks": "^2.1.1",
|
|
100
|
-
"rpc-toolkit-js-client": "^1.1.
|
|
108
|
+
"rpc-toolkit-js-client": "^1.1.3"
|
|
101
109
|
},
|
|
102
110
|
"optionalDependencies": {
|
|
103
111
|
"node-fetch": "^2.7.0"
|
package/src/index.d.ts
CHANGED
|
@@ -1,290 +1,287 @@
|
|
|
1
1
|
// Type definitions for rpc-express-toolkit
|
|
2
2
|
|
|
3
|
-
import type {
|
|
3
|
+
import type { Request, Response, Router } from 'express';
|
|
4
4
|
import {
|
|
5
5
|
RpcClient as SharedRpcClient,
|
|
6
6
|
RpcError as SharedRpcError,
|
|
7
7
|
RpcHttpError as SharedRpcHttpError,
|
|
8
|
-
RpcSafeClient as SharedRpcSafeClient
|
|
8
|
+
RpcSafeClient as SharedRpcSafeClient,
|
|
9
9
|
} from 'rpc-toolkit-js-client';
|
|
10
10
|
import type {
|
|
11
11
|
RpcBatchRequest as SharedRpcBatchRequest,
|
|
12
|
-
RpcClientOptions as SharedRpcClientOptions
|
|
12
|
+
RpcClientOptions as SharedRpcClientOptions,
|
|
13
13
|
} from 'rpc-toolkit-js-client';
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
* JSON-RPC
|
|
16
|
+
* The main RpcEndpoint class for JSON-RPC 2.0 endpoints.
|
|
17
17
|
*/
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
declare class RpcEndpoint<C = any> {
|
|
19
|
+
constructor(router: Router, context: C, endpoint?: string);
|
|
20
|
+
constructor(
|
|
21
|
+
router: Router,
|
|
22
|
+
context: C,
|
|
23
|
+
options?: RpcEndpoint.RpcEndpointOptions
|
|
24
|
+
);
|
|
25
|
+
constructor(
|
|
26
|
+
router: Router,
|
|
27
|
+
context: C,
|
|
28
|
+
endpoint?: string,
|
|
29
|
+
options?: RpcEndpoint.RpcEndpointOptions
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
/** The endpoint path, for example "/api". */
|
|
33
|
+
get endpoint(): string;
|
|
23
34
|
|
|
24
|
-
/**
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
schema?: object;
|
|
30
|
-
}
|
|
35
|
+
/** All registered methods. */
|
|
36
|
+
get methods(): Record<
|
|
37
|
+
string,
|
|
38
|
+
RpcEndpoint.JSONRPCHandler<C> | RpcEndpoint.MethodConfig<C>
|
|
39
|
+
>;
|
|
31
40
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
code: number;
|
|
37
|
-
message: string;
|
|
38
|
-
data?: any;
|
|
39
|
-
}
|
|
41
|
+
get logger(): RpcEndpoint.Logger;
|
|
42
|
+
get middleware(): RpcEndpoint.MiddlewareManager;
|
|
43
|
+
get validator(): RpcEndpoint.SchemaValidator;
|
|
44
|
+
get options(): RpcEndpoint.RpcEndpointOptions;
|
|
40
45
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
46
|
+
addMethod(name: string, handler: RpcEndpoint.JSONRPCHandler<C>): void;
|
|
47
|
+
addMethod(
|
|
48
|
+
name: string,
|
|
49
|
+
handler: RpcEndpoint.JSONRPCHandler<C>,
|
|
50
|
+
schemaOrOptions: object
|
|
51
|
+
): void;
|
|
52
|
+
addMethod(name: string, config: RpcEndpoint.MethodConfig<C>): void;
|
|
49
53
|
|
|
50
|
-
|
|
51
|
-
* Logging configuration
|
|
52
|
-
*/
|
|
53
|
-
interface LoggingConfig {
|
|
54
|
-
level?: 'trace' | 'debug' | 'info' | 'warn' | 'error';
|
|
55
|
-
format?: 'json' | 'simple';
|
|
56
|
-
transports?: Array<'console' | 'file'>;
|
|
57
|
-
file?: {
|
|
58
|
-
filename?: string;
|
|
59
|
-
maxsize?: number;
|
|
60
|
-
maxFiles?: number;
|
|
61
|
-
};
|
|
62
|
-
}
|
|
54
|
+
use(hook: RpcEndpoint.MiddlewareHook, middleware: Function): void;
|
|
63
55
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
methods?: string[];
|
|
71
|
-
allowedHeaders?: string[];
|
|
72
|
-
}
|
|
56
|
+
removeMethod(name: string): void;
|
|
57
|
+
getMethod(
|
|
58
|
+
name: string
|
|
59
|
+
): RpcEndpoint.JSONRPCHandler<C> | RpcEndpoint.MethodConfig<C> | undefined;
|
|
60
|
+
listMethods(): string[];
|
|
61
|
+
getMetrics(): object;
|
|
73
62
|
|
|
74
|
-
|
|
75
|
-
* Rate limiting configuration
|
|
76
|
-
*/
|
|
77
|
-
interface RateLimitConfig {
|
|
78
|
-
windowMs?: number;
|
|
79
|
-
max?: number;
|
|
80
|
-
message?: string;
|
|
81
|
-
standardHeaders?: boolean;
|
|
82
|
-
legacyHeaders?: boolean;
|
|
83
|
-
}
|
|
63
|
+
static serveScripts(router: Router, url?: string): void;
|
|
84
64
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
65
|
+
reply(
|
|
66
|
+
res: Response,
|
|
67
|
+
responsePayload: RpcEndpoint.JSONRPCResponsePayload
|
|
68
|
+
): void;
|
|
89
69
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
coerceTypes?: boolean;
|
|
96
|
-
removeAdditional?: boolean;
|
|
70
|
+
serializeBigIntsAndDates(value: any): any;
|
|
71
|
+
deserializeBigIntsAndDates(
|
|
72
|
+
value: any,
|
|
73
|
+
options?: RpcEndpoint.DeserializationOptions
|
|
74
|
+
): any;
|
|
97
75
|
}
|
|
98
76
|
|
|
99
77
|
/**
|
|
100
|
-
*
|
|
78
|
+
* Public properties and types attached to the CommonJS export.
|
|
101
79
|
*/
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
rateLimit?: RateLimitConfig;
|
|
113
|
-
logging?: LoggingConfig;
|
|
114
|
-
warnOnUnsafe?: boolean;
|
|
115
|
-
strictMode?: boolean;
|
|
116
|
-
maxSerializationDepth?: number;
|
|
117
|
-
maxDeserializationDepth?: number;
|
|
118
|
-
autoJsonMiddleware?: boolean;
|
|
119
|
-
jsonOptions?: {
|
|
120
|
-
limit?: string;
|
|
121
|
-
strict?: boolean;
|
|
122
|
-
type?: string | string[] | ((req: any) => boolean);
|
|
123
|
-
verify?: (req: any, res: any, buf: Buffer, encoding: string) => void;
|
|
80
|
+
declare namespace RpcEndpoint {
|
|
81
|
+
// module.exports.RpcEndpoint === module.exports
|
|
82
|
+
export { RpcEndpoint };
|
|
83
|
+
|
|
84
|
+
// Classes re-exported from rpc-toolkit-js-client.
|
|
85
|
+
export {
|
|
86
|
+
SharedRpcClient as RpcClient,
|
|
87
|
+
SharedRpcError as RpcError,
|
|
88
|
+
SharedRpcHttpError as RpcHttpError,
|
|
89
|
+
SharedRpcSafeClient as RpcSafeClient,
|
|
124
90
|
};
|
|
125
|
-
// Additional options supported by implementation
|
|
126
|
-
methodWhitelist?: string[];
|
|
127
|
-
timing?: boolean;
|
|
128
|
-
metrics?: boolean;
|
|
129
|
-
healthCheck?: boolean;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* RPC client configuration options
|
|
134
|
-
*/
|
|
135
|
-
type RpcClientOptions = SharedRpcClientOptions;
|
|
136
|
-
type RpcBatchRequest = SharedRpcBatchRequest;
|
|
137
|
-
type RpcClient = SharedRpcClient;
|
|
138
|
-
type RpcError = SharedRpcError;
|
|
139
|
-
type RpcHttpError = SharedRpcHttpError;
|
|
140
|
-
type RpcSafeClient = SharedRpcSafeClient;
|
|
141
|
-
|
|
142
|
-
declare const RpcClient: typeof SharedRpcClient;
|
|
143
|
-
declare const RpcError: typeof SharedRpcError;
|
|
144
|
-
declare const RpcHttpError: typeof SharedRpcHttpError;
|
|
145
|
-
declare const RpcSafeClient: typeof SharedRpcSafeClient;
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Deserialization options for safe prefixes
|
|
149
|
-
*/
|
|
150
|
-
interface DeserializationOptions {
|
|
151
|
-
safeEnabled?: boolean;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* The main RpcEndpoint class for JSON-RPC 2.0 endpoints.
|
|
156
|
-
*/
|
|
157
|
-
declare class RpcEndpoint<C = any> {
|
|
158
|
-
/**
|
|
159
|
-
* Constructor.
|
|
160
|
-
*/
|
|
161
|
-
constructor(router: Router, context: C, endpoint?: string);
|
|
162
|
-
constructor(router: Router, context: C, options?: RpcEndpointOptions);
|
|
163
|
-
constructor(router: Router, context: C, endpoint?: string, options?: RpcEndpointOptions);
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Returns the endpoint path, e.g. "/api".
|
|
167
|
-
*/
|
|
168
|
-
get endpoint(): string;
|
|
169
91
|
|
|
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
|
-
|
|
92
|
+
/** JSON-RPC handler function. */
|
|
93
|
+
export type JSONRPCHandler<C> = (
|
|
94
|
+
req: Request,
|
|
95
|
+
context: C,
|
|
96
|
+
params: any
|
|
97
|
+
) => any | Promise<any>;
|
|
98
|
+
|
|
99
|
+
/** Method configuration object. */
|
|
100
|
+
export interface MethodConfig<C> {
|
|
101
|
+
handler: JSONRPCHandler<C>;
|
|
102
|
+
schema?: object;
|
|
103
|
+
exposeSchema?: boolean;
|
|
104
|
+
description?: string;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** JSON-RPC error object. */
|
|
108
|
+
export interface JSONRPCError {
|
|
109
|
+
code: number;
|
|
110
|
+
message: string;
|
|
111
|
+
data?: any;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** JSON-RPC response payload. */
|
|
115
|
+
export interface JSONRPCResponsePayload {
|
|
116
|
+
id?: string | number | null;
|
|
117
|
+
result?: any;
|
|
118
|
+
error?: JSONRPCError;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error';
|
|
122
|
+
|
|
123
|
+
/** Logging configuration. */
|
|
124
|
+
export interface LoggingConfig {
|
|
125
|
+
level?: LogLevel;
|
|
126
|
+
enabled?: boolean;
|
|
127
|
+
prefix?: string;
|
|
128
|
+
logger?: Partial<
|
|
129
|
+
Record<LogLevel, (entry: Record<string, unknown>) => void>
|
|
130
|
+
>;
|
|
131
|
+
format?: 'json' | 'simple';
|
|
132
|
+
transports?: Array<'console' | 'file'>;
|
|
133
|
+
file?: {
|
|
134
|
+
filename?: string;
|
|
135
|
+
maxsize?: number;
|
|
136
|
+
maxFiles?: number;
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** CORS configuration. */
|
|
141
|
+
export interface CorsConfig {
|
|
142
|
+
origin?: string | string[] | boolean;
|
|
143
|
+
credentials?: boolean;
|
|
144
|
+
methods?: string[];
|
|
145
|
+
allowedHeaders?: string[];
|
|
146
|
+
headers?: string[];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Rate limiting configuration. */
|
|
150
|
+
export interface RateLimitConfig {
|
|
151
|
+
windowMs?: number;
|
|
152
|
+
max?: number;
|
|
153
|
+
message?: string;
|
|
154
|
+
standardHeaders?: boolean;
|
|
155
|
+
legacyHeaders?: boolean;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Authentication verifier. Return a truthy value to allow the call. */
|
|
159
|
+
export type AuthConfig = (req: Request) => boolean | Promise<boolean>;
|
|
160
|
+
|
|
161
|
+
/** Schema validation configuration. */
|
|
162
|
+
export interface ValidationConfig {
|
|
163
|
+
strict?: boolean;
|
|
164
|
+
coerceTypes?: boolean;
|
|
165
|
+
removeAdditional?: boolean;
|
|
166
|
+
useDefaults?: boolean;
|
|
167
|
+
ajvOptions?: Record<string, unknown>;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** RPC endpoint configuration options. */
|
|
171
|
+
export interface RpcEndpointOptions {
|
|
172
|
+
endpoint?: string;
|
|
173
|
+
prefix?: string;
|
|
174
|
+
maxBodySize?: string;
|
|
175
|
+
timeout?: number;
|
|
176
|
+
cors?: boolean | CorsConfig;
|
|
177
|
+
auth?: AuthConfig;
|
|
178
|
+
validation?: ValidationConfig;
|
|
179
|
+
safeEnabled?: boolean;
|
|
180
|
+
crossConfigurationEnabled?: boolean;
|
|
181
|
+
enableSchema?: boolean;
|
|
182
|
+
rateLimit?: RateLimitConfig;
|
|
183
|
+
logging?: LoggingConfig;
|
|
184
|
+
warnOnUnsafe?: boolean;
|
|
185
|
+
strictMode?: boolean;
|
|
186
|
+
maxSerializationDepth?: number;
|
|
187
|
+
maxDeserializationDepth?: number;
|
|
188
|
+
autoJsonMiddleware?: boolean;
|
|
189
|
+
jsonOptions?: {
|
|
190
|
+
limit?: string;
|
|
191
|
+
strict?: boolean;
|
|
192
|
+
type?: string | string[] | ((req: any) => boolean);
|
|
193
|
+
verify?: (req: any, res: any, buf: Buffer, encoding: string) => void;
|
|
194
|
+
};
|
|
195
|
+
methodWhitelist?: string[];
|
|
196
|
+
timing?: boolean;
|
|
197
|
+
metrics?: boolean;
|
|
198
|
+
healthCheck?: boolean;
|
|
199
|
+
enableIntrospection?: boolean;
|
|
200
|
+
introspectionPrefix?: string;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** RPC client configuration options. */
|
|
204
|
+
export type RpcClientOptions = SharedRpcClientOptions;
|
|
205
|
+
export type RpcBatchRequest = SharedRpcBatchRequest;
|
|
206
|
+
|
|
207
|
+
/** Deserialization options for safe prefixes. */
|
|
208
|
+
export interface DeserializationOptions {
|
|
209
|
+
safeEnabled?: boolean;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export type MiddlewareHook =
|
|
213
|
+
| 'beforeCall'
|
|
214
|
+
| 'afterCall'
|
|
215
|
+
| 'onError'
|
|
216
|
+
| 'beforeValidation'
|
|
217
|
+
| 'afterValidation';
|
|
218
|
+
|
|
219
|
+
export interface MiddlewareContext<C = any> {
|
|
220
|
+
req?: Request;
|
|
221
|
+
res?: Response | null;
|
|
222
|
+
context?: C;
|
|
223
|
+
method?: string;
|
|
224
|
+
params?: any;
|
|
225
|
+
result?: any;
|
|
226
|
+
error?: any;
|
|
227
|
+
batchIndex?: number;
|
|
228
|
+
isNotification?: boolean;
|
|
229
|
+
startTime?: number;
|
|
230
|
+
[key: string]: any;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export type Middleware = (context: MiddlewareContext) => any | Promise<any>;
|
|
234
|
+
|
|
235
|
+
export interface RpcMetrics {
|
|
236
|
+
methods: number;
|
|
237
|
+
middleware: {
|
|
238
|
+
beforeCall: number;
|
|
239
|
+
afterCall: number;
|
|
240
|
+
onError: number;
|
|
241
|
+
};
|
|
242
|
+
uptime: number;
|
|
243
|
+
memory: Record<string, number>;
|
|
244
|
+
timestamp: string;
|
|
245
|
+
}
|
|
201
246
|
|
|
202
247
|
/**
|
|
203
|
-
*
|
|
248
|
+
* Utility helpers were historically exposed as `any`. Keep their instance
|
|
249
|
+
* contracts permissive in this patch while still publishing value/type names.
|
|
204
250
|
*/
|
|
205
|
-
|
|
251
|
+
export type Logger = any;
|
|
252
|
+
export const Logger: {
|
|
253
|
+
new (...args: any[]): Logger;
|
|
254
|
+
};
|
|
206
255
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
256
|
+
export type MiddlewareManager = any;
|
|
257
|
+
export const MiddlewareManager: {
|
|
258
|
+
new (...args: any[]): MiddlewareManager;
|
|
259
|
+
};
|
|
211
260
|
|
|
212
|
-
|
|
213
|
-
* List all registered methods
|
|
214
|
-
*/
|
|
215
|
-
listMethods(): string[];
|
|
261
|
+
export const builtInMiddlewares: any;
|
|
216
262
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
263
|
+
export interface SchemaValidationResult<T = any> {
|
|
264
|
+
valid: boolean;
|
|
265
|
+
errors: any[] | null;
|
|
266
|
+
data: T;
|
|
267
|
+
}
|
|
221
268
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
269
|
+
export type SchemaValidator = any;
|
|
270
|
+
export const SchemaValidator: {
|
|
271
|
+
new (...args: any[]): SchemaValidator;
|
|
272
|
+
};
|
|
226
273
|
|
|
227
|
-
|
|
228
|
-
* Send a JSON-RPC 2.0 response to the client.
|
|
229
|
-
*/
|
|
230
|
-
reply(res: Response, responsePayload: JSONRPCResponsePayload): void;
|
|
274
|
+
export const commonSchemas: any;
|
|
231
275
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
276
|
+
export type SchemaBuilder = any;
|
|
277
|
+
export const SchemaBuilder: {
|
|
278
|
+
new (...args: any[]): SchemaBuilder;
|
|
279
|
+
};
|
|
236
280
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
281
|
+
export type BatchHandler = any;
|
|
282
|
+
export const BatchHandler: {
|
|
283
|
+
new (...args: any[]): BatchHandler;
|
|
284
|
+
};
|
|
241
285
|
}
|
|
242
286
|
|
|
243
|
-
|
|
244
|
-
* Export types for external use
|
|
245
|
-
*/
|
|
246
|
-
export {
|
|
247
|
-
JSONRPCHandler,
|
|
248
|
-
MethodConfig,
|
|
249
|
-
JSONRPCError,
|
|
250
|
-
JSONRPCResponsePayload,
|
|
251
|
-
LoggingConfig,
|
|
252
|
-
CorsConfig,
|
|
253
|
-
RateLimitConfig,
|
|
254
|
-
AuthConfig,
|
|
255
|
-
ValidationConfig,
|
|
256
|
-
RpcEndpointOptions,
|
|
257
|
-
RpcClientOptions,
|
|
258
|
-
RpcBatchRequest,
|
|
259
|
-
DeserializationOptions,
|
|
260
|
-
RpcEndpoint,
|
|
261
|
-
RpcClient,
|
|
262
|
-
RpcError,
|
|
263
|
-
RpcHttpError,
|
|
264
|
-
RpcSafeClient
|
|
265
|
-
};
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Main export - RpcEndpoint class with static properties
|
|
269
|
-
*/
|
|
270
|
-
declare const Main: {
|
|
271
|
-
new <C>(router: Router, context: C, endpoint?: string): RpcEndpoint<C>;
|
|
272
|
-
new <C>(router: Router, context: C, options?: RpcEndpointOptions): RpcEndpoint<C>;
|
|
273
|
-
new <C>(router: Router, context: C, endpoint?: string, options?: RpcEndpointOptions): RpcEndpoint<C>;
|
|
274
|
-
prototype: RpcEndpoint<any>;
|
|
275
|
-
serveScripts(router: Router, url?: string): void;
|
|
276
|
-
RpcEndpoint: typeof RpcEndpoint;
|
|
277
|
-
RpcClient: typeof RpcClient;
|
|
278
|
-
RpcError: typeof RpcError;
|
|
279
|
-
RpcHttpError: typeof RpcHttpError;
|
|
280
|
-
RpcSafeClient: typeof RpcSafeClient;
|
|
281
|
-
Logger: any;
|
|
282
|
-
MiddlewareManager: any;
|
|
283
|
-
builtInMiddlewares: any;
|
|
284
|
-
SchemaValidator: any;
|
|
285
|
-
commonSchemas: any;
|
|
286
|
-
SchemaBuilder: any;
|
|
287
|
-
BatchHandler: any;
|
|
288
|
-
};
|
|
289
|
-
|
|
290
|
-
export = Main;
|
|
287
|
+
export = RpcEndpoint;
|
package/src/index.js
CHANGED
|
@@ -1158,3 +1158,6 @@ module.exports.SchemaValidator = SchemaValidator;
|
|
|
1158
1158
|
module.exports.commonSchemas = commonSchemas;
|
|
1159
1159
|
module.exports.SchemaBuilder = SchemaBuilder;
|
|
1160
1160
|
module.exports.BatchHandler = BatchHandler;
|
|
1161
|
+
// Keep the existing static method discoverable as a named export to Node's
|
|
1162
|
+
// CommonJS lexer without changing the RpcEndpoint object or its descriptor.
|
|
1163
|
+
module.exports.serveScripts = RpcEndpoint.serveScripts;
|
package/src/safe.d.ts
CHANGED
|
@@ -1,63 +1,91 @@
|
|
|
1
1
|
import type { Router } from 'express';
|
|
2
|
-
import
|
|
2
|
+
import Main = require('./index');
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
* Extends RpcClient with safeEnabled: true preset.
|
|
5
|
+
* Public properties attached to the CommonJS safe preset export.
|
|
7
6
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
declare namespace Safe {
|
|
8
|
+
// Root values re-exported by src/safe.js.
|
|
9
|
+
export import RpcEndpoint = Main;
|
|
10
|
+
export import RpcClient = Main.RpcClient;
|
|
11
|
+
export import RpcError = Main.RpcError;
|
|
12
|
+
export import RpcHttpError = Main.RpcHttpError;
|
|
13
|
+
export import Logger = Main.Logger;
|
|
14
|
+
export import MiddlewareManager = Main.MiddlewareManager;
|
|
15
|
+
export import builtInMiddlewares = Main.builtInMiddlewares;
|
|
16
|
+
export import SchemaValidator = Main.SchemaValidator;
|
|
17
|
+
export import commonSchemas = Main.commonSchemas;
|
|
18
|
+
export import SchemaBuilder = Main.SchemaBuilder;
|
|
19
|
+
export import BatchHandler = Main.BatchHandler;
|
|
15
20
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
export
|
|
21
|
-
|
|
21
|
+
// Root public types previously re-exported by `export *`.
|
|
22
|
+
export import JSONRPCHandler = Main.JSONRPCHandler;
|
|
23
|
+
export import MethodConfig = Main.MethodConfig;
|
|
24
|
+
export import JSONRPCError = Main.JSONRPCError;
|
|
25
|
+
export import JSONRPCResponsePayload = Main.JSONRPCResponsePayload;
|
|
26
|
+
export import LogLevel = Main.LogLevel;
|
|
27
|
+
export import LoggingConfig = Main.LoggingConfig;
|
|
28
|
+
export import CorsConfig = Main.CorsConfig;
|
|
29
|
+
export import RateLimitConfig = Main.RateLimitConfig;
|
|
30
|
+
export import AuthConfig = Main.AuthConfig;
|
|
31
|
+
export import ValidationConfig = Main.ValidationConfig;
|
|
32
|
+
export import RpcEndpointOptions = Main.RpcEndpointOptions;
|
|
33
|
+
export import RpcClientOptions = Main.RpcClientOptions;
|
|
34
|
+
export import RpcBatchRequest = Main.RpcBatchRequest;
|
|
35
|
+
export import DeserializationOptions = Main.DeserializationOptions;
|
|
36
|
+
export import MiddlewareHook = Main.MiddlewareHook;
|
|
37
|
+
export import MiddlewareContext = Main.MiddlewareContext;
|
|
38
|
+
export import Middleware = Main.Middleware;
|
|
39
|
+
export import RpcMetrics = Main.RpcMetrics;
|
|
40
|
+
export import SchemaValidationResult = Main.SchemaValidationResult;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* RPC client with safe serialization enabled by default.
|
|
44
|
+
*/
|
|
45
|
+
export class RpcSafeClient extends Main.RpcClient {
|
|
46
|
+
constructor(
|
|
47
|
+
endpoint: string,
|
|
48
|
+
defaultHeaders?: Record<string, string>,
|
|
49
|
+
options?: Main.RpcClientOptions
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* RPC endpoint with safe serialization and strict mode enabled by default.
|
|
55
|
+
*/
|
|
56
|
+
export class RpcSafeEndpoint<C = any> extends Main<C> {
|
|
57
|
+
constructor(
|
|
58
|
+
router: Router,
|
|
59
|
+
context: C,
|
|
60
|
+
endpoint?: string,
|
|
61
|
+
options?: Main.RpcEndpointOptions
|
|
62
|
+
);
|
|
63
|
+
constructor(router: Router, context: C, options?: Main.RpcEndpointOptions);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @deprecated Use `new RpcSafeEndpoint(...)` instead.
|
|
68
|
+
*/
|
|
69
|
+
export function createSafeEndpoint<C = any>(
|
|
22
70
|
router: Router,
|
|
23
71
|
context: C,
|
|
24
72
|
endpoint?: string,
|
|
25
|
-
options?: RpcEndpointOptions
|
|
26
|
-
)
|
|
27
|
-
|
|
28
|
-
constructor(
|
|
73
|
+
options?: Main.RpcEndpointOptions
|
|
74
|
+
): RpcSafeEndpoint<C>;
|
|
75
|
+
export function createSafeEndpoint<C = any>(
|
|
29
76
|
router: Router,
|
|
30
77
|
context: C,
|
|
31
|
-
options?: RpcEndpointOptions
|
|
32
|
-
)
|
|
33
|
-
}
|
|
78
|
+
options?: Main.RpcEndpointOptions
|
|
79
|
+
): RpcSafeEndpoint<C>;
|
|
34
80
|
|
|
35
|
-
/**
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
): RpcEndpoint<C>;
|
|
45
|
-
|
|
46
|
-
export function createSafeEndpoint<C = any>(
|
|
47
|
-
router: Router,
|
|
48
|
-
context: C,
|
|
49
|
-
options?: RpcEndpointOptions
|
|
50
|
-
): RpcEndpoint<C>;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* @deprecated Use `new RpcSafeClient(...)` instead.
|
|
54
|
-
* Factory: create a RpcClient with safe defaults (safeEnabled=true).
|
|
55
|
-
*/
|
|
56
|
-
export function createSafeClient(
|
|
57
|
-
endpoint: string,
|
|
58
|
-
defaultHeaders?: Record<string, string>,
|
|
59
|
-
options?: RpcClientOptions
|
|
60
|
-
): RpcClient;
|
|
61
|
-
|
|
62
|
-
export * from './index';
|
|
81
|
+
/**
|
|
82
|
+
* @deprecated Use `new RpcSafeClient(...)` instead.
|
|
83
|
+
*/
|
|
84
|
+
export function createSafeClient(
|
|
85
|
+
endpoint: string,
|
|
86
|
+
defaultHeaders?: Record<string, string>,
|
|
87
|
+
options?: Main.RpcClientOptions
|
|
88
|
+
): RpcSafeClient;
|
|
89
|
+
}
|
|
63
90
|
|
|
91
|
+
export = Safe;
|
package/src/safe.js
CHANGED
|
@@ -110,3 +110,17 @@ module.exports = {
|
|
|
110
110
|
createSafeEndpoint,
|
|
111
111
|
createSafeClient,
|
|
112
112
|
};
|
|
113
|
+
|
|
114
|
+
// Make spread-based root re-exports visible to Node's CommonJS named-export
|
|
115
|
+
// detection. These assignments preserve the existing object values and order.
|
|
116
|
+
module.exports.RpcEndpoint = Main.RpcEndpoint;
|
|
117
|
+
module.exports.RpcClient = Main.RpcClient;
|
|
118
|
+
module.exports.RpcError = Main.RpcError;
|
|
119
|
+
module.exports.RpcHttpError = Main.RpcHttpError;
|
|
120
|
+
module.exports.Logger = Main.Logger;
|
|
121
|
+
module.exports.MiddlewareManager = Main.MiddlewareManager;
|
|
122
|
+
module.exports.builtInMiddlewares = Main.builtInMiddlewares;
|
|
123
|
+
module.exports.SchemaValidator = Main.SchemaValidator;
|
|
124
|
+
module.exports.commonSchemas = Main.commonSchemas;
|
|
125
|
+
module.exports.SchemaBuilder = Main.SchemaBuilder;
|
|
126
|
+
module.exports.BatchHandler = Main.BatchHandler;
|