-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.ts
196 lines (173 loc) · 7.2 KB
/
server.ts
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
import HARRemix from "har-remix";
import * as HAR from "@tracerbench/har";
import * as url from "url";
import * as http from "http";
import * as fs from "fs";
import * as path from "path";
import { walk } from "estree-walker";
import { parse } from "acorn";
import * as estree from "estree";
declare const gc: {
(full: boolean): void;
};
const DIST_REGEX = /^GET\/(.+)(\.js)$/;
const KEY_FINGERPRINT = /-[0-9a-fA-Z]{31,32}\./;
const config = JSON.parse(fs.readFileSync("config.json", "utf8"));
config.servers.forEach(server => {
startServer(server.name, config.har, server.port, (key, text) => {
if (!key.startsWith("GET/")) {
return;
}
if (server.dist) {
let matches = key.match(DIST_REGEX);
if (!matches) {
return text;
}
let asset = matches[1] + matches[2];
let localPath = path.join(server.dist, asset);
console.log("");
if (fs.existsSync(localPath)) {
console.log("Looked up on disk: ", localPath);
return fs.readFileSync(localPath, { encoding: "utf-8" });
} else {
console.log("Failed to load: ", localPath);
console.log("Falling through");
}
} else if (server.ember) {
if (/GET\/assets\/vendor(?:-[0-9a-fA-Z]{31,32})?\.js/.test(key)) {
return replaceEmber(text, server.ember);
}
}
return text;
});
});
function replaceProtocolAndDomain(text: string, host: string) {
return text
.replace(/https:\/\//g, "http://")
.replace(
/[a-z\.\-]+\.(?:emberaddons|amazonaws|gravatar|google-analytics|googleapis|trackjs)\.com\b/g,
host
);
}
function key(method: string, url: url.Url) {
// ignore query on root
if (url.pathname === "/") {
return method + url.pathname;
}
// allow loose GET match on fingerprint
if (method === "GET") {
return "GET" + url.path.replace(KEY_FINGERPRINT, ".");
}
return method + url.path;
}
function startServer(
name: string,
archivePath: string,
port: number,
vary?: (key: string, text: string) => string
) {
let host = `localhost:${port}`;
function keyForArchiveEntry(entry: HAR.Entry) {
let { request, response } = entry;
let { status } = response;
if (status >= 200 && status < 300 && request.method !== "OPTIONS") {
return key(request.method, url.parse(request.url));
}
}
function keyForServerRequest(
request: http.IncomingMessage
): string | undefined {
return key(request.method, url.parse(request.url));
}
function textFor(entry: HAR.Entry, key: string, text: string): string {
if (entry.request.method !== "GET") return text;
if (key === "GET/analytics.js" || key === "GET/ember-cli-live-reload.js") {
return "";
}
if (key === "GET/") {
text = text.replace(/integrity="[^"]+"/g, "");
text = text.replace(/\/assets\/favicon-\w+\.png/, "/assets/favicon.png");
text = text.replace(
"https%3A//io-builtwithember-addons-data.s3.amazonaws.com",
"http%3A//" + host
);
return replaceProtocolAndDomain(text, host);
}
if (vary) {
text = vary(key, text);
}
return replaceProtocolAndDomain(text, host);
}
let harRemix = new HARRemix({
keyForArchiveEntry,
keyForServerRequest,
textFor
});
harRemix.loadArchive(archivePath);
let favicon = Buffer.from(
"iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAGl0lEQVR4AX2W22+lVfnHv8/zrHd3t9PpYfobpMA0ZVBoEOHHKTOAEyBB4iHBC7kwMZ4Sb73Rv8A7b7zySrnXGAOJJiqGKCgHR0chBIgOBzsyHTpTZqYt7e7e+33X83x9V9pSCoOfi7WTnazPp+86vLvyzre+4B6qEChaGM5IlrJnU3WnqqgaSZDZ3cwiaCZsCaopCpI9V6miCEQE9KZRMIZDFSCZhCMiGJE9TLUVJTVAz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DmraGeneejfJIhLB+6jq1KH3v6nPLg5OPjP46zPN8lKxexYIVRD798DMVCWc4YEgFIWqI7gC23ZfX+298IfmpZPN2UVfWwUCAjWVmZno97m5jpz3AiLSrgmArT//Pi+/nQ7PyvRMmpq2/7saH4KMjfV29Yb/fHl4+lVfOhPDLbJU9eDUyCcX7PA1g1dONf0e9wVAkPToP/fU8PQr1ZH5anYuzV5bzR1N18zpxPTeKjHivbWtp3+z9eJJIQXQqpKR0fYP6szf2D1+QkcP5uWzeXmJ2CNJzpnQlAD4pQt+8Xz98ikBCEx+53tjJx7eu2gQqOUL56upQ3RHMp08lOaOjh1/sHv7cXrO55es3aT9pCY3ChFBi1iianY3IIDmlVO8+8ReQDVdc+TwD35cL50RUiembGpaxsZ34qKinQDZ1Ps3Wa01tla0REClSp3Gy+Hq/+3Z7oNfGlm4Tbqj2EUOjI/cdAs+AiP75QvNv1//UKA1ZxPlNhHiZNOYSlslsPbzx9o5KHP+J2RsbvZPPduceQP7se8vHFGBiIoISIIChnuVqiClt9mcOyNmafaIpOpj7asXN379s43f/pL1EPtJjKAiAoUSEfdQBXMDtVzXXHxjc/D48M3To7cfS0fm0yeuBcDBIF9aaX8MfGW5Xjzd3uf63OI++757QEKk+HdHIhgEG7EUw0Fz7u12BbD6bvt+xsSkikbr6m34xnp70dqDF5ffjaYBeaWAKknBHiVAIchwE6OA9bDdwP7lFYKMENWIMDUAjIBpUTNwJVQALcYgCCBAgZQIUBQMkgAEksNFNACBJDURtAQID6+HdL9yAED2MlMAMky0JKGtjkR4IALunptkJmA7Oj2AKO2d/50EH0tqcrbihIgCbE0qEmRlqXE3hSIAZbEOLVUOmhrDAWmBiKnkTHwM/wUr4jS84/B+CgAAAABJRU5ErkJggg==",
"base64"
);
harRemix.setResponse(
"GET/assets/favicon.png",
harRemix.buildResponse(200, "image/png", favicon, false)
);
console.log(`starting ${name}`);
let server = harRemix.createServer();
server.on("listening", () => {
if (typeof gc !== "undefined") {
console.log("GC");
gc(true);
}
console.log(`${name} started at http://localhost:${port}/`);
});
server.on("error", e => console.error(e));
server.listen(port);
}
function replaceEmber(code: string, emberFile: string): string {
let topiife = null;
let ember = null;
let program = parse(code);
walk(program, {
enter(node, parent, key) {
// console.log(stack.length, node.type);
if (topiife === null) {
if (
node.type === "FunctionExpression" &&
parent.type === "CallExpression" &&
key === "callee"
) {
topiife = parent;
}
} else {
if (
isIdentifier(node) &&
node.name === "Ember" &&
isMemberExpression(parent) &&
key === "property" &&
parent.object.type === "ThisExpression"
) {
ember = topiife;
}
}
},
leave(node, _parent, _key) {
if (node === topiife) {
topiife = null;
}
}
});
return (
code.substring(0, ember.start) +
fs.readFileSync(emberFile, "utf8") +
code.substring(ember.end)
);
}
function isMemberExpression(
node: estree.BaseNode
): node is estree.MemberExpression {
return node.type === "MemberExpression";
}
function isIdentifier(node: estree.BaseNode): node is estree.Identifier {
return node.type === "Identifier";
}