Skip to content

Commit

Permalink
Merge branch 'main' of github.com:sagemathinc/websocketfs
Browse files Browse the repository at this point in the history
  • Loading branch information
williamstein committed Aug 25, 2023
2 parents 4787bc3 + 2fe762d commit d8cf990
Show file tree
Hide file tree
Showing 12 changed files with 441 additions and 550 deletions.
80 changes: 44 additions & 36 deletions lib/sftp/fs-safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface HashAlgToHashSizeMap {
[alg: string]: number;
}

var _hashSizes = <HashAlgToHashSizeMap>{};
const _hashSizes: HashAlgToHashSizeMap = {};

export class SafeFilesystem implements IFilesystem {
isSafe: boolean;
Expand Down Expand Up @@ -85,8 +85,8 @@ export class SafeFilesystem implements IFilesystem {
}

private toVirtualPath(fullPath: string): string {
var i = 0;
var path: string;
let i = 0;
let path: string;
while (true) {
if (i >= this.root.length) {
path = fullPath.substr(this.root.length);
Expand Down Expand Up @@ -146,7 +146,7 @@ export class SafeFilesystem implements IFilesystem {
callback(err ?? Error("bug -- handleInfo must be specified"));
return;
}
var safeHandle = handleInfo.safe;
const safeHandle = handleInfo.safe;
if (err) {
delete this._handles[safeHandle];
callback(err);
Expand Down Expand Up @@ -178,8 +178,8 @@ export class SafeFilesystem implements IFilesystem {

//TODO: make sure all pending operations either complete or fail gracefully

for (var handle = 1; handle <= SafeFilesystem.MAX_HANDLE_COUNT; handle++) {
var handleInfo = this.toHandleInfo(handle);
for (let handle = 1; handle <= SafeFilesystem.MAX_HANDLE_COUNT; handle++) {
const handleInfo = this.toHandleInfo(handle);
if (handleInfo && handleInfo.real !== null) {
try {
this.fs.close(handleInfo.real, (_err) => {
Expand All @@ -204,20 +204,20 @@ export class SafeFilesystem implements IFilesystem {
) => void,
callback?: (err: Error | null, ...args) => any,
): void {
var handleInfo = this.toHandleInfo(safeHandle);
const handleInfo = this.toHandleInfo(safeHandle);

if (!handleInfo) {
return FileUtil.fail("Invalid handle", callback);
}

var finished = false;
var asynchronous = false;
let finished = false;
let asynchronous = false;

if (!handleInfo.busy) {
handleInfo.busy = true;
run();
} else {
var queue = handleInfo.actions;
let queue = handleInfo.actions;
if (!queue) {
queue = [];
handleInfo.actions = queue;
Expand Down Expand Up @@ -255,7 +255,7 @@ export class SafeFilesystem implements IFilesystem {
if (handleInfo == null) {
throw Error("bug");
}
var queue = handleInfo.actions;
const queue = handleInfo.actions;
if (!queue || queue.length == 0) {
handleInfo.busy = false;
} else {
Expand Down Expand Up @@ -418,7 +418,7 @@ export class SafeFilesystem implements IFilesystem {
): void {
path = this.toRealPath(path);

var handleInfo = this.createHandleInfo();
const handleInfo = this.createHandleInfo();
if (!handleInfo) {
return FileUtil.fail("ENFILE", callback);
}
Expand Down Expand Up @@ -618,17 +618,17 @@ export class SafeFilesystem implements IFilesystem {
): void {
if (this.isReadOnly()) return FileUtil.fail("EROFS", callback);

var fs = this.fs;
var same = fromHandle === toHandle;
var blockSize = MAX_WRITE_BLOCK_LENGTH;
const fs = this.fs;
const same = fromHandle === toHandle;
const blockSize = MAX_WRITE_BLOCK_LENGTH;
length = length > 0 ? length : -1;

var fh: any;
var th: any;
var fc: Function;
var tc: Function | null;
var fr = false;
var tr = false;
let fh: any;
let th: any;
let fc: Function;
let tc: Function | null;
let fr = false;
let tr = false;

//TODO: add argument checks
//TODO: fail on overlapping ranges in a single file
Expand Down Expand Up @@ -672,7 +672,7 @@ export class SafeFilesystem implements IFilesystem {
}

function copy() {
var bytesToRead = length >= 0 ? Math.min(blockSize, length) : blockSize;
const bytesToRead = length >= 0 ? Math.min(blockSize, length) : blockSize;
if (bytesToRead == 0) {
return done(null);
}
Expand All @@ -684,18 +684,26 @@ export class SafeFilesystem implements IFilesystem {
bytesToRead,
fromPosition,
(err, buffer, bytesRead) => {
if (err) return done(err);
if (err) {
return done(err);
}

if (bytesRead == 0) {
if (length == 0) return done(null);
if (length == 0) {
return done(null);
}
return FileUtil.fail("EOF", done);
}

if (length >= 0) length -= bytesRead;
if (length >= 0) {
length -= bytesRead;
}
fromPosition += bytesRead;

fs.write(th, buffer, 0, bytesRead, toPosition, (err) => {
if (err) return done(err);
if (err) {
return done(err);
}

toPosition += bytesRead;
copy();
Expand Down Expand Up @@ -739,9 +747,9 @@ export class SafeFilesystem implements IFilesystem {
}

// determine hash size
var hashSize = alg ? _hashSizes[alg] : 0;
let hashSize = alg ? _hashSizes[alg] : 0;
if (typeof hashSize === "undefined" && alg) {
var hasher;
let hasher;
try {
hasher = crypto.createHash(alg);
} catch (err) {
Expand All @@ -760,22 +768,22 @@ export class SafeFilesystem implements IFilesystem {
}

// calculate block count
var count = ((length + blockSize - 1) / blockSize) | 0;
const count = ((length + blockSize - 1) / blockSize) | 0;

// prepare buffers
var block = Buffer.alloc(blockSize);
var hashes = Buffer.alloc(count * hashSize);
var hashesOffset = 0;
const block = Buffer.alloc(blockSize);
const hashes = Buffer.alloc(count * hashSize);
let hashesOffset = 0;

var fs = this.fs;
const fs = this.fs;

this._execute(
handle,
(handle, callback) => {
next();

function next() {
var bytesToRead = Math.min(blockSize, length);
let bytesToRead = Math.min(blockSize, length);

if (bytesToRead == 0) {
return callback(null, hashes.slice(0, hashesOffset), alg);
Expand Down Expand Up @@ -803,9 +811,9 @@ export class SafeFilesystem implements IFilesystem {
length -= bytesRead;

// calculate hash
var hasher = crypto.createHash(alg);
const hasher = crypto.createHash(alg);
hasher.update(block.slice(0, bytesRead));
var hash = hasher.digest();
const hash = hasher.digest();

// copy hash to results
hash.copy(hashes, hashesOffset);
Expand Down
50 changes: 31 additions & 19 deletions lib/sftp/fs-sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class FileDataSource extends EventEmitter implements IDataSource {

if (this.nextChunkPosition - this.expectedPosition > 0x20000) break;

var chunkSize = 0x8000;
const chunkSize = 0x8000;
this._next(this.nextChunkPosition, chunkSize);
this.nextChunkPosition += chunkSize;
}
Expand Down Expand Up @@ -144,11 +144,11 @@ export class FileDataSource extends EventEmitter implements IDataSource {

try {
// prepare the chunk for the queue
var chunk = <IChunk>buffer.slice(0, bytesRead); //WEB: var chunk = <IChunk>buffer.subarray(0, bytesRead);
const chunk = <IChunk>buffer.slice(0, bytesRead);
chunk.position = position;

// insert the chunk into the appropriate position in the queue
var index = this.queue.length;
let index = this.queue.length;
while (--index >= 0) {
if (position > this.queue[index].position) break;
}
Expand Down Expand Up @@ -246,7 +246,7 @@ export class FileDataSource extends EventEmitter implements IDataSource {
private _close(): void {
if (!this.handle) return;

var handle = this.handle;
const handle = this.handle;
this.handle = null;
try {
this.fs.close(handle, (err) => {
Expand Down Expand Up @@ -298,7 +298,7 @@ class BlobDataSource extends EventEmitter implements IDataSource {
this.busy = false;

if (!this.finished) {
var chunk = Buffer.alloc(e.target.result);
const chunk = Buffer.alloc(e.target.result);
if (chunk.length > 0) {
this.queue.push(chunk);
if (!this.readable) {
Expand Down Expand Up @@ -338,7 +338,7 @@ class BlobDataSource extends EventEmitter implements IDataSource {

// read more data unless the queue is full
if (this.queue.length < 4) {
var slice = this.blob.slice(this.pos, this.pos + 0x8000);
const slice = this.blob.slice(this.pos, this.pos + 0x8000);
this.pos += slice.size;
this.busy = true;
this.reader.readAsArrayBuffer(slice);
Expand All @@ -360,10 +360,12 @@ class BlobDataSource extends EventEmitter implements IDataSource {
}

// get next chunk
var chunk = this.queue.shift();
const chunk = this.queue.shift();

// if no more chunks are available, become unreadable
if (this.queue.length == 0) this.readable = false;
if (this.queue.length == 0) {
this.readable = false;
}

return chunk ?? null;
}
Expand Down Expand Up @@ -410,7 +412,7 @@ export function toDataSource(

function openBlobDataSource(blob: Blob): void {
process.nextTick(() => {
var source = <IDataSource>(<any>new BlobDataSource(blob, 0));
const source = <IDataSource>(<any>new BlobDataSource(blob, 0));
callback(null, [source]);
});
}
Expand All @@ -437,15 +439,17 @@ export function toDataSource(
}

function toArrayDataSource(input: any[]): void {
var source = <IDataSource[]>[];
var array = <any[]>[];
const source: IDataSource[] = [];
const array: any[] = [];
Array.prototype.push.apply(array, input);
next();

function next(): void {
try {
var item = array.shift();
if (!item) return callback(null, source);
const item = array.shift();
if (!item) {
return callback(null, source);
}

if (isArray(item))
throw new Error("Unsupported array of arrays data source");
Expand All @@ -458,7 +462,9 @@ export function toDataSource(
}

function add(err: Error, src: IDataSource[]): void {
if (err) return callback(err);
if (err) {
return callback(err);
}
Array.prototype.push.apply(source, src);
next();
}
Expand All @@ -468,12 +474,16 @@ export function toDataSource(
path: string,
callback: (err: Error | null, source?: IDataSource[]) => void,
): void {
if (!fs) throw new Error("Source file system not available");
if (!fs) {
throw new Error("Source file system not available");
}

fs.stat(path, (err, stats) => {
if (err) return callback(err);
if (err) {
return callback(err);
}

var item = new FileDataSource(fs, path, undefined, stats, 0);
const item = new FileDataSource(fs, path, undefined, stats, 0);
callback(null, [item]);
});
}
Expand All @@ -485,9 +495,11 @@ export function toDataSource(
if (err) {
return callback(err);
}
if (items == null) throw Error("bug");
if (items == null) {
throw Error("bug");
}

var source = <IDataSource[]>[];
const source: IDataSource[] = [];
items.forEach((it) => {
const item = new FileDataSource(
fs,
Expand Down
Loading

0 comments on commit d8cf990

Please sign in to comment.