@sachinthapa572/fast 0.1.5-rc → 0.1.6-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 +98 -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,98 @@
1
+ const { execSync } = require("child_process");
2
+ const { createWriteStream, existsSync, mkdirSync } = require("fs");
3
+ const { chmod, unlink } = require("fs").promises;
4
+ const { join } = require("path");
5
+ const { platform, arch } = require("os");
6
+ const https = require("https");
7
+ const http = require("http");
8
+
9
+ const pkg = require("./package.json");
10
+ const BIN_DIR = join(__dirname, "bin");
11
+ const BINARY_NAME = process.platform === "win32" ? "fast.exe" : "fast";
12
+ const BINARY_PATH = join(BIN_DIR, BINARY_NAME);
13
+
14
+ const OS_MAP = { win32: "Windows", darwin: "Darwin", linux: "Linux" };
15
+ const ARCH_MAP = { x64: "x86_64", arm64: "aarch64" };
16
+ const EXT_MAP = { win32: ".zip", darwin: ".tar.gz", linux: ".tar.gz" };
17
+
18
+ const GH_OWNER = "sachinthapa572";
19
+ const GH_REPO = "fast-release";
20
+ const DOWNLOAD_BASE =
21
+ process.env.FAST_DOWNLOAD_URL ||
22
+ `https://github.com/${GH_OWNER}/${GH_REPO}/releases/download`;
23
+
24
+ function downloadUrl() {
25
+ const os = OS_MAP[platform()];
26
+ const cpu = ARCH_MAP[arch()];
27
+ const ext = EXT_MAP[platform()];
28
+
29
+ if (!os || !cpu) {
30
+ throw new Error(`Unsupported platform: ${platform()}/${arch()}`);
31
+ }
32
+
33
+ return `${DOWNLOAD_BASE}/v${pkg.version}/fast_${os}_${cpu}${ext}`;
34
+ }
35
+
36
+ function download(url, dest) {
37
+ return new Promise((resolve, reject) => {
38
+ const mod = url.startsWith("https") ? https : http;
39
+ mod
40
+ .get(url, (res) => {
41
+ if (
42
+ res.statusCode >= 300 &&
43
+ res.statusCode < 400 &&
44
+ res.headers.location
45
+ ) {
46
+ download(res.headers.location, dest).then(resolve).catch(reject);
47
+ return;
48
+ }
49
+ if (res.statusCode !== 200) {
50
+ reject(
51
+ new Error(`Download failed: HTTP ${res.statusCode} for ${url}`),
52
+ );
53
+ return;
54
+ }
55
+ const file = createWriteStream(dest);
56
+ res.pipe(file);
57
+ file.on("finish", () => file.close(resolve));
58
+ file.on("error", reject);
59
+ })
60
+ .on("error", reject);
61
+ });
62
+ }
63
+
64
+ function extractArchive(src, dest) {
65
+ if (process.platform === "win32") {
66
+ execSync(
67
+ `powershell -NoProfile -Command "Expand-Archive -Path '${src}' -DestinationPath '${dest}' -Force"`,
68
+ { stdio: "ignore" },
69
+ );
70
+ return;
71
+ }
72
+ execSync(`tar xzf "${src}" -C "${dest}" --strip-components 1`, {
73
+ stdio: "ignore",
74
+ });
75
+ }
76
+
77
+ async function ensureBinary() {
78
+ if (existsSync(BINARY_PATH)) return;
79
+
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.error(`Downloading fast v${pkg.version}...`);
87
+ await download(url, tmpArchive);
88
+
89
+ console.error("Extracting...");
90
+ extractArchive(tmpArchive, BIN_DIR);
91
+ await unlink(tmpArchive);
92
+
93
+ if (process.platform !== "win32") {
94
+ await chmod(BINARY_PATH, 0o755);
95
+ }
96
+ }
97
+
98
+ 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.6-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": {