node-cloudworker

1.4.3 • Public • Published

The node-cloudworker is a cloudflare worker shim for node combined with a small webserver, enabling debugging locally. It should not be used in production.

It's most likely not as close to the production environment as https://github.com/dollarshaveclub/cloudworker, which most of the runtime has been borrowed from, but it makes debugging and testing possible.

Installing

Installing via NPM:

npm install node-cloudworker --save

Package Usage

const ncw = require('node-cloudworker');

// Some handlers may depend on the shims
ncw.applyShims();

const handler = event => {
    return new Response('hello', { status: 200 });
};

ncw.start(handler);

Cloudflare API's

Cloudflare exposes some cloudflare-specific API's inside the workers. Parts of these are supported with shims.

Cache

The cache api is borrowed from the Cloudworker repo.

KV-Storage (Beta)

The kv-storage api is using the Cloudflare rest-api to access the KV-Storage. It requires some configuration when applying the shims so that it can access the cloudflare api's:

ncw.applyShims({
  kv: {
    accountId: process.env.CLOUDFLARE_ACCOUNT_ID,
    authEmail: process.env.CLOUDFLARE_AUTH_EMAIL,
    authKey: process.env.CLOUDFLARE_AUTH_KEY,
    bindings: [
      {
        variable: 'TEST',
        namespace: process.env.KV_STORAGE_NAMESPACE,
      },
    ],
  },
});

It is also possible to use the local filesystem to mock KV-Storage. So far it only supports get requests:

ncw.applyShims({
  kv: {
    accountId: process.env.CLOUDFLARE_ACCOUNT_ID,
    authEmail: process.env.CLOUDFLARE_AUTH_EMAIL,
    authKey: process.env.CLOUDFLARE_AUTH_KEY,
    bindings: [
      {
        useFilesystem: true,
        variable: 'TEST_FS',
        namespace: 'examples/testfs',
      },
    ],
  },
});

Package Sidebar

Install

npm i node-cloudworker

Weekly Downloads

0

Version

1.4.3

License

MIT

Unpacked Size

21.2 kB

Total Files

21

Last publish

Collaborators

  • markusahlstrand