-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//@ts-check | ||
|
||
import fs from "node:fs/promises" | ||
import path from "node:path"; | ||
|
||
// If this program is running on node-altjtalk-binding repo, | ||
// do not download prebuilt binary | ||
const isRepo = await fs.access("build.rs").then(() => true, () => false); | ||
if (isRepo) process.exit(0); | ||
|
||
async function isMusl() { | ||
// For Node 10 | ||
if (!process.report || typeof process.report.getReport !== 'function') { | ||
try { | ||
const lddPath = require('child_process').execSync('which ldd').toString().trim() | ||
return (await fs.readFile(lddPath, 'utf8')).includes('musl') | ||
} catch (e) { | ||
return true | ||
} | ||
} else { | ||
const report = process.report.getReport(); | ||
const { glibcVersionRuntime } = (typeof report === "object" ? report : JSON.parse(report)).header | ||
return !glibcVersionRuntime | ||
} | ||
} | ||
|
||
const baseUrl = "https://github.com/femshima/node-altjtalk-binding/releases/download"; | ||
|
||
const file = new URL(import.meta.url).pathname; | ||
const dir = path.dirname(file); | ||
|
||
const packageJSON = JSON.parse( | ||
await fs.readFile(path.join(dir, "package.json"), { encoding: "utf-8" }) | ||
); | ||
const packageName = packageJSON.name ?? "node-altjtalk-binding"; | ||
const version = `v${packageJSON.version}` ?? "latest"; | ||
|
||
const { platform, arch } = process; | ||
|
||
let suffix = ""; | ||
switch (platform) { | ||
case 'win32': | ||
suffix = "-msvc"; | ||
break; | ||
case 'linux': | ||
suffix = (await isMusl()) ? "-musl" : "-gnu"; | ||
break; | ||
} | ||
|
||
const fileName = `${packageName}.${platform}-${arch}${suffix}.node` | ||
|
||
const url = `${baseUrl}/${version}/${fileName}` | ||
|
||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error(`Fetch failed.\nRequest to ${url} returned status code ${response.status}.`); | ||
} | ||
|
||
const buf = await response.arrayBuffer(); | ||
fs.writeFile(fileName, Buffer.from(buf)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters