69 lines
2.6 KiB
JavaScript
69 lines
2.6 KiB
JavaScript
|
/*---------------------------------------------------------------------------------------------
|
||
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||
|
*--------------------------------------------------------------------------------------------*/
|
||
|
import { Lazy } from './lazy.js';
|
||
|
const hasBuffer = (typeof Buffer !== 'undefined');
|
||
|
const indexOfTable = new Lazy(() => new Uint8Array(256));
|
||
|
let textDecoder;
|
||
|
export class VSBuffer {
|
||
|
/**
|
||
|
* When running in a nodejs context, if `actual` is not a nodejs Buffer, the backing store for
|
||
|
* the returned `VSBuffer` instance might use a nodejs Buffer allocated from node's Buffer pool,
|
||
|
* which is not transferrable.
|
||
|
*/
|
||
|
static wrap(actual) {
|
||
|
if (hasBuffer && !(Buffer.isBuffer(actual))) {
|
||
|
// https://nodejs.org/dist/latest-v10.x/docs/api/buffer.html#buffer_class_method_buffer_from_arraybuffer_byteoffset_length
|
||
|
// Create a zero-copy Buffer wrapper around the ArrayBuffer pointed to by the Uint8Array
|
||
|
actual = Buffer.from(actual.buffer, actual.byteOffset, actual.byteLength);
|
||
|
}
|
||
|
return new VSBuffer(actual);
|
||
|
}
|
||
|
constructor(buffer) {
|
||
|
this.buffer = buffer;
|
||
|
this.byteLength = this.buffer.byteLength;
|
||
|
}
|
||
|
toString() {
|
||
|
if (hasBuffer) {
|
||
|
return this.buffer.toString();
|
||
|
}
|
||
|
else {
|
||
|
if (!textDecoder) {
|
||
|
textDecoder = new TextDecoder();
|
||
|
}
|
||
|
return textDecoder.decode(this.buffer);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
export function readUInt16LE(source, offset) {
|
||
|
return (((source[offset + 0] << 0) >>> 0) |
|
||
|
((source[offset + 1] << 8) >>> 0));
|
||
|
}
|
||
|
export function writeUInt16LE(destination, value, offset) {
|
||
|
destination[offset + 0] = (value & 0b11111111);
|
||
|
value = value >>> 8;
|
||
|
destination[offset + 1] = (value & 0b11111111);
|
||
|
}
|
||
|
export function readUInt32BE(source, offset) {
|
||
|
return (source[offset] * Math.pow(2, 24)
|
||
|
+ source[offset + 1] * Math.pow(2, 16)
|
||
|
+ source[offset + 2] * Math.pow(2, 8)
|
||
|
+ source[offset + 3]);
|
||
|
}
|
||
|
export function writeUInt32BE(destination, value, offset) {
|
||
|
destination[offset + 3] = value;
|
||
|
value = value >>> 8;
|
||
|
destination[offset + 2] = value;
|
||
|
value = value >>> 8;
|
||
|
destination[offset + 1] = value;
|
||
|
value = value >>> 8;
|
||
|
destination[offset] = value;
|
||
|
}
|
||
|
export function readUInt8(source, offset) {
|
||
|
return source[offset];
|
||
|
}
|
||
|
export function writeUInt8(destination, value, offset) {
|
||
|
destination[offset] = value;
|
||
|
}
|