-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathpre.js
More file actions
168 lines (157 loc) · 4.73 KB
/
pre.js
File metadata and controls
168 lines (157 loc) · 4.73 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict'
const { Readable } = require('streamx')
const Module = require('bare-module')
const plink = require('pear-link')
const fs = require('bare-fs')
const { isWindows } = require('which-runtime')
const { RUNTIME } = require('pear-constants')
const { spawn } = require('bare-subprocess')
const { pathToFileURL } = require('url-file-url')
const { ERR_INVALID_CONFIG } = require('pear-errors')
const State = require('pear-state')
const path = require('bare-path')
const cenc = require('compact-encoding')
module.exports = class Pre extends Readable {
pkg = null
options = null
pre = null
#finalled = false
constructor(op, { dir, cwd, entrypoint = '/' }, pkg) {
super()
this.op = op
this.dir = dir
this.applink = pathToFileURL(dir).href
this.cwd = cwd
this.link = plink.normalize(pathToFileURL(path.join(dir, entrypoint)).href)
this.pkg = pkg
this.index = 0
this.#pre().catch((err) => this.destroy(err))
}
#final() {
if (this.#finalled) return
this.#finalled = true
this.push({ tag: 'final', data: this.pkg })
this.push(null)
}
async #pre() {
if (this.pkg === null) {
this.#final()
return
}
this.options = this.pkg?.pear ?? {}
const topPre = this.options.pre
? Array.isArray(this.options.pre)
? this.options.pre
: [this.options.pre]
: []
const opOpts = this.options[this.op]
const opPre = opOpts?.pre ? (Array.isArray(opOpts.pre) ? opOpts.pre : [opOpts.pre]) : []
const pre = [...topPre, ...opPre]
this.pre = pre.length > 0 ? pre : null
const state = new State()
await State.build(state, this.pkg)
if (this.pre === null) {
this.#final()
return
}
if (this.op === 'run') {
let hasPipe
try {
hasPipe = isWindows ? fs.fstatSync(3).isFIFO() : fs.fstatSync(3).isSocket()
} catch {
hasPipe = false
}
if (hasPipe) {
this.#final()
return
}
}
for (let specifier of this.pre) {
if (this.link.endsWith(specifier)) continue
specifier = specifier[0] === '/' ? '.' + specifier : specifier
const base = this.applink.endsWith('/') ? this.applink : this.applink + '/'
const url =
specifier[0] === '.' ? new URL(specifier, base) : Module.resolve(specifier, new URL(base))
const link = url.href
if (this.link === link) continue
this.options = await this.#run(this.options, link, this.index++, specifier)
}
this.pkg.pear = this.options
this.#final()
}
#run(options, link, index, from) {
const { cwd } = this
const sp = spawn(RUNTIME, ['run', '--base', this.dir, '--prerunning', '--trusted', link], {
stdio: ['ignore', 'pipe', 'pipe', 'overlapped'],
windowsHide: true,
cwd
})
const IDLE_TIMEOUT = 5000
const SELF_CLOSE_WAIT = 2500
sp.stdio[1].on('data', (output) =>
this.push({ tag: 'preio', data: { from, output, index, fd: 1 } })
)
sp.stdio[2].on('data', (output) =>
this.push({ tag: 'preio', data: { from, output, index, fd: 2 } })
)
const pipe = sp.stdio[3]
this.once('end', () => {
pipe.end()
pipe.destroy()
try {
sp.kill()
} catch {
/* ignore if already closed */
}
})
const promise = new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
pipe.end()
pipe.destroy()
setTimeout(() => {
try {
sp.kill()
} catch {
/* ignore if already closed */
}
}, SELF_CLOSE_WAIT).unref()
reject(
new ERR_INVALID_CONFIG(
'pear.pre "' + link + '" did not respond with configuration data in time'
)
)
}, IDLE_TIMEOUT).unref()
const ondata = (data) => {
let output = null
try {
output = cenc.decode(cenc.any, data)
const success = output?.tag !== 'error'
this.push({ tag: 'pre', data: { from, output, index, success } })
} catch (err) {
reject(err)
pipe.end()
}
if (output.tag === 'configure') {
clearTimeout(timeout)
pipe.removeListener('data', ondata)
pipe.on('data', () => {
try {
output = cenc.decode(cenc.any, data)
const success = output?.tag !== 'error'
this.push({ tag: 'pre', data: { from, output, index, success } })
} catch (err) {
this.push({
tag: 'pre',
data: { from, output: err, index, success: false }
})
}
})
resolve(output.data)
}
}
pipe.on('data', ondata)
})
pipe.write(cenc.encode(cenc.any, options))
return promise
}
}