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:
12
src/services/__mocks__/timer.js
Normal file
12
src/services/__mocks__/timer.js
Normal 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;
|
||||
@@ -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);
|
||||
|
||||
41
src/services/timeoutScript.js
Normal file
41
src/services/timeoutScript.js
Normal 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
40
src/services/timer.js
Normal 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;
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user