feat: add browser environment compatibility support

- Remove top-level Node.js imports that break bundlers
- Use universal adapters for crypto operations
- Add dynamic imports for Node.js-specific modules
- Add browser field to package.json for bundler hints
- Maintain full Node.js functionality while enabling browser usage

This allows Brainy to be used with modern bundlers (Vite, Webpack, etc.)
without requiring Node.js polyfills. Browser environments get core features
while Node.js retains all capabilities including filesystem and networking.
This commit is contained in:
David Snelling 2025-09-17 15:48:02 -07:00
parent 2793cef197
commit c303ead318
7 changed files with 122 additions and 41 deletions

View file

@ -5,7 +5,7 @@
import { BaseAugmentation } from './brainyAugmentation.js'
import { AugmentationManifest } from './manifest.js'
import { createHash } from 'node:crypto'
import { createHash } from '../universal/crypto.js'
export interface AuditLogConfig {
enabled?: boolean

View file

@ -1,14 +1,34 @@
/**
* Local Augmentation Discovery
*
*
* Discovers augmentations installed locally in node_modules
* and built-in augmentations that ship with Brainy
*
* NOTE: This is a Node.js-only feature that requires filesystem access
*/
import { existsSync, readdirSync, readFileSync } from 'node:fs'
import { join } from 'node:path'
import { AugmentationManifest } from '../manifest.js'
// Node.js modules - dynamically imported to avoid bundler issues
let fs: any = null
let path: any = null
// Load Node.js modules if available
if (typeof window === 'undefined') {
try {
fs = await import('node:fs')
path = await import('node:path')
} catch (e) {
// Will throw error in methods if not available
}
}
// Create compatibility layer for sync methods
const existsSync = fs?.existsSync || (() => { throw new Error('Filesystem not available') })
const readdirSync = fs?.readdirSync || (() => { throw new Error('Filesystem not available') })
const readFileSync = fs?.readFileSync || (() => { throw new Error('Filesystem not available') })
const join = path?.join || ((...args: string[]) => args.join('/'))
export interface LocalAugmentation {
id: string
name: string