@adcp/sdk 16.0.0-beta.3 → 16.0.0-beta.4

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.
Files changed (182) hide show
  1. package/dist/lib/auth/oauth/diagnose.js +3 -11
  2. package/dist/lib/auth/oauth/diagnose.mjs +4 -12
  3. package/dist/lib/auth/oauth/diagnostics.d.mts +2 -1
  4. package/dist/lib/auth/oauth/diagnostics.d.ts +2 -1
  5. package/dist/lib/auth/oauth/diagnostics.js +8 -10
  6. package/dist/lib/auth/oauth/diagnostics.mjs +7 -10
  7. package/dist/lib/index.d.mts +1 -1
  8. package/dist/lib/index.d.ts +1 -1
  9. package/dist/lib/index.js +2 -0
  10. package/dist/lib/index.mjs +7 -1
  11. package/dist/lib/reporting/consumer-runtime.js +2 -1
  12. package/dist/lib/reporting/consumer-runtime.mjs +2 -1
  13. package/dist/lib/reporting/ledger/handler.js +50 -11
  14. package/dist/lib/reporting/ledger/handler.mjs +50 -12
  15. package/dist/lib/reporting/ledger/index.d.mts +17 -14
  16. package/dist/lib/reporting/ledger/index.d.ts +17 -14
  17. package/dist/lib/reporting/ledger/index.js +32 -20
  18. package/dist/lib/reporting/ledger/index.mjs +21 -10
  19. package/dist/lib/reporting/ledger/offering-validation.d.mts +9 -0
  20. package/dist/lib/reporting/ledger/offering-validation.d.ts +9 -0
  21. package/dist/lib/reporting/{gcs/bucket-policy.js → ledger/offering-validation.js} +9 -8
  22. package/dist/lib/reporting/ledger/offering-validation.mjs +8 -0
  23. package/dist/lib/reporting/ledger/receipts.d.mts +48 -0
  24. package/dist/lib/reporting/ledger/receipts.d.ts +48 -0
  25. package/dist/lib/reporting/ledger/receipts.js +186 -0
  26. package/dist/lib/reporting/ledger/receipts.mjs +161 -0
  27. package/dist/lib/reporting/ledger/status-ingest.js +3 -3
  28. package/dist/lib/reporting/ledger/status-ingest.mjs +4 -5
  29. package/dist/lib/reporting/ledger/types.d.mts +43 -0
  30. package/dist/lib/reporting/ledger/types.d.ts +43 -0
  31. package/dist/lib/reporting/ledger/types.js +31 -2
  32. package/dist/lib/reporting/ledger/types.mjs +26 -1
  33. package/dist/lib/reporting/pagination.d.mts +6 -0
  34. package/dist/lib/reporting/pagination.d.ts +6 -0
  35. package/dist/lib/reporting/{gcs/errors.js → pagination.js} +12 -17
  36. package/dist/lib/reporting/pagination.mjs +12 -0
  37. package/dist/lib/reporting/reconciliation.d.mts +1 -0
  38. package/dist/lib/reporting/reconciliation.d.ts +1 -0
  39. package/dist/lib/reporting/reconciliation.js +3 -2
  40. package/dist/lib/reporting/reconciliation.mjs +3 -2
  41. package/dist/lib/reporting/service/index.d.mts +79 -153
  42. package/dist/lib/reporting/service/index.d.ts +79 -153
  43. package/dist/lib/reporting/service/index.js +34 -491
  44. package/dist/lib/reporting/service/index.mjs +36 -510
  45. package/dist/lib/reporting/service/scheduler-support.d.mts +43 -0
  46. package/dist/lib/reporting/service/scheduler-support.d.ts +43 -0
  47. package/dist/lib/reporting/service/scheduler-support.js +125 -0
  48. package/dist/lib/reporting/service/scheduler-support.mjs +96 -0
  49. package/dist/lib/schemas-data/v2.5/_provenance.json +1 -1
  50. package/dist/lib/signing/canonicalize.d.mts +4 -0
  51. package/dist/lib/signing/canonicalize.d.ts +4 -0
  52. package/dist/lib/signing/canonicalize.js +14 -8
  53. package/dist/lib/signing/canonicalize.mjs +14 -8
  54. package/dist/lib/signing/content-digest.d.mts +8 -0
  55. package/dist/lib/signing/content-digest.d.ts +8 -0
  56. package/dist/lib/signing/content-digest.js +11 -5
  57. package/dist/lib/signing/content-digest.mjs +10 -5
  58. package/dist/lib/signing/signer.js +9 -6
  59. package/dist/lib/signing/signer.mjs +10 -7
  60. package/dist/lib/signing/web-bot-auth.js +103 -37
  61. package/dist/lib/signing/web-bot-auth.mjs +108 -37
  62. package/dist/lib/testing/storyboard/junit.js +16 -3
  63. package/dist/lib/testing/storyboard/junit.mjs +16 -3
  64. package/dist/lib/testing/storyboard/request-signing/builder.js +10 -9
  65. package/dist/lib/testing/storyboard/request-signing/builder.mjs +10 -9
  66. package/dist/lib/testing/storyboard/runner.js +94 -25
  67. package/dist/lib/testing/storyboard/runner.mjs +94 -25
  68. package/dist/lib/testing/storyboard/types.d.mts +6 -0
  69. package/dist/lib/testing/storyboard/types.d.ts +6 -0
  70. package/dist/lib/testing/storyboard/validations.d.mts +1 -1
  71. package/dist/lib/testing/storyboard/validations.d.ts +1 -1
  72. package/dist/lib/version.d.mts +3 -3
  73. package/dist/lib/version.d.ts +3 -3
  74. package/dist/lib/version.js +3 -3
  75. package/dist/lib/version.mjs +3 -3
  76. package/docs/TYPE-SUMMARY.md +23 -5
  77. package/docs/guides/REPORTING-GCS-FENCE.md +3 -3
  78. package/docs/guides/REPORTING-GCS-MANAGED.md +4 -4
  79. package/docs/guides/REPORTING-LEDGER-CUTOVER.md +7 -7
  80. package/docs/guides/REPORTING-LEDGER.md +107 -51
  81. package/docs/guides/REPORTING-OPERATIONS.md +66 -5
  82. package/docs/guides/REPORTING-STABILITY.md +36 -32
  83. package/docs/llms.txt +4 -3
  84. package/docs/migration-14.x-rc-worksheet.md +4 -7
  85. package/docs/migration-reporting-storage-package.md +186 -0
  86. package/examples/README.md +1 -1
  87. package/package.json +13 -78
  88. package/dist/lib/reporting/azure/index.d.mts +0 -31
  89. package/dist/lib/reporting/azure/index.d.ts +0 -31
  90. package/dist/lib/reporting/azure/index.js +0 -266
  91. package/dist/lib/reporting/azure/index.mjs +0 -249
  92. package/dist/lib/reporting/bigquery/index.d.mts +0 -246
  93. package/dist/lib/reporting/bigquery/index.d.ts +0 -246
  94. package/dist/lib/reporting/bigquery/index.js +0 -633
  95. package/dist/lib/reporting/bigquery/index.mjs +0 -594
  96. package/dist/lib/reporting/gcs/adapter.d.mts +0 -38
  97. package/dist/lib/reporting/gcs/adapter.d.ts +0 -38
  98. package/dist/lib/reporting/gcs/adapter.js +0 -342
  99. package/dist/lib/reporting/gcs/adapter.mjs +0 -325
  100. package/dist/lib/reporting/gcs/bucket-policy.d.mts +0 -3
  101. package/dist/lib/reporting/gcs/bucket-policy.d.ts +0 -3
  102. package/dist/lib/reporting/gcs/bucket-policy.mjs +0 -7
  103. package/dist/lib/reporting/gcs/errors.d.mts +0 -11
  104. package/dist/lib/reporting/gcs/errors.d.ts +0 -11
  105. package/dist/lib/reporting/gcs/errors.mjs +0 -16
  106. package/dist/lib/reporting/gcs/fence.d.mts +0 -41
  107. package/dist/lib/reporting/gcs/fence.d.ts +0 -41
  108. package/dist/lib/reporting/gcs/fence.js +0 -250
  109. package/dist/lib/reporting/gcs/fence.mjs +0 -230
  110. package/dist/lib/reporting/gcs/index.d.mts +0 -18
  111. package/dist/lib/reporting/gcs/index.d.ts +0 -18
  112. package/dist/lib/reporting/gcs/index.js +0 -44
  113. package/dist/lib/reporting/gcs/index.mjs +0 -14
  114. package/dist/lib/reporting/gcs/io.d.mts +0 -7
  115. package/dist/lib/reporting/gcs/io.d.ts +0 -7
  116. package/dist/lib/reporting/gcs/io.js +0 -95
  117. package/dist/lib/reporting/gcs/io.mjs +0 -69
  118. package/dist/lib/reporting/gcs/reader.d.mts +0 -67
  119. package/dist/lib/reporting/gcs/reader.d.ts +0 -67
  120. package/dist/lib/reporting/gcs/reader.js +0 -193
  121. package/dist/lib/reporting/gcs/reader.mjs +0 -169
  122. package/dist/lib/reporting/gcs/row-provider.d.mts +0 -39
  123. package/dist/lib/reporting/gcs/row-provider.d.ts +0 -39
  124. package/dist/lib/reporting/gcs/row-provider.js +0 -225
  125. package/dist/lib/reporting/gcs/row-provider.mjs +0 -208
  126. package/dist/lib/reporting/gcs/validation.d.mts +0 -5
  127. package/dist/lib/reporting/gcs/validation.d.ts +0 -5
  128. package/dist/lib/reporting/gcs/validation.js +0 -49
  129. package/dist/lib/reporting/gcs/validation.mjs +0 -25
  130. package/dist/lib/reporting/ledger/change-feed.d.mts +0 -69
  131. package/dist/lib/reporting/ledger/change-feed.d.ts +0 -69
  132. package/dist/lib/reporting/ledger/change-feed.js +0 -107
  133. package/dist/lib/reporting/ledger/change-feed.mjs +0 -80
  134. package/dist/lib/reporting/ledger/managed-postgres.d.mts +0 -293
  135. package/dist/lib/reporting/ledger/managed-postgres.d.ts +0 -293
  136. package/dist/lib/reporting/ledger/managed-postgres.js +0 -2346
  137. package/dist/lib/reporting/ledger/managed-postgres.mjs +0 -2332
  138. package/dist/lib/reporting/ledger/managed-router.d.mts +0 -148
  139. package/dist/lib/reporting/ledger/managed-router.d.ts +0 -148
  140. package/dist/lib/reporting/ledger/managed-router.js +0 -280
  141. package/dist/lib/reporting/ledger/managed-router.mjs +0 -258
  142. package/dist/lib/reporting/ledger/managed.d.mts +0 -535
  143. package/dist/lib/reporting/ledger/managed.d.ts +0 -535
  144. package/dist/lib/reporting/ledger/managed.js +0 -761
  145. package/dist/lib/reporting/ledger/managed.mjs +0 -729
  146. package/dist/lib/reporting/ledger/notification-activity.d.mts +0 -332
  147. package/dist/lib/reporting/ledger/notification-activity.d.ts +0 -332
  148. package/dist/lib/reporting/ledger/notification-activity.js +0 -1366
  149. package/dist/lib/reporting/ledger/notification-activity.mjs +0 -1337
  150. package/dist/lib/reporting/ledger/object-writes.d.mts +0 -128
  151. package/dist/lib/reporting/ledger/object-writes.d.ts +0 -128
  152. package/dist/lib/reporting/ledger/object-writes.js +0 -147
  153. package/dist/lib/reporting/ledger/object-writes.mjs +0 -114
  154. package/dist/lib/reporting/ledger/postgres.d.mts +0 -689
  155. package/dist/lib/reporting/ledger/postgres.d.ts +0 -689
  156. package/dist/lib/reporting/ledger/postgres.js +0 -4462
  157. package/dist/lib/reporting/ledger/postgres.mjs +0 -4462
  158. package/dist/lib/reporting/ledger/row-storage-migration.d.mts +0 -59
  159. package/dist/lib/reporting/ledger/row-storage-migration.d.ts +0 -59
  160. package/dist/lib/reporting/ledger/row-storage-migration.js +0 -365
  161. package/dist/lib/reporting/ledger/row-storage-migration.mjs +0 -339
  162. package/dist/lib/reporting/ledger/row-storage-object.d.mts +0 -203
  163. package/dist/lib/reporting/ledger/row-storage-object.d.ts +0 -203
  164. package/dist/lib/reporting/ledger/row-storage-object.js +0 -379
  165. package/dist/lib/reporting/ledger/row-storage-object.mjs +0 -335
  166. package/dist/lib/reporting/ledger/row-storage-provider-io.d.mts +0 -44
  167. package/dist/lib/reporting/ledger/row-storage-provider-io.d.ts +0 -44
  168. package/dist/lib/reporting/ledger/row-storage-provider-io.js +0 -142
  169. package/dist/lib/reporting/ledger/row-storage-provider-io.mjs +0 -111
  170. package/dist/lib/reporting/ledger/row-storage.d.mts +0 -316
  171. package/dist/lib/reporting/ledger/row-storage.d.ts +0 -316
  172. package/dist/lib/reporting/ledger/row-storage.js +0 -812
  173. package/dist/lib/reporting/ledger/row-storage.mjs +0 -802
  174. package/dist/lib/reporting/s3/index.d.mts +0 -34
  175. package/dist/lib/reporting/s3/index.d.ts +0 -34
  176. package/dist/lib/reporting/s3/index.js +0 -347
  177. package/dist/lib/reporting/s3/index.mjs +0 -340
  178. package/dist/lib/reporting/service/maintenance.d.mts +0 -148
  179. package/dist/lib/reporting/service/maintenance.d.ts +0 -148
  180. package/dist/lib/reporting/service/maintenance.js +0 -185
  181. package/dist/lib/reporting/service/maintenance.mjs +0 -159
  182. package/examples/reliable-reporting-service/README.md +0 -169
@@ -1,802 +0,0 @@
1
- import { createHash, randomUUID } from "crypto";
2
- import { gunzipSync, gzipSync } from "zlib";
3
- import { canonicalize } from "../../utils/jcs.mjs";
4
- import {
5
- REPORTING_ROW_CHUNK_MAX_BYTES,
6
- REPORTING_ROW_ALGORITHM_V1,
7
- REPORTING_ROW_FORMAT_V1,
8
- createReportingRevisionEnvelopeHasherV1,
9
- createReportingRowsOnlyHasherV1,
10
- decodeVerifiedReportingRowSegmentsV1,
11
- encodeReportingRowsV1,
12
- isReportingRowEncodingError,
13
- verifyReportingRowChunkV1,
14
- verifyReportingRowManifestsV1
15
- } from "./row-encoding.mjs";
16
- import { probeReportingRowStorageSchemaV1 } from "./row-storage-migration.mjs";
17
- import { ReportingRowStoreError, isReportingRowStoreError } from "./row-storage-errors.mjs";
18
- import {
19
- REPORTING_ROW_DEFAULT_KEY_TEMPLATE,
20
- REPORTING_ROW_NATIVE_VERSION_REF_MAX_LENGTH,
21
- assertReportingRowKeyTemplateV1,
22
- assertReportingRowPrefixV1,
23
- renderReportingRowObjectKeyV1
24
- } from "./row-storage-object.mjs";
25
- import { ReportingRowStoreError as ReportingRowStoreError2, isReportingRowStoreError as isReportingRowStoreError2 } from "./row-storage-errors.mjs";
26
- const DEFAULT_BINDING_ID = "postgres";
27
- const BINDING_ID = /^[A-Za-z0-9_.:-]{1,128}$/;
28
- const CREDENTIAL_REF = /^[A-Za-z0-9_.:-]{1,128}$/;
29
- const MAX_ADOPTED_OBJECT_BYTES = 2 * REPORTING_ROW_CHUNK_MAX_BYTES + 1024 * 1024;
30
- class ReportingRowStorageV1 {
31
- constructor(pool, options = {}) {
32
- this.pool = pool;
33
- this.options = options;
34
- const bindings = options.bindings ?? { [DEFAULT_BINDING_ID]: { kind: "postgres" } };
35
- this.providers = Object.freeze({ ...options.providers ?? {} });
36
- const ids = Object.keys(bindings);
37
- if (ids.length === 0) throw new ReportingRowStoreError("INVALID_INPUT", "at least one row binding is required");
38
- for (const id of ids) {
39
- if (!BINDING_ID.test(id)) throw new ReportingRowStoreError("INVALID_INPUT", `invalid row binding ID ${id}`);
40
- const binding = bindings[id];
41
- if (binding.kind === "postgres") continue;
42
- if (binding.kind !== "object") {
43
- throw new ReportingRowStoreError("INVALID_INPUT", `row binding ${id} has an unsupported kind`);
44
- }
45
- const provider = this.providers[binding.provider];
46
- if (!provider) throw new ReportingRowStoreError("INVALID_INPUT", `row binding ${id} names an unknown provider`);
47
- provider.validateLocation(binding.location);
48
- assertReportingRowPrefixV1(binding.prefix);
49
- if (binding.credentialRef !== void 0 && !CREDENTIAL_REF.test(binding.credentialRef)) {
50
- throw new ReportingRowStoreError("INVALID_INPUT", `row binding ${id} has an invalid credentialRef`);
51
- }
52
- }
53
- this.bindings = Object.freeze(
54
- Object.fromEntries(Object.entries(bindings).map(([id, binding]) => [id, Object.freeze({ ...binding })]))
55
- );
56
- this.assertBindingKeyTemplates();
57
- this.deploymentNamespace = options.deploymentNamespace ?? "default";
58
- if (!this.deploymentNamespace || this.deploymentNamespace.length > 256) {
59
- throw new ReportingRowStoreError("INVALID_INPUT", "deploymentNamespace must be 1-256 characters");
60
- }
61
- this.writeDeadline = positiveMilliseconds(options.writeDeadlineMilliseconds ?? 6e4, "writeDeadlineMilliseconds");
62
- this.readDeadline = positiveMilliseconds(options.readDeadlineMilliseconds ?? 3e4, "readDeadlineMilliseconds");
63
- }
64
- pool;
65
- options;
66
- bindings;
67
- providers;
68
- deploymentNamespace;
69
- writeDeadline;
70
- readDeadline;
71
- readied;
72
- installation;
73
- /** Deadline budget a writer must still hold on its lease before uploading. */
74
- get writeDeadlineMilliseconds() {
75
- return this.writeDeadline;
76
- }
77
- /** Every writable object binding's key template must include `{revision_key}`; throws `INVALID_INPUT`. */
78
- assertBindingKeyTemplates() {
79
- for (const [id, binding] of Object.entries(this.bindings)) {
80
- if (binding.kind !== "object") continue;
81
- try {
82
- assertReportingRowKeyTemplateV1(binding.keyTemplate ?? REPORTING_ROW_DEFAULT_KEY_TEMPLATE, {
83
- requireRevisionKey: binding.readOnly !== true
84
- });
85
- } catch (error) {
86
- if (isReportingRowStoreError(error)) {
87
- throw new ReportingRowStoreError(
88
- "INVALID_INPUT",
89
- `row binding ${id}: ${error.message.replace(/^Reporting row store: INVALID_INPUT: /, "")}`
90
- );
91
- }
92
- throw error;
93
- }
94
- }
95
- }
96
- /**
97
- * Refuse a configuration in which compaction could delete an object a concurrent commit is
98
- * adopting. An object binding whose key template omits `{revision_key}` can map two revisions
99
- * with identical chunk content to one object key, and a create-only write adopts an existing
100
- * object with identical bytes. Compaction deleting that object at the same moment would leave
101
- * the newer revision without its rows. Every writable binding is already required to include
102
- * `{revision_key}`, so this cannot fail for a validated configuration. A `readOnly` binding is
103
- * skipped: it never takes commits, so nothing can adopt through it, and every delete is
104
- * reference-checked.
105
- *
106
- * Since the reference-checked, per-object-locked delete this is no longer the only guard: every
107
- * delete path now skips an object another row set still references. It is kept as defense in
108
- * depth for compaction, the one path that deletes content while the obligation stays live.
109
- */
110
- assertCompactionSafeKeyTemplates() {
111
- const unsafe = Object.entries(this.bindings).filter(
112
- ([, binding]) => binding.kind === "object" && binding.readOnly !== true && !(binding.keyTemplate ?? REPORTING_ROW_DEFAULT_KEY_TEMPLATE).includes("{revision_key}")
113
- ).map(([id]) => id);
114
- if (unsafe.length > 0) {
115
- throw new ReportingRowStoreError(
116
- "INVALID_INPUT",
117
- `snapshot compaction requires every object row binding's keyTemplate to include {revision_key}; binding(s) ${unsafe.join(", ")} omit it, so two revisions with identical content could share an object that compaction deletes while a concurrent commit adopts it`
118
- );
119
- }
120
- }
121
- /** IDs of the object bindings this host configures, the only ones it can delete objects through. */
122
- objectBindingIds() {
123
- return Object.entries(this.bindings).filter(([, binding]) => binding.kind === "object").map(([id]) => id);
124
- }
125
- /** Probe the schema and every provider, and register (or verify) every configured binding. Idempotent. */
126
- ready() {
127
- this.readied ??= this.register().catch((error) => {
128
- this.readied = void 0;
129
- throw error;
130
- });
131
- return this.readied;
132
- }
133
- /** Encode rows, prove the chunks reproduce the committed binding, select a binding and plan objects. */
134
- async prepare(input) {
135
- await this.ready();
136
- let encoded;
137
- try {
138
- encoded = encodeReportingRowsV1(input.rows);
139
- } catch (error) {
140
- if (isReportingRowEncodingError(error)) throw new ReportingRowStoreError("INVALID_INPUT", error.message);
141
- throw error;
142
- }
143
- const digestProfile = input.rowSetKind === "revision" ? "revision_envelope_v1" : "rows_v1";
144
- const hasher = digestProfile === "revision_envelope_v1" ? createReportingRevisionEnvelopeHasherV1({
145
- reporting_revision_id: input.rowSetId,
146
- row_count: input.rows.length,
147
- control_totals: input.controlTotals ?? []
148
- }) : createReportingRowsOnlyHasherV1(input.rows.length);
149
- for (const chunk of encoded.chunks) hasher.update(chunk.bytes);
150
- const digest = hasher.digest();
151
- if (digest.sha256 !== input.binding.sha256 || digest.byteCount !== input.binding.byteCount || input.binding.rowCount !== input.rows.length) {
152
- throw new ReportingRowStoreError("INVALID_INPUT", "rows do not reproduce their committed binding");
153
- }
154
- const writable = Object.keys(this.bindings).filter((id) => {
155
- const candidate = this.bindings[id];
156
- return candidate.kind !== "object" || candidate.readOnly !== true;
157
- });
158
- if (writable.length === 0 && !input.bindingId && !this.options.selectRowBinding) {
159
- throw new ReportingRowStoreError(
160
- "UNSAFE_BINDING",
161
- "no writable row binding is configured: every binding is readOnly"
162
- );
163
- }
164
- const bindingId = input.bindingId ? input.bindingId : this.options.selectRowBinding ? await this.options.selectRowBinding({
165
- account_id: input.accountId,
166
- reporting_obligation_id: input.obligationId,
167
- row_set_kind: input.rowSetKind,
168
- ...input.finality ? { finality: input.finality } : {},
169
- row_count: input.rows.length,
170
- content_size_bytes: digest.byteCount
171
- }) : writable.length === 1 ? writable[0] : DEFAULT_BINDING_ID;
172
- const binding = this.bindings[bindingId];
173
- if (!binding) throw new ReportingRowStoreError("STATE_UNAVAILABLE", `row binding ${bindingId} is not configured`);
174
- if (binding.kind === "object" && binding.readOnly === true) {
175
- throw new ReportingRowStoreError(
176
- "UNSAFE_BINDING",
177
- `row binding ${bindingId} is read-only and cannot take new row sets`
178
- );
179
- }
180
- if (binding.kind === "postgres") return Object.freeze({ input, encoded, bindingId, binding, digestProfile });
181
- const compression = binding.compression ?? "gzip";
182
- const namespaceKey = this.installation.namespaceKey;
183
- const objects = encoded.chunks.map((chunk) => {
184
- const physicalBytes = compression === "gzip" ? gzipSync(chunk.bytes, { level: 6 }) : chunk.bytes;
185
- return Object.freeze({
186
- objectRef: renderReportingRowObjectKeyV1({
187
- template: binding.keyTemplate ?? REPORTING_ROW_DEFAULT_KEY_TEMPLATE,
188
- prefix: binding.prefix,
189
- namespaceKey,
190
- accountId: input.accountId,
191
- rowSetId: input.rowSetId,
192
- ...input.periodStart ? { periodStart: input.periodStart } : {},
193
- ...input.sourceTimezone ? { sourceTimezone: input.sourceTimezone } : {},
194
- ...input.finality ? { finality: input.finality } : {},
195
- contentSha256: input.binding.sha256,
196
- chunkIndex: chunk.manifest.chunk_index,
197
- compression
198
- }),
199
- physicalBytes,
200
- physicalSha256: sha256Hex(physicalBytes),
201
- compression
202
- });
203
- });
204
- return Object.freeze({ input, encoded, bindingId, binding, digestProfile, objects: Object.freeze(objects) });
205
- }
206
- /**
207
- * Upload a prepared object-kind row set, create-only. An object already at a
208
- * planned key is adopted only when its bytes decode to the exact chunk;
209
- * otherwise `CONTENT_CONFLICT`. `onCreated` is awaited for every object this
210
- * call created, before the next upload, so the caller can record it in its
211
- * write intent.
212
- */
213
- async upload(prepared, intentId, onCreated, signal) {
214
- const binding = prepared.binding;
215
- if (binding.kind !== "object" || !prepared.objects) {
216
- throw new ReportingRowStoreError("INVALID_INPUT", "only object-kind row sets are uploaded");
217
- }
218
- const provider = this.providers[binding.provider];
219
- const context = { signal: deadlineSignal(this.writeDeadline, signal) };
220
- const location = providerLocation(binding);
221
- const locators = [];
222
- for (const [index, object] of prepared.objects.entries()) {
223
- const manifest = prepared.encoded.manifests[index];
224
- const put = await this.provider(
225
- () => provider.putIfAbsent(
226
- {
227
- ...location,
228
- objectRef: object.objectRef,
229
- bytes: object.physicalBytes,
230
- contentType: object.compression === "gzip" ? "application/gzip" : "application/x-ndjson",
231
- metadata: {
232
- "adcp-installation": this.installation.installationId,
233
- "adcp-intent": intentId
234
- }
235
- },
236
- context
237
- )
238
- );
239
- if (put.created) {
240
- await onCreated({ objectRef: object.objectRef, nativeVersionRef: put.nativeVersionRef });
241
- assertNativeVersionRef(put.nativeVersionRef);
242
- locators.push({
243
- objectRef: object.objectRef,
244
- nativeVersionRef: put.nativeVersionRef,
245
- compression: object.compression,
246
- physicalSha256: object.physicalSha256,
247
- sizeBytes: object.physicalBytes.byteLength,
248
- created: true
249
- });
250
- continue;
251
- }
252
- assertNativeVersionRef(put.nativeVersionRef);
253
- const existing = await this.provider(
254
- () => provider.get(
255
- {
256
- ...location,
257
- objectRef: object.objectRef,
258
- nativeVersionRef: put.nativeVersionRef,
259
- maxBytes: MAX_ADOPTED_OBJECT_BYTES
260
- },
261
- context
262
- )
263
- );
264
- const logical = existing ? this.inflate(Buffer.from(existing), object.compression, manifest.byte_count) : null;
265
- if (!existing || !logical || sha256Hex(logical) !== manifest.sha256) {
266
- throw new ReportingRowStoreError("CONTENT_CONFLICT", `object for chunk ${index} holds different bytes`);
267
- }
268
- locators.push({
269
- objectRef: object.objectRef,
270
- nativeVersionRef: put.nativeVersionRef,
271
- compression: object.compression,
272
- physicalSha256: sha256Hex(existing),
273
- sizeBytes: existing.byteLength,
274
- created: false
275
- });
276
- }
277
- return locators;
278
- }
279
- /**
280
- * Persist a prepared row set inside the caller's ledger transaction:
281
- * bodies for the `postgres` kind, or the uploaded locators for the object
282
- * kind. Replays of an identical row set are no-ops; a conflicting row set
283
- * under the same ID is refused.
284
- */
285
- async writeInTransaction(transaction, prepared, locators, sharedFromRowSetId) {
286
- const { input, encoded } = prepared;
287
- if (prepared.binding.kind === "object" && locators?.length !== encoded.chunks.length) {
288
- throw new ReportingRowStoreError("INVALID_INPUT", "object-kind row sets need one locator per chunk");
289
- }
290
- const inserted = await transaction.query(
291
- `INSERT INTO adcp_reporting_row_sets
292
- (row_set_id, row_set_kind, account_id, obligation_id, format, algorithm, digest_profile, content_sha256,
293
- content_size_bytes, row_count, row_manifest_sha256, chunk_count, row_binding_id,
294
- rows_shared_from_row_set_id)
295
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
296
- ON CONFLICT (row_set_id) DO NOTHING`,
297
- [
298
- input.rowSetId,
299
- input.rowSetKind,
300
- input.accountId,
301
- input.obligationId,
302
- REPORTING_ROW_FORMAT_V1,
303
- REPORTING_ROW_ALGORITHM_V1,
304
- prepared.digestProfile,
305
- input.binding.sha256,
306
- input.binding.byteCount,
307
- encoded.rowCount,
308
- encoded.rowManifestSha256,
309
- encoded.chunks.length,
310
- prepared.bindingId,
311
- sharedFromRowSetId ?? null
312
- ]
313
- );
314
- if (inserted.rowCount !== 1) {
315
- const existing = await transaction.query(
316
- "SELECT content_sha256, row_manifest_sha256 FROM adcp_reporting_row_sets WHERE row_set_id = $1",
317
- [input.rowSetId]
318
- );
319
- const row = existing.rows[0];
320
- if (!row || row.content_sha256 !== input.binding.sha256 || row.row_manifest_sha256 !== encoded.rowManifestSha256) {
321
- throw new ReportingRowStoreError("CONTENT_CONFLICT", "row set identity names different content");
322
- }
323
- return;
324
- }
325
- for (const [index, chunk] of encoded.chunks.entries()) {
326
- const manifest = chunk.manifest;
327
- const locator = locators?.[index];
328
- if (!locator) {
329
- await transaction.query(
330
- `INSERT INTO adcp_reporting_chunk_bodies (account_id, obligation_id, sha256, body)
331
- VALUES ($1, $2, $3, $4) ON CONFLICT DO NOTHING`,
332
- [input.accountId, input.obligationId, manifest.sha256, chunk.bytes]
333
- );
334
- }
335
- await transaction.query(
336
- `INSERT INTO adcp_reporting_row_chunks
337
- (row_set_id, chunk_index, account_id, obligation_id, first_ordinal, row_count, canonical_size_bytes, sha256,
338
- segments, object_ref, native_version_ref, compression, physical_sha256, size_bytes)
339
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9::jsonb, $10, $11, $12, $13, $14)`,
340
- [
341
- input.rowSetId,
342
- manifest.chunk_index,
343
- input.accountId,
344
- input.obligationId,
345
- manifest.first_ordinal,
346
- manifest.row_count,
347
- manifest.byte_count,
348
- manifest.sha256,
349
- JSON.stringify(manifest.segments),
350
- locator?.objectRef ?? null,
351
- locator?.nativeVersionRef ?? null,
352
- locator?.compression ?? "none",
353
- locator?.physicalSha256 ?? null,
354
- locator?.sizeBytes ?? null
355
- ]
356
- );
357
- }
358
- }
359
- /**
360
- * Load a row set and verify its chunk manifest against the committed digest.
361
- * Returns `null` when no row set exists (legacy inline rows) or it belongs
362
- * to another account. Throws `ROWS_EXPIRED` / `ROWS_UNAVAILABLE` when the
363
- * rows were pruned or lost.
364
- */
365
- async find(rowSetId, accountId, queryable = this.pool) {
366
- const found = await queryable.query(
367
- `SELECT row_set.row_set_id, row_set.row_set_kind, row_set.account_id, row_set.obligation_id,
368
- row_set.digest_profile, row_set.content_sha256, row_set.content_size_bytes, row_set.row_count,
369
- row_set.row_manifest_sha256, row_set.chunk_count, row_set.row_binding_id,
370
- row_set.row_location_version, row_set.rows_state, binding.kind, binding.identity_sha256
371
- FROM adcp_reporting_row_sets row_set
372
- JOIN adcp_reporting_row_bindings binding ON binding.row_binding_id = row_set.row_binding_id
373
- WHERE row_set.row_set_id = $1 AND row_set.account_id = $2`,
374
- [rowSetId, accountId]
375
- );
376
- const record = found.rows[0];
377
- if (!record) return null;
378
- if (record.rows_state === "pruning" || record.rows_state === "pruned") {
379
- throw new ReportingRowStoreError("ROWS_EXPIRED");
380
- }
381
- if (record.rows_state === "unavailable") throw new ReportingRowStoreError("ROWS_UNAVAILABLE");
382
- const chunks = await queryable.query(
383
- `SELECT chunk_index, first_ordinal, row_count, canonical_size_bytes, sha256, segments,
384
- object_ref, native_version_ref, compression, physical_sha256, size_bytes
385
- FROM adcp_reporting_row_chunks WHERE row_set_id = $1 ORDER BY chunk_index`,
386
- [rowSetId]
387
- );
388
- const manifests = chunks.rows.map((chunk) => ({
389
- chunk_index: chunk.chunk_index,
390
- first_ordinal: Number(chunk.first_ordinal),
391
- row_count: chunk.row_count,
392
- byte_count: Number(chunk.canonical_size_bytes),
393
- sha256: chunk.sha256,
394
- segments: chunk.segments.map((segment) => ({
395
- first_ordinal: Number(segment.first_ordinal),
396
- row_count: Number(segment.row_count),
397
- byte_offset: Number(segment.byte_offset),
398
- byte_count: Number(segment.byte_count),
399
- sha256: segment.sha256
400
- }))
401
- }));
402
- const rowCount = Number(record.row_count);
403
- try {
404
- verifyReportingRowManifestsV1(manifests, { rowCount, rowManifestSha256: record.row_manifest_sha256 });
405
- } catch (error) {
406
- if (isReportingRowEncodingError(error)) throw new ReportingRowStoreError("ROWS_INTEGRITY_FAILED", error.message);
407
- throw error;
408
- }
409
- if (manifests.length !== record.chunk_count) {
410
- throw new ReportingRowStoreError("ROWS_INTEGRITY_FAILED", "chunk count does not match the row set");
411
- }
412
- const locators = chunks.rows.map((chunk) => {
413
- const hasLocation = chunk.object_ref !== null;
414
- if (hasLocation !== (record.kind === "object")) {
415
- throw new ReportingRowStoreError("ROWS_INTEGRITY_FAILED", "chunk location does not match its binding kind");
416
- }
417
- return hasLocation ? Object.freeze({
418
- objectRef: chunk.object_ref,
419
- nativeVersionRef: chunk.native_version_ref,
420
- compression: chunk.compression,
421
- physicalSha256: chunk.physical_sha256,
422
- sizeBytes: Number(chunk.size_bytes)
423
- }) : void 0;
424
- });
425
- return Object.freeze({
426
- rowSetId: record.row_set_id,
427
- rowSetKind: record.row_set_kind,
428
- accountId: record.account_id,
429
- obligationId: record.obligation_id,
430
- digestProfile: record.digest_profile,
431
- contentSha256: record.content_sha256,
432
- contentSizeBytes: Number(record.content_size_bytes),
433
- rowCount,
434
- rowManifestSha256: record.row_manifest_sha256,
435
- bindingId: record.row_binding_id,
436
- bindingIdentitySha256: record.identity_sha256,
437
- kind: record.kind,
438
- manifests: Object.freeze(manifests),
439
- locators: Object.freeze(locators)
440
- });
441
- }
442
- /** Verified rows `[offset, offset + limit)` in ordinal order. */
443
- async readPage(handle, offset, limit, signal) {
444
- if (!Number.isSafeInteger(offset) || offset < 0 || !Number.isSafeInteger(limit) || limit < 1) {
445
- throw new ReportingRowStoreError("INVALID_INPUT", "row page bounds are invalid");
446
- }
447
- const end = Math.min(handle.rowCount, offset + limit);
448
- const rows = [];
449
- for (const manifest of handle.manifests) {
450
- if (offset >= end) break;
451
- const chunkEnd = manifest.first_ordinal + manifest.row_count;
452
- if (chunkEnd <= offset || manifest.first_ordinal >= end) continue;
453
- signal?.throwIfAborted();
454
- const from = manifest.segments.findIndex(
455
- (segment) => segment.first_ordinal + segment.row_count > Math.max(offset, manifest.first_ordinal)
456
- );
457
- let to = from;
458
- while (to + 1 < manifest.segments.length && manifest.segments[to + 1].first_ordinal < end) to += 1;
459
- const firstSegment = manifest.segments[from];
460
- const lastSegment = manifest.segments[to];
461
- const bytes = await this.readChunkRange(
462
- handle,
463
- manifest,
464
- firstSegment.byte_offset,
465
- lastSegment.byte_offset + lastSegment.byte_count - firstSegment.byte_offset,
466
- signal
467
- );
468
- const decoded = this.decode(() => decodeVerifiedReportingRowSegmentsV1(manifest, [from, to], bytes));
469
- const startIndex = Math.max(offset, firstSegment.first_ordinal) - firstSegment.first_ordinal;
470
- const endIndex = Math.min(end, lastSegment.first_ordinal + lastSegment.row_count) - firstSegment.first_ordinal;
471
- rows.push(...decoded.slice(startIndex, endIndex));
472
- }
473
- return rows;
474
- }
475
- /**
476
- * Every row, verified chunk by chunk and against the committed binding.
477
- * `controlTotals` are required for revision row sets.
478
- */
479
- async readAll(handle, controlTotals, options = {}) {
480
- if (options.maxBytes !== void 0 && handle.contentSizeBytes > options.maxBytes) {
481
- throw new ReportingRowStoreError("INVALID_INPUT", "row set exceeds the hydration byte limit");
482
- }
483
- const hasher = handle.digestProfile === "revision_envelope_v1" ? createReportingRevisionEnvelopeHasherV1({
484
- reporting_revision_id: handle.rowSetId,
485
- row_count: handle.rowCount,
486
- control_totals: controlTotals ?? []
487
- }) : createReportingRowsOnlyHasherV1(handle.rowCount);
488
- const rows = [];
489
- for (const manifest of handle.manifests) {
490
- options.signal?.throwIfAborted();
491
- const bytes = await this.readChunkRange(handle, manifest, 0, manifest.byte_count, options.signal);
492
- this.decode(() => verifyReportingRowChunkV1(manifest, bytes));
493
- hasher.update(bytes);
494
- rows.push(
495
- ...this.decode(() => decodeVerifiedReportingRowSegmentsV1(manifest, [0, manifest.segments.length - 1], bytes))
496
- );
497
- }
498
- const digest = this.decode(() => hasher.digest());
499
- if (digest.sha256 !== handle.contentSha256 || digest.byteCount !== handle.contentSizeBytes) {
500
- throw new ReportingRowStoreError("ROWS_INTEGRITY_FAILED", "rows do not reproduce the committed binding");
501
- }
502
- return rows;
503
- }
504
- /**
505
- * Delete exact object versions through the configured binding's provider.
506
- * `absent` counts as success. Only object-kind bindings are accepted.
507
- */
508
- async deleteObjects(bindingId, objects, signal) {
509
- await this.ready();
510
- const binding = this.bindings[bindingId];
511
- if (!binding || binding.kind !== "object") {
512
- throw new ReportingRowStoreError("STATE_UNAVAILABLE", `row binding ${bindingId} is not configured for objects`);
513
- }
514
- const provider = this.providers[binding.provider];
515
- const ownedPrefix = `${binding.prefix}/${this.installation.namespaceKey}/`;
516
- const context = { signal: deadlineSignal(this.writeDeadline, signal) };
517
- for (const object of objects) {
518
- if (!object.objectRef.startsWith(ownedPrefix)) {
519
- throw new ReportingRowStoreError("UNSAFE_BINDING", "refusing to delete an object outside the owned prefix");
520
- }
521
- await this.provider(
522
- () => provider.delete(
523
- { ...providerLocation(binding), objectRef: object.objectRef, nativeVersionRef: object.nativeVersionRef },
524
- context
525
- )
526
- );
527
- }
528
- }
529
- /**
530
- * Prove each object is still present at its recorded version with the recorded size.
531
- * A commit that adopted an object another row set created calls this under the object's
532
- * lock, so a concurrent delete either finished first (this throws, the commit fails and
533
- * the writer re-uploads) or sees the commit's chunk reference and skips the delete.
534
- *
535
- * Native versions are write-unique (S3 VersionId, Azure versionId, GCS generation, filesystem
536
- * `ino:birthtime:mtime:size`), so a version-pinned metadata check, "this exact version exists at the
537
- * recorded size", proves the object unchanged without downloading it while the commit holds its
538
- * locks. Bytes were verified when the object was uploaded or adopted. A custom provider without
539
- * `stat` falls back to reading the whole object and comparing its physical digest.
540
- */
541
- async assertObjectsPresent(bindingId, objects, signal) {
542
- await this.ready();
543
- const binding = this.bindings[bindingId];
544
- if (!binding || binding.kind !== "object") {
545
- throw new ReportingRowStoreError("STATE_UNAVAILABLE", `row binding ${bindingId} is not configured for objects`);
546
- }
547
- const provider = this.providers[binding.provider];
548
- const context = { signal: deadlineSignal(this.writeDeadline, signal) };
549
- const location = providerLocation(binding);
550
- const changed = () => new ReportingRowStoreError(
551
- "STATE_UNAVAILABLE",
552
- "an adopted row object was deleted or replaced before its commit; retry the upload"
553
- );
554
- for (const object of objects) {
555
- const identity = { ...location, objectRef: object.objectRef, nativeVersionRef: object.nativeVersionRef };
556
- if (provider.stat) {
557
- const stat = await this.provider(() => provider.stat(identity, context));
558
- if (!stat || stat.nativeVersionRef !== object.nativeVersionRef || !Number.isSafeInteger(stat.sizeBytes) || stat.sizeBytes !== object.sizeBytes) {
559
- throw changed();
560
- }
561
- continue;
562
- }
563
- const bytes = await this.provider(
564
- () => provider.get({ ...identity, maxBytes: MAX_ADOPTED_OBJECT_BYTES }, context)
565
- );
566
- if (!bytes || sha256Hex(bytes) !== object.physicalSha256) throw changed();
567
- }
568
- }
569
- /**
570
- * A live row set in the same obligation and binding whose rows are
571
- * byte-identical (same manifest digest), with its chunk locations. Sharing
572
- * never crosses obligations, so it never crosses consumers.
573
- */
574
- async findShareable(queryable, prepared) {
575
- if (prepared.encoded.chunks.length === 0) return void 0;
576
- const found = await queryable.query(
577
- `SELECT row_set_id FROM adcp_reporting_row_sets
578
- WHERE obligation_id = $1 AND account_id = $2 AND row_binding_id = $3 AND row_manifest_sha256 = $4
579
- AND rows_state = 'live' AND row_set_id <> $5
580
- ORDER BY recorded_at DESC, row_set_id LIMIT 1
581
- FOR SHARE`,
582
- [
583
- prepared.input.obligationId,
584
- prepared.input.accountId,
585
- prepared.bindingId,
586
- prepared.encoded.rowManifestSha256,
587
- prepared.input.rowSetId
588
- ]
589
- );
590
- const rowSetId = found.rows[0]?.row_set_id;
591
- if (!rowSetId) return void 0;
592
- const chunks = await queryable.query(
593
- `SELECT object_ref, native_version_ref, compression, physical_sha256, size_bytes
594
- FROM adcp_reporting_row_chunks WHERE row_set_id = $1 ORDER BY chunk_index`,
595
- [rowSetId]
596
- );
597
- if (chunks.rows.length !== prepared.encoded.chunks.length) return void 0;
598
- if (prepared.binding.kind === "postgres") return { rowSetId, locators: [] };
599
- if (chunks.rows.some((chunk) => chunk.object_ref === null)) return void 0;
600
- return {
601
- rowSetId,
602
- locators: chunks.rows.map((chunk) => ({
603
- objectRef: chunk.object_ref,
604
- nativeVersionRef: chunk.native_version_ref,
605
- compression: chunk.compression,
606
- physicalSha256: chunk.physical_sha256,
607
- sizeBytes: Number(chunk.size_bytes),
608
- created: false
609
- }))
610
- };
611
- }
612
- /** Installation identity and namespace key; available after `ready()`. */
613
- get installationIdentity() {
614
- if (!this.installation) throw new ReportingRowStoreError("STATE_UNAVAILABLE", "row storage is not ready");
615
- return this.installation;
616
- }
617
- async readChunkRange(handle, manifest, byteOffset, byteCount, signal) {
618
- if (handle.kind === "postgres") {
619
- const result = await this.pool.query(
620
- `SELECT substring(body FROM $4::integer + 1 FOR $5::integer) AS bytes
621
- FROM adcp_reporting_chunk_bodies
622
- WHERE account_id = $1 AND obligation_id = $2 AND sha256 = $3`,
623
- [handle.accountId, handle.obligationId, manifest.sha256, byteOffset, byteCount]
624
- );
625
- const bytes = result.rows[0]?.bytes;
626
- if (!bytes) throw new ReportingRowStoreError("ROWS_UNAVAILABLE", `chunk ${manifest.chunk_index} body is missing`);
627
- return bytes;
628
- }
629
- const binding = this.bindings[handle.bindingId];
630
- if (!binding || binding.kind !== "object" || bindingIdentitySha256(binding) !== handle.bindingIdentitySha256) {
631
- throw new ReportingRowStoreError("STATE_UNAVAILABLE", `row binding ${handle.bindingId} is not configured`);
632
- }
633
- const locator = handle.locators[manifest.chunk_index];
634
- const provider = this.providers[binding.provider];
635
- const context = { signal: deadlineSignal(this.readDeadline, signal) };
636
- const location = providerLocation(binding);
637
- if (locator.compression === "none" && provider.rangedReads) {
638
- const bytes = await this.provider(
639
- () => provider.get(
640
- {
641
- ...location,
642
- objectRef: locator.objectRef,
643
- nativeVersionRef: locator.nativeVersionRef,
644
- range: { offset: byteOffset, length: byteCount },
645
- maxBytes: byteCount
646
- },
647
- context
648
- )
649
- );
650
- if (!bytes)
651
- throw new ReportingRowStoreError("ROWS_UNAVAILABLE", `chunk ${manifest.chunk_index} object is missing`);
652
- if (bytes.byteLength !== byteCount) {
653
- throw new ReportingRowStoreError("ROWS_INTEGRITY_FAILED", `chunk ${manifest.chunk_index} range is truncated`);
654
- }
655
- return Buffer.from(bytes);
656
- }
657
- const stored = await this.provider(
658
- () => provider.get(
659
- {
660
- ...location,
661
- objectRef: locator.objectRef,
662
- nativeVersionRef: locator.nativeVersionRef,
663
- maxBytes: locator.sizeBytes
664
- },
665
- context
666
- )
667
- );
668
- if (!stored)
669
- throw new ReportingRowStoreError("ROWS_UNAVAILABLE", `chunk ${manifest.chunk_index} object is missing`);
670
- const physical = Buffer.from(stored);
671
- if (physical.byteLength !== locator.sizeBytes || sha256Hex(physical) !== locator.physicalSha256) {
672
- throw new ReportingRowStoreError("ROWS_INTEGRITY_FAILED", `chunk ${manifest.chunk_index} stored bytes differ`);
673
- }
674
- const logical = this.inflate(physical, locator.compression, manifest.byte_count);
675
- if (!logical) {
676
- throw new ReportingRowStoreError("ROWS_INTEGRITY_FAILED", `chunk ${manifest.chunk_index} does not decompress`);
677
- }
678
- return logical.subarray(byteOffset, byteOffset + byteCount);
679
- }
680
- inflate(physical, compression, byteCount) {
681
- if (compression === "none") return physical.byteLength === byteCount ? physical : null;
682
- try {
683
- const logical = gunzipSync(physical, { maxOutputLength: byteCount });
684
- return logical.byteLength === byteCount ? logical : null;
685
- } catch {
686
- return null;
687
- }
688
- }
689
- decode(operation) {
690
- try {
691
- return operation();
692
- } catch (error) {
693
- if (isReportingRowEncodingError(error)) throw new ReportingRowStoreError("ROWS_INTEGRITY_FAILED", error.message);
694
- throw error;
695
- }
696
- }
697
- /** Collapse provider failures to stable, secret-free codes. */
698
- async provider(operation) {
699
- try {
700
- return await operation();
701
- } catch (error) {
702
- if (isReportingRowStoreError(error)) throw error;
703
- if (error instanceof Error && (error.name === "TimeoutError" || error.name === "AbortError")) {
704
- throw new ReportingRowStoreError(error.name === "TimeoutError" ? "DEADLINE_EXCEEDED" : "ABORTED");
705
- }
706
- throw new ReportingRowStoreError("PROVIDER_UNAVAILABLE");
707
- }
708
- }
709
- async register() {
710
- this.assertBindingKeyTemplates();
711
- const { installationId } = await probeReportingRowStorageSchemaV1(this.pool);
712
- const namespaceKey = sha256Hex(canonicalize(["adcp.rows.v1", this.deploymentNamespace, installationId]));
713
- this.installation = { installationId, namespaceKey };
714
- for (const [bindingId, binding] of Object.entries(this.bindings)) {
715
- if (binding.kind === "object") {
716
- const provider = this.providers[binding.provider];
717
- await this.provider(
718
- () => provider.probe(
719
- { ...providerLocation(binding), prefix: binding.prefix },
720
- { signal: AbortSignal.timeout(this.readDeadline) }
721
- )
722
- );
723
- }
724
- const identityConfig = bindingIdentityConfig(binding);
725
- const identitySha256 = bindingIdentitySha256(binding);
726
- const operationalConfig = binding.kind === "object" ? {
727
- compression: binding.compression ?? "gzip",
728
- ...binding.credentialRef ? { credentialRef: binding.credentialRef } : {}
729
- } : {};
730
- await this.pool.query(
731
- `INSERT INTO adcp_reporting_row_bindings
732
- (row_binding_id, kind, provider, identity_config, identity_sha256, operational_config, namespace_key)
733
- VALUES ($1, $2, $3, $4::jsonb, $5, $6::jsonb, $7)
734
- ON CONFLICT (row_binding_id) DO NOTHING`,
735
- [
736
- bindingId,
737
- binding.kind,
738
- binding.kind === "object" ? binding.provider : "postgres",
739
- JSON.stringify(identityConfig),
740
- identitySha256,
741
- JSON.stringify(operationalConfig),
742
- namespaceKey
743
- ]
744
- );
745
- const stored = await this.pool.query(
746
- "SELECT identity_sha256, namespace_key, state FROM adcp_reporting_row_bindings WHERE row_binding_id = $1",
747
- [bindingId]
748
- );
749
- const row = stored.rows[0];
750
- if (!row || row.identity_sha256 !== identitySha256) {
751
- throw new ReportingRowStoreError(
752
- "UNSAFE_BINDING",
753
- `row binding ${bindingId} identity differs from its definition`
754
- );
755
- }
756
- if (row.namespace_key !== namespaceKey) {
757
- throw new ReportingRowStoreError("UNSAFE_BINDING", `row binding ${bindingId} belongs to another installation`);
758
- }
759
- }
760
- }
761
- }
762
- function bindingIdentityConfig(binding) {
763
- return binding.kind === "object" ? {
764
- location: { ...binding.location },
765
- prefix: binding.prefix,
766
- keyTemplate: binding.keyTemplate ?? REPORTING_ROW_DEFAULT_KEY_TEMPLATE
767
- } : {};
768
- }
769
- function bindingIdentitySha256(binding) {
770
- const provider = binding.kind === "object" ? binding.provider : binding.kind;
771
- return sha256Hex(canonicalize([binding.kind, provider, bindingIdentityConfig(binding)]));
772
- }
773
- function providerLocation(binding) {
774
- return { location: binding.location, ...binding.credentialRef ? { credentialRef: binding.credentialRef } : {} };
775
- }
776
- function deadlineSignal(milliseconds, signal) {
777
- const timeout = AbortSignal.timeout(milliseconds);
778
- return signal ? AbortSignal.any([signal, timeout]) : timeout;
779
- }
780
- function assertNativeVersionRef(value) {
781
- if (typeof value !== "string" || value.length < 1 || value.length > REPORTING_ROW_NATIVE_VERSION_REF_MAX_LENGTH) {
782
- throw new ReportingRowStoreError("PROVIDER_UNAVAILABLE", "provider returned an invalid native version reference");
783
- }
784
- }
785
- function positiveMilliseconds(value, name) {
786
- if (!Number.isSafeInteger(value) || value < 1e3 || value > 6e5) {
787
- throw new ReportingRowStoreError("INVALID_INPUT", `${name} must be between 1000 and 600000`);
788
- }
789
- return value;
790
- }
791
- function sha256Hex(value) {
792
- return createHash("sha256").update(value).digest("hex");
793
- }
794
- function reportingRowWriteIntentIdV1() {
795
- return randomUUID();
796
- }
797
- export {
798
- ReportingRowStorageV1,
799
- ReportingRowStoreError2 as ReportingRowStoreError,
800
- isReportingRowStoreError2 as isReportingRowStoreError,
801
- reportingRowWriteIntentIdV1
802
- };