@atproto/tap 0.1.3 → 0.2.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/CHANGELOG.md +17 -0
- package/LICENSE.txt +1 -1
- package/dist/channel.d.ts.map +1 -1
- package/dist/channel.js +15 -10
- package/dist/channel.js.map +1 -1
- package/dist/lex-indexer.d.ts.map +1 -1
- package/dist/lex-indexer.js +8 -6
- package/dist/lex-indexer.js.map +1 -1
- package/dist/types.d.ts +107 -258
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +33 -30
- package/dist/types.js.map +1 -1
- package/package.json +8 -9
- package/src/channel.ts +17 -13
- package/src/lex-indexer.ts +14 -8
- package/src/types.ts +41 -37
- package/tests/_util.ts +24 -1
- package/tests/channel.test.ts +40 -48
- package/tests/client.test.ts +6 -5
- package/tests/lex-indexer.test.ts +4 -1
- package/tests/simple-indexer.test.ts +1 -0
- package/tests/util.test.ts +1 -0
- package/tsconfig.json +4 -1
- package/tsconfig.tests.json +9 -0
- package/vitest.config.ts +5 -0
- package/jest.config.js +0 -10
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @atproto/tap
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#4532](https://github.com/bluesky-social/atproto/pull/4532) [`aaedafc`](https://github.com/bluesky-social/atproto/commit/aaedafc6baef106b85e0954d8474cec21c00c1c2) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Expose `record` data as parsed atproto data (including CIDs and Uint8Arrays)
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- [#4532](https://github.com/bluesky-social/atproto/pull/4532) [`aaedafc`](https://github.com/bluesky-social/atproto/commit/aaedafc6baef106b85e0954d8474cec21c00c1c2) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Replace event validation from "zod" to "@atproto/lex"
|
|
12
|
+
|
|
13
|
+
- [#4562](https://github.com/bluesky-social/atproto/pull/4562) [`7310b97`](https://github.com/bluesky-social/atproto/commit/7310b9704de678a3b389a741784d58bb7f79b10b) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Add validation error as cause when handling invalid records
|
|
14
|
+
|
|
15
|
+
- Updated dependencies [[`99963d0`](https://github.com/bluesky-social/atproto/commit/99963d002a9e030e79aed5fba700e0a68f31e101), [`99963d0`](https://github.com/bluesky-social/atproto/commit/99963d002a9e030e79aed5fba700e0a68f31e101), [`7310b97`](https://github.com/bluesky-social/atproto/commit/7310b9704de678a3b389a741784d58bb7f79b10b), [`99963d0`](https://github.com/bluesky-social/atproto/commit/99963d002a9e030e79aed5fba700e0a68f31e101), [`7310b97`](https://github.com/bluesky-social/atproto/commit/7310b9704de678a3b389a741784d58bb7f79b10b), [`99963d0`](https://github.com/bluesky-social/atproto/commit/99963d002a9e030e79aed5fba700e0a68f31e101), [`7310b97`](https://github.com/bluesky-social/atproto/commit/7310b9704de678a3b389a741784d58bb7f79b10b), [`7310b97`](https://github.com/bluesky-social/atproto/commit/7310b9704de678a3b389a741784d58bb7f79b10b)]:
|
|
16
|
+
- @atproto/syntax@0.4.3
|
|
17
|
+
- @atproto/lex@0.0.12
|
|
18
|
+
- @atproto/common@0.5.9
|
|
19
|
+
|
|
3
20
|
## 0.1.3
|
|
4
21
|
|
|
5
22
|
### Patch Changes
|
package/LICENSE.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Dual MIT/Apache-2.0 License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2022-
|
|
3
|
+
Copyright (c) 2022-2026 Bluesky Social PBC, and Contributors
|
|
4
4
|
|
|
5
5
|
Except as otherwise noted in individual files, this software is licensed under the MIT license (<http://opensource.org/licenses/MIT>), or the Apache License, Version 2.0 (<http://www.apache.org/licenses/LICENSE-2.0>).
|
|
6
6
|
|
package/dist/channel.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../src/channel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,IAAI,CAAA;
|
|
1
|
+
{"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../src/channel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,IAAI,CAAA;AAIlC,OAAO,EAAE,QAAQ,EAAiB,MAAM,SAAS,CAAA;AAGjD,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,WAAW,CAAA;IACnB,GAAG,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CACzB;AAED,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,CAAC,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,WAAW,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IACnE,OAAO,EAAE,CAAC,GAAG,EAAE,KAAK,KAAK,IAAI,CAAA;CAC9B;AAED,MAAM,MAAM,mBAAmB,GAAG,aAAa,GAAG;IAChD,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,mBAAmB,CAAC,EAAE,MAAM,CAAA;IAC5B,mBAAmB,CAAC,EAAE,MAAM,CAAA;IAC5B,gBAAgB,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,YAAY,EAAE,OAAO,KAAK,IAAI,CAAA;CAC9E,CAAA;AAOD,qBAAa,UAAW,YAAW,eAAe;IAChD,OAAO,CAAC,EAAE,CAAoB;IAC9B,OAAO,CAAC,OAAO,CAAY;IAE3B,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAyC;IACzE,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAiC;IAE9D,OAAO,CAAC,YAAY,CAAoB;gBAGtC,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,UAAU,EACnB,MAAM,GAAE,mBAAwB;IAoB5B,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;YAY3B,OAAO;YAKP,gBAAgB;YAShB,iBAAiB;IAqBzB,KAAK;YAeG,cAAc;IA6BtB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAKxB,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC;CAG7C"}
|
package/dist/channel.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TapChannel = void 0;
|
|
4
4
|
const common_1 = require("@atproto/common");
|
|
5
|
+
const lex_1 = require("@atproto/lex");
|
|
5
6
|
const ws_client_1 = require("@atproto/ws-client");
|
|
6
7
|
const types_1 = require("./types");
|
|
7
8
|
const util_1 = require("./util");
|
|
@@ -90,10 +91,9 @@ class TapChannel {
|
|
|
90
91
|
ack.defer.resolve();
|
|
91
92
|
this.bufferedAcks = this.bufferedAcks.slice(1);
|
|
92
93
|
}
|
|
93
|
-
catch (
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}));
|
|
94
|
+
catch (cause) {
|
|
95
|
+
const error = new Error(`failed to send ack for event ${this.bufferedAcks[0]}`, { cause });
|
|
96
|
+
this.handler.onError(error);
|
|
97
97
|
return;
|
|
98
98
|
}
|
|
99
99
|
}
|
|
@@ -117,11 +117,15 @@ class TapChannel {
|
|
|
117
117
|
async processWsEvent(chunk) {
|
|
118
118
|
let evt;
|
|
119
119
|
try {
|
|
120
|
-
const data = chunk.toString()
|
|
121
|
-
|
|
120
|
+
const data = (0, lex_1.lexParse)(chunk.toString(), {
|
|
121
|
+
// Reject invalid CIDs and blobs
|
|
122
|
+
strict: true,
|
|
123
|
+
});
|
|
124
|
+
evt = (0, types_1.parseTapEvent)(data);
|
|
122
125
|
}
|
|
123
|
-
catch (
|
|
124
|
-
|
|
126
|
+
catch (cause) {
|
|
127
|
+
const error = new Error(`Failed to parse message`, { cause });
|
|
128
|
+
this.handler.onError(error);
|
|
125
129
|
return;
|
|
126
130
|
}
|
|
127
131
|
try {
|
|
@@ -132,9 +136,10 @@ class TapChannel {
|
|
|
132
136
|
},
|
|
133
137
|
});
|
|
134
138
|
}
|
|
135
|
-
catch (
|
|
139
|
+
catch (cause) {
|
|
136
140
|
// Don't ack on error - let Tap retry
|
|
137
|
-
|
|
141
|
+
const error = new Error(`Failed to process event ${evt.id}`, { cause });
|
|
142
|
+
this.handler.onError(error);
|
|
138
143
|
return;
|
|
139
144
|
}
|
|
140
145
|
}
|
package/dist/channel.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"channel.js","sourceRoot":"","sources":["../src/channel.ts"],"names":[],"mappings":";;;AACA,4CAA8D;AAC9D,kDAAuD;AACvD,mCAAiD;AACjD,iCAAgE;AAwBhE,MAAa,UAAU;IASrB,YACE,GAAW,EACX,OAAmB,EACnB,SAA8B,EAAE;QAX1B;;;;;WAAsB;QACtB;;;;;WAAmB;QAEV;;;;mBAAmC,IAAI,eAAe,EAAE;WAAA;QACxD;;;;mBAA2B,IAAA,yBAAgB,GAAE;WAAA;QAEtD;;;;mBAA8B,EAAE;WAAA;QAOtC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,MAAM,EAAE,aAAa,EAAE,GAAG,IAAI,EAAE,GAAG,MAAM,CAAA;QACzC,IAAI,OAAO,GAAG,IAAI,CAAC,OAAO,CAAA;QAC1B,IAAI,aAAa,EAAE,CAAC;YAClB,OAAO,KAAP,OAAO,GAAK,EAAE,EAAA;YACd,OAAO,CAAC,eAAe,CAAC,GAAG,IAAA,4BAAqB,EAAC,aAAa,CAAC,CAAA;QACjE,CAAC;QACD,IAAI,CAAC,EAAE,GAAG,IAAI,8BAAkB,CAAC;YAC/B,MAAM,EAAE,KAAK,IAAI,EAAE,CAAC,GAAG;YACvB,WAAW,EAAE,GAAG,EAAE;gBAChB,IAAI,CAAC,iBAAiB,EAAE,CAAA;YAC1B,CAAC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AAM,EAAE,IAAI,CAAC,eAAe,CAAC,MAAM;gBACnC,GAAG,EAAE,KAAK,IAAI,EAAE;oBACd,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;gBAC7B,CAAC;aACF,CAAC,CAAA;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,qCAAqC;YACrC,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,2BAA2B,GAAG,CAAC,EAAE,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,CAAA;YACvE,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;YAC3B,OAAM;QACR,CAAC;IACH,CAAC;IAED,KAAK,CAAC,OAAO;QACX,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAA;QAC5B,MAAM,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAA;IAClC,CAAC;IAED,KAAK,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC;QACzB,MAAM,IAAI,CAAC,OAAO,EAAE,CAAA;IACtB,CAAC;CACF;AAnID,gCAmIC","sourcesContent":["import { ClientOptions } from 'ws'\nimport { Deferrable, createDeferrable } from '@atproto/common'\nimport { lexParse } from '@atproto/lex'\nimport { WebSocketKeepAlive } from '@atproto/ws-client'\nimport { TapEvent, parseTapEvent } from './types'\nimport { formatAdminAuthHeader, isCausedBySignal } from './util'\n\nexport interface HandlerOpts {\n signal: AbortSignal\n ack: () => Promise<void>\n}\n\nexport interface TapHandler {\n onEvent: (evt: TapEvent, opts: HandlerOpts) => void | Promise<void>\n onError: (err: Error) => void\n}\n\nexport type TapWebsocketOptions = ClientOptions & {\n adminPassword?: string\n maxReconnectSeconds?: number\n heartbeatIntervalMs?: number\n onReconnectError?: (error: unknown, n: number, initialSetup: boolean) => void\n}\n\ntype BufferedAck = {\n id: number\n defer: Deferrable\n}\n\nexport class TapChannel implements AsyncDisposable {\n private ws: WebSocketKeepAlive\n private handler: TapHandler\n\n private readonly abortController: AbortController = new AbortController()\n private readonly destroyDefer: Deferrable = createDeferrable()\n\n private bufferedAcks: BufferedAck[] = []\n\n constructor(\n url: string,\n handler: TapHandler,\n wsOpts: TapWebsocketOptions = {},\n ) {\n this.handler = handler\n const { adminPassword, ...rest } = wsOpts\n let headers = rest.headers\n if (adminPassword) {\n headers ??= {}\n headers['Authorization'] = formatAdminAuthHeader(adminPassword)\n }\n this.ws = new WebSocketKeepAlive({\n getUrl: async () => url,\n onReconnect: () => {\n this.flushBufferedAcks()\n },\n signal: this.abortController.signal,\n ...rest,\n headers,\n })\n }\n\n async ackEvent(id: number): Promise<void> {\n if (this.ws.isConnected()) {\n try {\n await this.sendAck(id)\n } catch {\n await this.bufferAndSendAck(id)\n }\n } else {\n await this.bufferAndSendAck(id)\n }\n }\n\n private async sendAck(id: number): Promise<void> {\n await this.ws.send(JSON.stringify({ type: 'ack', id }))\n }\n\n // resolves after the ack has been actually sent\n private async bufferAndSendAck(id: number): Promise<void> {\n const defer = createDeferrable()\n this.bufferedAcks.push({\n id,\n defer,\n })\n await defer.complete\n }\n\n private async flushBufferedAcks(): Promise<void> {\n while (this.bufferedAcks.length > 0) {\n try {\n const ack = this.bufferedAcks.at(0)\n if (!ack) {\n return\n }\n await this.sendAck(ack.id)\n ack.defer.resolve()\n this.bufferedAcks = this.bufferedAcks.slice(1)\n } catch (cause) {\n const error = new Error(\n `failed to send ack for event ${this.bufferedAcks[0]}`,\n { cause },\n )\n this.handler.onError(error)\n return\n }\n }\n }\n\n async start() {\n this.abortController.signal.throwIfAborted()\n try {\n for await (const chunk of this.ws) {\n await this.processWsEvent(chunk)\n }\n } catch (err) {\n if (!isCausedBySignal(err, this.abortController.signal)) {\n throw err\n }\n } finally {\n this.destroyDefer.resolve()\n }\n }\n\n private async processWsEvent(chunk: Uint8Array) {\n let evt: TapEvent\n try {\n const data = lexParse(chunk.toString(), {\n // Reject invalid CIDs and blobs\n strict: true,\n })\n evt = parseTapEvent(data)\n } catch (cause) {\n const error = new Error(`Failed to parse message`, { cause })\n this.handler.onError(error)\n return\n }\n\n try {\n await this.handler.onEvent(evt, {\n signal: this.abortController.signal,\n ack: async () => {\n await this.ackEvent(evt.id)\n },\n })\n } catch (cause) {\n // Don't ack on error - let Tap retry\n const error = new Error(`Failed to process event ${evt.id}`, { cause })\n this.handler.onError(error)\n return\n }\n }\n\n async destroy(): Promise<void> {\n this.abortController.abort()\n await this.destroyDefer.complete\n }\n\n async [Symbol.asyncDispose](): Promise<void> {\n await this.destroy()\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lex-indexer.d.ts","sourceRoot":"","sources":["../src/lex-indexer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,YAAY,EAAW,MAAM,cAAc,CAAA;AAEjE,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AACnD,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAE9D,KAAK,eAAe,GAAG,IAAI,CAAC,WAAW,EAAE,QAAQ,GAAG,QAAQ,GAAG,KAAK,CAAC,CAAA;AAErE,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI,eAAe,GAAG;IAC7C,MAAM,EAAE,QAAQ,CAAA;IAChB,MAAM,EAAE,CAAC,CAAA;IACT,GAAG,EAAE,MAAM,CAAA;CACZ,CAAA;AAED,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI,eAAe,GAAG;IAC7C,MAAM,EAAE,QAAQ,CAAA;IAChB,MAAM,EAAE,CAAC,CAAA;IACT,GAAG,EAAE,MAAM,CAAA;CACZ,CAAA;AAED,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI,WAAW,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAA;AAEzD,MAAM,MAAM,WAAW,GAAG,eAAe,GAAG;IAC1C,MAAM,EAAE,QAAQ,C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C,CAAC,EACX,OAAO,EAAE,aAAa,GACrB,IAAI;IAIP,GAAG,CAAC,KAAK,CAAC,CAAC,SAAS,YAAY,EAC9B,EAAE,EAAE,IAAI,CAAC,CAAC,CAAC,EACX,OAAO,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAC5B,IAAI;IAMP,KAAK,CAAC,EAAE,EAAE,cAAc,GAAG,IAAI;IAQ/B,QAAQ,CAAC,EAAE,EAAE,eAAe,GAAG,IAAI;IAQnC,KAAK,CAAC,EAAE,EAAE,YAAY,GAAG,IAAI;IAQvB,OAAO,CAAC,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;YAShD,iBAAiB;IA0B/B,OAAO,CAAC,GAAG,EAAE,KAAK,GAAG,IAAI;CAO1B"}
|
package/dist/lex-indexer.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LexIndexer = void 0;
|
|
4
4
|
const lex_1 = require("@atproto/lex");
|
|
5
|
-
const syntax_1 = require("@atproto/syntax");
|
|
6
5
|
class LexIndexer {
|
|
7
6
|
constructor() {
|
|
8
7
|
Object.defineProperty(this, "handlers", {
|
|
@@ -53,7 +52,8 @@ class LexIndexer {
|
|
|
53
52
|
}
|
|
54
53
|
put(ns, handler) {
|
|
55
54
|
this.register('create', ns, handler);
|
|
56
|
-
|
|
55
|
+
this.register('update', ns, handler);
|
|
56
|
+
return this;
|
|
57
57
|
}
|
|
58
58
|
other(fn) {
|
|
59
59
|
if (this.otherHandler) {
|
|
@@ -94,10 +94,12 @@ class LexIndexer {
|
|
|
94
94
|
return;
|
|
95
95
|
}
|
|
96
96
|
if (action === 'create' || action === 'update') {
|
|
97
|
-
const match = registered.schema.
|
|
98
|
-
if (!match) {
|
|
99
|
-
const uriStr =
|
|
100
|
-
throw new Error(`Record validation failed for ${uriStr}
|
|
97
|
+
const match = registered.schema.safeValidate(evt.record);
|
|
98
|
+
if (!match.success) {
|
|
99
|
+
const uriStr = `at://${evt.did}/${evt.collection}/${evt.rkey}`;
|
|
100
|
+
throw new Error(`Record validation failed for ${uriStr}`, {
|
|
101
|
+
cause: match.reason,
|
|
102
|
+
});
|
|
101
103
|
}
|
|
102
104
|
}
|
|
103
105
|
await registered.handler(evt, opts);
|
package/dist/lex-indexer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lex-indexer.js","sourceRoot":"","sources":["../src/lex-indexer.ts"],"names":[],"mappings":";;;AAAA,sCAAiE;
|
|
1
|
+
{"version":3,"file":"lex-indexer.js","sourceRoot":"","sources":["../src/lex-indexer.ts"],"names":[],"mappings":";;;AAAA,sCAAiE;AAqEjE,MAAa,UAAU;IAAvB;QACU;;;;mBAAW,IAAI,GAAG,EAA6B;WAAA;QAC/C;;;;;WAAwC;QACxC;;;;;WAA4C;QAC5C;;;;;WAAsC;IAuHhD,CAAC;IArHS,UAAU,CAChB,UAAsB,EACtB,MAA6B;QAE7B,OAAO,GAAG,UAAU,IAAI,MAAM,EAAE,CAAA;IAClC,CAAC;IAEO,QAAQ,CACd,MAA6B,EAC7B,EAAW,EACX,OAAgC;QAEhC,MAAM,MAAM,GAAG,IAAA,aAAO,EAAC,EAAE,CAAC,CAAA;QAC1B,MAAM,GAAG,GAAG,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;QACjD,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YAC3B,MAAM,IAAI,KAAK,CAAC,kCAAkC,GAAG,EAAE,CAAC,CAAA;QAC1D,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAA;QAC3C,OAAO,IAAI,CAAA;IACb,CAAC;IAED,MAAM,CACJ,EAAW,EACX,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IACzD,CAAC;IAED,OAAO,CAAC,GAAU;QAChB,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;YACtB,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,CAAA;QACxB,CAAC;aAAM,CAAC;YACN,MAAM,GAAG,CAAA;QACX,CAAC;IACH,CAAC;CACF;AA3HD,gCA2HC","sourcesContent":["import { Infer, Main, RecordSchema, getMain } from '@atproto/lex'\nimport { AtUriString, NsidString } from '@atproto/syntax'\nimport { HandlerOpts, TapHandler } from './channel'\nimport { IdentityEvent, RecordEvent, TapEvent } from './types'\n\ntype BaseRecordEvent = Omit<RecordEvent, 'record' | 'action' | 'cid'>\n\nexport type CreateEvent<R> = BaseRecordEvent & {\n action: 'create'\n record: R\n cid: string\n}\n\nexport type UpdateEvent<R> = BaseRecordEvent & {\n action: 'update'\n record: R\n cid: string\n}\n\nexport type PutEvent<R> = CreateEvent<R> | UpdateEvent<R>\n\nexport type DeleteEvent = BaseRecordEvent & {\n action: 'delete'\n}\n\nexport type CreateHandler<R> = (\n evt: CreateEvent<R>,\n opts: HandlerOpts,\n) => Promise<void>\n\nexport type UpdateHandler<R> = (\n evt: UpdateEvent<R>,\n opts: HandlerOpts,\n) => Promise<void>\n\nexport type PutHandler<R> = (\n evt: PutEvent<R>,\n opts: HandlerOpts,\n) => Promise<void>\n\nexport type DeleteHandler = (\n evt: DeleteEvent,\n opts: HandlerOpts,\n) => Promise<void>\n\nexport type UntypedHandler = (\n evt: RecordEvent,\n opts: HandlerOpts,\n) => Promise<void>\n\nexport type IdentityHandler = (\n evt: IdentityEvent,\n opts: HandlerOpts,\n) => Promise<void>\n\nexport type ErrorHandler = (err: Error) => void\n\nexport type RecordHandler<R> =\n | CreateHandler<R>\n | UpdateHandler<R>\n | PutHandler<R>\n | DeleteHandler\n\ninterface RegisteredHandler {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n handler: RecordHandler<any>\n schema: RecordSchema\n}\n\nexport class LexIndexer implements TapHandler {\n private handlers = new Map<string, RegisteredHandler>()\n private otherHandler: UntypedHandler | undefined\n private identityHandler: IdentityHandler | undefined\n private errorHandler: ErrorHandler | undefined\n\n private handlerKey(\n collection: NsidString,\n action: RecordEvent['action'],\n ): string {\n return `${collection}:${action}`\n }\n\n private register<const T extends RecordSchema>(\n action: RecordEvent['action'],\n ns: Main<T>,\n handler: RecordHandler<Infer<T>>,\n ): this {\n const schema = getMain(ns)\n const key = this.handlerKey(schema.$type, action)\n if (this.handlers.has(key)) {\n throw new Error(`Handler already registered for ${key}`)\n }\n this.handlers.set(key, { schema, handler })\n return this\n }\n\n create<const T extends RecordSchema>(\n ns: Main<T>,\n handler: CreateHandler<Infer<T>>,\n ): this {\n return this.register('create', ns, handler)\n }\n\n update<const T extends RecordSchema>(\n ns: Main<T>,\n handler: UpdateHandler<Infer<T>>,\n ): this {\n return this.register('update', ns, handler)\n }\n\n delete<const T extends RecordSchema>(\n ns: Main<T>,\n handler: DeleteHandler,\n ): this {\n return this.register('delete', ns, handler)\n }\n\n put<const T extends RecordSchema>(\n ns: Main<T>,\n handler: PutHandler<Infer<T>>,\n ): this {\n this.register('create', ns, handler)\n this.register('update', ns, handler)\n return this\n }\n\n other(fn: UntypedHandler): this {\n if (this.otherHandler) {\n throw new Error(`Handler already registered for \"other\"`)\n }\n this.otherHandler = fn\n return this\n }\n\n identity(fn: IdentityHandler): this {\n if (this.identityHandler) {\n throw new Error(`Handler already registered for \"identity\"`)\n }\n this.identityHandler = fn\n return this\n }\n\n error(fn: ErrorHandler): this {\n if (this.errorHandler) {\n throw new Error(`Handler already registered for \"error\"`)\n }\n this.errorHandler = fn\n return this\n }\n\n async onEvent(evt: TapEvent, opts: HandlerOpts): Promise<void> {\n if (evt.type === 'identity') {\n await this.identityHandler?.(evt, opts)\n } else {\n await this.handleRecordEvent(evt, opts)\n }\n await opts.ack()\n }\n\n private async handleRecordEvent(\n evt: RecordEvent,\n opts: HandlerOpts,\n ): Promise<void> {\n const { collection, action } = evt\n const key = this.handlerKey(collection, action)\n const registered = this.handlers.get(key)\n\n if (!registered) {\n await this.otherHandler?.(evt, opts)\n return\n }\n\n if (action === 'create' || action === 'update') {\n const match = registered.schema.safeValidate(evt.record)\n if (!match.success) {\n const uriStr: AtUriString = `at://${evt.did}/${evt.collection}/${evt.rkey}`\n throw new Error(`Record validation failed for ${uriStr}`, {\n cause: match.reason,\n })\n }\n }\n\n await (registered.handler as UntypedHandler)(evt, opts)\n }\n\n onError(err: Error): void {\n if (this.errorHandler) {\n this.errorHandler(err)\n } else {\n throw err\n }\n }\n}\n"]}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,286 +1,135 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}, {
|
|
21
|
-
did: string;
|
|
22
|
-
rev: string;
|
|
23
|
-
collection: string;
|
|
24
|
-
rkey: string;
|
|
25
|
-
action: "create" | "update" | "delete";
|
|
26
|
-
live: boolean;
|
|
27
|
-
record?: Record<string, unknown> | undefined;
|
|
28
|
-
cid?: string | undefined;
|
|
1
|
+
import { LexMap, LexValue, l } from '@atproto/lex';
|
|
2
|
+
import { DidString, HandleString, NsidString } from '@atproto/syntax';
|
|
3
|
+
export declare const recordEventDataSchema: l.ObjectSchema<{
|
|
4
|
+
readonly did: l.StringSchema<{
|
|
5
|
+
readonly format: "did";
|
|
6
|
+
}>;
|
|
7
|
+
readonly rev: l.StringSchema<{}>;
|
|
8
|
+
readonly collection: l.StringSchema<{
|
|
9
|
+
readonly format: "nsid";
|
|
10
|
+
}>;
|
|
11
|
+
readonly rkey: l.StringSchema<{
|
|
12
|
+
readonly format: "record-key";
|
|
13
|
+
}>;
|
|
14
|
+
readonly action: l.EnumSchema<"create" | "update" | "delete">;
|
|
15
|
+
readonly record: l.OptionalSchema<l.UnknownObjectSchema>;
|
|
16
|
+
readonly cid: l.OptionalSchema<l.StringSchema<{
|
|
17
|
+
readonly format: "cid";
|
|
18
|
+
}>>;
|
|
19
|
+
readonly live: l.BooleanSchema;
|
|
29
20
|
}>;
|
|
30
|
-
export declare const identityEventDataSchema:
|
|
31
|
-
did:
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
is_active: boolean;
|
|
40
|
-
}, {
|
|
41
|
-
did: string;
|
|
42
|
-
status: "active" | "takendown" | "suspended" | "deactivated" | "deleted";
|
|
43
|
-
handle: string;
|
|
44
|
-
is_active: boolean;
|
|
21
|
+
export declare const identityEventDataSchema: l.ObjectSchema<{
|
|
22
|
+
readonly did: l.StringSchema<{
|
|
23
|
+
readonly format: "did";
|
|
24
|
+
}>;
|
|
25
|
+
readonly handle: l.StringSchema<{
|
|
26
|
+
readonly format: "handle";
|
|
27
|
+
}>;
|
|
28
|
+
readonly is_active: l.BooleanSchema;
|
|
29
|
+
readonly status: l.EnumSchema<"active" | "takendown" | "suspended" | "deactivated" | "deleted">;
|
|
45
30
|
}>;
|
|
46
|
-
export declare const recordEventSchema:
|
|
47
|
-
id:
|
|
48
|
-
type:
|
|
49
|
-
record:
|
|
50
|
-
did:
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
cid?: string | undefined;
|
|
67
|
-
}, {
|
|
68
|
-
did: string;
|
|
69
|
-
rev: string;
|
|
70
|
-
collection: string;
|
|
71
|
-
rkey: string;
|
|
72
|
-
action: "create" | "update" | "delete";
|
|
73
|
-
live: boolean;
|
|
74
|
-
record?: Record<string, unknown> | undefined;
|
|
75
|
-
cid?: string | undefined;
|
|
31
|
+
export declare const recordEventSchema: l.ObjectSchema<{
|
|
32
|
+
readonly id: l.IntegerSchema;
|
|
33
|
+
readonly type: l.LiteralSchema<"record">;
|
|
34
|
+
readonly record: l.ObjectSchema<{
|
|
35
|
+
readonly did: l.StringSchema<{
|
|
36
|
+
readonly format: "did";
|
|
37
|
+
}>;
|
|
38
|
+
readonly rev: l.StringSchema<{}>;
|
|
39
|
+
readonly collection: l.StringSchema<{
|
|
40
|
+
readonly format: "nsid";
|
|
41
|
+
}>;
|
|
42
|
+
readonly rkey: l.StringSchema<{
|
|
43
|
+
readonly format: "record-key";
|
|
44
|
+
}>;
|
|
45
|
+
readonly action: l.EnumSchema<"create" | "update" | "delete">;
|
|
46
|
+
readonly record: l.OptionalSchema<l.UnknownObjectSchema>;
|
|
47
|
+
readonly cid: l.OptionalSchema<l.StringSchema<{
|
|
48
|
+
readonly format: "cid";
|
|
49
|
+
}>>;
|
|
50
|
+
readonly live: l.BooleanSchema;
|
|
76
51
|
}>;
|
|
77
|
-
}, "strip", z.ZodTypeAny, {
|
|
78
|
-
type: "record";
|
|
79
|
-
record: {
|
|
80
|
-
did: string;
|
|
81
|
-
rev: string;
|
|
82
|
-
collection: string;
|
|
83
|
-
rkey: string;
|
|
84
|
-
action: "create" | "update" | "delete";
|
|
85
|
-
live: boolean;
|
|
86
|
-
record?: Record<string, unknown> | undefined;
|
|
87
|
-
cid?: string | undefined;
|
|
88
|
-
};
|
|
89
|
-
id: number;
|
|
90
|
-
}, {
|
|
91
|
-
type: "record";
|
|
92
|
-
record: {
|
|
93
|
-
did: string;
|
|
94
|
-
rev: string;
|
|
95
|
-
collection: string;
|
|
96
|
-
rkey: string;
|
|
97
|
-
action: "create" | "update" | "delete";
|
|
98
|
-
live: boolean;
|
|
99
|
-
record?: Record<string, unknown> | undefined;
|
|
100
|
-
cid?: string | undefined;
|
|
101
|
-
};
|
|
102
|
-
id: number;
|
|
103
52
|
}>;
|
|
104
|
-
export declare const identityEventSchema:
|
|
105
|
-
id:
|
|
106
|
-
type:
|
|
107
|
-
identity:
|
|
108
|
-
did:
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
is_active: boolean;
|
|
117
|
-
}, {
|
|
118
|
-
did: string;
|
|
119
|
-
status: "active" | "takendown" | "suspended" | "deactivated" | "deleted";
|
|
120
|
-
handle: string;
|
|
121
|
-
is_active: boolean;
|
|
53
|
+
export declare const identityEventSchema: l.ObjectSchema<{
|
|
54
|
+
readonly id: l.IntegerSchema;
|
|
55
|
+
readonly type: l.LiteralSchema<"identity">;
|
|
56
|
+
readonly identity: l.ObjectSchema<{
|
|
57
|
+
readonly did: l.StringSchema<{
|
|
58
|
+
readonly format: "did";
|
|
59
|
+
}>;
|
|
60
|
+
readonly handle: l.StringSchema<{
|
|
61
|
+
readonly format: "handle";
|
|
62
|
+
}>;
|
|
63
|
+
readonly is_active: l.BooleanSchema;
|
|
64
|
+
readonly status: l.EnumSchema<"active" | "takendown" | "suspended" | "deactivated" | "deleted">;
|
|
122
65
|
}>;
|
|
123
|
-
}, "strip", z.ZodTypeAny, {
|
|
124
|
-
type: "identity";
|
|
125
|
-
id: number;
|
|
126
|
-
identity: {
|
|
127
|
-
did: string;
|
|
128
|
-
status: "active" | "takendown" | "suspended" | "deactivated" | "deleted";
|
|
129
|
-
handle: string;
|
|
130
|
-
is_active: boolean;
|
|
131
|
-
};
|
|
132
|
-
}, {
|
|
133
|
-
type: "identity";
|
|
134
|
-
id: number;
|
|
135
|
-
identity: {
|
|
136
|
-
did: string;
|
|
137
|
-
status: "active" | "takendown" | "suspended" | "deactivated" | "deleted";
|
|
138
|
-
handle: string;
|
|
139
|
-
is_active: boolean;
|
|
140
|
-
};
|
|
141
66
|
}>;
|
|
142
|
-
export declare const tapEventSchema:
|
|
143
|
-
id:
|
|
144
|
-
type:
|
|
145
|
-
record:
|
|
146
|
-
did:
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
cid?: string | undefined;
|
|
163
|
-
}, {
|
|
164
|
-
did: string;
|
|
165
|
-
rev: string;
|
|
166
|
-
collection: string;
|
|
167
|
-
rkey: string;
|
|
168
|
-
action: "create" | "update" | "delete";
|
|
169
|
-
live: boolean;
|
|
170
|
-
record?: Record<string, unknown> | undefined;
|
|
171
|
-
cid?: string | undefined;
|
|
67
|
+
export declare const tapEventSchema: l.DiscriminatedUnionSchema<"type", readonly [l.ObjectSchema<{
|
|
68
|
+
readonly id: l.IntegerSchema;
|
|
69
|
+
readonly type: l.LiteralSchema<"record">;
|
|
70
|
+
readonly record: l.ObjectSchema<{
|
|
71
|
+
readonly did: l.StringSchema<{
|
|
72
|
+
readonly format: "did";
|
|
73
|
+
}>;
|
|
74
|
+
readonly rev: l.StringSchema<{}>;
|
|
75
|
+
readonly collection: l.StringSchema<{
|
|
76
|
+
readonly format: "nsid";
|
|
77
|
+
}>;
|
|
78
|
+
readonly rkey: l.StringSchema<{
|
|
79
|
+
readonly format: "record-key";
|
|
80
|
+
}>;
|
|
81
|
+
readonly action: l.EnumSchema<"create" | "update" | "delete">;
|
|
82
|
+
readonly record: l.OptionalSchema<l.UnknownObjectSchema>;
|
|
83
|
+
readonly cid: l.OptionalSchema<l.StringSchema<{
|
|
84
|
+
readonly format: "cid";
|
|
85
|
+
}>>;
|
|
86
|
+
readonly live: l.BooleanSchema;
|
|
172
87
|
}>;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
id: number;
|
|
186
|
-
}, {
|
|
187
|
-
type: "record";
|
|
188
|
-
record: {
|
|
189
|
-
did: string;
|
|
190
|
-
rev: string;
|
|
191
|
-
collection: string;
|
|
192
|
-
rkey: string;
|
|
193
|
-
action: "create" | "update" | "delete";
|
|
194
|
-
live: boolean;
|
|
195
|
-
record?: Record<string, unknown> | undefined;
|
|
196
|
-
cid?: string | undefined;
|
|
197
|
-
};
|
|
198
|
-
id: number;
|
|
199
|
-
}>, z.ZodObject<{
|
|
200
|
-
id: z.ZodNumber;
|
|
201
|
-
type: z.ZodLiteral<"identity">;
|
|
202
|
-
identity: z.ZodObject<{
|
|
203
|
-
did: z.ZodString;
|
|
204
|
-
handle: z.ZodString;
|
|
205
|
-
is_active: z.ZodBoolean;
|
|
206
|
-
status: z.ZodEnum<["active", "takendown", "suspended", "deactivated", "deleted"]>;
|
|
207
|
-
}, "strip", z.ZodTypeAny, {
|
|
208
|
-
did: string;
|
|
209
|
-
status: "active" | "takendown" | "suspended" | "deactivated" | "deleted";
|
|
210
|
-
handle: string;
|
|
211
|
-
is_active: boolean;
|
|
212
|
-
}, {
|
|
213
|
-
did: string;
|
|
214
|
-
status: "active" | "takendown" | "suspended" | "deactivated" | "deleted";
|
|
215
|
-
handle: string;
|
|
216
|
-
is_active: boolean;
|
|
88
|
+
}>, l.ObjectSchema<{
|
|
89
|
+
readonly id: l.IntegerSchema;
|
|
90
|
+
readonly type: l.LiteralSchema<"identity">;
|
|
91
|
+
readonly identity: l.ObjectSchema<{
|
|
92
|
+
readonly did: l.StringSchema<{
|
|
93
|
+
readonly format: "did";
|
|
94
|
+
}>;
|
|
95
|
+
readonly handle: l.StringSchema<{
|
|
96
|
+
readonly format: "handle";
|
|
97
|
+
}>;
|
|
98
|
+
readonly is_active: l.BooleanSchema;
|
|
99
|
+
readonly status: l.EnumSchema<"active" | "takendown" | "suspended" | "deactivated" | "deleted">;
|
|
217
100
|
}>;
|
|
218
|
-
}, "strip", z.ZodTypeAny, {
|
|
219
|
-
type: "identity";
|
|
220
|
-
id: number;
|
|
221
|
-
identity: {
|
|
222
|
-
did: string;
|
|
223
|
-
status: "active" | "takendown" | "suspended" | "deactivated" | "deleted";
|
|
224
|
-
handle: string;
|
|
225
|
-
is_active: boolean;
|
|
226
|
-
};
|
|
227
|
-
}, {
|
|
228
|
-
type: "identity";
|
|
229
|
-
id: number;
|
|
230
|
-
identity: {
|
|
231
|
-
did: string;
|
|
232
|
-
status: "active" | "takendown" | "suspended" | "deactivated" | "deleted";
|
|
233
|
-
handle: string;
|
|
234
|
-
is_active: boolean;
|
|
235
|
-
};
|
|
236
101
|
}>]>;
|
|
237
102
|
export type RecordEvent = {
|
|
238
103
|
id: number;
|
|
239
104
|
type: 'record';
|
|
240
105
|
action: 'create' | 'update' | 'delete';
|
|
241
|
-
did:
|
|
106
|
+
did: DidString;
|
|
242
107
|
rev: string;
|
|
243
|
-
collection:
|
|
108
|
+
collection: NsidString;
|
|
244
109
|
rkey: string;
|
|
245
|
-
record?:
|
|
110
|
+
record?: LexMap;
|
|
246
111
|
cid?: string;
|
|
247
112
|
live: boolean;
|
|
248
113
|
};
|
|
249
114
|
export type IdentityEvent = {
|
|
250
115
|
id: number;
|
|
251
116
|
type: 'identity';
|
|
252
|
-
did:
|
|
253
|
-
handle:
|
|
117
|
+
did: DidString;
|
|
118
|
+
handle: HandleString;
|
|
254
119
|
isActive: boolean;
|
|
255
120
|
status: RepoStatus;
|
|
256
121
|
};
|
|
257
122
|
export type RepoStatus = 'active' | 'takendown' | 'suspended' | 'deactivated' | 'deleted';
|
|
258
123
|
export type TapEvent = IdentityEvent | RecordEvent;
|
|
259
|
-
export declare const parseTapEvent: (data:
|
|
260
|
-
export declare const repoInfoSchema:
|
|
261
|
-
did:
|
|
262
|
-
handle:
|
|
263
|
-
state:
|
|
264
|
-
rev:
|
|
265
|
-
records:
|
|
266
|
-
error:
|
|
267
|
-
retries:
|
|
268
|
-
}, "strip", z.ZodTypeAny, {
|
|
269
|
-
did: string;
|
|
270
|
-
rev: string;
|
|
271
|
-
handle: string;
|
|
272
|
-
state: string;
|
|
273
|
-
records: number;
|
|
274
|
-
error?: string | undefined;
|
|
275
|
-
retries?: number | undefined;
|
|
276
|
-
}, {
|
|
277
|
-
did: string;
|
|
278
|
-
rev: string;
|
|
279
|
-
handle: string;
|
|
280
|
-
state: string;
|
|
281
|
-
records: number;
|
|
282
|
-
error?: string | undefined;
|
|
283
|
-
retries?: number | undefined;
|
|
124
|
+
export declare const parseTapEvent: (data: LexValue) => TapEvent;
|
|
125
|
+
export declare const repoInfoSchema: l.ObjectSchema<{
|
|
126
|
+
readonly did: l.StringSchema<{}>;
|
|
127
|
+
readonly handle: l.StringSchema<{}>;
|
|
128
|
+
readonly state: l.StringSchema<{}>;
|
|
129
|
+
readonly rev: l.StringSchema<{}>;
|
|
130
|
+
readonly records: l.IntegerSchema;
|
|
131
|
+
readonly error: l.OptionalSchema<l.StringSchema<{}>>;
|
|
132
|
+
readonly retries: l.OptionalSchema<l.IntegerSchema>;
|
|
284
133
|
}>;
|
|
285
|
-
export type RepoInfo =
|
|
134
|
+
export type RepoInfo = l.Infer<typeof repoInfoSchema>;
|
|
286
135
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,EAAE,MAAM,cAAc,CAAA;AAClD,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAErE,eAAO,MAAM,qBAAqB;;;;;;;;;;;;;;;;;EAShC,CAAA;AAEF,eAAO,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