@sachinthapa572/fast 0.1.6-rc → 0.1.7-rc

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/download.js +58 -6
  2. package/package.json +1 -1
package/download.js CHANGED
@@ -1,5 +1,13 @@
1
1
  const { execSync } = require("child_process");
2
- const { createWriteStream, existsSync, mkdirSync } = require("fs");
2
+ const {
3
+ createWriteStream,
4
+ existsSync,
5
+ mkdirSync,
6
+ readdirSync,
7
+ openSync,
8
+ readSync,
9
+ closeSync,
10
+ } = require("fs");
3
11
  const { chmod, unlink } = require("fs").promises;
4
12
  const { join } = require("path");
5
13
  const { platform, arch } = require("os");
@@ -61,17 +69,49 @@ function download(url, dest) {
61
69
  });
62
70
  }
63
71
 
64
- function extractArchive(src, dest) {
72
+ function extractArchive(src, dest, binaryName) {
65
73
  if (process.platform === "win32") {
66
74
  execSync(
67
75
  `powershell -NoProfile -Command "Expand-Archive -Path '${src}' -DestinationPath '${dest}' -Force"`,
68
76
  { stdio: "ignore" },
69
77
  );
78
+ // PowerShell puts files in a subfolder named after the archive
79
+ const subdirs = readdirSync(dest).filter((f) => f !== binaryName);
80
+ for (const dir of subdirs) {
81
+ const subpath = join(dest, dir);
82
+ if (existsSync(subpath) && existsSync(join(subpath, binaryName))) {
83
+ execSync(
84
+ `move "${join(subpath, binaryName)}" "${join(dest, binaryName)}"`,
85
+ { stdio: "ignore" },
86
+ );
87
+ execSync(`rmdir /s /q "${subpath}"`, { stdio: "ignore" });
88
+ }
89
+ }
70
90
  return;
71
91
  }
72
- execSync(`tar xzf "${src}" -C "${dest}" --strip-components 1`, {
73
- stdio: "ignore",
74
- });
92
+
93
+ // Extract to a temp dir so we can find the binary regardless of wrapping
94
+ const tmpDir = join(dest, `._extract_${Date.now()}`);
95
+ mkdirSync(tmpDir, { recursive: true });
96
+
97
+ try {
98
+ execSync(`tar xzf "${src}" -C "${tmpDir}"`, { stdio: "ignore" });
99
+
100
+ // Find the binary anywhere in the extracted tree
101
+ const result = execSync(`find "${tmpDir}" -type f -name "${binaryName}"`, {
102
+ encoding: "utf8",
103
+ timeout: 5000,
104
+ }).trim();
105
+
106
+ if (!result) {
107
+ throw new Error(`Binary "${binaryName}" not found in archive`);
108
+ }
109
+
110
+ const found = result.split("\n")[0];
111
+ execSync(`mv "${found}" "${join(dest, binaryName)}"`, { stdio: "ignore" });
112
+ } finally {
113
+ execSync(`rm -rf "${tmpDir}"`, { stdio: "ignore" });
114
+ }
75
115
  }
76
116
 
77
117
  async function ensureBinary() {
@@ -86,8 +126,20 @@ async function ensureBinary() {
86
126
  console.error(`Downloading fast v${pkg.version}...`);
87
127
  await download(url, tmpArchive);
88
128
 
129
+ // Quick sanity check: make sure it's actually a gzip, not an HTML error page
130
+ const header = Buffer.alloc(2);
131
+ const fd = openSync(tmpArchive, "r");
132
+ readSync(fd, header, 0, 2, 0);
133
+ closeSync(fd);
134
+ if (header[0] !== 0x1f || header[1] !== 0x8b) {
135
+ await unlink(tmpArchive);
136
+ throw new Error(
137
+ `Downloaded file is not a valid gzip archive (got 0x${header.toString("hex")}). Release v${pkg.version} may not exist yet.`,
138
+ );
139
+ }
140
+
89
141
  console.error("Extracting...");
90
- extractArchive(tmpArchive, BIN_DIR);
142
+ extractArchive(tmpArchive, BIN_DIR, BINARY_NAME);
91
143
  await unlink(tmpArchive);
92
144
 
93
145
  if (process.platform !== "win32") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sachinthapa572/fast",
3
- "version": "0.1.6-rc",
3
+ "version": "0.1.7-rc",
4
4
  "description": "Test your internet speed from the command-line",
5
5
  "bin": {
6
6
  "fast": "cli.js"