popsub 0.0.0-stage → 0.1.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 +196 -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
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the application receives: messages, and subscriptions that yield
|
|
3
|
+
* them in `seq` order. A message carries its payload string as it
|
|
4
|
+
* travelled and its `encoding`; `Message.bytes` returns the body as bytes
|
|
5
|
+
* for either kind.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import type { Encoding } from "./protocol.js";
|
|
10
|
+
/** The fields of a delivered message. */
|
|
11
|
+
export interface MessageFields {
|
|
12
|
+
/** The topic it was published to. */
|
|
13
|
+
topic: string;
|
|
14
|
+
/** Its position in the topic. */
|
|
15
|
+
seq: number;
|
|
16
|
+
/** The publisher's id for it. */
|
|
17
|
+
messageId: string;
|
|
18
|
+
/** The body as it travelled: the text itself, or base64 when `encoding` says so. */
|
|
19
|
+
payload: string;
|
|
20
|
+
/** How `payload` encodes the message's bytes; `null` or left out for text. */
|
|
21
|
+
encoding?: Encoding | null;
|
|
22
|
+
/** 0 or 1. */
|
|
23
|
+
qos: 0 | 1;
|
|
24
|
+
/** When it was published, in milliseconds since the Unix epoch. */
|
|
25
|
+
publishedAtMs: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A message delivered on a subscription. Each `(topic, seq)` reaches the
|
|
29
|
+
* application at most once per client.
|
|
30
|
+
*/
|
|
31
|
+
export declare class Message implements Required<MessageFields> {
|
|
32
|
+
#private;
|
|
33
|
+
/** The topic it was published to. */
|
|
34
|
+
readonly topic: string;
|
|
35
|
+
/** Its position in the topic. */
|
|
36
|
+
readonly seq: number;
|
|
37
|
+
/** The publisher's id for it. */
|
|
38
|
+
readonly messageId: string;
|
|
39
|
+
/**
|
|
40
|
+
* The body as it travelled: the text itself, or base64 when `encoding`
|
|
41
|
+
* says so. `bytes` decodes it either way.
|
|
42
|
+
*/
|
|
43
|
+
readonly payload: string;
|
|
44
|
+
/**
|
|
45
|
+
* How `payload` encodes the message's bytes: `null` for text, `"base64"`
|
|
46
|
+
* for a message published as bytes.
|
|
47
|
+
*/
|
|
48
|
+
readonly encoding: Encoding | null;
|
|
49
|
+
/** 0 or 1. */
|
|
50
|
+
readonly qos: 0 | 1;
|
|
51
|
+
/** When it was published, in milliseconds since the Unix epoch. */
|
|
52
|
+
readonly publishedAtMs: number;
|
|
53
|
+
/**
|
|
54
|
+
* Builds a message. Applications receive messages from a `Subscription`
|
|
55
|
+
* rather than building them.
|
|
56
|
+
*
|
|
57
|
+
* @param fields - The message's fields.
|
|
58
|
+
* @param acker - What `ack` calls, or `null` when there is nothing to acknowledge.
|
|
59
|
+
*/
|
|
60
|
+
constructor(fields: MessageFields, acker: (() => void) | null);
|
|
61
|
+
/**
|
|
62
|
+
* The message's bytes: the payload's UTF-8 bytes for a text message, or
|
|
63
|
+
* the decoded payload for one published as bytes.
|
|
64
|
+
*
|
|
65
|
+
* @returns The bytes the publisher sent, in a new array on every call.
|
|
66
|
+
* @throws PopSubError of kind `protocol` when the payload says it is
|
|
67
|
+
* base64 and isn't. The server checks this on publish, so it means the
|
|
68
|
+
* record was stored some other way. The message itself was still
|
|
69
|
+
* delivered, and `payload` holds the string as it arrived.
|
|
70
|
+
*/
|
|
71
|
+
bytes(): Uint8Array;
|
|
72
|
+
/**
|
|
73
|
+
* Acknowledges the message, so the server stops resending it and a
|
|
74
|
+
* durable session's cursor can move past it.
|
|
75
|
+
*
|
|
76
|
+
* Does nothing for QoS 0 messages and in automatic mode, where the client
|
|
77
|
+
* acknowledges on its own. Calling it more than once is harmless. While
|
|
78
|
+
* disconnected the acknowledgement is only remembered, and a redelivery
|
|
79
|
+
* after reconnecting is acknowledged again.
|
|
80
|
+
*/
|
|
81
|
+
ack(): void;
|
|
82
|
+
}
|
|
83
|
+
/** What a channel tells the client about the application's use of it. */
|
|
84
|
+
export interface ChannelHooks {
|
|
85
|
+
/** Called when the application takes a message. */
|
|
86
|
+
taken(message: Message): void;
|
|
87
|
+
/** Called when the application closes the subscription. */
|
|
88
|
+
leave(): void;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The client's side of a subscription: a queue of messages the application
|
|
92
|
+
* hasn't taken yet, and the application's waiting `next` calls.
|
|
93
|
+
*/
|
|
94
|
+
export declare class Channel {
|
|
95
|
+
#private;
|
|
96
|
+
/** The topic. */
|
|
97
|
+
readonly topic: string;
|
|
98
|
+
/**
|
|
99
|
+
* Builds an empty channel.
|
|
100
|
+
*
|
|
101
|
+
* @param topic - The topic.
|
|
102
|
+
* @param hooks - What to tell the client.
|
|
103
|
+
*/
|
|
104
|
+
constructor(topic: string, hooks: ChannelHooks);
|
|
105
|
+
/**
|
|
106
|
+
* How many messages wait for the application.
|
|
107
|
+
*
|
|
108
|
+
* @returns The count.
|
|
109
|
+
*/
|
|
110
|
+
get size(): number;
|
|
111
|
+
/**
|
|
112
|
+
* Whether the channel has ended: nothing more will be added.
|
|
113
|
+
*
|
|
114
|
+
* @returns True once ended.
|
|
115
|
+
*/
|
|
116
|
+
get ended(): boolean;
|
|
117
|
+
/**
|
|
118
|
+
* Adds a message, handing it straight to a waiting `next` call if there is one.
|
|
119
|
+
*
|
|
120
|
+
* @param message - The message.
|
|
121
|
+
*/
|
|
122
|
+
push(message: Message): void;
|
|
123
|
+
/**
|
|
124
|
+
* Ends the channel. Messages already queued can still be taken; waiting
|
|
125
|
+
* `next` calls get `undefined`.
|
|
126
|
+
*/
|
|
127
|
+
end(): void;
|
|
128
|
+
/** Ends the channel and drops the messages still queued. */
|
|
129
|
+
discard(): void;
|
|
130
|
+
/**
|
|
131
|
+
* Takes the next message, waiting for one if none is queued.
|
|
132
|
+
*
|
|
133
|
+
* @returns The message, or `undefined` once the channel has ended and is empty.
|
|
134
|
+
*/
|
|
135
|
+
next(): Promise<Message | undefined>;
|
|
136
|
+
/** Tells the client the application closed the subscription. */
|
|
137
|
+
leave(): void;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* The messages of one subscribed topic, in `seq` order. Iterate it with
|
|
141
|
+
* `for await`, or call `next`.
|
|
142
|
+
*
|
|
143
|
+
* It ends when the server ends the subscription (see the `unsubscribed`
|
|
144
|
+
* event), when the application unsubscribes, or when the client stops.
|
|
145
|
+
* Breaking out of a `for await` loop over it, or calling `close`,
|
|
146
|
+
* unsubscribes.
|
|
147
|
+
*/
|
|
148
|
+
export declare class Subscription implements AsyncIterable<Message> {
|
|
149
|
+
#private;
|
|
150
|
+
/**
|
|
151
|
+
* Wraps a channel. Applications get subscriptions from `Client.subscribe`.
|
|
152
|
+
*
|
|
153
|
+
* @param channel - The client's side of the subscription.
|
|
154
|
+
*/
|
|
155
|
+
constructor(channel: Channel);
|
|
156
|
+
/**
|
|
157
|
+
* The subscribed topic.
|
|
158
|
+
*
|
|
159
|
+
* @returns The topic name.
|
|
160
|
+
*/
|
|
161
|
+
get topic(): string;
|
|
162
|
+
/**
|
|
163
|
+
* How many messages wait to be taken.
|
|
164
|
+
*
|
|
165
|
+
* @returns The count.
|
|
166
|
+
*/
|
|
167
|
+
get buffered(): number;
|
|
168
|
+
/**
|
|
169
|
+
* Waits for the next message.
|
|
170
|
+
*
|
|
171
|
+
* @returns The message, or `undefined` once the subscription has ended
|
|
172
|
+
* and every message it received has been taken.
|
|
173
|
+
*/
|
|
174
|
+
next(): Promise<Message | undefined>;
|
|
175
|
+
/**
|
|
176
|
+
* Unsubscribes without waiting for the server's reply, and drops the
|
|
177
|
+
* messages not yet taken. Does nothing if the topic was subscribed again
|
|
178
|
+
* since this subscription was made.
|
|
179
|
+
*/
|
|
180
|
+
close(): void;
|
|
181
|
+
/**
|
|
182
|
+
* Iterates the messages until the subscription ends.
|
|
183
|
+
*
|
|
184
|
+
* @returns An iterator whose `return` (called by `break`) closes the subscription.
|
|
185
|
+
*/
|
|
186
|
+
[Symbol.asyncIterator](): AsyncIterator<Message>;
|
|
187
|
+
}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the application receives: messages, and subscriptions that yield
|
|
3
|
+
* them in `seq` order. A message carries its payload string as it
|
|
4
|
+
* travelled and its `encoding`; `Message.bytes` returns the body as bytes
|
|
5
|
+
* for either kind.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import { decodeBase64 } from "./base64.js";
|
|
10
|
+
import { PopSubError } from "./errors.js";
|
|
11
|
+
/**
|
|
12
|
+
* A message delivered on a subscription. Each `(topic, seq)` reaches the
|
|
13
|
+
* application at most once per client.
|
|
14
|
+
*/
|
|
15
|
+
export class Message {
|
|
16
|
+
/** The topic it was published to. */
|
|
17
|
+
topic;
|
|
18
|
+
/** Its position in the topic. */
|
|
19
|
+
seq;
|
|
20
|
+
/** The publisher's id for it. */
|
|
21
|
+
messageId;
|
|
22
|
+
/**
|
|
23
|
+
* The body as it travelled: the text itself, or base64 when `encoding`
|
|
24
|
+
* says so. `bytes` decodes it either way.
|
|
25
|
+
*/
|
|
26
|
+
payload;
|
|
27
|
+
/**
|
|
28
|
+
* How `payload` encodes the message's bytes: `null` for text, `"base64"`
|
|
29
|
+
* for a message published as bytes.
|
|
30
|
+
*/
|
|
31
|
+
encoding;
|
|
32
|
+
/** 0 or 1. */
|
|
33
|
+
qos;
|
|
34
|
+
/** When it was published, in milliseconds since the Unix epoch. */
|
|
35
|
+
publishedAtMs;
|
|
36
|
+
/** Sends the acknowledgement, for QoS 1 messages in manual mode; otherwise `null`. */
|
|
37
|
+
#acker;
|
|
38
|
+
/**
|
|
39
|
+
* Builds a message. Applications receive messages from a `Subscription`
|
|
40
|
+
* rather than building them.
|
|
41
|
+
*
|
|
42
|
+
* @param fields - The message's fields.
|
|
43
|
+
* @param acker - What `ack` calls, or `null` when there is nothing to acknowledge.
|
|
44
|
+
*/
|
|
45
|
+
constructor(fields, acker) {
|
|
46
|
+
this.topic = fields.topic;
|
|
47
|
+
this.seq = fields.seq;
|
|
48
|
+
this.messageId = fields.messageId;
|
|
49
|
+
this.payload = fields.payload;
|
|
50
|
+
this.encoding = fields.encoding ?? null;
|
|
51
|
+
this.qos = fields.qos;
|
|
52
|
+
this.publishedAtMs = fields.publishedAtMs;
|
|
53
|
+
this.#acker = acker;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The message's bytes: the payload's UTF-8 bytes for a text message, or
|
|
57
|
+
* the decoded payload for one published as bytes.
|
|
58
|
+
*
|
|
59
|
+
* @returns The bytes the publisher sent, in a new array on every call.
|
|
60
|
+
* @throws PopSubError of kind `protocol` when the payload says it is
|
|
61
|
+
* base64 and isn't. The server checks this on publish, so it means the
|
|
62
|
+
* record was stored some other way. The message itself was still
|
|
63
|
+
* delivered, and `payload` holds the string as it arrived.
|
|
64
|
+
*/
|
|
65
|
+
bytes() {
|
|
66
|
+
if (this.encoding === null) {
|
|
67
|
+
return new TextEncoder().encode(this.payload);
|
|
68
|
+
}
|
|
69
|
+
const decoded = decodeBase64(this.payload);
|
|
70
|
+
if (decoded === null) {
|
|
71
|
+
throw new PopSubError("protocol", "the payload isn't valid base64");
|
|
72
|
+
}
|
|
73
|
+
return decoded;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Acknowledges the message, so the server stops resending it and a
|
|
77
|
+
* durable session's cursor can move past it.
|
|
78
|
+
*
|
|
79
|
+
* Does nothing for QoS 0 messages and in automatic mode, where the client
|
|
80
|
+
* acknowledges on its own. Calling it more than once is harmless. While
|
|
81
|
+
* disconnected the acknowledgement is only remembered, and a redelivery
|
|
82
|
+
* after reconnecting is acknowledged again.
|
|
83
|
+
*/
|
|
84
|
+
ack() {
|
|
85
|
+
this.#acker?.();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* The client's side of a subscription: a queue of messages the application
|
|
90
|
+
* hasn't taken yet, and the application's waiting `next` calls.
|
|
91
|
+
*/
|
|
92
|
+
export class Channel {
|
|
93
|
+
/** The topic. */
|
|
94
|
+
topic;
|
|
95
|
+
/** Queued messages; the live ones start at `#head`. */
|
|
96
|
+
#items = [];
|
|
97
|
+
/** The index of the oldest queued message in `#items`. */
|
|
98
|
+
#head = 0;
|
|
99
|
+
/** `next` calls waiting for a message. */
|
|
100
|
+
#waiters = [];
|
|
101
|
+
/** Whether nothing more will be pushed. */
|
|
102
|
+
#ended = false;
|
|
103
|
+
/** What to tell the client. */
|
|
104
|
+
#hooks;
|
|
105
|
+
/**
|
|
106
|
+
* Builds an empty channel.
|
|
107
|
+
*
|
|
108
|
+
* @param topic - The topic.
|
|
109
|
+
* @param hooks - What to tell the client.
|
|
110
|
+
*/
|
|
111
|
+
constructor(topic, hooks) {
|
|
112
|
+
this.topic = topic;
|
|
113
|
+
this.#hooks = hooks;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* How many messages wait for the application.
|
|
117
|
+
*
|
|
118
|
+
* @returns The count.
|
|
119
|
+
*/
|
|
120
|
+
get size() {
|
|
121
|
+
return this.#items.length - this.#head;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Whether the channel has ended: nothing more will be added.
|
|
125
|
+
*
|
|
126
|
+
* @returns True once ended.
|
|
127
|
+
*/
|
|
128
|
+
get ended() {
|
|
129
|
+
return this.#ended;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Adds a message, handing it straight to a waiting `next` call if there is one.
|
|
133
|
+
*
|
|
134
|
+
* @param message - The message.
|
|
135
|
+
*/
|
|
136
|
+
push(message) {
|
|
137
|
+
if (this.#ended) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const waiter = this.#waiters.shift();
|
|
141
|
+
if (waiter !== undefined) {
|
|
142
|
+
this.#hooks.taken(message);
|
|
143
|
+
waiter(message);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
this.#items.push(message);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Ends the channel. Messages already queued can still be taken; waiting
|
|
150
|
+
* `next` calls get `undefined`.
|
|
151
|
+
*/
|
|
152
|
+
end() {
|
|
153
|
+
if (this.#ended) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
this.#ended = true;
|
|
157
|
+
for (const waiter of this.#waiters.splice(0)) {
|
|
158
|
+
waiter(undefined);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/** Ends the channel and drops the messages still queued. */
|
|
162
|
+
discard() {
|
|
163
|
+
this.end();
|
|
164
|
+
this.#items = [];
|
|
165
|
+
this.#head = 0;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Takes the next message, waiting for one if none is queued.
|
|
169
|
+
*
|
|
170
|
+
* @returns The message, or `undefined` once the channel has ended and is empty.
|
|
171
|
+
*/
|
|
172
|
+
next() {
|
|
173
|
+
const message = this.#items[this.#head];
|
|
174
|
+
if (message !== undefined) {
|
|
175
|
+
this.#items[this.#head] = undefined;
|
|
176
|
+
this.#head += 1;
|
|
177
|
+
if (this.#head > 1024 && this.#head * 2 > this.#items.length) {
|
|
178
|
+
this.#items = this.#items.slice(this.#head);
|
|
179
|
+
this.#head = 0;
|
|
180
|
+
}
|
|
181
|
+
this.#hooks.taken(message);
|
|
182
|
+
return Promise.resolve(message);
|
|
183
|
+
}
|
|
184
|
+
if (this.#ended) {
|
|
185
|
+
return Promise.resolve(undefined);
|
|
186
|
+
}
|
|
187
|
+
return new Promise((resolve) => this.#waiters.push(resolve));
|
|
188
|
+
}
|
|
189
|
+
/** Tells the client the application closed the subscription. */
|
|
190
|
+
leave() {
|
|
191
|
+
this.#hooks.leave();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* The messages of one subscribed topic, in `seq` order. Iterate it with
|
|
196
|
+
* `for await`, or call `next`.
|
|
197
|
+
*
|
|
198
|
+
* It ends when the server ends the subscription (see the `unsubscribed`
|
|
199
|
+
* event), when the application unsubscribes, or when the client stops.
|
|
200
|
+
* Breaking out of a `for await` loop over it, or calling `close`,
|
|
201
|
+
* unsubscribes.
|
|
202
|
+
*/
|
|
203
|
+
export class Subscription {
|
|
204
|
+
/** The client's side of the subscription. */
|
|
205
|
+
#channel;
|
|
206
|
+
/**
|
|
207
|
+
* Wraps a channel. Applications get subscriptions from `Client.subscribe`.
|
|
208
|
+
*
|
|
209
|
+
* @param channel - The client's side of the subscription.
|
|
210
|
+
*/
|
|
211
|
+
constructor(channel) {
|
|
212
|
+
this.#channel = channel;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* The subscribed topic.
|
|
216
|
+
*
|
|
217
|
+
* @returns The topic name.
|
|
218
|
+
*/
|
|
219
|
+
get topic() {
|
|
220
|
+
return this.#channel.topic;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* How many messages wait to be taken.
|
|
224
|
+
*
|
|
225
|
+
* @returns The count.
|
|
226
|
+
*/
|
|
227
|
+
get buffered() {
|
|
228
|
+
return this.#channel.size;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Waits for the next message.
|
|
232
|
+
*
|
|
233
|
+
* @returns The message, or `undefined` once the subscription has ended
|
|
234
|
+
* and every message it received has been taken.
|
|
235
|
+
*/
|
|
236
|
+
next() {
|
|
237
|
+
return this.#channel.next();
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Unsubscribes without waiting for the server's reply, and drops the
|
|
241
|
+
* messages not yet taken. Does nothing if the topic was subscribed again
|
|
242
|
+
* since this subscription was made.
|
|
243
|
+
*/
|
|
244
|
+
close() {
|
|
245
|
+
if (!this.#channel.ended) {
|
|
246
|
+
this.#channel.leave();
|
|
247
|
+
}
|
|
248
|
+
this.#channel.discard();
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Iterates the messages until the subscription ends.
|
|
252
|
+
*
|
|
253
|
+
* @returns An iterator whose `return` (called by `break`) closes the subscription.
|
|
254
|
+
*/
|
|
255
|
+
[Symbol.asyncIterator]() {
|
|
256
|
+
return {
|
|
257
|
+
next: async () => {
|
|
258
|
+
const message = await this.#channel.next();
|
|
259
|
+
return message === undefined ? { value: undefined, done: true } : { value: message, done: false };
|
|
260
|
+
},
|
|
261
|
+
return: () => {
|
|
262
|
+
this.close();
|
|
263
|
+
return Promise.resolve({ value: undefined, done: true });
|
|
264
|
+
},
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
//# sourceMappingURL=subscription.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subscription.js","sourceRoot":"","sources":["../src/subscription.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAqB1C;;;GAGG;AACH,MAAM,OAAO,OAAO;IAClB,qCAAqC;IAC5B,KAAK,CAAS;IACvB,iCAAiC;IACxB,GAAG,CAAS;IACrB,iCAAiC;IACxB,SAAS,CAAS;IAC3B;;;OAGG;IACM,OAAO,CAAS;IACzB;;;OAGG;IACM,QAAQ,CAAkB;IACnC,cAAc;IACL,GAAG,CAAQ;IACpB,mEAAmE;IAC1D,aAAa,CAAS;IAC/B,sFAAsF;IAC7E,MAAM,CAAsB;IAErC;;;;;;OAMG;IACH,YAAY,MAAqB,EAAE,KAA0B;QAC3D,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QAC1B,IAAI,CAAC,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC;QACtB,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC;QAClC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;QAC9B,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,IAAI,IAAI,CAAC;QACxC,IAAI,CAAC,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC;QACtB,IAAI,CAAC,aAAa,GAAG,MAAM,CAAC,aAAa,CAAC;QAC1C,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAED;;;;;;;;;OASG;IACH,KAAK;QACH,IAAI,IAAI,CAAC,QAAQ,KAAK,IAAI,EAAE,CAAC;YAC3B,OAAO,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAChD,CAAC;QACD,MAAM,OAA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C,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;YACpG,CAAC;YACD,MAAM,EAAE,GAAqC,EAAE;gBAC7C,IAAI,CAAC,KAAK,EAAE,CAAC;gBACb,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;YAC3D,CAAC;SACF,CAAC;IACJ,CAAC;CACF","sourcesContent":["/**\n * What the application receives: messages, and subscriptions that yield\n * them in `seq` order. A message carries its payload string as it\n * travelled and its `encoding`; `Message.bytes` returns the body as bytes\n * for either kind.\n *\n * @module\n */\n\nimport { decodeBase64 } from \"./base64.js\";\nimport { PopSubError } from \"./errors.js\";\nimport type { Encoding } from \"./protocol.js\";\n\n/** The fields of a delivered message. */\nexport interface MessageFields {\n /** The topic it was published to. */\n topic: string;\n /** Its position in the topic. */\n seq: number;\n /** The publisher's id for it. */\n messageId: string;\n /** The body as it travelled: the text itself, or base64 when `encoding` says so. */\n payload: string;\n /** How `payload` encodes the message's bytes; `null` or left out for text. */\n encoding?: Encoding | null;\n /** 0 or 1. */\n qos: 0 | 1;\n /** When it was published, in milliseconds since the Unix epoch. */\n publishedAtMs: number;\n}\n\n/**\n * A message delivered on a subscription. Each `(topic, seq)` reaches the\n * application at most once per client.\n */\nexport class Message implements Required<MessageFields> {\n /** The topic it was published to. */\n readonly topic: string;\n /** Its position in the topic. */\n readonly seq: number;\n /** The publisher's id for it. */\n readonly messageId: string;\n /**\n * The body as it travelled: the text itself, or base64 when `encoding`\n * says so. `bytes` decodes it either way.\n */\n readonly payload: string;\n /**\n * How `payload` encodes the message's bytes: `null` for text, `\"base64\"`\n * for a message published as bytes.\n */\n readonly encoding: Encoding | null;\n /** 0 or 1. */\n readonly qos: 0 | 1;\n /** When it was published, in milliseconds since the Unix epoch. */\n readonly publishedAtMs: number;\n /** Sends the acknowledgement, for QoS 1 messages in manual mode; otherwise `null`. */\n readonly #acker: (() => void) | null;\n\n /**\n * Builds a message. Applications receive messages from a `Subscription`\n * rather than building them.\n *\n * @param fields - The message's fields.\n * @param acker - What `ack` calls, or `null` when there is nothing to acknowledge.\n */\n constructor(fields: MessageFields, acker: (() => void) | null) {\n this.topic = fields.topic;\n this.seq = fields.seq;\n this.messageId = fields.messageId;\n this.payload = fields.payload;\n this.encoding = fields.encoding ?? null;\n this.qos = fields.qos;\n this.publishedAtMs = fields.publishedAtMs;\n this.#acker = acker;\n }\n\n /**\n * The message's bytes: the payload's UTF-8 bytes for a text message, or\n * the decoded payload for one published as bytes.\n *\n * @returns The bytes the publisher sent, in a new array on every call.\n * @throws PopSubError of kind `protocol` when the payload says it is\n * base64 and isn't. The server checks this on publish, so it means the\n * record was stored some other way. The message itself was still\n * delivered, and `payload` holds the string as it arrived.\n */\n bytes(): Uint8Array {\n if (this.encoding === null) {\n return new TextEncoder().encode(this.payload);\n }\n const decoded = decodeBase64(this.payload);\n if (decoded === null) {\n throw new PopSubError(\"protocol\", \"the payload isn't valid base64\");\n }\n return decoded;\n }\n\n /**\n * Acknowledges the message, so the server stops resending it and a\n * durable session's cursor can move past it.\n *\n * Does nothing for QoS 0 messages and in automatic mode, where the client\n * acknowledges on its own. Calling it more than once is harmless. While\n * disconnected the acknowledgement is only remembered, and a redelivery\n * after reconnecting is acknowledged again.\n */\n ack(): void {\n this.#acker?.();\n }\n}\n\n/** What a channel tells the client about the application's use of it. */\nexport interface ChannelHooks {\n /** Called when the application takes a message. */\n taken(message: Message): void;\n /** Called when the application closes the subscription. */\n leave(): void;\n}\n\n/**\n * The client's side of a subscription: a queue of messages the application\n * hasn't taken yet, and the application's waiting `next` calls.\n */\nexport class Channel {\n /** The topic. */\n readonly topic: string;\n /** Queued messages; the live ones start at `#head`. */\n #items: (Message | undefined)[] = [];\n /** The index of the oldest queued message in `#items`. */\n #head = 0;\n /** `next` calls waiting for a message. */\n #waiters: ((message: Message | undefined) => void)[] = [];\n /** Whether nothing more will be pushed. */\n #ended = false;\n /** What to tell the client. */\n readonly #hooks: ChannelHooks;\n\n /**\n * Builds an empty channel.\n *\n * @param topic - The topic.\n * @param hooks - What to tell the client.\n */\n constructor(topic: string, hooks: ChannelHooks) {\n this.topic = topic;\n this.#hooks = hooks;\n }\n\n /**\n * How many messages wait for the application.\n *\n * @returns The count.\n */\n get size(): number {\n return this.#items.length - this.#head;\n }\n\n /**\n * Whether the channel has ended: nothing more will be added.\n *\n * @returns True once ended.\n */\n get ended(): boolean {\n return this.#ended;\n }\n\n /**\n * Adds a message, handing it straight to a waiting `next` call if there is one.\n *\n * @param message - The message.\n */\n push(message: Message): void {\n if (this.#ended) {\n return;\n }\n const waiter = this.#waiters.shift();\n if (waiter !== undefined) {\n this.#hooks.taken(message);\n waiter(message);\n return;\n }\n this.#items.push(message);\n }\n\n /**\n * Ends the channel. Messages already queued can still be taken; waiting\n * `next` calls get `undefined`.\n */\n end(): void {\n if (this.#ended) {\n return;\n }\n this.#ended = true;\n for (const waiter of this.#waiters.splice(0)) {\n waiter(undefined);\n }\n }\n\n /** Ends the channel and drops the messages still queued. */\n discard(): void {\n this.end();\n this.#items = [];\n this.#head = 0;\n }\n\n /**\n * Takes the next message, waiting for one if none is queued.\n *\n * @returns The message, or `undefined` once the channel has ended and is empty.\n */\n next(): Promise<Message | undefined> {\n const message = this.#items[this.#head];\n if (message !== undefined) {\n this.#items[this.#head] = undefined;\n this.#head += 1;\n if (this.#head > 1024 && this.#head * 2 > this.#items.length) {\n this.#items = this.#items.slice(this.#head);\n this.#head = 0;\n }\n this.#hooks.taken(message);\n return Promise.resolve(message);\n }\n if (this.#ended) {\n return Promise.resolve(undefined);\n }\n return new Promise((resolve) => this.#waiters.push(resolve));\n }\n\n /** Tells the client the application closed the subscription. */\n leave(): void {\n this.#hooks.leave();\n }\n}\n\n/**\n * The messages of one subscribed topic, in `seq` order. Iterate it with\n * `for await`, or call `next`.\n *\n * It ends when the server ends the subscription (see the `unsubscribed`\n * event), when the application unsubscribes, or when the client stops.\n * Breaking out of a `for await` loop over it, or calling `close`,\n * unsubscribes.\n */\nexport class Subscription implements AsyncIterable<Message> {\n /** The client's side of the subscription. */\n readonly #channel: Channel;\n\n /**\n * Wraps a channel. Applications get subscriptions from `Client.subscribe`.\n *\n * @param channel - The client's side of the subscription.\n */\n constructor(channel: Channel) {\n this.#channel = channel;\n }\n\n /**\n * The subscribed topic.\n *\n * @returns The topic name.\n */\n get topic(): string {\n return this.#channel.topic;\n }\n\n /**\n * How many messages wait to be taken.\n *\n * @returns The count.\n */\n get buffered(): number {\n return this.#channel.size;\n }\n\n /**\n * Waits for the next message.\n *\n * @returns The message, or `undefined` once the subscription has ended\n * and every message it received has been taken.\n */\n next(): Promise<Message | undefined> {\n return this.#channel.next();\n }\n\n /**\n * Unsubscribes without waiting for the server's reply, and drops the\n * messages not yet taken. Does nothing if the topic was subscribed again\n * since this subscription was made.\n */\n close(): void {\n if (!this.#channel.ended) {\n this.#channel.leave();\n }\n this.#channel.discard();\n }\n\n /**\n * Iterates the messages until the subscription ends.\n *\n * @returns An iterator whose `return` (called by `break`) closes the subscription.\n */\n [Symbol.asyncIterator](): AsyncIterator<Message> {\n return {\n next: async (): Promise<IteratorResult<Message>> => {\n const message = await this.#channel.next();\n return message === undefined ? { value: undefined, done: true } : { value: message, done: false };\n },\n return: (): Promise<IteratorResult<Message>> => {\n this.close();\n return Promise.resolve({ value: undefined, done: true });\n },\n };\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "popsub",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "PopSub client for browsers and Node.js 22+: reconnects, resubscribes without gaps, deduplicates and acknowledges, on the global WebSocket.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"popsub",
|
|
7
|
+
"pubsub",
|
|
8
|
+
"websocket",
|
|
9
|
+
"client"
|
|
10
|
+
],
|
|
11
|
+
"license": "Apache-2.0",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/soumyadepp/popsub.git",
|
|
15
|
+
"directory": "clients/typescript"
|
|
16
|
+
},
|
|
17
|
+
"type": "module",
|
|
18
|
+
"main": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"import": "./dist/index.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"!dist/driver.*"
|
|
29
|
+
],
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=22"
|
|
32
|
+
},
|
|
33
|
+
"sideEffects": false,
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsc -p tsconfig.build.json",
|
|
36
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
37
|
+
"lint": "eslint --max-warnings 0 .",
|
|
38
|
+
"format": "prettier --write .",
|
|
39
|
+
"format:check": "prettier --check .",
|
|
40
|
+
"test": "tsc -p tsconfig.test.json && node --test \"dist-test/test/*.test.js\"",
|
|
41
|
+
"check": "npm run typecheck && npm run lint && npm run format:check && npm run test",
|
|
42
|
+
"prepack": "npm run build",
|
|
43
|
+
"scenarios": "npm run build && cd ../.. && target/debug/popsub-scenarios --driver \"node clients/typescript/dist/driver.js\""
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/node": "^22.20.5",
|
|
47
|
+
"eslint": "^10.15.0",
|
|
48
|
+
"prettier": "^3.10.1",
|
|
49
|
+
"typescript": "~5.9.3",
|
|
50
|
+
"typescript-eslint": "^8.73.0"
|
|
51
|
+
}
|
|
52
|
+
}
|