fix readableStreamToArrayBuffer (#3181)

* fix discord.js again

* remove one of the async hooks warnings

* clarify hardcoded modules docs
This commit is contained in:
dave caruso
2023-06-02 22:03:16 -04:00
committed by GitHub
parent 51846d0277
commit f798a0cfe8
6 changed files with 51 additions and 23 deletions

View File

@@ -148,11 +148,17 @@ $ make generate-sink
You probably won't need to run that one much.
## Modifying ESM core modules
## Modifying ESM hardcoded modules
Certain modules like `node:fs`, `node:path`, `node:stream`, and `bun:sqlite` are implemented in JavaScript. These live in `src/bun.js/*.exports.js` files.
Certain modules like `node:fs`, `node:stream`, `bun:sqlite`, and `ws` are implemented in JavaScript. These live in `src/js/{node,bun,thirdparty}` files and are pre-bundled using Bun. The bundled code is committed so CI builds can run without needing a copy of Bun.
While Bun is in beta, you can modify them at runtime in release builds via the environment variable `BUN_OVERRIDE_MODULE_PATH`. When set, Bun will look in the override directory for `<name>.exports.js` before checking the files from `src/bun.js` (which are now baked in to the binary). This lets you test changes to the ESM modules without needing to re-compile Bun.
When these are changed, run:
```
$ make hardcoded
```
In debug builds, Bun automatically loads these from the filesystem, wherever it was compiled, so no need to re-run `make dev`. In release builds, this same behavior can be done via the environment variable `BUN_OVERRIDE_MODULE_PATH`. When set to the repository root, Bun will read from the bundled modules in the repository instead of the ones baked into the binary.
## Release build

View File

@@ -281,7 +281,7 @@ declare module "bun" {
* @returns A promise that resolves with the concatenated chunks or the concatenated chunks as an `ArrayBuffer`.
*/
export function readableStreamToArrayBuffer(
stream: ReadableStream<ArrayBuffer>,
stream: ReadableStream<ArrayBufferView | ArrayBufferLike>,
): Promise<ArrayBuffer> | ArrayBuffer;
/**

View File

@@ -132,12 +132,14 @@ export function readableStreamToArrayBuffer(stream: ReadableStream<ArrayBuffer>)
return $readableStreamToArrayBufferDirect(stream, underlyingSource);
}
var array = Bun.readableStreamToArray(stream);
if ($isPromise(array)) {
return array.$then(Bun.concatArrayBuffers);
var result = Bun.readableStreamToArray(stream);
if ($isPromise(result)) {
// `result` is an InternalPromise, which doesn't have a `.$then` method
// but `.then` isn't user-overridable, so we can use it safely.
return result.then(Bun.concatArrayBuffers);
}
return Bun.concatArrayBuffers(array);
return Bun.concatArrayBuffers(result);
}
$linkTimeConstant;

View File

@@ -5,7 +5,9 @@ var drainMicrotasks = () => {
};
var notImplemented = () => {
console.warn("[bun]: async_hooks has not been implemented yet. See https://github.com/oven-sh/bun/issues/1832");
console.warn(
"[bun] Warning: async_hooks has not been implemented yet. See https://github.com/oven-sh/bun/issues/1832",
);
notImplemented = () => {};
};
@@ -146,19 +148,11 @@ class AsyncResource {
constructor(type, triggerAsyncId) {
this.type = type;
this.triggerAsyncId = triggerAsyncId;
if (AsyncResource.allowedRunInAsyncScope.has(type)) {
this.runInAsyncScope = this.#runInAsyncScope;
}
}
type;
triggerAsyncId;
// We probably will not fully support AsyncResource
// But some packages in the wild do depend on it
static allowedRunInAsyncScope = new Set(["prisma-client-request"]);
emitBefore() {
return true;
}
@@ -169,10 +163,7 @@ class AsyncResource {
emitDestroy() {}
runInAsyncScope;
#runInAsyncScope(fn, ...args) {
notImplemented();
runInAsyncScope(fn, ...args) {
var result, err;
process.nextTick(fn => {
try {

View File

@@ -2420,9 +2420,9 @@ const char* const s_readableStreamReadableStreamToTextCode = "(function (p){\"us
const JSC::ConstructAbility s_readableStreamReadableStreamToArrayBufferCodeConstructAbility = JSC::ConstructAbility::CannotConstruct;
const JSC::ConstructorKind s_readableStreamReadableStreamToArrayBufferCodeConstructorKind = JSC::ConstructorKind::None;
const JSC::ImplementationVisibility s_readableStreamReadableStreamToArrayBufferCodeImplementationVisibility = JSC::ImplementationVisibility::Private;
const int s_readableStreamReadableStreamToArrayBufferCodeLength = 271;
const int s_readableStreamReadableStreamToArrayBufferCodeLength = 270;
static const JSC::Intrinsic s_readableStreamReadableStreamToArrayBufferCodeIntrinsic = JSC::NoIntrinsic;
const char* const s_readableStreamReadableStreamToArrayBufferCode = "(function (_){\"use strict\";var b=@getByIdDirectPrivate(_,\"underlyingSource\");if(b!==@undefined)return @readableStreamToArrayBufferDirect(_,b);var p=@Bun.readableStreamToArray(_);if(@isPromise(p))return p.@then(@Bun.concatArrayBuffers);return @Bun.concatArrayBuffers(p)})\n";
const char* const s_readableStreamReadableStreamToArrayBufferCode = "(function (_){\"use strict\";var b=@getByIdDirectPrivate(_,\"underlyingSource\");if(b!==@undefined)return @readableStreamToArrayBufferDirect(_,b);var p=@Bun.readableStreamToArray(_);if(@isPromise(p))return p.then(@Bun.concatArrayBuffers);return @Bun.concatArrayBuffers(p)})\n";
// readableStreamToJSON
const JSC::ConstructAbility s_readableStreamReadableStreamToJSONCodeConstructAbility = JSC::ConstructAbility::CannotConstruct;

View File

@@ -0,0 +1,29 @@
import { test, expect } from "bun:test";
test("readableStreamToArrayBuffer works", async () => {
// the test calls InternalPromise.then. this test ensures that such function is not user-overridable.
let _then = Promise.prototype.then;
let counter = 0;
// @ts-ignore
Promise.prototype.then = (...args) => {
counter++;
return _then.apply(this, args);
};
try {
const result = await Bun.readableStreamToArrayBuffer(
new ReadableStream({
async start(controller) {
controller.enqueue(new TextEncoder().encode("bun is"));
controller.enqueue(new TextEncoder().encode(" awesome!"));
controller.close();
},
}),
);
expect(counter).toBe(0);
expect(new TextDecoder().decode(result)).toBe("bun is awesome!");
} catch (error) {
throw error;
} finally {
Promise.prototype.then = _then;
}
});