popsub 0.0.0-stage → 0.2.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.
- package/LICENSE +201 -0
- package/README.md +198 -2
- package/dist/admin.d.ts +175 -0
- package/dist/admin.js +257 -0
- package/dist/admin.js.map +1 -0
- package/dist/backoff.d.ts +32 -0
- package/dist/backoff.js +31 -0
- package/dist/backoff.js.map +1 -0
- package/dist/base64.d.ts +24 -0
- package/dist/base64.js +104 -0
- package/dist/base64.js.map +1 -0
- package/dist/client.d.ts +250 -0
- package/dist/client.js +322 -0
- package/dist/client.js.map +1 -0
- package/dist/config.d.ts +117 -0
- package/dist/config.js +116 -0
- package/dist/config.js.map +1 -0
- package/dist/dedup.d.ts +107 -0
- package/dist/dedup.js +150 -0
- package/dist/dedup.js.map +1 -0
- package/dist/engine.d.ts +224 -0
- package/dist/engine.js +1306 -0
- package/dist/engine.js.map +1 -0
- package/dist/errors.d.ts +80 -0
- package/dist/errors.js +90 -0
- package/dist/errors.js.map +1 -0
- package/dist/events.d.ts +111 -0
- package/dist/events.js +57 -0
- package/dist/events.js.map +1 -0
- package/dist/handshake.d.ts +91 -0
- package/dist/handshake.js +266 -0
- package/dist/handshake.js.map +1 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/pattern.d.ts +24 -0
- package/dist/pattern.js +43 -0
- package/dist/pattern.js.map +1 -0
- package/dist/protocol.d.ts +313 -0
- package/dist/protocol.js +173 -0
- package/dist/protocol.js.map +1 -0
- package/dist/subscription.d.ts +187 -0
- package/dist/subscription.js +268 -0
- package/dist/subscription.js.map +1 -0
- package/package.json +50 -4
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a `Client` connects: where, as whom, and how it behaves when things
|
|
3
|
+
* go wrong. `resolveConfig` fills in defaults and rejects invalid values.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* A username and password. The client logs in on every connection and
|
|
9
|
+
* refreshes its token on its own before it expires.
|
|
10
|
+
*/
|
|
11
|
+
export interface PasswordCredentials {
|
|
12
|
+
/** The username. */
|
|
13
|
+
username: string;
|
|
14
|
+
/** The password. */
|
|
15
|
+
password: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* A JWT obtained elsewhere. The client can't refresh it: when it expires
|
|
19
|
+
* the server closes the connection with `token_expired` and the client
|
|
20
|
+
* stops, after an `authExpiring` event.
|
|
21
|
+
*/
|
|
22
|
+
export interface TokenCredentials {
|
|
23
|
+
/** The token. */
|
|
24
|
+
token: string;
|
|
25
|
+
}
|
|
26
|
+
/** How the client proves who it is. */
|
|
27
|
+
export type Credentials = PasswordCredentials | TokenCredentials;
|
|
28
|
+
/**
|
|
29
|
+
* When QoS 1 messages are acknowledged.
|
|
30
|
+
*
|
|
31
|
+
* - `auto`: as the client hands them to the subscription's buffer (or, when
|
|
32
|
+
* the buffer is full, as the application takes them). A message the
|
|
33
|
+
* application was processing when it crashed isn't redelivered.
|
|
34
|
+
* - `manual`: when the application calls `Message.ack`. A message never
|
|
35
|
+
* acknowledged is redelivered after a reconnect (to a durable session),
|
|
36
|
+
* and stalls its subscription once the server runs out of retries.
|
|
37
|
+
*/
|
|
38
|
+
export type AckMode = "auto" | "manual";
|
|
39
|
+
/**
|
|
40
|
+
* How the client waits between reconnection attempts: a random wait
|
|
41
|
+
* between zero and a ceiling that doubles from `initialMs` up to `maxMs`.
|
|
42
|
+
*/
|
|
43
|
+
export interface ReconnectPolicy {
|
|
44
|
+
/** The starting ceiling, in milliseconds. */
|
|
45
|
+
initialMs: number;
|
|
46
|
+
/** The highest ceiling, in milliseconds. */
|
|
47
|
+
maxMs: number;
|
|
48
|
+
/** Give up after this many failed attempts in a row; `null` keeps trying. */
|
|
49
|
+
maxAttempts: number | null;
|
|
50
|
+
}
|
|
51
|
+
/** Everything a `Client` needs to connect. Only `url` and `credentials` are required. */
|
|
52
|
+
export interface ClientConfig {
|
|
53
|
+
/** The server's WebSocket URL, such as `ws://127.0.0.1:8080`, or `wss://` for TLS. */
|
|
54
|
+
url: string;
|
|
55
|
+
/** Who to connect as. */
|
|
56
|
+
credentials: Credentials;
|
|
57
|
+
/**
|
|
58
|
+
* A durable session to hold, 1 to 128 characters, so subscriptions and
|
|
59
|
+
* unacknowledged messages survive disconnects. Absent or `null` is an
|
|
60
|
+
* ephemeral connection.
|
|
61
|
+
*/
|
|
62
|
+
sessionId?: string | null;
|
|
63
|
+
/** Discard the session's stored subscriptions on the first connection. Never sent on reconnects. */
|
|
64
|
+
cleanStart?: boolean;
|
|
65
|
+
/** When QoS 1 messages are acknowledged; `auto` by default. */
|
|
66
|
+
ackMode?: AckMode;
|
|
67
|
+
/** How to reconnect; 250 ms doubling up to 30 seconds, forever, by default. */
|
|
68
|
+
reconnect?: Partial<ReconnectPolicy>;
|
|
69
|
+
/**
|
|
70
|
+
* How long a request (connect, subscribe, unsubscribe, confirmed publish,
|
|
71
|
+
* admin call) may take, reconnects included, in milliseconds; 30 seconds
|
|
72
|
+
* by default.
|
|
73
|
+
*/
|
|
74
|
+
requestTimeoutMs?: number;
|
|
75
|
+
/**
|
|
76
|
+
* How many messages each subscription buffers for the application before
|
|
77
|
+
* the client starts holding back; 1024 by default. See the README's
|
|
78
|
+
* section on backpressure for what happens beyond it.
|
|
79
|
+
*/
|
|
80
|
+
subscriptionBuffer?: number;
|
|
81
|
+
}
|
|
82
|
+
/** A `ClientConfig` with every default filled in. */
|
|
83
|
+
export interface ResolvedConfig {
|
|
84
|
+
/** The server's WebSocket URL. */
|
|
85
|
+
url: string;
|
|
86
|
+
/** Who to connect as. */
|
|
87
|
+
credentials: Credentials;
|
|
88
|
+
/** The durable session, or `null` for an ephemeral connection. */
|
|
89
|
+
sessionId: string | null;
|
|
90
|
+
/** Whether the first connection discards the session's stored subscriptions. */
|
|
91
|
+
cleanStart: boolean;
|
|
92
|
+
/** When QoS 1 messages are acknowledged. */
|
|
93
|
+
ackMode: AckMode;
|
|
94
|
+
/** How to reconnect. */
|
|
95
|
+
reconnect: ReconnectPolicy;
|
|
96
|
+
/** How long a request may take, in milliseconds. */
|
|
97
|
+
requestTimeoutMs: number;
|
|
98
|
+
/** How many messages each subscription buffers before holding back. */
|
|
99
|
+
subscriptionBuffer: number;
|
|
100
|
+
}
|
|
101
|
+
/** The reconnect policy used for fields the configuration leaves out. */
|
|
102
|
+
export declare const DEFAULT_RECONNECT: Readonly<ReconnectPolicy>;
|
|
103
|
+
/** The request timeout used when the configuration leaves it out. */
|
|
104
|
+
export declare const DEFAULT_REQUEST_TIMEOUT_MS = 30000;
|
|
105
|
+
/** The subscription buffer size used when the configuration leaves it out. */
|
|
106
|
+
export declare const DEFAULT_SUBSCRIPTION_BUFFER = 1024;
|
|
107
|
+
/**
|
|
108
|
+
* Fills in defaults and checks every setting.
|
|
109
|
+
*
|
|
110
|
+
* @param config - The configuration from the application.
|
|
111
|
+
* @returns The complete configuration.
|
|
112
|
+
* @throws PopSubError of kind `config` for a URL that isn't `ws://` or
|
|
113
|
+
* `wss://`, credentials that are neither a password nor a token, a
|
|
114
|
+
* session id outside 1 to 128 characters, or a timeout, delay or buffer
|
|
115
|
+
* size that isn't positive.
|
|
116
|
+
*/
|
|
117
|
+
export declare function resolveConfig(config: ClientConfig): ResolvedConfig;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a `Client` connects: where, as whom, and how it behaves when things
|
|
3
|
+
* go wrong. `resolveConfig` fills in defaults and rejects invalid values.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { PopSubError } from "./errors.js";
|
|
8
|
+
/** The reconnect policy used for fields the configuration leaves out. */
|
|
9
|
+
export const DEFAULT_RECONNECT = Object.freeze({
|
|
10
|
+
initialMs: 250,
|
|
11
|
+
maxMs: 30_000,
|
|
12
|
+
maxAttempts: null,
|
|
13
|
+
});
|
|
14
|
+
/** The request timeout used when the configuration leaves it out. */
|
|
15
|
+
export const DEFAULT_REQUEST_TIMEOUT_MS = 30_000;
|
|
16
|
+
/** The subscription buffer size used when the configuration leaves it out. */
|
|
17
|
+
export const DEFAULT_SUBSCRIPTION_BUFFER = 1024;
|
|
18
|
+
/**
|
|
19
|
+
* Builds the error for an invalid setting.
|
|
20
|
+
*
|
|
21
|
+
* @param message - What is wrong.
|
|
22
|
+
* @returns The `config` error.
|
|
23
|
+
*/
|
|
24
|
+
function invalid(message) {
|
|
25
|
+
return new PopSubError("config", `invalid configuration: ${message}`);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Checks that a setting is a positive, finite number.
|
|
29
|
+
*
|
|
30
|
+
* @param name - The setting's name, for the error.
|
|
31
|
+
* @param value - Its value.
|
|
32
|
+
* @returns The value.
|
|
33
|
+
* @throws PopSubError of kind `config` when it isn't.
|
|
34
|
+
*/
|
|
35
|
+
function positive(name, value) {
|
|
36
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
|
|
37
|
+
throw invalid(`${name} must be a positive number`);
|
|
38
|
+
}
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Checks credentials and copies them, so later changes by the caller don't
|
|
43
|
+
* reach the client.
|
|
44
|
+
*
|
|
45
|
+
* @param credentials - The credentials.
|
|
46
|
+
* @returns The copy.
|
|
47
|
+
* @throws PopSubError of kind `config` when they are neither a username and
|
|
48
|
+
* password nor a token.
|
|
49
|
+
*/
|
|
50
|
+
function checkCredentials(credentials) {
|
|
51
|
+
if (typeof credentials !== "object" || credentials === null) {
|
|
52
|
+
throw invalid("credentials are required");
|
|
53
|
+
}
|
|
54
|
+
const fields = credentials;
|
|
55
|
+
const { token, username, password } = fields;
|
|
56
|
+
if ("token" in fields) {
|
|
57
|
+
if (typeof token !== "string" || token === "") {
|
|
58
|
+
throw invalid("the token must be a non-empty string");
|
|
59
|
+
}
|
|
60
|
+
return { token };
|
|
61
|
+
}
|
|
62
|
+
if (typeof username !== "string" || typeof password !== "string") {
|
|
63
|
+
throw invalid("credentials need a username and password, or a token");
|
|
64
|
+
}
|
|
65
|
+
return { username, password };
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Fills in defaults and checks every setting.
|
|
69
|
+
*
|
|
70
|
+
* @param config - The configuration from the application.
|
|
71
|
+
* @returns The complete configuration.
|
|
72
|
+
* @throws PopSubError of kind `config` for a URL that isn't `ws://` or
|
|
73
|
+
* `wss://`, credentials that are neither a password nor a token, a
|
|
74
|
+
* session id outside 1 to 128 characters, or a timeout, delay or buffer
|
|
75
|
+
* size that isn't positive.
|
|
76
|
+
*/
|
|
77
|
+
export function resolveConfig(config) {
|
|
78
|
+
const raw = config;
|
|
79
|
+
if (typeof raw !== "object" || raw === null) {
|
|
80
|
+
throw invalid("a configuration object is required");
|
|
81
|
+
}
|
|
82
|
+
if (typeof config.url !== "string" || !/^wss?:\/\//i.test(config.url)) {
|
|
83
|
+
throw invalid("url must start with ws:// or wss://");
|
|
84
|
+
}
|
|
85
|
+
const sessionId = config.sessionId ?? null;
|
|
86
|
+
if (sessionId !== null &&
|
|
87
|
+
(typeof sessionId !== "string" || sessionId.length < 1 || sessionId.length > 128)) {
|
|
88
|
+
throw invalid("sessionId must be 1 to 128 characters");
|
|
89
|
+
}
|
|
90
|
+
const ackMode = config.ackMode ?? "auto";
|
|
91
|
+
if (ackMode !== "auto" && ackMode !== "manual") {
|
|
92
|
+
throw invalid("ackMode must be auto or manual");
|
|
93
|
+
}
|
|
94
|
+
const reconnect = { ...DEFAULT_RECONNECT, ...config.reconnect };
|
|
95
|
+
positive("reconnect.initialMs", reconnect.initialMs);
|
|
96
|
+
positive("reconnect.maxMs", reconnect.maxMs);
|
|
97
|
+
if (reconnect.maxAttempts !== null &&
|
|
98
|
+
(!Number.isInteger(reconnect.maxAttempts) || reconnect.maxAttempts < 0)) {
|
|
99
|
+
throw invalid("reconnect.maxAttempts must be a whole number of at least 0, or null");
|
|
100
|
+
}
|
|
101
|
+
const subscriptionBuffer = config.subscriptionBuffer ?? DEFAULT_SUBSCRIPTION_BUFFER;
|
|
102
|
+
if (!Number.isInteger(subscriptionBuffer) || subscriptionBuffer < 1) {
|
|
103
|
+
throw invalid("subscriptionBuffer must be a whole number of at least 1");
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
url: config.url,
|
|
107
|
+
credentials: checkCredentials(config.credentials),
|
|
108
|
+
sessionId,
|
|
109
|
+
cleanStart: config.cleanStart ?? false,
|
|
110
|
+
ackMode,
|
|
111
|
+
reconnect,
|
|
112
|
+
requestTimeoutMs: positive("requestTimeoutMs", config.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS),
|
|
113
|
+
subscriptionBuffer,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAuG1C,yEAAyE;AACzE,MAAM,CAAC,MAAM,iBAAiB,GAA8B,MAAM,CAAC,MAAM,CAAC;IACxE,SAAS,EAAE,GAAG;IACd,KAAK,EAAE,MAAM;IACb,WAAW,EAAE,IAAI;CAClB,CAAC,CAAC;AAEH,qEAAqE;AACrE,MAAM,CAAC,MAAM,0BAA0B,GAAG,MAAM,CAAC;AAEjD,8EAA8E;AAC9E,MAAM,CAAC,MAAM,2BAA2B,GAAG,IAAI,CAAC;AAEhD;;;;;GAKG;AACH,SAAS,OAAO,CAAC,OAAe;IAC9B,OAAO,IAAI,WAAW,CAAC,QAAQ,EAAE,0BAA0B,OAAO,EAAE,CAAC,CAAC;AACxE,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,QAA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;IAC3E,CAAC;IACD,OAAO;QACL,GAAG,EAAE,MAAM,CAAC,GAAG;QACf,WAAW,EAAE,gBAAgB,CAAC,MAAM,CAAC,WAAW,CAAC;QACjD,SAAS;QACT,UAAU,EAAE,MAAM,CAAC,UAAU,IAAI,KAAK;QACtC,OAAO;QACP,SAAS;QACT,gBAAgB,EAAE,QAAQ,CAAC,kBAAkB,EAAE,MAAM,CAAC,gBAAgB,IAAI,0BAA0B,CAAC;QACrG,kBAAkB;KACnB,CAAC;AACJ,CAAC","sourcesContent":["/**\n * How a `Client` connects: where, as whom, and how it behaves when things\n * go wrong. `resolveConfig` fills in defaults and rejects invalid values.\n *\n * @module\n */\n\nimport { PopSubError } from \"./errors.js\";\n\n/**\n * A username and password. The client logs in on every connection and\n * refreshes its token on its own before it expires.\n */\nexport interface PasswordCredentials {\n /** The username. */\n username: string;\n /** The password. */\n password: string;\n}\n\n/**\n * A JWT obtained elsewhere. The client can't refresh it: when it expires\n * the server closes the connection with `token_expired` and the client\n * stops, after an `authExpiring` event.\n */\nexport interface TokenCredentials {\n /** The token. */\n token: string;\n}\n\n/** How the client proves who it is. */\nexport type Credentials = PasswordCredentials | TokenCredentials;\n\n/**\n * When QoS 1 messages are acknowledged.\n *\n * - `auto`: as the client hands them to the subscription's buffer (or, when\n * the buffer is full, as the application takes them). A message the\n * application was processing when it crashed isn't redelivered.\n * - `manual`: when the application calls `Message.ack`. A message never\n * acknowledged is redelivered after a reconnect (to a durable session),\n * and stalls its subscription once the server runs out of retries.\n */\nexport type AckMode = \"auto\" | \"manual\";\n\n/**\n * How the client waits between reconnection attempts: a random wait\n * between zero and a ceiling that doubles from `initialMs` up to `maxMs`.\n */\nexport interface ReconnectPolicy {\n /** The starting ceiling, in milliseconds. */\n initialMs: number;\n /** The highest ceiling, in milliseconds. */\n maxMs: number;\n /** Give up after this many failed attempts in a row; `null` keeps trying. */\n maxAttempts: number | null;\n}\n\n/** Everything a `Client` needs to connect. Only `url` and `credentials` are required. */\nexport interface ClientConfig {\n /** The server's WebSocket URL, such as `ws://127.0.0.1:8080`, or `wss://` for TLS. */\n url: string;\n /** Who to connect as. */\n credentials: Credentials;\n /**\n * A durable session to hold, 1 to 128 characters, so subscriptions and\n * unacknowledged messages survive disconnects. Absent or `null` is an\n * ephemeral connection.\n */\n sessionId?: string | null;\n /** Discard the session's stored subscriptions on the first connection. Never sent on reconnects. */\n cleanStart?: boolean;\n /** When QoS 1 messages are acknowledged; `auto` by default. */\n ackMode?: AckMode;\n /** How to reconnect; 250 ms doubling up to 30 seconds, forever, by default. */\n reconnect?: Partial<ReconnectPolicy>;\n /**\n * How long a request (connect, subscribe, unsubscribe, confirmed publish,\n * admin call) may take, reconnects included, in milliseconds; 30 seconds\n * by default.\n */\n requestTimeoutMs?: number;\n /**\n * How many messages each subscription buffers for the application before\n * the client starts holding back; 1024 by default. See the README's\n * section on backpressure for what happens beyond it.\n */\n subscriptionBuffer?: number;\n}\n\n/** A `ClientConfig` with every default filled in. */\nexport interface ResolvedConfig {\n /** The server's WebSocket URL. */\n url: string;\n /** Who to connect as. */\n credentials: Credentials;\n /** The durable session, or `null` for an ephemeral connection. */\n sessionId: string | null;\n /** Whether the first connection discards the session's stored subscriptions. */\n cleanStart: boolean;\n /** When QoS 1 messages are acknowledged. */\n ackMode: AckMode;\n /** How to reconnect. */\n reconnect: ReconnectPolicy;\n /** How long a request may take, in milliseconds. */\n requestTimeoutMs: number;\n /** How many messages each subscription buffers before holding back. */\n subscriptionBuffer: number;\n}\n\n/** The reconnect policy used for fields the configuration leaves out. */\nexport const DEFAULT_RECONNECT: Readonly<ReconnectPolicy> = Object.freeze({\n initialMs: 250,\n maxMs: 30_000,\n maxAttempts: null,\n});\n\n/** The request timeout used when the configuration leaves it out. */\nexport const DEFAULT_REQUEST_TIMEOUT_MS = 30_000;\n\n/** The subscription buffer size used when the configuration leaves it out. */\nexport const DEFAULT_SUBSCRIPTION_BUFFER = 1024;\n\n/**\n * Builds the error for an invalid setting.\n *\n * @param message - What is wrong.\n * @returns The `config` error.\n */\nfunction invalid(message: string): PopSubError {\n return new PopSubError(\"config\", `invalid configuration: ${message}`);\n}\n\n/**\n * Checks that a setting is a positive, finite number.\n *\n * @param name - The setting's name, for the error.\n * @param value - Its value.\n * @returns The value.\n * @throws PopSubError of kind `config` when it isn't.\n */\nfunction positive(name: string, value: number): number {\n if (typeof value !== \"number\" || !Number.isFinite(value) || value <= 0) {\n throw invalid(`${name} must be a positive number`);\n }\n return value;\n}\n\n/**\n * Checks credentials and copies them, so later changes by the caller don't\n * reach the client.\n *\n * @param credentials - The credentials.\n * @returns The copy.\n * @throws PopSubError of kind `config` when they are neither a username and\n * password nor a token.\n */\nfunction checkCredentials(credentials: unknown): Credentials {\n if (typeof credentials !== \"object\" || credentials === null) {\n throw invalid(\"credentials are required\");\n }\n const fields = credentials as Record<string, unknown>;\n const { token, username, password } = fields;\n if (\"token\" in fields) {\n if (typeof token !== \"string\" || token === \"\") {\n throw invalid(\"the token must be a non-empty string\");\n }\n return { token };\n }\n if (typeof username !== \"string\" || typeof password !== \"string\") {\n throw invalid(\"credentials need a username and password, or a token\");\n }\n return { username, password };\n}\n\n/**\n * Fills in defaults and checks every setting.\n *\n * @param config - The configuration from the application.\n * @returns The complete configuration.\n * @throws PopSubError of kind `config` for a URL that isn't `ws://` or\n * `wss://`, credentials that are neither a password nor a token, a\n * session id outside 1 to 128 characters, or a timeout, delay or buffer\n * size that isn't positive.\n */\nexport function resolveConfig(config: ClientConfig): ResolvedConfig {\n const raw: unknown = config;\n if (typeof raw !== \"object\" || raw === null) {\n throw invalid(\"a configuration object is required\");\n }\n if (typeof config.url !== \"string\" || !/^wss?:\\/\\//i.test(config.url)) {\n throw invalid(\"url must start with ws:// or wss://\");\n }\n const sessionId = config.sessionId ?? null;\n if (\n sessionId !== null &&\n (typeof sessionId !== \"string\" || sessionId.length < 1 || sessionId.length > 128)\n ) {\n throw invalid(\"sessionId must be 1 to 128 characters\");\n }\n const ackMode: unknown = config.ackMode ?? \"auto\";\n if (ackMode !== \"auto\" && ackMode !== \"manual\") {\n throw invalid(\"ackMode must be auto or manual\");\n }\n const reconnect = { ...DEFAULT_RECONNECT, ...config.reconnect };\n positive(\"reconnect.initialMs\", reconnect.initialMs);\n positive(\"reconnect.maxMs\", reconnect.maxMs);\n if (\n reconnect.maxAttempts !== null &&\n (!Number.isInteger(reconnect.maxAttempts) || reconnect.maxAttempts < 0)\n ) {\n throw invalid(\"reconnect.maxAttempts must be a whole number of at least 0, or null\");\n }\n const subscriptionBuffer = config.subscriptionBuffer ?? DEFAULT_SUBSCRIPTION_BUFFER;\n if (!Number.isInteger(subscriptionBuffer) || subscriptionBuffer < 1) {\n throw invalid(\"subscriptionBuffer must be a whole number of at least 1\");\n }\n return {\n url: config.url,\n credentials: checkCredentials(config.credentials),\n sessionId,\n cleanStart: config.cleanStart ?? false,\n ackMode,\n reconnect,\n requestTimeoutMs: positive(\"requestTimeoutMs\", config.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS),\n subscriptionBuffer,\n };\n}\n"]}
|
package/dist/dedup.d.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-topic delivery bookkeeping: the next `seq` the application expects
|
|
3
|
+
* (or, for a shared subscription, the `seq`s it was handed), which `seq`s
|
|
4
|
+
* it acknowledged recently, and which acknowledgements are held back while
|
|
5
|
+
* its buffer is full. Pure, so it can be unit tested.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
/** How many acknowledged `seq`s, and for a shared subscription delivered `seq`s, a topic remembers. */
|
|
10
|
+
export declare const ACK_MEMORY = 4096;
|
|
11
|
+
/**
|
|
12
|
+
* The `seq`s a topic acknowledged recently, so a duplicate the application
|
|
13
|
+
* already acknowledged is acknowledged again. Bounded: the oldest are
|
|
14
|
+
* forgotten first.
|
|
15
|
+
*/
|
|
16
|
+
export declare class AckMemory {
|
|
17
|
+
#private;
|
|
18
|
+
/**
|
|
19
|
+
* Builds an empty memory.
|
|
20
|
+
*
|
|
21
|
+
* @param capacity - How many `seq`s to keep; `ACK_MEMORY` by default.
|
|
22
|
+
*/
|
|
23
|
+
constructor(capacity?: number);
|
|
24
|
+
/**
|
|
25
|
+
* Remembers an acknowledged `seq`, forgetting the oldest beyond the capacity.
|
|
26
|
+
*
|
|
27
|
+
* @param seq - The acknowledged message.
|
|
28
|
+
*/
|
|
29
|
+
insert(seq: number): void;
|
|
30
|
+
/**
|
|
31
|
+
* Whether a `seq` is remembered as acknowledged.
|
|
32
|
+
*
|
|
33
|
+
* @param seq - The message.
|
|
34
|
+
* @returns True if it was acknowledged and not yet forgotten.
|
|
35
|
+
*/
|
|
36
|
+
has(seq: number): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* How many `seq`s are remembered.
|
|
39
|
+
*
|
|
40
|
+
* @returns The count.
|
|
41
|
+
*/
|
|
42
|
+
get size(): number;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* What a topic knows about its deliveries. A message whose `seq` is below
|
|
46
|
+
* `nextExpected` is a duplicate and never reaches the application.
|
|
47
|
+
*
|
|
48
|
+
* A shared subscription gets only some of a topic's messages, out of
|
|
49
|
+
* order, so its tracker never uses `nextExpected`: a duplicate is a `seq`
|
|
50
|
+
* found in a bounded set of the `seq`s already handed over.
|
|
51
|
+
*/
|
|
52
|
+
export declare class DeliveryTracker {
|
|
53
|
+
/** Whether this tracks a shared subscription. */
|
|
54
|
+
readonly shared: boolean;
|
|
55
|
+
/** The next `seq` the application should see, or `null` before the first and always for a shared subscription. */
|
|
56
|
+
nextExpected: number | null;
|
|
57
|
+
/** The `seq`s handed to the application, for a shared subscription; the oldest are forgotten first. */
|
|
58
|
+
readonly seen: AckMemory;
|
|
59
|
+
/** The `seq`s acknowledged recently. */
|
|
60
|
+
readonly acked: AckMemory;
|
|
61
|
+
/**
|
|
62
|
+
* QoS 1 `seq`s handed to the buffer in automatic mode whose
|
|
63
|
+
* acknowledgement waits until the application takes them, because the
|
|
64
|
+
* buffer was full when they arrived.
|
|
65
|
+
*/
|
|
66
|
+
readonly deferred: Set<number>;
|
|
67
|
+
/**
|
|
68
|
+
* Builds a tracker with nothing seen yet.
|
|
69
|
+
*
|
|
70
|
+
* @param ackMemory - How many acknowledged `seq`s, and delivered ones
|
|
71
|
+
* for a shared subscription, to remember.
|
|
72
|
+
* @param shared - Whether this tracks a shared subscription.
|
|
73
|
+
*/
|
|
74
|
+
constructor(ackMemory?: number, shared?: boolean);
|
|
75
|
+
/**
|
|
76
|
+
* Moves the next expected `seq` forward, never back. Used for
|
|
77
|
+
* `subscribed.replay_from` and `truncated.first_available`. Does nothing
|
|
78
|
+
* for a shared subscription, whose position is the group's, on the server.
|
|
79
|
+
*
|
|
80
|
+
* @param start - The `seq` delivery continues from.
|
|
81
|
+
*/
|
|
82
|
+
moveTo(start: number): void;
|
|
83
|
+
/**
|
|
84
|
+
* Whether a delivered `seq` was already handed to the application.
|
|
85
|
+
*
|
|
86
|
+
* @param seq - The delivered message.
|
|
87
|
+
* @returns True when it is below the next expected `seq`, or, for a
|
|
88
|
+
* shared subscription, among the remembered delivered `seq`s.
|
|
89
|
+
*/
|
|
90
|
+
isDuplicate(seq: number): boolean;
|
|
91
|
+
/**
|
|
92
|
+
* Records that a message is handed to the application.
|
|
93
|
+
*
|
|
94
|
+
* @param seq - The message.
|
|
95
|
+
*/
|
|
96
|
+
accept(seq: number): void;
|
|
97
|
+
/**
|
|
98
|
+
* Whether a QoS 1 duplicate should be acknowledged again, so a lost
|
|
99
|
+
* acknowledgement can't stall the subscription.
|
|
100
|
+
*
|
|
101
|
+
* @param seq - The duplicate.
|
|
102
|
+
* @param automatic - Whether the client acknowledges on its own.
|
|
103
|
+
* @returns True when it was acknowledged before, or in automatic mode
|
|
104
|
+
* unless its acknowledgement is being held back for a full buffer.
|
|
105
|
+
*/
|
|
106
|
+
reacksDuplicate(seq: number, automatic: boolean): boolean;
|
|
107
|
+
}
|
package/dist/dedup.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-topic delivery bookkeeping: the next `seq` the application expects
|
|
3
|
+
* (or, for a shared subscription, the `seq`s it was handed), which `seq`s
|
|
4
|
+
* it acknowledged recently, and which acknowledgements are held back while
|
|
5
|
+
* its buffer is full. Pure, so it can be unit tested.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
/** How many acknowledged `seq`s, and for a shared subscription delivered `seq`s, a topic remembers. */
|
|
10
|
+
export const ACK_MEMORY = 4096;
|
|
11
|
+
/**
|
|
12
|
+
* The `seq`s a topic acknowledged recently, so a duplicate the application
|
|
13
|
+
* already acknowledged is acknowledged again. Bounded: the oldest are
|
|
14
|
+
* forgotten first.
|
|
15
|
+
*/
|
|
16
|
+
export class AckMemory {
|
|
17
|
+
/** The remembered `seq`s, in the order they were first inserted. */
|
|
18
|
+
#seen = new Set();
|
|
19
|
+
/** How many `seq`s to keep. */
|
|
20
|
+
#capacity;
|
|
21
|
+
/**
|
|
22
|
+
* Builds an empty memory.
|
|
23
|
+
*
|
|
24
|
+
* @param capacity - How many `seq`s to keep; `ACK_MEMORY` by default.
|
|
25
|
+
*/
|
|
26
|
+
constructor(capacity = ACK_MEMORY) {
|
|
27
|
+
this.#capacity = Math.max(1, capacity);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Remembers an acknowledged `seq`, forgetting the oldest beyond the capacity.
|
|
31
|
+
*
|
|
32
|
+
* @param seq - The acknowledged message.
|
|
33
|
+
*/
|
|
34
|
+
insert(seq) {
|
|
35
|
+
if (this.#seen.has(seq)) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
this.#seen.add(seq);
|
|
39
|
+
if (this.#seen.size > this.#capacity) {
|
|
40
|
+
const oldest = this.#seen.values().next();
|
|
41
|
+
if (!oldest.done) {
|
|
42
|
+
this.#seen.delete(oldest.value);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Whether a `seq` is remembered as acknowledged.
|
|
48
|
+
*
|
|
49
|
+
* @param seq - The message.
|
|
50
|
+
* @returns True if it was acknowledged and not yet forgotten.
|
|
51
|
+
*/
|
|
52
|
+
has(seq) {
|
|
53
|
+
return this.#seen.has(seq);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* How many `seq`s are remembered.
|
|
57
|
+
*
|
|
58
|
+
* @returns The count.
|
|
59
|
+
*/
|
|
60
|
+
get size() {
|
|
61
|
+
return this.#seen.size;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* What a topic knows about its deliveries. A message whose `seq` is below
|
|
66
|
+
* `nextExpected` is a duplicate and never reaches the application.
|
|
67
|
+
*
|
|
68
|
+
* A shared subscription gets only some of a topic's messages, out of
|
|
69
|
+
* order, so its tracker never uses `nextExpected`: a duplicate is a `seq`
|
|
70
|
+
* found in a bounded set of the `seq`s already handed over.
|
|
71
|
+
*/
|
|
72
|
+
export class DeliveryTracker {
|
|
73
|
+
/** Whether this tracks a shared subscription. */
|
|
74
|
+
shared;
|
|
75
|
+
/** The next `seq` the application should see, or `null` before the first and always for a shared subscription. */
|
|
76
|
+
nextExpected = null;
|
|
77
|
+
/** The `seq`s handed to the application, for a shared subscription; the oldest are forgotten first. */
|
|
78
|
+
seen;
|
|
79
|
+
/** The `seq`s acknowledged recently. */
|
|
80
|
+
acked;
|
|
81
|
+
/**
|
|
82
|
+
* QoS 1 `seq`s handed to the buffer in automatic mode whose
|
|
83
|
+
* acknowledgement waits until the application takes them, because the
|
|
84
|
+
* buffer was full when they arrived.
|
|
85
|
+
*/
|
|
86
|
+
deferred = new Set();
|
|
87
|
+
/**
|
|
88
|
+
* Builds a tracker with nothing seen yet.
|
|
89
|
+
*
|
|
90
|
+
* @param ackMemory - How many acknowledged `seq`s, and delivered ones
|
|
91
|
+
* for a shared subscription, to remember.
|
|
92
|
+
* @param shared - Whether this tracks a shared subscription.
|
|
93
|
+
*/
|
|
94
|
+
constructor(ackMemory = ACK_MEMORY, shared = false) {
|
|
95
|
+
this.acked = new AckMemory(ackMemory);
|
|
96
|
+
this.seen = new AckMemory(ackMemory);
|
|
97
|
+
this.shared = shared;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Moves the next expected `seq` forward, never back. Used for
|
|
101
|
+
* `subscribed.replay_from` and `truncated.first_available`. Does nothing
|
|
102
|
+
* for a shared subscription, whose position is the group's, on the server.
|
|
103
|
+
*
|
|
104
|
+
* @param start - The `seq` delivery continues from.
|
|
105
|
+
*/
|
|
106
|
+
moveTo(start) {
|
|
107
|
+
if (this.shared) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
this.nextExpected = this.nextExpected === null ? start : Math.max(this.nextExpected, start);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Whether a delivered `seq` was already handed to the application.
|
|
114
|
+
*
|
|
115
|
+
* @param seq - The delivered message.
|
|
116
|
+
* @returns True when it is below the next expected `seq`, or, for a
|
|
117
|
+
* shared subscription, among the remembered delivered `seq`s.
|
|
118
|
+
*/
|
|
119
|
+
isDuplicate(seq) {
|
|
120
|
+
if (this.shared) {
|
|
121
|
+
return this.seen.has(seq);
|
|
122
|
+
}
|
|
123
|
+
return this.nextExpected !== null && seq < this.nextExpected;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Records that a message is handed to the application.
|
|
127
|
+
*
|
|
128
|
+
* @param seq - The message.
|
|
129
|
+
*/
|
|
130
|
+
accept(seq) {
|
|
131
|
+
if (this.shared) {
|
|
132
|
+
this.seen.insert(seq);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
this.nextExpected = seq + 1;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Whether a QoS 1 duplicate should be acknowledged again, so a lost
|
|
139
|
+
* acknowledgement can't stall the subscription.
|
|
140
|
+
*
|
|
141
|
+
* @param seq - The duplicate.
|
|
142
|
+
* @param automatic - Whether the client acknowledges on its own.
|
|
143
|
+
* @returns True when it was acknowledged before, or in automatic mode
|
|
144
|
+
* unless its acknowledgement is being held back for a full buffer.
|
|
145
|
+
*/
|
|
146
|
+
reacksDuplicate(seq, automatic) {
|
|
147
|
+
return this.acked.has(seq) || (automatic && !this.deferred.has(seq));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
//# sourceMappingURL=dedup.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dedup.js","sourceRoot":"","sources":["../src/dedup.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,uGAAuG;AACvG,MAAM,CAAC,MAAM,UAAU,GAAG,IAAI,CAAC;AAE/B;;;;GAIG;AACH,MAAM,OAAO,SAAS;IACpB,oEAAoE;IAC3D,KAAK,GAAG,IAAI,GAAG,EAAU,CAAC;IACnC,+BAA+B;IACtB,SAAS,CAAS;IAE3B;;;;OAIG;IACH,YAAY,WAAmB,UAAU;QACvC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC;IACzC,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,GAAW;QAChB,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACxB,OAAO;QACT,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACpB,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC;YACrC,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC;YAC1C,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;gBACjB,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CA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AAI,IAAI,GAAG,GAAG,IAAI,CAAC,YAAY,CAAC;IAC/D,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,GAAW;QAChB,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YAChB,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YACtB,OAAO;QACT,CAAC;QACD,IAAI,CAAC,YAAY,GAAG,GAAG,GAAG,CAAC,CAAC;IAC9B,CAAC;IAED;;;;;;;;OAQG;IACH,eAAe,CAAC,GAAW,EAAE,SAAkB;QAC7C,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;IACvE,CAAC;CACF","sourcesContent":["/**\n * Per-topic delivery bookkeeping: the next `seq` the application expects\n * (or, for a shared subscription, the `seq`s it was handed), which `seq`s\n * it acknowledged recently, and which acknowledgements are held back while\n * its buffer is full. Pure, so it can be unit tested.\n *\n * @module\n */\n\n/** How many acknowledged `seq`s, and for a shared subscription delivered `seq`s, a topic remembers. */\nexport const ACK_MEMORY = 4096;\n\n/**\n * The `seq`s a topic acknowledged recently, so a duplicate the application\n * already acknowledged is acknowledged again. Bounded: the oldest are\n * forgotten first.\n */\nexport class AckMemory {\n /** The remembered `seq`s, in the order they were first inserted. */\n readonly #seen = new Set<number>();\n /** How many `seq`s to keep. */\n readonly #capacity: number;\n\n /**\n * Builds an empty memory.\n *\n * @param capacity - How many `seq`s to keep; `ACK_MEMORY` by default.\n */\n constructor(capacity: number = ACK_MEMORY) {\n this.#capacity = Math.max(1, capacity);\n }\n\n /**\n * Remembers an acknowledged `seq`, forgetting the oldest beyond the capacity.\n *\n * @param seq - The acknowledged message.\n */\n insert(seq: number): void {\n if (this.#seen.has(seq)) {\n return;\n }\n this.#seen.add(seq);\n if (this.#seen.size > this.#capacity) {\n const oldest = this.#seen.values().next();\n if (!oldest.done) {\n this.#seen.delete(oldest.value);\n }\n }\n }\n\n /**\n * Whether a `seq` is remembered as acknowledged.\n *\n * @param seq - The message.\n * @returns True if it was acknowledged and not yet forgotten.\n */\n has(seq: number): boolean {\n return this.#seen.has(seq);\n }\n\n /**\n * How many `seq`s are remembered.\n *\n * @returns The count.\n */\n get size(): number {\n return this.#seen.size;\n }\n}\n\n/**\n * What a topic knows about its deliveries. A message whose `seq` is below\n * `nextExpected` is a duplicate and never reaches the application.\n *\n * A shared subscription gets only some of a topic's messages, out of\n * order, so its tracker never uses `nextExpected`: a duplicate is a `seq`\n * found in a bounded set of the `seq`s already handed over.\n */\nexport class DeliveryTracker {\n /** Whether this tracks a shared subscription. */\n readonly shared: boolean;\n /** The next `seq` the application should see, or `null` before the first and always for a shared subscription. */\n nextExpected: number | null = null;\n /** The `seq`s handed to the application, for a shared subscription; the oldest are forgotten first. */\n readonly seen: AckMemory;\n /** The `seq`s acknowledged recently. */\n readonly acked: AckMemory;\n /**\n * QoS 1 `seq`s handed to the buffer in automatic mode whose\n * acknowledgement waits until the application takes them, because the\n * buffer was full when they arrived.\n */\n readonly deferred = new Set<number>();\n\n /**\n * Builds a tracker with nothing seen yet.\n *\n * @param ackMemory - How many acknowledged `seq`s, and delivered ones\n * for a shared subscription, to remember.\n * @param shared - Whether this tracks a shared subscription.\n */\n constructor(ackMemory: number = ACK_MEMORY, shared = false) {\n this.acked = new AckMemory(ackMemory);\n this.seen = new AckMemory(ackMemory);\n this.shared = shared;\n }\n\n /**\n * Moves the next expected `seq` forward, never back. Used for\n * `subscribed.replay_from` and `truncated.first_available`. Does nothing\n * for a shared subscription, whose position is the group's, on the server.\n *\n * @param start - The `seq` delivery continues from.\n */\n moveTo(start: number): void {\n if (this.shared) {\n return;\n }\n this.nextExpected = this.nextExpected === null ? start : Math.max(this.nextExpected, start);\n }\n\n /**\n * Whether a delivered `seq` was already handed to the application.\n *\n * @param seq - The delivered message.\n * @returns True when it is below the next expected `seq`, or, for a\n * shared subscription, among the remembered delivered `seq`s.\n */\n isDuplicate(seq: number): boolean {\n if (this.shared) {\n return this.seen.has(seq);\n }\n return this.nextExpected !== null && seq < this.nextExpected;\n }\n\n /**\n * Records that a message is handed to the application.\n *\n * @param seq - The message.\n */\n accept(seq: number): void {\n if (this.shared) {\n this.seen.insert(seq);\n return;\n }\n this.nextExpected = seq + 1;\n }\n\n /**\n * Whether a QoS 1 duplicate should be acknowledged again, so a lost\n * acknowledgement can't stall the subscription.\n *\n * @param seq - The duplicate.\n * @param automatic - Whether the client acknowledges on its own.\n * @returns True when it was acknowledged before, or in automatic mode\n * unless its acknowledgement is being held back for a full buffer.\n */\n reacksDuplicate(seq: number, automatic: boolean): boolean {\n return this.acked.has(seq) || (automatic && !this.deferred.has(seq));\n }\n}\n"]}
|