refactor: dynamically import Node.js modules in file system storage adapter
Refactored `fileSystemStorage` to dynamically import `fs` and `path` modules, improving compatibility with various environments. Updated initialization logic to set paths post-import. Added error handling for failed imports and enhanced type annotations for better maintainability.
This commit is contained in:
parent
f73d3b0c97
commit
1bcfaa96c8
1 changed files with 33 additions and 11 deletions
|
|
@ -1,7 +1,9 @@
|
||||||
import fs from 'fs'
|
|
||||||
import path from 'path'
|
|
||||||
import { Edge, HNSWNode, StorageAdapter } from '../coreTypes.js'
|
import { Edge, HNSWNode, StorageAdapter } from '../coreTypes.js'
|
||||||
|
|
||||||
|
// We'll dynamically import Node.js built-in modules
|
||||||
|
let fs: any
|
||||||
|
let path: any
|
||||||
|
|
||||||
// Constants for directory and file names
|
// Constants for directory and file names
|
||||||
const ROOT_DIR = 'brainy-data'
|
const ROOT_DIR = 'brainy-data'
|
||||||
const NODES_DIR = 'nodes'
|
const NODES_DIR = 'nodes'
|
||||||
|
|
@ -19,11 +21,11 @@ export class FileSystemStorage implements StorageAdapter {
|
||||||
private isInitialized = false
|
private isInitialized = false
|
||||||
|
|
||||||
constructor(rootDirectory?: string) {
|
constructor(rootDirectory?: string) {
|
||||||
// Use the provided root directory or default to the current working directory
|
// We'll set the paths in the init method after dynamically importing the modules
|
||||||
this.rootDir = rootDirectory ? path.resolve(rootDirectory, ROOT_DIR) : path.resolve(process.cwd(), ROOT_DIR)
|
this.rootDir = rootDirectory || ''
|
||||||
this.nodesDir = path.join(this.rootDir, NODES_DIR)
|
this.nodesDir = ''
|
||||||
this.edgesDir = path.join(this.rootDir, EDGES_DIR)
|
this.edgesDir = ''
|
||||||
this.metadataDir = path.join(this.rootDir, METADATA_DIR)
|
this.metadataDir = ''
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -35,6 +37,26 @@ export class FileSystemStorage implements StorageAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Dynamically import Node.js built-in modules
|
||||||
|
try {
|
||||||
|
// Import the modules
|
||||||
|
const fsModule = await import('fs')
|
||||||
|
const pathModule = await import('path')
|
||||||
|
|
||||||
|
// Assign to our module-level variables
|
||||||
|
fs = fsModule.default || fsModule
|
||||||
|
path = pathModule.default || pathModule
|
||||||
|
|
||||||
|
// Now set up the directory paths
|
||||||
|
const rootDir = this.rootDir || process.cwd()
|
||||||
|
this.rootDir = path.resolve(rootDir, ROOT_DIR)
|
||||||
|
this.nodesDir = path.join(this.rootDir, NODES_DIR)
|
||||||
|
this.edgesDir = path.join(this.rootDir, EDGES_DIR)
|
||||||
|
this.metadataDir = path.join(this.rootDir, METADATA_DIR)
|
||||||
|
} catch (importError) {
|
||||||
|
throw new Error(`Failed to import Node.js modules: ${importError}. This adapter requires a Node.js environment.`)
|
||||||
|
}
|
||||||
|
|
||||||
// Create directories if they don't exist
|
// Create directories if they don't exist
|
||||||
await this.ensureDirectoryExists(this.rootDir)
|
await this.ensureDirectoryExists(this.rootDir)
|
||||||
await this.ensureDirectoryExists(this.nodesDir)
|
await this.ensureDirectoryExists(this.nodesDir)
|
||||||
|
|
@ -118,8 +140,8 @@ export class FileSystemStorage implements StorageAdapter {
|
||||||
try {
|
try {
|
||||||
const files = await fs.promises.readdir(this.nodesDir)
|
const files = await fs.promises.readdir(this.nodesDir)
|
||||||
const nodePromises = files
|
const nodePromises = files
|
||||||
.filter(file => file.endsWith('.json'))
|
.filter((file: string) => file.endsWith('.json'))
|
||||||
.map(file => {
|
.map((file: string) => {
|
||||||
const id = path.basename(file, '.json')
|
const id = path.basename(file, '.json')
|
||||||
return this.getNode(id)
|
return this.getNode(id)
|
||||||
})
|
})
|
||||||
|
|
@ -230,8 +252,8 @@ export class FileSystemStorage implements StorageAdapter {
|
||||||
try {
|
try {
|
||||||
const files = await fs.promises.readdir(this.edgesDir)
|
const files = await fs.promises.readdir(this.edgesDir)
|
||||||
const edgePromises = files
|
const edgePromises = files
|
||||||
.filter(file => file.endsWith('.json'))
|
.filter((file: string) => file.endsWith('.json'))
|
||||||
.map(file => {
|
.map((file: string) => {
|
||||||
const id = path.basename(file, '.json')
|
const id = path.basename(file, '.json')
|
||||||
return this.getEdge(id)
|
return this.getEdge(id)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue