Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: drop ts type inference for event source #706

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/workflows/build-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,18 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

test-as-dependency:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: 20.x
cache: yarn
- name: Yarn
run: yarn
- name: Test install in other package
run: ./scripts/test-package/run.sh
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ build/Release
# typescript lives in src, output in lib
lib/
.vscode

# test files
/test-package
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"@types/make-fetch-happen": "^10.0.4",
"@types/murmurhash3js": "^3.0.3",
"@types/nock": "^11.1.0",
"@types/node": "^20.2.5",
"@types/node": "^20.17.17",
"@types/proxy-from-env": "^1.0.4",
"@types/semver": "^7.5.0",
"@types/sinon": "^17.0.0",
Expand Down
25 changes: 25 additions & 0 deletions scripts/test-package/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash
set -e

echo -e "\nTesting that the package can be installed in another project and compiled with TypeScript without errors"

TEST_DIR="test-package"
mkdir "$TEST_DIR"

cp scripts/test-package/test-tsconfig.json "$TEST_DIR/tsconfig.json"
cp scripts/test-package/test-package.json "$TEST_DIR/package.json"
cd "$TEST_DIR"
npm install --install-links
mkdir src
echo -e "import { Unleash } from 'unleash-client';\nvoid Unleash;\nconsole.log('Hello world');" > src/index.ts
./node_modules/.bin/tsc -b tsconfig.json

if [ "$(node . 2>&1)" = "Hello world" ]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need those checks? Isn't checking exit code from tsc to be 0 enough?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way we can extend tests, and check if Unleash is reporting errors.

echo "Output is correct"
(cd .. && rm -rf "$TEST_DIR")
else
echo "Output is incorrect" >&2
echo $(node . 2>&1)
(cd .. && rm -rf "$TEST_DIR")
exit 1
fi
19 changes: 19 additions & 0 deletions scripts/test-package/test-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "test",
"version": "1.0.0",
"main": "lib/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"devDependencies": {
"typescript": "^5.7.3"
},
"dependencies": {
"unleash-client": "file:.."
}
}
19 changes: 19 additions & 0 deletions scripts/test-package/test-tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"declaration": true,
"inlineSourceMap": true,
"pretty": true,
"noImplicitAny": true,
"allowJs": false,
"resolveJsonModule": true,
"composite": true,
"outDir": "./lib",
"rootDir": "./src"
},
"include": ["src/**/*"]
}
3 changes: 3 additions & 0 deletions src/event-source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { EventSource as moduleToPatch } from 'launchdarkly-eventsource';

export const EventSource = moduleToPatch; // Re-export from .js file, because the original module doesn't have types
2 changes: 1 addition & 1 deletion src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default class Metrics extends EventEmitter {

private url: string;

private timer: NodeJS.Timer | undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how did you find that this needs to change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to update Node.js types to fix other error

../node_modules/@types/node/module.d.ts:121:13 - error TS2386: Overload signatures must all be optional or required.

121             resolve?(specified: string, parent?: string | URL): Promise<string>;
                ~~~~~~~

And new types where complaining about NodeJS.Timer

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, we should add Node v22 to our test array.
https://nodejs.org/en/about/previous-releases

private timer: NodeJS.Timeout | undefined;

private started: Date;

Expand Down
5 changes: 2 additions & 3 deletions src/repository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import {
Segment,
StrategyTransportInterface,
} from '../strategy/strategy';
// @ts-expect-error
import { EventSource } from 'launchdarkly-eventsource';
import type { EventSource } from '../event-source';

export const SUPPORTED_SPEC_VERSION = '4.3.0';

Expand Down Expand Up @@ -50,7 +49,7 @@ interface FeatureToggleData {
}

export default class Repository extends EventEmitter implements EventEmitter {
private timer: NodeJS.Timer | undefined;
private timer: NodeJS.Timeout | undefined;

private url: string;

Expand Down
5 changes: 3 additions & 2 deletions src/test/repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1413,22 +1413,23 @@ test('Streaming', async (t) => {
};
setup(url, [{ ...feature, name: 'initialFetch' }]);
const storageProvider: StorageProvider<ClientFeaturesResponse> = new InMemStorageProvider();
const eventSource = {
const eventSource: any = {
eventEmitter: new EventEmitter(),
listeners: new Set<string>(),
addEventListener(eventName: string, handler: () => void) {
eventSource.listeners.add(eventName);
eventSource.eventEmitter.on(eventName, handler);
},
close() {
eventSource.listeners.forEach((eventName) => {
eventSource.listeners.forEach((eventName: string) => {
eventSource.eventEmitter.removeAllListeners(eventName);
});
},
emit(eventName: string, data: unknown) {
eventSource.eventEmitter.emit(eventName, data);
},
};

const repo = new Repository({
url,
appName,
Expand Down
3 changes: 1 addition & 2 deletions src/unleash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import { ImpressionEvent, UnleashEvents } from './events';
import { UnleashConfig } from './unleash-config';
import FileStorageProvider from './repository/storage-provider-file';
import { resolveUrl } from './url-utils';
// @ts-expect-error
import { EventSource } from 'launchdarkly-eventsource';
import { EventSource } from './event-source';
import { buildHeaders } from './request';
import { uuidv4 } from './uuidv4';
export { Strategy, UnleashEvents, UnleashConfig };
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"declarationMap": true,
"resolveJsonModule": true,
"esModuleInterop": false,
"allowJs": true,
"skipLibCheck": false
},
"exclude": ["examples/", "lib/", "node_modules/"]
"exclude": ["examples/", "lib/", "node_modules/"],
"include": ["src/**/*.ts", "src/**/*.js"]
}
15 changes: 11 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -731,10 +731,12 @@
dependencies:
undici-types "~5.26.4"

"@types/node@^20.2.5":
version "20.4.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.4.0.tgz#01d637d1891e419bc85763b46f42809cd2d5addb"
integrity sha512-jfT7iTf/4kOQ9S7CHV9BIyRaQqHu67mOjsIQBC3BKZvzvUB6zLxEwJ6sBE3ozcvP8kF6Uk5PXN0Q+c0dfhGX0g==
"@types/node@^20.17.17":
version "20.17.17"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.17.17.tgz#5cea2af2e271313742c14f418eaf5dcfa8ae2e3a"
integrity sha512-/WndGO4kIfMicEQLTi/mDANUu/iVUhT7KboZPdEqqHQ4aTS+3qT3U5gIqWDFV+XouorjfgGqvKILJeHhuQgFYg==
dependencies:
undici-types "~6.19.2"

"@types/normalize-package-data@^2.4.0":
version "2.4.4"
Expand Down Expand Up @@ -5105,6 +5107,11 @@ undici-types@~5.26.4:
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617"
integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==

undici-types@~6.19.2:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it from the upgraded @types/node transitive deps?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

version "6.19.8"
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.19.8.tgz#35111c9d1437ab83a7cdc0abae2f26d88eda0a02"
integrity sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==

unique-filename@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/unique-filename/-/unique-filename-3.0.0.tgz#48ba7a5a16849f5080d26c760c86cf5cf05770ea"
Expand Down