@cloudbitmaps/core 0.8.2 → 0.10.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 (132) hide show
  1. package/NOTICE +4 -4
  2. package/README.md +28 -6
  3. package/dist/chunk-BUPDGZRR.js +461 -0
  4. package/dist/chunk-BUPDGZRR.js.map +7 -0
  5. package/dist/core/audit.d.ts +127 -0
  6. package/dist/core/bit-route.d.ts +21 -0
  7. package/dist/core/blob.d.ts +34 -0
  8. package/dist/core/budget.d.ts +66 -0
  9. package/dist/core/codec.d.ts +114 -0
  10. package/dist/core/concurrency.d.ts +15 -0
  11. package/dist/core/consistency.d.ts +73 -0
  12. package/dist/core/cooperative.d.ts +78 -0
  13. package/dist/core/cost.d.ts +102 -0
  14. package/dist/core/crbm/crc32c.d.ts +10 -0
  15. package/dist/core/crbm/format.d.ts +116 -0
  16. package/dist/core/crbm/reader.d.ts +69 -0
  17. package/dist/core/crbm/varint.d.ts +13 -0
  18. package/dist/core/crbm/writer.d.ts +39 -0
  19. package/dist/core/crbm-storage-source.d.ts +432 -0
  20. package/dist/core/crypto.d.ts +80 -0
  21. package/dist/core/determinism.d.ts +42 -0
  22. package/dist/core/discover.d.ts +75 -0
  23. package/dist/core/due-index.d.ts +55 -0
  24. package/dist/core/engine.d.ts +204 -0
  25. package/dist/core/erase-id.d.ts +141 -0
  26. package/dist/core/erasure.d.ts +179 -0
  27. package/dist/core/errors.d.ts +120 -0
  28. package/dist/core/generation-gc.d.ts +52 -0
  29. package/dist/core/keys.d.ts +23 -0
  30. package/dist/core/load.d.ts +102 -0
  31. package/dist/core/lru.d.ts +69 -0
  32. package/dist/core/metrics.d.ts +132 -0
  33. package/dist/core/name-codec.d.ts +79 -0
  34. package/dist/core/pinned-storage-source.d.ts +69 -0
  35. package/dist/core/ports.d.ts +340 -0
  36. package/dist/core/registry-scan.d.ts +38 -0
  37. package/dist/core/retention-sweep.d.ts +190 -0
  38. package/dist/core/retention.d.ts +67 -0
  39. package/dist/core/retry.d.ts +65 -0
  40. package/dist/core/rollback.d.ts +71 -0
  41. package/dist/core/validate.d.ts +20 -0
  42. package/dist/driver-kit.d.ts +10 -0
  43. package/dist/driver-kit.js +314 -0
  44. package/dist/driver-kit.js.map +7 -0
  45. package/dist/drivers/_shared/aws-errors.d.ts +18 -0
  46. package/dist/drivers/_shared/keys.d.ts +27 -0
  47. package/dist/drivers/_shared/object-registry-keys.d.ts +28 -0
  48. package/dist/drivers/_shared/object-registry.d.ts +84 -0
  49. package/dist/drivers/_shared/registry.d.ts +62 -0
  50. package/dist/drivers/backends.d.ts +49 -0
  51. package/dist/drivers/crypto.d.ts +38 -0
  52. package/dist/drivers/localfs/fs-util.d.ts +20 -0
  53. package/dist/drivers/localfs/paths.d.ts +33 -0
  54. package/dist/drivers/localfs/registry.d.ts +28 -0
  55. package/dist/drivers/localfs/storage.d.ts +19 -0
  56. package/dist/drivers/memory.d.ts +57 -0
  57. package/dist/drivers/retry/retrying-drivers.d.ts +90 -0
  58. package/dist/export/index.d.ts +92 -0
  59. package/dist/index.d.ts +85 -2534
  60. package/dist/index.js +3076 -2978
  61. package/dist/index.js.map +7 -1
  62. package/dist/testing/fuzz-core.d.ts +14 -0
  63. package/package.json +19 -115
  64. package/dist/azure/index.cjs +0 -368
  65. package/dist/azure/index.cjs.map +0 -1
  66. package/dist/azure/index.d.cts +0 -48
  67. package/dist/azure/index.d.ts +0 -48
  68. package/dist/azure/index.js +0 -304
  69. package/dist/azure/index.js.map +0 -1
  70. package/dist/cassandra/index.cjs +0 -265
  71. package/dist/cassandra/index.cjs.map +0 -1
  72. package/dist/cassandra/index.d.cts +0 -52
  73. package/dist/cassandra/index.d.ts +0 -52
  74. package/dist/cassandra/index.js +0 -204
  75. package/dist/cassandra/index.js.map +0 -1
  76. package/dist/chunk-7LMLYSVJ.js +0 -43
  77. package/dist/chunk-7LMLYSVJ.js.map +0 -1
  78. package/dist/chunk-L54FL5DW.js +0 -203
  79. package/dist/chunk-L54FL5DW.js.map +0 -1
  80. package/dist/chunk-NUIDEEFZ.js +0 -91
  81. package/dist/chunk-NUIDEEFZ.js.map +0 -1
  82. package/dist/chunk-SNJVZ227.js +0 -35
  83. package/dist/chunk-SNJVZ227.js.map +0 -1
  84. package/dist/chunk-YEHMG6W2.js +0 -6
  85. package/dist/chunk-YEHMG6W2.js.map +0 -1
  86. package/dist/dynamodb/index.cjs +0 -740
  87. package/dist/dynamodb/index.cjs.map +0 -1
  88. package/dist/dynamodb/index.d.cts +0 -106
  89. package/dist/dynamodb/index.d.ts +0 -106
  90. package/dist/dynamodb/index.js +0 -469
  91. package/dist/dynamodb/index.js.map +0 -1
  92. package/dist/gcs/index.cjs +0 -343
  93. package/dist/gcs/index.cjs.map +0 -1
  94. package/dist/gcs/index.d.cts +0 -46
  95. package/dist/gcs/index.d.ts +0 -46
  96. package/dist/gcs/index.js +0 -279
  97. package/dist/gcs/index.js.map +0 -1
  98. package/dist/index.cjs +0 -4608
  99. package/dist/index.cjs.map +0 -1
  100. package/dist/index.d.cts +0 -2535
  101. package/dist/mongodb/index.cjs +0 -260
  102. package/dist/mongodb/index.cjs.map +0 -1
  103. package/dist/mongodb/index.d.cts +0 -45
  104. package/dist/mongodb/index.d.ts +0 -45
  105. package/dist/mongodb/index.js +0 -199
  106. package/dist/mongodb/index.js.map +0 -1
  107. package/dist/mysql/index.cjs +0 -281
  108. package/dist/mysql/index.cjs.map +0 -1
  109. package/dist/mysql/index.d.cts +0 -56
  110. package/dist/mysql/index.d.ts +0 -56
  111. package/dist/mysql/index.js +0 -211
  112. package/dist/mysql/index.js.map +0 -1
  113. package/dist/ports-5BQTGKze.d.cts +0 -356
  114. package/dist/ports-5BQTGKze.d.ts +0 -356
  115. package/dist/postgres/index.cjs +0 -273
  116. package/dist/postgres/index.cjs.map +0 -1
  117. package/dist/postgres/index.d.cts +0 -54
  118. package/dist/postgres/index.d.ts +0 -54
  119. package/dist/postgres/index.js +0 -203
  120. package/dist/postgres/index.js.map +0 -1
  121. package/dist/redis/index.cjs +0 -257
  122. package/dist/redis/index.cjs.map +0 -1
  123. package/dist/redis/index.d.cts +0 -42
  124. package/dist/redis/index.d.ts +0 -42
  125. package/dist/redis/index.js +0 -197
  126. package/dist/redis/index.js.map +0 -1
  127. package/dist/s3/index.cjs +0 -891
  128. package/dist/s3/index.cjs.map +0 -1
  129. package/dist/s3/index.d.cts +0 -112
  130. package/dist/s3/index.d.ts +0 -112
  131. package/dist/s3/index.js +0 -555
  132. package/dist/s3/index.js.map +0 -1
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Internal surface for the coverage-guided fuzz harness, CORE half.
3
+ *
4
+ * Exposes the untrusted-bytes entry points the fuzz targets drive — including `parseIndex`, which is
5
+ * deliberately **NOT public API**. Built by a dedicated `tsup` entry to the git-ignored repo-root `fuzz/build/`,
6
+ * so it never enters `dist/` or the published package. Keeping it here (rather than widening
7
+ * `packages/core/src/index.ts`) is the point: a test harness must not expand the published surface.
8
+ * The codec half lives in the flavor package (`packages/roaring/src/testing/fuzz-codec.ts`), because a concrete
9
+ * bitmap codec is exactly what core does not have.
10
+ */
11
+ export { CrbmReader, parseIndex } from '../core/crbm/reader.js';
12
+ export { BufferReader } from '../core/blob.js';
13
+ export { CloudRoaringError, isCloudRoaringError } from '../core/errors.js';
14
+ export { DEFAULT_MAX_PAYLOAD_BYTES } from '../core/crbm/format.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cloudbitmaps/core",
3
- "version": "0.8.2",
4
- "description": "The codec-agnostic cloud engine behind the @cloudbitmaps family: tiered storage (RAM → warm KV → cold object store), chunk-skipping intersection, the .crbm format, crash-safe compaction, encryption-at-rest, and every storage driver.",
3
+ "version": "0.10.0",
4
+ "description": "Codec-agnostic engine for CloudBitmaps: the .crbm format, the write-once load/publish path, generation GC, encryption-at-rest, and the driver ports. No cloud SDK.",
5
5
  "keywords": [
6
6
  "roaring",
7
7
  "bitmap",
@@ -15,18 +15,8 @@
15
15
  "typescript",
16
16
  "set",
17
17
  "set-intersection",
18
- "s3",
19
- "dynamodb",
20
- "gcs",
21
- "azure",
22
- "postgres",
23
- "redis",
24
- "mongodb",
25
- "cassandra",
26
- "mysql",
27
- "mariadb",
28
18
  "serverless",
29
- "tiered-storage",
19
+ "object-storage",
30
20
  "scale-to-zero",
31
21
  "segmentation",
32
22
  "audience",
@@ -36,7 +26,8 @@
36
26
  "author": "Sharvil Kadam",
37
27
  "repository": {
38
28
  "type": "git",
39
- "url": "git+https://github.com/cloudbitmaps/cloudbitmaps.git"
29
+ "url": "git+https://github.com/cloudbitmaps/cloudbitmaps.git",
30
+ "directory": "packages/core"
40
31
  },
41
32
  "homepage": "https://github.com/cloudbitmaps/cloudbitmaps#readme",
42
33
  "bugs": {
@@ -44,62 +35,19 @@
44
35
  },
45
36
  "type": "module",
46
37
  "engines": {
47
- "node": ">=22"
38
+ "node": ">=22.12"
48
39
  },
49
40
  "sideEffects": false,
50
- "main": "./dist/index.cjs",
51
- "module": "./dist/index.js",
41
+ "main": "./dist/index.js",
52
42
  "types": "./dist/index.d.ts",
53
43
  "exports": {
54
44
  ".": {
55
45
  "types": "./dist/index.d.ts",
56
- "import": "./dist/index.js",
57
- "require": "./dist/index.cjs"
46
+ "default": "./dist/index.js"
58
47
  },
59
- "./s3": {
60
- "types": "./dist/s3/index.d.ts",
61
- "import": "./dist/s3/index.js",
62
- "require": "./dist/s3/index.cjs"
63
- },
64
- "./dynamodb": {
65
- "types": "./dist/dynamodb/index.d.ts",
66
- "import": "./dist/dynamodb/index.js",
67
- "require": "./dist/dynamodb/index.cjs"
68
- },
69
- "./gcs": {
70
- "types": "./dist/gcs/index.d.ts",
71
- "import": "./dist/gcs/index.js",
72
- "require": "./dist/gcs/index.cjs"
73
- },
74
- "./azure": {
75
- "types": "./dist/azure/index.d.ts",
76
- "import": "./dist/azure/index.js",
77
- "require": "./dist/azure/index.cjs"
78
- },
79
- "./postgres": {
80
- "types": "./dist/postgres/index.d.ts",
81
- "import": "./dist/postgres/index.js",
82
- "require": "./dist/postgres/index.cjs"
83
- },
84
- "./redis": {
85
- "types": "./dist/redis/index.d.ts",
86
- "import": "./dist/redis/index.js",
87
- "require": "./dist/redis/index.cjs"
88
- },
89
- "./mongodb": {
90
- "types": "./dist/mongodb/index.d.ts",
91
- "import": "./dist/mongodb/index.js",
92
- "require": "./dist/mongodb/index.cjs"
93
- },
94
- "./cassandra": {
95
- "types": "./dist/cassandra/index.d.ts",
96
- "import": "./dist/cassandra/index.js",
97
- "require": "./dist/cassandra/index.cjs"
98
- },
99
- "./mysql": {
100
- "types": "./dist/mysql/index.d.ts",
101
- "import": "./dist/mysql/index.js",
102
- "require": "./dist/mysql/index.cjs"
48
+ "./driver-kit": {
49
+ "types": "./dist/driver-kit.d.ts",
50
+ "default": "./dist/driver-kit.js"
103
51
  }
104
52
  },
105
53
  "files": [
@@ -111,62 +59,18 @@
111
59
  "publishConfig": {
112
60
  "access": "public"
113
61
  },
114
- "peerDependencies": {
115
- "@aws-sdk/client-dynamodb": "^3",
116
- "@aws-sdk/client-s3": "^3",
117
- "@azure/storage-blob": "^12",
118
- "@google-cloud/storage": "^7",
119
- "cassandra-driver": "^4",
120
- "ioredis": "^5",
121
- "mongodb": "^6",
122
- "mysql2": "^3",
123
- "pg": "^8"
124
- },
125
- "peerDependenciesMeta": {
126
- "@aws-sdk/client-dynamodb": {
127
- "optional": true
128
- },
129
- "@aws-sdk/client-s3": {
130
- "optional": true
131
- },
132
- "@azure/storage-blob": {
133
- "optional": true
134
- },
135
- "@google-cloud/storage": {
136
- "optional": true
137
- },
138
- "cassandra-driver": {
139
- "optional": true
140
- },
141
- "ioredis": {
142
- "optional": true
143
- },
144
- "mongodb": {
145
- "optional": true
146
- },
147
- "mysql2": {
148
- "optional": true
149
- },
150
- "pg": {
151
- "optional": true
152
- }
153
- },
154
62
  "devDependencies": {
155
- "@aws-sdk/client-dynamodb": "^3.1075.0",
156
- "@aws-sdk/client-s3": "^3.1075.0",
157
- "@azure/storage-blob": "^12",
158
- "@google-cloud/storage": "^7.0.0",
159
- "cassandra-driver": "^4",
160
- "ioredis": "^5",
161
- "mongodb": "^6",
162
- "mysql2": "^3.23.1",
163
- "pg": "^8",
164
- "@types/pg": "^8",
165
63
  "@types/node": "^22.0.0",
166
- "tsup": "^8.0.0",
167
64
  "typescript": "^5.5.0"
168
65
  },
66
+ "typesVersions": {
67
+ "*": {
68
+ "driver-kit": [
69
+ "./dist/driver-kit.d.ts"
70
+ ]
71
+ }
72
+ },
169
73
  "scripts": {
170
- "build": "tsup"
74
+ "build": "node ../../scripts/build.mjs"
171
75
  }
172
76
  }
@@ -1,368 +0,0 @@
1
- 'use strict';
2
-
3
- var crypto = require('crypto');
4
-
5
- // src/drivers/azure/cold.ts
6
-
7
- // src/core/errors.ts
8
- var ERROR_BRAND = /* @__PURE__ */ Symbol.for("cloud-roaring.error");
9
- var TRANSIENT_BRAND = /* @__PURE__ */ Symbol.for("cloud-roaring.error.transient");
10
- var CloudRoaringError = class extends Error {
11
- /** Cross-bundle brand — see the predicates ({@link isCloudRoaringError}, …). Non-enumerable-ish (symbol key ⇒ not in JSON). */
12
- [ERROR_BRAND] = true;
13
- constructor(message) {
14
- super(message);
15
- this.name = new.target.name;
16
- }
17
- };
18
- var ValidationError = class extends CloudRoaringError {
19
- };
20
- var WriteConflictError = class extends CloudRoaringError {
21
- };
22
- var NotFoundError = class extends CloudRoaringError {
23
- };
24
- var TransientError = class extends CloudRoaringError {
25
- /** A second brand so the whole transient subtree (incl. {@link TimeoutError}) is classifiable cross-bundle. */
26
- [TRANSIENT_BRAND] = true;
27
- constructor(message, options) {
28
- super(message);
29
- if (options && "cause" in options) this.cause = options.cause;
30
- }
31
- };
32
- function hasBrand(err, brand) {
33
- return typeof err === "object" && err !== null && err[brand] === true;
34
- }
35
- function isCloudRoaringError(err) {
36
- return hasBrand(err, ERROR_BRAND);
37
- }
38
- function isWriteConflictError(err) {
39
- return isCloudRoaringError(err) && err.name === "WriteConflictError";
40
- }
41
- function isNotFoundError(err) {
42
- return isCloudRoaringError(err) && err.name === "NotFoundError";
43
- }
44
- function isValidationError(err) {
45
- return isCloudRoaringError(err) && err.name === "ValidationError";
46
- }
47
-
48
- // src/core/validate.ts
49
- var NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,255}$/;
50
- function validatePart(value, field) {
51
- if (typeof value !== "string" || !NAME.test(value) || value.includes("..")) {
52
- throw new ValidationError(
53
- `${field} must match ${String(NAME)} and contain no "..": got ${JSON.stringify(value)}`
54
- );
55
- }
56
- }
57
- function validateSegmentRef(ref) {
58
- validatePart(ref.segment, "segment");
59
- if (ref.namespace !== void 0) validatePart(ref.namespace, "namespace");
60
- }
61
-
62
- // src/drivers/_shared/keys.ts
63
- var DEFAULT_NAMESPACE = "_default";
64
- function namespacePart(namespace) {
65
- return namespace ?? DEFAULT_NAMESPACE;
66
- }
67
-
68
- // src/drivers/azure/keys.ts
69
- var SUFFIX = ".crbm";
70
- function prefixPart(prefix) {
71
- if (prefix === void 0) return "";
72
- const trimmed = prefix.replace(/^\/+|\/+$/g, "");
73
- return trimmed === "" ? "" : `${trimmed}/`;
74
- }
75
- function normalizeAzurePrefix(prefix) {
76
- if (prefix === void 0) return void 0;
77
- for (const ch of prefix) {
78
- if (ch.charCodeAt(0) < 32) {
79
- throw new ValidationError("prefix must not contain control characters");
80
- }
81
- }
82
- for (const segment of prefix.split("/")) {
83
- if (segment === "." || segment === "..") {
84
- throw new ValidationError(
85
- `prefix must not contain "." or ".." path segments: ${JSON.stringify(prefix)}`
86
- );
87
- }
88
- }
89
- return prefix;
90
- }
91
- function segmentObjectPrefix(prefix, ref) {
92
- validateSegmentRef(ref);
93
- return `${prefixPart(prefix)}${namespacePart(ref.namespace)}/segments/${ref.segment}.`;
94
- }
95
- function coldObjectName(prefix, key) {
96
- if (!Number.isInteger(key.generation) || key.generation < 0) {
97
- throw new ValidationError(`generation must be a non-negative integer; got ${key.generation}`);
98
- }
99
- return `${segmentObjectPrefix(prefix, key)}${key.generation}${SUFFIX}`;
100
- }
101
- function parseGenerationFromName(segmentPrefix, objectName) {
102
- if (!objectName.startsWith(segmentPrefix) || !objectName.endsWith(SUFFIX)) return null;
103
- const middle = objectName.slice(segmentPrefix.length, objectName.length - SUFFIX.length);
104
- if (!/^(0|[1-9]\d*)$/.test(middle)) return null;
105
- const generation = Number(middle);
106
- return Number.isSafeInteger(generation) ? generation : null;
107
- }
108
-
109
- // src/drivers/azure/azure-errors.ts
110
- function httpStatus(err) {
111
- const code = err?.statusCode;
112
- return typeof code === "number" ? code : void 0;
113
- }
114
- function azureCode(err) {
115
- const e = err;
116
- if (typeof e?.code === "string") return e.code;
117
- if (typeof e?.details?.errorCode === "string") return e.details.errorCode;
118
- return void 0;
119
- }
120
- function networkCode(err) {
121
- const code = err?.code;
122
- return typeof code === "string" ? code : void 0;
123
- }
124
- function isConditionalConflict(err) {
125
- const status = httpStatus(err);
126
- if (status === 409 || status === 412) return true;
127
- const code = azureCode(err);
128
- return code === "BlobAlreadyExists" || code === "ConditionNotMet";
129
- }
130
- function isNotFound(err) {
131
- return httpStatus(err) === 404 || azureCode(err) === "BlobNotFound";
132
- }
133
- function isInvalidRange(err) {
134
- return httpStatus(err) === 416 || azureCode(err) === "InvalidRange";
135
- }
136
- function isTransient(err) {
137
- if (isConditionalConflict(err) || isNotFound(err) || isInvalidRange(err)) return false;
138
- const status = httpStatus(err);
139
- if (status === 429 || status !== void 0 && status >= 500 && status < 600) return true;
140
- const code = azureCode(err);
141
- if (code === "ServerBusy" || code === "OperationTimedOut" || code === "InternalError")
142
- return true;
143
- const net = networkCode(err);
144
- return net === "ECONNRESET" || net === "ECONNABORTED" || net === "ETIMEDOUT" || net === "ESOCKETTIMEDOUT" || net === "ECONNREFUSED" || net === "EHOSTUNREACH" || net === "ENETUNREACH" || net === "EPIPE" || net === "EAI_AGAIN" || net === "ENOTFOUND";
145
- }
146
-
147
- // src/drivers/azure/cold.ts
148
- var AZURE_BLOCK_BYTES = 8 * 1024 * 1024;
149
- var AZURE_MAX_BLOCKS = 5e4;
150
- var OCTET_STREAM = { blobContentType: "application/octet-stream" };
151
- var IF_ABSENT = { conditions: { ifNoneMatch: "*" }, blobHTTPHeaders: OCTET_STREAM };
152
- var AzureBlobColdDriver = class {
153
- container;
154
- prefix;
155
- maxObjectBytes;
156
- blockBytes;
157
- constructor(options) {
158
- this.container = options.containerClient;
159
- this.prefix = normalizeAzurePrefix(options.prefix);
160
- const requestedBlock = options.blockBytes ?? AZURE_BLOCK_BYTES;
161
- if (!Number.isSafeInteger(requestedBlock) || requestedBlock < 1) {
162
- throw new ValidationError(
163
- `blockBytes must be a positive safe integer; got ${requestedBlock}`
164
- );
165
- }
166
- if (options.maxObjectBytes !== void 0 && (!Number.isSafeInteger(options.maxObjectBytes) || options.maxObjectBytes < 1)) {
167
- throw new ValidationError(
168
- `maxObjectBytes must be a positive safe integer; got ${options.maxObjectBytes}`
169
- );
170
- }
171
- this.maxObjectBytes = options.maxObjectBytes ?? requestedBlock * AZURE_MAX_BLOCKS;
172
- this.blockBytes = Math.max(requestedBlock, Math.ceil(this.maxObjectBytes / AZURE_MAX_BLOCKS));
173
- }
174
- capabilities() {
175
- return { rangeRead: true, maxObjectBytes: this.maxObjectBytes, conditionalPut: true };
176
- }
177
- blob(name) {
178
- return this.container.getBlockBlobClient(name);
179
- }
180
- async putImmutable(key, write) {
181
- const objectName = coldObjectName(this.prefix, key);
182
- const sink = new AzureBlockBlobSink(
183
- this.blob(objectName),
184
- this.blockBytes,
185
- this.maxObjectBytes
186
- );
187
- try {
188
- await write(sink);
189
- return await sink.finish();
190
- } catch (err) {
191
- if (isConditionalConflict(err)) {
192
- throw new WriteConflictError(
193
- `generation already exists (write-once): ${key.segment}.${key.generation}`
194
- );
195
- }
196
- if (isValidationError(err) || isWriteConflictError(err) || isNotFoundError(err)) throw err;
197
- throw this.mapError(err);
198
- }
199
- }
200
- async getRange(key, offset, length) {
201
- if (!Number.isInteger(offset) || !Number.isInteger(length) || offset < 0 || length < 0) {
202
- throw new ValidationError(`invalid range offset=${offset} length=${length}`);
203
- }
204
- if (length === 0) return new Uint8Array(0);
205
- const objectName = coldObjectName(this.prefix, key);
206
- try {
207
- const res = await this.blob(objectName).download(offset, length);
208
- const bytes = await collect(res.readableStreamBody);
209
- if (bytes.length !== length) {
210
- throw new ValidationError(
211
- `range [${offset}, ${offset + length}) out of bounds (got ${bytes.length}B)`
212
- );
213
- }
214
- return bytes;
215
- } catch (err) {
216
- throw this.mapReadError(err, key);
217
- }
218
- }
219
- async getTail(key, maxBytes) {
220
- const objectName = coldObjectName(this.prefix, key);
221
- try {
222
- const props = await this.blob(objectName).getProperties();
223
- const size = props.contentLength ?? 0;
224
- if (!Number.isSafeInteger(size) || size < 0) {
225
- throw new ValidationError(`Azure returned an invalid blob size: ${String(size)}`);
226
- }
227
- if (maxBytes <= 0 || size === 0) return { bytes: new Uint8Array(0), size };
228
- const take = Math.min(maxBytes, size);
229
- const res = await this.blob(objectName).download(size - take, take);
230
- return { bytes: await collect(res.readableStreamBody), size };
231
- } catch (err) {
232
- throw this.mapReadError(err, key);
233
- }
234
- }
235
- async delete(key) {
236
- try {
237
- await this.blob(coldObjectName(this.prefix, key)).deleteIfExists();
238
- } catch (err) {
239
- throw this.mapError(err);
240
- }
241
- }
242
- async *list(ref) {
243
- const prefix = segmentObjectPrefix(this.prefix, ref);
244
- try {
245
- for await (const item of this.container.listBlobsFlat({ prefix })) {
246
- const generation = parseGenerationFromName(prefix, item.name);
247
- if (generation !== null) {
248
- yield { namespace: ref.namespace, segment: ref.segment, generation };
249
- }
250
- }
251
- } catch (err) {
252
- throw this.mapError(err);
253
- }
254
- }
255
- /** Map Azure read errors to the driver vocabulary; pass everything else through {@link mapError}. */
256
- mapReadError(err, key) {
257
- if (isValidationError(err)) return err;
258
- if (isNotFound(err))
259
- return new NotFoundError(`no such generation: ${key.segment}.${key.generation}`);
260
- if (isInvalidRange(err)) {
261
- return new ValidationError(`range out of bounds for ${key.segment}.${key.generation}`);
262
- }
263
- return this.mapError(err);
264
- }
265
- /**
266
- * Reclassify a transient Azure fault (throttle/5xx/dropped connection) as a retryable {@link TransientError}
267
- * so the retry decorator can ride it out; everything else propagates unchanged. The final fallback at every
268
- * client-call site, so callers + the decorator only ever see typed errors.
269
- */
270
- mapError(err) {
271
- if (isTransient(err)) {
272
- return new TransientError(
273
- `transient Azure fault: ${err?.code ?? "unknown"}`,
274
- { cause: err }
275
- );
276
- }
277
- return err;
278
- }
279
- };
280
- function concatBytes(parts, total) {
281
- const out = new Uint8Array(total);
282
- let offset = 0;
283
- for (const p of parts) {
284
- out.set(p, offset);
285
- offset += p.length;
286
- }
287
- return out;
288
- }
289
- var AzureBlockBlobSink = class {
290
- constructor(blob, blockBytes, maxObjectBytes) {
291
- this.blob = blob;
292
- this.blockBytes = blockBytes;
293
- this.maxObjectBytes = maxObjectBytes;
294
- }
295
- blob;
296
- blockBytes;
297
- maxObjectBytes;
298
- hash = crypto.createHash("sha256");
299
- pending = [];
300
- pendingLen = 0;
301
- total = 0;
302
- blockIds = [];
303
- /**
304
- * A random per-sink nonce folded into every block id. Azure pools **uncommitted** blocks per *blob name*,
305
- * keyed by id — so two writers racing the same generation key on the staged path MUST use disjoint id spaces,
306
- * else one would overwrite the other's staged blocks and a commit could reference an interleaved mix of both
307
- * writers' bytes (a corrupt blob whose committed content wouldn't match the returned SHA-256). A per-instance
308
- * nonce makes each writer's ids unique, so the winner commits only its own blocks and the loser cleanly 409s.
309
- * 6 random bytes → 12 fixed hex chars, keeping every id equal-length (Azure's within-blob id requirement).
310
- */
311
- uploadNonce = crypto.randomBytes(6).toString("hex");
312
- /** Fixed-width (constant length within a blob) AND per-sink-unique block id: `<nonce>-<zero-padded index>`. */
313
- blockId(n) {
314
- return Buffer.from(`${this.uploadNonce}-${String(n).padStart(6, "0")}`).toString("base64");
315
- }
316
- async write(bytes) {
317
- if (bytes.length === 0) return;
318
- this.total += bytes.length;
319
- if (this.total > this.maxObjectBytes) {
320
- throw new ValidationError(`object exceeds maxObjectBytes ${this.maxObjectBytes}`);
321
- }
322
- this.hash.update(bytes);
323
- this.pending.push(new Uint8Array(bytes));
324
- this.pendingLen += bytes.length;
325
- if (this.pendingLen >= this.blockBytes) await this.flushBlock();
326
- }
327
- /** Stage the buffered bytes (≥ one block) as a single block, freeing them. */
328
- async flushBlock() {
329
- if (this.blockIds.length >= AZURE_MAX_BLOCKS) {
330
- throw new ValidationError(`upload exceeded the Azure ${AZURE_MAX_BLOCKS}-block limit`);
331
- }
332
- const body = concatBytes(this.pending, this.pendingLen);
333
- this.pending.length = 0;
334
- this.pendingLen = 0;
335
- const id = this.blockId(this.blockIds.length);
336
- await this.blob.stageBlock(id, body, body.length);
337
- this.blockIds.push(id);
338
- }
339
- /** Commit the blob: a single conditional `upload` if it fit in one block, else commit the staged block list. */
340
- async finish() {
341
- const sha256 = this.hash.digest("hex");
342
- if (this.blockIds.length === 0) {
343
- const body = concatBytes(this.pending, this.pendingLen);
344
- await this.blob.upload(body, body.length, IF_ABSENT);
345
- return { size: this.total, sha256 };
346
- }
347
- if (this.pendingLen > 0) await this.flushBlock();
348
- await this.blob.commitBlockList(this.blockIds, IF_ABSENT);
349
- return { size: this.total, sha256 };
350
- }
351
- };
352
- async function collect(body) {
353
- if (body === void 0) {
354
- throw new NotFoundError("Azure download returned no body");
355
- }
356
- const chunks = [];
357
- let total = 0;
358
- for await (const chunk of body) {
359
- const u8 = typeof chunk === "string" ? new TextEncoder().encode(chunk) : new Uint8Array(chunk);
360
- chunks.push(u8);
361
- total += u8.length;
362
- }
363
- return concatBytes(chunks, total);
364
- }
365
-
366
- exports.AzureBlobColdDriver = AzureBlobColdDriver;
367
- //# sourceMappingURL=index.cjs.map
368
- //# sourceMappingURL=index.cjs.map