@noy-db/to-icloud 0.2.0 → 0.3.0-pre.1
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/dist/index.d.ts +2 -2
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -66,7 +66,7 @@ interface ICloudStoreOptions {
|
|
|
66
66
|
* Build a `NoydbPodStore` over an iCloud Drive folder. Wrap with
|
|
67
67
|
* `wrapBundleStore()` to consume via `createNoydb({ store })`.
|
|
68
68
|
*/
|
|
69
|
-
declare function
|
|
69
|
+
declare function toIcloud(options: ICloudStoreOptions): NoydbPodStore;
|
|
70
70
|
/**
|
|
71
71
|
* Wire a Node `fs/promises` implementation as an `ICloudFs`. Dynamic
|
|
72
72
|
* import keeps the package browser-loadable (the caller's bundler
|
|
@@ -74,4 +74,4 @@ declare function icloud(options: ICloudStoreOptions): NoydbPodStore;
|
|
|
74
74
|
*/
|
|
75
75
|
declare function nodeFs(): Promise<ICloudFs>;
|
|
76
76
|
|
|
77
|
-
export { DEFAULT_FOLDER, type ICloudFs, type ICloudStoreOptions,
|
|
77
|
+
export { DEFAULT_FOLDER, type ICloudFs, type ICloudStoreOptions, nodeFs, toIcloud };
|
package/dist/index.js
CHANGED
|
@@ -10,7 +10,7 @@ function isStub(name) {
|
|
|
10
10
|
function isConflictCopy(name, expected) {
|
|
11
11
|
return name.includes("'s conflicted copy") || name.includes("(conflicted copy") && name.endsWith(expected.slice(expected.lastIndexOf(".")));
|
|
12
12
|
}
|
|
13
|
-
function
|
|
13
|
+
function toIcloud(options) {
|
|
14
14
|
const suffix = options.suffix ?? ".noydb";
|
|
15
15
|
const dir = options.folder.replace(/\/+$/, "");
|
|
16
16
|
const { fs } = options;
|
|
@@ -142,7 +142,7 @@ async function nodeFs() {
|
|
|
142
142
|
}
|
|
143
143
|
export {
|
|
144
144
|
DEFAULT_FOLDER,
|
|
145
|
-
|
|
146
|
-
|
|
145
|
+
nodeFs,
|
|
146
|
+
toIcloud
|
|
147
147
|
};
|
|
148
148
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * **@noy-db/to-icloud** — iCloud Drive bundle store for noy-db.\n *\n * Treats each vault as a single `.noydb` bundle stored under\n * `~/Library/Mobile Documents/…` (or any user-chosen iCloud path).\n * Pair with `wrapBundleStore()` from `@noy-db/hub` to get the\n * standard six-method `NoydbStore` surface.\n *\n * ## Why a dedicated package if `to-file` \"works\"?\n *\n * `@noy-db/to-file` pointed at an iCloud Drive directory technically\n * works — until one of three iCloud-specific behaviors bites you:\n *\n * 1. **On-demand eviction.** iCloud may evict the file to cloud-only\n * storage, leaving a `.icloud` stub. `readFile()` on the stub\n * either throws ENOENT or returns stub metadata. This store\n * detects the stub, nudges the OS to redownload (via `xattr` on\n * macOS), and retries.\n * 2. **Conflict files.** Parallel writes from two devices create\n * `name (device conflicted copy DATE).noydb`. This store detects\n * those files and raises `PodVersionConflictError`, giving the\n * caller a chance to merge deliberately.\n * 3. **Sync-not-yet-complete writes.** A completed `writeFile()` does\n * not mean the bytes are on Apple's servers. `ping()` reports\n * on upload status so callers can wait before considering a\n * write durable.\n *\n * ## Scope\n *\n * - **Node (macOS) only for v1.** Browser / iOS consumers go through\n * CloudKit JS — a separate package will ship as `@noy-db/to-cloudkit`.\n * - **Bundle granularity** — whole vault in one file. Pair with\n * `syncPolicy: { push: { mode: 'debounce', 30_000 } }` so every\n * record mutation doesn't trigger a full bundle upload.\n * - **No extra auth** — iCloud syncs via the user's signed-in Apple ID;\n * the store never sees credentials.\n *\n * @packageDocumentation\n */\n\nimport type { NoydbPodStore } from '@noy-db/hub/to'\nimport { PodVersionConflictError } from '@noy-db/hub/to'\n\n/** Default iCloud Drive folder name inside a user's mobile-documents tree. */\nexport const DEFAULT_FOLDER = 'NoyDB'\n\nexport interface ICloudFs {\n readFile(path: string): Promise<Uint8Array | Buffer | null>\n writeFile(path: string, data: Uint8Array): Promise<void>\n unlink(path: string): Promise<void>\n readdir(path: string): Promise<string[]>\n stat(path: string): Promise<{ mtimeMs: number; size: number } | null>\n /** macOS-only: force iCloud to materialise an evicted `.icloud` stub. */\n triggerDownload?(path: string): Promise<void>\n}\n\nexport interface ICloudStoreOptions {\n /** Absolute path to the iCloud Drive folder that holds bundles. */\n readonly folder: string\n /** File-system facade — swap in a mock or a cross-platform shim. */\n readonly fs: ICloudFs\n /** Bundle filename suffix. Default `'.noydb'`. */\n readonly suffix?: string\n}\n\nfunction fileName(vault: string, suffix: string): string {\n return `${vault}${suffix}`\n}\n\nfunction isStub(name: string): boolean {\n // macOS writes `<name>.icloud` stubs when offloading files.\n return name.endsWith('.icloud')\n}\n\nfunction isConflictCopy(name: string, expected: string): boolean {\n // Canonical shape: `foo (device conflicted copy DATE).noydb`\n return name.includes(\"'s conflicted copy\") ||\n (name.includes('(conflicted copy') && name.endsWith(expected.slice(expected.lastIndexOf('.'))))\n}\n\n/**\n * Build a `NoydbPodStore` over an iCloud Drive folder. Wrap with\n * `wrapBundleStore()` to consume via `createNoydb({ store })`.\n */\nexport function icloud(options: ICloudStoreOptions): NoydbPodStore {\n const suffix = options.suffix ?? '.noydb'\n const dir = options.folder.replace(/\\/+$/, '')\n const { fs } = options\n\n async function pathFor(vault: string): Promise<string> {\n return `${dir}/${fileName(vault, suffix)}`\n }\n\n async function stubAwareRead(path: string): Promise<Uint8Array | null> {\n // Direct read first.\n let bytes = await fs.readFile(path)\n if (bytes !== null) return bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes)\n\n // Check for an adjacent `.icloud` stub and try to materialise it.\n const stubPath = `${path}.icloud`\n const stubExists = await fs.stat(stubPath)\n if (!stubExists) return null\n if (fs.triggerDownload) {\n await fs.triggerDownload(stubPath)\n bytes = await fs.readFile(path)\n if (bytes !== null) return bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes)\n }\n // Still offloaded. Surface a clean error so the caller can retry\n // or invite the user to open iCloud.\n throw new Error(\n `iCloud file \"${path}\" is offloaded. Open the file in Finder to trigger a download, ` +\n `or install an OS-level trigger (\\`brctl download \"${path}\"\\`).`,\n )\n }\n\n async function detectConflict(vault: string): Promise<string | null> {\n const target = fileName(vault, suffix)\n const entries = await fs.readdir(dir).catch(() => [] as string[])\n for (const entry of entries) {\n if (isConflictCopy(entry, target)) return entry\n }\n return null\n }\n\n function versionOf(stat: { mtimeMs: number; size: number }): string {\n // mtime-based opaque token — sufficient for OCC within a single\n // syncing agent. iCloud's native conflict file machinery provides\n // the cross-agent guard.\n return `${stat.mtimeMs}-${stat.size}`\n }\n\n return {\n kind: 'bundle',\n name: 'icloud',\n\n async readBundle(vaultId) {\n const path = await pathFor(vaultId)\n const stat = await fs.stat(path)\n if (!stat) {\n // Perhaps only a stub exists — detect and trigger.\n const stubStat = await fs.stat(`${path}.icloud`)\n if (!stubStat) return null\n const bytes = await stubAwareRead(path)\n if (!bytes) return null\n const redoStat = await fs.stat(path)\n if (!redoStat) throw new Error(`icloud: file vanished after download at ${path}`)\n return { bytes, version: versionOf(redoStat) }\n }\n const bytes = await stubAwareRead(path)\n if (!bytes) return null\n return { bytes, version: versionOf(stat) }\n },\n\n async writeBundle(vaultId, bytes, expectedVersion) {\n // Conflict file present → refuse; caller merges.\n const conflict = await detectConflict(vaultId)\n if (conflict) {\n throw new PodVersionConflictError(\n `iCloud conflict file detected alongside \"${vaultId}${suffix}\": \"${conflict}\". ` +\n `Open iCloud Drive, resolve the conflict, then retry.`,\n )\n }\n const path = await pathFor(vaultId)\n const stat = await fs.stat(path)\n const current = stat ? versionOf(stat) : null\n if (expectedVersion !== null && current !== null && expectedVersion !== current) {\n throw new PodVersionConflictError(\n `iCloud bundle version mismatch: expected ${expectedVersion}, found ${current}`,\n )\n }\n await fs.writeFile(path, bytes)\n const newStat = await fs.stat(path)\n if (!newStat) throw new Error(`icloud: write reported success but stat failed at ${path}`)\n return { version: versionOf(newStat) }\n },\n\n async deleteBundle(vaultId) {\n const path = await pathFor(vaultId)\n try {\n await fs.unlink(path)\n } catch {\n // Idempotent\n }\n },\n\n async listBundles() {\n const entries = await fs.readdir(dir).catch(() => [] as string[])\n const out: Array<{ vaultId: string; version: string; size: number }> = []\n for (const entry of entries) {\n if (!entry.endsWith(suffix) || isStub(entry)) continue\n const vaultId = entry.slice(0, -suffix.length)\n const stat = await fs.stat(`${dir}/${entry}`)\n if (!stat) continue\n out.push({ vaultId, version: versionOf(stat), size: stat.size })\n }\n return out\n },\n }\n}\n\n/**\n * Wire a Node `fs/promises` implementation as an `ICloudFs`. Dynamic\n * import keeps the package browser-loadable (the caller's bundler\n * prunes the Node path).\n */\nexport async function nodeFs(): Promise<ICloudFs> {\n const fs = await import('node:fs/promises')\n const { spawn } = await import('node:child_process')\n return {\n async readFile(path) {\n try {\n return await fs.readFile(path)\n } catch {\n return null\n }\n },\n async writeFile(path, data) {\n await fs.writeFile(path, data)\n },\n async unlink(path) {\n await fs.unlink(path).catch(() => undefined)\n },\n async readdir(path) {\n return fs.readdir(path)\n },\n async stat(path) {\n try {\n const s = await fs.stat(path)\n return { mtimeMs: s.mtimeMs, size: s.size }\n } catch {\n return null\n }\n },\n async triggerDownload(path) {\n // macOS: `brctl download` forces iCloud to materialise a stub.\n await new Promise<void>((resolve) => {\n const child = spawn('brctl', ['download', path.replace(/\\.icloud$/, '')])\n child.on('close', () => resolve())\n child.on('error', () => resolve())\n })\n },\n }\n}\n"],"mappings":";AAyCA,SAAS,+BAA+B;AAGjC,IAAM,iBAAiB;AAqB9B,SAAS,SAAS,OAAe,QAAwB;AACvD,SAAO,GAAG,KAAK,GAAG,MAAM;AAC1B;AAEA,SAAS,OAAO,MAAuB;AAErC,SAAO,KAAK,SAAS,SAAS;AAChC;AAEA,SAAS,eAAe,MAAc,UAA2B;AAE/D,SAAO,KAAK,SAAS,oBAAoB,KACjC,KAAK,SAAS,kBAAkB,K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C,QAAQ;AACN,eAAO;AAAA,MACT;AAAA,IACF;AAAA,IACA,MAAM,gBAAgB,MAAM;AAE1B,YAAM,IAAI,QAAc,CAAC,YAAY;AACnC,cAAM,QAAQ,MAAM,SAAS,CAAC,YAAY,KAAK,QAAQ,aAAa,EAAE,CAAC,CAAC;AACxE,cAAM,GAAG,SAAS,MAAM,QAAQ,CAAC;AACjC,cAAM,GAAG,SAAS,MAAM,QAAQ,CAAC;AAAA,MACnC,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["bytes"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * **@noy-db/to-icloud** — iCloud Drive bundle store for noy-db.\n *\n * Treats each vault as a single `.noydb` bundle stored under\n * `~/Library/Mobile Documents/…` (or any user-chosen iCloud path).\n * Pair with `wrapBundleStore()` from `@noy-db/hub` to get the\n * standard six-method `NoydbStore` surface.\n *\n * ## Why a dedicated package if `to-file` \"works\"?\n *\n * `@noy-db/to-file` pointed at an iCloud Drive directory technically\n * works — until one of three iCloud-specific behaviors bites you:\n *\n * 1. **On-demand eviction.** iCloud may evict the file to cloud-only\n * storage, leaving a `.icloud` stub. `readFile()` on the stub\n * either throws ENOENT or returns stub metadata. This store\n * detects the stub, nudges the OS to redownload (via `xattr` on\n * macOS), and retries.\n * 2. **Conflict files.** Parallel writes from two devices create\n * `name (device conflicted copy DATE).noydb`. This store detects\n * those files and raises `PodVersionConflictError`, giving the\n * caller a chance to merge deliberately.\n * 3. **Sync-not-yet-complete writes.** A completed `writeFile()` does\n * not mean the bytes are on Apple's servers. `ping()` reports\n * on upload status so callers can wait before considering a\n * write durable.\n *\n * ## Scope\n *\n * - **Node (macOS) only for v1.** Browser / iOS consumers go through\n * CloudKit JS — a separate package will ship as `@noy-db/to-cloudkit`.\n * - **Bundle granularity** — whole vault in one file. Pair with\n * `syncPolicy: { push: { mode: 'debounce', 30_000 } }` so every\n * record mutation doesn't trigger a full bundle upload.\n * - **No extra auth** — iCloud syncs via the user's signed-in Apple ID;\n * the store never sees credentials.\n *\n * @packageDocumentation\n */\n\nimport type { NoydbPodStore } from '@noy-db/hub/to'\nimport { PodVersionConflictError } from '@noy-db/hub/to'\n\n/** Default iCloud Drive folder name inside a user's mobile-documents tree. */\nexport const DEFAULT_FOLDER = 'NoyDB'\n\nexport interface ICloudFs {\n readFile(path: string): Promise<Uint8Array | Buffer | null>\n writeFile(path: string, data: Uint8Array): Promise<void>\n unlink(path: string): Promise<void>\n readdir(path: string): Promise<string[]>\n stat(path: string): Promise<{ mtimeMs: number; size: number } | null>\n /** macOS-only: force iCloud to materialise an evicted `.icloud` stub. */\n triggerDownload?(path: string): Promise<void>\n}\n\nexport interface ICloudStoreOptions {\n /** Absolute path to the iCloud Drive folder that holds bundles. */\n readonly folder: string\n /** File-system facade — swap in a mock or a cross-platform shim. */\n readonly fs: ICloudFs\n /** Bundle filename suffix. Default `'.noydb'`. */\n readonly suffix?: string\n}\n\nfunction fileName(vault: string, suffix: string): string {\n return `${vault}${suffix}`\n}\n\nfunction isStub(name: string): boolean {\n // macOS writes `<name>.icloud` stubs when offloading files.\n return name.endsWith('.icloud')\n}\n\nfunction isConflictCopy(name: string, expected: string): boolean {\n // Canonical shape: `foo (device conflicted copy DATE).noydb`\n return name.includes(\"'s conflicted copy\") ||\n (name.includes('(conflicted copy') && name.endsWith(expected.slice(expected.lastIndexOf('.'))))\n}\n\n/**\n * Build a `NoydbPodStore` over an iCloud Drive folder. Wrap with\n * `wrapBundleStore()` to consume via `createNoydb({ store })`.\n */\nexport function toIcloud(options: ICloudStoreOptions): NoydbPodStore {\n const suffix = options.suffix ?? '.noydb'\n const dir = options.folder.replace(/\\/+$/, '')\n const { fs } = options\n\n async function pathFor(vault: string): Promise<string> {\n return `${dir}/${fileName(vault, suffix)}`\n }\n\n async function stubAwareRead(path: string): Promise<Uint8Array | null> {\n // Direct read first.\n let bytes = await fs.readFile(path)\n if (bytes !== null) return bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes)\n\n // Check for an adjacent `.icloud` stub and try to materialise it.\n const stubPath = `${path}.icloud`\n const stubExists = await fs.stat(stubPath)\n if (!stubExists) return null\n if (fs.triggerDownload) {\n await fs.triggerDownload(stubPath)\n bytes = await fs.readFile(path)\n if (bytes !== null) return bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes)\n }\n // Still offloaded. Surface a clean error so the caller can retry\n // or invite the user to open iCloud.\n throw new Error(\n `iCloud file \"${path}\" is offloaded. Open the file in Finder to trigger a download, ` +\n `or install an OS-level trigger (\\`brctl download \"${path}\"\\`).`,\n )\n }\n\n async function detectConflict(vault: string): Promise<string | null> {\n const target = fileName(vault, suffix)\n const entries = await fs.readdir(dir).catch(() => [] as string[])\n for (const entry of entries) {\n if (isConflictCopy(entry, target)) return entry\n }\n return null\n }\n\n function versionOf(stat: { mtimeMs: number; size: number }): string {\n // mtime-based opaque token — sufficient for OCC within a single\n // syncing agent. iCloud's native conflict file machinery provides\n // the cross-agent guard.\n return `${stat.mtimeMs}-${stat.size}`\n }\n\n return {\n kind: 'bundle',\n name: 'icloud',\n\n async readBundle(vaultId) {\n const path = await pathFor(vaultId)\n const stat = await fs.stat(path)\n if (!stat) {\n // Perhaps only a stub exists — detect and trigger.\n const stubStat = await fs.stat(`${path}.icloud`)\n if (!stubStat) return null\n const bytes = await stubAwareRead(path)\n if (!bytes) return null\n const redoStat = await fs.stat(path)\n if (!redoStat) throw new Error(`icloud: file vanished after download at ${path}`)\n return { bytes, version: versionOf(redoStat) }\n }\n const bytes = await stubAwareRead(path)\n if (!bytes) return null\n return { bytes, version: versionOf(stat) }\n },\n\n async writeBundle(vaultId, bytes, expectedVersion) {\n // Conflict file present → refuse; caller merges.\n const conflict = await detectConflict(vaultId)\n if (conflict) {\n throw new PodVersionConflictError(\n `iCloud conflict file detected alongside \"${vaultId}${suffix}\": \"${conflict}\". ` +\n `Open iCloud Drive, resolve the conflict, then retry.`,\n )\n }\n const path = await pathFor(vaultId)\n const stat = await fs.stat(path)\n const current = stat ? versionOf(stat) : null\n if (expectedVersion !== null && current !== null && expectedVersion !== current) {\n throw new PodVersionConflictError(\n `iCloud bundle version mismatch: expected ${expectedVersion}, found ${current}`,\n )\n }\n await fs.writeFile(path, bytes)\n const newStat = await fs.stat(path)\n if (!newStat) throw new Error(`icloud: write reported success but stat failed at ${path}`)\n return { version: versionOf(newStat) }\n },\n\n async deleteBundle(vaultId) {\n const path = await pathFor(vaultId)\n try {\n await fs.unlink(path)\n } catch {\n // Idempotent\n }\n },\n\n async listBundles() {\n const entries = await fs.readdir(dir).catch(() => [] as string[])\n const out: Array<{ vaultId: string; version: string; size: number }> = []\n for (const entry of entries) {\n if (!entry.endsWith(suffix) || isStub(entry)) continue\n const vaultId = entry.slice(0, -suffix.length)\n const stat = await fs.stat(`${dir}/${entry}`)\n if (!stat) continue\n out.push({ vaultId, version: versionOf(stat), size: stat.size })\n }\n return out\n },\n }\n}\n\n/**\n * Wire a Node `fs/promises` implementation as an `ICloudFs`. Dynamic\n * import keeps the package browser-loadable (the caller's bundler\n * prunes the Node path).\n */\nexport async function nodeFs(): Promise<ICloudFs> {\n const fs = await import('node:fs/promises')\n const { spawn } = await import('node:child_process')\n return {\n async readFile(path) {\n try {\n return await fs.readFile(path)\n } catch {\n return null\n }\n },\n async writeFile(path, data) {\n await fs.writeFile(path, data)\n },\n async unlink(path) {\n await fs.unlink(path).catch(() => undefined)\n },\n async readdir(path) {\n return fs.readdir(path)\n },\n async stat(path) {\n try {\n const s = await fs.stat(path)\n return { mtimeMs: s.mtimeMs, size: s.size }\n } catch {\n return null\n }\n },\n async triggerDownload(path) {\n // macOS: `brctl download` forces iCloud to materialise a stub.\n await new Promise<void>((resolve) => {\n const child = spawn('brctl', ['download', path.replace(/\\.icloud$/, '')])\n child.on('close', () => resolve())\n child.on('error', () => resolve())\n })\n },\n }\n}\n"],"mappings":";AAyCA,SAAS,+BAA+B;AAGjC,IAAM,iBAAiB;AAqB9B,SAAS,SAAS,OAAe,QAAw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Q,MAAM;AAClB,aAAO,GAAG,QAAQ,IAAI;AAAA,IACxB;AAAA,IACA,MAAM,KAAK,MAAM;AACf,UAAI;AACF,cAAM,IAAI,MAAM,GAAG,KAAK,IAAI;AAC5B,eAAO,EAAE,SAAS,EAAE,SAAS,MAAM,EAAE,KAAK;AAAA,MAC5C,QAAQ;AACN,eAAO;AAAA,MACT;AAAA,IACF;AAAA,IACA,MAAM,gBAAgB,MAAM;AAE1B,YAAM,IAAI,QAAc,CAAC,YAAY;AACnC,cAAM,QAAQ,MAAM,SAAS,CAAC,YAAY,KAAK,QAAQ,aAAa,EAAE,CAAC,CAAC;AACxE,cAAM,GAAG,SAAS,MAAM,QAAQ,CAAC;AACjC,cAAM,GAAG,SAAS,MAAM,QAAQ,CAAC;AAAA,MACnC,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["bytes"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noy-db/to-icloud",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0-pre.1",
|
|
4
4
|
"description": "iCloud Drive bundle store for noy-db — macOS-aware persistence that detects eviction stubs (.icloud) and conflict files automatically. Treats each vault as a single .noydb bundle, safe for the Apple ecosystem.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "vLannaAi <vicio@lanna.ai>",
|
|
@@ -31,10 +31,10 @@
|
|
|
31
31
|
"node": ">=22.0.0"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@noy-db/hub": "^0.3.0"
|
|
34
|
+
"@noy-db/hub": "^0.3.0 || ^0.4.0-pre.10"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@noy-db/hub": "0.
|
|
37
|
+
"@noy-db/hub": "0.4.0-pre.10",
|
|
38
38
|
"@types/node": "^22.0.0"
|
|
39
39
|
},
|
|
40
40
|
"keywords": [
|