Skip to content

Commit 1e07b87

Browse files
Merge pull request mozilla#18933 from Snuffleupagus/base-factory-fetchData
Change the `BaseCMapReaderFactory` fetch-helper to return a `Uint8Array`
2 parents 54a77d2 + 6c3336f commit 1e07b87

File tree

4 files changed

+42
-47
lines changed

4 files changed

+42
-47
lines changed

src/display/base_factory.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,22 +127,27 @@ class BaseCMapReaderFactory {
127127
throw new Error("CMap name must be specified.");
128128
}
129129
const url = this.baseUrl + name + (this.isCompressed ? ".bcmap" : "");
130-
const compressionType = this.isCompressed
131-
? CMapCompressionType.BINARY
132-
: CMapCompressionType.NONE;
133130

134-
return this._fetchData(url, compressionType).catch(reason => {
135-
throw new Error(
136-
`Unable to load ${this.isCompressed ? "binary " : ""}CMap at: ${url}`
137-
);
138-
});
131+
return this._fetch(url)
132+
.then(cMapData => ({
133+
cMapData,
134+
compressionType: this.isCompressed
135+
? CMapCompressionType.BINARY
136+
: CMapCompressionType.NONE,
137+
}))
138+
.catch(reason => {
139+
throw new Error(
140+
`Unable to load ${this.isCompressed ? "binary " : ""}CMap at: ${url}`
141+
);
142+
});
139143
}
140144

141145
/**
142146
* @ignore
147+
* @returns {Promise<Uint8Array>}
143148
*/
144-
_fetchData(url, compressionType) {
145-
unreachable("Abstract method `_fetchData` called.");
149+
async _fetch(url) {
150+
unreachable("Abstract method `_fetch` called.");
146151
}
147152
}
148153

@@ -168,16 +173,17 @@ class BaseStandardFontDataFactory {
168173
}
169174
const url = `${this.baseUrl}${filename}`;
170175

171-
return this._fetchData(url).catch(reason => {
176+
return this._fetch(url).catch(reason => {
172177
throw new Error(`Unable to load font data at: ${url}`);
173178
});
174179
}
175180

176181
/**
177182
* @ignore
183+
* @returns {Promise<Uint8Array>}
178184
*/
179-
_fetchData(url) {
180-
unreachable("Abstract method `_fetchData` called.");
185+
async _fetch(url) {
186+
unreachable("Abstract method `_fetch` called.");
181187
}
182188
}
183189

src/display/display_utils.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -564,28 +564,24 @@ class DOMCMapReaderFactory extends BaseCMapReaderFactory {
564564
/**
565565
* @ignore
566566
*/
567-
_fetchData(url, compressionType) {
568-
return fetchData(
567+
async _fetch(url) {
568+
const data = await fetchData(
569569
url,
570570
/* type = */ this.isCompressed ? "arraybuffer" : "text"
571-
).then(data => ({
572-
cMapData:
573-
data instanceof ArrayBuffer
574-
? new Uint8Array(data)
575-
: stringToBytes(data),
576-
compressionType,
577-
}));
571+
);
572+
return data instanceof ArrayBuffer
573+
? new Uint8Array(data)
574+
: stringToBytes(data);
578575
}
579576
}
580577

581578
class DOMStandardFontDataFactory extends BaseStandardFontDataFactory {
582579
/**
583580
* @ignore
584581
*/
585-
_fetchData(url) {
586-
return fetchData(url, /* type = */ "arraybuffer").then(
587-
data => new Uint8Array(data)
588-
);
582+
async _fetch(url) {
583+
const data = await fetchData(url, /* type = */ "arraybuffer");
584+
return new Uint8Array(data);
589585
}
590586
}
591587

src/display/node_utils.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,11 @@ class NodePackages {
113113
}
114114
}
115115

116-
const fetchData = function (url) {
116+
async function fetchData(url) {
117117
const fs = NodePackages.get("fs");
118-
return fs.promises.readFile(url).then(data => new Uint8Array(data));
119-
};
118+
const data = await fs.promises.readFile(url);
119+
return new Uint8Array(data);
120+
}
120121

121122
class NodeFilterFactory extends BaseFilterFactory {}
122123

@@ -134,21 +135,22 @@ class NodeCMapReaderFactory extends BaseCMapReaderFactory {
134135
/**
135136
* @ignore
136137
*/
137-
_fetchData(url, compressionType) {
138-
return fetchData(url).then(data => ({ cMapData: data, compressionType }));
138+
async _fetch(url) {
139+
return fetchData(url);
139140
}
140141
}
141142

142143
class NodeStandardFontDataFactory extends BaseStandardFontDataFactory {
143144
/**
144145
* @ignore
145146
*/
146-
_fetchData(url) {
147+
async _fetch(url) {
147148
return fetchData(url);
148149
}
149150
}
150151

151152
export {
153+
fetchData,
152154
NodeCanvasFactory,
153155
NodeCMapReaderFactory,
154156
NodeFilterFactory,

test/unit/test_utils.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import { assert, isNodeJS } from "../../src/shared/util.js";
1717
import { NullStream, StringStream } from "../../src/core/stream.js";
1818
import { Page, PDFDocument } from "../../src/core/document.js";
19+
import { fetchData as fetchDataDOM } from "../../src/display/display_utils.js";
20+
import { fetchData as fetchDataNode } from "../../src/display/node_utils.js";
1921
import { Ref } from "../../src/core/primitives.js";
2022

2123
let fs, http;
@@ -33,27 +35,16 @@ const STANDARD_FONT_DATA_URL = isNodeJS
3335
? "./external/standard_fonts/"
3436
: "../../external/standard_fonts/";
3537

36-
class DOMFileReaderFactory {
38+
class DefaultFileReaderFactory {
3739
static async fetch(params) {
38-
const response = await fetch(params.path);
39-
if (!response.ok) {
40-
throw new Error(response.statusText);
40+
if (isNodeJS) {
41+
return fetchDataNode(params.path);
4142
}
42-
return new Uint8Array(await response.arrayBuffer());
43-
}
44-
}
45-
46-
class NodeFileReaderFactory {
47-
static async fetch(params) {
48-
const data = await fs.promises.readFile(params.path);
43+
const data = await fetchDataDOM(params.path, /* type = */ "arraybuffer");
4944
return new Uint8Array(data);
5045
}
5146
}
5247

53-
const DefaultFileReaderFactory = isNodeJS
54-
? NodeFileReaderFactory
55-
: DOMFileReaderFactory;
56-
5748
function buildGetDocumentParams(filename, options) {
5849
const params = Object.create(null);
5950
params.url = isNodeJS

0 commit comments

Comments
 (0)