-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor `modules` library * add back shebang * move dist tag to top of file
- Loading branch information
1 parent
746844c
commit 94801cf
Showing
4 changed files
with
120 additions
and
101 deletions.
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,94 @@ | ||
import * as os from 'node:os' | ||
import assert from 'node:assert' | ||
import { join } from 'node:path' | ||
import { mkdir, chmod } from 'node:fs/promises' | ||
import { fetch } from 'undici' | ||
import { pipeline } from 'node:stream/promises' | ||
import gunzip from 'gunzip-maybe' | ||
import tar from 'tar-fs' | ||
import unzip from 'unzip-stream' | ||
import { once } from 'node:events' | ||
import { createWriteStream } from 'node:fs' | ||
import { paths } from './paths.js' | ||
|
||
const { GITHUB_TOKEN } = process.env | ||
const authorization = GITHUB_TOKEN ? `Bearer ${GITHUB_TOKEN}` : undefined | ||
|
||
export const getBinaryModuleExecutable = ({ | ||
module, | ||
executable | ||
}) => { | ||
return join( | ||
paths.moduleBinaries, | ||
module, | ||
`${executable}${os.platform() === 'win32' ? '.exe' : ''}` | ||
) | ||
} | ||
|
||
export const installBinaryModule = async ({ | ||
module, | ||
repo, | ||
distTag, | ||
executable, | ||
arch = os.arch(), | ||
targets | ||
}) => { | ||
console.log( | ||
`[${module}] GitHub client: ${authorization ? 'authorized' : 'anonymous'}` | ||
) | ||
const target = targets.find(target => | ||
target.platform === os.platform() && target.arch === arch | ||
) | ||
assert(target, `[${module}] Unsupported platform: ${os.platform()} ${arch}`) | ||
|
||
await mkdir(paths.moduleBinaries, { recursive: true }) | ||
const outFile = join(paths.moduleBinaries, module) | ||
|
||
console.log(`[${module}] ⇣ downloading ${os.platform()} ${arch}`) | ||
const res = await fetch( | ||
`https://github.com/${repo}/releases/download/${distTag}/${target.asset}`, | ||
{ | ||
headers: { | ||
...(authorization ? { authorization } : {}) | ||
}, | ||
redirect: 'follow' | ||
} | ||
) | ||
|
||
if (res.status >= 300) { | ||
throw new Error( | ||
`[${module}] Cannot fetch binary ${os.platform()} ${arch}: ${res.status}\n` + | ||
await res.text() | ||
) | ||
} | ||
|
||
if (!res.body) { | ||
throw new Error( | ||
`[${module}] Cannot fetch binary ${os.platform()} ${arch}: no response body` | ||
) | ||
} | ||
|
||
if (target.asset.endsWith('tar.gz')) { | ||
await pipeline(res.body, gunzip(), tar.extract(outFile)) | ||
} else { | ||
await mkdir(join(paths.moduleBinaries, module), { recursive: true }) | ||
const parser = unzip.Parse() | ||
await Promise.all([ | ||
(async () => { | ||
while (true) { | ||
const [entry] = | ||
/** @type {[UnzipStreamEntry]} */ | ||
(await once(parser, 'entry')) | ||
if (entry.path === executable) { | ||
const outPath = join(paths.moduleBinaries, module, executable) | ||
await pipeline(entry, createWriteStream(outPath)) | ||
await chmod(outPath, 0o755) | ||
return | ||
} | ||
} | ||
})(), | ||
pipeline(res.body, parser) | ||
]) | ||
} | ||
console.log(`[${module}] ✓ ${outFile}`) | ||
} |
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 |
---|---|---|
@@ -1,94 +1,5 @@ | ||
#!/usr/bin/env node | ||
|
||
import { fetch } from 'undici' | ||
import { join, dirname } from 'node:path' | ||
import { mkdir, chmod } from 'node:fs/promises' | ||
import { createWriteStream } from 'node:fs' | ||
import { fileURLToPath } from 'node:url' | ||
import { pipeline } from 'node:stream/promises' | ||
import tar from 'tar-fs' | ||
import gunzip from 'gunzip-maybe' | ||
import unzip from 'unzip-stream' | ||
import { once } from 'node:events' | ||
import { platform, arch } from 'node:os' | ||
import assert from 'node:assert' | ||
import { install as installSaturn } from '../lib/saturn-node.js' | ||
|
||
const SATURN_DIST_TAG = 'v0.5.0' | ||
const githubToken = process.env.GITHUB_TOKEN | ||
const authorization = githubToken ? `Bearer ${githubToken}` : undefined | ||
|
||
console.log('GitHub client:', authorization ? 'authorized' : 'anonymous') | ||
|
||
const targets = [ | ||
{ platform: 'darwin', arch: 'x64', url: 'Darwin_x86_64', archive: 'zip' }, | ||
{ platform: 'linux', arch: 'arm64', url: 'Linux_arm64', archive: 'tar.gz' }, | ||
{ platform: 'linux', arch: 'ia32', url: 'Linux_i386', archive: 'tar.gz' }, | ||
{ platform: 'linux', arch: 'x64', url: 'Linux_x86_64', archive: 'tar.gz' }, | ||
{ platform: 'win32', arch: 'x64', url: 'Windows_x86_64', archive: 'tar.gz' } | ||
] | ||
|
||
const archOverwritten = platform() === 'darwin' ? 'x64' : arch() | ||
const target = targets.find(target => | ||
target.platform === platform() && | ||
target.arch === archOverwritten | ||
) | ||
assert(target, `Unsupported platform: ${platform} ${arch}`) | ||
|
||
const outDir = join( | ||
dirname(fileURLToPath(import.meta.url)), | ||
'..', | ||
'modules' | ||
) | ||
await mkdir(outDir, { recursive: true }) | ||
|
||
const outName = `saturn-L2-node-${platform()}-${archOverwritten}` | ||
const outFile = join(outDir, outName) | ||
|
||
console.log(' ⇣ downloading %s', outName) | ||
const res = await fetch( | ||
`https://github.com/filecoin-saturn/L2-node/releases/download/${SATURN_DIST_TAG}/L2-node_${target.url}.${target.archive}`, | ||
{ | ||
headers: { | ||
...(authorization ? { authorization } : {}) | ||
}, | ||
redirect: 'follow' | ||
} | ||
) | ||
|
||
if (res.status >= 300) { | ||
throw new Error( | ||
`Cannot fetch saturn-l2 binary ${platform()} ${archOverwritten}: ${res.status}\n` + | ||
await res.text() | ||
) | ||
} | ||
|
||
if (!res.body) { | ||
throw new Error( | ||
`Cannot fetch saturn-l2 binary ${platform()} ${archOverwritten}: no response body` | ||
) | ||
} | ||
|
||
if (target.archive === 'tar.gz') { | ||
await pipeline(res.body, gunzip(), tar.extract(outFile)) | ||
} else { | ||
await mkdir(join(outDir, outName), { recursive: true }) | ||
const parser = unzip.Parse() | ||
await Promise.all([ | ||
(async () => { | ||
while (true) { | ||
const [entry] = | ||
/** @type {[UnzipStreamEntry]} */ | ||
(await once(parser, 'entry')) | ||
if (entry.path === 'L2-node') { | ||
const outPath = join(outDir, outName, 'saturn-L2-node') | ||
await pipeline(entry, createWriteStream(outPath)) | ||
await chmod(outPath, 0o755) | ||
return | ||
} | ||
} | ||
})(), | ||
pipeline(res.body, parser) | ||
]) | ||
} | ||
|
||
console.log(' ✓ %s', outFile) | ||
await installSaturn() |