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,313 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The PopSub wire protocol, version 1: the JSON frames a client sends and
|
|
3
|
+
* receives, the error codes and the WebSocket close codes, as described in
|
|
4
|
+
* `docs/DESIGN.md` section 4 and `docs/protocol/*.json`.
|
|
5
|
+
*
|
|
6
|
+
* These types keep the protocol's own snake_case field names. The public
|
|
7
|
+
* API converts them to camelCase at its edges.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
/** The protocol version this client speaks, sent in every `auth`. */
|
|
12
|
+
export declare const PROTOCOL_VERSION = 1;
|
|
13
|
+
/** A request id. This client only ever sends numbers. */
|
|
14
|
+
export type RequestId = number | string;
|
|
15
|
+
/** Where replay starts when subscribing, as it is written on the wire. */
|
|
16
|
+
export type WireStartFrom = "resume" | "new" | {
|
|
17
|
+
last: number;
|
|
18
|
+
} | {
|
|
19
|
+
since_ms: number;
|
|
20
|
+
} | {
|
|
21
|
+
seq: number;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* How a payload string encodes a message's bytes when it isn't text.
|
|
25
|
+
* `base64` is standard padded base64; a frame without an encoding carries text.
|
|
26
|
+
*/
|
|
27
|
+
export type Encoding = "base64";
|
|
28
|
+
/** What a topic grant allows. */
|
|
29
|
+
export type Access = "subscribe" | "publish" | "both";
|
|
30
|
+
/** The kind of account. */
|
|
31
|
+
export type RoleKind = "admin" | "user" | "readonly";
|
|
32
|
+
/** One topic pattern an account may use, and how. */
|
|
33
|
+
export interface TopicGrant {
|
|
34
|
+
/** The topic pattern, such as `orders/#`. */
|
|
35
|
+
pattern: string;
|
|
36
|
+
/** What the pattern allows. */
|
|
37
|
+
access: Access;
|
|
38
|
+
}
|
|
39
|
+
/** What an account may do. `topics` is ignored for admins. */
|
|
40
|
+
export interface RoleSpec {
|
|
41
|
+
/** The kind of account. */
|
|
42
|
+
kind: RoleKind;
|
|
43
|
+
/** The account's topic grants. */
|
|
44
|
+
topics?: TopicGrant[];
|
|
45
|
+
}
|
|
46
|
+
/** An account as the server describes it in `user` and `users` frames. */
|
|
47
|
+
export interface WireUser {
|
|
48
|
+
/** The username. */
|
|
49
|
+
username: string;
|
|
50
|
+
/** What it may do. */
|
|
51
|
+
role: RoleSpec;
|
|
52
|
+
/** Whether it may log in and use topics. */
|
|
53
|
+
enabled: boolean;
|
|
54
|
+
/** When it was created, in milliseconds since the Unix epoch. */
|
|
55
|
+
created_at_ms: number;
|
|
56
|
+
}
|
|
57
|
+
/** A topic as the server describes it in `topic_info` and `topics` frames. */
|
|
58
|
+
export interface WireTopicInfo {
|
|
59
|
+
/** The topic's name. */
|
|
60
|
+
name: string;
|
|
61
|
+
/** The oldest `seq` still kept, or `last_seq + 1` when nothing is kept. */
|
|
62
|
+
first_seq: number;
|
|
63
|
+
/** The newest `seq` ever published. */
|
|
64
|
+
last_seq: number;
|
|
65
|
+
/** How many records are kept now. */
|
|
66
|
+
messages: number;
|
|
67
|
+
/** How many live subscriptions it has. */
|
|
68
|
+
subscribers: number;
|
|
69
|
+
}
|
|
70
|
+
/** The admin requests, which are never queued or resent. */
|
|
71
|
+
export type AdminRequest = {
|
|
72
|
+
type: "create_user";
|
|
73
|
+
username: string;
|
|
74
|
+
password: string;
|
|
75
|
+
role: RoleSpec;
|
|
76
|
+
} | {
|
|
77
|
+
type: "get_user";
|
|
78
|
+
username: string;
|
|
79
|
+
} | {
|
|
80
|
+
type: "list_users";
|
|
81
|
+
} | {
|
|
82
|
+
type: "set_role";
|
|
83
|
+
username: string;
|
|
84
|
+
role: RoleSpec;
|
|
85
|
+
} | {
|
|
86
|
+
type: "grant";
|
|
87
|
+
username: string;
|
|
88
|
+
pattern: string;
|
|
89
|
+
access: Access;
|
|
90
|
+
} | {
|
|
91
|
+
type: "revoke";
|
|
92
|
+
username: string;
|
|
93
|
+
pattern: string;
|
|
94
|
+
} | {
|
|
95
|
+
type: "set_password";
|
|
96
|
+
username: string;
|
|
97
|
+
password: string;
|
|
98
|
+
} | {
|
|
99
|
+
type: "set_enabled";
|
|
100
|
+
username: string;
|
|
101
|
+
enabled: boolean;
|
|
102
|
+
} | {
|
|
103
|
+
type: "delete_user";
|
|
104
|
+
username: string;
|
|
105
|
+
} | {
|
|
106
|
+
type: "list_topics";
|
|
107
|
+
} | {
|
|
108
|
+
type: "get_topic";
|
|
109
|
+
topic: string;
|
|
110
|
+
} | {
|
|
111
|
+
type: "purge_topic";
|
|
112
|
+
topic: string;
|
|
113
|
+
};
|
|
114
|
+
/** Every frame this client sends, without its request id. */
|
|
115
|
+
export type ClientMessage = {
|
|
116
|
+
type: "login";
|
|
117
|
+
username: string;
|
|
118
|
+
password: string;
|
|
119
|
+
} | {
|
|
120
|
+
type: "auth";
|
|
121
|
+
token: string;
|
|
122
|
+
protocol?: number;
|
|
123
|
+
session_id?: string;
|
|
124
|
+
clean_start?: boolean;
|
|
125
|
+
hold_resumed?: boolean;
|
|
126
|
+
} | {
|
|
127
|
+
type: "subscribe";
|
|
128
|
+
topic: string;
|
|
129
|
+
from?: WireStartFrom;
|
|
130
|
+
group?: string;
|
|
131
|
+
} | {
|
|
132
|
+
type: "unsubscribe";
|
|
133
|
+
topic: string;
|
|
134
|
+
} | {
|
|
135
|
+
type: "publish";
|
|
136
|
+
topic: string;
|
|
137
|
+
payload: string;
|
|
138
|
+
encoding?: Encoding;
|
|
139
|
+
message_id?: string;
|
|
140
|
+
qos?: 0 | 1;
|
|
141
|
+
} | {
|
|
142
|
+
type: "ack";
|
|
143
|
+
topic: string;
|
|
144
|
+
seq: number;
|
|
145
|
+
} | {
|
|
146
|
+
type: "ping";
|
|
147
|
+
} | AdminRequest;
|
|
148
|
+
/** The limits a server announces in `authenticated`. */
|
|
149
|
+
export interface Limits {
|
|
150
|
+
/** How many QoS 1 records each subscription may have unacknowledged. */
|
|
151
|
+
max_inflight: number;
|
|
152
|
+
/** How long an untouched durable session is kept, in seconds. */
|
|
153
|
+
session_expiry_secs: number;
|
|
154
|
+
/** How often the server pings, in seconds. */
|
|
155
|
+
heartbeat_secs: number;
|
|
156
|
+
/** How long the server waits for anything from the client, in seconds. */
|
|
157
|
+
idle_timeout_secs: number;
|
|
158
|
+
/** The largest payload the server accepts, in bytes. */
|
|
159
|
+
max_payload_bytes: number;
|
|
160
|
+
/** The longest topic name, in bytes. */
|
|
161
|
+
max_topic_bytes: number;
|
|
162
|
+
}
|
|
163
|
+
/** Every frame the server sends that this client reads, without its id. */
|
|
164
|
+
export type ServerMessage = {
|
|
165
|
+
type: "registered";
|
|
166
|
+
} | {
|
|
167
|
+
type: "logged_in";
|
|
168
|
+
token: string;
|
|
169
|
+
} | {
|
|
170
|
+
type: "authenticated";
|
|
171
|
+
protocol: number;
|
|
172
|
+
limits: Limits;
|
|
173
|
+
session_id?: string | null;
|
|
174
|
+
resumed?: string[];
|
|
175
|
+
held?: boolean;
|
|
176
|
+
} | {
|
|
177
|
+
type: "subscribed";
|
|
178
|
+
topic: string;
|
|
179
|
+
replay_from: number;
|
|
180
|
+
replay_to: number;
|
|
181
|
+
} | {
|
|
182
|
+
type: "unsubscribed";
|
|
183
|
+
topic: string;
|
|
184
|
+
} | {
|
|
185
|
+
type: "published";
|
|
186
|
+
message_id: string;
|
|
187
|
+
seq: number;
|
|
188
|
+
} | {
|
|
189
|
+
type: "message";
|
|
190
|
+
topic: string;
|
|
191
|
+
seq: number;
|
|
192
|
+
message_id: string;
|
|
193
|
+
payload: string;
|
|
194
|
+
encoding?: Encoding | null;
|
|
195
|
+
qos: number;
|
|
196
|
+
published_at_ms: number;
|
|
197
|
+
} | {
|
|
198
|
+
type: "truncated";
|
|
199
|
+
topic: string;
|
|
200
|
+
first_available: number;
|
|
201
|
+
} | {
|
|
202
|
+
type: "stalled";
|
|
203
|
+
topic: string;
|
|
204
|
+
seq: number;
|
|
205
|
+
} | {
|
|
206
|
+
type: "auth_expiring";
|
|
207
|
+
expires_at_ms: number;
|
|
208
|
+
} | {
|
|
209
|
+
type: "user";
|
|
210
|
+
user: WireUser;
|
|
211
|
+
} | {
|
|
212
|
+
type: "users";
|
|
213
|
+
users: WireUser[];
|
|
214
|
+
} | {
|
|
215
|
+
type: "user_deleted";
|
|
216
|
+
username: string;
|
|
217
|
+
} | {
|
|
218
|
+
type: "pong";
|
|
219
|
+
} | {
|
|
220
|
+
type: "topic_info";
|
|
221
|
+
topic: WireTopicInfo;
|
|
222
|
+
} | {
|
|
223
|
+
type: "topics";
|
|
224
|
+
topics: WireTopicInfo[];
|
|
225
|
+
} | {
|
|
226
|
+
type: "error";
|
|
227
|
+
code: string;
|
|
228
|
+
message: string;
|
|
229
|
+
retry_after?: number | null;
|
|
230
|
+
};
|
|
231
|
+
/** A server frame: a message plus the id of the request it answers, if any. */
|
|
232
|
+
export type ServerFrame = ServerMessage & {
|
|
233
|
+
id?: RequestId | null;
|
|
234
|
+
};
|
|
235
|
+
/** Every error code protocol version 1 defines. */
|
|
236
|
+
export declare const ERROR_CODES: readonly ["bad_request", "unauthenticated", "auth_failed", "invalid_credentials", "locked_out", "rate_limited", "not_authorized", "registration_failed", "invalid_session_id", "session_unavailable", "unsupported_protocol", "storage_unavailable", "payload_too_large", "invalid_topic", "forbidden", "user_not_found", "user_exists", "topic_not_found", "internal"];
|
|
237
|
+
/** Why the server refused a request. */
|
|
238
|
+
export type ErrorCode = (typeof ERROR_CODES)[number];
|
|
239
|
+
/**
|
|
240
|
+
* Reads an error code from a frame. The protocol says to treat a code this
|
|
241
|
+
* client doesn't know like `internal`.
|
|
242
|
+
*
|
|
243
|
+
* @param raw - The `code` field as received.
|
|
244
|
+
* @returns The code, or `internal` when it is unknown.
|
|
245
|
+
*/
|
|
246
|
+
export declare function errorCode(raw: unknown): ErrorCode;
|
|
247
|
+
/** The WebSocket close codes a PopSub server sends, by name. */
|
|
248
|
+
export declare const CloseCode: {
|
|
249
|
+
readonly GoingAway: 1001;
|
|
250
|
+
readonly MessageTooBig: 1009;
|
|
251
|
+
readonly InternalError: 1011;
|
|
252
|
+
readonly TryAgainLater: 1013;
|
|
253
|
+
readonly AuthFailed: 4001;
|
|
254
|
+
readonly Unauthenticated: 4002;
|
|
255
|
+
readonly SessionTakenOver: 4003;
|
|
256
|
+
readonly TokenExpired: 4004;
|
|
257
|
+
readonly IdleTimeout: 4005;
|
|
258
|
+
readonly UnsupportedProtocol: 4006;
|
|
259
|
+
readonly SessionUnavailable: 4007;
|
|
260
|
+
readonly SlowConsumer: 4008;
|
|
261
|
+
readonly AccountDisabled: 4009;
|
|
262
|
+
};
|
|
263
|
+
/** One of the close codes a PopSub server sends. */
|
|
264
|
+
export type CloseCode = (typeof CloseCode)[keyof typeof CloseCode];
|
|
265
|
+
/**
|
|
266
|
+
* Reads a close code from a WebSocket close event.
|
|
267
|
+
*
|
|
268
|
+
* @param raw - The close event's code.
|
|
269
|
+
* @returns The PopSub close code, or `null` for any other code, such as
|
|
270
|
+
* 1006 when the connection dropped without a close frame.
|
|
271
|
+
*/
|
|
272
|
+
export declare function closeCode(raw: number): CloseCode | null;
|
|
273
|
+
/**
|
|
274
|
+
* Whether a close code allows reconnecting, by the table in
|
|
275
|
+
* `docs/protocol/close-codes.json`. The client adds one rule on top: after
|
|
276
|
+
* `TokenExpired` it reconnects only with password credentials.
|
|
277
|
+
*
|
|
278
|
+
* @param code - The close code.
|
|
279
|
+
* @returns False for `AuthFailed`, `SessionTakenOver`,
|
|
280
|
+
* `UnsupportedProtocol` and `AccountDisabled`, true otherwise.
|
|
281
|
+
*/
|
|
282
|
+
export declare function shouldReconnect(code: CloseCode): boolean;
|
|
283
|
+
/**
|
|
284
|
+
* Parses a text frame from the server.
|
|
285
|
+
*
|
|
286
|
+
* Frames that aren't JSON objects, have a `type` this client doesn't know,
|
|
287
|
+
* or miss a field the client reads are ignored, as the protocol asks of
|
|
288
|
+
* clients for frame types added later.
|
|
289
|
+
*
|
|
290
|
+
* @param text - The frame's text.
|
|
291
|
+
* @returns The frame, or `null` when it should be ignored.
|
|
292
|
+
*/
|
|
293
|
+
export declare function parseServerFrame(text: string): ServerFrame | null;
|
|
294
|
+
/**
|
|
295
|
+
* Builds a QoS 0 `publish` frame. A text publish carries no `encoding`
|
|
296
|
+
* field at all, which is what a server reads as text.
|
|
297
|
+
*
|
|
298
|
+
* @param topic - The topic.
|
|
299
|
+
* @param payload - The body: text, or base64 when `encoding` says so.
|
|
300
|
+
* @param encoding - How `payload` encodes the body, or `null` for text.
|
|
301
|
+
* @returns The frame.
|
|
302
|
+
*/
|
|
303
|
+
export declare function publishFrame(topic: string, payload: string, encoding: Encoding | null): Extract<ClientMessage, {
|
|
304
|
+
type: "publish";
|
|
305
|
+
}>;
|
|
306
|
+
/**
|
|
307
|
+
* Writes a frame as JSON text.
|
|
308
|
+
*
|
|
309
|
+
* @param message - The frame.
|
|
310
|
+
* @param id - The request id, when a reply is expected.
|
|
311
|
+
* @returns The JSON text.
|
|
312
|
+
*/
|
|
313
|
+
export declare function encodeFrame(message: ClientMessage, id?: RequestId): string;
|
package/dist/protocol.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The PopSub wire protocol, version 1: the JSON frames a client sends and
|
|
3
|
+
* receives, the error codes and the WebSocket close codes, as described in
|
|
4
|
+
* `docs/DESIGN.md` section 4 and `docs/protocol/*.json`.
|
|
5
|
+
*
|
|
6
|
+
* These types keep the protocol's own snake_case field names. The public
|
|
7
|
+
* API converts them to camelCase at its edges.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
/** The protocol version this client speaks, sent in every `auth`. */
|
|
12
|
+
export const PROTOCOL_VERSION = 1;
|
|
13
|
+
/** Every error code protocol version 1 defines. */
|
|
14
|
+
export const ERROR_CODES = [
|
|
15
|
+
"bad_request",
|
|
16
|
+
"unauthenticated",
|
|
17
|
+
"auth_failed",
|
|
18
|
+
"invalid_credentials",
|
|
19
|
+
"locked_out",
|
|
20
|
+
"rate_limited",
|
|
21
|
+
"not_authorized",
|
|
22
|
+
"registration_failed",
|
|
23
|
+
"invalid_session_id",
|
|
24
|
+
"session_unavailable",
|
|
25
|
+
"unsupported_protocol",
|
|
26
|
+
"storage_unavailable",
|
|
27
|
+
"payload_too_large",
|
|
28
|
+
"invalid_topic",
|
|
29
|
+
"forbidden",
|
|
30
|
+
"user_not_found",
|
|
31
|
+
"user_exists",
|
|
32
|
+
"topic_not_found",
|
|
33
|
+
"internal",
|
|
34
|
+
];
|
|
35
|
+
/** The codes `errorCode` recognises, for fast lookup. */
|
|
36
|
+
const KNOWN_ERROR_CODES = new Set(ERROR_CODES);
|
|
37
|
+
/**
|
|
38
|
+
* Reads an error code from a frame. The protocol says to treat a code this
|
|
39
|
+
* client doesn't know like `internal`.
|
|
40
|
+
*
|
|
41
|
+
* @param raw - The `code` field as received.
|
|
42
|
+
* @returns The code, or `internal` when it is unknown.
|
|
43
|
+
*/
|
|
44
|
+
export function errorCode(raw) {
|
|
45
|
+
return typeof raw === "string" && KNOWN_ERROR_CODES.has(raw) ? raw : "internal";
|
|
46
|
+
}
|
|
47
|
+
/** The WebSocket close codes a PopSub server sends, by name. */
|
|
48
|
+
export const CloseCode = {
|
|
49
|
+
GoingAway: 1001,
|
|
50
|
+
MessageTooBig: 1009,
|
|
51
|
+
InternalError: 1011,
|
|
52
|
+
TryAgainLater: 1013,
|
|
53
|
+
AuthFailed: 4001,
|
|
54
|
+
Unauthenticated: 4002,
|
|
55
|
+
SessionTakenOver: 4003,
|
|
56
|
+
TokenExpired: 4004,
|
|
57
|
+
IdleTimeout: 4005,
|
|
58
|
+
UnsupportedProtocol: 4006,
|
|
59
|
+
SessionUnavailable: 4007,
|
|
60
|
+
SlowConsumer: 4008,
|
|
61
|
+
AccountDisabled: 4009,
|
|
62
|
+
};
|
|
63
|
+
/** The close codes `closeCode` recognises. */
|
|
64
|
+
const KNOWN_CLOSE_CODES = new Set(Object.values(CloseCode));
|
|
65
|
+
/** The close codes after which the client never reconnects on its own. */
|
|
66
|
+
const FINAL_CLOSE_CODES = new Set([
|
|
67
|
+
CloseCode.AuthFailed,
|
|
68
|
+
CloseCode.SessionTakenOver,
|
|
69
|
+
CloseCode.UnsupportedProtocol,
|
|
70
|
+
CloseCode.AccountDisabled,
|
|
71
|
+
]);
|
|
72
|
+
/**
|
|
73
|
+
* Reads a close code from a WebSocket close event.
|
|
74
|
+
*
|
|
75
|
+
* @param raw - The close event's code.
|
|
76
|
+
* @returns The PopSub close code, or `null` for any other code, such as
|
|
77
|
+
* 1006 when the connection dropped without a close frame.
|
|
78
|
+
*/
|
|
79
|
+
export function closeCode(raw) {
|
|
80
|
+
return KNOWN_CLOSE_CODES.has(raw) ? raw : null;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Whether a close code allows reconnecting, by the table in
|
|
84
|
+
* `docs/protocol/close-codes.json`. The client adds one rule on top: after
|
|
85
|
+
* `TokenExpired` it reconnects only with password credentials.
|
|
86
|
+
*
|
|
87
|
+
* @param code - The close code.
|
|
88
|
+
* @returns False for `AuthFailed`, `SessionTakenOver`,
|
|
89
|
+
* `UnsupportedProtocol` and `AccountDisabled`, true otherwise.
|
|
90
|
+
*/
|
|
91
|
+
export function shouldReconnect(code) {
|
|
92
|
+
return !FINAL_CLOSE_CODES.has(code);
|
|
93
|
+
}
|
|
94
|
+
/** The fields each known server frame type must have, with their types. */
|
|
95
|
+
const REQUIRED_FIELDS = {
|
|
96
|
+
registered: {},
|
|
97
|
+
logged_in: { token: "string" },
|
|
98
|
+
authenticated: { limits: "object" },
|
|
99
|
+
subscribed: { topic: "string", replay_from: "number" },
|
|
100
|
+
unsubscribed: { topic: "string" },
|
|
101
|
+
published: { message_id: "string", seq: "number" },
|
|
102
|
+
message: { topic: "string", seq: "number", message_id: "string", payload: "string", qos: "number" },
|
|
103
|
+
truncated: { topic: "string", first_available: "number" },
|
|
104
|
+
stalled: { topic: "string", seq: "number" },
|
|
105
|
+
auth_expiring: { expires_at_ms: "number" },
|
|
106
|
+
user: { user: "object" },
|
|
107
|
+
users: { users: "object" },
|
|
108
|
+
user_deleted: { username: "string" },
|
|
109
|
+
pong: {},
|
|
110
|
+
topic_info: { topic: "object" },
|
|
111
|
+
topics: { topics: "object" },
|
|
112
|
+
error: { code: "string", message: "string" },
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* Parses a text frame from the server.
|
|
116
|
+
*
|
|
117
|
+
* Frames that aren't JSON objects, have a `type` this client doesn't know,
|
|
118
|
+
* or miss a field the client reads are ignored, as the protocol asks of
|
|
119
|
+
* clients for frame types added later.
|
|
120
|
+
*
|
|
121
|
+
* @param text - The frame's text.
|
|
122
|
+
* @returns The frame, or `null` when it should be ignored.
|
|
123
|
+
*/
|
|
124
|
+
export function parseServerFrame(text) {
|
|
125
|
+
let value;
|
|
126
|
+
try {
|
|
127
|
+
value = JSON.parse(text);
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
const record = value;
|
|
136
|
+
const type = record.type;
|
|
137
|
+
if (typeof type !== "string" || !Object.hasOwn(REQUIRED_FIELDS, type)) {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
const required = REQUIRED_FIELDS[type];
|
|
141
|
+
for (const [field, kind] of Object.entries(required)) {
|
|
142
|
+
const present = record[field];
|
|
143
|
+
if (typeof present !== kind || present === null) {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return record;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Builds a QoS 0 `publish` frame. A text publish carries no `encoding`
|
|
151
|
+
* field at all, which is what a server reads as text.
|
|
152
|
+
*
|
|
153
|
+
* @param topic - The topic.
|
|
154
|
+
* @param payload - The body: text, or base64 when `encoding` says so.
|
|
155
|
+
* @param encoding - How `payload` encodes the body, or `null` for text.
|
|
156
|
+
* @returns The frame.
|
|
157
|
+
*/
|
|
158
|
+
export function publishFrame(topic, payload, encoding) {
|
|
159
|
+
return encoding === null
|
|
160
|
+
? { type: "publish", topic, payload }
|
|
161
|
+
: { type: "publish", topic, payload, encoding };
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Writes a frame as JSON text.
|
|
165
|
+
*
|
|
166
|
+
* @param message - The frame.
|
|
167
|
+
* @param id - The request id, when a reply is expected.
|
|
168
|
+
* @returns The JSON text.
|
|
169
|
+
*/
|
|
170
|
+
export function encodeFrame(message, id) {
|
|
171
|
+
return JSON.stringify(id === undefined ? message : { ...message, id });
|
|
172
|
+
}
|
|
173
|
+
//# sourceMappingURL=protocol.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol.js","sourceRoot":"","sources":["../src/protocol.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,qEAAqE;AACrE,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC;AA6JlC,mDAAmD;AACnD,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,aAAa;IACb,iBAAiB;IACjB,aAAa;IACb,qBAAqB;IACrB,YAAY;IACZ,cAAc;IACd,gBAAgB;IAChB,qBAAqB;IACrB,oBAAoB;IACpB,qBAAqB;IACrB,sBAAsB;IACtB,qBAAqB;IACrB,mBAAmB;IACnB,eAAe;IACf,WAAW;IACX,gBAAgB;IAChB,aAAa;IACb,iBAAiB;IACjB,UAAU;CACF,CAAC;AAKX,yDAAyD;AACzD,MAAM,iBAAiB,GAAwB,IAAI,GAAG,CAAC,WAAW,CAAC,CAAC;AAEpE;;;;;;GAMG;AACH,MAAM,UAAU,SAAS,CAAC,GAAY;IACpC,OAAO,OAAO,GAAG,KAAK,QAAQ,IAAI,iBAAiB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAE,GAAiB,CAAC,CAAC,CAAC,UAAU,CAAC;AACjG,CAAC;AAED,gEAAgE;AAChE,MAAM,CAAC,MAAM,SAAS,GAAG;IACvB,SAAS,EAAE,IAAI;IACf,aAAa,EAAE,IAAI;IACnB,aAAa,EAAE,IAAI;IACnB,aAAa,EAAE,IAAI;IACnB,UAAU,EAAE,IAAI;IAChB,eAAe,EAAE,IAAI;IACrB,gBAAgB,EAAE,IAAI;I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AC,IAA6B,CAAC,CAAC;IAChE,KAAK,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;QACrD,MAAM,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;QAC9B,IAAI,OAAO,OAAO,KAAK,IAAI,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;YAChD,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IACD,OAAO,MAAgC,CAAC;AAC1C,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,YAAY,CAC1B,KAAa,EACb,OAAe,EACf,QAAyB;IAEzB,OAAO,QAAQ,KAAK,IAAI;QACtB,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,OAAO,EAAE;QACrC,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC;AACpD,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,OAAsB,EAAE,EAAc;IAChE,OAAO,IAAI,CAAC,SAAS,CAAC,EAAE,KAAK,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC;AACzE,CAAC","sourcesContent":["/**\n * The PopSub wire protocol, version 1: the JSON frames a client sends and\n * receives, the error codes and the WebSocket close codes, as described in\n * `docs/DESIGN.md` section 4 and `docs/protocol/*.json`.\n *\n * These types keep the protocol's own snake_case field names. The public\n * API converts them to camelCase at its edges.\n *\n * @module\n */\n\n/** The protocol version this client speaks, sent in every `auth`. */\nexport const PROTOCOL_VERSION = 1;\n\n/** A request id. This client only ever sends numbers. */\nexport type RequestId = number | string;\n\n/** Where replay starts when subscribing, as it is written on the wire. */\nexport type WireStartFrom = \"resume\" | \"new\" | { last: number } | { since_ms: number } | { seq: number };\n\n/**\n * How a payload string encodes a message's bytes when it isn't text.\n * `base64` is standard padded base64; a frame without an encoding carries text.\n */\nexport type Encoding = \"base64\";\n\n/** What a topic grant allows. */\nexport type Access = \"subscribe\" | \"publish\" | \"both\";\n\n/** The kind of account. */\nexport type RoleKind = \"admin\" | \"user\" | \"readonly\";\n\n/** One topic pattern an account may use, and how. */\nexport interface TopicGrant {\n /** The topic pattern, such as `orders/#`. */\n pattern: string;\n /** What the pattern allows. */\n access: Access;\n}\n\n/** What an account may do. `topics` is ignored for admins. */\nexport interface RoleSpec {\n /** The kind of account. */\n kind: RoleKind;\n /** The account's topic grants. */\n topics?: TopicGrant[];\n}\n\n/** An account as the server describes it in `user` and `users` frames. */\nexport interface WireUser {\n /** The username. */\n username: string;\n /** What it may do. */\n role: RoleSpec;\n /** Whether it may log in and use topics. */\n enabled: boolean;\n /** When it was created, in milliseconds since the Unix epoch. */\n created_at_ms: number;\n}\n\n/** A topic as the server describes it in `topic_info` and `topics` frames. */\nexport interface WireTopicInfo {\n /** The topic's name. */\n name: string;\n /** The oldest `seq` still kept, or `last_seq + 1` when nothing is kept. */\n first_seq: number;\n /** The newest `seq` ever published. */\n last_seq: number;\n /** How many records are kept now. */\n messages: number;\n /** How many live subscriptions it has. */\n subscribers: number;\n}\n\n/** The admin requests, which are never queued or resent. */\nexport type AdminRequest =\n | { type: \"create_user\"; username: string; password: string; role: RoleSpec }\n | { type: \"get_user\"; username: string }\n | { type: \"list_users\" }\n | { type: \"set_role\"; username: string; role: RoleSpec }\n | { type: \"grant\"; username: string; pattern: string; access: Access }\n | { type: \"revoke\"; username: string; pattern: string }\n | { type: \"set_password\"; username: string; password: string }\n | { type: \"set_enabled\"; username: string; enabled: boolean }\n | { type: \"delete_user\"; username: string }\n | { type: \"list_topics\" }\n | { type: \"get_topic\"; topic: string }\n | { type: \"purge_topic\"; topic: string };\n\n/** Every frame this client sends, without its request id. */\nexport type ClientMessage =\n | { type: \"login\"; username: string; password: string }\n | {\n type: \"auth\";\n token: string;\n protocol?: number;\n session_id?: string;\n clean_start?: boolean;\n hold_resumed?: boolean;\n }\n | { type: \"subscribe\"; topic: string; from?: WireStartFrom; group?: string }\n | { type: \"unsubscribe\"; topic: string }\n | {\n type: \"publish\";\n topic: string;\n payload: string;\n encoding?: Encoding;\n message_id?: string;\n qos?: 0 | 1;\n }\n | { type: \"ack\"; topic: string; seq: number }\n | { type: \"ping\" }\n | AdminRequest;\n\n/** The limits a server announces in `authenticated`. */\nexport interface Limits {\n /** How many QoS 1 records each subscription may have unacknowledged. */\n max_inflight: number;\n /** How long an untouched durable session is kept, in seconds. */\n session_expiry_secs: number;\n /** How often the server pings, in seconds. */\n heartbeat_secs: number;\n /** How long the server waits for anything from the client, in seconds. */\n idle_timeout_secs: number;\n /** The largest payload the server accepts, in bytes. */\n max_payload_bytes: number;\n /** The longest topic name, in bytes. */\n max_topic_bytes: number;\n}\n\n/** Every frame the server sends that this client reads, without its id. */\nexport type ServerMessage =\n | { type: \"registered\" }\n | { type: \"logged_in\"; token: string }\n | {\n type: \"authenticated\";\n protocol: number;\n limits: Limits;\n session_id?: string | null;\n resumed?: string[];\n held?: boolean;\n }\n | { type: \"subscribed\"; topic: string; replay_from: number; replay_to: number }\n | { type: \"unsubscribed\"; topic: string }\n | { type: \"published\"; message_id: string; seq: number }\n | {\n type: \"message\";\n topic: string;\n seq: number;\n message_id: string;\n payload: string;\n encoding?: Encoding | null;\n qos: number;\n published_at_ms: number;\n }\n | { type: \"truncated\"; topic: string; first_available: number }\n | { type: \"stalled\"; topic: string; seq: number }\n | { type: \"auth_expiring\"; expires_at_ms: number }\n | { type: \"user\"; user: WireUser }\n | { type: \"users\"; users: WireUser[] }\n | { type: \"user_deleted\"; username: string }\n | { type: \"pong\" }\n | { type: \"topic_info\"; topic: WireTopicInfo }\n | { type: \"topics\"; topics: WireTopicInfo[] }\n | { type: \"error\"; code: string; message: string; retry_after?: number | null };\n\n/** A server frame: a message plus the id of the request it answers, if any. */\nexport type ServerFrame = ServerMessage & { id?: RequestId | null };\n\n/** Every error code protocol version 1 defines. */\nexport const ERROR_CODES = [\n \"bad_request\",\n \"unauthenticated\",\n \"auth_failed\",\n \"invalid_credentials\",\n \"locked_out\",\n \"rate_limited\",\n \"not_authorized\",\n \"registration_failed\",\n \"invalid_session_id\",\n \"session_unavailable\",\n \"unsupported_protocol\",\n \"storage_unavailable\",\n \"payload_too_large\",\n \"invalid_topic\",\n \"forbidden\",\n \"user_not_found\",\n \"user_exists\",\n \"topic_not_found\",\n \"internal\",\n] as const;\n\n/** Why the server refused a request. */\nexport type ErrorCode = (typeof ERROR_CODES)[number];\n\n/** The codes `errorCode` recognises, for fast lookup. */\nconst KNOWN_ERROR_CODES: ReadonlySet<string> = new Set(ERROR_CODES);\n\n/**\n * Reads an error code from a frame. The protocol says to treat a code this\n * client doesn't know like `internal`.\n *\n * @param raw - The `code` field as received.\n * @returns The code, or `internal` when it is unknown.\n */\nexport function errorCode(raw: unknown): ErrorCode {\n return typeof raw === \"string\" && KNOWN_ERROR_CODES.has(raw) ? (raw as ErrorCode) : \"internal\";\n}\n\n/** The WebSocket close codes a PopSub server sends, by name. */\nexport const CloseCode = {\n GoingAway: 1001,\n MessageTooBig: 1009,\n InternalError: 1011,\n TryAgainLater: 1013,\n AuthFailed: 4001,\n Unauthenticated: 4002,\n SessionTakenOver: 4003,\n TokenExpired: 4004,\n IdleTimeout: 4005,\n UnsupportedProtocol: 4006,\n SessionUnavailable: 4007,\n SlowConsumer: 4008,\n AccountDisabled: 4009,\n} as const;\n\n/** One of the close codes a PopSub server sends. */\nexport type CloseCode = (typeof CloseCode)[keyof typeof CloseCode];\n\n/** The close codes `closeCode` recognises. */\nconst KNOWN_CLOSE_CODES: ReadonlySet<number> = new Set(Object.values(CloseCode));\n\n/** The close codes after which the client never reconnects on its own. */\nconst FINAL_CLOSE_CODES: ReadonlySet<number> = new Set([\n CloseCode.AuthFailed,\n CloseCode.SessionTakenOver,\n CloseCode.UnsupportedProtocol,\n CloseCode.AccountDisabled,\n]);\n\n/**\n * Reads a close code from a WebSocket close event.\n *\n * @param raw - The close event's code.\n * @returns The PopSub close code, or `null` for any other code, such as\n * 1006 when the connection dropped without a close frame.\n */\nexport function closeCode(raw: number): CloseCode | null {\n return KNOWN_CLOSE_CODES.has(raw) ? (raw as CloseCode) : null;\n}\n\n/**\n * Whether a close code allows reconnecting, by the table in\n * `docs/protocol/close-codes.json`. The client adds one rule on top: after\n * `TokenExpired` it reconnects only with password credentials.\n *\n * @param code - The close code.\n * @returns False for `AuthFailed`, `SessionTakenOver`,\n * `UnsupportedProtocol` and `AccountDisabled`, true otherwise.\n */\nexport function shouldReconnect(code: CloseCode): boolean {\n return !FINAL_CLOSE_CODES.has(code);\n}\n\n/** The fields each known server frame type must have, with their types. */\nconst REQUIRED_FIELDS: Readonly<Record<ServerMessage[\"type\"], Readonly<Record<string, string>>>> = {\n registered: {},\n logged_in: { token: \"string\" },\n authenticated: { limits: \"object\" },\n subscribed: { topic: \"string\", replay_from: \"number\" },\n unsubscribed: { topic: \"string\" },\n published: { message_id: \"string\", seq: \"number\" },\n message: { topic: \"string\", seq: \"number\", message_id: \"string\", payload: \"string\", qos: \"number\" },\n truncated: { topic: \"string\", first_available: \"number\" },\n stalled: { topic: \"string\", seq: \"number\" },\n auth_expiring: { expires_at_ms: \"number\" },\n user: { user: \"object\" },\n users: { users: \"object\" },\n user_deleted: { username: \"string\" },\n pong: {},\n topic_info: { topic: \"object\" },\n topics: { topics: \"object\" },\n error: { code: \"string\", message: \"string\" },\n};\n\n/**\n * Parses a text frame from the server.\n *\n * Frames that aren't JSON objects, have a `type` this client doesn't know,\n * or miss a field the client reads are ignored, as the protocol asks of\n * clients for frame types added later.\n *\n * @param text - The frame's text.\n * @returns The frame, or `null` when it should be ignored.\n */\nexport function parseServerFrame(text: string): ServerFrame | null {\n let value: unknown;\n try {\n value = JSON.parse(text);\n } catch {\n return null;\n }\n if (typeof value !== \"object\" || value === null || Array.isArray(value)) {\n return null;\n }\n const record = value as Record<string, unknown>;\n const type = record.type;\n if (typeof type !== \"string\" || !Object.hasOwn(REQUIRED_FIELDS, type)) {\n return null;\n }\n const required = REQUIRED_FIELDS[type as ServerMessage[\"type\"]];\n for (const [field, kind] of Object.entries(required)) {\n const present = record[field];\n if (typeof present !== kind || present === null) {\n return null;\n }\n }\n return record as unknown as ServerFrame;\n}\n\n/**\n * Builds a QoS 0 `publish` frame. A text publish carries no `encoding`\n * field at all, which is what a server reads as text.\n *\n * @param topic - The topic.\n * @param payload - The body: text, or base64 when `encoding` says so.\n * @param encoding - How `payload` encodes the body, or `null` for text.\n * @returns The frame.\n */\nexport function publishFrame(\n topic: string,\n payload: string,\n encoding: Encoding | null,\n): Extract<ClientMessage, { type: \"publish\" }> {\n return encoding === null\n ? { type: \"publish\", topic, payload }\n : { type: \"publish\", topic, payload, encoding };\n}\n\n/**\n * Writes a frame as JSON text.\n *\n * @param message - The frame.\n * @param id - The request id, when a reply is expected.\n * @returns The JSON text.\n */\nexport function encodeFrame(message: ClientMessage, id?: RequestId): string {\n return JSON.stringify(id === undefined ? message : { ...message, id });\n}\n"]}
|