Merge pull request #14575 from Snuffleupagus/rm-isStream

Remove the `isStream` helper function
This commit is contained in:
Tim van der Meij 2022-02-19 14:59:19 +01:00 committed by GitHub
commit df0aa1a9c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 51 additions and 80 deletions

View File

@ -39,15 +39,8 @@ import {
createDefaultAppearance, createDefaultAppearance,
parseDefaultAppearance, parseDefaultAppearance,
} from "./default_appearance.js"; } from "./default_appearance.js";
import { import { Dict, isDict, isName, isRef, Name, RefSet } from "./primitives.js";
Dict, import { BaseStream } from "./base_stream.js";
isDict,
isName,
isRef,
isStream,
Name,
RefSet,
} from "./primitives.js";
import { bidi } from "./bidi.js"; import { bidi } from "./bidi.js";
import { Catalog } from "./catalog.js"; import { Catalog } from "./catalog.js";
import { ColorSpace } from "./colorspace.js"; import { ColorSpace } from "./colorspace.js";
@ -693,7 +686,7 @@ class Annotation {
// In case the normal appearance is a stream, then it is used directly. // In case the normal appearance is a stream, then it is used directly.
const normalAppearanceState = appearanceStates.get("N"); const normalAppearanceState = appearanceStates.get("N");
if (isStream(normalAppearanceState)) { if (normalAppearanceState instanceof BaseStream) {
this.appearance = normalAppearanceState; this.appearance = normalAppearanceState;
return; return;
} }

View File

@ -41,7 +41,6 @@ import {
isName, isName,
isRef, isRef,
isRefsEqual, isRefsEqual,
isStream,
Name, Name,
Ref, Ref,
RefSet, RefSet,
@ -993,7 +992,7 @@ class Catalog {
} }
let js = jsDict.get("JS"); let js = jsDict.get("JS");
if (isStream(js)) { if (js instanceof BaseStream) {
js = js.getString(); js = js.getString();
} else if (typeof js !== "string") { } else if (typeof js !== "string") {
return; return;
@ -1551,7 +1550,7 @@ class Catalog {
const jsAction = action.get("JS"); const jsAction = action.get("JS");
let js; let js;
if (isStream(jsAction)) { if (jsAction instanceof BaseStream) {
js = jsAction.getString(); js = jsAction.getString();
} else if (isString(jsAction)) { } else if (isString(jsAction)) {
js = jsAction; js = jsAction;

View File

@ -20,7 +20,8 @@ import {
unreachable, unreachable,
warn, warn,
} from "../shared/util.js"; } from "../shared/util.js";
import { EOF, isCmd, isName, isStream } from "./primitives.js"; import { EOF, isCmd, isName } from "./primitives.js";
import { BaseStream } from "./base_stream.js";
import { Lexer } from "./parser.js"; import { Lexer } from "./parser.js";
import { MissingDataException } from "./core_utils.js"; import { MissingDataException } from "./core_utils.js";
import { Stream } from "./stream.js"; import { Stream } from "./stream.js";
@ -1025,7 +1026,7 @@ const CMapFactory = (function CMapFactoryClosure() {
if (isName(encoding)) { if (isName(encoding)) {
return createBuiltInCMap(encoding.name, fetchBuiltInCMap); return createBuiltInCMap(encoding.name, fetchBuiltInCMap);
} else if (isStream(encoding)) { } else if (encoding instanceof BaseStream) {
const parsedCMap = await parseCMap( const parsedCMap = await parseCMap(
/* cMap = */ new CMap(), /* cMap = */ new CMap(),
/* lexer = */ new Lexer(encoding), /* lexer = */ new Lexer(encoding),

View File

@ -21,7 +21,8 @@ import {
unreachable, unreachable,
warn, warn,
} from "../shared/util.js"; } from "../shared/util.js";
import { isDict, isName, isStream, Name, Ref } from "./primitives.js"; import { isDict, isName, Name, Ref } from "./primitives.js";
import { BaseStream } from "./base_stream.js";
import { MissingDataException } from "./core_utils.js"; import { MissingDataException } from "./core_utils.js";
/** /**
@ -642,7 +643,7 @@ class IndexedCS extends ColorSpace {
const length = base.numComps * highVal; const length = base.numComps * highVal;
this.lookup = new Uint8Array(length); this.lookup = new Uint8Array(length);
if (isStream(lookup)) { if (lookup instanceof BaseStream) {
const bytes = lookup.getBytes(length); const bytes = lookup.getBytes(length);
this.lookup.set(bytes); this.lookup.set(bytes);
} else if (typeof lookup === "string") { } else if (typeof lookup === "string") {

View File

@ -44,15 +44,7 @@ import {
XRefEntryException, XRefEntryException,
XRefParseException, XRefParseException,
} from "./core_utils.js"; } from "./core_utils.js";
import { import { Dict, isDict, isName, isRef, Name, Ref } from "./primitives.js";
Dict,
isDict,
isName,
isRef,
isStream,
Name,
Ref,
} from "./primitives.js";
import { getXfaFontDict, getXfaFontName } from "./xfa_fonts.js"; import { getXfaFontDict, getXfaFontName } from "./xfa_fonts.js";
import { NullStream, Stream } from "./stream.js"; import { NullStream, Stream } from "./stream.js";
import { AnnotationFactory } from "./annotation.js"; import { AnnotationFactory } from "./annotation.js";
@ -638,7 +630,7 @@ function find(stream, signature, limit = 1024, backwards = false) {
class PDFDocument { class PDFDocument {
constructor(pdfManager, arg) { constructor(pdfManager, arg) {
let stream; let stream;
if (isStream(arg)) { if (arg instanceof BaseStream) {
stream = arg; stream = arg;
} else if (isArrayBuffer(arg)) { } else if (isArrayBuffer(arg)) {
stream = new Stream(arg); stream = new Stream(arg);
@ -848,7 +840,7 @@ class PDFDocument {
stylesheet: "", stylesheet: "",
"/xdp:xdp": "", "/xdp:xdp": "",
}; };
if (isStream(xfa) && !xfa.isEmpty) { if (xfa instanceof BaseStream && !xfa.isEmpty) {
try { try {
entries["xdp:xdp"] = stringToUTF8String(xfa.getString()); entries["xdp:xdp"] = stringToUTF8String(xfa.getString());
return entries; return entries;
@ -876,7 +868,7 @@ class PDFDocument {
continue; continue;
} }
const data = this.xref.fetchIfRef(xfa[i + 1]); const data = this.xref.fetchIfRef(xfa[i + 1]);
if (!isStream(data) || data.isEmpty) { if (!(data instanceof BaseStream) || data.isEmpty) {
continue; continue;
} }
try { try {
@ -923,10 +915,9 @@ class PDFDocument {
const xfaImages = new Map(); const xfaImages = new Map();
for (const key of keys) { for (const key of keys) {
const stream = xfaImagesDict.get(key); const stream = xfaImagesDict.get(key);
if (!isStream(stream)) { if (stream instanceof BaseStream) {
continue; xfaImages.set(key, stream.getBytes());
} }
xfaImages.set(key, stream.getBytes());
} }
this.xfaFactory.setImages(xfaImages); this.xfaFactory.setImages(xfaImages);
@ -1115,7 +1106,7 @@ class PDFDocument {
const xfa = acroForm.get("XFA"); const xfa = acroForm.get("XFA");
formInfo.hasXfa = formInfo.hasXfa =
(Array.isArray(xfa) && xfa.length > 0) || (Array.isArray(xfa) && xfa.length > 0) ||
(isStream(xfa) && !xfa.isEmpty); (xfa instanceof BaseStream && !xfa.isEmpty);
// The document contains AcroForm data if the `Fields` entry is a // The document contains AcroForm data if the `Fields` entry is a
// non-empty array and it doesn't consist of only document signatures. // non-empty array and it doesn't consist of only document signatures.

View File

@ -42,7 +42,6 @@ import {
isDict, isDict,
isName, isName,
isRef, isRef,
isStream,
Name, Name,
Ref, Ref,
RefSet, RefSet,
@ -340,7 +339,7 @@ class PartialEvaluator {
continue; continue;
} }
} }
if (!isStream(xObject)) { if (!(xObject instanceof BaseStream)) {
continue; continue;
} }
if (xObject.dict.objId) { if (xObject.dict.objId) {
@ -1420,7 +1419,7 @@ class PartialEvaluator {
const pattern = this.xref.fetchIfRef(rawPattern); const pattern = this.xref.fetchIfRef(rawPattern);
if (pattern) { if (pattern) {
const dict = isStream(pattern) ? pattern.dict : pattern; const dict = pattern instanceof BaseStream ? pattern.dict : pattern;
const typeNum = dict.get("PatternType"); const typeNum = dict.get("PatternType");
if (typeNum === PatternType.TILING) { if (typeNum === PatternType.TILING) {
@ -1664,7 +1663,7 @@ class PartialEvaluator {
xobj = xref.fetch(xobj); xobj = xref.fetch(xobj);
} }
if (!isStream(xobj)) { if (!(xobj instanceof BaseStream)) {
throw new FormatError("XObject should be a stream"); throw new FormatError("XObject should be a stream");
} }
@ -2986,7 +2985,7 @@ class PartialEvaluator {
xobj = xref.fetch(xobj); xobj = xref.fetch(xobj);
} }
if (!isStream(xobj)) { if (!(xobj instanceof BaseStream)) {
throw new FormatError("XObject should be a stream"); throw new FormatError("XObject should be a stream");
} }
@ -3509,7 +3508,7 @@ class PartialEvaluator {
} }
return new ToUnicodeMap(cmap.getMap()); return new ToUnicodeMap(cmap.getMap());
}); });
} else if (isStream(cmapObj)) { } else if (cmapObj instanceof BaseStream) {
return CMapFactory.create({ return CMapFactory.create({
encoding: cmapObj, encoding: cmapObj,
fetchBuiltInCMap: this._fetchBuiltInCMapBound, fetchBuiltInCMap: this._fetchBuiltInCMapBound,
@ -3813,7 +3812,7 @@ class PartialEvaluator {
hash.update(`${firstChar}-${lastChar}`); // Fixes issue10665_reduced.pdf hash.update(`${firstChar}-${lastChar}`); // Fixes issue10665_reduced.pdf
toUnicode = dict.get("ToUnicode") || baseDict.get("ToUnicode"); toUnicode = dict.get("ToUnicode") || baseDict.get("ToUnicode");
if (isStream(toUnicode)) { if (toUnicode instanceof BaseStream) {
const stream = toUnicode.str || toUnicode; const stream = toUnicode.str || toUnicode;
const uint8array = stream.buffer const uint8array = stream.buffer
? new Uint8Array(stream.buffer.buffer, 0, stream.bufferLength) ? new Uint8Array(stream.buffer.buffer, 0, stream.bufferLength)

View File

@ -13,8 +13,9 @@
* limitations under the License. * limitations under the License.
*/ */
import { isDict, isStream } from "./primitives.js";
import { stringToPDFString, warn } from "../shared/util.js"; import { stringToPDFString, warn } from "../shared/util.js";
import { BaseStream } from "./base_stream.js";
import { isDict } from "./primitives.js";
function pickPlatformItem(dict) { function pickPlatformItem(dict) {
// Look for the filename in this order: // Look for the filename in this order:
@ -84,7 +85,7 @@ class FileSpec {
let content = null; let content = null;
if (this.contentRef) { if (this.contentRef) {
const fileObj = this.xref.fetchIfRef(this.contentRef); const fileObj = this.xref.fetchIfRef(this.contentRef);
if (fileObj && isStream(fileObj)) { if (fileObj instanceof BaseStream) {
content = fileObj.getBytes(); content = fileObj.getBytes();
} else { } else {
warn( warn(

View File

@ -13,7 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { Dict, isDict, isStream, Ref } from "./primitives.js"; import { Dict, isDict, Ref } from "./primitives.js";
import { import {
FormatError, FormatError,
info, info,
@ -23,6 +23,7 @@ import {
unreachable, unreachable,
} from "../shared/util.js"; } from "../shared/util.js";
import { PostScriptLexer, PostScriptParser } from "./ps_parser.js"; import { PostScriptLexer, PostScriptParser } from "./ps_parser.js";
import { BaseStream } from "./base_stream.js";
import { LocalFunctionCache } from "./image_utils.js"; import { LocalFunctionCache } from "./image_utils.js";
class PDFFunctionFactory { class PDFFunctionFactory {
@ -71,7 +72,7 @@ class PDFFunctionFactory {
fnRef = cacheKey; fnRef = cacheKey;
} else if (cacheKey instanceof Dict) { } else if (cacheKey instanceof Dict) {
fnRef = cacheKey.objId; fnRef = cacheKey.objId;
} else if (isStream(cacheKey)) { } else if (cacheKey instanceof BaseStream) {
fnRef = cacheKey.dict && cacheKey.dict.objId; fnRef = cacheKey.dict && cacheKey.dict.objId;
} }
if (fnRef) { if (fnRef) {
@ -97,7 +98,7 @@ class PDFFunctionFactory {
fnRef = cacheKey; fnRef = cacheKey;
} else if (cacheKey instanceof Dict) { } else if (cacheKey instanceof Dict) {
fnRef = cacheKey.objId; fnRef = cacheKey.objId;
} else if (isStream(cacheKey)) { } else if (cacheKey instanceof BaseStream) {
fnRef = cacheKey.dict && cacheKey.dict.objId; fnRef = cacheKey.dict && cacheKey.dict.objId;
} }
if (fnRef) { if (fnRef) {
@ -509,7 +510,7 @@ function isPDFFunction(v) {
return false; return false;
} else if (isDict(v)) { } else if (isDict(v)) {
fnDict = v; fnDict = v;
} else if (isStream(v)) { } else if (v instanceof BaseStream) {
fnDict = v.dict; fnDict = v.dict;
} else { } else {
return false; return false;

View File

@ -14,7 +14,8 @@
*/ */
import { assert, FormatError, ImageKind, info, warn } from "../shared/util.js"; import { assert, FormatError, ImageKind, info, warn } from "../shared/util.js";
import { isName, isStream, Name } from "./primitives.js"; import { isName, Name } from "./primitives.js";
import { BaseStream } from "./base_stream.js";
import { ColorSpace } from "./colorspace.js"; import { ColorSpace } from "./colorspace.js";
import { DecodeStream } from "./decode_stream.js"; import { DecodeStream } from "./decode_stream.js";
import { JpegStream } from "./jpeg_stream.js"; import { JpegStream } from "./jpeg_stream.js";
@ -223,7 +224,7 @@ class PDFImage {
localColorSpaceCache, localColorSpaceCache,
}); });
} else if (mask) { } else if (mask) {
if (isStream(mask)) { if (mask instanceof BaseStream) {
const maskDict = mask.dict, const maskDict = mask.dict,
imageMask = maskDict.get("IM", "ImageMask"); imageMask = maskDict.get("IM", "ImageMask");
if (!imageMask) { if (!imageMask) {
@ -268,7 +269,7 @@ class PDFImage {
if (smask) { if (smask) {
smaskData = smask; smaskData = smask;
} else if (mask) { } else if (mask) {
if (isStream(mask) || Array.isArray(mask)) { if (mask instanceof BaseStream || Array.isArray(mask)) {
maskData = mask; maskData = mask;
} else { } else {
warn("Unsupported mask format."); warn("Unsupported mask format.");

View File

@ -13,8 +13,9 @@
* limitations under the License. * limitations under the License.
*/ */
import { isDict, isStream } from "./primitives.js"; import { BaseStream } from "./base_stream.js";
import { DecodeStream } from "./decode_stream.js"; import { DecodeStream } from "./decode_stream.js";
import { isDict } from "./primitives.js";
import { Jbig2Image } from "./jbig2.js"; import { Jbig2Image } from "./jbig2.js";
import { shadow } from "../shared/util.js"; import { shadow } from "../shared/util.js";
@ -51,7 +52,7 @@ class Jbig2Stream extends DecodeStream {
const chunks = []; const chunks = [];
if (isDict(this.params)) { if (isDict(this.params)) {
const globalsStream = this.params.get("JBIG2Globals"); const globalsStream = this.params.get("JBIG2Globals");
if (isStream(globalsStream)) { if (globalsStream instanceof BaseStream) {
const globals = globalsStream.getBytes(); const globals = globalsStream.getBytes();
chunks.push({ data: globals, start: 0, end: globals.length }); chunks.push({ data: globals, start: 0, end: globals.length });
} }

View File

@ -13,7 +13,8 @@
* limitations under the License. * limitations under the License.
*/ */
import { Dict, isStream, Ref, RefSet } from "./primitives.js"; import { Dict, Ref, RefSet } from "./primitives.js";
import { BaseStream } from "./base_stream.js";
import { MissingDataException } from "./core_utils.js"; import { MissingDataException } from "./core_utils.js";
import { warn } from "../shared/util.js"; import { warn } from "../shared/util.js";
@ -21,15 +22,15 @@ function mayHaveChildren(value) {
return ( return (
value instanceof Ref || value instanceof Ref ||
value instanceof Dict || value instanceof Dict ||
Array.isArray(value) || value instanceof BaseStream ||
isStream(value) Array.isArray(value)
); );
} }
function addChildren(node, nodesToVisit) { function addChildren(node, nodesToVisit) {
if (node instanceof Dict) { if (node instanceof Dict) {
node = node.getRawValues(); node = node.getRawValues();
} else if (isStream(node)) { } else if (node instanceof BaseStream) {
node = node.dict.getRawValues(); node = node.dict.getRawValues();
} else if (!Array.isArray(node)) { } else if (!Array.isArray(node)) {
return; return;
@ -108,7 +109,7 @@ class ObjectLoader {
pendingRequests.push({ begin: ex.begin, end: ex.end }); pendingRequests.push({ begin: ex.begin, end: ex.end });
} }
} }
if (isStream(currentNode)) { if (currentNode instanceof BaseStream) {
const baseStreams = currentNode.getBaseStreams(); const baseStreams = currentNode.getBaseStreams();
if (baseStreams) { if (baseStreams) {
let foundMissingData = false; let foundMissingData = false;

View File

@ -23,8 +23,8 @@ import {
Util, Util,
warn, warn,
} from "../shared/util.js"; } from "../shared/util.js";
import { BaseStream } from "./base_stream.js";
import { ColorSpace } from "./colorspace.js"; import { ColorSpace } from "./colorspace.js";
import { isStream } from "./primitives.js";
import { MissingDataException } from "./core_utils.js"; import { MissingDataException } from "./core_utils.js";
const ShadingType = { const ShadingType = {
@ -50,7 +50,7 @@ class Pattern {
pdfFunctionFactory, pdfFunctionFactory,
localColorSpaceCache localColorSpaceCache
) { ) {
const dict = isStream(shading) ? shading.dict : shading; const dict = shading instanceof BaseStream ? shading.dict : shading;
const type = dict.get("ShadingType"); const type = dict.get("ShadingType");
try { try {
@ -403,7 +403,7 @@ class MeshShading extends BaseShading {
localColorSpaceCache localColorSpaceCache
) { ) {
super(); super();
if (!isStream(stream)) { if (!(stream instanceof BaseStream)) {
throw new FormatError("Mesh data is not a stream"); throw new FormatError("Mesh data is not a stream");
} }
const dict = stream.dict; const dict = stream.dict;

View File

@ -14,7 +14,6 @@
*/ */
import { assert, shadow, unreachable } from "../shared/util.js"; import { assert, shadow, unreachable } from "../shared/util.js";
import { BaseStream } from "./base_stream.js";
const CIRCULAR_REF = Symbol("CIRCULAR_REF"); const CIRCULAR_REF = Symbol("CIRCULAR_REF");
const EOF = Symbol("EOF"); const EOF = Symbol("EOF");
@ -412,10 +411,6 @@ function isRefsEqual(v1, v2) {
return v1.num === v2.num && v1.gen === v2.gen; return v1.num === v2.num && v1.gen === v2.gen;
} }
function isStream(v) {
return v instanceof BaseStream;
}
function clearPrimitiveCaches() { function clearPrimitiveCaches() {
Cmd._clearCache(); Cmd._clearCache();
Name._clearCache(); Name._clearCache();
@ -433,7 +428,6 @@ export {
isName, isName,
isRef, isRef,
isRefsEqual, isRefsEqual,
isStream,
Name, Name,
Ref, Ref,
RefSet, RefSet,

View File

@ -14,9 +14,10 @@
*/ */
import { bytesToString, escapeString, warn } from "../shared/util.js"; import { bytesToString, escapeString, warn } from "../shared/util.js";
import { Dict, isDict, isName, isRef, isStream, Name } from "./primitives.js"; import { Dict, isDict, isName, isRef, Name } from "./primitives.js";
import { escapePDFName, parseXFAPath } from "./core_utils.js"; import { escapePDFName, parseXFAPath } from "./core_utils.js";
import { SimpleDOMNode, SimpleXMLParser } from "./xml_parser.js"; import { SimpleDOMNode, SimpleXMLParser } from "./xml_parser.js";
import { BaseStream } from "./base_stream.js";
import { calculateMD5 } from "./crypto.js"; import { calculateMD5 } from "./crypto.js";
function writeDict(dict, buffer, transform) { function writeDict(dict, buffer, transform) {
@ -87,7 +88,7 @@ function writeValue(value, buffer, transform) {
buffer.push(value.toString()); buffer.push(value.toString());
} else if (isDict(value)) { } else if (isDict(value)) {
writeDict(value, buffer, transform); writeDict(value, buffer, transform);
} else if (isStream(value)) { } else if (value instanceof BaseStream) {
writeStream(value, buffer, transform); writeStream(value, buffer, transform);
} else if (value === null) { } else if (value === null) {
buffer.push("null"); buffer.push("null");

View File

@ -21,7 +21,6 @@ import {
isName, isName,
isRef, isRef,
isRefsEqual, isRefsEqual,
isStream,
Name, Name,
Ref, Ref,
RefSet, RefSet,
@ -560,16 +559,4 @@ describe("primitives", function () {
expect(isRefsEqual(ref1, ref2)).toEqual(false); expect(isRefsEqual(ref1, ref2)).toEqual(false);
}); });
}); });
describe("isStream", function () {
it("handles non-streams", function () {
const nonStream = {};
expect(isStream(nonStream)).toEqual(false);
});
it("handles streams", function () {
const stream = new StringStream("foo");
expect(isStream(stream)).toEqual(true);
});
});
}); });