@human-synthesis/norns 0.0.16 → 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.
@@ -0,0 +1,97 @@
1
+ import { mkdirSync, readFileSync, rmSync, writeFileSync, existsSync, readdirSync } from 'node:fs';
2
+ import { dirname, join, normalize, sep } from 'node:path';
3
+
4
+ /**
5
+ * Storage behind `container.resolve('storage')` — backing for the `file`
6
+ * field type. Two adapters with one surface:
7
+ *
8
+ * put(key, data, { contentType? }) → { key }
9
+ * get(key) → { body: Uint8Array, contentType? } | null
10
+ * delete(key) → void
11
+ * list(prefix) → string[] (keys, sorted)
12
+ *
13
+ * Keys are `/`-separated paths (`orders/abc/invoice.pdf`).
14
+ */
15
+
16
+ /**
17
+ * Cloudflare R2 adapter over a bucket binding.
18
+ * @param {*} bucket R2Bucket binding
19
+ */
20
+ export function r2Storage(bucket) {
21
+ return {
22
+ async put(key, data, { contentType } = {}) {
23
+ await bucket.put(key, data, contentType ? { httpMetadata: { contentType } } : undefined);
24
+ return { key };
25
+ },
26
+ async get(key) {
27
+ const obj = await bucket.get(key);
28
+ if (!obj) return null;
29
+ return {
30
+ body: new Uint8Array(await obj.arrayBuffer()),
31
+ contentType: obj.httpMetadata?.contentType
32
+ };
33
+ },
34
+ async delete(key) {
35
+ await bucket.delete(key);
36
+ },
37
+ async list(prefix = '') {
38
+ const keys = [];
39
+ let cursor;
40
+ do {
41
+ const page = await bucket.list({ prefix, cursor });
42
+ for (const obj of page.objects) keys.push(obj.key);
43
+ cursor = page.truncated ? page.cursor : undefined;
44
+ } while (cursor);
45
+ return keys.sort();
46
+ }
47
+ };
48
+ }
49
+
50
+ /**
51
+ * Local-dir shim for `norns dev` / tests. Content types ride in a `.meta`
52
+ * sidecar next to each object.
53
+ * @param {string} root
54
+ */
55
+ export function dirStorage(root) {
56
+ const safe = (key) => {
57
+ const p = normalize(join(root, key));
58
+ if (!p.startsWith(normalize(root) + sep)) throw new Error(`storage: invalid key ${key}`);
59
+ return p;
60
+ };
61
+ return {
62
+ async put(key, data, { contentType } = {}) {
63
+ const path = safe(key);
64
+ mkdirSync(dirname(path), { recursive: true });
65
+ writeFileSync(path, typeof data === 'string' ? data : new Uint8Array(data));
66
+ if (contentType) writeFileSync(`${path}.meta`, contentType);
67
+ return { key };
68
+ },
69
+ async get(key) {
70
+ const path = safe(key);
71
+ if (!existsSync(path)) return null;
72
+ const meta = existsSync(`${path}.meta`) ? readFileSync(`${path}.meta`, 'utf8') : undefined;
73
+ return { body: new Uint8Array(readFileSync(path)), contentType: meta };
74
+ },
75
+ async delete(key) {
76
+ const path = safe(key);
77
+ rmSync(path, { force: true });
78
+ rmSync(`${path}.meta`, { force: true });
79
+ },
80
+ async list(prefix = '') {
81
+ if (!existsSync(root)) return [];
82
+ const out = [];
83
+ const walk = (dir) => {
84
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
85
+ const full = join(dir, entry.name);
86
+ if (entry.isDirectory()) walk(full);
87
+ else if (!entry.name.endsWith('.meta')) {
88
+ const key = full.slice(normalize(root).length + 1).split(sep).join('/');
89
+ if (key.startsWith(prefix)) out.push(key);
90
+ }
91
+ }
92
+ };
93
+ walk(root);
94
+ return out.sort();
95
+ }
96
+ };
97
+ }