-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
44 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,56 @@ | ||
'use strict' | ||
import oozFactory from './build/ooz.js'; | ||
|
||
var oozModule = require('./build/ooz.js'); | ||
const OOZ_SAFE_SPACE = 64; | ||
|
||
var OOZ_SAFE_SPACE = 64; | ||
|
||
var module_ = null; | ||
var lastDecompressionPtr_ = 0; | ||
|
||
function load () { | ||
if (!module_) { | ||
module_ = oozModule(); | ||
} | ||
return module_; | ||
} | ||
const oozInst = await oozFactory(); | ||
let lastDecompressionPtr_ = 0; | ||
|
||
/** | ||
* @param {Uint8Array} data | ||
* @param {number} rawSize | ||
* @returns {Promise<Uint8Array>} | ||
* @param {Uint8Array} data | ||
* @param {number} rawSize | ||
* @returns {Uint8Array} | ||
*/ | ||
function decompressUnsafe (data, rawSize) { | ||
return load().then(module => { | ||
if (lastDecompressionPtr_) { | ||
module._free(lastDecompressionPtr_); | ||
lastDecompressionPtr_ = 0; | ||
} | ||
export function decompressUnsafe (data, rawSize) { | ||
if (lastDecompressionPtr_) { | ||
oozInst._free(lastDecompressionPtr_); | ||
lastDecompressionPtr_ = 0; | ||
} | ||
|
||
var compressedPtr = module._malloc(data.byteLength); | ||
module.HEAPU8.set(data, compressedPtr); | ||
const compressedPtr = oozInst._malloc(data.byteLength); | ||
oozInst.HEAPU8.set(data, compressedPtr); | ||
|
||
var decompressedPtr = module._malloc(rawSize + OOZ_SAFE_SPACE); | ||
lastDecompressionPtr_ = decompressedPtr; | ||
const decompressedPtr = oozInst._malloc(rawSize + OOZ_SAFE_SPACE); | ||
lastDecompressionPtr_ = decompressedPtr; | ||
|
||
var res = module._Kraken_Decompress( | ||
compressedPtr, data.byteLength, | ||
decompressedPtr, rawSize | ||
); | ||
const res = oozInst._Kraken_Decompress( | ||
compressedPtr, data.byteLength, | ||
decompressedPtr, rawSize | ||
); | ||
|
||
module._free(compressedPtr); | ||
oozInst._free(compressedPtr); | ||
|
||
if (res < 0) { | ||
throw new Error('Failed to decode'); | ||
} | ||
if (res !== rawSize) { | ||
throw new Error('Decompresed size is different from expected'); | ||
} | ||
if (res < 0) { | ||
throw new Error('Failed to decode'); | ||
} | ||
if (res !== rawSize) { | ||
throw new Error('Decompresed size is different from expected'); | ||
} | ||
|
||
return module.HEAPU8.subarray(decompressedPtr, decompressedPtr + rawSize); | ||
}) | ||
return oozInst.HEAPU8.subarray(decompressedPtr, decompressedPtr + rawSize); | ||
} | ||
|
||
/** | ||
* @param {Uint8Array} data | ||
* @param {number} rawSize | ||
* @returns {Promise<Uint8Array>} | ||
* @param {Uint8Array} data | ||
* @param {number} rawSize | ||
* @returns {Uint8Array} | ||
*/ | ||
function decompress (data, rawSize) { | ||
return decompressUnsafe(data, rawSize).then(decompressed => { | ||
const decompressedCopy = new Uint8Array(decompressed.byteLength); | ||
decompressedCopy.set(decompressed); | ||
export function decompress (data, rawSize) { | ||
const decompressed = decompressUnsafe(data, rawSize); | ||
const decompressedCopy = new Uint8Array(decompressed.byteLength); | ||
decompressedCopy.set(decompressed); | ||
|
||
module._free(lastDecompressionPtr_); | ||
lastDecompressionPtr_ = 0; | ||
oozInst._free(lastDecompressionPtr_); | ||
lastDecompressionPtr_ = 0; | ||
|
||
return decompressedCopy; | ||
}); | ||
return decompressedCopy; | ||
} | ||
|
||
exports.load = load; | ||
exports.decompressUnsafe = decompressUnsafe; | ||
exports.decompress = decompress; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters