Skip to content

Commit

Permalink
Set the busboy option defParamCharset to utf8.
Browse files Browse the repository at this point in the history
  • Loading branch information
krasivyy3954 committed Aug 1, 2022
1 parent ca3fae9 commit fb319c8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Patch

- Support non `latin1` characters in file names by setting the [`busboy`](https://npm.im/busboy) option `defParamCharset` to `utf8`, fixing [#328](https://github.com/jaydenseric/graphql-upload/issues/328).
- Removed a redundant `@ts-ignore` comment.

## 16.0.0
Expand Down
1 change: 1 addition & 0 deletions processRequest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default function processRequest(

const parser = busboy({
headers: request.headers,
defParamCharset: "utf8",
limits: {
fieldSize: maxFieldSize,
fields: 2, // Only operations and map.
Expand Down
63 changes: 62 additions & 1 deletion processRequest.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default (tests) => {
});

tests.add(
"`processRequest` with a single file and default `createReadStream` options.",
"`processRequest` with a single file, default `createReadStream` options, file name chars `latin1`.",
async () => {
let serverError;

Expand Down Expand Up @@ -113,6 +113,67 @@ export default (tests) => {
}
);

tests.add(
"`processRequest` with a single file, default `createReadStream` options, file name chars non `latin1`.",
async () => {
const fileName = "你好.txt";

let serverError;

const server = createServer(async (request, response) => {
try {
const operation =
/**
* @type {{
* variables: {
* file: Upload,
* },
* }}
*/
(await processRequest(request, response));

ok(operation.variables.file instanceof Upload);

const upload = await operation.variables.file.promise;

strictEqual(upload.filename, fileName);
strictEqual(upload.mimetype, "text/plain");
strictEqual(upload.encoding, "7bit");

const stream = upload.createReadStream();

ok(stream instanceof ReadStream);
strictEqual(stream.readableEncoding, null);
strictEqual(stream.readableHighWaterMark, 16384);
strictEqual(await streamToString(stream), "a");
} catch (error) {
serverError = error;
} finally {
response.end();
}
});

const { port, close } = await listen(server);

try {
const body = new FormData();

body.append(
"operations",
JSON.stringify({ variables: { file: null } })
);
body.append("map", JSON.stringify({ 1: ["variables.file"] }));
body.append("1", new File(["a"], fileName, { type: "text/plain" }));

await fetch(`http://localhost:${port}`, { method: "POST", body });

if (serverError) throw serverError;
} finally {
close();
}
}
);

tests.add(
"`processRequest` with a single file and custom `createReadStream` options.",
async () => {
Expand Down

0 comments on commit fb319c8

Please sign in to comment.