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

streamtparser

v0.1.0

Published

parser object from stream

Downloads

2

Readme

StreamParser

从Stream(Socket file)解出Object 可用于接收自定义协议

使用方式

定义协议

每个类中必须有个constructorFromStream 其返回值就是解出的值

class testPattern {
    constructor(key, val) {
        this.key = key;
        this.val = val;
    }
    static async constructorFromStream(stream) {
        let data = await stream.get(4);
        const key = data.toString();
        data = await stream.get(1);
        data = await stream.get(1);
        const val = data.toString();
        return new testPattern(key, val);
    }
}
class twoTestPattern {
    constructor(test1, test2) {
        this.test1 = test1;
        this.test2 = test2;
    }
    static async constructorFromStream(stream) {
        const test1 = await stream.parser(testPattern);
        const test2 = await stream.parser(testPattern);
        return new twoTestPattern(test1, test2);
    }

}

stream 有

  1. 异步的get(len) 方法获取固定大小的buffer
  2. 异步的parser(obj)方法获取对象 其中 obj 必须有constructorFromStream方法

定义StreamParser


const buffer = Buffer.from("test:atest:a");
const bufferStream = new PassThrough();
bufferStream.end(buffer);

const streamParser = new StreamParser(twoTestPattern, (obj) => {
        expect(obj).to.be.eql(new twoTestPattern(new testPattern('test', 'a'), new testPattern('test', 'a')));
    });

bufferStream.pipe(streamParser);

StreamParser 接受两个参数

  1. pattern 即第一步定义的模板
  2. 回调 当解出一个对象时 触发此回调函数

使用

将 stream pipe 到 streamParser中 详情见 ./test/StreamParser.test.js