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

qs-iconv

v1.0.4

Published

Character encoding for qs (and by extension: request) and querystring.

Downloads

2,869

Readme

ISC License Build Status Coverage Status js-standard-style

qs-iconv

Character encoding for qs (and by extension: request) and querystring.

Why?

Sending requests in Node.js usually encodes url using the utf-8 character code (both with querystring and qs). This means that sending a String like こんにちは! will become %E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF%EF%BC%81. Unfortunately not all servers are implemented in utf8 (particularly in Japan ShiftJIS is still very often seen) and those servers might expect form requests to be sent using another encoding.

Example: If the servers speaks shift_jis then the above string should be sent as: %82%B1%82%F1%82%C9%82%BF%82%CD%81I.

(Note: Browsers also send form data to servers with shift_jis support as shift_jis encoded String)

How?

This library implements the encoder and decoder option for qs after installing it you can use it like:

(encoder)

var qsIconv = require('qs-iconv')
var qs = require('qs')
qs.stringify({test: 'こんにちは!'}, {encoder: qsIconv.encoder('shift_jis')})

(decoder)

var qsIconv = require('qs-iconv')
var qs = require('qs')
qs.parse('%82%B1%82%F1%82%C9%82%BF%82%CD%81I', {decoder: qsIconv.decoder('shift_jis')})

Querystring

Node.js comes with require('querystring') right out of the box. It has fewer features than qs but should work in quite a few cases as well. This library supports querystring as well! Here is how you would encode the above example using querystring.

var qsIconv = require('qs-iconv')
var querystring = require('querystring')
var tmpEscape = querystring.escape
querystring.escape = qsIconv.encoder('shift_jis')
querystring.stringify({test: 'こんにちは!'})
querystring.escape = tmpEscape

Of course you unescape works as well:

var qsIconv = require('qs-iconv')
var querystring = require('querystring')
var tmpUnescape = querystring.unescape
querystring.unescape = qsIconv.decoder('shift_jis')
querystring.parse('%82%B1%82%F1%82%C9%82%BF%82%CD%81I')
querystring.unescape = tmpUnescape

Request

Most likely you will not come in touch with iconv through qs or querystring but rather through request. Here is how you can make a shift_jis POST request using this library:

var qsIconv = require('qs-iconv')
var request = require('request')
// inter-locale.com explains encoding in depth
request('https://encoding-server-jsdxxvgqvo.now.sh/', {
    method: 'POST',
    data: {
        field1: 'こんにちは!'
    },
    qsStringifyOptions: {
        encoder: qsIconv.encoder('shift_jis')
    },
    qsParseOptions: {
        decoder: qsIconv.decoder('shift_jis')
    }
})

(Note: request supports this library from version 2.72.0)

Iconv

This package uses by default iconv-lite which is a JavaScript-only variant of iconv. But since its stripped down to the bone and might be missing some tricky edge cases this library also allows to take the iconv instead. For it to work you need to have iconv installed additionally to qs-iconv!

var qsIconv = require('qs-iconv')
qsIconv.decoder('shift_jis', true)
qsIconv.encoder('shift_jis', true)

Performance Remark

Both iconv and iconv-lite are not optimized for de-/encoding single charaters but rather a string. Unless we have good support for character encoding this library will add a reasonable performance toll.

License

ISC

(Fixes and other contributions welcome)