Skip to content

Commit 13c75bf

Browse files
committed
Use the native DecompressionStream API to gunzip the .tgz archive
1 parent 1584135 commit 13c75bf

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

lib/deno/mod.ts

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type * as types from "../shared/types"
22
import * as common from "../shared/common"
33
import * as ourselves from "./mod"
4-
import * as denoflate from "https://deno.land/x/[email protected]/mod.ts"
54

65
declare const ESBUILD_VERSION: string
76

@@ -70,7 +69,7 @@ async function installFromNPM(name: string, subpath: string): Promise<string> {
7069
const npmRegistry = Deno.env.get("NPM_CONFIG_REGISTRY") || "https://registry.npmjs.org"
7170
const url = `${npmRegistry}/${name}/-/${name.replace("@esbuild/", "")}-${version}.tgz`
7271
const buffer = await fetch(url).then(r => r.arrayBuffer())
73-
const executable = extractFileFromTarGzip(new Uint8Array(buffer), subpath)
72+
const executable = await extractFileFromTarGzip(new Uint8Array(buffer), subpath)
7473

7574
await Deno.mkdir(finalDir, {
7675
recursive: true,
@@ -117,9 +116,29 @@ function getCachePath(name: string): { finalPath: string, finalDir: string } {
117116
return { finalPath, finalDir }
118117
}
119118

120-
function extractFileFromTarGzip(buffer: Uint8Array, file: string): Uint8Array {
119+
async function gunzip(data: Uint8Array): Promise<Uint8Array> {
120+
const stream = new DecompressionStream('gzip');
121+
const writer = stream.writable.getWriter();
122+
const reader = stream.readable.getReader();
123+
writer.write(data);
124+
writer.close();
125+
const chunks: Uint8Array[] = [];
126+
while (true) {
127+
const { value, done } = await reader.read();
128+
if (done) break;
129+
chunks.push(value);
130+
}
131+
const result = new Uint8Array(chunks.reduce((sum, chunk) => sum + chunk.length, 0));
132+
for (let i = 0, offset = 0; i < chunks.length; i++) {
133+
result.set(chunks[i], offset);
134+
offset += chunks[i].length;
135+
}
136+
return result;
137+
}
138+
139+
async function extractFileFromTarGzip(buffer: Uint8Array, file: string): Promise<Uint8Array> {
121140
try {
122-
buffer = denoflate.gunzip(buffer)
141+
buffer = await gunzip(buffer)
123142
} catch (err: any) {
124143
throw new Error(`Invalid gzip data in archive: ${err && err.message || err}`)
125144
}

0 commit comments

Comments
 (0)