launchdarkly-node-client-sdk 2.0.1 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,14 +16,17 @@ workflows:
16
16
  docker-image: cimg/node:current
17
17
  run-lint: true
18
18
  - build-test-linux:
19
- name: Node 16.3
20
- docker-image: cimg/node:16.3
19
+ name: Node 17.9
20
+ docker-image: cimg/node:17.9
21
+ - build-test-linux:
22
+ name: Node 16.14
23
+ docker-image: cimg/node:16.14
21
24
  - build-test-linux:
22
25
  name: Node 15.14
23
26
  docker-image: cimg/node:15.14
24
27
  - build-test-linux:
25
- name: Node 14.17
26
- docker-image: cimg/node:14.17
28
+ name: Node 14.19
29
+ docker-image: cimg/node:14.19
27
30
  - build-test-linux:
28
31
  name: Node 13.14
29
32
  docker-image: cimg/node:13.14
@@ -41,6 +44,9 @@ jobs:
41
44
  run-lint:
42
45
  type: boolean
43
46
  default: false
47
+ run-contract-tests:
48
+ type: boolean
49
+ default: true
44
50
 
45
51
  docker:
46
52
  - image: <<parameters.docker-image>>
@@ -54,10 +60,7 @@ jobs:
54
60
 
55
61
  - run: npm install
56
62
 
57
- - run:
58
- command: npm test
59
- environment:
60
- JEST_JUNIT_OUTPUT: "reports/junit/js-test-results.xml"
63
+ - run: npm run test
61
64
 
62
65
  - when:
63
66
  condition: <<parameters.run-lint>>
@@ -65,6 +68,19 @@ jobs:
65
68
  - run: npm run lint:all
66
69
  - run: npm run check-typescript
67
70
 
71
+ - when:
72
+ condition: <<parameters.run-contract-tests>>
73
+ steps:
74
+ - run:
75
+ name: start contact test service
76
+ command: npm run contract-test-service
77
+ background: true
78
+ - run:
79
+ name: run contract tests
80
+ command: |
81
+ mkdir -p reports/junit
82
+ TEST_HARNESS_PARAMS="-junit reports/junit/contract-test-results.xml" npm run contract-test-harness
83
+
68
84
  - run:
69
85
  name: dependency audit
70
86
  command: ./scripts/better-audit.sh
package/CHANGELOG.md CHANGED
@@ -2,6 +2,18 @@
2
2
 
3
3
  All notable changes to the LaunchDarkly Client-Side SDK for Node.js will be documented in this file.
4
4
 
5
+ ## [2.0.3] - 2022-03-14
6
+ ### Fixed:
7
+ - The `original` dependency (and therefore its transitive dependency `url-parse`) had accidentally been included twice, once as a dependency of `js-eventsource` and then again directly in the SDK's `package.json`. This has been removed so there is no longer any reference to the vulnerable `url-parse` that was meant to be removed in [2.0.2](https://github.com/launchdarkly/node-client-sdk/releases/tag/2.0.2). (Thanks, [AlexHladin](https://github.com/launchdarkly/node-client-sdk/pull/26)!)
8
+
9
+ ## [2.0.2] - 2022-03-10
10
+ ### Fixed:
11
+ - Removed a transitive dependency on the package `url-parse`, which was flagged in [CVE-2022-0686](https://nvd.nist.gov/vuln/detail/CVE-2022-0686).
12
+
13
+ ## [2.0.1] - 2022-02-17
14
+ ### Fixed:
15
+ - If the SDK receives invalid JSON data from a streaming connection (possibly as a result of the connection being cut off), it now uses its regular error-handling logic: the error is emitted as an `error` event or, if there are no `error` event listeners, it is logged. Previously, the error would be thrown as an unhandled exception.
16
+
5
17
  ## [2.0.0] - 2022-01-26
6
18
  This major version release is for updating Node.js compatibility, simplifying the SDK's dependencies, and removing deprecated names.
7
19
 
package/CONTRIBUTING.md CHANGED
@@ -39,3 +39,9 @@ To verify that the TypeScript declarations compile correctly (this involves comp
39
39
  ```
40
40
  npm run check-typescript
41
41
  ```
42
+
43
+ To run the SDK contract test suite (see [`contract-tests/README.md`](./contract-tests/README.md)):
44
+
45
+ ```bash
46
+ npm run contract-tests
47
+ ```
@@ -0,0 +1,7 @@
1
+ # SDK contract test service
2
+
3
+ This directory contains an implementation of the cross-platform SDK testing protocol defined by https://github.com/launchdarkly/sdk-test-harness. See that project's `README` for details of this protocol, and the kinds of SDK capabilities that are relevant to the contract tests. This code should not need to be updated unless the SDK has added or removed such capabilities.
4
+
5
+ To run these tests locally, run `npm run contract-tests` from the SDK project root directory. This will start the test service, download the correct version of the test harness tool, and run the tests.
6
+
7
+ Or, to test against an in-progress local version of the test harness, run `npm run contract-test-service` from the SDK project root directory; then, in the root directory of the `sdk-test-harness` project, build the test harness and run it from the command line.
@@ -0,0 +1,106 @@
1
+ const express = require('express');
2
+ const bodyParser = require('body-parser');
3
+
4
+ const { Log } = require('./log');
5
+ const { newSdkClientEntity, badCommandError } = require('./sdkClientEntity');
6
+
7
+ const app = express();
8
+ let server = null;
9
+
10
+ const port = 8000;
11
+
12
+ let clientCounter = 0;
13
+ const clients = {};
14
+
15
+ const mainLog = Log('service');
16
+
17
+ app.use(bodyParser.json());
18
+
19
+ app.get('/', (req, res) => {
20
+ res.header('Content-Type', 'application/json');
21
+ res.json({
22
+ capabilities: [
23
+ 'client-side',
24
+ 'service-endpoints',
25
+ 'tags'
26
+ ],
27
+ });
28
+ });
29
+
30
+ app.delete('/', (req, res) => {
31
+ mainLog.info('Test service has told us to exit');
32
+ res.status(204);
33
+ res.send();
34
+
35
+ // Defer the following actions till after the response has been sent
36
+ setTimeout(() => {
37
+ server.close(() => process.exit());
38
+ // We force-quit with process.exit because, even after closing the server, there could be some
39
+ // scheduled tasks lingering if an SDK instance didn't get cleaned up properly, and we don't want
40
+ // that to prevent us from quitting.
41
+ }, 1);
42
+ });
43
+
44
+ app.post('/', async (req, res) => {
45
+ const options = req.body;
46
+
47
+ clientCounter += 1;
48
+ const clientId = clientCounter.toString();
49
+ const resourceUrl = `/clients/${clientId}`;
50
+
51
+ try {
52
+ const client = await newSdkClientEntity(options);
53
+ clients[clientId] = client;
54
+
55
+ res.status(201);
56
+ res.set('Location', resourceUrl);
57
+ } catch (e) {
58
+ res.status(500);
59
+ const message = e.message || JSON.stringify(e);
60
+ mainLog.error('Error creating client: ' + message);
61
+ res.write(message);
62
+ }
63
+ res.send();
64
+ });
65
+
66
+ app.post('/clients/:id', async (req, res) => {
67
+ const client = clients[req.params.id];
68
+ if (!client) {
69
+ res.status(404);
70
+ } else {
71
+ try {
72
+ const respValue = await client.doCommand(req.body);
73
+ if (respValue) {
74
+ res.status(200);
75
+ res.write(JSON.stringify(respValue));
76
+ } else {
77
+ res.status(204);
78
+ }
79
+ } catch (e) {
80
+ const isBadRequest = e === badCommandError;
81
+ res.status(isBadRequest ? 400 : 500);
82
+ res.write(e.message || JSON.stringify(e));
83
+ if (!isBadRequest && e.stack) {
84
+ console.log(e.stack);
85
+ }
86
+ }
87
+ }
88
+ res.send();
89
+ });
90
+
91
+ app.delete('/clients/:id', async (req, res) => {
92
+ const client = clients[req.params.id];
93
+ if (!client) {
94
+ res.status(404);
95
+ res.send();
96
+ } else {
97
+ client.close();
98
+ delete clients[req.params.id];
99
+ res.status(204);
100
+ res.send();
101
+ }
102
+ });
103
+
104
+ server = app.listen(port, () => {
105
+ console.log('Listening on port %d', port);
106
+ });
@@ -0,0 +1,23 @@
1
+ const ld = require('launchdarkly-node-client-sdk');
2
+
3
+ function Log(tag) {
4
+ function doLog(level, message) {
5
+ console.log(new Date().toISOString() + ` [${tag}] ${level}: ${message}`);
6
+ }
7
+ return {
8
+ info: message => doLog('info', message),
9
+ error: message => doLog('error', message),
10
+ };
11
+ }
12
+
13
+ function sdkLogger(tag) {
14
+ return ld.basicLogger({
15
+ level: 'debug',
16
+ destination: line => {
17
+ console.log(new Date().toISOString() + ` [${tag}.sdk] ${line}`);
18
+ },
19
+ });
20
+ }
21
+
22
+ module.exports.Log = Log;
23
+ module.exports.sdkLogger = sdkLogger;
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "node-client-sdk-contract-tests",
3
+ "version": "0.0.0",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "start": "node index.js"
7
+ },
8
+ "author": "",
9
+ "license": "Apache-2.0",
10
+ "dependencies": {
11
+ "body-parser": "^1.19.0",
12
+ "express": "^4.17.1",
13
+ "launchdarkly-node-client-sdk": "file:.."
14
+ }
15
+ }
@@ -0,0 +1,145 @@
1
+ const ld = require('launchdarkly-node-client-sdk');
2
+
3
+ const { Log, sdkLogger } = require('./log');
4
+
5
+ const badCommandError = new Error('unsupported command');
6
+
7
+ function makeSdkConfig(options, tag) {
8
+ if (!options.clientSide) {
9
+ throw new Error('configuration did not include clientSide options');
10
+ }
11
+
12
+ const isSet = x => x !== null && x !== undefined;
13
+ const maybeTime = seconds => isSet(seconds) ? seconds / 1000 : undefined;
14
+
15
+ const cf = {
16
+ autoAliasingOptOut: options.clientSide.autoAliasingOptOut,
17
+ evaluationReasons: options.clientSide.evaluationReasons,
18
+ logger: sdkLogger(tag),
19
+ useReport: options.clientSide.useReport
20
+ };
21
+
22
+ if (options.serviceEndpoints) {
23
+ cf.streamUrl = options.serviceEndpoints.streaming;
24
+ cf.baseUrl = options.serviceEndpoints.polling;
25
+ cf.eventsUrl = options.serviceEndpoints.events;
26
+ }
27
+
28
+ if (options.streaming) {
29
+ if (options.streaming.baseUri) {
30
+ cf.streamUrl = options.streaming.baseUri;
31
+ }
32
+ cf.streaming = true;
33
+ cf.streamReconnectDelay = maybeTime(options.streaming.initialRetryDelayMs);
34
+ }
35
+
36
+ if (options.polling) {
37
+ if (options.polling.baseUri) {
38
+ cf.baseUrl = options.polling.baseUri;
39
+ }
40
+ }
41
+
42
+ if (options.events) {
43
+ if (options.events.baseUri) {
44
+ cf.eventsUrl = options.events.baseUri;
45
+ }
46
+ cf.allAttributesPrivate = options.events.allAttributesPrivate;
47
+ cf.eventCapacity = options.events.capacity;
48
+ cf.diagnosticOptOut = !options.events.enableDiagnostics;
49
+ cf.flushInterval = maybeTime(options.events.flushIntervalMs);
50
+ cf.inlineUsersInEvents = options.events.inlineUsers;
51
+ cf.privateAttributeNames = options.events.globalPrivateAttributes;
52
+ } else {
53
+ cf.sendEvents = false;
54
+ }
55
+
56
+ if (options.tags) {
57
+ cf.application = {
58
+ id: options.tags.applicationId,
59
+ version: options.tags.applicationVersion
60
+ };
61
+ }
62
+
63
+ return cf;
64
+ }
65
+
66
+ function makeDefaultInitialUser() {
67
+ return {key: 'key-not-specified'};
68
+ }
69
+
70
+ async function newSdkClientEntity(options) {
71
+ const c = {};
72
+ const log = Log(options.tag);
73
+
74
+ log.info('Creating client with configuration: ' + JSON.stringify(options.configuration));
75
+
76
+ const timeout =
77
+ options.configuration.startWaitTimeMs !== null && options.configuration.startWaitTimeMs !== undefined
78
+ ? options.configuration.startWaitTimeMs
79
+ : 5000;
80
+ const client = ld.initialize(
81
+ options.configuration.credential || 'unknown-env-id',
82
+ (options.clientSide && options.clientSide.initialUser) || makeDefaultInitialUser(),
83
+ makeSdkConfig(options.configuration, options.tag)
84
+ );
85
+ let failed = false;
86
+ try {
87
+ await Promise.race([client.waitForInitialization(), new Promise((resolve, reject) => setTimeout(reject, timeout))]);
88
+ } catch (_) {
89
+ // we get here if waitForInitialization() rejects or if we timed out
90
+ failed = true
91
+ }
92
+ if (failed && !options.configuration.initCanFail) {
93
+ client.close();
94
+ throw new Error('client initialization failed');
95
+ }
96
+
97
+ c.close = () => {
98
+ client.close();
99
+ log.info('Test ended');
100
+ };
101
+
102
+ c.doCommand = async params => {
103
+ log.info('Received command: ' + params.command);
104
+ switch (params.command) {
105
+ case 'evaluate': {
106
+ const pe = params.evaluate;
107
+ if (pe.detail) {
108
+ return client.variationDetail(pe.flagKey, pe.defaultValue);
109
+ } else {
110
+ const value = client.variation(pe.flagKey, pe.defaultValue);
111
+ return { value };
112
+ }
113
+ }
114
+
115
+ case 'evaluateAll':
116
+ return { state: client.allFlags() };
117
+
118
+ case 'identifyEvent':
119
+ await client.identify(params.identifyEvent.user);
120
+ return undefined;
121
+
122
+ case 'customEvent': {
123
+ const pce = params.customEvent;
124
+ client.track(pce.eventKey, pce.data, pce.metricValue);
125
+ return undefined;
126
+ }
127
+
128
+ case 'aliasEvent':
129
+ client.alias(params.aliasEvent.user, params.aliasEvent.previousUser);
130
+ return undefined;
131
+
132
+ case 'flushEvents':
133
+ client.flush();
134
+ return undefined;
135
+
136
+ default:
137
+ throw badCommandError;
138
+ }
139
+ };
140
+
141
+ return c;
142
+ }
143
+
144
+ module.exports.newSdkClientEntity = newSdkClientEntity;
145
+ module.exports.badCommandError = badCommandError;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "launchdarkly-node-client-sdk",
3
- "version": "2.0.1",
3
+ "version": "2.0.4",
4
4
  "description": "LaunchDarkly Client-Side SDK for Node.js",
5
5
  "author": "LaunchDarkly <team@launchdarkly.com>",
6
6
  "license": "Apache-2.0",
@@ -14,9 +14,11 @@
14
14
  "scripts": {
15
15
  "lint": "eslint --format 'node_modules/eslint-formatter-pretty' --ignore-path .eslintignore",
16
16
  "lint:all": "eslint --format 'node_modules/eslint-formatter-pretty' --ignore-path .eslintignore src",
17
- "test": "jest",
18
- "test:junit": "jest --testResultsProcessor jest-junit",
19
- "check-typescript": "node_modules/typescript/bin/tsc"
17
+ "test": "jest --ci",
18
+ "check-typescript": "node_modules/typescript/bin/tsc",
19
+ "contract-test-service": "npm --prefix contract-tests install && npm --prefix contract-tests start",
20
+ "contract-test-harness": "curl -s https://raw.githubusercontent.com/launchdarkly/sdk-test-harness/main/downloader/run.sh \\ | VERSION=v1 PARAMS=\"-url http://localhost:8000 -debug -stop-service-at-end $TEST_HARNESS_PARAMS\" sh",
21
+ "contract-tests": "npm run contract-test-service & npm run contract-test-harness"
20
22
  },
21
23
  "engines": {
22
24
  "node": ">= 12.0.0"
@@ -44,11 +46,22 @@
44
46
  "semver-compare": "^1.0.0",
45
47
  "typescript": "^3.8.3"
46
48
  },
49
+ "jest": {
50
+ "rootDir": "src",
51
+ "testEnvironment": "node",
52
+ "testMatch": [
53
+ "<rootDir>/**/*-test.js"
54
+ ],
55
+ "testResultsProcessor": "jest-junit"
56
+ },
57
+ "jest-junit": {
58
+ "outputDirectory": "./reports/junit",
59
+ "outputName": "junit.xml"
60
+ },
47
61
  "dependencies": {
48
- "launchdarkly-eventsource": "1.4.2",
49
- "launchdarkly-js-sdk-common": "4.0.3",
50
- "node-localstorage": "^1.3.1",
51
- "original": "^1.0.0"
62
+ "launchdarkly-eventsource": "1.4.3",
63
+ "launchdarkly-js-sdk-common": "4.1.0",
64
+ "node-localstorage": "^1.3.1"
52
65
  },
53
66
  "repository": {
54
67
  "type": "git",
package/jest.config.js DELETED
@@ -1,15 +0,0 @@
1
- const version = process.env.npm_package_version;
2
-
3
- module.exports = {
4
- automock: false,
5
- resetModules: true,
6
- rootDir: 'src',
7
- setupFiles: ['./jest.setup.js'],
8
- testMatch: ['**/__tests__/**/*-test.js'],
9
- transform: {
10
- '^.+\\.js$': 'babel-jest',
11
- },
12
- globals: {
13
- VERSION: version,
14
- },
15
- };
package/src/jest.setup.js DELETED
@@ -1 +0,0 @@
1
- // Test environment setup