-
Notifications
You must be signed in to change notification settings - Fork 48
/
RequestManager.ts
210 lines (190 loc) · 5.79 KB
/
RequestManager.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { patch } from "../patch";
type ResponseList = { mediaType: string; href: string; source: string }[];
export class RequestManager {
blobCache: Map<string, string>;
responses: ResponseList;
private constructor(
responses: {
mediaType: string;
href: string;
source: string;
}[]
) {
this.responses = responses;
this.blobCache = new Map();
}
public static interceptRequests(responses: ResponseList) {
const requestManager = new RequestManager(responses);
requestManager.interceptRequests();
return requestManager;
}
private resolveResourceUrl(url: string) {
// Serve local resources from the app
if (url.startsWith("app://")) {
return url;
}
// Allow data urls
if (url.startsWith("data:")) {
return url;
}
// TODO: catch http requests so we can work offline
// Allow fully qualified online resources
if (/^(https?:|\/\/)/.test(url)) {
return url;
}
// Seems to be an IE support thing
if (url === "#default#VML") {
return url;
}
// Use cached results
if (this.blobCache.has(url)) {
return this.blobCache.get(url);
}
const file = this.responses.find((file) => file.href === url);
if (typeof file === "undefined") {
console.warn("Missing local resource", "https://app.diagrams.net/" + url);
// TODO: catch http requests so we can work offline
// Allow fully qualified online resources
return "https://app.diagrams.net/" + url;
} else {
// console.info("Loading local resource", url);
}
const mediaType = file.mediaType;
const source = file.source;
const isBase64 = mediaType.endsWith(";base64");
let blobUrl;
if (isBase64 && source.length < 1024) {
blobUrl = "data:" + mediaType + "," + source;
} else {
const blob = new Blob([isBase64 ? atob(source) : source], {
type: mediaType,
});
blobUrl = URL.createObjectURL(blob);
}
// Add result to cache
this.blobCache.set(url, blobUrl);
return blobUrl;
}
private interceptStylesheets() {
const resolveResourceUrl = this.resolveResourceUrl.bind(this);
patch(
HTMLLinkElement.prototype,
"setAttribute",
(fn) =>
function (qualifiedName: string, value: string) {
if (qualifiedName === "href") {
value = resolveResourceUrl(value);
}
return fn.call(this, qualifiedName, value);
}
);
}
private interceptScripts() {
const resolveResourceUrl = this.resolveResourceUrl.bind(this);
patch(
HTMLScriptElement.prototype,
"setAttribute",
(fn) =>
function (qualifiedName: string, value: string) {
if (qualifiedName === "src") {
value = resolveResourceUrl(value);
}
return fn.call(this, qualifiedName, value);
}
);
Object.defineProperty(HTMLScriptElement.prototype, "src", {
set(value) {
this.setAttribute("src", value);
},
});
}
private interceptImages() {
const resolveResourceUrl = this.resolveResourceUrl.bind(this);
patch(
HTMLImageElement.prototype,
"setAttribute",
(fn) =>
function (qualifiedName: string, value: string) {
if (qualifiedName === "src") {
value = resolveResourceUrl(value);
}
return fn.call(this, qualifiedName, value);
}
);
Object.defineProperty(HTMLImageElement.prototype, "src", {
set(value) {
this.setAttribute("src", value);
},
});
}
private interceptCss() {
const resolveResourceUrl = this.resolveResourceUrl.bind(this);
const htmlElementStylePropertyDescriptor = Object.getOwnPropertyDescriptor(
HTMLElement.prototype,
"style"
);
const urlPropertyPattern = /(url\([\'\"]?)(.*)([\'\"]?\))/g;
Object.defineProperty(HTMLElement.prototype, "style", {
get() {
// Get the real CSS style declaration
const cssStyleDeclaration =
htmlElementStylePropertyDescriptor.get.call(this);
return new Proxy(cssStyleDeclaration, {
set(target, propertyKey, value, receiver) {
if (typeof value === "string") {
if (urlPropertyPattern.test(value)) {
value = value.replace(
urlPropertyPattern,
(_, a, b, c) => `${a}${resolveResourceUrl(b)}${c}`
);
}
try {
return Reflect.set(target, propertyKey, value);
} catch (err) {
console.warn(err);
}
} else {
return Reflect.set(target, propertyKey, value, receiver);
}
},
});
},
});
}
private interceptXhrRequests() {
const resolveResourceUrl = this.resolveResourceUrl.bind(this);
patch(
XMLHttpRequest.prototype,
"open",
(fn) =>
function (_method: string, url: string, ...args: any[]) {
url = resolveResourceUrl(url);
console.warn("XHR", _method, url);
return fn.call(this, _method, url, ...args);
}
);
}
public interceptRequests() {
this.interceptStylesheets();
this.interceptScripts();
this.interceptImages();
this.interceptCss();
this.interceptXhrRequests();
}
}
export function loadScript(src: string, scriptLoadCallback?: () => void) {
const script = document.createElement("script");
if (scriptLoadCallback) {
script.addEventListener("load", () => {
scriptLoadCallback();
});
}
script.setAttribute("src", src);
document.head.appendChild(script);
}
export function loadStylesheet(href: string) {
const link = document.createElement("link");
link.setAttribute("rel", "stylesheet");
link.setAttribute("href", href);
document.head.appendChild(link);
}