From 53e7f06b03cfcbcd4ee23b8d6d63f357d9b1006a Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sun, 3 Dec 2017 15:37:56 +0100 Subject: [PATCH] Add polyfill for WebSocket (node) (#31) --- README.md | 2 +- packages/api-format/package.json | 2 +- packages/api-provider/package.json | 5 ++-- packages/api-provider/src/ws/index.js | 2 ++ packages/api-provider/src/ws/polyfill.js | 6 +++++ packages/api-provider/src/ws/polyfill.spec.js | 24 +++++++++++++++++++ packages/api/package.json | 2 +- yarn.lock | 22 ++++++++++++++--- 8 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 packages/api-provider/src/ws/polyfill.js create mode 100644 packages/api-provider/src/ws/polyfill.spec.js diff --git a/README.md b/README.md index ba4b03353a..174367cce4 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [![coverage](https://img.shields.io/coveralls/polkadot-js/api.svg?style=flat-square)](https://coveralls.io/github/polkadot-js/api?branch=master) [![greenkeeper](https://img.shields.io/badge/greenkeeper-enabled-brightgreen.svg?style=flat-square)](https://greenkeeper.io/) -# @polkadot/client +# @polkadot/api This library provides a clean wrapper around all the methods exposed by a Polkadot network client. As part of the JsonRpc defintions, the [exposed methods are documented](packages/api-jsonrpc/docs/). diff --git a/packages/api-format/package.json b/packages/api-format/package.json index da3fab1060..fe4da0a0e5 100644 --- a/packages/api-format/package.json +++ b/packages/api-format/package.json @@ -33,7 +33,7 @@ "@polkadot/api-jsonrpc": "^0.5.11" }, "dependencies": { - "@polkadot/util": "^0.6.6", + "@polkadot/util": "^0.6.7", "babel-runtime": "^6.26.0" } } diff --git a/packages/api-provider/package.json b/packages/api-provider/package.json index 8d01c8591e..6f30b52090 100644 --- a/packages/api-provider/package.json +++ b/packages/api-provider/package.json @@ -34,8 +34,9 @@ "nock": "^9.1.0" }, "dependencies": { - "@polkadot/util": "^0.6.6", + "@polkadot/util": "^0.6.7", "babel-runtime": "^6.26.0", - "isomorphic-fetch": "^2.2.1" + "isomorphic-fetch": "^2.2.1", + "ws": "^3.3.2" } } diff --git a/packages/api-provider/src/ws/index.js b/packages/api-provider/src/ws/index.js index a8a90e2be9..730ee7abad 100644 --- a/packages/api-provider/src/ws/index.js +++ b/packages/api-provider/src/ws/index.js @@ -11,6 +11,8 @@ type WsMessageType = { data: any }; +require('./polyfill'); + const JsonRpcCoder = require('../jsonRpcCoder'); module.exports = class WsProvider extends JsonRpcCoder implements ProviderInterface { diff --git a/packages/api-provider/src/ws/polyfill.js b/packages/api-provider/src/ws/polyfill.js new file mode 100644 index 0000000000..4591359e36 --- /dev/null +++ b/packages/api-provider/src/ws/polyfill.js @@ -0,0 +1,6 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +if (typeof WebSocket === 'undefined') { + global.WebSocket = require('ws'); +} diff --git a/packages/api-provider/src/ws/polyfill.spec.js b/packages/api-provider/src/ws/polyfill.spec.js new file mode 100644 index 0000000000..86bae41f29 --- /dev/null +++ b/packages/api-provider/src/ws/polyfill.spec.js @@ -0,0 +1,24 @@ +// ISC, Copyright 2017 Jaco Greeff + +describe('ws/polyfill', () => { + let origWs; + + beforeEach(() => { + origWs = global.WebSocket; + global.fetch = null; + }); + + afterEach(() => { + global.WebSocket = origWs; + }); + + it('polyfills with no exceptions (without WebSocket)', () => { + expect(require('./polyfill')).toBeDefined(); + }); + + it('polyfills with no exceptions (with WebSocket)', () => { + global.WebSocket = () => true; + + expect(require('./polyfill')).toBeDefined(); + }); +}); diff --git a/packages/api/package.json b/packages/api/package.json index fea038f970..925b01da3a 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -33,7 +33,7 @@ "@polkadot/api-format": "^0.5.11", "@polkadot/api-jsonrpc": "^0.5.11", "@polkadot/api-provider": "^0.5.11", - "@polkadot/util": "^0.6.6", + "@polkadot/util": "^0.6.7", "babel-runtime": "^6.26.0" } } diff --git a/yarn.lock b/yarn.lock index a1a05e6ff5..19dd438964 100644 --- a/yarn.lock +++ b/yarn.lock @@ -91,9 +91,9 @@ stylelint "^8.2.0" stylelint-config-standard "^18.0.0" -"@polkadot/util@^0.6.6": - version "0.6.6" - resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.6.6.tgz#75cbbaa4215dcc353b680613237a76515b00833f" +"@polkadot/util@^0.6.7": + version "0.6.7" + resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.6.7.tgz#eb9a6bd3717b69c962f6fb70830ebc60380c333f" dependencies: babel-runtime "^6.26.0" bn.js "^4.11.8" @@ -305,6 +305,10 @@ async-each@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/async-each/-/async-each-1.0.1.tgz#19d386a1d9edc6e7c1c85d388aedbcc56d33602d" +async-limiter@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.0.tgz#78faed8c3d074ab81f22b4e985d79e8738f720f8" + async@^1.4.0, async@^1.5.0: version "1.5.2" resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a" @@ -5050,6 +5054,10 @@ uid-number@^0.0.6: version "0.0.6" resolved "https://registry.yarnpkg.com/uid-number/-/uid-number-0.0.6.tgz#0ea10e8035e8eb5b8e4449f06da1c730663baa81" +ultron@~1.1.0: + version "1.1.1" + resolved "https://registry.yarnpkg.com/ultron/-/ultron-1.1.1.tgz#9fe1536a10a664a65266a1e3ccf85fd36302bc9c" + unherit@^1.0.4: version "1.1.0" resolved "https://registry.yarnpkg.com/unherit/-/unherit-1.1.0.tgz#6b9aaedfbf73df1756ad9e316dd981885840cd7d" @@ -5309,6 +5317,14 @@ write@^0.2.1: dependencies: mkdirp "^0.5.1" +ws@^3.3.2: + version "3.3.2" + resolved "https://registry.yarnpkg.com/ws/-/ws-3.3.2.tgz#96c1d08b3fefda1d5c1e33700d3bfaa9be2d5608" + dependencies: + async-limiter "~1.0.0" + safe-buffer "~5.1.0" + ultron "~1.1.0" + x-is-function@^1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/x-is-function/-/x-is-function-1.0.4.tgz#5d294dc3d268cbdd062580e0c5df77a391d1fa1e"