skewtape 0.0.1 → 0.1.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.
Files changed (95) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +182 -0
  3. package/dist/cjs/backtest.d.ts +146 -0
  4. package/dist/cjs/backtest.js +410 -0
  5. package/dist/cjs/client.d.ts +255 -0
  6. package/dist/cjs/client.js +447 -0
  7. package/dist/cjs/download.d.ts +33 -0
  8. package/dist/cjs/download.js +160 -0
  9. package/dist/cjs/errors.d.ts +76 -0
  10. package/dist/cjs/errors.js +159 -0
  11. package/dist/cjs/exact.d.ts +36 -0
  12. package/dist/cjs/exact.js +168 -0
  13. package/dist/cjs/fees.d.ts +68 -0
  14. package/dist/cjs/fees.js +196 -0
  15. package/dist/cjs/geoblock.d.ts +48 -0
  16. package/dist/cjs/geoblock.js +78 -0
  17. package/dist/cjs/index.d.ts +31 -0
  18. package/dist/cjs/index.js +98 -0
  19. package/dist/cjs/kalshi.d.ts +261 -0
  20. package/dist/cjs/kalshi.js +416 -0
  21. package/dist/cjs/keccak.d.ts +10 -0
  22. package/dist/cjs/keccak.js +84 -0
  23. package/dist/cjs/msgpack.d.ts +15 -0
  24. package/dist/cjs/msgpack.js +112 -0
  25. package/dist/cjs/package.json +1 -0
  26. package/dist/cjs/paginated.d.ts +26 -0
  27. package/dist/cjs/paginated.js +51 -0
  28. package/dist/cjs/quote-math.d.ts +146 -0
  29. package/dist/cjs/quote-math.js +446 -0
  30. package/dist/cjs/quote.d.ts +108 -0
  31. package/dist/cjs/quote.js +307 -0
  32. package/dist/cjs/route-hyperliquid.d.ts +228 -0
  33. package/dist/cjs/route-hyperliquid.js +387 -0
  34. package/dist/cjs/route.d.ts +297 -0
  35. package/dist/cjs/route.js +516 -0
  36. package/dist/cjs/stream.d.ts +99 -0
  37. package/dist/cjs/stream.js +529 -0
  38. package/dist/cjs/version.d.ts +6 -0
  39. package/dist/cjs/version.js +9 -0
  40. package/dist/esm/backtest.d.ts +146 -0
  41. package/dist/esm/backtest.js +403 -0
  42. package/dist/esm/client.d.ts +255 -0
  43. package/dist/esm/client.js +441 -0
  44. package/dist/esm/download.d.ts +33 -0
  45. package/dist/esm/download.js +155 -0
  46. package/dist/esm/errors.d.ts +76 -0
  47. package/dist/esm/errors.js +142 -0
  48. package/dist/esm/exact.d.ts +36 -0
  49. package/dist/esm/exact.js +158 -0
  50. package/dist/esm/fees.d.ts +68 -0
  51. package/dist/esm/fees.js +185 -0
  52. package/dist/esm/geoblock.d.ts +48 -0
  53. package/dist/esm/geoblock.js +74 -0
  54. package/dist/esm/index.d.ts +31 -0
  55. package/dist/esm/index.js +30 -0
  56. package/dist/esm/kalshi.d.ts +261 -0
  57. package/dist/esm/kalshi.js +404 -0
  58. package/dist/esm/keccak.d.ts +10 -0
  59. package/dist/esm/keccak.js +80 -0
  60. package/dist/esm/msgpack.d.ts +15 -0
  61. package/dist/esm/msgpack.js +109 -0
  62. package/dist/esm/paginated.d.ts +26 -0
  63. package/dist/esm/paginated.js +47 -0
  64. package/dist/esm/quote-math.d.ts +146 -0
  65. package/dist/esm/quote-math.js +437 -0
  66. package/dist/esm/quote.d.ts +108 -0
  67. package/dist/esm/quote.js +297 -0
  68. package/dist/esm/route-hyperliquid.d.ts +228 -0
  69. package/dist/esm/route-hyperliquid.js +376 -0
  70. package/dist/esm/route.d.ts +297 -0
  71. package/dist/esm/route.js +507 -0
  72. package/dist/esm/stream.d.ts +99 -0
  73. package/dist/esm/stream.js +524 -0
  74. package/dist/esm/version.d.ts +6 -0
  75. package/dist/esm/version.js +6 -0
  76. package/package.json +48 -1
  77. package/src/backtest.ts +490 -0
  78. package/src/client.ts +595 -0
  79. package/src/download.ts +161 -0
  80. package/src/errors.ts +143 -0
  81. package/src/exact.ts +159 -0
  82. package/src/fees.ts +189 -0
  83. package/src/generated/schema.d.ts +2500 -0
  84. package/src/geoblock.ts +90 -0
  85. package/src/index.ts +146 -0
  86. package/src/kalshi.ts +563 -0
  87. package/src/keccak.ts +82 -0
  88. package/src/msgpack.ts +83 -0
  89. package/src/paginated.ts +51 -0
  90. package/src/quote-math.ts +555 -0
  91. package/src/quote.ts +324 -0
  92. package/src/route-hyperliquid.ts +457 -0
  93. package/src/route.ts +631 -0
  94. package/src/stream.ts +539 -0
  95. package/src/version.ts +6 -0
@@ -0,0 +1,529 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Stream = exports.DEDUPE_TRADES = exports.MAX_BUFFERED = exports.FEED_PATH = void 0;
4
+ exports.feedUrl = feedUrl;
5
+ /**
6
+ * sk.stream: the live feed (GET /v1/feed, a WebSocket) as an async iterable
7
+ * that reconnects by itself (ADR-0013: the SDK handles reconnects). The
8
+ * Python client's sk.stream does the same, rule for rule.
9
+ *
10
+ * for await (const msg of sk.stream({ topics: ["trades"], venues: ["kalshi"] })) {
11
+ * if (msg.op === "event" && msg.kind === "trade") ...
12
+ * }
13
+ *
14
+ * It yields the server's messages of interest: every `event` (the feed's own
15
+ * `gap` notices included), `snapshot`, `resync`, and the `error`s that leave
16
+ * the connection running (a topic a plan change took away, a subscription
17
+ * replaced). It handles the rest: it sends the key as the first message (so
18
+ * a browser, which cannot set headers, works too), subscribes, and on a
19
+ * reconnect subscribes again with resume_from set to the last seq it
20
+ * yielded, or before any event the seq the first connection's hello or a
21
+ * close reason (resume_from=N) gave, so a quiet stream misses nothing; it
22
+ * reconnects on the codes the feed says to reconnect on, at once on a
23
+ * `reconnect` notice, and after a wait that doubles from 1 s to 30 s, with
24
+ * jitter, when a connection fails; it pings every heartbeat_seconds and
25
+ * reconnects when nothing comes back for two of them; it drops a trade it
26
+ * already yielded, by (venue, tx_hash, log_index), and in a trades_batch the
27
+ * fills already yielded, and forgets a trade's identity on its
28
+ * trade_withdrawn; it leaves a topic a plan change took away out of the next
29
+ * subscription; it throws on what reconnecting cannot fix (a bad key, a plan
30
+ * without the feed or the topic, the connection limit on a first connection
31
+ * or for two minutes on a reconnect, a refused subscription, a message the
32
+ * server could not read). It holds at most MAX_BUFFERED messages the loop
33
+ * has not taken: past that it reconnects from the last seq yielded, as the
34
+ * server's slow-reader close would. The key is a private field: inspecting
35
+ * or logging the stream does not show it.
36
+ *
37
+ * It uses the platform's WebSocket (browsers, Node 22 and later, Deno, Bun);
38
+ * on Node 20 pass one, for example the ws package's: sk.stream({ ..., WebSocket }).
39
+ */
40
+ const errors_js_1 = require("./errors.js");
41
+ exports.FEED_PATH = "/v1/feed";
42
+ /** Close codes reconnecting cannot fix; every other end reconnects. */
43
+ const FATAL_CLOSES = {
44
+ 4001: errors_js_1.AuthenticationError,
45
+ 4003: errors_js_1.ForbiddenPlanError,
46
+ 4008: errors_js_1.ForbiddenPlanError,
47
+ 4029: errors_js_1.AuthenticationError,
48
+ 4400: errors_js_1.InvalidRequestError,
49
+ 1009: errors_js_1.InvalidRequestError,
50
+ };
51
+ /** Error codes that end the stream: the subscription cannot run as it is. */
52
+ const FATAL_ERRORS = {
53
+ unauthorized: errors_js_1.AuthenticationError,
54
+ lookup_rate_limited: errors_js_1.AuthenticationError,
55
+ plan_no_feed: errors_js_1.ForbiddenPlanError,
56
+ connection_limit: errors_js_1.ForbiddenPlanError,
57
+ venue_blocked: errors_js_1.BlockedError,
58
+ invalid_subscription: errors_js_1.InvalidRequestError,
59
+ unknown_op: errors_js_1.InvalidRequestError,
60
+ bad_message: errors_js_1.InvalidRequestError,
61
+ };
62
+ /**
63
+ * Error codes that end the stream when they answer a connection's first
64
+ * subscribe: nothing runs on it. topic_not_in_plan later is a topic a plan
65
+ * change took away, and the others go on.
66
+ */
67
+ const FIRST_SUBSCRIBE_FATAL = {
68
+ topic_not_in_plan: errors_js_1.ForbiddenPlanError,
69
+ unknown_plan: errors_js_1.ForbiddenPlanError,
70
+ unknown_topic: errors_js_1.InvalidRequestError,
71
+ };
72
+ const RECONNECT_ERRORS = new Set(["slow_consumer", "connection_expired", "internal"]);
73
+ const BACKOFF_FIRST = 1;
74
+ const BACKOFF_MAX = 30;
75
+ /** How long a reconnect refused for the connection limit is tried again (s): a moved connection can count for 90 s. */
76
+ const LIMIT_RETRY = 120;
77
+ /** The heartbeat when hello gives none (s). */
78
+ const HEARTBEAT_DEFAULT = 30;
79
+ /** Messages received and not yet taken by the loop, at most. */
80
+ exports.MAX_BUFFERED = 10_000;
81
+ /** How many trade identities are remembered. */
82
+ exports.DEDUPE_TRADES = 100_000;
83
+ /** The feed's WebSocket URL for an API base URL: https to wss, http to ws. */
84
+ function feedUrl(baseUrl) {
85
+ const base = baseUrl.replace(/\/+$/, "");
86
+ if (base.startsWith("https://"))
87
+ return "wss://" + base.slice("https://".length) + exports.FEED_PATH;
88
+ if (base.startsWith("http://"))
89
+ return "ws://" + base.slice("http://".length) + exports.FEED_PATH;
90
+ throw new errors_js_1.ConfigurationError(`baseUrl ${JSON.stringify(baseUrl)} is not http or https`);
91
+ }
92
+ class Reconnect {
93
+ atOnce;
94
+ cause;
95
+ constructor(atOnce, cause) {
96
+ this.atOnce = atOnce;
97
+ this.cause = cause;
98
+ }
99
+ }
100
+ /** One subscription to the live feed, across as many connections as it takes. */
101
+ class Stream {
102
+ url;
103
+ sleep;
104
+ jitter;
105
+ now;
106
+ /** The seq of the last event yielded, or the resumeFrom given. */
107
+ lastSeq;
108
+ hello;
109
+ subscribed;
110
+ #apiKey;
111
+ subscription;
112
+ seen = new Map();
113
+ socket;
114
+ closed = false;
115
+ wake;
116
+ /** Where to resume when no event was yielded since: the first hello's seq, raised by a close reason's or a resync's. */
117
+ floor;
118
+ failures = 0;
119
+ /** A connection of this stream was subscribed once: a connection limit after it may be its own moved connection. */
120
+ worked = false;
121
+ limitedSince;
122
+ closedSignal;
123
+ /** Ends the timer of a wait in progress (pause), at once. */
124
+ endPause;
125
+ signalClosed = () => undefined;
126
+ constructor(url, apiKey, options,
127
+ /** A sleep for tests; none is a timer close() clears. */
128
+ sleep, jitter = Math.random, now = Date.now) {
129
+ this.url = url;
130
+ this.sleep = sleep;
131
+ this.jitter = jitter;
132
+ this.now = now;
133
+ this.#apiKey = apiKey;
134
+ this.closedSignal = new Promise((r) => (this.signalClosed = r));
135
+ if (!options.topics?.length)
136
+ throw new errors_js_1.ConfigurationError("sk.stream needs at least one topic");
137
+ if (options.maxReconnects !== undefined && options.maxReconnects < 0)
138
+ throw new errors_js_1.ConfigurationError("maxReconnects must not be negative");
139
+ const WS = options.WebSocket ?? globalThis.WebSocket;
140
+ if (!WS)
141
+ throw new errors_js_1.ConfigurationError("this platform has no WebSocket: pass one, sk.stream({ ..., WebSocket })");
142
+ this.WS = WS;
143
+ this.maxReconnects = options.maxReconnects;
144
+ this.subscription = { op: "subscribe", topics: [...options.topics] };
145
+ if (options.venues?.length)
146
+ this.subscription.venues = [...options.venues];
147
+ if (options.markets?.length)
148
+ this.subscription.markets = options.markets.map((m) => ({ ...m }));
149
+ if (options.events?.length)
150
+ this.subscription.events = [...options.events];
151
+ this.lastSeq = options.resumeFrom;
152
+ }
153
+ WS;
154
+ maxReconnects;
155
+ /** Ends the stream: the connection closes, a wait ends, and iteration stops. */
156
+ close() {
157
+ this.closed = true;
158
+ this.socket?.close(1000, "closed by the client");
159
+ this.wake?.();
160
+ this.endPause?.();
161
+ this.signalClosed();
162
+ }
163
+ async *[Symbol.asyncIterator]() {
164
+ let reason = "closed by the client";
165
+ try {
166
+ while (!this.closed) {
167
+ const items = [];
168
+ const socket = new this.WS(this.url);
169
+ this.socket = socket;
170
+ let lastHeard = Date.now(); // the heartbeat runs on real timers, so on the real clock
171
+ let beat;
172
+ socket.onopen = () => this.push(items, { kind: "open" });
173
+ socket.onmessage = (ev) => {
174
+ lastHeard = Date.now();
175
+ if (items.length >= exports.MAX_BUFFERED) {
176
+ // The loop fell behind: stop reading, hand it what is held, then
177
+ // reconnect from the last seq yielded.
178
+ socket.onmessage = null;
179
+ this.push(items, { kind: "overflow" });
180
+ return;
181
+ }
182
+ const text = typeof ev.data === "string" ? ev.data : String(ev.data);
183
+ const msg = parse(text);
184
+ if (msg?.op === "hello" && beat === undefined) {
185
+ const every = typeof msg.heartbeat_seconds === "number" && msg.heartbeat_seconds > 0 ? msg.heartbeat_seconds : HEARTBEAT_DEFAULT;
186
+ // A ping every heartbeat; a socket silent for two is dead, even
187
+ // with no close event (a sleep, a NAT timeout).
188
+ beat = setInterval(() => {
189
+ if (Date.now() - lastHeard > 2 * every * 1000) {
190
+ this.push(items, { kind: "stale" });
191
+ return;
192
+ }
193
+ try {
194
+ socket.send(JSON.stringify({ op: "ping" }));
195
+ }
196
+ catch {
197
+ // closing: its close event follows
198
+ }
199
+ }, every * 1000);
200
+ }
201
+ this.push(items, msg ? { kind: "message", msg } : { kind: "unreadable", text });
202
+ };
203
+ socket.onclose = (ev) => this.push(items, { kind: "close", code: ev.code, reason: String(ev.reason) });
204
+ socket.onerror = () => undefined; // the close that follows says what happened
205
+ try {
206
+ for await (const msg of this.session(socket, items)) {
207
+ if (this.closed)
208
+ return;
209
+ // Data ends a run of failures; a refusal told to the caller (replay_limited) does not.
210
+ if (msg.op !== "error")
211
+ this.failures = 0;
212
+ yield msg;
213
+ }
214
+ return; // closed by the client
215
+ }
216
+ catch (err) {
217
+ if (!(err instanceof Reconnect)) {
218
+ reason = "the stream ended";
219
+ throw err;
220
+ }
221
+ if (!err.atOnce)
222
+ await this.wait(err.cause);
223
+ }
224
+ finally {
225
+ if (beat !== undefined)
226
+ clearInterval(beat);
227
+ socket.onopen = socket.onmessage = socket.onclose = null;
228
+ try {
229
+ socket.close(1000, this.closed ? "closed by the client" : reason === "the stream ended" ? reason : "reconnecting");
230
+ }
231
+ catch {
232
+ // already closed
233
+ }
234
+ this.socket = undefined;
235
+ }
236
+ }
237
+ }
238
+ finally {
239
+ this.closed = true;
240
+ this.signalClosed();
241
+ }
242
+ }
243
+ /**
244
+ * Waits seconds, or less when close() runs. With no sleep given, the wait
245
+ * is a timer close() clears, so a closed stream keeps no timer pending
246
+ * and the process can exit.
247
+ */
248
+ async pause(seconds) {
249
+ if (this.sleep !== undefined) {
250
+ await Promise.race([this.sleep(seconds), this.closedSignal]);
251
+ return;
252
+ }
253
+ if (this.closed)
254
+ return;
255
+ await new Promise((resolve) => {
256
+ const timer = setTimeout(done, seconds * 1000);
257
+ function done() {
258
+ clearTimeout(timer);
259
+ resolve();
260
+ }
261
+ this.endPause = done;
262
+ });
263
+ this.endPause = undefined;
264
+ }
265
+ push(items, item) {
266
+ items.push(item);
267
+ this.wake?.();
268
+ }
269
+ async next(items) {
270
+ while (items.length === 0) {
271
+ if (this.closed)
272
+ return undefined;
273
+ await new Promise((resolve) => (this.wake = resolve));
274
+ this.wake = undefined;
275
+ }
276
+ return items.shift();
277
+ }
278
+ async wait(cause) {
279
+ this.failures += 1;
280
+ const failures = this.failures;
281
+ if (this.maxReconnects !== undefined && failures > this.maxReconnects) {
282
+ throw new errors_js_1.SkewtapeError(`the live feed did not answer after ${failures} tries: ${String(cause)}`);
283
+ }
284
+ const delay = Math.min(BACKOFF_FIRST * 2 ** (failures - 1), BACKOFF_MAX);
285
+ await this.pause(delay * (0.5 + 0.5 * this.jitter()));
286
+ }
287
+ /**
288
+ * Where the stream would resume: the last seq yielded, or its position
289
+ * before its first event or past a resync, the later of the two. Keep
290
+ * this one, not lastSeq, to go on later with sk.stream({ ..., resumeFrom }).
291
+ */
292
+ get resumeFrom() {
293
+ if (this.lastSeq === undefined)
294
+ return this.floor;
295
+ if (this.floor === undefined)
296
+ return this.lastSeq;
297
+ return Math.max(this.lastSeq, this.floor);
298
+ }
299
+ raiseFloor(seq) {
300
+ if (this.floor === undefined || seq > this.floor)
301
+ this.floor = seq;
302
+ }
303
+ /** Whether a connection limit is tried again: on a reconnect, for LIMIT_RETRY after the first refusal. */
304
+ limitRetry() {
305
+ if (!this.worked)
306
+ return false;
307
+ const now = this.now();
308
+ if (this.limitedSince === undefined)
309
+ this.limitedSince = now;
310
+ return now - this.limitedSince < LIMIT_RETRY * 1000;
311
+ }
312
+ sendSubscribe(socket) {
313
+ const msg = { ...this.subscription };
314
+ const resume = this.resumeFrom;
315
+ if (resume !== undefined)
316
+ msg.resume_from = resume;
317
+ socket.send(JSON.stringify(msg));
318
+ }
319
+ async *session(socket, items) {
320
+ let subscribed = false;
321
+ let sent = false;
322
+ for (;;) {
323
+ const item = await this.next(items);
324
+ if (item === undefined || this.closed)
325
+ return; // closed by the client: lastSeq stays the last event yielded
326
+ if (item.kind === "open") {
327
+ socket.send(JSON.stringify({ op: "auth", key: this.#apiKey }));
328
+ continue;
329
+ }
330
+ if (item.kind === "stale")
331
+ throw new Reconnect(false, "no message for two heartbeats");
332
+ if (item.kind === "overflow")
333
+ throw new Reconnect(false, `more than ${exports.MAX_BUFFERED} messages not yet read`);
334
+ if (item.kind === "unreadable") {
335
+ throw new errors_js_1.SkewtapeError(`the live feed sent a message this client cannot read: ${item.text.slice(0, 200)}`);
336
+ }
337
+ if (item.kind === "close") {
338
+ if (this.closed)
339
+ return;
340
+ // A close reason's seq is this connection's position: trusted only
341
+ // once its subscription ran (a refused or unread subscribe leaves it
342
+ // at the hello's head, past events never delivered).
343
+ const resume = /resume_from=(\d+)/.exec(item.reason);
344
+ if (resume && subscribed)
345
+ this.raiseFloor(Number(resume[1]));
346
+ if (item.code === 4008 && this.limitRetry())
347
+ throw new Reconnect(false, `closed with 4008 ${item.reason}`);
348
+ const cls = FATAL_CLOSES[item.code];
349
+ if (cls)
350
+ throw new cls(`the live feed closed the connection (${item.code}): ${item.reason}`, { code: String(item.code) });
351
+ throw new Reconnect(false, `closed with ${item.code} ${item.reason}`);
352
+ }
353
+ const msg = item.msg;
354
+ switch (msg.op) {
355
+ case "hello":
356
+ this.hello = msg;
357
+ if (!sent) {
358
+ this.sendSubscribe(socket);
359
+ sent = true;
360
+ }
361
+ // The first connection subscribes from now; a later one, before any event, from this seq.
362
+ if (this.lastSeq === undefined && this.floor === undefined && typeof msg.seq === "number")
363
+ this.floor = msg.seq;
364
+ break;
365
+ case "subscribed":
366
+ subscribed = true;
367
+ this.subscribed = msg;
368
+ // A connection that works ends a run of failures.
369
+ this.failures = 0;
370
+ this.worked = true;
371
+ this.limitedSince = undefined;
372
+ break;
373
+ case "event": {
374
+ const out = this.event(msg);
375
+ if (out)
376
+ yield out;
377
+ break;
378
+ }
379
+ case "snapshot":
380
+ yield msg;
381
+ break;
382
+ case "resync":
383
+ // What came before head cannot be replayed: resume past it.
384
+ if (typeof msg.head === "number")
385
+ this.raiseFloor(msg.head);
386
+ yield msg;
387
+ break;
388
+ case "reconnect": {
389
+ // The notice's seq is this connection's position: trusted only once its subscription ran.
390
+ const resume = msg.resume_from;
391
+ if (subscribed && typeof resume === "number")
392
+ this.raiseFloor(resume);
393
+ throw new Reconnect(true);
394
+ }
395
+ case "error": {
396
+ const code = typeof msg.code === "string" ? msg.code : "";
397
+ const text = typeof msg.message === "string" ? msg.message : code;
398
+ if (code === "connection_limit" && this.limitRetry())
399
+ throw new Reconnect(false, text);
400
+ const firstFatal = subscribed ? undefined : FIRST_SUBSCRIBE_FATAL[code];
401
+ if (FATAL_ERRORS[code] || firstFatal) {
402
+ const cls = FATAL_ERRORS[code] ?? firstFatal ?? errors_js_1.ForbiddenPlanError;
403
+ throw new cls(text, { code, docsUrl: str(msg.docs_url), upgradeUrl: str(msg.upgrade_url) });
404
+ }
405
+ if (code === "replay_limited") {
406
+ // Told before the wait, which can last most of an hour: the
407
+ // message names it. Refusals in a row count toward maxReconnects.
408
+ yield msg;
409
+ this.failures += 1;
410
+ if (this.maxReconnects !== undefined && this.failures > this.maxReconnects) {
411
+ throw new errors_js_1.SkewtapeError(`the live feed refused ${this.failures} replays in a row: ${text}`, { code });
412
+ }
413
+ const wait = /in (\d+) seconds/.exec(text);
414
+ await this.pause(wait ? Number(wait[1]) : BACKOFF_FIRST);
415
+ if (this.closed)
416
+ return;
417
+ this.sendSubscribe(socket);
418
+ break;
419
+ }
420
+ if (code === "not_ready") {
421
+ // No wait is named: the failure backoff, with jitter, so a new
422
+ // instance's clients do not come back in lockstep.
423
+ await this.wait(text);
424
+ if (this.closed)
425
+ return;
426
+ this.sendSubscribe(socket);
427
+ break;
428
+ }
429
+ if (RECONNECT_ERRORS.has(code))
430
+ throw new Reconnect(false, text);
431
+ if (code === "topic_not_in_plan") {
432
+ // The server dropped the topic and kept the others: the next connection subscribes without it.
433
+ const lost = /^topic (\w+) needs/.exec(text)?.[1];
434
+ const topics = this.subscription.topics;
435
+ if (lost !== undefined && topics.includes(lost) && topics.length > 1)
436
+ this.subscription.topics = topics.filter((t) => t !== lost);
437
+ }
438
+ // superseded, a topic a plan change took away, and codes this
439
+ // version does not know: the connection goes on, and the caller is told.
440
+ yield msg;
441
+ break;
442
+ }
443
+ default:
444
+ break; // pong, and messages this version does not know
445
+ }
446
+ }
447
+ }
448
+ event(msg) {
449
+ const seq = msg.seq;
450
+ if (typeof seq === "number") {
451
+ if (this.lastSeq !== undefined && seq <= this.lastSeq)
452
+ return undefined;
453
+ this.lastSeq = seq;
454
+ }
455
+ const payload = msg.payload;
456
+ if (payload === null || typeof payload !== "object")
457
+ return msg;
458
+ const row = payload;
459
+ const venue = str(msg.venue) ?? str(row.venue) ?? "";
460
+ switch (msg.kind) {
461
+ case "trade": {
462
+ const key = identity(venue, row);
463
+ if (key === undefined)
464
+ return msg;
465
+ if (this.seen.has(key))
466
+ return undefined;
467
+ this.remember(key);
468
+ return msg;
469
+ }
470
+ case "trades_batch": {
471
+ const fills = row.fills;
472
+ if (!Array.isArray(fills))
473
+ return msg;
474
+ const fresh = fills.filter((f) => {
475
+ const key = f !== null && typeof f === "object" ? identity(venue, f) : undefined;
476
+ if (key === undefined)
477
+ return true;
478
+ if (this.seen.has(key))
479
+ return false;
480
+ this.remember(key);
481
+ return true;
482
+ });
483
+ if (fresh.length === 0)
484
+ return undefined;
485
+ if (fresh.length === fills.length)
486
+ return msg;
487
+ return { ...msg, payload: { ...row, fills: fresh, count: fresh.length } };
488
+ }
489
+ case "trade_withdrawn": {
490
+ const key = identity(venue, row);
491
+ if (key !== undefined)
492
+ this.seen.delete(key);
493
+ return msg;
494
+ }
495
+ default:
496
+ return msg;
497
+ }
498
+ }
499
+ remember(key) {
500
+ this.seen.set(key, true);
501
+ if (this.seen.size > exports.DEDUPE_TRADES) {
502
+ const oldest = this.seen.keys().next().value;
503
+ if (oldest !== undefined)
504
+ this.seen.delete(oldest);
505
+ }
506
+ }
507
+ }
508
+ exports.Stream = Stream;
509
+ function parse(text) {
510
+ try {
511
+ const v = JSON.parse(text);
512
+ if (v !== null && typeof v === "object" && typeof v.op === "string")
513
+ return v;
514
+ }
515
+ catch {
516
+ // unreadable: the caller says so
517
+ }
518
+ return undefined;
519
+ }
520
+ function identity(venue, row) {
521
+ const tx = row.tx_hash;
522
+ const index = row.log_index;
523
+ if (typeof tx !== "string" || typeof index !== "number")
524
+ return undefined;
525
+ return `${str(row.venue) ?? venue}|${tx.toLowerCase()}|${index}`;
526
+ }
527
+ function str(v) {
528
+ return typeof v === "string" ? v : undefined;
529
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * The package version: major and minor follow the API's info.version, the
3
+ * patch is this package's own (ADR-0015). release-sdks.yml sets package.json
4
+ * from the api/vX.Y.Z tag; keep this constant equal to it.
5
+ */
6
+ export declare const VERSION = "0.1.4";
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VERSION = void 0;
4
+ /**
5
+ * The package version: major and minor follow the API's info.version, the
6
+ * patch is this package's own (ADR-0015). release-sdks.yml sets package.json
7
+ * from the api/vX.Y.Z tag; keep this constant equal to it.
8
+ */
9
+ exports.VERSION = "0.1.4";