Skip to content

Commit

Permalink
Sidecar subsystem refactor (#155)
Browse files Browse the repository at this point in the history
* sidecar subsystem refactor

* Context -> State

* state store bug fix

* sidecar working

* rel dep path fixes, run fixes, http fixes

* decal rel dep path fix

* rm stray cmt

* make sure dev is false not falsy

* prod sidecar fix

* addons fix

* deadcode removal

* desktopSources fix
  • Loading branch information
davidmarkclements authored May 20, 2024
1 parent e6e6cd9 commit f9cd043
Show file tree
Hide file tree
Showing 49 changed files with 1,635 additions and 1,649 deletions.
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const IPC = require('pear-ipc')
const cmd = require('./cmd')
const crasher = require('./lib/crasher')
const tryboot = require('./lib/tryboot')
const { PLATFORM_LOCK, SWAP, SOCKET_PATH, CONNECT_TIMEOUT } = require('./lib/constants.js')
const { PLATFORM_LOCK, SWAP, SOCKET_PATH, CONNECT_TIMEOUT } = require('./constants.js')
crasher('cli', SWAP)

cli()
Expand Down
2 changes: 1 addition & 1 deletion cmd/dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const os = require('bare-os')
const { isAbsolute, resolve } = require('bare-path')
const { outputter } = require('./iface')
const { ERR_INVALID_INPUT } = require('../lib/errors')
const { ERR_INVALID_INPUT } = require('../errors')
const output = outputter('stage', {
dumping: ({ key, dir }) => `\n🍐 Dumping ${key} into ${dir}`,
complete: '\nDumping complete!\n',
Expand Down
6 changes: 3 additions & 3 deletions cmd/iface.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { once } = require('bare-events')
const byteSize = require('tiny-byte-size')
const { isWindows } = require('which-runtime')
const stdio = require('../lib/stdio')
const { CHECKOUT } = require('../lib/constants')
const { CHECKOUT } = require('../constants')
const ADD = 1
const REMOVE = -1
const CHANGE = 0
Expand Down Expand Up @@ -60,7 +60,7 @@ function indicator (value, type = 'success') {
return value < 0 ? ansi.cross + ' ' : (value > 0 ? ansi.tick + ' ' : ansi.gray('- '))
}

const outputter = (cmd, taggers = {}) => async (json, stream, state = {}) => {
const outputter = (cmd, taggers = {}) => async (json, stream, info = {}) => {
let error = null
try {
for await (const { tag, data = {} } of stream) {
Expand All @@ -70,7 +70,7 @@ const outputter = (cmd, taggers = {}) => async (json, stream, state = {}) => {
}
let result = null
try {
result = typeof taggers[tag] === 'function' ? taggers[tag](data, state) : (taggers[tag] || false)
result = typeof taggers[tag] === 'function' ? taggers[tag](data, info) : (taggers[tag] || false)
} catch (err) {
error = err
break
Expand Down
4 changes: 2 additions & 2 deletions cmd/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'
const { header, footer, command, flag, hiddenFlag, arg, summary, description, rest, bail } = require('paparam')
const { usage, print } = require('./iface')
const { CHECKOUT } = require('../lib/constants')
const errors = require('../lib/errors')
const { CHECKOUT } = require('../constants')
const errors = require('../errors')
const runners = {
init: require('./init'),
stage: require('./stage'),
Expand Down
2 changes: 1 addition & 1 deletion cmd/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const parseLink = require('../run/parse-link')
const { outputter } = require('./iface')
const os = require('bare-os')
const { isAbsolute, resolve } = require('bare-path')
const { ERR_INVALID_INPUT } = require('../lib/errors')
const { ERR_INVALID_INPUT } = require('../errors')

const keys = ({ content, discovery, project }) => `
keys hex
Expand Down
2 changes: 1 addition & 1 deletion cmd/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const os = require('bare-os')
const { isAbsolute, resolve } = require('bare-path')
const { outputter, ansi } = require('./iface')
const { ERR_INVALID_INPUT } = require('../lib/errors')
const { ERR_INVALID_INPUT } = require('../errors')
const parseLink = require('../run/parse-link')

const output = outputter('release', {
Expand Down
2 changes: 1 addition & 1 deletion cmd/shift.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'
const { outputter } = require('./iface')
const parseLink = require('../run/parse-link')
const { ERR_INVALID_INPUT } = require('../lib/errors')
const { ERR_INVALID_INPUT } = require('../errors')

const output = outputter('shift', {
moving: ({ src, dst, force }) => `Moving user application storage\n\nFrom: ${src}\nTo: ${dst}\n${force ? '\nForce flag used, overwriting existing application storage.' : ''}`,
Expand Down
2 changes: 1 addition & 1 deletion cmd/sidecar.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = (ipc) => async function sidecar (cmd) {

print('Sidecar has shutdown', true)

const { CHECKOUT, RUNTIME } = require('../lib/constants')
const { CHECKOUT, RUNTIME } = require('../constants')
const KEY = CHECKOUT.key

print('Rebooting current process as Sidecar\n - [ ' + KEY + ' ]', 0)
Expand Down
2 changes: 1 addition & 1 deletion cmd/stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const os = require('bare-os')
const { isAbsolute, resolve } = require('bare-path')
const { outputter, ansi } = require('./iface')
const parseLink = require('../run/parse-link')
const { ERR_INVALID_INPUT } = require('../lib/errors')
const { ERR_INVALID_INPUT } = require('../errors')

let blocks = 0
let total = 0
Expand Down
7 changes: 3 additions & 4 deletions lib/constants.js → constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ const { platform, arch, isWindows, isLinux } = require('which-runtime')
const { pathToFileURL, fileURLToPath } = require('url-file-url')
const sodium = require('sodium-native')
const b4a = require('b4a')
const CHECKOUT = require('../checkout')
const CHECKOUT = require('./checkout')
const { ERR_COULD_NOT_INFER_MODULE_PATH } = require('./errors')

const BIN = 'by-arch/' + platform + '-' + arch + '/bin/'

const url = module.url || electronModuleURL()
const mount = new URL('..', url)
const mount = new URL('.', url)

const LOCALDEV = CHECKOUT.length === null

const swapURL = mount.pathname.endsWith('.bundle/') ? new URL('..', mount) : mount
const swapPath = toPath(swapURL)

Expand Down Expand Up @@ -75,7 +74,7 @@ function electronModuleURL () {
const u = pathToFileURL(process.execPath)
const i = u.href.lastIndexOf(BIN)
if (i === -1) throw ERR_COULD_NOT_INFER_MODULE_PATH('Could not infer the actual module path')
return new URL(u.href.slice(0, i) + 'lib/constants.js')
return new URL(u.href.slice(0, i) + 'constants.js')
}

function toPath (u) {
Expand Down
3 changes: 1 addition & 2 deletions decal.html
Original file line number Diff line number Diff line change
Expand Up @@ -442,13 +442,12 @@ <h1 id="header">Unknown app</h1>
</script>
<div id="bar">
<script type="module">
import constants from './lib/constants.js'
import constants from './constants.js'
if (Pear.config.key?.z32 !== constants.ALIASES.keet.z32) {
if (Pear.config.options.platform?.__legacyTitlebar) {
document.getElementById('bar').style.display = 'block'
const winctrl = document.getElementById('window-controls')
const clone = winctrl.cloneNode()
// winctrl.remove()
global.winctrl = clone
}
}
Expand Down
16 changes: 8 additions & 8 deletions electron-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
const electron = require('electron')
const { isWindows, isMac, isLinux } = require('which-runtime')
const { command } = require('paparam')
const Context = require('./ctx/shared')
const State = require('./state')
const GUI = require('./gui')
const crasher = require('./lib/crasher')
const tryboot = require('./lib/tryboot')
const { SWAP, SOCKET_PATH, CONNECT_TIMEOUT } = require('./lib/constants')
const { SWAP, SOCKET_PATH, CONNECT_TIMEOUT } = require('./constants')
const runDefinition = require('./run/definition')

const argv = (process.argv.length > 1 && process.argv[1][0] === '-') ? process.argv.slice(1) : process.argv.slice(2)
Expand All @@ -20,14 +20,14 @@ const run = command('run', ...runDefinition, electronMain)
run.parse(argv)

async function electronMain (cmd) {
const ctx = new Context({
const state = new State({
link: cmd.args.link,
flags: cmd.flags
})
Context.storage(ctx)
State.storage(state)

if (ctx.error) {
console.error(ctx.error)
if (state.error) {
console.error(state.error)
electron.app.quit(1)
return
}
Expand All @@ -36,13 +36,13 @@ async function electronMain (cmd) {
socketPath: SOCKET_PATH,
connectTimeout: CONNECT_TIMEOUT,
tryboot,
ctx
state
})

await gui.ready()

// note: would be unhandled rejection on failure, but should never fail:
if (await gui.ipc.wakeup(ctx.link, ctx.storage, ctx.dir && ctx.link?.startsWith('pear://dev'))) {
if (await gui.ipc.wakeup(state.link, state.storage, state.dir && state.link?.startsWith('pear://dev'))) {
electron.app.quit(0)
return
}
Expand Down
File renamed without changes.
Loading

0 comments on commit f9cd043

Please sign in to comment.