arc-server

<a href="https://www.ebay.com"> <img src="https://img.shields.io/badge/ebay-open%20source-01d5c2.svg" alt="ebay open source"/> </a> <a href="https://img.shields.io/github/license/eBay/arc.svg"> <img src="https://img.shields.io/github/license/eBay/ar

Usage no npm install needed!

<script type="module">
  import arcServer from 'https://cdn.skypack.dev/arc-server';
</script>

README

arc-server

ebay open source MIT licensed travisci build Codecov npm version downloads

API

The arc-server api is split into 3 submodules:

arc-server

import { setFlagsForContext, getFlags } from 'arc-server';
  • setFlagsForContext(flags, fn): begins a new context. any calls made from fn can use getFlags to get the passed flags
  • getFlags(): get flags for the current context
  • useCustomFlagContext(fn): allows using a custom context to store flags. fn is a function that should return the current flagset from the custom context. when using a custom context, setFlagsForContext should not be used.

Example

import { setFlagsForContext, getFlags } from 'arc-server/install';

function start(flags, delay) {
    setFlagsForContext(flags, () => {
        wait(delay);
    }));
}

function wait(delay) {
    setTimeout(logFlags, delay);
}

function logFlags() {
    // The flags weren't passed here, but we can get them from the context
    console.log(getFlags());
}

start(['foo'], 100);
start(['bar'], 10);
start(['baz'], 50);

// After 10ms, { bar:true } is logged
// After 50ms, { baz:true } is logged
// After 100ms, { foo:true } is logged

Example usage in example-arc-server/index.js

arc-server/install

import 'arc-server/install';

If you are not bundling your server files with another arc plugin, you should import/require this module near the beginning of your application entry point before loading any modules that need to be adaptable.

arc-server/proxy

import AdaptiveProxy from 'arc-server/proxy';

An AdaptiveProxy is returned from an import/require call. It can be treated as if it were the underlying module (with a few caveats. You probably won't need to use this module directly.

new AdaptiveProxy(matches)

  • matches: a MatchSet where each value is the loaded module

Proxy caveats

Primitive values

Applies if you require an adaptive file that sets exports to a primitive value:

module.exports = "Hello World";

Proxy and Reflect are used to provide adaptive values, but these do not support primitive values (string, number, boolean).

To work around this, these primitives are converted into instances of String, Number, or Boolean. In many cases, you will be able to treat this as if it were the original value, but there are differences.

One notable example is truthiness:

// Objects are truthy, regardless of value
!!(new Boolean(false)) === true;
!!(new String('')) === true;
!!(new Number(0)) === true;

Another is typeof:

// typeof is object, regardless of value
typeof new Boolean(true) === 'object';
typeof new String('hello') === 'object';
typeof new Number(10) === 'object';

If you need a true primitive, you can convert an adaptive primitive to its resolved primitive value using valueOf:

let string = adaptiveString.valueOf();

Autobound Object.prototype functions

Functions from Object.prototype are bound to the adapted object:

let valueOf = adaptiveValue.valueOf;

// works because it is bound
valueOf();

// this doesn't change, because it was previously bound
valueOf.bind(newThis);