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 🙏

© 2025 – Pkg Stats / Ryan Hefner

hapi-serve-s3

v2.0.1

Published

Easily serve files from S3 using Hapi

Downloads

339

Readme

hapi-serve-s3

Build Status Dependency Status

Easily serve files from an S3 bucket.

Plugin Usage

Register the plugin to your hapi server:

server.register(require('hapi-serve-s3'), function(err) {
  if (err) {
    throw err;
  }
});

Plugin options

None so far.

Route Definition

Use s3 as a handler:

// Serve a file from s3://my-awesome-bucket/path/to/file.pdf
serve.route({
  method: 'GET',
  route: '/file.pdf',
  handler: {
    s3: {
      bucket: 'my-awesome-bucket',
      mode: 'attachment',
      filename: function(request) { // when downloaded from a browser, this will be the recommended download name
        return Promise.resolve('awesome-pdf.pdf');
      },
      key: function(request) {
        return Promise.resolve('path/to/file.pdf');
      },
      overrideContentTypes: {
        // application/octet-stream is the default that S3 serves if you don't
        // tell them the MIME type when uploading the file
        'application/octet-stream': 'application/pdf',
      },
    },
  },
});
// Upload + Serve files from s3://my-awesome-bucket/path/to/*.pdf
serve.route({
  method: ['GET', 'POST'],
  route: '/files/{path*}',
  handler: {
    s3: {
      bucket: 'my-awesome-bucket',
      mode: 'attachment',
      key: 'path/to',
      overrideContentTypes: {
        // application/octet-stream is the default that S3 serves if you don't
        // tell them the MIME type when uploading the file
        'application/octet-stream': 'application/pdf',
      },
    },
  },
});
// Upload + Serve + Delete files from s3 with custom authentication strategy
serve.route({
  method: ['GET', 'POST', 'DELETE'],
  route: '/files/{path*}',
  handler: {
    s3: {
      bucket: 'my-awesome-bucket',
      key: function(request)  {
        return request.pre.authPath
      }
    },
  },
  config: {
    pre: [
      {
        assign: 'authPath',
        method: function(request, reply) {
          // ... auth strategy here
          if (!ok) {
            return reply(Boom.unauthorized())
          }

          return replay(key)
        }
      }
    ]
  }
});
// Custom reply strategy
serve.route({
  method: 'POST',
  route: '/files/{path*}',
  handler: {
    s3: {
      bucket: 'my-awesome-bucket',
      onResponse(err, res, request, reply, options) {
        if (err) {
          return reply(err);
        }

        const myPayload options.uploads.map(/* custom mapping */);
        return reply(myPayload).code(options.defaultStatusCode);
      }
    },
  }
});

Handler Options:

  • bucket (String|Function)
    • If a string is provided it will be used as bucket name.
    • If a function is provided, it should return or resolve the bucket.
      • if function: bucket(request) -> Promise|String
  • key ([String|Function])
    • If a string is provided, then it will be used to look up the key:
      • if the route contains a parameter "path", the key will be treated as a prefix otherwise, the key will be treated as the literal S3 key
      • for "POST": always used as prefix
    • If a function is provided, it should return or resolve the key.
      • if function: key(request) -> Promise|String
    • If not given try:
      • to use the "path" parameter
      • 'POST': try to use the FormData's key name
  • randomPostKeys ([Bool]) default=false
    • If set, randomizes the S3 Key (basename) for POST request
  • mode ([Bool|String]) default=auto
    • Specifies whether to include the Content-Disposition header.
      • if false: no content-disposition header will be set
      • if auto:
        • for 'GET':
          • try to load header from S3 directly
          • try 'attachment'
        • for 'POST'
          • try 'attachement'
      • if attachment: content-disposition will always be set to 'attachment'
      • if inline: content-disposition will always be set to 'inline'
      • if <object>: key=['get', 'post', ...] value=
  • filename ([Function])
    • Get the filename for the content-disposition header.
    • If given, the function should return or resolve the filename. filename will then be added to the Content-Disposition header. @see Content-Disposition
      • if function: filename(request, { bucket, key, [filename] }) -> Promise|String
        • filename: content dispostion file name on S3 / POST form data
      • if not given:
        • if mode=auto: use the S3 ContentType / FormData if exists
        • if mode=attachment|inline: use the key's basenamece
  • overrideContentTypes ([Object]) default={}
    • If S3's reported content-type is key, replace it with value example: { "application/octet-stream" : "application/pdf" }
  • allowedContentTypes ([Array<String|RegExp>])
    • for POST requests, check if the content type is allowed to be uploaded
      • if undefined is part of the list, also allow if no content type was found / will be set
  • ignoredFormKeys ([Array<String|RegExp>])
    • for POST requets, don't try to upload FormData entries with the given names
  • contentType ([String|Function])
    • Set the content-type header to the given value.
      • if string: use as is
      • if function: contentType(request, { bucket, key, [contentType] }) -> Promise|String
        • contentType: content type on S3 / POST form data
      • if not given:
        • use the S3 ContentType / FormData if exists
  • onResponse ([Function])
    • on response handler to update the response
    • onResponse(error, res, request, reply, options) -> void
      • res:
        • "GET": file object stream
        • "POST": S3 Response, extended with ContentType and ContentDisposition if possible
        • "DELETE": null
      • options:
        • "GET": Object<{ bucket, key, contentType, contentDisposition, defaultStatusCode, data }>
        • "POST": Object<{ uploads: Array<Object<{ file: String, bucket, key, contentType, contentDisposition, defaultStatusCode, data }>> }>
        • "DELETE": Object<{ bucket, key, defaultStatusCode, data, s3Response }>
  • region ([String]) default='us-east-1'
    • bucket's region (defaults to us-standard: us-east-1)
  • sslEnabled ([Bool]) default=true
    • use SSL when communicating with S3 (default true)
  • accessKeyId ([String]) default=process.env.AWS_ACCESS_KEY_ID
  • secretAccessKey ([String]) default=process.env.AWS_SECRET_ACCESS_KEY
  • s3Params ([Object])