mirror of
https://github.com/snobu/destreamer.git
synced 2026-01-17 05:22:18 +00:00
Early work for token cache implementation
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
.token_cache
|
||||
*.txt
|
||||
*.log
|
||||
*.js
|
||||
|
||||
42
TokenCache.ts
Normal file
42
TokenCache.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import * as fs from 'fs';
|
||||
const jwtDecode = require('jwt-decode');
|
||||
|
||||
export class TokenCache {
|
||||
|
||||
public Read(): string | null {
|
||||
let token = null;
|
||||
try {
|
||||
token = fs.readFileSync(".token_cache", "utf8");
|
||||
}
|
||||
catch (e) {
|
||||
console.error(e);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
interface Jwt {
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
const decodedJwt: Jwt = jwtDecode(token);
|
||||
|
||||
let now = Math.floor(Date.now() / 1000);
|
||||
let exp = decodedJwt["exp"];
|
||||
let timeLeft = exp - now;
|
||||
|
||||
if (timeLeft < 120) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
public Write(token: string): void {
|
||||
fs.writeFile(".token_cache", token, (err: any) => {
|
||||
if (err) {
|
||||
return console.error(err);
|
||||
}
|
||||
console.log("Fresh access token dropped into .token_cache");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
import { BrowserTests } from './BrowserTests';
|
||||
import { TokenCache } from './TokenCache';
|
||||
|
||||
import { execSync } from 'child_process';
|
||||
import puppeteer from 'puppeteer';
|
||||
import { terminal as term } from 'terminal-kit';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { BrowserTests } from './BrowserTests';
|
||||
import yargs from 'yargs';
|
||||
import sanitize from 'sanitize-filename';
|
||||
import axios from 'axios';
|
||||
|
||||
11
package-lock.json
generated
11
package-lock.json
generated
@@ -60,6 +60,12 @@
|
||||
"integrity": "sha512-8+KAKzEvSUdeo+kmqnKrqgeE+LcA0tjYWFY7RPProVYwnqDjukzO+3b6dLD56rYX5TdWejnEOLJYOIeh4CXKuA==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/jwt-decode": {
|
||||
"version": "2.2.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/jwt-decode/-/jwt-decode-2.2.1.tgz",
|
||||
"integrity": "sha512-aWw2YTtAdT7CskFyxEX2K21/zSDStuf/ikI3yBqmwpwJF0pS+/IX5DWv+1UFffZIbruP6cnT9/LAJV1gFwAT1A==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/mime-types": {
|
||||
"version": "2.1.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/mime-types/-/mime-types-2.1.0.tgz",
|
||||
@@ -1036,6 +1042,11 @@
|
||||
"integrity": "sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=",
|
||||
"dev": true
|
||||
},
|
||||
"jwt-decode": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/jwt-decode/-/jwt-decode-2.2.0.tgz",
|
||||
"integrity": "sha1-fYa9VmefWM5qhHBKZX3TkruoGnk="
|
||||
},
|
||||
"lazyness": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/lazyness/-/lazyness-1.1.1.tgz",
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
"author": "snobu",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/jwt-decode": "^2.2.1",
|
||||
"@types/puppeteer": "^1.20.0",
|
||||
"@types/terminal-kit": "^1.28.1",
|
||||
"@types/yargs": "^15.0.3",
|
||||
@@ -26,6 +27,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^0.19.2",
|
||||
"jwt-decode": "^2.2.0",
|
||||
"puppeteer": "^2.1.1",
|
||||
"sanitize-filename": "^1.6.3",
|
||||
"terminal-kit": "^1.35.2",
|
||||
|
||||
Reference in New Issue
Block a user