@spine-event-engine/core 2.0.0-snapshot.25 → 2.0.0-snapshot.26

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.
@@ -0,0 +1,162 @@
1
+ /*
2
+ * Copyright 2026, CodeMatters. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
+ * in compliance with the License. You may obtain a copy of the License at
6
+ *
7
+ * https://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
10
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
+ * or implied. See the License for the specific language governing permissions and limitations under
12
+ * the License.
13
+ */
14
+ import { base64Decode, base64Encode, getTextEncoding } from "@bufbuild/protobuf/wire";
15
+ import { snapshotTrustedReader } from "./trusted-reader.js";
16
+ const envelopeHeader = "spine-internal-reader";
17
+ const proofHeader = "spine-internal-reader-proof";
18
+ /**
19
+ * File-private assertion framing and bounded header decoding.
20
+ */
21
+ const ReaderForwardingWire = Object.freeze({
22
+ /**
23
+ * Decodes bounded assertion metadata and rejects malformed JSON shapes.
24
+ *
25
+ * @param encoded Base64 assertion envelope.
26
+ * @param proof Transport-safe proof value.
27
+ * @param maxBytes Existing native request-size bound.
28
+ * @returns Canonical envelope bytes and its parsed record.
29
+ */
30
+ decodeEnvelope(encoded, proof, maxBytes) {
31
+ try {
32
+ const payload = base64Decode(encoded);
33
+ ReaderForwardingWire.assertBound(payload.byteLength, maxBytes);
34
+ ReaderForwardingWire.assertBound(proof.length, maxBytes);
35
+ const decoded = JSON.parse(getTextEncoding().decodeUtf8(payload, true));
36
+ if (decoded === null || typeof decoded !== "object" || Array.isArray(decoded))
37
+ throw new Error("Invalid forwarding proof.");
38
+ return { payload, decoded };
39
+ }
40
+ catch (error) {
41
+ if (error instanceof Error &&
42
+ error.message === "Forwarded reader envelope exceeds request limit.")
43
+ throw error;
44
+ throw new Error("Invalid forwarding proof.");
45
+ }
46
+ },
47
+ /**
48
+ * Builds signed bytes so neither request nor envelope can be exchanged.
49
+ *
50
+ * @param payload Bounded internal reader envelope.
51
+ * @param request Canonical request message bytes.
52
+ * @returns Binary input for the configured proof port.
53
+ */
54
+ proofBytes(payload, request) {
55
+ const framed = new Uint8Array(payload.byteLength + 1 + request.byteLength);
56
+ framed.set(payload);
57
+ framed.set(request, payload.byteLength + 1);
58
+ return framed;
59
+ },
60
+ /**
61
+ * Applies the receiving native transport's existing message-size bound.
62
+ *
63
+ * @param size Serialized internal envelope bytes.
64
+ * @param maxBytes Configured native request-size bound.
65
+ */
66
+ assertBound(size, maxBytes) {
67
+ if (!Number.isSafeInteger(maxBytes) || maxBytes < 1 || size > maxBytes)
68
+ throw new Error("Forwarded reader envelope exceeds request limit.");
69
+ },
70
+ /**
71
+ * Reads one reserved header from a native header collection or plain record.
72
+ *
73
+ * @param headers Received header collection.
74
+ * @param name Lowercase reserved header name.
75
+ * @returns Header value when present.
76
+ */
77
+ headerValue(headers, name) {
78
+ return "get" in headers && typeof headers.get === "function"
79
+ ? (headers.get(name) ?? undefined)
80
+ : headers[name];
81
+ },
82
+ });
83
+ /**
84
+ * Package-internal wire conversion for per-request verified reader assertions.
85
+ * It does not copy the incoming transport credential or track session expiry.
86
+ * Application ports remain responsible for the facts they supply, and no
87
+ * assertion is accepted without a configured peer verifier.
88
+ *
89
+ * @internal
90
+ */
91
+ export const ReaderForwarding = Object.freeze({
92
+ /**
93
+ * Identifies a caller-supplied copy of internal reader metadata.
94
+ *
95
+ * @param headers Incoming native request headers.
96
+ * @returns Whether either reserved header is present.
97
+ */
98
+ hasAssertion(headers) {
99
+ return (ReaderForwardingWire.headerValue(headers, envelopeHeader) !== undefined ||
100
+ ReaderForwardingWire.headerValue(headers, proofHeader) !== undefined);
101
+ },
102
+ /**
103
+ * Encodes detached reader data against one canonical operation and message.
104
+ *
105
+ * @param service Canonical RPC service name.
106
+ * @param method Canonical RPC method name.
107
+ * @param value Canonical request message bytes.
108
+ * @param reader Gateway-verified reader and authority facts.
109
+ * @param issuer Configured peer proof producer.
110
+ * @param maxBytes Existing native request-size bound.
111
+ * @returns Internal metadata for one backend request.
112
+ */
113
+ async seal(service, method, value, reader, issuer, maxBytes) {
114
+ const payload = getTextEncoding().encodeUtf8(JSON.stringify({
115
+ service,
116
+ method,
117
+ reader: snapshotTrustedReader(reader),
118
+ }));
119
+ ReaderForwardingWire.assertBound(payload.byteLength, maxBytes);
120
+ const proof = await issuer.issue(ReaderForwardingWire.proofBytes(payload, value));
121
+ if (typeof proof !== "string" || !proof)
122
+ throw new Error("Invalid forwarding proof.");
123
+ ReaderForwardingWire.assertBound(proof.length, maxBytes);
124
+ return Object.freeze({ [envelopeHeader]: base64Encode(payload), [proofHeader]: proof });
125
+ },
126
+ /**
127
+ * Opens one bounded assertion only after the configured port authenticates its peer.
128
+ *
129
+ * @param headers Native request headers.
130
+ * @param service Canonical RPC service name.
131
+ * @param method Canonical RPC method name.
132
+ * @param value Canonical request message bytes.
133
+ * @param verifier Configured trusted-forwarding peer verifier.
134
+ * @param peer Receiving transport's peer context.
135
+ * @param maxBytes Existing native request-size bound.
136
+ * @returns Detached verified reader facts.
137
+ * @typeParam Peer Native transport peer context.
138
+ */
139
+ async open(headers, service, method, value, verifier, peer, maxBytes) {
140
+ const encoded = ReaderForwardingWire.headerValue(headers, envelopeHeader);
141
+ const proof = ReaderForwardingWire.headerValue(headers, proofHeader);
142
+ if (verifier === undefined || encoded === undefined || proof === undefined || !proof)
143
+ throw new Error("Invalid forwarding proof.");
144
+ const { payload, decoded } = ReaderForwardingWire.decodeEnvelope(encoded, proof, maxBytes);
145
+ if (!("service" in decoded) ||
146
+ decoded.service !== service ||
147
+ !("method" in decoded) ||
148
+ decoded.method !== method ||
149
+ !("reader" in decoded) ||
150
+ decoded.reader === undefined)
151
+ throw new Error("Invalid forwarding proof.");
152
+ if (!(await verifier.verify(ReaderForwardingWire.proofBytes(payload, value), proof, peer)))
153
+ throw new Error("Invalid forwarding proof.");
154
+ try {
155
+ return snapshotTrustedReader(decoded.reader);
156
+ }
157
+ catch {
158
+ throw new Error("Invalid forwarding proof.");
159
+ }
160
+ },
161
+ });
162
+ //# sourceMappingURL=reader-forwarding.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reader-forwarding.js","sourceRoot":"","sources":["../../src/spi/reader-forwarding.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AACtF,OAAO,EAAE,qBAAqB,EAA6B,MAAM,qBAAqB,CAAC;AAEvF,MAAM,cAAc,GAAG,uBAAuB,CAAC;AAC/C,MAAM,WAAW,GAAG,6BAA6B,CAAC;AAiDlD;;GAEG;AACH,MAAM,oBAAoB,GAAG,MAAM,CAAC,MAAM,CAAC;IACzC;;;;;;;OAOG;IACH,cAAc,CAAC,OAAe,EAAE,KAAa,EAAE,QAAgB;QAC7D,IAAI,CAAC;YACH,MAAM,OAAO,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;YACtC,oBAAoB,CAAC,WAAW,CAAC,OAAO,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC;YAC/D,oBAAoB,CAAC,WAAW,CAAC,KAAK,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;YACzD,MAAM,OAAO,GAAY,IAAI,CAAC,KAAK,CAAC,eAAe,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;YACjF,IAAI,OAAO,KAAK,IAAI,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC;gBAC3E,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;YAC/C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC;QAC9B,CAAC;QAAC,OAAO,KAAK,EAAE,CAA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AAK,SAAS,IAAI,KAAK,KAAK,SAAS,IAAI,CAAC,KAAK;YAClF,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;QAC/C,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,oBAAoB,CAAC,cAAc,CAAC,OAAO,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC;QAC3F,IACE,CAAC,CAAC,SAAS,IAAI,OAAO,CAAC;YACvB,OAAO,CAAC,OAAO,KAAK,OAAO;YAC3B,CAAC,CAAC,QAAQ,IAAI,OAAO,CAAC;YACtB,OAAO,CAAC,MAAM,KAAK,MAAM;YACzB,CAAC,CAAC,QAAQ,IAAI,OAAO,CAAC;YACtB,OAAO,CAAC,MAAM,KAAK,SAAS;YAE5B,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;QAC/C,IAAI,CAAC,CAAC,MAAM,QAAQ,CAAC,MAAM,CAAC,oBAAoB,CAAC,UAAU,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;YACxF,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;QAC/C,IAAI,CAAC;YACH,OAAO,qBAAqB,CAAC,OAAO,CAAC,MAA8B,CAAC,CAAC;QACvE,CAAC;QAAC,MAAM,CAAC;YACP,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;QAC/C,CAAC;IACH,CAAC;CACF,CAAC,CAAC"}
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Application identity established by a configured authentication port.
3
+ */
4
+ export interface TrustedReaderPrincipal {
5
+ /**
6
+ * Application-defined identity, distinct from an ActorContext user ID.
7
+ */
8
+ readonly id: string;
9
+ /**
10
+ * Detached attributes returned by the configured authenticator.
11
+ */
12
+ readonly attributes?: Readonly<Record<string, string>>;
13
+ }
14
+ /**
15
+ * JSON-compatible authority value returned by an application authority port.
16
+ */
17
+ export type AuthorityFact = string | number | boolean | null | readonly AuthorityFact[] | Readonly<{
18
+ [name: string]: AuthorityFact;
19
+ }>;
20
+ /**
21
+ * Per-invocation identity and authority facts established by trusted ports.
22
+ * Absence of a principal is valid for deliberately public admission.
23
+ *
24
+ * @internal
25
+ */
26
+ export interface TrustedReaderContext {
27
+ /**
28
+ * Verified application principal when this invocation authenticated a reader.
29
+ */
30
+ readonly principal?: TrustedReaderPrincipal;
31
+ /**
32
+ * Application-provided authority facts, independent of provider credentials.
33
+ */
34
+ readonly authorityFacts?: Readonly<Record<string, AuthorityFact>>;
35
+ }
36
+ /**
37
+ * Stable subscription identity; authority facts are deliberately excluded.
38
+ */
39
+ export type ReaderIdentity = Readonly<{
40
+ /**
41
+ * Distinguishes a verified principal from explicit anonymous admission.
42
+ */
43
+ kind: "principal";
44
+ /**
45
+ * Stable identifier retained across reconnects.
46
+ */
47
+ id: string;
48
+ }> | Readonly<{
49
+ /**
50
+ * Marks a deliberately public reader without a principal.
51
+ */
52
+ kind: "anonymous";
53
+ }>;
54
+ /**
55
+ * Maps verified invocation facts to a stable subscription identity.
56
+ */
57
+ export declare const ReaderIdentities: Readonly<{
58
+ /**
59
+ * Converts verified invocation facts to a stable subscription identity.
60
+ *
61
+ * @param reader The verified reader facts for this operation.
62
+ * @returns The principal ID or explicit anonymous binding.
63
+ */
64
+ from(reader: TrustedReaderContext): ReaderIdentity;
65
+ /**
66
+ * Checks whether a verified reader matches a retained binding.
67
+ *
68
+ * @param expected Persisted identity, possibly absent in a legacy record.
69
+ * @param actual Verified invocation identity.
70
+ * @returns Whether this reader may continue the subscription.
71
+ */
72
+ matches(expected: ReaderIdentity | undefined, actual: ReaderIdentity): boolean;
73
+ /**
74
+ * Compares two optional retained reader identities.
75
+ *
76
+ * @param left First stored reader identity.
77
+ * @param right Second stored reader identity.
78
+ * @returns Whether both identities are equal, including legacy absence.
79
+ */
80
+ same(left: ReaderIdentity | undefined, right: ReaderIdentity | undefined): boolean;
81
+ }>;
82
+ /**
83
+ * Public SPI alias for the grouped immutable reader snapshot operation.
84
+ */
85
+ export declare const snapshotTrustedReader: (reader: TrustedReaderContext) => TrustedReaderContext;
86
+ //# sourceMappingURL=trusted-reader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trusted-reader.d.ts","sourceRoot":"","sources":["../../src/spi/trusted-reader.ts"],"names":[],"mappings":"AAcA;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CACxD;AAED;;GAEG;AACH,MAAM,MAAM,aAAa,GACrB,MAAM,GACN,MAAM,GACN,OAAO,GACP,IAAI,GACJ,SAAS,aAAa,EAAE,GACxB,QAAQ,CAAC;IAAE,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,CAAA;CAAE,CAAC,CAAC;AAEhD;;;;;GAKG;AACH,MAAM,WAAW,oBAAoB;IACnC;;OAEG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,sBAAsB,CAAC;IAE5C;;OAEG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC;CACnE;AAED;;GAEG;AACH,MAAM,MAAM,cAAc,GACtB,QAAQ,CAAC;IACP;;OAEG;IACH,IAAI,EAAE,WAAW,CAAC;IAElB;;OAEG;IACH,EAAE,EAAE,MAAM,CAAC;CACZ,CAAC,GACF,QAAQ,CAAC;IACP;;OAEG;IACH,IAAI,EAAE,WAAW,CAAC;CACnB,CAAC,CAAC;AAEP;;GAEG;AACH,eAAO,MAAM,gBAAgB,EAAE,QAAQ,CAAC;IACtC;;;;;OAKG;IACH,IAAI,CAAC,MAAM,EAAE,oBAAoB,GAAG,cAAc,CAAC;IAEnD;;;;;;OAMG;IACH,OAAO,CAAC,QAAQ,EAAE,cAAc,GAAG,SAAS,EAAE,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC;IAE/E;;;;;;OAMG;IACH,IAAI,CAAC,IAAI,EAAE,cAAc,GAAG,SAAS,EAAE,KAAK,EAAE,cAAc,GAAG,SAAS,GAAG,OAAO,CAAC;CACpF,CAwCC,CAAC;AAgGH;;GAEG;AACH,eAAO,MAAM,qBAAqB,EAAE,CAAC,MAAM,EAAE,oBAAoB,KAAK,oBACrC,CAAC"}
@@ -0,0 +1,145 @@
1
+ /*
2
+ * Copyright 2026, CodeMatters. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
+ * in compliance with the License. You may obtain a copy of the License at
6
+ *
7
+ * https://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
10
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
+ * or implied. See the License for the specific language governing permissions and limitations under
12
+ * the License.
13
+ */
14
+ /**
15
+ * Maps verified invocation facts to a stable subscription identity.
16
+ */
17
+ export const ReaderIdentities = Object.freeze({
18
+ /**
19
+ * Selects the stable reader identity without persisting changing authority facts.
20
+ *
21
+ * @param reader Verified invocation context.
22
+ * @returns Principal ID or explicit anonymous admission.
23
+ */
24
+ from(reader) {
25
+ const snapshot = snapshotTrustedReader(reader);
26
+ return snapshot.principal === undefined
27
+ ? Object.freeze({ kind: "anonymous" })
28
+ : Object.freeze({ kind: "principal", id: snapshot.principal.id });
29
+ },
30
+ /**
31
+ * Rejects missing legacy bindings and any different principal.
32
+ *
33
+ * @param expected Persisted identity, possibly absent in a legacy record.
34
+ * @param actual Verified invocation identity.
35
+ * @returns Whether this reader may continue the subscription.
36
+ */
37
+ matches(expected, actual) {
38
+ return (expected?.kind === actual.kind &&
39
+ (expected.kind === "anonymous" || (actual.kind === "principal" && expected.id === actual.id)));
40
+ },
41
+ /**
42
+ * Compares two optional stored identities, including two absent legacy values.
43
+ *
44
+ * @param left First stored reader identity.
45
+ * @param right Second stored reader identity.
46
+ * @returns Whether both identities are equal, including legacy absence.
47
+ */
48
+ same(left, right) {
49
+ return left === undefined
50
+ ? right === undefined
51
+ : right !== undefined && ReaderIdentities.matches(left, right);
52
+ },
53
+ });
54
+ /**
55
+ * Snapshots verified reader data before it crosses asynchronous or process boundaries.
56
+ *
57
+ * @param reader Trusted result of authentication and authority resolution.
58
+ * @returns Frozen detached facts without copying the transport credential.
59
+ * @internal
60
+ */
61
+ const TrustedReaderSnapshots = Object.freeze({
62
+ /**
63
+ * Copies verified reader facts into a frozen detached value.
64
+ *
65
+ * @param reader The verified reader facts for this operation.
66
+ * @returns Detached principal and authority facts.
67
+ */
68
+ snapshot(reader) {
69
+ const principal = reader.principal;
70
+ if (principal === undefined && reader.authorityFacts !== undefined)
71
+ throw new Error("Public reader cannot carry authenticated authority facts.");
72
+ if (principal !== undefined && (typeof principal.id !== "string" || !principal.id.trim()))
73
+ throw new Error("Trusted reader principal ID must be non-blank.");
74
+ const attributes = principal?.attributes === undefined
75
+ ? undefined
76
+ : TrustedReaderSnapshots.copyAttributes(principal.attributes);
77
+ const authorityFacts = reader.authorityFacts === undefined
78
+ ? undefined
79
+ : TrustedReaderSnapshots.copyAuthorityFacts(reader.authorityFacts);
80
+ return Object.freeze({
81
+ ...(principal === undefined
82
+ ? {}
83
+ : {
84
+ principal: Object.freeze({
85
+ id: principal.id,
86
+ ...(attributes === undefined ? {} : { attributes }),
87
+ }),
88
+ }),
89
+ ...(authorityFacts === undefined ? {} : { authorityFacts }),
90
+ });
91
+ },
92
+ /**
93
+ * Snapshots a string-valued fact map without admitting inherited or non-string values.
94
+ *
95
+ * @param facts Trusted application fact map.
96
+ * @returns Frozen own string entries.
97
+ */
98
+ copyAttributes(facts) {
99
+ const entries = [];
100
+ for (const [name, value] of Object.entries(facts)) {
101
+ if (typeof value !== "string")
102
+ throw new Error("Trusted reader facts must be strings.");
103
+ entries.push([name, value]);
104
+ }
105
+ return Object.freeze(Object.fromEntries(entries));
106
+ },
107
+ /**
108
+ * Copies JSON-compatible authority facts while rejecting cycles and special objects.
109
+ *
110
+ * @param facts Trusted application authority facts; null is rejected at runtime.
111
+ * @returns Frozen detached JSON fact tree.
112
+ */
113
+ copyAuthorityFacts(facts) {
114
+ const seen = new WeakSet();
115
+ /**
116
+ * Copies one JSON authority fact while rejecting cycles and invalid values.
117
+ */
118
+ const copy = (value) => {
119
+ if (value === null || typeof value === "string" || typeof value === "boolean")
120
+ return value;
121
+ if (typeof value === "number" && Number.isFinite(value))
122
+ return value;
123
+ if (typeof value !== "object" || seen.has(value))
124
+ throw new Error("Authority facts must be finite JSON values.");
125
+ if (!Array.isArray(value) &&
126
+ Object.getPrototypeOf(value) !== Object.prototype &&
127
+ Object.getPrototypeOf(value) !== null)
128
+ throw new Error("Authority facts must be plain JSON values.");
129
+ seen.add(value);
130
+ const copied = Array.isArray(value)
131
+ ? value.map((entry) => copy(entry))
132
+ : Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, copy(entry)]));
133
+ seen.delete(value);
134
+ return Object.freeze(copied);
135
+ };
136
+ if (facts === null || Array.isArray(facts) || typeof facts !== "object")
137
+ throw new Error("Authority facts must be a JSON object.");
138
+ return copy(facts);
139
+ },
140
+ });
141
+ /**
142
+ * Public SPI alias for the grouped immutable reader snapshot operation.
143
+ */
144
+ export const snapshotTrustedReader = TrustedReaderSnapshots.snapshot;
145
+ //# sourceMappingURL=trusted-reader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trusted-reader.js","sourceRoot":"","sources":["../../src/spi/trusted-reader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAoEH;;GAEG;AACH,MAAM,CAAC,MAAM,gBAAgB,GA0BxB,MAAM,CAAC,MAAM,CAAC;IACjB;;;;;OAKG;IACH,IAAI,CAAC,MAA4B;QAC/B,MAAM,QAAQ,GAAG,qBAAqB,CAAC,MAAM,CAAC,CAAC;QA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