pear-runtime-updater 0.0.2 → 0.0.3

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 (2) hide show
  1. package/index.js +50 -31
  2. package/package.json +2 -1
package/index.js CHANGED
@@ -4,6 +4,7 @@ const Localdrive = require('localdrive')
4
4
  const Corestore = require('corestore')
5
5
  const path = require('path')
6
6
  const fs = require('fs')
7
+ const fsx = require('fs-native-extensions')
7
8
  const ReadyResource = require('ready-resource')
8
9
  const link = require('pear-link')
9
10
  const hid = require('hypercore-id-encoding')
@@ -13,25 +14,30 @@ const host = platform + '-' + arch
13
14
  module.exports = class PearRuntime extends ReadyResource {
14
15
  constructor(config) {
15
16
  super()
17
+ this.updates = !!config.update && config.updates !== false
16
18
 
17
- if (config.updates === false) return {}
18
19
  if (!config.dir) throw new Error('dir required')
19
- if (!config.link) throw new Error('upgrade link required')
20
- const { drive } = link.parse(config.link)
21
- this.dir = config.dir
22
- this.version = config.version || 0
23
- this.key = hid.decode(drive.key)
24
- this.length = drive.length || 0
25
- this.fork = drive.fork || 0
26
- this.link = link.serialize({
27
- drive: { fork: this.fork, length: this.length, key: this.key }
28
- })
29
- this.store = new Corestore(path.join(this.dir, 'pear-runtime/corestore'))
30
- this.drive = new Hyperdrive(this.store, this.key)
20
+
21
+ if (this.updates) {
22
+ const { drive: upgrade } = link.parse(config.upgrade)
23
+ this.key = hid.decode(upgrade.key)
24
+ this.length = upgrade.length || 0
25
+ this.fork = upgrade.fork || 0
26
+ this.link = link.serialize({ drive: { fork: this.fork, length: this.length, key: this.key } })
27
+ this.store = new Corestore(path.join(this.dir, 'pear-runtime/corestore'))
28
+ this.drive = new Hyperdrive(this.store, this.key)
29
+ } else {
30
+ this.key = null
31
+ this.length = null
32
+ this.fork = null
33
+ this.link = null
34
+ this.store = null
35
+ this.drive = null
36
+ }
37
+
31
38
  this.swarm = null
32
39
  this.next = null
33
40
  this.checkout = null
34
-
35
41
  this.updating = false
36
42
  this.updated = false
37
43
 
@@ -39,24 +45,28 @@ module.exports = class PearRuntime extends ReadyResource {
39
45
  }
40
46
 
41
47
  async _open() {
42
- await fs.promises.rm(path.join(this.dir, 'pear-runtime/next'), {
43
- recursive: true,
44
- force: true
45
- })
46
-
47
- if (!this.swarm) {
48
- const keyPair = await this.store.createKeyPair('pear-container')
49
- this.swarm = new Hyperswarm({ keyPair })
48
+ await this.drive?.ready()
49
+
50
+ if (this.bundled && this.updates !== false) {
51
+ await fs.promises.rm(path.join(this.dir, 'pear-runtime/next'), {
52
+ recursive: true,
53
+ force: true
54
+ })
55
+
56
+ if (!this.swarm) {
57
+ const keyPair = await this.store.createKeyPair('pear-container')
58
+ this.swarm = new Hyperswarm({ keyPair })
59
+ }
60
+
61
+ this.swarm.on('connection', (connection) => this.store.replicate(connection))
62
+ this.swarm.join(this.drive.core.discoveryKey, {
63
+ client: true,
64
+ server: false
65
+ })
66
+
67
+ this._updateBackground()
68
+ this.drive.core.on('append', () => this._updateBackground())
50
69
  }
51
-
52
- this.swarm.on('connection', (connection) => this.store.replicate(connection))
53
- this.swarm.join(this.drive.core.discoveryKey, {
54
- client: true,
55
- server: false
56
- })
57
-
58
- this._updateBackground()
59
- this.drive.core.on('append', () => this._updateBackground())
60
70
  }
61
71
 
62
72
  async _close() {
@@ -66,6 +76,15 @@ module.exports = class PearRuntime extends ReadyResource {
66
76
  await this.swarm?.destroy()
67
77
  }
68
78
 
79
+ async applyUpdate() {
80
+ if (!this.updated || this.applied || !this.bundled) return
81
+ this.applied = true
82
+
83
+ // mac only for now, linux similar, windows, more pain
84
+ await fsx.swap(path.join(this.next, 'by-arch', host, 'app', this.name), this.app)
85
+ await fs.promises.rm(this.next, { recursive: true, force: true })
86
+ }
87
+
69
88
  _updateBackground() {
70
89
  this._update().catch(noop)
71
90
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pear-runtime-updater",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Listens for OTA Pear App updates",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Holepunch Inc",
@@ -36,6 +36,7 @@
36
36
  "bare-fs": "^4.5.3",
37
37
  "bare-path": "^3.0.0",
38
38
  "corestore": "^7.8.0",
39
+ "fs-native-extensions": "^1.4.5",
39
40
  "hypercore-id-encoding": "^1.3.0",
40
41
  "hyperdrive": "^13.2.1",
41
42
  "hyperswarm": "^4.16.0",