Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@polka/send-type

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@polka/send-type

A response helper that detects & handles Content-Types

  • 0.3.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6.3K
increased by14.95%
Maintainers
1
Weekly downloads
 
Created
Source

@polka/send-type npm

An HTTP response helper that detects Content-Types & handles them accordingly — not limited to Polka!

For a simpler, bare-bones alternative, check out @polka/send instead~

Install

$ npm install --save @polka/send-type

Usage

const { createReadStream } = require('fs');
const send = require('@polka/send-type');

module.exports = function (req, res) {
  if (!req.getHeader('authorization')) {
    // Objects are converted to JSON
    return send(res, 401, { error:'Token required!' });
  }
  // Streams & Buffers are auto-piped
  // Your 'Content-Type' is always used,
  // ~> otherwise 'application/octet-stream'
  let file = createReadStream('massive.mp4');
  send(res, 206, file, { 'Content-Type': 'video/mp4' });
}

API

send(res, code, data, headers)

res

Type: ServerReponse

The outgoing HTTP response.

code

Type: Number
Default: 200

The statusCode for your response.

data

Type: String
Default: ''

The body for your response. Defaults to the statusText for the given statusCode.

See Data Detections for special behaviors.

headers

Type: Object
Default: {}

The headers for your response.

See Data Detections for special behaviors.

Data Detections

The following operations will be performed for the following data types:

Important: If this is too much magic for you, check out @polka/send instead!

Buffers

  • Sets Content-Type to 'application/octet-stream', unless one exists in headers
  • Sets Content-Length

Objects

  • Casts data to string via JSON.stringify
  • Sets Content-Type to 'application/json; charset=utf-8'
  • Sets Content-Length

Streams

  • Sets Content-Type to 'application/octet-stream', unless one exists in headers
  • Pipes data into the res directly

Support

Any issues or questions can be sent to the Polka repo, but please specify that you are using @polka/send-type.

License

MIT © Luke Edwards

FAQs

Package last updated on 07 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc