Skip to content

Commit

Permalink
Merge pull request #18933 from Snuffleupagus/base-factory-fetchData
Browse files Browse the repository at this point in the history
Change the `BaseCMapReaderFactory` fetch-helper to return a `Uint8Array`
  • Loading branch information
timvandermeij authored Oct 22, 2024
2 parents 54a77d2 + 6c3336f commit 1e07b87
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 47 deletions.
32 changes: 19 additions & 13 deletions src/display/base_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,27 @@ class BaseCMapReaderFactory {
throw new Error("CMap name must be specified.");
}
const url = this.baseUrl + name + (this.isCompressed ? ".bcmap" : "");
const compressionType = this.isCompressed
? CMapCompressionType.BINARY
: CMapCompressionType.NONE;

return this._fetchData(url, compressionType).catch(reason => {
throw new Error(
`Unable to load ${this.isCompressed ? "binary " : ""}CMap at: ${url}`
);
});
return this._fetch(url)
.then(cMapData => ({
cMapData,
compressionType: this.isCompressed
? CMapCompressionType.BINARY
: CMapCompressionType.NONE,
}))
.catch(reason => {
throw new Error(
`Unable to load ${this.isCompressed ? "binary " : ""}CMap at: ${url}`
);
});
}

/**
* @ignore
* @returns {Promise<Uint8Array>}
*/
_fetchData(url, compressionType) {
unreachable("Abstract method `_fetchData` called.");
async _fetch(url) {
unreachable("Abstract method `_fetch` called.");
}
}

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

return this._fetchData(url).catch(reason => {
return this._fetch(url).catch(reason => {
throw new Error(`Unable to load font data at: ${url}`);
});
}

/**
* @ignore
* @returns {Promise<Uint8Array>}
*/
_fetchData(url) {
unreachable("Abstract method `_fetchData` called.");
async _fetch(url) {
unreachable("Abstract method `_fetch` called.");
}
}

Expand Down
22 changes: 9 additions & 13 deletions src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,28 +564,24 @@ class DOMCMapReaderFactory extends BaseCMapReaderFactory {
/**
* @ignore
*/
_fetchData(url, compressionType) {
return fetchData(
async _fetch(url) {
const data = await fetchData(
url,
/* type = */ this.isCompressed ? "arraybuffer" : "text"
).then(data => ({
cMapData:
data instanceof ArrayBuffer
? new Uint8Array(data)
: stringToBytes(data),
compressionType,
}));
);
return data instanceof ArrayBuffer
? new Uint8Array(data)
: stringToBytes(data);
}
}

class DOMStandardFontDataFactory extends BaseStandardFontDataFactory {
/**
* @ignore
*/
_fetchData(url) {
return fetchData(url, /* type = */ "arraybuffer").then(
data => new Uint8Array(data)
);
async _fetch(url) {
const data = await fetchData(url, /* type = */ "arraybuffer");
return new Uint8Array(data);
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/display/node_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,11 @@ class NodePackages {
}
}

const fetchData = function (url) {
async function fetchData(url) {
const fs = NodePackages.get("fs");
return fs.promises.readFile(url).then(data => new Uint8Array(data));
};
const data = await fs.promises.readFile(url);
return new Uint8Array(data);
}

class NodeFilterFactory extends BaseFilterFactory {}

Expand All @@ -134,21 +135,22 @@ class NodeCMapReaderFactory extends BaseCMapReaderFactory {
/**
* @ignore
*/
_fetchData(url, compressionType) {
return fetchData(url).then(data => ({ cMapData: data, compressionType }));
async _fetch(url) {
return fetchData(url);
}
}

class NodeStandardFontDataFactory extends BaseStandardFontDataFactory {
/**
* @ignore
*/
_fetchData(url) {
async _fetch(url) {
return fetchData(url);
}
}

export {
fetchData,
NodeCanvasFactory,
NodeCMapReaderFactory,
NodeFilterFactory,
Expand Down
21 changes: 6 additions & 15 deletions test/unit/test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import { assert, isNodeJS } from "../../src/shared/util.js";
import { NullStream, StringStream } from "../../src/core/stream.js";
import { Page, PDFDocument } from "../../src/core/document.js";
import { fetchData as fetchDataDOM } from "../../src/display/display_utils.js";
import { fetchData as fetchDataNode } from "../../src/display/node_utils.js";
import { Ref } from "../../src/core/primitives.js";

let fs, http;
Expand All @@ -33,27 +35,16 @@ const STANDARD_FONT_DATA_URL = isNodeJS
? "./external/standard_fonts/"
: "../../external/standard_fonts/";

class DOMFileReaderFactory {
class DefaultFileReaderFactory {
static async fetch(params) {
const response = await fetch(params.path);
if (!response.ok) {
throw new Error(response.statusText);
if (isNodeJS) {
return fetchDataNode(params.path);
}
return new Uint8Array(await response.arrayBuffer());
}
}

class NodeFileReaderFactory {
static async fetch(params) {
const data = await fs.promises.readFile(params.path);
const data = await fetchDataDOM(params.path, /* type = */ "arraybuffer");
return new Uint8Array(data);
}
}

const DefaultFileReaderFactory = isNodeJS
? NodeFileReaderFactory
: DOMFileReaderFactory;

function buildGetDocumentParams(filename, options) {
const params = Object.create(null);
params.url = isNodeJS
Expand Down

0 comments on commit 1e07b87

Please sign in to comment.