Skip to content

Commit

Permalink
push everything
Browse files Browse the repository at this point in the history
  • Loading branch information
junhaoliao committed Jan 23, 2025
1 parent b355b30 commit c191db4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/services/LogFileManager/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ const loadFile = async (fileSrc: FileSrcType)
let fileData: Uint8Array;
if ("string" === typeof fileSrc) {
fileName = getBasenameFromUrlOrDefault(fileSrc);
fileData = await getUint8ArrayFrom(fileSrc, () => null);
fileData = await getUint8ArrayFrom(fileSrc, (numBytesDownloaded, numBytesTotal) => {
console.log(`Downloading ${numBytesDownloaded} of ${numBytesTotal} bytes...`);
});
} else {
fileName = fileSrc.name;
fileData = new Uint8Array(await fileSrc.arrayBuffer());
Expand Down
64 changes: 53 additions & 11 deletions src/utils/http.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import axios, {AxiosError} from "axios";
import axios, {
AxiosError,
AxiosProgressEvent,
} from "axios";


type ProgressCallback = (numBytesDownloaded:number, numBytesTotal:number) => void;
Expand Down Expand Up @@ -32,25 +35,62 @@ const convertAxiosError = (e: AxiosError): Error => {
);
};


/**
* Normalizes total size if undefined and calls the provided onProgress callback with loaded and
* total sizes.
*
* @param onProgress
* @return The handler that wraps `onProgress`.
*/
const handleDownloadProgress = (onProgress: ProgressCallback) => ({
loaded,
total,
}: AxiosProgressEvent) => {
if ("undefined" === typeof total) {
total = loaded;
}
onProgress(loaded, total);
};

/**
* Retrieves an object that is stored as JSON in a remote location.
*
* @param remoteUrl
* @param onDownloadProgress
* @return The parsed JSON object.
* @throws {Error} if the download fails.
*/
const getJsonObjectFrom<Response> = async (remoteUrl: string, onDownloadProgress: ProgressCallback)

Check failure on line 64 in src/utils/http.ts

View workflow job for this annotation

GitHub Actions / lint-check

Parsing error: ','
: Promise<object> => {
try {
const {data} = await axios.get<ArrayBuffer>(remoteUrl, {
responseType: "json",
onDownloadProgress: handleDownloadProgress(onDownloadProgress),
});

return new Uint8Array(data);
} catch (e) {
throw (e instanceof AxiosError) ?
convertAxiosError(e) :
e;
}
};

/**
* Downloads (bypassing any caching) a file as a Uint8Array.
*
* @param fileUrl
* @param progressCallback
* @param onProgress
* @return The file's content.
* @throws {Error} if the download fails.
*/
const getUint8ArrayFrom = async (fileUrl: string, progressCallback: ProgressCallback)
const getUint8ArrayFrom = async (fileUrl: string, onProgress: ProgressCallback)
: Promise<Uint8Array> => {
try {
const {data} = await axios.get<ArrayBuffer>(fileUrl, {
responseType: "arraybuffer",
onDownloadProgress: ({loaded, total}) => {
if ("undefined" === typeof total) {
total = loaded;
}
progressCallback(loaded, total);
},
onDownloadProgress: handleDownloadProgress(onProgress),
headers: {
"Cache-Control": "no-cache",
"Pragma": "no-cache",
Expand All @@ -66,5 +106,7 @@ const getUint8ArrayFrom = async (fileUrl: string, progressCallback: ProgressCall
}
};


export {getUint8ArrayFrom};
export {
getJsonObjectFrom,
getUint8ArrayFrom,
};

0 comments on commit c191db4

Please sign in to comment.