diff --git a/README.md b/README.md index 2293912..1245615 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ A JavaScript implementation of the JSON Object Signing and Encryption (JOSE) for - [Converting to Buffer](#converting-to-buffer) - [URI-Safe Base64](#uri-safe-base64) - [Random Bytes](#random-bytes) +- [Angular Usage](#angular-usage) @@ -794,3 +795,36 @@ This function uses: * `crypto.randomBytes()` on node.js * `crypto.getRandomValues()` on modern browsers * A PRNG based on AES and SHA-1 for older platforms + + +### Angular Usage ### + +Import and use as any other package. All the methods will be supported in the browser, just as in node.js. + +```javascript +import * as jose from 'node-jose'; +``` + +The following changes are required to make few node-modules available in the browser + +- angular compilerOptions for stream + + In `tsconfig.json` , *compilerOptions* add + ```json + "paths": { + "stream": ["../node_modules/stream-browserify/index.js"] + } + ``` + This is to avoid the below error + > ERROR in ./node_modules/browserify-zlib/lib/index.js. + > Module not found: Error: Can't resolve 'stream' in '***/node_modules/browserify-zlib/lib' + +- polyfil for global object + + In `polyfills.ts` add + ``` + // Polyfill for node-jose + (window as any)['global'] = window; + ``` + This is to avoid the below error + > Uncaught ReferenceError: global is not defined \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index baa1f5e..3d9a056 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,6 +9,18 @@ if (typeof Promise === "undefined") { require("es6-promise").polyfill(); } +try { // for browsers + var global = global || window; + // Browser Support: make buffer, that emulates node's Buffer API, available globally in the browser + global.Buffer = global.Buffer || require("buffer").Buffer; + // Browser Support: make process, that emulates node's Process API, available globally in the browser + global.process = global.process || require("process"); +} +catch (e) { + // "window is not defined" for node.js +} + + var JWS = require("./jws"); module.exports = { diff --git a/lib/jwe/decrypt.js b/lib/jwe/decrypt.js index 1d15d1c..ef5026c 100644 --- a/lib/jwe/decrypt.js +++ b/lib/jwe/decrypt.js @@ -8,8 +8,15 @@ var base64url = require("../util/base64url"), AlgConfig = require("../util/algconfig"), JWK = require("../jwk"), - merge = require("../util/merge"), - zlib = require("zlib"); + merge = require("../util/merge"); + +var zlib; +// Browser Support: If 'zlib' can't be resolved use browserify-zlib that emulates Node's zlib module for the browser +try { + zlib = require("zlib") +} catch(e) { + zlib = require("browserify-zlib"); +} var DEFAULT_OPTIONS = { algorithms: "*" diff --git a/lib/jwe/encrypt.js b/lib/jwe/encrypt.js index 80affcf..6925bec 100644 --- a/lib/jwe/encrypt.js +++ b/lib/jwe/encrypt.js @@ -10,9 +10,16 @@ var lodash = require("lodash"), generateCEK = require("./helpers").generateCEK, JWK = require("../jwk"), slice = require("./helpers").slice, - zlib = require("zlib"), CONSTANTS = require("../algorithms/constants"); +var zlib; +// Browser Support: If 'zlib' can't be resolved use browserify-zlib that emulates Node's zlib module for the browser +try { + zlib = require("zlib") +} catch(e) { + zlib = require("browserify-zlib"); +} + var assign = lodash.assign; var clone = lodash.clone; diff --git a/package.json b/package.json index 0bd5984..ce898e9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-jose", - "version": "1.1.3", + "version": "1.1.4", "description": "A JavaScript implementation of the JSON Object Signing and Encryption (JOSE) for current web browsers and node.js-based servers", "keywords": [ "crypto", @@ -31,10 +31,14 @@ }, "dependencies": { "base64url": "^3.0.1", + "browserify-zlib": "^0.2.0", + "buffer": "^5.4.2", "es6-promise": "^4.2.6", "lodash": "^4.17.11", "long": "^4.0.0", "node-forge": "^0.8.1", + "process": "^0.11.10", + "stream-browserify": "^2.0.2", "uuid": "^3.3.2" }, "devDependencies": { @@ -73,4 +77,4 @@ "webpack-stream": "^4.0.0", "yargs": "^11.1.0" } -} +} \ No newline at end of file