pear-runtime-updater 0.0.0 → 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 +1 -1
- package/index.d.ts +1 -0
- package/index.js +75 -76
- package/package.json +14 -6
package/README.md
CHANGED
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare module 'pear-runtime-updater'
|
package/index.js
CHANGED
|
@@ -1,116 +1,115 @@
|
|
|
1
|
-
const Hyperswarm = require(
|
|
2
|
-
const Hyperdrive = require(
|
|
3
|
-
const Localdrive = require(
|
|
4
|
-
const Corestore = require(
|
|
5
|
-
const path = require(
|
|
6
|
-
const fs = require(
|
|
7
|
-
const ReadyResource = require(
|
|
8
|
-
const link = require(
|
|
9
|
-
const hid = require(
|
|
10
|
-
const { platform, arch } = require(
|
|
11
|
-
const host = platform +
|
|
1
|
+
const Hyperswarm = require('hyperswarm')
|
|
2
|
+
const Hyperdrive = require('hyperdrive')
|
|
3
|
+
const Localdrive = require('localdrive')
|
|
4
|
+
const Corestore = require('corestore')
|
|
5
|
+
const path = require('path')
|
|
6
|
+
const fs = require('fs')
|
|
7
|
+
const ReadyResource = require('ready-resource')
|
|
8
|
+
const link = require('pear-link')
|
|
9
|
+
const hid = require('hypercore-id-encoding')
|
|
10
|
+
const { platform, arch } = require('which-runtime')
|
|
11
|
+
const host = platform + '-' + arch
|
|
12
12
|
|
|
13
13
|
module.exports = class PearRuntime extends ReadyResource {
|
|
14
14
|
constructor(config) {
|
|
15
|
-
super()
|
|
16
|
-
|
|
17
|
-
if (
|
|
18
|
-
if (!config.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
15
|
+
super()
|
|
16
|
+
|
|
17
|
+
if (config.updates === false) return {}
|
|
18
|
+
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
|
|
25
26
|
this.link = link.serialize({
|
|
26
|
-
drive: { fork: this.fork, length: this.length, key: this.key }
|
|
27
|
-
})
|
|
28
|
-
this.store = new Corestore(path.join(this.dir,
|
|
29
|
-
this.drive = new Hyperdrive(this.store, this.key)
|
|
30
|
-
this.swarm = null
|
|
31
|
-
this.next = null
|
|
32
|
-
this.checkout = null
|
|
33
|
-
|
|
34
|
-
this.updating = false
|
|
35
|
-
this.updated = false
|
|
36
|
-
|
|
37
|
-
this.ready().catch(noop)
|
|
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)
|
|
31
|
+
this.swarm = null
|
|
32
|
+
this.next = null
|
|
33
|
+
this.checkout = null
|
|
34
|
+
|
|
35
|
+
this.updating = false
|
|
36
|
+
this.updated = false
|
|
37
|
+
|
|
38
|
+
this.ready().catch(noop)
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
async _open() {
|
|
41
|
-
await fs.promises.rm(path.join(this.dir,
|
|
42
|
+
await fs.promises.rm(path.join(this.dir, 'pear-runtime/next'), {
|
|
42
43
|
recursive: true,
|
|
43
|
-
force: true
|
|
44
|
-
})
|
|
44
|
+
force: true
|
|
45
|
+
})
|
|
45
46
|
|
|
46
47
|
if (!this.swarm) {
|
|
47
|
-
const keyPair = await this.store.createKeyPair(
|
|
48
|
-
this.swarm = new Hyperswarm({ keyPair })
|
|
48
|
+
const keyPair = await this.store.createKeyPair('pear-container')
|
|
49
|
+
this.swarm = new Hyperswarm({ keyPair })
|
|
49
50
|
}
|
|
50
51
|
|
|
51
|
-
this.swarm.on(
|
|
52
|
-
this.store.replicate(connection),
|
|
53
|
-
);
|
|
52
|
+
this.swarm.on('connection', (connection) => this.store.replicate(connection))
|
|
54
53
|
this.swarm.join(this.drive.core.discoveryKey, {
|
|
55
54
|
client: true,
|
|
56
|
-
server: false
|
|
57
|
-
})
|
|
55
|
+
server: false
|
|
56
|
+
})
|
|
58
57
|
|
|
59
|
-
this._updateBackground()
|
|
60
|
-
this.drive.core.on(
|
|
58
|
+
this._updateBackground()
|
|
59
|
+
this.drive.core.on('append', () => this._updateBackground())
|
|
61
60
|
}
|
|
62
61
|
|
|
63
62
|
async _close() {
|
|
64
|
-
await this.drive
|
|
65
|
-
|
|
66
|
-
await this.store
|
|
67
|
-
await this.swarm
|
|
63
|
+
await this.drive?.close()
|
|
64
|
+
await this.checkout?.close()
|
|
65
|
+
await this.store?.destroy()
|
|
66
|
+
await this.swarm?.destroy()
|
|
68
67
|
}
|
|
69
68
|
|
|
70
69
|
_updateBackground() {
|
|
71
|
-
this._update().catch(noop)
|
|
70
|
+
this._update().catch(noop)
|
|
72
71
|
}
|
|
73
72
|
|
|
74
73
|
async _update() {
|
|
75
|
-
if (this.updating) return
|
|
76
|
-
this.updating = true
|
|
74
|
+
if (this.updating) return
|
|
75
|
+
this.updating = true
|
|
77
76
|
|
|
78
|
-
const length = this.drive.core.length
|
|
79
|
-
const id = length +
|
|
80
|
-
const next = path.join(this.dir,
|
|
81
|
-
const co = this.drive.checkout(length)
|
|
77
|
+
const length = this.drive.core.length
|
|
78
|
+
const id = length + '.' + this.drive.core.fork
|
|
79
|
+
const next = path.join(this.dir, 'pear-runtime/next', id)
|
|
80
|
+
const co = this.drive.checkout(length)
|
|
82
81
|
|
|
83
|
-
this.checkout = co
|
|
82
|
+
this.checkout = co
|
|
84
83
|
|
|
85
|
-
const manifest = await co.get(
|
|
84
|
+
const manifest = await co.get('/package.json')
|
|
86
85
|
if (!manifest || JSON.parse(manifest).version === this.version) {
|
|
87
|
-
this.updating = false
|
|
88
|
-
this.checkout = null
|
|
89
|
-
await co.close()
|
|
90
|
-
return
|
|
86
|
+
this.updating = false
|
|
87
|
+
this.checkout = null
|
|
88
|
+
await co.close()
|
|
89
|
+
return
|
|
91
90
|
}
|
|
92
91
|
|
|
93
|
-
const local = new Localdrive(next)
|
|
92
|
+
const local = new Localdrive(next)
|
|
94
93
|
|
|
95
|
-
this.emit(
|
|
96
|
-
const prefix =
|
|
94
|
+
this.emit('updating')
|
|
95
|
+
const prefix = '/by-arch/' + host + '/app/' + this.name
|
|
97
96
|
for await (const data of co.mirror(local, { prefix })) {
|
|
98
|
-
this.emit(
|
|
97
|
+
this.emit('updating-delta', data)
|
|
99
98
|
}
|
|
100
99
|
|
|
101
|
-
await co.close()
|
|
102
|
-
await local.close()
|
|
100
|
+
await co.close()
|
|
101
|
+
await local.close()
|
|
103
102
|
|
|
104
|
-
this.checkout = null
|
|
105
|
-
this.length = length
|
|
106
|
-
this.next = next
|
|
103
|
+
this.checkout = null
|
|
104
|
+
this.length = length
|
|
105
|
+
this.next = next
|
|
107
106
|
|
|
108
|
-
this.updating = false
|
|
109
|
-
this.updated = true
|
|
110
|
-
this.emit(
|
|
107
|
+
this.updating = false
|
|
108
|
+
this.updated = true
|
|
109
|
+
this.emit('updated')
|
|
111
110
|
|
|
112
|
-
if (this.drive.core.length > length) this._updateBackground()
|
|
111
|
+
if (this.drive.core.length > length) this._updateBackground()
|
|
113
112
|
}
|
|
114
|
-
}
|
|
113
|
+
}
|
|
115
114
|
|
|
116
115
|
function noop() {}
|
package/package.json
CHANGED
|
@@ -1,16 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pear-runtime-updater",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "Listens for OTA Pear App updates",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Holepunch Inc",
|
|
7
7
|
"main": "index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
"./package": "./package.json",
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./index.d.ts",
|
|
12
|
+
"default": "./index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
8
15
|
"files": [
|
|
9
|
-
"
|
|
16
|
+
"package.json",
|
|
17
|
+
"index.js",
|
|
18
|
+
"index.d.ts"
|
|
10
19
|
],
|
|
11
20
|
"scripts": {
|
|
12
|
-
"format": "prettier --write
|
|
13
|
-
"test": "prettier --check
|
|
21
|
+
"format": "prettier . --write",
|
|
22
|
+
"test": "prettier . --check && lunte",
|
|
23
|
+
"lint": "lunte"
|
|
14
24
|
},
|
|
15
25
|
"imports": {
|
|
16
26
|
"fs": {
|
|
@@ -25,9 +35,7 @@
|
|
|
25
35
|
"dependencies": {
|
|
26
36
|
"bare-fs": "^4.5.3",
|
|
27
37
|
"bare-path": "^3.0.0",
|
|
28
|
-
"bare-sidecar": "^0.3.0",
|
|
29
38
|
"corestore": "^7.8.0",
|
|
30
|
-
"fs-native-extensions": "^1.4.5",
|
|
31
39
|
"hypercore-id-encoding": "^1.3.0",
|
|
32
40
|
"hyperdrive": "^13.2.1",
|
|
33
41
|
"hyperswarm": "^4.16.0",
|