2018-08-11 08:39:43 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
|
|
|
Copyright (C) 2018-present Raymond Hill
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* global lz4BlockCodec */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2021-07-25 08:55:35 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-28 17:48:38 -06:00
|
|
|
import µb from './background.js';
|
2021-07-25 08:55:35 -06:00
|
|
|
|
2018-08-11 08:39:43 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
Experimental support for storage compression.
|
|
|
|
|
|
|
|
For background information on the topic, see:
|
|
|
|
https://github.com/uBlockOrigin/uBlock-issues/issues/141#issuecomment-407737186
|
|
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
let lz4CodecInstance;
|
|
|
|
let pendingInitialization;
|
|
|
|
let textEncoder, textDecoder;
|
|
|
|
let ttlCount = 0;
|
|
|
|
let ttlTimer;
|
2018-08-22 06:13:10 -06:00
|
|
|
let ttlDelay = 60000;
|
2018-08-11 08:39:43 -06:00
|
|
|
|
2019-02-01 06:20:43 -07:00
|
|
|
const init = function() {
|
2021-07-28 17:48:38 -06:00
|
|
|
ttlDelay = µb.hiddenSettings.autoUpdateAssetFetchPeriod * 1000 + 15000;
|
2018-08-11 08:39:43 -06:00
|
|
|
if ( lz4CodecInstance === null ) {
|
|
|
|
return Promise.resolve(null);
|
|
|
|
}
|
|
|
|
if ( lz4CodecInstance !== undefined ) {
|
|
|
|
return Promise.resolve(lz4CodecInstance);
|
|
|
|
}
|
|
|
|
if ( pendingInitialization === undefined ) {
|
2018-11-16 08:19:06 -07:00
|
|
|
let flavor;
|
2021-07-28 17:48:38 -06:00
|
|
|
if ( µb.hiddenSettings.disableWebAssembly === true ) {
|
2018-11-16 08:19:06 -07:00
|
|
|
flavor = 'js';
|
|
|
|
}
|
|
|
|
pendingInitialization = lz4BlockCodec.createInstance(flavor)
|
2018-08-11 08:39:43 -06:00
|
|
|
.then(instance => {
|
|
|
|
lz4CodecInstance = instance;
|
|
|
|
pendingInitialization = undefined;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return pendingInitialization;
|
|
|
|
};
|
|
|
|
|
|
|
|
// We can't shrink memory usage of lz4 codec instances, and in the
|
|
|
|
// current case memory usage can grow to a significant amount given
|
|
|
|
// that a single contiguous memory buffer is required to accommodate
|
|
|
|
// both input and output data. Thus a time-to-live implementation
|
|
|
|
// which will cause the wasm instance to be forgotten after enough
|
|
|
|
// time elapse without the instance being used.
|
|
|
|
|
2019-02-01 06:20:43 -07:00
|
|
|
const destroy = function() {
|
2018-10-01 08:14:06 -06:00
|
|
|
//if ( lz4CodecInstance !== undefined ) {
|
|
|
|
// console.info(
|
|
|
|
// 'uBO: freeing lz4-block-codec instance (%s KB)',
|
|
|
|
// lz4CodecInstance.bytesInUse() >>> 10
|
|
|
|
// );
|
|
|
|
//}
|
2018-08-11 08:39:43 -06:00
|
|
|
lz4CodecInstance = undefined;
|
|
|
|
textEncoder = textDecoder = undefined;
|
|
|
|
ttlCount = 0;
|
|
|
|
ttlTimer = undefined;
|
|
|
|
};
|
|
|
|
|
2019-02-01 06:20:43 -07:00
|
|
|
const ttlManage = function(count) {
|
2018-08-11 08:39:43 -06:00
|
|
|
if ( ttlTimer !== undefined ) {
|
|
|
|
clearTimeout(ttlTimer);
|
|
|
|
ttlTimer = undefined;
|
|
|
|
}
|
|
|
|
ttlCount += count;
|
|
|
|
if ( ttlCount > 0 ) { return; }
|
|
|
|
if ( lz4CodecInstance === null ) { return; }
|
|
|
|
ttlTimer = vAPI.setTimeout(destroy, ttlDelay);
|
|
|
|
};
|
|
|
|
|
2020-08-16 09:53:55 -06:00
|
|
|
const encodeValue = function(dataIn) {
|
2018-08-11 08:39:43 -06:00
|
|
|
if ( !lz4CodecInstance ) { return; }
|
2018-10-01 08:14:06 -06:00
|
|
|
//let t0 = window.performance.now();
|
2018-08-11 08:39:43 -06:00
|
|
|
if ( textEncoder === undefined ) {
|
|
|
|
textEncoder = new TextEncoder();
|
|
|
|
}
|
2020-08-16 09:53:55 -06:00
|
|
|
const inputArray = textEncoder.encode(dataIn);
|
|
|
|
const inputSize = inputArray.byteLength;
|
|
|
|
const outputArray = lz4CodecInstance.encodeBlock(inputArray, 8);
|
2018-08-21 09:07:34 -06:00
|
|
|
if ( outputArray instanceof Uint8Array === false ) { return; }
|
2018-08-11 08:39:43 -06:00
|
|
|
outputArray[0] = 0x18;
|
|
|
|
outputArray[1] = 0x4D;
|
|
|
|
outputArray[2] = 0x22;
|
|
|
|
outputArray[3] = 0x04;
|
|
|
|
outputArray[4] = (inputSize >>> 0) & 0xFF;
|
|
|
|
outputArray[5] = (inputSize >>> 8) & 0xFF;
|
|
|
|
outputArray[6] = (inputSize >>> 16) & 0xFF;
|
|
|
|
outputArray[7] = (inputSize >>> 24) & 0xFF;
|
2018-10-01 08:14:06 -06:00
|
|
|
//console.info(
|
|
|
|
// 'uBO: [%s] compressed %d KB => %d KB (%s%%) in %s ms',
|
|
|
|
// inputArray.byteLength >> 10,
|
|
|
|
// outputArray.byteLength >> 10,
|
|
|
|
// (outputArray.byteLength / inputArray.byteLength * 100).toFixed(0),
|
|
|
|
// (window.performance.now() - t0).toFixed(1)
|
|
|
|
//);
|
2018-08-11 08:39:43 -06:00
|
|
|
return outputArray;
|
|
|
|
};
|
|
|
|
|
2020-08-16 09:53:55 -06:00
|
|
|
const decodeValue = function(inputArray) {
|
2018-08-11 08:39:43 -06:00
|
|
|
if ( !lz4CodecInstance ) { return; }
|
2018-10-01 08:14:06 -06:00
|
|
|
//let t0 = window.performance.now();
|
2018-08-11 08:39:43 -06:00
|
|
|
if (
|
|
|
|
inputArray[0] !== 0x18 || inputArray[1] !== 0x4D ||
|
|
|
|
inputArray[2] !== 0x22 || inputArray[3] !== 0x04
|
|
|
|
) {
|
2020-08-16 09:53:55 -06:00
|
|
|
console.error('decodeValue: invalid input array');
|
2018-08-11 08:39:43 -06:00
|
|
|
return;
|
|
|
|
}
|
2020-08-16 09:53:55 -06:00
|
|
|
const outputSize =
|
2018-08-11 08:39:43 -06:00
|
|
|
(inputArray[4] << 0) | (inputArray[5] << 8) |
|
|
|
|
(inputArray[6] << 16) | (inputArray[7] << 24);
|
2020-08-16 09:53:55 -06:00
|
|
|
const outputArray = lz4CodecInstance.decodeBlock(inputArray, 8, outputSize);
|
2018-08-21 09:07:34 -06:00
|
|
|
if ( outputArray instanceof Uint8Array === false ) { return; }
|
2018-08-11 08:39:43 -06:00
|
|
|
if ( textDecoder === undefined ) {
|
|
|
|
textDecoder = new TextDecoder();
|
|
|
|
}
|
2020-08-16 09:53:55 -06:00
|
|
|
const s = textDecoder.decode(outputArray);
|
2018-10-01 08:14:06 -06:00
|
|
|
//console.info(
|
|
|
|
// 'uBO: [%s] decompressed %d KB => %d KB (%s%%) in %s ms',
|
|
|
|
// inputArray.byteLength >>> 10,
|
|
|
|
// outputSize >>> 10,
|
|
|
|
// (inputArray.byteLength / outputSize * 100).toFixed(0),
|
|
|
|
// (window.performance.now() - t0).toFixed(1)
|
|
|
|
//);
|
2020-08-16 09:53:55 -06:00
|
|
|
return s;
|
2018-08-11 08:39:43 -06:00
|
|
|
};
|
|
|
|
|
2021-07-28 17:48:38 -06:00
|
|
|
const lz4Codec = {
|
2020-08-16 09:53:55 -06:00
|
|
|
// Arguments:
|
|
|
|
// dataIn: must be a string
|
|
|
|
// Returns:
|
|
|
|
// A Uint8Array, or the input string as is if compression is not
|
|
|
|
// possible.
|
|
|
|
encode: async function(dataIn, serialize = undefined) {
|
2018-08-12 05:18:34 -06:00
|
|
|
if ( typeof dataIn !== 'string' || dataIn.length < 4096 ) {
|
2020-08-16 09:53:55 -06:00
|
|
|
return dataIn;
|
2018-08-11 08:39:43 -06:00
|
|
|
}
|
|
|
|
ttlManage(1);
|
2020-08-16 09:53:55 -06:00
|
|
|
await init();
|
|
|
|
let dataOut = encodeValue(dataIn);
|
|
|
|
ttlManage(-1);
|
|
|
|
if ( serialize instanceof Function ) {
|
|
|
|
dataOut = await serialize(dataOut);
|
|
|
|
}
|
|
|
|
return dataOut || dataIn;
|
2018-08-11 08:39:43 -06:00
|
|
|
},
|
2020-08-16 09:53:55 -06:00
|
|
|
// Arguments:
|
|
|
|
// dataIn: must be a Uint8Array
|
|
|
|
// Returns:
|
|
|
|
// A string, or the input argument as is if decompression is not
|
|
|
|
// possible.
|
|
|
|
decode: async function(dataIn, deserialize = undefined) {
|
|
|
|
if ( deserialize instanceof Function ) {
|
|
|
|
dataIn = await deserialize(dataIn);
|
|
|
|
}
|
|
|
|
if ( dataIn instanceof Uint8Array === false ) {
|
|
|
|
return dataIn;
|
2018-08-11 08:39:43 -06:00
|
|
|
}
|
|
|
|
ttlManage(1);
|
2020-08-16 09:53:55 -06:00
|
|
|
await init();
|
|
|
|
const dataOut = decodeValue(dataIn);
|
|
|
|
ttlManage(-1);
|
|
|
|
return dataOut || dataIn;
|
2018-12-05 17:18:20 -07:00
|
|
|
},
|
|
|
|
relinquish: function() {
|
|
|
|
ttlDelay = 1;
|
|
|
|
ttlManage(0);
|
|
|
|
},
|
2018-08-11 08:39:43 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2021-07-28 17:48:38 -06:00
|
|
|
|
|
|
|
export default lz4Codec;
|
|
|
|
|
|
|
|
/******************************************************************************/
|