mushroomdb 0.1.2

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.
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ const { spawnSync } = require("child_process");
5
+ const fs = require("fs");
6
+ const path = require("path");
7
+
8
+ const bin = path.join(__dirname, "..", "vendor", "mushroomdb");
9
+ if (!fs.existsSync(bin)) {
10
+ process.stderr.write(
11
+ "mushroomdb binary is missing; re-run npm install (postinstall fetches the GitHub Release asset)\n",
12
+ );
13
+ process.exit(1);
14
+ }
15
+ const result = spawnSync(bin, process.argv.slice(2), { stdio: "inherit" });
16
+ if (result.error) {
17
+ process.stderr.write(result.error.message + "\n");
18
+ process.exit(1);
19
+ }
20
+ process.exit(result.status === null ? 1 : result.status);
package/install.js ADDED
@@ -0,0 +1,121 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ const crypto = require("crypto");
5
+ const fs = require("fs");
6
+ const http = require("http");
7
+ const https = require("https");
8
+ const os = require("os");
9
+ const path = require("path");
10
+ const { spawnSync } = require("child_process");
11
+
12
+ const pkg = require("./package.json");
13
+ const SUPPORTED = [
14
+ "darwin-arm64 → aarch64-apple-darwin",
15
+ "linux-x64 → x86_64-unknown-linux-gnu",
16
+ "linux-arm64 → aarch64-unknown-linux-gnu",
17
+ ];
18
+
19
+ function rustTarget(platform, arch) {
20
+ if (platform === "darwin" && arch === "arm64") return "aarch64-apple-darwin";
21
+ if (platform === "linux" && arch === "x64") return "x86_64-unknown-linux-gnu";
22
+ if (platform === "linux" && (arch === "arm64" || arch === "aarch64")) {
23
+ return "aarch64-unknown-linux-gnu";
24
+ }
25
+ return null;
26
+ }
27
+
28
+ function fail(msg) {
29
+ process.stderr.write(msg + "\n");
30
+ process.exit(1);
31
+ }
32
+
33
+ function fetchBuffer(url) {
34
+ return new Promise((resolve, reject) => {
35
+ const lib = url.startsWith("https:") ? https : http;
36
+ const req = lib.get(url, (res) => {
37
+ if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
38
+ res.resume();
39
+ fetchBuffer(res.headers.location).then(resolve, reject);
40
+ return;
41
+ }
42
+ if (res.statusCode !== 200) {
43
+ res.resume();
44
+ reject(new Error(`GET ${url} → ${res.statusCode}`));
45
+ return;
46
+ }
47
+ const chunks = [];
48
+ res.on("data", (c) => chunks.push(c));
49
+ res.on("end", () => resolve(Buffer.concat(chunks)));
50
+ res.on("error", reject);
51
+ });
52
+ req.on("error", reject);
53
+ });
54
+ }
55
+
56
+ function checksumOf(buf) {
57
+ return crypto.createHash("sha256").update(buf).digest("hex");
58
+ }
59
+
60
+ function expectedChecksum(sums, filename) {
61
+ const lines = sums.split(/\r?\n/);
62
+ for (const line of lines) {
63
+ const m = line.match(/^([0-9a-fA-F]{64})\s+(\S+)$/);
64
+ if (m && path.basename(m[2]) === filename) return m[1].toLowerCase();
65
+ }
66
+ return null;
67
+ }
68
+
69
+ async function main() {
70
+ const platform = process.env.MUSHROOMDB_FORCE_OS || process.platform;
71
+ const arch = process.env.MUSHROOMDB_FORCE_ARCH || process.arch;
72
+ const target = rustTarget(platform, arch);
73
+ if (!target) {
74
+ fail(
75
+ `unsupported platform: ${platform}-${arch}\nsupported: ${SUPPORTED.join("; ")}`,
76
+ );
77
+ }
78
+
79
+ const version = String(pkg.version).replace(/^v/, "");
80
+ const tag = `v${version}`;
81
+ const asset = `mushroomdb-${tag}-${target}.tar.gz`;
82
+ const repo = "MatthewSherlin/mushroomdb";
83
+ const base =
84
+ process.env.MUSHROOMDB_RELEASE_BASE ||
85
+ `https://github.com/${repo}/releases/download/${tag}`;
86
+ const vendor = path.join(__dirname, "vendor");
87
+ const dest = path.join(vendor, "mushroomdb");
88
+ fs.mkdirSync(vendor, { recursive: true });
89
+
90
+ const tarUrl = `${base.replace(/\/$/, "")}/${asset}`;
91
+ const sumsUrl = `${base.replace(/\/$/, "")}/SHA256SUMS`;
92
+ const tarball = await fetchBuffer(tarUrl);
93
+ const sums = (await fetchBuffer(sumsUrl)).toString("utf8");
94
+ const want = expectedChecksum(sums, asset);
95
+ if (!want) {
96
+ fail(`SHA256SUMS has no entry for ${asset}`);
97
+ }
98
+ const got = checksumOf(tarball);
99
+ if (got !== want) {
100
+ fail(`checksum mismatch for ${asset}: got ${got} want ${want}`);
101
+ }
102
+
103
+ const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "mushroomdb-npm-"));
104
+ const tarPath = path.join(tmp, asset);
105
+ fs.writeFileSync(tarPath, tarball);
106
+ const extracted = spawnSync("tar", ["-xzf", tarPath, "-C", tmp], {
107
+ encoding: "utf8",
108
+ });
109
+ if (extracted.status !== 0) {
110
+ fail(`tar extract failed: ${extracted.stderr || extracted.stdout || extracted.status}`);
111
+ }
112
+ const extractedBin = path.join(tmp, "mushroomdb");
113
+ if (!fs.existsSync(extractedBin)) {
114
+ fail(`tarball ${asset} did not contain ./mushroomdb`);
115
+ }
116
+ fs.copyFileSync(extractedBin, dest);
117
+ fs.chmodSync(dest, 0o755);
118
+ process.stdout.write(`installed ${dest} (${target} ${tag})\n`);
119
+ }
120
+
121
+ main().catch((err) => fail(err.stack || String(err)));
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "mushroomdb",
3
+ "version": "0.1.2",
4
+ "description": "Launcher for the mushroomdb native binary (downloaded from GitHub Releases).",
5
+ "license": "Apache-2.0",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/MatthewSherlin/mushroomdb.git"
9
+ },
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "bin": {
14
+ "mushroomdb": "bin/mushroomdb.js"
15
+ },
16
+ "scripts": {
17
+ "postinstall": "node install.js"
18
+ },
19
+ "files": [
20
+ "install.js",
21
+ "bin/mushroomdb.js"
22
+ ],
23
+ "engines": {
24
+ "node": ">=18"
25
+ },
26
+ "os": [
27
+ "darwin",
28
+ "linux"
29
+ ],
30
+ "cpu": [
31
+ "x64",
32
+ "arm64"
33
+ ]
34
+ }