pear-mobile 0.0.1-rc → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -15,18 +15,25 @@ Requires `react-native-bare-kit` to be listed in project dependencies.
15
15
  ## Usage
16
16
 
17
17
  ```js
18
+ /* React Native */
18
19
  import PearRuntime from 'pear-mobile'
19
20
  import bundle from './worker.bundle.js'
20
21
  import { version, upgrade } from './package.json'
21
22
 
22
- const runtime = new PearRuntime({ version, upgrade })
23
- runtime.on('updated', () => {
24
- runtime.applyUpdate()
25
- })
23
+ const runtime = new PearRuntime()
24
+ const IPC = runtime.run('/worker.bundle', bundle, [runtime.dir])
25
+
26
+ /* Bare Worklet */
27
+ const PearRuntime = require('pear-runtime')
28
+ const { version, upgrade } = require('./package.json')
26
29
 
27
- const appStorage = runtime.storage
30
+ const dir = Bare.argv[0]
28
31
 
29
- const IPC = runtime.run('/worker.bundle', bundle, [appStorage])
32
+ const runtime = new PearRuntime({ version, upgrade, dir })
33
+ runtime.on('updated', () => {
34
+ runtime.applyUpdate()
35
+ conosle.log('restart for update')
36
+ })
30
37
  ```
31
38
 
32
39
  ## API
@@ -51,11 +58,12 @@ Unsubscribe: remove `callback` for `event`, or remove all listeners for `event`
51
58
 
52
59
  Subscribe to an event once; listener is removed after the first emit. Returns `runtime`.
53
60
 
54
- #### `IPC = runtime.run(filename, bundle, argv)`
61
+ #### `const IPC <stream.Duplex> = runtime.run(filename, bundle, argv)`
55
62
 
56
63
  Start a bare worker (worklet). Returns an IPC duplex stream. `filename` is a virtual path, `bundle` is the worklet bundle, `argv` is an array of string arguments.
57
64
 
58
65
  `Bare.argv` in worker to access `argv`.
66
+ `Bare.IPC` in worker to access stream.
59
67
 
60
68
  #### `runtime.ready()`
61
69
 
package/index.js CHANGED
@@ -1,109 +1,16 @@
1
- const { Worklet } = require('react-native-bare-kit') // NEED TO INSTALL IN ROOT PRJECT (version match)
2
- const bundle = require('./lib/pear.bundle.js')
3
- const RPC = require('bare-rpc')
4
- const RNFS = require('react-native-fs')
5
- const AsyncStorage = require('@react-native-async-storage/async-storage') // NEED TO INSTALL IN ROOT PRJECT (version match)
6
- const { DevSettings } = require('react-native') // NEED TO INSTALL IN ROOT PRJECT (version match)
7
- const b4a = require('b4a')
8
-
9
- module.exports = class PearRuntime {
10
- constructor(config = {}) {
11
- if (!config.upgrade) throw new Error('upgrade link required')
12
- this.config = {
13
- dir: `${RNFS.DocumentDirectoryPath}/pear-runtime`,
14
- isDev: __DEV__,
15
- ...config
1
+ const PearRuntimeUpdater = require('pear-runtime-updater')
2
+ const path = require('bare-path')
3
+ const fs = require('bare-fs')
4
+
5
+ module.exports = class PearRuntime extends PearRuntimeUpdater {
6
+ constructor(opts = {}) {
7
+ const appPath = opts.app || path.join(opts.dir, 'pear-runtime/upgrade')
8
+ if (!fs.existsSync(appPath)) fs.mkdirSync(appPath, { recursive: true, force: true })
9
+ if (fs.existsSync(path.join(appPath, 'package.json'))) {
10
+ const manifest = fs.readFileSync(path.join(appPath, 'package.json'))
11
+ opts.version = JSON.parse(manifest).version
16
12
  }
17
-
18
- this._listeners = Object.create(null)
19
- this._calledReady = null
20
-
21
- this.IPCPromise = this.ready().catch(noop)
22
- }
23
-
24
- ready() {
25
- if (this._calledReady) return this._calledReady
26
- this._calledReady = this._open()
27
- return this._calledReady
28
- }
29
-
30
- async _open() {
31
- const runtimeDir = `${RNFS.DocumentDirectoryPath}/pear-runtime`
32
- const storageDir = `${runtimeDir}/storage`
33
-
34
- await RNFS.mkdir(runtimeDir)
35
- await RNFS.mkdir(storageDir)
36
-
37
- const argv = [JSON.stringify(this.config)]
38
- const worklet = new Worklet()
39
- worklet.start('/pear.bundle', bundle, argv)
40
-
41
- return new RPC(worklet.IPC, async (req) => {
42
- if (req.command === 0) {
43
- const version = b4a.toString(req.data)
44
- console.log('received version form pearend:', version)
45
- this.emit('updated')
46
- }
47
- if (req.command === 1){
48
- const updateData = b4a.toString(req.data)
49
- console.log('[Update Diff]', updateData)
50
- }
51
- if (req.command === 2){
52
- const logString = b4a.toString(req.data)
53
- console.log('[pear-runtime]:', logString)
54
- }
55
- })
56
- }
57
-
58
- async applyUpdate(version) {
59
- await AsyncStorage.multiSet([
60
- ['updatePending', 'true'],
61
- ['updateConfirmed', 'false'],
62
- ['runtimeVersion', String(version ?? this.version)]
63
- ])
64
-
65
- DevSettings.reload()
66
- }
67
-
68
- async close() {}
69
-
70
- on(event, callback) {
71
- if (!this._listeners[event]) this._listeners[event] = []
72
- this._listeners[event].push(callback)
73
- return this
74
- }
75
-
76
- off(event, callback) {
77
- if (!this._listeners[event]) return this
78
- if (callback) {
79
- this._listeners[event] =
80
- this._listeners[event].filter(fn => fn !== callback)
81
- } else {
82
- this._listeners[event] = []
83
- }
84
- return this
85
- }
86
-
87
- once(event, fn) {
88
- const wrap = (...args) => {
89
- this.off(event, wrap)
90
- fn(...args)
91
- }
92
- return this.on(event, wrap)
93
- }
94
-
95
- emit(event, ...args) {
96
- const list = this._listeners[event]
97
- if (!list) return this
98
- for (const fn of list) fn(...args)
99
- return this
100
- }
101
-
102
- run(filename, bundle, argv) {
103
- const worklet = new Worklet()
104
- worklet.start(filename, bundle, argv)
105
- return worklet.IPC
13
+ opts = { app: appPath, ...opts }
14
+ super(opts)
106
15
  }
107
16
  }
108
-
109
- function noop() {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pear-mobile",
3
- "version": "0.0.1-rc",
3
+ "version": "0.0.2",
4
4
  "description": "Embeddable Pear runtime for mobile applications",
5
5
  "main": "index.js",
6
6
  "exports": {
@@ -14,10 +14,9 @@
14
14
  "package.json",
15
15
  "index.js",
16
16
  "index.d.ts",
17
- "lib/pear.bundle.js"
17
+ "index.rn.js"
18
18
  ],
19
19
  "scripts": {
20
- "build": "npx bare-pack --host ios --host android --linked --out ./lib/pear.bundle.js ./lib/pear.js",
21
20
  "format": "prettier . --write",
22
21
  "test": "prettier . --check && lunte && brittle-bare test/index.js",
23
22
  "lint": "lunte"
@@ -36,27 +35,11 @@
36
35
  "brittle": "^3.19.0",
37
36
  "lunte": "^1.2.0",
38
37
  "prettier": "^3.6.2",
39
- "prettier-config-holepunch": "^2.0.0",
40
- "hyperdrive": "^13.2.1",
41
- "hyperswarm": "^4.16.0",
42
- "localdrive": "^2.2.0",
43
- "pear-link": "^4.2.1",
44
- "bare-bundle-id": "^1.0.2",
45
- "bare-module-traverse": "^2.0.1",
46
- "bare-pack": "^2.0.0",
47
- "bare-path": "^3.0.0",
48
- "corestore": "^7.8.0",
49
- "hypercore-id-encoding": "^1.3.0",
50
- "ready-resource": "^1.2.0"
38
+ "prettier-config-holepunch": "^2.0.0"
51
39
  },
52
40
  "dependencies": {
53
- "b4a": "^1.7.4",
54
- "bare-rpc": "^1.1.0",
55
- "react-native-fs": "^2.20.0"
56
- },
57
- "peerDependencies": {
58
- "react-native-bare-kit": "*",
59
- "@react-native-async-storage/async-storage": "*",
60
- "react-native": "*"
41
+ "bare-fs": "^4.5.4",
42
+ "bare-path": "^3.0.0",
43
+ "pear-runtime-updater": "^0.0.10"
61
44
  }
62
45
  }