@intuita-inc/utilities
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -56,4 +56,4 @@ /// <reference types="node" resolution-mode="require"/> | ||
initialize(): Promise<void>; | ||
protected _read(buffer: Buffer): Promise<void>; | ||
protected _read(buffer: Buffer): void; | ||
} | ||
export {}; |
@@ -268,3 +268,5 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
// 16*1024 is the MAX_LENGTH for strings | ||
this._circularBuffer = new CircularBuffer(16 * 1024 * 2); | ||
this._circularBuffer = new CircularBuffer(16 * 1024 * 2, (buffer) => { | ||
this._read(buffer); | ||
}); | ||
this._fileReadingService = new FileReadingService(pathLike, this._circularBuffer); | ||
@@ -278,5 +280,3 @@ this._fileWatcher = new FileWatcher(pathLike, () => { | ||
const byteLength = getByteLength(this._state); | ||
yield this._circularBuffer.requireByteLength(byteLength, (buffer) => __awaiter(this, void 0, void 0, function* () { | ||
yield this._read(buffer); | ||
})); | ||
this._circularBuffer.requireByteLength(byteLength); | ||
yield this._fileReadingService.open(); | ||
@@ -287,13 +287,22 @@ this._fileWatcher.watch(); | ||
_read(buffer) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const stateRecipe = read(buffer, this._state); | ||
if ('event' in stateRecipe && stateRecipe.event === 'error') { | ||
this._fileWatcher.close(); | ||
yield this._fileReadingService.close(); | ||
const stateRecipe = read(buffer, this._state); | ||
if ('event' in stateRecipe && stateRecipe.event === 'error') { | ||
this._fileWatcher.close(); | ||
this._fileReadingService | ||
.close() | ||
.then(() => { | ||
this.emit('error', stateRecipe.error); | ||
return; | ||
} | ||
if ('event' in stateRecipe && stateRecipe.event === 'end') { | ||
this._fileWatcher.close(); | ||
yield this._fileReadingService.close(); | ||
}) | ||
.catch((error) => { | ||
this.emit('error', error instanceof Error | ||
? error | ||
: new Error('Could not close the FileReadingService')); | ||
}); | ||
return; | ||
} | ||
if ('event' in stateRecipe && stateRecipe.event === 'end') { | ||
this._fileWatcher.close(); | ||
this._fileReadingService | ||
.close() | ||
.then(() => { | ||
const hashDigest = this._hash.digest(); | ||
@@ -306,24 +315,29 @@ if (Buffer.compare(stateRecipe.hashDigest, hashDigest) !== 0) { | ||
} | ||
return; | ||
} | ||
if ('event' in stateRecipe && stateRecipe.event === 'case') { | ||
this.emit('case', stateRecipe.surfaceAgnosticCase); | ||
} | ||
if ('event' in stateRecipe && stateRecipe.event === 'job') { | ||
this.emit('job', stateRecipe.surfaceAgnosticJob); | ||
} | ||
if (this._state.position !== POSITION.BEFORE_PREAMBLE && | ||
this._state.position !== POSITION.BEFORE_POSTAMBLE_HASH_DIGEST && | ||
stateRecipe.position !== POSITION.BEFORE_POSTAMBLE_HASH_DIGEST) { | ||
this._hash.update(buffer); | ||
} | ||
this._state = { | ||
position: stateRecipe.position, | ||
outerCase: stateRecipe.outerCase, | ||
outerJob: stateRecipe.outerJob, | ||
}; | ||
const byteLength = getByteLength(this._state); | ||
yield this._circularBuffer.requireByteLength(byteLength, (buffer) => this._read(buffer)); | ||
}); | ||
}) | ||
.catch((error) => { | ||
this.emit('error', error instanceof Error | ||
? error | ||
: new Error('Could not close the FileReadingService')); | ||
}); | ||
return; | ||
} | ||
if ('event' in stateRecipe && stateRecipe.event === 'case') { | ||
this.emit('case', stateRecipe.surfaceAgnosticCase); | ||
} | ||
if ('event' in stateRecipe && stateRecipe.event === 'job') { | ||
this.emit('job', stateRecipe.surfaceAgnosticJob); | ||
} | ||
if (this._state.position !== POSITION.BEFORE_PREAMBLE && | ||
this._state.position !== POSITION.BEFORE_POSTAMBLE_HASH_DIGEST && | ||
stateRecipe.position !== POSITION.BEFORE_POSTAMBLE_HASH_DIGEST) { | ||
this._hash.update(buffer); | ||
} | ||
this._state = { | ||
position: stateRecipe.position, | ||
outerCase: stateRecipe.outerCase, | ||
outerJob: stateRecipe.outerJob, | ||
}; | ||
const byteLength = getByteLength(this._state); | ||
this._circularBuffer.requireByteLength(byteLength); | ||
} | ||
} |
/// <reference types="node" resolution-mode="require"/> | ||
type Callback = (buffer: Buffer) => Promise<void>; | ||
type Callback = (buffer: Buffer) => void; | ||
export declare class CircularBuffer { | ||
protected readonly _MAX_BYTE_LENGTH: number; | ||
protected readonly _callback: Callback; | ||
protected _buffer: Buffer; | ||
@@ -10,9 +11,8 @@ protected _start: number; | ||
protected _requestedByteLength: number; | ||
protected _callback: Callback | null; | ||
constructor(_MAX_BYTE_LENGTH: number); | ||
requireByteLength(byteLength: number, callback: Callback): Promise<void>; | ||
constructor(_MAX_BYTE_LENGTH: number, _callback: Callback); | ||
requireByteLength(byteLength: number): void; | ||
getFreeByteLength(): number; | ||
write(buffer: Buffer, byteLength: number): Promise<void>; | ||
write(buffer: Buffer, byteLength: number): void; | ||
private __readConditionally; | ||
} | ||
export {}; |
@@ -1,13 +0,5 @@ | ||
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()); | ||
}); | ||
}; | ||
export class CircularBuffer { | ||
constructor(_MAX_BYTE_LENGTH) { | ||
constructor(_MAX_BYTE_LENGTH, _callback) { | ||
this._MAX_BYTE_LENGTH = _MAX_BYTE_LENGTH; | ||
this._callback = _callback; | ||
this._start = 0; | ||
@@ -17,11 +9,7 @@ this._end = 0; | ||
this._requestedByteLength = 0; | ||
this._callback = null; | ||
this._buffer = Buffer.alloc(_MAX_BYTE_LENGTH); | ||
} | ||
requireByteLength(byteLength, callback) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this._requestedByteLength = byteLength; | ||
this._callback = callback; | ||
yield this.__readConditionally(); | ||
}); | ||
requireByteLength(byteLength) { | ||
this._requestedByteLength = byteLength; | ||
this.__readConditionally(); | ||
} | ||
@@ -32,75 +20,69 @@ getFreeByteLength() { | ||
write(buffer, byteLength) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (byteLength === 0) { | ||
throw new Error('You cannot write 0 bytes into the circular buffer'); | ||
if (byteLength === 0) { | ||
throw new Error('You cannot write 0 bytes into the circular buffer'); | ||
} | ||
const freeByteLength = this.getFreeByteLength(); | ||
if (this.getFreeByteLength() < byteLength) { | ||
throw new Error(`You cannot write ${byteLength} byte(s) when only ${freeByteLength} is/are available`); | ||
} | ||
if (this._start <= this._end) { | ||
const freeRightByteLength = this._MAX_BYTE_LENGTH - this._end; | ||
const firstSourceStart = 0; | ||
const firstSourceEnd = Math.min(freeRightByteLength, byteLength); | ||
if (firstSourceEnd !== 0) { | ||
buffer.copy(this._buffer, this._end, firstSourceStart, firstSourceEnd); | ||
} | ||
const freeByteLength = this.getFreeByteLength(); | ||
if (this.getFreeByteLength() < byteLength) { | ||
throw new Error(`You cannot write ${byteLength} byte(s) when only ${freeByteLength} is/are available`); | ||
const secondSourceStart = firstSourceEnd; | ||
const secondSourceEnd = byteLength; | ||
if (firstSourceEnd !== byteLength) { | ||
buffer.copy(this._buffer, 0, secondSourceStart, secondSourceEnd); | ||
} | ||
if (this._start <= this._end) { | ||
const freeRightByteLength = this._MAX_BYTE_LENGTH - this._end; | ||
const firstSourceStart = 0; | ||
const firstSourceEnd = Math.min(freeRightByteLength, byteLength); | ||
if (firstSourceEnd !== 0) { | ||
buffer.copy(this._buffer, this._end, firstSourceStart, firstSourceEnd); | ||
} | ||
const secondSourceStart = firstSourceEnd; | ||
const secondSourceEnd = byteLength; | ||
if (firstSourceEnd !== byteLength) { | ||
buffer.copy(this._buffer, 0, secondSourceStart, secondSourceEnd); | ||
} | ||
} | ||
else { | ||
// end < start | ||
buffer.copy(this._buffer, this._end, 0, byteLength); | ||
} | ||
const overflownEnd = this._end + byteLength; | ||
this._end = | ||
overflownEnd >= this._MAX_BYTE_LENGTH | ||
? overflownEnd - this._MAX_BYTE_LENGTH | ||
: overflownEnd; | ||
this._currentByteLength += byteLength; | ||
yield this.__readConditionally(); | ||
}); | ||
} | ||
else { | ||
// end < start | ||
buffer.copy(this._buffer, this._end, 0, byteLength); | ||
} | ||
const overflownEnd = this._end + byteLength; | ||
this._end = | ||
overflownEnd >= this._MAX_BYTE_LENGTH | ||
? overflownEnd - this._MAX_BYTE_LENGTH | ||
: overflownEnd; | ||
this._currentByteLength += byteLength; | ||
this.__readConditionally(); | ||
} | ||
__readConditionally() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this._requestedByteLength === 0 || this._callback === null) { | ||
return; | ||
if (this._requestedByteLength === 0 || this._callback === null) { | ||
return; | ||
} | ||
if (this._currentByteLength < this._requestedByteLength) { | ||
return; | ||
} | ||
const targetBuffer = Buffer.alloc(this._requestedByteLength); | ||
if (this._start < this._end) { | ||
this._buffer.copy(targetBuffer, 0, this._start, this._start + this._requestedByteLength); | ||
} | ||
else { | ||
// start >= end | ||
const firstSourceStart = this._start; | ||
const firstSourceEnd = Math.min(this._start + this._requestedByteLength, this._MAX_BYTE_LENGTH); | ||
this._buffer.copy(targetBuffer, 0, firstSourceStart, firstSourceEnd); | ||
const secondSourceStart = 0; | ||
const secondSourceEnd = this._requestedByteLength > this._MAX_BYTE_LENGTH - this._start | ||
? this._requestedByteLength - | ||
(this._MAX_BYTE_LENGTH - this._start) | ||
: 0; | ||
const targetStart = firstSourceEnd - firstSourceStart; | ||
if (secondSourceEnd !== 0) { | ||
this._buffer.copy(targetBuffer, targetStart, secondSourceStart, secondSourceEnd); | ||
} | ||
if (this._currentByteLength < this._requestedByteLength) { | ||
return; | ||
} | ||
const targetBuffer = Buffer.alloc(this._requestedByteLength); | ||
if (this._start < this._end) { | ||
this._buffer.copy(targetBuffer, 0, this._start, this._start + this._requestedByteLength); | ||
} | ||
else { | ||
// start >= end | ||
const firstSourceStart = this._start; | ||
const firstSourceEnd = Math.min(this._start + this._requestedByteLength, this._MAX_BYTE_LENGTH); | ||
this._buffer.copy(targetBuffer, 0, firstSourceStart, firstSourceEnd); | ||
const secondSourceStart = 0; | ||
const secondSourceEnd = this._requestedByteLength > this._MAX_BYTE_LENGTH - this._start | ||
? this._requestedByteLength - | ||
(this._MAX_BYTE_LENGTH - this._start) | ||
: 0; | ||
const targetStart = firstSourceEnd - firstSourceStart; | ||
if (secondSourceEnd !== 0) { | ||
this._buffer.copy(targetBuffer, targetStart, secondSourceStart, secondSourceEnd); | ||
} | ||
} | ||
const overflownStart = this._start + this._requestedByteLength; | ||
this._start = | ||
overflownStart >= this._MAX_BYTE_LENGTH | ||
? overflownStart - this._MAX_BYTE_LENGTH | ||
: overflownStart; | ||
this._currentByteLength -= this._requestedByteLength; | ||
const callback = this._callback; | ||
this._requestedByteLength = 0; | ||
this._callback = null; | ||
yield callback(targetBuffer); | ||
}); | ||
} | ||
const overflownStart = this._start + this._requestedByteLength; | ||
this._start = | ||
overflownStart >= this._MAX_BYTE_LENGTH | ||
? overflownStart - this._MAX_BYTE_LENGTH | ||
: overflownStart; | ||
this._currentByteLength -= this._requestedByteLength; | ||
this._requestedByteLength = 0; | ||
process.nextTick(() => this._callback(targetBuffer)); | ||
} | ||
} |
{ | ||
"name": "@intuita-inc/utilities", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "The utilities used by Intuita", | ||
@@ -5,0 +5,0 @@ "type": "module", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55563
1074
0