diff --git a/README.md b/README.md index eca82bc..9845fa4 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,29 @@ A wrapper around @aws-sdk/client-appconfigdata to provide background polling and caching. +## Usage + +Initialize: + +```typescript +const poller = new Poller({ + dataClient: dataClient, + sessionConfig: { + ApplicationIdentifier: 'MyApp', + EnvironmentIdentifier: 'Test', + ConfigurationProfileIdentifier: 'Config1', + }, + logger: console.log, +}); + +await poller.start(); +``` + +Fetch: + +```typescript +const value = poller.getConfigurationString().latestValue; +``` ## License diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts deleted file mode 100644 index 9aaca6e..0000000 --- a/__tests__/main.test.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { Delays, greeter } from '../src/main.js'; - -describe('greeter function', () => { - const name = 'John'; - let hello: string; - - let timeoutSpy: jest.SpyInstance; - - // Act before assertions - beforeAll(async () => { - // Read more about fake timers - // http://facebook.github.io/jest/docs/en/timer-mocks.html#content - // Jest 27 now uses "modern" implementation of fake timers - // https://jestjs.io/blog/2021/05/25/jest-27#flipping-defaults - // https://github.com/facebook/jest/pull/5171 - jest.useFakeTimers(); - timeoutSpy = jest.spyOn(global, 'setTimeout'); - - const p: Promise = greeter(name); - jest.runOnlyPendingTimers(); - hello = await p; - }); - - // Teardown (cleanup) after assertions - afterAll(() => { - timeoutSpy.mockRestore(); - }); - - // Assert if setTimeout was called properly - it('delays the greeting by 2 seconds', () => { - expect(setTimeout).toHaveBeenCalledTimes(1); - expect(setTimeout).toHaveBeenLastCalledWith( - expect.any(Function), - Delays.Long, - ); - }); - - // Assert greeter result - it('greets a user with `Hello, {name}` message', () => { - expect(hello).toBe(`Hello, ${name}`); - }); -}); diff --git a/__tests__/poller.test.ts b/__tests__/poller.test.ts new file mode 100644 index 0000000..50d98e6 --- /dev/null +++ b/__tests__/poller.test.ts @@ -0,0 +1,49 @@ +import { Poller } from '../src/poller.js'; +import { + AppConfigDataClient, + GetLatestConfigurationCommand, + StartConfigurationSessionCommand, +} from '@aws-sdk/client-appconfigdata'; +import { mockClient } from 'aws-sdk-client-mock'; +import { Uint8ArrayBlobAdapter } from '@smithy/util-stream'; + +describe('Poller', () => { + const appConfigClientMock = mockClient(AppConfigDataClient); + + const configValue = 'Some Configuration'; + + appConfigClientMock.on(StartConfigurationSessionCommand).resolves({ + InitialConfigurationToken: 'initialToken', + }); + + appConfigClientMock.on(GetLatestConfigurationCommand).resolves({ + Configuration: Uint8ArrayBlobAdapter.fromString(configValue), + }); + + const dataClient = new AppConfigDataClient(); + + let poller: Poller | undefined; + + afterAll(() => { + console.log('Stopping poller'); + poller.stop(); + }); + + it('Polls', async () => { + poller = new Poller({ + dataClient: dataClient, + sessionConfig: { + ApplicationIdentifier: 'MyApp', + EnvironmentIdentifier: 'Test', + ConfigurationProfileIdentifier: 'Config1', + }, + logger: console.log, + }); + + await poller.start(); + const latest = poller.getConfigurationString(); + + expect(latest.latestValue).toEqual(configValue); + expect(latest.errorCausingStaleValue).toBeUndefined(); + }); +}); diff --git a/package-lock.json b/package-lock.json index 5a343a9..78e3729 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "@types/node": "~18", "@typescript-eslint/eslint-plugin": "~6.2", "@typescript-eslint/parser": "~6.2", + "aws-sdk-client-mock": "^3.0.0", "eslint": "~8.46", "eslint-config-prettier": "~9.0", "eslint-plugin-jest": "~27.2", @@ -1909,6 +1910,32 @@ "@sinonjs/commons": "^3.0.0" } }, + "node_modules/@sinonjs/samsam": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/@sinonjs/samsam/-/samsam-7.0.1.tgz", + "integrity": "sha512-zsAk2Jkiq89mhZovB2LLOdTCxJF4hqqTToGP0ASWlhp4I1hqOjcfmZGafXntCN7MDC6yySH0mFHrYtHceOeLmw==", + "dev": true, + "dependencies": { + "@sinonjs/commons": "^2.0.0", + "lodash.get": "^4.4.2", + "type-detect": "^4.0.8" + } + }, + "node_modules/@sinonjs/samsam/node_modules/@sinonjs/commons": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-2.0.0.tgz", + "integrity": "sha512-uLa0j859mMrg2slwQYdO/AkrOfmH+X6LTVmNTS9CqexuE2IvVORIkSpJLqePAbEnKJ77aMmCwr1NUZ57120Xcg==", + "dev": true, + "dependencies": { + "type-detect": "4.0.8" + } + }, + "node_modules/@sinonjs/text-encoding": { + "version": "0.7.2", + "resolved": "https://registry.npmjs.org/@sinonjs/text-encoding/-/text-encoding-0.7.2.tgz", + "integrity": "sha512-sXXKG+uL9IrKqViTtao2Ws6dy0znu9sOaP1di/jKGW1M6VssO8vlpXCQcpZ+jisQ1tTFAC5Jo/EOzFbggBagFQ==", + "dev": true + }, "node_modules/@smithy/abort-controller": { "version": "2.0.15", "resolved": "https://registry.npmjs.org/@smithy/abort-controller/-/abort-controller-2.0.15.tgz", @@ -2513,6 +2540,21 @@ "integrity": "sha512-G8hZ6XJiHnuhQKR7ZmysCeJWE08o8T0AXtk5darsCaTVsYZhhgUrq53jizaR2FvsoeCwJhlmwTjkXBY5Pn/ZHw==", "dev": true }, + "node_modules/@types/sinon": { + "version": "10.0.20", + "resolved": "https://registry.npmjs.org/@types/sinon/-/sinon-10.0.20.tgz", + "integrity": "sha512-2APKKruFNCAZgx3daAyACGzWuJ028VVCUDk6o2rw/Z4PXT0ogwdV4KUegW0MwVs0Zu59auPXbbuBJHF12Sx1Eg==", + "dev": true, + "dependencies": { + "@types/sinonjs__fake-timers": "*" + } + }, + "node_modules/@types/sinonjs__fake-timers": { + "version": "8.1.5", + "resolved": "https://registry.npmjs.org/@types/sinonjs__fake-timers/-/sinonjs__fake-timers-8.1.5.tgz", + "integrity": "sha512-mQkU2jY8jJEF7YHjHvsQO8+3ughTL1mcnn96igfhONmR+fUPSKIkefQYpSe8bsly2Ep7oQbn/6VG5/9/0qcArQ==", + "dev": true + }, "node_modules/@types/stack-utils": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-2.0.1.tgz", @@ -3113,6 +3155,17 @@ "node": ">=8" } }, + "node_modules/aws-sdk-client-mock": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/aws-sdk-client-mock/-/aws-sdk-client-mock-3.0.0.tgz", + "integrity": "sha512-4mBiWhuLYLZe1+K/iB8eYy5SAZyW2se+Keyh5u9QouMt6/qJ5SRZhss68xvUX5g3ApzROJ06QPRziYHP6buuvQ==", + "dev": true, + "dependencies": { + "@types/sinon": "^10.0.10", + "sinon": "^14.0.2", + "tslib": "^2.1.0" + } + }, "node_modules/babel-jest": { "version": "29.6.2", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-29.6.2.tgz", @@ -3509,6 +3562,15 @@ "node": ">=8" } }, + "node_modules/diff": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-5.1.0.tgz", + "integrity": "sha512-D+mk+qE8VC/PAUrlAU34N+VfXev0ghe5ywmpqrawphmVZc1bEfn56uo9qpyGp1p4xpzOHkSW4ztBd6L7Xx4ACw==", + "dev": true, + "engines": { + "node": ">=0.3.1" + } + }, "node_modules/diff-sequences": { "version": "29.4.3", "resolved": "https://registry.npmjs.org/diff-sequences/-/diff-sequences-29.4.3.tgz", @@ -4405,6 +4467,12 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha512-D2S+3GLxWH+uhrNEcoh/fnmYeP8E8/zHl644d/jdA0g2uyXvy3sb0qxotE+ne0LtccHknQzWwZEzhak7oJ0COQ==", + "dev": true + }, "node_modules/isexe": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", @@ -5119,6 +5187,12 @@ "node": ">=6" } }, + "node_modules/just-extend": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/just-extend/-/just-extend-4.2.1.tgz", + "integrity": "sha512-g3UB796vUFIY90VIv/WX3L2c8CS2MdWUww3CNrYmqza1Fg0DURc2K/O4YrnklBdQarSJ/y8JnJYDGc+1iumQjg==", + "dev": true + }, "node_modules/kleur": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/kleur/-/kleur-3.0.3.tgz", @@ -5171,6 +5245,12 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/lodash.get": { + "version": "4.4.2", + "resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz", + "integrity": "sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ==", + "dev": true + }, "node_modules/lodash.memoize": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz", @@ -5298,6 +5378,28 @@ "integrity": "sha512-Tj+HTDSJJKaZnfiuw+iaF9skdPpTo2GtEly5JHnWV/hfv2Qj/9RKsGISQtLh2ox3l5EAGw487hnBee0sIJ6v2g==", "dev": true }, + "node_modules/nise": { + "version": "5.1.5", + "resolved": "https://registry.npmjs.org/nise/-/nise-5.1.5.tgz", + "integrity": "sha512-VJuPIfUFaXNRzETTQEEItTOP8Y171ijr+JLq42wHes3DiryR8vT+1TXQW/Rx8JNUhyYYWyIvjXTU6dOhJcs9Nw==", + "dev": true, + "dependencies": { + "@sinonjs/commons": "^2.0.0", + "@sinonjs/fake-timers": "^10.0.2", + "@sinonjs/text-encoding": "^0.7.1", + "just-extend": "^4.0.2", + "path-to-regexp": "^1.7.0" + } + }, + "node_modules/nise/node_modules/@sinonjs/commons": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-2.0.0.tgz", + "integrity": "sha512-uLa0j859mMrg2slwQYdO/AkrOfmH+X6LTVmNTS9CqexuE2IvVORIkSpJLqePAbEnKJ77aMmCwr1NUZ57120Xcg==", + "dev": true, + "dependencies": { + "type-detect": "4.0.8" + } + }, "node_modules/node-int64": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz", @@ -5499,6 +5601,15 @@ "node": "14 || >=16.14" } }, + "node_modules/path-to-regexp": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-1.8.0.tgz", + "integrity": "sha512-n43JRhlUKUAlibEJhPeir1ncUID16QnEjNpwzNdO3Lm4ywrBpBZ5oLD0I6br9evr1Y9JTqwRtAh7JLoOzAQdVA==", + "dev": true, + "dependencies": { + "isarray": "0.0.1" + } + }, "node_modules/path-type": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/path-type/-/path-type-4.0.0.tgz", @@ -5935,6 +6046,52 @@ "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==", "dev": true }, + "node_modules/sinon": { + "version": "14.0.2", + "resolved": "https://registry.npmjs.org/sinon/-/sinon-14.0.2.tgz", + "integrity": "sha512-PDpV0ZI3ZCS3pEqx0vpNp6kzPhHrLx72wA0G+ZLaaJjLIYeE0n8INlgaohKuGy7hP0as5tbUd23QWu5U233t+w==", + "deprecated": "16.1.1", + "dev": true, + "dependencies": { + "@sinonjs/commons": "^2.0.0", + "@sinonjs/fake-timers": "^9.1.2", + "@sinonjs/samsam": "^7.0.1", + "diff": "^5.0.0", + "nise": "^5.1.2", + "supports-color": "^7.2.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/sinon" + } + }, + "node_modules/sinon/node_modules/@sinonjs/commons": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-2.0.0.tgz", + "integrity": "sha512-uLa0j859mMrg2slwQYdO/AkrOfmH+X6LTVmNTS9CqexuE2IvVORIkSpJLqePAbEnKJ77aMmCwr1NUZ57120Xcg==", + "dev": true, + "dependencies": { + "type-detect": "4.0.8" + } + }, + "node_modules/sinon/node_modules/@sinonjs/fake-timers": { + "version": "9.1.2", + "resolved": "https://registry.npmjs.org/@sinonjs/fake-timers/-/fake-timers-9.1.2.tgz", + "integrity": "sha512-BPS4ynJW/o92PUR4wgriz2Ud5gpST5vz6GQfMixEDK0Z8ZCUv2M7SkBLykH56T++Xs+8ln9zTGbOvNGIe02/jw==", + "dev": true, + "dependencies": { + "@sinonjs/commons": "^1.7.0" + } + }, + "node_modules/sinon/node_modules/@sinonjs/fake-timers/node_modules/@sinonjs/commons": { + "version": "1.8.6", + "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-1.8.6.tgz", + "integrity": "sha512-Ky+XkAkqPZSm3NLBeUng77EBQl3cmeJhITaGHdYH8kjVB+aun3S4XBRti2zt17mtt0mIUDiNxYeoJm6drVvBJQ==", + "dev": true, + "dependencies": { + "type-detect": "4.0.8" + } + }, "node_modules/sisteransi": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/sisteransi/-/sisteransi-1.0.5.tgz", @@ -8000,6 +8157,34 @@ "@sinonjs/commons": "^3.0.0" } }, + "@sinonjs/samsam": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/@sinonjs/samsam/-/samsam-7.0.1.tgz", + "integrity": "sha512-zsAk2Jkiq89mhZovB2LLOdTCxJF4hqqTToGP0ASWlhp4I1hqOjcfmZGafXntCN7MDC6yySH0mFHrYtHceOeLmw==", + "dev": true, + "requires": { + "@sinonjs/commons": "^2.0.0", + "lodash.get": "^4.4.2", + "type-detect": "^4.0.8" + }, + "dependencies": { + "@sinonjs/commons": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-2.0.0.tgz", + "integrity": "sha512-uLa0j859mMrg2slwQYdO/AkrOfmH+X6LTVmNTS9CqexuE2IvVORIkSpJLqePAbEnKJ77aMmCwr1NUZ57120Xcg==", + "dev": true, + "requires": { + "type-detect": "4.0.8" + } + } + } + }, + "@sinonjs/text-encoding": { + "version": "0.7.2", + "resolved": "https://registry.npmjs.org/@sinonjs/text-encoding/-/text-encoding-0.7.2.tgz", + "integrity": "sha512-sXXKG+uL9IrKqViTtao2Ws6dy0znu9sOaP1di/jKGW1M6VssO8vlpXCQcpZ+jisQ1tTFAC5Jo/EOzFbggBagFQ==", + "dev": true + }, "@smithy/abort-controller": { "version": "2.0.15", "resolved": "https://registry.npmjs.org/@smithy/abort-controller/-/abort-controller-2.0.15.tgz", @@ -8502,6 +8687,21 @@ "integrity": "sha512-G8hZ6XJiHnuhQKR7ZmysCeJWE08o8T0AXtk5darsCaTVsYZhhgUrq53jizaR2FvsoeCwJhlmwTjkXBY5Pn/ZHw==", "dev": true }, + "@types/sinon": { + "version": "10.0.20", + "resolved": "https://registry.npmjs.org/@types/sinon/-/sinon-10.0.20.tgz", + "integrity": "sha512-2APKKruFNCAZgx3daAyACGzWuJ028VVCUDk6o2rw/Z4PXT0ogwdV4KUegW0MwVs0Zu59auPXbbuBJHF12Sx1Eg==", + "dev": true, + "requires": { + "@types/sinonjs__fake-timers": "*" + } + }, + "@types/sinonjs__fake-timers": { + "version": "8.1.5", + "resolved": "https://registry.npmjs.org/@types/sinonjs__fake-timers/-/sinonjs__fake-timers-8.1.5.tgz", + "integrity": "sha512-mQkU2jY8jJEF7YHjHvsQO8+3ughTL1mcnn96igfhONmR+fUPSKIkefQYpSe8bsly2Ep7oQbn/6VG5/9/0qcArQ==", + "dev": true + }, "@types/stack-utils": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-2.0.1.tgz", @@ -8863,6 +9063,17 @@ "integrity": "sha512-HGyxoOTYUyCM6stUe6EJgnd4EoewAI7zMdfqO+kGjnlZmBDz/cR5pf8r/cR4Wq60sL/p0IkcjUEEPwS3GFrIyw==", "dev": true }, + "aws-sdk-client-mock": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/aws-sdk-client-mock/-/aws-sdk-client-mock-3.0.0.tgz", + "integrity": "sha512-4mBiWhuLYLZe1+K/iB8eYy5SAZyW2se+Keyh5u9QouMt6/qJ5SRZhss68xvUX5g3ApzROJ06QPRziYHP6buuvQ==", + "dev": true, + "requires": { + "@types/sinon": "^10.0.10", + "sinon": "^14.0.2", + "tslib": "^2.1.0" + } + }, "babel-jest": { "version": "29.6.2", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-29.6.2.tgz", @@ -9140,6 +9351,12 @@ "integrity": "sha512-TLz+x/vEXm/Y7P7wn1EJFNLxYpUD4TgMosxY6fAVJUnJMbupHBOncxyWUG9OpTaH9EBD7uFI5LfEgmMOc54DsA==", "dev": true }, + "diff": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-5.1.0.tgz", + "integrity": "sha512-D+mk+qE8VC/PAUrlAU34N+VfXev0ghe5ywmpqrawphmVZc1bEfn56uo9qpyGp1p4xpzOHkSW4ztBd6L7Xx4ACw==", + "dev": true + }, "diff-sequences": { "version": "29.4.3", "resolved": "https://registry.npmjs.org/diff-sequences/-/diff-sequences-29.4.3.tgz", @@ -9781,6 +9998,12 @@ "integrity": "sha512-hFoiJiTl63nn+kstHGBtewWSKnQLpyb155KHheA1l39uvtO9nWIop1p3udqPcUd/xbF1VLMO4n7OI6p7RbngDg==", "dev": true }, + "isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha512-D2S+3GLxWH+uhrNEcoh/fnmYeP8E8/zHl644d/jdA0g2uyXvy3sb0qxotE+ne0LtccHknQzWwZEzhak7oJ0COQ==", + "dev": true + }, "isexe": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", @@ -10327,6 +10550,12 @@ "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", "dev": true }, + "just-extend": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/just-extend/-/just-extend-4.2.1.tgz", + "integrity": "sha512-g3UB796vUFIY90VIv/WX3L2c8CS2MdWUww3CNrYmqza1Fg0DURc2K/O4YrnklBdQarSJ/y8JnJYDGc+1iumQjg==", + "dev": true + }, "kleur": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/kleur/-/kleur-3.0.3.tgz", @@ -10364,6 +10593,12 @@ "p-locate": "^5.0.0" } }, + "lodash.get": { + "version": "4.4.2", + "resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz", + "integrity": "sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ==", + "dev": true + }, "lodash.memoize": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz", @@ -10470,6 +10705,30 @@ "integrity": "sha512-Tj+HTDSJJKaZnfiuw+iaF9skdPpTo2GtEly5JHnWV/hfv2Qj/9RKsGISQtLh2ox3l5EAGw487hnBee0sIJ6v2g==", "dev": true }, + "nise": { + "version": "5.1.5", + "resolved": "https://registry.npmjs.org/nise/-/nise-5.1.5.tgz", + "integrity": "sha512-VJuPIfUFaXNRzETTQEEItTOP8Y171ijr+JLq42wHes3DiryR8vT+1TXQW/Rx8JNUhyYYWyIvjXTU6dOhJcs9Nw==", + "dev": true, + "requires": { + "@sinonjs/commons": "^2.0.0", + "@sinonjs/fake-timers": "^10.0.2", + "@sinonjs/text-encoding": "^0.7.1", + "just-extend": "^4.0.2", + "path-to-regexp": "^1.7.0" + }, + "dependencies": { + "@sinonjs/commons": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-2.0.0.tgz", + "integrity": "sha512-uLa0j859mMrg2slwQYdO/AkrOfmH+X6LTVmNTS9CqexuE2IvVORIkSpJLqePAbEnKJ77aMmCwr1NUZ57120Xcg==", + "dev": true, + "requires": { + "type-detect": "4.0.8" + } + } + } + }, "node-int64": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz", @@ -10616,6 +10875,15 @@ } } }, + "path-to-regexp": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-1.8.0.tgz", + "integrity": "sha512-n43JRhlUKUAlibEJhPeir1ncUID16QnEjNpwzNdO3Lm4ywrBpBZ5oLD0I6br9evr1Y9JTqwRtAh7JLoOzAQdVA==", + "dev": true, + "requires": { + "isarray": "0.0.1" + } + }, "path-type": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/path-type/-/path-type-4.0.0.tgz", @@ -10903,6 +11171,51 @@ "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==", "dev": true }, + "sinon": { + "version": "14.0.2", + "resolved": "https://registry.npmjs.org/sinon/-/sinon-14.0.2.tgz", + "integrity": "sha512-PDpV0ZI3ZCS3pEqx0vpNp6kzPhHrLx72wA0G+ZLaaJjLIYeE0n8INlgaohKuGy7hP0as5tbUd23QWu5U233t+w==", + "dev": true, + "requires": { + "@sinonjs/commons": "^2.0.0", + "@sinonjs/fake-timers": "^9.1.2", + "@sinonjs/samsam": "^7.0.1", + "diff": "^5.0.0", + "nise": "^5.1.2", + "supports-color": "^7.2.0" + }, + "dependencies": { + "@sinonjs/commons": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-2.0.0.tgz", + "integrity": "sha512-uLa0j859mMrg2slwQYdO/AkrOfmH+X6LTVmNTS9CqexuE2IvVORIkSpJLqePAbEnKJ77aMmCwr1NUZ57120Xcg==", + "dev": true, + "requires": { + "type-detect": "4.0.8" + } + }, + "@sinonjs/fake-timers": { + "version": "9.1.2", + "resolved": "https://registry.npmjs.org/@sinonjs/fake-timers/-/fake-timers-9.1.2.tgz", + "integrity": "sha512-BPS4ynJW/o92PUR4wgriz2Ud5gpST5vz6GQfMixEDK0Z8ZCUv2M7SkBLykH56T++Xs+8ln9zTGbOvNGIe02/jw==", + "dev": true, + "requires": { + "@sinonjs/commons": "^1.7.0" + }, + "dependencies": { + "@sinonjs/commons": { + "version": "1.8.6", + "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-1.8.6.tgz", + "integrity": "sha512-Ky+XkAkqPZSm3NLBeUng77EBQl3cmeJhITaGHdYH8kjVB+aun3S4XBRti2zt17mtt0mIUDiNxYeoJm6drVvBJQ==", + "dev": true, + "requires": { + "type-detect": "4.0.8" + } + } + } + } + } + }, "sisteransi": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/sisteransi/-/sisteransi-1.0.5.tgz", diff --git a/package.json b/package.json index da50948..3a9e706 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "@types/node": "~18", "@typescript-eslint/eslint-plugin": "~6.2", "@typescript-eslint/parser": "~6.2", + "aws-sdk-client-mock": "^3.0.0", "eslint": "~8.46", "eslint-config-prettier": "~9.0", "eslint-plugin-jest": "~27.2", diff --git a/src/main.ts b/src/main.ts deleted file mode 100644 index 5938134..0000000 --- a/src/main.ts +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Some predefined delay values (in milliseconds). - */ -export enum Delays { - Short = 500, - Medium = 2000, - Long = 5000, -} - -/** - * Returns a Promise that resolves after a given time. - * - * @param {string} name - A name. - * @param {number=} [delay=Delays.Medium] - A number of milliseconds to delay resolution of the Promise. - * @returns {Promise} - */ -function delayedHello( - name: string, - delay: number = Delays.Medium, -): Promise { - return new Promise((resolve: (value?: string) => void) => - setTimeout(() => resolve(`Hello, ${name}`), delay), - ); -} - -// Please see the comment in the .eslintrc.json file about the suppressed rule! -// Below is an example of how to use ESLint errors suppression. You can read more -// at https://eslint.org/docs/latest/user-guide/configuring/rules#disabling-rules - -// eslint-disable-next-line @typescript-eslint/explicit-function-return-type -export async function greeter(name: any) { // eslint-disable-line @typescript-eslint/no-explicit-any - // The name parameter should be of type string. Any is used only to trigger the rule. - return await delayedHello(name, Delays.Long); -} diff --git a/src/poller.ts b/src/poller.ts new file mode 100644 index 0000000..f09a854 --- /dev/null +++ b/src/poller.ts @@ -0,0 +1,193 @@ +import { + AppConfigDataClient, + GetLatestConfigurationCommand, + GetLatestConfigurationCommandOutput, + StartConfigurationSessionCommand, + StartConfigurationSessionCommandInput, +} from '@aws-sdk/client-appconfigdata'; + +interface PollerConfig { + dataClient: AppConfigDataClient; + sessionConfig: StartConfigurationSessionCommandInput; + pollIntervalSeconds?: number; + configTransformer?: (s: string) => T; + logger?: (s: string, obj?: unknown) => void; +} + +export interface ConfigStore { + latestValue?: T; + lastFreshTime?: Date; + errorCausingStaleValue?: Error; + versionLabel?: string; +} + +type PollingPhase = 'ready' | 'active' | 'stopped'; + +/** + * Starts polling immediately upon construction. + */ +export class Poller { + private readonly DEFAULT_POLL_INTERVAL_SECONDS = 30; + + private readonly config: PollerConfig; + + private pollingPhase: PollingPhase = 'ready'; + + private configurationToken?: string; + + private configStringStore: ConfigStore; + private configObjectStore: ConfigStore; + + private timeoutHandle?: NodeJS.Timeout; + + constructor(config: PollerConfig) { + this.config = config; + + this.configStringStore = {}; + this.configObjectStore = {}; + } + + public async start(): Promise { + if (this.pollingPhase != 'ready') { + throw new Error('Can only call start() once for an instance of Poller!'); + } + this.pollingPhase = 'active'; + await this.startPolling(); + } + + public stop(): void { + this.pollingPhase = 'stopped'; + if (this.timeoutHandle) { + clearTimeout(this.timeoutHandle); + } + } + + /** + * This will instantly return a cached value. Make sure you've awaited the + * completion of the start() call before calling this method. + * + * @returns The latest configuration in string format, along with some metadata. + */ + public getConfigurationString(): ConfigStore { + if (this.pollingPhase != 'active') { + throw new Error('Poller is not active!'); + } + return this.configStringStore; + } + + /** + * This returns a version of the config that's been parsed according to the + * configTransformer passed into this class. It's possible that this value is + * more stale than getConfigurationString, in the case where a new value + * retrieved from AppConfig is malformed and the transformer has been failing. + * + * This will instantly return a cached value. Make sure you've awaited the + * completion of the start() call before calling this method. + * + * @returns The latest configuration in object format, along with some metadata. + */ + public getConfigurationObject(): ConfigStore { + if (this.pollingPhase != 'active') { + throw new Error('Poller is not active!'); + } + return this.configObjectStore; + } + + private async startPolling(): Promise { + const { dataClient, sessionConfig } = this.config; + + const startCommand = new StartConfigurationSessionCommand(sessionConfig); + const result = await dataClient.send(startCommand); + + if (!result.InitialConfigurationToken) { + throw new Error( + 'Missing configuration token from AppConfig StartConfigurationSession response', + ); + } + + this.configurationToken = result.InitialConfigurationToken; + + await this.fetchLatestConfiguration(); + } + + private async fetchLatestConfiguration(): Promise { + const { dataClient, logger } = this.config; + + const getCommand = new GetLatestConfigurationCommand({ + ConfigurationToken: this.configurationToken, + }); + + let awsSuggestedIntervalSeconds: number | undefined; + + try { + const getResponse = await dataClient.send(getCommand); + awsSuggestedIntervalSeconds = getResponse.NextPollIntervalInSeconds; + + this.processGetResponse(getResponse); + } catch (e) { + // TODO: should we start a new configuration session in some cases? + this.configStringStore.errorCausingStaleValue = e; + this.configObjectStore.errorCausingStaleValue = e; + logger?.('Config string and object have gone stale', e); + } + + const nextIntervalInSeconds = this.getNextIntervalInSeconds( + awsSuggestedIntervalSeconds, + ); + + this.timeoutHandle = setTimeout(() => { + this.fetchLatestConfiguration(); + }, nextIntervalInSeconds * 1000); + } + + private processGetResponse( + getResponse: GetLatestConfigurationCommandOutput, + ): void { + const { logger, configTransformer } = this.config; + + if (getResponse.NextPollConfigurationToken) { + this.configurationToken = getResponse.NextPollConfigurationToken; + } + + if (getResponse.Configuration) { + try { + this.configStringStore.latestValue = + getResponse.Configuration.transformToString(); + this.configStringStore.lastFreshTime = new Date(); + this.configStringStore.versionLabel = getResponse.VersionLabel; + this.configStringStore.errorCausingStaleValue = undefined; + + if (configTransformer) { + try { + this.configObjectStore.latestValue = configTransformer( + this.configStringStore.latestValue, + ); + this.configObjectStore.lastFreshTime = new Date(); + this.configObjectStore.versionLabel = getResponse.VersionLabel; + this.configObjectStore.errorCausingStaleValue = undefined; + } catch (e) { + this.configObjectStore.errorCausingStaleValue = e; + logger?.('Config object has gone stale', e); + } + } + } catch (e) { + this.configStringStore.errorCausingStaleValue = e; + this.configObjectStore.errorCausingStaleValue = e; + logger?.('Config string and object have gone stale', e); + } + } else { + // When the configuration in the getResponse is not defined, that means the configuration is + // unchanged from the last time we polled. + this.configStringStore.lastFreshTime = new Date(); + this.configObjectStore.lastFreshTime = new Date(); + } + } + + private getNextIntervalInSeconds(awsSuggestedSeconds?: number): number { + return ( + this.config.pollIntervalSeconds || + awsSuggestedSeconds || + this.DEFAULT_POLL_INTERVAL_SECONDS + ); + } +}