Code Monkey home page Code Monkey logo

split-node-serializer's Introduction

split-node-serializer

Fetches split definitions and segments from Split.io and serializes them into a set of strings that the JavaScript SDK can consume.

Installation

Install via npm:

$ npm i @godaddy/split-node-serializer --save

Usage

Use this package in your server-side Node.js environment. The serializer exposes:

  1. a Poller that periodically requests raw experiment configuration data from Split.io. Requests happen in the background and the poller caches the latest data in local memory.
  2. a DataSerializer that reads from the poller's cache, serializes the data, and returns it in a script to be injected into a client's HTML.

Constructor

Create an instance of Poller and DataSerializer:

const { Poller, DataSerializer } = require('@godaddy/split-node-serializer')

const poller = new Poller({
  splitioApiKey: 'YOUR_API_KEY',
  pollingRateSeconds: 600,
  serializeSegments: false
})

const dataSerializer = new DataSerializer({ poller })

The following option properties are available to the Poller:

Property Description
splitioApiKey The Split.io SDK key for the environment your app is running in. (required)
pollingRateSeconds The interval at which to poll Split.io. Defaults to 300 (5 minutes).
serializeSegments Whether or not to fetch segment configuration data. Defaults to false.

Serializing segments

Segments are pre-defined groups of customers that features can be targeted to. More info here.

Note: Requesting serialized segments will increase the size of your response. Segments can be very large if they include all company employees, for example.

Methods

start

Make an initial request for changes and start polling for raw configuration data every pollingRateSeconds:

poller.start()

stop

To stop the poller:

poller.stop()

The poller emits an error event on errors from the Split.io API.

generateSerializedDataScript

generateSerializedDataScript is an async method that will read the latest data from the cache and return a script that adds serialized data to the window.__splitCachePreload object. The serialized data will be used to determine cohort allocations.

generateSerializedDataScript accepts the following arguments:

Property Description
splits Array of strings that, if included, filters the splitsData (Optional)
preloadLocation The property on the window object where the data is stored. Defaults to __splitCachePreload (Optional)
const serializedDataScript = await dataSerializer.generateSerializedDataScript()

console.log(serializedDataScript)

//<script>
//  window.__splitCachePreload = {
//    splitsData: {"split-1-name":'{"name":"split-1-name","status":"bar"}',"split-2-name":'{"name":"split-2-name","status":"baz"}'},
//    since: 1,
//    segmentsData: {"test-segment":'{"name":"test-segment","added":["foo","bar"]}'},
//    usingSegmentsCount: 2
//  };
//</script>

Note: Though this is an async method, there will only be a Split.io API call if you have serializeSegments set to true and are passing in a unique splits parameter that this instance's method has not received before.

Testing

Run the linter:

$ npm run lint

Run unit tests:

$ npm test

Generate a coverage report:

$ npm run coverage

License

MIT

split-node-serializer's People

Contributors

agerard-godaddy avatar celiawaggoner avatar cwaggoner-godaddy avatar jh2oman avatar jwaterman-godaddy avatar renovate[bot] avatar rohanmurthy avatar rxmarbles avatar silasbw avatar wendychiang avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

split-node-serializer's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update dependency standard to v17

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/cicd.yaml
  • actions/checkout v2
  • actions/setup-node v2
npm
package.json
  • axios ^0.21.0
  • chai 4.3.4
  • mocha 8.4.0
  • nock 13.2.0
  • nyc 15.1.0
  • sinon 11.1.1
  • standard 16.0.3
  • standard-version 9.3.1

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.