-
Notifications
You must be signed in to change notification settings - Fork 19
/
util.js
118 lines (110 loc) · 2.99 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
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
import path from 'path';
import fs from 'fs';
import url from 'url';
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 fetchFileFromId = async (id, importer, encoding = null) => {
id = id
.replace(/^\/@proxy\//, '')
.replace(/^(https?:\/(?!\/))/, '$1/');
if (/^https?:\/\//.test(id)) {
const u = url.parse(id, true);
u.query.noimport = 1 + '';
id = url.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 = process.cwd();
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) {
return new Function("return `"+templateString +"`;").call(templateVars);
};
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,
};
};