From b9460087e391c454f323390a42902a3ed024c8bc Mon Sep 17 00:00:00 2001 From: dave caruso Date: Thu, 29 Jun 2023 23:36:18 -0400 Subject: Fixes `node:http` and `node:stream` so `ytdl-core` works. (#3452) * fix crash in readablestate * make node:https request+get actually use https * use a native readablestream in IncomingMessage * tweaks * fix abort crash * emit close by default * remove abort. this isnt a real function * add validate functions, fixup some other requested changes. not done yet * Update WebCoreJSBuiltins.cpp * Update JSReadableState.cpp * Add some missing exports --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> --- src/js/builtins/ReadableStreamDefaultReader.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/js/builtins/ReadableStreamDefaultReader.ts') diff --git a/src/js/builtins/ReadableStreamDefaultReader.ts b/src/js/builtins/ReadableStreamDefaultReader.ts index ecd553ed5..70c6df8c3 100644 --- a/src/js/builtins/ReadableStreamDefaultReader.ts +++ b/src/js/builtins/ReadableStreamDefaultReader.ts @@ -43,7 +43,7 @@ export function cancel(this, reason) { return $readableStreamReaderGenericCancel(this, reason); } -export function readMany(this) { +export function readMany(this: ReadableStreamDefaultReader): ReadableStreamDefaultReadManyResult { if (!$isReadableStreamDefaultReader(this)) throw new TypeError("ReadableStreamDefaultReader.readMany() should not be called directly"); @@ -75,7 +75,7 @@ export function readMany(this) { var length = values.length; if (length > 0) { - var outValues = $newArrayWithSize(length); + var outValues = $newArrayWithSize(length); if ($isReadableByteStreamController(controller)) { { const buf = values[0]; -- cgit v1.2.3 From affd06d05cc756df33854bc1d56e2284ff22b22d Mon Sep 17 00:00:00 2001 From: Colin McDonnell Date: Fri, 7 Jul 2023 15:10:33 -0700 Subject: Update types, partially fix `typecheck` (#3551) * Update types * Remove caret --- packages/bun-types/assert.d.ts | 6 +- packages/bun-types/ffi.d.ts | 1 + packages/bun-types/fs.d.ts | 213 ++++++---- packages/bun-types/globals.d.ts | 21 +- packages/bun-types/http.d.ts | 18 + packages/bun-types/tests/fs.test-d.ts | 9 + src/js/builtins/ReadableStreamDefaultReader.ts | 4 +- src/js/builtins/ReadableStreamInternals.ts | 1 + src/js/builtins/StreamInternals.ts | 2 +- src/js/builtins/TransformStream.ts | 1 + src/js/builtins/TransformStreamInternals.ts | 1 + src/js/builtins/WritableStreamDefaultWriter.ts | 1 + src/js/builtins/WritableStreamInternals.ts | 9 +- src/js/builtins/builtins.d.ts | 6 +- src/js/bun/ffi.ts | 8 +- src/js/node/trace_events.ts | 2 + src/js/private.d.ts | 3 +- test/bun.lockb | Bin 141113 -> 148733 bytes test/js/node/watch/fs.watch.test.js | 522 ------------------------- test/js/node/watch/fs.watch.test.ts | 522 +++++++++++++++++++++++++ test/js/third_party/socket.io/support/util.ts | 1 + test/js/web/html/FormData.test.ts | 19 +- test/js/web/html/URLSearchParams.test.ts | 5 + test/package.json | 9 +- test/tsconfig.json | 34 +- tsconfig.json | 9 +- 26 files changed, 749 insertions(+), 678 deletions(-) delete mode 100644 test/js/node/watch/fs.watch.test.js create mode 100644 test/js/node/watch/fs.watch.test.ts (limited to 'src/js/builtins/ReadableStreamDefaultReader.ts') diff --git a/packages/bun-types/assert.d.ts b/packages/bun-types/assert.d.ts index ae3b54ff2..658e7df5b 100644 --- a/packages/bun-types/assert.d.ts +++ b/packages/bun-types/assert.d.ts @@ -931,7 +931,11 @@ declare module "assert" { * instance of an `Error` then it will be thrown instead of the `AssertionError`. */ // FIXME: assert.doesNotMatch is typed, but not in the browserify polyfill? - // function doesNotMatch(value: string, regExp: RegExp, message?: string | Error): void; + function doesNotMatch( + value: string, + regExp: RegExp, + message?: string | Error, + ): void; const strict: Omit< typeof assert, diff --git a/packages/bun-types/ffi.d.ts b/packages/bun-types/ffi.d.ts index 3e7e91534..9705e810b 100644 --- a/packages/bun-types/ffi.d.ts +++ b/packages/bun-types/ffi.d.ts @@ -344,6 +344,7 @@ declare module "bun:ffi" { * */ u64_fast = 16, + function = 17, } type UNTYPED = never; diff --git a/packages/bun-types/fs.d.ts b/packages/bun-types/fs.d.ts index 5dfb2c7f2..379e0ef25 100644 --- a/packages/bun-types/fs.d.ts +++ b/packages/bun-types/fs.d.ts @@ -3932,100 +3932,141 @@ declare module "fs" { } export interface FSWatcher extends EventEmitter { - /** - * Stop watching for changes on the given `fs.FSWatcher`. Once stopped, the `fs.FSWatcher` object is no longer usable. - * @since v0.6.8 - */ - close(): void; - - /** - * When called, requests that the Node.js event loop not exit so long as the is active. Calling watcher.ref() multiple times will have no effect. - */ - ref(): void; - - /** - * When called, the active object will not require the Node.js event loop to remain active. If there is no other activity keeping the event loop running, the process may exit before the object's callback is invoked. Calling watcher.unref() multiple times will have no effect. - */ - unref(): void; - - /** - * events.EventEmitter - * 1. change - * 2. error - */ - addListener(event: string, listener: (...args: any[]) => void): this; - addListener(event: 'change', listener: (eventType: string, filename: string | Buffer) => void): this; - addListener(event: 'error', listener: (error: Error) => void): this; - addListener(event: 'close', listener: () => void): this; - on(event: string, listener: (...args: any[]) => void): this; - on(event: 'change', listener: (eventType: string, filename: string | Buffer) => void): this; - on(event: 'error', listener: (error: Error) => void): this; - on(event: 'close', listener: () => void): this; - once(event: string, listener: (...args: any[]) => void): this; - once(event: 'change', listener: (eventType: string, filename: string | Buffer) => void): this; - once(event: 'error', listener: (error: Error) => void): this; - once(event: 'close', listener: () => void): this; - prependListener(event: string, listener: (...args: any[]) => void): this; - prependListener(event: 'change', listener: (eventType: string, filename: string | Buffer) => void): this; - prependListener(event: 'error', listener: (error: Error) => void): this; - prependListener(event: 'close', listener: () => void): this; - prependOnceListener(event: string, listener: (...args: any[]) => void): this; - prependOnceListener(event: 'change', listener: (eventType: string, filename: string | Buffer) => void): this; - prependOnceListener(event: 'error', listener: (error: Error) => void): this; - prependOnceListener(event: 'close', listener: () => void): this; - } - /** - * Watch for changes on `filename`, where `filename` is either a file or a - * directory. - * - * The second argument is optional. If `options` is provided as a string, it - * specifies the `encoding`. Otherwise `options` should be passed as an object. - * - * The listener callback gets two arguments `(eventType, filename)`. `eventType`is either `'rename'` or `'change'`, and `filename` is the name of the file - * which triggered the event. - * - * On most platforms, `'rename'` is emitted whenever a filename appears or - * disappears in the directory. - * - * The listener callback is attached to the `'change'` event fired by `fs.FSWatcher`, but it is not the same thing as the `'change'` value of`eventType`. - * - * If a `signal` is passed, aborting the corresponding AbortController will close - * the returned `fs.FSWatcher`. + /** + * Stop watching for changes on the given `fs.FSWatcher`. Once stopped, the `fs.FSWatcher` object is no longer usable. * @since v0.6.8 - * @param listener */ - export function watch( - filename: PathLike, - options: - | (WatchOptions & { - encoding: 'buffer'; - }) - | 'buffer', - listener?: WatchListener - ): FSWatcher; + close(): void; + /** - * 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. + * When called, requests that the Node.js event loop not exit so long as the is active. Calling watcher.ref() multiple times will have no effect. */ - export function watch(filename: PathLike, options?: WatchOptions | BufferEncoding | null, listener?: WatchListener): FSWatcher; + ref(): void; + /** - * 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. + * When called, the active object will not require the Node.js event loop to remain active. If there is no other activity keeping the event loop running, the process may exit before the object's callback is invoked. Calling watcher.unref() multiple times will have no effect. */ - export function watch(filename: PathLike, options: WatchOptions | string, listener?: WatchListener): FSWatcher; + unref(): void; + /** - * 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. + * events.EventEmitter + * 1. change + * 2. error */ - export function watch(filename: PathLike, listener?: WatchListener): FSWatcher; + addListener(event: string, listener: (...args: any[]) => void): this; + addListener( + event: "change", + listener: (eventType: string, filename: string | Buffer) => void, + ): this; + addListener(event: "error", listener: (error: Error) => void): this; + addListener(event: "close", listener: () => void): this; + on(event: string, listener: (...args: any[]) => void): this; + on( + event: "change", + listener: (eventType: string, filename: string | Buffer) => void, + ): this; + on(event: "error", listener: (error: Error) => void): this; + on(event: "close", listener: () => void): this; + once(event: string, listener: (...args: any[]) => void): this; + once( + event: "change", + listener: (eventType: string, filename: string | Buffer) => void, + ): this; + once(event: "error", listener: (error: Error) => void): this; + once(event: "close", listener: () => void): this; + prependListener(event: string, listener: (...args: any[]) => void): this; + prependListener( + event: "change", + listener: (eventType: string, filename: string | Buffer) => void, + ): this; + prependListener(event: "error", listener: (error: Error) => void): this; + prependListener(event: "close", listener: () => void): this; + prependOnceListener( + event: string, + listener: (...args: any[]) => void, + ): this; + prependOnceListener( + event: "change", + listener: (eventType: string, filename: string | Buffer) => void, + ): this; + prependOnceListener(event: "error", listener: (error: Error) => void): this; + prependOnceListener(event: "close", listener: () => void): this; + } + + type WatchOptions = { + encoding?: BufferEncoding; + persistent?: boolean; + recursive?: boolean; + signal?: AbortSignal; + }; + type WatchEventType = "rename" | "change" | "error" | "close"; + type WatchListener = ( + event: WatchEventType, + filename: T | Error | undefined, + ) => void; + /** + * Watch for changes on `filename`, where `filename` is either a file or a + * directory. + * + * The second argument is optional. If `options` is provided as a string, it + * specifies the `encoding`. Otherwise `options` should be passed as an object. + * + * The listener callback gets two arguments `(eventType, filename)`. `eventType`is either `'rename'` or `'change'`, and `filename` is the name of the file + * which triggered the event. + * + * On most platforms, `'rename'` is emitted whenever a filename appears or + * disappears in the directory. + * + * The listener callback is attached to the `'change'` event fired by `fs.FSWatcher`, but it is not the same thing as the `'change'` value of`eventType`. + * + * If a `signal` is passed, aborting the corresponding AbortController will close + * the returned `fs.FSWatcher`. + * @since v0.6.8 + * @param listener + */ + export function watch( + filename: PathLike, + options: + | (WatchOptions & { + encoding: "buffer"; + }) + | "buffer", + listener?: WatchListener, + ): FSWatcher; + /** + * 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. + */ + export function watch( + filename: PathLike, + options?: WatchOptions | BufferEncoding | null, + listener?: WatchListener, + ): FSWatcher; + /** + * 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. + */ + export function watch( + filename: PathLike, + options: WatchOptions | string, + listener?: WatchListener, + ): FSWatcher; + /** + * 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. + */ + export function watch( + filename: PathLike, + listener?: WatchListener, + ): FSWatcher; } declare module "node:fs" { diff --git a/packages/bun-types/globals.d.ts b/packages/bun-types/globals.d.ts index 5784f91c2..da412b211 100644 --- a/packages/bun-types/globals.d.ts +++ b/packages/bun-types/globals.d.ts @@ -1401,34 +1401,33 @@ declare function clearTimeout(id?: number | Timer): void; declare function clearImmediate(id?: number | Timer): void; // declare function createImageBitmap(image: ImageBitmapSource, options?: ImageBitmapOptions): Promise; // declare function createImageBitmap(image: ImageBitmapSource, sx: number, sy: number, sw: number, sh: number, options?: ImageBitmapOptions): Promise; + /** * Send a HTTP(s) request * - * @param url URL string + * @param request Request object * @param init A structured value that contains settings for the fetch() request. * * @returns A promise that resolves to {@link Response} object. * * */ - -declare function fetch( - url: string | URL | Request, - init?: FetchRequestInit, -): Promise; - +// tslint:disable-next-line:unified-signatures +declare function fetch(request: Request, init?: RequestInit): Promise; /** * Send a HTTP(s) request * - * @param request Request object + * @param url URL string * @param init A structured value that contains settings for the fetch() request. * * @returns A promise that resolves to {@link Response} object. * * */ -// tslint:disable-next-line:unified-signatures -declare function fetch(request: Request, init?: RequestInit): Promise; +declare function fetch( + url: string | URL | Request, + init?: FetchRequestInit, +): Promise; declare function queueMicrotask(callback: (...args: any[]) => void): void; /** @@ -1951,7 +1950,7 @@ interface URLSearchParams { ): void; /** Returns a string containing a query string suitable for use in a URL. Does not include the question mark. */ toString(): string; - [Symbol.iterator](): IterableIterator<[string, FormDataEntryValue]>; + [Symbol.iterator](): IterableIterator<[string, string]>; } declare var URLSearchParams: { diff --git a/packages/bun-types/http.d.ts b/packages/bun-types/http.d.ts index 710fea4f4..118cf7ac1 100644 --- a/packages/bun-types/http.d.ts +++ b/packages/bun-types/http.d.ts @@ -1785,6 +1785,24 @@ declare module "http" { callback?: (res: IncomingMessage) => void, ): ClientRequest; + /** + * Performs the low-level validations on the provided name that are done when `res.setHeader(name, value)` is called. + * Passing illegal value as name will result in a TypeError being thrown, identified by `code: 'ERR_INVALID_HTTP_TOKEN'`. + * @param name Header name + * @since v14.3.0 + */ + function validateHeaderName(name: string): void; + /** + * Performs the low-level validations on the provided value that are done when `res.setHeader(name, value)` is called. + * Passing illegal value as value will result in a TypeError being thrown. + * - Undefined value error is identified by `code: 'ERR_HTTP_INVALID_HEADER_VALUE'`. + * - Invalid value character error is identified by `code: 'ERR_INVALID_CHAR'`. + * @param name Header name + * @param value Header value + * @since v14.3.0 + */ + function validateHeaderValue(name: string, value: string): void; + let globalAgent: Agent; /** diff --git a/packages/bun-types/tests/fs.test-d.ts b/packages/bun-types/tests/fs.test-d.ts index 1ef14a2f8..3acfafa76 100644 --- a/packages/bun-types/tests/fs.test-d.ts +++ b/packages/bun-types/tests/fs.test-d.ts @@ -1,6 +1,15 @@ +import { watch } from "node:fs"; import * as tsd from "tsd"; import * as fs from "fs"; import { exists } from "fs/promises"; tsd.expectType>(exists("/etc/passwd")); tsd.expectType>(fs.promises.exists("/etc/passwd")); + +// file path +watch(".", (eventType, filename) => { + console.log(`event type = ${eventType}`); + if (filename) { + console.log(`filename = ${filename}`); + } +}); diff --git a/src/js/builtins/ReadableStreamDefaultReader.ts b/src/js/builtins/ReadableStreamDefaultReader.ts index 70c6df8c3..a5654d834 100644 --- a/src/js/builtins/ReadableStreamDefaultReader.ts +++ b/src/js/builtins/ReadableStreamDefaultReader.ts @@ -75,7 +75,7 @@ export function readMany(this: ReadableStreamDefaultReader): ReadableStreamDefau var length = values.length; if (length > 0) { - var outValues = $newArrayWithSize(length); + var outValues = $newArrayWithSize(length); if ($isReadableByteStreamController(controller)) { { const buf = values[0]; @@ -150,7 +150,7 @@ export function readMany(this: ReadableStreamDefaultReader): ReadableStreamDefau var pullResult = controller.$pull(controller); if (pullResult && $isPromise(pullResult)) { - return pullResult.$then(onPullMany); + return pullResult.$then(onPullMany) as any; } return onPullMany(pullResult); diff --git a/src/js/builtins/ReadableStreamInternals.ts b/src/js/builtins/ReadableStreamInternals.ts index 0c4e816f4..a9d67aa06 100644 --- a/src/js/builtins/ReadableStreamInternals.ts +++ b/src/js/builtins/ReadableStreamInternals.ts @@ -1,3 +1,4 @@ +// @ts-nocheck /* * Copyright (C) 2015 Canon Inc. All rights reserved. * Copyright (C) 2015 Igalia. diff --git a/src/js/builtins/StreamInternals.ts b/src/js/builtins/StreamInternals.ts index b42dc2f57..7bb262951 100644 --- a/src/js/builtins/StreamInternals.ts +++ b/src/js/builtins/StreamInternals.ts @@ -184,7 +184,7 @@ export function createFIFO() { this._capacityMask = (this._capacityMask << 1) | 1; } - shrinkArray() { + _shrinkArray() { this._list.length >>>= 1; this._capacityMask >>>= 1; } diff --git a/src/js/builtins/TransformStream.ts b/src/js/builtins/TransformStream.ts index 54467db39..2a124d4e1 100644 --- a/src/js/builtins/TransformStream.ts +++ b/src/js/builtins/TransformStream.ts @@ -1,3 +1,4 @@ +// @ts-nocheck /* * Copyright (C) 2020 Apple Inc. All rights reserved. * diff --git a/src/js/builtins/TransformStreamInternals.ts b/src/js/builtins/TransformStreamInternals.ts index 9994d1282..9da403e71 100644 --- a/src/js/builtins/TransformStreamInternals.ts +++ b/src/js/builtins/TransformStreamInternals.ts @@ -1,3 +1,4 @@ +// @ts-nocheck /* * Copyright (C) 2020 Apple Inc. All rights reserved. * diff --git a/src/js/builtins/WritableStreamDefaultWriter.ts b/src/js/builtins/WritableStreamDefaultWriter.ts index 795b43892..50b2cd13f 100644 --- a/src/js/builtins/WritableStreamDefaultWriter.ts +++ b/src/js/builtins/WritableStreamDefaultWriter.ts @@ -1,3 +1,4 @@ +// @ts-nocheck /* * Copyright (C) 2020 Apple Inc. All rights reserved. * diff --git a/src/js/builtins/WritableStreamInternals.ts b/src/js/builtins/WritableStreamInternals.ts index f436a285e..2008dab1c 100644 --- a/src/js/builtins/WritableStreamInternals.ts +++ b/src/js/builtins/WritableStreamInternals.ts @@ -610,16 +610,17 @@ export function setUpWritableStreamDefaultControllerFromUnderlyingSink( highWaterMark, sizeAlgorithm, ) { + // @ts-ignore const controller = new $WritableStreamDefaultController(); - let startAlgorithm = () => {}; - let writeAlgorithm = () => { + let startAlgorithm: (...args: any[]) => any = () => {}; + let writeAlgorithm: (...args: any[]) => any = () => { return Promise.$resolve(); }; - let closeAlgorithm = () => { + let closeAlgorithm: (...args: any[]) => any = () => { return Promise.$resolve(); }; - let abortAlgorithm = () => { + let abortAlgorithm: (...args: any[]) => any = () => { return Promise.$resolve(); }; diff --git a/src/js/builtins/builtins.d.ts b/src/js/builtins/builtins.d.ts index 2de8d8206..9b32ea45e 100644 --- a/src/js/builtins/builtins.d.ts +++ b/src/js/builtins/builtins.d.ts @@ -57,6 +57,9 @@ declare function $getPromiseInternalField( promise: Promise, key: K, ): PromiseFieldToValue; +declare function $fulfillPromise(...args: any[]): TODO; +declare function $evaluateCommonJSModule(...args: any[]): TODO; +declare function $loadCJS2ESM(...args: any[]): TODO; declare function $getGeneratorInternalField(): TODO; declare function $getAsyncGeneratorInternalField(): TODO; declare function $getAbstractModuleRecordInternalField(): TODO; @@ -229,7 +232,7 @@ declare function $createFIFO(): TODO; declare function $createNativeReadableStream(): TODO; declare function $createReadableStream(): TODO; declare function $createUninitializedArrayBuffer(size: number): ArrayBuffer; -declare function $createWritableStreamFromInternal(): TODO; +declare function $createWritableStreamFromInternal(...args: any[]): TODO; declare function $cwd(): TODO; declare function $data(): TODO; declare function $dataView(): TODO; @@ -330,6 +333,7 @@ declare function $read(): TODO; declare function $readIntoRequests(): TODO; declare function $readRequests(): TODO; declare function $readable(): TODO; +declare function $readableByteStreamControllerGetDesiredSize(...args: any): TODO; declare function $readableStreamController(): TODO; declare function $readableStreamToArray(): TODO; declare function $reader(): TODO; diff --git a/src/js/bun/ffi.ts b/src/js/bun/ffi.ts index 7abfe5078..1272e7450 100644 --- a/src/js/bun/ffi.ts +++ b/src/js/bun/ffi.ts @@ -239,7 +239,7 @@ ffiWrappers[FFIType.function] = function functionType(val) { }; function FFIBuilder(params, returnType, functionToCall, name) { - const hasReturnType = typeof FFIType[returnType] === "number" && FFIType[returnType] !== FFIType.void; + const hasReturnType = typeof FFIType[returnType] === "number" && FFIType[returnType as string] !== FFIType.void; var paramNames = new Array(params.length); var args = new Array(params.length); for (let i = 0; i < params.length; i++) { @@ -255,7 +255,7 @@ function FFIBuilder(params, returnType, functionToCall, name) { var code = `functionToCall(${args.join(", ")})`; if (hasReturnType) { - if (FFIType[returnType] === FFIType.cstring) { + if (FFIType[returnType as string] === FFIType.cstring) { code = `return (${cstringReturnType.toString()})(${code})`; } else { code = `return ${code}`; @@ -328,7 +328,7 @@ export function dlopen(path, options) { for (let key in result.symbols) { var symbol = result.symbols[key]; - if (options[key]?.args?.length || FFIType[options[key]?.returns] === FFIType.cstring) { + if (options[key]?.args?.length || FFIType[options[key]?.returns as string] === FFIType.cstring) { result.symbols[key] = FFIBuilder( options[key].args ?? [], options[key].returns ?? FFIType.void, @@ -354,7 +354,7 @@ export function linkSymbols(options) { for (let key in result.symbols) { var symbol = result.symbols[key]; - if (options[key]?.args?.length || FFIType[options[key]?.returns] === FFIType.cstring) { + if (options[key]?.args?.length || FFIType[options[key]?.returns as string] === FFIType.cstring) { result.symbols[key] = FFIBuilder(options[key].args ?? [], options[key].returns ?? FFIType.void, symbol, key); } else { // consistentcy diff --git a/src/js/node/trace_events.ts b/src/js/node/trace_events.ts index 789c41222..7edcc57d0 100644 --- a/src/js/node/trace_events.ts +++ b/src/js/node/trace_events.ts @@ -13,10 +13,12 @@ function ERR_INVALID_ARG_TYPE(name, type, value) { function createTracing(opts) { if (typeof opts !== "object" || opts == null) { + // @ts-ignore throw new ERR_INVALID_ARG_TYPE("options", "Object", opts); } // TODO: validate categories + // @ts-ignore return new Tracing(opts); } diff --git a/src/js/private.d.ts b/src/js/private.d.ts index 500048dd7..d59c6aad8 100644 --- a/src/js/private.d.ts +++ b/src/js/private.d.ts @@ -7,9 +7,8 @@ declare function $bundleError(error: string); type BunFSWatchOptions = { encoding?: BufferEncoding; persistent?: boolean; recursive?: boolean; signal?: AbortSignal }; - type BunWatchEventType = "rename" | "change" | "error" | "close"; -type BunWatchListener = (event: WatchEventType, filename: T | Error | undefined) => void; +type BunWatchListener = (event: WatchEventType, filename: T | undefined) => void; interface BunFSWatcher { /** diff --git a/test/bun.lockb b/test/bun.lockb index c35182d46..01afbfa4e 100755 Binary files a/test/bun.lockb and b/test/bun.lockb differ diff --git a/test/js/node/watch/fs.watch.test.js b/test/js/node/watch/fs.watch.test.js deleted file mode 100644 index faf6a8546..000000000 --- a/test/js/node/watch/fs.watch.test.js +++ /dev/null @@ -1,522 +0,0 @@ -import fs from "fs"; -import path from "path"; -import { tempDirWithFiles, bunRun, bunRunAsScript } from "harness"; -import { pathToFileURL } from "bun"; - -import { describe, expect, test } from "bun:test"; -// Because macOS (and possibly other operating systems) can return a watcher -// before it is actually watching, we need to repeat the operation to avoid -// a race condition. -function repeat(fn) { - const interval = setInterval(fn, 20); - return interval; -} -const encodingFileName = `新建文夹件.txt`; -const testDir = tempDirWithFiles("watch", { - "watch.txt": "hello", - "relative.txt": "hello", - "abort.txt": "hello", - "url.txt": "hello", - "close.txt": "hello", - "close-close.txt": "hello", - "sym-sync.txt": "hello", - "sym.txt": "hello", - [encodingFileName]: "hello", -}); - -describe("fs.watch", () => { - test("non-persistent watcher should not block the event loop", done => { - try { - // https://github.com/joyent/node/issues/2293 - non-persistent watcher should not block the event loop - bunRun(path.join(import.meta.dir, "fixtures", "persistent.js")); - done(); - } catch (e) { - done(e); - } - }); - - test("watcher should close and not block the event loop", done => { - try { - bunRun(path.join(import.meta.dir, "fixtures", "close.js")); - done(); - } catch (e) { - done(e); - } - }); - - test("unref watcher should not block the event loop", done => { - try { - bunRun(path.join(import.meta.dir, "fixtures", "unref.js")); - done(); - } catch (e) { - done(e); - } - }); - - test("should work with relative files", done => { - try { - bunRunAsScript(testDir, path.join(import.meta.dir, "fixtures", "relative.js")); - done(); - } catch (e) { - done(e); - } - }); - - test("add file/folder to folder", done => { - let count = 0; - const root = path.join(testDir, "add-directory"); - try { - fs.mkdirSync(root); - } catch {} - let err = undefined; - const watcher = fs.watch(root, { signal: AbortSignal.timeout(3000) }); - watcher.on("change", (event, filename) => { - count++; - try { - expect(event).toBe("rename"); - expect(["new-file.txt", "new-folder.txt"]).toContain(filename); - if (count >= 2) { - watcher.close(); - } - } catch (e) { - err = e; - watcher.close(); - } - }); - - watcher.on("error", e => (err = e)); - watcher.on("close", () => { - clearInterval(interval); - done(err); - }); - - const interval = repeat(() => { - fs.writeFileSync(path.join(root, "new-file.txt"), "hello"); - fs.mkdirSync(path.join(root, "new-folder.txt")); - fs.rmdirSync(path.join(root, "new-folder.txt")); - }); - }); - - test("add file/folder to subfolder", done => { - let count = 0; - const root = path.join(testDir, "add-subdirectory"); - try { - fs.mkdirSync(root); - } catch {} - const subfolder = path.join(root, "subfolder"); - fs.mkdirSync(subfolder); - const watcher = fs.watch(root, { recursive: true, signal: AbortSignal.timeout(3000) }); - let err = undefined; - watcher.on("change", (event, filename) => { - const basename = path.basename(filename); - - if (basename === "subfolder") return; - count++; - try { - expect(event).toBe("rename"); - expect(["new-file.txt", "new-folder.txt"]).toContain(basename); - if (count >= 2) { - watcher.close(); - } - } catch (e) { - err = e; - watcher.close(); - } - }); - watcher.on("error", e => (err = e)); - watcher.on("close", () => { - clearInterval(interval); - done(err); - }); - - const interval = repeat(() => { - fs.writeFileSync(path.join(subfolder, "new-file.txt"), "hello"); - fs.mkdirSync(path.join(subfolder, "new-folder.txt")); - fs.rmdirSync(path.join(subfolder, "new-folder.txt")); - }); - }); - - test("should emit event when file is deleted", done => { - const testsubdir = tempDirWithFiles("subdir", { - "deleted.txt": "hello", - }); - const filepath = path.join(testsubdir, "deleted.txt"); - let err = undefined; - const watcher = fs.watch(testsubdir, function (event, filename) { - try { - expect(event).toBe("rename"); - expect(filename).toBe("deleted.txt"); - } catch (e) { - err = e; - } finally { - clearInterval(interval); - watcher.close(); - } - }); - - watcher.once("close", () => { - done(err); - }); - - const interval = repeat(() => { - fs.rmSync(filepath, { force: true }); - const fd = fs.openSync(filepath, "w"); - fs.closeSync(fd); - }); - }); - - test("should emit 'change' event when file is modified", done => { - const filepath = path.join(testDir, "watch.txt"); - - const watcher = fs.watch(filepath); - let err = undefined; - watcher.on("change", function (event, filename) { - try { - expect(event).toBe("change"); - expect(filename).toBe("watch.txt"); - } catch (e) { - err = e; - } finally { - clearInterval(interval); - watcher.close(); - } - }); - - watcher.once("close", () => { - done(err); - }); - - const interval = repeat(() => { - fs.writeFileSync(filepath, "world"); - }); - }); - - test("should error on invalid path", done => { - try { - fs.watch(path.join(testDir, "404.txt")); - done(new Error("should not reach here")); - } catch (err) { - expect(err).toBeInstanceOf(Error); - expect(err.code).toBe("ENOENT"); - expect(err.syscall).toBe("watch"); - done(); - } - }); - - const encodings = ["utf8", "buffer", "hex", "ascii", "base64", "utf16le", "ucs2", "latin1", "binary"]; - - test(`should work with encodings ${encodings.join(", ")}`, async () => { - const watchers = []; - const filepath = path.join(testDir, encodingFileName); - - const promises = []; - encodings.forEach(name => { - const encoded_filename = - name !== "buffer" ? Buffer.from(encodingFileName, "utf8").toString(name) : Buffer.from(encodingFileName); - - promises.push( - new Promise((resolve, reject) => { - watchers.push( - fs.watch(filepath, { encoding: name }, (event, filename) => { - try { - expect(event).toBe("change"); - - if (name !== "buffer") { - expect(filename).toBe(encoded_filename); - } else { - expect(filename).toBeInstanceOf(Buffer); - expect(filename.toString("utf8")).toBe(encodingFileName); - } - - resolve(); - } catch (e) { - reject(e); - } - }), - ); - }), - ); - }); - - const interval = repeat(() => { - fs.writeFileSync(filepath, "world"); - }); - - try { - await Promise.all(promises); - } finally { - clearInterval(interval); - watchers.forEach(watcher => watcher.close()); - } - }); - - test("should work with url", done => { - const filepath = path.join(testDir, "url.txt"); - try { - const watcher = fs.watch(pathToFileURL(filepath)); - let err = undefined; - watcher.on("change", function (event, filename) { - try { - expect(event).toBe("change"); - expect(filename).toBe("url.txt"); - } catch (e) { - err = e; - } finally { - clearInterval(interval); - watcher.close(); - } - }); - - watcher.once("close", () => { - done(err); - }); - - const interval = repeat(() => { - fs.writeFileSync(filepath, "world"); - }); - } catch (e) { - done(e); - } - }); - - test("calling close from error event should not throw", done => { - const filepath = path.join(testDir, "close.txt"); - try { - const ac = new AbortController(); - const watcher = fs.watch(pathToFileURL(filepath), { signal: ac.signal }); - watcher.once("error", () => { - try { - watcher.close(); - done(); - } catch (e) { - done("Should not error when calling close from error event"); - } - }); - ac.abort(); - } catch (e) { - done(e); - } - }); - - test("calling close from close event should not throw", done => { - const filepath = path.join(testDir, "close-close.txt"); - try { - const ac = new AbortController(); - const watcher = fs.watch(pathToFileURL(filepath), { signal: ac.signal }); - - watcher.once("close", () => { - try { - watcher.close(); - done(); - } catch (e) { - done("Should not error when calling close from close event"); - } - }); - - ac.abort(); - } catch (e) { - done(e); - } - }); - - test("Signal aborted after creating the watcher", async () => { - const filepath = path.join(testDir, "abort.txt"); - - const ac = new AbortController(); - const promise = new Promise((resolve, reject) => { - const watcher = fs.watch(filepath, { signal: ac.signal }); - watcher.once("error", err => (err.message === "The operation was aborted." ? resolve() : reject(err))); - watcher.once("close", () => reject()); - }); - await Bun.sleep(10); - ac.abort(); - await promise; - }); - - test("Signal aborted before creating the watcher", async () => { - const filepath = path.join(testDir, "abort.txt"); - - const signal = AbortSignal.abort(); - await new Promise((resolve, reject) => { - const watcher = fs.watch(filepath, { signal }); - watcher.once("error", err => (err.message === "The operation was aborted." ? resolve() : reject(err))); - watcher.once("close", () => reject()); - }); - }); - - test("should work with symlink", async () => { - const filepath = path.join(testDir, "sym-symlink2.txt"); - await fs.promises.symlink(path.join(testDir, "sym-sync.txt"), filepath); - - const interval = repeat(() => { - fs.writeFileSync(filepath, "hello"); - }); - - const promise = new Promise((resolve, reject) => { - let timeout = null; - const watcher = fs.watch(filepath, event => { - clearTimeout(timeout); - clearInterval(interval); - try { - resolve(event); - } catch (e) { - reject(e); - } finally { - watcher.close(); - } - }); - setTimeout(() => { - clearInterval(interval); - watcher?.close(); - reject("timeout"); - }, 3000); - }); - expect(promise).resolves.toBe("change"); - }); -}); - -describe("fs.promises.watch", () => { - test("add file/folder to folder", async () => { - let count = 0; - const root = path.join(testDir, "add-promise-directory"); - try { - fs.mkdirSync(root); - } catch {} - let success = false; - let err = undefined; - try { - const ac = new AbortController(); - const watcher = fs.promises.watch(root, { signal: ac.signal }); - - const interval = repeat(() => { - fs.writeFileSync(path.join(root, "new-file.txt"), "hello"); - fs.mkdirSync(path.join(root, "new-folder.txt")); - fs.rmdirSync(path.join(root, "new-folder.txt")); - }); - - for await (const event of watcher) { - count++; - try { - expect(event.eventType).toBe("rename"); - expect(["new-file.txt", "new-folder.txt"]).toContain(event.filename); - - if (count >= 2) { - success = true; - clearInterval(interval); - ac.abort(); - } - } catch (e) { - err = e; - clearInterval(interval); - ac.abort(); - } - } - } catch (e) { - if (!success) { - throw err || e; - } - } - }); - - test("add file/folder to subfolder", async () => { - let count = 0; - const root = path.join(testDir, "add-promise-subdirectory"); - try { - fs.mkdirSync(root); - } catch {} - const subfolder = path.join(root, "subfolder"); - fs.mkdirSync(subfolder); - let success = false; - let err = undefined; - - try { - const ac = new AbortController(); - const watcher = fs.promises.watch(root, { recursive: true, signal: ac.signal }); - - const interval = repeat(() => { - fs.writeFileSync(path.join(subfolder, "new-file.txt"), "hello"); - fs.mkdirSync(path.join(subfolder, "new-folder.txt")); - fs.rmdirSync(path.join(subfolder, "new-folder.txt")); - }); - for await (const event of watcher) { - const basename = path.basename(event.filename); - if (basename === "subfolder") continue; - - count++; - try { - expect(event.eventType).toBe("rename"); - expect(["new-file.txt", "new-folder.txt"]).toContain(basename); - - if (count >= 2) { - success = true; - clearInterval(interval); - ac.abort(); - } - } catch (e) { - err = e; - clearInterval(interval); - ac.abort(); - } - } - } catch (e) { - if (!success) { - throw err || e; - } - } - }); - - test("Signal aborted after creating the watcher", async () => { - const filepath = path.join(testDir, "abort.txt"); - - const ac = new AbortController(); - const watcher = fs.promises.watch(filepath, { signal: ac.signal }); - - const promise = (async () => { - try { - for await (const _ of watcher); - } catch (e) { - expect(e.message).toBe("The operation was aborted."); - } - })(); - await Bun.sleep(10); - ac.abort(); - await promise; - }); - - test("Signal aborted before creating the watcher", async () => { - const filepath = path.join(testDir, "abort.txt"); - - const signal = AbortSignal.abort(); - const watcher = fs.promises.watch(filepath, { signal }); - await (async () => { - try { - for await (const _ of watcher); - } catch (e) { - expect(e.message).toBe("The operation was aborted."); - } - })(); - }); - - test("should work with symlink", async () => { - const filepath = path.join(testDir, "sym-symlink.txt"); - await fs.promises.symlink(path.join(testDir, "sym.txt"), filepath); - - const watcher = fs.promises.watch(filepath); - const interval = repeat(() => { - fs.writeFileSync(filepath, "hello"); - }); - - const promise = (async () => { - try { - for await (const event of watcher) { - return event.eventType; - } - } catch (e) { - expect("unreacheable").toBe(false); - } finally { - clearInterval(interval); - } - })(); - expect(promise).resolves.toBe("change"); - }); -}); diff --git a/test/js/node/watch/fs.watch.test.ts b/test/js/node/watch/fs.watch.test.ts new file mode 100644 index 000000000..aa7959bed --- /dev/null +++ b/test/js/node/watch/fs.watch.test.ts @@ -0,0 +1,522 @@ +import fs, { FSWatcher } from "node:fs"; +import path from "path"; +import { tempDirWithFiles, bunRun, bunRunAsScript } from "harness"; +import { pathToFileURL } from "bun"; + +import { describe, expect, test } from "bun:test"; +// Because macOS (and possibly other operating systems) can return a watcher +// before it is actually watching, we need to repeat the operation to avoid +// a race condition. +function repeat(fn: any) { + const interval = setInterval(fn, 20); + return interval; +} +const encodingFileName = `新建文夹件.txt`; +const testDir = tempDirWithFiles("watch", { + "watch.txt": "hello", + "relative.txt": "hello", + "abort.txt": "hello", + "url.txt": "hello", + "close.txt": "hello", + "close-close.txt": "hello", + "sym-sync.txt": "hello", + "sym.txt": "hello", + [encodingFileName]: "hello", +}); + +describe("fs.watch", () => { + test("non-persistent watcher should not block the event loop", done => { + try { + // https://github.com/joyent/node/issues/2293 - non-persistent watcher should not block the event loop + bunRun(path.join(import.meta.dir, "fixtures", "persistent.js")); + done(); + } catch (e: any) { + done(e); + } + }); + + test("watcher should close and not block the event loop", done => { + try { + bunRun(path.join(import.meta.dir, "fixtures", "close.js")); + done(); + } catch (e: any) { + done(e); + } + }); + + test("unref watcher should not block the event loop", done => { + try { + bunRun(path.join(import.meta.dir, "fixtures", "unref.js")); + done(); + } catch (e: any) { + done(e); + } + }); + + test("should work with relative files", done => { + try { + bunRunAsScript(testDir, path.join(import.meta.dir, "fixtures", "relative.js")); + done(); + } catch (e: any) { + done(e); + } + }); + + test("add file/folder to folder", done => { + let count = 0; + const root = path.join(testDir, "add-directory"); + try { + fs.mkdirSync(root); + } catch {} + let err: Error | undefined = undefined; + const watcher = fs.watch(root, { signal: AbortSignal.timeout(3000) }); + watcher.on("change", (event, filename) => { + count++; + try { + expect(event).toBe("rename"); + expect(["new-file.txt", "new-folder.txt"]).toContain(filename); + if (count >= 2) { + watcher.close(); + } + } catch (e: any) { + err = e; + watcher.close(); + } + }); + + watcher.on("error", e => (err = e)); + watcher.on("close", () => { + clearInterval(interval); + done(err); + }); + + const interval = repeat(() => { + fs.writeFileSync(path.join(root, "new-file.txt"), "hello"); + fs.mkdirSync(path.join(root, "new-folder.txt")); + fs.rmdirSync(path.join(root, "new-folder.txt")); + }); + }); + + test("add file/folder to subfolder", done => { + let count = 0; + const root = path.join(testDir, "add-subdirectory"); + try { + fs.mkdirSync(root); + } catch {} + const subfolder = path.join(root, "subfolder"); + fs.mkdirSync(subfolder); + const watcher = fs.watch(root, { recursive: true, signal: AbortSignal.timeout(3000) }); + let err: Error | undefined = undefined; + watcher.on("change", (event, filename) => { + const basename = path.basename(filename as string); + + if (basename === "subfolder") return; + count++; + try { + expect(event).toBe("rename"); + expect(["new-file.txt", "new-folder.txt"]).toContain(basename); + if (count >= 2) { + watcher.close(); + } + } catch (e: any) { + err = e; + watcher.close(); + } + }); + watcher.on("error", e => (err = e)); + watcher.on("close", () => { + clearInterval(interval); + done(err); + }); + + const interval = repeat(() => { + fs.writeFileSync(path.join(subfolder, "new-file.txt"), "hello"); + fs.mkdirSync(path.join(subfolder, "new-folder.txt")); + fs.rmdirSync(path.join(subfolder, "new-folder.txt")); + }); + }); + + test("should emit event when file is deleted", done => { + const testsubdir = tempDirWithFiles("subdir", { + "deleted.txt": "hello", + }); + const filepath = path.join(testsubdir, "deleted.txt"); + let err: Error | undefined = undefined; + const watcher = fs.watch(testsubdir, function (event, filename) { + try { + expect(event).toBe("rename"); + expect(filename).toBe("deleted.txt"); + } catch (e: any) { + err = e; + } finally { + clearInterval(interval); + watcher.close(); + } + }); + + watcher.once("close", () => { + done(err); + }); + + const interval = repeat(() => { + fs.rmSync(filepath, { force: true }); + const fd = fs.openSync(filepath, "w"); + fs.closeSync(fd); + }); + }); + + test("should emit 'change' event when file is modified", done => { + const filepath = path.join(testDir, "watch.txt"); + + const watcher = fs.watch(filepath); + let err: Error | undefined = undefined; + watcher.on("change", function (event, filename) { + try { + expect(event).toBe("change"); + expect(filename).toBe("watch.txt"); + } catch (e: any) { + err = e; + } finally { + clearInterval(interval); + watcher.close(); + } + }); + + watcher.once("close", () => { + done(err); + }); + + const interval = repeat(() => { + fs.writeFileSync(filepath, "world"); + }); + }); + + test("should error on invalid path", done => { + try { + fs.watch(path.join(testDir, "404.txt")); + done(new Error("should not reach here")); + } catch (err: any) { + expect(err).toBeInstanceOf(Error); + expect(err.code).toBe("ENOENT"); + expect(err.syscall).toBe("watch"); + done(); + } + }); + + const encodings = ["utf8", "buffer", "hex", "ascii", "base64", "utf16le", "ucs2", "latin1", "binary"] as const; + + test(`should work with encodings ${encodings.join(", ")}`, async () => { + const watchers: FSWatcher[] = []; + const filepath = path.join(testDir, encodingFileName); + + const promises: Promise[] = []; + encodings.forEach(name => { + const encoded_filename = + name !== "buffer" ? Buffer.from(encodingFileName, "utf8").toString(name) : Buffer.from(encodingFileName); + + promises.push( + new Promise((resolve, reject) => { + watchers.push( + fs.watch(filepath, { encoding: name }, (event, filename) => { + try { + expect(event).toBe("change"); + + if (name !== "buffer") { + expect(filename).toBe(encoded_filename); + } else { + expect(filename).toBeInstanceOf(Buffer); + expect((filename as any as Buffer)!.toString("utf8")).toBe(encodingFileName); + } + + resolve(undefined); + } catch (e: any) { + reject(e); + } + }), + ); + }), + ); + }); + + const interval = repeat(() => { + fs.writeFileSync(filepath, "world"); + }); + + try { + await Promise.all(promises); + } finally { + clearInterval(interval); + watchers.forEach(watcher => watcher.close()); + } + }); + + test("should work with url", done => { + const filepath = path.join(testDir, "url.txt"); + try { + const watcher = fs.watch(pathToFileURL(filepath)); + let err: Error | undefined = undefined; + watcher.on("change", function (event, filename) { + try { + expect(event).toBe("change"); + expect(filename).toBe("url.txt"); + } catch (e: any) { + err = e; + } finally { + clearInterval(interval); + watcher.close(); + } + }); + + watcher.once("close", () => { + done(err); + }); + + const interval = repeat(() => { + fs.writeFileSync(filepath, "world"); + }); + } catch (e: any) { + done(e); + } + }); + + test("calling close from error event should not throw", done => { + const filepath = path.join(testDir, "close.txt"); + try { + const ac = new AbortController(); + const watcher = fs.watch(pathToFileURL(filepath), { signal: ac.signal }); + watcher.once("error", () => { + try { + watcher.close(); + done(); + } catch (e: any) { + done("Should not error when calling close from error event"); + } + }); + ac.abort(); + } catch (e: any) { + done(e); + } + }); + + test("calling close from close event should not throw", done => { + const filepath = path.join(testDir, "close-close.txt"); + try { + const ac = new AbortController(); + const watcher = fs.watch(pathToFileURL(filepath), { signal: ac.signal }); + + watcher.once("close", () => { + try { + watcher.close(); + done(); + } catch (e: any) { + done("Should not error when calling close from close event"); + } + }); + + ac.abort(); + } catch (e: any) { + done(e); + } + }); + + test("Signal aborted after creating the watcher", async () => { + const filepath = path.join(testDir, "abort.txt"); + + const ac = new AbortController(); + const promise = new Promise((resolve, reject) => { + const watcher = fs.watch(filepath, { signal: ac.signal }); + watcher.once("error", err => (err.message === "The operation was aborted." ? resolve(undefined) : reject(err))); + watcher.once("close", () => reject()); + }); + await Bun.sleep(10); + ac.abort(); + await promise; + }); + + test("Signal aborted before creating the watcher", async () => { + const filepath = path.join(testDir, "abort.txt"); + + const signal = AbortSignal.abort(); + await new Promise((resolve, reject) => { + const watcher = fs.watch(filepath, { signal }); + watcher.once("error", err => (err.message === "The operation was aborted." ? resolve(undefined) : reject(err))); + watcher.once("close", () => reject()); + }); + }); + + test("should work with symlink", async () => { + const filepath = path.join(testDir, "sym-symlink2.txt"); + await fs.promises.symlink(path.join(testDir, "sym-sync.txt"), filepath); + + const interval = repeat(() => { + fs.writeFileSync(filepath, "hello"); + }); + + const promise = new Promise((resolve, reject) => { + let timeout: any = null; + const watcher = fs.watch(filepath, event => { + clearTimeout(timeout); + clearInterval(interval); + try { + resolve(event); + } catch (e: any) { + reject(e); + } finally { + watcher.close(); + } + }); + setTimeout(() => { + clearInterval(interval); + watcher?.close(); + reject("timeout"); + }, 3000); + }); + expect(promise).resolves.toBe("change"); + }); +}); + +describe("fs.promises.watch", () => { + test("add file/folder to folder", async () => { + let count = 0; + const root = path.join(testDir, "add-promise-directory"); + try { + fs.mkdirSync(root); + } catch {} + let success = false; + let err: Error | undefined = undefined; + try { + const ac = new AbortController(); + const watcher = fs.promises.watch(root, { signal: ac.signal }); + + const interval = repeat(() => { + fs.writeFileSync(path.join(root, "new-file.txt"), "hello"); + fs.mkdirSync(path.join(root, "new-folder.txt")); + fs.rmdirSync(path.join(root, "new-folder.txt")); + }); + + for await (const event of watcher) { + count++; + try { + expect(event.eventType).toBe("rename"); + expect(["new-file.txt", "new-folder.txt"]).toContain(event.filename); + + if (count >= 2) { + success = true; + clearInterval(interval); + ac.abort(); + } + } catch (e: any) { + err = e; + clearInterval(interval); + ac.abort(); + } + } + } catch (e: any) { + if (!success) { + throw err || e; + } + } + }); + + test("add file/folder to subfolder", async () => { + let count = 0; + const root = path.join(testDir, "add-promise-subdirectory"); + try { + fs.mkdirSync(root); + } catch {} + const subfolder = path.join(root, "subfolder"); + fs.mkdirSync(subfolder); + let success = false; + let err: Error | undefined = undefined; + + try { + const ac = new AbortController(); + const watcher = fs.promises.watch(root, { recursive: true, signal: ac.signal }); + + const interval = repeat(() => { + fs.writeFileSync(path.join(subfolder, "new-file.txt"), "hello"); + fs.mkdirSync(path.join(subfolder, "new-folder.txt")); + fs.rmdirSync(path.join(subfolder, "new-folder.txt")); + }); + for await (const event of watcher) { + const basename = path.basename(event.filename!); + if (basename === "subfolder") continue; + + count++; + try { + expect(event.eventType).toBe("rename"); + expect(["new-file.txt", "new-folder.txt"]).toContain(basename); + + if (count >= 2) { + success = true; + clearInterval(interval); + ac.abort(); + } + } catch (e: any) { + err = e; + clearInterval(interval); + ac.abort(); + } + } + } catch (e: any) { + if (!success) { + throw err || e; + } + } + }); + + test("Signal aborted after creating the watcher", async () => { + const filepath = path.join(testDir, "abort.txt"); + + const ac = new AbortController(); + const watcher = fs.promises.watch(filepath, { signal: ac.signal }); + + const promise = (async () => { + try { + for await (const _ of watcher); + } catch (e: any) { + expect(e.message).toBe("The operation was aborted."); + } + })(); + await Bun.sleep(10); + ac.abort(); + await promise; + }); + + test("Signal aborted before creating the watcher", async () => { + const filepath = path.join(testDir, "abort.txt"); + + const signal = AbortSignal.abort(); + const watcher = fs.promises.watch(filepath, { signal }); + await (async () => { + try { + for await (const _ of watcher); + } catch (e: any) { + expect(e.message).toBe("The operation was aborted."); + } + })(); + }); + + test("should work with symlink", async () => { + const filepath = path.join(testDir, "sym-symlink.txt"); + await fs.promises.symlink(path.join(testDir, "sym.txt"), filepath); + + const watcher = fs.promises.watch(filepath); + const interval = repeat(() => { + fs.writeFileSync(filepath, "hello"); + }); + + const promise = (async () => { + try { + for await (const event of watcher) { + return event.eventType; + } + } catch (e: any) { + expect("unreacheable").toBe(false); + } finally { + clearInterval(interval); + } + })(); + expect(promise).resolves.toBe("change"); + }); +}); diff --git a/test/js/third_party/socket.io/support/util.ts b/test/js/third_party/socket.io/support/util.ts index 597b40d65..b5f515568 100644 --- a/test/js/third_party/socket.io/support/util.ts +++ b/test/js/third_party/socket.io/support/util.ts @@ -1,3 +1,4 @@ +// @ts-nocheck import type { Server } from "socket.io"; import request from "supertest"; diff --git a/test/js/web/html/FormData.test.ts b/test/js/web/html/FormData.test.ts index cbaf5aaa7..45b4f2f5a 100644 --- a/test/js/web/html/FormData.test.ts +++ b/test/js/web/html/FormData.test.ts @@ -301,17 +301,18 @@ describe("FormData", () => { expect(await (body.get("foo") as Blob).text()).toBe("baz"); server.stop(true); }); - + type FetchReqArgs = [request: Request, init?: RequestInit]; + type FetchURLArgs = [url: string | URL | Request, init?: FetchRequestInit]; for (let useRequestConstructor of [true, false]) { describe(useRequestConstructor ? "Request constructor" : "fetch()", () => { - function send(args: Parameters) { + function send(args: FetchReqArgs | FetchURLArgs) { if (useRequestConstructor) { - return fetch(new Request(...args)); + return fetch(new Request(...(args as FetchReqArgs))); } else { - return fetch(...args); + return fetch(...(args as FetchURLArgs)); } } - for (let headers of [{}, undefined, { headers: { X: "Y" } }]) { + for (let headers of [{} as {}, undefined, { headers: { X: "Y" } }]) { describe("headers: " + Bun.inspect(headers).replaceAll(/([\n ])/gim, ""), () => { it("send on HTTP server with FormData & Blob (roundtrip)", async () => { let contentType = ""; @@ -330,11 +331,10 @@ describe("FormData", () => { form.append("bar", "baz"); // @ts-ignore - const reqBody = [ + const reqBody: FetchURLArgs = [ `http://${server.hostname}:${server.port}`, { body: form, - headers, method: "POST", }, @@ -364,7 +364,6 @@ describe("FormData", () => { form.append("foo", file); form.append("bar", "baz"); - // @ts-ignore const reqBody = [ `http://${server.hostname}:${server.port}`, { @@ -374,7 +373,7 @@ describe("FormData", () => { method: "POST", }, ]; - const res = await send(reqBody); + const res = await send(reqBody as FetchURLArgs); const body = await res.formData(); expect(await (body.get("foo") as Blob).text()).toBe(text); expect(contentType).toContain("multipart/form-data"); @@ -410,7 +409,7 @@ describe("FormData", () => { method: "POST", }, ]; - const res = await send(reqBody); + const res = await send(reqBody as FetchURLArgs); const body = await res.formData(); expect(contentType).toContain("multipart/form-data"); expect(body.get("foo")).toBe("boop"); diff --git a/test/js/web/html/URLSearchParams.test.ts b/test/js/web/html/URLSearchParams.test.ts index 120bb2321..41c42c25d 100644 --- a/test/js/web/html/URLSearchParams.test.ts +++ b/test/js/web/html/URLSearchParams.test.ts @@ -7,15 +7,20 @@ describe("URLSearchParams", () => { params.append("foo", "bar"); params.append("foo", "boop"); params.append("bar", "baz"); + // @ts-ignore expect(params.length).toBe(3); params.delete("foo"); + // @ts-ignore expect(params.length).toBe(1); params.append("foo", "bar"); + // @ts-ignore expect(params.length).toBe(2); params.delete("foo"); params.delete("foo"); + // @ts-ignore expect(params.length).toBe(1); params.delete("bar"); + // @ts-ignore expect(params.length).toBe(0); }); diff --git a/test/package.json b/test/package.json index 5529d3f20..75f8d35e3 100644 --- a/test/package.json +++ b/test/package.json @@ -8,6 +8,7 @@ "@swc/core": "1.3.38", "@types/react": "18.0.28", "@types/react-dom": "18.0.11", + "@types/supertest": "2.0.12", "bktree-fast": "0.0.7", "body-parser": "1.20.2", "dedent": "0.7.0", @@ -17,6 +18,9 @@ "jest-extended": "4.0.0", "lodash": "4.17.21", "nodemailer": "6.9.3", + "pg": "8.11.1", + "pg-connection-string": "2.6.1", + "postgres": "3.3.5", "prisma": "4.15.0", "socket.io": "4.7.1", "socket.io-client": "4.7.1", @@ -26,10 +30,7 @@ "undici": "5.20.0", "vitest": "0.32.2", "webpack": "5.88.0", - "webpack-cli": "4.7.2", - "pg": "8.11.1", - "postgres": "3.3.5", - "pg-connection-string": "2.6.1" + "webpack-cli": "4.7.2" }, "private": true, "scripts": { diff --git a/test/tsconfig.json b/test/tsconfig.json index 67f706cdf..a5e77bf59 100644 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -1,13 +1,8 @@ { - "include": [ - ".", - "../packages/bun-types/index.d.ts" - ], + "include": [".", "../packages/bun-types/index.d.ts"], "compilerOptions": { "noEmit": true, - "lib": [ - "ESNext" - ], + "lib": ["ESNext"], "module": "ESNext", "target": "ESNext", "moduleResolution": "bundler", @@ -23,25 +18,14 @@ "resolveJsonModule": true, "baseUrl": ".", "paths": { - "harness": [ - "harness.ts" - ], - "mkfifo": [ - "mkfifo.ts" - ], - "node-harness": [ - "js/node/harness.ts" - ], - "deno:harness": [ - "js/deno/harness.ts" - ], - "foo/bar": [ - "js/bun/resolve/baz.js" - ], - "@faasjs/*": [ - "js/bun/resolve/*.js" - ] + "harness": ["harness.ts"], + "mkfifo": ["mkfifo.ts"], + "node-harness": ["js/node/harness.ts"], + "deno:harness": ["js/deno/harness.ts"], + "foo/bar": ["js/bun/resolve/baz.js"], + "@faasjs/*": ["js/bun/resolve/*.js"] } }, + "exclude": ["bundler/fixtures", "snapshots", "js/deno"] } diff --git a/tsconfig.json b/tsconfig.json index 0ee640ea0..d8be0da03 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,12 +6,10 @@ // "skipLibCheck": true, "allowJs": true }, - "include": [ - ".", - "packages/bun-types/index.d.ts" - ], + "include": [".", "packages/bun-types/index.d.ts"], "exclude": [ "src/test", + // "src/js/builtins", "packages", "bench", "examples/*/*", @@ -21,5 +19,6 @@ "src/bun.js/WebKit", "src/api/demo", "node_modules" - ] + ], + "files": ["src/js/builtins/builtins.d.ts"] } -- cgit v1.2.3