node-opcua-packet-assembler
Advanced tools
Comparing version 2.66.0 to 2.69.0
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
import { EventEmitter } from "events"; | ||
@@ -17,12 +18,29 @@ /*** | ||
} | ||
export declare type ReadMessageFuncType = (data: Buffer) => PacketInfo; | ||
export declare type ReadChunkFuncType = (data: Buffer) => PacketInfo; | ||
export interface PacketAssemblerOptions { | ||
readMessageFunc: ReadMessageFuncType; | ||
readChunkFunc: ReadChunkFuncType; | ||
minimumSizeInBytes: number; | ||
maxChunkSize: number; | ||
} | ||
export declare enum PacketAssemblerErrorCode { | ||
ChunkSizeExceeded = 1, | ||
ChunkTooSmall = 2 | ||
} | ||
export interface PacketAssembler { | ||
on(eventName: "startChunk", eventHandler: (packetInfo: PacketInfo, partial: Buffer) => void): this; | ||
on(eventName: "chunk", eventHandler: (chunk: Buffer) => void): this; | ||
on(eventName: "error", eventHandler: (err: Error, errCode: PacketAssemblerErrorCode) => void): this; | ||
} | ||
/** | ||
* this class is used to assemble partial data from the tranport layer | ||
* into message chunks | ||
*/ | ||
export declare class PacketAssembler extends EventEmitter { | ||
static defaultMaxChunkCount: number; | ||
static defaultMaxMessageSize: number; | ||
private readonly _stack; | ||
private expectedLength; | ||
private currentLength; | ||
private readonly readMessageFunc; | ||
private maxChunkSize; | ||
private readonly readChunkFunc; | ||
private readonly minimumSizeInBytes; | ||
@@ -29,0 +47,0 @@ private packetInfo?; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PacketAssembler = void 0; | ||
exports.PacketAssembler = exports.PacketAssemblerErrorCode = void 0; | ||
const events_1 = require("events"); | ||
const node_opcua_assert_1 = require("node-opcua-assert"); | ||
const node_opcua_debug_1 = require("node-opcua-debug"); | ||
const doDebug = false; | ||
const warningLog = (0, node_opcua_debug_1.make_warningLog)("PacketAssembler"); | ||
var PacketAssemblerErrorCode; | ||
(function (PacketAssemblerErrorCode) { | ||
PacketAssemblerErrorCode[PacketAssemblerErrorCode["ChunkSizeExceeded"] = 1] = "ChunkSizeExceeded"; | ||
PacketAssemblerErrorCode[PacketAssemblerErrorCode["ChunkTooSmall"] = 2] = "ChunkTooSmall"; | ||
})(PacketAssemblerErrorCode = exports.PacketAssemblerErrorCode || (exports.PacketAssemblerErrorCode = {})); | ||
/** | ||
* this class is used to assemble partial data from the tranport layer | ||
* into message chunks | ||
*/ | ||
class PacketAssembler extends events_1.EventEmitter { | ||
@@ -13,5 +24,9 @@ constructor(options) { | ||
this.currentLength = 0; | ||
this.readMessageFunc = options.readMessageFunc; | ||
this.readChunkFunc = options.readChunkFunc; | ||
this.minimumSizeInBytes = options.minimumSizeInBytes || 8; | ||
(0, node_opcua_assert_1.assert)(typeof this.readMessageFunc === "function", "packet assembler requires a readMessageFunc"); | ||
(0, node_opcua_assert_1.assert)(typeof this.readChunkFunc === "function", "packet assembler requires a readChunkFunc"); | ||
// istanbul ignore next | ||
(0, node_opcua_assert_1.assert)(options.maxChunkSize === undefined || options.maxChunkSize !== 0); | ||
this.maxChunkSize = options.maxChunkSize || PacketAssembler.defaultMaxMessageSize; | ||
(0, node_opcua_assert_1.assert)(this.maxChunkSize >= this.minimumSizeInBytes); | ||
} | ||
@@ -29,7 +44,16 @@ feed(data) { | ||
this.packetInfo = this._readPacketInfo(data); | ||
this.expectedLength = this.packetInfo.length; | ||
(0, node_opcua_assert_1.assert)(this.currentLength === 0); | ||
(0, node_opcua_assert_1.assert)(this.expectedLength > 0); | ||
if (this.packetInfo.length < this.minimumSizeInBytes) { | ||
this.emit("error", new Error("chunk is too small "), PacketAssemblerErrorCode.ChunkTooSmall); | ||
return; | ||
} | ||
if (this.packetInfo.length > this.maxChunkSize) { | ||
const message = `maximum chunk size exceeded (maxChunkSize=${this.maxChunkSize} current chunk size = ${this.packetInfo.length})`; | ||
warningLog(message); | ||
this.emit("error", new Error(message), PacketAssemblerErrorCode.ChunkSizeExceeded); | ||
return; | ||
} | ||
// we can now emit an event to signal the start of a new packet | ||
this.emit("newMessage", this.packetInfo, data); | ||
this.emit("startChunk", this.packetInfo, data); | ||
this.expectedLength = this.packetInfo.length; | ||
} | ||
@@ -53,3 +77,3 @@ if (this.expectedLength === 0 || this.currentLength + data.length < this.expectedLength) { | ||
this.expectedLength = 0; | ||
this.emit("message", messageChunk); | ||
this.emit("chunk", messageChunk); | ||
} | ||
@@ -71,3 +95,3 @@ else { | ||
_readPacketInfo(data) { | ||
return this.readMessageFunc(data); | ||
return this.readChunkFunc(data); | ||
} | ||
@@ -90,2 +114,4 @@ _buildData(data) { | ||
exports.PacketAssembler = PacketAssembler; | ||
PacketAssembler.defaultMaxChunkCount = 777; | ||
PacketAssembler.defaultMaxMessageSize = 1024 * 64 - 7; | ||
//# sourceMappingURL=packet_assembler.js.map |
{ | ||
"name": "node-opcua-packet-assembler", | ||
"version": "2.66.0", | ||
"version": "2.69.0", | ||
"description": "pure nodejs OPCUA SDK - module -packet-assembler", | ||
@@ -15,3 +15,4 @@ "main": "./dist/index.js", | ||
"dependencies": { | ||
"node-opcua-assert": "2.66.0" | ||
"node-opcua-assert": "2.66.0", | ||
"node-opcua-debug": "2.69.0" | ||
}, | ||
@@ -34,3 +35,3 @@ "devDependencies": { | ||
"homepage": "http://node-opcua.github.io/", | ||
"gitHead": "97f47e2e242a1fd737495fd64cb65e8fb7a9964b" | ||
"gitHead": "6c88d05e8c82ce4bc9c8af9f0a8eb6136f31d2ce" | ||
} |
import { EventEmitter } from "events"; | ||
import { assert } from "node-opcua-assert"; | ||
import { make_warningLog } from "node-opcua-debug"; | ||
const doDebug = false; | ||
const warningLog = make_warningLog("PacketAssembler"); | ||
@@ -23,16 +25,35 @@ /*** | ||
export type ReadMessageFuncType = (data: Buffer) => PacketInfo; | ||
export type ReadChunkFuncType = (data: Buffer) => PacketInfo; | ||
export interface PacketAssemblerOptions { | ||
readMessageFunc: ReadMessageFuncType; | ||
// the minimum number of bytes that need to be received before the readMessageFunc can be called | ||
readChunkFunc: ReadChunkFuncType; | ||
// the minimum number of bytes that need to be received before the readChunkFunc can be called | ||
minimumSizeInBytes: number; | ||
maxChunkSize: number; | ||
} | ||
export enum PacketAssemblerErrorCode { | ||
ChunkSizeExceeded = 1, | ||
ChunkTooSmall = 2 | ||
} | ||
export interface PacketAssembler { | ||
on(eventName: "startChunk", eventHandler: (packetInfo: PacketInfo, partial: Buffer) => void): this; | ||
on(eventName: "chunk", eventHandler: (chunk: Buffer) => void): this; | ||
on(eventName: "error", eventHandler: (err: Error, errCode: PacketAssemblerErrorCode) => void): this; | ||
} | ||
/** | ||
* this class is used to assemble partial data from the tranport layer | ||
* into message chunks | ||
*/ | ||
export class PacketAssembler extends EventEmitter { | ||
public static defaultMaxChunkCount = 777; | ||
public static defaultMaxMessageSize = 1024 * 64 - 7; | ||
private readonly _stack: Buffer[]; | ||
private expectedLength: number; | ||
private currentLength: number; | ||
private readonly readMessageFunc: ReadMessageFuncType; | ||
private maxChunkSize: number; | ||
private readonly readChunkFunc: ReadChunkFuncType; | ||
private readonly minimumSizeInBytes: number; | ||
@@ -46,5 +67,11 @@ private packetInfo?: PacketInfo; | ||
this.currentLength = 0; | ||
this.readMessageFunc = options.readMessageFunc; | ||
this.readChunkFunc = options.readChunkFunc; | ||
this.minimumSizeInBytes = options.minimumSizeInBytes || 8; | ||
assert(typeof this.readMessageFunc === "function", "packet assembler requires a readMessageFunc"); | ||
assert(typeof this.readChunkFunc === "function", "packet assembler requires a readChunkFunc"); | ||
// istanbul ignore next | ||
assert(options.maxChunkSize === undefined || options.maxChunkSize !== 0); | ||
this.maxChunkSize = options.maxChunkSize || PacketAssembler.defaultMaxMessageSize; | ||
assert(this.maxChunkSize >= this.minimumSizeInBytes); | ||
} | ||
@@ -65,8 +92,18 @@ | ||
this.packetInfo = this._readPacketInfo(data); | ||
this.expectedLength = this.packetInfo.length; | ||
assert(this.currentLength === 0); | ||
assert(this.expectedLength > 0); | ||
if (this.packetInfo.length < this.minimumSizeInBytes) { | ||
this.emit("error", new Error("chunk is too small "), PacketAssemblerErrorCode.ChunkTooSmall); | ||
return; | ||
} | ||
if (this.packetInfo.length > this.maxChunkSize) { | ||
const message = `maximum chunk size exceeded (maxChunkSize=${this.maxChunkSize} current chunk size = ${this.packetInfo.length})`; | ||
warningLog(message); | ||
this.emit("error", new Error(message), PacketAssemblerErrorCode.ChunkSizeExceeded); | ||
return; | ||
} | ||
// we can now emit an event to signal the start of a new packet | ||
this.emit("newMessage", this.packetInfo, data); | ||
this.emit("startChunk", this.packetInfo, data); | ||
this.expectedLength = this.packetInfo.length; | ||
} | ||
@@ -93,3 +130,3 @@ | ||
this.emit("message", messageChunk); | ||
this.emit("chunk", messageChunk); | ||
} else { | ||
@@ -111,6 +148,6 @@ // there is more data in this chunk than expected... | ||
private _readPacketInfo(data: Buffer) { | ||
return this.readMessageFunc(data); | ||
return this.readChunkFunc(data); | ||
} | ||
private _buildData(data: Buffer) { | ||
private _buildData(data: Buffer): Buffer { | ||
if (data && this._stack.length === 0) { | ||
@@ -117,0 +154,0 @@ return data; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19609
318
2
+ Addednode-opcua-debug@2.69.0
+ Addedhexy@0.3.4(transitive)
+ Addednode-opcua-buffer-utils@2.69.0(transitive)
+ Addednode-opcua-debug@2.69.0(transitive)