Skip to content

Commit b6248a6

Browse files
authored
Use fetch in URLResolver, remove URLBrowserResolver (#2873)
1 parent 4395ebd commit b6248a6

File tree

3 files changed

+13
-132
lines changed

3 files changed

+13
-132
lines changed

src/URLResolver.js

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,15 @@
1-
import http from 'http';
2-
import https from 'https';
3-
4-
const MAX_REDIRECTS = 30;
5-
6-
const fetchUrl = (url, headers = {}, redirectCount = 0) => {
7-
if (redirectCount >= MAX_REDIRECTS) {
8-
return new Promise((_, reject) => {
9-
reject(new Error(`Too many redirects (limit: ${MAX_REDIRECTS})`));
10-
});
11-
}
12-
return new Promise((resolve, reject) => {
13-
const parsedUrl = new URL(url);
14-
const h = (parsedUrl.protocol === 'https:') ? https : http;
15-
let options = {
16-
headers: headers
17-
};
18-
19-
h.get(url, options, res => {
20-
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) { // redirect url
21-
res.resume();
22-
23-
fetchUrl(res.headers.location, {}, redirectCount + 1).then(buffer => {
24-
resolve(buffer);
25-
}, result => {
26-
reject(result);
27-
});
28-
return;
29-
}
30-
31-
const ok = res.statusCode >= 200 && res.statusCode < 300;
32-
if (!ok) {
33-
reject(new TypeError(`Failed to fetch (status code: ${res.statusCode}, url: "${url}")`));
34-
res.resume();
35-
return;
1+
const fetchUrl = (url, headers = {}) => {
2+
return fetch(url, { headers }).then(
3+
response => {
4+
if (!response.ok) {
5+
throw new TypeError(`Failed to fetch (status code: ${response.status}, url: "${url}")`);
366
}
37-
38-
const chunks = [];
39-
res.on('end', () => resolve(Buffer.concat(chunks)));
40-
res.on('data', d => chunks.push(d));
41-
}).on('error', reject);
42-
});
7+
return response.arrayBuffer();
8+
},
9+
() => {
10+
throw new TypeError(`Network request failed (url: "${url}")`);
11+
}
12+
);
4313
};
4414

4515
class URLResolver {

src/browser-extensions/URLBrowserResolver.js

Lines changed: 0 additions & 89 deletions
This file was deleted.

src/browser-extensions/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pdfmakeBase from '../base';
22
import OutputDocumentBrowser from './OutputDocumentBrowser';
3-
import URLBrowserResolver from './URLBrowserResolver';
3+
import URLResolver from '../URLResolver';
44
import fs from 'fs';
55
import configurator from 'core-js/configurator';
66

@@ -21,7 +21,7 @@ let defaultClientFonts = {
2121
class pdfmake extends pdfmakeBase {
2222
constructor() {
2323
super();
24-
this.urlResolver = () => new URLBrowserResolver(this.virtualfs);
24+
this.urlResolver = () => new URLResolver(this.virtualfs);
2525
this.fonts = defaultClientFonts;
2626
}
2727

0 commit comments

Comments
 (0)