@cloudbitmaps/gcs 0.10.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,11 @@
1
+ @cloudbitmaps/gcs
2
+ Copyright 2026 Sharvil Kadam
3
+
4
+ This product includes software developed as an open-source project.
5
+ Licensed under the Apache License, Version 2.0 (the "License"); see the LICENSE
6
+ file. You may obtain a copy of the License at:
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ @cloudbitmaps/gcs is a storage driver for CloudBitmaps. It contains no bitmap code and is
11
+ codec-agnostic: it moves opaque payload bytes.
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # @cloudbitmaps/gcs
2
+
3
+ **Google Cloud Storage for [CloudBitmaps](https://github.com/cloudbitmaps/cloudbitmaps).**
4
+
5
+ > **ESM-only, Node ≥ 22.12.** This package ships as ES modules; there is no CommonJS bundle.
6
+ > `require()` works on Node 22.12+ through `require(esm)`, but a runner with its own CommonJS loader
7
+ > (notably Jest in its default configuration) does not get that and needs `import` instead. On TypeScript,
8
+ > a CommonJS project needs `"module": "nodenext"` or `"node20"`. See the
9
+ > [repository README](https://github.com/cloudbitmaps/cloudbitmaps#install--entry-points) for the details.
10
+
11
+ Google Cloud Storage. Compare-and-swap rides GCS object preconditions, so a deployment runs on **one bucket alone** — no second service holds the pointer.
12
+
13
+ ## Install
14
+
15
+ ```bash
16
+ pnpm add @cloudbitmaps/roaring @cloudbitmaps/gcs
17
+ # npm i @cloudbitmaps/roaring @cloudbitmaps/gcs # the same, with npm
18
+ ```
19
+
20
+ > **On pnpm 10+, allow the one build script.** pnpm 10 skips dependency build scripts by default, so
21
+ > the `roaring` native addon never downloads and the package throws at `import` — while the install
22
+ > itself prints a warning and **exits 0**. Add this to your `package.json`, then install:
23
+ >
24
+ > ```json
25
+ > { "pnpm": { "onlyBuiltDependencies": ["roaring"] } }
26
+ > ```
27
+ >
28
+ > pnpm 9 and npm run it already. [Full symptoms and fixes](https://github.com/cloudbitmaps/cloudbitmaps/blob/main/docs/guide/getting-started.md#cannot-find-module-buildreleaseroaringnode-after-a-successful-install).
29
+
30
+ Two packages: the **codec** you want and the **storage** you have. `@google-cloud/storage` (`^7 || ^8`) is a real dependency of
31
+ this package, so installing it is the whole step — there is no optional peer to remember. `@cloudbitmaps/core` is one
32
+ too, so the engine lands in your tree without you installing it — you never name it yourself.
33
+
34
+ ## Use
35
+
36
+ ```ts
37
+ import { CloudRoaring } from '@cloudbitmaps/roaring';
38
+ import { GcsStorage } from '@cloudbitmaps/gcs';
39
+
40
+ const store = new CloudRoaring({
41
+ storage: new GcsStorage({ bucket: 'bitmaps', prefix: 'cr' }),
42
+ });
43
+
44
+ await store.load({ segment: 'active-users' }, [1, 2, 3]);
45
+ const seg = store.segment('active-users');
46
+ await seg.has(2); // true
47
+ ```
48
+
49
+ `GcsStorage` configures both halves — the immutable generation objects and the registry pointer row — from one set
50
+ of values. Need them apart? This package also exports the two drivers and their option types; see the
51
+ [API reference](https://github.com/cloudbitmaps/cloudbitmaps/blob/main/docs/guide/api-reference.md).
52
+
53
+ ## What this package is
54
+
55
+ These drivers move opaque payload bytes, so they are codec-agnostic: the same package serves every codec
56
+ flavor. That is why storage is a package rather than a subpath of one — as a subpath, each flavor needed a
57
+ re-export barrel per service, and the count multiplied with every codec added.
58
+
59
+ Built against `@cloudbitmaps/core/driver-kit`, the declared contract for a driver — the same surface a
60
+ third-party driver would use.
61
+
62
+ ## Documentation
63
+
64
+ - [Getting started](https://github.com/cloudbitmaps/cloudbitmaps/blob/main/docs/guide/getting-started.md)
65
+ - [API reference](https://github.com/cloudbitmaps/cloudbitmaps/blob/main/docs/guide/api-reference.md)
66
+ - [Changelog](https://github.com/cloudbitmaps/cloudbitmaps/blob/main/CHANGELOG.md)
67
+
68
+ ## License
69
+
70
+ Apache-2.0
@@ -0,0 +1,43 @@
1
+ /**
2
+ * `GcsStorage` — the Google Cloud Storage backend as one object: generations and pointer, one bucket, stated once.
3
+ *
4
+ * See {@link StorageBackend} for why the two halves are configured together rather than separately.
5
+ *
6
+ * **A note on the word.** `@google-cloud/storage` names its own client class `Storage`, so the driver option
7
+ * that takes it is also called `storage` — which, since our durable tier is now called storage too, reads as
8
+ * the same word meaning two things. That is why this class builds the client itself: `new GcsStorage({ bucket })`
9
+ * never makes you write `{ storage: storage }`. Supply `client` when you need your own.
10
+ */
11
+ import { STORAGE_BACKEND } from '@cloudbitmaps/core/driver-kit';
12
+ import type { IRegistryDriver, IStorageDriver, StorageBackend } from '@cloudbitmaps/core/driver-kit';
13
+ import { Storage as GcsClient } from '@google-cloud/storage';
14
+ export interface GcsStorageOptions {
15
+ /** Target bucket (must already exist). */
16
+ readonly bucket: string;
17
+ /** Optional object-name prefix under which everything lives — generations and the registry alike. */
18
+ readonly prefix?: string;
19
+ /** A constructed `@google-cloud/storage` client. One is built from the ambient credentials when absent. */
20
+ readonly client?: GcsClient;
21
+ /**
22
+ * Not an option — the old `GcsStorageDriver` took the client as `storage`, and a migrating caller keeps the
23
+ * name. Typed `never` so the object literal is a compile error, and rejected at runtime for JavaScript
24
+ * callers, because silently ignoring it falls back to ambient credentials and the **public** endpoint: for
25
+ * anyone whose client pointed at an emulator, that is production traffic from a wiring typo.
26
+ */
27
+ readonly storage?: never;
28
+ /** Project id for the client built when `client` is absent. Falls back to the SDK's own resolution. */
29
+ readonly projectId?: string;
30
+ /** Endpoint override — point it at fake-gcs-server locally. Ignored when `client` is supplied. */
31
+ readonly apiEndpoint?: string;
32
+ /** Injected clock for the registry's `createdAt`/`updatedAt`; defaults to `Date.now`. */
33
+ readonly now?: () => number;
34
+ }
35
+ export declare class GcsStorage implements StorageBackend {
36
+ /** Cross-bundle brand, stamped non-enumerably in the constructor so a spread cannot carry it. */
37
+ readonly [STORAGE_BACKEND]: true;
38
+ readonly storage: IStorageDriver;
39
+ readonly registry: IRegistryDriver;
40
+ /** The client both halves share — built here unless one was supplied. */
41
+ readonly client: GcsClient;
42
+ constructor(options: GcsStorageOptions);
43
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Pure helpers for classifying Google Cloud Storage SDK errors (the transient class mirrors S3's).
3
+ *
4
+ * SDK-free + side-effect-free — they only read structural shapes off the thrown value (`err.code`,
5
+ * `err.response.status`, `err.name`), so the GCS-specific translation is unit-testable without a live GCS or
6
+ * an emulator, and without importing `@google-cloud/storage`. GCS's `ApiError` carries the HTTP status on
7
+ * `.code` (a number); dropped/timed-out sockets surface as a Node error with a string `.code` (`ECONNRESET`,
8
+ * `ETIMEDOUT`, …).
9
+ */
10
+ /**
11
+ * A conditional `ifGenerationMatch: 0` write lost the write-once race — the object already existed, so GCS
12
+ * returns **412 Precondition Failed**. Maps to `WriteConflictError` (caller OCC), never a blind retry.
13
+ */
14
+ export declare function isPreconditionFailed(err: unknown): boolean;
15
+ /** The object / generation does not exist (GCS returns 404). */
16
+ export declare function isNotFound(err: unknown): boolean;
17
+ /** A range request started past EOF (HTTP 416 Requested Range Not Satisfiable). */
18
+ export declare function isInvalidRange(err: unknown): boolean;
19
+ /**
20
+ * A transient GCS fault that is safe to retry: throttling (429), any 5xx, or a dropped/timed-out socket.
21
+ * Excludes the deterministic, caller-meaningful outcomes (412/404/416) — those must never be reclassified as
22
+ * a blind transient (a retried doomed conditional write would just fail again, and mask an OCC conflict).
23
+ */
24
+ export declare function isTransient(err: unknown): boolean;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * `@cloudbitmaps/gcs` — Google Cloud Storage.
3
+ *
4
+ * One package per storage SERVICE, with `@google-cloud/storage` as a real dependency. Install it alongside a
5
+ * flavor package and wire the backend in one line:
6
+ *
7
+ * ```ts
8
+ * import { CloudRoaring } from '@cloudbitmaps/roaring';
9
+ * import { GcsStorage } from '@cloudbitmaps/gcs';
10
+ *
11
+ * const store = new CloudRoaring({ storage: new GcsStorage({ bucket: 'bitmaps', prefix: 'cr' }) });
12
+ * ```
13
+ *
14
+ * These drivers move opaque payload bytes, so they are codec-agnostic: the same package serves every
15
+ * flavor. That is why they are a package rather than a subpath of one — as a subpath, each flavor needed a
16
+ * re-export barrel per service, and the count multiplied with every new codec.
17
+ *
18
+ * The engine, the `.crbm` format and the ports live in `@cloudbitmaps/core`, which is a real dependency of
19
+ * THIS package: it lands in your tree without you installing it, and you never name it yourself. What this
20
+ * package builds against is `@cloudbitmaps/core/driver-kit`, the declared contract for a driver — the same
21
+ * surface a third-party driver would use.
22
+ */
23
+ export { GcsStorageDriver } from './storage.js';
24
+ export type { GcsStorageDriverOptions } from './storage.js';
25
+ export { GcsRegistryDriver } from './registry.js';
26
+ export type { GcsRegistryDriverOptions } from './registry.js';
27
+ export { GcsStorage } from './backend.js';
28
+ export type { GcsStorageOptions } from './backend.js';
package/dist/index.js ADDED
@@ -0,0 +1,430 @@
1
+ // src/storage.ts
2
+ import {
3
+ NotFoundError,
4
+ TransientError,
5
+ ValidationError as ValidationError2,
6
+ WriteConflictError,
7
+ isNotFoundError,
8
+ isValidationError,
9
+ isWriteConflictError
10
+ } from "@cloudbitmaps/core/driver-kit";
11
+ import { createHash } from "node:crypto";
12
+ import { once } from "node:events";
13
+
14
+ // src/keys.ts
15
+ import {
16
+ ValidationError,
17
+ encodeNameForKey,
18
+ namespaceKeyPart,
19
+ prefixPart,
20
+ validateSegmentRef
21
+ } from "@cloudbitmaps/core/driver-kit";
22
+ var SUFFIX = ".crbm";
23
+ function normalizeGcsPrefix(prefix) {
24
+ if (prefix === void 0) return void 0;
25
+ for (const ch of prefix) {
26
+ if (ch.charCodeAt(0) < 32) {
27
+ throw new ValidationError("prefix must not contain control characters");
28
+ }
29
+ }
30
+ for (const segment of prefix.split("/")) {
31
+ if (segment === "." || segment === "..") {
32
+ throw new ValidationError(
33
+ `prefix must not contain "." or ".." path segments: ${JSON.stringify(prefix)}`
34
+ );
35
+ }
36
+ }
37
+ return prefix;
38
+ }
39
+ function segmentObjectPrefix(prefix, ref) {
40
+ validateSegmentRef(ref);
41
+ return `${prefixPart(prefix)}${namespaceKeyPart(ref.namespace)}/segments/${encodeNameForKey(ref.segment)}.`;
42
+ }
43
+ function storageObjectName(prefix, key) {
44
+ if (!Number.isInteger(key.generation) || key.generation < 0) {
45
+ throw new ValidationError(`generation must be a non-negative integer; got ${key.generation}`);
46
+ }
47
+ return `${segmentObjectPrefix(prefix, key)}${key.generation}${SUFFIX}`;
48
+ }
49
+ function parseGenerationFromName(segmentPrefix, objectName) {
50
+ if (!objectName.startsWith(segmentPrefix) || !objectName.endsWith(SUFFIX)) return null;
51
+ const middle = objectName.slice(segmentPrefix.length, objectName.length - SUFFIX.length);
52
+ if (!/^(0|[1-9]\d*)$/.test(middle)) return null;
53
+ const generation = Number(middle);
54
+ return Number.isSafeInteger(generation) ? generation : null;
55
+ }
56
+
57
+ // src/gcs-errors.ts
58
+ function httpStatus(err) {
59
+ const e = err;
60
+ if (typeof e?.code === "number") return e.code;
61
+ if (typeof e?.response?.status === "number") return e.response.status;
62
+ return void 0;
63
+ }
64
+ function networkCode(err) {
65
+ const code = err?.code;
66
+ return typeof code === "string" ? code : void 0;
67
+ }
68
+ function isPreconditionFailed(err) {
69
+ return httpStatus(err) === 412;
70
+ }
71
+ function isNotFound(err) {
72
+ return httpStatus(err) === 404;
73
+ }
74
+ function isInvalidRange(err) {
75
+ return httpStatus(err) === 416;
76
+ }
77
+ function isTransient(err) {
78
+ if (isPreconditionFailed(err) || isNotFound(err) || isInvalidRange(err)) return false;
79
+ const status = httpStatus(err);
80
+ if (status === 429 || status !== void 0 && status >= 500 && status < 600) return true;
81
+ const net = networkCode(err);
82
+ return net === "ECONNRESET" || net === "ETIMEDOUT" || net === "ECONNREFUSED" || net === "EPIPE" || net === "EAI_AGAIN" || net === "ENOTFOUND";
83
+ }
84
+
85
+ // src/storage.ts
86
+ var DEFAULT_MAX_OBJECT_BYTES = 5 * 1024 * 1024 * 1024 * 1024;
87
+ var DEFAULT_UPLOAD_THRESHOLD_BYTES = 8 * 1024 * 1024;
88
+ var GcsStorageDriver = class {
89
+ storage;
90
+ bucket;
91
+ prefix;
92
+ maxObjectBytes;
93
+ threshold;
94
+ constructor(options) {
95
+ this.storage = options.storage;
96
+ this.bucket = options.bucket;
97
+ this.prefix = normalizeGcsPrefix(options.prefix);
98
+ this.maxObjectBytes = options.maxObjectBytes ?? DEFAULT_MAX_OBJECT_BYTES;
99
+ this.threshold = options.simpleUploadThresholdBytes ?? DEFAULT_UPLOAD_THRESHOLD_BYTES;
100
+ }
101
+ capabilities() {
102
+ return { rangeRead: true, maxObjectBytes: this.maxObjectBytes, conditionalPut: true };
103
+ }
104
+ file(name) {
105
+ return this.storage.bucket(this.bucket).file(name);
106
+ }
107
+ async putImmutable(key, write) {
108
+ const objectName = storageObjectName(this.prefix, key);
109
+ const sink = new GcsUploadSink(this.file(objectName), this.maxObjectBytes, this.threshold);
110
+ try {
111
+ await write(sink);
112
+ return await sink.finish();
113
+ } catch (err) {
114
+ await sink.abort();
115
+ if (isPreconditionFailed(err)) {
116
+ throw new WriteConflictError(
117
+ `generation already exists (write-once): ${key.segment}.${key.generation}`
118
+ );
119
+ }
120
+ if (isValidationError(err) || isWriteConflictError(err) || isNotFoundError(err)) throw err;
121
+ throw this.mapError(err);
122
+ }
123
+ }
124
+ async getRange(key, offset, length) {
125
+ if (!Number.isInteger(offset) || !Number.isInteger(length) || offset < 0 || length < 0) {
126
+ throw new ValidationError2(`invalid range offset=${offset} length=${length}`);
127
+ }
128
+ if (length === 0) return new Uint8Array(0);
129
+ const objectName = storageObjectName(this.prefix, key);
130
+ try {
131
+ const [buf] = await this.file(objectName).download({
132
+ start: offset,
133
+ end: offset + length - 1
134
+ });
135
+ if (buf.length !== length) {
136
+ throw new ValidationError2(
137
+ `range [${offset}, ${offset + length}) out of bounds (got ${buf.length}B)`
138
+ );
139
+ }
140
+ return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
141
+ } catch (err) {
142
+ throw this.mapReadError(err, key);
143
+ }
144
+ }
145
+ async getTail(key, maxBytes) {
146
+ const objectName = storageObjectName(this.prefix, key);
147
+ try {
148
+ const [meta] = await this.file(objectName).getMetadata();
149
+ const size = Number(meta.size ?? 0);
150
+ if (!Number.isSafeInteger(size) || size < 0) {
151
+ throw new ValidationError2(`GCS returned an invalid object size: ${String(meta.size)}`);
152
+ }
153
+ if (maxBytes <= 0 || size === 0) return { bytes: new Uint8Array(0), size };
154
+ const start = Math.max(0, size - maxBytes);
155
+ const [buf] = await this.file(objectName).download({ start, end: size - 1 });
156
+ return { bytes: new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength), size };
157
+ } catch (err) {
158
+ throw this.mapReadError(err, key);
159
+ }
160
+ }
161
+ async delete(key) {
162
+ try {
163
+ await this.file(storageObjectName(this.prefix, key)).delete({ ignoreNotFound: true });
164
+ } catch (err) {
165
+ throw this.mapError(err);
166
+ }
167
+ }
168
+ async *list(ref) {
169
+ const prefix = segmentObjectPrefix(this.prefix, ref);
170
+ let files;
171
+ try {
172
+ [files] = await this.storage.bucket(this.bucket).getFiles({ prefix });
173
+ } catch (err) {
174
+ throw this.mapError(err);
175
+ }
176
+ for (const f of files) {
177
+ const generation = parseGenerationFromName(prefix, f.name);
178
+ if (generation !== null) {
179
+ yield { namespace: ref.namespace, segment: ref.segment, generation };
180
+ }
181
+ }
182
+ }
183
+ /** Map GCS read errors to the driver vocabulary; pass everything else through {@link mapError}. */
184
+ mapReadError(err, key) {
185
+ if (isValidationError(err)) return err;
186
+ if (isNotFound(err))
187
+ return new NotFoundError(`no such generation: ${key.segment}.${key.generation}`);
188
+ if (isInvalidRange(err)) {
189
+ return new ValidationError2(`range out of bounds for ${key.segment}.${key.generation}`);
190
+ }
191
+ return this.mapError(err);
192
+ }
193
+ /**
194
+ * Reclassify a transient GCS fault (throttle/5xx/dropped connection) as a retryable {@link TransientError}
195
+ * so the retry decorator can ride it out; everything else propagates unchanged. The final fallback at every
196
+ * client-call site, so callers + the decorator only ever see typed errors.
197
+ */
198
+ mapError(err) {
199
+ if (isTransient(err)) {
200
+ return new TransientError(
201
+ `transient GCS fault: ${err?.code ?? "unknown"}`,
202
+ { cause: err }
203
+ );
204
+ }
205
+ return err;
206
+ }
207
+ };
208
+ function concatBytes(parts, total) {
209
+ const out = new Uint8Array(total);
210
+ let offset = 0;
211
+ for (const p of parts) {
212
+ out.set(p, offset);
213
+ offset += p.length;
214
+ }
215
+ return out;
216
+ }
217
+ var GcsUploadSink = class _GcsUploadSink {
218
+ constructor(file, maxObjectBytes, threshold) {
219
+ this.file = file;
220
+ this.maxObjectBytes = maxObjectBytes;
221
+ this.threshold = threshold;
222
+ }
223
+ file;
224
+ maxObjectBytes;
225
+ threshold;
226
+ hash = createHash("sha256");
227
+ total = 0;
228
+ buffered = [];
229
+ bufferedLen = 0;
230
+ stream;
231
+ // set once we cross the threshold → resumable mode
232
+ done;
233
+ failure;
234
+ static WRITE_OPTS = {
235
+ preconditionOpts: { ifGenerationMatch: 0 },
236
+ // write-once: create only if absent
237
+ metadata: { contentType: "application/octet-stream" }
238
+ };
239
+ async write(bytes) {
240
+ if (this.failure !== void 0) throw this.failure;
241
+ if (bytes.length === 0) return;
242
+ this.total += bytes.length;
243
+ if (this.total > this.maxObjectBytes) {
244
+ throw new ValidationError2(`object exceeds maxObjectBytes ${this.maxObjectBytes}`);
245
+ }
246
+ this.hash.update(bytes);
247
+ if (this.stream !== void 0) {
248
+ if (!this.stream.write(bytes)) await once(this.stream, "drain");
249
+ return;
250
+ }
251
+ this.buffered.push(bytes);
252
+ this.bufferedLen += bytes.length;
253
+ if (this.bufferedLen > this.threshold) this.startResumable();
254
+ }
255
+ /** Cross into resumable streaming: open the stream, flush the buffered bytes, keep only ~one threshold resident. */
256
+ startResumable() {
257
+ const stream = this.file.createWriteStream({ resumable: true, ..._GcsUploadSink.WRITE_OPTS });
258
+ this.stream = stream;
259
+ this.done = once(stream, "finish");
260
+ stream.on("error", (e) => {
261
+ this.failure ??= e;
262
+ });
263
+ this.done.catch(() => void 0);
264
+ const buf = concatBytes(this.buffered, this.bufferedLen);
265
+ this.buffered.length = 0;
266
+ this.bufferedLen = 0;
267
+ stream.write(buf);
268
+ }
269
+ async finish() {
270
+ if (this.failure !== void 0) throw this.failure;
271
+ const sha256 = this.hash.digest("hex");
272
+ if (this.stream === void 0) {
273
+ await this.file.save(concatBytes(this.buffered, this.bufferedLen), {
274
+ resumable: false,
275
+ ..._GcsUploadSink.WRITE_OPTS
276
+ });
277
+ return { size: this.total, sha256 };
278
+ }
279
+ this.stream.end();
280
+ await this.done;
281
+ return { size: this.total, sha256 };
282
+ }
283
+ async abort() {
284
+ if (this.stream !== void 0 && !this.stream.destroyed) this.stream.destroy();
285
+ }
286
+ };
287
+
288
+ // src/registry.ts
289
+ import {
290
+ IntegrityError,
291
+ MAX_ROW_BYTES,
292
+ ObjectStoreRegistry,
293
+ ObjectVersionRaced,
294
+ TransientError as TransientError2,
295
+ WriteConflictError as WriteConflictError2,
296
+ normalizeObjectPrefix
297
+ } from "@cloudbitmaps/core/driver-kit";
298
+ var GcsStore = class {
299
+ constructor(storage, bucket) {
300
+ this.storage = storage;
301
+ this.bucket = bucket;
302
+ }
303
+ storage;
304
+ bucket;
305
+ label = "GCS";
306
+ /** A file handle, optionally pinned to one object generation (GCS's version fence). */
307
+ file(name, generation) {
308
+ return generation === void 0 ? this.storage.bucket(this.bucket).file(name) : this.storage.bucket(this.bucket).file(name, { generation });
309
+ }
310
+ async read(key) {
311
+ let generation;
312
+ let size;
313
+ try {
314
+ const [meta] = await this.file(key).getMetadata();
315
+ generation = String(meta.generation ?? "");
316
+ size = Number(meta.size ?? 0);
317
+ } catch (err) {
318
+ if (isNotFound(err)) return null;
319
+ throw mapError(err);
320
+ }
321
+ if (size > MAX_ROW_BYTES) {
322
+ throw new IntegrityError(`registry object ${size}B exceeds cap ${MAX_ROW_BYTES}B`);
323
+ }
324
+ try {
325
+ const [buf] = await this.file(key, generation).download();
326
+ return { bytes: new Uint8Array(buf), version: generation };
327
+ } catch (err) {
328
+ if (isNotFound(err)) throw new ObjectVersionRaced(key);
329
+ throw mapError(err);
330
+ }
331
+ }
332
+ async write(key, body, expect) {
333
+ try {
334
+ await this.file(key).save(Buffer.from(body), {
335
+ contentType: "application/json",
336
+ // `resumable: false` is load-bearing, not a tuning knob. `save()` otherwise opens a resumable
337
+ // session, and a registry row is a few hundred bytes — one round trip's worth of data carried over
338
+ // two. It also costs the fence: fake-gcs-server does not enforce `ifGenerationMatch` on the
339
+ // resumable path, so the whole integration lane would pass over a registry with no
340
+ // compare-and-swap at all. `GcsStorageDriver` pins the same flag for the same reason.
341
+ resumable: false,
342
+ preconditionOpts: {
343
+ ifGenerationMatch: expect === "absent" ? 0 : generationFence(expect.version, key)
344
+ }
345
+ });
346
+ } catch (err) {
347
+ if (isPreconditionFailed(err)) {
348
+ throw new WriteConflictError2(`registry OCC conflict for ${key}`);
349
+ }
350
+ throw mapError(err);
351
+ }
352
+ }
353
+ async *listKeys(prefix) {
354
+ let pageToken;
355
+ do {
356
+ let files;
357
+ let next;
358
+ try {
359
+ [files, next] = await this.storage.bucket(this.bucket).getFiles({ prefix, autoPaginate: false, maxResults: 1e3, pageToken });
360
+ } catch (err) {
361
+ throw mapError(err);
362
+ }
363
+ for (const f of files ?? []) yield f.name;
364
+ pageToken = next?.pageToken;
365
+ } while (pageToken !== void 0);
366
+ }
367
+ };
368
+ function generationFence(version, key) {
369
+ const generation = Number(version);
370
+ if (!Number.isSafeInteger(generation) || generation <= 0) {
371
+ throw new IntegrityError(`registry version fence is not a GCS generation: ${key}`);
372
+ }
373
+ return generation;
374
+ }
375
+ function mapError(err) {
376
+ if (isTransient(err)) {
377
+ return new TransientError2(
378
+ `transient GCS fault: ${err?.name ?? "unknown"}`,
379
+ { cause: err }
380
+ );
381
+ }
382
+ return err;
383
+ }
384
+ var GcsRegistryDriver = class extends ObjectStoreRegistry {
385
+ constructor(options) {
386
+ super(
387
+ new GcsStore(options.storage, options.bucket),
388
+ normalizeObjectPrefix(options.prefix),
389
+ options.now ?? (() => Date.now())
390
+ );
391
+ }
392
+ };
393
+
394
+ // src/backend.ts
395
+ import { ValidationError as ValidationError3, brandAsBackend } from "@cloudbitmaps/core/driver-kit";
396
+ import { Storage as GcsClient } from "@google-cloud/storage";
397
+ var GcsStorage = class {
398
+ storage;
399
+ registry;
400
+ /** The client both halves share — built here unless one was supplied. */
401
+ client;
402
+ constructor(options) {
403
+ if (options.storage !== void 0) {
404
+ throw new ValidationError3(
405
+ "GcsStorage takes the @google-cloud/storage client as `client`, not `storage` (which was the old GcsStorageDriver option). Rename it, or omit it and let the backend build one."
406
+ );
407
+ }
408
+ this.client = options.client ?? new GcsClient({
409
+ ...options.projectId === void 0 ? {} : { projectId: options.projectId },
410
+ ...options.apiEndpoint === void 0 ? {} : { apiEndpoint: options.apiEndpoint }
411
+ });
412
+ const shared = {
413
+ storage: this.client,
414
+ bucket: options.bucket,
415
+ ...options.prefix === void 0 ? {} : { prefix: options.prefix }
416
+ };
417
+ this.storage = new GcsStorageDriver(shared);
418
+ this.registry = new GcsRegistryDriver({
419
+ ...shared,
420
+ ...options.now === void 0 ? {} : { now: options.now }
421
+ });
422
+ brandAsBackend(this);
423
+ }
424
+ };
425
+ export {
426
+ GcsRegistryDriver,
427
+ GcsStorage,
428
+ GcsStorageDriver
429
+ };
430
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/storage.ts", "../src/keys.ts", "../src/gcs-errors.ts", "../src/registry.ts", "../src/backend.ts"],
4
+ "sourcesContent": ["/**\n * `GcsStorageDriver` \u2014 an {@link IStorageDriver} over Google Cloud Storage.\n *\n * Uses the official `@google-cloud/storage`, a real dependency of this package \u2014 installing\n * `@cloudbitmaps/gcs` is what installs it. The `Storage` client is **injected** (dependency injection): the driver owns\n * no credential/project/endpoint logic, so it's thin, reuses the caller's client, and is testable against the\n * `fake-gcs-server` emulator (point a `Storage` at its `apiEndpoint`).\n *\n * Generations are write-once immutable objects: a resumable upload with the **`ifGenerationMatch: 0`**\n * precondition (\"create only if it does not exist\") makes the publish atomic \u2014 a second write to the same\n * object fails with 412 \u2192 {@link WriteConflictError}, never a silent overwrite (hard invariant 2: storage\n * objects are immutable and never overwritten in place), the GCS analogue of S3's `If-None-Match: *` and\n * LocalFs's atomic `link`. **Writes stream in constant memory:** the codec's bytes are\n * piped into a GCS resumable-upload `Writable` (chunked/freed by the SDK as they go), so a load's write\n * footprint stays bounded regardless of segment size, up to the advertised `maxObjectBytes`. Drivers may use\n * `node:crypto`; only `core/` is bound by the determinism lint.\n */\nimport {\n NotFoundError,\n TransientError,\n ValidationError,\n WriteConflictError,\n isNotFoundError,\n isValidationError,\n isWriteConflictError,\n} from '@cloudbitmaps/core/driver-kit';\nimport type {\n BlobSink,\n GenKey,\n IStorageDriver,\n SegmentRef,\n StorageCaps,\n} from '@cloudbitmaps/core/driver-kit';\nimport { createHash, type Hash } from 'node:crypto';\nimport type { Writable } from 'node:stream';\nimport { once } from 'node:events';\nimport type { Storage } from '@google-cloud/storage';\nimport {\n storageObjectName,\n normalizeGcsPrefix,\n parseGenerationFromName,\n segmentObjectPrefix,\n} from './keys';\nimport { isInvalidRange, isNotFound, isPreconditionFailed, isTransient } from './gcs-errors';\n\n/** Default object ceiling: GCS's 5 TiB per-object hard max. Set lower to fail fast on a runaway write. */\nconst DEFAULT_MAX_OBJECT_BYTES = 5 * 1024 * 1024 * 1024 * 1024;\n\n/**\n * Objects at/under this size are uploaded in a **single simple (non-resumable) request**; larger ones switch to\n * a **resumable stream** (constant memory). Mirrors S3's single-PUT-vs-multipart split \u2014 the simple path buffers\n * only \u2264 this many bytes, the resumable path streams. Default 8 MiB (\u2248 S3's part size), so peak write memory is\n * ~one threshold's worth regardless of object size.\n */\nconst DEFAULT_UPLOAD_THRESHOLD_BYTES = 8 * 1024 * 1024;\n\nexport interface GcsStorageDriverOptions {\n /** A constructed `@google-cloud/storage` `Storage` client (point `apiEndpoint` at fake-gcs-server locally). */\n readonly storage: Storage;\n /** Target bucket (must already exist). */\n readonly bucket: string;\n /** Optional object-name prefix under which all objects live (e.g. `cloudroaring/`). */\n readonly prefix?: string;\n /** Largest object this driver will write/advertise (default = GCS's 5 TiB max). */\n readonly maxObjectBytes?: number;\n /** Bytes at/under which a single non-resumable upload is used instead of a resumable stream (default 8 MiB). */\n readonly simpleUploadThresholdBytes?: number;\n}\n\nexport class GcsStorageDriver implements IStorageDriver {\n private readonly storage: Storage;\n private readonly bucket: string;\n private readonly prefix: string | undefined;\n private readonly maxObjectBytes: number;\n private readonly threshold: number;\n\n constructor(options: GcsStorageDriverOptions) {\n this.storage = options.storage;\n this.bucket = options.bucket;\n this.prefix = normalizeGcsPrefix(options.prefix);\n this.maxObjectBytes = options.maxObjectBytes ?? DEFAULT_MAX_OBJECT_BYTES;\n this.threshold = options.simpleUploadThresholdBytes ?? DEFAULT_UPLOAD_THRESHOLD_BYTES;\n }\n\n capabilities(): StorageCaps {\n return { rangeRead: true, maxObjectBytes: this.maxObjectBytes, conditionalPut: true };\n }\n\n private file(name: string) {\n return this.storage.bucket(this.bucket).file(name);\n }\n\n async putImmutable(\n key: GenKey,\n write: (sink: BlobSink) => Promise<void>,\n ): Promise<{ size: number; sha256: string }> {\n const objectName = storageObjectName(this.prefix, key); // validates ref + generation\n const sink = new GcsUploadSink(this.file(objectName), this.maxObjectBytes, this.threshold);\n try {\n await write(sink);\n return await sink.finish();\n } catch (err) {\n await sink.abort(); // best-effort teardown of the in-flight upload\n // A lost write-once race \u2014 the object already existed, so `ifGenerationMatch: 0` failed with 412.\n if (isPreconditionFailed(err)) {\n throw new WriteConflictError(\n `generation already exists (write-once): ${key.segment}.${key.generation}`,\n );\n }\n if (isValidationError(err) || isWriteConflictError(err) || isNotFoundError(err)) throw err;\n throw this.mapError(err);\n }\n }\n\n async getRange(key: GenKey, offset: number, length: number): Promise<Uint8Array> {\n if (!Number.isInteger(offset) || !Number.isInteger(length) || offset < 0 || length < 0) {\n throw new ValidationError(`invalid range offset=${offset} length=${length}`);\n }\n if (length === 0) return new Uint8Array(0);\n const objectName = storageObjectName(this.prefix, key);\n try {\n // GCS `end` is inclusive.\n const [buf] = await this.file(objectName).download({\n start: offset,\n end: offset + length - 1,\n });\n // A short read means the range ran past EOF \u2014 treat as out-of-bounds, never a partial result.\n if (buf.length !== length) {\n throw new ValidationError(\n `range [${offset}, ${offset + length}) out of bounds (got ${buf.length}B)`,\n );\n }\n return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);\n } catch (err) {\n throw this.mapReadError(err, key);\n }\n }\n\n async getTail(key: GenKey, maxBytes: number): Promise<{ bytes: Uint8Array; size: number }> {\n const objectName = storageObjectName(this.prefix, key);\n try {\n // Two round-trips (metadata for the size, then a ranged download) vs S3's one (suffix-range +\n // Content-Range). This is on the per-*generation* open path, which the reader caches \u2014 NOT the per-op cache\n // path (add/has/count/intersect) \u2014 so it's amortized; collapsing to one RT is a tracked follow-up.\n const [meta] = await this.file(objectName).getMetadata();\n const size = Number(meta.size ?? 0);\n if (!Number.isSafeInteger(size) || size < 0) {\n throw new ValidationError(`GCS returned an invalid object size: ${String(meta.size)}`);\n }\n if (maxBytes <= 0 || size === 0) return { bytes: new Uint8Array(0), size };\n const start = Math.max(0, size - maxBytes);\n const [buf] = await this.file(objectName).download({ start, end: size - 1 });\n return { bytes: new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength), size };\n } catch (err) {\n throw this.mapReadError(err, key);\n }\n }\n\n async delete(key: GenKey): Promise<void> {\n // Idempotent: `ignoreNotFound` so a racing/retried GC sweep of an absent object is a no-op.\n try {\n await this.file(storageObjectName(this.prefix, key)).delete({ ignoreNotFound: true });\n } catch (err) {\n throw this.mapError(err);\n }\n }\n\n async *list(ref: SegmentRef): AsyncIterable<GenKey> {\n const prefix = segmentObjectPrefix(this.prefix, ref); // validates ref\n let files;\n try {\n // autoPaginate (default) drains every page; a segment has few generations, so the set is small.\n [files] = await this.storage.bucket(this.bucket).getFiles({ prefix });\n } catch (err) {\n throw this.mapError(err);\n }\n for (const f of files) {\n const generation = parseGenerationFromName(prefix, f.name);\n if (generation !== null) {\n yield { namespace: ref.namespace, segment: ref.segment, generation };\n }\n }\n }\n\n /** Map GCS read errors to the driver vocabulary; pass everything else through {@link mapError}. */\n private mapReadError(err: unknown, key: GenKey): unknown {\n if (isValidationError(err)) return err;\n if (isNotFound(err))\n return new NotFoundError(`no such generation: ${key.segment}.${key.generation}`);\n // A fully out-of-range request (start past EOF) \u2014 the BlobReader contract treats range errors as\n // ValidationError, never a short/empty read.\n if (isInvalidRange(err)) {\n return new ValidationError(`range out of bounds for ${key.segment}.${key.generation}`);\n }\n return this.mapError(err);\n }\n\n /**\n * Reclassify a transient GCS fault (throttle/5xx/dropped connection) as a retryable {@link TransientError}\n * so the retry decorator can ride it out; everything else propagates unchanged. The final fallback at every\n * client-call site, so callers + the decorator only ever see typed errors.\n */\n private mapError(err: unknown): unknown {\n if (isTransient(err)) {\n return new TransientError(\n `transient GCS fault: ${(err as { code?: unknown } | null)?.code ?? 'unknown'}`,\n { cause: err },\n );\n }\n return err;\n }\n}\n\ntype GcsFile = ReturnType<ReturnType<Storage['bucket']>['file']>;\n\n/** Concatenate byte chunks of known total length into one buffer. */\nfunction concatBytes(parts: readonly Uint8Array[], total: number): Uint8Array {\n const out = new Uint8Array(total);\n let offset = 0;\n for (const p of parts) {\n out.set(p, offset);\n offset += p.length;\n }\n return out;\n}\n\n/**\n * {@link BlobSink} that uploads one GCS object write-once. It **buffers up to a threshold**: an object that\n * finishes at/under it is committed in a **single simple (non-resumable) request** (`file.save`) \u2014 the write-once\n * path both real GCS and `fake-gcs-server` enforce; a larger one switches to a **resumable stream**, flushing the\n * buffer then piping the rest in constant memory (peak \u2248 one threshold). SHA-256 is hashed incrementally. Both\n * paths carry `ifGenerationMatch: 0` (create-only-if-absent) \u2192 a conflict is a 412, mapped by the driver to\n * {@link WriteConflictError}. On error the caller invokes {@link abort}.\n */\nclass GcsUploadSink implements BlobSink {\n private readonly hash: Hash = createHash('sha256');\n private total = 0;\n private readonly buffered: Uint8Array[] = [];\n private bufferedLen = 0;\n private stream: Writable | undefined; // set once we cross the threshold \u2192 resumable mode\n private done: Promise<unknown[]> | undefined;\n private failure: unknown;\n\n constructor(\n private readonly file: GcsFile,\n private readonly maxObjectBytes: number,\n private readonly threshold: number,\n ) {}\n\n private static readonly WRITE_OPTS = {\n preconditionOpts: { ifGenerationMatch: 0 as const }, // write-once: create only if absent\n metadata: { contentType: 'application/octet-stream' },\n };\n\n async write(bytes: Uint8Array): Promise<void> {\n if (this.failure !== undefined) throw this.failure;\n if (bytes.length === 0) return;\n this.total += bytes.length;\n if (this.total > this.maxObjectBytes) {\n throw new ValidationError(`object exceeds maxObjectBytes ${this.maxObjectBytes}`);\n }\n this.hash.update(bytes);\n if (this.stream !== undefined) {\n // Resumable mode: honor backpressure. `events.once(stream,'drain')` REJECTS if the stream emits 'error'\n // while we wait (that's its documented behavior for any awaited event except 'error'), and auto-removes\n // both listeners on settle \u2014 so there's no listener accumulation across drain cycles.\n if (!this.stream.write(bytes)) await once(this.stream, 'drain');\n return;\n }\n this.buffered.push(bytes);\n this.bufferedLen += bytes.length;\n if (this.bufferedLen > this.threshold) this.startResumable();\n }\n\n /** Cross into resumable streaming: open the stream, flush the buffered bytes, keep only ~one threshold resident. */\n private startResumable(): void {\n const stream = this.file.createWriteStream({ resumable: true, ...GcsUploadSink.WRITE_OPTS });\n this.stream = stream;\n this.done = once(stream, 'finish'); // resolves on a clean commit; rejects on 'error' (e.g. 412)\n stream.on('error', (e: unknown) => {\n this.failure ??= e; // a persistent listener so a stray 'error' is never an unhandled 'error' event\n });\n this.done.catch(() => undefined); // finish() awaits this; guard against unhandled rejection until then\n const buf = concatBytes(this.buffered, this.bufferedLen);\n this.buffered.length = 0;\n this.bufferedLen = 0;\n stream.write(buf); // \u2264 threshold+one-chunk; backpressure is absorbed by the next write()'s drain-await / end()\n }\n\n async finish(): Promise<{ size: number; sha256: string }> {\n if (this.failure !== undefined) throw this.failure;\n const sha256 = this.hash.digest('hex');\n if (this.stream === undefined) {\n // Small object: a single simple (non-resumable) upload \u2014 write-once enforced everywhere (incl. the emulator).\n await this.file.save(concatBytes(this.buffered, this.bufferedLen), {\n resumable: false,\n ...GcsUploadSink.WRITE_OPTS,\n });\n return { size: this.total, sha256 };\n }\n this.stream.end();\n await this.done; // rejects if the commit fails (e.g. 412 write-once conflict)\n return { size: this.total, sha256 };\n }\n\n async abort(): Promise<void> {\n if (this.stream !== undefined && !this.stream.destroyed) this.stream.destroy();\n }\n}\n", "/**\n * Logical-ref \u2192 GCS object-name mapping for {@link GcsStorageDriver}.\n *\n * Pure string logic, no SDK dependency \u2014 unit-testable without GCS or an emulator. Uses the **same\n * backend-agnostic `.crbm` object-name scheme** as the S3 + LocalFs storage drivers\n * (`<prefix><ns>/segments/<segment>.<gen>.crbm`), so a segment reads identically whichever storage backend holds\n * it. The default (absent) namespace maps to `_default`, which cannot collide with a real namespace because a\n * caller's `_default` encodes to `%5Fdefault` while the sentinel is emitted literally.\n *\n * The storage-key layout is shared, not copied: `prefixPart` and the name encoders come from\n * `@cloudbitmaps/core/driver-kit`, so a segment written by one driver reads identically under another. Two\n * drivers disagreeing about how a name becomes a key would be a silent cross-driver incompatibility on the\n * same bucket, which is exactly the kind of thing a local copy drifts into.\n */\n\nimport {\n ValidationError,\n encodeNameForKey,\n namespaceKeyPart,\n prefixPart,\n validateSegmentRef,\n} from '@cloudbitmaps/core/driver-kit';\nimport type { GenKey, SegmentRef } from '@cloudbitmaps/core/driver-kit';\n\nconst SUFFIX = '.crbm';\n\n/**\n * Validate the caller-supplied key prefix (trusted config, but a real containment boundary): reject control\n * characters and `.`/`..` path segments so a prefix can't traverse out of its intended space. Returns it\n * unchanged (the key builders normalize slashes).\n */\nexport function normalizeGcsPrefix(prefix: string | undefined): string | undefined {\n if (prefix === undefined) return undefined;\n for (const ch of prefix) {\n if (ch.charCodeAt(0) < 0x20) {\n throw new ValidationError('prefix must not contain control characters');\n }\n }\n for (const segment of prefix.split('/')) {\n if (segment === '.' || segment === '..') {\n throw new ValidationError(\n `prefix must not contain \".\" or \"..\" path segments: ${JSON.stringify(prefix)}`,\n );\n }\n }\n return prefix;\n}\n\n/**\n * The GCS object-name prefix shared by all of a segment's generations: `<prefix><ns>/segments/<segment>.`.\n * Used both as the `getFiles` prefix and as the string stripped by {@link parseGenerationFromName}.\n */\nexport function segmentObjectPrefix(prefix: string | undefined, ref: SegmentRef): string {\n validateSegmentRef(ref);\n return `${prefixPart(prefix)}${namespaceKeyPart(ref.namespace)}/segments/${encodeNameForKey(ref.segment)}.`;\n}\n\n/** The full GCS object name of one `.crbm` generation: `<segmentPrefix><gen>.crbm`. */\nexport function storageObjectName(prefix: string | undefined, key: GenKey): string {\n if (!Number.isInteger(key.generation) || key.generation < 0) {\n throw new ValidationError(`generation must be a non-negative integer; got ${key.generation}`);\n }\n return `${segmentObjectPrefix(prefix, key)}${key.generation}${SUFFIX}`;\n}\n\n/**\n * Parse a generation number out of a full object name, given its segment prefix, or `null` if it doesn't\n * match. Canonical decimal only \u2014 no leading zeros (so `\u2026s.07.crbm` can't alias `\u2026s.7.crbm`) and within\n * safe-integer range. Also rejects a *different* segment whose name merely shares the prefix (its middle\n * isn't all digits).\n */\nexport function parseGenerationFromName(segmentPrefix: string, objectName: string): number | null {\n if (!objectName.startsWith(segmentPrefix) || !objectName.endsWith(SUFFIX)) return null;\n const middle = objectName.slice(segmentPrefix.length, objectName.length - SUFFIX.length);\n if (!/^(0|[1-9]\\d*)$/.test(middle)) return null;\n const generation = Number(middle);\n return Number.isSafeInteger(generation) ? generation : null;\n}\n", "/**\n * Pure helpers for classifying Google Cloud Storage SDK errors (the transient class mirrors S3's).\n *\n * SDK-free + side-effect-free \u2014 they only read structural shapes off the thrown value (`err.code`,\n * `err.response.status`, `err.name`), so the GCS-specific translation is unit-testable without a live GCS or\n * an emulator, and without importing `@google-cloud/storage`. GCS's `ApiError` carries the HTTP status on\n * `.code` (a number); dropped/timed-out sockets surface as a Node error with a string `.code` (`ECONNRESET`,\n * `ETIMEDOUT`, \u2026).\n */\n\n/** The HTTP status of a GCS `ApiError`, if present (`err.code` as a number, or `err.response.status`). */\nfunction httpStatus(err: unknown): number | undefined {\n const e = err as { code?: unknown; response?: { status?: unknown } } | null;\n if (typeof e?.code === 'number') return e.code;\n if (typeof e?.response?.status === 'number') return e.response.status;\n return undefined;\n}\n\n/** A network-level error `code` string (e.g. `ECONNRESET`, `ETIMEDOUT`, `EAI_AGAIN`, `EPIPE`). */\nfunction networkCode(err: unknown): string | undefined {\n const code = (err as { code?: unknown } | null)?.code;\n return typeof code === 'string' ? code : undefined;\n}\n\n/**\n * A conditional `ifGenerationMatch: 0` write lost the write-once race \u2014 the object already existed, so GCS\n * returns **412 Precondition Failed**. Maps to `WriteConflictError` (caller OCC), never a blind retry.\n */\nexport function isPreconditionFailed(err: unknown): boolean {\n return httpStatus(err) === 412;\n}\n\n/** The object / generation does not exist (GCS returns 404). */\nexport function isNotFound(err: unknown): boolean {\n return httpStatus(err) === 404;\n}\n\n/** A range request started past EOF (HTTP 416 Requested Range Not Satisfiable). */\nexport function isInvalidRange(err: unknown): boolean {\n return httpStatus(err) === 416;\n}\n\n/**\n * A transient GCS fault that is safe to retry: throttling (429), any 5xx, or a dropped/timed-out socket.\n * Excludes the deterministic, caller-meaningful outcomes (412/404/416) \u2014 those must never be reclassified as\n * a blind transient (a retried doomed conditional write would just fail again, and mask an OCC conflict).\n */\nexport function isTransient(err: unknown): boolean {\n if (isPreconditionFailed(err) || isNotFound(err) || isInvalidRange(err)) return false;\n const status = httpStatus(err);\n if (status === 429 || (status !== undefined && status >= 500 && status < 600)) return true;\n const net = networkCode(err);\n return (\n net === 'ECONNRESET' ||\n net === 'ETIMEDOUT' ||\n net === 'ECONNREFUSED' ||\n net === 'EPIPE' ||\n net === 'EAI_AGAIN' ||\n net === 'ENOTFOUND'\n );\n}\n", "/**\n * `GcsRegistryDriver` \u2014 an {@link IRegistryDriver} over Google Cloud Storage.\n *\n * Lets a **GCS deployment run on one bucket alone** \u2014 storage `.crbm` generations and the registry in the same\n * place, with no second cloud involved. Before this existed, a GCS user had to point the registry at a\n * separate AWS-hosted table, which meant holding an AWS account purely to store the pointer that says which\n * generation is current.\n *\n * The protocol (an ABA-safe OCC counter, tombstoning delete, the bounded retry, the key layout) lives once\n * in {@link ObjectStoreRegistry}; this file is only the three I/O calls GCS makes.\n *\n * **The atomic swap is offloaded to GCS's object preconditions.** `ifGenerationMatch: 0` is create-only\n * (\"only if it does not exist\") and `ifGenerationMatch: <generation>` is compare-and-swap \u2014 the same pair S3\n * spells `If-None-Match: *` / `If-Match: <etag>`. A lost race returns `412` \u2192 {@link WriteConflictError}.\n * **The version fence is the object's `generation`, not its ETag**: GCS mutates the generation on every\n * overwrite and it is the value `ifGenerationMatch` compares, so it is the only correct fence here. (Note\n * the unrelated collision of vocabulary \u2014 a GCS *object generation* has nothing to do with a CloudBitmaps\n * `.crbm` *generation*.) Reads are strongly consistent, satisfying the registry's `strongRead` contract.\n *\n * **Deployment requirements** (a policy that violates these silently corrupts the registry):\n * - The principal needs `storage.objects.get`, `create`, `update` and `list` on the bucket. Without `list`\n * the registry cannot enumerate, and a missing-object read may surface as `403` rather than `404`.\n * - **Do not apply an Object Lifecycle rule to the `registry/` prefix**, and do not enable a retention\n * policy that blocks overwrite. `delete` tombstones rather than removing, for ABA-safety \u2014 see\n * {@link ObjectStoreRegistry}.\n * - Object versioning is neither required nor used; the driver always reads the live generation. Because a\n * non-versioned bucket retires a superseded generation immediately, a read whose pinned generation is\n * overwritten mid-flight reports {@link ObjectVersionRaced} and is retried by {@link ObjectStoreRegistry},\n * never mistaken for an absent row.\n */\nimport {\n IntegrityError,\n MAX_ROW_BYTES,\n ObjectStoreRegistry,\n ObjectVersionRaced,\n TransientError,\n WriteConflictError,\n normalizeObjectPrefix,\n} from '@cloudbitmaps/core/driver-kit';\nimport type { ObjectRegistryStore, ObjectRow } from '@cloudbitmaps/core/driver-kit';\nimport type { Storage } from '@google-cloud/storage';\nimport { isNotFound, isPreconditionFailed, isTransient } from './gcs-errors';\n\nexport interface GcsRegistryDriverOptions {\n /** A constructed `@google-cloud/storage` `Storage` client (point `apiEndpoint` at fake-gcs-server locally). */\n readonly storage: Storage;\n /** Target bucket (must already exist). */\n readonly bucket: string;\n /** Optional object-name prefix under which all registry objects live (e.g. `cloudroaring/`). */\n readonly prefix?: string;\n /** Injected clock for `createdAt`/`updatedAt`; defaults to `Date.now`. */\n readonly now?: () => number;\n}\n\n/** The three calls {@link ObjectStoreRegistry} needs, in GCS's dialect. */\nclass GcsStore implements ObjectRegistryStore {\n readonly label = 'GCS';\n\n constructor(\n private readonly storage: Storage,\n private readonly bucket: string,\n ) {}\n\n /** A file handle, optionally pinned to one object generation (GCS's version fence). */\n private file(name: string, generation?: string) {\n return generation === undefined\n ? this.storage.bucket(this.bucket).file(name)\n : this.storage.bucket(this.bucket).file(name, { generation });\n }\n\n async read(key: string): Promise<ObjectRow | null> {\n // Metadata first: it carries the `generation` fence AND the size, so a hostile object is rejected on its\n // advertised length before any of it is buffered.\n let generation: string;\n let size: number;\n try {\n const [meta] = await this.file(key).getMetadata();\n generation = String(meta.generation ?? '');\n size = Number(meta.size ?? 0);\n } catch (err) {\n if (isNotFound(err)) return null;\n throw mapError(err);\n }\n if (size > MAX_ROW_BYTES) {\n throw new IntegrityError(`registry object ${size}B exceeds cap ${MAX_ROW_BYTES}B`);\n }\n try {\n // Pin the download to the generation we just measured, so a concurrent overwrite between the two calls\n // cannot hand us bytes that do not match the fence we are about to compare-and-swap against.\n const [buf] = await this.file(key, generation).download();\n return { bytes: new Uint8Array(buf), version: generation };\n } catch (err) {\n // A 404 on the PINNED download does not mean the object is gone \u2014 it means the generation we pinned\n // is. Buckets here run without Object Versioning (the registry neither needs nor uses it), so an\n // overwrite retires the superseded generation immediately and this is simply what losing the pin looks\n // like. Reporting it as absence would make a live row disappear from `get`, hand `delete` a false\n // success, and silently drop rows from `list`; the caller re-reads instead.\n if (isNotFound(err)) throw new ObjectVersionRaced(key);\n throw mapError(err);\n }\n }\n\n async write(\n key: string,\n body: Uint8Array,\n expect: 'absent' | { version: string },\n ): Promise<void> {\n try {\n await this.file(key).save(Buffer.from(body), {\n contentType: 'application/json',\n // `resumable: false` is load-bearing, not a tuning knob. `save()` otherwise opens a resumable\n // session, and a registry row is a few hundred bytes \u2014 one round trip's worth of data carried over\n // two. It also costs the fence: fake-gcs-server does not enforce `ifGenerationMatch` on the\n // resumable path, so the whole integration lane would pass over a registry with no\n // compare-and-swap at all. `GcsStorageDriver` pins the same flag for the same reason.\n resumable: false,\n preconditionOpts: {\n ifGenerationMatch: expect === 'absent' ? 0 : generationFence(expect.version, key),\n },\n });\n } catch (err) {\n if (isPreconditionFailed(err)) {\n throw new WriteConflictError(`registry OCC conflict for ${key}`);\n }\n throw mapError(err);\n }\n }\n\n async *listKeys(prefix: string): AsyncIterable<string> {\n let pageToken: string | undefined;\n do {\n let files;\n let next: { pageToken?: string } | null | undefined;\n try {\n // Explicit paging rather than autoPaginate: a registry can hold far more rows than a segment holds\n // generations, and draining every page into memory first is what `list()` exists to avoid.\n [files, next] = await this.storage\n .bucket(this.bucket)\n .getFiles({ prefix, autoPaginate: false, maxResults: 1000, pageToken });\n } catch (err) {\n throw mapError(err);\n }\n for (const f of files ?? []) yield f.name;\n pageToken = next?.pageToken;\n } while (pageToken !== undefined);\n }\n}\n\n/**\n * Narrow a version fence back to the number `ifGenerationMatch` takes. The fence always originates as\n * `meta.generation` in {@link GcsStore.read}, so this cannot fire in practice \u2014 but `Number()` answers `NaN`\n * for anything non-numeric, and `ifGenerationMatch: NaN` serializes to a precondition GCS ignores. That\n * failure is invisible (writes simply stop being fenced), so it is checked rather than assumed.\n */\nfunction generationFence(version: string, key: string): number {\n const generation = Number(version);\n if (!Number.isSafeInteger(generation) || generation <= 0) {\n throw new IntegrityError(`registry version fence is not a GCS generation: ${key}`);\n }\n return generation;\n}\n\n/** Reclassify a transient GCS fault as a retryable {@link TransientError}; pass everything else through. */\nfunction mapError(err: unknown): unknown {\n if (isTransient(err)) {\n return new TransientError(\n `transient GCS fault: ${(err as { name?: string } | null)?.name ?? 'unknown'}`,\n { cause: err },\n );\n }\n return err;\n}\n\nexport class GcsRegistryDriver extends ObjectStoreRegistry {\n constructor(options: GcsRegistryDriverOptions) {\n super(\n new GcsStore(options.storage, options.bucket),\n normalizeObjectPrefix(options.prefix),\n options.now ?? ((): number => Date.now()),\n );\n }\n}\n", "/**\n * `GcsStorage` \u2014 the Google Cloud Storage backend as one object: generations and pointer, one bucket, stated once.\n *\n * See {@link StorageBackend} for why the two halves are configured together rather than separately.\n *\n * **A note on the word.** `@google-cloud/storage` names its own client class `Storage`, so the driver option\n * that takes it is also called `storage` \u2014 which, since our durable tier is now called storage too, reads as\n * the same word meaning two things. That is why this class builds the client itself: `new GcsStorage({ bucket })`\n * never makes you write `{ storage: storage }`. Supply `client` when you need your own.\n */\nimport { STORAGE_BACKEND, ValidationError, brandAsBackend } from '@cloudbitmaps/core/driver-kit';\nimport type {\n IRegistryDriver,\n IStorageDriver,\n StorageBackend,\n} from '@cloudbitmaps/core/driver-kit';\nimport { Storage as GcsClient } from '@google-cloud/storage';\nimport { GcsStorageDriver } from './storage';\nimport { GcsRegistryDriver } from './registry';\n\nexport interface GcsStorageOptions {\n /** Target bucket (must already exist). */\n readonly bucket: string;\n /** Optional object-name prefix under which everything lives \u2014 generations and the registry alike. */\n readonly prefix?: string;\n /** A constructed `@google-cloud/storage` client. One is built from the ambient credentials when absent. */\n readonly client?: GcsClient;\n /**\n * Not an option \u2014 the old `GcsStorageDriver` took the client as `storage`, and a migrating caller keeps the\n * name. Typed `never` so the object literal is a compile error, and rejected at runtime for JavaScript\n * callers, because silently ignoring it falls back to ambient credentials and the **public** endpoint: for\n * anyone whose client pointed at an emulator, that is production traffic from a wiring typo.\n */\n readonly storage?: never;\n /** Project id for the client built when `client` is absent. Falls back to the SDK's own resolution. */\n readonly projectId?: string;\n /** Endpoint override \u2014 point it at fake-gcs-server locally. Ignored when `client` is supplied. */\n readonly apiEndpoint?: string;\n /** Injected clock for the registry's `createdAt`/`updatedAt`; defaults to `Date.now`. */\n readonly now?: () => number;\n}\n\nexport class GcsStorage implements StorageBackend {\n /** Cross-bundle brand, stamped non-enumerably in the constructor so a spread cannot carry it. */\n declare readonly [STORAGE_BACKEND]: true;\n readonly storage: IStorageDriver;\n readonly registry: IRegistryDriver;\n /** The client both halves share \u2014 built here unless one was supplied. */\n readonly client: GcsClient;\n\n constructor(options: GcsStorageOptions) {\n if ((options as { storage?: unknown }).storage !== undefined) {\n throw new ValidationError(\n 'GcsStorage takes the @google-cloud/storage client as `client`, not `storage` (which was the old ' +\n 'GcsStorageDriver option). Rename it, or omit it and let the backend build one.',\n );\n }\n this.client =\n options.client ??\n new GcsClient({\n ...(options.projectId === undefined ? {} : { projectId: options.projectId }),\n ...(options.apiEndpoint === undefined ? {} : { apiEndpoint: options.apiEndpoint }),\n });\n const shared = {\n storage: this.client,\n bucket: options.bucket,\n ...(options.prefix === undefined ? {} : { prefix: options.prefix }),\n };\n this.storage = new GcsStorageDriver(shared);\n this.registry = new GcsRegistryDriver({\n ...shared,\n ...(options.now === undefined ? {} : { now: options.now }),\n });\n brandAsBackend(this);\n }\n}\n"],
5
+ "mappings": ";AAiBA;AAAA,EACE;AAAA,EACA;AAAA,EACA,mBAAAA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAQP,SAAS,kBAA6B;AAEtC,SAAS,YAAY;;;ACpBrB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAGP,IAAM,SAAS;AAOR,SAAS,mBAAmB,QAAgD;AACjF,MAAI,WAAW,OAAW,QAAO;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C,QAAQ,SAAS;AAAA,MAC5E,EAAE,OAAO,IAAI;AAAA,IACf;AAAA,EACF;AACA,SAAO;AACT;AAEO,IAAM,oBAAN,cAAgC,oBAAoB;AAAA,EACzD,YAAY,SAAmC;AAC7C;AAAA,MACE,IAAI,SAAS,QAAQ,SAAS,QAAQ,MAAM;AAAA,MAC5C,sBAAsB,QAAQ,MAAM;AAAA,MACpC,QAAQ,QAAQ,MAAc,KAAK,IAAI;AAAA,IACzC;AAAA,EACF;AACF;;;AC3KA,SAA0B,mBAAAC,kBAAiB,sBAAsB;AAMjE,SAAS,WAAW,iBAAiB;AA0B9B,IAAM,aAAN,MAA2C;AAAA,EAGvC;AAAA,EACA;AAAA;AAAA,EAEA;AAAA,EAET,YAAY,SAA4B;AACtC,QAAK,QAAkC,YAAY,QAAW;AAC5D,YAAM,IAAIC;AAAA,QACR;AAAA,MAEF;AAAA,IACF;AACA,SAAK,SACH,QAAQ,UACR,IAAI,UAAU;AAAA,MACZ,GAAI,QAAQ,cAAc,SAAY,CAAC,IAAI,EAAE,WAAW,QAAQ,UAAU;AAAA,MAC1E,GAAI,QAAQ,gBAAgB,SAAY,CAAC,IAAI,EAAE,aAAa,QAAQ,YAAY;AAAA,IAClF,CAAC;AACH,UAAM,SAAS;AAAA,MACb,SAAS,KAAK;AAAA,MACd,QAAQ,QAAQ;AAAA,MAChB,GAAI,QAAQ,WAAW,SAAY,CAAC,IAAI,EAAE,QAAQ,QAAQ,OAAO;AAAA,IACnE;AACA,SAAK,UAAU,IAAI,iBAAiB,MAAM;AAC1C,SAAK,WAAW,IAAI,kBAAkB;AAAA,MACpC,GAAG;AAAA,MACH,GAAI,QAAQ,QAAQ,SAAY,CAAC,IAAI,EAAE,KAAK,QAAQ,IAAI;AAAA,IAC1D,CAAC;AACD,mBAAe,IAAI;AAAA,EACrB;AACF;",
6
+ "names": ["ValidationError", "ValidationError", "TransientError", "WriteConflictError", "WriteConflictError", "TransientError", "ValidationError", "ValidationError"]
7
+ }
package/dist/keys.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Logical-ref → GCS object-name mapping for {@link GcsStorageDriver}.
3
+ *
4
+ * Pure string logic, no SDK dependency — unit-testable without GCS or an emulator. Uses the **same
5
+ * backend-agnostic `.crbm` object-name scheme** as the S3 + LocalFs storage drivers
6
+ * (`<prefix><ns>/segments/<segment>.<gen>.crbm`), so a segment reads identically whichever storage backend holds
7
+ * it. The default (absent) namespace maps to `_default`, which cannot collide with a real namespace because a
8
+ * caller's `_default` encodes to `%5Fdefault` while the sentinel is emitted literally.
9
+ *
10
+ * The storage-key layout is shared, not copied: `prefixPart` and the name encoders come from
11
+ * `@cloudbitmaps/core/driver-kit`, so a segment written by one driver reads identically under another. Two
12
+ * drivers disagreeing about how a name becomes a key would be a silent cross-driver incompatibility on the
13
+ * same bucket, which is exactly the kind of thing a local copy drifts into.
14
+ */
15
+ import type { GenKey, SegmentRef } from '@cloudbitmaps/core/driver-kit';
16
+ /**
17
+ * Validate the caller-supplied key prefix (trusted config, but a real containment boundary): reject control
18
+ * characters and `.`/`..` path segments so a prefix can't traverse out of its intended space. Returns it
19
+ * unchanged (the key builders normalize slashes).
20
+ */
21
+ export declare function normalizeGcsPrefix(prefix: string | undefined): string | undefined;
22
+ /**
23
+ * The GCS object-name prefix shared by all of a segment's generations: `<prefix><ns>/segments/<segment>.`.
24
+ * Used both as the `getFiles` prefix and as the string stripped by {@link parseGenerationFromName}.
25
+ */
26
+ export declare function segmentObjectPrefix(prefix: string | undefined, ref: SegmentRef): string;
27
+ /** The full GCS object name of one `.crbm` generation: `<segmentPrefix><gen>.crbm`. */
28
+ export declare function storageObjectName(prefix: string | undefined, key: GenKey): string;
29
+ /**
30
+ * Parse a generation number out of a full object name, given its segment prefix, or `null` if it doesn't
31
+ * match. Canonical decimal only — no leading zeros (so `…s.07.crbm` can't alias `…s.7.crbm`) and within
32
+ * safe-integer range. Also rejects a *different* segment whose name merely shares the prefix (its middle
33
+ * isn't all digits).
34
+ */
35
+ export declare function parseGenerationFromName(segmentPrefix: string, objectName: string): number | null;
@@ -0,0 +1,45 @@
1
+ /**
2
+ * `GcsRegistryDriver` — an {@link IRegistryDriver} over Google Cloud Storage.
3
+ *
4
+ * Lets a **GCS deployment run on one bucket alone** — storage `.crbm` generations and the registry in the same
5
+ * place, with no second cloud involved. Before this existed, a GCS user had to point the registry at a
6
+ * separate AWS-hosted table, which meant holding an AWS account purely to store the pointer that says which
7
+ * generation is current.
8
+ *
9
+ * The protocol (an ABA-safe OCC counter, tombstoning delete, the bounded retry, the key layout) lives once
10
+ * in {@link ObjectStoreRegistry}; this file is only the three I/O calls GCS makes.
11
+ *
12
+ * **The atomic swap is offloaded to GCS's object preconditions.** `ifGenerationMatch: 0` is create-only
13
+ * ("only if it does not exist") and `ifGenerationMatch: <generation>` is compare-and-swap — the same pair S3
14
+ * spells `If-None-Match: *` / `If-Match: <etag>`. A lost race returns `412` → {@link WriteConflictError}.
15
+ * **The version fence is the object's `generation`, not its ETag**: GCS mutates the generation on every
16
+ * overwrite and it is the value `ifGenerationMatch` compares, so it is the only correct fence here. (Note
17
+ * the unrelated collision of vocabulary — a GCS *object generation* has nothing to do with a CloudBitmaps
18
+ * `.crbm` *generation*.) Reads are strongly consistent, satisfying the registry's `strongRead` contract.
19
+ *
20
+ * **Deployment requirements** (a policy that violates these silently corrupts the registry):
21
+ * - The principal needs `storage.objects.get`, `create`, `update` and `list` on the bucket. Without `list`
22
+ * the registry cannot enumerate, and a missing-object read may surface as `403` rather than `404`.
23
+ * - **Do not apply an Object Lifecycle rule to the `registry/` prefix**, and do not enable a retention
24
+ * policy that blocks overwrite. `delete` tombstones rather than removing, for ABA-safety — see
25
+ * {@link ObjectStoreRegistry}.
26
+ * - Object versioning is neither required nor used; the driver always reads the live generation. Because a
27
+ * non-versioned bucket retires a superseded generation immediately, a read whose pinned generation is
28
+ * overwritten mid-flight reports {@link ObjectVersionRaced} and is retried by {@link ObjectStoreRegistry},
29
+ * never mistaken for an absent row.
30
+ */
31
+ import { ObjectStoreRegistry } from '@cloudbitmaps/core/driver-kit';
32
+ import type { Storage } from '@google-cloud/storage';
33
+ export interface GcsRegistryDriverOptions {
34
+ /** A constructed `@google-cloud/storage` `Storage` client (point `apiEndpoint` at fake-gcs-server locally). */
35
+ readonly storage: Storage;
36
+ /** Target bucket (must already exist). */
37
+ readonly bucket: string;
38
+ /** Optional object-name prefix under which all registry objects live (e.g. `cloudroaring/`). */
39
+ readonly prefix?: string;
40
+ /** Injected clock for `createdAt`/`updatedAt`; defaults to `Date.now`. */
41
+ readonly now?: () => number;
42
+ }
43
+ export declare class GcsRegistryDriver extends ObjectStoreRegistry {
44
+ constructor(options: GcsRegistryDriverOptions);
45
+ }
@@ -0,0 +1,43 @@
1
+ import type { BlobSink, GenKey, IStorageDriver, SegmentRef, StorageCaps } from '@cloudbitmaps/core/driver-kit';
2
+ import type { Storage } from '@google-cloud/storage';
3
+ export interface GcsStorageDriverOptions {
4
+ /** A constructed `@google-cloud/storage` `Storage` client (point `apiEndpoint` at fake-gcs-server locally). */
5
+ readonly storage: Storage;
6
+ /** Target bucket (must already exist). */
7
+ readonly bucket: string;
8
+ /** Optional object-name prefix under which all objects live (e.g. `cloudroaring/`). */
9
+ readonly prefix?: string;
10
+ /** Largest object this driver will write/advertise (default = GCS's 5 TiB max). */
11
+ readonly maxObjectBytes?: number;
12
+ /** Bytes at/under which a single non-resumable upload is used instead of a resumable stream (default 8 MiB). */
13
+ readonly simpleUploadThresholdBytes?: number;
14
+ }
15
+ export declare class GcsStorageDriver implements IStorageDriver {
16
+ private readonly storage;
17
+ private readonly bucket;
18
+ private readonly prefix;
19
+ private readonly maxObjectBytes;
20
+ private readonly threshold;
21
+ constructor(options: GcsStorageDriverOptions);
22
+ capabilities(): StorageCaps;
23
+ private file;
24
+ putImmutable(key: GenKey, write: (sink: BlobSink) => Promise<void>): Promise<{
25
+ size: number;
26
+ sha256: string;
27
+ }>;
28
+ getRange(key: GenKey, offset: number, length: number): Promise<Uint8Array>;
29
+ getTail(key: GenKey, maxBytes: number): Promise<{
30
+ bytes: Uint8Array;
31
+ size: number;
32
+ }>;
33
+ delete(key: GenKey): Promise<void>;
34
+ list(ref: SegmentRef): AsyncIterable<GenKey>;
35
+ /** Map GCS read errors to the driver vocabulary; pass everything else through {@link mapError}. */
36
+ private mapReadError;
37
+ /**
38
+ * Reclassify a transient GCS fault (throttle/5xx/dropped connection) as a retryable {@link TransientError}
39
+ * so the retry decorator can ride it out; everything else propagates unchanged. The final fallback at every
40
+ * client-call site, so callers + the decorator only ever see typed errors.
41
+ */
42
+ private mapError;
43
+ }
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@cloudbitmaps/gcs",
3
+ "version": "0.10.0-rc.0",
4
+ "description": "Google Cloud Storage for CloudBitmaps",
5
+ "keywords": [
6
+ "cloudbitmaps",
7
+ "bitmap",
8
+ "roaring-bitmap",
9
+ "serverless",
10
+ "cloud",
11
+ "typescript",
12
+ "gcs",
13
+ "google-cloud",
14
+ "google-cloud-storage",
15
+ "object-storage"
16
+ ],
17
+ "license": "Apache-2.0",
18
+ "author": "Sharvil Kadam",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/cloudbitmaps/cloudbitmaps.git",
22
+ "directory": "packages/gcs"
23
+ },
24
+ "homepage": "https://github.com/cloudbitmaps/cloudbitmaps#readme",
25
+ "bugs": {
26
+ "url": "https://github.com/cloudbitmaps/cloudbitmaps/issues"
27
+ },
28
+ "type": "module",
29
+ "engines": {
30
+ "node": ">=22.12"
31
+ },
32
+ "sideEffects": false,
33
+ "main": "./dist/index.js",
34
+ "types": "./dist/index.d.ts",
35
+ "exports": {
36
+ ".": {
37
+ "types": "./dist/index.d.ts",
38
+ "default": "./dist/index.js"
39
+ }
40
+ },
41
+ "files": [
42
+ "dist",
43
+ "README.md",
44
+ "LICENSE",
45
+ "NOTICE"
46
+ ],
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "dependencies": {
51
+ "@google-cloud/storage": "^7 || ^8",
52
+ "@cloudbitmaps/core": "^0.10.0"
53
+ },
54
+ "devDependencies": {
55
+ "@types/node": "^22.0.0",
56
+ "typescript": "^5.5.0"
57
+ },
58
+ "scripts": {
59
+ "build": "node ../../scripts/build.mjs"
60
+ }
61
+ }