Skip to content
This repository was archived by the owner on Dec 11, 2022. It is now read-only.

Commit 29b5cd3

Browse files
committed
Provide ponyfills instead of polyfills. Version bump.
1 parent a1e4a02 commit 29b5cd3

File tree

4 files changed

+42
-48
lines changed

4 files changed

+42
-48
lines changed

README.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ npm install --save nymph-client-node
1212

1313
This repository is the JavaScript client for Node. There is also a **[browser client](https://github.com/sciactive/nymph-client)**. For more information, you can see the [main Nymph repository](https://github.com/sciactive/nymph).
1414

15-
The Nymph client requires some browser globals that Node doesn't provide, so this client sets up some globals to provide them.
16-
17-
It also sets up Nymph.init to also call PubSub.init with your configs, so you don't need to set up PubSub yourself.
15+
This package provides fetch and WebSocket ponyfills to Nymph, handles Tilmeld auth tokens, and sets up `Nymph.init` to also call `PubSub.init` with your configs if you provide a `pubsubURL`.
1816

1917
## Usage
2018

@@ -24,14 +22,6 @@ To use, require it instead of `nymph-client`:
2422
const { Nymph } = require('nymph-client-node');
2523
```
2624

27-
Or, if you need cookie support (like, if you're using [Tilmeld](http://tilmeld.org/)):
28-
29-
```js
30-
const NymphNode = require('nymph-client-node');
31-
NymphNode.enableCookies();
32-
const { Nymph } = NymphNode;
33-
```
34-
3525
Then provide the options to Nymph.init:
3626

3727
```js

index.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
// Nymph Node Client
22

3-
// (really, it's just the browser client with some Node libraries to supply fake
4-
// browser globals.)
5-
6-
// Nymph expects the global browser objects XMLHttpRequest and WebSocket.
7-
global.XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
8-
global.WebSocket = require('websocket').w3cwebsocket;
9-
// And sometimes we need the XHR object to support cookies.
10-
const enableCookies = () => {
11-
const xhrc = require('xmlhttprequest-cookie');
12-
global.XMLHttpRequest = xhrc.XMLHttpRequest;
13-
};
14-
3+
// Nymph expects fetch and WebSocket.
4+
const WebSocket = require('websocket').w3cwebsocket;
5+
const fetch = require('node-fetch');
156
const NymphClient = require('nymph-client');
167
const { Nymph, PubSub } = NymphClient;
178

189
// Make a shortcut for PubSub init.
1910
const _init = Nymph.init;
2011
Nymph.init = nymphOptions => {
21-
_init.call(Nymph, nymphOptions);
12+
_init.call(Nymph, {
13+
fetch,
14+
...nymphOptions
15+
});
2216
if (nymphOptions.pubsubURL) {
23-
PubSub.init(nymphOptions);
17+
PubSub.init({
18+
WebSocket,
19+
...nymphOptions
20+
});
2421
}
2522
};
2623

27-
module.exports = { Nymph, PubSub, enableCookies, ...NymphClient };
24+
// Save the Tilmeld auth token and send it in the header.
25+
let authToken = null;
26+
Nymph.on('request', (_url, options) => {
27+
if (authToken) {
28+
options.headers['X-TILMELDAUTH'] = authToken;
29+
}
30+
});
31+
Nymph.on('response', response => {
32+
if (response.headers.has('X-TILMELDAUTH')) {
33+
authToken = response.headers.get('X-TILMELDAUTH');
34+
if (authToken === '') {
35+
authToken = null;
36+
}
37+
}
38+
});
39+
40+
module.exports = NymphClient;

package-lock.json

Lines changed: 9 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nymph-client-node",
3-
"version": "4.0.0",
3+
"version": "4.1.0",
44
"description": "A client wrapper for using Nymph Client in Node.js.",
55
"main": "index.js",
66
"scripts": {
@@ -20,9 +20,8 @@
2020
},
2121
"homepage": "https://github.com/sciactive/nymph-client-node#readme",
2222
"dependencies": {
23-
"nymph-client": "^5.0.1",
24-
"websocket": "^1.0.28",
25-
"xmlhttprequest": "^1.8.0",
26-
"xmlhttprequest-cookie": "^0.9.8"
23+
"node-fetch": "^2.6.0",
24+
"nymph-client": "^5.1.0",
25+
"websocket": "^1.0.28"
2726
}
2827
}

0 commit comments

Comments
 (0)