From af4319090c2ca95951b0933b03033d478ae80874 Mon Sep 17 00:00:00 2001 From: David Antoon Date: Fri, 19 Jun 2026 01:27:39 +0300 Subject: [PATCH 1/2] Add worker and browser entry points to package.json --- libs/vectoriadb/package.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libs/vectoriadb/package.json b/libs/vectoriadb/package.json index 7a9bd76..71a234c 100644 --- a/libs/vectoriadb/package.json +++ b/libs/vectoriadb/package.json @@ -33,6 +33,8 @@ ".": { "development": "./src/index.ts", "types": "./dist/src/index.d.ts", + "worker": "./dist/src/worker.js", + "browser": "./dist/src/worker.js", "import": "./dist/src/index.js", "default": "./dist/src/index.js" }, From 221c4fe46425e9edaf40db503ea61a3d229818c3 Mon Sep 17 00:00:00 2001 From: David Antoon Date: Fri, 19 Jun 2026 02:05:47 +0300 Subject: [PATCH 2/2] Refactor FileStorageAdapter to lazily load fs/promises and update package.json for browser/worker entry points --- libs/vectoriadb/package.json | 8 +++++-- libs/vectoriadb/src/storage/file.adapter.ts | 26 ++++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/libs/vectoriadb/package.json b/libs/vectoriadb/package.json index 71a234c..82a264d 100644 --- a/libs/vectoriadb/package.json +++ b/libs/vectoriadb/package.json @@ -33,8 +33,6 @@ ".": { "development": "./src/index.ts", "types": "./dist/src/index.d.ts", - "worker": "./dist/src/worker.js", - "browser": "./dist/src/worker.js", "import": "./dist/src/index.js", "default": "./dist/src/index.js" }, @@ -43,6 +41,12 @@ "types": "./dist/src/worker.d.ts", "import": "./dist/src/worker.js", "default": "./dist/src/worker.js" + }, + "./browser": { + "development": "./src/worker.ts", + "types": "./dist/src/worker.d.ts", + "import": "./dist/src/worker.js", + "default": "./dist/src/worker.js" } }, "peerDependencies": { diff --git a/libs/vectoriadb/src/storage/file.adapter.ts b/libs/vectoriadb/src/storage/file.adapter.ts index 5e759f5..90492ca 100644 --- a/libs/vectoriadb/src/storage/file.adapter.ts +++ b/libs/vectoriadb/src/storage/file.adapter.ts @@ -1,4 +1,3 @@ -import * as fs from 'fs/promises'; import * as path from 'path'; import type { DocumentMetadata } from '../interfaces'; import type { StorageAdapterConfig, StoredData } from './adapter.interface'; @@ -28,9 +27,30 @@ export interface FileStorageConfig extends StorageAdapterConfig { * Perfect for local development to avoid recalculating embeddings */ export class FileStorageAdapter extends BaseStorageAdapter { + // Cached `fs/promises` module. Loaded lazily (see `getFs`) so that importing + // the package root does not force browser/worker bundlers to resolve + // `node:fs`. This is a Node-only adapter; `path` stays a static import because + // it is pure JS, tree-shakeable, and needed synchronously in the constructor. + private static _fs: typeof import('fs/promises') | null = null; + private fileConfig: Required>; private filePath: string; + /** + * Lazily import `fs/promises`. Keeping this off the top-level import graph + * means importing the package root never forces a bundler to statically + * resolve `node:fs`; the module is only loaded when this Node-only adapter is + * actually used. The adapter throws naturally if `fs` is unavailable at runtime. + */ + private async getFs(): Promise { + if (FileStorageAdapter._fs) { + return FileStorageAdapter._fs; + } + const mod = await import('fs/promises'); + FileStorageAdapter._fs = mod; + return mod; + } + constructor(config: FileStorageConfig = {}) { super(config); @@ -86,6 +106,7 @@ export class FileStorageAdapter e override async initialize(): Promise { // Ensure cache directory exists + const fs = await this.getFs(); const dir = path.dirname(this.filePath); try { await fs.mkdir(dir, { recursive: true }); @@ -101,6 +122,7 @@ export class FileStorageAdapter e override async load(): Promise | null> { try { + const fs = await this.getFs(); const content = await fs.readFile(this.filePath, 'utf-8'); return this.safeJsonParse>(content); } catch { @@ -115,6 +137,7 @@ export class FileStorageAdapter e if (!content) { throw new StorageError('Failed to serialize embeddings data'); } + const fs = await this.getFs(); await fs.writeFile(this.filePath, content, 'utf-8'); } catch (error) { if (error instanceof StorageError) { @@ -129,6 +152,7 @@ export class FileStorageAdapter e override async clear(): Promise { try { + const fs = await this.getFs(); await fs.unlink(this.filePath); } catch { // File doesn't exist, ignore