pear-runtime-updater 3.0.8 → 3.0.11

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 +7 -0
  2. package/index.js +21 -3
  3. package/package.json +16 -2
package/README.md CHANGED
@@ -41,6 +41,7 @@ const updater = new PearRuntimeUpdater({
41
41
 
42
42
  await updater.ready()
43
43
 
44
+ updater.on('update-scheduled', (delay) => console.log('Update will start in', delay))
44
45
  updater.on('updating', () => console.log('Update downloading…'))
45
46
  updater.on('updated', async () => {
46
47
  console.log('Update ready')
@@ -71,6 +72,7 @@ goodbye(async () => {
71
72
  - Appends update content via [Hyperdrive](https://github.com/holepunchto/hyperdrive)
72
73
  - Emits when an update is in progress, update diffs and when it’s ready
73
74
  - `applyUpdate()` to atomic swap the new build (bundled apps; macOS/Linux)
75
+ - Default random update delay to avoid seeder overload on new releases
74
76
 
75
77
  ## API
76
78
 
@@ -84,6 +86,11 @@ goodbye(async () => {
84
86
  - `opts.app` – (optional) Path to the app bundle (for bundled apps; used with `applyUpdate()`).
85
87
  - `opts.bundled` – (optional) Whether the app is bundled. Defaults to `!!opts.app`.
86
88
  - `opts.updates` – (optional) Set to false to opt out of updates.
89
+ - `opts.delay` – (optional) Upper bound (in ms) for the randomized update mirror delay. Defaults to 3_600_000 (60 minutes).
90
+
91
+ #### `updater.on('update-scheduled')`
92
+
93
+ Emitted when a new upgrade drive length is detected and the update has been scheduled with a random delay.
87
94
 
88
95
  #### `updater.on('updating')`
89
96
 
package/index.js CHANGED
@@ -34,12 +34,21 @@ module.exports = class PearRuntimeUpdater extends ReadyResource {
34
34
  this.link = link.serialize({ drive: { fork: this.fork, length: this.length, key: this.key } })
35
35
  this.drive = new Hyperdrive(this.store, this.key)
36
36
 
37
+ this._start = Date.now()
38
+ this._bootGracePeriod = 60000
39
+
37
40
  this.next = null
38
41
  this.checkout = null
39
42
  this.prefetched = false
40
43
  this.updating = false
41
44
  this.updated = false
42
45
 
46
+ this._defaultDelay = 3_600_000 // defaults to 1h
47
+ this._delay = Math.floor(
48
+ Math.random() * (Number.isInteger(opts.delay) ? opts.delay : this._defaultDelay)
49
+ )
50
+ this._scheduledUpdate = null
51
+
43
52
  this._debouncedUpdate = debounceify(this._update.bind(this))
44
53
 
45
54
  this.ready().catch(noop)
@@ -56,9 +65,17 @@ module.exports = class PearRuntimeUpdater extends ReadyResource {
56
65
  })
57
66
 
58
67
  this._debouncedUpdate().catch((err) => this.emit('error', err))
59
- this.drive.core.on('append', () =>
60
- this._debouncedUpdate().catch((err) => this.emit('error', err))
61
- )
68
+ this.drive.core.on('append', () => {
69
+ if (this._scheduledUpdate !== null) clearTimeout(this._scheduledUpdate) // cancel old pending updates before scheduling new one
70
+ const recentBoot = Date.now() - this._start <= this._bootGracePeriod
71
+ this._scheduledUpdate = setTimeout(
72
+ () => {
73
+ this._debouncedUpdate().catch((err) => this.emit('error', err))
74
+ },
75
+ recentBoot ? 0 : this._delay
76
+ )
77
+ this.emit('update-scheduled', this._delay)
78
+ })
62
79
  }
63
80
  }
64
81
 
@@ -67,6 +84,7 @@ module.exports = class PearRuntimeUpdater extends ReadyResource {
67
84
 
68
85
  if (!this.updates) return
69
86
  if (this.checkout !== null) await this.checkout.close()
87
+ if (this._scheduledUpdate) clearTimeout(this._scheduledUpdate)
70
88
  }
71
89
 
72
90
  async applyUpdate() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pear-runtime-updater",
3
- "version": "3.0.8",
3
+ "version": "3.0.11",
4
4
  "description": "Listens for OTA Pear App updates",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Holepunch Inc",
@@ -69,5 +69,19 @@
69
69
  "prettier": "^3.8.1",
70
70
  "prettier-config-holepunch": "^2.0.0",
71
71
  "test-tmp": "^1.4.0"
72
- }
72
+ },
73
+ "directories": {
74
+ "test": "test"
75
+ },
76
+ "repository": {
77
+ "type": "git",
78
+ "url": "git+https://github.com/holepunchto/pear-runtime-updater.git"
79
+ },
80
+ "keywords": [],
81
+ "type": "commonjs",
82
+ "types": "./index.d.ts",
83
+ "bugs": {
84
+ "url": "https://github.com/holepunchto/pear-runtime-updater/issues"
85
+ },
86
+ "homepage": "https://github.com/holepunchto/pear-runtime-updater#readme"
73
87
  }