diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000000..62f203b283 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,19 @@ +module.exports = { + env: { + node: true, + }, + extends: [ + 'airbnb-base', + 'airbnb-typescript/base' + ], + parser: '@typescript-eslint/parser', + parserOptions: { + project: './tsconfig.eslint.json' + }, + plugins: [ + '@typescript-eslint', + ], + ignorePatterns: ["**/dist/**"], + rules: { + }, +}; diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..1ca0d4b299 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +package-lock.json +dist/ +node_modules/ +**/*.tsbuildinfo diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000000..215e2da5c8 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,49 @@ +# Contributing to the LaunchDarkly Server-Side SDK for Node.js + +LaunchDarkly has published an [SDK contributor's guide](https://docs.launchdarkly.com/sdk/concepts/contributors-guide) that provides a detailed explanation of how our SDKs work. See below for additional information on how to contribute to this SDK. + +## Submitting bug reports and feature requests + +The LaunchDarkly SDK team monitors the [issue tracker](https://github.com/launchdarkly/node-server-sdk/issues) in the SDK repository. Bug reports and feature requests specific to this SDK should be filed in this issue tracker. The SDK team will respond to all newly filed issues within two business days. + +## Submitting pull requests + +We encourage pull requests and other contributions from the community. Before submitting pull requests, ensure that all temporary or unintended code is removed. Don't worry about adding reviewers to the pull request; the LaunchDarkly SDK team will add themselves. The SDK team will acknowledge all pull requests within two business days. + +## Build instructions + +### Prerequisites + +The project should be built and tested against the lowest compatible version, Node 12. It uses `npm`, which is bundled in all supported versions of Node. + +### Setup + +To install project dependencies, from the project root directory: + +``` +npm install +``` + +### Testing + +To run all unit tests: + +``` +npm test +``` + +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. + +Runtime dependencies can affect applications using the SDK; they can only be fixed by updating one of the explicit dependencies in `package.json`. Development dependencies cannot affect applications, but will still cause `npm audit` to flag the project; they can be fixed by running `npm audit fix` to add overrides for transitive dependencies in `package-lock.json`. + +It is important _not_ to run `npm audit fix` if there are any bad _runtime_ dependencies, because it will hide the problem in our own build, without actually fixing the vulnerability when an application uses the SDK. + +The script `scripts/better-audit.sh`, which is run in the CI build and can also be run manually, processes the output of `npm audit` to eliminate all duplicate entries and then determines whether each entry is coming from a runtime dependency or a development dependency. If there are any runtime ones, it terminates with an error code so the build will fail. diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000000..d238a2b01d --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,13 @@ +Copyright 2022 Catamorphic, Co. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000000..ee30e8149c --- /dev/null +++ b/README.md @@ -0,0 +1,54 @@ +# LaunchDarkly Server-Side SDK for JavaScript + + + +The LaunchDarkly Server-Side SDK for Node.js is designed primarily for use in multi-user systems such as web servers and applications. It follows the server-side LaunchDarkly model for multi-user contexts. It is not intended for use in desktop and embedded systems applications. + +For using LaunchDarkly in *client-side* Node.js applications, refer to our [Client-side Node.js SDK](https://github.com/launchdarkly/node-client-sdk). + +## LaunchDarkly overview + +[LaunchDarkly](https://www.launchdarkly.com) is a feature management platform that serves over 100 billion feature flags daily to help teams build better software, faster. [Get started](https://docs.launchdarkly.com/home/getting-started) using LaunchDarkly today! + +[![Twitter Follow](https://img.shields.io/twitter/follow/launchdarkly.svg?style=social&label=Follow&maxAge=2592000)](https://twitter.com/intent/follow?screen_name=launchdarkly) + +## Supported Node versions + +This version of the LaunchDarkly SDK is compatible with Node.js versions 12 and above. + +## Getting started + +Refer to the [SDK reference guide](https://docs.launchdarkly.com/sdk/server-side/node-js) for instructions on getting started with using the SDK. + +## Learn more + +Check out our [documentation](http://docs.launchdarkly.com) for in-depth instructions on configuring and using LaunchDarkly. You can also head straight to the [complete reference guide for this SDK](https://docs.launchdarkly.com/sdk/server-side/node-js). + +The authoritative description of all properties and methods is in the [TypeScript documentation](https://launchdarkly.github.io/node-server-sdk/). + +## Testing + +We run integration tests for all our SDKs using a centralized test harness. This approach gives us the ability to test for consistency across SDKs, as well as test networking behavior in a long-running application. These tests cover each method in the SDK, and verify that event sending, flag evaluation, stream reconnection, and other aspects of the SDK all behave correctly. + +## Contributing + +We encourage pull requests and other contributions from the community. Check out our [contributing guidelines](CONTRIBUTING.md) for instructions on how to contribute to this SDK. + +## About LaunchDarkly + +* LaunchDarkly is a continuous delivery platform that provides feature flags as a service and allows developers to iterate quickly and safely. We allow you to easily flag your features and manage them from the LaunchDarkly dashboard. With LaunchDarkly, you can: + * Roll out a new feature to a subset of your users (like a group of users who opt-in to a beta tester group), gathering feedback and bug reports from real-world use cases. + * Gradually roll out a feature to an increasing percentage of users, and track the effect that the feature has on key metrics (for instance, how likely is a user to complete a purchase if they have feature A versus feature B?). + * Turn off a feature that you realize is causing performance problems in production, without needing to re-deploy, or even restart the application with a changed configuration file. + * Grant access to certain features based on user attributes, like payment plan (eg: users on the ‘gold’ plan get access to more features than users in the ‘silver’ plan). Disable parts of your application to facilitate maintenance, without taking everything offline. +* LaunchDarkly provides feature flag SDKs for a wide variety of languages and technologies. Check out [our documentation](https://docs.launchdarkly.com/sdk) for a complete list. +* Explore LaunchDarkly + * [launchdarkly.com](https://www.launchdarkly.com/ "LaunchDarkly Main Website") for more information + * [docs.launchdarkly.com](https://docs.launchdarkly.com/ "LaunchDarkly Documentation") for our documentation and SDK reference guides + * [apidocs.launchdarkly.com](https://apidocs.launchdarkly.com/ "LaunchDarkly API Documentation") for our API documentation + * [blog.launchdarkly.com](https://blog.launchdarkly.com/ "LaunchDarkly Blog Documentation") for the latest product updates diff --git a/package.json b/package.json new file mode 100644 index 0000000000..d1a31dda1f --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "@launchdarkly/js-server-sdk", + "workspaces": [ + "sdk-common", + "server-sdk-common", + "platform-node" + ], + "scripts": { + "clean": "tsc --build --clean", + "build": "tsc --build", + "lint": "eslint . --ext .ts", + "lint:fix": "npm run lint -- --fix" + }, + "devDependencies": { + "@typescript-eslint/eslint-plugin": "^5.22.0", + "@typescript-eslint/parser": "^5.22.0", + "eslint": "^8.14.0", + "eslint-config-airbnb-base": "^15.0.0", + "eslint-config-airbnb-typescript": "^17.0.0", + "eslint-plugin-import": "^2.26.0" + } +} diff --git a/platform-node/package.json b/platform-node/package.json new file mode 100644 index 0000000000..0ec7bbff11 --- /dev/null +++ b/platform-node/package.json @@ -0,0 +1,27 @@ +{ + "name": "@launchdarkly/js-server-sdk-platform-node", + "version": "1.0.0", + "description": "Node platform for the JS Server SDK", + "type": "commonjs", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "keywords": [ + "launchdarkly", + "analytics", + "client" + ], + "scripts": { + "test": "jest --ci", + "build": "tsc" + }, + "license": "Apache-2.0", + "dependencies": { + "@launchdarkly/js-server-sdk-common": "@launchdarkly/js-server-sdk-common" + }, + "devDependencies": { + "@types/jest": "^27.4.1", + "jest": "^27.5.1", + "ts-jest": "^27.1.4", + "typescript": "^4.6.3" + } +} diff --git a/platform-node/src/index.ts b/platform-node/src/index.ts new file mode 100644 index 0000000000..e9efeb0ad3 --- /dev/null +++ b/platform-node/src/index.ts @@ -0,0 +1,3 @@ +import doesItWork from '@launchdarkly/js-server-sdk-common'; + +doesItWork(); diff --git a/platform-node/tsconfig.json b/platform-node/tsconfig.json new file mode 100644 index 0000000000..34a316938d --- /dev/null +++ b/platform-node/tsconfig.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "rootDir": "src", + "outDir": "dist", + "target": "es6", + "lib": [ + "es6" + ], + "module": "commonjs", + "strict": true, + "noImplicitOverride": true, + // Needed for CommonJS modules: markdown-it, fs-extra + "allowSyntheticDefaultImports": true, + "sourceMap": true, + "declaration": true, + "declarationMap": true, // enables importers to jump to source + } +} diff --git a/platform-node/tsconfig.ref.json b/platform-node/tsconfig.ref.json new file mode 100644 index 0000000000..6ac0afa664 --- /dev/null +++ b/platform-node/tsconfig.ref.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "include": ["src/**/*"], + "compilerOptions": { + "composite": true, + }, +} diff --git a/sdk-common/package.json b/sdk-common/package.json new file mode 100644 index 0000000000..43388206eb --- /dev/null +++ b/sdk-common/package.json @@ -0,0 +1,26 @@ +{ + "name": "@launchdarkly/js-sdk-common", + "type": "commonjs", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "description": "LaunchDarkly SDK for JavaScript - common code", + "files": [ + "/dist" + ], + "keywords": [ + "launchdarkly", + "analytics", + "client" + ], + "scripts": { + "test": "jest --ci", + "build": "tsc" + }, + "license": "Apache-2.0", + "devDependencies": { + "@types/jest": "^27.4.1", + "jest": "^27.5.1", + "ts-jest": "^27.1.4", + "typescript": "^4.6.3" + } +} diff --git a/sdk-common/src/index.ts b/sdk-common/src/index.ts new file mode 100644 index 0000000000..e69de29bb2 diff --git a/sdk-common/tsconfig.json b/sdk-common/tsconfig.json new file mode 100644 index 0000000000..0c10db0c9d --- /dev/null +++ b/sdk-common/tsconfig.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "rootDir": "src", + "outDir": "dist", + "target": "es6", + "lib": [ + "es6", + ], + "module": "commonjs", + "strict": true, + "noImplicitOverride": true, + // Needed for CommonJS modules: markdown-it, fs-extra + "allowSyntheticDefaultImports": true, + "sourceMap": true, + "declaration": true, + "declarationMap": true, // enables importers to jump to source + } +} diff --git a/sdk-common/tsconfig.ref.json b/sdk-common/tsconfig.ref.json new file mode 100644 index 0000000000..6ac0afa664 --- /dev/null +++ b/sdk-common/tsconfig.ref.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "include": ["src/**/*"], + "compilerOptions": { + "composite": true, + }, +} diff --git a/server-sdk-common/package.json b/server-sdk-common/package.json new file mode 100644 index 0000000000..97edcb76bd --- /dev/null +++ b/server-sdk-common/package.json @@ -0,0 +1,26 @@ +{ + "name": "@launchdarkly/js-server-sdk-common", + "type": "commonjs", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "description": "LaunchDarkly Server SDK for JavaScript - common code", + "files": [ + "/dist" + ], + "keywords": [ + "launchdarkly", + "analytics", + "client" + ], + "scripts": { + "test": "jest --ci", + "build": "tsc" + }, + "license": "Apache-2.0", + "devDependencies": { + "@types/jest": "^27.4.1", + "jest": "^27.5.1", + "ts-jest": "^27.1.4", + "typescript": "^4.6.3" + } +} diff --git a/server-sdk-common/src/index.ts b/server-sdk-common/src/index.ts new file mode 100644 index 0000000000..243647d62c --- /dev/null +++ b/server-sdk-common/src/index.ts @@ -0,0 +1,3 @@ +export default function doesItWork() { + console.log('yes'); +} diff --git a/server-sdk-common/tsconfig.json b/server-sdk-common/tsconfig.json new file mode 100644 index 0000000000..0c10db0c9d --- /dev/null +++ b/server-sdk-common/tsconfig.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "rootDir": "src", + "outDir": "dist", + "target": "es6", + "lib": [ + "es6", + ], + "module": "commonjs", + "strict": true, + "noImplicitOverride": true, + // Needed for CommonJS modules: markdown-it, fs-extra + "allowSyntheticDefaultImports": true, + "sourceMap": true, + "declaration": true, + "declarationMap": true, // enables importers to jump to source + } +} diff --git a/server-sdk-common/tsconfig.ref.json b/server-sdk-common/tsconfig.ref.json new file mode 100644 index 0000000000..6ac0afa664 --- /dev/null +++ b/server-sdk-common/tsconfig.ref.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "include": ["src/**/*"], + "compilerOptions": { + "composite": true, + }, +} diff --git a/tsconfig.eslint.json b/tsconfig.eslint.json new file mode 100644 index 0000000000..56c9b38305 --- /dev/null +++ b/tsconfig.eslint.json @@ -0,0 +1,5 @@ +{ + "extends": "./tsconfig.json", + "include": ["/**/*.ts"], + "exclude": ["node_modules"] +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000000..d405b65d56 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,14 @@ +{ + "files": [], + "references": [ + { + "path": "./sdk-common/tsconfig.ref.json" + }, + { + "path": "./server-sdk-common/tsconfig.ref.json" + }, + { + "path": "./platform-node/tsconfig.ref.json" + }, + ], +}