lmdb-localstorage
Version:
A browser localStorage implementation of the NodeJS lmdb API
1,060 lines (1,055 loc) • 81 kB
JavaScript
/*!
LZ-UTF8 v0.6.3
Copyright (c) 2021, Rotem Dan
Released under the MIT license.
Build date: 2022-07-06
Please report any issue at https://github.com/rotemdan/lzutf8.js/issues
Modifications under same license.
Simon Y. Blackwell, AnyWhichWay, LLC 2023
*/
var LZUTF8;
(function (LZUTF8) {
LZUTF8.runningInNodeJS = function () {
return ((typeof process === "object") && (typeof process.versions === "object") && (typeof process.versions.node === "string"));
};
LZUTF8.runningInMainNodeJSModule = function () {
return LZUTF8.runningInNodeJS() && require.main === module;
};
LZUTF8.commonJSAvailable = function () {
return typeof module === "object" && typeof module.exports === "object";
};
LZUTF8.runningInWebWorker = function () {
return typeof window === "undefined" && typeof self === "object" && typeof self.addEventListener === "function" && typeof self.close === "function";
};
LZUTF8.runningInNodeChildProcess = function () {
return LZUTF8.runningInNodeJS() && typeof process.send === "function";
};
LZUTF8.runningInNullOrigin = function () {
if (typeof window !== "object" || typeof window.location !== "object" || typeof document !== "object")
return false;
return document.location.protocol !== 'http:' && document.location.protocol !== 'https:';
};
LZUTF8.webWorkersAvailable = function () {
if (typeof Worker !== "function" || LZUTF8.runningInNullOrigin())
return false;
if (LZUTF8.runningInNodeJS())
return false;
if (navigator && navigator.userAgent && navigator.userAgent.indexOf("Android 4.3") >= 0)
return false;
return true;
};
LZUTF8.log = function (message, appendToDocument) {
if (appendToDocument === void 0) { appendToDocument = false; }
if (typeof console !== "object")
return;
console.log(message);
if (appendToDocument && typeof document == "object")
document.body.innerHTML += message + "<br/>";
};
LZUTF8.createErrorMessage = function (exception, title) {
if (title === void 0) { title = "Unhandled exception"; }
if (exception == null)
return title;
title += ": ";
if (typeof exception.content === "object") {
if (LZUTF8.runningInNodeJS()) {
return title + exception.content.stack;
}
else {
var exceptionJSON = JSON.stringify(exception.content);
if (exceptionJSON !== "{}")
return title + exceptionJSON;
else
return title + exception.content;
}
}
else if (typeof exception.content === "string") {
return title + exception.content;
}
else {
return title + exception;
}
};
LZUTF8.printExceptionAndStackTraceToConsole = function (exception, title) {
if (title === void 0) { title = "Unhandled exception"; }
LZUTF8.log(LZUTF8.createErrorMessage(exception, title));
};
LZUTF8.getGlobalObject = function () {
if (typeof global === "object")
return global;
else if (typeof window === "object")
return window;
else if (typeof self === "object")
return self;
else
return {};
};
LZUTF8.toString = Object.prototype.toString;
if (LZUTF8.commonJSAvailable())
module.exports = LZUTF8;
})(LZUTF8 || (LZUTF8 = {}));
var IE10SubarrayBugPatcher;
(function (IE10SubarrayBugPatcher) {
if (typeof Uint8Array === "function" && new Uint8Array(1).subarray(1).byteLength !== 0) {
var subarray = function (start, end) {
var clamp = function (v, min, max) { return v < min ? min : v > max ? max : v; };
start = start | 0;
end = end | 0;
if (arguments.length < 1)
start = 0;
if (arguments.length < 2)
end = this.length;
if (start < 0)
start = this.length + start;
if (end < 0)
end = this.length + end;
start = clamp(start, 0, this.length);
end = clamp(end, 0, this.length);
var len = end - start;
if (len < 0)
len = 0;
return new this.constructor(this.buffer, this.byteOffset + start * this.BYTES_PER_ELEMENT, len);
};
var types = ['Int8Array', 'Uint8Array', 'Uint8ClampedArray', 'Int16Array', 'Uint16Array', 'Int32Array', 'Uint32Array', 'Float32Array', 'Float64Array'];
var globalObject = void 0;
if (typeof window === "object")
globalObject = window;
else if (typeof self === "object")
globalObject = self;
if (globalObject !== undefined) {
for (var i = 0; i < types.length; i++) {
if (globalObject[types[i]])
globalObject[types[i]].prototype.subarray = subarray;
}
}
}
})(IE10SubarrayBugPatcher || (IE10SubarrayBugPatcher = {}));
var LZUTF8;
(function (LZUTF8) {
var AsyncCompressor = (function () {
function AsyncCompressor() {
}
AsyncCompressor.compressAsync = function (input, options, callback) {
var timer = new LZUTF8.Timer();
var compressor = new LZUTF8.Compressor();
if (!callback)
throw new TypeError("compressAsync: No callback argument given");
if (typeof input === "string") {
input = LZUTF8.encodeUTF8(input);
}
else if (input == null || !(input instanceof Uint8Array)) {
callback(undefined, new TypeError("compressAsync: Invalid input argument, only 'string' and 'Uint8Array' are supported"));
return;
}
var sourceBlocks = LZUTF8.ArrayTools.splitByteArray(input, options.blockSize);
var compressedBlocks = [];
var compressBlocksStartingAt = function (index) {
if (index < sourceBlocks.length) {
var compressedBlock = void 0;
try {
compressedBlock = compressor.compressBlock(sourceBlocks[index]);
}
catch (e) {
callback(undefined, e);
return;
}
compressedBlocks.push(compressedBlock);
if (timer.getElapsedTime() <= 20) {
compressBlocksStartingAt(index + 1);
}
else {
LZUTF8.enqueueImmediate(function () { return compressBlocksStartingAt(index + 1); });
timer.restart();
}
}
else {
var joinedCompressedBlocks_1 = LZUTF8.ArrayTools.concatUint8Arrays(compressedBlocks);
LZUTF8.enqueueImmediate(function () {
var result;
try {
result = LZUTF8.CompressionCommon.encodeCompressedBytes(joinedCompressedBlocks_1, options.outputEncoding);
}
catch (e) {
callback(undefined, e);
return;
}
LZUTF8.enqueueImmediate(function () { return callback(result); });
});
}
};
LZUTF8.enqueueImmediate(function () { return compressBlocksStartingAt(0); });
};
AsyncCompressor.createCompressionStream = function () {
var compressor = new LZUTF8.Compressor();
var NodeStream = require("readable-stream");
var compressionStream = new NodeStream.Transform({ decodeStrings: true, highWaterMark: 65536 });
compressionStream._transform = function (data, encoding, done) {
var buffer;
try {
buffer = LZUTF8.BufferTools.uint8ArrayToBuffer(compressor.compressBlock(LZUTF8.BufferTools.bufferToUint8Array(data)));
}
catch (e) {
compressionStream.emit("error", e);
return;
}
compressionStream.push(buffer);
done();
};
return compressionStream;
};
return AsyncCompressor;
}());
LZUTF8.AsyncCompressor = AsyncCompressor;
})(LZUTF8 || (LZUTF8 = {}));
var LZUTF8;
(function (LZUTF8) {
var AsyncDecompressor = (function () {
function AsyncDecompressor() {
}
AsyncDecompressor.decompressAsync = function (input, options, callback) {
if (!callback)
throw new TypeError("decompressAsync: No callback argument given");
var timer = new LZUTF8.Timer();
try {
input = LZUTF8.CompressionCommon.decodeCompressedBytes(input, options.inputEncoding);
}
catch (e) {
callback(undefined, e);
return;
}
var decompressor = new LZUTF8.Decompressor();
var sourceBlocks = LZUTF8.ArrayTools.splitByteArray(input, options.blockSize);
var decompressedBlocks = [];
var decompressBlocksStartingAt = function (index) {
if (index < sourceBlocks.length) {
var decompressedBlock = void 0;
try {
decompressedBlock = decompressor.decompressBlock(sourceBlocks[index]);
}
catch (e) {
callback(undefined, e);
return;
}
decompressedBlocks.push(decompressedBlock);
if (timer.getElapsedTime() <= 20) {
decompressBlocksStartingAt(index + 1);
}
else {
LZUTF8.enqueueImmediate(function () { return decompressBlocksStartingAt(index + 1); });
timer.restart();
}
}
else {
var joinedDecompressedBlocks_1 = LZUTF8.ArrayTools.concatUint8Arrays(decompressedBlocks);
LZUTF8.enqueueImmediate(function () {
var result;
try {
result = LZUTF8.CompressionCommon.encodeDecompressedBytes(joinedDecompressedBlocks_1, options.outputEncoding);
}
catch (e) {
callback(undefined, e);
return;
}
LZUTF8.enqueueImmediate(function () { return callback(result); });
});
}
};
LZUTF8.enqueueImmediate(function () { return decompressBlocksStartingAt(0); });
};
AsyncDecompressor.createDecompressionStream = function () {
var decompressor = new LZUTF8.Decompressor();
var NodeStream = require("readable-stream");
var decompressionStream = new NodeStream.Transform({ decodeStrings: true, highWaterMark: 65536 });
decompressionStream._transform = function (data, encoding, done) {
var buffer;
try {
buffer = LZUTF8.BufferTools.uint8ArrayToBuffer(decompressor.decompressBlock(LZUTF8.BufferTools.bufferToUint8Array(data)));
}
catch (e) {
decompressionStream.emit("error", e);
return;
}
decompressionStream.push(buffer);
done();
};
return decompressionStream;
};
return AsyncDecompressor;
}());
LZUTF8.AsyncDecompressor = AsyncDecompressor;
})(LZUTF8 || (LZUTF8 = {}));
var LZUTF8;
(function (LZUTF8) {
var WebWorker;
(function (WebWorker) {
WebWorker.compressAsync = function (input, options, callback) {
if (options.inputEncoding == "ByteArray") {
if (!(input instanceof Uint8Array)) {
callback(undefined, new TypeError("compressAsync: input is not a Uint8Array"));
return;
}
}
var request = {
token: Math.random().toString(),
type: "compress",
data: input,
inputEncoding: options.inputEncoding,
outputEncoding: options.outputEncoding
};
var responseListener = function (e) {
var response = e.data;
if (!response || response.token != request.token)
return;
WebWorker.globalWorker.removeEventListener("message", responseListener);
if (response.type == "error")
callback(undefined, new Error(response.error));
else
callback(response.data);
};
WebWorker.globalWorker.addEventListener("message", responseListener);
WebWorker.globalWorker.postMessage(request, []);
};
WebWorker.decompressAsync = function (input, options, callback) {
var request = {
token: Math.random().toString(),
type: "decompress",
data: input,
inputEncoding: options.inputEncoding,
outputEncoding: options.outputEncoding
};
var responseListener = function (e) {
var response = e.data;
if (!response || response.token != request.token)
return;
WebWorker.globalWorker.removeEventListener("message", responseListener);
if (response.type == "error")
callback(undefined, new Error(response.error));
else
callback(response.data);
};
WebWorker.globalWorker.addEventListener("message", responseListener);
WebWorker.globalWorker.postMessage(request, []);
};
WebWorker.installWebWorkerIfNeeded = function () {
if (typeof self == "object" && self.document === undefined && self.addEventListener != undefined) {
self.addEventListener("message", function (e) {
var request = e.data;
if (request.type == "compress") {
var compressedData = void 0;
try {
compressedData = LZUTF8.compress(request.data, { outputEncoding: request.outputEncoding });
}
catch (e) {
self.postMessage({ token: request.token, type: "error", error: LZUTF8.createErrorMessage(e) }, []);
return;
}
var response = {
token: request.token,
type: "compressionResult",
data: compressedData,
encoding: request.outputEncoding,
};
if (response.data instanceof Uint8Array && navigator.appVersion.indexOf("MSIE 10") === -1)
self.postMessage(response, [response.data.buffer]);
else
self.postMessage(response, []);
}
else if (request.type == "decompress") {
var decompressedData = void 0;
try {
decompressedData = LZUTF8.decompress(request.data, { inputEncoding: request.inputEncoding, outputEncoding: request.outputEncoding });
}
catch (e) {
self.postMessage({ token: request.token, type: "error", error: LZUTF8.createErrorMessage(e) }, []);
return;
}
var response = {
token: request.token,
type: "decompressionResult",
data: decompressedData,
encoding: request.outputEncoding,
};
if (response.data instanceof Uint8Array && navigator.appVersion.indexOf("MSIE 10") === -1)
self.postMessage(response, [response.data.buffer]);
else
self.postMessage(response, []);
}
});
self.addEventListener("error", function (e) {
LZUTF8.log(LZUTF8.createErrorMessage(e.error, "Unexpected LZUTF8 WebWorker exception"));
});
}
};
WebWorker.createGlobalWorkerIfNeeded = function () {
if (WebWorker.globalWorker)
return true;
if (!LZUTF8.webWorkersAvailable())
return false;
if (!WebWorker.scriptURI && typeof document === "object") {
var scriptElement = document.getElementById("lzutf8");
if (scriptElement != null)
WebWorker.scriptURI = scriptElement.getAttribute("src") || undefined;
}
if (WebWorker.scriptURI) {
WebWorker.globalWorker = new Worker(WebWorker.scriptURI);
return true;
}
else {
return false;
}
};
WebWorker.terminate = function () {
if (WebWorker.globalWorker) {
WebWorker.globalWorker.terminate();
WebWorker.globalWorker = undefined;
}
};
})(WebWorker = LZUTF8.WebWorker || (LZUTF8.WebWorker = {}));
WebWorker.installWebWorkerIfNeeded();
})(LZUTF8 || (LZUTF8 = {}));
var LZUTF8;
(function (LZUTF8) {
var ArraySegment = (function () {
function ArraySegment(container, startPosition, length) {
this.container = container;
this.startPosition = startPosition;
this.length = length;
}
ArraySegment.prototype.get = function (index) {
return this.container[this.startPosition + index];
};
ArraySegment.prototype.getInReversedOrder = function (reverseIndex) {
return this.container[this.startPosition + this.length - 1 - reverseIndex];
};
ArraySegment.prototype.set = function (index, value) {
this.container[this.startPosition + index] = value;
};
return ArraySegment;
}());
LZUTF8.ArraySegment = ArraySegment;
})(LZUTF8 || (LZUTF8 = {}));
var LZUTF8;
(function (LZUTF8) {
var ArrayTools;
(function (ArrayTools) {
ArrayTools.copyElements = function (source, sourceIndex, destination, destinationIndex, count) {
while (count--)
destination[destinationIndex++] = source[sourceIndex++];
};
ArrayTools.zeroElements = function (collection, index, count) {
while (count--)
collection[index++] = 0;
};
ArrayTools.countNonzeroValuesInArray = function (array) {
var result = 0;
for (var i = 0; i < array.length; i++)
if (array[i])
result++;
return result;
};
ArrayTools.truncateStartingElements = function (array, truncatedLength) {
if (array.length <= truncatedLength)
throw new RangeError("truncateStartingElements: Requested length should be smaller than array length");
var sourcePosition = array.length - truncatedLength;
for (var i = 0; i < truncatedLength; i++)
array[i] = array[sourcePosition + i];
array.length = truncatedLength;
};
ArrayTools.doubleByteArrayCapacity = function (array) {
var newArray = new Uint8Array(array.length * 2);
newArray.set(array);
return newArray;
};
ArrayTools.concatUint8Arrays = function (arrays) {
var totalLength = 0;
for (var _i = 0, arrays_1 = arrays; _i < arrays_1.length; _i++) {
var array = arrays_1[_i];
totalLength += array.length;
}
var result = new Uint8Array(totalLength);
var offset = 0;
for (var _a = 0, arrays_2 = arrays; _a < arrays_2.length; _a++) {
var array = arrays_2[_a];
result.set(array, offset);
offset += array.length;
}
return result;
};
ArrayTools.splitByteArray = function (byteArray, maxPartLength) {
var result = [];
for (var offset = 0; offset < byteArray.length;) {
var blockLength = Math.min(maxPartLength, byteArray.length - offset);
result.push(byteArray.subarray(offset, offset + blockLength));
offset += blockLength;
}
return result;
};
})(ArrayTools = LZUTF8.ArrayTools || (LZUTF8.ArrayTools = {}));
})(LZUTF8 || (LZUTF8 = {}));
var LZUTF8;
(function (LZUTF8) {
var BufferTools;
(function (BufferTools) {
BufferTools.convertToUint8ArrayIfNeeded = function (input) {
if (typeof Buffer === "function" && Buffer.isBuffer(input))
return BufferTools.bufferToUint8Array(input);
else
return input;
};
BufferTools.uint8ArrayToBuffer = function (arr) {
if (Buffer.prototype instanceof Uint8Array) {
var arrClone = new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
Object["setPrototypeOf"](arrClone, Buffer.prototype);
return arrClone;
}
else {
var len = arr.length;
var buf = new Buffer(len);
for (var i = 0; i < len; i++)
buf[i] = arr[i];
return buf;
}
};
BufferTools.bufferToUint8Array = function (buf) {
if (Buffer.prototype instanceof Uint8Array) {
return new Uint8Array(buf["buffer"], buf["byteOffset"], buf["byteLength"]);
}
else {
var len = buf.length;
var arr = new Uint8Array(len);
for (var i = 0; i < len; i++)
arr[i] = buf[i];
return arr;
}
};
})(BufferTools = LZUTF8.BufferTools || (LZUTF8.BufferTools = {}));
})(LZUTF8 || (LZUTF8 = {}));
var LZUTF8;
(function (LZUTF8) {
var CompressionCommon;
(function (CompressionCommon) {
CompressionCommon.getCroppedBuffer = function (buffer, cropStartOffset, cropLength, additionalCapacity) {
if (additionalCapacity === void 0) { additionalCapacity = 0; }
var croppedBuffer = new Uint8Array(cropLength + additionalCapacity);
croppedBuffer.set(buffer.subarray(cropStartOffset, cropStartOffset + cropLength));
return croppedBuffer;
};
CompressionCommon.getCroppedAndAppendedByteArray = function (bytes, cropStartOffset, cropLength, byteArrayToAppend) {
return LZUTF8.ArrayTools.concatUint8Arrays([bytes.subarray(cropStartOffset, cropStartOffset + cropLength), byteArrayToAppend]);
};
CompressionCommon.detectCompressionSourceEncoding = function (input) {
if (input == null)
throw new TypeError("detectCompressionSourceEncoding: input is null or undefined");
if (typeof input === "string")
return "String";
else if (input instanceof Uint8Array || (typeof Buffer === "function" && Buffer.isBuffer(input)))
return "ByteArray";
else
throw new TypeError("detectCompressionSourceEncoding: input must be of type 'string', 'Uint8Array' or 'Buffer'");
};
CompressionCommon.encodeCompressedBytes = function (compressedBytes, outputEncoding) {
switch (outputEncoding) {
case "ByteArray":
return compressedBytes;
case "Buffer":
return LZUTF8.BufferTools.uint8ArrayToBuffer(compressedBytes);
case "Base64":
return LZUTF8.encodeBase64(compressedBytes);
case "BinaryString":
return LZUTF8.encodeBinaryString(compressedBytes);
case "StorageBinaryString":
return LZUTF8.encodeStorageBinaryString(compressedBytes);
default:
throw new TypeError("encodeCompressedBytes: invalid output encoding requested");
}
};
CompressionCommon.decodeCompressedBytes = function (compressedData, inputEncoding) {
if (inputEncoding == null)
throw new TypeError("decodeCompressedData: Input is null or undefined");
switch (inputEncoding) {
case "ByteArray":
case "Buffer":
var normalizedBytes = LZUTF8.BufferTools.convertToUint8ArrayIfNeeded(compressedData);
if (!(normalizedBytes instanceof Uint8Array))
throw new TypeError("decodeCompressedData: 'ByteArray' or 'Buffer' input type was specified but input is not a Uint8Array or Buffer");
return normalizedBytes;
case "Base64":
if (typeof compressedData !== "string")
throw new TypeError("decodeCompressedData: 'Base64' input type was specified but input is not a string");
return LZUTF8.decodeBase64(compressedData);
case "BinaryString":
if (typeof compressedData !== "string")
throw new TypeError("decodeCompressedData: 'BinaryString' input type was specified but input is not a string");
return LZUTF8.decodeBinaryString(compressedData);
case "StorageBinaryString":
if (typeof compressedData !== "string")
throw new TypeError("decodeCompressedData: 'StorageBinaryString' input type was specified but input is not a string");
return LZUTF8.decodeStorageBinaryString(compressedData);
default:
throw new TypeError("decodeCompressedData: invalid input encoding requested: '".concat(inputEncoding, "'"));
}
};
CompressionCommon.encodeDecompressedBytes = function (decompressedBytes, outputEncoding) {
switch (outputEncoding) {
case "String":
return LZUTF8.decodeUTF8(decompressedBytes);
case "ByteArray":
return decompressedBytes;
case "Buffer":
if (typeof Buffer !== "function")
throw new TypeError("encodeDecompressedBytes: a 'Buffer' type was specified but is not supported at the current envirnment");
return LZUTF8.BufferTools.uint8ArrayToBuffer(decompressedBytes);
default:
throw new TypeError("encodeDecompressedBytes: invalid output encoding requested");
}
};
})(CompressionCommon = LZUTF8.CompressionCommon || (LZUTF8.CompressionCommon = {}));
})(LZUTF8 || (LZUTF8 = {}));
var LZUTF8;
(function (LZUTF8) {
var EventLoop;
(function (EventLoop) {
var queuedFunctions = [];
var asyncFlushFunc;
EventLoop.enqueueImmediate = function (func) {
queuedFunctions.push(func);
if (queuedFunctions.length === 1) {
asyncFlushFunc();
}
};
EventLoop.initializeScheduler = function () {
var flush = function () {
for (var _i = 0, queuedFunctions_1 = queuedFunctions; _i < queuedFunctions_1.length; _i++) {
var func = queuedFunctions_1[_i];
try {
func.call(undefined);
}
catch (exception) {
LZUTF8.printExceptionAndStackTraceToConsole(exception, "enqueueImmediate exception");
}
}
queuedFunctions.length = 0;
};
if (LZUTF8.runningInNodeJS()) {
asyncFlushFunc = function () { return setImmediate(function () { return flush(); }); };
}
else if (typeof window === "object" && typeof window.addEventListener === "function" && typeof window.postMessage === "function") {
var token_1 = "enqueueImmediate-" + Math.random().toString();
window.addEventListener("message", function (event) {
if (event.data === token_1)
flush();
});
var targetOrigin_1;
if (LZUTF8.runningInNullOrigin())
targetOrigin_1 = '*';
else
targetOrigin_1 = window.location.href;
asyncFlushFunc = function () { return window.postMessage(token_1, targetOrigin_1); };
}
else if (typeof MessageChannel === "function" && typeof MessagePort === "function") {
var channel_1 = new MessageChannel();
channel_1.port1.onmessage = function () { return flush(); };
asyncFlushFunc = function () { return channel_1.port2.postMessage(0); };
}
else {
asyncFlushFunc = function () { return setTimeout(function () { return flush(); }, 0); };
}
};
EventLoop.initializeScheduler();
})(EventLoop = LZUTF8.EventLoop || (LZUTF8.EventLoop = {}));
LZUTF8.enqueueImmediate = function (func) { return EventLoop.enqueueImmediate(func); };
})(LZUTF8 || (LZUTF8 = {}));
var LZUTF8;
(function (LZUTF8) {
var ObjectTools;
(function (ObjectTools) {
ObjectTools.override = function (obj, newPropertyValues) {
return ObjectTools.extend(obj, newPropertyValues);
};
ObjectTools.extend = function (obj, newProperties) {
if (obj == null)
throw new TypeError("obj is null or undefined");
if (typeof obj !== "object")
throw new TypeError("obj is not an object");
if (newProperties == null)
newProperties = {};
if (typeof newProperties !== "object")
throw new TypeError("newProperties is not an object");
if (newProperties != null) {
for (var property in newProperties)
obj[property] = newProperties[property];
}
return obj;
};
})(ObjectTools = LZUTF8.ObjectTools || (LZUTF8.ObjectTools = {}));
})(LZUTF8 || (LZUTF8 = {}));
var LZUTF8;
(function (LZUTF8) {
LZUTF8.getRandomIntegerInRange = function (low, high) {
return low + Math.floor(Math.random() * (high - low));
};
LZUTF8.getRandomUTF16StringOfLength = function (length) {
var randomString = "";
for (var i = 0; i < length; i++) {
var randomCodePoint = void 0;
do {
randomCodePoint = LZUTF8.getRandomIntegerInRange(0, 0x10FFFF + 1);
} while (randomCodePoint >= 0xD800 && randomCodePoint <= 0xDFFF);
randomString += LZUTF8.Encoding.CodePoint.decodeToString(randomCodePoint);
}
return randomString;
};
})(LZUTF8 || (LZUTF8 = {}));
var LZUTF8;
(function (LZUTF8) {
var StringBuilder = (function () {
function StringBuilder(outputBufferCapacity) {
if (outputBufferCapacity === void 0) { outputBufferCapacity = 1024; }
this.outputBufferCapacity = outputBufferCapacity;
this.outputPosition = 0;
this.outputString = "";
this.outputBuffer = new Uint16Array(this.outputBufferCapacity);
}
StringBuilder.prototype.appendCharCode = function (charCode) {
this.outputBuffer[this.outputPosition++] = charCode;
if (this.outputPosition === this.outputBufferCapacity)
this.flushBufferToOutputString();
};
StringBuilder.prototype.appendCharCodes = function (charCodes) {
for (var i = 0, length_1 = charCodes.length; i < length_1; i++)
this.appendCharCode(charCodes[i]);
};
StringBuilder.prototype.appendString = function (str) {
for (var i = 0, length_2 = str.length; i < length_2; i++)
this.appendCharCode(str.charCodeAt(i));
};
StringBuilder.prototype.appendCodePoint = function (codePoint) {
if (codePoint <= 0xFFFF) {
this.appendCharCode(codePoint);
}
else if (codePoint <= 0x10FFFF) {
this.appendCharCode(0xD800 + ((codePoint - 0x10000) >>> 10));
this.appendCharCode(0xDC00 + ((codePoint - 0x10000) & 1023));
}
else
throw new Error("appendCodePoint: A code point of ".concat(codePoint, " cannot be encoded in UTF-16"));
};
StringBuilder.prototype.getOutputString = function () {
this.flushBufferToOutputString();
return this.outputString;
};
StringBuilder.prototype.flushBufferToOutputString = function () {
if (this.outputPosition === this.outputBufferCapacity)
this.outputString += String.fromCharCode.apply(null, this.outputBuffer);
else
this.outputString += String.fromCharCode.apply(null, this.outputBuffer.subarray(0, this.outputPosition));
this.outputPosition = 0;
};
return StringBuilder;
}());
LZUTF8.StringBuilder = StringBuilder;
})(LZUTF8 || (LZUTF8 = {}));
var LZUTF8;
(function (LZUTF8) {
var Timer = (function () {
function Timer() {
this.restart();
}
Timer.prototype.restart = function () {
this.startTime = Timer.getTimestamp();
};
Timer.prototype.getElapsedTime = function () {
return Timer.getTimestamp() - this.startTime;
};
Timer.prototype.getElapsedTimeAndRestart = function () {
var elapsedTime = this.getElapsedTime();
this.restart();
return elapsedTime;
};
Timer.prototype.logAndRestart = function (title, logToDocument) {
if (logToDocument === void 0) { logToDocument = true; }
var elapsedTime = this.getElapsedTime();
var message = "".concat(title, ": ").concat(elapsedTime.toFixed(3), "ms");
LZUTF8.log(message, logToDocument);
this.restart();
return elapsedTime;
};
Timer.getTimestamp = function () {
if (!this.timestampFunc)
this.createGlobalTimestampFunction();
return this.timestampFunc();
};
Timer.getMicrosecondTimestamp = function () {
return Math.floor(Timer.getTimestamp() * 1000);
};
Timer.createGlobalTimestampFunction = function () {
if (typeof process === "object" && typeof process.hrtime === "function") {
var baseTimestamp_1 = 0;
this.timestampFunc = function () {
var nodeTimeStamp = process.hrtime();
var millisecondTime = (nodeTimeStamp[0] * 1000) + (nodeTimeStamp[1] / 1000000);
return baseTimestamp_1 + millisecondTime;
};
baseTimestamp_1 = Date.now() - this.timestampFunc();
}
else if (typeof chrome === "object" && chrome.Interval) {
var baseTimestamp_2 = Date.now();
var chromeIntervalObject_1 = new chrome.Interval();
chromeIntervalObject_1.start();
this.timestampFunc = function () { return baseTimestamp_2 + chromeIntervalObject_1.microseconds() / 1000; };
}
else if (typeof performance === "object" && performance.now) {
var baseTimestamp_3 = Date.now() - performance.now();
this.timestampFunc = function () { return baseTimestamp_3 + performance.now(); };
}
else if (Date.now) {
this.timestampFunc = function () { return Date.now(); };
}
else {
this.timestampFunc = function () { return (new Date()).getTime(); };
}
};
return Timer;
}());
LZUTF8.Timer = Timer;
})(LZUTF8 || (LZUTF8 = {}));
var LZUTF8;
(function (LZUTF8) {
var Compressor = (function () {
function Compressor(useCustomHashTable) {
if (useCustomHashTable === void 0) { useCustomHashTable = true; }
this.MinimumSequenceLength = 4;
this.MaximumSequenceLength = 31;
this.MaximumMatchDistance = 32767;
this.PrefixHashTableSize = 65537;
this.inputBufferStreamOffset = 1;
if (useCustomHashTable && typeof Uint32Array == "function")
this.prefixHashTable = new LZUTF8.CompressorCustomHashTable(this.PrefixHashTableSize);
else
this.prefixHashTable = new LZUTF8.CompressorSimpleHashTable(this.PrefixHashTableSize);
}
Compressor.prototype.compressBlock = function (input) {
if (input === undefined || input === null)
throw new TypeError("compressBlock: undefined or null input received");
if (typeof input == "string")
input = LZUTF8.encodeUTF8(input);
input = LZUTF8.BufferTools.convertToUint8ArrayIfNeeded(input);
return this.compressUtf8Block(input);
};
Compressor.prototype.compressUtf8Block = function (utf8Bytes) {
if (!utf8Bytes || utf8Bytes.length == 0)
return new Uint8Array(0);
var bufferStartingReadOffset = this.cropAndAddNewBytesToInputBuffer(utf8Bytes);
var inputBuffer = this.inputBuffer;
var inputBufferLength = this.inputBuffer.length;
this.outputBuffer = new Uint8Array(utf8Bytes.length);
this.outputBufferPosition = 0;
var latestMatchEndPosition = 0;
for (var readPosition = bufferStartingReadOffset; readPosition < inputBufferLength; readPosition++) {
var inputValue = inputBuffer[readPosition];
var withinAMatchedRange = readPosition < latestMatchEndPosition;
if (readPosition > inputBufferLength - this.MinimumSequenceLength) {
if (!withinAMatchedRange)
this.outputRawByte(inputValue);
continue;
}
var targetBucketIndex = this.getBucketIndexForPrefix(readPosition);
if (!withinAMatchedRange) {
var matchLocator = this.findLongestMatch(readPosition, targetBucketIndex);
if (matchLocator != null) {
this.outputPointerBytes(matchLocator.length, matchLocator.distance);
latestMatchEndPosition = readPosition + matchLocator.length;
withinAMatchedRange = true;
}
}
if (!withinAMatchedRange)
this.outputRawByte(inputValue);
var inputStreamPosition = this.inputBufferStreamOffset + readPosition;
this.prefixHashTable.addValueToBucket(targetBucketIndex, inputStreamPosition);
}
return this.outputBuffer.subarray(0, this.outputBufferPosition);
};
Compressor.prototype.findLongestMatch = function (matchedSequencePosition, bucketIndex) {
var bucket = this.prefixHashTable.getArraySegmentForBucketIndex(bucketIndex, this.reusableArraySegmentObject);
if (bucket == null)
return null;
var input = this.inputBuffer;
var longestMatchDistance;
var longestMatchLength = 0;
for (var i = 0; i < bucket.length; i++) {
var testedSequencePosition = bucket.getInReversedOrder(i) - this.inputBufferStreamOffset;
var testedSequenceDistance = matchedSequencePosition - testedSequencePosition;
var lengthToSurpass = void 0;
if (longestMatchDistance === undefined)
lengthToSurpass = this.MinimumSequenceLength - 1;
else if (longestMatchDistance < 128 && testedSequenceDistance >= 128)
lengthToSurpass = longestMatchLength + (longestMatchLength >>> 1);
else
lengthToSurpass = longestMatchLength;
if (testedSequenceDistance > this.MaximumMatchDistance ||
lengthToSurpass >= this.MaximumSequenceLength ||
matchedSequencePosition + lengthToSurpass >= input.length)
break;
if (input[testedSequencePosition + lengthToSurpass] !== input[matchedSequencePosition + lengthToSurpass])
continue;
for (var offset = 0;; offset++) {
if (matchedSequencePosition + offset === input.length ||
input[testedSequencePosition + offset] !== input[matchedSequencePosition + offset]) {
if (offset > lengthToSurpass) {
longestMatchDistance = testedSequenceDistance;
longestMatchLength = offset;
}
break;
}
else if (offset === this.MaximumSequenceLength)
return { distance: testedSequenceDistance, length: this.MaximumSequenceLength };
}
}
if (longestMatchDistance !== undefined)
return { distance: longestMatchDistance, length: longestMatchLength };
else
return null;
};
Compressor.prototype.getBucketIndexForPrefix = function (startPosition) {
return (this.inputBuffer[startPosition] * 7880599 +
this.inputBuffer[startPosition + 1] * 39601 +
this.inputBuffer[startPosition + 2] * 199 +
this.inputBuffer[startPosition + 3]) % this.PrefixHashTableSize;
};
Compressor.prototype.outputPointerBytes = function (length, distance) {
if (distance < 128) {
this.outputRawByte(192 | length);
this.outputRawByte(distance);
}
else {
this.outputRawByte(224 | length);
this.outputRawByte(distance >>> 8);
this.outputRawByte(distance & 255);
}
};
Compressor.prototype.outputRawByte = function (value) {
this.outputBuffer[this.outputBufferPosition++] = value;
};
Compressor.prototype.cropAndAddNewBytesToInputBuffer = function (newInput) {
if (this.inputBuffer === undefined) {
this.inputBuffer = newInput;
return 0;
}
else {
var cropLength = Math.min(this.inputBuffer.length, this.MaximumMatchDistance);
var cropStartOffset = this.inputBuffer.length - cropLength;
this.inputBuffer = LZUTF8.CompressionCommon.getCroppedAndAppendedByteArray(this.inputBuffer, cropStartOffset, cropLength, newInput);
this.inputBufferStreamOffset += cropStartOffset;
return cropLength;
}
};
return Compressor;
}());
LZUTF8.Compressor = Compressor;
})(LZUTF8 || (LZUTF8 = {}));
var LZUTF8;
(function (LZUTF8) {
var CompressorCustomHashTable = (function () {
function CompressorCustomHashTable(bucketCount) {
this.minimumBucketCapacity = 4;
this.maximumBucketCapacity = 64;
this.bucketLocators = new Uint32Array(bucketCount * 2);
this.storage = new Uint32Array(bucketCount * 2);
this.storageIndex = 1;
}
CompressorCustomHashTable.prototype.addValueToBucket = function (bucketIndex, valueToAdd) {
bucketIndex <<= 1;
if (this.storageIndex >= (this.storage.length >>> 1))
this.compact();
var startPosition = this.bucketLocators[bucketIndex];
var length;
if (startPosition === 0) {
startPosition = this.storageIndex;
length = 1;
this.storage[this.storageIndex] = valueToAdd;
this.storageIndex += this.minimumBucketCapacity;
}
else {
length = this.bucketLocators[bucketIndex + 1];
if (length === this.maximumBucketCapacity - 1)
length = this.truncateBucketToNewerElements(startPosition, length, this.maximumBucketCapacity / 2);
var endPosition = startPosition + length;
if (this.storage[endPosition] === 0) {
this.storage[endPosition] = valueToAdd;
if (endPosition === this.storageIndex)
this.storageIndex += length;
}
else {
LZUTF8.ArrayTools.copyElements(this.storage, startPosition, this.storage, this.storageIndex, length);
startPosition = this.storageIndex;
this.storageIndex += length;
this.storage[this.storageIndex++] = valueToAdd;
this.storageIndex += length;
}
length++;
}
this.bucketLocators[bucketIndex] = startPosition;
this.bucketLocators[bucketIndex + 1] = length;
};
CompressorCustomHashTable.prototype.truncateBucketToNewerElements = function (startPosition, bucketLength, truncatedBucketLength) {
var sourcePosition = startPosition + bucketLength - truncatedBucketLength;
LZUTF8.ArrayTools.copyElements(this.storage, sourcePosition, this.storage, startPosition, truncatedBucketLength);
LZUTF8.ArrayTools.zeroElements(this.storage, startPosition + truncatedBucketLength, bucketLength - truncatedBucketLength);
return truncatedBucketLength;
};
CompressorCustomHashTable.prototype.compact = function () {
var oldBucketLocators = this.bucketLocators;
var oldStorage = this.storage;
this.bucketLocators = new Uint32Array(this.bucketLocators.length);
this.storageIndex = 1;
for (var bucketIndex = 0; bucketIndex < oldBucketLocators.length; bucketIndex += 2) {
var length_3 = oldBucketLocators[bucketIndex + 1];
if (length_3 === 0)
continue;
this.bucketLocators[bucketIndex] = this.storageIndex;
this.bucketLocators[bucketIndex + 1] = length_3;
this.storageIndex += Math.max(Math.min(length_3 * 2, this.maximumBucketCapacity), this.minimumBucketCapacity);
}
this.storage = new Uint32Array(this.storageIndex * 8);
for (var bucketIndex = 0; bucketIndex < oldBucketLocators.length; bucketIndex += 2) {
var sourcePosition = oldBucketLocators[bucketIndex];
if (sourcePosition === 0)
continue;
var destPosition = this.bucketLocators[bucketIndex];
var length_4 = this.bucketLocators[bucketIndex + 1];
LZUTF8.ArrayTools.copyElements(oldStorage, sourcePosition, this.storage, destPosition, length_4);
}
};
CompressorCustomHashTable.prototype.getArraySegmentForBucketIndex = function (bucketIndex, outputObject) {
bucketIndex <<= 1;
var startPosition = this.bucketLocators[bucketIndex];
if (startPosition === 0)
return null;
if (outputObject === undefined)
outputObject = new LZUTF8.ArraySegment(this.storage, startPosition, this.bucketLocators[bucketIndex + 1]);
return outputObject;
};
CompressorCustomHashTable.prototype.getUsedBucketCount = function () {
return Math.floor(LZUTF8.ArrayTools.countNonzeroValuesInArray(this.bucketLocators) / 2);
};
CompressorCustomHashTable.prototype.getTotalElementCount = function () {
var result = 0;
for (var i = 0; i < this.bucketLocators.length; i += 2)
result += this.bucketLocators[i + 1];
return result;
};
return CompressorCustomHashTable;
}());
LZUTF8.CompressorCustomHashTable = CompressorCustomHashTable;
})(LZUTF8 || (LZUTF8 = {}));
var LZUTF8;
(function (LZUTF8) {
var CompressorSimpleHashTable = (function () {
function CompressorSimpleHashTable(size) {
this.maximumBucketCapacity = 64;
this.buckets = new Array(size);
}
CompressorSimpleHashTable.prototype.addValueToBucket = function (bucketIndex, valueToAdd) {
var bucket = this.buckets[bucketIndex];
if (bucket === undefined) {
this.buckets[bucketIn