Repository navigation
Expand file tree
/
Copy pathutil.js
More file actions
146 lines (136 loc) · 3.88 KB
/
Copy pathutil.js
File metadata and controls
146 lines (136 loc) · 3.88 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
import path from 'path';
import fs from 'fs';
import url, {URL, format} from 'url';
// import fetch from 'node-fetch';
export function jsonParse(s) {
try {
return JSON.parse(s);
} catch (err) {
return null;
}
}
export const resolveFileFromId = (id, importer) => {
id = id.replace(/^[\/\\]+/, '');
let match;
// console.log('load', id, match);
if (match = id.match(/^ipfs:\/+([a-z0-9]+)((?:\/?[^\/\?]*)*)(\?\.(.+))?$/i)) {
return `https://ipfs.webaverse.com/ipfs/${match[1]}${match[2]}`;
/* } else if (match = id.match(/^\/@proxy\/(.+)$/)) {
return match[1]; */
} else {
return null;
}
};
export const getCwd = () => path.resolve(process.cwd(), process.env.BASE_CWD ?? '');
export const readFile = async id => {
if (/^https?:\/\//.test(id)) {
const res = await fetch(id)
const text = await res.text();
return text;
} else {
// read from disk
const rs = fs.createReadStream(id);
const chunks = [];
for await (const chunk of rs) {
chunks.push(chunk);
}
return Buffer.concat(chunks).toString('utf8');
}
};
export const fetchFileFromId = async (id, importer, encoding = null) => {
// .replace(/^\/@proxy\//, '')
// .replace(/^(https?:\/(?!\/))/, '$1/');
if (/^https?:\/\//.test(id)) {
const u = url.parse(id, true);
u.query.noimport = 1 + '';
id = format(u);
const res = await fetch(id)
if (encoding === 'utf8') {
const s = await res.text();
return s;
} else {
const arrayBuffer = await res.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
return buffer;
}
} else {
return await new Promise((accept, reject) => {
const cwd = getCwd();
const p = path.join(cwd, id.replace(/^[\/\\]+/, ''));
// console.log('read dir', {id, importer, p});
fs.readFile(p, encoding, (err, d) => {
if (!err) {
accept(d);
} else {
if (err.code === 'ENOENT') {
accept(null);
} else {
reject(err);
}
}
});
});
}
};
export const fillTemplate = function(templateString, templateVars) {
// eslint-disable-next-line no-new-func
// return new Function("return `"+templateString +"`;").call(templateVars);
for (const k in templateVars) {
const v = templateVars[k];
console.log('replace', k, v);
if (k === undefined || v === undefined) {
throw new Error(`undefined template var: ${k}`);
}
templateString = templateString.replace(new RegExp(`\\$\\{this\\.${k}\\}`, 'g'), v);
}
return templateString;
};
export const createRelativeFromAbsolutePath = path => {
/* const cwd = process.cwd();
if (path.startsWith(cwd.replaceAll('\\','/'))) {
path = path.slice(cwd.length);
} */
return path;
}
export const parseIdHash = id => {
let contentId = '';
let name = '';
let description = '';
let components = [];
const match = id.match(/#([\s\S]+)$/);
if (match) {
const q = new URLSearchParams(match[1]);
const qContentId = q.get('contentId');
if (qContentId !== undefined) {
contentId = qContentId;
}
const qName = q.get('name');
if (qName !== undefined) {
name = qName;
}
const qDescription = q.get('description');
if (qDescription !== undefined) {
description = qDescription;
}
const qComponents = q.get('components');
if (qComponents !== undefined) {
components = jsonParse(qComponents) ?? [];
}
}
if (!contentId) {
contentId = id.match(/^([^#]*)/)[1];
}
if (!name) {
if (/^data:/.test(contentId)) {
name = contentId.match(/^data:([^\;\,]*)/)[1];
} else {
name = contentId.match(/([^\/\.]*)(?:\.[a-zA-Z0-9]*)?$/)[1];
}
}
return {
contentId,
name,
description,
components,
};
};