@hops-ops/distributed 0.0.0

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 (242) hide show
  1. package/README.md +454 -0
  2. package/dist/auth-headers.d.ts +8 -0
  3. package/dist/auth-headers.js +35 -0
  4. package/dist/diagnostics.d.ts +2 -0
  5. package/dist/diagnostics.js +1 -0
  6. package/dist/document.d.ts +8 -0
  7. package/dist/document.js +6 -0
  8. package/dist/identity.d.ts +23 -0
  9. package/dist/identity.js +73 -0
  10. package/dist/index.d.ts +7 -0
  11. package/dist/index.js +6 -0
  12. package/dist/internal/cache-engine/create.d.ts +5 -0
  13. package/dist/internal/cache-engine/create.js +7 -0
  14. package/dist/internal/cache-engine/engine.d.ts +32 -0
  15. package/dist/internal/cache-engine/engine.js +1287 -0
  16. package/dist/internal/cache-engine/errors.d.ts +5 -0
  17. package/dist/internal/cache-engine/errors.js +10 -0
  18. package/dist/internal/cache-engine/helpers.d.ts +55 -0
  19. package/dist/internal/cache-engine/helpers.js +545 -0
  20. package/dist/internal/cache-engine/index.d.ts +3 -0
  21. package/dist/internal/cache-engine/index.js +2 -0
  22. package/dist/internal/cache-engine/types.d.ts +300 -0
  23. package/dist/internal/cache-engine/types.js +1 -0
  24. package/dist/internal/cache-engine.d.ts +3 -0
  25. package/dist/internal/cache-engine.js +2 -0
  26. package/dist/lib/assert-name.d.ts +2 -0
  27. package/dist/lib/assert-name.js +6 -0
  28. package/dist/lib/compare-code-units.d.ts +2 -0
  29. package/dist/lib/compare-code-units.js +4 -0
  30. package/dist/lib/deep-equal.d.ts +2 -0
  31. package/dist/lib/deep-equal.js +24 -0
  32. package/dist/lib/freeze-record.d.ts +2 -0
  33. package/dist/lib/freeze-record.js +13 -0
  34. package/dist/lib/index.d.ts +6 -0
  35. package/dist/lib/index.js +6 -0
  36. package/dist/lib/is-plain-record.d.ts +2 -0
  37. package/dist/lib/is-plain-record.js +8 -0
  38. package/dist/lib/report.d.ts +7 -0
  39. package/dist/lib/report.js +26 -0
  40. package/dist/protocol.d.ts +179 -0
  41. package/dist/protocol.js +558 -0
  42. package/dist/react/context.d.ts +20 -0
  43. package/dist/react/context.js +24 -0
  44. package/dist/react/index.d.ts +2 -0
  45. package/dist/react/index.js +3 -0
  46. package/dist/react/query.d.ts +20 -0
  47. package/dist/react/query.js +99 -0
  48. package/dist/replica/command-id.d.ts +2 -0
  49. package/dist/replica/command-id.js +19 -0
  50. package/dist/replica/command-runtime/constants.d.ts +5 -0
  51. package/dist/replica/command-runtime/constants.js +5 -0
  52. package/dist/replica/command-runtime/create.d.ts +2 -0
  53. package/dist/replica/command-runtime/create.js +644 -0
  54. package/dist/replica/command-runtime/errors.d.ts +16 -0
  55. package/dist/replica/command-runtime/errors.js +41 -0
  56. package/dist/replica/command-runtime/index.d.ts +5 -0
  57. package/dist/replica/command-runtime/index.js +4 -0
  58. package/dist/replica/command-runtime/lib/binding.d.ts +4 -0
  59. package/dist/replica/command-runtime/lib/binding.js +64 -0
  60. package/dist/replica/command-runtime/lib/effects.d.ts +11 -0
  61. package/dist/replica/command-runtime/lib/effects.js +89 -0
  62. package/dist/replica/command-runtime/lib/index.d.ts +9 -0
  63. package/dist/replica/command-runtime/lib/index.js +9 -0
  64. package/dist/replica/command-runtime/lib/inventory.d.ts +10 -0
  65. package/dist/replica/command-runtime/lib/inventory.js +141 -0
  66. package/dist/replica/command-runtime/lib/output.d.ts +13 -0
  67. package/dist/replica/command-runtime/lib/output.js +190 -0
  68. package/dist/replica/command-runtime/lib/projection.d.ts +20 -0
  69. package/dist/replica/command-runtime/lib/projection.js +255 -0
  70. package/dist/replica/command-runtime/lib/status.d.ts +7 -0
  71. package/dist/replica/command-runtime/lib/status.js +127 -0
  72. package/dist/replica/command-runtime/lib/transport.d.ts +16 -0
  73. package/dist/replica/command-runtime/lib/transport.js +112 -0
  74. package/dist/replica/command-runtime/lib/util.d.ts +16 -0
  75. package/dist/replica/command-runtime/lib/util.js +127 -0
  76. package/dist/replica/command-runtime/lifecycle.d.ts +11 -0
  77. package/dist/replica/command-runtime/lifecycle.js +13 -0
  78. package/dist/replica/command-runtime/symbols.d.ts +30 -0
  79. package/dist/replica/command-runtime/symbols.js +30 -0
  80. package/dist/replica/command-runtime/types.d.ts +222 -0
  81. package/dist/replica/command-runtime/types.js +1 -0
  82. package/dist/replica/command-runtime.d.ts +3 -0
  83. package/dist/replica/command-runtime.js +2 -0
  84. package/dist/replica/commands/clone.d.ts +12 -0
  85. package/dist/replica/commands/clone.js +187 -0
  86. package/dist/replica/commands/constants.d.ts +6 -0
  87. package/dist/replica/commands/constants.js +6 -0
  88. package/dist/replica/commands/errors.d.ts +7 -0
  89. package/dist/replica/commands/errors.js +22 -0
  90. package/dist/replica/commands/implementation.d.ts +8 -0
  91. package/dist/replica/commands/implementation.js +8 -0
  92. package/dist/replica/commands/index.d.ts +5 -0
  93. package/dist/replica/commands/index.js +4 -0
  94. package/dist/replica/commands/prepare.d.ts +27 -0
  95. package/dist/replica/commands/prepare.js +77 -0
  96. package/dist/replica/commands/presets.d.ts +12 -0
  97. package/dist/replica/commands/presets.js +84 -0
  98. package/dist/replica/commands/receipt.d.ts +3 -0
  99. package/dist/replica/commands/receipt.js +36 -0
  100. package/dist/replica/commands/resolve.d.ts +11 -0
  101. package/dist/replica/commands/resolve.js +165 -0
  102. package/dist/replica/commands/types.d.ts +278 -0
  103. package/dist/replica/commands/types.js +2 -0
  104. package/dist/replica/commands/util.d.ts +21 -0
  105. package/dist/replica/commands/util.js +96 -0
  106. package/dist/replica/commands/validate.d.ts +18 -0
  107. package/dist/replica/commands/validate.js +477 -0
  108. package/dist/replica/commands.d.ts +3 -0
  109. package/dist/replica/commands.js +2 -0
  110. package/dist/replica/diagnostics/event-log.d.ts +4 -0
  111. package/dist/replica/diagnostics/event-log.js +463 -0
  112. package/dist/replica/diagnostics/implementation.d.ts +4 -0
  113. package/dist/replica/diagnostics/implementation.js +4 -0
  114. package/dist/replica/diagnostics/index.d.ts +2 -0
  115. package/dist/replica/diagnostics/index.js +1 -0
  116. package/dist/replica/diagnostics/inspect.d.ts +6 -0
  117. package/dist/replica/diagnostics/inspect.js +170 -0
  118. package/dist/replica/diagnostics/types.d.ts +295 -0
  119. package/dist/replica/diagnostics/types.js +2 -0
  120. package/dist/replica/diagnostics.d.ts +3 -0
  121. package/dist/replica/diagnostics.js +2 -0
  122. package/dist/replica/distributed-replica/clocks.d.ts +23 -0
  123. package/dist/replica/distributed-replica/clocks.js +156 -0
  124. package/dist/replica/distributed-replica/constants.d.ts +12 -0
  125. package/dist/replica/distributed-replica/constants.js +10 -0
  126. package/dist/replica/distributed-replica/helpers.d.ts +48 -0
  127. package/dist/replica/distributed-replica/helpers.js +445 -0
  128. package/dist/replica/distributed-replica/hydration.d.ts +19 -0
  129. package/dist/replica/distributed-replica/hydration.js +380 -0
  130. package/dist/replica/distributed-replica/impl-diagnostics.d.ts +22 -0
  131. package/dist/replica/distributed-replica/impl-diagnostics.js +189 -0
  132. package/dist/replica/distributed-replica/impl-fetch-live.d.ts +39 -0
  133. package/dist/replica/distributed-replica/impl-fetch-live.js +395 -0
  134. package/dist/replica/distributed-replica/impl-hydration-orchestrate.d.ts +56 -0
  135. package/dist/replica/distributed-replica/impl-hydration-orchestrate.js +299 -0
  136. package/dist/replica/distributed-replica/impl-optimistic.d.ts +31 -0
  137. package/dist/replica/distributed-replica/impl-optimistic.js +172 -0
  138. package/dist/replica/distributed-replica/impl-protocol.d.ts +53 -0
  139. package/dist/replica/distributed-replica/impl-protocol.js +108 -0
  140. package/dist/replica/distributed-replica/impl.d.ts +44 -0
  141. package/dist/replica/distributed-replica/impl.js +1776 -0
  142. package/dist/replica/distributed-replica/index.d.ts +2 -0
  143. package/dist/replica/distributed-replica/index.js +4 -0
  144. package/dist/replica/distributed-replica/optimistic.d.ts +30 -0
  145. package/dist/replica/distributed-replica/optimistic.js +221 -0
  146. package/dist/replica/distributed-replica/types.d.ts +147 -0
  147. package/dist/replica/distributed-replica/types.js +1 -0
  148. package/dist/replica/distributed-replica/watch.d.ts +19 -0
  149. package/dist/replica/distributed-replica/watch.js +95 -0
  150. package/dist/replica/distributed-replica.d.ts +2 -0
  151. package/dist/replica/distributed-replica.js +2 -0
  152. package/dist/replica/graphql-transport.d.ts +24 -0
  153. package/dist/replica/graphql-transport.js +122 -0
  154. package/dist/replica/identity/clone.d.ts +6 -0
  155. package/dist/replica/identity/clone.js +54 -0
  156. package/dist/replica/identity/codec.d.ts +48 -0
  157. package/dist/replica/identity/codec.js +789 -0
  158. package/dist/replica/identity/constants.d.ts +3 -0
  159. package/dist/replica/identity/constants.js +18 -0
  160. package/dist/replica/identity/implementation.d.ts +5 -0
  161. package/dist/replica/identity/implementation.js +5 -0
  162. package/dist/replica/identity/index.d.ts +3 -0
  163. package/dist/replica/identity/index.js +3 -0
  164. package/dist/replica/identity/keys.d.ts +29 -0
  165. package/dist/replica/identity/keys.js +173 -0
  166. package/dist/replica/identity.d.ts +2 -0
  167. package/dist/replica/identity.js +2 -0
  168. package/dist/replica/index-maintenance/engine.d.ts +84 -0
  169. package/dist/replica/index-maintenance/engine.js +677 -0
  170. package/dist/replica/index-maintenance/implementation.d.ts +4 -0
  171. package/dist/replica/index-maintenance/implementation.js +4 -0
  172. package/dist/replica/index-maintenance/index.d.ts +2 -0
  173. package/dist/replica/index-maintenance/index.js +1 -0
  174. package/dist/replica/index-maintenance/registry.d.ts +2 -0
  175. package/dist/replica/index-maintenance/registry.js +49 -0
  176. package/dist/replica/index-maintenance/types.d.ts +58 -0
  177. package/dist/replica/index-maintenance/types.js +1 -0
  178. package/dist/replica/index-maintenance.d.ts +3 -0
  179. package/dist/replica/index-maintenance.js +2 -0
  180. package/dist/replica/index.d.ts +17 -0
  181. package/dist/replica/index.js +9 -0
  182. package/dist/replica/materialize.d.ts +10 -0
  183. package/dist/replica/materialize.js +163 -0
  184. package/dist/replica/normalize.d.ts +27 -0
  185. package/dist/replica/normalize.js +298 -0
  186. package/dist/replica/operation-binding.d.ts +14 -0
  187. package/dist/replica/operation-binding.js +76 -0
  188. package/dist/replica/persistence/idb.d.ts +20 -0
  189. package/dist/replica/persistence/idb.js +193 -0
  190. package/dist/replica/persistence/implementation.d.ts +4 -0
  191. package/dist/replica/persistence/implementation.js +4 -0
  192. package/dist/replica/persistence/index.d.ts +2 -0
  193. package/dist/replica/persistence/index.js +1 -0
  194. package/dist/replica/persistence/state.d.ts +104 -0
  195. package/dist/replica/persistence/state.js +734 -0
  196. package/dist/replica/persistence/types.d.ts +54 -0
  197. package/dist/replica/persistence/types.js +1 -0
  198. package/dist/replica/persistence.d.ts +3 -0
  199. package/dist/replica/persistence.js +2 -0
  200. package/dist/replica/query-plan/constants.d.ts +13 -0
  201. package/dist/replica/query-plan/constants.js +7 -0
  202. package/dist/replica/query-plan/filter.d.ts +39 -0
  203. package/dist/replica/query-plan/filter.js +544 -0
  204. package/dist/replica/query-plan/index.d.ts +4 -0
  205. package/dist/replica/query-plan/index.js +3 -0
  206. package/dist/replica/query-plan/order.d.ts +11 -0
  207. package/dist/replica/query-plan/order.js +158 -0
  208. package/dist/replica/query-plan/pagination.d.ts +6 -0
  209. package/dist/replica/query-plan/pagination.js +90 -0
  210. package/dist/replica/query-plan/resolve.d.ts +14 -0
  211. package/dist/replica/query-plan/resolve.js +163 -0
  212. package/dist/replica/query-plan/types.d.ts +89 -0
  213. package/dist/replica/query-plan/types.js +1 -0
  214. package/dist/replica/query-plan/util.d.ts +13 -0
  215. package/dist/replica/query-plan/util.js +62 -0
  216. package/dist/replica/query-plan.d.ts +3 -0
  217. package/dist/replica/query-plan.js +2 -0
  218. package/dist/replica/revalidation.d.ts +9 -0
  219. package/dist/replica/revalidation.js +86 -0
  220. package/dist/replica/selection.d.ts +20 -0
  221. package/dist/replica/selection.js +109 -0
  222. package/dist/replica/types.d.ts +657 -0
  223. package/dist/replica/types.js +1 -0
  224. package/dist/request.d.ts +17 -0
  225. package/dist/request.js +123 -0
  226. package/dist/sveltekit/auth.d.ts +18 -0
  227. package/dist/sveltekit/auth.js +9 -0
  228. package/dist/sveltekit/context.d.ts +28 -0
  229. package/dist/sveltekit/context.js +58 -0
  230. package/dist/sveltekit/index.d.ts +4 -0
  231. package/dist/sveltekit/index.js +4 -0
  232. package/dist/sveltekit/replica.d.ts +145 -0
  233. package/dist/sveltekit/replica.js +491 -0
  234. package/dist/sveltekit/server-replica.d.ts +52 -0
  235. package/dist/sveltekit/server-replica.js +176 -0
  236. package/dist/sveltekit/vite.d.ts +103 -0
  237. package/dist/sveltekit/vite.js +878 -0
  238. package/dist/types.d.ts +33 -0
  239. package/dist/types.js +1 -0
  240. package/dist/websocket.d.ts +41 -0
  241. package/dist/websocket.js +187 -0
  242. package/package.json +95 -0
@@ -0,0 +1,33 @@
1
+ /** Shared GraphQL client types for browser and server runtimes. */
2
+ import type { GraphqlResponseExtensions } from './protocol.js';
3
+ /** Variables accepted by an untyped GraphQL operation. */
4
+ export type GraphqlVariables = Record<string, unknown>;
5
+ /** Standard source location attached to a GraphQL error. */
6
+ export type GqlErrorLocation = {
7
+ line: number;
8
+ column: number;
9
+ };
10
+ /** GraphQL execution or transport error returned by the server. */
11
+ export type GqlError = {
12
+ message: string;
13
+ locations?: GqlErrorLocation[];
14
+ path?: Array<string | number>;
15
+ extensions?: Record<string, unknown> & {
16
+ code?: string;
17
+ };
18
+ };
19
+ /** Result returned by {@link requestGraphql}. */
20
+ export type GqlResult<TData> = {
21
+ data?: TData;
22
+ errors?: GqlError[];
23
+ /** Validated GraphQL response extensions, including Distributed receipts. */
24
+ extensions?: GraphqlResponseExtensions;
25
+ status: number;
26
+ };
27
+ /** Authentication material understood by Distributed's GraphQL transports. */
28
+ export type GqlAuth = {
29
+ accessToken?: string | null;
30
+ /** DevHeaders offline fallback only; ignored when an access token is present. */
31
+ userId?: string | null;
32
+ role?: string | null;
33
+ };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,41 @@
1
+ import { type GqlDocument } from './document.js';
2
+ import { type DistributedLiveCursor, type GraphqlResponseExtensions } from './protocol.js';
3
+ import type { GqlAuth, GqlError, GraphqlVariables } from './types.js';
4
+ /** Constructor accepted by the WebSocket transport (native or test implementation). */
5
+ export type WebSocketConstructor = typeof globalThis.WebSocket;
6
+ /** GraphQL execution payload delivered by a subscription. */
7
+ export type GqlWsResult<TData = unknown> = {
8
+ data?: TData | null;
9
+ errors?: GqlError[];
10
+ /** Validated top-level GraphQL extensions for this live frame. */
11
+ extensions?: GraphqlResponseExtensions;
12
+ };
13
+ export type GqlWsHandlers<TData = unknown> = {
14
+ onNext: (result: GqlWsResult<TData>) => void;
15
+ onError?: (error: unknown) => void;
16
+ onComplete?: () => void;
17
+ };
18
+ export type SubscribeOptions<TVariables extends GraphqlVariables = GraphqlVariables> = {
19
+ /** HTTP GraphQL URL used to derive the `.../ws` endpoint. */
20
+ httpUrl?: string;
21
+ /** Variables for the subscription operation. */
22
+ variables?: TVariables;
23
+ /**
24
+ * Latest server-issued cursors for a generated live operation.
25
+ * @internal Application code must not synthesize resume tokens.
26
+ */
27
+ resume?: readonly DistributedLiveCursor[];
28
+ /**
29
+ * Framework-owned GraphQL request extensions.
30
+ * @internal Generated transports, not application code, supply this value.
31
+ */
32
+ extensions?: Readonly<Record<string, unknown>>;
33
+ /** Override the runtime's global WebSocket constructor. */
34
+ webSocket?: WebSocketConstructor;
35
+ };
36
+ /** Build a same-origin WebSocket URL in a browser, or retain the path on a server. */
37
+ export declare function graphqlWsUrl(path?: string): string;
38
+ /** Map an HTTP GraphQL URL to its `graphql-transport-ws` endpoint. */
39
+ export declare function httpUrlToWsUrl(httpUrl: string): string;
40
+ /** Open one GraphQL subscription and return an idempotent unsubscribe function. */
41
+ export declare function subscribe<TData = unknown, TVariables extends GraphqlVariables = GraphqlVariables>(document: GqlDocument<TData, TVariables>, auth: GqlAuth | undefined, handlers: GqlWsHandlers<TData>, options?: SubscribeOptions<TVariables>): () => void;
@@ -0,0 +1,187 @@
1
+ /** Minimal `graphql-transport-ws` client with injectable browser primitives. */
2
+ import { applyWsDevHeaderParams, wsConnectionInitPayload } from './auth-headers.js';
3
+ import { documentToString } from './document.js';
4
+ import { DistributedProtocolError, distributedLiveResumeExtensions, parseGraphqlResponseExtensions } from './protocol.js';
5
+ function browserLocation() {
6
+ return typeof window === 'undefined' ? undefined : window.location;
7
+ }
8
+ /** Build a same-origin WebSocket URL in a browser, or retain the path on a server. */
9
+ export function graphqlWsUrl(path = '/graphql/ws') {
10
+ const location = browserLocation();
11
+ if (!location)
12
+ return path;
13
+ const protocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
14
+ const normalizedPath = path.startsWith('/') ? path : `/${path}`;
15
+ return `${protocol}//${location.host}${normalizedPath}`;
16
+ }
17
+ /** Map an HTTP GraphQL URL to its `graphql-transport-ws` endpoint. */
18
+ export function httpUrlToWsUrl(httpUrl) {
19
+ const location = browserLocation();
20
+ const base = location?.href ?? 'http://127.0.0.1/';
21
+ try {
22
+ const url = new URL(httpUrl, base);
23
+ const path = url.pathname.replace(/\/$/, '');
24
+ const wsPath = path.endsWith('/ws') ? path : `${path}/ws`;
25
+ if (httpUrl.startsWith('/')) {
26
+ return location
27
+ ? graphqlWsUrl(wsPath)
28
+ : `ws://127.0.0.1${wsPath.startsWith('/') ? wsPath : `/${wsPath}`}`;
29
+ }
30
+ url.protocol = url.protocol === 'https:' || url.protocol === 'wss:' ? 'wss:' : 'ws:';
31
+ url.pathname = wsPath;
32
+ url.search = '';
33
+ url.hash = '';
34
+ return url.toString();
35
+ }
36
+ catch {
37
+ return location ? graphqlWsUrl('/graphql/ws') : 'ws://127.0.0.1/graphql/ws';
38
+ }
39
+ }
40
+ /** Open one GraphQL subscription and return an idempotent unsubscribe function. */
41
+ export function subscribe(document, auth = {}, handlers, options = {}) {
42
+ const WebSocketImpl = options.webSocket ?? globalThis.WebSocket;
43
+ if (typeof WebSocketImpl !== 'function') {
44
+ throw new Error('subscribe requires a WebSocket implementation in this runtime; pass { webSocket } in the options');
45
+ }
46
+ const query = documentToString(document);
47
+ const location = browserLocation();
48
+ const href = options.httpUrl
49
+ ? httpUrlToWsUrl(options.httpUrl)
50
+ : graphqlWsUrl('/graphql/ws');
51
+ const url = new URL(href, location?.href ?? 'ws://127.0.0.1/');
52
+ applyWsDevHeaderParams(url, auth);
53
+ const socket = new WebSocketImpl(url.toString(), 'graphql-transport-ws');
54
+ const operationId = '1';
55
+ let closed = false;
56
+ const rejectProtocolFrame = (error) => {
57
+ handlers.onError?.(error);
58
+ if (closed)
59
+ return;
60
+ closed = true;
61
+ if (socket.readyState === WebSocketImpl.OPEN) {
62
+ try {
63
+ socket.send(JSON.stringify({ type: 'complete', id: operationId }));
64
+ }
65
+ catch {
66
+ // The protocol is already rejected; closing is the remaining fence.
67
+ }
68
+ }
69
+ socket.close();
70
+ };
71
+ socket.onopen = () => {
72
+ socket.send(JSON.stringify({
73
+ type: 'connection_init',
74
+ payload: wsConnectionInitPayload(auth)
75
+ }));
76
+ };
77
+ socket.onmessage = (event) => {
78
+ let message;
79
+ try {
80
+ message = JSON.parse(String(event.data));
81
+ }
82
+ catch {
83
+ return;
84
+ }
85
+ switch (message.type) {
86
+ case 'connection_ack':
87
+ const resumeExtensions = options.resume === undefined || options.resume.length === 0
88
+ ? undefined
89
+ : distributedLiveResumeExtensions(options.resume);
90
+ const extensions = mergeRequestExtensions(options.extensions, resumeExtensions);
91
+ socket.send(JSON.stringify({
92
+ type: 'subscribe',
93
+ id: operationId,
94
+ payload: {
95
+ query,
96
+ variables: options.variables ?? {},
97
+ ...(extensions === undefined ? {} : { extensions })
98
+ }
99
+ }));
100
+ break;
101
+ case 'next':
102
+ try {
103
+ handlers.onNext(parseNextPayload(message.payload));
104
+ }
105
+ catch (error) {
106
+ rejectProtocolFrame(error);
107
+ }
108
+ break;
109
+ case 'error':
110
+ handlers.onError?.(message.payload ?? 'subscription error');
111
+ break;
112
+ case 'complete':
113
+ handlers.onComplete?.();
114
+ break;
115
+ case 'ping':
116
+ socket.send(JSON.stringify({ type: 'pong' }));
117
+ break;
118
+ case 'connection_error':
119
+ handlers.onError?.(message.payload ?? 'connection error');
120
+ break;
121
+ default:
122
+ break;
123
+ }
124
+ };
125
+ socket.onerror = (event) => handlers.onError?.(event);
126
+ socket.onclose = (event) => {
127
+ if (!closed && !event.wasClean) {
128
+ handlers.onError?.(`WebSocket closed (${event.code}${event.reason ? `: ${event.reason}` : ''}) — check the GraphQL WebSocket endpoint`);
129
+ }
130
+ if (!closed)
131
+ handlers.onComplete?.();
132
+ };
133
+ return () => {
134
+ if (closed)
135
+ return;
136
+ closed = true;
137
+ if (socket.readyState === WebSocketImpl.OPEN) {
138
+ try {
139
+ socket.send(JSON.stringify({ type: 'complete', id: operationId }));
140
+ }
141
+ catch {
142
+ // The transport may have closed between the ready-state check and send.
143
+ }
144
+ }
145
+ socket.close();
146
+ };
147
+ }
148
+ function mergeRequestExtensions(base, resume) {
149
+ if (base === undefined)
150
+ return resume;
151
+ if (resume === undefined)
152
+ return base;
153
+ const baseDistributed = requestExtensionRecord(base.distributed, 'request.extensions.distributed');
154
+ const resumeDistributed = requestExtensionRecord(resume.distributed, 'request.extensions.distributed');
155
+ for (const key of Object.keys(resumeDistributed)) {
156
+ if (Object.hasOwn(baseDistributed, key)) {
157
+ throw new DistributedProtocolError('DISTRIBUTED_PROTOCOL_INVALID', `request.extensions.distributed.${key}`);
158
+ }
159
+ }
160
+ return Object.freeze({
161
+ ...base,
162
+ distributed: Object.freeze({
163
+ ...baseDistributed,
164
+ ...resumeDistributed
165
+ })
166
+ });
167
+ }
168
+ function requestExtensionRecord(value, path) {
169
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
170
+ throw new DistributedProtocolError('DISTRIBUTED_PROTOCOL_INVALID', path);
171
+ }
172
+ return value;
173
+ }
174
+ function parseNextPayload(value) {
175
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
176
+ throw new DistributedProtocolError('DISTRIBUTED_PROTOCOL_INVALID', 'websocket.next.payload');
177
+ }
178
+ const payload = value;
179
+ if (payload.errors !== undefined && !Array.isArray(payload.errors)) {
180
+ throw new DistributedProtocolError('DISTRIBUTED_PROTOCOL_INVALID', 'websocket.next.payload.errors');
181
+ }
182
+ const extensions = parseGraphqlResponseExtensions(payload.extensions);
183
+ return {
184
+ ...payload,
185
+ ...(extensions === undefined ? {} : { extensions })
186
+ };
187
+ }
package/package.json ADDED
@@ -0,0 +1,95 @@
1
+ {
2
+ "name": "@hops-ops/distributed",
3
+ "version": "0.0.0",
4
+ "description": "Typed GraphQL client, causal replica, command runtime, and framework adapters for Distributed services",
5
+ "type": "module",
6
+ "license": "UNLICENSED",
7
+ "sideEffects": false,
8
+ "files": [
9
+ "dist",
10
+ "README.md"
11
+ ],
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js"
16
+ },
17
+ "./replica": {
18
+ "types": "./dist/replica/index.d.ts",
19
+ "import": "./dist/replica/index.js"
20
+ },
21
+ "./diagnostics": {
22
+ "types": "./dist/diagnostics.d.ts",
23
+ "import": "./dist/diagnostics.js"
24
+ },
25
+ "./sveltekit": {
26
+ "types": "./dist/sveltekit/index.d.ts",
27
+ "import": "./dist/sveltekit/index.js"
28
+ },
29
+ "./sveltekit/vite": {
30
+ "types": "./dist/sveltekit/vite.d.ts",
31
+ "import": "./dist/sveltekit/vite.js"
32
+ },
33
+ "./react": {
34
+ "types": "./dist/react/index.d.ts",
35
+ "import": "./dist/react/index.js"
36
+ },
37
+ "./package.json": "./package.json"
38
+ },
39
+ "scripts": {
40
+ "clean": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\"",
41
+ "build": "npm run clean && tsc -p tsconfig.json",
42
+ "check": "tsc -p tsconfig.json --noEmit && npm run check:generated",
43
+ "check:generated": "tsc -p tsconfig.generated-tests.json --noEmit",
44
+ "test": "npm run build && node --test tests/*.test.mjs",
45
+ "measure:cache-engines": "node scripts/measure-cache-engines.mjs",
46
+ "pack:smoke": "node scripts/pack-smoke.mjs",
47
+ "publint": "publint",
48
+ "release:dry-run": "npm publish --dry-run",
49
+ "quality": "npm run check && npm test && npm run pack:smoke && npm run publint",
50
+ "prepack": "npm run build"
51
+ },
52
+ "dependencies": {
53
+ "@graphql-typed-document-node/core": "^3.2.0",
54
+ "graphql": "^16.14.0"
55
+ },
56
+ "peerDependencies": {
57
+ "react": "^18.2.0 || ^19.0.0",
58
+ "svelte": "^5.0.0"
59
+ },
60
+ "peerDependenciesMeta": {
61
+ "react": {
62
+ "optional": true
63
+ },
64
+ "svelte": {
65
+ "optional": true
66
+ }
67
+ },
68
+ "devDependencies": {
69
+ "@apollo/client": "^4.2.7",
70
+ "@types/node": "^20.0.0",
71
+ "@types/react": "19.2.17",
72
+ "esbuild": "^0.25.12",
73
+ "publint": "^0.3.22",
74
+ "react": "19.2.8",
75
+ "react-dom": "19.2.8",
76
+ "react-test-renderer": "19.2.8",
77
+ "svelte": "5.56.7",
78
+ "typescript": "5.8.3"
79
+ },
80
+ "engines": {
81
+ "node": ">=20.0.0"
82
+ },
83
+ "publishConfig": {
84
+ "access": "public"
85
+ },
86
+ "repository": {
87
+ "type": "git",
88
+ "url": "git+https://github.com/hops-ops/distributed.git",
89
+ "directory": "js"
90
+ },
91
+ "homepage": "https://github.com/hops-ops/distributed/tree/main/js#readme",
92
+ "bugs": {
93
+ "url": "https://github.com/hops-ops/distributed/issues"
94
+ }
95
+ }