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

utf8-align-stream

v1.0.1

Published

Streams2 interface to align chunks to UTF-8 character boundaries

Downloads

11

Readme

UTF-8 Align Stream

This is a little module to prevent broken stringification (and possible segmentation faults) when streaming UTF-8 data. It works as a Streams2 transform stream that checks the last 6 bytes of every chunk for incomplete UTF-8 characters. If it finds any, it chops them off and only pushes along the chunk up to that point, saving the remainder to prepend to the next chunk.

Usage

var AlignStream = require('utf8-align-stream');

http.get('http://foo.com/bar', function (res) {
    var aligned = res.pipe(new AlignStream());
    // do whatever with your stream
});

Why?

UTF-8 is a multi-byte encoding. Node streams have no consideration for whether the end of a chunk is a complete UTF-8 sequence; http responses, file reads, and really any stream can potentially break two chunks in the middle of a UTF-8 sequence. If you then convert that chunk to a string, either implicitly or explicitly, or by virtue of how some module you use operates (e.g. html parsers), the result will be incorrect (since the two chunks will be stringified separately, each piece of the UTF-8 sequence will be seen as a separate, invalid, sequence and treated accordingly).

Here's an example:

'use strict';

var AlignStream = require('./index'),
    Parser = require('htmlparser2').Parser;

function getParser(tag) {
    return new Parser({
        ontext: function (text) { console.log(tag + ': ' + text); }
    });
}

var in1 = new AlignStream();
in1.pipe(getParser('Stream 1'));

var in2 = getParser('Stream 2');

var kanji = new Buffer('\u6f22\u5b57');

var part1 = kanji.slice(0, 2),
    part2 = kanji.slice(2, 4),
    part3 = kanji.slice(4, 6);

in1.write(part1);
in2.write(part1);

in1.write(part2);
in2.write(part2);

in1.write(part3);
in2.write(part3);

In the above code, a Buffer is created from two unicode characters, each three bytes long. The buffer is then sliced into three two-byte pieces; none of these are valid on their own. The buffers are then written one at a time (in some cases, these might get combined in memory and the test would be required to be asynchronous, but in the specific case of htmlparser2, this is not necessary). One is written through an instance of AlignStream, and one is written directly to the parser. The output looks like this:

Stream 2: �� Stream 1: 漢 Stream 2: �� Stream 1: 字 Stream 2: ��

You can see that the AlignStream-buffered output is as expected, while the non-buffered output is made up of a bunch of unicode replacement characters, which are inserted in place of invalid UTF-8.