Skip to content

Commit

Permalink
fix(server): copy ArrayBuffer instead of reusing it
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Jul 13, 2023
1 parent 8afe2ce commit 6082626
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 62 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-hairs-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@whatwg-node/server': patch
---

Make sure we copy the buffer from uWS
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@babel/preset-typescript": "7.22.5",
"@changesets/changelog-github": "0.4.8",
"@changesets/cli": "2.26.2",
"@theguild/prettier-config": "1.2.0",
"@theguild/prettier-config": "2.0.0",
"@types/jest": "29.5.3",
"@types/node": "18.16.19",
"@types/react-dom": "18.2.7",
Expand All @@ -51,7 +51,7 @@
"jest": "29.6.1",
"lint-staged": "13.2.3",
"patch-package": "7.0.2",
"prettier": "2.8.8",
"prettier": "3.0.0",
"ts-jest": "29.1.1",
"typescript": "5.0.4"
},
Expand Down
5 changes: 4 additions & 1 deletion packages/node-fetch/src/Blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ function isBlob(obj: any): obj is Blob {
export class PonyfillBlob implements Blob {
type: string;
private encoding: BufferEncoding;
constructor(private blobParts: BlobPart[], options?: BlobOptions) {
constructor(
private blobParts: BlobPart[],
options?: BlobOptions,
) {
this.type = options?.type || 'application/octet-stream';
this.encoding = options?.encoding || 'utf8';
}
Expand Down
6 changes: 5 additions & 1 deletion packages/node-fetch/src/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { PonyfillBlob } from './Blob.js';

export class PonyfillFile extends PonyfillBlob implements File {
public lastModified: number;
constructor(fileBits: BlobPart[], public name: string, options?: FilePropertyBag) {
constructor(
fileBits: BlobPart[],
public name: string,
options?: FilePropertyBag,
) {
super(fileBits, options);
this.lastModified = options?.lastModified || Date.now();
}
Expand Down
5 changes: 4 additions & 1 deletion packages/node-fetch/src/TextEncoderDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export class PonyfillTextEncoder implements TextEncoder {
export class PonyfillTextDecoder implements TextDecoder {
fatal = false;
ignoreBOM = false;
constructor(public encoding: BufferEncoding = 'utf-8', options: TextDecoderOptions) {
constructor(
public encoding: BufferEncoding = 'utf-8',
options: TextDecoderOptions,
) {
if (options) {
this.fatal = options.fatal || false;
this.ignoreBOM = options.ignoreBOM || false;
Expand Down
6 changes: 4 additions & 2 deletions packages/server/src/createServerAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ const EMPTY_OBJECT = {};

function createServerAdapter<
TServerContext = {},
THandleRequest extends ServerAdapterRequestHandler<TServerContext> = ServerAdapterRequestHandler<TServerContext>,
THandleRequest extends
ServerAdapterRequestHandler<TServerContext> = ServerAdapterRequestHandler<TServerContext>,
>(
serverAdapterRequestHandler: THandleRequest,
options?: ServerAdapterOptions<TServerContext>,
Expand All @@ -79,7 +80,8 @@ function createServerAdapter<
): ServerAdapter<TServerContext, TBaseObject>;
function createServerAdapter<
TServerContext = {},
THandleRequest extends ServerAdapterRequestHandler<TServerContext> = ServerAdapterRequestHandler<TServerContext>,
THandleRequest extends
ServerAdapterRequestHandler<TServerContext> = ServerAdapterRequestHandler<TServerContext>,
TBaseObject extends ServerAdapterBaseObject<
TServerContext,
THandleRequest
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export interface FetchEvent extends Event {

export interface ServerAdapterBaseObject<
TServerContext,
THandleRequest extends ServerAdapterRequestHandler<TServerContext> = ServerAdapterRequestHandler<TServerContext>,
THandleRequest extends
ServerAdapterRequestHandler<TServerContext> = ServerAdapterRequestHandler<TServerContext>,
> {
/**
* An async function that takes `Request` and the server context and returns a `Response`.
Expand Down
11 changes: 9 additions & 2 deletions packages/server/src/uwebsockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,18 @@ export function getRequestFromUWSRequest({ req, res, fetchAPI }: GetRequestFromU
res.onAborted(() => {
readable.push(null);
});
res.onData(function (chunk, isLast) {
readable.push(Buffer.from(chunk, 0, chunk.byteLength));
let multipleChunks = false;
res.onData(function (ab, isLast) {
const chunk = Buffer.from(ab, 0, ab.byteLength);
if (!multipleChunks && isLast) {
readable.push(chunk);
} else {
readable.push(Buffer.concat([chunk]));
}
if (isLast) {
readable.push(null);
}
multipleChunks = true;
});
}
const headers = new fetchAPI.Headers();
Expand Down
95 changes: 43 additions & 52 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
resolved "https://registry.yarnpkg.com/@babel/compat-data/-/compat-data-7.22.9.tgz#71cdb00a1ce3a329ce4cbec3a44f9fef35669730"
integrity sha512-5UamI7xkUcJ3i9qVDS+KFDEK8/7oJ55/sJMB1Ge7IEapr7KfdfV/HErR+koZwOfd+SgtFKOKRhRakdg++DcJpQ==

"@babel/[email protected]", "@babel/core@^7.11.6", "@babel/core@^7.12.3", "@babel/core@^7.17.7":
"@babel/[email protected]", "@babel/core@^7.11.6", "@babel/core@^7.12.3", "@babel/core@^7.21.8":
version "7.22.9"
resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.22.9.tgz#bd96492c68822198f33e8a256061da3cf391f58f"
integrity sha512-G2EgeufBcYw27U4hhoIwFcgc1XU7TlXJ3mv04oOv1WCuo900U/anZSPzEqNjwdjgffkk2Gs0AN0dW1CKVLcG7w==
Expand All @@ -57,7 +57,7 @@
json5 "^2.2.2"
semver "^6.3.1"

"@babel/generator@^7.17.7", "@babel/generator@^7.22.7", "@babel/generator@^7.22.9", "@babel/generator@^7.7.2":
"@babel/generator@^7.21.5", "@babel/generator@^7.22.7", "@babel/generator@^7.22.9", "@babel/generator@^7.7.2":
version "7.22.9"
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.22.9.tgz#572ecfa7a31002fa1de2a9d91621fd895da8493d"
integrity sha512-KtLMbmicyuK2Ak/FTCJVbDnkN1SlT8/kceFTiuDiiRUUSMnHMidxSCdG4ndkTOHHpoomWe/4xkvHkEOncwjYIw==
Expand Down Expand Up @@ -270,7 +270,7 @@
chalk "^2.0.0"
js-tokens "^4.0.0"

"@babel/parser@^7.1.0", "@babel/parser@^7.14.7", "@babel/parser@^7.17.7", "@babel/parser@^7.20.7", "@babel/parser@^7.22.5", "@babel/parser@^7.22.7":
"@babel/parser@^7.1.0", "@babel/parser@^7.14.7", "@babel/parser@^7.20.7", "@babel/parser@^7.21.8", "@babel/parser@^7.22.5", "@babel/parser@^7.22.7":
version "7.22.7"
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.22.7.tgz#df8cf085ce92ddbdbf668a7f186ce848c9036cae"
integrity sha512-7NF8pOkHP5o2vpmGgNGcfAeCvOYhGLyA3Z4eBQkT1RJlWu47n63bCs93QfJ2hIAFCil7L5P2IWhs1oToVgrL0Q==
Expand Down Expand Up @@ -987,7 +987,7 @@
"@babel/parser" "^7.22.5"
"@babel/types" "^7.22.5"

"@babel/traverse@^7.17.3", "@babel/traverse@^7.22.5", "@babel/traverse@^7.22.6", "@babel/traverse@^7.22.8":
"@babel/traverse@^7.21.5", "@babel/traverse@^7.22.5", "@babel/traverse@^7.22.6", "@babel/traverse@^7.22.8":
version "7.22.8"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.22.8.tgz#4d4451d31bc34efeae01eac222b514a77aa4000e"
integrity sha512-y6LPR+wpM2I3qJrsheCTwhIinzkETbplIgPBbwvqPKc+uljeA5gP+3nP8irdYt1mjQaDnlIcG+dw8OjAco4GXw==
Expand All @@ -1003,7 +1003,7 @@
debug "^4.1.0"
globals "^11.1.0"

"@babel/types@^7.0.0", "@babel/types@^7.17.0", "@babel/types@^7.20.7", "@babel/types@^7.22.5", "@babel/types@^7.3.3", "@babel/types@^7.4.4":
"@babel/types@^7.0.0", "@babel/types@^7.20.7", "@babel/types@^7.21.5", "@babel/types@^7.22.5", "@babel/types@^7.3.3", "@babel/types@^7.4.4":
version "7.22.5"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.22.5.tgz#cd93eeaab025880a3a47ec881f4b096a5b786fbe"
integrity sha512-zo3MIHGOkPOfoRXitsgHLjEXmlDaD/5KU1Uzuc9GNiZPhSqVxVRtxuPaSBZDsYZ9qV88AjtMtWW7ww98loJ9KA==
Expand Down Expand Up @@ -1614,18 +1614,16 @@
resolved "https://registry.yarnpkg.com/@humanwhocodes/object-schema/-/object-schema-1.2.1.tgz#b520529ec21d8e5945a1851dfd1c32e94e39ff45"
integrity sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA==

"@ianvs/[email protected]":
version "4.0.0-alpha.3"
resolved "https://registry.yarnpkg.com/@ianvs/prettier-plugin-sort-imports/-/prettier-plugin-sort-imports-4.0.0-alpha.3.tgz#7c07fb2cc5f00fb00f2d3da5370cc2da89fe40a9"
integrity sha512-3zUL/BQoNVoe1OHV7GQ5d4j51/QNvo4tvYNm0j87ZWzsj90jSPHIiI4KtQGM7ZCbmK+TXQBwJWSKVxkHl+iUPg==
dependencies:
"@babel/core" "^7.17.7"
"@babel/generator" "^7.17.7"
"@babel/parser" "^7.17.7"
"@babel/traverse" "^7.17.3"
"@babel/types" "^7.17.0"
lodash.clone "^4.5.0"
lodash.isequal "^4.5.0"
"@ianvs/[email protected]":
version "4.0.2"
resolved "https://registry.yarnpkg.com/@ianvs/prettier-plugin-sort-imports/-/prettier-plugin-sort-imports-4.0.2.tgz#a76f9ed2b5a3494a23564f6f189f3b4e4a472f67"
integrity sha512-VnsTzyb9aSWpc3v6HvZKD6eolZRvofIYjhda+6IbW1GYwr2byWqK0KhLPbYNkit9MAgShad5bhZ1hgBn867A1A==
dependencies:
"@babel/core" "^7.21.8"
"@babel/generator" "^7.21.5"
"@babel/parser" "^7.21.8"
"@babel/traverse" "^7.21.5"
"@babel/types" "^7.21.5"
semver "^7.5.0"

"@istanbuljs/load-nyc-config@^1.0.0":
Expand Down Expand Up @@ -2295,14 +2293,14 @@
dependencies:
tslib "^2.4.0"

"@theguild/prettier-config@1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@theguild/prettier-config/-/prettier-config-1.2.0.tgz#e6f419ddde2d15cf12e0c3bb44d2ed29bcf541e3"
integrity sha512-7zws6rvfO63xPEExM8A8/hFmFnleS/oHEov+iNbWe4QddO5e/zGmm69LuoptsrB9GAPsaxxOAUf8tn23V/1ljA==
"@theguild/prettier-config@2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@theguild/prettier-config/-/prettier-config-2.0.0.tgz#3c8a134e092356e0a4227bdd4bd35b816c491004"
integrity sha512-CWTob4UPfVFZmdxjCkan/Yw2EjH6+bRcUeL0YEAGD63HCk2y4ATxfhPADohM4yfL0xSH2ryU6we4yHs0u6paiQ==
dependencies:
"@ianvs/prettier-plugin-sort-imports" "4.0.0-alpha.3"
prettier-plugin-pkg "^0.17.1"
prettier-plugin-sh "^0.12.8"
"@ianvs/prettier-plugin-sort-imports" "4.0.2"
prettier-plugin-pkg "^0.18.0"
prettier-plugin-sh "^0.13.0"

"@tsconfig/node10@^1.0.7":
version "1.0.9"
Expand Down Expand Up @@ -6052,11 +6050,6 @@ lodash.camelcase@^4.3.0:
resolved "https://registry.yarnpkg.com/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz#b28aa6288a2b9fc651035c7711f65ab6190331a6"
integrity sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==

lodash.clone@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.clone/-/lodash.clone-4.5.0.tgz#195870450f5a13192478df4bc3d23d2dea1907b6"
integrity sha512-GhrVeweiTD6uTmmn5hV/lzgCQhccwReIVRLHp7LT4SopOjqEZ5BbX8b5WWEtAKasjmy8hR7ZPwsYlxRCku5odg==

lodash.debounce@^4.0.8:
version "4.0.8"
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
Expand All @@ -6067,11 +6060,6 @@ lodash.get@^4.4.2:
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
integrity sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ==

lodash.isequal@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
integrity sha512-pDo3lu8Jhfjqls6GkMgpahsF9kCyayhgykjyLMNFTKWrpVdAQtYyB4muAMWozBB4ig/dtWAmsMxLEI8wuz+DYQ==

[email protected]:
version "4.1.2"
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
Expand Down Expand Up @@ -6906,21 +6894,25 @@ prelude-ls@^1.2.1:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==

prettier-plugin-pkg@^0.17.1:
version "0.17.1"
resolved "https://registry.yarnpkg.com/prettier-plugin-pkg/-/prettier-plugin-pkg-0.17.1.tgz#f160c97fa971e8af28e78e34414b16db1360d18d"
integrity sha512-XPRRMQR5oseJXdfK8kQDj2LCV1UjmTuDlPbbJ8C2WLaATNhdvZLhQO0+NtWnRrQTP+erLR5cVxfcwyqF+3R8SA==
prettier-plugin-pkg@^0.18.0:
version "0.18.0"
resolved "https://registry.yarnpkg.com/prettier-plugin-pkg/-/prettier-plugin-pkg-0.18.0.tgz#c0b9158416c50b0c0414faccb650fd82e3abd0fc"
integrity sha512-cme+OUHj25cVj3HwGK6ek/GkCHhlhM1u/IYspOHYsFImaXMJCmjs8xeCcvLreD0HMX5QxObot+3TtQR3Bd2wHw==

prettier-plugin-sh@^0.12.8:
version "0.12.8"
resolved "https://registry.yarnpkg.com/prettier-plugin-sh/-/prettier-plugin-sh-0.12.8.tgz#174106774c06304e082f030ecb05cdcaeabb2585"
integrity sha512-VOq8h2Gn5UzrCIKm4p/nAScXJbN09HdyFDknAcxt6Qu/tv/juu9bahxSrcnM9XWYA+Spz1F1ANJ4LhfwB7+Q1Q==
prettier-plugin-sh@^0.13.0:
version "0.13.1"
resolved "https://registry.yarnpkg.com/prettier-plugin-sh/-/prettier-plugin-sh-0.13.1.tgz#99388c0811994f24334d5233eb5a7cb874dcb840"
integrity sha512-ytMcl1qK4s4BOFGvsc9b0+k9dYECal7U29bL/ke08FEUsF/JLN0j6Peo0wUkFDG4y2UHLMhvpyd6Sd3zDXe/eg==
dependencies:
mvdan-sh "^0.10.1"
sh-syntax "^0.3.6"
synckit "^0.8.1"
sh-syntax "^0.4.1"

[email protected]:
version "3.0.0"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.0.0.tgz#e7b19f691245a21d618c68bc54dc06122f6105ae"
integrity sha512-zBf5eHpwHOGPC47h0zrPyNn+eAEIdEzfywMoYn2XPi0P44Zp0tSq64rq0xAREh4auw2cJZHo9QUob+NqCQky4g==

prettier@2.8.8, prettier@^2.7.1:
prettier@^2.7.1:
version "2.8.8"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.8.tgz#e8c5d7e98a4305ffe3de2e1fc4aca1a71c28b1da"
integrity sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==
Expand Down Expand Up @@ -7535,12 +7527,12 @@ [email protected]:
resolved "https://registry.yarnpkg.com/setprototypeof/-/setprototypeof-1.2.0.tgz#66c9a24a73f9fc28cbe66b09fed3d33dcaf1b424"
integrity sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==

sh-syntax@^0.3.6:
version "0.3.7"
resolved "https://registry.yarnpkg.com/sh-syntax/-/sh-syntax-0.3.7.tgz#8b86862c2de1488dd8fb9a9306e2124501617ae3"
integrity sha512-xIB/uRniZ9urxAuXp1Ouh/BKSI1VK8RSqfwGj7cV57HvGrFo3vHdJfv8Tdp/cVcxJgXQTkmHr5mG5rqJW8r4wQ==
sh-syntax@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/sh-syntax/-/sh-syntax-0.4.1.tgz#21d45a7428d712ecb0c90d517a2b27fd897bc354"
integrity sha512-MW/ZsCYTu11EIYYTSZcfAgMFszAodCmQVB27XssHoIN6L4EG0KSA3h32x8whaSOKuYBX5wz9EybfnPBUFQMCKA==
dependencies:
tslib "^2.4.0"
tslib "^2.6.0"

shebang-command@^1.2.0:
version "1.2.0"
Expand Down Expand Up @@ -7972,7 +7964,7 @@ supports-preserve-symlinks-flag@^1.0.0:
resolved "https://registry.yarnpkg.com/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09"
integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==

synckit@^0.8.1, synckit@^0.8.5:
synckit@^0.8.5:
version "0.8.5"
resolved "https://registry.yarnpkg.com/synckit/-/synckit-0.8.5.tgz#b7f4358f9bb559437f9f167eb6bc46b3c9818fa3"
integrity sha512-L1dapNV6vu2s/4Sputv8xGsCdAVlb5nRDMFU/E27D44l5U6cw1g0dGd45uLc+OXjNMmF4ntiMdCimzcjFKQI8Q==
Expand Down Expand Up @@ -8255,7 +8247,6 @@ typescript@~3.8.3:

uWebSockets.js@uNetworking/uWebSockets.js#v20.27.0:
version "20.27.0"
uid "666954999fb67b303c6bc11b577599d51aa39e7c"
resolved "https://codeload.github.com/uNetworking/uWebSockets.js/tar.gz/666954999fb67b303c6bc11b577599d51aa39e7c"

unbox-primitive@^1.0.2:
Expand Down

0 comments on commit 6082626

Please sign in to comment.