@airnauts/comments-storage-fs 0.1.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 +21 -0
- package/README.md +24 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +68 -0
- package/dist/index.js.map +1 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Airnauts
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# @airnauts/comments-storage-fs
|
|
2
|
+
|
|
3
|
+
Filesystem attachment-storage adapter for the
|
|
4
|
+
[Airnauts commenting tool](https://github.com/Airnauts/commenting-tool) server.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pnpm add @airnauts/comments-storage-fs
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { FileSystemStorage } from '@airnauts/comments-storage-fs'
|
|
16
|
+
|
|
17
|
+
const storage = new FileSystemStorage({ rootDir: './uploads' })
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Pass `storage` to `createCommentsServer` from `@airnauts/comments-server`.
|
|
21
|
+
|
|
22
|
+
## License
|
|
23
|
+
|
|
24
|
+
MIT © Airnauts
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { PutBlob, PutResult, StorageAdapter } from '@airnauts/comments-server';
|
|
2
|
+
export type FileSystemStorageOptions = {
|
|
3
|
+
rootDir: string;
|
|
4
|
+
/**
|
|
5
|
+
* Public URL base. When set, `put` returns `${baseUrl}/${key}` (a browser-served
|
|
6
|
+
* path) instead of a `file://` URL. A trailing slash is trimmed.
|
|
7
|
+
*/
|
|
8
|
+
baseUrl?: string;
|
|
9
|
+
};
|
|
10
|
+
export declare class FileSystemStorage implements StorageAdapter {
|
|
11
|
+
private readonly opts;
|
|
12
|
+
constructor(opts: FileSystemStorageOptions);
|
|
13
|
+
put(blob: PutBlob): Promise<PutResult>;
|
|
14
|
+
}
|
|
15
|
+
/** Construct a filesystem `StorageAdapter` (uniform `xxxStorage(config)` shape). */
|
|
16
|
+
export declare function fileSystemStorage(opts: FileSystemStorageOptions): StorageAdapter;
|
|
17
|
+
export declare const packageName = "@airnauts/comments-storage-fs";
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAA;AAEnF,MAAM,MAAM,wBAAwB,GAAG;IACrC,OAAO,EAAE,MAAM,CAAA;IACf;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB,CAAA;AAwCD,qBAAa,iBAAkB,YAAW,cAAc;IAC1C,OAAO,CAAC,QAAQ,CAAC,IAAI;gBAAJ,IAAI,EAAE,wBAAwB;IAErD,GAAG,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC;CAc7C;AAED,oFAAoF;AACpF,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,wBAAwB,GAAG,cAAc,CAEhF;AAED,eAAO,MAAM,WAAW,kCAAkC,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { randomBytes } from "crypto";
|
|
3
|
+
import { mkdir, writeFile } from "fs/promises";
|
|
4
|
+
import { join, posix } from "path";
|
|
5
|
+
import { pathToFileURL } from "url";
|
|
6
|
+
function sanitizeName(name) {
|
|
7
|
+
const cleaned = name.replace(/[^A-Za-z0-9._-]/g, "_").slice(0, 200);
|
|
8
|
+
return cleaned.length > 0 ? cleaned : "file";
|
|
9
|
+
}
|
|
10
|
+
function uniqueKey(name) {
|
|
11
|
+
const ts = Date.now().toString(36);
|
|
12
|
+
const rand = randomBytes(6).toString("hex");
|
|
13
|
+
const safe = sanitizeName(name);
|
|
14
|
+
return posix.join(ts, `${rand}-${safe}`);
|
|
15
|
+
}
|
|
16
|
+
async function readAllBytes(data) {
|
|
17
|
+
if (data instanceof Uint8Array) return data;
|
|
18
|
+
const reader = data.getReader();
|
|
19
|
+
const chunks = [];
|
|
20
|
+
let total = 0;
|
|
21
|
+
try {
|
|
22
|
+
for (; ; ) {
|
|
23
|
+
const { value, done } = await reader.read();
|
|
24
|
+
if (done) break;
|
|
25
|
+
if (value) {
|
|
26
|
+
chunks.push(value);
|
|
27
|
+
total += value.byteLength;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
} finally {
|
|
31
|
+
reader.releaseLock();
|
|
32
|
+
}
|
|
33
|
+
const out = new Uint8Array(total);
|
|
34
|
+
let offset = 0;
|
|
35
|
+
for (const c of chunks) {
|
|
36
|
+
out.set(c, offset);
|
|
37
|
+
offset += c.byteLength;
|
|
38
|
+
}
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
var FileSystemStorage = class {
|
|
42
|
+
constructor(opts) {
|
|
43
|
+
this.opts = opts;
|
|
44
|
+
}
|
|
45
|
+
opts;
|
|
46
|
+
async put(blob) {
|
|
47
|
+
const key = uniqueKey(blob.name);
|
|
48
|
+
const abs = join(this.opts.rootDir, key);
|
|
49
|
+
await mkdir(join(abs, ".."), { recursive: true });
|
|
50
|
+
const bytes = await readAllBytes(blob.data);
|
|
51
|
+
await writeFile(abs, bytes);
|
|
52
|
+
return {
|
|
53
|
+
key,
|
|
54
|
+
url: this.opts.baseUrl ? `${this.opts.baseUrl.replace(/\/$/, "")}/${key}` : pathToFileURL(abs).href,
|
|
55
|
+
size: bytes.byteLength
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
function fileSystemStorage(opts) {
|
|
60
|
+
return new FileSystemStorage(opts);
|
|
61
|
+
}
|
|
62
|
+
var packageName = "@airnauts/comments-storage-fs";
|
|
63
|
+
export {
|
|
64
|
+
FileSystemStorage,
|
|
65
|
+
fileSystemStorage,
|
|
66
|
+
packageName
|
|
67
|
+
};
|
|
68
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { randomBytes } from 'node:crypto'\nimport { mkdir, writeFile } from 'node:fs/promises'\nimport { join, posix } from 'node:path'\nimport { pathToFileURL } from 'node:url'\nimport type { PutBlob, PutResult, StorageAdapter } from '@airnauts/comments-server'\n\nexport type FileSystemStorageOptions = {\n rootDir: string\n /**\n * Public URL base. When set, `put` returns `${baseUrl}/${key}` (a browser-served\n * path) instead of a `file://` URL. A trailing slash is trimmed.\n */\n baseUrl?: string\n}\n\nfunction sanitizeName(name: string): string {\n const cleaned = name.replace(/[^A-Za-z0-9._-]/g, '_').slice(0, 200)\n return cleaned.length > 0 ? cleaned : 'file'\n}\n\nfunction uniqueKey(name: string): string {\n const ts = Date.now().toString(36)\n const rand = randomBytes(6).toString('hex')\n const safe = sanitizeName(name)\n return posix.join(ts, `${rand}-${safe}`)\n}\n\nasync function readAllBytes(data: Uint8Array | ReadableStream<Uint8Array>): Promise<Uint8Array> {\n if (data instanceof Uint8Array) return data\n const reader = data.getReader()\n const chunks: Uint8Array[] = []\n let total = 0\n try {\n for (;;) {\n const { value, done } = await reader.read()\n if (done) break\n if (value) {\n chunks.push(value)\n total += value.byteLength\n }\n }\n } finally {\n reader.releaseLock()\n }\n const out = new Uint8Array(total)\n let offset = 0\n for (const c of chunks) {\n out.set(c, offset)\n offset += c.byteLength\n }\n return out\n}\n\nexport class FileSystemStorage implements StorageAdapter {\n constructor(private readonly opts: FileSystemStorageOptions) {}\n\n async put(blob: PutBlob): Promise<PutResult> {\n const key = uniqueKey(blob.name)\n const abs = join(this.opts.rootDir, key)\n await mkdir(join(abs, '..'), { recursive: true })\n const bytes = await readAllBytes(blob.data)\n await writeFile(abs, bytes)\n return {\n key,\n url: this.opts.baseUrl\n ? `${this.opts.baseUrl.replace(/\\/$/, '')}/${key}`\n : pathToFileURL(abs).href,\n size: bytes.byteLength,\n }\n }\n}\n\n/** Construct a filesystem `StorageAdapter` (uniform `xxxStorage(config)` shape). */\nexport function fileSystemStorage(opts: FileSystemStorageOptions): StorageAdapter {\n return new FileSystemStorage(opts)\n}\n\nexport const packageName = '@airnauts/comments-storage-fs'\n"],"mappings":";AAAA,SAAS,mBAAmB;AAC5B,SAAS,OAAO,iBAAi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I;AAC1C,UAAM,UAAU,KAAK,KAAK;AAC1B,WAAO;AAAA,MACL;AAAA,MACA,KAAK,KAAK,KAAK,UACX,GAAG,KAAK,KAAK,QAAQ,QAAQ,OAAO,EAAE,CAAC,IAAI,GAAG,KAC9C,cAAc,GAAG,EAAE;AAAA,MACvB,MAAM,MAAM;AAAA,IACd;AAAA,EACF;AACF;AAGO,SAAS,kBAAkB,MAAgD;AAChF,SAAO,IAAI,kBAAkB,IAAI;AACnC;AAEO,IAAM,cAAc;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@airnauts/comments-storage-fs",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Filesystem attachment-storage adapter for the Airnauts commenting tool server.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"comments",
|
|
7
|
+
"commenting",
|
|
8
|
+
"annotations",
|
|
9
|
+
"feedback",
|
|
10
|
+
"airnauts",
|
|
11
|
+
"storage",
|
|
12
|
+
"filesystem"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": "Airnauts",
|
|
16
|
+
"homepage": "https://github.com/Airnauts/commenting-tool#readme",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/Airnauts/commenting-tool.git",
|
|
20
|
+
"directory": "packages/storage-fs"
|
|
21
|
+
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/Airnauts/commenting-tool/issues"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"import": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"!dist/.tsbuildinfo",
|
|
35
|
+
"README.md",
|
|
36
|
+
"LICENSE"
|
|
37
|
+
],
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@airnauts/comments-core": "^0.1.0",
|
|
43
|
+
"@airnauts/comments-server": "^0.1.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@airnauts/comments-test-support": "0.0.0"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "tsup && tsc --build --force",
|
|
50
|
+
"typecheck": "tsc --build",
|
|
51
|
+
"test": "vitest run"
|
|
52
|
+
}
|
|
53
|
+
}
|