snapreq 0.0.1 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Client-side handle for a 1:1 connection opened via
3
+ * `SnapReqWebSocketClient.openConnection()`. Mirrors the server's connection
4
+ * lifecycle — `onConnect` / `onMessage` / `onClose` plus `sendMessage` /
5
+ * `close`.
6
+ */
7
+ export default class SnapReqWebSocketConnection {
8
+ /**
9
+ * @param {object} args - Connection arguments.
10
+ * @param {import("./websocket-client.js").default} args.client - Owning client.
11
+ * @param {string} args.connectionId - Generated id unique within the session.
12
+ * @param {string} args.connectionType - Name the server registered the class under.
13
+ * @param {Record<string, any>} [args.params] - Opaque params forwarded to the server.
14
+ * @param {() => void} [args.onConnect] - Fired after the server confirms `connection-opened`.
15
+ * @param {(body: any) => void} [args.onMessage] - Fired on each `connection-message` from the server.
16
+ * @param {() => void} [args.onDisconnect] - Fired when the socket drops; connection is preserved pending resume.
17
+ * @param {() => void} [args.onResume] - Fired when the session successfully resumes after a drop.
18
+ * @param {(reason: string) => void} [args.onClose] - Fired exactly once when the handle closes permanently.
19
+ */
20
+ constructor({ client, connectionId, connectionType, params, onConnect, onMessage, onDisconnect, onResume, onClose }: {
21
+ client: import("./websocket-client.js").default;
22
+ connectionId: string;
23
+ connectionType: string;
24
+ params?: Record<string, any>;
25
+ onConnect?: () => void;
26
+ onMessage?: (body: any) => void;
27
+ onDisconnect?: () => void;
28
+ onResume?: () => void;
29
+ onClose?: (reason: string) => void;
30
+ });
31
+ client: import("./websocket-client.js").default;
32
+ connectionId: string;
33
+ connectionType: string;
34
+ params: Record<string, any>;
35
+ _onConnect: () => void;
36
+ _onMessage: (body: any) => void;
37
+ _onDisconnect: () => void;
38
+ _onResume: () => void;
39
+ _onClose: (reason: string) => void;
40
+ _connected: boolean;
41
+ _closed: boolean;
42
+ /** @type {Promise<void>} - Resolves once the server sends `connection-opened`. */
43
+ ready: Promise<void>;
44
+ _resolveReady: (value: void | PromiseLike<void>) => void;
45
+ _rejectReady: (reason?: any) => void;
46
+ /**
47
+ * Called by the client dispatcher when `{type: "connection-opened"}` arrives.
48
+ * Fires the user's `onConnect` and resolves `ready`.
49
+ * @returns {void}
50
+ */
51
+ _handleOpened(): void;
52
+ /**
53
+ * Called by the client dispatcher for each `connection-message` targeted at
54
+ * this connection id.
55
+ * @param {any} body - Message payload.
56
+ * @returns {void}
57
+ */
58
+ _handleMessage(body: any): void;
59
+ /**
60
+ * Called by the client when the underlying socket drops. The connection stays
61
+ * alive pending session resume.
62
+ * @returns {void}
63
+ */
64
+ _handleDisconnected(): void;
65
+ /**
66
+ * Called by the client after `session-resumed` confirms the server still has
67
+ * this connection.
68
+ * @returns {void}
69
+ */
70
+ _handleResumed(): void;
71
+ /**
72
+ * Called by the client dispatcher when the connection closes for any reason.
73
+ * Fires `onClose(reason)` at most once.
74
+ * @param {string} reason - Why the connection closed.
75
+ * @returns {void}
76
+ */
77
+ _handleClosed(reason: string): void;
78
+ /**
79
+ * Sends a message to the server side of this connection.
80
+ * @param {any} body - Message payload.
81
+ * @returns {void}
82
+ */
83
+ sendMessage(body: any): void;
84
+ /**
85
+ * Closes the connection from the client side. Fires `onClose("client_close")`
86
+ * locally and notifies the server. No-op if already closed.
87
+ * @returns {void}
88
+ */
89
+ close(): void;
90
+ /** @returns {boolean} - Whether the connection is closed. */
91
+ isClosed(): boolean;
92
+ /** @returns {boolean} - Whether the connection is open. */
93
+ isConnected(): boolean;
94
+ }
package/src/index.js DELETED
@@ -1,22 +0,0 @@
1
- // @ts-check
2
-
3
- import SnapReq from "./snap-req.js"
4
-
5
- export default SnapReq
6
- export {default as SnapReq} from "./snap-req.js"
7
- export {default as SnapReqResponse} from "./response.js"
8
- export {default as SnapReqHeaders} from "./headers.js"
9
- export {
10
- SnapReqError,
11
- SnapReqHttpError,
12
- SnapReqUnsupportedFeatureError,
13
- SnapReqAbortError
14
- } from "./errors.js"
15
- export {defaultRetryableError} from "./retry.js"
16
- export {detectRuntime, selectTransport} from "./transports/select.js"
17
- export {default as FetchTransport} from "./transports/fetch-transport.js"
18
- export {default as XhrTransport} from "./transports/xhr-transport.js"
19
-
20
- // The WebSocket client is exported from its own module so importing the HTTP
21
- // client never pulls WebSocket code into a bundle that does not need it.
22
- export {default as SnapReqWebSocketClient} from "./websocket/websocket-client.js"