@mettlelabs/mettle 0.0.0-beta.7 → 0.0.0-beta.9

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.
package/bin/mettle.cjs ADDED
@@ -0,0 +1,152 @@
1
+ #!/usr/bin/env node
2
+
3
+ const childProcess = require("node:child_process")
4
+ const fs = require("node:fs")
5
+ const path = require("node:path")
6
+ const os = require("node:os")
7
+
8
+ const forwardedSignals =
9
+ process.platform === "win32" ? ["SIGINT", "SIGTERM", "SIGHUP"] : ["SIGINT", "SIGTERM", "SIGHUP", "SIGUSR1"]
10
+
11
+ function run(target) {
12
+ // Tell the CLI which npm package launched it, so the self-updater can detect its install
13
+ // method. The native binary lives in an aliased sub-package without a bin field, so the
14
+ // updater cannot rediscover the wrapper from its own path.
15
+ const env = { ...process.env, METTLE_NPM_ROOT: wrapperDir }
16
+ const child = childProcess.spawn(target, process.argv.slice(2), { stdio: "inherit", env })
17
+ child.on("error", (error) => {
18
+ console.error(error.message)
19
+ process.exit(1)
20
+ })
21
+ const forwarders = {}
22
+ for (const signal of forwardedSignals) {
23
+ forwarders[signal] = () => {
24
+ try {
25
+ child.kill(signal)
26
+ } catch {}
27
+ }
28
+ process.on(signal, forwarders[signal])
29
+ }
30
+ child.on("exit", (code, signal) => {
31
+ for (const forwardedSignal of forwardedSignals) process.removeListener(forwardedSignal, forwarders[forwardedSignal])
32
+ if (signal) return process.kill(process.pid, signal)
33
+ process.exit(typeof code === "number" ? code : 0)
34
+ })
35
+ }
36
+
37
+ const envPath = process.env.METTLE_BIN_PATH
38
+ const scriptDir = path.dirname(fs.realpathSync(__filename))
39
+ const wrapperDir = path.dirname(scriptDir)
40
+ const command = path.basename(__filename).replace(/\.cjs$/, "")
41
+ const platform = { darwin: "darwin", linux: "linux", win32: "windows" }[os.platform()] || os.platform()
42
+ const arch = { x64: "x64", arm64: "arm64", arm: "arm" }[os.arch()] || os.arch()
43
+ const base = `@mettlelabs/mettle-${platform}-${arch}`
44
+ const binary = platform === "windows" ? "mettle.exe" : "mettle"
45
+
46
+ // AVX2 support is a fixed property of the machine, so probe it once and cache the result next
47
+ // to the launcher. The Windows probe spawns PowerShell, which is too slow to run on every
48
+ // invocation. Binary resolution itself stays dynamic so an `npm update` is picked up at once.
49
+ function supportsAvx2() {
50
+ if (arch !== "x64") return false
51
+ const cacheFile = path.join(scriptDir, `.${command}-avx2`)
52
+ try {
53
+ const value = fs.readFileSync(cacheFile, "utf8").trim()
54
+ if (value === "1") return true
55
+ if (value === "0") return false
56
+ } catch {}
57
+ const result = detectAvx2()
58
+ try {
59
+ fs.writeFileSync(cacheFile, result ? "1" : "0")
60
+ } catch {}
61
+ return result
62
+ }
63
+
64
+ function detectAvx2() {
65
+ if (arch !== "x64") return false
66
+ if (platform === "linux") {
67
+ try {
68
+ return /(^|\s)avx2(\s|$)/i.test(fs.readFileSync("/proc/cpuinfo", "utf8"))
69
+ } catch {
70
+ return false
71
+ }
72
+ }
73
+ if (platform === "darwin") {
74
+ try {
75
+ const result = childProcess.spawnSync("sysctl", ["-n", "hw.optional.avx2_0"], { encoding: "utf8", timeout: 1500 })
76
+ return result.status === 0 && (result.stdout || "").trim() === "1"
77
+ } catch {
78
+ return false
79
+ }
80
+ }
81
+ if (platform === "windows") {
82
+ const command =
83
+ '(Add-Type -MemberDefinition "[DllImport(""kernel32.dll"")] public static extern bool IsProcessorFeaturePresent(int ProcessorFeature);" -Name Kernel32 -Namespace Win32 -PassThru)::IsProcessorFeaturePresent(40)'
84
+ for (const executable of ["powershell.exe", "pwsh.exe", "pwsh", "powershell"]) {
85
+ try {
86
+ const result = childProcess.spawnSync(executable, ["-NoProfile", "-NonInteractive", "-Command", command], {
87
+ encoding: "utf8",
88
+ timeout: 3000,
89
+ windowsHide: true,
90
+ })
91
+ if (result.status !== 0) continue
92
+ const output = (result.stdout || "").trim().toLowerCase()
93
+ if (output === "true" || output === "1") return true
94
+ if (output === "false" || output === "0") return false
95
+ } catch {}
96
+ }
97
+ }
98
+ return false
99
+ }
100
+
101
+ const names = (() => {
102
+ const baseline = arch === "x64" && !supportsAvx2()
103
+ if (platform === "linux") {
104
+ const musl = (() => {
105
+ try {
106
+ if (fs.existsSync("/etc/alpine-release")) return true
107
+ const result = childProcess.spawnSync("ldd", ["--version"], { encoding: "utf8" })
108
+ return ((result.stdout || "") + (result.stderr || "")).toLowerCase().includes("musl")
109
+ } catch {
110
+ return false
111
+ }
112
+ })()
113
+ if (musl)
114
+ return arch === "x64"
115
+ ? baseline
116
+ ? [`${base}-baseline-musl`, `${base}-musl`, `${base}-baseline`, base]
117
+ : [`${base}-musl`, `${base}-baseline-musl`, base, `${base}-baseline`]
118
+ : [`${base}-musl`, base]
119
+ return arch === "x64"
120
+ ? baseline
121
+ ? [`${base}-baseline`, base, `${base}-baseline-musl`, `${base}-musl`]
122
+ : [base, `${base}-baseline`, `${base}-musl`, `${base}-baseline-musl`]
123
+ : [base, `${base}-musl`]
124
+ }
125
+ return arch === "x64" ? (baseline ? [`${base}-baseline`, base] : [base, `${base}-baseline`]) : [base]
126
+ })()
127
+
128
+ function findBinary(startDir) {
129
+ let current = startDir
130
+ for (;;) {
131
+ const modules = path.join(current, "node_modules")
132
+ if (fs.existsSync(modules))
133
+ for (const name of names) {
134
+ const candidate = path.join(modules, name, "bin", binary)
135
+ if (fs.existsSync(candidate)) return candidate
136
+ }
137
+ const parent = path.dirname(current)
138
+ if (parent === current) return
139
+ current = parent
140
+ }
141
+ }
142
+
143
+ const resolved = envPath || findBinary(scriptDir)
144
+ if (!resolved) {
145
+ console.error(
146
+ `It seems that your package manager failed to install the right ${command} CLI package. Try manually installing ` +
147
+ names.map((name) => `"${name}"`).join(" or ") +
148
+ " package",
149
+ )
150
+ process.exit(1)
151
+ }
152
+ run(resolved)
package/package.json CHANGED
@@ -1,16 +1,13 @@
1
1
  {
2
2
  "name": "@mettlelabs/mettle",
3
- "version": "0.0.0-beta.7",
3
+ "version": "0.0.0-beta.9",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/prolabsinc/mettle-cli.git"
8
8
  },
9
9
  "bin": {
10
- "mettle": "./bin/mettle.exe"
11
- },
12
- "scripts": {
13
- "postinstall": "node ./postinstall.mjs"
10
+ "mettle": "./bin/mettle.cjs"
14
11
  },
15
12
  "os": [
16
13
  "darwin",
@@ -22,17 +19,17 @@
22
19
  "x64"
23
20
  ],
24
21
  "optionalDependencies": {
25
- "@mettlelabs/mettle-darwin-arm64": "npm:@mettlelabs/mettle@0.0.0-0.beta.7.darwin-arm64",
26
- "@mettlelabs/mettle-darwin-x64": "npm:@mettlelabs/mettle@0.0.0-0.beta.7.darwin-x64",
27
- "@mettlelabs/mettle-darwin-x64-baseline": "npm:@mettlelabs/mettle@0.0.0-0.beta.7.darwin-x64-baseline",
28
- "@mettlelabs/mettle-linux-arm64": "npm:@mettlelabs/mettle@0.0.0-0.beta.7.linux-arm64",
29
- "@mettlelabs/mettle-linux-arm64-musl": "npm:@mettlelabs/mettle@0.0.0-0.beta.7.linux-arm64-musl",
30
- "@mettlelabs/mettle-linux-x64": "npm:@mettlelabs/mettle@0.0.0-0.beta.7.linux-x64",
31
- "@mettlelabs/mettle-linux-x64-baseline": "npm:@mettlelabs/mettle@0.0.0-0.beta.7.linux-x64-baseline",
32
- "@mettlelabs/mettle-linux-x64-baseline-musl": "npm:@mettlelabs/mettle@0.0.0-0.beta.7.linux-x64-baseline-musl",
33
- "@mettlelabs/mettle-linux-x64-musl": "npm:@mettlelabs/mettle@0.0.0-0.beta.7.linux-x64-musl",
34
- "@mettlelabs/mettle-windows-arm64": "npm:@mettlelabs/mettle@0.0.0-0.beta.7.windows-arm64",
35
- "@mettlelabs/mettle-windows-x64": "npm:@mettlelabs/mettle@0.0.0-0.beta.7.windows-x64",
36
- "@mettlelabs/mettle-windows-x64-baseline": "npm:@mettlelabs/mettle@0.0.0-0.beta.7.windows-x64-baseline"
22
+ "@mettlelabs/mettle-darwin-arm64": "npm:@mettlelabs/mettle@0.0.0-0.beta.9.darwin-arm64",
23
+ "@mettlelabs/mettle-darwin-x64": "npm:@mettlelabs/mettle@0.0.0-0.beta.9.darwin-x64",
24
+ "@mettlelabs/mettle-darwin-x64-baseline": "npm:@mettlelabs/mettle@0.0.0-0.beta.9.darwin-x64-baseline",
25
+ "@mettlelabs/mettle-linux-arm64": "npm:@mettlelabs/mettle@0.0.0-0.beta.9.linux-arm64",
26
+ "@mettlelabs/mettle-linux-arm64-musl": "npm:@mettlelabs/mettle@0.0.0-0.beta.9.linux-arm64-musl",
27
+ "@mettlelabs/mettle-linux-x64": "npm:@mettlelabs/mettle@0.0.0-0.beta.9.linux-x64",
28
+ "@mettlelabs/mettle-linux-x64-baseline": "npm:@mettlelabs/mettle@0.0.0-0.beta.9.linux-x64-baseline",
29
+ "@mettlelabs/mettle-linux-x64-baseline-musl": "npm:@mettlelabs/mettle@0.0.0-0.beta.9.linux-x64-baseline-musl",
30
+ "@mettlelabs/mettle-linux-x64-musl": "npm:@mettlelabs/mettle@0.0.0-0.beta.9.linux-x64-musl",
31
+ "@mettlelabs/mettle-windows-arm64": "npm:@mettlelabs/mettle@0.0.0-0.beta.9.windows-arm64",
32
+ "@mettlelabs/mettle-windows-x64": "npm:@mettlelabs/mettle@0.0.0-0.beta.9.windows-x64",
33
+ "@mettlelabs/mettle-windows-x64-baseline": "npm:@mettlelabs/mettle@0.0.0-0.beta.9.windows-x64-baseline"
37
34
  }
38
35
  }
package/bin/mettle.exe DELETED
@@ -1,5 +0,0 @@
1
- echo "Error: @mettlelabs/mettle's postinstall script was not run." >&2
2
- echo "" >&2
3
- echo "This occurs when installation scripts are disabled." >&2
4
- echo "Run the package postinstall script or reinstall with scripts enabled." >&2
5
- exit 1
package/postinstall.mjs DELETED
@@ -1,183 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- import childProcess from "node:child_process"
4
- import fs from "node:fs"
5
- import { createRequire } from "node:module"
6
- import os from "node:os"
7
- import path from "node:path"
8
- import { fileURLToPath } from "node:url"
9
-
10
- const directory = path.dirname(fileURLToPath(import.meta.url))
11
- const require = createRequire(import.meta.url)
12
- const packageJson = JSON.parse(fs.readFileSync(path.join(directory, "package.json"), "utf8"))
13
- const command = Object.keys(packageJson.bin ?? {})[0]
14
- if (!command) throw new Error("mettle package does not declare a binary")
15
- const sourceCommand = packageJson.mettleSourceBinary ?? command
16
-
17
- const platform = { darwin: "darwin", linux: "linux", win32: "windows" }[os.platform()] ?? os.platform()
18
- const arch = { x64: "x64", arm64: "arm64", arm: "arm" }[os.arch()] ?? os.arch()
19
- const sourceBinary = platform === "windows" ? `${sourceCommand}.exe` : sourceCommand
20
- const targetBinary = path.resolve(directory, packageJson.bin[command])
21
- const dependencies = packageJson.optionalDependencies ?? {}
22
- const base = Object.keys(dependencies).find((name) => name.endsWith(`-${platform}-${arch}`))
23
- if (!base) throw new Error(`mettle does not provide a binary for ${platform}-${arch}`)
24
-
25
- function supportsAvx2() {
26
- if (arch !== "x64") return false
27
- if (platform === "linux") {
28
- try {
29
- return /(^|\s)avx2(\s|$)/i.test(fs.readFileSync("/proc/cpuinfo", "utf8"))
30
- } catch {
31
- return false
32
- }
33
- }
34
- if (platform === "darwin") {
35
- try {
36
- const result = childProcess.spawnSync("sysctl", ["-n", "hw.optional.avx2_0"], {
37
- encoding: "utf8",
38
- timeout: 1500,
39
- })
40
- return result.status === 0 && (result.stdout || "").trim() === "1"
41
- } catch {
42
- return false
43
- }
44
- }
45
- if (platform === "windows") {
46
- // PowerShell 7 reads the CPU flag from .NET directly. Windows PowerShell has to compile a
47
- // kernel32 binding with Add-Type first, which can take several seconds on a cold machine;
48
- // timing out would quietly install the slower baseline build.
49
- const intrinsic = "[System.Runtime.Intrinsics.X86.Avx2]::IsSupported"
50
- const kernel32 =
51
- '(Add-Type -MemberDefinition "[DllImport(""kernel32.dll"")] public static extern bool IsProcessorFeaturePresent(int ProcessorFeature);" -Name Kernel32 -Namespace Win32 -PassThru)::IsProcessorFeaturePresent(40)'
52
- for (const [executable, script] of [
53
- ["pwsh.exe", intrinsic],
54
- ["pwsh", intrinsic],
55
- ["powershell.exe", kernel32],
56
- ["powershell", kernel32],
57
- ]) {
58
- try {
59
- const result = childProcess.spawnSync(executable, ["-NoProfile", "-NonInteractive", "-Command", script], {
60
- encoding: "utf8",
61
- timeout: 20_000,
62
- windowsHide: true,
63
- })
64
- if (result.status !== 0) continue
65
- const output = (result.stdout || "").trim().toLowerCase()
66
- if (output === "true" || output === "1") return true
67
- if (output === "false" || output === "0") return false
68
- } catch {}
69
- }
70
- }
71
- return false
72
- }
73
-
74
- function isMusl() {
75
- if (platform !== "linux") return false
76
- try {
77
- if (fs.existsSync("/etc/alpine-release")) return true
78
- const result = childProcess.spawnSync("ldd", ["--version"], { encoding: "utf8" })
79
- return `${result.stdout || ""}${result.stderr || ""}`.toLowerCase().includes("musl")
80
- } catch {
81
- return false
82
- }
83
- }
84
-
85
- function packageNames() {
86
- const baseline = arch === "x64" && !supportsAvx2()
87
- const names =
88
- platform === "linux"
89
- ? isMusl()
90
- ? arch === "x64"
91
- ? baseline
92
- ? [`${base}-baseline-musl`, `${base}-musl`, `${base}-baseline`, base]
93
- : [`${base}-musl`, `${base}-baseline-musl`, base, `${base}-baseline`]
94
- : [`${base}-musl`, base]
95
- : arch === "x64"
96
- ? baseline
97
- ? [`${base}-baseline`, base, `${base}-baseline-musl`, `${base}-musl`]
98
- : [base, `${base}-baseline`, `${base}-musl`, `${base}-baseline-musl`]
99
- : [base, `${base}-musl`]
100
- : arch === "x64"
101
- ? baseline
102
- ? [`${base}-baseline`, base]
103
- : [base, `${base}-baseline`]
104
- : [base]
105
- return names.filter((name) => dependencies[name])
106
- }
107
-
108
- function copyBinary(source) {
109
- if (!fs.existsSync(source)) throw new Error(`Binary not found at ${source}`)
110
- fs.mkdirSync(path.dirname(targetBinary), { recursive: true })
111
- if (fs.existsSync(targetBinary)) {
112
- try {
113
- fs.unlinkSync(targetBinary)
114
- } catch {}
115
- }
116
- try {
117
- fs.linkSync(source, targetBinary)
118
- } catch {
119
- fs.copyFileSync(source, targetBinary)
120
- }
121
- fs.chmodSync(targetBinary, 0o755)
122
- }
123
-
124
- function resolveBinary(name) {
125
- const packagePath = require.resolve(`${name}/package.json`)
126
- return path.join(path.dirname(packagePath), "bin", sourceBinary)
127
- }
128
-
129
- function installPackage(name) {
130
- const temp = fs.mkdtempSync(path.join(os.tmpdir(), "mettle-install-"))
131
- try {
132
- const result = childProcess.spawnSync(
133
- "npm",
134
- [
135
- "install",
136
- "--ignore-scripts",
137
- "--no-save",
138
- "--loglevel=error",
139
- "--prefix",
140
- temp,
141
- `${name}@${dependencies[name]}`,
142
- ],
143
- { stdio: "inherit", windowsHide: true },
144
- )
145
- if (result.status !== 0) return false
146
- copyBinary(path.join(temp, "node_modules", name, "bin", sourceBinary))
147
- return true
148
- } finally {
149
- fs.rmSync(temp, { recursive: true, force: true })
150
- }
151
- }
152
-
153
- function verifyBinary() {
154
- return (
155
- childProcess.spawnSync(targetBinary, ["--version"], {
156
- stdio: "ignore",
157
- windowsHide: true,
158
- }).status === 0
159
- )
160
- }
161
-
162
- function main() {
163
- const names = packageNames()
164
- for (const name of names) {
165
- try {
166
- copyBinary(resolveBinary(name))
167
- if (verifyBinary()) return
168
- } catch {
169
- if (installPackage(name) && verifyBinary()) return
170
- }
171
- }
172
-
173
- throw new Error(
174
- `Failed to install mettle. Try manually installing ${names.map((name) => JSON.stringify(name)).join(" or ")}.`,
175
- )
176
- }
177
-
178
- try {
179
- main()
180
- } catch (error) {
181
- console.error(error instanceof Error ? error.message : String(error))
182
- process.exit(1)
183
- }