135 lines
4.3 KiB
JavaScript
135 lines
4.3 KiB
JavaScript
"use strict";
|
|
var __defProp = Object.defineProperty;
|
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __export = (target, all) => {
|
|
for (var name in all)
|
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
};
|
|
var __copyProps = (to, from, except, desc) => {
|
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
for (let key of __getOwnPropNames(from))
|
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
}
|
|
return to;
|
|
};
|
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
var qp_decoder_exports = {};
|
|
__export(qp_decoder_exports, {
|
|
default: () => QPDecoder
|
|
});
|
|
module.exports = __toCommonJS(qp_decoder_exports);
|
|
var import_decode_strings = require("./decode-strings.cjs");
|
|
const VALID_QP_REGEX = /^=[a-f0-9]{2}$/i;
|
|
const QP_SPLIT_REGEX = /(?==[a-f0-9]{2})/i;
|
|
const SOFT_LINE_BREAK_REGEX = /=\r?\n/g;
|
|
const PARTIAL_QP_ENDING_REGEX = /=[a-fA-F0-9]?$/;
|
|
class QPDecoder {
|
|
constructor(opts) {
|
|
opts = opts || {};
|
|
this.decoder = opts.decoder || new TextDecoder();
|
|
this.maxChunkSize = 100 * 1024;
|
|
this.remainder = "";
|
|
this.chunks = [];
|
|
}
|
|
decodeQPBytes(encodedBytes) {
|
|
let buf = new ArrayBuffer(encodedBytes.length);
|
|
let dataView = new DataView(buf);
|
|
for (let i = 0, len = encodedBytes.length; i < len; i++) {
|
|
dataView.setUint8(i, parseInt(encodedBytes[i], 16));
|
|
}
|
|
return buf;
|
|
}
|
|
decodeChunks(str) {
|
|
str = str.replace(SOFT_LINE_BREAK_REGEX, "");
|
|
let list = str.split(QP_SPLIT_REGEX);
|
|
let encodedBytes = [];
|
|
for (let part of list) {
|
|
if (part.charAt(0) !== "=") {
|
|
if (encodedBytes.length) {
|
|
this.chunks.push(this.decodeQPBytes(encodedBytes));
|
|
encodedBytes = [];
|
|
}
|
|
this.chunks.push(part);
|
|
continue;
|
|
}
|
|
if (part.length === 3) {
|
|
if (VALID_QP_REGEX.test(part)) {
|
|
encodedBytes.push(part.substr(1));
|
|
} else {
|
|
if (encodedBytes.length) {
|
|
this.chunks.push(this.decodeQPBytes(encodedBytes));
|
|
encodedBytes = [];
|
|
}
|
|
this.chunks.push(part);
|
|
}
|
|
continue;
|
|
}
|
|
if (part.length > 3) {
|
|
const firstThree = part.substr(0, 3);
|
|
if (VALID_QP_REGEX.test(firstThree)) {
|
|
encodedBytes.push(part.substr(1, 2));
|
|
this.chunks.push(this.decodeQPBytes(encodedBytes));
|
|
encodedBytes = [];
|
|
part = part.substr(3);
|
|
this.chunks.push(part);
|
|
} else {
|
|
if (encodedBytes.length) {
|
|
this.chunks.push(this.decodeQPBytes(encodedBytes));
|
|
encodedBytes = [];
|
|
}
|
|
this.chunks.push(part);
|
|
}
|
|
}
|
|
}
|
|
if (encodedBytes.length) {
|
|
this.chunks.push(this.decodeQPBytes(encodedBytes));
|
|
}
|
|
}
|
|
update(buffer) {
|
|
let str = this.decoder.decode(buffer) + "\n";
|
|
str = this.remainder + str;
|
|
if (str.length < this.maxChunkSize) {
|
|
this.remainder = str;
|
|
return;
|
|
}
|
|
this.remainder = "";
|
|
let partialEnding = str.match(PARTIAL_QP_ENDING_REGEX);
|
|
if (partialEnding) {
|
|
if (partialEnding.index === 0) {
|
|
this.remainder = str;
|
|
return;
|
|
}
|
|
this.remainder = str.substr(partialEnding.index);
|
|
str = str.substr(0, partialEnding.index);
|
|
}
|
|
this.decodeChunks(str);
|
|
}
|
|
finalize() {
|
|
if (this.remainder.length) {
|
|
this.decodeChunks(this.remainder);
|
|
this.remainder = "";
|
|
}
|
|
return (0, import_decode_strings.blobToArrayBuffer)(new Blob(this.chunks, { type: "application/octet-stream" }));
|
|
}
|
|
}
|
|
|
|
// Make default export work naturally with require()
|
|
if (module.exports.default) {
|
|
var defaultExport = module.exports.default;
|
|
var namedExports = {};
|
|
for (var key in module.exports) {
|
|
if (key !== 'default' && key !== '__esModule') {
|
|
namedExports[key] = module.exports[key];
|
|
}
|
|
}
|
|
module.exports = defaultExport;
|
|
Object.assign(module.exports, namedExports);
|
|
// Preserve __esModule and .default for bundler/transpiler interop
|
|
Object.defineProperty(module.exports, '__esModule', { value: true });
|
|
module.exports.default = defaultExport;
|
|
}
|
|
|