fix(superset-embedded-sdk): Buffer is not defined (#21641)

This commit is contained in:
kevin 2022-10-17 16:14:28 +08:00 committed by GitHub
parent 6f2e76bc09
commit 7ec136fec2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 3 deletions

View File

@ -9,7 +9,8 @@
"version": "0.1.0-alpha.7",
"license": "Apache-2.0",
"dependencies": {
"@superset-ui/switchboard": "^0.18.26-0"
"@superset-ui/switchboard": "^0.18.26-0",
"jwt-decode": "^3.1.2"
},
"devDependencies": {
"@babel/cli": "^7.16.8",
@ -6495,6 +6496,11 @@
"node": ">=6"
}
},
"node_modules/jwt-decode": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/jwt-decode/-/jwt-decode-3.1.2.tgz",
"integrity": "sha512-UfpWE/VZn0iP50d8cz9NrZLM9lSWhcJ+0Gt/nm4by88UL+J1SiKN8/5dkjMmbEzwL2CAe+67GsegCbIKtbp75A=="
},
"node_modules/kind-of": {
"version": "6.0.3",
"resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.3.tgz",
@ -13042,6 +13048,11 @@
"minimist": "^1.2.5"
}
},
"jwt-decode": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/jwt-decode/-/jwt-decode-3.1.2.tgz",
"integrity": "sha512-UfpWE/VZn0iP50d8cz9NrZLM9lSWhcJ+0Gt/nm4by88UL+J1SiKN8/5dkjMmbEzwL2CAe+67GsegCbIKtbp75A=="
},
"kind-of": {
"version": "6.0.3",
"resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.3.tgz",

View File

@ -33,7 +33,8 @@
"last 3 edge versions"
],
"dependencies": {
"@superset-ui/switchboard": "^0.18.26-0"
"@superset-ui/switchboard": "^0.18.26-0",
"jwt-decode": "^3.1.2"
},
"devDependencies": {
"@babel/cli": "^7.16.8",

View File

@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import jwt_decode from "jwt-decode";
export const REFRESH_TIMING_BUFFER_MS = 5000 // refresh guest token early to avoid failed superset requests
export const MIN_REFRESH_WAIT_MS = 10000 // avoid blasting requests as fast as the cpu can handle
@ -23,7 +24,7 @@ export const DEFAULT_TOKEN_EXP_MS = 300000 // (5 min) used only when parsing gue
// when do we refresh the guest token?
export function getGuestTokenRefreshTiming(currentGuestToken: string) {
const parsedJwt = JSON.parse(Buffer.from(currentGuestToken.split('.')[1], 'base64').toString());
const parsedJwt = jwt_decode<Record<string, any>>(currentGuestToken);
// if exp is int, it is in seconds, but Date() takes milliseconds
const exp = new Date(/[^0-9\.]/g.test(parsedJwt.exp) ? parsedJwt.exp : parseFloat(parsedJwt.exp) * 1000);
const isValidDate = exp.toString() !== 'Invalid Date';