@sachinthapa572/fast 0.1.5-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 (4) hide show
  1. package/cli.js +13 -18
  2. package/download.js +150 -0
  3. package/install.js +6 -96
  4. package/package.json +1 -2
package/cli.js CHANGED
@@ -1,20 +1,15 @@
1
1
  #!/usr/bin/env node
2
2
  const { spawnSync } = require("child_process");
3
- const { existsSync } = require("fs");
4
- const { join } = require("path");
5
-
6
- const binName = process.platform === "win32" ? "fast.exe" : "fast";
7
- const binPath = join(__dirname, "bin", binName);
8
-
9
- if (!existsSync(binPath)) {
10
- console.error(
11
- "fast binary not found. Run `npm rebuild` or reinstall the package."
12
- );
13
- process.exit(1);
14
- }
15
-
16
- const result = spawnSync(binPath, process.argv.slice(2), {
17
- stdio: "inherit",
18
- });
19
-
20
- process.exit(result.status ?? 1);
3
+ const { ensureBinary, BINARY_PATH } = require("./download");
4
+
5
+ ensureBinary()
6
+ .then(() => {
7
+ const result = spawnSync(BINARY_PATH, process.argv.slice(2), {
8
+ stdio: "inherit",
9
+ });
10
+ process.exit(result.status ?? 1);
11
+ })
12
+ .catch((err) => {
13
+ console.error("Failed to install fast binary:", err.message);
14
+ process.exit(1);
15
+ });
package/download.js ADDED
@@ -0,0 +1,150 @@
1
+ const { execSync } = require("child_process");
2
+ const {
3
+ createWriteStream,
4
+ existsSync,
5
+ mkdirSync,
6
+ readdirSync,
7
+ openSync,
8
+ readSync,
9
+ closeSync,
10
+ } = require("fs");
11
+ const { chmod, unlink } = require("fs").promises;
12
+ const { join } = require("path");
13
+ const { platform, arch } = require("os");
14
+ const https = require("https");
15
+ const http = require("http");
16
+
17
+ const pkg = require("./package.json");
18
+ const BIN_DIR = join(__dirname, "bin");
19
+ const BINARY_NAME = process.platform === "win32" ? "fast.exe" : "fast";
20
+ const BINARY_PATH = join(BIN_DIR, BINARY_NAME);
21
+
22
+ const OS_MAP = { win32: "Windows", darwin: "Darwin", linux: "Linux" };
23
+ const ARCH_MAP = { x64: "x86_64", arm64: "aarch64" };
24
+ const EXT_MAP = { win32: ".zip", darwin: ".tar.gz", linux: ".tar.gz" };
25
+
26
+ const GH_OWNER = "sachinthapa572";
27
+ const GH_REPO = "fast-release";
28
+ const DOWNLOAD_BASE =
29
+ process.env.FAST_DOWNLOAD_URL ||
30
+ `https://github.com/${GH_OWNER}/${GH_REPO}/releases/download`;
31
+
32
+ function downloadUrl() {
33
+ const os = OS_MAP[platform()];
34
+ const cpu = ARCH_MAP[arch()];
35
+ const ext = EXT_MAP[platform()];
36
+
37
+ if (!os || !cpu) {
38
+ throw new Error(`Unsupported platform: ${platform()}/${arch()}`);
39
+ }
40
+
41
+ return `${DOWNLOAD_BASE}/v${pkg.version}/fast_${os}_${cpu}${ext}`;
42
+ }
43
+
44
+ function download(url, dest) {
45
+ return new Promise((resolve, reject) => {
46
+ const mod = url.startsWith("https") ? https : http;
47
+ mod
48
+ .get(url, (res) => {
49
+ if (
50
+ res.statusCode >= 300 &&
51
+ res.statusCode < 400 &&
52
+ res.headers.location
53
+ ) {
54
+ download(res.headers.location, dest).then(resolve).catch(reject);
55
+ return;
56
+ }
57
+ if (res.statusCode !== 200) {
58
+ reject(
59
+ new Error(`Download failed: HTTP ${res.statusCode} for ${url}`),
60
+ );
61
+ return;
62
+ }
63
+ const file = createWriteStream(dest);
64
+ res.pipe(file);
65
+ file.on("finish", () => file.close(resolve));
66
+ file.on("error", reject);
67
+ })
68
+ .on("error", reject);
69
+ });
70
+ }
71
+
72
+ function extractArchive(src, dest, binaryName) {
73
+ if (process.platform === "win32") {
74
+ execSync(
75
+ `powershell -NoProfile -Command "Expand-Archive -Path '${src}' -DestinationPath '${dest}' -Force"`,
76
+ { stdio: "ignore" },
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
+ }
90
+ return;
91
+ }
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
+ }
115
+ }
116
+
117
+ async function ensureBinary() {
118
+ if (existsSync(BINARY_PATH)) return;
119
+
120
+ if (!existsSync(BIN_DIR)) mkdirSync(BIN_DIR, { recursive: true });
121
+
122
+ const url = downloadUrl();
123
+ const ext = EXT_MAP[platform()];
124
+ const tmpArchive = join(BIN_DIR, `download${ext}`);
125
+
126
+ console.error(`Downloading fast v${pkg.version}...`);
127
+ await download(url, tmpArchive);
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
+
141
+ console.error("Extracting...");
142
+ extractArchive(tmpArchive, BIN_DIR, BINARY_NAME);
143
+ await unlink(tmpArchive);
144
+
145
+ if (process.platform !== "win32") {
146
+ await chmod(BINARY_PATH, 0o755);
147
+ }
148
+ }
149
+
150
+ module.exports = { ensureBinary, BINARY_PATH };
package/install.js CHANGED
@@ -1,102 +1,12 @@
1
1
  #!/usr/bin/env node
2
- const { execSync } = require("child_process");
3
- const { createWriteStream, existsSync, mkdirSync } = require("fs");
4
- const { chmod, unlink } = require("fs").promises;
5
- const { join } = require("path");
6
- const { platform, arch } = require("os");
7
- const https = require("https");
8
- const http = require("http");
2
+ const { ensureBinary } = require("./download");
9
3
 
10
- const pkg = require("./package.json");
11
- const BIN_DIR = join(__dirname, "bin");
12
- const BINARY_NAME = process.platform === "win32" ? "fast.exe" : "fast";
13
- const BINARY_PATH = join(BIN_DIR, BINARY_NAME);
14
-
15
- const OS_MAP = { win32: "Windows", darwin: "Darwin", linux: "Linux" };
16
- const ARCH_MAP = { x64: "x86_64", arm64: "aarch64" };
17
- const EXT_MAP = { win32: ".zip", darwin: ".tar.gz", linux: ".tar.gz" };
18
-
19
- // Default: download from GitHub Releases. Override via FAST_DOWNLOAD_URL env var.
20
- const GH_OWNER = "sachinthapa572";
21
- const GH_REPO = "fast-release";
22
- const DOWNLOAD_BASE =
23
- process.env.FAST_DOWNLOAD_URL ||
24
- `https://github.com/${GH_OWNER}/${GH_REPO}/releases/download`;
25
-
26
- function downloadUrl() {
27
- const os = OS_MAP[platform()];
28
- const cpu = ARCH_MAP[arch()];
29
- const ext = EXT_MAP[platform()];
30
-
31
- if (!os || !cpu) {
32
- throw new Error(`Unsupported platform: ${platform()}/${arch()}`);
33
- }
34
-
35
- return `${DOWNLOAD_BASE}/v${pkg.version}/fast_${os}_${cpu}${ext}`;
36
- }
37
-
38
- function download(url, dest) {
39
- return new Promise((resolve, reject) => {
40
- const mod = url.startsWith("https") ? https : http;
41
- mod
42
- .get(url, (res) => {
43
- if (
44
- res.statusCode >= 300 &&
45
- res.statusCode < 400 &&
46
- res.headers.location
47
- ) {
48
- download(res.headers.location, dest).then(resolve).catch(reject);
49
- return;
50
- }
51
- if (res.statusCode !== 200) {
52
- reject(
53
- new Error(`Download failed: HTTP ${res.statusCode} for ${url}`),
54
- );
55
- return;
56
- }
57
- const file = createWriteStream(dest);
58
- res.pipe(file);
59
- file.on("finish", () => file.close(resolve));
60
- file.on("error", reject);
61
- })
62
- .on("error", reject);
63
- });
64
- }
65
-
66
- function extractArchive(src, dest) {
67
- if (process.platform === "win32") {
68
- execSync(
69
- `powershell -NoProfile -Command "Expand-Archive -Path '${src}' -DestinationPath '${dest}' -Force"`,
70
- { stdio: "ignore" },
71
- );
72
- return;
73
- }
74
- execSync(`tar xzf "${src}" -C "${dest}" --strip-components 1`, {
75
- stdio: "ignore",
4
+ ensureBinary()
5
+ .then(() => console.log("fast installed successfully."))
6
+ .catch((err) => {
7
+ console.error("Failed to install fast binary:", err.message);
8
+ process.exit(1);
76
9
  });
77
- }
78
-
79
- async function install() {
80
- if (!existsSync(BIN_DIR)) mkdirSync(BIN_DIR, { recursive: true });
81
-
82
- const url = downloadUrl();
83
- const ext = EXT_MAP[platform()];
84
- const tmpArchive = join(BIN_DIR, `download${ext}`);
85
-
86
- console.log(`Downloading fast v${pkg.version}...`);
87
- await download(url, tmpArchive);
88
-
89
- console.log("Extracting...");
90
- extractArchive(tmpArchive, BIN_DIR);
91
-
92
- await unlink(tmpArchive);
93
-
94
- if (process.platform !== "win32") {
95
- await chmod(BINARY_PATH, 0o755);
96
- }
97
-
98
- console.log("fast installed successfully.");
99
- }
100
10
 
101
11
  install().catch((err) => {
102
12
  console.error("Failed to install fast binary:", err.message);
package/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "@sachinthapa572/fast",
3
- "version": "0.1.5-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"
7
7
  },
8
8
  "scripts": {
9
- "postinstall": "node install.js",
10
9
  "preuninstall": "node uninstall.js"
11
10
  },
12
11
  "repository": {