-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.js
227 lines (205 loc) · 6.16 KB
/
debug.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
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
const fs = require("fs");
const http = require("http");
const URL = require("url");
const chalk = require("chalk");
const ep = "grimoire-e2e-worker.herokuapp.com";
function exists(path) {
return new Promise((resolve, reject) => {
fs.stat(path, (err, stat) => {
resolve(!err);
});
});
}
function getUserHome() {
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}
function read(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, "utf-8", (err, content) => {
if (err) {
reject(err);
} else {
resolve(content);
}
});
});
}
async function readJSON(path) {
return JSON.parse(await read(path));
}
function write(path, content) {
return new Promise((resolve, reject) => {
fs.writeFile(path, content, { encoding: "utf-8" }, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
function writeJSON(path, content) {
return write(path, JSON.stringify(content, null, 2));
}
function toBase64(str) {
const b = new Buffer(str);
return b.toString('base64');
}
function request(method, host, path, data, contentType = "application/json") {
return new Promise((resolve, reject) => {
let result = "";
const req = http.request({
host: host,
path: path,
port: 80,
method: method,
headers: {
'Content-Type': contentType,
'Content-Length': Buffer.byteLength(data),
'Cache-Control': 'no-cache,no-store'
}
}, (res) => {
res.setEncoding("utf-8");
res.on("close", () => {
resolve();
});
res.on("error", (err) => {
reject(err);
});
res.on("data", (data) => {
result += data;
});
}).on("close", () => {
resolve(result);
});
req.write(data);
req.end();
});
}
function btoa(str) {
var buffer;
if (Buffer.isBuffer(str)) {
buffer = str;
}
else {
buffer = new Buffer(str.toString(), 'binary');
}
return buffer.toString('base64');
};
function deflate(val) {
val = encodeURIComponent(val); // UTF16 → UTF8
val = btoa(val); // base64エンコード
return val;
}
async function uploadAndGetShortName(id, expire, cachedSignedAddr) {
const pkgJson = await read("./package.json");
const pkgJsonObj = JSON.parse(pkgJson);
let name = pkgJsonObj.name;
let shortName;
if (name === "grimoirejs") {
name = "grimoire";
shortName = "grimoirejs";
} else {
const regexedName = /grimoirejs-(.+)/.exec(name);
name = "grimoire-" + regexedName[1];
shortName = regexedName[1];
}
const files = await Promise.all([read(`./register/${name}.js`), read(`./register/${name}.js.map`)]);
const ct = ["text/javascript", "text/plain"];
let addr;
if (expire <= Date.now()) {
addr = JSON.parse(await request("GET", ep, `/debugAddr/${id}/${name}`, ""));
const cfg = await readJSON("./common/.grimoire");
cfg.cachedSignedAddr = addr;
cfg.expire = Date.now() + 1000 * 60 * 60 * 24 * 6;
await writeJSON("./common/.grimoire", cfg);
console.log("SignedURL updated");
} else {
addr = cachedSignedAddr;
}
const uploads = [addr["js"], addr["map"]];
await Promise.all(uploads.map(u => URL.parse(u)).map((p, i) => request("PUT", p.host, p.path, files[i], ct[i])));
return shortName;
}
async function configure() {
if (!await exists("./common/.grimoire")) {
const id = Math.random().toString(36).slice(-8)
await writeJSON("./common/.grimoire", {
id,
arg: [],
expire: 0,
configAddr: {
}
});
}
return await readJSON("./common/.grimoire");
}
async function getUserConfigure() {
const home = getUserHome();
const configFile = `${home}/.grimoire`
if (!await exists(configFile)) {
await writeJSON(configFile, {
overrideVersions: {
}
});
}
return await readJSON(configFile);
}
function parameterObjectToParameterString(obj) {
let result = "";
let isFirst = true;
for (let key in obj) {
if (!isFirst) {
result += "&";
}
result += `${key}=${obj[key]}`;
isFirst = false;
}
return result;
}
function logVersions(obj, io) {
const black = '\u001b[30m';
const red = '\u001b[31m';
const green = '\u001b[32m';
const yellow = '\u001b[33m';
const blue = '\u001b[34m';
const magenta = '\u001b[35m';
const cyan = '\u001b[36m';
const white = '\u001b[37m';
const reset = '\u001b[0m';
for (let key in io) {
if (obj[key]) {
console.log(`・ ${red}${key} -> ${io[key]} (Overriden by local config)${reset}`);
} else {
console.log(`・ ${blue}${key}${reset} -> ${yellow}${io[key]}${blue} (From global config)${reset}`);
}
}
for (let key in obj) {
console.log(`・ ${green}${key}${reset} -> ${yellow}${obj[key]}${reset}`);
}
console.log("\n");
}
async function main() {
const { id, arg, expire, cachedSignedAddr, configAddr } = await configure();
const uc = await getUserConfigure();
const shortName = await uploadAndGetShortName(id, expire, cachedSignedAddr);
let paramObj = {
[shortName]: `staging-${id}`,
...configAddr,
}
logVersions(paramObj, uc.overrideVersions);
paramObj = {
...uc.overrideVersions,
...paramObj
}
const updateGlobal = process.argv.includes("-ug");
if (updateGlobal) {
const home = getUserHome();
uc.overrideVersions[shortName] = paramObj[shortName];
const configFile = `${home}/.grimoire`
await write(configFile, JSON.stringify(uc, null, 2))
console.log("Global config updated");
}
console.log(`http://${ep}/?arg=${toBase64(parameterObjectToParameterString(paramObj))}`);
}
main();