Web Worker Sign Out and Use Go API (#13)

* Calculate checksum and send with file upload

* Limit file upload and display rejected file error

* Add sign in timeout

* Check auth token structure before setting
Clean up

* Use web worker timer to sign out
Remove checksum
Point to Go ota update

* Remove checksum dependency
This commit is contained in:
John Wu
2021-02-08 15:23:36 -08:00
committed by GitHub
parent 1ddf9fe813
commit e1f0006d5e
13 changed files with 1630 additions and 2173 deletions

View File

@@ -0,0 +1,12 @@
const timer = {
start: jest.fn(),
stop: jest.fn(),
onMessage: jest.fn(),
terminate: jest.fn()
}
const getTimerWorker = () => {
return timer;
}
export default getTimerWorker;

View File

@@ -11,7 +11,7 @@ export const getCancelToken = () => {
return issuedCancelToken;
}
export const uploadFile = async (file, token, onProgress, cancelToken) => {
export const uploadFile = async (file, token, onProgress, cancelToken, hash) => {
if (!uploadFileDelay) return uploadFileResponse;
onProgress(50);
await delay(10000);

View File

@@ -0,0 +1,41 @@
const workerscript = () => {
var INTERVAL = 60000;
// eslint-disable-next-line no-restricted-globals
var me = self;
var timerID = 0;
var deadline;
function startTimer(duration) {
deadline = new Date(Date.now() + duration - INTERVAL);
stopTimer();
timerID = setInterval(function() {
var now = new Date();
if (now > deadline) {
me.postMessage("timeout");
stopTimer();
}
}, INTERVAL);
}
function stopTimer() {
if (timerID > 0) clearInterval(timerID);
timerID = 0;
}
me.onmessage = function(e) {
if (e.data.action === "start") {
startTimer(e.data.duration);
}
else if (e.data.action === "stop") {
stopTimer();
}
}
};
let code = workerscript.toString();
code = code.substring(code.indexOf("{")+1, code.lastIndexOf("}"));
const blob = new Blob([code], {type: "application/javascript"});
const timeout_script = URL.createObjectURL(blob);
module.exports = timeout_script;

40
src/services/timer.js Normal file
View File

@@ -0,0 +1,40 @@
import worker_script from "./timeoutScript";
const getTimerWorker = () => {
const worker = new Worker(worker_script);
let messageHandler = null;
const workerHandler = (e) => {
if (messageHandler === null) return;
messageHandler(e);
}
worker.addEventListener("message", workerHandler);
return {
start: (duration) => {
if (!worker) return;
worker.postMessage({
action: "start",
duration,
});
},
stop: () => {
worker.postMessage({
action: "stop",
});
},
onMessage: (handler) => {
messageHandler = handler;
},
terminate: () => {
worker.removeEventListener("message", workerHandler);
worker.terminate();
messageHandler = null;
}
}
}
export default getTimerWorker;

View File

@@ -1,6 +1,6 @@
import axios from 'axios';
const UPLOAD_ENDPOINT = process.env.REACT_APP_UPLOAD_SERVICE_URL || "https://gw-dev.fiskerdps.com";
const UPLOAD_ENDPOINT = process.env.REACT_APP_UPLOAD_SERVICE_URL || "https://gw-dev.fiskerdps.com/ota_update";
export const getCancelToken = () => {
const token = axios.CancelToken;
@@ -10,10 +10,10 @@ export const getCancelToken = () => {
export const uploadFile = (file, token, onProgress, cancelToken) => {
const form = new FormData();
let options = {
method: 'POST',
method: "POST",
headers: {
'Content-Type': 'multipart/form-data',
'Authorization': `Bearer ${token}`,
"Content-Type": "multipart/form-data",
"Authorization": `Bearer ${token}`,
},
cancelToken,
};