@pkerschbaum/code-oss-file-service
Version:
VS Code ([microsoft/vscode](https://github.com/microsoft/vscode)) includes a rich "`FileService`" and "`DiskFileSystemProvider`" abstraction built on top of Node.js core modules (`fs`, `path`) and Electron's `shell` module. This package allows to use that
260 lines • 11 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.prefixedBufferStream = exports.prefixedBufferReadable = exports.newWriteableBufferStream = exports.streamToBufferReadableStream = exports.bufferToStream = exports.bufferedStreamToBuffer = exports.streamToBuffer = exports.bufferToReadable = exports.readableToBuffer = exports.writeUInt8 = exports.readUInt8 = exports.writeUInt32LE = exports.readUInt32LE = exports.writeUInt32BE = exports.readUInt32BE = exports.writeUInt16LE = exports.readUInt16LE = exports.VSBuffer = void 0;
const streams = require("../../base/common/stream");
const hasBuffer = (typeof Buffer !== 'undefined');
let textEncoder;
let textDecoder;
class VSBuffer {
constructor(buffer) {
this.buffer = buffer;
this.byteLength = this.buffer.byteLength;
}
/**
* When running in a nodejs context, the backing store for the returned `VSBuffer` instance
* might use a nodejs Buffer allocated from node's Buffer pool, which is not transferrable.
*/
static alloc(byteLength) {
if (hasBuffer) {
return new VSBuffer(Buffer.allocUnsafe(byteLength));
}
else {
return new VSBuffer(new Uint8Array(byteLength));
}
}
/**
* 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);
}
/**
* When running in a nodejs context, the backing store for the returned `VSBuffer` instance
* might use a nodejs Buffer allocated from node's Buffer pool, which is not transferrable.
*/
static fromString(source, options) {
const dontUseNodeBuffer = (options === null || options === void 0 ? void 0 : options.dontUseNodeBuffer) || false;
if (!dontUseNodeBuffer && hasBuffer) {
return new VSBuffer(Buffer.from(source));
}
else {
if (!textEncoder) {
textEncoder = new TextEncoder();
}
return new VSBuffer(textEncoder.encode(source));
}
}
/**
* When running in a nodejs context, the backing store for the returned `VSBuffer` instance
* might use a nodejs Buffer allocated from node's Buffer pool, which is not transferrable.
*/
static fromByteArray(source) {
const result = VSBuffer.alloc(source.length);
for (let i = 0, len = source.length; i < len; i++) {
result.buffer[i] = source[i];
}
return result;
}
/**
* When running in a nodejs context, the backing store for the returned `VSBuffer` instance
* might use a nodejs Buffer allocated from node's Buffer pool, which is not transferrable.
*/
static concat(buffers, totalLength) {
if (typeof totalLength === 'undefined') {
totalLength = 0;
for (let i = 0, len = buffers.length; i < len; i++) {
totalLength += buffers[i].byteLength;
}
}
const ret = VSBuffer.alloc(totalLength);
let offset = 0;
for (let i = 0, len = buffers.length; i < len; i++) {
const element = buffers[i];
ret.set(element, offset);
offset += element.byteLength;
}
return ret;
}
/**
* When running in a nodejs context, the backing store for the returned `VSBuffer` instance
* might use a nodejs Buffer allocated from node's Buffer pool, which is not transferrable.
*/
clone() {
const result = VSBuffer.alloc(this.byteLength);
result.set(this);
return result;
}
toString() {
if (hasBuffer) {
return this.buffer.toString();
}
else {
if (!textDecoder) {
textDecoder = new TextDecoder();
}
return textDecoder.decode(this.buffer);
}
}
slice(start, end) {
// IMPORTANT: use subarray instead of slice because TypedArray#slice
// creates shallow copy and NodeBuffer#slice doesn't. The use of subarray
// ensures the same, performance, behaviour.
return new VSBuffer(this.buffer.subarray(start, end));
}
set(array, offset) {
if (array instanceof VSBuffer) {
this.buffer.set(array.buffer, offset);
}
else if (array instanceof Uint8Array) {
this.buffer.set(array, offset);
}
else if (array instanceof ArrayBuffer) {
this.buffer.set(new Uint8Array(array), offset);
}
else if (ArrayBuffer.isView(array)) {
this.buffer.set(new Uint8Array(array.buffer, array.byteOffset, array.byteLength), offset);
}
else {
throw new Error(`Unkown argument 'array'`);
}
}
readUInt32BE(offset) {
return readUInt32BE(this.buffer, offset);
}
writeUInt32BE(value, offset) {
writeUInt32BE(this.buffer, value, offset);
}
readUInt32LE(offset) {
return readUInt32LE(this.buffer, offset);
}
writeUInt32LE(value, offset) {
writeUInt32LE(this.buffer, value, offset);
}
readUInt8(offset) {
return readUInt8(this.buffer, offset);
}
writeUInt8(value, offset) {
writeUInt8(this.buffer, value, offset);
}
}
exports.VSBuffer = VSBuffer;
function readUInt16LE(source, offset) {
return (((source[offset + 0] << 0) >>> 0) |
((source[offset + 1] << 8) >>> 0));
}
exports.readUInt16LE = readUInt16LE;
function writeUInt16LE(destination, value, offset) {
destination[offset + 0] = (value & 0b11111111);
value = value >>> 8;
destination[offset + 1] = (value & 0b11111111);
}
exports.writeUInt16LE = writeUInt16LE;
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]);
}
exports.readUInt32BE = readUInt32BE;
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;
}
exports.writeUInt32BE = writeUInt32BE;
function readUInt32LE(source, offset) {
return (((source[offset + 0] << 0) >>> 0) |
((source[offset + 1] << 8) >>> 0) |
((source[offset + 2] << 16) >>> 0) |
((source[offset + 3] << 24) >>> 0));
}
exports.readUInt32LE = readUInt32LE;
function writeUInt32LE(destination, value, offset) {
destination[offset + 0] = (value & 0b11111111);
value = value >>> 8;
destination[offset + 1] = (value & 0b11111111);
value = value >>> 8;
destination[offset + 2] = (value & 0b11111111);
value = value >>> 8;
destination[offset + 3] = (value & 0b11111111);
}
exports.writeUInt32LE = writeUInt32LE;
function readUInt8(source, offset) {
return source[offset];
}
exports.readUInt8 = readUInt8;
function writeUInt8(destination, value, offset) {
destination[offset] = value;
}
exports.writeUInt8 = writeUInt8;
function readableToBuffer(readable) {
return streams.consumeReadable(readable, chunks => VSBuffer.concat(chunks));
}
exports.readableToBuffer = readableToBuffer;
function bufferToReadable(buffer) {
return streams.toReadable(buffer);
}
exports.bufferToReadable = bufferToReadable;
function streamToBuffer(stream) {
return streams.consumeStream(stream, chunks => VSBuffer.concat(chunks));
}
exports.streamToBuffer = streamToBuffer;
function bufferedStreamToBuffer(bufferedStream) {
return __awaiter(this, void 0, void 0, function* () {
if (bufferedStream.ended) {
return VSBuffer.concat(bufferedStream.buffer);
}
return VSBuffer.concat([
// Include already read chunks...
...bufferedStream.buffer,
// ...and all additional chunks
yield streamToBuffer(bufferedStream.stream)
]);
});
}
exports.bufferedStreamToBuffer = bufferedStreamToBuffer;
function bufferToStream(buffer) {
return streams.toStream(buffer, chunks => VSBuffer.concat(chunks));
}
exports.bufferToStream = bufferToStream;
function streamToBufferReadableStream(stream) {
return streams.transform(stream, { data: data => typeof data === 'string' ? VSBuffer.fromString(data) : VSBuffer.wrap(data) }, chunks => VSBuffer.concat(chunks));
}
exports.streamToBufferReadableStream = streamToBufferReadableStream;
function newWriteableBufferStream(options) {
return streams.newWriteableStream(chunks => VSBuffer.concat(chunks), options);
}
exports.newWriteableBufferStream = newWriteableBufferStream;
function prefixedBufferReadable(prefix, readable) {
return streams.prefixedReadable(prefix, readable, chunks => VSBuffer.concat(chunks));
}
exports.prefixedBufferReadable = prefixedBufferReadable;
function prefixedBufferStream(prefix, stream) {
return streams.prefixedStream(prefix, stream, chunks => VSBuffer.concat(chunks));
}
exports.prefixedBufferStream = prefixedBufferStream;
//# sourceMappingURL=buffer.js.map
;