-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutil.js
73 lines (65 loc) · 2.12 KB
/
util.js
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
import fs from 'fs'
import path from 'path'
export const fixturesDir = new URL('../fixtures/', import.meta.url)
export const negativeFixturesDir = new URL('../negative-fixtures/', import.meta.url)
export async function loadFixture (dir) {
const data = {}
for (const file of await fs.promises.readdir(dir)) {
const ext = path.extname(file).slice(1)
const cid = file.substring(0, file.length - ext.length - 1)
const bytes = await fs.promises.readFile(new URL(file, dir))
data[ext] = { cid, bytes }
}
return data
}
function * iterate (type, ...dirs) {
let part = `.`
if (dirs.length > 1) {
part = `${part}/${dirs.slice(1).join('/')}/`
}
const base = new URL(part, dirs[0])
try {
fs.statSync(base)
} catch (e) {
if (e.code == 'ENOENT') { // ignore missing
return
}
}
for (const name of fs.readdirSync(base)) {
// if name is 'string-𐅑' and OS is windows and Node.js version is 20.x then
// skip, see https://github.com/nodejs/node/issues/48673 which may be fixed
// at some point by a backport to LTS, in which case this skip can be undone
if (name === 'string-𐅑' && process.platform === 'win32' && process.version.startsWith('v20.')) {
continue
}
let url = new URL(`./${name}`, base)
const stat = fs.statSync(url)
if ((type === 'dir' && !stat.isDirectory()) || (type === 'file' && stat.isDirectory())) {
continue
}
if (type === 'dir') {
url = new URL(`./${name}/`, base)
}
yield { name, url }
}
}
export function * fixtureDirectories () {
yield * iterate('dir', fixturesDir)
}
export function * negativeFixtureCodecs () {
for (const { name } of iterate('dir', negativeFixturesDir)) {
yield name
}
}
export function * negativeFixtures (type, codec) {
for (const { name, url } of iterate('file', negativeFixturesDir, codec, type)) {
const fixtureText = fs.readFileSync(url, 'utf8')
yield JSON.parse(fixtureText)
}
}
export function * negativeFixturesEncode (codec) {
yield * negativeFixtures('encode', codec)
}
export function * negativeFixturesDecode (codec) {
yield * negativeFixtures('decode', codec)
}