-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement experimental support for Bun
- Loading branch information
Showing
13 changed files
with
279 additions
and
10,813 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { EventEmitter } from "events"; | ||
import type { IConfigCatClient } from "../ConfigCatClient"; | ||
import type { IAutoPollOptions, ILazyLoadingOptions, IManualPollOptions } from "../ConfigCatClientOptions"; | ||
import { PollingMode } from "../ConfigCatClientOptions"; | ||
import { getClient as getClientCommon } from "../index.pubternals.core"; | ||
import { NodeHttpConfigFetcher } from "../node/NodeHttpConfigFetcher"; | ||
import CONFIGCAT_SDK_VERSION from "../Version"; | ||
|
||
/* Package public API for Bun */ | ||
|
||
/** | ||
* Returns an instance of `ConfigCatClient` for the specified SDK Key. | ||
* @remarks This method returns a single, shared instance per each distinct SDK Key. | ||
* That is, a new client object is created only when there is none available for the specified SDK Key. | ||
* Otherwise, the already created instance is returned (in which case the `pollingMode` and `options` arguments are ignored). | ||
* So, please keep in mind that when you make multiple calls to this method using the same SDK Key, you may end up with multiple references to the same client object. | ||
* @param sdkKey SDK Key to access the ConfigCat config. | ||
* @param pollingMode The polling mode to use. | ||
* @param options Options for the specified polling mode. | ||
*/ | ||
export function getClient<TMode extends PollingMode | undefined>(sdkKey: string, pollingMode?: TMode, options?: OptionsForPollingMode<TMode>): IConfigCatClient { | ||
return getClientCommon(sdkKey, pollingMode ?? PollingMode.AutoPoll, options, | ||
{ | ||
configFetcher: new NodeHttpConfigFetcher(), | ||
sdkType: "ConfigCat-UnifiedJS-Bun", | ||
sdkVersion: CONFIGCAT_SDK_VERSION, | ||
eventEmitterFactory: () => new EventEmitter() | ||
}); | ||
} | ||
|
||
export { createConsoleLogger, createFlagOverridesFromMap, disposeAllClients } from "../index.pubternals.core"; | ||
|
||
/** Options used to configure the ConfigCat SDK in the case of Auto Polling mode. */ | ||
export interface IBunAutoPollOptions extends IAutoPollOptions { | ||
} | ||
|
||
/** Options used to configure the ConfigCat SDK in the case of Lazy Loading mode. */ | ||
export interface IBunLazyLoadingOptions extends ILazyLoadingOptions { | ||
} | ||
|
||
/** Options used to configure the ConfigCat SDK in the case of Manual Polling mode. */ | ||
export interface IBunManualPollOptions extends IManualPollOptions { | ||
} | ||
|
||
export type OptionsForPollingMode<TMode extends PollingMode | undefined> = | ||
TMode extends PollingMode.AutoPoll ? IBunAutoPollOptions : | ||
TMode extends PollingMode.ManualPoll ? IBunManualPollOptions : | ||
TMode extends PollingMode.LazyLoad ? IBunLazyLoadingOptions : | ||
TMode extends undefined ? IBunAutoPollOptions : | ||
never; | ||
|
||
export * from ".."; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { assert } from "chai"; | ||
import { FlagOverrides, IConfigCatClient, PollingMode } from "#lib"; | ||
import * as configcatClient from "#lib/bun"; | ||
|
||
describe("ConfigCatClient tests", () => { | ||
|
||
for (const pollingMode of [PollingMode.AutoPoll, PollingMode.LazyLoad, PollingMode.ManualPoll]) { | ||
it(`getClient() should createInstance with ${PollingMode[pollingMode]}`, () => { | ||
|
||
const client: IConfigCatClient = configcatClient.getClient("SDKKEY-890123456789012/1234567890123456789012", pollingMode); | ||
|
||
assert.isDefined(client); | ||
|
||
client.dispose(); | ||
}); | ||
} | ||
|
||
it("createFlagOverridesFromMap() should createOverrides", () => { | ||
|
||
const overrides: FlagOverrides = configcatClient.createFlagOverridesFromMap({ test: true }, configcatClient.OverrideBehaviour.LocalOnly); | ||
|
||
assert.isDefined(overrides); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import "mocha"; | ||
import { EventEmitter } from "events"; | ||
import * as fs from "fs"; | ||
import * as glob from "glob"; | ||
import * as path from "path"; | ||
import { PlatformAbstractions, initPlatform } from "../helpers/platform"; | ||
import { normalizePathSeparator } from "../helpers/utils"; | ||
import { isTestSpec } from "../index"; | ||
import type { IBunAutoPollOptions, IBunLazyLoadingOptions, IBunManualPollOptions } from "#lib/bun"; | ||
import { getClient } from "#lib/bun"; | ||
import type { IConfigCatKernel } from "#lib/index.pubternals"; | ||
import { NodeHttpConfigFetcher } from "#lib/node/NodeHttpConfigFetcher"; | ||
import sdkVersion from "#lib/Version"; | ||
|
||
const sdkType = "ConfigCat-UnifiedJS-Bun"; | ||
|
||
type IBunOptions = IBunAutoPollOptions | IBunManualPollOptions | IBunLazyLoadingOptions; | ||
|
||
class BunPlatform extends PlatformAbstractions<IBunAutoPollOptions, IBunManualPollOptions, IBunLazyLoadingOptions> { | ||
constructor() { | ||
super(); | ||
this.gc = () => { | ||
Bun.gc(true); | ||
return Promise.resolve(); | ||
}; | ||
} | ||
|
||
pathJoin(...segments: string[]) { return path.join(...segments); } | ||
|
||
readFileUtf8(path: string) { return fs.readFileSync(path, "utf8"); } | ||
|
||
createConfigFetcher(options?: IBunOptions) { return new NodeHttpConfigFetcher(options); } | ||
|
||
createKernel(setupKernel?: (kernel: IConfigCatKernel) => IConfigCatKernel, options?: IBunOptions) { | ||
const kernel: IConfigCatKernel = { configFetcher: this.createConfigFetcher(options), sdkType, sdkVersion, eventEmitterFactory: () => new EventEmitter() }; | ||
return (setupKernel ?? (k => k))(kernel); | ||
} | ||
|
||
protected getClientImpl = getClient; | ||
} | ||
|
||
export const platform = new BunPlatform(); | ||
|
||
initPlatform(platform); | ||
|
||
/* Discover and load tests */ | ||
|
||
const testDir = path.resolve(__dirname, ".."); | ||
|
||
for (const file of glob.globIterateSync(normalizePathSeparator(testDir) + "/**/*.ts", { absolute: false })) { | ||
const [isTest, segments] = isTestSpec(file, "bun"); | ||
if (isTest) { | ||
const fileName = segments[segments.length - 1]; | ||
segments[segments.length - 1] = path.basename(fileName, path.extname(fileName)); | ||
|
||
/* eslint-disable @typescript-eslint/no-require-imports */ | ||
require("../" + segments.join("/")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
/* Override TS compiler settings for IDEs (like VSCode) */ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"types": ["bun", "chai", "mocha"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.