@kubun/blob-node-fs 0.13.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.md ADDED
@@ -0,0 +1,57 @@
1
+ # The Prosperity Public License 3.0.0
2
+
3
+ Contributor: Paul Le Cam
4
+
5
+ Source Code: https://github.com/PaulLeCam/kubun
6
+
7
+ ## Purpose
8
+
9
+ This license allows you to use and share this software for noncommercial purposes for free and to try this software for commercial purposes for thirty days.
10
+
11
+ ## Agreement
12
+
13
+ In order to receive this license, you have to agree to its rules. Those rules are both obligations under that agreement and conditions to your license. Don't do anything with this software that triggers a rule you can't or won't follow.
14
+
15
+ ## Notices
16
+
17
+ Make sure everyone who gets a copy of any part of this software from you, with or without changes, also gets the text of this license and the contributor and source code lines above.
18
+
19
+ ## Commercial Trial
20
+
21
+ Limit your use of this software for commercial purposes to a thirty-day trial period. If you use this software for work, your company gets one trial period for all personnel, not one trial per person.
22
+
23
+ ## Contributions Back
24
+
25
+ Developing feedback, changes, or additions that you contribute back to the contributor on the terms of a standardized public software license such as [the Blue Oak Model License 1.0.0](https://blueoakcouncil.org/license/1.0.0), [the Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0.html), [the MIT license](https://spdx.org/licenses/MIT.html), or [the two-clause BSD license](https://spdx.org/licenses/BSD-2-Clause.html) doesn't count as use for a commercial purpose.
26
+
27
+ ## Personal Uses
28
+
29
+ Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, doesn't count as use for a commercial purpose.
30
+
31
+ ## Noncommercial Organizations
32
+
33
+ Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution doesn't count as use for a commercial purpose regardless of the source of funding or obligations resulting from the funding.
34
+
35
+ ## Defense
36
+
37
+ Don't make any legal claim against anyone accusing this software, with or without changes, alone or with other technology, of infringing any patent.
38
+
39
+ ## Copyright
40
+
41
+ The contributor licenses you to do everything with this software that would otherwise infringe their copyright in it.
42
+
43
+ ## Patent
44
+
45
+ The contributor licenses you to do everything with this software that would otherwise infringe any patents they can license or become able to license.
46
+
47
+ ## Reliability
48
+
49
+ The contributor can't revoke this license.
50
+
51
+ ## Excuse
52
+
53
+ You're excused for unknowingly breaking [Notices](#notices) if you take all practical steps to comply within thirty days of learning you broke the rule.
54
+
55
+ ## No Liability
56
+
57
+ ***As far as the law allows, this software comes as is, without any warranty or condition, and the contributor won't be liable to anyone for any damages related to this software or this license, under any kind of legal claim.***
package/lib/fs.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import type { BlobBackend, BlobRange } from '@kubun/blob-backend';
2
+ export declare class FSBlobBackend implements BlobBackend {
3
+ #private;
4
+ constructor(root: string);
5
+ createStaging(stagingID: string): Promise<WritableStream<Uint8Array>>;
6
+ writeChunk(stagingID: string, offset: number, bytes: Uint8Array): Promise<void>;
7
+ commit(stagingID: string, key: string): Promise<void>;
8
+ abortStaging(stagingID: string): Promise<void>;
9
+ createReadStream(key: string, range?: BlobRange): Promise<ReadableStream<Uint8Array>>;
10
+ has(key: string): Promise<boolean>;
11
+ delete(key: string): Promise<void>;
12
+ getURL(key: string): Promise<string | null>;
13
+ }
package/lib/fs.js ADDED
@@ -0,0 +1,92 @@
1
+ import { constants, createReadStream, createWriteStream } from 'node:fs';
2
+ import { mkdir, open, rename, rm, stat } from 'node:fs/promises';
3
+ import { join } from 'node:path';
4
+ import { Readable, Writable } from 'node:stream';
5
+ import { pathToFileURL } from 'node:url';
6
+ // Filesystem BlobBackend. Staged uploads land under `<root>/staging`, committed
7
+ // blobs under `<root>/content/<key>`. Commit is an atomic rename within the same
8
+ // filesystem. Content-addressed keys are AttachmentID base36 strings (safe as
9
+ // flat filenames).
10
+ export class FSBlobBackend {
11
+ #stagingDir;
12
+ #contentDir;
13
+ #ready = null;
14
+ constructor(root){
15
+ this.#stagingDir = join(root, 'staging');
16
+ this.#contentDir = join(root, 'content');
17
+ }
18
+ #ensureDirs() {
19
+ this.#ready ??= (async ()=>{
20
+ await mkdir(this.#stagingDir, {
21
+ recursive: true
22
+ });
23
+ await mkdir(this.#contentDir, {
24
+ recursive: true
25
+ });
26
+ })();
27
+ return this.#ready;
28
+ }
29
+ #stagingPath(stagingID) {
30
+ return join(this.#stagingDir, stagingID);
31
+ }
32
+ #contentPath(key) {
33
+ return join(this.#contentDir, key);
34
+ }
35
+ async createStaging(stagingID) {
36
+ await this.#ensureDirs();
37
+ const nodeStream = createWriteStream(this.#stagingPath(stagingID));
38
+ return Writable.toWeb(nodeStream);
39
+ }
40
+ async writeChunk(stagingID, offset, bytes) {
41
+ await this.#ensureDirs();
42
+ // O_RDWR | O_CREAT: create-if-absent, no truncate — positioned writes into a
43
+ // possibly-sparse file as out-of-order ranges arrive.
44
+ const handle = await open(this.#stagingPath(stagingID), constants.O_RDWR | constants.O_CREAT);
45
+ try {
46
+ await handle.write(bytes, 0, bytes.length, offset);
47
+ } finally{
48
+ await handle.close();
49
+ }
50
+ }
51
+ async commit(stagingID, key) {
52
+ await this.#ensureDirs();
53
+ if (await this.has(key)) {
54
+ await rm(this.#stagingPath(stagingID), {
55
+ force: true
56
+ });
57
+ return;
58
+ }
59
+ await rename(this.#stagingPath(stagingID), this.#contentPath(key));
60
+ }
61
+ async abortStaging(stagingID) {
62
+ await rm(this.#stagingPath(stagingID), {
63
+ force: true
64
+ });
65
+ }
66
+ async createReadStream(key, range) {
67
+ // createReadStream's `end` is inclusive, matching BlobRange.
68
+ const options = range == null ? undefined : {
69
+ start: range.start,
70
+ end: range.end
71
+ };
72
+ const nodeStream = createReadStream(this.#contentPath(key), options);
73
+ return Readable.toWeb(nodeStream);
74
+ }
75
+ async has(key) {
76
+ try {
77
+ await stat(this.#contentPath(key));
78
+ return true;
79
+ } catch {
80
+ return false;
81
+ }
82
+ }
83
+ async delete(key) {
84
+ await rm(this.#contentPath(key), {
85
+ force: true
86
+ });
87
+ }
88
+ async getURL(key) {
89
+ if (!await this.has(key)) return null;
90
+ return pathToFileURL(this.#contentPath(key)).href;
91
+ }
92
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export { FSBlobBackend } from './fs.js';
package/lib/index.js ADDED
@@ -0,0 +1 @@
1
+ export { FSBlobBackend } from './fs.js';
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@kubun/blob-node-fs",
3
+ "version": "0.13.0",
4
+ "keywords": [],
5
+ "license": "see LICENSE.md",
6
+ "sideEffects": false,
7
+ "type": "module",
8
+ "exports": {
9
+ ".": "./lib/index.js"
10
+ },
11
+ "main": "lib/index.js",
12
+ "types": "lib/index.d.ts",
13
+ "files": [
14
+ "lib/*",
15
+ "LICENSE.md"
16
+ ],
17
+ "dependencies": {
18
+ "@kubun/blob-backend": "^0.13.0"
19
+ },
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "scripts": {
24
+ "build": "pnpm run build:clean && pnpm run build:js && pnpm run build:types",
25
+ "build:clean": "del lib",
26
+ "build:js": "swc src -d ./lib --config-file ../../node_modules/@kigu/dev/swc.json --strip-leading-paths",
27
+ "build:types": "tsc --emitDeclarationOnly --skipLibCheck",
28
+ "build:types:ci": "tsc --emitDeclarationOnly --declarationMap false",
29
+ "test": "pnpm run test:types && pnpm run test:unit",
30
+ "test:types": "tsc --noEmit -p tsconfig.test.json",
31
+ "test:unit": "vitest run"
32
+ }
33
+ }