@atcute/firehose 0.1.1 → 1.0.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/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/subscription.d.ts +10 -0
- package/dist/subscription.d.ts.map +1 -1
- package/dist/subscription.js +22 -3
- package/dist/subscription.js.map +1 -1
- package/dist/types.d.ts +7 -3
- package/dist/types.d.ts.map +1 -1
- package/lib/index.ts +1 -1
- package/lib/subscription.ts +24 -3
- package/lib/types.ts +7 -3
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACxE,YAAY,EAAE,2BAA2B,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { FirehoseSubscription } from './subscription.js';
|
|
1
|
+
export { FirehoseError, FirehoseSubscription } from './subscription.js';
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC"}
|
package/dist/subscription.d.ts
CHANGED
|
@@ -2,6 +2,16 @@ import { type XRPCSubscriptionMetadata } from '@atcute/lexicons/validations';
|
|
|
2
2
|
import { EventIterator } from '@mary-ext/event-iterator';
|
|
3
3
|
import type { ReadonlyDeep } from 'type-fest';
|
|
4
4
|
import type { FirehoseSubscriptionOptions, MessageOf } from './types.ts';
|
|
5
|
+
/**
|
|
6
|
+
* non-fatal error frame received from the upstream firehose, carrying the
|
|
7
|
+
* atproto-spec `error` code alongside the human-readable `message`.
|
|
8
|
+
*/
|
|
9
|
+
export declare class FirehoseError extends Error {
|
|
10
|
+
readonly name = "FirehoseError";
|
|
11
|
+
/** atproto error code from the error frame */
|
|
12
|
+
readonly error: string;
|
|
13
|
+
constructor(error: string, message?: string);
|
|
14
|
+
}
|
|
5
15
|
/**
|
|
6
16
|
* generic XRPC subscription client for AT Protocol
|
|
7
17
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscription.d.ts","sourceRoot":"","sources":["../lib/subscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,KAAK,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAExF,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAGzD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAG9C,OAAO,KAAK,EAAE,2BAA2B,EAAE,SAAS,EAAY,MAAM,YAAY,CAAC;AAEnF;;GAEG;AACH,qBAAa,oBAAoB,CAAC,OAAO,SAAS,wBAAwB;;IAQzE;;OAEG;IACH,YAAY,OAAO,EAAE,2BAA2B,CAAC,OAAO,CAAC,EAExD;
|
|
1
|
+
{"version":3,"file":"subscription.d.ts","sourceRoot":"","sources":["../lib/subscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,KAAK,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAExF,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAGzD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAG9C,OAAO,KAAK,EAAE,2BAA2B,EAAE,SAAS,EAAY,MAAM,YAAY,CAAC;AAEnF;;;GAGG;AACH,qBAAa,aAAc,SAAQ,KAAK;IACvC,SAAkB,IAAI,mBAAmB;IACzC,8CAA8C;IAC9C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,YAAY,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,EAG1C;CACD;AAED;;GAEG;AACH,qBAAa,oBAAoB,CAAC,OAAO,SAAS,wBAAwB;;IAQzE;;OAEG;IACH,YAAY,OAAO,EAAE,2BAA2B,CAAC,OAAO,CAAC,EAExD;IAiGD,CAAC,MAAM,CAAC,aAAa,CAAC,sCAkBrB;IAED;;OAEG;IACH,UAAU,IAAI,YAAY,CAAC,2BAA2B,CAAC,OAAO,CAAC,CAAC,CAE/D;IAED;;OAEG;IACH,aAAa,CAAC,OAAO,EAAE,OAAO,CAAC,2BAA2B,CAAC,OAAO,CAAC,CAAC,GAAG,IAAI,CAO1E;CACD"}
|
package/dist/subscription.js
CHANGED
|
@@ -3,6 +3,17 @@ import { EventIterator } from '@mary-ext/event-iterator';
|
|
|
3
3
|
import { SimpleEventEmitter } from '@mary-ext/simple-event-emitter';
|
|
4
4
|
import { WebSocket as ReconnectingWebSocket } from 'partysocket';
|
|
5
5
|
import { addTypeToBody, decodeFrame } from './frame-decoder.js';
|
|
6
|
+
/**
|
|
7
|
+
* non-fatal error frame received from the upstream firehose, carrying the
|
|
8
|
+
* atproto-spec `error` code alongside the human-readable `message`.
|
|
9
|
+
*/
|
|
10
|
+
export class FirehoseError extends Error {
|
|
11
|
+
constructor(error, message) {
|
|
12
|
+
super(message ?? error);
|
|
13
|
+
this.name = 'FirehoseError';
|
|
14
|
+
this.error = error;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
6
17
|
/**
|
|
7
18
|
* generic XRPC subscription client for AT Protocol
|
|
8
19
|
*/
|
|
@@ -21,7 +32,7 @@ export class FirehoseSubscription {
|
|
|
21
32
|
if (this.#ws !== undefined) {
|
|
22
33
|
return;
|
|
23
34
|
}
|
|
24
|
-
const { service: wsUrls, nsid, params, ws: wsOptions,
|
|
35
|
+
const { service: wsUrls, nsid, params, ws: wsOptions, validateEvents = true, onConnectionClose, onConnectionError, onConnectionOpen, onError, } = this.#options;
|
|
25
36
|
const emitter = this.#emitter;
|
|
26
37
|
const getUrl = () => {
|
|
27
38
|
let selectedUrl;
|
|
@@ -54,13 +65,21 @@ export class FirehoseSubscription {
|
|
|
54
65
|
const buffer = new Uint8Array(ev.data);
|
|
55
66
|
const frame = decodeFrame(buffer);
|
|
56
67
|
if (frame.type === 'error') {
|
|
57
|
-
onError?.(frame.error, frame.message);
|
|
68
|
+
onError?.(new FirehoseError(frame.error, frame.message));
|
|
58
69
|
return;
|
|
59
70
|
}
|
|
60
71
|
let body = addTypeToBody(frame.body, frame.discriminator, nsid.nsid);
|
|
61
|
-
if (
|
|
72
|
+
if (validateEvents && nsid.message !== null) {
|
|
62
73
|
const result = safeParse(nsid.message, body);
|
|
63
74
|
if (!result.ok) {
|
|
75
|
+
if (onError) {
|
|
76
|
+
try {
|
|
77
|
+
result.throw();
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
onError(err);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
64
83
|
return;
|
|
65
84
|
}
|
|
66
85
|
body = result.value;
|
package/dist/subscription.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscription.js","sourceRoot":"","sources":["../lib/subscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAiC,MAAM,8BAA8B,CAAC;AAExF,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AACpE,O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,OAAsD;QACnE,IAAI,CAAC,QAAQ,GAAG,EAAE,GAAG,IAAI,CAAC,QAAQ,EAAE,GAAG,OAAO,EAAE,CAAC;QAEjD,IAAI,IAAI,CAAC,GAAG,KAAK,SAAS,EAAE,CAAC;YAC5B,IAAI,CAAC,QAAQ,EAAE,CAAC;YAChB,IAAI,CAAC,OAAO,EAAE,CAAC;QAChB,CAAC;IACF,CAAC;CACD"}
|
package/dist/types.d.ts
CHANGED
|
@@ -28,14 +28,18 @@ export interface FirehoseSubscriptionOptions<TSchema extends XRPCSubscriptionMet
|
|
|
28
28
|
*/
|
|
29
29
|
params?: ParamsOf<TSchema> | (() => ParamsOf<TSchema>);
|
|
30
30
|
/**
|
|
31
|
-
* whether to validate incoming
|
|
31
|
+
* whether to validate incoming events against the schema
|
|
32
32
|
* @default true
|
|
33
33
|
*/
|
|
34
|
-
|
|
34
|
+
validateEvents?: boolean;
|
|
35
35
|
onConnectionOpen?: (event: Event) => void;
|
|
36
36
|
onConnectionClose?: (event: CloseEvent) => void;
|
|
37
37
|
onConnectionError?: (event: ErrorEvent) => void;
|
|
38
|
-
|
|
38
|
+
/**
|
|
39
|
+
* called for non-fatal frame-level errors: atproto error frames (passed as `FirehoseError`)
|
|
40
|
+
* and message validation failures (passed as `ValidationError`).
|
|
41
|
+
*/
|
|
42
|
+
onError?: (err: unknown) => void;
|
|
39
43
|
/**
|
|
40
44
|
* WebSocket connection options
|
|
41
45
|
*/
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../lib/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAC1F,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAE/D,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEt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ACd,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,MAAM,YAAY,GACrB;IACA,IAAI,EAAE,SAAS,CAAC;IAChB,IAAI,EAAE,OAAO,CAAC;IACd,aAAa,CAAC,EAAE,MAAM,CAAC;CACtB,GACD;IACA,IAAI,EAAE,OAAO,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC"}
|
package/lib/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { FirehoseSubscription } from './subscription.ts';
|
|
1
|
+
export { FirehoseError, FirehoseSubscription } from './subscription.ts';
|
|
2
2
|
export type { FirehoseSubscriptionOptions, MessageOf, ParamsOf } from './types.ts';
|
package/lib/subscription.ts
CHANGED
|
@@ -8,6 +8,20 @@ import type { ReadonlyDeep } from 'type-fest';
|
|
|
8
8
|
import { addTypeToBody, decodeFrame } from './frame-decoder.ts';
|
|
9
9
|
import type { FirehoseSubscriptionOptions, MessageOf, ParamsOf } from './types.ts';
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* non-fatal error frame received from the upstream firehose, carrying the
|
|
13
|
+
* atproto-spec `error` code alongside the human-readable `message`.
|
|
14
|
+
*/
|
|
15
|
+
export class FirehoseError extends Error {
|
|
16
|
+
override readonly name = 'FirehoseError';
|
|
17
|
+
/** atproto error code from the error frame */
|
|
18
|
+
readonly error: string;
|
|
19
|
+
constructor(error: string, message?: string) {
|
|
20
|
+
super(message ?? error);
|
|
21
|
+
this.error = error;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
11
25
|
/**
|
|
12
26
|
* generic XRPC subscription client for AT Protocol
|
|
13
27
|
*/
|
|
@@ -36,7 +50,7 @@ export class FirehoseSubscription<TSchema extends XRPCSubscriptionMetadata> {
|
|
|
36
50
|
nsid,
|
|
37
51
|
params,
|
|
38
52
|
ws: wsOptions,
|
|
39
|
-
|
|
53
|
+
validateEvents = true,
|
|
40
54
|
onConnectionClose,
|
|
41
55
|
onConnectionError,
|
|
42
56
|
onConnectionOpen,
|
|
@@ -85,15 +99,22 @@ export class FirehoseSubscription<TSchema extends XRPCSubscriptionMetadata> {
|
|
|
85
99
|
const frame = decodeFrame(buffer);
|
|
86
100
|
|
|
87
101
|
if (frame.type === 'error') {
|
|
88
|
-
onError?.(frame.error, frame.message);
|
|
102
|
+
onError?.(new FirehoseError(frame.error, frame.message));
|
|
89
103
|
return;
|
|
90
104
|
}
|
|
91
105
|
|
|
92
106
|
let body = addTypeToBody(frame.body, frame.discriminator, nsid.nsid);
|
|
93
107
|
|
|
94
|
-
if (
|
|
108
|
+
if (validateEvents && nsid.message !== null) {
|
|
95
109
|
const result = safeParse(nsid.message, body);
|
|
96
110
|
if (!result.ok) {
|
|
111
|
+
if (onError) {
|
|
112
|
+
try {
|
|
113
|
+
result.throw();
|
|
114
|
+
} catch (err) {
|
|
115
|
+
onError(err);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
97
118
|
return;
|
|
98
119
|
}
|
|
99
120
|
body = result.value;
|
package/lib/types.ts
CHANGED
|
@@ -49,15 +49,19 @@ export interface FirehoseSubscriptionOptions<TSchema extends XRPCSubscriptionMet
|
|
|
49
49
|
params?: ParamsOf<TSchema> | (() => ParamsOf<TSchema>);
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
|
-
* whether to validate incoming
|
|
52
|
+
* whether to validate incoming events against the schema
|
|
53
53
|
* @default true
|
|
54
54
|
*/
|
|
55
|
-
|
|
55
|
+
validateEvents?: boolean;
|
|
56
56
|
|
|
57
57
|
onConnectionOpen?: (event: Event) => void;
|
|
58
58
|
onConnectionClose?: (event: CloseEvent) => void;
|
|
59
59
|
onConnectionError?: (event: ErrorEvent) => void;
|
|
60
|
-
|
|
60
|
+
/**
|
|
61
|
+
* called for non-fatal frame-level errors: atproto error frames (passed as `FirehoseError`)
|
|
62
|
+
* and message validation failures (passed as `ValidationError`).
|
|
63
|
+
*/
|
|
64
|
+
onError?: (err: unknown) => void;
|
|
61
65
|
|
|
62
66
|
/**
|
|
63
67
|
* WebSocket connection options
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atcute/firehose",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "lightweight and cute XRPC subscription client for AT Protocol",
|
|
5
5
|
"license": "0BSD",
|
|
6
6
|
"repository": {
|
|
@@ -27,14 +27,14 @@
|
|
|
27
27
|
"partysocket": "^1.1.18",
|
|
28
28
|
"type-fest": "^4.41.0",
|
|
29
29
|
"@atcute/cbor": "^2.3.3",
|
|
30
|
-
"@atcute/lexicons": "^
|
|
30
|
+
"@atcute/lexicons": "^2.0.0",
|
|
31
31
|
"@atcute/uint8array": "^1.1.1"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@atcute/atproto": "^
|
|
34
|
+
"@atcute/atproto": "^4.0.0"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"@atcute/lexicons": "^
|
|
37
|
+
"@atcute/lexicons": "^2.0.0"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "tsgo",
|