diff --git a/.circleci/config.yml b/.circleci/config.yml index 172cc1c..7cc281e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -16,14 +16,17 @@ workflows: docker-image: cimg/node:current run-lint: true - build-test-linux: - name: Node 16.3 - docker-image: cimg/node:16.3 + name: Node 17.9 + docker-image: cimg/node:17.9 + - build-test-linux: + name: Node 16.14 + docker-image: cimg/node:16.14 - build-test-linux: name: Node 15.14 docker-image: cimg/node:15.14 - build-test-linux: - name: Node 14.17 - docker-image: cimg/node:14.17 + name: Node 14.19 + docker-image: cimg/node:14.19 - build-test-linux: name: Node 13.14 docker-image: cimg/node:13.14 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 295f510..af7f2f0 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -38,6 +38,12 @@ To verify that the TypeScript declarations compile correctly (this involves comp npm run check-typescript ``` +To run the SDK contract test suite (see [`contract-tests/README.md`](./contract-tests/README.md)): + +```bash +npm run contract-tests +``` + ### Auditing package dependencies The `npm audit` tool compares all dependencies and transitive dependencies to a database of package versions with known vulnerabilities. However, the output of this tool includes both runtime and development dependencies. diff --git a/configuration.js b/configuration.js index 8f0bb3c..cb846a5 100644 --- a/configuration.js +++ b/configuration.js @@ -63,6 +63,10 @@ module.exports = (function () { config.logger.warn(messages.invalidTagValue(name)); return undefined; } + if (tagValue.length > 64) { + config.logger.warn(messages.tagValueTooLong(name)); + return undefined; + } return tagValue; } diff --git a/messages.js b/messages.js index 0912a6c..eee8f8c 100644 --- a/messages.js +++ b/messages.js @@ -27,3 +27,5 @@ exports.wrongOptionTypeBoolean = (name, actualType) => `Config option "${name}" should be a boolean, got ${actualType}, converting to boolean`; exports.invalidTagValue = name => `Config option "${name}" must only contain letters, numbers, ., _ or -.`; + +exports.tagValueTooLong = name => `Value of "${name}" was longer than 64 characters and was discarded.`; diff --git a/package.json b/package.json index f2602fc..86a353c 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "eslint-plugin-prettier": "^3.4.0", "jest": "^27.0.4", "jest-junit": "^12.2.0", - "launchdarkly-js-test-helpers": "^1.2.1", + "launchdarkly-js-test-helpers": "^2.2.0", "prettier": "^2.3.1", "tmp": "^0.2.1", "typescript": "~4.4.4", diff --git a/test/LDClient-tls-test.js b/test/LDClient-tls-test.js index 9f0b612..4ede726 100644 --- a/test/LDClient-tls-test.js +++ b/test/LDClient-tls-test.js @@ -4,10 +4,10 @@ import { AsyncQueue, TestHttpHandlers, TestHttpServer, + failOnTimeout, withCloseable } from 'launchdarkly-js-test-helpers'; import * as stubs from './stubs'; -import { failIfTimeout } from './test_helpers'; describe('LDClient TLS configuration', () => { const sdkKey = 'secret'; @@ -46,9 +46,9 @@ describe('LDClient TLS configuration', () => { }; await withCloseable(LDClient.init(sdkKey, config), async client => { - const message1 = await failIfTimeout(logCapture.warn.take(), 1000); + const message1 = await failOnTimeout(logCapture.warn.take(), 1000, 'timed out waiting for log message'); expect(message1).toMatch(/only disable the streaming API/); // irrelevant message due to our use of polling mode - const message2 = await failIfTimeout(logCapture.warn.take(), 1000); + const message2 = await failOnTimeout(logCapture.warn.take(), 1000, 'timed out waiting for log message'); expect(message2).toMatch(/self.signed/); }); }); diff --git a/test/configuration-test.js b/test/configuration-test.js index bd96ce9..6334e5f 100644 --- a/test/configuration-test.js +++ b/test/configuration-test.js @@ -196,6 +196,13 @@ describe('configuration', function() { expect(configIn.logger.warn).toHaveBeenCalledTimes(1); }); + it('logs a warning when a tag value is too long', async () => { + const configIn = emptyConfigWithMockLogger(); + configIn.application = { id: 'a'.repeat(65), version: 'b'.repeat(64) }; + expect(configuration.validate(configIn).application.id).toBeUndefined(); + expect(configIn.logger.warn).toHaveBeenCalledTimes(1); + }); + it('handles a valid application version', () => { const configIn = emptyConfigWithMockLogger(); configIn.application = {version: 'test-version'}; diff --git a/test/event_processor-test.js b/test/event_processor-test.js index 612a09b..3092f45 100644 --- a/test/event_processor-test.js +++ b/test/event_processor-test.js @@ -1,7 +1,6 @@ const { DiagnosticsManager, DiagnosticId } = require('../diagnostic_events'); const EventProcessor = require('../event_processor'); -const { TestHttpHandlers, TestHttpServer, withCloseable } = require('launchdarkly-js-test-helpers'); -const { failIfTimeout } = require('./test_helpers'); +const { failOnTimeout, TestHttpHandlers, TestHttpServer, withCloseable } = require('launchdarkly-js-test-helpers'); describe('EventProcessor', () => { @@ -657,8 +656,8 @@ describe('EventProcessor', () => { ep.sendEvent({ kind: 'identify', creationDate: 1000, user: user }); // unfortunately we must wait for both the flush interval and the 1-second retry interval - await failIfTimeout(s.nextRequest(), 500); - await failIfTimeout(s.nextRequest(), 1500); + await failOnTimeout(s.nextRequest(), 500, 'timed out waiting for event payload'); + await failOnTimeout(s.nextRequest(), 1500, 'timed out waiting for event payload'); }); })); diff --git a/test/polling-test.js b/test/polling-test.js index ae4162a..171b7ae 100644 --- a/test/polling-test.js +++ b/test/polling-test.js @@ -1,9 +1,8 @@ const InMemoryFeatureStore = require('../feature_store'); const PollingProcessor = require('../polling'); const dataKind = require('../versioned_data_kind'); -const { AsyncQueue, promisify, promisifySingle } = require('launchdarkly-js-test-helpers'); +const { AsyncQueue, failOnResolve, failOnTimeout, promisify, promisifySingle } = require('launchdarkly-js-test-helpers'); const stubs = require('./stubs'); -const { failIfTimeout } = require('./test_helpers'); describe('PollingProcessor', () => { const longInterval = 100000; @@ -81,7 +80,7 @@ describe('PollingProcessor', () => { processor.start(() => {}); const startTime = new Date().getTime(); for (let i = 0; i < 4; i++) { - await failIfTimeout(calls.take(), 500); + await failOnTimeout(calls.take(), 500, 'timed out waiting for poll request #' + (i + 1)); } expect(new Date().getTime() - startTime).toBeLessThanOrEqual(500); }); @@ -107,9 +106,9 @@ describe('PollingProcessor', () => { let errReceived; processor.start(e => { errReceived = e; }); - await failIfTimeout(calls.take(), 500); - await failIfTimeout(calls.take(), 500); - await failIfTimeout(calls.take(), 500); + for (let i = 0; i < 3; i++) { + await failOnTimeout(calls.take(), 500, 'timed out waiting for poll request #' + (i + 1)); + } expect(config.logger.error).not.toHaveBeenCalled(); expect(errReceived).toBeUndefined(); @@ -143,10 +142,11 @@ describe('PollingProcessor', () => { const result = new AsyncQueue(); processor.start(e => result.add(e)); - const errReceived = await failIfTimeout(result.take(), 1000); + const errReceived = await failOnTimeout(result.take(), 1000, 'timed out waiting for initialization to complete'); expect(errReceived.message).toMatch(new RegExp('error ' + status + '.*giving up permanently')); - expect(calls.length()).toEqual(1); + await failOnTimeout(calls.take(), 10, 'expected initial poll request but did not see one'); + await failOnResolve(calls.take(), 100, 'received unexpected second poll request'); expect(config.logger.error).toHaveBeenCalledTimes(1); } diff --git a/test/streaming-test.js b/test/streaming-test.js index e2ac9c6..7d71700 100644 --- a/test/streaming-test.js +++ b/test/streaming-test.js @@ -2,10 +2,9 @@ const { DiagnosticId, DiagnosticsManager } = require('../diagnostic_events'); const InMemoryFeatureStore = require('../feature_store'); const StreamProcessor = require('../streaming'); import * as httpUtils from '../utils/httpUtils'; -import { failIfResolves, failIfTimeout } from './test_helpers'; const dataKind = require('../versioned_data_kind'); -const { promisifySingle } = require('launchdarkly-js-test-helpers'); +const { failOnResolve, failOnTimeout, promisifySingle } = require('launchdarkly-js-test-helpers'); const stubs = require('./stubs'); describe('StreamProcessor', () => { @@ -258,10 +257,10 @@ describe('StreamProcessor', () => { es.instance.simulateError(err); - await failIfTimeout(logCapture.warn.take(), 1000); + await failOnTimeout(logCapture.warn.take(), 1000, 'timed out waiting for log message'); - await failIfResolves(waitForStart, 50); - await failIfResolves(logCapture.error.take(), 50); + await failOnResolve(waitForStart, 50, 'initialization completed unexpectedly'); + await failOnResolve(logCapture.error.take(), 50, 'got unexpected log error'); expect(es.closed).not.toBeTruthy(); @@ -297,11 +296,11 @@ describe('StreamProcessor', () => { const waitForStart = promisifySingle(sp.start)(); es.instance.simulateError(err); - const errReceived = await failIfTimeout(waitForStart, 1000); + const errReceived = await failOnTimeout(waitForStart, 1000, 'timed out waiting for error result'); expect(errReceived).toEqual(err); - await failIfTimeout(logCapture.error.take(), 50); + await failOnTimeout(logCapture.error.take(), 50, 'timed out waiting for log error'); expect(es.closed).toBe(true); diff --git a/test/test_helpers.js b/test/test_helpers.js deleted file mode 100644 index cf25225..0000000 --- a/test/test_helpers.js +++ /dev/null @@ -1,38 +0,0 @@ - -function failIfResolves(promise, timeout, waitingForWhat) { - let timer; - return Promise.race([ - new Promise( - (resolve, reject) => { - promise.then(() => { - timer && clearTimeout(timer); - reject('received unexpected ' + (waitingForWhat || 'value')); - }) - } - ), - new Promise( - (resolve) => { - timer = setTimeout(resolve, timeout); - } - ), - ]); -} - -function failIfTimeout(promise, timeout, waitingForWhat) { - let timer; - return Promise.race([ - promise.finally(timer && clearTimeout(timer)), - new Promise( - (resolve, reject) => { - timer = setTimeout(() => { - reject(waitingForWhat ? 'timed out waiting for ' + waitingForWhat : 'timed out'); - }, timeout); - } - ), - ]); -} - -module.exports = { - failIfResolves, - failIfTimeout, -};