gecko-dev/toolkit/components/lz4/lz4.js
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16750

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

153 lines
5.8 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
var SharedAll;
if (typeof Components != "undefined") {
SharedAll = {};
ChromeUtils.import("resource://gre/modules/osfile/osfile_shared_allthreads.jsm", SharedAll);
var {Primitives} = ChromeUtils.import("resource://gre/modules/lz4_internal.js");
var {ctypes} = ChromeUtils.import("resource://gre/modules/ctypes.jsm");
this.EXPORTED_SYMBOLS = [
"Lz4",
];
this.exports = {};
} else if (typeof module != "undefined" && typeof require != "undefined") {
/* eslint-env commonjs */
SharedAll = require("resource://gre/modules/osfile/osfile_shared_allthreads.jsm");
Primitives = require("resource://gre/modules/lz4_internal.js");
ctypes = self.ctypes;
} else {
throw new Error("Please load this module with Component.utils.import or with require()");
}
const MAGIC_NUMBER = new Uint8Array([109, 111, 122, 76, 122, 52, 48, 0]); // "mozLz40\0"
const BYTES_IN_SIZE_HEADER = ctypes.uint32_t.size;
const HEADER_SIZE = MAGIC_NUMBER.byteLength + BYTES_IN_SIZE_HEADER;
/**
* An error during (de)compression
*
* @param {string} operation The name of the operation ("compress", "decompress")
* @param {string} reason A reason to be used when matching errors. Must start
* with "because", e.g. "becauseInvalidContent".
* @param {string} message A human-readable message.
*/
function LZError(operation, reason, message) {
SharedAll.OSError.call(this);
this.operation = operation;
this[reason] = true;
this.message = message;
}
LZError.prototype = Object.create(SharedAll.OSError);
LZError.prototype.toString = function toString() {
return this.message;
};
exports.Error = LZError;
/**
* Compress a block to a form suitable for writing to disk.
*
* Compatibility note: For the moment, we are basing our code on lz4
* 1.3, which does not specify a *file* format. Therefore, we define
* our own format. Once lz4 defines a complete file format, we will
* migrate both |compressFileContent| and |decompressFileContent| to this file
* format. For backwards-compatibility, |decompressFileContent| will however
* keep the ability to decompress files provided with older versions of
* |compressFileContent|.
*
* Compressed files have the following layout:
*
* | MAGIC_NUMBER (8 bytes) | content size (uint32_t, little endian) | content, as obtained from lz4_compress |
*
* @param {TypedArray|void*} buffer The buffer to write to the disk.
* @param {object=} options An object that may contain the following fields:
* - {number} bytes The number of bytes to read from |buffer|. If |buffer|
* is an |ArrayBuffer|, |bytes| defaults to |buffer.byteLength|. If
* |buffer| is a |void*|, |bytes| MUST be provided.
* @return {Uint8Array} An array of bytes suitable for being written to the
* disk.
*/
function compressFileContent(array, options = {}) {
// Prepare the output array
let inputBytes;
if (SharedAll.isTypedArray(array) && !(options && "bytes" in options)) {
inputBytes = array.byteLength;
} else if (options && options.bytes) {
inputBytes = options.bytes;
} else {
throw new TypeError("compressFileContent requires a size");
}
let maxCompressedSize = Primitives.maxCompressedSize(inputBytes);
let outputArray = new Uint8Array(HEADER_SIZE + maxCompressedSize);
// Compress to output array
let payload = new Uint8Array(outputArray.buffer, outputArray.byteOffset + HEADER_SIZE);
let compressedSize = Primitives.compress(array, inputBytes, payload);
// Add headers
outputArray.set(MAGIC_NUMBER);
let view = new DataView(outputArray.buffer);
view.setUint32(MAGIC_NUMBER.byteLength, inputBytes, true);
return new Uint8Array(outputArray.buffer, 0, HEADER_SIZE + compressedSize);
}
exports.compressFileContent = compressFileContent;
function decompressFileContent(array, options = {}) {
let bytes = SharedAll.normalizeBufferArgs(array, options.bytes || null);
if (bytes < HEADER_SIZE) {
throw new LZError("decompress", "becauseLZNoHeader",
`Buffer is too short (no header) - Data: ${ options.path || array }`);
}
// Read headers
let expectMagicNumber = new DataView(array.buffer, 0, MAGIC_NUMBER.byteLength);
for (let i = 0; i < MAGIC_NUMBER.byteLength; ++i) {
if (expectMagicNumber.getUint8(i) != MAGIC_NUMBER[i]) {
throw new LZError("decompress", "becauseLZWrongMagicNumber",
`Invalid header (no magic number) - Data: ${ options.path || array }`);
}
}
let sizeBuf = new DataView(array.buffer, MAGIC_NUMBER.byteLength, BYTES_IN_SIZE_HEADER);
let expectDecompressedSize =
sizeBuf.getUint8(0) +
(sizeBuf.getUint8(1) << 8) +
(sizeBuf.getUint8(2) << 16) +
(sizeBuf.getUint8(3) << 24);
if (expectDecompressedSize == 0) {
// The underlying algorithm cannot handle a size of 0
return new Uint8Array(0);
}
// Prepare the input buffer
let inputData = new DataView(array.buffer, HEADER_SIZE);
// Prepare the output buffer
let outputBuffer = new Uint8Array(expectDecompressedSize);
let decompressedBytes = (new SharedAll.Type.size_t.implementation(0));
// Decompress
let success = Primitives.decompress(inputData, bytes - HEADER_SIZE,
outputBuffer, outputBuffer.byteLength,
decompressedBytes.address());
if (!success) {
throw new LZError("decompress", "becauseLZInvalidContent",
`Invalid content: Decompression stopped at ${decompressedBytes.value} - Data: ${ options.path || array }`);
}
return new Uint8Array(outputBuffer.buffer, outputBuffer.byteOffset, decompressedBytes.value);
}
exports.decompressFileContent = decompressFileContent;
if (typeof Components != "undefined") {
this.Lz4 = {
compressFileContent,
decompressFileContent,
};
}