-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathconstants.js
More file actions
104 lines (96 loc) · 2.74 KB
/
Copy pathconstants.js
File metadata and controls
104 lines (96 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict'
const sodium = require('sodium-native')
const { isWindows, isLinux } = require('which-runtime')
const { fileURLToPath, pathToFileURL } = require('url-file-url')
const path = require('bare-path')
const os = require('bare-os')
const b4a = require('b4a')
const pkg = require('./package.json')
const BIN = 'out/make/'
const IPC_ID = 'pear'
const RUNTIME_EXEC = isWindows ? 'pear-runtime.exe' : 'pear-runtime'
const dir = (p) => toPath(new URL(p, platformUrl()))
let _channel = null
let _standalone = null
let _pearDevRoot = null
let _platformDir = null
let _platformLock = null
let _platformCorestore = null
let _logPath = null
let _gc = null
let _runtime = null
let _socketPath = null
module.exports = {
VERSION: pkg.version,
CONNECT_TIMEOUT: 20_000,
SPINDOWN_TIMEOUT: 60_000,
KNOWN_NODES_LIMIT: 100,
get LOCALDEV() {
return !_standalone
},
get UPGRADE() {
if (_channel === null) throw new Error('UPGRADE read before init()')
return pkg.upgrade[_channel]
},
get PEAR_DEV_ROOT() {
return _pearDevRoot
},
get PLATFORM_DIR() {
return _platformDir
},
get PLATFORM_LOCK() {
return _platformLock
},
get PLATFORM_CORESTORE() {
return _platformCorestore
},
get LOG_PATH() {
return _logPath
},
get GC() {
return _gc
},
get RUNTIME() {
return _runtime
},
get SOCKET_PATH() {
return _socketPath
},
init(channel, standalone, pearDevRoot) {
const valid = ['production', 'stage', 'dev']
if (_channel !== null) throw new Error('channel already set')
if (!valid.includes(channel)) throw new Error(`invalid channel: ${channel}`)
_channel = channel
_standalone = standalone
_pearDevRoot = pearDevRoot
_platformDir = toPath(platformUrl())
_platformLock = dir('pear.lock')
_platformCorestore = dir('corestores/platform-next')
_logPath = dir('pear.log')
_gc = dir('gc')
_runtime = dir(BIN + RUNTIME_EXEC)
_socketPath = isWindows
? `\\\\.\\pipe\\${IPC_ID}-${pipeId(_platformDir)}`
: `${_platformDir}/${IPC_ID}.sock`
}
}
function platformUrl() {
const p = platformDir()
const u = pathToFileURL(p)
return u.pathname.endsWith('/') ? u : new URL(u.pathname + '/', u)
}
function platformDir() {
if (!_standalone) return path.join(__dirname, 'pear')
if (_pearDevRoot) return path.join(_pearDevRoot, 'pear')
if (isWindows) return path.join(os.homedir(), 'AppData', 'Roaming', 'pear')
if (isLinux) return path.join(os.homedir(), '.config', 'pear')
return path.join(os.homedir(), 'Library', 'Application Support', 'pear')
}
function toPath(u) {
return fileURLToPath(u).replace(/[/\\]$/, '') || '/'
}
function pipeId(s) {
const buf = b4a.allocUnsafe(32)
sodium.crypto_generichash(buf, b4a.from(s))
return b4a.toString(buf, 'hex')
}