aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Colin McDonnell <colinmcd94@gmail.com> 2023-07-13 17:40:46 -0700
committerGravatar GitHub <noreply@github.com> 2023-07-13 17:40:46 -0700
commit325147261ff9ecf3e249ba796606cd84687ebe8c (patch)
tree600f16edb5d2606f39aad27be7d070717e5bcb63
parent044b09afc2a8ce315d7f54a37a14aabf7e013744 (diff)
downloadbun-325147261ff9ecf3e249ba796606cd84687ebe8c.tar.gz
bun-325147261ff9ecf3e249ba796606cd84687ebe8c.tar.zst
bun-325147261ff9ecf3e249ba796606cd84687ebe8c.zip
Update docs/types for process (#3631)
* Update docs/types for process * Tweaks * Tweaks * Fix types
-rw-r--r--docs/runtime/nodejs-apis.md2
-rw-r--r--packages/bun-types/ffi.d.ts1
-rw-r--r--packages/bun-types/fs.d.ts2
-rw-r--r--packages/bun-types/fs/promises.d.ts123
-rw-r--r--packages/bun-types/globals.d.ts90
-rw-r--r--packages/bun-types/tests/process.test-d.ts51
6 files changed, 210 insertions, 59 deletions
diff --git a/docs/runtime/nodejs-apis.md b/docs/runtime/nodejs-apis.md
index 3dfe76b97..2e45fd945 100644
--- a/docs/runtime/nodejs-apis.md
+++ b/docs/runtime/nodejs-apis.md
@@ -510,7 +510,7 @@ The table below lists all globals implemented by Node.js and Bun's current compa
- {% anchor id="node_process" %} [`process`](https://nodejs.org/api/process.html) {% /anchor %}
- 🟡
-- Missing `process.allowedNodeEnvironmentFlags` `process.channel()` `process.connected` `process.constrainedMemory()` `process.cpuUsage()` `process.debugPort` `process.disconnect()` `process.{get|set}ActiveResourcesInfo()` `process.{get|set}{uid|gid|egid|euid|groups}()` `process.hasUncaughtExceptionCaptureCallback` `process.initGroups()` `process.kill()` `process.listenerCount` `process.memoryUsage()` `process.report` `process.resourceUsage()` `process.setSourceMapsEnabled()` `process.send()`.
+- Missing `process.allowedNodeEnvironmentFlags` `process.channel()` `process.connected` `process.constrainedMemory()` `process.disconnect()` `process.getActiveResourcesInfo/setActiveResourcesInfo()` `process.setuid/setgid/setegid/seteuid/setgroups()` `process.hasUncaughtExceptionCaptureCallback` `process.initGroups()` `process.report` `process.resourceUsage()` `process.send()`.
---
diff --git a/packages/bun-types/ffi.d.ts b/packages/bun-types/ffi.d.ts
index 9705e810b..c4811e98a 100644
--- a/packages/bun-types/ffi.d.ts
+++ b/packages/bun-types/ffi.d.ts
@@ -380,6 +380,7 @@ declare module "bun:ffi" {
[FFIType.cstring]: CString;
[FFIType.i64_fast]: number | bigint;
[FFIType.u64_fast]: number | bigint;
+ [FFIType.function]: (...args: any[]) => any;
}
interface FFITypeStringToType {
["char"]: FFIType.char;
diff --git a/packages/bun-types/fs.d.ts b/packages/bun-types/fs.d.ts
index 379e0ef25..cc3483b53 100644
--- a/packages/bun-types/fs.d.ts
+++ b/packages/bun-types/fs.d.ts
@@ -3999,7 +3999,7 @@ declare module "fs" {
recursive?: boolean;
signal?: AbortSignal;
};
- type WatchEventType = "rename" | "change" | "error" | "close";
+ export type WatchEventType = "rename" | "change" | "error" | "close";
type WatchListener<T> = (
event: WatchEventType,
filename: T | Error | undefined,
diff --git a/packages/bun-types/fs/promises.d.ts b/packages/bun-types/fs/promises.d.ts
index 2b908fceb..953c59a42 100644
--- a/packages/bun-types/fs/promises.d.ts
+++ b/packages/bun-types/fs/promises.d.ts
@@ -27,6 +27,7 @@ declare module "fs/promises" {
RmOptions,
RmDirOptions,
WatchOptions,
+ WatchEventType,
} from "node:fs";
const constants: typeof import("node:fs")["constants"];
@@ -711,62 +712,74 @@ declare module "fs/promises" {
*/
function rmdir(path: PathLike, options?: RmDirOptions): Promise<void>;
+ interface FileChangeInfo<T extends string | Buffer> {
+ eventType: WatchEventType;
+ filename: T;
+ }
/**
- * Returns an async iterator that watches for changes on `filename`, where `filename`is either a file or a directory.
- *
- * ```js
- * const { watch } = require('node:fs/promises');
- *
- * const ac = new AbortController();
- * const { signal } = ac;
- * setTimeout(() => ac.abort(), 10000);
- *
- * (async () => {
- * try {
- * const watcher = watch(__filename, { signal });
- * for await (const event of watcher)
- * console.log(event);
- * } catch (err) {
- * if (err.name === 'AbortError')
- * return;
- * throw err;
- * }
- * })();
- * ```
- *
- * On most platforms, `'rename'` is emitted whenever a filename appears or
- * disappears in the directory.
- *
- * All the `caveats` for `fs.watch()` also apply to `fsPromises.watch()`.
- * @since v0.6.8
- * @return of objects with the properties:
- */
- function watch(
- filename: PathLike,
- options:
- | (WatchOptions & {
- encoding: 'buffer';
- })
- | 'buffer'
- ): AsyncIterable<FileChangeInfo<Buffer>>;
- /**
- * Watch for changes on `filename`, where `filename` is either a file or a directory, returning an `FSWatcher`.
- * @param filename A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
- * @param options Either the encoding for the filename provided to the listener, or an object optionally specifying encoding, persistent, and recursive options.
- * If `encoding` is not supplied, the default of `'utf8'` is used.
- * If `persistent` is not supplied, the default of `true` is used.
- * If `recursive` is not supplied, the default of `false` is used.
- */
- function watch(filename: PathLike, options?: WatchOptions | BufferEncoding): AsyncIterable<FileChangeInfo<string>>;
- /**
- * Watch for changes on `filename`, where `filename` is either a file or a directory, returning an `FSWatcher`.
- * @param filename A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
- * @param options Either the encoding for the filename provided to the listener, or an object optionally specifying encoding, persistent, and recursive options.
- * If `encoding` is not supplied, the default of `'utf8'` is used.
- * If `persistent` is not supplied, the default of `true` is used.
- * If `recursive` is not supplied, the default of `false` is used.
- */
- function watch(filename: PathLike, options: WatchOptions | string): AsyncIterable<FileChangeInfo<string>> | AsyncIterable<FileChangeInfo<Buffer>>;
+ * Returns an async iterator that watches for changes on `filename`, where `filename`is either a file or a directory.
+ *
+ * ```js
+ * const { watch } = require('node:fs/promises');
+ *
+ * const ac = new AbortController();
+ * const { signal } = ac;
+ * setTimeout(() => ac.abort(), 10000);
+ *
+ * (async () => {
+ * try {
+ * const watcher = watch(__filename, { signal });
+ * for await (const event of watcher)
+ * console.log(event);
+ * } catch (err) {
+ * if (err.name === 'AbortError')
+ * return;
+ * throw err;
+ * }
+ * })();
+ * ```
+ *
+ * On most platforms, `'rename'` is emitted whenever a filename appears or
+ * disappears in the directory.
+ *
+ * All the `caveats` for `fs.watch()` also apply to `fsPromises.watch()`.
+ * @since v0.6.8
+ * @return of objects with the properties:
+ */
+ function watch(
+ filename: PathLike,
+ options:
+ | (WatchOptions & {
+ encoding: "buffer";
+ })
+ | "buffer",
+ ): AsyncIterable<FileChangeInfo<Buffer>>;
+ /**
+ * Watch for changes on `filename`, where `filename` is either a file or a directory, returning an `FSWatcher`.
+ * @param filename A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
+ * @param options Either the encoding for the filename provided to the listener, or an object optionally specifying encoding, persistent, and recursive options.
+ * If `encoding` is not supplied, the default of `'utf8'` is used.
+ * If `persistent` is not supplied, the default of `true` is used.
+ * If `recursive` is not supplied, the default of `false` is used.
+ */
+ function watch(
+ filename: PathLike,
+ options?: WatchOptions | BufferEncoding,
+ ): AsyncIterable<FileChangeInfo<string>>;
+ /**
+ * Watch for changes on `filename`, where `filename` is either a file or a directory, returning an `FSWatcher`.
+ * @param filename A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
+ * @param options Either the encoding for the filename provided to the listener, or an object optionally specifying encoding, persistent, and recursive options.
+ * If `encoding` is not supplied, the default of `'utf8'` is used.
+ * If `persistent` is not supplied, the default of `true` is used.
+ * If `recursive` is not supplied, the default of `false` is used.
+ */
+ function watch(
+ filename: PathLike,
+ options: WatchOptions | string,
+ ):
+ | AsyncIterable<FileChangeInfo<string>>
+ | AsyncIterable<FileChangeInfo<Buffer>>;
}
declare module "node:fs/promises" {
diff --git a/packages/bun-types/globals.d.ts b/packages/bun-types/globals.d.ts
index d74fabf1f..cb9f3d1bc 100644
--- a/packages/bun-types/globals.d.ts
+++ b/packages/bun-types/globals.d.ts
@@ -75,6 +75,34 @@ interface ArrayConstructor {
): Promise<Array<T>>;
}
+type UncaughtExceptionOrigin = "uncaughtException" | "unhandledRejection";
+type MultipleResolveType = "resolve" | "reject";
+type BeforeExitListener = (code: number) => void;
+type DisconnectListener = () => void;
+type ExitListener = (code: number) => void;
+type RejectionHandledListener = (promise: Promise<unknown>) => void;
+type UncaughtExceptionListener = (
+ error: Error,
+ origin: UncaughtExceptionOrigin,
+) => void;
+/**
+ * Most of the time the unhandledRejection will be an Error, but this should not be relied upon
+ * as *anything* can be thrown/rejected, it is therefore unsafe to assume that the value is an Error.
+ */
+type UnhandledRejectionListener = (
+ reason: unknown,
+ promise: Promise<unknown>,
+) => void;
+type WarningListener = (warning: Error) => void;
+type MessageListener = (message: unknown, sendHandle: unknown) => void;
+type SignalsListener = (signal: Signals) => void;
+type MultipleResolveListener = (
+ type: MultipleResolveType,
+ promise: Promise<unknown>,
+ value: unknown,
+) => void;
+// type WorkerListener = (worker: Worker) => void;
+
interface Console {
/**
* Asynchronously read lines from standard input (fd 0)
@@ -369,6 +397,8 @@ interface Process {
argv: string[];
execArgv: string[];
env: import("bun").Env;
+ allowedNodeEnvironmentFlags: Set<string>;
+ debugPort: number;
/** Whether you are using Bun */
isBun: 1; // FIXME: this should actually return a boolean
@@ -377,16 +407,29 @@ interface Process {
chdir(directory: string): void;
cwd(): string;
exit(code?: number): never;
+ reallyExit(code?: number): never;
getgid(): number;
- setgid(id: number | string): void;
+ // setgid(id: number | string): void;
getuid(): number;
- setuid(id: number | string): void;
+ // setuid(id: number | string): void;
+ geteuid: () => number;
+ // seteuid: (id: number | string) => void;
+ getegid: () => number;
+ // setegid: (id: number | string) => void;
+ getgroups: () => number[];
+ // setgroups?: (groups: ReadonlyArray<string | number>) => void;
dlopen(module: { exports: any }, filename: string, flags?: number): void;
stdin: import("stream").Duplex & { isTTY: boolean };
stdout: import("stream").Writable & { isTTY: boolean };
stderr: import("stream").Writable & { isTTY: boolean };
/**
+ *
+ * @deprecated This is deprecated; use the "node:assert" module instead.
+ */
+ assert(value: unknown, message?: string | Error): asserts value;
+
+ /**
* exit the process with a fatal exception, sending SIGABRT
*/
abort(): never;
@@ -435,6 +478,49 @@ interface Process {
setSourceMapsEnabled(enabled: boolean): void;
kill(pid: number, signal?: string | number): void;
+
+ on(event: "beforeExit", listener: BeforeExitListener): this;
+ // on(event: "disconnect", listener: DisconnectListener): this;
+ on(event: "exit", listener: ExitListener): this;
+ // on(event: "rejectionHandled", listener: RejectionHandledListener): this;
+ // on(event: "uncaughtException", listener: UncaughtExceptionListener): this;
+ // on(
+ // event: "uncaughtExceptionMonitor",
+ // listener: UncaughtExceptionListener,
+ // ): this;
+ // on(event: "unhandledRejection", listener: UnhandledRejectionListener): this;
+ // on(event: "warning", listener: WarningListener): this;
+ // on(event: "message", listener: MessageListener): this;
+ on(event: Signals, listener: SignalsListener): this;
+ // on(event: "multipleResolves", listener: MultipleResolveListener): this;
+ // on(event: "worker", listener: WorkerListener): this;
+ on(event: string | symbol, listener: (...args: any[]) => void): this;
+ once(event: "beforeExit", listener: BeforeExitListener): this;
+ // once(event: "disconnect", listener: DisconnectListener): this;
+ once(event: "exit", listener: ExitListener): this;
+ // once(event: "rejectionHandled", listener: RejectionHandledListener): this;
+ // once(event: "uncaughtException", listener: UncaughtExceptionListener): this;
+ // once(
+ // event: "uncaughtExceptionMonitor",
+ // listener: UncaughtExceptionListener,
+ // ): this;
+ // once(event: "unhandledRejection", listener: UnhandledRejectionListener): this;
+ // once(event: "warning", listener: WarningListener): this;
+ // once(event: "message", listener: MessageListener): this;
+ once(event: Signals, listener: SignalsListener): this;
+ // once(event: "multipleResolves", listener: MultipleResolveListener): this;
+ // once(event: "worker", listener: WorkerListener): this;
+ once(event: string | symbol, listener: (...args: any[]) => void): this;
+
+ /**
+ * Returns the number of listeners listening for the event named `eventName`.
+ * If `listener` is provided, it will return how many times the listener is found
+ * in the list of the listeners of the event.
+ * @since v3.2.0
+ * @param eventName The name of the event being listened for
+ * @param listener The event handler function
+ */
+ listenerCount(eventName: string | symbol, listener?: Function): number;
}
interface MemoryUsageObject {
diff --git a/packages/bun-types/tests/process.test-d.ts b/packages/bun-types/tests/process.test-d.ts
new file mode 100644
index 000000000..0aa0f9b27
--- /dev/null
+++ b/packages/bun-types/tests/process.test-d.ts
@@ -0,0 +1,51 @@
+process.memoryUsage();
+process.cpuUsage().system;
+process.cpuUsage().user;
+process.on("SIGINT", () => {
+ console.log("Interrupt from keyboard");
+});
+
+process.on("beforeExit", code => {
+ console.log("Event loop is empty and no work is left to schedule.", code);
+});
+
+process.on("exit", code => {
+ console.log("Exiting with code:", code);
+});
+process.kill(123, "SIGTERM");
+
+process.getegid();
+process.geteuid();
+process.getgid();
+process.getgroups();
+process.getuid();
+
+process.once("SIGINT", () => {
+ console.log("Interrupt from keyboard");
+});
+
+process.reallyExit();
+
+process.assert(false, "PleAsE don't Use THIs It IS dEpReCATED");
+
+console.log(process.allowedNodeEnvironmentFlags);
+// console.log(process.channel);
+// console.log(process.connected);
+// console.log(process.constrainedMemory);
+console.log(process.debugPort);
+// console.log(process.disconnect);
+// console.log(process.getActiveResourcesInfo);
+// console.log(process.setActiveResourcesInfo);
+// console.log(process.setuid);
+// console.log(process.setgid);
+// console.log(process.setegid);
+// console.log(process.seteuid);
+// console.log(process.setgroups);
+// console.log(process.hasUncaughtExceptionCaptureCallback);
+// console.log(process.initGroups);
+console.log(process.listenerCount("exit"));
+console.log(process.memoryUsage());
+// console.log(process.report);
+// console.log(process.resourceUsage);
+// console.log(process.setSourceMapsEnabled());
+// console.log(process.send);