Skip to content

Commit

Permalink
switch to publishing js-sdk-common as a regular Node module (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
eli-darkly authored Jan 20, 2022
1 parent edf5c7a commit 89c7a41
Show file tree
Hide file tree
Showing 22 changed files with 241 additions and 226 deletions.
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ To verify that the TypeScript declarations compile correctly (this involves comp
```
npm run check-typescript
```

### Coding guidelines

This code is shared between several SDK projects: `js-client-sdk` which runs in browsers, `node-client-sdk` which runs in Node.js, and `electron-client-sdk` which uses it both in a Node environment and in a browser environment.

Therefore, it should not have any JavaScript usages that work _only_ in a browser or _only_ in Node.js. All such things, if they depend on the runtime environment, must be accessed indirectly via the "platform" abstraction, where each SDK provides its own platform-specific callbacks for the `js-sdk-common` code to use. Or, if it's just a question of JavaScript syntax usages, use whatever will work in both browsers and Node (for instance, use only `require` and `module.exports`, not ES6 imports).
30 changes: 2 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,8 @@
"description": "LaunchDarkly SDK for JavaScript - common code",
"author": "LaunchDarkly <team@launchdarkly.com>",
"license": "Apache-2.0",
"files": [
"ldclient-common.cjs.js",
"ldclient-common.cjs.js.map",
"ldclient-common.es.js",
"ldclient-common.es.js.map",
"ldclient-common.min.js",
"ldclient-common.min.js.map",
"typings.d.ts"
],
"types": "./typings.d.ts",
"main": "dist/ldclient-common.cjs.js",
"module": "dist/ldclient-common.es.js",
"unpkg": "dist/ldclient-common.min.js",
"jsdelivr": "dist/ldclient-common.min.js",
"main": "src/index.js",
"scripts": {
"lint": "eslint --format 'node_modules/eslint-formatter-pretty' --ignore-path .eslintignore",
"lint:all": "eslint --format 'node_modules/eslint-formatter-pretty' --ignore-path .eslintignore src",
Expand All @@ -27,14 +15,8 @@
"format:test": "npm run format:test:md && npm run format:test:js",
"format:test:md": "prettier --parser markdown --ignore-path .prettierignore --list-different '*.md'",
"format:test:js": "prettier --ignore-path .prettierignore --list-different 'src/**/*.js'",
"build": "cross-env NODE_ENV=development rollup -c rollup.config.js",
"build:min": "cross-env NODE_ENV=production rollup -c rollup.config.js",
"test": "cross-env NODE_ENV=test jest",
"test:junit": "cross-env NODE_ENV=test jest --testResultsProcessor jest-junit",
"check-typescript": "node_modules/typescript/bin/tsc",
"clean": "rimraf dist/**",
"prepublishOnly": "npm run build:min",
"prepare": "npm run build"
"check-typescript": "node_modules/typescript/bin/tsc"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
Expand All @@ -43,7 +25,6 @@
"@babel/plugin-transform-runtime": "^7.6.2",
"@babel/preset-env": "^7.6.3",
"@babel/runtime": "7.6.3",
"@rollup/plugin-node-resolve": "^6.0.0",
"@rollup/plugin-replace": "^2.2.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^25.1.0",
Expand All @@ -59,13 +40,6 @@
"launchdarkly-js-test-helpers": "1.1.0",
"prettier": "1.11.1",
"readline-sync": "^1.4.9",
"rimraf": "^2.6.2",
"rollup": "^1.26.0",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-filesize": "^6.2.1",
"rollup-plugin-terser": "5.2.0",
"rollup-plugin-uglify": "6.0.4",
"semver": "^5.5.0",
"semver-compare": "^1.0.0",
"typescript": "~4.4.4"
Expand Down
73 changes: 0 additions & 73 deletions rollup.config.js

This file was deleted.

4 changes: 3 additions & 1 deletion src/EventEmitter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function EventEmitter(logger) {
function EventEmitter(logger) {
const emitter = {};
const events = {};

Expand Down Expand Up @@ -56,3 +56,5 @@ export default function EventEmitter(logger) {
};
return emitter;
}

module.exports = EventEmitter;
16 changes: 9 additions & 7 deletions src/EventProcessor.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import EventSender from './EventSender';
import EventSummarizer from './EventSummarizer';
import UserFilter from './UserFilter';
import * as errors from './errors';
import * as messages from './messages';
import * as utils from './utils';
const EventSender = require('./EventSender');
const EventSummarizer = require('./EventSummarizer');
const UserFilter = require('./UserFilter');
const errors = require('./errors');
const messages = require('./messages');
const utils = require('./utils');

export default function EventProcessor(
function EventProcessor(
platform,
options,
environmentId,
Expand Down Expand Up @@ -171,3 +171,5 @@ export default function EventProcessor(

return processor;
}

module.exports = EventProcessor;
8 changes: 5 additions & 3 deletions src/EventSender.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as errors from './errors';
import * as utils from './utils';
const errors = require('./errors');
const utils = require('./utils');
const { v1: uuidv1 } = require('uuid');

const MAX_URL_LENGTH = 2000;

export default function EventSender(platform, environmentId, options) {
function EventSender(platform, environmentId, options) {
const imageUrlPath = '/a/' + environmentId + '.gif';
const baseHeaders = utils.extend({ 'Content-Type': 'application/json' }, utils.getLDHeaders(platform, options));
const httpFallbackPing = platform.httpFallbackPing; // this will be set for us if we're in the browser SDK
Expand Down Expand Up @@ -83,3 +83,5 @@ export default function EventSender(platform, environmentId, options) {

return sender;
}

module.exports = EventSender;
4 changes: 3 additions & 1 deletion src/EventSummarizer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function EventSummarizer() {
function EventSummarizer() {
const es = {};

let startDate = 0,
Expand Down Expand Up @@ -80,3 +80,5 @@ export default function EventSummarizer() {

return es;
}

module.exports = EventSummarizer;
6 changes: 4 additions & 2 deletions src/Identity.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as utils from './utils';
const utils = require('./utils');

export default function Identity(initialUser, onChange) {
function Identity(initialUser, onChange) {
const ident = {};
let user;

Expand All @@ -22,3 +22,5 @@ export default function Identity(initialUser, onChange) {

return ident;
}

module.exports = Identity;
6 changes: 4 additions & 2 deletions src/PersistentFlagStore.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as utils from './utils';
const utils = require('./utils');

export default function PersistentFlagStore(storage, environment, hash, ident) {
function PersistentFlagStore(storage, environment, hash, ident) {
const store = {};

function getFlagsKey() {
Expand Down Expand Up @@ -46,3 +46,5 @@ export default function PersistentFlagStore(storage, environment, hash, ident) {

return store;
}

module.exports = PersistentFlagStore;
6 changes: 4 additions & 2 deletions src/PersistentStorage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as messages from './messages';
const messages = require('./messages');

// The localStorageProvider is provided by the platform object. It should have the following
// methods, each of which should return a Promise:
Expand All @@ -14,7 +14,7 @@ import * as messages from './messages';
// It is always possible that the underlying platform storage mechanism might fail or be
// disabled. If so, it's likely that it will keep failing, so we will only log one warning
// instead of repetitive warnings.
export default function PersistentStorage(localStorageProvider, logger) {
function PersistentStorage(localStorageProvider, logger) {
const storage = {};
let loggedError = false;

Expand Down Expand Up @@ -77,3 +77,5 @@ export default function PersistentStorage(localStorageProvider, logger) {

return storage;
}

module.exports = PersistentStorage;
12 changes: 7 additions & 5 deletions src/Requestor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as utils from './utils';
import * as errors from './errors';
import * as messages from './messages';
import promiseCoalescer from './promiseCoalescer';
const utils = require('./utils');
const errors = require('./errors');
const messages = require('./messages');
const promiseCoalescer = require('./promiseCoalescer');

const jsonContentType = 'application/json';

Expand All @@ -13,7 +13,7 @@ function getResponseError(result) {
}
}

export default function Requestor(platform, options, environment) {
function Requestor(platform, options, environment) {
const baseUrl = options.baseUrl;
const useReport = options.useReport;
const withReasons = options.evaluationReasons;
Expand Down Expand Up @@ -107,3 +107,5 @@ export default function Requestor(platform, options, environment) {

return requestor;
}

module.exports = Requestor;
8 changes: 5 additions & 3 deletions src/Stream.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as messages from './messages';
import { base64URLEncode, getLDHeaders, transformHeaders, objectHasOwnProperty } from './utils';
const messages = require('./messages');
const { base64URLEncode, getLDHeaders, transformHeaders, objectHasOwnProperty } = require('./utils');

// The underlying event source implementation is abstracted via the platform object, which should
// have these three properties:
Expand All @@ -16,7 +16,7 @@ import { base64URLEncode, getLDHeaders, transformHeaders, objectHasOwnProperty }
// with no new data, the connection will be cycled.
const streamReadTimeoutMillis = 5 * 60 * 1000; // 5 minutes

export default function Stream(platform, config, environment, diagnosticsAccumulator) {
function Stream(platform, config, environment, diagnosticsAccumulator) {
const baseUrl = config.streamUrl;
const logger = config.logger;
const stream = {};
Expand Down Expand Up @@ -150,3 +150,5 @@ export default function Stream(platform, config, environment, diagnosticsAccumul

return stream;
}

module.exports = Stream;
6 changes: 4 additions & 2 deletions src/UserFilter.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as utils from './utils';
const utils = require('./utils');

/**
* The UserFilter object transforms user objects into objects suitable to be sent as JSON to
* the server, hiding any private user attributes.
*
* @param {Object} the LaunchDarkly client configuration object
**/
export default function UserFilter(config) {
function UserFilter(config) {
const filter = {};
const allAttributesPrivate = config.allAttributesPrivate;
const privateAttributeNames = config.privateAttributeNames || [];
Expand Down Expand Up @@ -71,3 +71,5 @@ export default function UserFilter(config) {
};
return filter;
}

module.exports = UserFilter;
10 changes: 6 additions & 4 deletions src/UserValidator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { v1: uuidv1 } = require('uuid');

import * as errors from './errors';
import * as messages from './messages';
import * as utils from './utils';
const errors = require('./errors');
const messages = require('./messages');
const utils = require('./utils');

// Transforms the user object if necessary to make sure it has a valid key.
// 1. If a key is present, but is not a string, change it to a string.
Expand All @@ -12,7 +12,7 @@ import * as utils from './utils';

const ldUserIdKey = 'ld:$anonUserId';

export default function UserValidator(persistentStorage) {
function UserValidator(persistentStorage) {
function getCachedUserId() {
return persistentStorage.get(ldUserIdKey);
}
Expand Down Expand Up @@ -52,3 +52,5 @@ export default function UserValidator(persistentStorage) {

return ret;
}

module.exports = UserValidator;
1 change: 1 addition & 0 deletions src/__tests__/stubPlatform.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { MockHttpState } from './mockHttp';
// diagnosticSdkData: object // provides the "sdk" property in diagnostic events
// diagnosticPlatformData: object // provides the "platform" property in diagnostic events
// diagnosticUseCombinedEvent: boolean // true if diagnostic events should use the combined model (browser SDK)
// uuid: () => string // function to generate a UUID (this is done differently in browsers vs. Node)
// userAgent: string
// userAgentHeaderName?: string // custom header name to use instead of User-Agent
// version?: string // the SDK version for the User-Agent header, if that is *not* the same as the version of launchdarkly-js-sdk-common
Expand Down
Loading

0 comments on commit 89c7a41

Please sign in to comment.