Skip to content

Commit

Permalink
ES6 and Formatting
Browse files Browse the repository at this point in the history
Did some general fixer-uppers to the codebase! Now it's more ES6-friendly in some spots, it consistently uses double-quotes now, and part of the ES6-friendly bits, now arrow functions are used a bit more, and any var declaration that's modified once is now a const declaration :)

Forgot to include some links that helped out for the last few commits:
nodejs/help#3902
https://stackoverflow.com/questions/35470511/setting-up-tsconfig-with-spec-test-folder
https://jestjs.io/docs/ecmascript-modules
https://khalilstemmler.com/blogs/typescript/abstract-class/ (Going to look into if this is what MediaFileReader should instead be made with. Haven't used this before!)
https://stackoverflow.com/questions/45251664/derive-union-type-from-tuple-array-values (THIS IS FREAKING EPIC, big thanks to this post)
  • Loading branch information
Offroaders123 committed Apr 9, 2023
1 parent 9d2e48f commit 1389d1f
Show file tree
Hide file tree
Showing 21 changed files with 881 additions and 859 deletions.
54 changes: 27 additions & 27 deletions src/ArrayBufferFileReader.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import ChunkedFileData from './ChunkedFileData.js';
import MediaFileReader from './MediaFileReader.js';
import ChunkedFileData from "./ChunkedFileData.js";
import MediaFileReader from "./MediaFileReader.js";

import type { LoadCallbackType } from './FlowTypes.js';
import type { LoadCallbackType } from "./FlowTypes.js";

export default class ArrayBufferFileReader extends MediaFileReader {
declare _buffer: ArrayBuffer;
declare _fileData: ChunkedFileData;
declare _size: number;
declare _buffer: ArrayBuffer;
declare _fileData: ChunkedFileData;
declare _size: number;

constructor(buffer: ArrayBuffer) {
super();
this._buffer = buffer;
this._fileData = new ChunkedFileData();
}
constructor(buffer: ArrayBuffer) {
super();
this._buffer = buffer;
this._fileData = new ChunkedFileData();
}

static canReadFile(file: any): boolean {
return typeof ArrayBuffer === 'function' && file instanceof ArrayBuffer
}
static canReadFile(file: any): boolean {
return file instanceof ArrayBuffer
}

_init(callbacks: LoadCallbackType): void {
this._size = this._buffer.byteLength;
setTimeout(callbacks.onSuccess, 1);
}
_init({ onSuccess }: LoadCallbackType): void {
this._size = this._buffer.byteLength;
setTimeout(onSuccess, 1);
}

loadRange(range: [number, number], callbacks: LoadCallbackType): void {
var arrayBuf = this._buffer.slice(range[0], range[1] + 1);
var viewData = new Uint8Array(arrayBuf);
this._fileData.addData(range[0], viewData);
callbacks.onSuccess();
}
loadRange(range: [number, number], { onSuccess }: LoadCallbackType): void {
const arrayBuf = this._buffer.slice(range[0], range[1] + 1);
const viewData = new Uint8Array(arrayBuf);
this._fileData.addData(range[0], viewData);
onSuccess();
}

getByteAt(offset: number): number {
return this._fileData.getByteAt(offset);
}
getByteAt(offset: number): number {
return this._fileData.getByteAt(offset);
}
}
16 changes: 8 additions & 8 deletions src/ArrayFileReader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import MediaFileReader from './MediaFileReader.js';
import MediaFileReader from "./MediaFileReader.js";

import type { Byte, ByteArray, LoadCallbackType } from './FlowTypes.js';
import type { Byte, ByteArray, LoadCallbackType } from "./FlowTypes.js";

export default class ArrayFileReader extends MediaFileReader {
declare _array: ByteArray;
Expand All @@ -17,21 +17,21 @@ export default class ArrayFileReader extends MediaFileReader {
static canReadFile(file: any): boolean {
return (
Array.isArray(file) ||
(typeof Buffer === 'function' && Buffer.isBuffer(file))
(typeof Buffer === "function" && Buffer.isBuffer(file))
);
}

init(callbacks: LoadCallbackType) {
setTimeout(callbacks.onSuccess, 0);
init({ onSuccess }: LoadCallbackType) {
setTimeout(onSuccess, 0);
}

loadRange(range: [number, number], callbacks: LoadCallbackType) {
setTimeout(callbacks.onSuccess, 0);
loadRange(range: [number, number], { onSuccess }: LoadCallbackType) {
setTimeout(onSuccess, 0);
}

getByteAt(offset: number): Byte {
if (offset >= this._array.length) {
throw new Error("Offset " + offset + " hasn't been loaded yet.");
throw new Error(`Offset ${offset} hasn't been loaded yet.`);
}
return this._array[offset];
}
Expand Down
35 changes: 17 additions & 18 deletions src/BlobFileReader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ChunkedFileData from './ChunkedFileData.js';
import MediaFileReader from './MediaFileReader.js';
import ChunkedFileData from "./ChunkedFileData.js";
import MediaFileReader from "./MediaFileReader.js";

import type { LoadCallbackType } from './FlowTypes.js';
import type { LoadCallbackType } from "./FlowTypes.js";

export default class BlobFileReader extends MediaFileReader {
declare _blob: Blob;
Expand All @@ -23,28 +23,27 @@ export default class BlobFileReader extends MediaFileReader {
);
}

_init(callbacks: LoadCallbackType): void {
_init({ onSuccess }: LoadCallbackType): void {
this._size = this._blob.size;
setTimeout(callbacks.onSuccess, 1);
setTimeout(onSuccess, 1);
}

loadRange(range: [number, number], callbacks: LoadCallbackType): void {
var self = this;
// $FlowIssue - flow isn't aware of mozSlice or webkitSlice
// @ts-expect-error
var blobSlice = this._blob.slice || this._blob.mozSlice || this._blob.webkitSlice;
var blob = blobSlice.call(this._blob, range[0], range[1] + 1);
var browserFileReader = new FileReader();
loadRange(range: [number, number], { onSuccess, onError }: LoadCallbackType): void {
const self = this;
// @ts-expect-error - flow isn't aware of mozSlice or webkitSlice
const blobSlice = this._blob.slice || this._blob.mozSlice || this._blob.webkitSlice;
const blob = blobSlice.call(this._blob, range[0], range[1] + 1);
const browserFileReader = new FileReader();

browserFileReader.onloadend = function(event) {
var intArray = new Uint8Array(browserFileReader.result! as ArrayBuffer);
browserFileReader.onloadend = () => {
const intArray = new Uint8Array(browserFileReader.result! as ArrayBuffer);
self._fileData.addData(range[0], intArray);
callbacks.onSuccess();
onSuccess();
};
browserFileReader.onerror =
browserFileReader.onabort = function(event) {
if (callbacks.onError) {
callbacks.onError({"type": "blob", "info": browserFileReader.error});
browserFileReader.onabort = () => {
if (onError) {
onError({"type": "blob", "info": browserFileReader.error});
}
};

Expand Down
8 changes: 4 additions & 4 deletions src/ByteArrayUtils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { ByteArray } from './FlowTypes.js';
import type { ByteArray } from "./FlowTypes.js";

/**
* Converts a string to a binary array
*/
export function bin(string: string): ByteArray {
var binaryArray: ByteArray = new Array(string.length);
for (var i = 0; i < string.length; i++) {
const binaryArray: ByteArray = new Array(string.length);
for (let i = 0; i < string.length; i++) {
binaryArray[i] = string.charCodeAt(i);
}
return binaryArray;
Expand All @@ -15,7 +15,7 @@ export function bin(string: string): ByteArray {
* Pads an array with \0 until it is size length.
*/
export function pad(array: any[], size: number): any[] {
for (var i = array.length; i < size; i++) {
for (let i = array.length; i < size; i++) {
array.push(0);
}
return array;
Expand Down
57 changes: 26 additions & 31 deletions src/ChunkedFileData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ChunkType, DataType, TypedArray } from './FlowTypes.js';
import type { ChunkType, DataType, TypedArray } from "./FlowTypes.js";

const NOT_FOUND = -1;

Expand All @@ -12,7 +12,6 @@ const NOT_FOUND = -1;
* to add and read data from the file.
*/
export default class ChunkedFileData {
// $FlowIssue - get/set properties not yet supported
static get NOT_FOUND() { return NOT_FOUND; }
declare _fileData: ChunkType[];

Expand All @@ -24,8 +23,8 @@ export default class ChunkedFileData {
* Adds data to the file storage at a specific offset.
*/
addData(offset: number, data: DataType): void {
var offsetEnd = offset+data.length-1;
var chunkRange = this._getChunkRange(offset, offsetEnd);
const offsetEnd = offset+data.length-1;
const chunkRange = this._getChunkRange(offset, offsetEnd);

if (chunkRange.startIx === NOT_FOUND) {
this._fileData.splice(chunkRange.insertIx || 0, 0, {
Expand All @@ -36,12 +35,12 @@ export default class ChunkedFileData {
// If the data to add collides with existing chunks we prepend and
// append data from the half colliding chunks to make the collision at
// 100%. The new data can then replace all the colliding chunkes.
var firstChunk = this._fileData[chunkRange.startIx];
var lastChunk = this._fileData[chunkRange.endIx];
var needsPrepend = offset > firstChunk.offset;
var needsAppend = offsetEnd < lastChunk.offset + lastChunk.data.length - 1;
const firstChunk = this._fileData[chunkRange.startIx];
const lastChunk = this._fileData[chunkRange.endIx];
const needsPrepend = offset > firstChunk.offset;
const needsAppend = offsetEnd < lastChunk.offset + lastChunk.data.length - 1;

var chunk = {
const chunk = {
offset: Math.min(offset, firstChunk.offset),
data: data
};
Expand Down Expand Up @@ -79,17 +78,13 @@ export default class ChunkedFileData {
typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView &&
ArrayBuffer.isView(dataA)
) {
// $FlowIssue - flow thinks dataAandB is a string but it's not
// @ts-expect-error
var dataAandB = new (dataA.constructor as TypedArray)(dataA.length + dataB.length);
// $FlowIssue - flow thinks dataAandB is a string but it's not
// @ts-expect-error - flow thinks dataAandB is a string but it's not
const dataAandB = new (dataA.constructor as TypedArray)(dataA.length + dataB.length);
dataAandB.set(dataA, 0);
// $FlowIssue - flow thinks dataAandB is a string but it's not
dataAandB.set(dataB, dataA.length);
return dataAandB;
} else {
// $FlowIssue - flow thinks dataAandB is a TypedArray but it's not
// @ts-expect-error
// @ts-expect-error - flow thinks dataAandB is a TypedArray but it's not
return dataA.concat(dataB);
}
}
Expand All @@ -116,14 +111,14 @@ export default class ChunkedFileData {
offsetStart: number,
offsetEnd: number
): {startIx: number, endIx: number, insertIx?: number} {
var startChunkIx = NOT_FOUND;
var endChunkIx = NOT_FOUND;
var insertIx = 0;
let startChunkIx = NOT_FOUND;
let endChunkIx = NOT_FOUND;
let insertIx = 0;

// Could use binary search but not expecting that many blocks to exist.
for (var i = 0; i < this._fileData.length; i++, insertIx = i) {
var chunkOffsetStart = this._fileData[i].offset;
var chunkOffsetEnd = chunkOffsetStart + this._fileData[i].data.length;
for (let i = 0; i < this._fileData.length; i++, insertIx = i) {
const chunkOffsetStart = this._fileData[i].offset;
const chunkOffsetEnd = chunkOffsetStart + this._fileData[i].data.length;

if (offsetEnd < chunkOffsetStart-1) {
// This offset range doesn't overlap with any chunks.
Expand All @@ -149,9 +144,9 @@ export default class ChunkedFileData {
}

// Find the ending chunk.
for (var i = startChunkIx; i < this._fileData.length; i++) {
var chunkOffsetStart = this._fileData[i].offset;
var chunkOffsetEnd = chunkOffsetStart + this._fileData[i].data.length;
for (let i = startChunkIx; i < this._fileData.length; i++) {
const chunkOffsetStart = this._fileData[i].offset;
const chunkOffsetEnd = chunkOffsetStart + this._fileData[i].data.length;

if (offsetEnd >= chunkOffsetStart-1) {
// Candidate for the end chunk, it doesn't mean it is yet.
Expand All @@ -173,8 +168,8 @@ export default class ChunkedFileData {
}

hasDataRange(offsetStart: number, offsetEnd: number): boolean {
for (var i = 0; i < this._fileData.length; i++) {
var chunk = this._fileData[i];
for (let i = 0; i < this._fileData.length; i++) {
const chunk = this._fileData[i];
if (offsetEnd < chunk.offset) {
return false;
}
Expand All @@ -189,11 +184,11 @@ export default class ChunkedFileData {
}

getByteAt(offset: number): any {
var dataChunk;
let dataChunk;

for (var i = 0; i < this._fileData.length; i++) {
var dataChunkStart = this._fileData[i].offset;
var dataChunkEnd = dataChunkStart + this._fileData[i].data.length - 1;
for (let i = 0; i < this._fileData.length; i++) {
const dataChunkStart = this._fileData[i].offset;
const dataChunkEnd = dataChunkStart + this._fileData[i].data.length - 1;

if (offset >= dataChunkStart && offset <= dataChunkEnd) {
dataChunk = this._fileData[i];
Expand Down
8 changes: 4 additions & 4 deletions src/FLACTagContents.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { bin } from './ByteArrayUtils.js';
import { getInteger24 } from './ByteArrayUtils.js';
import { getInteger32 } from './ByteArrayUtils.js';
import { bin } from "./ByteArrayUtils.js";
import { getInteger24 } from "./ByteArrayUtils.js";
import { getInteger32 } from "./ByteArrayUtils.js";

import type { ByteArray } from './FlowTypes.js';
import type { ByteArray } from "./FlowTypes.js";

export default class FLACTagContents {
declare _blocks: MetadataBlock[];
Expand Down
Loading

0 comments on commit 1389d1f

Please sign in to comment.