diff --git a/src/utils/node-modules/assert.js b/src/utils/node-modules/assert.js new file mode 100644 index 00000000..ab4b9c5c --- /dev/null +++ b/src/utils/node-modules/assert.js @@ -0,0 +1,31 @@ +// Minimal assert module implementation for browser +const assert = { + ok: (value, message) => { + if (!value) { + throw new Error(message || "Assertion failed"); + } + }, + equal: (actual, expected, message) => { + if (actual !== expected) { + throw new Error(message || `Expected ${expected} but got ${actual}`); + } + }, + strictEqual: (actual, expected, message) => { + if (actual !== expected) { + throw new Error(message || `Expected ${expected} but got ${actual}`); + } + }, + deepEqual: (actual, expected, message) => { + if (JSON.stringify(actual) !== JSON.stringify(expected)) { + throw new Error( + message || + `Expected ${JSON.stringify(expected)} but got ${JSON.stringify(actual)}`, + ); + } + }, + fail: (message) => { + throw new Error(message || "Assertion failed"); + }, +}; + +export default assert; diff --git a/src/utils/node-modules/http.js b/src/utils/node-modules/http.js new file mode 100644 index 00000000..176d529a --- /dev/null +++ b/src/utils/node-modules/http.js @@ -0,0 +1,14 @@ +// Minimal http module implementation for browser +const http = { + request: () => { + throw new Error("http.request is not supported in browser"); + }, + get: () => { + throw new Error("http.get is not supported in browser"); + }, + createServer: () => { + throw new Error("http.createServer is not supported in browser"); + }, +}; + +export default http; diff --git a/src/utils/node-modules/https.js b/src/utils/node-modules/https.js new file mode 100644 index 00000000..0d5e547c --- /dev/null +++ b/src/utils/node-modules/https.js @@ -0,0 +1,14 @@ +// Minimal https module implementation for browser +const https = { + request: () => { + throw new Error("https.request is not supported in browser"); + }, + get: () => { + throw new Error("https.get is not supported in browser"); + }, + createServer: () => { + throw new Error("https.createServer is not supported in browser"); + }, +}; + +export default https; diff --git a/src/utils/node-modules/stream.js b/src/utils/node-modules/stream.js new file mode 100644 index 00000000..c2b00633 --- /dev/null +++ b/src/utils/node-modules/stream.js @@ -0,0 +1,25 @@ +// Minimal stream module implementation for browser +const stream = { + Readable: class Readable { + constructor() { + throw new Error("stream.Readable is not supported in browser"); + } + }, + Writable: class Writable { + constructor() { + throw new Error("stream.Writable is not supported in browser"); + } + }, + Transform: class Transform { + constructor() { + throw new Error("stream.Transform is not supported in browser"); + } + }, + PassThrough: class PassThrough { + constructor() { + throw new Error("stream.PassThrough is not supported in browser"); + } + }, +}; + +export default stream; diff --git a/src/utils/node-modules/tty.js b/src/utils/node-modules/tty.js new file mode 100644 index 00000000..8b5309df --- /dev/null +++ b/src/utils/node-modules/tty.js @@ -0,0 +1,16 @@ +// Minimal tty module implementation for browser +const tty = { + isatty: () => false, + ReadStream: class ReadStream { + constructor() { + throw new Error("tty.ReadStream is not supported in browser"); + } + }, + WriteStream: class WriteStream { + constructor() { + throw new Error("tty.WriteStream is not supported in browser"); + } + }, +}; + +export default tty; diff --git a/src/utils/node-modules/url.js b/src/utils/node-modules/url.js new file mode 100644 index 00000000..e885c427 --- /dev/null +++ b/src/utils/node-modules/url.js @@ -0,0 +1,25 @@ +// Minimal url module implementation for browser +const url = { + parse: (urlString) => { + try { + return new URL(urlString); + } catch (e) { + throw new Error(`Invalid URL: ${urlString}`); + } + }, + format: (urlObj) => { + if (urlObj instanceof URL) { + return urlObj.href; + } + return urlObj.toString(); + }, + resolve: (from, to) => { + try { + return new URL(to, from).href; + } catch (e) { + throw new Error(`Invalid URL resolution: ${from} -> ${to}`); + } + }, +}; + +export default url; diff --git a/src/utils/node-modules/util.js b/src/utils/node-modules/util.js new file mode 100644 index 00000000..48ab77d8 --- /dev/null +++ b/src/utils/node-modules/util.js @@ -0,0 +1,31 @@ +// Minimal util module implementation for browser +const util = { + inherits: () => {}, + format: (...args) => args.join(" "), + inspect: (obj) => JSON.stringify(obj), + isArray: Array.isArray, + isBoolean: (val) => typeof val === "boolean", + isNull: (val) => val === null, + isNullOrUndefined: (val) => val == null, + isNumber: (val) => typeof val === "number", + isString: (val) => typeof val === "string", + isSymbol: (val) => typeof val === "symbol", + isUndefined: (val) => val === undefined, + isRegExp: (val) => val instanceof RegExp, + isDate: (val) => val instanceof Date, + isError: (val) => val instanceof Error, + isFunction: (val) => typeof val === "function", + isObject: (val) => val !== null && typeof val === "object", + isPrimitive: (val) => { + return ( + val === null || + typeof val === "boolean" || + typeof val === "number" || + typeof val === "string" || + typeof val === "symbol" || + typeof val === "undefined" + ); + }, +}; + +export default util; diff --git a/src/utils/node-modules/zlib.js b/src/utils/node-modules/zlib.js new file mode 100644 index 00000000..7549bd8b --- /dev/null +++ b/src/utils/node-modules/zlib.js @@ -0,0 +1,29 @@ +// Minimal zlib module implementation for browser +const zlib = { + gzip: () => { + throw new Error("zlib.gzip is not supported in browser"); + }, + gunzip: () => { + throw new Error("zlib.gunzip is not supported in browser"); + }, + deflate: () => { + throw new Error("zlib.deflate is not supported in browser"); + }, + inflate: () => { + throw new Error("zlib.inflate is not supported in browser"); + }, + createGzip: () => { + throw new Error("zlib.createGzip is not supported in browser"); + }, + createGunzip: () => { + throw new Error("zlib.createGunzip is not supported in browser"); + }, + createDeflate: () => { + throw new Error("zlib.createDeflate is not supported in browser"); + }, + createInflate: () => { + throw new Error("zlib.createInflate is not supported in browser"); + }, +}; + +export default zlib; diff --git a/vite.config.common.mts b/vite.config.common.mts index 200057dc..1c80b966 100644 --- a/vite.config.common.mts +++ b/vite.config.common.mts @@ -79,6 +79,14 @@ export async function createBuildConfig(mode: string): Promise { 'path': path.resolve(__dirname, './src/utils/node-modules/path.js'), 'fs': path.resolve(__dirname, './src/utils/node-modules/fs.js'), 'crypto': path.resolve(__dirname, './src/utils/node-modules/crypto.js'), + 'util': path.resolve(__dirname, './src/utils/node-modules/util.js'), + 'stream': path.resolve(__dirname, './src/utils/node-modules/stream.js'), + 'http': path.resolve(__dirname, './src/utils/node-modules/http.js'), + 'https': path.resolve(__dirname, './src/utils/node-modules/https.js'), + 'url': path.resolve(__dirname, './src/utils/node-modules/url.js'), + 'assert': path.resolve(__dirname, './src/utils/node-modules/assert.js'), + 'tty': path.resolve(__dirname, './src/utils/node-modules/tty.js'), + 'zlib': path.resolve(__dirname, './src/utils/node-modules/zlib.js'), 'nostr-tools/nip06': mode === 'development' ? 'nostr-tools/nip06' : path.resolve(__dirname, 'node_modules/nostr-tools/nip06'),