@alan-eu/now-whitelisted-static-build

0.0.3 • Public • Published

alan-now-whitelisted-static-build

This Builder allows you to build and deploy a static site to now while only giving access to whitelisted ips. It is based off of @now/static-build. The usage is exactly the same except there is an additional config option allowedIps which is an array of IP addresses that can access your site.

Usage

{
  "version": 2,
  "builds": [
    {
      "src": "package.json",
      "use": "@alan-eu/now-whitelisted-static-build",
      "config": { "allowedIps": ["some-ip-address"] }
    }
  ]
}

Deployment

In order for this builder to run on the now platform, the source code needs to be publicly available (see related issue). For this reason, this package is published to npm. The package home page is here.

Currently the deployment is done manually.

# make sure you are on acceptance and that the version
# in package.json has been bumped since the previous publish
cd alan-now-whitelisted-static-build
npm login # if you are not a part of the @alan-eu org in npm ping @terrence.wong or @cg
npm publish --access public

How Do Builders Work?

In the future, I may write a lengthier blog post about how Builders work. Here is a short overview:

The now platform has a serverless execution model. A deployed project will be made of many endpoints. These endpoints are also called lambdas. A lambda is simply a function that responds to an HTTP request.

A Builder is a module that transforms source files into lambdas. Zeit has created some utilities to help write your own custom builder. The utility that allows you to declare a lamba is called createLambda.

const { createLambda, FileBlob } = require('@now/build-utils')
exports.build = async () => {
  const lambda = await createLambda({
    runtime: 'nodejs8.10',
    handler: 'index.main',
    files: {
      'index.js': new FileBlob({ data: 'exports.main = () => {}' })
    }
  });
  return {
    output: {
      "/": lambda
    }
  }
}

The lambda above contains a single file called index.js and when the lambda executes, it should run the export main inside index.js.

To declare the endpoint for a lambda, the Builder module has an export called build which should return an object mapping paths to lambdas. In this case if someone requests the path "/", then our lambda will run.

Additional documentaiton on buidlers can be found here.

How Does This Builder Work?

This builder creates a single lambda at the endpoint ${srcBase}/serve-static-files-to-whitelisted-ips. srcBase is based off of your now.json file.

Example The endpoint the builder creates for the following now.json will be /design-system/serve-static-files-to-whitelisted-ips

{
  "version": 2,
  "builds": [
    {
      "src": "design-system/package.json",
      "use": "@alan-eu/now-whitelisted-static-build",
      "config": { "allowedIps": ["some-ip-address"] }
    }
  ]
}

This builder also sets up routing so that any request to /design-system/(.*) will execute the lambda as /design-system/serve-static-files-to-whitelisted-ips?pathname=$1.

When the lambda executes, it first checks that the requester's IP address is one of the allowedIps declared in now.json. If no, the builder returns 403. If yes, then the builder serves the file that was requested. If the path requested is a directory, then we try to serve an index.html file located in the directory.

Readme

Keywords

none

Package Sidebar

Install

npm i @alan-eu/now-whitelisted-static-build

Weekly Downloads

1

Version

0.0.3

License

MIT

Unpacked Size

12.2 kB

Total Files

4

Last publish

Collaborators

  • charles_go
  • wongterrencew