pear-state 1.0.1 → 1.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.
- package/README.md +1 -1
- package/bare/env.js +1 -0
- package/index.js +165 -28
- package/node/env.js +1 -0
- package/package.json +29 -7
package/README.md
CHANGED
package/bare/env.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('bare-env')
|
package/index.js
CHANGED
|
@@ -9,7 +9,11 @@ const { PLATFORM_DIR, SWAP, RUNTIME } = require('pear-constants')
|
|
|
9
9
|
const CWD = isBare ? os.cwd() : process.cwd()
|
|
10
10
|
const ENV = isBare ? require('bare-env') : process.env
|
|
11
11
|
const plink = require('pear-link')
|
|
12
|
-
const {
|
|
12
|
+
const {
|
|
13
|
+
ERR_INVALID_PROJECT_DIR,
|
|
14
|
+
ERR_INVALID_APP_STORAGE,
|
|
15
|
+
ERR_INVALID_APP_NAME
|
|
16
|
+
} = require('pear-errors')
|
|
13
17
|
|
|
14
18
|
module.exports = class State {
|
|
15
19
|
env = null
|
|
@@ -31,7 +35,7 @@ module.exports = class State {
|
|
|
31
35
|
version = { key: null, length: 0, fork: 0 }
|
|
32
36
|
options = null
|
|
33
37
|
manifest = null
|
|
34
|
-
static async localPkg
|
|
38
|
+
static async localPkg(state) {
|
|
35
39
|
let pkg
|
|
36
40
|
try {
|
|
37
41
|
pkg = JSON.parse(await fsp.readFile(path.join(state.dir, 'package.json')))
|
|
@@ -45,15 +49,18 @@ module.exports = class State {
|
|
|
45
49
|
return pkg
|
|
46
50
|
}
|
|
47
51
|
|
|
48
|
-
static appname
|
|
52
|
+
static appname(pkg) {
|
|
49
53
|
return pkg?.pear?.name ?? pkg?.name ?? null
|
|
50
54
|
}
|
|
51
55
|
|
|
52
|
-
static async build
|
|
56
|
+
static async build(state, pkg = null) {
|
|
53
57
|
if (state.manifest) return state.manifest
|
|
54
58
|
const originDir = state.dir
|
|
55
59
|
if (pkg === null && state.key === null) pkg = await this.localPkg(state)
|
|
56
|
-
if (pkg === null)
|
|
60
|
+
if (pkg === null)
|
|
61
|
+
throw ERR_INVALID_PROJECT_DIR(
|
|
62
|
+
`"package.json not found from: ${originDir}. Pear project must have a package.json`
|
|
63
|
+
)
|
|
57
64
|
state.pkg = pkg
|
|
58
65
|
state.options = state.pkg?.pear ?? {}
|
|
59
66
|
|
|
@@ -62,13 +69,18 @@ module.exports = class State {
|
|
|
62
69
|
state.main = state.options.main ?? pkg?.main ?? 'index.js'
|
|
63
70
|
|
|
64
71
|
const invalidName = /^[@/a-z0-9-_]+$/.test(state.name) === false
|
|
65
|
-
if (invalidName)
|
|
72
|
+
if (invalidName)
|
|
73
|
+
throw ERR_INVALID_APP_NAME(
|
|
74
|
+
'App name must be lowercase and one word, and may contain letters, numbers, hyphens (-), underscores (_), forward slashes (/) and asperands (@).'
|
|
75
|
+
)
|
|
66
76
|
|
|
67
77
|
state.links = {
|
|
68
|
-
...Object.fromEntries(Object.entries(
|
|
78
|
+
...Object.fromEntries(Object.entries(state.options.links ?? {})),
|
|
69
79
|
...(state.links ?? {})
|
|
70
80
|
}
|
|
71
|
-
state.entrypoints = Array.isArray(state.options.stage?.entrypoints)
|
|
81
|
+
state.entrypoints = Array.isArray(state.options.stage?.entrypoints)
|
|
82
|
+
? state.options.stage?.entrypoints
|
|
83
|
+
: []
|
|
72
84
|
state.routes = state.options.routes || null
|
|
73
85
|
const unrouted = Array.isArray(state.options.unrouted) ? state.options.unrouted : []
|
|
74
86
|
state.unrouted = Array.from(new Set([...unrouted, ...state.entrypoints]))
|
|
@@ -79,14 +91,15 @@ module.exports = class State {
|
|
|
79
91
|
return state.manifest
|
|
80
92
|
}
|
|
81
93
|
|
|
82
|
-
static route
|
|
94
|
+
static route(state) {
|
|
83
95
|
let result = null
|
|
84
96
|
if (state.prerunning || !state.routes) {
|
|
85
97
|
result = { entrypoint: state.route, routed: false }
|
|
86
98
|
} else if (state.unrouted.some((unroute) => state.route.startsWith(unroute))) {
|
|
87
99
|
result = { entrypoint: state.route, routed: false }
|
|
88
100
|
} else {
|
|
89
|
-
let route =
|
|
101
|
+
let route =
|
|
102
|
+
typeof state.routes === 'string' ? state.routes : (state.routes[state.route] ?? state.route)
|
|
90
103
|
if (route[0] === '.') route = route.length === 1 ? '/' : route.slice(1)
|
|
91
104
|
result = { entrypoint: route, routed: true }
|
|
92
105
|
}
|
|
@@ -95,39 +108,147 @@ module.exports = class State {
|
|
|
95
108
|
return result
|
|
96
109
|
}
|
|
97
110
|
|
|
98
|
-
static storageFromLink
|
|
111
|
+
static storageFromLink(link) {
|
|
99
112
|
const parsed = typeof link === 'string' ? plink.parse(link) : link
|
|
100
113
|
const appStorage = path.join(PLATFORM_DIR, 'app-storage')
|
|
101
114
|
return parsed.protocol !== 'pear:'
|
|
102
115
|
? path.join(appStorage, 'by-random', crypto.randomBytes(16).toString('hex'))
|
|
103
|
-
: path.join(
|
|
116
|
+
: path.join(
|
|
117
|
+
appStorage,
|
|
118
|
+
'by-dkey',
|
|
119
|
+
crypto.discoveryKey(hypercoreid.decode(parsed.drive.key)).toString('hex')
|
|
120
|
+
)
|
|
104
121
|
}
|
|
105
122
|
|
|
106
|
-
static configFrom
|
|
107
|
-
const {
|
|
123
|
+
static configFrom(state) {
|
|
124
|
+
const {
|
|
125
|
+
id,
|
|
126
|
+
startId,
|
|
127
|
+
key,
|
|
128
|
+
links,
|
|
129
|
+
alias,
|
|
130
|
+
env,
|
|
131
|
+
gui,
|
|
132
|
+
assets,
|
|
133
|
+
options,
|
|
134
|
+
checkpoint,
|
|
135
|
+
checkout,
|
|
136
|
+
flags,
|
|
137
|
+
dev,
|
|
138
|
+
stage,
|
|
139
|
+
storage,
|
|
140
|
+
name,
|
|
141
|
+
main,
|
|
142
|
+
args,
|
|
143
|
+
channel,
|
|
144
|
+
release,
|
|
145
|
+
applink,
|
|
146
|
+
query,
|
|
147
|
+
fragment,
|
|
148
|
+
link,
|
|
149
|
+
linkData,
|
|
150
|
+
entrypoint,
|
|
151
|
+
route,
|
|
152
|
+
routes,
|
|
153
|
+
dir,
|
|
154
|
+
dht,
|
|
155
|
+
prerunning,
|
|
156
|
+
version
|
|
157
|
+
} = state
|
|
108
158
|
const pearDir = PLATFORM_DIR
|
|
109
159
|
const swapDir = SWAP
|
|
110
|
-
return {
|
|
160
|
+
return {
|
|
161
|
+
id,
|
|
162
|
+
startId,
|
|
163
|
+
key,
|
|
164
|
+
links,
|
|
165
|
+
alias,
|
|
166
|
+
env,
|
|
167
|
+
gui,
|
|
168
|
+
assets,
|
|
169
|
+
options,
|
|
170
|
+
checkpoint,
|
|
171
|
+
checkout,
|
|
172
|
+
flags,
|
|
173
|
+
dev,
|
|
174
|
+
stage,
|
|
175
|
+
storage,
|
|
176
|
+
name,
|
|
177
|
+
main,
|
|
178
|
+
args,
|
|
179
|
+
channel,
|
|
180
|
+
release,
|
|
181
|
+
applink,
|
|
182
|
+
query,
|
|
183
|
+
fragment,
|
|
184
|
+
link,
|
|
185
|
+
linkData,
|
|
186
|
+
entrypoint,
|
|
187
|
+
route,
|
|
188
|
+
routes,
|
|
189
|
+
dir,
|
|
190
|
+
dht,
|
|
191
|
+
prerunning,
|
|
192
|
+
pearDir,
|
|
193
|
+
swapDir,
|
|
194
|
+
length: version?.length,
|
|
195
|
+
fork: version?.fork
|
|
196
|
+
}
|
|
111
197
|
}
|
|
112
198
|
|
|
113
|
-
update
|
|
199
|
+
update(state) {
|
|
114
200
|
Object.assign(this, state)
|
|
115
201
|
this.#onupdate()
|
|
116
202
|
}
|
|
117
203
|
|
|
118
|
-
constructor
|
|
119
|
-
const { dht, link = '.', startId = null, id = null, args = null, env = ENV, cwd = CWD, dir = cwd, cmdArgs, onupdate = () => {}, flags = {}, run, storage = null, pid } = params
|
|
204
|
+
constructor(params = {}) {
|
|
120
205
|
const {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
206
|
+
dht,
|
|
207
|
+
link = '.',
|
|
208
|
+
startId = null,
|
|
209
|
+
id = null,
|
|
210
|
+
args = null,
|
|
211
|
+
env = ENV,
|
|
212
|
+
cwd = CWD,
|
|
213
|
+
dir = cwd,
|
|
214
|
+
cmdArgs,
|
|
215
|
+
onupdate = () => {},
|
|
216
|
+
flags = {},
|
|
217
|
+
run,
|
|
218
|
+
storage = null,
|
|
219
|
+
pid
|
|
220
|
+
} = params
|
|
221
|
+
const {
|
|
222
|
+
appling,
|
|
223
|
+
channel,
|
|
224
|
+
devtools,
|
|
225
|
+
checkout,
|
|
226
|
+
stage,
|
|
227
|
+
updates,
|
|
228
|
+
updatesDiff,
|
|
229
|
+
links = '',
|
|
230
|
+
prerunning = false,
|
|
231
|
+
dev = false,
|
|
232
|
+
parent = null,
|
|
233
|
+
followSymlinks,
|
|
234
|
+
unsafeClearAppStorage,
|
|
235
|
+
chromeWebrtcInternals
|
|
124
236
|
} = flags
|
|
125
237
|
const parsedLink = plink.parse(link)
|
|
126
|
-
const {
|
|
238
|
+
const {
|
|
239
|
+
drive: { alias = null, key = null } = {},
|
|
240
|
+
pathname: route = '',
|
|
241
|
+
protocol,
|
|
242
|
+
origin,
|
|
243
|
+
hash,
|
|
244
|
+
search
|
|
245
|
+
} = parsedLink
|
|
127
246
|
let pathname = protocol === 'file:' && isWindows ? route.slice(1) : route
|
|
128
247
|
// for on disk route support, this relies on passed in dir being the actual project dir:
|
|
129
248
|
if (protocol === 'file:') pathname = pathname.slice(dir.length)
|
|
130
|
-
const store = flags.tmpStore
|
|
249
|
+
const store = flags.tmpStore
|
|
250
|
+
? path.join(os.tmpdir(), crypto.randomBytes(16).toString('hex'))
|
|
251
|
+
: flags.store
|
|
131
252
|
this.#onupdate = onupdate
|
|
132
253
|
this.startId = startId
|
|
133
254
|
this.dht = dht
|
|
@@ -151,7 +272,11 @@ module.exports = class State {
|
|
|
151
272
|
this.route = pathname
|
|
152
273
|
this.linkData = this.route?.startsWith('/') ? this.route.slice(1) : this.route
|
|
153
274
|
this.key = key
|
|
154
|
-
this.link = link
|
|
275
|
+
this.link = link
|
|
276
|
+
? link.startsWith(protocol)
|
|
277
|
+
? link
|
|
278
|
+
: plink.normalize(plink.serialize(parsedLink))
|
|
279
|
+
: null
|
|
155
280
|
this.applink = key ? origin : plink.normalize(plink.serialize(plink.parse(this.dir)))
|
|
156
281
|
this.alias = alias
|
|
157
282
|
this.cmdArgs = cmdArgs
|
|
@@ -172,9 +297,21 @@ module.exports = class State {
|
|
|
172
297
|
links[key] = value
|
|
173
298
|
return links
|
|
174
299
|
}, {})
|
|
175
|
-
this.storage = this.store
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
300
|
+
this.storage = this.store
|
|
301
|
+
? path.isAbsolute(this.store)
|
|
302
|
+
? this.store
|
|
303
|
+
: path.resolve(this.cwd, this.store)
|
|
304
|
+
: this.storage
|
|
305
|
+
const invalidStorage =
|
|
306
|
+
this.key === null &&
|
|
307
|
+
this.storage !== null &&
|
|
308
|
+
this.storage.startsWith(this.dir) &&
|
|
309
|
+
this.storage.includes(path.sep + 'pear' + path.sep + 'pear' + path.sep) === false
|
|
310
|
+
if (invalidStorage)
|
|
311
|
+
throw ERR_INVALID_APP_STORAGE(
|
|
312
|
+
'Application Storage may not be inside the project directory. --store "' +
|
|
313
|
+
this.storage +
|
|
314
|
+
'" is invalid'
|
|
315
|
+
)
|
|
179
316
|
}
|
|
180
317
|
}
|
package/node/env.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = process.env
|
package/package.json
CHANGED
|
@@ -1,21 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pear-state",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"description": "Pear state",
|
|
7
7
|
"license": "Apache-2.0",
|
|
8
|
-
"files": [
|
|
8
|
+
"files": [
|
|
9
|
+
"bare/",
|
|
10
|
+
"node/"
|
|
11
|
+
],
|
|
12
|
+
"author": "Holepunch Inc",
|
|
9
13
|
"scripts": {
|
|
14
|
+
"format": "prettier --write .",
|
|
15
|
+
"lint": "prettier --check .",
|
|
10
16
|
"test:gen": "brittle -r test/all.js test/*.test.js",
|
|
11
|
-
"test": "
|
|
12
|
-
"test:
|
|
13
|
-
"
|
|
17
|
+
"test": "npm run test:bare && npm run test:node",
|
|
18
|
+
"test:bare": "brittle-bare --coverage test/all.js",
|
|
19
|
+
"test:node": "brittle-node --coverage test/all.js"
|
|
14
20
|
},
|
|
15
21
|
"imports": {
|
|
22
|
+
"#env": {
|
|
23
|
+
"bare": "./bare/env.js",
|
|
24
|
+
"default": "./node/env.js"
|
|
25
|
+
},
|
|
16
26
|
"fs": {
|
|
17
27
|
"bare": "bare-fs"
|
|
18
28
|
},
|
|
29
|
+
"fs/promises": {
|
|
30
|
+
"bare": "bare-fs/promises"
|
|
31
|
+
},
|
|
19
32
|
"path": {
|
|
20
33
|
"bare": "bare-path"
|
|
21
34
|
},
|
|
@@ -26,7 +39,8 @@
|
|
|
26
39
|
"devDependencies": {
|
|
27
40
|
"brittle": "^3.0.0",
|
|
28
41
|
"pear-errors": "^1.0.0",
|
|
29
|
-
"
|
|
42
|
+
"prettier": "^3.6.2",
|
|
43
|
+
"prettier-config-holepunch": "^2.0.0",
|
|
30
44
|
"url-file-url": "^1.0.5"
|
|
31
45
|
},
|
|
32
46
|
"dependencies": {
|
|
@@ -39,5 +53,13 @@
|
|
|
39
53
|
"pear-constants": "^1.0.0",
|
|
40
54
|
"pear-link": "^4.1.0",
|
|
41
55
|
"which-runtime": "^1.3.0"
|
|
42
|
-
}
|
|
56
|
+
},
|
|
57
|
+
"repository": {
|
|
58
|
+
"type": "git",
|
|
59
|
+
"url": "git+https://github.com/holepunchto/pear-state.git"
|
|
60
|
+
},
|
|
61
|
+
"bugs": {
|
|
62
|
+
"url": "https://github.com/holepunchto/pear-state/issues"
|
|
63
|
+
},
|
|
64
|
+
"homepage": "https://github.com/holepunchto/pear-state#readme"
|
|
43
65
|
}
|