-
Notifications
You must be signed in to change notification settings - Fork 162
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
90 changed files
with
6,450 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
node_modules/ | ||
dist/ | ||
dist.*/ | ||
# dist/ | ||
# dist.*/ | ||
build/ | ||
.nyc_output/ | ||
coverage/ | ||
|
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { utf8DecodeJs } from "./utils/utf8.mjs"; | ||
var DEFAULT_MAX_KEY_LENGTH = 16; | ||
var DEFAULT_MAX_LENGTH_PER_KEY = 16; | ||
var CachedKeyDecoder = /** @class */ (function () { | ||
function CachedKeyDecoder(maxKeyLength, maxLengthPerKey) { | ||
if (maxKeyLength === void 0) { maxKeyLength = DEFAULT_MAX_KEY_LENGTH; } | ||
if (maxLengthPerKey === void 0) { maxLengthPerKey = DEFAULT_MAX_LENGTH_PER_KEY; } | ||
this.maxKeyLength = maxKeyLength; | ||
this.maxLengthPerKey = maxLengthPerKey; | ||
this.hit = 0; | ||
this.miss = 0; | ||
// avoid `new Array(N)`, which makes a sparse array, | ||
// because a sparse array is typically slower than a non-sparse array. | ||
this.caches = []; | ||
for (var i = 0; i < this.maxKeyLength; i++) { | ||
this.caches.push([]); | ||
} | ||
} | ||
CachedKeyDecoder.prototype.canBeCached = function (byteLength) { | ||
return byteLength > 0 && byteLength <= this.maxKeyLength; | ||
}; | ||
CachedKeyDecoder.prototype.find = function (bytes, inputOffset, byteLength) { | ||
var records = this.caches[byteLength - 1]; | ||
FIND_CHUNK: for (var _i = 0, records_1 = records; _i < records_1.length; _i++) { | ||
var record = records_1[_i]; | ||
var recordBytes = record.bytes; | ||
for (var j = 0; j < byteLength; j++) { | ||
if (recordBytes[j] !== bytes[inputOffset + j]) { | ||
continue FIND_CHUNK; | ||
} | ||
} | ||
return record.str; | ||
} | ||
return null; | ||
}; | ||
CachedKeyDecoder.prototype.store = function (bytes, value) { | ||
var records = this.caches[bytes.length - 1]; | ||
var record = { bytes: bytes, str: value }; | ||
if (records.length >= this.maxLengthPerKey) { | ||
// `records` are full! | ||
// Set `record` to an arbitrary position. | ||
records[(Math.random() * records.length) | 0] = record; | ||
} | ||
else { | ||
records.push(record); | ||
} | ||
}; | ||
CachedKeyDecoder.prototype.decode = function (bytes, inputOffset, byteLength) { | ||
var cachedValue = this.find(bytes, inputOffset, byteLength); | ||
if (cachedValue != null) { | ||
this.hit++; | ||
return cachedValue; | ||
} | ||
this.miss++; | ||
var str = utf8DecodeJs(bytes, inputOffset, byteLength); | ||
// Ensure to copy a slice of bytes because the byte may be NodeJS Buffer and Buffer#slice() returns a reference to its internal ArrayBuffer. | ||
var slicedCopyOfBytes = Uint8Array.prototype.slice.call(bytes, inputOffset, inputOffset + byteLength); | ||
this.store(slicedCopyOfBytes, str); | ||
return str; | ||
}; | ||
return CachedKeyDecoder; | ||
}()); | ||
export { CachedKeyDecoder }; | ||
//# sourceMappingURL=CachedKeyDecoder.mjs.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var DecodeError = /** @class */ (function (_super) { | ||
__extends(DecodeError, _super); | ||
function DecodeError(message) { | ||
var _this = _super.call(this, message) || this; | ||
// fix the prototype chain in a cross-platform way | ||
var proto = Object.create(DecodeError.prototype); | ||
Object.setPrototypeOf(_this, proto); | ||
Object.defineProperty(_this, "name", { | ||
configurable: true, | ||
enumerable: false, | ||
value: DecodeError.name, | ||
}); | ||
return _this; | ||
} | ||
return DecodeError; | ||
}(Error)); | ||
export { DecodeError }; | ||
//# sourceMappingURL=DecodeError.mjs.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.