Skip to content
Draft
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
146 changes: 146 additions & 0 deletions index.mobile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
const SystemLog = require('bare-system-logger')
const Console = require('bare-console')
global.console = new Console(new SystemLog())
const Module = require('bare-module')
const { startsWithWindowsDriveLetter } = require('bare-module-resolve')
const path = require('bare-path')
const fs = require('bare-fs')
const crypto = require('bare-crypto')
const { fileURLToPath, pathToFileURL } = require('bare-url')
const os = require('bare-os')
const goodbye = require('graceful-goodbye')

let bundle = Bare.argv.pop()
const filename = Bare.argv.pop()
const assets = null // TODO: support assets

class API {
constructor (){
this.isMobile = true
this.argv = Bare.argv
this.pid = Bare.pid
this.exitCode = Bare.exitCode
this.app = {} // need to adjust

this.app.startId = crypto.randomBytes(16).toString('hex') // ID for the thread
this.app.id = null // `${client.id}@${startId}`

}

checkpoint (state) {
global.Pear.app.checkpoint = state
// return ref.track(this.#ipc.checkpoint(state)) // TODO: find equivalent
}

versions () {
return { runtimes: { bare: Bare.versions.bare }, engines: {}}
}

exit () {
return os.kill(Bare.pid, 'SIGTERM')
}

teardown (callback, position){
return goodbye(callback, position)
}
}

// eg:
// id,
// startId,
// key,
// links,
// alias,
// env,
// gui,
// assets,
// options,
// checkpoint,
// checkout,
// flags,
// dev,
// stage,
// storage,
// name,
// main,
// args,
// channel,
// release,
// applink,
// query,
// fragment,
// link,
// linkData,
// entrypoint,
// route,
// routes,
// dir,
// dht,
// prerunning,
// version

global.Pear = new API()
load()

async function load() {
if (assets !== null) {
let url

if (startsWithWindowsDriveLetter(assets)) {
url = null
} else {
url = URL.parse(assets)
}

if (url === null) url = pathToFileURL(assets)

assets = fileURLToPath(url)
}

let url

if (startsWithWindowsDriveLetter(filename)) {
url = null
} else {
url = URL.parse(filename)
}
console.log(url)
if (url === null) url = pathToFileURL(filename)

if (bundle === null) bundle = Module.protocol.read(url)
else bundle = Buffer.from(bundle)

if (assets !== null && path.extname(url.href) === '.bundle') {
const bundle = Bundle.from(bundle)

if (bundle.id !== null && bundle.assets.length > 0) {
const id = crypto.createHash('blake2b256').update(bundle.id).digest('hex')

const root = path.join(assets, id)

const tmp = fs.existsSync(root) ? null : path.join(assets, 'tmp')

if (tmp !== null) {
fs.rmSync(tmp, { recursive: true, force: true })
fs.mkdirSync(tmp, { recursive: true })
}

bundle = await unpack(bundle, { files: false, assets: true }, (key) => {
if (tmp !== null) {
const target = path.join(tmp, key)

fs.mkdirSync(path.dirname(target), { recursive: true })
fs.writeFileSync(target, bundle.read(key))
}

return pathToFileURL(path.join(root, key)).href
})

if (tmp !== null) fs.renameSync(tmp, root)
}
}

const cache = Object.create(null) // use clean cache to avoid id collisions

Module.load(url, bundle, {cache})
}
21 changes: 18 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"teardown.js",
"terminal.js",
"transform.js",
"tryboot.js"
"tryboot.js",
"index.mobile.js"
],
"keywords": [
"pear",
Expand Down Expand Up @@ -57,7 +58,20 @@
"bare": "bare-module"
}
},
"exports": {
".": {
"pear-mobile": "./index.mobile.js",
"default": "./index.js"
},
"./wrapper": "./index.mobile.js"
},
"dependencies": {
"bare-console": "^6.1.0",
"bare-crypto": "^1.13.0",
"bare-module-resolve": "^1.12.1",
"bare-system-logger": "^1.0.2",
"bare-url": "^2.3.2",
"graceful-goodbye": "^1.3.3",
"pear-constants": "^1.0.0",
"pear-errors": "^1.0.0",
"pear-gracedown": "^1.0.0",
Expand All @@ -68,10 +82,11 @@
"pear-ref": "^1.0.2",
"pear-run": "^1.0.8",
"pear-updates": "^1.0.1",
"pear-wakeups": "^1.0.0"
"pear-wakeups": "^1.0.0",
"which-runtime": "^1.3.2"
},
"devDependencies": {
"bare-fs": "^4.3.3",
"bare-fs": "^4.5.3",
"bare-module": "^5.0.3",
"bare-os": "^3.6.2",
"bare-path": "^3.0.0",
Expand Down
Loading