Skip to content

Commit 63d7f66

Browse files
Add getOptions for NodeJS
1 parent a365808 commit 63d7f66

File tree

9 files changed

+76
-80
lines changed

9 files changed

+76
-80
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@splitsoftware/splitio",
3-
"version": "10.26.1-rc.1",
3+
"version": "10.26.1-rc.2",
44
"description": "Split SDK",
55
"files": [
66
"README.md",
@@ -40,7 +40,7 @@
4040
"node": ">=6"
4141
},
4242
"dependencies": {
43-
"@splitsoftware/splitio-commons": "1.15.1-rc.1",
43+
"@splitsoftware/splitio-commons": "1.15.1-rc.2",
4444
"@types/google.analytics": "0.0.40",
4545
"@types/ioredis": "^4.28.0",
4646
"bloom-filters": "^3.0.0",

src/__tests__/nodeSuites/push-synchronization.spec.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const MILLIS_IFFU_UPDATE_EVENT_WITH_NEW_SEGMENTS = 700;
5858
const MILLIS_IFFU_UPDATE_EVENT_WITH_WRONG_COMPRESS = 800;
5959
const MILLIS_IFFU_UPDATE_EVENT_WITH_OLD_CHANGENUMBER = 900;
6060
const MILLIS_IFFU_UPDATE_EVENT_WITH_ZERO_PCN = 1000;
61-
const MILLIS_IFFU_UPDATE_EVENT_WITH_MISSING_PCN= 1100;
61+
const MILLIS_IFFU_UPDATE_EVENT_WITH_MISSING_PCN = 1100;
6262
const MILLIS_IFFU_UPDATE_EVENT_WITH_ARCHIVED = 1200;
6363
const MILLIS_DESTROY = 1300;
6464

@@ -89,13 +89,11 @@ export function testSynchronization(fetchMock, assert) {
8989
setMockListener(function (eventSourceInstance) {
9090
const expectedSSEurl = `${url(settings, '/sse')}?channels=NzM2MDI5Mzc0_NDEzMjQ1MzA0Nw%3D%3D_segments,NzM2MDI5Mzc0_NDEzMjQ1MzA0Nw%3D%3D_splits,%5B%3Foccupancy%3Dmetrics.publishers%5Dcontrol_pri,%5B%3Foccupancy%3Dmetrics.publishers%5Dcontrol_sec&accessToken=${authPushEnabled.token}&v=1.1&heartbeats=true`;
9191
assert.equals(eventSourceInstance.url, expectedSSEurl, 'EventSource URL is the expected');
92-
assert.deepEqual(eventSourceInstance.__eventSourceInitDict, {
93-
headers: {
94-
SplitSDKClientKey: 'h-1>',
95-
SplitSDKVersion: settings.version,
96-
SplitSDKMachineIP: settings.runtime.ip,
97-
SplitSDKMachineName: settings.runtime.hostname
98-
}
92+
assert.deepEqual(eventSourceInstance.__eventSourceInitDict.headers, {
93+
SplitSDKClientKey: 'h-1>',
94+
SplitSDKVersion: settings.version,
95+
SplitSDKMachineIP: settings.runtime.ip,
96+
SplitSDKMachineName: settings.runtime.hostname
9997
}, 'EventSource headers are the expected');
10098

10199
setTimeout(() => {
@@ -285,7 +283,7 @@ export function testSynchronization(fetchMock, assert) {
285283

286284
// fetch segments due to IFFU SPLIT_UPDATE event with new segments
287285
fetchMock.getOnce(url(settings, '/segmentChanges/maur-2?since=-1'), function () {
288-
return { status: 200, body: { since: 1457552650000, till: 1457552650000, name: 'maur-2', added: ['admin','mauro','nico'], removed: [] } };
286+
return { status: 200, body: { since: 1457552650000, till: 1457552650000, name: 'maur-2', added: ['admin', 'mauro', 'nico'], removed: [] } };
289287
});
290288

291289
// fetch feature flags due to IFFU SPLIT_UPDATE event with wrong compress code
Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,8 @@
11
import tape from 'tape-catch';
2-
import sinon from 'sinon';
3-
import { getFetch, __setFetch } from '../node';
2+
import { getFetch } from '../node';
43

5-
tape('getFetch returns a wrapped node-fetch module in Node', assert => {
6-
assert.equal(typeof getFetch(), 'function');
7-
8-
assert.end();
9-
});
10-
11-
tape('getFetch passes an agent object to HTTPs requests', assert => {
12-
const fetchMock = sinon.stub();
13-
__setFetch(fetchMock);
14-
15-
const fetch = getFetch();
16-
17-
fetch('http://test.com');
18-
assert.true(fetchMock.calledWithExactly('http://test.com', { agent: undefined }));
19-
20-
fetch('https-https://', { option: 'value' });
21-
assert.true(fetchMock.calledWithExactly('https-https://', { option: 'value', agent: undefined }));
22-
23-
fetch('https://test.com');
24-
assert.true(fetchMock.calledWithExactly('https://test.com', { agent: sinon.match.object }));
25-
26-
fetch('https://test.com', { option: 'value' });
27-
assert.true(fetchMock.calledWithExactly('https://test.com', { option: 'value', agent: sinon.match.object }));
28-
29-
// Restore
30-
__setFetch(require('node-fetch'));
4+
tape('getFetch returns node-fetch module in Node', assert => {
5+
assert.equal(getFetch(), require('node-fetch'));
316

327
assert.end();
338
});

src/platform/getFetch/node.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
/* eslint-disable compat/compat */
2-
import https from 'https';
3-
4-
// @TODO
5-
// 1- handle multiple protocols automatically
6-
// 2- destroy it once the sdk is destroyed
7-
const agent = new https.Agent({
8-
keepAlive: true,
9-
keepAliveMsecs: 1500
10-
});
11-
121
let nodeFetch;
132

143
try {
@@ -29,12 +18,7 @@ export function __setFetch(fetch) {
2918

3019
/**
3120
* Retrieves 'node-fetch', a Fetch API polyfill for NodeJS, with fallback to global 'fetch' if available.
32-
* It passes an https agent with keepAlive enabled if URL is https.
3321
*/
3422
export function getFetch() {
35-
if (nodeFetch) {
36-
return (url, options) => {
37-
return nodeFetch(url, Object.assign({ agent: url.startsWith('https:') ? agent : undefined }, options));
38-
};
39-
}
23+
return nodeFetch;
4024
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import tape from 'tape-catch';
2+
import { settingsFactory } from '../../../settings/node';
3+
import { getOptions } from '../node';
4+
5+
tape('getOptions returns an object with a custom agent if all urls are https', assert => {
6+
const settings = settingsFactory({});
7+
assert.true(typeof getOptions(settings).agent === 'object');
8+
9+
assert.end();
10+
});
11+
12+
tape('getOptions returns undefined if some url is not https', assert => {
13+
const settings = settingsFactory({ urls: { sdk: 'http://sdk.split.io' } });
14+
assert.equal(getOptions(settings), undefined);
15+
16+
assert.end();
17+
});

src/platform/getOptions/node.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// @TODO
2+
// 1- handle multiple protocols automatically
3+
// 2- destroy it once the sdk is destroyed
4+
import https from 'https';
5+
6+
import { find } from '@splitsoftware/splitio-commons/src/utils/lang';
7+
8+
const agent = new https.Agent({
9+
keepAlive: true,
10+
keepAliveMsecs: 1500
11+
});
12+
13+
export function getOptions(settings) {
14+
// If some URL is not HTTPS, we don't use the agent, to let the SDK connect to HTTP endpoints
15+
if (find(settings.urls, url => !url.startsWith('https:'))) return;
16+
17+
return {
18+
agent
19+
};
20+
}

src/platform/node.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import EventEmitter from 'events';
22
import { getFetch } from '../platform/getFetch/node';
33
import { getEventSource } from '../platform/getEventSource/node';
4+
import { getOptions } from '../platform/getOptions/node';
45
import { NodeSignalListener } from '@splitsoftware/splitio-commons/src/listeners/node';
56
import { now } from '@splitsoftware/splitio-commons/src/utils/timeTracker/now/node';
67

78
export const platform = {
89
getFetch,
910
getEventSource,
11+
getOptions,
1012
EventEmitter,
1113
now
1214
};

src/settings/defaults/version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const packageVersion = '10.26.1-rc.1';
1+
export const packageVersion = '10.26.1-rc.2';

0 commit comments

Comments
 (0)