shim plugin related APIs, proxy web requests.

This commit is contained in:
Nystik
2026-03-11 23:03:14 +01:00
parent 9789be6d70
commit ac41ac3c4e
10 changed files with 452 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
function notAvailable(name) {
return function () {
throw new Error(
`child_process.${name}() is not available in the web version.`,
);
};
}
export const exec = notAvailable("exec");
export const execSync = notAvailable("execSync");
export const spawn = notAvailable("spawn");
export const fork = notAvailable("fork");
export const execFile = notAvailable("execFile");
export const execFileSync = notAvailable("execFileSync");
export const spawnSync = notAvailable("spawnSync");

82
shims/node/events.js Normal file
View File

@@ -0,0 +1,82 @@
export class EventEmitter {
constructor() {
this._events = {};
}
on(event, listener) {
if (!this._events[event]) this._events[event] = [];
this._events[event].push(listener);
return this;
}
once(event, listener) {
const wrapped = (...args) => {
this.removeListener(event, wrapped);
listener.apply(this, args);
};
wrapped._original = listener;
return this.on(event, wrapped);
}
emit(event, ...args) {
const listeners = this._events[event];
if (!listeners || listeners.length === 0) return false;
for (const fn of [...listeners]) {
fn.apply(this, args);
}
return true;
}
removeListener(event, listener) {
const arr = this._events[event];
if (!arr) return this;
const idx = arr.findIndex((fn) => fn === listener || fn._original === listener);
if (idx >= 0) arr.splice(idx, 1);
return this;
}
off(event, listener) {
return this.removeListener(event, listener);
}
removeAllListeners(event) {
if (event) {
delete this._events[event];
} else {
this._events = {};
}
return this;
}
listeners(event) {
return (this._events[event] || []).slice();
}
listenerCount(event) {
return (this._events[event] || []).length;
}
addListener(event, listener) {
return this.on(event, listener);
}
prependListener(event, listener) {
if (!this._events[event]) this._events[event] = [];
this._events[event].unshift(listener);
return this;
}
eventNames() {
return Object.keys(this._events);
}
setMaxListeners() {
return this;
}
getMaxListeners() {
return 10;
}
}
export default EventEmitter;

47
shims/node/http.js Normal file
View File

@@ -0,0 +1,47 @@
// Minimal http/https stub. Plugins needing full http.request won't work,
// but this prevents crashes for plugins that just import the module.
import { EventEmitter } from "./events.js";
export class IncomingMessage extends EventEmitter {
constructor() {
super();
this.headers = {};
this.statusCode = 0;
}
}
export class ClientRequest extends EventEmitter {
constructor() {
super();
}
end() {}
write() {}
abort() {}
destroy() {}
}
export function request(options, callback) {
const req = new ClientRequest();
if (callback) {
req.once("response", callback);
}
// Immediately error - real HTTP requests need fetch or the proxy
setTimeout(() => {
req.emit("error", new Error("http.request is not available in the web version. Use requestUrl() instead."));
}, 0);
return req;
}
export function get(options, callback) {
const req = request(options, callback);
req.end();
return req;
}
export function createServer() {
throw new Error("http.createServer is not available in the web version.");
}
export const Agent = class {};
export const globalAgent = new Agent();

19
shims/node/net.js Normal file
View File

@@ -0,0 +1,19 @@
function notAvailable(name) {
return function () {
throw new Error(`net.${name}() is not available in the web version.`);
};
}
export const createServer = notAvailable("createServer");
export const createConnection = notAvailable("createConnection");
export const connect = notAvailable("connect");
export class Socket {
constructor() {
throw new Error("net.Socket is not available in the web version.");
}
}
export class Server {
constructor() {
throw new Error("net.Server is not available in the web version.");
}
}

49
shims/node/os.js Normal file
View File

@@ -0,0 +1,49 @@
export function platform() {
return "linux";
}
export function arch() {
return "x64";
}
export function homedir() {
return "/";
}
export function tmpdir() {
return "/tmp";
}
export function hostname() {
return "localhost";
}
export function type() {
return "Linux";
}
export function release() {
return "0.0.0";
}
export function cpus() {
return [{ model: "browser", speed: 0 }];
}
export function totalmem() {
return 0;
}
export function freemem() {
return 0;
}
export function networkInterfaces() {
return {};
}
export function endianness() {
return "LE";
}
export const EOL = "\n";