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

node-smart-buffer

v3.0.2

Published

ring buffer & packet compile

Downloads

42

Readme

环形缓冲区 & 数据包序列化/反序列化

安装

npm install node-smart-buffer

InputBuffer/OutputBuffer接口

smartbuffer.InputBuffer.length
smartbuffer.InputBuffer.endian = smartbuffer.LE | smartbuffer.BE;
smartbuffer.InputBuffer.clear()
smartbuffer.InputBuffer.reset()
smartbuffer.InputBuffer.skip( cursor )
smartbuffer.InputBuffer.skipTo( cursor )
smartbuffer.InputBuffer.remaining()
smartbuffer.InputBuffer.read( length )
smartbuffer.InputBuffer.add( arrayBuffer )
smartbuffer.InputBuffer.discard( length )
smartbuffer.InputBuffer.int8()/uint8()
smartbuffer.InputBuffer.int16()/uint16()
smartbuffer.InputBuffer.int32()/uint32()
smartbuffer.InputBuffer.int64()/uint64()(返回buffer)
smartbuffer.InputBuffer.float()
smartbuffer.InputBuffer.double()

smartbuffer.OutputBuffer.length
smartbuffer.OutputBuffer.endian = smartbuffer.LE | smartbuffer.BE;
smartbuffer.OutputBuffer.clear()
smartbuffer.OutputBuffer.skip( cursor )
smartbuffer.OutputBuffer.skipTo( cursor )
smartbuffer.OutputBuffer.toBuffer()
smartbuffer.OutputBuffer.write( arrayBuffer )
smartbuffer.OutputBuffer.int8( number )/uint8( number )
smartbuffer.OutputBuffer.int16( number )/uint16( number )
smartbuffer.OutputBuffer.int32( number )/uint32( number )
smartbuffer.OutputBuffer.int64( buffer )/uint64( buffer )
smartbuffer.OutputBuffer.float( number )
smartbuffer.OutputBuffer.double( number )

示例


// 引用
const smartbuffer = require( 'node-smart-buffer' );

// 编译包
const packet = smartbuffer.compile( {
    utf8: {
        encode: function( buffer, data ) {
            data = Buffer.from( data );
            buffer.uint16( data.length );
            buffer.write( data );
        },
        decode: function( buffer ) {
            return buffer.read( buffer.uint16() ).toString();
        },
    },
    DEMO: [
        '.attributes',                // 自动命名 _0
        {                             // 自动命名 _1
            index: 100,               // 设置索引
            a: 1,
            b: 2,
            c: 3,
        },
        'name:utf8',                  // 自动命名 _2
        'data:uint8[uint16]',         // 自动命名 _3
        ':DEMO2',                     // 自动命名 _4
        {
            encode: function( buffer, data ) {
                // 仅当序列化时调用
                console.log( 'DEMO.encode' );
            },
            decode: function( buffer, data ) {
                // 仅当反序列化时调用
                console.log( 'DEMO.decode' );
            },
        },
        `if ( data.name === "Job" ) {`,     // 直接使用
            'a:uint32',
        `}`,
        'b:uint8[5]',
        'c:uint8[.a]',
    ],
    DEMO2: [
        'value:utf8',
    ],
} );

// 序列化数据
const output = new smartbuffer.OutputBuffer();
packet.encode.DEMO( output, {
    name: 'Job',
    data: [ 1, 2, 3, 4 ],
    _4: {
        value: 'DEMO2',
    },
    a: 2,
} );
console.log( output.toBuffer() );
/*
输出:
DEMO.encode
<Buffer 00 03 4a 6f 62 00 04 01 02 03 04 00 05 44 45 4d 4f 32 00 00 00 02>
*/

// 反序列化数据
const input = new smartbuffer.InputBuffer();
input.add( output.toBuffer() );
console.log( packet.decode.DEMO( input ) );
/*
输出:
DEMO.decode
{ name: 'Job',
  data: [ 1, 2, 3, 4 ],
  _4: { value: 'DEMO2' },
  a: 2 }
*/

// 索引
console.log( packet.indexed );
// { '100': 'DEMO' }

// 自定义属性
console.log( packet.storage );
// { DEMO: { index: 100, a: 1, b: 2, c: 3 }, DEMO2: {} }