npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@symplify-conversion/sst-sdk-nodejs

v0.5.3

Published

This is the Node.js implementation of the Symplify Server-Side Testing SDK.

Downloads

583

Readme

Symplify Server-Side Testing SDK for Node.js

This is the Node.js implementation of the Symplify Server-Side Testing SDK.

Changes

See CHANGELOG.md

Requirements

Should be compatible with any web framework, as long as it can get and set cookies.

Installing

npm i @symplify-conversion/sst-sdk-nodejs

Usage

See the examples directory for full code examples, the snippets below have been extracted from their surrounding app code for brevity.

Initializing

// You need to import the SDK, of course.
const sstsdk = require('@symplify-conversion/sst-sdk-nodejs');

// The SDK should be setup once in a long runnning server process, e.g. on
// startup with your other dependecies.
const sst = new sstsdk(process.env['SSTSDK_WEBSITEID']);

Finding a project variation

When you want to select different code paths based on variations in an A/B test, call the findVariation method of the SDK client.

For example: if you have a webshop, with a function views.productPage which renders HTML for a product details page, and shows any applicable discounts:

app.get('/products/:sku', (req, res) => {
    const discounts = getDiscounts(req, res);
    res.send(views.productPage(sku, discounts));
})

getDiscounts has some business logic for identifying applicable discounts, and here you decide to test different variations. You set up a test named "Discounts, May 2022" with variations "Original", "huge", and "small", and then use the SDK client to find the variation for each web request.

function getDiscounts(req, res) {

    // `findVariation` needs a cookie adapter, see below in this README for example code.
    const cookies = cookieJar('.example.com', req, res);

    switch (sst.findVariation('Discounts, May 2022', cookies)) {
        case 'huge':
            return [0.25];
        case 'small':
            return [0.1];
    }

    // Always have a fall back. `findVariation` returns null if the visitor was
    // not allocated. This example project also has a variation named 'Original'
    // which we let fall through here.
    return [];
}

Cookie integration

See SST-documentation repository for general cookie setup information.

To ensure visitors get the same variation consistently, the SDK needs to read and write cookies. Each web framework has a different way to get this functionality. This function cookieJar is one way to make an adapter for using the SDK when using the express and cookie-parser libraries.

function cookieJar(domain, req, res) {
    return {
        get: (name) => req.cookies[name],
        set: (name, value, expiresInDays) => {
            const expires = new Date(Date.now() + expiresInDays * 24 * 3600 * 1000);
            res.cookie(name, value, { expires, domain });
        },
    }
}

Custom audience

It's possible to limit for which requests/visitors a certain test project should apply by using "audience" rules. See Audiences.md for details.

The audience is evaluated when your server calls findVariation, and if the rules you have setup in the audience references "custom attributes" your server must provide the values of these attributes for each request.

For example, you might want a test project to only apply for visitors from a certain country. The audience can be configured in your project, using a custom attribute "country", and then your server provides it when finding the variation:

function getDiscounts(req, res) {

    // `cookieJar` is an example function explained in this README
    const cookies = cookieJar('.example.com', req, res);
    // this code assumes you have a `lookupGeoIP` helper function in your project 
    const country = lookupGeoIP(req)?.country || "unknown";

    const customAttributes = { country };

    switch (sst.findVariation('Discounts, May 2022', cookies, customAttributes)) {
        case 'huge':
            return [0.25];
        case 'small':
            return [0.1];
    }

    // `findVariation` returns null if the project audience does not match for
    // a given request. We handle that by a fallthrough return here.
    return [];
}

SDK Development

See CONTRIBUTING.md or RELEASING.md.