diff --git a/src/index.ts b/src/index.ts index 335ebd5..0569d05 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,4 +8,4 @@ export { getFITSHeaders, parseFITSHeaderRow } from './header' export { getFITSBlocks } from './fits' // Utilities -export { getExcessByteSize } from './utilities' +export { getExcessByteSize, readBlockAsArrayBuffer } from './utilities' diff --git a/src/types/block.ts b/src/types/block.ts index 2bcdaf2..0e13ea0 100644 --- a/src/types/block.ts +++ b/src/types/block.ts @@ -5,6 +5,12 @@ export interface FITSBlock { * */ buffer: string | ArrayBuffer + /** + * + * The ArrayBuffer as a raw parsed string + * + */ + s?: string /** * * diff --git a/src/utilities/index.ts b/src/utilities/index.ts index 657dce4..c0bd031 100644 --- a/src/utilities/index.ts +++ b/src/utilities/index.ts @@ -1 +1,3 @@ export { getExcessByteSize } from './getExcessByteSize' + +export { readBlockAsArrayBuffer } from './readBlockAsArrayBuffer' diff --git a/src/utilities/readBlockAsArrayBuffer.ts b/src/utilities/readBlockAsArrayBuffer.ts new file mode 100644 index 0000000..30adaa8 --- /dev/null +++ b/src/utilities/readBlockAsArrayBuffer.ts @@ -0,0 +1,35 @@ +/** + * + * readBlockAsArrayBuffer() + * + * @description asynchronously reads a file slice as an ArrayBuffer + * and onloadend will pass the ArrayBuffer result to a callback function. + * + * @param block represents a File or File.slice + * @param callback function callback to be called onloadend passing + * @returns a Promise which resolves the callback of the callback function provided + */ +export const readBlockAsArrayBuffer = async ( + block: Blob, + callback: (result: ArrayBuffer) => { s: string; end: boolean; parsed: boolean } +): Promise<{ s: string; end: boolean; parsed: boolean }> => { + return new Promise((resolve, reject) => { + const reader = new FileReader() + + reader.onloadend = (e: ProgressEvent) => { + if (e.target?.result && e.target.result instanceof ArrayBuffer) { + resolve(callback(e.target.result)) + } + + resolve({ + s: '', + end: false, + parsed: false + }) + } + + reader.onerror = reject + + reader.readAsArrayBuffer(block) + }) +}