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

cff-tools

v0.6.0

Published

## Install

Downloads

257

Readme

CloudFront Function management Tools (cff-tools)

Install

$ yarn add cff-tools

or

$ npm install --save cff-tools

Define Function

Inline code

import { Function } from "cff-tools";


export const ViewerResponseFunction = new Function({
    name: 'ViewerResponseFunctionFromAPI',
    runtime: 'cloudfront-js-1.0'
  }
).putInlineCode(`
function handler(event) {
    var response = event.response;
    var headers = response.headers;

    // Set HTTP security headers
    // Since JavaScript doesn't allow for hyphens in variable names, we use the dict["key"] notation 
    headers['strict-transport-security'] = { value: 'max-age=63072000; includeSubdomains; preload'}; 
    headers['content-security-policy'] = { value: "default-src 'none'; img-src 'self'; script-src 'self'; style-src 'self'; object-src 'none'"}; 
    headers['x-content-type-options'] = { value: 'nosniff'}; 
    headers['x-frame-options'] = {value: 'DENY'}; 
    headers['x-xss-protection'] = {value: '1; mode=block'}; 

    return response;
}
`)

Import JavaScript file from path

import { join } from 'path'
import { Function } from "cff-tools";

export const ViewerRequestFunction = new Function({
    name: 'ViewerRequestFunctionFromAPI',
    runtime: 'cloudfront-js-1.0'
  }, {
    functionFilePath: join(__dirname, '../functions/viewer_request.js')
  }
)

Create/Update function

The putFunction method will create or update your CloudFront function. When the defined function name exists, it will update. If not, will create a new one.

import { FunctionTask } from 'cff-tools'
import { ViewerResponseFunction } from './test'

(async () => {
  const cfFunction = new FunctionTask(ViewerResponseFunction)
  await cfFunction.putFunction()
})()

If you given withPublish attributes, the function will publish automatically.

import { FunctionTask } from 'cff-tools'
import { ViewerResponseFunction } from './test'

(async () => {
  const cfFunction = new FunctionTask(ViewerResponseFunction)
  await cfFunction.putFunction({
    withPublish: true
  })
})()

Publish function

If you already deployed the function, you can publish it by these script

import { FunctionTask } from 'cff-tools'
import { ViewerResponseFunction } from './function'

(async () => {
  const cfFunction = new FunctionTask(ViewerResponseFunction)
  await cfFunction.publish()
})()

E2E testing

We can test the funcion by using Jest and it.


import { TestResponseEventFactory, FunctionTask } from 'cff-tools';
import { ViewerResponseFunction } from './function'

describe('e2e', () => {
  it('should put security headers', async () => {
    const task = new FunctionTask(ViewerResponseFunction);
    const event = TestResponseEventFactory.create();
    const result = await task.runTestToGetFunctionOutput(event, 'DEVELOPMENT');
    expect(result).toEqual({
      response: {
        headers: {
          'content-security-policy': {
            value:
              "default-src 'none'; img-src 'self'; script-src 'self'; style-src 'self'; object-src 'none'",
          },
          'x-xss-protection': {
            value: '1; mode=block',
          },
          'x-content-type-options': {
            value: 'nosniff',
          },
          'x-frame-options': {
            value: 'DENY',
          },
          'strict-transport-security': {
            value: 'max-age=63072000; includeSubdomains; preload',
          },
        },
        statusDescription: 'OK',
        cookies: {},
        statusCode: 200,
      },
    });
  });
});

Or, we can get the native TestFunction API response by using runTest method instead.


import { TestResponseEventFactory, FunctionTask } from 'cff-tools';
import { ViewerResponseFunction } from './function'

describe('e2e', () => {
  it('should put security headers', async () => {
    const task = new FunctionTask(ViewerResponseFunction);
    const event = TestResponseEventFactory.create();
    const result = await task.runTest(event, 'DEVELOPMENT');
    if (!result || !result.FunctionOutput) return;
    expect(JSON.parse(result.FunctionOutput)).toEqual({
      response: {
        headers: {
          'content-security-policy': {
            value:
              "default-src 'none'; img-src 'self'; script-src 'self'; style-src 'self'; object-src 'none'",
          },
          'x-xss-protection': {
            value: '1; mode=block',
          },
          'x-content-type-options': {
            value: 'nosniff',
          },
          'x-frame-options': {
            value: 'DENY',
          },
          'strict-transport-security': {
            value: 'max-age=63072000; includeSubdomains; preload',
          },
        },
        statusDescription: 'OK',
        cookies: {},
        statusCode: 200,
      },
    });
  });
});