Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/node): don't encode buffer data as utf8 in node:http2 #24016

Merged
merged 5 commits into from
May 29, 2024
Merged
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
14 changes: 6 additions & 8 deletions ext/node/polyfills/http2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -933,25 +933,23 @@ export class ClientHttp2Stream extends Duplex {

// TODO(bartlomieju): clean up
_write(chunk, encoding, callback?: () => void) {
debugHttp2(">>> _write", callback);
debugHttp2(">>> _write", encoding, callback);
if (typeof encoding === "function") {
callback = encoding;
encoding = "utf8";
encoding = this.#encoding;
}
let data;
if (typeof encoding === "string") {
if (encoding === "utf8") {
data = ENCODER.encode(chunk);
} else {
} else if (encoding === "buffer") {
this.#encoding = encoding;
data = chunk.buffer;
}

this.#requestPromise
.then(() => {
debugHttp2(">>> _write", this.#rid, data, encoding, callback);
return op_http2_client_send_data(
this.#rid,
data,
);
return op_http2_client_send_data(this.#rid, new Uint8Array(data));
})
.then(() => {
callback?.();
Expand Down
39 changes: 39 additions & 0 deletions tests/unit_node/http2_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import * as http2 from "node:http2";
import { Buffer } from "node:buffer";
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import * as net from "node:net";
import { assert, assertEquals } from "@std/assert/mod.ts";
import { curlRequest } from "../unit/test_util.ts";
Expand Down Expand Up @@ -164,3 +167,39 @@ Deno.test("[node/http2.createServer()]", {
// Issue: https://github.com/denoland/deno/issues/22764
await new Promise<void>((resolve) => server.on("close", resolve));
});

Deno.test("[node/http2 client] write image buffer on request stream works", async () => {
const url = "https://localhost:5545";
const client = http2.connect(url);
client.on("error", (err) => console.error(err));

const imagePath = join(import.meta.dirname!, "testdata", "green.jpg");
const buffer = await readFile(imagePath);
const req = client.request({ ":method": "POST", ":path": "/echo_server" });
req.write(buffer, (err) => {
if (err) throw err;
});

let receivedData: Buffer;
req.on("data", (chunk) => {
if (!receivedData) {
receivedData = chunk;
} else {
receivedData = Buffer.concat([receivedData, chunk]);
}
});
req.end();

const endPromise = Promise.withResolvers<void>();
setTimeout(() => {
try {
client.close();
} catch (_) {
// pass
}
endPromise.resolve();
}, 2000);
littledivy marked this conversation as resolved.
Show resolved Hide resolved

await endPromise.promise;
assertEquals(receivedData!, buffer);
});
Binary file added tests/unit_node/testdata/green.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.