@haverstack/blob-adapter-disk 0.6.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,7 @@
1
+ CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
2
+
3
+ To the extent possible under law, the Haverstack contributors have waived all
4
+ copyright and related or neighboring rights to Haverstack. This work is
5
+ published from the United States.
6
+
7
+ https://creativecommons.org/publicdomain/zero/1.0/
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Haverstack — Disk Blob Adapter
3
+ * -------------------------------------------------------
4
+ * Implements StackBlobAdapter by storing content-addressed
5
+ * blobs on the local filesystem. File IDs are SHA-256 hashes
6
+ * of the content, enabling deduplication: if a file with the
7
+ * same hash already exists it is not overwritten.
8
+ */
9
+ import type { StackBlobAdapter, FileId } from '@haverstack/core';
10
+ export declare class DiskBlobAdapter implements StackBlobAdapter {
11
+ private readonly dir;
12
+ constructor(dir: string);
13
+ putAttachment(data: Uint8Array): Promise<FileId>;
14
+ getAttachment(fileId: FileId): Promise<Uint8Array>;
15
+ deleteAttachment(fileId: FileId): Promise<void>;
16
+ }
17
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAMH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAUjE,qBAAa,eAAgB,YAAW,gBAAgB;IAC1C,OAAO,CAAC,QAAQ,CAAC,GAAG;gBAAH,GAAG,EAAE,MAAM;IAIlC,aAAa,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC;IAQhD,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IAMlD,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAQtD"}
package/dist/index.js ADDED
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Haverstack — Disk Blob Adapter
3
+ * -------------------------------------------------------
4
+ * Implements StackBlobAdapter by storing content-addressed
5
+ * blobs on the local filesystem. File IDs are SHA-256 hashes
6
+ * of the content, enabling deduplication: if a file with the
7
+ * same hash already exists it is not overwritten.
8
+ */
9
+ import { createHash } from 'node:crypto';
10
+ import { mkdirSync, existsSync } from 'fs';
11
+ import { readFile, writeFile, unlink } from 'fs/promises';
12
+ import { join } from 'path';
13
+ const SHA256_HEX_RE = /^[0-9a-f]{64}$/;
14
+ const assertFileId = (fileId) => {
15
+ if (!SHA256_HEX_RE.test(fileId)) {
16
+ throw new Error(`Invalid fileId: expected 64-character lowercase hex string`);
17
+ }
18
+ };
19
+ export class DiskBlobAdapter {
20
+ dir;
21
+ constructor(dir) {
22
+ this.dir = dir;
23
+ mkdirSync(dir, { recursive: true });
24
+ }
25
+ async putAttachment(data) {
26
+ const fileId = createHash('sha256').update(data).digest('hex');
27
+ if (!existsSync(join(this.dir, fileId))) {
28
+ await writeFile(join(this.dir, fileId), data);
29
+ }
30
+ return fileId;
31
+ }
32
+ async getAttachment(fileId) {
33
+ assertFileId(fileId);
34
+ if (!existsSync(join(this.dir, fileId)))
35
+ throw new Error(`Attachment not found: "${fileId}"`);
36
+ return readFile(join(this.dir, fileId));
37
+ }
38
+ async deleteAttachment(fileId) {
39
+ assertFileId(fileId);
40
+ try {
41
+ await unlink(join(this.dir, fileId));
42
+ }
43
+ catch {
44
+ // Non-fatal — file may already be gone.
45
+ }
46
+ }
47
+ }
48
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAC3C,O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KAAK,CAAC,gBAAgB,CAAC,MAAc;QACnC,YAAY,CAAC,MAAM,CAAC,CAAC;QACrB,IAAI,CAAC;YACH,MAAM,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,CAAC;QACvC,CAAC;QAAC,MAAM,CAAC;YACP,wCAAwC;QAC1C,CAAC;IACH,CAAC;CACF"}
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@haverstack/blob-adapter-disk",
3
+ "version": "0.6.0",
4
+ "description": "Disk blob adapter for Haverstack",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "import": "./dist/index.js",
9
+ "types": "./dist/index.d.ts"
10
+ }
11
+ },
12
+ "main": "./dist/index.js",
13
+ "types": "./dist/index.d.ts",
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/haverstack/core",
20
+ "directory": "packages/blob-adapter-disk"
21
+ },
22
+ "license": "CC0-1.0",
23
+ "keywords": [
24
+ "haverstack",
25
+ "disk",
26
+ "blob",
27
+ "adapter",
28
+ "personal data",
29
+ "storage"
30
+ ],
31
+ "dependencies": {
32
+ "@haverstack/core": "0.6.0"
33
+ },
34
+ "devDependencies": {
35
+ "@types/node": "^22.0.0",
36
+ "typescript": "^5.5.0",
37
+ "vitest": "^2.0.0"
38
+ },
39
+ "scripts": {
40
+ "build": "tsc -p tsconfig.build.json",
41
+ "test": "vitest run",
42
+ "typecheck": "tsc --noEmit",
43
+ "lint": "eslint src tests"
44
+ }
45
+ }