@yxcinebendjebbar/asdc 0.0.5 → 0.0.6
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/core/errors.d.ts +4 -4
- package/dist/http/HttpClient.d.ts +2 -2
- package/dist/http/HttpClient.js +2 -2
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/rpc/HttpRpcAdapter.d.ts +8 -0
- package/dist/rpc/HttpRpcAdapter.js +16 -0
- package/dist/rpc/JsonRpcClient.d.ts +4 -13
- package/dist/rpc/JsonRpcClient.js +4 -58
- package/dist/rpc/RpcError.d.ts +5 -0
- package/dist/rpc/RpcError.js +10 -0
- package/dist/rpc/RpcTransport.d.ts +3 -0
- package/dist/rpc/RpcTransport.js +1 -0
- package/dist/rpc/WebSocketRpcAdapter.d.ts +10 -0
- package/dist/rpc/WebSocketRpcAdapter.js +41 -0
- package/dist/ws/WebSocketClient.d.ts +9 -5
- package/dist/ws/WebSocketClient.js +53 -8
- package/package.json +1 -1
package/dist/core/errors.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
export declare class HttpToolkitError extends Error {
|
|
2
|
-
config?: unknown
|
|
3
|
-
constructor(message: string, config?: unknown
|
|
2
|
+
config?: unknown;
|
|
3
|
+
constructor(message: string, config?: unknown);
|
|
4
4
|
}
|
|
5
5
|
export declare class NetworkError extends HttpToolkitError {
|
|
6
|
-
cause?: unknown
|
|
7
|
-
constructor(message: string, cause?: unknown
|
|
6
|
+
cause?: unknown;
|
|
7
|
+
constructor(message: string, cause?: unknown, config?: unknown);
|
|
8
8
|
}
|
|
9
9
|
export declare class TimeoutError extends HttpToolkitError {
|
|
10
10
|
constructor(message?: string, config?: unknown);
|
|
@@ -3,12 +3,12 @@ import { HttpResponse } from '../core/Response.ts';
|
|
|
3
3
|
import { InterceptorManager } from '../core/InterceptorManager.ts';
|
|
4
4
|
import type { Transport } from '../transport/Transport.ts';
|
|
5
5
|
export declare class HttpClient {
|
|
6
|
-
private readonly config;
|
|
7
|
-
private readonly transport;
|
|
8
6
|
readonly interceptors: {
|
|
9
7
|
request: InterceptorManager<RequestConfig>;
|
|
10
8
|
response: InterceptorManager<HttpResponse<unknown>>;
|
|
11
9
|
};
|
|
10
|
+
private readonly config;
|
|
11
|
+
private readonly transport;
|
|
12
12
|
constructor(config?: ClientConfig, transport?: Transport);
|
|
13
13
|
request<T = unknown>(config: RequestConfig): Promise<HttpResponse<T>>;
|
|
14
14
|
private method;
|
package/dist/http/HttpClient.js
CHANGED
|
@@ -2,12 +2,12 @@ import { InterceptorManager } from "../core/InterceptorManager.js";
|
|
|
2
2
|
import { ResponseError } from "../core/errors.js";
|
|
3
3
|
import { FetchTransport } from "../transport/FetchTransport.js";
|
|
4
4
|
export class HttpClient {
|
|
5
|
-
config;
|
|
6
|
-
transport;
|
|
7
5
|
interceptors = {
|
|
8
6
|
request: new InterceptorManager(),
|
|
9
7
|
response: new InterceptorManager(),
|
|
10
8
|
};
|
|
9
|
+
config;
|
|
10
|
+
transport;
|
|
11
11
|
constructor(config = {}, transport = new FetchTransport()) {
|
|
12
12
|
this.config = config;
|
|
13
13
|
this.transport = transport;
|
package/dist/index.d.ts
CHANGED
|
@@ -6,4 +6,8 @@ export * from './transport/Transport.ts';
|
|
|
6
6
|
export * from './transport/FetchTransport.ts';
|
|
7
7
|
export * from './http/HttpClient.ts';
|
|
8
8
|
export * from './ws/WebSocketClient.ts';
|
|
9
|
+
export * from './rpc/RpcError.ts';
|
|
10
|
+
export * from './rpc/RpcTransport.ts';
|
|
11
|
+
export * from './rpc/HttpRpcAdapter.ts';
|
|
12
|
+
export * from './rpc/WebSocketRpcAdapter.ts';
|
|
9
13
|
export * from './rpc/JsonRpcClient.ts';
|
package/dist/index.js
CHANGED
|
@@ -6,4 +6,8 @@ export * from "./transport/Transport.js";
|
|
|
6
6
|
export * from "./transport/FetchTransport.js";
|
|
7
7
|
export * from "./http/HttpClient.js";
|
|
8
8
|
export * from "./ws/WebSocketClient.js";
|
|
9
|
+
export * from "./rpc/RpcError.js";
|
|
10
|
+
export * from "./rpc/RpcTransport.js";
|
|
11
|
+
export * from "./rpc/HttpRpcAdapter.js";
|
|
12
|
+
export * from "./rpc/WebSocketRpcAdapter.js";
|
|
9
13
|
export * from "./rpc/JsonRpcClient.js";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { HttpClient } from '../http/HttpClient.ts';
|
|
2
|
+
import { RpcTransport } from './RpcTransport.ts';
|
|
3
|
+
export declare class HttpRpcAdapter implements RpcTransport {
|
|
4
|
+
private readonly client;
|
|
5
|
+
private readonly endpoint;
|
|
6
|
+
constructor(client: HttpClient, endpoint?: string);
|
|
7
|
+
call<T = unknown>(payload: unknown): Promise<T>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { RpcError } from "./RpcError.js";
|
|
2
|
+
export class HttpRpcAdapter {
|
|
3
|
+
client;
|
|
4
|
+
endpoint;
|
|
5
|
+
constructor(client, endpoint = '/') {
|
|
6
|
+
this.client = client;
|
|
7
|
+
this.endpoint = endpoint;
|
|
8
|
+
}
|
|
9
|
+
async call(payload) {
|
|
10
|
+
const res = await this.client.post(this.endpoint, payload);
|
|
11
|
+
if (res.data.error) {
|
|
12
|
+
throw new RpcError(res.data.error.message, res.data.error.code, res.data.error.data);
|
|
13
|
+
}
|
|
14
|
+
return res.data.result;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -1,17 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export declare class RpcError extends Error {
|
|
4
|
-
code: number;
|
|
5
|
-
data?: unknown | undefined;
|
|
6
|
-
constructor(message: string, code: number, data?: unknown | undefined);
|
|
7
|
-
}
|
|
1
|
+
import { RpcTransport } from './RpcTransport.ts';
|
|
2
|
+
export { RpcError } from './RpcError.ts';
|
|
8
3
|
export declare class JsonRpcClient {
|
|
9
|
-
private readonly transport;
|
|
10
|
-
private readonly endpoint;
|
|
11
4
|
private nextId;
|
|
12
|
-
private
|
|
13
|
-
constructor(transport:
|
|
5
|
+
private readonly transport;
|
|
6
|
+
constructor(transport: RpcTransport);
|
|
14
7
|
call<T = unknown>(method: string, params?: unknown): Promise<T>;
|
|
15
|
-
private handleMessage;
|
|
16
|
-
private rejectAllPending;
|
|
17
8
|
}
|
|
@@ -1,67 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
export class RpcError extends Error {
|
|
3
|
-
code;
|
|
4
|
-
data;
|
|
5
|
-
constructor(message, code, data) {
|
|
6
|
-
super(message);
|
|
7
|
-
this.code = code;
|
|
8
|
-
this.data = data;
|
|
9
|
-
this.name = 'RpcError';
|
|
10
|
-
}
|
|
11
|
-
}
|
|
1
|
+
export { RpcError } from "./RpcError.js";
|
|
12
2
|
export class JsonRpcClient {
|
|
13
|
-
transport;
|
|
14
|
-
endpoint;
|
|
15
3
|
nextId = 1;
|
|
16
|
-
|
|
17
|
-
constructor(transport
|
|
4
|
+
transport;
|
|
5
|
+
constructor(transport) {
|
|
18
6
|
this.transport = transport;
|
|
19
|
-
this.endpoint = endpoint;
|
|
20
|
-
if (transport instanceof WebSocketClient) {
|
|
21
|
-
transport.on('message', (raw) => this.handleMessage(raw));
|
|
22
|
-
transport.on('close', () => this.rejectAllPending(new RpcError('WebSocket closed', -32000)));
|
|
23
|
-
transport.on('error', () => this.rejectAllPending(new RpcError('WebSocket error', -32000)));
|
|
24
|
-
}
|
|
25
7
|
}
|
|
26
8
|
async call(method, params) {
|
|
27
9
|
const id = this.nextId++;
|
|
28
10
|
const payload = { jsonrpc: '2.0', id, method, params };
|
|
29
|
-
|
|
30
|
-
if (transport instanceof WebSocketClient) {
|
|
31
|
-
return new Promise((resolve, reject) => {
|
|
32
|
-
this.pending.set(id, { resolve: resolve, reject });
|
|
33
|
-
transport.send(JSON.stringify(payload));
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
const res = await transport.post(this.endpoint, payload);
|
|
37
|
-
if (res.data.error) {
|
|
38
|
-
throw new RpcError(res.data.error.message, res.data.error.code, res.data.error.data);
|
|
39
|
-
}
|
|
40
|
-
return res.data.result;
|
|
41
|
-
}
|
|
42
|
-
handleMessage(raw) {
|
|
43
|
-
let msg;
|
|
44
|
-
try {
|
|
45
|
-
msg = JSON.parse(raw);
|
|
46
|
-
}
|
|
47
|
-
catch {
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
const pending = this.pending.get(msg.id);
|
|
51
|
-
if (!pending)
|
|
52
|
-
return;
|
|
53
|
-
this.pending.delete(msg.id);
|
|
54
|
-
if (msg.error) {
|
|
55
|
-
pending.reject(new RpcError(msg.error.message, msg.error.code, msg.error.data));
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
pending.resolve(msg.result);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
rejectAllPending(error) {
|
|
62
|
-
for (const pending of this.pending.values()) {
|
|
63
|
-
pending.reject(error);
|
|
64
|
-
}
|
|
65
|
-
this.pending.clear();
|
|
11
|
+
return this.transport.call(payload);
|
|
66
12
|
}
|
|
67
13
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { WebSocketClient } from '../ws/WebSocketClient.ts';
|
|
2
|
+
import { RpcTransport } from './RpcTransport.ts';
|
|
3
|
+
export declare class WebSocketRpcAdapter implements RpcTransport {
|
|
4
|
+
private pending;
|
|
5
|
+
private readonly client;
|
|
6
|
+
constructor(client: WebSocketClient);
|
|
7
|
+
call<T = unknown>(payload: unknown): Promise<T>;
|
|
8
|
+
private handleMessage;
|
|
9
|
+
private rejectAllPending;
|
|
10
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { RpcError } from "./RpcError.js";
|
|
2
|
+
export class WebSocketRpcAdapter {
|
|
3
|
+
pending = new Map();
|
|
4
|
+
client;
|
|
5
|
+
constructor(client) {
|
|
6
|
+
this.client = client;
|
|
7
|
+
this.client.on('message', (parsed) => this.handleMessage(parsed));
|
|
8
|
+
this.client.on('close', () => this.rejectAllPending(new RpcError('WebSocket closed', -32000)));
|
|
9
|
+
this.client.on('error', () => this.rejectAllPending(new RpcError('WebSocket error', -32000)));
|
|
10
|
+
}
|
|
11
|
+
async call(payload) {
|
|
12
|
+
const id = payload.id;
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
if (id !== undefined) {
|
|
15
|
+
this.pending.set(id, { resolve: resolve, reject });
|
|
16
|
+
}
|
|
17
|
+
this.client.send(payload);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
handleMessage(msg) {
|
|
21
|
+
if (!msg || typeof msg !== 'object' || !('id' in msg))
|
|
22
|
+
return;
|
|
23
|
+
const response = msg;
|
|
24
|
+
const pending = this.pending.get(response.id);
|
|
25
|
+
if (!pending)
|
|
26
|
+
return;
|
|
27
|
+
this.pending.delete(response.id);
|
|
28
|
+
if (response.error) {
|
|
29
|
+
pending.reject(new RpcError(response.error.message, response.error.code, response.error.data));
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
pending.resolve(response.result);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
rejectAllPending(error) {
|
|
36
|
+
for (const pending of this.pending.values()) {
|
|
37
|
+
pending.reject(error);
|
|
38
|
+
}
|
|
39
|
+
this.pending.clear();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -4,17 +4,21 @@ export interface ReconnectPolicy {
|
|
|
4
4
|
maxAttempts?: number;
|
|
5
5
|
delayMs?: (attempt: number) => number;
|
|
6
6
|
}
|
|
7
|
+
export type WebSocketFactory = (url: string) => WebSocket;
|
|
7
8
|
export declare class WebSocketClient {
|
|
8
|
-
private readonly url;
|
|
9
|
-
private readonly reconnect;
|
|
10
9
|
private ws?;
|
|
11
10
|
private listeners;
|
|
12
11
|
private attempt;
|
|
13
12
|
private shouldReconnect;
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
private messageQueue;
|
|
14
|
+
private readonly url;
|
|
15
|
+
private readonly reconnect;
|
|
16
|
+
private readonly factory;
|
|
17
|
+
constructor(url: string, reconnect?: ReconnectPolicy, factory?: WebSocketFactory);
|
|
18
|
+
private connect;
|
|
16
19
|
private tryReconnect;
|
|
17
|
-
|
|
20
|
+
private flushQueue;
|
|
21
|
+
send(data: unknown, isRaw?: boolean): void;
|
|
18
22
|
close(code?: number, reason?: string): void;
|
|
19
23
|
on(event: WSEvent, listener: Listener): void;
|
|
20
24
|
off(event: WSEvent, listener: Listener): void;
|
|
@@ -1,19 +1,41 @@
|
|
|
1
1
|
export class WebSocketClient {
|
|
2
|
-
url;
|
|
3
|
-
reconnect;
|
|
4
2
|
ws;
|
|
5
3
|
listeners = new Map();
|
|
6
4
|
attempt = 0;
|
|
7
5
|
shouldReconnect = true;
|
|
8
|
-
|
|
6
|
+
messageQueue = [];
|
|
7
|
+
url;
|
|
8
|
+
reconnect;
|
|
9
|
+
factory;
|
|
10
|
+
constructor(url, reconnect = { maxAttempts: 5, delayMs: (a) => Math.min(1000 * 2 ** a, 30000) }, factory = (u) => new WebSocket(u)) {
|
|
9
11
|
this.url = url;
|
|
10
12
|
this.reconnect = reconnect;
|
|
13
|
+
this.factory = factory;
|
|
11
14
|
}
|
|
12
15
|
connect() {
|
|
16
|
+
if (this.ws && (this.ws.readyState === WebSocket.CONNECTING || this.ws.readyState === WebSocket.OPEN)) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
13
19
|
this.shouldReconnect = true;
|
|
14
|
-
this.ws =
|
|
15
|
-
this.ws.addEventListener('open', (e) =>
|
|
16
|
-
|
|
20
|
+
this.ws = this.factory(this.url);
|
|
21
|
+
this.ws.addEventListener('open', (e) => {
|
|
22
|
+
this.flushQueue();
|
|
23
|
+
this.emit('open', e);
|
|
24
|
+
});
|
|
25
|
+
this.ws.addEventListener('message', (e) => {
|
|
26
|
+
const data = e.data;
|
|
27
|
+
let parsed = data;
|
|
28
|
+
try {
|
|
29
|
+
if (typeof data === 'string') {
|
|
30
|
+
parsed = JSON.parse(data);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
// Keep as raw data
|
|
35
|
+
}
|
|
36
|
+
// Pass both parsed (or raw if not JSON) and original raw data
|
|
37
|
+
this.emit('message', parsed, data);
|
|
38
|
+
});
|
|
17
39
|
this.ws.addEventListener('close', (e) => {
|
|
18
40
|
this.emit('close', e);
|
|
19
41
|
if (this.shouldReconnect)
|
|
@@ -29,8 +51,31 @@ export class WebSocketClient {
|
|
|
29
51
|
this.attempt++;
|
|
30
52
|
setTimeout(() => this.connect(), delay);
|
|
31
53
|
}
|
|
32
|
-
|
|
33
|
-
this.ws
|
|
54
|
+
flushQueue() {
|
|
55
|
+
if (!this.ws || this.ws.readyState !== WebSocket.OPEN)
|
|
56
|
+
return;
|
|
57
|
+
while (this.messageQueue.length > 0) {
|
|
58
|
+
const msg = this.messageQueue.shift();
|
|
59
|
+
if (msg !== undefined) {
|
|
60
|
+
this.ws.send(msg);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
send(data, isRaw = false) {
|
|
65
|
+
let payload;
|
|
66
|
+
if (isRaw) {
|
|
67
|
+
payload = data;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
payload = typeof data === 'string' ? data : JSON.stringify(data);
|
|
71
|
+
}
|
|
72
|
+
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
73
|
+
this.messageQueue.push(payload);
|
|
74
|
+
this.connect();
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
this.ws.send(payload);
|
|
78
|
+
}
|
|
34
79
|
}
|
|
35
80
|
close(code, reason) {
|
|
36
81
|
this.shouldReconnect = false;
|
package/package.json
CHANGED