diff --git a/.gitignore b/.gitignore index 1606d7b..1f6c9b8 100644 --- a/.gitignore +++ b/.gitignore @@ -19,5 +19,4 @@ npm-debug.log node_modules build/* !build/jwt-decode.js -coverage/* -dist/* \ No newline at end of file +coverage/* \ No newline at end of file diff --git a/README.md b/README.md index e0720ea..b10c74b 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ const jwt_decode = require('jwt-decode'); #### Include with a script tag -Copy the file `jwt-decode.js` from the `dist/` folder to your project somewhere, then include it like so: +Copy the file `jwt-decode.js` from the `build/` folder to your project somewhere, then include it like so: ```html diff --git a/build/jwt-decode.js b/build/jwt-decode.js new file mode 100644 index 0000000..261e2b8 --- /dev/null +++ b/build/jwt-decode.js @@ -0,0 +1,82 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : + typeof define === 'function' && define.amd ? define(factory) : + (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.jwt_decode = factory()); +})(this, (function () { 'use strict'; + + const global = (typeof globalThis !== "undefined" && globalThis) || window; + var atob = global.atob.bind(global); + + function b64DecodeUnicode(str) { + return decodeURIComponent(atob(str).replace(/(.)/g, function (m, p) { + var code = p.charCodeAt(0).toString(16).toUpperCase(); + if (code.length < 2) { + code = "0" + code; + } + return "%" + code; + })); + } + function base64_url_decode (str) { + var output = str.replace(/-/g, "+").replace(/_/g, "/"); + switch (output.length % 4) { + case 0: + break; + case 2: + output += "=="; + break; + case 3: + output += "="; + break; + default: + throw new Error("base64 string is not of the correct length"); + } + try { + return b64DecodeUnicode(output); + } + catch (err) { + return atob(output); + } + } + + class InvalidTokenError extends Error { + constructor(message) { + super(message); + } + } + InvalidTokenError.prototype.name = "InvalidTokenError"; + function jwtDecode(token, options = { header: false }) { + if (typeof token !== "string") { + throw new InvalidTokenError("Invalid token specified: must be a string"); + } + options = options || {}; + var pos = options.header === true ? 0 : 1; + var part = token.split(".")[pos]; + if (typeof part !== "string") { + throw new InvalidTokenError("Invalid token specified: missing part #" + (pos + 1)); + } + try { + var decoded = base64_url_decode(part); + } + catch (e) { + throw new InvalidTokenError("Invalid token specified: invalid base64 for part #" + + (pos + 1) + + " (" + + e.message + + ")"); + } + try { + return JSON.parse(decoded); + } + catch (e) { + throw new InvalidTokenError("Invalid token specified: invalid json for part #" + + (pos + 1) + + " (" + + e.message + + ")"); + } + } + + return jwtDecode; + +})); +//# sourceMappingURL=jwt-decode.js.map diff --git a/package.json b/package.json index a96461f..fb8a211 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,9 @@ "name": "jwt-decode", "version": "3.1.2", "description": "Decode JWT tokens, mostly useful for browser applications.", - "main": "dist/cjs/jwt-decode.js", - "module": "dist/esm/jwt-decode.js", - "types": "dist/typings/index.d.ts", + "main": "build/cjs/jwt-decode.js", + "module": "build/esm/jwt-decode.js", + "types": "build/typings/index.d.ts", "keywords": [ "jwt", "browser" @@ -17,8 +17,8 @@ "homepage": "https://github.com/auth0/jwt-decode#readme", "scripts": { "dev": "rollup -m -c", - "build": "rimraf dist && rollup -m -c --environment NODE_ENV:production", - "postbuild": "echo '{\"type\": \"commonjs\"}'> dist/cjs/package.json", + "build": "rimraf build && rollup -m -c --environment NODE_ENV:production", + "postbuild": "echo '{\"type\": \"commonjs\"}'> build/cjs/package.json", "test": "npm run test:node && npm run test:browser", "test:node": "NODE_OPTIONS='--experimental-vm-modules --no-warnings' jest --coverage", "test:browser": "NODE_OPTIONS='--experimental-vm-modules --no-warnings' jest --coverage --testEnvironment=jsdom" @@ -50,7 +50,7 @@ "uglify-js": "^3.15.2" }, "files": [ - "dist/", + "build/", "index.d.ts" ], "engines": { @@ -59,9 +59,9 @@ "type": "module", "exports": { ".": { - "require": "./dist/cjs/jwt-decode.js", - "import": "./dist/esm/jwt-decode.js", - "types": "./dist/typings/index.d.ts" + "require": "./build/cjs/jwt-decode.js", + "import": "./build/esm/jwt-decode.js", + "types": "./build/typings/index.d.ts" } } } diff --git a/rollup.config.js b/rollup.config.js index 57a50cc..9c5ec6f 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -34,7 +34,7 @@ export default [{ input: "lib/index.standalone.ts", output: { name: "jwt_decode", - file: "dist/jwt-decode.js", + file: "build/jwt-decode.js", format: "umd", }, plugins: [ @@ -55,7 +55,7 @@ export default [{ input: "lib/index.cjs.ts", output: [{ name: EXPORT_NAME, - file: "dist/cjs/jwt-decode.js", + file: "build/cjs/jwt-decode.js", format: "cjs", exports: "auto", }, ], @@ -65,12 +65,12 @@ export default [{ input: "lib/index.ts", output: [{ name: EXPORT_NAME, - file: "dist/esm/jwt-decode.js", + file: "build/esm/jwt-decode.js", format: "esm", }, ], plugins: [!isProduction && serve({ - contentBase: ["dist", "static"], + contentBase: ["build", "static"], open: true, port: 3000, }), !isProduction && livereload(), diff --git a/tsconfig.json b/tsconfig.json index c136997..ffe5ca1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,7 @@ "esModuleInterop": true, "sourceMap": true, "declaration": true, - "declarationDir": "./dist/typings", + "declarationDir": "./build/typings", "moduleResolution": "node", "noImplicitAny": true, "downlevelIteration": true, @@ -16,6 +16,6 @@ "strictFunctionTypes": true, "strictPropertyInitialization": true, }, - "exclude": ["./test", "./dist/typings"] + "exclude": ["./test", "./build/typings"] } \ No newline at end of file