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

hitter

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hitter

A simple library for hit requests

  • 0.0.10
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Hitter

npm version build status Coverage Status npm downloads

A small library with zero dependencies for doing request hits (that is, requests in where you don't expect a response from the server). This library will try different methods, via Image, XHR or sendBeacon to get there, depending on availability and options passed.

Why?

The main use for this is when you want to do hits to an analytics endpoint, where you just want a reliable and fast way to send something and you don't care about the response.

That's why this library will try first using a Image request, and if the payload is too long, then with sendBeacon and XMLHttpRequest (if supported).

Although with XMLHttpRequest and sendBeacon will use a POST method, it will still send the payload as a query param string on plain text.

Installation

npm install hitter

Support

It should work on IE11+. Only for browser.

Example

Since this is thought to be a library to hit usually a unique endpoint, you pass a url to the constructor where you will get the hits. That way you can call many times the instance with different payloads.

var Hitter = require('hitter');
var hit = Hitter('//myapi.com/data');
// or var hit = require('hitter')('//myapi.com/data');

hit(
  {
    payload: {
      data1: 'value1',
      data2: 2,
    },
  },
  function() {
    // Do something after the request is sent
    // Not guaranteed that the request has finished
  }
);

Creating an instance

Hitter(baseUrl)

What you get from the library is a constructor that will take a url as a parameter that will be the base of where to do the calls (can be expanded with paths on the config options later at the instance level).

It will return a function that can be called to do the hits.

Using the instance

hit(config, callback?)

Will take a config object and an optional callback to do something after the hit (you won't get any parameters).

hit(callback?)

Optionally you can also call it with only an optional callback if you don't care about the config options.

Config object

All parameters are optional.

Config.force

Forces a specific method to call on. Possible values are beacon, img and xhr.

Config.path

Takes an array of strings that will as a path to the base url.

Config.payload

The payload to send along the request. It can be either a string or an object where the key/values will be translated to query parameters.

Config.maxImgLength

The max length of the query params to do an Image request. Defaults to 2036.

FAQs

Package last updated on 02 Aug 2019

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