Skip to content

Commit

Permalink
Remove the BaseFullReader and BaseRangeReader classes in the `src…
Browse files Browse the repository at this point in the history
…/display/node_stream.js` file

After the previous patch these base-classes are only extended once each and they can thus be combined with the final classes.
  • Loading branch information
Snuffleupagus committed Nov 2, 2024
1 parent d100db6 commit 5e94521
Showing 1 changed file with 26 additions and 38 deletions.
64 changes: 26 additions & 38 deletions src/display/node_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class PDFNodeStream {
}
}

class BaseFullReader {
class PDFNodeStreamFsFullReader {
constructor(stream) {
this._url = stream.url;
this._done = false;
Expand All @@ -99,6 +99,24 @@ class BaseFullReader {
this._readableStream = null;
this._readCapability = Promise.withResolvers();
this._headersCapability = Promise.withResolvers();

const fs = process.getBuiltinModule("fs");
fs.promises.lstat(this._url).then(
stat => {
// Setting right content length.
this._contentLength = stat.size;

this._setReadableStream(fs.createReadStream(this._url));
this._headersCapability.resolve();
},
error => {
if (error.code === "ENOENT") {
error = new MissingPDFException(`Missing PDF "${this._url}".`);
}
this._storedError = error;
this._headersCapability.reject(error);
}
);
}

get headersReady() {
Expand Down Expand Up @@ -191,8 +209,8 @@ class BaseFullReader {
}
}

class BaseRangeReader {
constructor(stream) {
class PDFNodeStreamFsRangeReader {
constructor(stream, start, end) {
this._url = stream.url;
this._done = false;
this._storedError = null;
Expand All @@ -202,6 +220,11 @@ class BaseRangeReader {
this._readCapability = Promise.withResolvers();
const source = stream.source;
this._isStreamingSupported = !source.disableStream;

const fs = process.getBuiltinModule("fs");
this._setReadableStream(
fs.createReadStream(this._url, { start, end: end - 1 })
);
}

get isStreamingSupported() {
Expand Down Expand Up @@ -269,39 +292,4 @@ class BaseRangeReader {
}
}

class PDFNodeStreamFsFullReader extends BaseFullReader {
constructor(stream) {
super(stream);

const fs = process.getBuiltinModule("fs");
fs.promises.lstat(this._url).then(
stat => {
// Setting right content length.
this._contentLength = stat.size;

this._setReadableStream(fs.createReadStream(this._url));
this._headersCapability.resolve();
},
error => {
if (error.code === "ENOENT") {
error = new MissingPDFException(`Missing PDF "${this._url}".`);
}
this._storedError = error;
this._headersCapability.reject(error);
}
);
}
}

class PDFNodeStreamFsRangeReader extends BaseRangeReader {
constructor(stream, start, end) {
super(stream);

const fs = process.getBuiltinModule("fs");
this._setReadableStream(
fs.createReadStream(this._url, { start, end: end - 1 })
);
}
}

export { PDFNodeStream };

0 comments on commit 5e94521

Please sign in to comment.