@msaki/jsonrpc 0.0.3 → 0.0.4
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/client.d.ts +9 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +30 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +4 -47
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -133
- package/dist/index.js.map +1 -0
- package/dist/server.d.ts +15 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +59 -0
- package/dist/server.js.map +1 -0
- package/dist/types.d.ts +24 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/types.js.map +1 -0
- package/package.json +8 -5
- package/src/index.ts +3 -0
- package/dist/index.d.mts +0 -47
- package/dist/index.mjs +0 -99
- package/index.ts +0 -3
- package/tsconfig.json +0 -28
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { JsonRpcRequest, JsonRpcResponse } from "./types";
|
|
2
|
+
export declare class JsonRpcClient {
|
|
3
|
+
private send;
|
|
4
|
+
private id;
|
|
5
|
+
constructor(send: (req: JsonRpcRequest) => Promise<JsonRpcResponse>);
|
|
6
|
+
call<T = unknown>(method: string, params?: unknown): Promise<T>;
|
|
7
|
+
notify(method: string, params?: unknown): Promise<JsonRpcResponse>;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/D,qBAAa,aAAa;IAItB,OAAO,CAAC,IAAI;IAHd,OAAO,CAAC,EAAE,CAAI;gBAGJ,IAAI,EAAE,CAAC,GAAG,EAAE,cAAc,KAAK,OAAO,CAAC,eAAe,CAAC;IAG3D,IAAI,CAAC,CAAC,GAAG,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC;IAerE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO;CAQxC"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export class JsonRpcClient {
|
|
2
|
+
send;
|
|
3
|
+
id = 0;
|
|
4
|
+
constructor(send) {
|
|
5
|
+
this.send = send;
|
|
6
|
+
}
|
|
7
|
+
async call(method, params) {
|
|
8
|
+
const request = {
|
|
9
|
+
jsonrpc: "2.0",
|
|
10
|
+
method,
|
|
11
|
+
params,
|
|
12
|
+
id: ++this.id,
|
|
13
|
+
};
|
|
14
|
+
const response = await this.send(request);
|
|
15
|
+
if ("error" in response) {
|
|
16
|
+
const e = response.error;
|
|
17
|
+
throw new Error(`RPC error ${e.code} ${e.message}`);
|
|
18
|
+
}
|
|
19
|
+
return response.result;
|
|
20
|
+
}
|
|
21
|
+
notify(method, params) {
|
|
22
|
+
const request = {
|
|
23
|
+
jsonrpc: "2.0",
|
|
24
|
+
method,
|
|
25
|
+
params
|
|
26
|
+
};
|
|
27
|
+
return this.send(request);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,aAAa;IAId;IAHF,EAAE,GAAG,CAAC,CAAA;IAEd,YACU,IAAuD;QAAvD,SAAI,GAAJ,IAAI,CAAmD;IAC9D,CAAC;IAEJ,KAAK,CAAC,IAAI,CAAc,MAAc,EAAE,MAAgB;QACtD,MAAM,OAAO,GAAmB;YAC9B,OAAO,EAAE,KAAK;YACd,MAAM;YACN,MAAM;YACN,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE;SACd,CAAA;QACD,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC1C,IAAI,OAAO,IAAI,QAAQ,EAAE,CAAC;YACxB,MAAM,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAA;YACxB,MAAM,IAAI,KAAK,CAAC,aAAa,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,OAAO,EAAE,CAAC,CAAA;QACrD,CAAC;QACD,OAAO,QAAQ,CAAC,MAAW,CAAA;IAC7B,CAAC;IAED,MAAM,CAAC,MAAc,EAAE,MAAgB;QACrC,MAAM,OAAO,GAAmB;YAC9B,OAAO,EAAE,KAAK;YACd,MAAM;YACN,MAAM;SACP,CAAA;QACD,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IAC3B,CAAC;CACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,47 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
params?: unknown;
|
|
6
|
-
id?: JsonRpcId;
|
|
7
|
-
}
|
|
8
|
-
interface JsonRpcSuccess {
|
|
9
|
-
jsonrpc: "2.0";
|
|
10
|
-
result: unknown;
|
|
11
|
-
id: JsonRpcId;
|
|
12
|
-
}
|
|
13
|
-
interface JsonRpcErrorObject {
|
|
14
|
-
code: number;
|
|
15
|
-
message: string;
|
|
16
|
-
data?: unknown;
|
|
17
|
-
}
|
|
18
|
-
interface JsonRpcError {
|
|
19
|
-
jsonrpc: "2.0";
|
|
20
|
-
error: JsonRpcErrorObject;
|
|
21
|
-
id: JsonRpcId;
|
|
22
|
-
}
|
|
23
|
-
type JsonRpcResponse = JsonRpcSuccess | JsonRpcError;
|
|
24
|
-
|
|
25
|
-
declare const INVALID_REQUEST = -32600;
|
|
26
|
-
declare const METHOD_NOT_FOUND = -32601;
|
|
27
|
-
declare const INVALID_PARAMS = -32602;
|
|
28
|
-
declare const INTERNAL_ERROR = -32603;
|
|
29
|
-
declare const PARSE_ERROR = -32700;
|
|
30
|
-
declare const REQUEST_ABORTED = -32800;
|
|
31
|
-
type Handler = (params: any) => any | Promise<any>;
|
|
32
|
-
declare class JsonRpcServer {
|
|
33
|
-
private methods;
|
|
34
|
-
register(method: string, handler: Handler): void;
|
|
35
|
-
handle(raw: unknown): Promise<JsonRpcResponse | JsonRpcResponse[] | null>;
|
|
36
|
-
private error;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
declare class JsonRpcClient {
|
|
40
|
-
private send;
|
|
41
|
-
private id;
|
|
42
|
-
constructor(send: (req: JsonRpcRequest) => Promise<JsonRpcResponse>);
|
|
43
|
-
call<T = unknown>(method: string, params?: unknown): Promise<T>;
|
|
44
|
-
notify(method: string, params?: unknown): Promise<JsonRpcResponse>;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export { type Handler, INTERNAL_ERROR, INVALID_PARAMS, INVALID_REQUEST, JsonRpcClient, type JsonRpcError, type JsonRpcErrorObject, type JsonRpcId, type JsonRpcRequest, type JsonRpcResponse, JsonRpcServer, type JsonRpcSuccess, METHOD_NOT_FOUND, PARSE_ERROR, REQUEST_ABORTED };
|
|
1
|
+
export * from "./types";
|
|
2
|
+
export * from "./server";
|
|
3
|
+
export * from "./client";
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAA;AACvB,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,133 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
INTERNAL_ERROR: () => INTERNAL_ERROR,
|
|
24
|
-
INVALID_PARAMS: () => INVALID_PARAMS,
|
|
25
|
-
INVALID_REQUEST: () => INVALID_REQUEST,
|
|
26
|
-
JsonRpcClient: () => JsonRpcClient,
|
|
27
|
-
JsonRpcServer: () => JsonRpcServer,
|
|
28
|
-
METHOD_NOT_FOUND: () => METHOD_NOT_FOUND,
|
|
29
|
-
PARSE_ERROR: () => PARSE_ERROR,
|
|
30
|
-
REQUEST_ABORTED: () => REQUEST_ABORTED
|
|
31
|
-
});
|
|
32
|
-
module.exports = __toCommonJS(index_exports);
|
|
33
|
-
|
|
34
|
-
// src/server.ts
|
|
35
|
-
var INVALID_REQUEST = -32600;
|
|
36
|
-
var METHOD_NOT_FOUND = -32601;
|
|
37
|
-
var INVALID_PARAMS = -32602;
|
|
38
|
-
var INTERNAL_ERROR = -32603;
|
|
39
|
-
var PARSE_ERROR = -32700;
|
|
40
|
-
var REQUEST_ABORTED = -32800;
|
|
41
|
-
var JsonRpcServer = class {
|
|
42
|
-
methods = /* @__PURE__ */ new Map();
|
|
43
|
-
register(method, handler) {
|
|
44
|
-
this.methods.set(method, handler);
|
|
45
|
-
}
|
|
46
|
-
async handle(raw) {
|
|
47
|
-
if (Array.isArray(raw)) {
|
|
48
|
-
if (raw.length === 0) {
|
|
49
|
-
return this.error(null, INVALID_REQUEST, "Invalid request");
|
|
50
|
-
}
|
|
51
|
-
const responses = await Promise.all(
|
|
52
|
-
raw.map((item) => this.handle(item))
|
|
53
|
-
);
|
|
54
|
-
const filtered = responses.filter(
|
|
55
|
-
(r) => r !== null
|
|
56
|
-
);
|
|
57
|
-
return filtered.length > 0 ? filtered : null;
|
|
58
|
-
}
|
|
59
|
-
let req = raw;
|
|
60
|
-
if (typeof req !== "object" || req === null || req.jsonrpc !== "2.0" || typeof req.method !== "string") {
|
|
61
|
-
return this.error(null, INVALID_REQUEST, "Invalid request");
|
|
62
|
-
}
|
|
63
|
-
const id = typeof req?.id === "string" || typeof req?.id === "number" || req?.id === null ? req.id : null;
|
|
64
|
-
const handler = this.methods.get(req.method);
|
|
65
|
-
if (!handler) {
|
|
66
|
-
return id === null ? null : this.error(req.id, METHOD_NOT_FOUND, "Method not found");
|
|
67
|
-
}
|
|
68
|
-
try {
|
|
69
|
-
const result = await handler(req.params);
|
|
70
|
-
if (req.id === void 0) return null;
|
|
71
|
-
return {
|
|
72
|
-
jsonrpc: "2.0",
|
|
73
|
-
result,
|
|
74
|
-
id: req.id
|
|
75
|
-
};
|
|
76
|
-
} catch (err) {
|
|
77
|
-
return this.error(
|
|
78
|
-
req.id ?? null,
|
|
79
|
-
INTERNAL_ERROR,
|
|
80
|
-
"Internal error",
|
|
81
|
-
err instanceof Error ? err.message : err
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
error(id, code, message, data) {
|
|
86
|
-
return {
|
|
87
|
-
jsonrpc: "2.0",
|
|
88
|
-
error: { code, message, data },
|
|
89
|
-
id: id ?? null
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
// src/client.ts
|
|
95
|
-
var JsonRpcClient = class {
|
|
96
|
-
constructor(send) {
|
|
97
|
-
this.send = send;
|
|
98
|
-
}
|
|
99
|
-
id = 0;
|
|
100
|
-
async call(method, params) {
|
|
101
|
-
const request = {
|
|
102
|
-
jsonrpc: "2.0",
|
|
103
|
-
method,
|
|
104
|
-
params,
|
|
105
|
-
id: ++this.id
|
|
106
|
-
};
|
|
107
|
-
const response = await this.send(request);
|
|
108
|
-
if ("error" in response) {
|
|
109
|
-
const e = response.error;
|
|
110
|
-
throw new Error(`RPC error ${e.code} ${e.message}`);
|
|
111
|
-
}
|
|
112
|
-
return response.result;
|
|
113
|
-
}
|
|
114
|
-
notify(method, params) {
|
|
115
|
-
const request = {
|
|
116
|
-
jsonrpc: "2.0",
|
|
117
|
-
method,
|
|
118
|
-
params
|
|
119
|
-
};
|
|
120
|
-
return this.send(request);
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
124
|
-
0 && (module.exports = {
|
|
125
|
-
INTERNAL_ERROR,
|
|
126
|
-
INVALID_PARAMS,
|
|
127
|
-
INVALID_REQUEST,
|
|
128
|
-
JsonRpcClient,
|
|
129
|
-
JsonRpcServer,
|
|
130
|
-
METHOD_NOT_FOUND,
|
|
131
|
-
PARSE_ERROR,
|
|
132
|
-
REQUEST_ABORTED
|
|
133
|
-
});
|
|
1
|
+
export * from "./types";
|
|
2
|
+
export * from "./server";
|
|
3
|
+
export * from "./client";
|
|
4
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAA;AACvB,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC"}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { JsonRpcResponse } from "./types";
|
|
2
|
+
export declare const INVALID_REQUEST = -32600;
|
|
3
|
+
export declare const METHOD_NOT_FOUND = -32601;
|
|
4
|
+
export declare const INVALID_PARAMS = -32602;
|
|
5
|
+
export declare const INTERNAL_ERROR = -32603;
|
|
6
|
+
export declare const PARSE_ERROR = -32700;
|
|
7
|
+
export declare const REQUEST_ABORTED = -32800;
|
|
8
|
+
export type Handler = (params: any) => any | Promise<any>;
|
|
9
|
+
export declare class JsonRpcServer {
|
|
10
|
+
private methods;
|
|
11
|
+
register(method: string, handler: Handler): void;
|
|
12
|
+
handle(raw: unknown): Promise<JsonRpcResponse | JsonRpcResponse[] | null>;
|
|
13
|
+
private error;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAmB,MAAM,SAAS,CAAC;AAEhE,eAAO,MAAM,eAAe,SAAU,CAAC;AACvC,eAAO,MAAM,gBAAgB,SAAS,CAAC;AACvC,eAAO,MAAM,cAAc,SAAW,CAAC;AACvC,eAAO,MAAM,cAAc,SAAW,CAAC;AACvC,eAAO,MAAM,WAAW,SAAc,CAAC;AACvC,eAAO,MAAM,eAAe,SAAU,CAAC;AAEvC,MAAM,MAAM,OAAO,GAAG,CAAC,MAAM,EAAE,GAAG,KAAK,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAAA;AAEzD,qBAAa,aAAa;IACxB,OAAO,CAAC,OAAO,CAA6B;IAE5C,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO;IAInC,MAAM,CACV,GAAG,EAAE,OAAO,GACX,OAAO,CAAC,eAAe,GAAG,eAAe,EAAE,GAAG,IAAI,CAAC;IAuDtD,OAAO,CAAC,KAAK;CAYd"}
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export const INVALID_REQUEST = -32600;
|
|
2
|
+
export const METHOD_NOT_FOUND = -32601;
|
|
3
|
+
export const INVALID_PARAMS = -32602; // unused
|
|
4
|
+
export const INTERNAL_ERROR = -32603;
|
|
5
|
+
export const PARSE_ERROR = -32700; // unused
|
|
6
|
+
export const REQUEST_ABORTED = -32800; // unused
|
|
7
|
+
export class JsonRpcServer {
|
|
8
|
+
methods = new Map();
|
|
9
|
+
register(method, handler) {
|
|
10
|
+
this.methods.set(method, handler);
|
|
11
|
+
}
|
|
12
|
+
async handle(raw) {
|
|
13
|
+
// handle batch
|
|
14
|
+
if (Array.isArray(raw)) {
|
|
15
|
+
if (raw.length === 0) {
|
|
16
|
+
return this.error(null, INVALID_REQUEST, "Invalid request");
|
|
17
|
+
}
|
|
18
|
+
const responses = await Promise.all(raw.map(item => this.handle(item)));
|
|
19
|
+
const filtered = responses.filter((r) => r !== null);
|
|
20
|
+
return filtered.length > 0 ? filtered : null;
|
|
21
|
+
}
|
|
22
|
+
// handle single response
|
|
23
|
+
let req = raw;
|
|
24
|
+
if (typeof req !== "object" ||
|
|
25
|
+
req === null ||
|
|
26
|
+
req.jsonrpc !== "2.0" ||
|
|
27
|
+
typeof req.method !== "string") {
|
|
28
|
+
return this.error(null, INVALID_REQUEST, "Invalid request");
|
|
29
|
+
}
|
|
30
|
+
const id = typeof req?.id === "string" ||
|
|
31
|
+
typeof req?.id === "number" ||
|
|
32
|
+
req?.id === null ? req.id : null;
|
|
33
|
+
const handler = this.methods.get(req.method);
|
|
34
|
+
if (!handler) {
|
|
35
|
+
return id === null ? null : this.error(req.id, METHOD_NOT_FOUND, "Method not found");
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
const result = await handler(req.params);
|
|
39
|
+
if (req.id === undefined)
|
|
40
|
+
return null; // notification
|
|
41
|
+
return {
|
|
42
|
+
jsonrpc: "2.0",
|
|
43
|
+
result,
|
|
44
|
+
id: req.id
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
catch (err) {
|
|
48
|
+
return this.error(req.id ?? null, INTERNAL_ERROR, "Internal error", err instanceof Error ? err.message : err);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
error(id, code, message, data) {
|
|
52
|
+
return {
|
|
53
|
+
jsonrpc: "2.0",
|
|
54
|
+
error: { code, message, data },
|
|
55
|
+
id: id ?? null
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"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"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export type JsonRpcId = string | number | null;
|
|
2
|
+
export interface JsonRpcRequest {
|
|
3
|
+
jsonrpc: "2.0";
|
|
4
|
+
method: string;
|
|
5
|
+
params?: unknown;
|
|
6
|
+
id?: JsonRpcId;
|
|
7
|
+
}
|
|
8
|
+
export interface JsonRpcSuccess {
|
|
9
|
+
jsonrpc: "2.0";
|
|
10
|
+
result: unknown;
|
|
11
|
+
id: JsonRpcId;
|
|
12
|
+
}
|
|
13
|
+
export interface JsonRpcErrorObject {
|
|
14
|
+
code: number;
|
|
15
|
+
message: string;
|
|
16
|
+
data?: unknown;
|
|
17
|
+
}
|
|
18
|
+
export interface JsonRpcError {
|
|
19
|
+
jsonrpc: "2.0";
|
|
20
|
+
error: JsonRpcErrorObject;
|
|
21
|
+
id: JsonRpcId;
|
|
22
|
+
}
|
|
23
|
+
export type JsonRpcResponse = JsonRpcSuccess | JsonRpcError;
|
|
24
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;AAE9C,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,KAAK,CAAA;IACd,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,EAAE,CAAC,EAAE,SAAS,CAAA;CACf;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,KAAK,CAAA;IACd,MAAM,EAAE,OAAO,CAAA;IACf,EAAE,EAAE,SAAS,CAAA;CACd;AAED,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAE,OAAO,CAAA;CACf;AAED,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,KAAK,CAAA;IACd,KAAK,EAAE,kBAAkB,CAAA;IACzB,EAAE,EAAE,SAAS,CAAA;CACd;AAED,MAAM,MAAM,eAAe,GAAG,cAAc,GAAG,YAAY,CAAA"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@msaki/jsonrpc",
|
|
3
3
|
"author": "Meek Msaki",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.4",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"module": "dist/index.mjs",
|
|
8
8
|
"types": "dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"src"
|
|
12
|
+
],
|
|
9
13
|
"repository": {
|
|
10
14
|
"type": "git",
|
|
11
15
|
"url": "git+https://github.com/mmsaki/libs.git",
|
|
@@ -16,13 +20,12 @@
|
|
|
16
20
|
"directory": "dist"
|
|
17
21
|
},
|
|
18
22
|
"scripts": {
|
|
19
|
-
"build": "
|
|
20
|
-
"publish": "bun run build && npm publish --access public",
|
|
23
|
+
"build": "tsc -p tsconfig.json",
|
|
24
|
+
"publish": "bun run build && bun run npm login && bun run npm publish --access public",
|
|
21
25
|
"lint": "tsc"
|
|
22
26
|
},
|
|
23
27
|
"devDependencies": {
|
|
24
|
-
"@types/bun": "latest"
|
|
25
|
-
"tsup": "^8.5.1"
|
|
28
|
+
"@types/bun": "latest"
|
|
26
29
|
},
|
|
27
30
|
"peerDependencies": {
|
|
28
31
|
"typescript": "^5"
|
package/src/index.ts
ADDED
package/dist/index.d.mts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
type JsonRpcId = string | number | null;
|
|
2
|
-
interface JsonRpcRequest {
|
|
3
|
-
jsonrpc: "2.0";
|
|
4
|
-
method: string;
|
|
5
|
-
params?: unknown;
|
|
6
|
-
id?: JsonRpcId;
|
|
7
|
-
}
|
|
8
|
-
interface JsonRpcSuccess {
|
|
9
|
-
jsonrpc: "2.0";
|
|
10
|
-
result: unknown;
|
|
11
|
-
id: JsonRpcId;
|
|
12
|
-
}
|
|
13
|
-
interface JsonRpcErrorObject {
|
|
14
|
-
code: number;
|
|
15
|
-
message: string;
|
|
16
|
-
data?: unknown;
|
|
17
|
-
}
|
|
18
|
-
interface JsonRpcError {
|
|
19
|
-
jsonrpc: "2.0";
|
|
20
|
-
error: JsonRpcErrorObject;
|
|
21
|
-
id: JsonRpcId;
|
|
22
|
-
}
|
|
23
|
-
type JsonRpcResponse = JsonRpcSuccess | JsonRpcError;
|
|
24
|
-
|
|
25
|
-
declare const INVALID_REQUEST = -32600;
|
|
26
|
-
declare const METHOD_NOT_FOUND = -32601;
|
|
27
|
-
declare const INVALID_PARAMS = -32602;
|
|
28
|
-
declare const INTERNAL_ERROR = -32603;
|
|
29
|
-
declare const PARSE_ERROR = -32700;
|
|
30
|
-
declare const REQUEST_ABORTED = -32800;
|
|
31
|
-
type Handler = (params: any) => any | Promise<any>;
|
|
32
|
-
declare class JsonRpcServer {
|
|
33
|
-
private methods;
|
|
34
|
-
register(method: string, handler: Handler): void;
|
|
35
|
-
handle(raw: unknown): Promise<JsonRpcResponse | JsonRpcResponse[] | null>;
|
|
36
|
-
private error;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
declare class JsonRpcClient {
|
|
40
|
-
private send;
|
|
41
|
-
private id;
|
|
42
|
-
constructor(send: (req: JsonRpcRequest) => Promise<JsonRpcResponse>);
|
|
43
|
-
call<T = unknown>(method: string, params?: unknown): Promise<T>;
|
|
44
|
-
notify(method: string, params?: unknown): Promise<JsonRpcResponse>;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export { type Handler, INTERNAL_ERROR, INVALID_PARAMS, INVALID_REQUEST, JsonRpcClient, type JsonRpcError, type JsonRpcErrorObject, type JsonRpcId, type JsonRpcRequest, type JsonRpcResponse, JsonRpcServer, type JsonRpcSuccess, METHOD_NOT_FOUND, PARSE_ERROR, REQUEST_ABORTED };
|
package/dist/index.mjs
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
// src/server.ts
|
|
2
|
-
var INVALID_REQUEST = -32600;
|
|
3
|
-
var METHOD_NOT_FOUND = -32601;
|
|
4
|
-
var INVALID_PARAMS = -32602;
|
|
5
|
-
var INTERNAL_ERROR = -32603;
|
|
6
|
-
var PARSE_ERROR = -32700;
|
|
7
|
-
var REQUEST_ABORTED = -32800;
|
|
8
|
-
var JsonRpcServer = class {
|
|
9
|
-
methods = /* @__PURE__ */ new Map();
|
|
10
|
-
register(method, handler) {
|
|
11
|
-
this.methods.set(method, handler);
|
|
12
|
-
}
|
|
13
|
-
async handle(raw) {
|
|
14
|
-
if (Array.isArray(raw)) {
|
|
15
|
-
if (raw.length === 0) {
|
|
16
|
-
return this.error(null, INVALID_REQUEST, "Invalid request");
|
|
17
|
-
}
|
|
18
|
-
const responses = await Promise.all(
|
|
19
|
-
raw.map((item) => this.handle(item))
|
|
20
|
-
);
|
|
21
|
-
const filtered = responses.filter(
|
|
22
|
-
(r) => r !== null
|
|
23
|
-
);
|
|
24
|
-
return filtered.length > 0 ? filtered : null;
|
|
25
|
-
}
|
|
26
|
-
let req = raw;
|
|
27
|
-
if (typeof req !== "object" || req === null || req.jsonrpc !== "2.0" || typeof req.method !== "string") {
|
|
28
|
-
return this.error(null, INVALID_REQUEST, "Invalid request");
|
|
29
|
-
}
|
|
30
|
-
const id = typeof req?.id === "string" || typeof req?.id === "number" || req?.id === null ? req.id : null;
|
|
31
|
-
const handler = this.methods.get(req.method);
|
|
32
|
-
if (!handler) {
|
|
33
|
-
return id === null ? null : this.error(req.id, METHOD_NOT_FOUND, "Method not found");
|
|
34
|
-
}
|
|
35
|
-
try {
|
|
36
|
-
const result = await handler(req.params);
|
|
37
|
-
if (req.id === void 0) return null;
|
|
38
|
-
return {
|
|
39
|
-
jsonrpc: "2.0",
|
|
40
|
-
result,
|
|
41
|
-
id: req.id
|
|
42
|
-
};
|
|
43
|
-
} catch (err) {
|
|
44
|
-
return this.error(
|
|
45
|
-
req.id ?? null,
|
|
46
|
-
INTERNAL_ERROR,
|
|
47
|
-
"Internal error",
|
|
48
|
-
err instanceof Error ? err.message : err
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
error(id, code, message, data) {
|
|
53
|
-
return {
|
|
54
|
-
jsonrpc: "2.0",
|
|
55
|
-
error: { code, message, data },
|
|
56
|
-
id: id ?? null
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
// src/client.ts
|
|
62
|
-
var JsonRpcClient = class {
|
|
63
|
-
constructor(send) {
|
|
64
|
-
this.send = send;
|
|
65
|
-
}
|
|
66
|
-
id = 0;
|
|
67
|
-
async call(method, params) {
|
|
68
|
-
const request = {
|
|
69
|
-
jsonrpc: "2.0",
|
|
70
|
-
method,
|
|
71
|
-
params,
|
|
72
|
-
id: ++this.id
|
|
73
|
-
};
|
|
74
|
-
const response = await this.send(request);
|
|
75
|
-
if ("error" in response) {
|
|
76
|
-
const e = response.error;
|
|
77
|
-
throw new Error(`RPC error ${e.code} ${e.message}`);
|
|
78
|
-
}
|
|
79
|
-
return response.result;
|
|
80
|
-
}
|
|
81
|
-
notify(method, params) {
|
|
82
|
-
const request = {
|
|
83
|
-
jsonrpc: "2.0",
|
|
84
|
-
method,
|
|
85
|
-
params
|
|
86
|
-
};
|
|
87
|
-
return this.send(request);
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
export {
|
|
91
|
-
INTERNAL_ERROR,
|
|
92
|
-
INVALID_PARAMS,
|
|
93
|
-
INVALID_REQUEST,
|
|
94
|
-
JsonRpcClient,
|
|
95
|
-
JsonRpcServer,
|
|
96
|
-
METHOD_NOT_FOUND,
|
|
97
|
-
PARSE_ERROR,
|
|
98
|
-
REQUEST_ABORTED
|
|
99
|
-
};
|
package/index.ts
DELETED
package/tsconfig.json
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
// Environment setup & latest features
|
|
4
|
-
"lib": [
|
|
5
|
-
"ESNext"
|
|
6
|
-
],
|
|
7
|
-
"target": "ESNext",
|
|
8
|
-
"module": "Preserve",
|
|
9
|
-
"moduleDetection": "force",
|
|
10
|
-
"jsx": "react-jsx",
|
|
11
|
-
"allowJs": true,
|
|
12
|
-
// Bundler mode
|
|
13
|
-
"moduleResolution": "bundler",
|
|
14
|
-
"allowImportingTsExtensions": true,
|
|
15
|
-
"verbatimModuleSyntax": true,
|
|
16
|
-
"noEmit": true,
|
|
17
|
-
// Best practices
|
|
18
|
-
"strict": true,
|
|
19
|
-
"skipLibCheck": true,
|
|
20
|
-
"noFallthroughCasesInSwitch": true,
|
|
21
|
-
"noUncheckedIndexedAccess": true,
|
|
22
|
-
"noImplicitOverride": true,
|
|
23
|
-
// Some stricter flags (disabled by default)
|
|
24
|
-
"noUnusedLocals": false,
|
|
25
|
-
"noUnusedParameters": false,
|
|
26
|
-
"noPropertyAccessFromIndexSignature": false
|
|
27
|
-
}
|
|
28
|
-
}
|