plsremind 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 +21 -0
- package/README.md +277 -2
- package/dist/cache.d.ts +54 -0
- package/dist/cache.js +287 -0
- package/dist/client.d.ts +39 -0
- package/dist/client.js +199 -0
- package/dist/convert.d.ts +8 -0
- package/dist/convert.js +123 -0
- package/dist/duration.d.ts +4 -0
- package/dist/duration.js +35 -0
- package/dist/emitter.d.ts +10 -0
- package/dist/emitter.js +39 -0
- package/dist/errors.d.ts +21 -0
- package/dist/errors.js +31 -0
- package/dist/events.d.ts +152 -0
- package/dist/events.js +21 -0
- package/dist/gateway.d.ts +34 -0
- package/dist/gateway.js +283 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +4 -0
- package/dist/optimistic/index.d.ts +51 -0
- package/dist/optimistic/index.js +540 -0
- package/dist/rest.d.ts +212 -0
- package/dist/rest.js +394 -0
- package/dist/types.d.ts +307 -0
- package/dist/types.js +1 -0
- package/package.json +58 -4
package/dist/gateway.js
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { GatewayError } from "./errors.js";
|
|
2
|
+
const opDispatch = 0;
|
|
3
|
+
const opHeartbeat = 1;
|
|
4
|
+
const opIdentify = 2;
|
|
5
|
+
const opResume = 6;
|
|
6
|
+
const opReconnect = 7;
|
|
7
|
+
const opInvalidSession = 9;
|
|
8
|
+
const opHello = 10;
|
|
9
|
+
const opHeartbeatAck = 11;
|
|
10
|
+
const closeNormal = 1000;
|
|
11
|
+
const closeResuming = 4000;
|
|
12
|
+
const closeBadSequence = 4007;
|
|
13
|
+
const closeTooMany = 4008;
|
|
14
|
+
const fatal = new Set([4004, 4013, 4014]);
|
|
15
|
+
const firstWait = 1000;
|
|
16
|
+
const longestWait = 60_000;
|
|
17
|
+
const waitAfterTooMany = 5000;
|
|
18
|
+
const silenceWhileLoggingIn = 15_000;
|
|
19
|
+
const answerProbeWithin = 5000;
|
|
20
|
+
/** One gateway session kept alive across drops. It knows nothing about what the events mean. */
|
|
21
|
+
export class Gateway {
|
|
22
|
+
#options;
|
|
23
|
+
#running = false;
|
|
24
|
+
#generation = 0;
|
|
25
|
+
#address;
|
|
26
|
+
#socket;
|
|
27
|
+
#session;
|
|
28
|
+
#seq = null;
|
|
29
|
+
#established = false;
|
|
30
|
+
#attempts = 0;
|
|
31
|
+
#acked = true;
|
|
32
|
+
#heartbeat;
|
|
33
|
+
#wait;
|
|
34
|
+
#login;
|
|
35
|
+
#probe;
|
|
36
|
+
#online = false;
|
|
37
|
+
#greeted = false;
|
|
38
|
+
constructor(options) {
|
|
39
|
+
this.#options = options;
|
|
40
|
+
}
|
|
41
|
+
start() {
|
|
42
|
+
if (this.#running)
|
|
43
|
+
return;
|
|
44
|
+
this.#running = true;
|
|
45
|
+
this.#options.onStatus("connecting");
|
|
46
|
+
void this.#open();
|
|
47
|
+
}
|
|
48
|
+
stop() {
|
|
49
|
+
this.#running = false;
|
|
50
|
+
this.#generation++;
|
|
51
|
+
this.#release(closeNormal);
|
|
52
|
+
clearTimeout(this.#wait);
|
|
53
|
+
this.#wait = undefined;
|
|
54
|
+
this.#session = undefined;
|
|
55
|
+
this.#seq = null;
|
|
56
|
+
this.#established = false;
|
|
57
|
+
this.#online = false;
|
|
58
|
+
this.#attempts = 0;
|
|
59
|
+
}
|
|
60
|
+
/** Connects now instead of sitting out the rest of a wait. */
|
|
61
|
+
wake() {
|
|
62
|
+
if (!this.#running)
|
|
63
|
+
return;
|
|
64
|
+
if (this.#wait !== undefined) {
|
|
65
|
+
clearTimeout(this.#wait);
|
|
66
|
+
this.#wait = undefined;
|
|
67
|
+
void this.#open();
|
|
68
|
+
}
|
|
69
|
+
else if (this.#login !== undefined && !this.#greeted && this.#established) {
|
|
70
|
+
this.#release(closeResuming);
|
|
71
|
+
void this.#open();
|
|
72
|
+
}
|
|
73
|
+
else if (this.#socket && this.#login === undefined && this.#probe === undefined) {
|
|
74
|
+
// After sleep or a network change the socket may be dead without a close event.
|
|
75
|
+
const socket = this.#socket;
|
|
76
|
+
this.#acked = false;
|
|
77
|
+
this.#send(socket, opHeartbeat, this.#seq);
|
|
78
|
+
this.#probe = setTimeout(() => {
|
|
79
|
+
this.#probe = undefined;
|
|
80
|
+
if (this.#socket !== socket || this.#acked)
|
|
81
|
+
return;
|
|
82
|
+
this.#release(closeResuming);
|
|
83
|
+
this.#lost(undefined, "The hub stopped answering heartbeats.");
|
|
84
|
+
}, answerProbeWithin);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
async #open() {
|
|
88
|
+
if (!this.#running)
|
|
89
|
+
return;
|
|
90
|
+
const generation = ++this.#generation;
|
|
91
|
+
let socket;
|
|
92
|
+
try {
|
|
93
|
+
this.#address ??= await this.#options.url();
|
|
94
|
+
if (generation !== this.#generation)
|
|
95
|
+
return;
|
|
96
|
+
socket = this.#options.createSocket(this.#address);
|
|
97
|
+
}
|
|
98
|
+
catch (cause) {
|
|
99
|
+
if (generation !== this.#generation)
|
|
100
|
+
return;
|
|
101
|
+
const why = cause instanceof Error ? cause.message : String(cause);
|
|
102
|
+
this.#lost(undefined, `The gateway could not be reached: ${why}`, cause);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
this.#socket = socket;
|
|
106
|
+
this.#greeted = false;
|
|
107
|
+
this.#expectLogin(socket);
|
|
108
|
+
socket.onmessage = (event) => {
|
|
109
|
+
if (this.#socket === socket)
|
|
110
|
+
this.#receive(socket, event.data);
|
|
111
|
+
};
|
|
112
|
+
socket.onclose = (event) => {
|
|
113
|
+
if (this.#socket !== socket)
|
|
114
|
+
return;
|
|
115
|
+
this.#release();
|
|
116
|
+
this.#lost(event.code, event.reason);
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
#release(code) {
|
|
120
|
+
clearTimeout(this.#heartbeat);
|
|
121
|
+
clearTimeout(this.#login);
|
|
122
|
+
clearTimeout(this.#probe);
|
|
123
|
+
this.#heartbeat = this.#login = this.#probe = undefined;
|
|
124
|
+
const socket = this.#socket;
|
|
125
|
+
this.#socket = undefined;
|
|
126
|
+
if (!socket)
|
|
127
|
+
return;
|
|
128
|
+
socket.onopen = socket.onmessage = socket.onclose = socket.onerror = null;
|
|
129
|
+
if (code !== undefined) {
|
|
130
|
+
try {
|
|
131
|
+
socket.close(code);
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
// already closed
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
#lost(code, reason, cause) {
|
|
139
|
+
if (code !== undefined && fatal.has(code)) {
|
|
140
|
+
this.#end(new GatewayError(reason || `The gateway closed with ${code}.`, code));
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (!this.#established) {
|
|
144
|
+
const message = reason || "The gateway connection could not be established.";
|
|
145
|
+
this.#end(new GatewayError(message, code, cause));
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (code === closeBadSequence)
|
|
149
|
+
this.#forget();
|
|
150
|
+
if (!this.#disconnected(code, reason))
|
|
151
|
+
return;
|
|
152
|
+
const step = Math.min(longestWait, firstWait * 2 ** this.#attempts);
|
|
153
|
+
this.#attempts++;
|
|
154
|
+
let wait = step * (0.5 + 0.5 * this.#options.random());
|
|
155
|
+
if (code === closeTooMany)
|
|
156
|
+
wait = Math.max(wait, waitAfterTooMany);
|
|
157
|
+
this.#wait = setTimeout(() => {
|
|
158
|
+
this.#wait = undefined;
|
|
159
|
+
void this.#open();
|
|
160
|
+
}, wait);
|
|
161
|
+
}
|
|
162
|
+
/** Reports the start of an outage. False if the callback stopped the gateway. */
|
|
163
|
+
#disconnected(code, reason) {
|
|
164
|
+
if (!this.#online)
|
|
165
|
+
return true;
|
|
166
|
+
this.#online = false;
|
|
167
|
+
const generation = this.#generation;
|
|
168
|
+
this.#options.onStatus("reconnecting");
|
|
169
|
+
this.#options.onDisconnected(code, reason);
|
|
170
|
+
return this.#running && generation === this.#generation;
|
|
171
|
+
}
|
|
172
|
+
// A socket that goes quiet before the session stands would otherwise be waited on forever.
|
|
173
|
+
#expectLogin(socket) {
|
|
174
|
+
clearTimeout(this.#login);
|
|
175
|
+
this.#login = setTimeout(() => {
|
|
176
|
+
this.#login = undefined;
|
|
177
|
+
if (this.#socket !== socket)
|
|
178
|
+
return;
|
|
179
|
+
this.#release(closeResuming);
|
|
180
|
+
this.#lost(undefined, "The hub did not finish the handshake.");
|
|
181
|
+
}, silenceWhileLoggingIn);
|
|
182
|
+
}
|
|
183
|
+
#end(error) {
|
|
184
|
+
this.stop();
|
|
185
|
+
this.#options.onClosed(error);
|
|
186
|
+
}
|
|
187
|
+
#forget() {
|
|
188
|
+
this.#session = undefined;
|
|
189
|
+
this.#seq = null;
|
|
190
|
+
}
|
|
191
|
+
#receive(socket, raw) {
|
|
192
|
+
let frame;
|
|
193
|
+
try {
|
|
194
|
+
frame = JSON.parse(String(raw));
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (frame === null || typeof frame !== "object")
|
|
200
|
+
return;
|
|
201
|
+
// An answered heartbeat says nothing about a login that is still due.
|
|
202
|
+
if (this.#login !== undefined && frame.op !== opHeartbeatAck)
|
|
203
|
+
this.#expectLogin(socket);
|
|
204
|
+
switch (frame.op) {
|
|
205
|
+
case opHello:
|
|
206
|
+
this.#greeted = true;
|
|
207
|
+
this.#beat(socket, frame.d.heartbeat_interval);
|
|
208
|
+
this.#identify(socket);
|
|
209
|
+
break;
|
|
210
|
+
case opHeartbeatAck:
|
|
211
|
+
this.#acked = true;
|
|
212
|
+
break;
|
|
213
|
+
case opReconnect:
|
|
214
|
+
this.#release(closeResuming);
|
|
215
|
+
if (this.#disconnected(undefined, "The hub asked to reconnect."))
|
|
216
|
+
void this.#open();
|
|
217
|
+
break;
|
|
218
|
+
case opInvalidSession:
|
|
219
|
+
this.#forget();
|
|
220
|
+
this.#expectLogin(socket);
|
|
221
|
+
this.#identify(socket);
|
|
222
|
+
break;
|
|
223
|
+
case opDispatch:
|
|
224
|
+
if (typeof frame.t === "string")
|
|
225
|
+
this.#dispatch(frame.t, frame.d, frame.s);
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
#dispatch(name, data, seq) {
|
|
230
|
+
if (typeof seq === "number")
|
|
231
|
+
this.#seq = seq;
|
|
232
|
+
if (name === "READY") {
|
|
233
|
+
this.#session = data.session_id;
|
|
234
|
+
}
|
|
235
|
+
if (name === "READY" || name === "RESUMED") {
|
|
236
|
+
clearTimeout(this.#login);
|
|
237
|
+
this.#login = undefined;
|
|
238
|
+
this.#established = true;
|
|
239
|
+
this.#online = true;
|
|
240
|
+
this.#attempts = 0;
|
|
241
|
+
this.#options.onStatus("connected");
|
|
242
|
+
}
|
|
243
|
+
this.#options.onDispatch(name, data);
|
|
244
|
+
}
|
|
245
|
+
#identify(socket) {
|
|
246
|
+
const token = this.#options.token === undefined ? {} : { token: this.#options.token };
|
|
247
|
+
if (this.#session === undefined) {
|
|
248
|
+
this.#send(socket, opIdentify, { ...token, intents: this.#options.intents });
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
this.#send(socket, opResume, { ...token, session_id: this.#session, seq: this.#seq });
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
#beat(socket, interval) {
|
|
255
|
+
clearTimeout(this.#heartbeat);
|
|
256
|
+
this.#acked = true;
|
|
257
|
+
const beat = () => {
|
|
258
|
+
if (this.#socket !== socket)
|
|
259
|
+
return;
|
|
260
|
+
if (this.#probe !== undefined) {
|
|
261
|
+
this.#heartbeat = setTimeout(beat, interval);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
if (!this.#acked) {
|
|
265
|
+
this.#release(closeResuming);
|
|
266
|
+
this.#lost(undefined, "The hub stopped answering heartbeats.");
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
this.#acked = false;
|
|
270
|
+
this.#send(socket, opHeartbeat, this.#seq);
|
|
271
|
+
this.#heartbeat = setTimeout(beat, interval);
|
|
272
|
+
};
|
|
273
|
+
this.#heartbeat = setTimeout(beat, interval * this.#options.random());
|
|
274
|
+
}
|
|
275
|
+
#send(socket, op, d) {
|
|
276
|
+
try {
|
|
277
|
+
socket.send(JSON.stringify({ op, d }));
|
|
278
|
+
}
|
|
279
|
+
catch {
|
|
280
|
+
// The close that follows a dead socket starts the reconnect.
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { formatDuration, parseDuration } from "./duration.js";
|
|
2
|
+
export type * from "./types.js";
|
|
3
|
+
export { ApiError, GatewayError, NetworkError } from "./errors.js";
|
|
4
|
+
export { RestClient } from "./rest.js";
|
|
5
|
+
export type { AdminWriteOptions, CallOptions, ListItemsQuery, ProviderInput, RestOptions, UserChanges, UserOptions, WriteOptions, } from "./rest.js";
|
|
6
|
+
export type { Cache, CacheChange, CacheKind, UserState } from "./cache.js";
|
|
7
|
+
export { Client } from "./client.js";
|
|
8
|
+
export type { ClientOptions, ClientStatus } from "./client.js";
|
|
9
|
+
export type * from "./events.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { Cache, Client, Item, List, Priority, Profile, ReminderPlan } from "../index.js";
|
|
2
|
+
/** One change to show before the hub confirmed it. */
|
|
3
|
+
export type Change = {
|
|
4
|
+
readonly kind: "item";
|
|
5
|
+
readonly create: Item;
|
|
6
|
+
} | {
|
|
7
|
+
readonly kind: "item";
|
|
8
|
+
readonly id: string;
|
|
9
|
+
readonly update: (item: Item) => Item;
|
|
10
|
+
} | {
|
|
11
|
+
readonly kind: "item";
|
|
12
|
+
readonly id: string;
|
|
13
|
+
readonly remove: true;
|
|
14
|
+
} | {
|
|
15
|
+
readonly kind: "list";
|
|
16
|
+
readonly create: List;
|
|
17
|
+
} | {
|
|
18
|
+
readonly kind: "list";
|
|
19
|
+
readonly id: string;
|
|
20
|
+
readonly update: (list: List) => List;
|
|
21
|
+
} | {
|
|
22
|
+
readonly kind: "list";
|
|
23
|
+
readonly id: string;
|
|
24
|
+
readonly remove: true;
|
|
25
|
+
} | {
|
|
26
|
+
readonly kind: "plan";
|
|
27
|
+
readonly priority: Priority;
|
|
28
|
+
readonly update: (plan: ReminderPlan) => ReminderPlan;
|
|
29
|
+
} | {
|
|
30
|
+
readonly kind: "profile";
|
|
31
|
+
readonly update: (profile: Profile) => Profile;
|
|
32
|
+
};
|
|
33
|
+
/** The cache with changes that are still on their way laid over it. */
|
|
34
|
+
export interface OptimisticView extends Cache {
|
|
35
|
+
/**
|
|
36
|
+
* Shows the changes at once, runs the call and returns its result. The call gets a nonce
|
|
37
|
+
* to pass on to REST, and a function that gives a nonce of its own for the request that
|
|
38
|
+
* creates a draft. If it rejects, the changes are taken back and the error is rethrown.
|
|
39
|
+
*/
|
|
40
|
+
apply<T>(changes: readonly Change[], call: (nonce: string, nonceFor: (tempId: string) => string) => Promise<T>, options?: {
|
|
41
|
+
user?: string;
|
|
42
|
+
}): Promise<T>;
|
|
43
|
+
/** An id for an object that is created ahead of the hub. */
|
|
44
|
+
tempId(): string;
|
|
45
|
+
/** The real id behind a temporary one, once it is known. */
|
|
46
|
+
resolveId(tempId: string): string | undefined;
|
|
47
|
+
/** Whether the draft with this id still waits for the hub. False once it is real or taken back. */
|
|
48
|
+
isPending(tempId: string): boolean;
|
|
49
|
+
}
|
|
50
|
+
/** A view of the client's cache that shows changes before the hub confirmed them. */
|
|
51
|
+
export declare function optimistic(client: Client): OptimisticView;
|