From 8186e1bb1c600b4173996bbf3edf6e190bf77c3c Mon Sep 17 00:00:00 2001 From: Michael Roberts Date: Fri, 14 Jan 2022 12:38:07 +0000 Subject: [PATCH 1/2] feat: Added readBlockAsArrayBuffer() in utilities module. feat: Added readBlockAsArrayBuffer() in utilities module. --- src/index.ts | 2 +- src/utilities/index.ts | 2 ++ src/utilities/readBlockAsArrayBuffer.ts | 29 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/utilities/readBlockAsArrayBuffer.ts 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/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..e7df881 --- /dev/null +++ b/src/utilities/readBlockAsArrayBuffer.ts @@ -0,0 +1,29 @@ +/** + * + * 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: File, + callback: (result: ArrayBuffer) => unknown +) => { + 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)) + } + } + + reader.onerror = reject + + reader.readAsArrayBuffer(block) + }) +} From 54fb921cc241be68e2b7686e1d44817865b05bea Mon Sep 17 00:00:00 2001 From: Michael Roberts Date: Fri, 14 Jan 2022 16:45:33 +0000 Subject: [PATCH 2/2] feat: Improved TS for the readBlockAsArrayBuffer() call. feat: Improved TS for the readBlockAsArrayBuffer() call. Added 's' to the FITSBlock interface which holds the buffer as a raw parsed string. --- src/types/block.ts | 6 ++++++ src/utilities/readBlockAsArrayBuffer.ts | 12 +++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) 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/readBlockAsArrayBuffer.ts b/src/utilities/readBlockAsArrayBuffer.ts index e7df881..30adaa8 100644 --- a/src/utilities/readBlockAsArrayBuffer.ts +++ b/src/utilities/readBlockAsArrayBuffer.ts @@ -10,9 +10,9 @@ * @returns a Promise which resolves the callback of the callback function provided */ export const readBlockAsArrayBuffer = async ( - block: File, - callback: (result: ArrayBuffer) => unknown -) => { + 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() @@ -20,6 +20,12 @@ export const readBlockAsArrayBuffer = async ( if (e.target?.result && e.target.result instanceof ArrayBuffer) { resolve(callback(e.target.result)) } + + resolve({ + s: '', + end: false, + parsed: false + }) } reader.onerror = reject