Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/jsc/bindings/ModuleLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,10 @@ OnLoadResult handleOnLoadResultNotPromise(Zig::GlobalObject* globalObject, JSC::
result.value.sourceText.value = contentsValue;
}
} else if (JSC::JSArrayBufferView* view = dynamicDowncast<JSC::JSArrayBufferView>(contentsValue)) {
result.value.sourceText.string = ZigString { reinterpret_cast<const unsigned char*>(view->vector()), view->byteLength() };
// Raw bytes from a typed array are opaque to us: tag as UTF-8 so the
// Rust side's ZigString::to_slice() does not Latin-1 -> UTF-8 transcode
// bytes >= 0x80 (which would corrupt UTF-8 source and binary payloads).
result.value.sourceText.string = ZigString { Zig::taggedUTF8Ptr(reinterpret_cast<const unsigned char*>(view->vector())), view->byteLength() };
result.value.sourceText.value = contentsValue;
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/jsc/bindings/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ static const unsigned char* taggedUTF16Ptr(const char16_t* ptr)
return reinterpret_cast<const unsigned char*>(reinterpret_cast<uintptr_t>(ptr) | (static_cast<uint64_t>(1) << 63));
}

static const unsigned char* taggedUTF8Ptr(const unsigned char* ptr)
{
return reinterpret_cast<const unsigned char*>(reinterpret_cast<uintptr_t>(ptr) | (static_cast<uint64_t>(1) << 61));
}

static ZigString toZigString(WTF::String* str)
{
return str->isEmpty()
Expand Down
46 changes: 46 additions & 0 deletions test/js/bun/plugin/plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,39 @@ plugin({
},
});

plugin({
name: "typed array contents",
setup(builder) {
// UTF-8 source with a non-ASCII literal. The UTF-8 bytes for "é" are
// [0xC3, 0xA9]; if those bytes were (wrongly) Latin-1 decoded and then
// re-encoded as UTF-8 the parser would see "é" instead.
const jsWithUtf8 = Buffer.from('export default "é";\n', "utf8");

builder.module("buffer-module-sync", () => ({
contents: jsWithUtf8,
loader: "js",
}));

builder.module("buffer-module-async", async () => {
await 1;
return { contents: new Uint8Array(jsWithUtf8), loader: "js" };
});

builder.onResolve({ filter: /.*/, namespace: "buffer-contents" }, ({ path }) => ({
path,
namespace: "buffer-contents",
}));
builder.onLoad({ filter: /^sync$/, namespace: "buffer-contents" }, () => ({
contents: jsWithUtf8,
loader: "js",
}));
builder.onLoad({ filter: /^async$/, namespace: "buffer-contents" }, async () => {
await 1;
return { contents: jsWithUtf8, loader: "js" };
});
},
});

// This is to test that it works when imported from a separate file
import { bunEnv, bunExe, tempDir } from "harness";
import { render as svelteRender } from "svelte/server";
Expand Down Expand Up @@ -303,6 +336,19 @@ describe("module", () => {
});
});

describe("typed array contents", () => {
it.each([
["build.module sync", "buffer-module-sync"],
["build.module async", "buffer-module-async"],
["onLoad sync", "buffer-contents:sync"],
["onLoad async", "buffer-contents:async"],
])("passes raw bytes through for %s", async (_, specifier) => {
const { default: value } = await import(specifier);
expect(value).toBe("é");
expect([...Buffer.from(value, "utf8")]).toEqual([0xc3, 0xa9]);
});
});

describe("dynamic import", () => {
it("SSRs `<h1>Hello world!</h1>` with Svelte", async () => {
const { default: App }: any = await import("./hello.svelte");
Expand Down
Loading