Socket
Socket
Sign inDemoInstall

charset

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

charset

Get the content charset from header and html content-type.


Version published
Weekly downloads
1M
increased by4.53%
Maintainers
1
Weekly downloads
 
Created
Source

charset

NPM version build status Test coverage David deps npm download

logo

Get the content charset from header and html content-type.

Install

$ npm install charset --save

Usage

Detect charset from http client response and content

var charset = require('charset');
var http = require('http');

http.get('http://nodejs.org', function (res) {
  res.on('data', function (chunk) {
    console.log(charset(res.headers, chunk));
    // or `console.log(charset(res, chunk));`
    res.destroy();
  });
});

Stdout will should log: utf8 .

Detect from String

charset(res.headers['content-type']);

Detect combine with jschardet

As you know, charset only detect from http response headers and html content-type meta tag. You can combine with jschardet to help you detect the finally charset.

This example codes come from stackoverflow#12326688:

var request = require('request');
var charset = require('charset');
var jschardet = require('jschardet');

request({
  url: 'http://www.example.com',
  encoding: null
}, function (err, res, body) {
  if (err) {
    throw err;
  }
  enc = charset(res.headers, body);
  enc = enc || jschardet.detect(body).encoding.toLowerCase();
  console.log(enc);
});

License

MIT

Keywords

FAQs

Package last updated on 07 Sep 2017

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