@oblax/realtime 0.0.0-stage → 2.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/dist/errors.d.ts +20 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +11 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/realtime-module.d.ts +59 -0
- package/dist/realtime-module.d.ts.map +1 -0
- package/dist/realtime-module.js +95 -0
- package/dist/realtime-module.js.map +1 -0
- package/dist/sse.d.ts +49 -0
- package/dist/sse.d.ts.map +1 -0
- package/dist/sse.js +101 -0
- package/dist/sse.js.map +1 -0
- package/dist/stream.d.ts +125 -0
- package/dist/stream.d.ts.map +1 -0
- package/dist/stream.js +471 -0
- package/dist/stream.js.map +1 -0
- package/dist/types.d.ts +167 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +4 -0
- package/dist/types.js.map +1 -0
- package/package.json +49 -3
- package/README.md +0 -3
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { OblaxError } from '@oblax/core';
|
|
2
|
+
/**
|
|
3
|
+
* Why a realtime stream failed without an HTTP error:
|
|
4
|
+
*
|
|
5
|
+
* - `stream_ended` — the stream ended with no close frame: the server dropped
|
|
6
|
+
* a client that fell behind, or the network or a proxy cut it. Reconnected.
|
|
7
|
+
* - `stream_silent` — nothing arrived for `idleTimeout`, not even a ping: a
|
|
8
|
+
* half-open connection. Reconnected.
|
|
9
|
+
* - `bad_response` — a 200 that isn't an event stream (a proxy's page).
|
|
10
|
+
* Reconnected.
|
|
11
|
+
* - `unsupported` — the runtime's `fetch` can't stream a response body
|
|
12
|
+
* (React Native). Fatal.
|
|
13
|
+
*/
|
|
14
|
+
export type RealtimeErrorCode = 'stream_ended' | 'stream_silent' | 'bad_response' | 'unsupported';
|
|
15
|
+
/** A realtime stream failure that isn't an HTTP error (those are `OblaxHttpError`). */
|
|
16
|
+
export declare class OblaxRealtimeError extends OblaxError {
|
|
17
|
+
readonly code: RealtimeErrorCode;
|
|
18
|
+
constructor(code: RealtimeErrorCode, message: string);
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAExC;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,iBAAiB,GAAG,cAAc,GAAG,eAAe,GAAG,cAAc,GAAG,aAAa,CAAA;AAEjG,uFAAuF;AACvF,qBAAa,kBAAmB,SAAQ,UAAU;IAChD,QAAQ,CAAC,IAAI,EAAE,iBAAiB,CAAA;gBAEpB,IAAI,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM;CAMrD"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { OblaxError } from '@oblax/core';
|
|
2
|
+
/** A realtime stream failure that isn't an HTTP error (those are `OblaxHttpError`). */
|
|
3
|
+
export class OblaxRealtimeError extends OblaxError {
|
|
4
|
+
constructor(code, message) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = 'OblaxRealtimeError';
|
|
7
|
+
this.code = code;
|
|
8
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAgBxC,uFAAuF;AACvF,MAAM,OAAO,kBAAmB,SAAQ,UAAU;IAGhD,YAAY,IAAuB,EAAE,OAAe;QAClD,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAA;QAChC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;IACnD,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { OblaxModulePlugin } from '@oblax/core';
|
|
2
|
+
import { RealtimeModule } from './realtime-module.js';
|
|
3
|
+
import type { RealtimeConfig } from './types.js';
|
|
4
|
+
export { RealtimeModule } from './realtime-module.js';
|
|
5
|
+
export { RealtimeStream } from './stream.js';
|
|
6
|
+
export { OblaxRealtimeError } from './errors.js';
|
|
7
|
+
export type { RealtimeErrorCode } from './errors.js';
|
|
8
|
+
export type { RealtimeConfig, ConnectOptions, RealtimeEventMap, UntypedEvents, RealtimeEvent, AnyRealtimeEvent, StreamState, CloseReason, OpenInfo, CloseInfo, StreamErrorSource, StreamErrorInfo, RealtimeTarget, RealtimeTargetLists, SendEventParams, SendEventResult, Ticket, } from './types.js';
|
|
9
|
+
/**
|
|
10
|
+
* Realtime module factory.
|
|
11
|
+
*
|
|
12
|
+
* Registers the realtime module with the Oblax SDK; it's available on
|
|
13
|
+
* `oblax.realtime`. `config` sets the reconnect and liveness defaults for every
|
|
14
|
+
* stream; `connect(options)` can override them.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { Oblax } from '@oblax/core'
|
|
19
|
+
* import { PlatformBrowser } from '@oblax/platform-browser'
|
|
20
|
+
* import { Auth } from '@oblax/auth'
|
|
21
|
+
* import { Realtime } from '@oblax/realtime'
|
|
22
|
+
*
|
|
23
|
+
* const oblax = new Oblax({
|
|
24
|
+
* endpoint: 'https://api.my-app.oblax.io',
|
|
25
|
+
* clientId: 'my-web-app',
|
|
26
|
+
* modules: [PlatformBrowser(), Auth({ realm: 'webapp' }), Realtime()],
|
|
27
|
+
* })
|
|
28
|
+
*
|
|
29
|
+
* const stream = oblax.realtime.connect()
|
|
30
|
+
* stream.onOpen(() => refetchAll())
|
|
31
|
+
* stream.on('invoice.ready', (e) => reloadInvoice(e.data.invoice_id))
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function Realtime(config?: RealtimeConfig): OblaxModulePlugin<'realtime', RealtimeModule>;
|
|
35
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAgB,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAClE,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAEhD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAC5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAChD,YAAY,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAEpD,YAAY,EACV,cAAc,EACd,cAAc,EACd,gBAAgB,EAChB,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,WAAW,EACX,WAAW,EACX,QAAQ,EACR,SAAS,EACT,iBAAiB,EACjB,eAAe,EACf,cAAc,EACd,mBAAmB,EACnB,eAAe,EACf,eAAe,EACf,MAAM,GACP,MAAM,YAAY,CAAA;AAEnB;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,QAAQ,CAAC,MAAM,GAAE,cAAmB,GAAG,iBAAiB,CAAC,UAAU,EAAE,cAAc,CAAC,CAOnG"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { RealtimeModule } from './realtime-module.js';
|
|
2
|
+
export { RealtimeModule } from './realtime-module.js';
|
|
3
|
+
export { RealtimeStream } from './stream.js';
|
|
4
|
+
export { OblaxRealtimeError } from './errors.js';
|
|
5
|
+
/**
|
|
6
|
+
* Realtime module factory.
|
|
7
|
+
*
|
|
8
|
+
* Registers the realtime module with the Oblax SDK; it's available on
|
|
9
|
+
* `oblax.realtime`. `config` sets the reconnect and liveness defaults for every
|
|
10
|
+
* stream; `connect(options)` can override them.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { Oblax } from '@oblax/core'
|
|
15
|
+
* import { PlatformBrowser } from '@oblax/platform-browser'
|
|
16
|
+
* import { Auth } from '@oblax/auth'
|
|
17
|
+
* import { Realtime } from '@oblax/realtime'
|
|
18
|
+
*
|
|
19
|
+
* const oblax = new Oblax({
|
|
20
|
+
* endpoint: 'https://api.my-app.oblax.io',
|
|
21
|
+
* clientId: 'my-web-app',
|
|
22
|
+
* modules: [PlatformBrowser(), Auth({ realm: 'webapp' }), Realtime()],
|
|
23
|
+
* })
|
|
24
|
+
*
|
|
25
|
+
* const stream = oblax.realtime.connect()
|
|
26
|
+
* stream.onOpen(() => refetchAll())
|
|
27
|
+
* stream.on('invoice.ready', (e) => reloadInvoice(e.data.invoice_id))
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function Realtime(config = {}) {
|
|
31
|
+
return {
|
|
32
|
+
name: 'realtime',
|
|
33
|
+
create(ctx) {
|
|
34
|
+
return new RealtimeModule(ctx, config);
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAGrD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAC5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAuBhD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,QAAQ,CAAC,SAAyB,EAAE;IAClD,OAAO;QACL,IAAI,EAAE,UAAU;QAChB,MAAM,CAAC,GAAiB;YACtB,OAAO,IAAI,cAAc,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;QACxC,CAAC;KACF,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { OblaxContext } from '@oblax/core';
|
|
2
|
+
import { RealtimeStream } from './stream.js';
|
|
3
|
+
import type { ConnectOptions, RealtimeConfig, RealtimeEventMap, SendEventParams, SendEventResult, UntypedEvents } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* The Realtime module: server-sent events from Oblax.
|
|
6
|
+
*
|
|
7
|
+
* - `connect()` — keeps one stream open for the signed-in user and dispatches
|
|
8
|
+
* its events; it reconnects on its own.
|
|
9
|
+
* - `send()` — sends an event to open streams (app admins only).
|
|
10
|
+
*
|
|
11
|
+
* Events are hints, not data: put an id in `data` and refetch the record
|
|
12
|
+
* through its module. Nothing is replayed after a reconnect, so refetch in
|
|
13
|
+
* `onOpen` as well.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const stream = oblax.realtime.connect()
|
|
18
|
+
* stream.onOpen(() => reloadInvoices())
|
|
19
|
+
* stream.on('invoice.ready', (e) => reloadInvoice(e.data.invoice_id))
|
|
20
|
+
*
|
|
21
|
+
* await oblax.realtime.send({
|
|
22
|
+
* event: 'invoice.ready',
|
|
23
|
+
* data: { invoice_id: 'inv_001' },
|
|
24
|
+
* target: { users: ['user-id'] },
|
|
25
|
+
* })
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare class RealtimeModule {
|
|
29
|
+
private readonly ctx;
|
|
30
|
+
private readonly defaults;
|
|
31
|
+
constructor(ctx: OblaxContext, config: RealtimeConfig);
|
|
32
|
+
/**
|
|
33
|
+
* Opens a stream for the signed-in user and starts connecting at once: a
|
|
34
|
+
* ticket (with the platform's credentials), then the stream with it. Keep
|
|
35
|
+
* one per tab and share it; a user has at most 10 on a server, and an 11th
|
|
36
|
+
* replaces the oldest.
|
|
37
|
+
*
|
|
38
|
+
* Pass an event map to type `on()`:
|
|
39
|
+
* `connect<{ 'invoice.ready': { invoice_id: string } }>()`.
|
|
40
|
+
*
|
|
41
|
+
* Browsers and Node 18+ only: React Native's `fetch` can't stream, so the
|
|
42
|
+
* stream closes with `fatal` there.
|
|
43
|
+
*/
|
|
44
|
+
connect<E extends RealtimeEventMap = UntypedEvents>(options?: ConnectOptions): RealtimeStream<E>;
|
|
45
|
+
/**
|
|
46
|
+
* Sends an event to the open streams the target names, in the caller's
|
|
47
|
+
* project and environment. App admins only (403 otherwise). Answers once
|
|
48
|
+
* the event is published, not delivered; sending to nobody online isn't an
|
|
49
|
+
* error.
|
|
50
|
+
*
|
|
51
|
+
* `data` and `target` are sent as-is. Like every POST, a send is resent once
|
|
52
|
+
* only after a 401 and a successful token refresh. Errors are `OblaxHttpError`s: 400
|
|
53
|
+
* `VALIDATION_FAILED` names the broken rule in `body.validation_errors`
|
|
54
|
+
* (`event`, `data`, `target.*`), 429 `RATE_LIMIT_EXCEEDED` is over 50 sends
|
|
55
|
+
* a second.
|
|
56
|
+
*/
|
|
57
|
+
send(params: SendEventParams): Promise<SendEventResult>;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=realtime-module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"realtime-module.d.ts","sourceRoot":"","sources":["../src/realtime-module.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE/C,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAE5C,OAAO,KAAK,EACV,cAAc,EACd,cAAc,EACd,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,aAAa,EACd,MAAM,YAAY,CAAA;AAWnB;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAc;IAClC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA0B;gBAEvC,GAAG,EAAE,YAAY,EAAE,MAAM,EAAE,cAAc;IAKrD;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,CAAC,SAAS,gBAAgB,GAAG,aAAa,EAAE,OAAO,GAAE,cAAmB,GAAG,cAAc,CAAC,CAAC,CAAC;IAQpG;;;;;;;;;;;OAWG;IACG,IAAI,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,eAAe,CAAC;CAO9D"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { OblaxError } from '@oblax/core';
|
|
2
|
+
import { RealtimeStream } from './stream.js';
|
|
3
|
+
/** The longest delay setTimeout keeps: a longer one fires at once. */
|
|
4
|
+
const MAX_DELAY = 2147483647;
|
|
5
|
+
const DEFAULTS = {
|
|
6
|
+
reconnectDelay: 1000,
|
|
7
|
+
maxReconnectDelay: 30000,
|
|
8
|
+
idleTimeout: 60000,
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* The Realtime module: server-sent events from Oblax.
|
|
12
|
+
*
|
|
13
|
+
* - `connect()` — keeps one stream open for the signed-in user and dispatches
|
|
14
|
+
* its events; it reconnects on its own.
|
|
15
|
+
* - `send()` — sends an event to open streams (app admins only).
|
|
16
|
+
*
|
|
17
|
+
* Events are hints, not data: put an id in `data` and refetch the record
|
|
18
|
+
* through its module. Nothing is replayed after a reconnect, so refetch in
|
|
19
|
+
* `onOpen` as well.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const stream = oblax.realtime.connect()
|
|
24
|
+
* stream.onOpen(() => reloadInvoices())
|
|
25
|
+
* stream.on('invoice.ready', (e) => reloadInvoice(e.data.invoice_id))
|
|
26
|
+
*
|
|
27
|
+
* await oblax.realtime.send({
|
|
28
|
+
* event: 'invoice.ready',
|
|
29
|
+
* data: { invoice_id: 'inv_001' },
|
|
30
|
+
* target: { users: ['user-id'] },
|
|
31
|
+
* })
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export class RealtimeModule {
|
|
35
|
+
constructor(ctx, config) {
|
|
36
|
+
this.ctx = ctx;
|
|
37
|
+
this.defaults = resolve(DEFAULTS, config, 'Realtime module');
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Opens a stream for the signed-in user and starts connecting at once: a
|
|
41
|
+
* ticket (with the platform's credentials), then the stream with it. Keep
|
|
42
|
+
* one per tab and share it; a user has at most 10 on a server, and an 11th
|
|
43
|
+
* replaces the oldest.
|
|
44
|
+
*
|
|
45
|
+
* Pass an event map to type `on()`:
|
|
46
|
+
* `connect<{ 'invoice.ready': { invoice_id: string } }>()`.
|
|
47
|
+
*
|
|
48
|
+
* Browsers and Node 18+ only: React Native's `fetch` can't stream, so the
|
|
49
|
+
* stream closes with `fatal` there.
|
|
50
|
+
*/
|
|
51
|
+
connect(options = {}) {
|
|
52
|
+
const settings = {
|
|
53
|
+
...resolve(this.defaults, options, 'realtime.connect()'),
|
|
54
|
+
signal: options.signal,
|
|
55
|
+
};
|
|
56
|
+
return new RealtimeStream(this.ctx, settings);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Sends an event to the open streams the target names, in the caller's
|
|
60
|
+
* project and environment. App admins only (403 otherwise). Answers once
|
|
61
|
+
* the event is published, not delivered; sending to nobody online isn't an
|
|
62
|
+
* error.
|
|
63
|
+
*
|
|
64
|
+
* `data` and `target` are sent as-is. Like every POST, a send is resent once
|
|
65
|
+
* only after a 401 and a successful token refresh. Errors are `OblaxHttpError`s: 400
|
|
66
|
+
* `VALIDATION_FAILED` names the broken rule in `body.validation_errors`
|
|
67
|
+
* (`event`, `data`, `target.*`), 429 `RATE_LIMIT_EXCEEDED` is over 50 sends
|
|
68
|
+
* a second.
|
|
69
|
+
*/
|
|
70
|
+
async send(params) {
|
|
71
|
+
return this.ctx.http.post('/realtime/events', {
|
|
72
|
+
event: params.event,
|
|
73
|
+
data: params.data,
|
|
74
|
+
target: params.target,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** Fills the tuning from `base`, failing fast on a value that isn't a positive number. */
|
|
79
|
+
function resolve(base, config, where) {
|
|
80
|
+
const out = {
|
|
81
|
+
reconnectDelay: config.reconnectDelay ?? base.reconnectDelay,
|
|
82
|
+
maxReconnectDelay: config.maxReconnectDelay ?? base.maxReconnectDelay,
|
|
83
|
+
idleTimeout: config.idleTimeout ?? base.idleTimeout,
|
|
84
|
+
};
|
|
85
|
+
for (const [key, value] of Object.entries(out)) {
|
|
86
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0 || value > MAX_DELAY) {
|
|
87
|
+
throw new OblaxError(`${where}: \`${key}\` must be a positive number of milliseconds, at most ${MAX_DELAY}.`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (out.maxReconnectDelay < out.reconnectDelay) {
|
|
91
|
+
throw new OblaxError(`${where}: \`maxReconnectDelay\` must not be shorter than \`reconnectDelay\`.`);
|
|
92
|
+
}
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=realtime-module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"realtime-module.js","sourceRoot":"","sources":["../src/realtime-module.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAW5C,sEAAsE;AACtE,MAAM,SAAS,GAAG,UAAa,CAAA;AAE/B,MAAM,QAAQ,GAAG;IACf,cAAc,EA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,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;QAC/C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,KAAK,GAAG,SAAS,EAAE,CAAC;YAC5F,MAAM,IAAI,UAAU,CAClB,GAAG,KAAK,OAAO,GAAG,yDAAyD,SAAS,GAAG,CACxF,CAAA;QACH,CAAC;IACH,CAAC;IACD,IAAI,GAAG,CAAC,iBAAiB,GAAG,GAAG,CAAC,cAAc,EAAE,CAAC;QAC/C,MAAM,IAAI,UAAU,CAAC,GAAG,KAAK,sEAAsE,CAAC,CAAA;IACtG,CAAC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC"}
|
package/dist/sse.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One server-sent event: the fields of a frame, dispatched on its blank line.
|
|
3
|
+
*
|
|
4
|
+
* `data` is the frame's `data:` lines joined with `\n`, as sent: parsing it as
|
|
5
|
+
* JSON is the stream's job, not the parser's.
|
|
6
|
+
*/
|
|
7
|
+
export interface SseFrame {
|
|
8
|
+
/** The frame's `id:` field, or `undefined` when it had none. */
|
|
9
|
+
id: string | undefined;
|
|
10
|
+
/** The frame's `event:` field; `'message'` when it had none. */
|
|
11
|
+
event: string;
|
|
12
|
+
data: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Incremental parser for a `text/event-stream` body, as the HTML spec reads one.
|
|
16
|
+
*
|
|
17
|
+
* Feed it the body's bytes in whatever chunks they arrive with {@link push}: it
|
|
18
|
+
* decodes UTF-8 across chunk boundaries, splits lines on LF, CRLF or CR (a CRLF
|
|
19
|
+
* split across two chunks counts once), and calls `onFrame` for each frame on
|
|
20
|
+
* its blank line.
|
|
21
|
+
*
|
|
22
|
+
* - `data:` lines are joined with `\n`; a frame without any is not dispatched
|
|
23
|
+
* (e.g. the `retry: 3000` frame Oblax opens a stream with).
|
|
24
|
+
* - `event:` names the frame; without it the frame is a `message`.
|
|
25
|
+
* - `id:` is kept per frame (a value with NUL is ignored, as the spec says).
|
|
26
|
+
* - `retry:` is ignored: the realtime stream owns its own backoff.
|
|
27
|
+
* - Comment lines (`: ping`) are ignored; the stream counts any bytes as liveness.
|
|
28
|
+
* - A frame the body ends in the middle of, with no blank line after it, is
|
|
29
|
+
* dropped, as `EventSource` drops it: it may be cut short.
|
|
30
|
+
*/
|
|
31
|
+
export declare class SseParser {
|
|
32
|
+
private readonly decoder;
|
|
33
|
+
private readonly onFrame;
|
|
34
|
+
/** The current line, up to the last chunk's end. */
|
|
35
|
+
private line;
|
|
36
|
+
/** The last chunk ended with CR: a LF opening the next one is the same line end. */
|
|
37
|
+
private skipLF;
|
|
38
|
+
private data;
|
|
39
|
+
private event;
|
|
40
|
+
private id;
|
|
41
|
+
constructor(onFrame: (frame: SseFrame) => void);
|
|
42
|
+
/** Parses the next chunk of the body. */
|
|
43
|
+
push(bytes: Uint8Array): void;
|
|
44
|
+
/** Parses the next piece of already decoded text. */
|
|
45
|
+
pushText(text: string): void;
|
|
46
|
+
private processLine;
|
|
47
|
+
private dispatch;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=sse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../src/sse.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,WAAW,QAAQ;IACvB,gEAAgE;IAChE,EAAE,EAAE,MAAM,GAAG,SAAS,CAAA;IACtB,gEAAgE;IAChE,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,MAAM,CAAA;CACb;AAKD;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,SAAS;IACpB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA2B;IACnD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA2B;IACnD,oDAAoD;IACpD,OAAO,CAAC,IAAI,CAAK;IACjB,oFAAoF;IACpF,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,IAAI,CAAe;IAC3B,OAAO,CAAC,KAAK,CAAK;IAClB,OAAO,CAAC,EAAE,CAAgC;gBAE9B,OAAO,EAAE,CAAC,KAAK,EAAE,QAAQ,KAAK,IAAI;IAI9C,yCAAyC;IACzC,IAAI,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI;IAI7B,qDAAqD;IACrD,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAuB5B,OAAO,CAAC,WAAW;IA2BnB,OAAO,CAAC,QAAQ;CASjB"}
|
package/dist/sse.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
const LF = 10;
|
|
2
|
+
const CR = 13;
|
|
3
|
+
/**
|
|
4
|
+
* Incremental parser for a `text/event-stream` body, as the HTML spec reads one.
|
|
5
|
+
*
|
|
6
|
+
* Feed it the body's bytes in whatever chunks they arrive with {@link push}: it
|
|
7
|
+
* decodes UTF-8 across chunk boundaries, splits lines on LF, CRLF or CR (a CRLF
|
|
8
|
+
* split across two chunks counts once), and calls `onFrame` for each frame on
|
|
9
|
+
* its blank line.
|
|
10
|
+
*
|
|
11
|
+
* - `data:` lines are joined with `\n`; a frame without any is not dispatched
|
|
12
|
+
* (e.g. the `retry: 3000` frame Oblax opens a stream with).
|
|
13
|
+
* - `event:` names the frame; without it the frame is a `message`.
|
|
14
|
+
* - `id:` is kept per frame (a value with NUL is ignored, as the spec says).
|
|
15
|
+
* - `retry:` is ignored: the realtime stream owns its own backoff.
|
|
16
|
+
* - Comment lines (`: ping`) are ignored; the stream counts any bytes as liveness.
|
|
17
|
+
* - A frame the body ends in the middle of, with no blank line after it, is
|
|
18
|
+
* dropped, as `EventSource` drops it: it may be cut short.
|
|
19
|
+
*/
|
|
20
|
+
export class SseParser {
|
|
21
|
+
constructor(onFrame) {
|
|
22
|
+
this.decoder = new TextDecoder('utf-8');
|
|
23
|
+
/** The current line, up to the last chunk's end. */
|
|
24
|
+
this.line = '';
|
|
25
|
+
/** The last chunk ended with CR: a LF opening the next one is the same line end. */
|
|
26
|
+
this.skipLF = false;
|
|
27
|
+
this.data = [];
|
|
28
|
+
this.event = '';
|
|
29
|
+
this.id = undefined;
|
|
30
|
+
this.onFrame = onFrame;
|
|
31
|
+
}
|
|
32
|
+
/** Parses the next chunk of the body. */
|
|
33
|
+
push(bytes) {
|
|
34
|
+
this.pushText(this.decoder.decode(bytes, { stream: true }));
|
|
35
|
+
}
|
|
36
|
+
/** Parses the next piece of already decoded text. */
|
|
37
|
+
pushText(text) {
|
|
38
|
+
let start = 0;
|
|
39
|
+
if (this.skipLF && text.length > 0) {
|
|
40
|
+
if (text.charCodeAt(0) === LF)
|
|
41
|
+
start = 1;
|
|
42
|
+
this.skipLF = false;
|
|
43
|
+
}
|
|
44
|
+
for (let i = start; i < text.length; i++) {
|
|
45
|
+
const c = text.charCodeAt(i);
|
|
46
|
+
if (c !== LF && c !== CR)
|
|
47
|
+
continue;
|
|
48
|
+
this.processLine(this.line + text.slice(start, i));
|
|
49
|
+
this.line = '';
|
|
50
|
+
if (c === CR) {
|
|
51
|
+
if (i + 1 < text.length) {
|
|
52
|
+
if (text.charCodeAt(i + 1) === LF)
|
|
53
|
+
i++;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
this.skipLF = true;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
start = i + 1;
|
|
60
|
+
}
|
|
61
|
+
this.line += text.slice(start);
|
|
62
|
+
}
|
|
63
|
+
processLine(line) {
|
|
64
|
+
if (line === '') {
|
|
65
|
+
this.dispatch();
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
// a comment: ": ping"
|
|
69
|
+
if (line.charCodeAt(0) === 58)
|
|
70
|
+
return;
|
|
71
|
+
const colon = line.indexOf(':');
|
|
72
|
+
const field = colon === -1 ? line : line.slice(0, colon);
|
|
73
|
+
let value = colon === -1 ? '' : line.slice(colon + 1);
|
|
74
|
+
if (value.charCodeAt(0) === 32)
|
|
75
|
+
value = value.slice(1);
|
|
76
|
+
switch (field) {
|
|
77
|
+
case 'data':
|
|
78
|
+
this.data.push(value);
|
|
79
|
+
break;
|
|
80
|
+
case 'event':
|
|
81
|
+
this.event = value;
|
|
82
|
+
break;
|
|
83
|
+
case 'id':
|
|
84
|
+
if (!value.includes('\0'))
|
|
85
|
+
this.id = value;
|
|
86
|
+
break;
|
|
87
|
+
// 'retry' and unknown fields are ignored
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
dispatch() {
|
|
91
|
+
const frame = this.data.length === 0
|
|
92
|
+
? null
|
|
93
|
+
: { id: this.id, event: this.event || 'message', data: this.data.join('\n') };
|
|
94
|
+
this.data = [];
|
|
95
|
+
this.event = '';
|
|
96
|
+
this.id = undefined;
|
|
97
|
+
if (frame)
|
|
98
|
+
this.onFrame(frame);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=sse.js.map
|
package/dist/sse.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.js","sourceRoot":"","sources":["../src/sse.ts"],"names":[],"mappings":"AAcA,MAAM,EAAE,GAAG,EAAE,CAAA;AACb,MAAM,EAAE,GAAG,EAAE,CAAA;AAEb;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,OAAO,SAAS;IAWpB,YAAY,OAAkC;QAV7B,YAAO,GAAG,IAAI,WAAW,CAAC,OAAO,CAAC,CAAA;QAEnD,oDAAoD;QAC5C,SAAI,GAAG,EAAE,CAAA;QACjB,oFAAoF;QAC5E,WAAM,GAAG,KAAK,CAAA;QACd,SAAI,GAAa,EAAE,CAAA;QACnB,UAAK,GAAG,EAAE,CAAA;QACV,OAAE,GAAuB,SAAS,CAAA;QAGxC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;IACxB,CAAC;IAED,yCAAyC;IACzC,IAAI,CAAC,KA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CAAC,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,IAAI,SAAS,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAA;QAC/E,IAAI,CAAC,IAAI,GAAG,EAAE,CAAA;QACd,IAAI,CAAC,KAAK,GAAG,EAAE,CAAA;QACf,IAAI,CAAC,EAAE,GAAG,SAAS,CAAA;QACnB,IAAI,KAAK;YAAE,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAChC,CAAC;CACF"}
|
package/dist/stream.d.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type { OblaxContext } from '@oblax/core';
|
|
2
|
+
import type { AnyRealtimeEvent, CloseInfo, OpenInfo, RealtimeEvent, RealtimeEventMap, StreamErrorInfo, StreamState, UntypedEvents } from './types.js';
|
|
3
|
+
/** A stream's resolved tuning. */
|
|
4
|
+
export interface StreamSettings {
|
|
5
|
+
reconnectDelay: number;
|
|
6
|
+
maxReconnectDelay: number;
|
|
7
|
+
idleTimeout: number;
|
|
8
|
+
signal?: AbortSignal | undefined;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* An open realtime stream: it gets a ticket, opens the stream, dispatches
|
|
12
|
+
* events to its handlers, and reconnects (new ticket, new stream) until it's
|
|
13
|
+
* closed. Created by `oblax.realtime.connect()`, which starts it at once.
|
|
14
|
+
*
|
|
15
|
+
* Every `on…` method returns a function that removes the handler. Handlers may
|
|
16
|
+
* be added and removed while events are dispatched; one that throws is reported
|
|
17
|
+
* to `onError` (`source: 'handler'`) and the stream carries on.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const stream = oblax.realtime.connect<{ 'invoice.ready': { invoice_id: string } }>()
|
|
22
|
+
* stream.onOpen(() => reloadInvoices()) // after every connect: nothing is replayed
|
|
23
|
+
* stream.on('invoice.ready', (e) => reloadInvoice(e.data.invoice_id))
|
|
24
|
+
* // on sign-out
|
|
25
|
+
* stream.close()
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare class RealtimeStream<E extends RealtimeEventMap = UntypedEvents> {
|
|
29
|
+
private current;
|
|
30
|
+
private readonly ctx;
|
|
31
|
+
private readonly settings;
|
|
32
|
+
/** Aborted by close(): ends the attempt in flight and any wait. */
|
|
33
|
+
private readonly stopper;
|
|
34
|
+
private readonly timers;
|
|
35
|
+
private readonly onSignalAbort;
|
|
36
|
+
private readonly eventHandlers;
|
|
37
|
+
private readonly anyHandlers;
|
|
38
|
+
private readonly openHandlers;
|
|
39
|
+
private readonly closeHandlers;
|
|
40
|
+
private readonly errorHandlers;
|
|
41
|
+
/** Failed attempts in a row: drives the backoff. */
|
|
42
|
+
private failures;
|
|
43
|
+
/** A stream opened at least once: the next open is a reconnect. */
|
|
44
|
+
private opened;
|
|
45
|
+
/** @internal Use `oblax.realtime.connect()`. */
|
|
46
|
+
constructor(ctx: OblaxContext, settings: StreamSettings);
|
|
47
|
+
/** Where the stream is: `connecting`, `open`, `reconnecting` or `closed`. */
|
|
48
|
+
get state(): StreamState;
|
|
49
|
+
/**
|
|
50
|
+
* Calls `handler` for each event named `event`. The stream's own system
|
|
51
|
+
* events (`oblax.stream.*`) never reach it.
|
|
52
|
+
* @returns a function that removes the handler.
|
|
53
|
+
*/
|
|
54
|
+
on<K extends keyof E & string>(event: K, handler: (event: RealtimeEvent<K, E[K]>) => void): () => void;
|
|
55
|
+
/**
|
|
56
|
+
* Calls `handler` for every event, after the handlers for its name.
|
|
57
|
+
* @returns a function that removes the handler.
|
|
58
|
+
*/
|
|
59
|
+
onAny(handler: (event: AnyRealtimeEvent<E>) => void): () => void;
|
|
60
|
+
/**
|
|
61
|
+
* Calls `handler` each time a stream opens, the first time and after every
|
|
62
|
+
* reconnect. Events sent while the stream was down are gone: refetch what
|
|
63
|
+
* the page shows here.
|
|
64
|
+
* @returns a function that removes the handler.
|
|
65
|
+
*/
|
|
66
|
+
onOpen(handler: (info: OpenInfo) => void): () => void;
|
|
67
|
+
/**
|
|
68
|
+
* Calls `handler` once, when the stream closes for good: `closed` (by the
|
|
69
|
+
* app), `replaced` or `fatal`. It doesn't fire for a reconnect.
|
|
70
|
+
* @returns a function that removes the handler.
|
|
71
|
+
*/
|
|
72
|
+
onClose(handler: (info: CloseInfo) => void): () => void;
|
|
73
|
+
/**
|
|
74
|
+
* Calls `handler` for every failed attempt (`fatal: false` with `retryIn`
|
|
75
|
+
* when it retries; `fatal: true` right before a fatal close) and for every
|
|
76
|
+
* error thrown by one of the app's handlers (`source: 'handler'`). HTTP
|
|
77
|
+
* failures are `OblaxHttpError`s; others are `OblaxRealtimeError`,
|
|
78
|
+
* `OblaxTimeoutError` or the runtime's network error.
|
|
79
|
+
* @returns a function that removes the handler.
|
|
80
|
+
*/
|
|
81
|
+
onError(handler: (error: unknown, info: StreamErrorInfo) => void): () => void;
|
|
82
|
+
/**
|
|
83
|
+
* Closes the stream for good: aborts the request in flight, clears every
|
|
84
|
+
* timer and calls `onClose({ reason: 'closed' })`. Safe to call more than once.
|
|
85
|
+
*/
|
|
86
|
+
close(): void;
|
|
87
|
+
private get isClosed();
|
|
88
|
+
private run;
|
|
89
|
+
/** One ticket and one stream, read until it ends. */
|
|
90
|
+
private attempt;
|
|
91
|
+
private connectOnce;
|
|
92
|
+
/**
|
|
93
|
+
* Gets a ticket. An expired token answers 401 `AUTH_TOKEN_EXPIRED`, which
|
|
94
|
+
* the HTTP client refreshes and retries once; a 401 left after that (no
|
|
95
|
+
* refresh to be had, or another request's refresh was already running) is
|
|
96
|
+
* retried with backoff. A 403 means not signed in, a visitor or not allowed:
|
|
97
|
+
* fatal, with no refresh.
|
|
98
|
+
*/
|
|
99
|
+
private postTicket;
|
|
100
|
+
/**
|
|
101
|
+
* Reads frames until the stream ends: the reason a close frame gave, or the
|
|
102
|
+
* error (no close frame, silence, the network). A watchdog aborts the
|
|
103
|
+
* request when nothing, not even a ping, arrived for `idleTimeout`.
|
|
104
|
+
*/
|
|
105
|
+
private read;
|
|
106
|
+
/** Dispatches an app event; returns why the stream ends for a close frame. */
|
|
107
|
+
private handleFrame;
|
|
108
|
+
/** The delay before attempt `n` of a run of failures: doubling, capped, with jitter. */
|
|
109
|
+
private backoff;
|
|
110
|
+
/** Resolves after `ms`, or at once when the stream closes. */
|
|
111
|
+
private wait;
|
|
112
|
+
private setTimer;
|
|
113
|
+
private clearTimer;
|
|
114
|
+
private add;
|
|
115
|
+
/**
|
|
116
|
+
* Calls each handler of a snapshot, skipping one removed meanwhile, and
|
|
117
|
+
* stopping when a handler closed the stream (except for close handlers).
|
|
118
|
+
*/
|
|
119
|
+
private emit;
|
|
120
|
+
/** Calls the error handlers; one that throws is ignored, there's nowhere left to report it. */
|
|
121
|
+
private report;
|
|
122
|
+
/** Closes for good, once: stops the attempt and timers, then calls the close handlers. */
|
|
123
|
+
private finish;
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAK/C,OAAO,KAAK,EACV,gBAAgB,EAChB,SAAS,EACT,QAAQ,EACR,aAAa,EACb,gBAAgB,EAChB,eAAe,EACf,WAAW,EAEX,aAAa,EACd,MAAM,YAAY,CAAA;AAWnB,kCAAkC;AAClC,MAAM,WAAW,cAAc;IAC7B,cAAc,EAAE,MAAM,CAAA;IACtB,iBAAiB,EAAE,MAAM,CAAA;IACzB,WAAW,EAAE,MAAM,CAAA;IACnB,MAAM,CAAC,EAAE,WAAW,GAAG,SAAS,CAAA;CACjC;AAaD;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,cAAc,CAAC,CAAC,SAAS,gBAAgB,GAAG,aAAa;IACpE,OAAO,CAAC,OAAO,CAA4B;IAC3C,OAAO,CA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