@adcp/sdk 15.5.0 → 15.7.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.
Files changed (81) hide show
  1. package/README.md +15 -0
  2. package/dist/lib/auth/oauth/index.d.mts +1 -0
  3. package/dist/lib/auth/oauth/index.d.ts +1 -0
  4. package/dist/lib/auth/oauth/index.js +5 -0
  5. package/dist/lib/auth/oauth/index.mjs +3 -0
  6. package/dist/lib/auth/oauth/pending-web-flow.d.mts +16 -0
  7. package/dist/lib/auth/oauth/pending-web-flow.d.ts +16 -0
  8. package/dist/lib/auth/oauth/pending-web-flow.js +66 -0
  9. package/dist/lib/auth/oauth/pending-web-flow.mjs +41 -0
  10. package/dist/lib/auth/oauth/web-flow.js +3 -3
  11. package/dist/lib/auth/oauth/web-flow.mjs +3 -3
  12. package/dist/lib/registry/types.generated.d.mts +63 -1
  13. package/dist/lib/registry/types.generated.d.ts +63 -1
  14. package/dist/lib/reporting/azure/index.d.mts +26 -0
  15. package/dist/lib/reporting/azure/index.d.ts +26 -0
  16. package/dist/lib/reporting/azure/index.js +247 -0
  17. package/dist/lib/reporting/azure/index.mjs +230 -0
  18. package/dist/lib/reporting/bigquery/index.d.mts +127 -0
  19. package/dist/lib/reporting/bigquery/index.d.ts +127 -0
  20. package/dist/lib/reporting/bigquery/index.js +379 -0
  21. package/dist/lib/reporting/bigquery/index.mjs +341 -0
  22. package/dist/lib/reporting/ledger/handler.js +20 -8
  23. package/dist/lib/reporting/ledger/handler.mjs +20 -8
  24. package/dist/lib/reporting/ledger/postgres.d.mts +21 -0
  25. package/dist/lib/reporting/ledger/postgres.d.ts +21 -0
  26. package/dist/lib/reporting/ledger/postgres.js +35 -2
  27. package/dist/lib/reporting/ledger/postgres.mjs +35 -2
  28. package/dist/lib/reporting/ledger/row-storage-object.js +31 -8
  29. package/dist/lib/reporting/ledger/row-storage-object.mjs +32 -9
  30. package/dist/lib/reporting/ledger/row-storage-provider-io.d.mts +0 -7
  31. package/dist/lib/reporting/ledger/row-storage-provider-io.d.ts +0 -7
  32. package/dist/lib/reporting/ledger/row-storage-provider-io.js +0 -10
  33. package/dist/lib/reporting/ledger/row-storage-provider-io.mjs +0 -8
  34. package/dist/lib/reporting/s3/index.d.mts +29 -0
  35. package/dist/lib/reporting/s3/index.d.ts +29 -0
  36. package/dist/lib/reporting/s3/index.js +324 -0
  37. package/dist/lib/reporting/s3/index.mjs +317 -0
  38. package/dist/lib/reporting/service/index.d.mts +39 -22
  39. package/dist/lib/reporting/service/index.d.ts +39 -22
  40. package/dist/lib/reporting/service/index.js +75 -64
  41. package/dist/lib/reporting/service/index.mjs +79 -63
  42. package/dist/lib/reporting/service/maintenance.d.mts +91 -0
  43. package/dist/lib/reporting/service/maintenance.d.ts +91 -0
  44. package/dist/lib/reporting/service/maintenance.js +140 -0
  45. package/dist/lib/reporting/service/maintenance.mjs +114 -0
  46. package/dist/lib/schemas/browser.d.mts +14 -0
  47. package/dist/lib/schemas/browser.d.ts +14 -0
  48. package/dist/lib/schemas/browser.js +48 -0
  49. package/dist/lib/schemas/browser.mjs +20 -0
  50. package/dist/lib/schemas/index.d.mts +2 -120
  51. package/dist/lib/schemas/index.d.ts +2 -120
  52. package/dist/lib/schemas/index.js +3 -11
  53. package/dist/lib/schemas/index.mjs +1 -9
  54. package/dist/lib/schemas-data/v2.5/_provenance.json +1 -1
  55. package/dist/lib/testing/index.d.mts +1 -0
  56. package/dist/lib/testing/index.d.ts +1 -0
  57. package/dist/lib/testing/index.js +3 -0
  58. package/dist/lib/testing/index.mjs +2 -0
  59. package/dist/lib/testing/pending-web-flow-store.d.mts +20 -0
  60. package/dist/lib/testing/pending-web-flow-store.d.ts +20 -0
  61. package/dist/lib/testing/pending-web-flow-store.js +132 -0
  62. package/dist/lib/testing/pending-web-flow-store.mjs +98 -0
  63. package/dist/lib/testing/storyboard/validations.d.mts +1 -1
  64. package/dist/lib/testing/storyboard/validations.d.ts +1 -1
  65. package/dist/lib/validation/get-products-request.d.mts +122 -0
  66. package/dist/lib/validation/get-products-request.d.ts +122 -0
  67. package/dist/lib/validation/get-products-request.js +39 -0
  68. package/dist/lib/validation/get-products-request.mjs +14 -0
  69. package/dist/lib/version.d.mts +3 -3
  70. package/dist/lib/version.d.ts +3 -3
  71. package/dist/lib/version.js +3 -3
  72. package/dist/lib/version.mjs +3 -3
  73. package/docs/TYPE-SUMMARY.md +1 -1
  74. package/docs/ZOD-SCHEMAS.md +13 -1
  75. package/docs/guides/OAUTH-ISSUER-BINDING.md +6 -0
  76. package/docs/guides/REPORTING-LEDGER.md +254 -0
  77. package/docs/guides/REPORTING-OPERATIONS.md +82 -1
  78. package/docs/guides/WEB-OAUTH.md +48 -1
  79. package/docs/llms.txt +1 -1
  80. package/docs/migration-14.x-rc-worksheet.md +6 -4
  81. package/package.json +64 -1
@@ -0,0 +1,324 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var s3_exports = {};
20
+ __export(s3_exports, {
21
+ createS3ReportingRowObjectProviderV1: () => createS3ReportingRowObjectProviderV1
22
+ });
23
+ module.exports = __toCommonJS(s3_exports);
24
+ var import_node_crypto = require("node:crypto");
25
+ var import_client_s3 = require("@aws-sdk/client-s3");
26
+ var import_row_storage_errors = require('../ledger/row-storage-errors.js');
27
+ var import_row_storage_provider_io = require('../ledger/row-storage-provider-io.js');
28
+ const BUCKET = /^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$/;
29
+ const REGION = /^[a-z0-9][a-z0-9-]{0,30}[a-z0-9]$/;
30
+ const VERSION_ID = /^[\x21\x23-\x7e]{1,1024}$/;
31
+ function statusOf(error) {
32
+ const status = Number(error?.$metadata?.httpStatusCode);
33
+ return Number.isInteger(status) ? status : void 0;
34
+ }
35
+ function nameOf(error) {
36
+ const value = error?.name ?? error?.Code;
37
+ return typeof value === "string" ? value : "";
38
+ }
39
+ const isPreconditionFailed = (error) => statusOf(error) === 412 || nameOf(error) === "PreconditionFailed";
40
+ const isConditionalConflict = (error) => statusOf(error) === 409 || nameOf(error) === "ConditionalRequestConflict";
41
+ const isNotFound = (error) => statusOf(error) === 404 || ["NoSuchKey", "NotFound", "NoSuchVersion", "NoSuchBucket"].includes(nameOf(error));
42
+ const isNotImplemented = (error) => statusOf(error) === 501 || nameOf(error) === "NotImplemented";
43
+ const isForbidden = (error) => statusOf(error) === 401 || statusOf(error) === 403;
44
+ const isRangeError = (error) => statusOf(error) === 416 || nameOf(error) === "InvalidRange";
45
+ function versionOf(output) {
46
+ const id = output.VersionId;
47
+ if (typeof id !== "string" || id === "null" || !VERSION_ID.test(id)) {
48
+ throw new import_row_storage_errors.ReportingRowStoreError(
49
+ "UNSAFE_BINDING",
50
+ "store returned no object VersionId; S3 bucket versioning must be enabled so deletes are fenced to one write"
51
+ );
52
+ }
53
+ return id;
54
+ }
55
+ function selectorOf(nativeVersion) {
56
+ if (typeof nativeVersion !== "string" || nativeVersion === "null") return null;
57
+ return VERSION_ID.test(nativeVersion) ? { VersionId: nativeVersion } : null;
58
+ }
59
+ function servedVersion(output, selector) {
60
+ return output.VersionId === selector.VersionId;
61
+ }
62
+ function liveExpirationCovers(rule, prefix) {
63
+ if (rule.Status !== "Enabled") return false;
64
+ if (rule.Expiration?.Days === void 0 && rule.Expiration?.Date === void 0) return false;
65
+ if (rule.Filter?.Tag !== void 0 || (rule.Filter?.And?.Tags?.length ?? 0) > 0) return false;
66
+ const filter = rule.Filter?.Prefix ?? rule.Filter?.And?.Prefix ?? rule.Prefix;
67
+ return (0, import_row_storage_provider_io.reportingRowPrefixFilterOverlaps)(filter, prefix);
68
+ }
69
+ function createS3ReportingRowObjectProviderV1(options) {
70
+ if (!options || typeof options !== "object") {
71
+ throw new import_row_storage_errors.ReportingRowStoreError("INVALID_INPUT", "S3 row provider options are required");
72
+ }
73
+ const clients = (0, import_row_storage_provider_io.reportingRowClientMap)(options.clients);
74
+ const isClient = (value) => !!value && typeof value === "object" && typeof value.send === "function";
75
+ if (options.client !== void 0 && !isClient(options.client) || [...clients.values()].some((client) => !isClient(client)) || options.client === void 0 && clients.size === 0) {
76
+ throw new import_row_storage_errors.ReportingRowStoreError("INVALID_INPUT", "inject an official S3Client as client or clients");
77
+ }
78
+ const validateLocation = (location) => {
79
+ (0, import_row_storage_provider_io.assertReportingRowLocationKeys)(location, ["bucket"], ["region"]);
80
+ const bucket = location.bucket;
81
+ if (!BUCKET.test(bucket) || bucket.includes("..") || /^\d+\.\d+\.\d+\.\d+$/.test(bucket)) {
82
+ throw new import_row_storage_errors.ReportingRowStoreError("INVALID_INPUT", "S3 bucket name is invalid");
83
+ }
84
+ if (location.region !== void 0 && !REGION.test(location.region)) {
85
+ throw new import_row_storage_errors.ReportingRowStoreError("INVALID_INPUT", "S3 region is invalid");
86
+ }
87
+ };
88
+ const target = (input) => {
89
+ validateLocation(input.location);
90
+ return {
91
+ client: (0, import_row_storage_provider_io.selectReportingRowClient)(options.client, clients, input.credentialRef),
92
+ Bucket: input.location.bucket
93
+ };
94
+ };
95
+ const assertKey = (key) => {
96
+ if (typeof key !== "string" || key.length === 0 || Buffer.byteLength(key) > 1024) {
97
+ throw new import_row_storage_errors.ReportingRowStoreError("INVALID_INPUT", "object key is invalid");
98
+ }
99
+ };
100
+ async function putCreateOnly(client, params, signal) {
101
+ for (let attempt = 0; ; attempt++) {
102
+ try {
103
+ return await client.send(new import_client_s3.PutObjectCommand({ ...params, IfNoneMatch: "*" }), { abortSignal: signal });
104
+ } catch (error) {
105
+ if (attempt === 0 && isConditionalConflict(error) && !signal.aborted) continue;
106
+ throw error;
107
+ }
108
+ }
109
+ }
110
+ async function assertVersioningEnabled(client, Bucket, signal) {
111
+ let status;
112
+ try {
113
+ status = (await client.send(new import_client_s3.GetBucketVersioningCommand({ Bucket }), { abortSignal: signal })).Status;
114
+ } catch (error) {
115
+ if (isForbidden(error) || isNotImplemented(error)) {
116
+ throw new import_row_storage_errors.ReportingRowStoreError("UNSAFE_BINDING", "bucket versioning status cannot be verified");
117
+ }
118
+ throw error;
119
+ }
120
+ if (status !== "Enabled") {
121
+ throw new import_row_storage_errors.ReportingRowStoreError(
122
+ "UNSAFE_BINDING",
123
+ "S3 bucket versioning must be enabled so row object deletes are fenced to one write"
124
+ );
125
+ }
126
+ }
127
+ async function assertCreateOnlyEnforced(client, Bucket, prefix, signal) {
128
+ const Key = `${prefix}/.adcp-probe/${(0, import_node_crypto.randomUUID)()}`;
129
+ const written = [];
130
+ const put = async (body) => {
131
+ const output = await putCreateOnly(
132
+ client,
133
+ { Bucket, Key, Body: Buffer.from(body), ContentType: "application/x-ndjson", CacheControl: "no-store" },
134
+ signal
135
+ );
136
+ written.push(output);
137
+ versionOf(output);
138
+ };
139
+ try {
140
+ try {
141
+ await put('{"probe":1}\n');
142
+ } catch (error) {
143
+ if (isNotImplemented(error) || isPreconditionFailed(error)) {
144
+ throw new import_row_storage_errors.ReportingRowStoreError("UNSAFE_BINDING", "store does not support create-only writes");
145
+ }
146
+ throw error;
147
+ }
148
+ try {
149
+ await put('{"probe":2}\n');
150
+ } catch (error) {
151
+ if (isPreconditionFailed(error)) return;
152
+ if (isNotImplemented(error)) {
153
+ throw new import_row_storage_errors.ReportingRowStoreError("UNSAFE_BINDING", "store does not support create-only writes");
154
+ }
155
+ throw error;
156
+ }
157
+ throw new import_row_storage_errors.ReportingRowStoreError("UNSAFE_BINDING", "store ignores If-None-Match and replaces objects");
158
+ } finally {
159
+ const versionIds = written.map((output) => output.VersionId).filter((id) => !!id);
160
+ const deletes = versionIds.length > 0 ? versionIds.map((VersionId) => ({ VersionId })) : written.length ? [{}] : [];
161
+ for (const selector of deletes) {
162
+ if (signal.aborted) break;
163
+ await client.send(new import_client_s3.DeleteObjectCommand({ Bucket, Key, ...selector }), { abortSignal: signal }).catch(() => void 0);
164
+ }
165
+ }
166
+ }
167
+ return {
168
+ name: "s3",
169
+ rangedReads: true,
170
+ validateLocation,
171
+ probe(input, context) {
172
+ return (0, import_row_storage_provider_io.runReportingRowProviderOperation)(context.signal, async () => {
173
+ const { client, Bucket } = target(input);
174
+ const signal = context.signal;
175
+ try {
176
+ await client.send(new import_client_s3.HeadBucketCommand({ Bucket }), { abortSignal: signal });
177
+ } catch (error) {
178
+ if (isNotFound(error)) throw new import_row_storage_errors.ReportingRowStoreError("UNSAFE_BINDING", "bucket does not exist");
179
+ if (isForbidden(error)) throw new import_row_storage_errors.ReportingRowStoreError("UNSAFE_BINDING", "bucket is not accessible");
180
+ if (statusOf(error) === 301) {
181
+ throw new import_row_storage_errors.ReportingRowStoreError("UNSAFE_BINDING", "bucket is in a different region");
182
+ }
183
+ throw error;
184
+ }
185
+ if (input.location.region !== void 0) {
186
+ const configured = client.config?.region;
187
+ const region = typeof configured === "function" ? await configured() : configured;
188
+ if (region !== input.location.region) {
189
+ throw new import_row_storage_errors.ReportingRowStoreError("UNSAFE_BINDING", "client region does not match the binding");
190
+ }
191
+ }
192
+ const policy = await client.send(new import_client_s3.GetBucketPolicyStatusCommand({ Bucket }), { abortSignal: signal }).catch(() => void 0);
193
+ if (policy?.PolicyStatus?.IsPublic === true) {
194
+ throw new import_row_storage_errors.ReportingRowStoreError("UNSAFE_BINDING", "bucket policy makes objects public");
195
+ }
196
+ let rules = [];
197
+ try {
198
+ const lifecycle = await client.send(new import_client_s3.GetBucketLifecycleConfigurationCommand({ Bucket }), {
199
+ abortSignal: signal
200
+ });
201
+ rules = lifecycle.Rules ?? [];
202
+ } catch (error) {
203
+ if (isForbidden(error)) {
204
+ throw new import_row_storage_errors.ReportingRowStoreError("UNSAFE_BINDING", "bucket lifecycle cannot be read with this client");
205
+ }
206
+ if (!isNotFound(error) && !isNotImplemented(error) && nameOf(error) !== "NoSuchLifecycleConfiguration") {
207
+ throw error;
208
+ }
209
+ }
210
+ if (rules.some((rule) => liveExpirationCovers(rule, input.prefix))) {
211
+ throw new import_row_storage_errors.ReportingRowStoreError("UNSAFE_BINDING", "a bucket lifecycle rule can expire live row objects");
212
+ }
213
+ await assertVersioningEnabled(client, Bucket, signal);
214
+ await assertCreateOnlyEnforced(client, Bucket, input.prefix, signal);
215
+ });
216
+ },
217
+ putIfAbsent(input, context) {
218
+ return (0, import_row_storage_provider_io.runReportingRowProviderOperation)(context.signal, async () => {
219
+ const { client, Bucket } = target(input);
220
+ assertKey(input.key);
221
+ const bytes = Buffer.from(input.bytes);
222
+ try {
223
+ const output = await putCreateOnly(
224
+ client,
225
+ {
226
+ Bucket,
227
+ Key: input.key,
228
+ Body: bytes,
229
+ ContentLength: bytes.byteLength,
230
+ ContentType: input.contentType,
231
+ CacheControl: "no-store",
232
+ Metadata: { ...input.metadata }
233
+ },
234
+ context.signal
235
+ );
236
+ return { created: true, nativeVersion: versionOf(output) };
237
+ } catch (error) {
238
+ if (!isPreconditionFailed(error)) throw error;
239
+ }
240
+ const head = await client.send(new import_client_s3.HeadObjectCommand({ Bucket, Key: input.key }), {
241
+ abortSignal: context.signal
242
+ });
243
+ return { created: false, nativeVersion: versionOf(head) };
244
+ });
245
+ },
246
+ get(input, context) {
247
+ return (0, import_row_storage_provider_io.runReportingRowProviderOperation)(context.signal, async () => {
248
+ const { client, Bucket } = target(input);
249
+ assertKey(input.key);
250
+ (0, import_row_storage_provider_io.assertReportingRowReadBounds)(input);
251
+ const selector = selectorOf(input.nativeVersion);
252
+ if (selector === null) return null;
253
+ const Key = input.key;
254
+ try {
255
+ if (input.range?.length === 0) {
256
+ const head = await client.send(new import_client_s3.HeadObjectCommand({ Bucket, Key, ...selector }), {
257
+ abortSignal: context.signal
258
+ });
259
+ if (!servedVersion(head, selector)) return null;
260
+ if (input.range.offset > Number(head.ContentLength ?? 0)) {
261
+ throw new import_row_storage_errors.ReportingRowStoreError("ROWS_INTEGRITY_FAILED", "requested range exceeds the stored object");
262
+ }
263
+ return Buffer.alloc(0);
264
+ }
265
+ const output = await client.send(
266
+ new import_client_s3.GetObjectCommand({
267
+ Bucket,
268
+ Key,
269
+ ...selector,
270
+ ...input.range ? { Range: `bytes=${input.range.offset}-${input.range.offset + input.range.length - 1}` } : {}
271
+ }),
272
+ { abortSignal: context.signal }
273
+ );
274
+ const body = output.Body;
275
+ if (!body || typeof body[Symbol.asyncIterator] !== "function") {
276
+ throw new import_row_storage_errors.ReportingRowStoreError("PROVIDER_UNAVAILABLE", "object body is not a stream");
277
+ }
278
+ if (!servedVersion(output, selector)) {
279
+ body.destroy?.();
280
+ return null;
281
+ }
282
+ if (typeof output.ContentLength === "number" && output.ContentLength > input.maxBytes) {
283
+ body.destroy?.();
284
+ throw new import_row_storage_errors.ReportingRowStoreError("ROWS_INTEGRITY_FAILED", "stored object exceeds its recorded size");
285
+ }
286
+ return await (0, import_row_storage_provider_io.readReportingRowStream)(body, input.maxBytes, context.signal);
287
+ } catch (error) {
288
+ if (isNotFound(error) || isPreconditionFailed(error)) return null;
289
+ if (statusOf(error) === 400 && nameOf(error) !== "InvalidRange") return null;
290
+ if (isRangeError(error)) {
291
+ throw new import_row_storage_errors.ReportingRowStoreError("ROWS_INTEGRITY_FAILED", "requested range exceeds the stored object");
292
+ }
293
+ throw error;
294
+ }
295
+ });
296
+ },
297
+ delete(input, context) {
298
+ return (0, import_row_storage_provider_io.runReportingRowProviderOperation)(context.signal, async () => {
299
+ const { client, Bucket } = target(input);
300
+ assertKey(input.key);
301
+ const selector = selectorOf(input.nativeVersion);
302
+ if (selector === null) return "absent";
303
+ const Key = input.key;
304
+ const signal = context.signal;
305
+ try {
306
+ const head = await client.send(new import_client_s3.HeadObjectCommand({ Bucket, Key, ...selector }), { abortSignal: signal });
307
+ if (!servedVersion(head, selector)) return "absent";
308
+ } catch (error) {
309
+ if (isNotFound(error) || isPreconditionFailed(error)) return "absent";
310
+ if (statusOf(error) === 400 || statusOf(error) === 405) return "absent";
311
+ throw error;
312
+ }
313
+ await client.send(new import_client_s3.DeleteObjectCommand({ Bucket, Key, VersionId: selector.VersionId }), {
314
+ abortSignal: signal
315
+ });
316
+ return "deleted";
317
+ });
318
+ }
319
+ };
320
+ }
321
+ // Annotate the CommonJS export names for ESM import in node:
322
+ 0 && (module.exports = {
323
+ createS3ReportingRowObjectProviderV1
324
+ });
@@ -0,0 +1,317 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import {
3
+ DeleteObjectCommand,
4
+ GetBucketLifecycleConfigurationCommand,
5
+ GetBucketPolicyStatusCommand,
6
+ GetBucketVersioningCommand,
7
+ GetObjectCommand,
8
+ HeadBucketCommand,
9
+ HeadObjectCommand,
10
+ PutObjectCommand
11
+ } from "@aws-sdk/client-s3";
12
+ import { ReportingRowStoreError } from "../ledger/row-storage-errors.mjs";
13
+ import {
14
+ assertReportingRowLocationKeys,
15
+ assertReportingRowReadBounds,
16
+ readReportingRowStream,
17
+ reportingRowClientMap,
18
+ reportingRowPrefixFilterOverlaps,
19
+ runReportingRowProviderOperation,
20
+ selectReportingRowClient
21
+ } from "../ledger/row-storage-provider-io.mjs";
22
+ const BUCKET = /^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$/;
23
+ const REGION = /^[a-z0-9][a-z0-9-]{0,30}[a-z0-9]$/;
24
+ const VERSION_ID = /^[\x21\x23-\x7e]{1,1024}$/;
25
+ function statusOf(error) {
26
+ const status = Number(error?.$metadata?.httpStatusCode);
27
+ return Number.isInteger(status) ? status : void 0;
28
+ }
29
+ function nameOf(error) {
30
+ const value = error?.name ?? error?.Code;
31
+ return typeof value === "string" ? value : "";
32
+ }
33
+ const isPreconditionFailed = (error) => statusOf(error) === 412 || nameOf(error) === "PreconditionFailed";
34
+ const isConditionalConflict = (error) => statusOf(error) === 409 || nameOf(error) === "ConditionalRequestConflict";
35
+ const isNotFound = (error) => statusOf(error) === 404 || ["NoSuchKey", "NotFound", "NoSuchVersion", "NoSuchBucket"].includes(nameOf(error));
36
+ const isNotImplemented = (error) => statusOf(error) === 501 || nameOf(error) === "NotImplemented";
37
+ const isForbidden = (error) => statusOf(error) === 401 || statusOf(error) === 403;
38
+ const isRangeError = (error) => statusOf(error) === 416 || nameOf(error) === "InvalidRange";
39
+ function versionOf(output) {
40
+ const id = output.VersionId;
41
+ if (typeof id !== "string" || id === "null" || !VERSION_ID.test(id)) {
42
+ throw new ReportingRowStoreError(
43
+ "UNSAFE_BINDING",
44
+ "store returned no object VersionId; S3 bucket versioning must be enabled so deletes are fenced to one write"
45
+ );
46
+ }
47
+ return id;
48
+ }
49
+ function selectorOf(nativeVersion) {
50
+ if (typeof nativeVersion !== "string" || nativeVersion === "null") return null;
51
+ return VERSION_ID.test(nativeVersion) ? { VersionId: nativeVersion } : null;
52
+ }
53
+ function servedVersion(output, selector) {
54
+ return output.VersionId === selector.VersionId;
55
+ }
56
+ function liveExpirationCovers(rule, prefix) {
57
+ if (rule.Status !== "Enabled") return false;
58
+ if (rule.Expiration?.Days === void 0 && rule.Expiration?.Date === void 0) return false;
59
+ if (rule.Filter?.Tag !== void 0 || (rule.Filter?.And?.Tags?.length ?? 0) > 0) return false;
60
+ const filter = rule.Filter?.Prefix ?? rule.Filter?.And?.Prefix ?? rule.Prefix;
61
+ return reportingRowPrefixFilterOverlaps(filter, prefix);
62
+ }
63
+ function createS3ReportingRowObjectProviderV1(options) {
64
+ if (!options || typeof options !== "object") {
65
+ throw new ReportingRowStoreError("INVALID_INPUT", "S3 row provider options are required");
66
+ }
67
+ const clients = reportingRowClientMap(options.clients);
68
+ const isClient = (value) => !!value && typeof value === "object" && typeof value.send === "function";
69
+ if (options.client !== void 0 && !isClient(options.client) || [...clients.values()].some((client) => !isClient(client)) || options.client === void 0 && clients.size === 0) {
70
+ throw new ReportingRowStoreError("INVALID_INPUT", "inject an official S3Client as client or clients");
71
+ }
72
+ const validateLocation = (location) => {
73
+ assertReportingRowLocationKeys(location, ["bucket"], ["region"]);
74
+ const bucket = location.bucket;
75
+ if (!BUCKET.test(bucket) || bucket.includes("..") || /^\d+\.\d+\.\d+\.\d+$/.test(bucket)) {
76
+ throw new ReportingRowStoreError("INVALID_INPUT", "S3 bucket name is invalid");
77
+ }
78
+ if (location.region !== void 0 && !REGION.test(location.region)) {
79
+ throw new ReportingRowStoreError("INVALID_INPUT", "S3 region is invalid");
80
+ }
81
+ };
82
+ const target = (input) => {
83
+ validateLocation(input.location);
84
+ return {
85
+ client: selectReportingRowClient(options.client, clients, input.credentialRef),
86
+ Bucket: input.location.bucket
87
+ };
88
+ };
89
+ const assertKey = (key) => {
90
+ if (typeof key !== "string" || key.length === 0 || Buffer.byteLength(key) > 1024) {
91
+ throw new ReportingRowStoreError("INVALID_INPUT", "object key is invalid");
92
+ }
93
+ };
94
+ async function putCreateOnly(client, params, signal) {
95
+ for (let attempt = 0; ; attempt++) {
96
+ try {
97
+ return await client.send(new PutObjectCommand({ ...params, IfNoneMatch: "*" }), { abortSignal: signal });
98
+ } catch (error) {
99
+ if (attempt === 0 && isConditionalConflict(error) && !signal.aborted) continue;
100
+ throw error;
101
+ }
102
+ }
103
+ }
104
+ async function assertVersioningEnabled(client, Bucket, signal) {
105
+ let status;
106
+ try {
107
+ status = (await client.send(new GetBucketVersioningCommand({ Bucket }), { abortSignal: signal })).Status;
108
+ } catch (error) {
109
+ if (isForbidden(error) || isNotImplemented(error)) {
110
+ throw new ReportingRowStoreError("UNSAFE_BINDING", "bucket versioning status cannot be verified");
111
+ }
112
+ throw error;
113
+ }
114
+ if (status !== "Enabled") {
115
+ throw new ReportingRowStoreError(
116
+ "UNSAFE_BINDING",
117
+ "S3 bucket versioning must be enabled so row object deletes are fenced to one write"
118
+ );
119
+ }
120
+ }
121
+ async function assertCreateOnlyEnforced(client, Bucket, prefix, signal) {
122
+ const Key = `${prefix}/.adcp-probe/${randomUUID()}`;
123
+ const written = [];
124
+ const put = async (body) => {
125
+ const output = await putCreateOnly(
126
+ client,
127
+ { Bucket, Key, Body: Buffer.from(body), ContentType: "application/x-ndjson", CacheControl: "no-store" },
128
+ signal
129
+ );
130
+ written.push(output);
131
+ versionOf(output);
132
+ };
133
+ try {
134
+ try {
135
+ await put('{"probe":1}\n');
136
+ } catch (error) {
137
+ if (isNotImplemented(error) || isPreconditionFailed(error)) {
138
+ throw new ReportingRowStoreError("UNSAFE_BINDING", "store does not support create-only writes");
139
+ }
140
+ throw error;
141
+ }
142
+ try {
143
+ await put('{"probe":2}\n');
144
+ } catch (error) {
145
+ if (isPreconditionFailed(error)) return;
146
+ if (isNotImplemented(error)) {
147
+ throw new ReportingRowStoreError("UNSAFE_BINDING", "store does not support create-only writes");
148
+ }
149
+ throw error;
150
+ }
151
+ throw new ReportingRowStoreError("UNSAFE_BINDING", "store ignores If-None-Match and replaces objects");
152
+ } finally {
153
+ const versionIds = written.map((output) => output.VersionId).filter((id) => !!id);
154
+ const deletes = versionIds.length > 0 ? versionIds.map((VersionId) => ({ VersionId })) : written.length ? [{}] : [];
155
+ for (const selector of deletes) {
156
+ if (signal.aborted) break;
157
+ await client.send(new DeleteObjectCommand({ Bucket, Key, ...selector }), { abortSignal: signal }).catch(() => void 0);
158
+ }
159
+ }
160
+ }
161
+ return {
162
+ name: "s3",
163
+ rangedReads: true,
164
+ validateLocation,
165
+ probe(input, context) {
166
+ return runReportingRowProviderOperation(context.signal, async () => {
167
+ const { client, Bucket } = target(input);
168
+ const signal = context.signal;
169
+ try {
170
+ await client.send(new HeadBucketCommand({ Bucket }), { abortSignal: signal });
171
+ } catch (error) {
172
+ if (isNotFound(error)) throw new ReportingRowStoreError("UNSAFE_BINDING", "bucket does not exist");
173
+ if (isForbidden(error)) throw new ReportingRowStoreError("UNSAFE_BINDING", "bucket is not accessible");
174
+ if (statusOf(error) === 301) {
175
+ throw new ReportingRowStoreError("UNSAFE_BINDING", "bucket is in a different region");
176
+ }
177
+ throw error;
178
+ }
179
+ if (input.location.region !== void 0) {
180
+ const configured = client.config?.region;
181
+ const region = typeof configured === "function" ? await configured() : configured;
182
+ if (region !== input.location.region) {
183
+ throw new ReportingRowStoreError("UNSAFE_BINDING", "client region does not match the binding");
184
+ }
185
+ }
186
+ const policy = await client.send(new GetBucketPolicyStatusCommand({ Bucket }), { abortSignal: signal }).catch(() => void 0);
187
+ if (policy?.PolicyStatus?.IsPublic === true) {
188
+ throw new ReportingRowStoreError("UNSAFE_BINDING", "bucket policy makes objects public");
189
+ }
190
+ let rules = [];
191
+ try {
192
+ const lifecycle = await client.send(new GetBucketLifecycleConfigurationCommand({ Bucket }), {
193
+ abortSignal: signal
194
+ });
195
+ rules = lifecycle.Rules ?? [];
196
+ } catch (error) {
197
+ if (isForbidden(error)) {
198
+ throw new ReportingRowStoreError("UNSAFE_BINDING", "bucket lifecycle cannot be read with this client");
199
+ }
200
+ if (!isNotFound(error) && !isNotImplemented(error) && nameOf(error) !== "NoSuchLifecycleConfiguration") {
201
+ throw error;
202
+ }
203
+ }
204
+ if (rules.some((rule) => liveExpirationCovers(rule, input.prefix))) {
205
+ throw new ReportingRowStoreError("UNSAFE_BINDING", "a bucket lifecycle rule can expire live row objects");
206
+ }
207
+ await assertVersioningEnabled(client, Bucket, signal);
208
+ await assertCreateOnlyEnforced(client, Bucket, input.prefix, signal);
209
+ });
210
+ },
211
+ putIfAbsent(input, context) {
212
+ return runReportingRowProviderOperation(context.signal, async () => {
213
+ const { client, Bucket } = target(input);
214
+ assertKey(input.key);
215
+ const bytes = Buffer.from(input.bytes);
216
+ try {
217
+ const output = await putCreateOnly(
218
+ client,
219
+ {
220
+ Bucket,
221
+ Key: input.key,
222
+ Body: bytes,
223
+ ContentLength: bytes.byteLength,
224
+ ContentType: input.contentType,
225
+ CacheControl: "no-store",
226
+ Metadata: { ...input.metadata }
227
+ },
228
+ context.signal
229
+ );
230
+ return { created: true, nativeVersion: versionOf(output) };
231
+ } catch (error) {
232
+ if (!isPreconditionFailed(error)) throw error;
233
+ }
234
+ const head = await client.send(new HeadObjectCommand({ Bucket, Key: input.key }), {
235
+ abortSignal: context.signal
236
+ });
237
+ return { created: false, nativeVersion: versionOf(head) };
238
+ });
239
+ },
240
+ get(input, context) {
241
+ return runReportingRowProviderOperation(context.signal, async () => {
242
+ const { client, Bucket } = target(input);
243
+ assertKey(input.key);
244
+ assertReportingRowReadBounds(input);
245
+ const selector = selectorOf(input.nativeVersion);
246
+ if (selector === null) return null;
247
+ const Key = input.key;
248
+ try {
249
+ if (input.range?.length === 0) {
250
+ const head = await client.send(new HeadObjectCommand({ Bucket, Key, ...selector }), {
251
+ abortSignal: context.signal
252
+ });
253
+ if (!servedVersion(head, selector)) return null;
254
+ if (input.range.offset > Number(head.ContentLength ?? 0)) {
255
+ throw new ReportingRowStoreError("ROWS_INTEGRITY_FAILED", "requested range exceeds the stored object");
256
+ }
257
+ return Buffer.alloc(0);
258
+ }
259
+ const output = await client.send(
260
+ new GetObjectCommand({
261
+ Bucket,
262
+ Key,
263
+ ...selector,
264
+ ...input.range ? { Range: `bytes=${input.range.offset}-${input.range.offset + input.range.length - 1}` } : {}
265
+ }),
266
+ { abortSignal: context.signal }
267
+ );
268
+ const body = output.Body;
269
+ if (!body || typeof body[Symbol.asyncIterator] !== "function") {
270
+ throw new ReportingRowStoreError("PROVIDER_UNAVAILABLE", "object body is not a stream");
271
+ }
272
+ if (!servedVersion(output, selector)) {
273
+ body.destroy?.();
274
+ return null;
275
+ }
276
+ if (typeof output.ContentLength === "number" && output.ContentLength > input.maxBytes) {
277
+ body.destroy?.();
278
+ throw new ReportingRowStoreError("ROWS_INTEGRITY_FAILED", "stored object exceeds its recorded size");
279
+ }
280
+ return await readReportingRowStream(body, input.maxBytes, context.signal);
281
+ } catch (error) {
282
+ if (isNotFound(error) || isPreconditionFailed(error)) return null;
283
+ if (statusOf(error) === 400 && nameOf(error) !== "InvalidRange") return null;
284
+ if (isRangeError(error)) {
285
+ throw new ReportingRowStoreError("ROWS_INTEGRITY_FAILED", "requested range exceeds the stored object");
286
+ }
287
+ throw error;
288
+ }
289
+ });
290
+ },
291
+ delete(input, context) {
292
+ return runReportingRowProviderOperation(context.signal, async () => {
293
+ const { client, Bucket } = target(input);
294
+ assertKey(input.key);
295
+ const selector = selectorOf(input.nativeVersion);
296
+ if (selector === null) return "absent";
297
+ const Key = input.key;
298
+ const signal = context.signal;
299
+ try {
300
+ const head = await client.send(new HeadObjectCommand({ Bucket, Key, ...selector }), { abortSignal: signal });
301
+ if (!servedVersion(head, selector)) return "absent";
302
+ } catch (error) {
303
+ if (isNotFound(error) || isPreconditionFailed(error)) return "absent";
304
+ if (statusOf(error) === 400 || statusOf(error) === 405) return "absent";
305
+ throw error;
306
+ }
307
+ await client.send(new DeleteObjectCommand({ Bucket, Key, VersionId: selector.VersionId }), {
308
+ abortSignal: signal
309
+ });
310
+ return "deleted";
311
+ });
312
+ }
313
+ };
314
+ }
315
+ export {
316
+ createS3ReportingRowObjectProviderV1
317
+ };