From 6fd41b93b1926669967196d396d829d7582b57ab Mon Sep 17 00:00:00 2001 From: Paul Dijou Date: Mon, 8 Feb 2016 14:46:32 +0100 Subject: [PATCH] 2.0: Support browserify and webpack --- .gitignore | 3 +++ .npmignore | 1 + bower.json | 2 +- browser.js | 1 + jwt-client.js | 16 ++++++++-------- node.js | 1 + package.json | 13 +++++++++---- test/browserify/index.html | 7 +++++++ test/browserify/main.js | 18 ++++++++++++++++++ test/node/main.js | 2 ++ test/webpack/index.html | 7 +++++++ test/webpack/main.js | 18 ++++++++++++++++++ 12 files changed, 76 insertions(+), 13 deletions(-) create mode 100644 browser.js create mode 100644 node.js create mode 100644 test/browserify/index.html create mode 100644 test/browserify/main.js create mode 100644 test/node/main.js create mode 100644 test/webpack/index.html create mode 100644 test/webpack/main.js diff --git a/.gitignore b/.gitignore index 3c3629e..f6357a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ node_modules +npm-debug.log +test/browserify/main.bundle.js +test/webpack/main.bundle.js diff --git a/.npmignore b/.npmignore index 9daeafb..561b23c 100644 --- a/.npmignore +++ b/.npmignore @@ -1 +1,2 @@ test +karma.conf.js diff --git a/bower.json b/bower.json index 1b64481..9d6d2d1 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "jwt-client", "main": "jwt-client.js", - "version": "0.1.4", + "version": "0.2.0", "homepage": "https://github.com/pauldijou/jwt-client", "authors": [ "Paul Dijou (http://pauldijou.fr)" diff --git a/browser.js b/browser.js new file mode 100644 index 0000000..c99267a --- /dev/null +++ b/browser.js @@ -0,0 +1 @@ +module.exports = require('./jwt-client')(window); diff --git a/jwt-client.js b/jwt-client.js index 2ce14c4..643af6f 100644 --- a/jwt-client.js +++ b/jwt-client.js @@ -1,16 +1,16 @@ -(function (glob, factory) { +(function (global_, factory) { if (typeof exports === 'object') { - module.exports = factory(glob); + module.exports = factory; } else if (typeof define === 'function' && define.amd) { define([], function () { - return factory(glob); + return factory(global_); }); } else { - window.JWT = factory(glob); + window.JWT = factory(global_); } -})(this, function (global) { +})(this, function (_global) { var JWT = {}; JWT.defaults = { @@ -19,20 +19,20 @@ // This is the official token to use for JWT but feel free to use another one if you want tokenPrefix: 'Bearer ', // Where to store the token, by default localStorage - storage: global.localStorage, + storage: _global.localStorage, // In Base64 url-safe mode, padding isn't mandatory, so we will disable it by default // but you can force it by setting this param to true if you want padding: false }; JWT.encode64 = function encode64(value) { - var encoded = global.btoa(global.unescape(global.encodeURIComponent(value))); + var encoded = _global.btoa(_global.unescape(_global.encodeURIComponent(value))); if (!JWT.defaults.padding) { return encoded.replace(/=+$/, ''); } else { return encoded; } }; JWT.decode64 = function decode64(value) { - return global.decodeURIComponent(global.escape(global.atob(value))); + return _global.decodeURIComponent(_global.escape(_global.atob(value))); }; JWT.write = function write(value) { diff --git a/node.js b/node.js new file mode 100644 index 0000000..c29ff7b --- /dev/null +++ b/node.js @@ -0,0 +1 @@ +throw new Error('JWT Client does not support Node.js yet. PR welcome.'); diff --git a/package.json b/package.json index 98ecf77..5041c6f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "jwt-client", "description": "Micro service to read, write and save JWT headers", - "version": "0.1.4", + "version": "0.2.0", "author": "Paul Dijou (http://pauldijou.fr)", "license": "Apache 2", "homepage": "https://github.com/pauldijou/jwt-client", @@ -15,12 +15,16 @@ "keywords": [ "jwt" ], - "main": "jwt-client.js", + "main": "node.js", + "browser": "browser.js", "directories": { "test": "test" }, "scripts": { - "test": "./node_modules/karma/bin/karma start" + "test": "./node_modules/karma/bin/karma start", + "node": "node ./test/node/main.js", + "browserify": "browserify ./test/browserify/main.js -o ./test/browserify/main.bundle.js", + "webpack": "webpack ./test/webpack/main.js ./test/webpack/main.bundle.js" }, "devDependencies": { "karma": "^0.12.x", @@ -31,6 +35,7 @@ "karma-opera-launcher": "^0.1.x", "karma-phantomjs-launcher": "^0.1.x", "karma-safari-launcher": "^0.1.x", - "requirejs": "^2.1.22" + "requirejs": "^2.1.22", + "webpack": "^1.12.13" } } diff --git a/test/browserify/index.html b/test/browserify/index.html new file mode 100644 index 0000000..a1dc345 --- /dev/null +++ b/test/browserify/index.html @@ -0,0 +1,7 @@ + + + + + + + diff --git a/test/browserify/main.js b/test/browserify/main.js new file mode 100644 index 0000000..f096a3c --- /dev/null +++ b/test/browserify/main.js @@ -0,0 +1,18 @@ +console.log('Init browserify'); + +var JWT = require('../../'); + +console.log(JWT); + +var token = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjEyMzQ1Njc4OTAsIm5hbWUiOiJKb2huIERvZSIsImFkbWluIjp0cnVlfQ.eoaDVGTClRdfxUZXiPs3f8FmJDkDE_VCQFXqKxpLsts'; +var value = JWT.read(token); + +console.log('Read', value); +console.log('Validate', JWT.validate(value)); + +JWT.keep(value); +console.log('Keep', window.localStorage.getItem('JWT_TOKEN')); +console.log('Remember', JWT.remember()); + +JWT.forget(); +console.log('Forget', JWT.remember()); diff --git a/test/node/main.js b/test/node/main.js new file mode 100644 index 0000000..ea780c0 --- /dev/null +++ b/test/node/main.js @@ -0,0 +1,2 @@ +var JWT = require('../../'); +// Should throw diff --git a/test/webpack/index.html b/test/webpack/index.html new file mode 100644 index 0000000..a1dc345 --- /dev/null +++ b/test/webpack/index.html @@ -0,0 +1,7 @@ + + + + + + + diff --git a/test/webpack/main.js b/test/webpack/main.js new file mode 100644 index 0000000..5db7f16 --- /dev/null +++ b/test/webpack/main.js @@ -0,0 +1,18 @@ +console.log('Init webpack'); + +var JWT = require('../../'); + +console.log(JWT); + +var token = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjEyMzQ1Njc4OTAsIm5hbWUiOiJKb2huIERvZSIsImFkbWluIjp0cnVlfQ.eoaDVGTClRdfxUZXiPs3f8FmJDkDE_VCQFXqKxpLsts'; +var value = JWT.read(token); + +console.log('Read', value); +console.log('Validate', JWT.validate(value)); + +JWT.keep(value); +console.log('Keep', window.localStorage.getItem('JWT_TOKEN')); +console.log('Remember', JWT.remember()); + +JWT.forget(); +console.log('Forget', JWT.remember());