Skip to content

Commit eee831a

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

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

packages/web/src.ts/geturl.ts

Lines changed: 16 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,30 @@ 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+
4353
if (response.headers["content-encoding"] === "gzip") {
4454
//const size = response.body.length;
45-
response.body = arrayify(gunzipSync(response.body));
55+
response.body = arrayify(gunzipSync(body));
4656
//console.log("Delta:", response.body.length - size, Buffer.from(response.body).toString());
57+
} else {
58+
response.body = body;
4759
}
4860
resolve(response);
4961
});

0 commit comments

Comments
 (0)