pear-state 1.0.2 → 1.0.4

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 CHANGED
@@ -6,4 +6,4 @@
6
6
 
7
7
  ## License
8
8
 
9
- Apache-2.0
9
+ Apache-2.0
package/bare/env.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('bare-env')
package/index.js CHANGED
@@ -7,9 +7,13 @@ const hypercoreid = require('hypercore-id-encoding')
7
7
  const crypto = require('hypercore-crypto')
8
8
  const { PLATFORM_DIR, SWAP, RUNTIME } = require('pear-constants')
9
9
  const CWD = isBare ? os.cwd() : process.cwd()
10
- const ENV = isBare ? require('bare-env') : process.env
10
+ const ENV = require('#env')
11
11
  const plink = require('pear-link')
12
- const { ERR_INVALID_PROJECT_DIR, ERR_INVALID_APP_STORAGE, ERR_INVALID_APP_NAME } = require('pear-errors')
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 (state) {
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 (pkg) {
52
+ static appname(pkg) {
49
53
  return pkg?.pear?.name ?? pkg?.name ?? null
50
54
  }
51
55
 
52
- static async build (state, pkg = null) {
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) throw ERR_INVALID_PROJECT_DIR(`"package.json not found from: ${originDir}. Pear project must have a package.json`)
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) throw ERR_INVALID_APP_NAME('App name must be lowercase and one word, and may contain letters, numbers, hyphens (-), underscores (_), forward slashes (/) and asperands (@).')
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((state.options.links ?? {}))),
78
+ ...Object.fromEntries(Object.entries(state.options.links ?? {})),
69
79
  ...(state.links ?? {})
70
80
  }
71
- state.entrypoints = Array.isArray(state.options.stage?.entrypoints) ? 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 (state) {
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 = typeof state.routes === 'string' ? state.routes : (state.routes[state.route] ?? state.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 (link) {
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(appStorage, 'by-dkey', crypto.discoveryKey(hypercoreid.decode(parsed.drive.key)).toString('hex'))
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 (state) {
107
- const { id, startId, key, links, alias, env, gui, assets, options, checkpoint, checkout, flags, dev, stage, storage, name, main, args, channel, release, applink, query, fragment, link, linkData, entrypoint, route, routes, dir, dht, prerunning, version } = state
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 { id, startId, key, links, alias, env, gui, assets, options, checkpoint, checkout, flags, dev, stage, storage, name, main, args, channel, release, applink, query, fragment, link, linkData, entrypoint, route, routes, dir, dht, prerunning, pearDir, swapDir, length: version?.length, fork: version?.fork }
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 (state) {
199
+ update(state) {
114
200
  Object.assign(this, state)
115
201
  this.#onupdate()
116
202
  }
117
203
 
118
- constructor (params = {}) {
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
- appling, channel, devtools, checkout, stage, updates, updatesDiff,
122
- links = '', prerunning = false, dev = false, parent = null,
123
- followSymlinks, unsafeClearAppStorage, chromeWebrtcInternals
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 { drive: { alias = null, key = null } = {}, pathname: route = '', protocol, origin, hash, search } = parsedLink
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 ? path.join(os.tmpdir(), crypto.randomBytes(16).toString('hex')) : flags.store
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 ? (link.startsWith(protocol) ? link : plink.normalize(plink.serialize(parsedLink))) : null
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 ? (path.isAbsolute(this.store) ? this.store : path.resolve(this.cwd, this.store)) : this.storage
176
- const invalidStorage = this.key === null && this.storage !== null &&
177
- this.storage.startsWith(this.dir) && this.storage.includes(path.sep + 'pear' + path.sep + 'pear' + path.sep) === false
178
- if (invalidStorage) throw ERR_INVALID_APP_STORAGE('Application Storage may not be inside the project directory. --store "' + this.storage + '" is invalid')
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,18 +1,28 @@
1
1
  {
2
2
  "name": "pear-state",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
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": "brittle-bare --coverage test/all.js",
12
- "test:node": "brittle-node --coverage test/all.js",
13
- "lint": "standard"
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,7 +39,8 @@
29
39
  "devDependencies": {
30
40
  "brittle": "^3.0.0",
31
41
  "pear-errors": "^1.0.0",
32
- "standard": "^17.1.2",
42
+ "prettier": "^3.6.2",
43
+ "prettier-config-holepunch": "^2.0.0",
33
44
  "url-file-url": "^1.0.5"
34
45
  },
35
46
  "dependencies": {
@@ -42,5 +53,13 @@
42
53
  "pear-constants": "^1.0.0",
43
54
  "pear-link": "^4.1.0",
44
55
  "which-runtime": "^1.3.0"
45
- }
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"
46
65
  }