snapreq 0.0.4 → 0.0.8

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 (68) hide show
  1. package/{types → build}/capabilities.d.ts +1 -0
  2. package/build/capabilities.d.ts.map +1 -0
  3. package/{src → build}/capabilities.js +11 -12
  4. package/{types → build}/errors.d.ts +1 -0
  5. package/build/errors.d.ts.map +1 -0
  6. package/build/errors.js +82 -0
  7. package/{types → build}/headers.d.ts +3 -0
  8. package/build/headers.d.ts.map +1 -0
  9. package/build/headers.js +93 -0
  10. package/{types → build}/request.d.ts +1 -0
  11. package/build/request.d.ts.map +1 -0
  12. package/build/request.js +89 -0
  13. package/{types → build}/response.d.ts +1 -0
  14. package/build/response.d.ts.map +1 -0
  15. package/build/response.js +171 -0
  16. package/{types → build}/retry.d.ts +1 -0
  17. package/build/retry.d.ts.map +1 -0
  18. package/build/retry.js +95 -0
  19. package/{types → build}/snap-req.d.ts +1 -0
  20. package/build/snap-req.d.ts.map +1 -0
  21. package/build/snap-req.js +349 -0
  22. package/{types → build}/transports/fetch-transport.d.ts +1 -0
  23. package/build/transports/fetch-transport.d.ts.map +1 -0
  24. package/build/transports/fetch-transport.js +116 -0
  25. package/build/transports/fetch.d.ts +2 -0
  26. package/build/transports/fetch.d.ts.map +1 -0
  27. package/build/transports/fetch.js +3 -0
  28. package/{types → build}/transports/node-transport.d.ts +1 -0
  29. package/build/transports/node-transport.d.ts.map +1 -0
  30. package/build/transports/node-transport.js +287 -0
  31. package/build/transports/proxy-bounce-transport.d.ts +27 -0
  32. package/build/transports/proxy-bounce-transport.d.ts.map +1 -0
  33. package/build/transports/proxy-bounce-transport.js +127 -0
  34. package/{types → build}/transports/select.d.ts +11 -0
  35. package/build/transports/select.d.ts.map +1 -0
  36. package/build/transports/select.js +77 -0
  37. package/{types → build}/transports/xhr-transport.d.ts +1 -0
  38. package/build/transports/xhr-transport.d.ts.map +1 -0
  39. package/build/transports/xhr-transport.js +93 -0
  40. package/build/transports/xhr.d.ts +2 -0
  41. package/build/transports/xhr.d.ts.map +1 -0
  42. package/build/transports/xhr.js +3 -0
  43. package/{types → build}/websocket/websocket-channel.d.ts +1 -0
  44. package/build/websocket/websocket-channel.d.ts.map +1 -0
  45. package/build/websocket/websocket-channel.js +171 -0
  46. package/{types → build}/websocket/websocket-client.d.ts +1 -0
  47. package/build/websocket/websocket-client.d.ts.map +1 -0
  48. package/build/websocket/websocket-client.js +920 -0
  49. package/{types → build}/websocket/websocket-connection.d.ts +1 -0
  50. package/build/websocket/websocket-connection.d.ts.map +1 -0
  51. package/build/websocket/websocket-connection.js +148 -0
  52. package/build/websocket.d.ts +3 -0
  53. package/build/websocket.d.ts.map +1 -0
  54. package/build/websocket.js +4 -0
  55. package/package.json +15 -50
  56. package/src/errors.js +0 -86
  57. package/src/headers.js +0 -92
  58. package/src/request.js +0 -104
  59. package/src/response.js +0 -197
  60. package/src/retry.js +0 -107
  61. package/src/snap-req.js +0 -390
  62. package/src/transports/fetch-transport.js +0 -128
  63. package/src/transports/node-transport.js +0 -323
  64. package/src/transports/select.js +0 -73
  65. package/src/transports/xhr-transport.js +0 -112
  66. package/src/websocket/websocket-channel.js +0 -176
  67. package/src/websocket/websocket-client.js +0 -1034
  68. 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
- }