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 🙏

© 2026 – Pkg Stats / Ryan Hefner

node-opcua-packet-assembler

v2.162.0

Published

pure nodejs OPCUA SDK - module packet-assembler

Readme

node-opcua-packet-assembler

A high-performance packet assembler for reassembling fragmented data from transport layers into complete message chunks. Features zero-copy optimization for maximum performance.

Installation

npm install node-opcua-packet-assembler

Quick Start

import { PacketAssembler } from "node-opcua-packet-assembler";

// Create assembler
const assembler = new PacketAssembler({
    readChunkFunc: (data) => ({
        length: data.readUInt32LE(4),
        messageHeader: {
            msgType: data.toString("ascii", 0, 4),
            isFinal: "F",
            length: data.readUInt32LE(4)
        },
        extra: ""
    }),
    minimumSizeInBytes: 8,
    maxChunkSize: 65536
});

// Listen for complete chunks
assembler.on("chunk", (chunk) => {
    console.log("Complete chunk:", chunk.length, "bytes");
    processMessage(chunk);
});

// Feed data from transport
socket.on("data", (data) => assembler.feed(data));

Key Features

Zero-Copy Performance

  • Single-chunk messages: Returns buffer views without copying (fast!)
  • Multi-chunk messages: Concatenates fragments safely with Buffer.concat()
  • Optimized for the common case where complete messages arrive in one buffer

Event-Driven API

// Track chunk assembly progress
assembler.on("startChunk", (packetInfo, partial) => {
    console.log(`Starting chunk: ${packetInfo.length} bytes`);
});

// Process complete chunks
assembler.on("chunk", (chunk) => {
    handleMessage(chunk);
});

// Handle errors
assembler.on("error", (error, errorCode) => {
    console.error("Assembly error:", error.message);
});

Important: Buffer Lifetime

⚠️ When using zero-copy buffers, YOU are responsible for buffer lifetime management.

✅ Safe Usage

assembler.on("chunk", (chunk) => {
    // Option 1: Process immediately
    const value = chunk.readUInt32LE(0);
    console.log("Value:", value);

    // Option 2: Make a copy if storing
    const copy = Buffer.from(chunk);
    messageQueue.push(copy);
});

❌ Unsafe Usage

const storedChunks = [];

assembler.on("chunk", (chunk) => {
    // UNSAFE! Transport may reuse this buffer
    storedChunks.push(chunk);
});

Rule of thumb: If you store buffers beyond immediate processing or pass them to async handlers, create a copy with Buffer.from(chunk).

API Overview

For complete API documentation with TypeScript types and detailed examples, see the source code JSDoc comments.

Constructor

new PacketAssembler(options: PacketAssemblerOptions)

| Option | Type | Description | | -------------------- | ------------------------------ | ----------------------- | | readChunkFunc | (data: Buffer) => PacketInfo | Extract packet metadata | | minimumSizeInBytes | number | Minimum header size | | maxChunkSize | number | Maximum chunk size |

Methods

  • feed(data: Buffer): Feed incoming data to the assembler

Events

  • "startChunk": (packetInfo, partial) => void - New chunk detected
  • "chunk": (chunk: Buffer) => void - Complete chunk assembled
  • "error": (error, errorCode) => void - Assembly error occurred

Common Patterns

TCP Socket Integration

import net from "net";

const server = net.createServer((socket) => {
    const assembler = new PacketAssembler({
        readChunkFunc: readHeader,
        minimumSizeInBytes: 8,
        maxChunkSize: 65536
    });

    assembler.on("chunk", handleMessage);
    assembler.on("error", (err) => socket.destroy());

    socket.on("data", (data) => assembler.feed(data));
});

With Progress Tracking

assembler.on("startChunk", (packetInfo) => {
    console.log(`📦 Expecting ${packetInfo.length} bytes`);
});

assembler.on("chunk", (chunk) => {
    console.log(`✅ Received ${chunk.length} bytes`);
});

Error Handling

import { PacketAssemblerErrorCode } from "node-opcua-packet-assembler";

assembler.on("error", (error, errorCode) => {
    if (errorCode === PacketAssemblerErrorCode.ChunkSizeExceeded) {
        console.error("Chunk too large:", error.message);
    }
});

Performance Tips

  1. Avoid unnecessary copies: The assembler already optimizes for zero-copy when possible
  2. Set appropriate limits: Configure maxChunkSize based on your protocol needs
  3. Process immediately: When safe, process chunks in the event handler for best performance
  4. Only copy when needed: Create copies only when storing or passing to async handlers

TypeScript Support

Full TypeScript definitions included. All interfaces, types, and methods are fully documented with JSDoc comments in the source code.

Testing

npm test

License

MIT

Related Packages

  • node-opcua - Main package
  • node-opcua-transport - Transport layer
  • node-opcua-chunkmanager - Message chunk management