Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundle Saturn L2 node #34

Merged
merged 2 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ jobs:

- name: Build
run: npm run build
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Test
run: npm run test
Expand Down Expand Up @@ -95,9 +97,6 @@ jobs:
- name: Install dependencies
run: npm ci --prefer-offline --no-audit --progress=false --cache ${{ github.workspace }}/.cache/npm

- name: Build
run: npm run build

- name: Get tag
id: tag
if: startsWith(github.ref, 'refs/tags/v')
Expand All @@ -123,6 +122,7 @@ jobs:
# Apple notarization
APPLEID: ${{ secrets.apple_id }}
APPLEIDPASS: ${{ secrets.apple_id_pass }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Show dist/
run: du -sh dist/ && ls -l dist/
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ dev-app-update.yml
*.njsproj
*.sln
*.sw?

build/saturn
112 changes: 112 additions & 0 deletions build/download-saturn-l2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/usr/bin/env -S node

'use strict'

const SATURN_DIST_TAG = 'v0.0.2'

const { mkdir } = require('node:fs/promises')
const path = require('node:path')
const tar = require('tar-fs')
const gunzip = require('gunzip-maybe')
const { request } = require('undici')
const { pipeline } = require('node:stream/promises')

const githubToken = process.env.GITHUB_TOKEN
const authorization = githubToken ? `Bearer ${githubToken}` : undefined

main().catch(err => {
console.error('Unhandled error:', err)
process.exit(1)
})

async function main () {
console.log('Fetching release metadata for %s', SATURN_DIST_TAG)
console.log('GitHub client:', authorization ? 'authorized' : 'anonymous')

const { assets } = await fetchReleaseMetadata()

const outDir = path.resolve(__dirname, 'saturn')
await mkdir(outDir, { recursive: true })

await Promise.all(
assets
.map(async ({ name, browser_download_url: url }) => {
const match = name.match(/^saturn-l2_\d+\.\d+\.\d+_([A-Za-z0-9]+)_([A-Za-z0-9_]+)\.tar\.gz$/)
const platform = match && getPlatform(match[1])
if (!match || platform !== process.platform) {
console.log(' ⨯ skipping %s', name)
return
}

const outName = `l2node-${platform}-${getArch(match[2])}`
console.log(' ⇣ downloading %s', outName)
const res = await request(url, {
headers: { authorization },
maxRedirections: 5
})

if (res.statusCode >= 300) {
throw new Error(
`Cannot fetch saturn-l2 binary ${name}: ${res.statusCode}\n${await res.body.text()}`
)
}

const outFile = path.join(outDir, outName)
await pipeline(res.body, gunzip(), tar.extract(outFile))
console.log(' ✓ %s', outFile)
})
)

console.log('✨ DONE ✨')
}

/**
* @returns {Promise<{
* assets: {name:string, browser_download_url: string}[];
* }>}
*/
async function fetchReleaseMetadata () {
const res = await request(
`https://github.com/gitapi/repos/filecoin-project/saturn-l2/releases/tags/${SATURN_DIST_TAG}`,
{
headers: {
accept: 'application/vnd.github.v3+json',
'user-agent': 'undici',
authorization
}
}
)
if (res.statusCode >= 300) {
throw new Error(`Cannot fetch saturn-l2 release ${SATURN_DIST_TAG}: ${res.statusCode}\n${await res.body.text()}`)
}
const data = await res.body.json()
return data
}

/**
* @param {string} golangOs
*/
function getPlatform (golangOs) {
switch (golangOs) {
case 'Windows': return 'win32'
case 'Linux': return 'linux'
case 'Darwin': return 'darwin'
}
throw new Error(`Unkown OS string: ${golangOs}`)
}

/**
* @param {string} golangArch
*/
function getArch (golangArch) {
switch (golangArch) {
case 'x86_64':
return 'x64'
case 'i386':
return 'ia32'
case 'arm64':
return 'arm64'
}

throw new Error(`Unkown ARCH string: ${golangArch}`)
}
8 changes: 6 additions & 2 deletions build/notarize-macos.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const { notarize } = require('electron-notarize')

const isSet = (value) => value && value !== 'false'
const isSet = (/** @type {string | undefined} */ value) => value && value !== 'false'

// electron-build hook responsible for Apple Notarization of signed DMG
/**
* electron-build hook responsible for Apple Notarization of signed DMG
*
* @param {import('electron-builder').AfterPackContext} context
*/
exports.default = async function notarizing (context) {
const { electronPlatformName, appOutDir } = context
if (electronPlatformName !== 'darwin') return
Expand Down
5 changes: 5 additions & 0 deletions electron-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ files:
- node_modules/**/*
- package.json

extraResources:
- from: 'build/saturn/l2node-${platform}-${arch}'
to: 'saturn-l2-node'
filter: ['*']

asarUnpack: 'main/**/scripts/**/*'

afterSign: 'build/notarize-macos.js'
Expand Down
3 changes: 3 additions & 0 deletions main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const path = require('node:path')
const setupUI = require('./ui')
const setupTray = require('./tray')
const setupUpdater = require('./updater')
const setupSaturnNode = require('./saturn-node')

const inTest = (process.env.NODE_ENV === 'test')

Expand Down Expand Up @@ -49,6 +50,8 @@ async function run () {
await setupTray(ctx)
await setupUI(ctx)
await setupUpdater(ctx)

await setupSaturnNode(ctx)
} catch (e) {
handleError(e)
}
Expand Down
24 changes: 24 additions & 0 deletions main/saturn-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

const { app } = require('electron')
const path = require('path')
const fs = require('node:fs/promises')

module.exports = async function setupSaturnNode (/** @type {import('./typings').Context} */ ctx) {
const saturnBinaryPath = getSaturnBinaryPath()
console.log('Using Saturn L2 Node binary: %s', saturnBinaryPath)

const stat = await fs.stat(saturnBinaryPath)
if (!stat) {
throw new Error(`Invalid configuration or deployment. Saturn L2 Node was not found: ${saturnBinaryPath}`)
}

console.log('todo: start saturn node')
}

function getSaturnBinaryPath () {
const name = 'saturn-l2' + (process.platform === 'win32' ? '.exe' : '')
return app.isPackaged
? path.resolve(process.resourcesPath, 'saturn-l2-node', name)
: path.resolve(__dirname, '..', 'build', 'saturn', `l2node-${process.platform}-${process.arch}`, name)
}
Loading