-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathio_deno.mjs
303 lines (242 loc) · 6.79 KB
/
io_deno.mjs
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* global Deno */
import * as l from './lang.mjs'
import * as p from './path.mjs'
import * as cl from './cli.mjs'
export const IS_WINDOWS = l.reqStr(Deno.build.os) === `windows`
export const SEP = IS_WINDOWS ? p.SEP_WINDOWS : p.SEP_POSIX
export const paths = IS_WINDOWS ? p.windows : p.posix
export function isErrNotFound(val) {return l.isInst(val, Deno.errors.NotFound)}
// Defined for symmetry with `readTextOpt`.
export function readText(path) {return Deno.readTextFile(path)}
export async function readTextOpt(path) {
if (l.isNil(path)) return ``
try {
return await readText(path)
}
catch (err) {
if (isErrNotFound(err)) return ``
throw err
}
}
// Defined for symmetry with `readTextSyncOpt`.
export function readTextSync(path) {return Deno.readTextFileSync(path)}
export function readTextSyncOpt(path) {
if (l.isNil(path)) return ``
try {
return Deno.readTextFileSync(path)
}
catch (err) {
if (isErrNotFound(err)) return ``
throw err
}
}
export async function readTextCreate(path) {
try {
return await readText(path)
}
catch (err) {
if (isErrNotFound(err)) {
await create(path)
return ``
}
throw err
}
}
export async function readJson(path) {
return JSON.parse(await readText(path))
}
export function writeText(path, val, opt) {
return Deno.writeTextFile(p.reqPath(path), l.reqStr(val), opt)
}
export function create(path) {return Deno.create(p.reqPath(path))}
export async function touch(path) {
const info = await FileInfo.statOpt(path)
if (!info) {
await create(path)
return
}
if (!info.isFile()) throw Error(`${l.show(path)} is not a file`)
}
export function cwd() {return Deno.cwd()}
export function watchCwd() {return watchRel(Deno.cwd())}
/*
Needs a better name. Watches a path, converting all paths in each `Deno.FsEvent`
from absolute to relative.
*/
export async function* watchRel(base) {
l.req(base, paths.isAbs.bind(paths))
const toRel = path => paths.strictRelTo(path, base)
for await (const event of Deno.watchFs(base, {recursive: true})) {
event.paths = event.paths.map(toRel)
yield event
}
}
export async function* filterWatch(iter, fun) {
l.reqFun(fun)
for await (const event of iter) {
if (event.paths.some(fun)) yield event
}
}
export class FileInfo extends l.Emp {
constructor(stat, path) {
super()
this.path = p.reqPath(path)
this.stat = l.reqStruct(stat)
}
isFile() {return this.stat.isFile}
isDir() {return this.stat.isDirectory}
onlyFile() {return this.isFile() ? this : undefined}
onlyDir() {return this.isDir() ? this : undefined}
static async stat(path) {
return new this(await Deno.stat(p.reqPath(path)), path)
}
static async statOpt(path) {
if (l.isNil(path)) return undefined
try {
return await this.stat(path)
}
catch (err) {
if (isErrNotFound(err)) return undefined
throw err
}
}
}
export function isReader(val) {return l.hasMeth(val, `read`)}
export function reqReader(val) {return l.req(val, isReader)}
export function isCloser(val) {return l.hasMeth(val, `close`)}
export function reqCloser(val) {return l.req(val, isCloser)}
function optClose(val) {if (isCloser(val)) val.close()}
// Adapter between `Deno.Reader` and standard `ReadableStream`.
export class ReaderStreamSource extends l.Emp {
constructor(src) {
super()
this.src = reqReader(src)
this.closed = false
}
// Called by `ReadableStream` when data is requested.
async pull(ctr) {
const buf = new Uint8Array(this.size())
try {
const len = await this.src.read(buf)
if (l.isNil(len)) {
this.deinit()
ctr.close()
}
else {
ctr.enqueue(buf.subarray(0, len))
}
}
catch (err) {
this.deinit()
ctr.error(err)
}
}
// Called by `ReadableStream` when canceled.
cancel() {this.deinit()}
// Just for completeness.
close() {this.deinit()}
deinit() {
if (!this.closed) {
this.closed = true
optClose(this.src)
}
}
static async open(path) {return new this(await Deno.open(path))}
size() {return 4096 * 16}
}
export class FileStream extends ReadableStream {
constructor(src, path) {
super(src)
this.src = l.reqInst(src, ReaderStreamSource)
this.path = p.optPath(path)
}
cancel() {return this.deinit(), super.cancel()}
close() {this.deinit()}
deinit() {this.src.deinit()}
static async open(path) {
return new this(await this.Source.open(path), path)
}
static get Source() {return ReaderStreamSource}
}
/*
Concatenates inputs into one stream. Supported inputs:
* Nils.
* `ReadableStream` instances.
* `Uint8Array` instances.
* String values.
* Stringable primitives such as numbers.
* Stringable objects such as `URL` instances.
Other inputs are rejected with an exception.
This class uses only standard web APIs, nothing Deno-related. If we end up
writing other stream tools, we'll move this to a dedicated module.
*/
export class ConcatStreamSource extends l.Emp {
constructor(...src) {
super()
this.src = src.map(this.usable, this).filter(l.id)
}
async pull(ctr) {
try {await this.pullUnchecked(ctr)}
catch (err) {
this.deinit(err)
ctr.error(err)
}
}
async pullUnchecked(ctr) {
while (this.src.length) {
const src = this.src[0]
if (l.isNil(src)) {
this.src.shift()
continue
}
if (l.isInst(src, Uint8Array)) {
this.src.shift()
ctr.enqueue(src)
return
}
const {done, value} = await src.read()
if (done) {
this.src.shift()
continue
}
ctr.enqueue(value)
return
}
ctr.close()
}
cancel(why) {return this.deinit(why)}
deinit(why) {
for (const val of this.src) deinit(val, why)
this.src.length = 0
}
usable(val) {
if (l.isNil(val)) return val
if (l.isInst(val, ReadableStream)) return val.getReader()
if (l.isInst(val, Uint8Array)) return val
return new TextEncoder().encode(l.renderLax(val))
}
static stream(...val) {return new this.Stream(new this(...val))}
static get Stream() {return ReadableStream}
}
// Readers implement only `.cancel`. However, as a policy, we always support
// `.deinit` which is our "standard" deinitialization interface.
function deinit(val, why) {
if (l.hasMeth(val, `cancel`)) val.cancel(why)
else if (l.hasMeth(val, `deinit`)) val.deinit(why)
}
export class EnvMap extends cl.EnvMap {
mutFromFile(path) {return this.mut(readTextSync(path))}
mutFromFileOpt(path) {return this.mut(readTextSyncOpt(path))}
deleteInstalled() {
for (const key of this.keys()) {
if (l.isSome(Deno.env.get(key))) this.delete(key)
}
return this
}
install() {
for (const [key, val] of this.entries()) {
Deno.env.set(l.reqStr(key), l.render(val))
}
return this
}
}