pear-runtime-updater 2.0.2 → 3.0.1

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 +26 -4
  2. package/index.js +11 -36
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -14,11 +14,18 @@ This boilerplate is MVP and Experimental.
14
14
 
15
15
  ## Usage
16
16
 
17
+ Example in node.js:
18
+
17
19
  ```js
18
20
  const PearRuntimeUpdater = require('pear-runtime-updater')
19
21
  const path = require('path')
22
+ const Corestore = require('corestore')
23
+ const Hyperswarm = require('hyperswarm')
24
+ const goodbye = require('graceful-goodbye')
20
25
  const { version, upgrade } = require('./package.json')
21
26
 
27
+ const store = new Corestore('./my-app/corestore')
28
+
22
29
  function getApp() {
23
30
  return path.join(process.resourcesPath, '../..')
24
31
  }
@@ -27,8 +34,9 @@ const updater = new PearRuntimeUpdater({
27
34
  dir: path.join(app.getPath('userData')),
28
35
  upgrade,
29
36
  version,
30
- app: getApp() // path to .app / .AppImage
31
- name: 'name.ext' // <name>.app, <name>.AppImage, <name>.msix
37
+ app: getApp(), // path to .app / .AppImage
38
+ name: 'name.ext', // <name>.app, <name>.AppImage, <name>.msix
39
+ store
32
40
  })
33
41
 
34
42
  await updater.ready()
@@ -41,13 +49,26 @@ updater.on('updated', async () => {
41
49
  app.exit(0)
42
50
  })
43
51
 
44
- process.on('beforeExit', () => updater.close())
52
+ const keyPair = await store.createKeyPair('pear-runtime')
53
+ const swarm = new Hyperswarm({ keyPair })
54
+ swarm.on('connection', (connection) => store.replicate(connection))
55
+ swarm.join(updater.drive.core.discoveryKey, {
56
+ client: true,
57
+ server: false
58
+ })
59
+
60
+ // handle teardown
61
+ goodbye(async () => {
62
+ await swarm.destroy()
63
+ await updater.close()
64
+ await store.close()
65
+ })
45
66
  ```
46
67
 
47
68
  ## Features
48
69
 
49
70
  - Peer-to-peer over-the-air (P2P OTA) update listening
50
- - Replicates update content via [Hyperdrive](https://github.com/holepunchto/hyperdrive) / [Hyperswarm](https://github.com/holepunchto/hyperswarm)
71
+ - Appends update content via [Hyperdrive](https://github.com/holepunchto/hyperdrive)
51
72
  - Emits when an update is in progress, update diffs and when it’s ready
52
73
  - `applyUpdate()` to atomic swap the new build (bundled apps; macOS/Linux)
53
74
 
@@ -58,6 +79,7 @@ process.on('beforeExit', () => updater.close())
58
79
  - `opts.dir` – (required) Directory to store data (e.g. app data dir).
59
80
  - `opts.upgrade` – (required) Pear upgrade link (e.g. from `package.json` `upgrade` field).
60
81
  - `opts.name` – (required) Application name with extension.
82
+ - `opts.store` - (required) Pass a [Corestore](https://github.com/holepunchto/corestore) to be used for updates.
61
83
  - `opts.version` – (optional) Current app version; used to decide if an update should be stored.
62
84
  - `opts.app` – (optional) Path to the app bundle (for bundled apps; used with `applyUpdate()`).
63
85
  - `opts.bundled` – (optional) Whether the app is bundled. Defaults to `!!opts.app`.
package/index.js CHANGED
@@ -1,7 +1,5 @@
1
- const Hyperswarm = require('hyperswarm')
2
1
  const Hyperdrive = require('hyperdrive')
3
2
  const Localdrive = require('localdrive')
4
- const Corestore = require('corestore')
5
3
  const path = require('path')
6
4
  const fs = require('fs')
7
5
  const fsx = require('fs-native-extensions')
@@ -18,33 +16,23 @@ module.exports = class PearRuntimeUpdater extends ReadyResource {
18
16
  if (!opts.dir) throw new Error('dir required')
19
17
  if (!opts.upgrade) throw new Error('upgrade link required')
20
18
  if (!opts.name) throw new Error('name required')
19
+ if (!opts.store) throw new Error('store required')
21
20
 
22
21
  this.dir = opts.dir
22
+ this.store = opts.store
23
23
  this.version = opts.version || 0
24
24
  this.app = opts.app
25
25
  this.name = opts.name
26
- this.bootstrap = opts.bootstrap
27
26
  this.bundled = opts.bundled || !!this.app
28
27
  this.win32RestartAfterUpdate = opts.win32 && opts.win32.restart
29
28
 
30
- if (this.updates) {
31
- const { drive: upgrade } = link.parse(opts.upgrade)
32
- this.key = hid.decode(upgrade.key)
33
- this.length = upgrade.length || 0
34
- this.fork = upgrade.fork || 0
35
- this.link = link.serialize({ drive: { fork: this.fork, length: this.length, key: this.key } })
36
- this.store = new Corestore(path.join(this.dir, 'pear-runtime/corestore'))
37
- this.drive = new Hyperdrive(this.store, this.key)
38
- } else {
39
- this.key = null
40
- this.length = null
41
- this.fork = null
42
- this.link = null
43
- this.store = null
44
- this.drive = null
45
- }
29
+ const { drive: upgrade } = link.parse(opts.upgrade)
30
+ this.key = hid.decode(upgrade.key)
31
+ this.length = upgrade.length || 0
32
+ this.fork = upgrade.fork || 0
33
+ this.link = link.serialize({ drive: { fork: this.fork, length: this.length, key: this.key } })
34
+ this.drive = new Hyperdrive(this.store, this.key)
46
35
 
47
- this.swarm = null
48
36
  this.next = null
49
37
  this.checkout = null
50
38
  this.updating = false
@@ -54,8 +42,8 @@ module.exports = class PearRuntimeUpdater extends ReadyResource {
54
42
  }
55
43
 
56
44
  async _open() {
57
- if (!this.updates) return
58
45
  await this.drive.ready()
46
+ if (!this.updates) return
59
47
 
60
48
  if (this.bundled) {
61
49
  await fs.promises.rm(path.join(this.dir, 'pear-runtime/next'), {
@@ -63,29 +51,16 @@ module.exports = class PearRuntimeUpdater extends ReadyResource {
63
51
  force: true
64
52
  })
65
53
 
66
- if (!this.swarm) {
67
- const keyPair = await this.store.createKeyPair('pear-container')
68
- this.swarm = new Hyperswarm({ keyPair, bootstrap: this.bootstrap })
69
- }
70
-
71
- this.swarm.on('connection', (connection) => this.store.replicate(connection))
72
- this.swarm.join(this.drive.core.discoveryKey, {
73
- client: true,
74
- server: false
75
- })
76
-
77
54
  this._updateBackground()
78
55
  this.drive.core.on('append', () => this._updateBackground())
79
56
  }
80
57
  }
81
58
 
82
59
  async _close() {
83
- if (!this.updates) return
84
-
85
60
  await this.drive.close()
61
+
62
+ if (!this.updates) return
86
63
  if (this.checkout !== null) await this.checkout.close()
87
- await this.store.close()
88
- if (this.swarm) await this.swarm.destroy()
89
64
  }
90
65
 
91
66
  async applyUpdate() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pear-runtime-updater",
3
- "version": "2.0.2",
3
+ "version": "3.0.1",
4
4
  "description": "Listens for OTA Pear App updates",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Holepunch Inc",
@@ -45,11 +45,9 @@
45
45
  "dependencies": {
46
46
  "bare-fs": "^4.5.3",
47
47
  "bare-path": "^3.0.0",
48
- "corestore": "^7.8.0",
49
48
  "fs-native-extensions": "^1.4.5",
50
49
  "hypercore-id-encoding": "^1.3.0",
51
50
  "hyperdrive": "^13.2.1",
52
- "hyperswarm": "^4.16.0",
53
51
  "localdrive": "^2.2.0",
54
52
  "msix-manager": "^0.2.1",
55
53
  "pear-link": "^4.2.1",
@@ -62,6 +60,8 @@
62
60
  "bare-os": "^3.7.0",
63
61
  "bare-subprocess": "^5.2.2",
64
62
  "brittle": "^3.19.1",
63
+ "corestore": "^7.9.1",
64
+ "hyperswarm": "^4.17.0",
65
65
  "lunte": "^1.6.0",
66
66
  "pear-build": "^0.2.0",
67
67
  "prettier": "^3.8.1",