pear-mobile 4.0.1 → 4.1.0

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.
Files changed (3) hide show
  1. package/README.md +6 -4
  2. package/index.js +23 -2
  3. package/package.json +4 -2
package/README.md CHANGED
@@ -51,10 +51,12 @@ Create a runtime. `opts` may include:
51
51
  - **`dir`** Directory to store data (e.g. app data dir). Defaults to `/Documents`.
52
52
  - **`upgrade`** – (required) Pear link for OTA updates (e.g. from `package.json` `upgrade` field).
53
53
  - **`name`** - (required) The package.json prductName of the app.
54
- - **`app`** The path to the local OTA react-native bundle as booted from native code. (defaults to [pear-runtime-react-native](https://github.com/holepunchto/pear-runtime-react-native) default)
55
- - **`version`** Current app version (e.g. from `package.json`); used for update checks.
56
- - **`updates`** – Set to `false` to disable P2P OTA updates.
57
- - **`storage`** – Saves the app storage path.
54
+ - **`store`** - (optional) pass a [Corestore](https://github.com/holepunchto/corestore) to be used for updates. If passed `swarm` must also be passed. The `store` should be replicated over the `swarm`.
55
+ - **`swarm`** - (optional) pass a [Hyperswarm](https://github.com/holepunchto/hyperswarm) to be used for swarming updates. If passed `store` must also be passed. The `store` should be replicated over the `swarm`.
56
+ - **`app`** – (optional) The path to the local OTA react-native bundle as booted from native code. (defaults to [pear-runtime-react-native](https://github.com/holepunchto/pear-runtime-react-native) default)
57
+ - **`version`** – (optional) Current app version (i.e. from `package.json`); used for update checks.
58
+ - **`updates`** – (optional) Set to `false` to disable P2P OTA updates.
59
+ - **`storage`** – (optional) Saves the app storage path.
58
60
 
59
61
  #### `pear.storage`
60
62
 
package/index.js CHANGED
@@ -1,14 +1,24 @@
1
1
  const PearRuntimeUpdater = require('pear-runtime-updater')
2
- const ReadyResouce = require('ready-resource')
2
+ const ReadyResource = require('ready-resource')
3
+ const Corestore = require('corestore')
4
+ const Hyperswarm = require('hyperswarm')
3
5
  const path = require('bare-path')
4
6
  const dir = require('bare-storage')
5
7
  const fs = require('bare-fs')
6
8
 
7
- module.exports = class PearRuntime extends ReadyResouce {
9
+ module.exports = class PearRuntime extends ReadyResource {
8
10
  constructor(opts = {}) {
9
11
  super()
12
+ if ((!opts.store && opts.swarm) || (opts.store && !opts.swarm)) {
13
+ throw new Error('must pass store if passing swarm and vice versa')
14
+ }
10
15
  if (!opts.dir) opts.dir = dir.persistent()
16
+ this.opts = opts
17
+ if (!opts.store) opts.store = new Corestore(path.join(opts.dir, 'pear-runtime/corestore'))
18
+ this.store = opts.store
11
19
 
20
+ this.swarm = opts.swarm || null
21
+ this.bootstrap = opts.bootstrap
12
22
  this.dir = opts.dir
13
23
  this.storage = opts.storage || path.join(this.dir, 'app-storage')
14
24
 
@@ -20,9 +30,20 @@ module.exports = class PearRuntime extends ReadyResouce {
20
30
 
21
31
  async _open() {
22
32
  await this.updater.ready()
33
+ if (this.swarm === null) {
34
+ const keyPair = await this.store.createKeyPair('pear-runtime')
35
+ this.swarm = new Hyperswarm({ keyPair, bootstrap: this.bootstrap })
36
+ this.swarm.on('connection', (connection) => this.store.replicate(connection))
37
+ this.swarm.join(this.updater.drive.core.discoveryKey, {
38
+ client: true,
39
+ server: false
40
+ })
41
+ }
23
42
  }
24
43
 
25
44
  async _close() {
45
+ if (this.opts.swarm) await this.swarm.destroy()
26
46
  await this.updater.close()
47
+ if (this.opts.store) await this.store.close()
27
48
  }
28
49
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pear-mobile",
3
- "version": "4.0.1",
3
+ "version": "4.1.0",
4
4
  "description": "Embeddable Pear runtime for mobile applications",
5
5
  "main": "index.js",
6
6
  "exports": {
@@ -41,7 +41,9 @@
41
41
  "bare-fs": "^4.5.5",
42
42
  "bare-path": "^3.0.0",
43
43
  "bare-storage": "^1.0.1",
44
- "pear-runtime-updater": "^2.0.0",
44
+ "corestore": "^7.9.1",
45
+ "hyperswarm": "^4.17.0",
46
+ "pear-runtime-updater": "^3.0.0",
45
47
  "ready-resource": "^1.2.0"
46
48
  }
47
49
  }