@dabble/patches 0.4.5 → 0.4.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/algorithms/client/applyCommittedChanges.d.ts +8 -2
- package/dist/algorithms/client/applyCommittedChanges.js +30 -38
- package/dist/algorithms/client/batching.d.ts +8 -2
- package/dist/algorithms/client/batching.js +38 -37
- package/dist/algorithms/client/breakChange.d.ts +8 -2
- package/dist/algorithms/client/breakChange.js +191 -240
- package/dist/algorithms/client/createStateFromSnapshot.d.ts +8 -2
- package/dist/algorithms/client/createStateFromSnapshot.js +7 -8
- package/dist/algorithms/client/getJSONByteSize.d.ts +3 -1
- package/dist/algorithms/client/getJSONByteSize.js +12 -11
- package/dist/algorithms/client/makeChange.d.ts +8 -2
- package/dist/algorithms/client/makeChange.js +28 -36
- package/dist/algorithms/server/commitChanges.d.ts +9 -3
- package/dist/algorithms/server/commitChanges.js +69 -78
- package/dist/algorithms/server/createVersion.d.ts +9 -3
- package/dist/algorithms/server/createVersion.js +21 -27
- package/dist/algorithms/server/getSnapshotAtRevision.d.ts +9 -3
- package/dist/algorithms/server/getSnapshotAtRevision.js +27 -28
- package/dist/algorithms/server/getStateAtRevision.d.ts +9 -3
- package/dist/algorithms/server/getStateAtRevision.js +13 -17
- package/dist/algorithms/server/handleOfflineSessionsAndBatches.d.ts +9 -3
- package/dist/algorithms/server/handleOfflineSessionsAndBatches.js +60 -77
- package/dist/algorithms/server/transformIncomingChanges.d.ts +8 -2
- package/dist/algorithms/server/transformIncomingChanges.js +27 -39
- package/dist/algorithms/shared/applyChanges.d.ts +8 -2
- package/dist/algorithms/shared/applyChanges.js +11 -16
- package/dist/algorithms/shared/rebaseChanges.d.ts +8 -2
- package/dist/algorithms/shared/rebaseChanges.js +30 -49
- package/dist/chunk-IZ2YBCUP.js +56 -0
- package/dist/client/InMemoryStore.d.ts +9 -3
- package/dist/client/InMemoryStore.js +92 -101
- package/dist/client/IndexedDBStore.d.ts +9 -3
- package/dist/client/IndexedDBStore.js +378 -491
- package/dist/client/Patches.d.ts +18 -13
- package/dist/client/Patches.js +152 -207
- package/dist/client/PatchesDoc.d.ts +14 -8
- package/dist/client/PatchesDoc.js +147 -154
- package/dist/client/PatchesHistoryClient.d.ts +12 -5
- package/dist/client/PatchesHistoryClient.js +110 -117
- package/dist/client/PatchesStore.d.ts +9 -3
- package/dist/client/PatchesStore.js +0 -1
- package/dist/client/index.d.ts +12 -6
- package/dist/client/index.js +5 -5
- package/dist/data/change.d.ts +9 -3
- package/dist/data/change.js +23 -15
- package/dist/data/version.d.ts +9 -3
- package/dist/data/version.js +11 -15
- package/dist/event-signal.d.ts +7 -6
- package/dist/event-signal.js +24 -39
- package/dist/index-CvQws3AB.d.ts +36 -0
- package/dist/index.d.ts +27 -5
- package/dist/index.js +10 -4
- package/dist/json-patch/JSONPatch.d.ts +9 -5
- package/dist/json-patch/JSONPatch.js +175 -183
- package/dist/json-patch/applyPatch.d.ts +5 -2
- package/dist/json-patch/applyPatch.js +27 -35
- package/dist/json-patch/composePatch.d.ts +5 -2
- package/dist/json-patch/composePatch.js +34 -34
- package/dist/json-patch/createJSONPatch.d.ts +7 -2
- package/dist/json-patch/createJSONPatch.js +11 -38
- package/dist/json-patch/index.d.ts +14 -6
- package/dist/json-patch/index.js +20 -9
- package/dist/json-patch/invertPatch.d.ts +5 -2
- package/dist/json-patch/invertPatch.js +31 -30
- package/dist/json-patch/ops/add.d.ts +5 -2
- package/dist/json-patch/ops/add.js +53 -51
- package/dist/json-patch/ops/bitmask.d.ts +8 -5
- package/dist/json-patch/ops/bitmask.js +41 -44
- package/dist/json-patch/ops/copy.d.ts +5 -2
- package/dist/json-patch/ops/copy.js +32 -33
- package/dist/json-patch/ops/increment.d.ts +5 -2
- package/dist/json-patch/ops/increment.js +21 -20
- package/dist/json-patch/ops/index.d.ts +10 -21
- package/dist/json-patch/ops/index.js +34 -24
- package/dist/json-patch/ops/move.d.ts +5 -2
- package/dist/json-patch/ops/move.js +132 -198
- package/dist/json-patch/ops/remove.d.ts +5 -2
- package/dist/json-patch/ops/remove.js +33 -30
- package/dist/json-patch/ops/replace.d.ts +5 -2
- package/dist/json-patch/ops/replace.js +45 -43
- package/dist/json-patch/ops/test.d.ts +5 -2
- package/dist/json-patch/ops/test.js +25 -21
- package/dist/json-patch/ops/text.d.ts +5 -2
- package/dist/json-patch/ops/text.js +54 -54
- package/dist/json-patch/pathProxy.d.ts +9 -3
- package/dist/json-patch/pathProxy.js +27 -48
- package/dist/json-patch/state.d.ts +5 -2
- package/dist/json-patch/state.js +11 -7
- package/dist/json-patch/transformPatch.d.ts +6 -2
- package/dist/json-patch/transformPatch.js +21 -24
- package/dist/json-patch/types.d.ts +9 -7
- package/dist/json-patch/types.js +0 -1
- package/dist/json-patch/utils/deepEqual.d.ts +3 -1
- package/dist/json-patch/utils/deepEqual.js +32 -28
- package/dist/json-patch/utils/exit.d.ts +5 -2
- package/dist/json-patch/utils/exit.js +7 -3
- package/dist/json-patch/utils/get.d.ts +5 -2
- package/dist/json-patch/utils/get.js +8 -4
- package/dist/json-patch/utils/getOpData.d.ts +5 -2
- package/dist/json-patch/utils/getOpData.js +12 -9
- package/dist/json-patch/utils/getType.d.ts +6 -3
- package/dist/json-patch/utils/getType.js +9 -4
- package/dist/json-patch/utils/index.d.ts +15 -14
- package/dist/json-patch/utils/index.js +14 -14
- package/dist/json-patch/utils/log.d.ts +4 -2
- package/dist/json-patch/utils/log.js +8 -3
- package/dist/json-patch/utils/ops.d.ts +8 -5
- package/dist/json-patch/utils/ops.js +83 -100
- package/dist/json-patch/utils/paths.d.ts +12 -9
- package/dist/json-patch/utils/paths.js +54 -51
- package/dist/json-patch/utils/pluck.d.ts +8 -5
- package/dist/json-patch/utils/pluck.js +32 -26
- package/dist/json-patch/utils/shallowCopy.d.ts +3 -1
- package/dist/json-patch/utils/shallowCopy.js +22 -18
- package/dist/json-patch/utils/softWrites.d.ts +6 -3
- package/dist/json-patch/utils/softWrites.js +17 -16
- package/dist/json-patch/utils/toArrayIndex.d.ts +3 -1
- package/dist/json-patch/utils/toArrayIndex.js +14 -10
- package/dist/json-patch/utils/toKeys.d.ts +3 -1
- package/dist/json-patch/utils/toKeys.js +15 -11
- package/dist/json-patch/utils/updateArrayIndexes.d.ts +5 -2
- package/dist/json-patch/utils/updateArrayIndexes.js +33 -37
- package/dist/json-patch/utils/updateArrayPath.d.ts +5 -2
- package/dist/json-patch/utils/updateArrayPath.js +29 -42
- package/dist/net/PatchesClient.d.ts +128 -0
- package/dist/net/PatchesClient.js +161 -0
- package/dist/net/PatchesSync.d.ts +19 -9
- package/dist/net/PatchesSync.js +291 -386
- package/dist/net/error.d.ts +3 -1
- package/dist/net/error.js +9 -6
- package/dist/net/http/FetchTransport.d.ts +21 -0
- package/dist/net/http/FetchTransport.js +34 -0
- package/dist/net/index.d.ts +26 -12
- package/dist/net/index.js +12 -10
- package/dist/net/protocol/JSONRPCClient.d.ts +11 -4
- package/dist/net/protocol/JSONRPCClient.js +95 -103
- package/dist/net/protocol/JSONRPCServer.d.ts +15 -8
- package/dist/net/protocol/JSONRPCServer.js +101 -123
- package/dist/net/protocol/types.d.ts +21 -15
- package/dist/net/protocol/types.js +0 -1
- package/dist/net/protocol/utils.d.ts +12 -0
- package/dist/net/protocol/utils.js +15 -0
- package/dist/net/types.d.ts +4 -2
- package/dist/net/types.js +0 -1
- package/dist/net/webrtc/WebRTCAwareness.d.ts +14 -4
- package/dist/net/webrtc/WebRTCAwareness.js +111 -120
- package/dist/net/webrtc/WebRTCTransport.d.ts +16 -8
- package/dist/net/webrtc/WebRTCTransport.js +149 -157
- package/dist/net/webrtc/index.d.ts +10 -2
- package/dist/net/webrtc/index.js +2 -2
- package/dist/net/websocket/AuthorizationProvider.d.ts +7 -5
- package/dist/net/websocket/AuthorizationProvider.js +12 -17
- package/dist/net/websocket/PatchesWebSocket.d.ts +14 -109
- package/dist/net/websocket/PatchesWebSocket.js +37 -184
- package/dist/net/websocket/RPCServer.d.ts +19 -10
- package/dist/net/websocket/RPCServer.js +190 -192
- package/dist/net/websocket/SignalingService.d.ts +12 -32
- package/dist/net/websocket/SignalingService.js +126 -133
- package/dist/net/websocket/WebSocketServer.d.ts +17 -4
- package/dist/net/websocket/WebSocketServer.js +64 -72
- package/dist/net/websocket/WebSocketTransport.d.ts +13 -5
- package/dist/net/websocket/WebSocketTransport.js +178 -207
- package/dist/net/websocket/onlineState.d.ts +6 -3
- package/dist/net/websocket/onlineState.js +25 -21
- package/dist/server/PatchesBranchManager.d.ts +12 -5
- package/dist/server/PatchesBranchManager.js +132 -142
- package/dist/server/PatchesHistoryManager.d.ts +11 -3
- package/dist/server/PatchesHistoryManager.js +81 -84
- package/dist/server/PatchesServer.d.ts +16 -10
- package/dist/server/PatchesServer.js +131 -137
- package/dist/server/index.d.ts +7 -2
- package/dist/server/index.js +9 -3
- package/dist/server/types.d.ts +9 -3
- package/dist/server/types.js +0 -1
- package/dist/types.d.ts +38 -19
- package/dist/types.js +1 -1
- package/dist/utils/concurrency.d.ts +7 -5
- package/dist/utils/concurrency.js +43 -53
- package/dist/utils/deferred.d.ts +4 -2
- package/dist/utils/deferred.js +25 -21
- package/package.json +5 -7
package/dist/net/error.d.ts
CHANGED
package/dist/net/error.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import "../chunk-IZ2YBCUP.js";
|
|
2
|
+
class StatusError extends Error {
|
|
3
|
+
constructor(code, message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.code = code;
|
|
6
|
+
}
|
|
7
7
|
}
|
|
8
|
+
export {
|
|
9
|
+
StatusError
|
|
10
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Signal } from '../../event-signal.js';
|
|
2
|
+
import { ClientTransport } from '../protocol/types.js';
|
|
3
|
+
import '../../types.js';
|
|
4
|
+
import '../../json-patch/JSONPatch.js';
|
|
5
|
+
import '@dabble/delta';
|
|
6
|
+
import '../../json-patch/types.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Transport that uses fetch to send and receive messages.
|
|
10
|
+
*/
|
|
11
|
+
declare class FetchTransport implements ClientTransport {
|
|
12
|
+
private url;
|
|
13
|
+
private authHeader;
|
|
14
|
+
readonly onMessage: Signal<(raw: string) => void>;
|
|
15
|
+
private headers;
|
|
16
|
+
constructor(url: string, authHeader: string);
|
|
17
|
+
addHeadersToNextCall(headers: Record<string, string>): void;
|
|
18
|
+
send(raw: string): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { FetchTransport };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import "../../chunk-IZ2YBCUP.js";
|
|
2
|
+
import { signal } from "../../event-signal.js";
|
|
3
|
+
import { rpcError } from "../protocol/utils.js";
|
|
4
|
+
class FetchTransport {
|
|
5
|
+
constructor(url, authHeader) {
|
|
6
|
+
this.url = url;
|
|
7
|
+
this.authHeader = authHeader;
|
|
8
|
+
}
|
|
9
|
+
onMessage = signal();
|
|
10
|
+
headers;
|
|
11
|
+
addHeadersToNextCall(headers) {
|
|
12
|
+
this.headers = headers;
|
|
13
|
+
}
|
|
14
|
+
async send(raw) {
|
|
15
|
+
try {
|
|
16
|
+
const response = await fetch(this.url, {
|
|
17
|
+
method: "POST",
|
|
18
|
+
headers: {
|
|
19
|
+
"Content-Type": "application/json",
|
|
20
|
+
Authorization: this.authHeader,
|
|
21
|
+
...this.headers
|
|
22
|
+
},
|
|
23
|
+
body: raw
|
|
24
|
+
});
|
|
25
|
+
this.headers = void 0;
|
|
26
|
+
this.onMessage.emit(await response.text());
|
|
27
|
+
} catch (error) {
|
|
28
|
+
this.onMessage.emit(JSON.stringify(rpcError(-32e3, error.message)));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export {
|
|
33
|
+
FetchTransport
|
|
34
|
+
};
|
package/dist/net/index.d.ts
CHANGED
|
@@ -1,12 +1,26 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
export
|
|
9
|
-
export
|
|
10
|
-
export
|
|
11
|
-
export
|
|
12
|
-
export
|
|
1
|
+
export { FetchTransport } from './http/FetchTransport.js';
|
|
2
|
+
export { PatchesClient } from './PatchesClient.js';
|
|
3
|
+
export { PatchesSync, PatchesSyncOptions, PatchesSyncState } from './PatchesSync.js';
|
|
4
|
+
export { JSONRPCClient } from './protocol/JSONRPCClient.js';
|
|
5
|
+
export { ConnectionSignalSubscriber, JSONRPCServer, MessageHandler } from './protocol/JSONRPCServer.js';
|
|
6
|
+
export { AwarenessUpdateNotificationParams, ClientTransport, ConnectionState, JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, ListOptions, Message, PatchesAPI, PatchesNotificationParams, ServerTransport, SignalNotificationParams } from './protocol/types.js';
|
|
7
|
+
export { PatchesState, SyncingState } from './types.js';
|
|
8
|
+
export { Access, AuthContext, AuthorizationProvider, allowAll, denyAll } from './websocket/AuthorizationProvider.js';
|
|
9
|
+
export { onlineState } from './websocket/onlineState.js';
|
|
10
|
+
export { PatchesWebSocket } from './websocket/PatchesWebSocket.js';
|
|
11
|
+
export { RPCServer, RPCServerOptions } from './websocket/RPCServer.js';
|
|
12
|
+
export { JsonRpcMessage, SendFn, SignalingService } from './websocket/SignalingService.js';
|
|
13
|
+
export { WebSocketServer } from './websocket/WebSocketServer.js';
|
|
14
|
+
export { WebSocketOptions, WebSocketTransport } from './websocket/WebSocketTransport.js';
|
|
15
|
+
import '../event-signal.js';
|
|
16
|
+
import '../types.js';
|
|
17
|
+
import '../json-patch/JSONPatch.js';
|
|
18
|
+
import '@dabble/delta';
|
|
19
|
+
import '../json-patch/types.js';
|
|
20
|
+
import '../client/Patches.js';
|
|
21
|
+
import '../client/PatchesDoc.js';
|
|
22
|
+
import '../client/PatchesStore.js';
|
|
23
|
+
import '../server/PatchesBranchManager.js';
|
|
24
|
+
import '../server/PatchesServer.js';
|
|
25
|
+
import '../server/types.js';
|
|
26
|
+
import '../server/PatchesHistoryManager.js';
|
package/dist/net/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
8
|
-
export * from
|
|
9
|
-
export * from
|
|
10
|
-
export * from
|
|
1
|
+
export * from "./http/FetchTransport.js";
|
|
2
|
+
export * from "./PatchesClient.js";
|
|
3
|
+
export * from "./PatchesSync.js";
|
|
4
|
+
export * from "./protocol/JSONRPCClient.js";
|
|
5
|
+
export * from "./protocol/JSONRPCServer.js";
|
|
6
|
+
export * from "./websocket/AuthorizationProvider.js";
|
|
7
|
+
export * from "./websocket/onlineState.js";
|
|
8
|
+
export * from "./websocket/PatchesWebSocket.js";
|
|
9
|
+
export * from "./websocket/RPCServer.js";
|
|
10
|
+
export * from "./websocket/SignalingService.js";
|
|
11
|
+
export * from "./websocket/WebSocketServer.js";
|
|
12
|
+
export * from "./websocket/WebSocketTransport.js";
|
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import { SignalSubscriber, Unsubscriber } from '../../event-signal.js';
|
|
2
|
+
import { ClientTransport } from './types.js';
|
|
3
|
+
import '../../types.js';
|
|
4
|
+
import '../../json-patch/JSONPatch.js';
|
|
5
|
+
import '@dabble/delta';
|
|
6
|
+
import '../../json-patch/types.js';
|
|
7
|
+
|
|
3
8
|
/**
|
|
4
9
|
* Implementation of a JSON-RPC 2.0 client that communicates over a provided transport layer.
|
|
5
10
|
* This client handles sending requests, notifications, and processing responses from a server.
|
|
6
11
|
* It also supports subscription to server-sent notifications.
|
|
7
12
|
*/
|
|
8
|
-
|
|
13
|
+
declare class JSONRPCClient {
|
|
9
14
|
private transport;
|
|
10
15
|
private nextId;
|
|
11
16
|
private pending;
|
|
@@ -24,7 +29,7 @@ export declare class JSONRPCClient {
|
|
|
24
29
|
* @returns A promise that resolves with the result of the procedure call or rejects with an error
|
|
25
30
|
* @template T - The expected return type of the remote procedure
|
|
26
31
|
*/
|
|
27
|
-
|
|
32
|
+
call<T = any>(method: string, params?: any): Promise<T>;
|
|
28
33
|
/**
|
|
29
34
|
* Sends a JSON-RPC notification to the server (no response expected).
|
|
30
35
|
*
|
|
@@ -53,3 +58,5 @@ export declare class JSONRPCClient {
|
|
|
53
58
|
*/
|
|
54
59
|
private handleMessage;
|
|
55
60
|
}
|
|
61
|
+
|
|
62
|
+
export { JSONRPCClient };
|
|
@@ -1,107 +1,99 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
import "../../chunk-IZ2YBCUP.js";
|
|
2
|
+
import { signal } from "../../event-signal.js";
|
|
3
|
+
class JSONRPCClient {
|
|
4
|
+
/**
|
|
5
|
+
* Creates a new JSON-RPC client instance.
|
|
6
|
+
*
|
|
7
|
+
* @param transport - The transport layer implementation that will be used for sending/receiving messages
|
|
8
|
+
*/
|
|
9
|
+
constructor(transport) {
|
|
10
|
+
this.transport = transport;
|
|
11
|
+
transport.onMessage(this.handleMessage.bind(this));
|
|
12
|
+
}
|
|
13
|
+
nextId = 1;
|
|
14
|
+
pending = /* @__PURE__ */ new Map();
|
|
15
|
+
notificationSignals = /* @__PURE__ */ new Map();
|
|
16
|
+
/**
|
|
17
|
+
* Sends a JSON-RPC request to the server and returns a promise for the response.
|
|
18
|
+
*
|
|
19
|
+
* @param method - The name of the remote procedure to call
|
|
20
|
+
* @param params - The parameters to pass to the remote procedure (optional)
|
|
21
|
+
* @returns A promise that resolves with the result of the procedure call or rejects with an error
|
|
22
|
+
* @template T - The expected return type of the remote procedure
|
|
23
|
+
*/
|
|
24
|
+
async call(method, params) {
|
|
25
|
+
const id = this.nextId++;
|
|
26
|
+
const message = { jsonrpc: "2.0", id, method, params };
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
this.pending.set(id, { resolve, reject });
|
|
29
|
+
this.transport.send(JSON.stringify(message));
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Sends a JSON-RPC notification to the server (no response expected).
|
|
34
|
+
*
|
|
35
|
+
* @param method - The name of the remote procedure to call
|
|
36
|
+
* @param params - The parameters to pass to the remote procedure (optional)
|
|
37
|
+
*/
|
|
38
|
+
notify(method, params) {
|
|
39
|
+
const message = { jsonrpc: "2.0", method, params };
|
|
40
|
+
this.transport.send(JSON.stringify(message));
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Subscribes to server-sent notifications for a specific method.
|
|
44
|
+
*
|
|
45
|
+
* @param method - The notification method name to subscribe to
|
|
46
|
+
* @param handler - The callback function that will be invoked when notifications are received
|
|
47
|
+
* @returns A function that can be called to unsubscribe from the notifications
|
|
48
|
+
* @template T - The type of the handler function
|
|
49
|
+
*/
|
|
50
|
+
on(method, handler) {
|
|
51
|
+
let thisSignal = this.notificationSignals.get(method);
|
|
52
|
+
if (!thisSignal) {
|
|
53
|
+
thisSignal = signal();
|
|
54
|
+
this.notificationSignals.set(method, thisSignal);
|
|
20
55
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
* @template T - The type of the handler function
|
|
54
|
-
*/
|
|
55
|
-
on(method, handler) {
|
|
56
|
-
let thisSignal = this.notificationSignals.get(method);
|
|
57
|
-
if (!thisSignal) {
|
|
58
|
-
thisSignal = signal();
|
|
59
|
-
this.notificationSignals.set(method, thisSignal);
|
|
60
|
-
}
|
|
61
|
-
return thisSignal(handler);
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Processes incoming messages from the transport layer.
|
|
65
|
-
* Handles three types of messages:
|
|
66
|
-
* - Notifications: Emitted to registered subscribers
|
|
67
|
-
* - Responses: Resolved/rejected to the corresponding pending promise
|
|
68
|
-
* - Invalid messages: Logged as warnings
|
|
69
|
-
*
|
|
70
|
-
* @private
|
|
71
|
-
* @param data - The raw message data received from the transport
|
|
72
|
-
*/
|
|
73
|
-
handleMessage(data) {
|
|
74
|
-
try {
|
|
75
|
-
const message = JSON.parse(data);
|
|
76
|
-
// Check if it's a notification (has method but no id)
|
|
77
|
-
if (typeof message === 'object' && message !== null && 'method' in message && !('id' in message)) {
|
|
78
|
-
const thisSignal = this.notificationSignals.get(message.method);
|
|
79
|
-
if (thisSignal)
|
|
80
|
-
thisSignal.emit(message.params);
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
// Must be a response (has id)
|
|
84
|
-
if (typeof message === 'object' && message !== null && 'id' in message) {
|
|
85
|
-
const response = message;
|
|
86
|
-
const pending = this.pending.get(response.id);
|
|
87
|
-
if (pending) {
|
|
88
|
-
this.pending.delete(response.id);
|
|
89
|
-
if ('error' in response) {
|
|
90
|
-
pending.reject(response.error);
|
|
91
|
-
}
|
|
92
|
-
else {
|
|
93
|
-
pending.resolve(response.result);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
console.warn(`Received response for unknown id: ${response.id}`);
|
|
98
|
-
}
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
console.warn('Received unexpected message format:', message);
|
|
102
|
-
}
|
|
103
|
-
catch (error) {
|
|
104
|
-
console.error('Failed to parse incoming message:', data, error);
|
|
56
|
+
return thisSignal(handler);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Processes incoming messages from the transport layer.
|
|
60
|
+
* Handles three types of messages:
|
|
61
|
+
* - Notifications: Emitted to registered subscribers
|
|
62
|
+
* - Responses: Resolved/rejected to the corresponding pending promise
|
|
63
|
+
* - Invalid messages: Logged as warnings
|
|
64
|
+
*
|
|
65
|
+
* @private
|
|
66
|
+
* @param data - The raw message data received from the transport
|
|
67
|
+
*/
|
|
68
|
+
handleMessage(data) {
|
|
69
|
+
try {
|
|
70
|
+
const message = JSON.parse(data);
|
|
71
|
+
if (typeof message === "object" && message !== null && "method" in message && !("id" in message)) {
|
|
72
|
+
const thisSignal = this.notificationSignals.get(message.method);
|
|
73
|
+
if (thisSignal) thisSignal.emit(message.params);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (typeof message === "object" && message !== null && "id" in message) {
|
|
77
|
+
const response = message;
|
|
78
|
+
const pending = this.pending.get(response.id);
|
|
79
|
+
if (pending) {
|
|
80
|
+
this.pending.delete(response.id);
|
|
81
|
+
if ("error" in response) {
|
|
82
|
+
pending.reject(response.error);
|
|
83
|
+
} else {
|
|
84
|
+
pending.resolve(response.result);
|
|
85
|
+
}
|
|
86
|
+
} else {
|
|
87
|
+
console.warn(`Received response for unknown id: ${response.id}`);
|
|
105
88
|
}
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
console.warn("Received unexpected message format:", message);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
console.error("Failed to parse incoming message:", data, error);
|
|
106
94
|
}
|
|
95
|
+
}
|
|
107
96
|
}
|
|
97
|
+
export {
|
|
98
|
+
JSONRPCClient
|
|
99
|
+
};
|
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { Signal, Unsubscriber } from '../../event-signal.js';
|
|
2
|
+
import { AuthContext } from '../websocket/AuthorizationProvider.js';
|
|
3
|
+
import { JsonRpcNotification, Message, JsonRpcResponse } from './types.js';
|
|
4
|
+
import '../../types.js';
|
|
5
|
+
import '../../json-patch/JSONPatch.js';
|
|
6
|
+
import '@dabble/delta';
|
|
7
|
+
import '../../json-patch/types.js';
|
|
8
|
+
|
|
9
|
+
type ConnectionSignalSubscriber = (params: any, clientId?: string) => any;
|
|
10
|
+
type MessageHandler<P = any, R = any> = (params: P, ctx?: AuthContext) => Promise<R> | R;
|
|
6
11
|
/**
|
|
7
12
|
* Lightweight JSON-RPC 2.0 server adapter for {@link PatchesServer}.
|
|
8
13
|
*
|
|
@@ -19,13 +24,13 @@ export type MessageHandler<P = any, R = any> = (params: P, ctx?: AuthContext) =>
|
|
|
19
24
|
* calls. How you generate that ID (auth token, random GUID, etc.) is left
|
|
20
25
|
* to the host application.
|
|
21
26
|
*/
|
|
22
|
-
|
|
27
|
+
declare class JSONRPCServer {
|
|
23
28
|
/** Map of fully-qualified JSON-RPC method → handler function */
|
|
24
29
|
private readonly handlers;
|
|
25
30
|
/** Allow external callers to emit server-initiated notifications. */
|
|
26
31
|
private readonly notificationSignals;
|
|
27
32
|
/** Allow external callers to emit server-initiated notifications. */
|
|
28
|
-
readonly onNotify: Signal<(msg:
|
|
33
|
+
readonly onNotify: Signal<(msg: JsonRpcNotification, exceptConnectionId?: string) => void>;
|
|
29
34
|
/**
|
|
30
35
|
* Registers a JSON-RPC method.
|
|
31
36
|
*
|
|
@@ -60,7 +65,7 @@ export declare class JSONRPCServer {
|
|
|
60
65
|
* internally; the returned string is forwarded over the socket.
|
|
61
66
|
*/
|
|
62
67
|
processMessage(raw: string, ctx?: AuthContext): Promise<string | undefined>;
|
|
63
|
-
processMessage(message: Message, ctx?: AuthContext): Promise<
|
|
68
|
+
processMessage(message: Message, ctx?: AuthContext): Promise<JsonRpcResponse | undefined>;
|
|
64
69
|
/**
|
|
65
70
|
* Maps JSON-RPC method names to {@link PatchesServer} calls.
|
|
66
71
|
* @param connectionId - The WebSocket transport object.
|
|
@@ -70,3 +75,5 @@ export declare class JSONRPCServer {
|
|
|
70
75
|
*/
|
|
71
76
|
protected _dispatch(method: string, params: any, ctx?: AuthContext): Promise<any>;
|
|
72
77
|
}
|
|
78
|
+
|
|
79
|
+
export { type ConnectionSignalSubscriber, JSONRPCServer, type MessageHandler };
|