Skip to content

Commit b495130

Browse files
committed
Better handling of response chunks
1 parent ec1b958 commit b495130

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

packages/web/src.ts/geturl.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import https from "https";
55
import { gunzipSync } from "zlib";
66
import { parse } from "url"
77

8-
import { arrayify, concat } from "@ethersproject/bytes";
8+
import { arrayify } from "@ethersproject/bytes";
99
import { shallowCopy } from "@ethersproject/properties";
1010

1111
import type { GetUrlResponse, Options } from "./types";
@@ -32,18 +32,32 @@ function getResponse(request: http.ClientRequest): Promise<GetUrlResponse> {
3232
}, <{ [ name: string ]: string }>{ }),
3333
body: null
3434
};
35+
const chunks: Uint8Array[] = [];
36+
let size = 0;
3537
//resp.setEncoding("utf8");
3638

3739
resp.on("data", (chunk: Uint8Array) => {
38-
if (response.body == null) { response.body = new Uint8Array(0); }
39-
response.body = concat([ response.body, chunk ]);
40+
chunks.push(chunk);
41+
size += chunk.length;
4042
});
4143

4244
resp.on("end", () => {
45+
const body = new Uint8Array(size);
46+
let offset = 0;
47+
48+
for (const chunk of chunks) {
49+
body.set(chunk, offset);
50+
offset += chunk.length;
51+
}
52+
53+
response.body = body;
54+
4355
if (response.headers["content-encoding"] === "gzip") {
4456
//const size = response.body.length;
45-
response.body = arrayify(gunzipSync(response.body));
57+
response.body = arrayify(gunzipSync(body));
4658
//console.log("Delta:", response.body.length - size, Buffer.from(response.body).toString());
59+
} else {
60+
response.body = body;
4761
}
4862
resolve(response);
4963
});

0 commit comments

Comments
 (0)