Update docs/types for process (#3631)

* Update docs/types for process

* Tweaks

* Tweaks

* Fix types
This commit is contained in:
Colin McDonnell
2023-07-13 17:40:46 -07:00
committed by GitHub
parent 044b09afc2
commit 325147261f
6 changed files with 210 additions and 59 deletions

View File

@@ -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()`.
---

View File

@@ -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;

View File

@@ -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,

View File

@@ -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" {

View File

@@ -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,15 +407,28 @@ 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
*/
@@ -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 {

View File

@@ -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);