snapreq 0.0.4 → 0.0.5
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/{types → build}/capabilities.d.ts +1 -0
- package/build/capabilities.d.ts.map +1 -0
- package/{src → build}/capabilities.js +11 -12
- package/{types → build}/errors.d.ts +1 -0
- package/build/errors.d.ts.map +1 -0
- package/build/errors.js +82 -0
- package/{types → build}/headers.d.ts +1 -0
- package/build/headers.d.ts.map +1 -0
- package/build/headers.js +89 -0
- package/{types → build}/request.d.ts +1 -0
- package/build/request.d.ts.map +1 -0
- package/build/request.js +89 -0
- package/{types → build}/response.d.ts +1 -0
- package/build/response.d.ts.map +1 -0
- package/build/response.js +171 -0
- package/{types → build}/retry.d.ts +1 -0
- package/build/retry.d.ts.map +1 -0
- package/build/retry.js +95 -0
- package/{types → build}/snap-req.d.ts +1 -0
- package/build/snap-req.d.ts.map +1 -0
- package/build/snap-req.js +349 -0
- package/{types → build}/transports/fetch-transport.d.ts +1 -0
- package/build/transports/fetch-transport.d.ts.map +1 -0
- package/build/transports/fetch-transport.js +116 -0
- package/build/transports/fetch.d.ts +2 -0
- package/build/transports/fetch.d.ts.map +1 -0
- package/build/transports/fetch.js +3 -0
- package/{types → build}/transports/node-transport.d.ts +1 -0
- package/build/transports/node-transport.d.ts.map +1 -0
- package/build/transports/node-transport.js +287 -0
- package/build/transports/proxy-bounce-transport.d.ts +27 -0
- package/build/transports/proxy-bounce-transport.d.ts.map +1 -0
- package/build/transports/proxy-bounce-transport.js +116 -0
- package/{types → build}/transports/select.d.ts +1 -0
- package/build/transports/select.d.ts.map +1 -0
- package/build/transports/select.js +69 -0
- package/{types → build}/transports/xhr-transport.d.ts +1 -0
- package/build/transports/xhr-transport.d.ts.map +1 -0
- package/build/transports/xhr-transport.js +93 -0
- package/build/transports/xhr.d.ts +2 -0
- package/build/transports/xhr.d.ts.map +1 -0
- package/build/transports/xhr.js +3 -0
- package/{types → build}/websocket/websocket-channel.d.ts +1 -0
- package/build/websocket/websocket-channel.d.ts.map +1 -0
- package/build/websocket/websocket-channel.js +162 -0
- package/{types → build}/websocket/websocket-client.d.ts +1 -0
- package/build/websocket/websocket-client.d.ts.map +1 -0
- package/build/websocket/websocket-client.js +920 -0
- package/{types → build}/websocket/websocket-connection.d.ts +1 -0
- package/build/websocket/websocket-connection.d.ts.map +1 -0
- package/build/websocket/websocket-connection.js +148 -0
- package/build/websocket.d.ts +3 -0
- package/build/websocket.d.ts.map +1 -0
- package/build/websocket.js +4 -0
- package/package.json +12 -48
- package/src/errors.js +0 -86
- package/src/headers.js +0 -92
- package/src/request.js +0 -104
- package/src/response.js +0 -197
- package/src/retry.js +0 -107
- package/src/snap-req.js +0 -390
- package/src/transports/fetch-transport.js +0 -128
- package/src/transports/node-transport.js +0 -323
- package/src/transports/select.js +0 -73
- package/src/transports/xhr-transport.js +0 -112
- package/src/websocket/websocket-channel.js +0 -176
- package/src/websocket/websocket-client.js +0 -1034
- package/src/websocket/websocket-connection.js +0 -154
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Client-side handle for a channel subscription opened via
|
|
5
|
-
* `SnapReqWebSocketClient.subscribeChannel()`. Mirrors the server's
|
|
6
|
-
* subscription lifecycle — `subscribed` (resolves `ready`) / `onMessage` /
|
|
7
|
-
* `onClose`.
|
|
8
|
-
*/
|
|
9
|
-
export default class SnapReqWebSocketChannel {
|
|
10
|
-
/**
|
|
11
|
-
* @param {object} args - Channel arguments.
|
|
12
|
-
* @param {import("./websocket-client.js").default} args.client - Owning client.
|
|
13
|
-
* @param {string} args.subscriptionId - Generated id unique within the session.
|
|
14
|
-
* @param {string} args.channelType - Name the server registered the channel under.
|
|
15
|
-
* @param {Record<string, any>} [args.params] - Opaque params forwarded to the server.
|
|
16
|
-
* @param {string} [args.lastEventId] - Resume replay from this event id.
|
|
17
|
-
* @param {(body: any) => void} [args.onMessage] - Fired on each `channel-message` from the server.
|
|
18
|
-
* @param {() => void} [args.onDisconnect] - Fired when the socket drops.
|
|
19
|
-
* @param {() => void} [args.onResume] - Fired when the session resumes after a drop.
|
|
20
|
-
* @param {(reason: string) => void} [args.onClose] - Fired exactly once when the subscription closes permanently.
|
|
21
|
-
*/
|
|
22
|
-
constructor({client, subscriptionId, channelType, params, lastEventId, onMessage, onDisconnect, onResume, onClose}) {
|
|
23
|
-
this.client = client
|
|
24
|
-
this.subscriptionId = subscriptionId
|
|
25
|
-
this.channelType = channelType
|
|
26
|
-
this.params = params || {}
|
|
27
|
-
this.lastEventId = lastEventId
|
|
28
|
-
this._onMessage = onMessage
|
|
29
|
-
this._onDisconnect = onDisconnect
|
|
30
|
-
this._onResume = onResume
|
|
31
|
-
this._onClose = onClose
|
|
32
|
-
this._ready = false
|
|
33
|
-
this._resumeReadyOnResume = false
|
|
34
|
-
this._subscribed = false
|
|
35
|
-
this._subscribeSent = false
|
|
36
|
-
this._closed = false
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** @returns {Promise<void>} - Resolves once the subscription is acknowledged. */
|
|
40
|
-
_ensureReadyPromise() {
|
|
41
|
-
if (!this._readyPromise || !this._resolveReady || !this._rejectReady) {
|
|
42
|
-
/** @type {Promise<void>} */
|
|
43
|
-
this._readyPromise = new Promise((resolve, reject) => {
|
|
44
|
-
this._resolveReady = resolve
|
|
45
|
-
this._rejectReady = reject
|
|
46
|
-
})
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return this._readyPromise
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** @returns {Promise<void>} - Resolves once the subscription is acknowledged. */
|
|
53
|
-
get ready() {
|
|
54
|
-
return this._ensureReadyPromise()
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** @returns {void} */
|
|
58
|
-
_resolveReadyState() {
|
|
59
|
-
this._ready = true
|
|
60
|
-
this._resolveReady?.()
|
|
61
|
-
this._resolveReady = null
|
|
62
|
-
this._rejectReady = null
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** @returns {void} */
|
|
66
|
-
_markNotReady() {
|
|
67
|
-
this._ready = false
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/** @returns {void} */
|
|
71
|
-
_handleSubscribed() {
|
|
72
|
-
if (this._closed || this._subscribed) return
|
|
73
|
-
this._subscribed = true
|
|
74
|
-
this._resolveReadyState()
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** @returns {void} */
|
|
78
|
-
_markSubscribeSent() {
|
|
79
|
-
this._subscribeSent = true
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/** @returns {boolean} - Whether the subscription still needs to be sent. */
|
|
83
|
-
_needsSubscribe() {
|
|
84
|
-
return !this._closed && !this._subscribeSent
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* @param {any} body - Message payload.
|
|
89
|
-
* @returns {void}
|
|
90
|
-
*/
|
|
91
|
-
_handleMessage(body) {
|
|
92
|
-
if (this._closed) return
|
|
93
|
-
this._onMessage?.(body)
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/** @returns {void} */
|
|
97
|
-
_handleDisconnected() {
|
|
98
|
-
if (this._closed) return
|
|
99
|
-
this._resumeReadyOnResume ||= this._subscribed
|
|
100
|
-
this._subscribed = false
|
|
101
|
-
this._markNotReady()
|
|
102
|
-
this._onDisconnect?.()
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/** @returns {void} */
|
|
106
|
-
_handleResumed() {
|
|
107
|
-
if (this._closed) return
|
|
108
|
-
if (this._resumeReadyOnResume) {
|
|
109
|
-
this._subscribed = true
|
|
110
|
-
this._resolveReadyState()
|
|
111
|
-
}
|
|
112
|
-
this._resumeReadyOnResume = false
|
|
113
|
-
this._onResume?.()
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* @param {string} reason - Why the subscription closed.
|
|
118
|
-
* @returns {void}
|
|
119
|
-
*/
|
|
120
|
-
_handleClosed(reason) {
|
|
121
|
-
if (this._closed) return
|
|
122
|
-
this._closed = true
|
|
123
|
-
|
|
124
|
-
try {
|
|
125
|
-
this._onClose?.(reason)
|
|
126
|
-
} finally {
|
|
127
|
-
this._resumeReadyOnResume = false
|
|
128
|
-
if (!this._ready) {
|
|
129
|
-
this._rejectReady?.(new Error(`Subscription closed before acknowledgement: ${reason}`))
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
this._resolveReady = null
|
|
133
|
-
this._rejectReady = null
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* @param {{timeoutMs?: number}} [params] - Options.
|
|
139
|
-
* @returns {Promise<void>} - Resolves once ready or rejects on timeout.
|
|
140
|
-
*/
|
|
141
|
-
async waitForReady({timeoutMs = 5000} = {}) {
|
|
142
|
-
if (this._ready) return
|
|
143
|
-
|
|
144
|
-
const readyPromise = this._ensureReadyPromise()
|
|
145
|
-
const timeoutPromise = new Promise((_, reject) => {
|
|
146
|
-
setTimeout(() => reject(new Error(`Subscription not ready after ${timeoutMs}ms`)), timeoutMs)
|
|
147
|
-
})
|
|
148
|
-
|
|
149
|
-
await Promise.race([readyPromise, timeoutPromise])
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/** @returns {void} */
|
|
153
|
-
close() {
|
|
154
|
-
if (this._closed) return
|
|
155
|
-
|
|
156
|
-
try {
|
|
157
|
-
if (this.client.isOpen()) {
|
|
158
|
-
this.client._sendMessage({type: "channel-unsubscribe", subscriptionId: this.subscriptionId})
|
|
159
|
-
}
|
|
160
|
-
} catch {
|
|
161
|
-
// Socket already gone; server will clean up on session teardown.
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
this.client._removeChannelSubscription(this.subscriptionId)
|
|
165
|
-
this._handleClosed("client_unsubscribe")
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/** @returns {boolean} - Whether the subscription is closed. */
|
|
169
|
-
isClosed() { return this._closed }
|
|
170
|
-
|
|
171
|
-
/** @returns {boolean} - Whether the subscription is acknowledged and ready. */
|
|
172
|
-
isReady() { return this._ready }
|
|
173
|
-
|
|
174
|
-
/** @returns {boolean} - Whether the subscription is active. */
|
|
175
|
-
isSubscribed() { return this._subscribed && !this._closed }
|
|
176
|
-
}
|