@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.
Files changed (181) hide show
  1. package/dist/algorithms/client/applyCommittedChanges.d.ts +8 -2
  2. package/dist/algorithms/client/applyCommittedChanges.js +30 -38
  3. package/dist/algorithms/client/batching.d.ts +8 -2
  4. package/dist/algorithms/client/batching.js +38 -37
  5. package/dist/algorithms/client/breakChange.d.ts +8 -2
  6. package/dist/algorithms/client/breakChange.js +191 -240
  7. package/dist/algorithms/client/createStateFromSnapshot.d.ts +8 -2
  8. package/dist/algorithms/client/createStateFromSnapshot.js +7 -8
  9. package/dist/algorithms/client/getJSONByteSize.d.ts +3 -1
  10. package/dist/algorithms/client/getJSONByteSize.js +12 -11
  11. package/dist/algorithms/client/makeChange.d.ts +8 -2
  12. package/dist/algorithms/client/makeChange.js +28 -36
  13. package/dist/algorithms/server/commitChanges.d.ts +9 -3
  14. package/dist/algorithms/server/commitChanges.js +69 -78
  15. package/dist/algorithms/server/createVersion.d.ts +9 -3
  16. package/dist/algorithms/server/createVersion.js +21 -27
  17. package/dist/algorithms/server/getSnapshotAtRevision.d.ts +9 -3
  18. package/dist/algorithms/server/getSnapshotAtRevision.js +27 -28
  19. package/dist/algorithms/server/getStateAtRevision.d.ts +9 -3
  20. package/dist/algorithms/server/getStateAtRevision.js +13 -17
  21. package/dist/algorithms/server/handleOfflineSessionsAndBatches.d.ts +9 -3
  22. package/dist/algorithms/server/handleOfflineSessionsAndBatches.js +60 -77
  23. package/dist/algorithms/server/transformIncomingChanges.d.ts +8 -2
  24. package/dist/algorithms/server/transformIncomingChanges.js +27 -39
  25. package/dist/algorithms/shared/applyChanges.d.ts +8 -2
  26. package/dist/algorithms/shared/applyChanges.js +11 -16
  27. package/dist/algorithms/shared/rebaseChanges.d.ts +8 -2
  28. package/dist/algorithms/shared/rebaseChanges.js +30 -49
  29. package/dist/chunk-IZ2YBCUP.js +56 -0
  30. package/dist/client/InMemoryStore.d.ts +9 -3
  31. package/dist/client/InMemoryStore.js +92 -101
  32. package/dist/client/IndexedDBStore.d.ts +9 -3
  33. package/dist/client/IndexedDBStore.js +378 -491
  34. package/dist/client/Patches.d.ts +18 -13
  35. package/dist/client/Patches.js +152 -207
  36. package/dist/client/PatchesDoc.d.ts +14 -8
  37. package/dist/client/PatchesDoc.js +147 -154
  38. package/dist/client/PatchesHistoryClient.d.ts +12 -5
  39. package/dist/client/PatchesHistoryClient.js +110 -117
  40. package/dist/client/PatchesStore.d.ts +9 -3
  41. package/dist/client/PatchesStore.js +0 -1
  42. package/dist/client/index.d.ts +12 -6
  43. package/dist/client/index.js +5 -5
  44. package/dist/data/change.d.ts +9 -3
  45. package/dist/data/change.js +23 -15
  46. package/dist/data/version.d.ts +9 -3
  47. package/dist/data/version.js +11 -15
  48. package/dist/event-signal.d.ts +7 -6
  49. package/dist/event-signal.js +24 -39
  50. package/dist/index-CvQws3AB.d.ts +36 -0
  51. package/dist/index.d.ts +27 -5
  52. package/dist/index.js +10 -4
  53. package/dist/json-patch/JSONPatch.d.ts +9 -5
  54. package/dist/json-patch/JSONPatch.js +175 -183
  55. package/dist/json-patch/applyPatch.d.ts +5 -2
  56. package/dist/json-patch/applyPatch.js +27 -35
  57. package/dist/json-patch/composePatch.d.ts +5 -2
  58. package/dist/json-patch/composePatch.js +34 -34
  59. package/dist/json-patch/createJSONPatch.d.ts +7 -2
  60. package/dist/json-patch/createJSONPatch.js +11 -38
  61. package/dist/json-patch/index.d.ts +14 -6
  62. package/dist/json-patch/index.js +20 -9
  63. package/dist/json-patch/invertPatch.d.ts +5 -2
  64. package/dist/json-patch/invertPatch.js +31 -30
  65. package/dist/json-patch/ops/add.d.ts +5 -2
  66. package/dist/json-patch/ops/add.js +53 -51
  67. package/dist/json-patch/ops/bitmask.d.ts +8 -5
  68. package/dist/json-patch/ops/bitmask.js +41 -44
  69. package/dist/json-patch/ops/copy.d.ts +5 -2
  70. package/dist/json-patch/ops/copy.js +32 -33
  71. package/dist/json-patch/ops/increment.d.ts +5 -2
  72. package/dist/json-patch/ops/increment.js +21 -20
  73. package/dist/json-patch/ops/index.d.ts +10 -21
  74. package/dist/json-patch/ops/index.js +34 -24
  75. package/dist/json-patch/ops/move.d.ts +5 -2
  76. package/dist/json-patch/ops/move.js +132 -198
  77. package/dist/json-patch/ops/remove.d.ts +5 -2
  78. package/dist/json-patch/ops/remove.js +33 -30
  79. package/dist/json-patch/ops/replace.d.ts +5 -2
  80. package/dist/json-patch/ops/replace.js +45 -43
  81. package/dist/json-patch/ops/test.d.ts +5 -2
  82. package/dist/json-patch/ops/test.js +25 -21
  83. package/dist/json-patch/ops/text.d.ts +5 -2
  84. package/dist/json-patch/ops/text.js +54 -54
  85. package/dist/json-patch/pathProxy.d.ts +9 -3
  86. package/dist/json-patch/pathProxy.js +27 -48
  87. package/dist/json-patch/state.d.ts +5 -2
  88. package/dist/json-patch/state.js +11 -7
  89. package/dist/json-patch/transformPatch.d.ts +6 -2
  90. package/dist/json-patch/transformPatch.js +21 -24
  91. package/dist/json-patch/types.d.ts +9 -7
  92. package/dist/json-patch/types.js +0 -1
  93. package/dist/json-patch/utils/deepEqual.d.ts +3 -1
  94. package/dist/json-patch/utils/deepEqual.js +32 -28
  95. package/dist/json-patch/utils/exit.d.ts +5 -2
  96. package/dist/json-patch/utils/exit.js +7 -3
  97. package/dist/json-patch/utils/get.d.ts +5 -2
  98. package/dist/json-patch/utils/get.js +8 -4
  99. package/dist/json-patch/utils/getOpData.d.ts +5 -2
  100. package/dist/json-patch/utils/getOpData.js +12 -9
  101. package/dist/json-patch/utils/getType.d.ts +6 -3
  102. package/dist/json-patch/utils/getType.js +9 -4
  103. package/dist/json-patch/utils/index.d.ts +15 -14
  104. package/dist/json-patch/utils/index.js +14 -14
  105. package/dist/json-patch/utils/log.d.ts +4 -2
  106. package/dist/json-patch/utils/log.js +8 -3
  107. package/dist/json-patch/utils/ops.d.ts +8 -5
  108. package/dist/json-patch/utils/ops.js +83 -100
  109. package/dist/json-patch/utils/paths.d.ts +12 -9
  110. package/dist/json-patch/utils/paths.js +54 -51
  111. package/dist/json-patch/utils/pluck.d.ts +8 -5
  112. package/dist/json-patch/utils/pluck.js +32 -26
  113. package/dist/json-patch/utils/shallowCopy.d.ts +3 -1
  114. package/dist/json-patch/utils/shallowCopy.js +22 -18
  115. package/dist/json-patch/utils/softWrites.d.ts +6 -3
  116. package/dist/json-patch/utils/softWrites.js +17 -16
  117. package/dist/json-patch/utils/toArrayIndex.d.ts +3 -1
  118. package/dist/json-patch/utils/toArrayIndex.js +14 -10
  119. package/dist/json-patch/utils/toKeys.d.ts +3 -1
  120. package/dist/json-patch/utils/toKeys.js +15 -11
  121. package/dist/json-patch/utils/updateArrayIndexes.d.ts +5 -2
  122. package/dist/json-patch/utils/updateArrayIndexes.js +33 -37
  123. package/dist/json-patch/utils/updateArrayPath.d.ts +5 -2
  124. package/dist/json-patch/utils/updateArrayPath.js +29 -42
  125. package/dist/net/PatchesClient.d.ts +128 -0
  126. package/dist/net/PatchesClient.js +161 -0
  127. package/dist/net/PatchesSync.d.ts +19 -9
  128. package/dist/net/PatchesSync.js +291 -386
  129. package/dist/net/error.d.ts +3 -1
  130. package/dist/net/error.js +9 -6
  131. package/dist/net/http/FetchTransport.d.ts +21 -0
  132. package/dist/net/http/FetchTransport.js +34 -0
  133. package/dist/net/index.d.ts +26 -12
  134. package/dist/net/index.js +12 -10
  135. package/dist/net/protocol/JSONRPCClient.d.ts +11 -4
  136. package/dist/net/protocol/JSONRPCClient.js +95 -103
  137. package/dist/net/protocol/JSONRPCServer.d.ts +15 -8
  138. package/dist/net/protocol/JSONRPCServer.js +101 -123
  139. package/dist/net/protocol/types.d.ts +21 -15
  140. package/dist/net/protocol/types.js +0 -1
  141. package/dist/net/protocol/utils.d.ts +12 -0
  142. package/dist/net/protocol/utils.js +15 -0
  143. package/dist/net/types.d.ts +4 -2
  144. package/dist/net/types.js +0 -1
  145. package/dist/net/webrtc/WebRTCAwareness.d.ts +14 -4
  146. package/dist/net/webrtc/WebRTCAwareness.js +111 -120
  147. package/dist/net/webrtc/WebRTCTransport.d.ts +16 -8
  148. package/dist/net/webrtc/WebRTCTransport.js +149 -157
  149. package/dist/net/webrtc/index.d.ts +10 -2
  150. package/dist/net/webrtc/index.js +2 -2
  151. package/dist/net/websocket/AuthorizationProvider.d.ts +7 -5
  152. package/dist/net/websocket/AuthorizationProvider.js +12 -17
  153. package/dist/net/websocket/PatchesWebSocket.d.ts +14 -109
  154. package/dist/net/websocket/PatchesWebSocket.js +37 -184
  155. package/dist/net/websocket/RPCServer.d.ts +19 -10
  156. package/dist/net/websocket/RPCServer.js +190 -192
  157. package/dist/net/websocket/SignalingService.d.ts +12 -32
  158. package/dist/net/websocket/SignalingService.js +126 -133
  159. package/dist/net/websocket/WebSocketServer.d.ts +17 -4
  160. package/dist/net/websocket/WebSocketServer.js +64 -72
  161. package/dist/net/websocket/WebSocketTransport.d.ts +13 -5
  162. package/dist/net/websocket/WebSocketTransport.js +178 -207
  163. package/dist/net/websocket/onlineState.d.ts +6 -3
  164. package/dist/net/websocket/onlineState.js +25 -21
  165. package/dist/server/PatchesBranchManager.d.ts +12 -5
  166. package/dist/server/PatchesBranchManager.js +132 -142
  167. package/dist/server/PatchesHistoryManager.d.ts +11 -3
  168. package/dist/server/PatchesHistoryManager.js +81 -84
  169. package/dist/server/PatchesServer.d.ts +16 -10
  170. package/dist/server/PatchesServer.js +131 -137
  171. package/dist/server/index.d.ts +7 -2
  172. package/dist/server/index.js +9 -3
  173. package/dist/server/types.d.ts +9 -3
  174. package/dist/server/types.js +0 -1
  175. package/dist/types.d.ts +38 -19
  176. package/dist/types.js +1 -1
  177. package/dist/utils/concurrency.d.ts +7 -5
  178. package/dist/utils/concurrency.js +43 -53
  179. package/dist/utils/deferred.d.ts +4 -2
  180. package/dist/utils/deferred.js +25 -21
  181. package/package.json +5 -7
@@ -1,4 +1,6 @@
1
- export declare class StatusError extends Error {
1
+ declare class StatusError extends Error {
2
2
  code: number;
3
3
  constructor(code: number, message: string);
4
4
  }
5
+
6
+ export { StatusError };
package/dist/net/error.js CHANGED
@@ -1,7 +1,10 @@
1
- export class StatusError extends Error {
2
- code;
3
- constructor(code, message) {
4
- super(message);
5
- this.code = code;
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
+ };
@@ -1,12 +1,26 @@
1
- export * from './PatchesSync.js';
2
- export * from './protocol/JSONRPCClient.js';
3
- export * from './protocol/JSONRPCServer.js';
4
- export type * from './protocol/types.js';
5
- export type * from './types.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
+ 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 './PatchesSync.js';
2
- export * from './protocol/JSONRPCClient.js';
3
- export * from './protocol/JSONRPCServer.js';
4
- export * from './websocket/AuthorizationProvider.js';
5
- export * from './websocket/onlineState.js';
6
- export * from './websocket/PatchesWebSocket.js';
7
- export * from './websocket/RPCServer.js';
8
- export * from './websocket/SignalingService.js';
9
- export * from './websocket/WebSocketServer.js';
10
- export * from './websocket/WebSocketTransport.js';
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 { type SignalSubscriber, type Unsubscriber } from '../../event-signal.js';
2
- import type { ClientTransport } from './types.js';
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
- export declare class JSONRPCClient {
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
- request<T = any>(method: string, params?: any): Promise<T>;
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 { signal } from '../../event-signal.js';
2
- /**
3
- * Implementation of a JSON-RPC 2.0 client that communicates over a provided transport layer.
4
- * This client handles sending requests, notifications, and processing responses from a server.
5
- * It also supports subscription to server-sent notifications.
6
- */
7
- export class JSONRPCClient {
8
- transport;
9
- nextId = 1;
10
- pending = new Map();
11
- notificationSignals = new Map();
12
- /**
13
- * Creates a new JSON-RPC client instance.
14
- *
15
- * @param transport - The transport layer implementation that will be used for sending/receiving messages
16
- */
17
- constructor(transport) {
18
- this.transport = transport;
19
- transport.onMessage(this.handleMessage.bind(this));
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
- * Sends a JSON-RPC request to the server and returns a promise for the response.
23
- *
24
- * @param method - The name of the remote procedure to call
25
- * @param params - The parameters to pass to the remote procedure (optional)
26
- * @returns A promise that resolves with the result of the procedure call or rejects with an error
27
- * @template T - The expected return type of the remote procedure
28
- */
29
- async request(method, params) {
30
- const id = this.nextId++;
31
- const message = { jsonrpc: '2.0', id, method, params };
32
- return new Promise((resolve, reject) => {
33
- this.pending.set(id, { resolve, reject });
34
- this.transport.send(JSON.stringify(message));
35
- });
36
- }
37
- /**
38
- * Sends a JSON-RPC notification to the server (no response expected).
39
- *
40
- * @param method - The name of the remote procedure to call
41
- * @param params - The parameters to pass to the remote procedure (optional)
42
- */
43
- notify(method, params) {
44
- const message = { jsonrpc: '2.0', method, params };
45
- this.transport.send(JSON.stringify(message));
46
- }
47
- /**
48
- * Subscribes to server-sent notifications for a specific method.
49
- *
50
- * @param method - The notification method name to subscribe to
51
- * @param handler - The callback function that will be invoked when notifications are received
52
- * @returns A function that can be called to unsubscribe from the notifications
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 { type Signal, type Unsubscriber } from '../../event-signal.js';
2
- import type { AuthContext } from '../websocket/AuthorizationProvider.js';
3
- import type { Message, Notification, Response } from './types.js';
4
- export type ConnectionSignalSubscriber = (params: any, clientId?: string) => any;
5
- export type MessageHandler<P = any, R = any> = (params: P, ctx?: AuthContext) => Promise<R> | R;
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
- export declare class JSONRPCServer {
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: Notification, exceptConnectionId?: string) => void>;
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<Response | undefined>;
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 };