Skip to content

Commit

Permalink
Resolve double polling for node-sdk
Browse files Browse the repository at this point in the history
Fixes #204
  • Loading branch information
rvowles committed Feb 26, 2025
1 parent 079953e commit 1a4bff0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion featurehub-javascript-client-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"eslint": "^8.0.0",
"eslint-plugin-filenames-simple": "0.7.0",
"mocha": "^9.1.4",
"sinon": "^15.0.1",
"sinon": "^15.0.1",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
},
Expand Down
3 changes: 3 additions & 0 deletions featurehub-javascript-node-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#### 1.4.0
- polling functionality changed to ensure double polling doesn't happen when the context is changed, but as node
relies on client-sdk, we have to bump the versions.
#### 1.3.3
- listeners were not being fired when added to contexts that matched strategies. [bugfix](https://github.com/featurehub-io/featurehub-javascript-sdk/issues/196)
- all the getX methods on the Context now have defaults, so you can say fhContext.getFlag("feature", false) and if it isn't set or doesn't exist, it will return false. This is an optional field so it doesn't break existing code. (feature)
Expand Down
33 changes: 31 additions & 2 deletions featurehub-javascript-node-sdk/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import {
FeatureHubPollingClient,
FeatureStateUpdate, FeatureUpdatePostManager, FeatureUpdater, GoogleAnalyticsApiClient, GoogleAnalyticsCollector,
FeatureStateUpdate,
FeatureUpdatePostManager,
FeatureUpdater,
GoogleAnalyticsApiClient,
GoogleAnalyticsCollector,
NodejsOptions,
PollingBase,
FeatureHubEventSourceClient,
PollingService, FeaturesFunction, FeatureEnvironmentCollection, EdgeFeatureHubConfig, FHLog, fhLog
PollingService,
FeaturesFunction,
FeatureEnvironmentCollection,
EdgeFeatureHubConfig,
FHLog,
fhLog,
PromiseLikeFunction, RejectLikeFunction
} from 'featurehub-javascript-client-sdk';
import { URL } from 'url';
import { RequestOptions } from 'https';
Expand All @@ -17,6 +27,11 @@ FeatureHubEventSourceClient.eventSourceProvider = (url, dict) => {
return new ES(url, dict);
};

interface PromiseLikeData {
resolve: PromiseLikeFunction;
reject: RejectLikeFunction;
}

export type ModifyRequestFunction = (options: RequestOptions) => void;

export class NodejsPollingService extends PollingBase implements PollingService {
Expand All @@ -32,10 +47,18 @@ export class NodejsPollingService extends PollingBase implements PollingService
}

public poll(): Promise<void> {
if (this._busy) {
return new Promise((resolve, reject) => {
this._outstandingPromises.push({ resolve: resolve, reject: reject } as PromiseLikeData);
});
}

if (this._stopped) {
return new Promise((resolve) => resolve());
}

this._busy = true;

return new Promise(((resolve, reject) => {
const http = this.uri.protocol === 'http:' ? require('http') : require('https');
let data = '';
Expand Down Expand Up @@ -71,10 +94,16 @@ export class NodejsPollingService extends PollingBase implements PollingService
this._etag = res.headers.etag;
this._callback(JSON.parse(data) as Array<FeatureEnvironmentCollection>);
this._stopped = (res.statusCode === 236);
this._busy = false;
this.resolveOutstanding();
resolve();
} else if (res.statusCode == 304) {
this._busy = false;
this.resolveOutstanding();
resolve();
} else {
this._busy = false;
this.rejectOutstanding(req.status);
reject(res.statusCode);
}
});
Expand Down
9 changes: 4 additions & 5 deletions featurehub-javascript-node-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "featurehub-javascript-node-sdk",
"version": "1.3.3",
"version": "1.4.0",
"description": "FeatureHub NodeJS SDK",
"author": "[email protected]",
"main": "dist/index.js",
Expand Down Expand Up @@ -48,10 +48,9 @@
"@fluffy-spoon/substitute": "^1.208.0",
"@types/chai": "^4.2.18",
"@types/mocha": "^9.0.0",
"@types/node": "^12.20.12",
"@types/node": "^18.0.0",
"chai": "^4.2.0",
"mocha": "^9.2.0",
"nyc": "^15.1.0",
"typescript": "4.5.4",
"ts-node": "^10.0.0",
"eslint": "^8.0.0",
Expand All @@ -62,9 +61,9 @@
},
"dependencies": {
"eventsource": "^2.0.2",
"featurehub-javascript-client-sdk": "^1.3.2"
"featurehub-javascript-client-sdk": "^1.4.0"
},
"engines": {
"node": ">=14.0.0"
"node": ">=18.0.0"
}
}

0 comments on commit 1a4bff0

Please sign in to comment.