Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit a7c0f17

Browse files
committed
fix export queue
1 parent ce50c2f commit a7c0f17

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/api/export.ts

+21-17
Original file line numberDiff line numberDiff line change
@@ -132,26 +132,32 @@ async function _export({
132132
if (seen.has(pathname)) return;
133133
seen.add(pathname);
134134

135-
const timeout_deferred = new Deferred();
136-
const the_timeout = setTimeout(() => {
137-
timeout_deferred.reject(new Error(`Timed out waiting for ${url.href}`));
138-
}, timeout);
135+
const r = await q.add(async () => {
136+
const timeout_deferred = new Deferred();
137+
const the_timeout = setTimeout(() => {
138+
timeout_deferred.reject(new Error(`Timed out waiting for ${url.href}`));
139+
}, timeout);
139140

140-
const r = await Promise.race([
141-
q.add(() => fetch(url.href, {
142-
redirect: 'manual'
143-
})),
144-
timeout_deferred.promise
145-
]);
141+
const r = await Promise.race([
142+
fetch(url.href, {
143+
redirect: 'manual'
144+
}),
145+
timeout_deferred.promise
146+
]);
146147

147-
clearTimeout(the_timeout); // prevent it hanging at the end
148+
clearTimeout(the_timeout); // prevent it hanging at the end
149+
150+
return r;
151+
}) as Response;
148152

149153
let type = r.headers.get('Content-Type');
150154

151155
let body = await r.text();
152156

153157
const range = ~~(r.status / 100);
154158

159+
let last_task;
160+
155161
if (range === 2) {
156162
if (type === 'text/html') {
157163
// parse link rel=preload headers and embed them in the HTML
@@ -173,8 +179,6 @@ async function _export({
173179
let match;
174180
let pattern = /<a ([\s\S]+?)>/gm;
175181

176-
let promise;
177-
178182
while (match = pattern.exec(cleaned)) {
179183
const attrs = match[1];
180184
const href = get_href(attrs);
@@ -183,12 +187,10 @@ async function _export({
183187
const url = resolve(base.href, href);
184188

185189
if (url.protocol === protocol && url.host === host) {
186-
promise = handle(url);
190+
last_task = handle(url);
187191
}
188192
}
189193
}
190-
191-
await promise;
192194
}
193195
}
194196
}
@@ -199,10 +201,12 @@ async function _export({
199201
type = 'text/html';
200202
body = `<script>window.location.href = "${location.replace(origin, '')}"</script>`;
201203

202-
await handle(resolve(root.href, location));
204+
last_task = handle(resolve(root.href, location));
203205
}
204206

205207
save(pathname, r.status, type, body);
208+
209+
await last_task;
206210
}
207211

208212
try {

0 commit comments

Comments
 (0)