diff --git a/index.mobile.js b/index.mobile.js new file mode 100644 index 0000000..34e29c2 --- /dev/null +++ b/index.mobile.js @@ -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}) +} diff --git a/package.json b/package.json index 3a8b79e..d1a4cdc 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,8 @@ "teardown.js", "terminal.js", "transform.js", - "tryboot.js" + "tryboot.js", + "index.mobile.js" ], "keywords": [ "pear", @@ -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", @@ -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",