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

@ -197,8 +197,8 @@ export class TransformerEmbedding implements EmbeddingModel {
const { createRequire } = await import('module')
const require = createRequire(import.meta.url)
const path = require('path')
const fs = require('fs')
const path = require('node:path')
const fs = require('node:fs')
// Try to resolve the package location
try {

View file

@ -7,7 +7,33 @@
import { FindParams, AddParams, UpdateParams, RelateParams } from '../types/brainy.types.js'
import { NounType, VerbType } from '../types/graphTypes.js'
import * as os from 'node:os'
// Dynamic import for Node.js os module
let os: any = null
if (typeof window === 'undefined') {
try {
os = await import('node:os')
} catch (e) {
// OS module not available
}
}
// Browser-safe memory detection
const getSystemMemory = (): number => {
if (os) {
return os.totalmem()
}
// Browser fallback: assume 4GB
return 4 * 1024 * 1024 * 1024
}
const getAvailableMemory = (): number => {
if (os) {
return os.freemem()
}
// Browser fallback: assume 2GB available
return 2 * 1024 * 1024 * 1024
}
/**
* Auto-configured limits based on system resources
@ -27,8 +53,8 @@ class ValidationConfig {
private constructor() {
// Auto-configure based on system resources
const totalMemory = os.totalmem()
const availableMemory = os.freemem()
const totalMemory = getSystemMemory()
const availableMemory = getAvailableMemory()
// Scale limits based on available memory
// 1GB = 10K limit, 8GB = 80K limit, etc.
@ -224,8 +250,8 @@ export function getValidationConfig() {
maxLimit: config.maxLimit,
maxQueryLength: config.maxQueryLength,
maxVectorDimensions: config.maxVectorDimensions,
systemMemory: os.totalmem(),
availableMemory: os.freemem()
systemMemory: getSystemMemory(),
availableMemory: getAvailableMemory()
}
}

View file

@ -5,8 +5,7 @@
*/
import { performance } from 'perf_hooks'
import { hostname } from 'node:os'
import { randomUUID } from 'node:crypto'
import { randomUUID } from '../universal/crypto.js'
export enum LogLevel {
SILENT = -1,
@ -312,10 +311,20 @@ export class StructuredLogger {
}
if (this.config.includeHost) {
entry.host = hostname()
// Use dynamic import for hostname (Node.js only)
if (typeof window === 'undefined') {
try {
const os = require('node:os')
entry.host = os.hostname()
} catch {
entry.host = 'unknown'
}
} else {
entry.host = window.location?.hostname || 'browser'
}
}
if (this.config.includeMemory) {
if (this.config.includeMemory && typeof process !== 'undefined' && process.memoryUsage) {
const mem = process.memoryUsage()
entry.performance = {
memory: {