@rljson/bs 0.0.18

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 (42) hide show
  1. package/LICENSE +21 -0
  2. package/README.architecture.md +9 -0
  3. package/README.blog.md +11 -0
  4. package/README.contributors.md +32 -0
  5. package/README.md +24 -0
  6. package/README.public.md +15 -0
  7. package/README.trouble.md +23 -0
  8. package/dist/README.architecture.md +9 -0
  9. package/dist/README.blog.md +11 -0
  10. package/dist/README.contributors.md +32 -0
  11. package/dist/README.md +24 -0
  12. package/dist/README.public.md +15 -0
  13. package/dist/README.trouble.md +23 -0
  14. package/dist/bs-mem.d.ts +33 -0
  15. package/dist/bs-test-setup.d.ts +25 -0
  16. package/dist/bs.d.ts +108 -0
  17. package/dist/bs.js +129 -0
  18. package/dist/conformance-tests/bs-conformance.setup.ts +39 -0
  19. package/dist/conformance-tests/bs-conformance.spec.ts +528 -0
  20. package/dist/conformance-tests/goldens/binary-content.json +4 -0
  21. package/dist/conformance-tests/goldens/consistent-ordering.json +14 -0
  22. package/dist/conformance-tests/goldens/deduplication.json +4 -0
  23. package/dist/conformance-tests/goldens/different-content.json +10 -0
  24. package/dist/conformance-tests/goldens/empty-content.json +4 -0
  25. package/dist/conformance-tests/goldens/from-buffer.json +4 -0
  26. package/dist/conformance-tests/goldens/from-stream.json +4 -0
  27. package/dist/conformance-tests/goldens/from-string.json +4 -0
  28. package/dist/conformance-tests/goldens/get-blob-properties.json +4 -0
  29. package/dist/conformance-tests/goldens/get-blob-stream.json +1 -0
  30. package/dist/conformance-tests/goldens/large-content.json +4 -0
  31. package/dist/conformance-tests/goldens/list-all-blobs.json +14 -0
  32. package/dist/conformance-tests/goldens/list-by-prefix.json +6 -0
  33. package/dist/conformance-tests/goldens/paginate-all-results.json +7 -0
  34. package/dist/conformance-tests/goldens/paginated-listing.json +26 -0
  35. package/dist/conformance-tests/goldens/range-request.json +6 -0
  36. package/dist/conformance-tests/goldens/retrieve-binary.json +6 -0
  37. package/dist/conformance-tests/goldens/retrieve-blob.json +6 -0
  38. package/dist/conformance-tests/scripts/install-conformance-tests.js +103 -0
  39. package/dist/example.d.ts +1 -0
  40. package/dist/index.d.ts +3 -0
  41. package/dist/src/example.ts +9 -0
  42. package/package.json +62 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Rljson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,9 @@
1
+ <!--
2
+ @license
3
+ Copyright (c) 2025 Rljson
4
+
5
+ Use of this source code is governed by terms that can be
6
+ found in the LICENSE file in the root of this package.
7
+ -->
8
+
9
+ # Architecture
package/README.blog.md ADDED
@@ -0,0 +1,11 @@
1
+ <!--
2
+ @license
3
+ Copyright (c) 2025 Rljson
4
+
5
+ Use of this source code is governed by terms that can be
6
+ found in the LICENSE file in the root of this package.
7
+ -->
8
+
9
+ # Blog
10
+
11
+ Add latest posts at the end.
@@ -0,0 +1,32 @@
1
+ <!--
2
+ @license
3
+ Copyright (c) 2025 Rljson
4
+
5
+ Use of this source code is governed by terms that can be
6
+ found in the LICENSE file in the root of this package.
7
+ -->
8
+
9
+ # Contributors Guide
10
+
11
+ - [Prepare](#prepare)
12
+ - [Develop](#develop)
13
+ - [Administrate](#administrate)
14
+ - [Fast Coding](#fast-coding)
15
+
16
+ ## Prepare
17
+
18
+ Read [prepare.md](doc/prepare.md)
19
+
20
+ <!-- ........................................................................-->
21
+
22
+ ## Develop
23
+
24
+ Read [develop.md](doc/develop.md)
25
+
26
+ ## Administrate
27
+
28
+ Read [create-new-repo.md](doc/create-new-repo.md)
29
+
30
+ ## Fast Coding
31
+
32
+ Read [fast-coding-guide.md](doc/fast-coding-guide.md)
package/README.md ADDED
@@ -0,0 +1,24 @@
1
+ <!--
2
+ @license
3
+ Copyright (c) 2025 Rljson
4
+
5
+ Use of this source code is governed by terms that can be
6
+ found in the LICENSE file in the root of this package.
7
+ -->
8
+
9
+ # @rljson/bs
10
+
11
+ ## Users
12
+
13
+ | File | Purpose |
14
+ | ------------------------------------ | --------------------------- |
15
+ | [README.public.md](README.public.md) | Install and use the package |
16
+
17
+ ## Contributors
18
+
19
+ | File | Purpose |
20
+ | ------------------------------------------------ | ----------------------------- |
21
+ | [README.contributors.md](README.contributors.md) | Run, debug, build and publish |
22
+ | [README.architecture.md](README.architecture.md) | Software architecture guide |
23
+ | [README.trouble.md](README.trouble.md) | Errors & solutions |
24
+ | [README.blog.md](README.blog.md) | Blog |
@@ -0,0 +1,15 @@
1
+ <!--
2
+ @license
3
+ Copyright (c) 2025 Rljson
4
+
5
+ Use of this source code is governed by terms that can be
6
+ found in the LICENSE file in the root of this package.
7
+ -->
8
+
9
+ # @rljson/bs
10
+
11
+ Blob storage interface and implementations for rljson.
12
+
13
+ ## Example
14
+
15
+ [src/example.ts](src/example.ts)
@@ -0,0 +1,23 @@
1
+ <!--
2
+ @license
3
+ Copyright (c) 2025 Rljson
4
+
5
+ Use of this source code is governed by terms that can be
6
+ found in the LICENSE file in the root of this package.
7
+ -->
8
+
9
+ # Trouble shooting
10
+
11
+ ## Table of contents <!-- omit in toc -->
12
+
13
+ - [Vscode Windows: Debugging is not working](#vscode-windows-debugging-is-not-working)
14
+
15
+ ## Vscode Windows: Debugging is not working
16
+
17
+ Date: 2025-03-08
18
+
19
+ ⚠️ IMPORTANT: On Windows, please check out the repo on drive C. There is a bug
20
+ in the VS Code Vitest extension (v1.14.4), which prevents test debugging from
21
+ working: <https://github.com/vitest-dev/vscode/issues/548> Please check from
22
+ time to time if the issue has been fixed and remove this note once it is
23
+ resolved.
@@ -0,0 +1,9 @@
1
+ <!--
2
+ @license
3
+ Copyright (c) 2025 Rljson
4
+
5
+ Use of this source code is governed by terms that can be
6
+ found in the LICENSE file in the root of this package.
7
+ -->
8
+
9
+ # Architecture
@@ -0,0 +1,11 @@
1
+ <!--
2
+ @license
3
+ Copyright (c) 2025 Rljson
4
+
5
+ Use of this source code is governed by terms that can be
6
+ found in the LICENSE file in the root of this package.
7
+ -->
8
+
9
+ # Blog
10
+
11
+ Add latest posts at the end.
@@ -0,0 +1,32 @@
1
+ <!--
2
+ @license
3
+ Copyright (c) 2025 Rljson
4
+
5
+ Use of this source code is governed by terms that can be
6
+ found in the LICENSE file in the root of this package.
7
+ -->
8
+
9
+ # Contributors Guide
10
+
11
+ - [Prepare](#prepare)
12
+ - [Develop](#develop)
13
+ - [Administrate](#administrate)
14
+ - [Fast Coding](#fast-coding)
15
+
16
+ ## Prepare
17
+
18
+ Read [prepare.md](doc/prepare.md)
19
+
20
+ <!-- ........................................................................-->
21
+
22
+ ## Develop
23
+
24
+ Read [develop.md](doc/develop.md)
25
+
26
+ ## Administrate
27
+
28
+ Read [create-new-repo.md](doc/create-new-repo.md)
29
+
30
+ ## Fast Coding
31
+
32
+ Read [fast-coding-guide.md](doc/fast-coding-guide.md)
package/dist/README.md ADDED
@@ -0,0 +1,24 @@
1
+ <!--
2
+ @license
3
+ Copyright (c) 2025 Rljson
4
+
5
+ Use of this source code is governed by terms that can be
6
+ found in the LICENSE file in the root of this package.
7
+ -->
8
+
9
+ # @rljson/bs
10
+
11
+ ## Users
12
+
13
+ | File | Purpose |
14
+ | ------------------------------------ | --------------------------- |
15
+ | [README.public.md](README.public.md) | Install and use the package |
16
+
17
+ ## Contributors
18
+
19
+ | File | Purpose |
20
+ | ------------------------------------------------ | ----------------------------- |
21
+ | [README.contributors.md](README.contributors.md) | Run, debug, build and publish |
22
+ | [README.architecture.md](README.architecture.md) | Software architecture guide |
23
+ | [README.trouble.md](README.trouble.md) | Errors & solutions |
24
+ | [README.blog.md](README.blog.md) | Blog |
@@ -0,0 +1,15 @@
1
+ <!--
2
+ @license
3
+ Copyright (c) 2025 Rljson
4
+
5
+ Use of this source code is governed by terms that can be
6
+ found in the LICENSE file in the root of this package.
7
+ -->
8
+
9
+ # @rljson/bs
10
+
11
+ Blob storage interface and implementations for rljson.
12
+
13
+ ## Example
14
+
15
+ [src/example.ts](src/example.ts)
@@ -0,0 +1,23 @@
1
+ <!--
2
+ @license
3
+ Copyright (c) 2025 Rljson
4
+
5
+ Use of this source code is governed by terms that can be
6
+ found in the LICENSE file in the root of this package.
7
+ -->
8
+
9
+ # Trouble shooting
10
+
11
+ ## Table of contents <!-- omit in toc -->
12
+
13
+ - [Vscode Windows: Debugging is not working](#vscode-windows-debugging-is-not-working)
14
+
15
+ ## Vscode Windows: Debugging is not working
16
+
17
+ Date: 2025-03-08
18
+
19
+ ⚠️ IMPORTANT: On Windows, please check out the repo on drive C. There is a bug
20
+ in the VS Code Vitest extension (v1.14.4), which prevents test debugging from
21
+ working: <https://github.com/vitest-dev/vscode/issues/548> Please check from
22
+ time to time if the issue has been fixed and remove this note once it is
23
+ resolved.
@@ -0,0 +1,33 @@
1
+ import { BlobProperties, Bs, DownloadBlobOptions, ListBlobsOptions, ListBlobsResult } from './bs.js';
2
+ /**
3
+ * In-memory implementation of content-addressable blob storage.
4
+ * All blobs are stored in memory using a Map.
5
+ * Useful for testing and development.
6
+ */
7
+ export declare class BsMem implements Bs {
8
+ private readonly blobs;
9
+ /**
10
+ * Convert content to Buffer
11
+ * @param content - Content to convert (Buffer, string, or ReadableStream)
12
+ */
13
+ private toBuffer;
14
+ setBlob(content: Buffer | string | ReadableStream): Promise<BlobProperties>;
15
+ getBlob(blobId: string, options?: DownloadBlobOptions): Promise<{
16
+ content: Buffer;
17
+ properties: BlobProperties;
18
+ }>;
19
+ getBlobStream(blobId: string): Promise<ReadableStream>;
20
+ deleteBlob(blobId: string): Promise<void>;
21
+ blobExists(blobId: string): Promise<boolean>;
22
+ getBlobProperties(blobId: string): Promise<BlobProperties>;
23
+ listBlobs(options?: ListBlobsOptions): Promise<ListBlobsResult>;
24
+ generateSignedUrl(blobId: string, expiresIn: number, permissions?: 'read' | 'delete'): Promise<string>;
25
+ /**
26
+ * Clear all blobs from storage (useful for testing)
27
+ */
28
+ clear(): void;
29
+ /**
30
+ * Get the number of blobs in storage
31
+ */
32
+ get size(): number;
33
+ }
@@ -0,0 +1,25 @@
1
+ import { Bs } from './bs.js';
2
+ /**
3
+ * Bs implementation need to implement this interface to be used in
4
+ * conformance tests.
5
+ */
6
+ export interface BsTestSetup {
7
+ /** Setup before all tests */
8
+ beforeAll: () => Promise<void>;
9
+ /**
10
+ * Initializes the Bs implementation.
11
+ * Should create a fresh instance for each test.
12
+ */
13
+ beforeEach: () => Promise<void>;
14
+ /**
15
+ * Tears down the Bs implementation.
16
+ * Should clean up resources after each test.
17
+ */
18
+ afterEach: () => Promise<void>;
19
+ /** Cleanup after all tests */
20
+ afterAll: () => Promise<void>;
21
+ /**
22
+ * The Bs implementation to be used in the conformance tests.
23
+ */
24
+ bs: Bs;
25
+ }
package/dist/bs.d.ts ADDED
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Content-addressable blob storage interface.
3
+ * Blobs are identified by SHA256 hash of their content.
4
+ * Uses a flat storage pool - all blobs are stored globally by their content hash.
5
+ * Metadata and organizational grouping is managed externally.
6
+ */
7
+ /**
8
+ * Properties of a blob (minimal, as metadata is external)
9
+ */
10
+ export interface BlobProperties {
11
+ /** SHA256 hash of the blob content (unique identifier) */
12
+ blobId: string;
13
+ /** Size in bytes */
14
+ size: number;
15
+ /** When the blob was first stored */
16
+ createdAt: Date;
17
+ }
18
+ /**
19
+ * Options for downloading a blob
20
+ */
21
+ export interface DownloadBlobOptions {
22
+ /** Download only a byte range (e.g., for resumable downloads) */
23
+ range?: {
24
+ start: number;
25
+ end?: number;
26
+ };
27
+ }
28
+ /**
29
+ * Options for listing blobs
30
+ */
31
+ export interface ListBlobsOptions {
32
+ /** Filter by ID prefix */
33
+ prefix?: string;
34
+ /** Maximum number of results */
35
+ maxResults?: number;
36
+ /** Continuation token for pagination */
37
+ continuationToken?: string;
38
+ }
39
+ /**
40
+ * Result of listing blobs
41
+ */
42
+ export interface ListBlobsResult {
43
+ /** Array of blob properties */
44
+ blobs: BlobProperties[];
45
+ /** Token for fetching next page */
46
+ continuationToken?: string;
47
+ }
48
+ /**
49
+ * Content-Addressable Blob Storage Interface
50
+ * Flat storage pool - no containers/buckets needed
51
+ */
52
+ export interface Bs {
53
+ /**
54
+ * Store a blob. The blob ID (SHA256 hash) is calculated from content.
55
+ * If a blob with the same content already exists, it returns existing blob properties.
56
+ * @param content - Blob content (Buffer, string, or stream)
57
+ * @returns Properties of the stored blob (including calculated blobId)
58
+ */
59
+ setBlob(content: Buffer | string | ReadableStream): Promise<BlobProperties>;
60
+ /**
61
+ * Get a blob from storage by its ID
62
+ * @param blobId - SHA256 hash of the blob
63
+ * @param options - Download options
64
+ * @returns Blob content and properties
65
+ */
66
+ getBlob(blobId: string, options?: DownloadBlobOptions): Promise<{
67
+ content: Buffer;
68
+ properties: BlobProperties;
69
+ }>;
70
+ /**
71
+ * Get a readable stream for a blob
72
+ * @param blobId - SHA256 hash of the blob
73
+ * @returns Readable stream
74
+ */
75
+ getBlobStream(blobId: string): Promise<ReadableStream>;
76
+ /**
77
+ * Delete a blob by its ID
78
+ * Note: In a production system, consider reference counting before deletion
79
+ * @param blobId - SHA256 hash of the blob
80
+ */
81
+ deleteBlob(blobId: string): Promise<void>;
82
+ /**
83
+ * Check if a blob exists
84
+ * @param blobId - SHA256 hash of the blob
85
+ * @returns True if exists
86
+ */
87
+ blobExists(blobId: string): Promise<boolean>;
88
+ /**
89
+ * Get blob properties without downloading content
90
+ * @param blobId - SHA256 hash of the blob
91
+ * @returns Blob properties
92
+ */
93
+ getBlobProperties(blobId: string): Promise<BlobProperties>;
94
+ /**
95
+ * List all blobs in the storage pool
96
+ * @param options - List options
97
+ * @returns List of blobs with optional continuation token
98
+ */
99
+ listBlobs(options?: ListBlobsOptions): Promise<ListBlobsResult>;
100
+ /**
101
+ * Get a signed URL for temporary access to a blob
102
+ * @param blobId - SHA256 hash of the blob
103
+ * @param expiresIn - Expiration time in seconds
104
+ * @param permissions - Permissions ('read' or 'delete')
105
+ * @returns Signed URL
106
+ */
107
+ generateSignedUrl(blobId: string, expiresIn: number, permissions?: 'read' | 'delete'): Promise<string>;
108
+ }
package/dist/bs.js ADDED
@@ -0,0 +1,129 @@
1
+ import { hshBuffer } from "@rljson/hash";
2
+ import { Readable } from "node:stream";
3
+ class BsMem {
4
+ blobs = /* @__PURE__ */ new Map();
5
+ /**
6
+ * Convert content to Buffer
7
+ * @param content - Content to convert (Buffer, string, or ReadableStream)
8
+ */
9
+ async toBuffer(content) {
10
+ if (Buffer.isBuffer(content)) {
11
+ return content;
12
+ }
13
+ if (typeof content === "string") {
14
+ return Buffer.from(content, "utf8");
15
+ }
16
+ const reader = content.getReader();
17
+ const chunks = [];
18
+ while (true) {
19
+ const { done, value } = await reader.read();
20
+ if (done) break;
21
+ chunks.push(value);
22
+ }
23
+ return Buffer.concat(chunks);
24
+ }
25
+ async setBlob(content) {
26
+ const buffer = await this.toBuffer(content);
27
+ const blobId = hshBuffer(buffer);
28
+ const existing = this.blobs.get(blobId);
29
+ if (existing) {
30
+ return existing.properties;
31
+ }
32
+ const properties = {
33
+ blobId,
34
+ size: buffer.length,
35
+ createdAt: /* @__PURE__ */ new Date()
36
+ };
37
+ this.blobs.set(blobId, {
38
+ content: buffer,
39
+ properties
40
+ });
41
+ return properties;
42
+ }
43
+ async getBlob(blobId, options) {
44
+ const stored = this.blobs.get(blobId);
45
+ if (!stored) {
46
+ throw new Error(`Blob not found: ${blobId}`);
47
+ }
48
+ let content = stored.content;
49
+ if (options?.range) {
50
+ const { start, end } = options.range;
51
+ content = stored.content.subarray(start, end);
52
+ }
53
+ return {
54
+ content,
55
+ properties: stored.properties
56
+ };
57
+ }
58
+ async getBlobStream(blobId) {
59
+ const stored = this.blobs.get(blobId);
60
+ if (!stored) {
61
+ throw new Error(`Blob not found: ${blobId}`);
62
+ }
63
+ const nodeStream = Readable.from(stored.content);
64
+ return Readable.toWeb(nodeStream);
65
+ }
66
+ async deleteBlob(blobId) {
67
+ const deleted = this.blobs.delete(blobId);
68
+ if (!deleted) {
69
+ throw new Error(`Blob not found: ${blobId}`);
70
+ }
71
+ }
72
+ async blobExists(blobId) {
73
+ return this.blobs.has(blobId);
74
+ }
75
+ async getBlobProperties(blobId) {
76
+ const stored = this.blobs.get(blobId);
77
+ if (!stored) {
78
+ throw new Error(`Blob not found: ${blobId}`);
79
+ }
80
+ return stored.properties;
81
+ }
82
+ async listBlobs(options) {
83
+ let blobs = Array.from(this.blobs.values()).map(
84
+ (stored) => stored.properties
85
+ );
86
+ if (options?.prefix) {
87
+ blobs = blobs.filter((blob) => blob.blobId.startsWith(options.prefix));
88
+ }
89
+ blobs.sort((a, b) => a.blobId.localeCompare(b.blobId));
90
+ const maxResults = options?.maxResults ?? blobs.length;
91
+ let startIndex = 0;
92
+ if (options?.continuationToken) {
93
+ const tokenIndex = blobs.findIndex(
94
+ (blob) => blob.blobId === options.continuationToken
95
+ );
96
+ startIndex = tokenIndex === -1 ? 0 : tokenIndex + 1;
97
+ }
98
+ const endIndex = Math.min(startIndex + maxResults, blobs.length);
99
+ const pageBlobs = blobs.slice(startIndex, endIndex);
100
+ const continuationToken = endIndex < blobs.length ? pageBlobs[pageBlobs.length - 1]?.blobId : void 0;
101
+ return {
102
+ blobs: pageBlobs,
103
+ continuationToken
104
+ };
105
+ }
106
+ async generateSignedUrl(blobId, expiresIn, permissions) {
107
+ if (!this.blobs.has(blobId)) {
108
+ throw new Error(`Blob not found: ${blobId}`);
109
+ }
110
+ const expires = Date.now() + expiresIn * 1e3;
111
+ const perm = permissions ?? "read";
112
+ return `mem://${blobId}?expires=${expires}&permissions=${perm}`;
113
+ }
114
+ /**
115
+ * Clear all blobs from storage (useful for testing)
116
+ */
117
+ clear() {
118
+ this.blobs.clear();
119
+ }
120
+ /**
121
+ * Get the number of blobs in storage
122
+ */
123
+ get size() {
124
+ return this.blobs.size;
125
+ }
126
+ }
127
+ export {
128
+ BsMem
129
+ };
@@ -0,0 +1,39 @@
1
+ // @license
2
+ // Copyright (c) 2026 Rljson
3
+ //
4
+ // Use of this source code is governed by terms that can be
5
+ // found in the LICENSE file in the root of this package.
6
+
7
+ import { BsMem, BsTestSetup } from '../src';
8
+
9
+ // .............................................................................
10
+ /**
11
+ * Test setup for BsMem conformance tests
12
+ */
13
+ class MyBsTestSetup implements BsTestSetup {
14
+ bs: BsMem;
15
+
16
+ constructor() {
17
+ this.bs = new BsMem();
18
+ }
19
+
20
+ async beforeAll(): Promise<void> {
21
+ // Setup before all tests
22
+ }
23
+
24
+ async beforeEach(): Promise<void> {
25
+ // Clear before each test
26
+ this.bs.clear();
27
+ }
28
+
29
+ async afterEach(): Promise<void> {
30
+ // Cleanup after each test
31
+ }
32
+
33
+ async afterAll(): Promise<void> {
34
+ // Cleanup after all tests
35
+ }
36
+ }
37
+
38
+ // .............................................................................
39
+ export const testSetup = () => new MyBsTestSetup();