Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
case "buffer": {
this.result = copyToArrayBufferInNewRealm(data, this._globalObject);
break;
}
case "binaryString": {
this.result = data.toString("binary");
break;
}
case "dataURL": {
// Spec seems very unclear here; see https://github.com/w3c/FileAPI/issues/104.
const contentType = MIMEType.parse(file.type) || "application/octet-stream";
this.result = `data:${contentType};base64,${data.toString("base64")}`;
break;
}
case "text": {
this.result = whatwgEncoding.decode(data, encoding);
break;
}
}
this.readyState = READY_STATES.DONE;
this._fireProgressEvent("load");
this._fireProgressEvent("loadend");
});
});
function readDataURL(dataURL, { defaultEncoding, detectMetaCharset }, callback) {
try {
const parsed = parseDataURL(dataURL);
// If default encoding does not exist, pass on binary data.
if (defaultEncoding) {
const sniffOptions = {
transportLayerEncodingLabel: parsed.mimeType.parameters.get("charset"),
defaultEncoding
};
const encoding = detectMetaCharset ?
sniffHTMLEncoding(parsed.body, sniffOptions) :
whatwgEncoding.getBOMEncoding(parsed.body) ||
whatwgEncoding.labelToName(parsed.mimeType.parameters.get("charset")) ||
defaultEncoding;
const decoded = whatwgEncoding.decode(parsed.body, encoding);
parsed.mimeType.parameters.set("charset", encoding);
callback(null, decoded, { headers: { "content-type": parsed.mimeType.toString() } });
} else {
callback(null, parsed.body, { headers: { "content-type": parsed.mimeType.toString() } });
}
} catch (err) {
callback(err, null);
}
return null;
}
function onStylesheetLoad(data) {
const css = whatwgEncoding.decode(data, defaultEncoding);
// TODO: MIME type checking?
if (elementImpl.sheet) {
exports.removeStylesheet(elementImpl.sheet, elementImpl);
}
exports.createStylesheet(css, elementImpl, parsedURL);
}
let isXML = false;
const parsed = MIMEType.parse(contentType);
if (parsed) {
isHTML = parsed.isHTML();
isXML = parsed.isXML();
if (!isXML && !isHTML) {
return null;
}
}
if (this.responseType === "" && isHTML) {
return null;
}
const encoding = finalCharset(this) || whatwgEncoding.getBOMEncoding(responseBuffer) || "UTF-8";
const resText = whatwgEncoding.decode(responseBuffer, encoding);
if (!resText) {
return null;
}
const res = Document.create([], { options: {
url: flag.uri,
lastModified: new Date(getResponseHeader(this, "last-modified")),
parsingMode: isHTML ? "html" : "xml",
cookieJar: { setCookieSync: () => undefined, getCookieStringSync: () => "" },
encoding,
parseOptions: this._ownerDocument._parseOptions
} });
const resImpl = idlUtils.implForWrapper(res);
try {
resImpl._htmlToDom.appendToDocument(resText, resImpl);
} catch (e) {
function normalizeHTML(html = "", mimeType) {
let encoding = "UTF-8";
if (ArrayBuffer.isView(html)) {
html = Buffer.from(html.buffer, html.byteOffset, html.byteLength);
} else if (html instanceof ArrayBuffer) {
html = Buffer.from(html);
}
if (Buffer.isBuffer(html)) {
encoding = sniffHTMLEncoding(html, {
defaultEncoding: mimeType.isXML() ? "UTF-8" : "windows-1252",
transportLayerEncodingLabel: mimeType.parameters.get("charset")
});
html = whatwgEncoding.decode(html, encoding);
} else {
html = String(html);
}
return { html, encoding };
}
if (this.readyState !== XMLHttpRequest.LOADING && this.readyState !== XMLHttpRequest.DONE) {
return "";
}
if (properties.responseTextCache) {
return properties.responseTextCache;
}
const responseBuffer = properties.responseBuffer ?
properties.responseBuffer.slice(0, properties.totalReceivedChunkSize) :
null;
if (!responseBuffer) {
return "";
}
const fallbackEncoding = finalCharset(this) || whatwgEncoding.getBOMEncoding(responseBuffer) || "UTF-8";
const res = whatwgEncoding.decode(responseBuffer, fallbackEncoding);
properties.responseTextCache = res;
return res;
}
get responseXML() {
if (this.readyState !== XMLHttpRequest.LOADING && this.readyState !== XMLHttpRequest.DONE) {
return "";
}
if (properties.responseTextCache) {
return properties.responseTextCache;
}
const responseBuffer = properties.responseBuffer ?
properties.responseBuffer.slice(0, properties.totalReceivedChunkSize) :
null;
if (!responseBuffer) {
return "";
}
const fallbackEncoding = finalCharset(this) || whatwgEncoding.getBOMEncoding(responseBuffer) || "UTF-8";
const res = whatwgEncoding.decode(responseBuffer, fallbackEncoding);
properties.responseTextCache = res;
return res;
}
get responseXML() {
function decodeContent(html, contentType) {
let transportLayerEncodingLabel;
if (contentType) {
const mimeType = new MIMEType(contentType);
transportLayerEncodingLabel = mimeType.parameters.get("charset");
}
html = Buffer.from(html.buffer, html.byteOffset, html.byteLength);
let encoding = sniffHTMLEncoding(html, {defaultEncoding: "UTF-8", transportLayerEncodingLabel});
html = whatwgEncoding.decode(html, encoding);
return html;
}