Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ndn/lp

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ndn/lp - npm Package Compare versions

Comparing version 0.0.20210203 to 0.0.20210930

0

lib/an.d.ts

@@ -0,0 +0,0 @@ export declare const TT: {

23

lib/fragmenter_browser.js

@@ -5,7 +5,4 @@ import { Encoder } from "@ndn/tlv";

constructor() {
const nibbles = [];
for (let i = 0; i < 16; ++i) {
nibbles.push(Math.floor(Math.random() * 0x100)).toString(16);
}
this.current = BigInt(`0x${nibbles.join("")}`);
this.current = (BigInt(Math.floor(Math.random() * 0x100000000)) << 32n) |
BigInt(Math.floor(Math.random() * 0x100000000));
}

@@ -25,2 +22,3 @@ next() {

0;
/** NDNLPv2 fragmenter. */
export class Fragmenter {

@@ -32,6 +30,9 @@ constructor(mtu) {

}
/**
* Fragment a packet.
* @returns a sequence of fragment, or empty array if fragmentation fails.
*/
fragment(full) {
var _a, _b;
const sizeofL3Headers = Encoder.encode(full.encodeL3Headers()).length;
const sizeofPayload = (_b = (_a = full.payload) === null || _a === void 0 ? void 0 : _a.byteLength) !== null && _b !== void 0 ? _b : 0;
const sizeofPayload = full.payload?.byteLength ?? 0;
const sizeofFirstFragment = Math.min(sizeofPayload, this.fragmentRoom - sizeofL3Headers);

@@ -48,3 +49,3 @@ if (sizeofFirstFragment === sizeofPayload) { // no fragmentation necessary

first.fragSeqNum = this.seqNumGen.next();
first.payload = full.payload.slice(0, sizeofFirstFragment);
first.payload = full.payload.subarray(0, sizeofFirstFragment);
fragments.push(first);

@@ -55,8 +56,10 @@ for (let offset = sizeofFirstFragment; offset < sizeofPayload; offset += this.fragmentRoom) {

fragment.fragIndex = fragments.length;
fragment.payload = full.payload.slice(offset, offset + this.fragmentRoom);
fragment.payload = full.payload.subarray(offset, offset + this.fragmentRoom);
fragments.push(fragment);
}
fragments.forEach((fragment) => fragment.fragCount = fragments.length);
for (const fragment of fragments) {
fragment.fragCount = fragments.length;
}
return fragments;
}
}

@@ -5,7 +5,4 @@ import { Encoder } from "@ndn/tlv";

constructor() {
const nibbles = [];
for (let i = 0; i < 16; ++i) {
nibbles.push(Math.floor(Math.random() * 0x100)).toString(16);
}
this.current = BigInt(`0x${nibbles.join("")}`);
this.current = (BigInt(Math.floor(Math.random() * 0x100000000)) << 32n) |
BigInt(Math.floor(Math.random() * 0x100000000));
}

@@ -25,2 +22,3 @@ next() {

0;
/** NDNLPv2 fragmenter. */
export class Fragmenter {

@@ -32,6 +30,9 @@ constructor(mtu) {

}
/**
* Fragment a packet.
* @returns a sequence of fragment, or empty array if fragmentation fails.
*/
fragment(full) {
var _a, _b;
const sizeofL3Headers = Encoder.encode(full.encodeL3Headers()).length;
const sizeofPayload = (_b = (_a = full.payload) === null || _a === void 0 ? void 0 : _a.byteLength) !== null && _b !== void 0 ? _b : 0;
const sizeofPayload = full.payload?.byteLength ?? 0;
const sizeofFirstFragment = Math.min(sizeofPayload, this.fragmentRoom - sizeofL3Headers);

@@ -48,3 +49,3 @@ if (sizeofFirstFragment === sizeofPayload) { // no fragmentation necessary

first.fragSeqNum = this.seqNumGen.next();
first.payload = full.payload.slice(0, sizeofFirstFragment);
first.payload = full.payload.subarray(0, sizeofFirstFragment);
fragments.push(first);

@@ -55,8 +56,10 @@ for (let offset = sizeofFirstFragment; offset < sizeofPayload; offset += this.fragmentRoom) {

fragment.fragIndex = fragments.length;
fragment.payload = full.payload.slice(offset, offset + this.fragmentRoom);
fragment.payload = full.payload.subarray(offset, offset + this.fragmentRoom);
fragments.push(fragment);
}
fragments.forEach((fragment) => fragment.fragCount = fragments.length);
for (const fragment of fragments) {
fragment.fragCount = fragments.length;
}
return fragments;
}
}
import { LpPacket } from "./packet";
/** NDNLPv2 fragmenter. */
export declare class Fragmenter {
readonly mtu: number;
constructor(mtu: number);
private seqNumGen;
private fragmentRoom;
private readonly seqNumGen;
private readonly fragmentRoom;
/**
* Fragment a packet.
* @returns a sequence of fragment, or empty array if fragmentation fails.
*/
fragment(full: LpPacket): LpPacket[];
}
export * from "./an_browser.js";
export * from "./fragmenter_browser.js";
export * from "./packet_browser.js";
export * from "./pit-token_browser.js";
export * from "./reassembler_browser.js";
export * from "./service_browser.js";
export * from "./an_node.js";
export * from "./fragmenter_node.js";
export * from "./packet_node.js";
export * from "./pit-token_node.js";
export * from "./reassembler_node.js";
export * from "./service_node.js";
export * from "./an";
export * from "./fragmenter";
export * from "./packet";
export * from "./pit-token";
export * from "./reassembler";
export * from "./service";

@@ -9,3 +9,3 @@ import { NackHeader } from "@ndn/packet";

.setIsCritical(isCritical)
.add(TT.LpSeqNum, (t, { value }) => t.fragSeqNum = Encoder.asDataView(value).getBigUint64(0))
.add(TT.LpSeqNum, (t, { value }) => t.fragSeqNum = Encoder.getBigUint64(Encoder.asDataView(value), 0))
.add(TT.FragIndex, (t, { nni }) => t.fragIndex = nni)

@@ -26,3 +26,3 @@ .add(TT.FragCount, (t, { nni }) => t.fragCount = nni)

encodeTo(encoder) {
encoder.prependTlv(TT.LpPacket, typeof this.fragSeqNum === "undefined" ? undefined : [TT.LpSeqNum, NNI(this.fragSeqNum, { len: 8 })], this.fragIndex > 0 ? [TT.FragIndex, NNI(this.fragIndex)] : undefined, this.fragCount > 1 ? [TT.FragCount, NNI(this.fragCount)] : undefined, ...this.encodeL3Headers(), [TT.LpPayload, Encoder.OmitEmpty, this.payload]);
encoder.prependTlv(TT.LpPacket, this.fragSeqNum === undefined ? undefined : [TT.LpSeqNum, NNI(this.fragSeqNum, { len: 8 })], this.fragIndex > 0 ? [TT.FragIndex, NNI(this.fragIndex)] : undefined, this.fragCount > 1 ? [TT.FragCount, NNI(this.fragCount)] : undefined, ...this.encodeL3Headers(), [TT.LpPayload, Encoder.OmitEmpty, this.payload]);
}

@@ -29,0 +29,0 @@ encodeL3Headers() {

@@ -9,3 +9,3 @@ import { NackHeader } from "@ndn/packet";

.setIsCritical(isCritical)
.add(TT.LpSeqNum, (t, { value }) => t.fragSeqNum = Encoder.asDataView(value).getBigUint64(0))
.add(TT.LpSeqNum, (t, { value }) => t.fragSeqNum = Encoder.getBigUint64(Encoder.asDataView(value), 0))
.add(TT.FragIndex, (t, { nni }) => t.fragIndex = nni)

@@ -26,3 +26,3 @@ .add(TT.FragCount, (t, { nni }) => t.fragCount = nni)

encodeTo(encoder) {
encoder.prependTlv(TT.LpPacket, typeof this.fragSeqNum === "undefined" ? undefined : [TT.LpSeqNum, NNI(this.fragSeqNum, { len: 8 })], this.fragIndex > 0 ? [TT.FragIndex, NNI(this.fragIndex)] : undefined, this.fragCount > 1 ? [TT.FragCount, NNI(this.fragCount)] : undefined, ...this.encodeL3Headers(), [TT.LpPayload, Encoder.OmitEmpty, this.payload]);
encoder.prependTlv(TT.LpPacket, this.fragSeqNum === undefined ? undefined : [TT.LpSeqNum, NNI(this.fragSeqNum, { len: 8 })], this.fragIndex > 0 ? [TT.FragIndex, NNI(this.fragIndex)] : undefined, this.fragCount > 1 ? [TT.FragCount, NNI(this.fragCount)] : undefined, ...this.encodeL3Headers(), [TT.LpPayload, Encoder.OmitEmpty, this.payload]);
}

@@ -29,0 +29,0 @@ encodeL3Headers() {

import { NackHeader } from "@ndn/packet";
import { Decoder, Encodable, Encoder } from "@ndn/tlv";
import type { PitToken } from "./pit-token";
/** NDNLPv2 packet. */

@@ -10,3 +9,3 @@ export declare class LpPacket {

fragCount: number;
pitToken?: PitToken;
pitToken?: Uint8Array;
nack?: NackHeader;

@@ -13,0 +12,0 @@ payload?: Uint8Array;

@@ -11,3 +11,3 @@ import { LpPacket } from "./packet_browser.js";

if (this.accepted === 0) { // first
this.buffer = new Array(fragment.fragCount);
this.buffer.length = fragment.fragCount;
this.acceptOne(fragment);

@@ -29,6 +29,5 @@ return undefined;

acceptOne(fragment) {
var _a, _b;
this.buffer[fragment.fragIndex] = fragment;
++this.accepted;
this.payloadLength += (_b = (_a = fragment.payload) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
this.payloadLength += fragment.payload?.length ?? 0;
}

@@ -50,2 +49,3 @@ reassemble() {

}
/** NDNLPv2 reassembler. */
export class Reassembler {

@@ -56,2 +56,6 @@ constructor(capacity) {

}
/**
* Process a fragment.
* @returns fully reassembled packet, or undefined if packet is not yet complete.
*/
accept(fragment) {

@@ -61,7 +65,7 @@ if (fragment.fragCount === 1) { // not fragmented

}
if (typeof fragment.fragSeqNum === "undefined" ||
if (fragment.fragSeqNum === undefined ||
fragment.fragIndex >= fragment.fragCount) { // bad fragment
return undefined;
}
const seqNumBase = fragment.fragSeqNum - BigInt(fragment.fragIndex);
const seqNumBase = BigInt.asUintN(64, fragment.fragSeqNum - BigInt(fragment.fragIndex));
const partial = this.getPartial(seqNumBase);

@@ -87,3 +91,3 @@ const result = partial.accept(fragment);

this.partials.set(partial.seqNumBase, partial);
if (this.partials.size >= this.capacity) { // exceed capacity, delete oldest
if (this.partials.size > this.capacity) { // exceed capacity, delete oldest
// eslint-disable-next-line no-unreachable-loop

@@ -90,0 +94,0 @@ for (const key of this.partials.keys()) {

@@ -11,3 +11,3 @@ import { LpPacket } from "./packet_node.js";

if (this.accepted === 0) { // first
this.buffer = new Array(fragment.fragCount);
this.buffer.length = fragment.fragCount;
this.acceptOne(fragment);

@@ -29,6 +29,5 @@ return undefined;

acceptOne(fragment) {
var _a, _b;
this.buffer[fragment.fragIndex] = fragment;
++this.accepted;
this.payloadLength += (_b = (_a = fragment.payload) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
this.payloadLength += fragment.payload?.length ?? 0;
}

@@ -50,2 +49,3 @@ reassemble() {

}
/** NDNLPv2 reassembler. */
export class Reassembler {

@@ -56,2 +56,6 @@ constructor(capacity) {

}
/**
* Process a fragment.
* @returns fully reassembled packet, or undefined if packet is not yet complete.
*/
accept(fragment) {

@@ -61,7 +65,7 @@ if (fragment.fragCount === 1) { // not fragmented

}
if (typeof fragment.fragSeqNum === "undefined" ||
if (fragment.fragSeqNum === undefined ||
fragment.fragIndex >= fragment.fragCount) { // bad fragment
return undefined;
}
const seqNumBase = fragment.fragSeqNum - BigInt(fragment.fragIndex);
const seqNumBase = BigInt.asUintN(64, fragment.fragSeqNum - BigInt(fragment.fragIndex));
const partial = this.getPartial(seqNumBase);

@@ -87,3 +91,3 @@ const result = partial.accept(fragment);

this.partials.set(partial.seqNumBase, partial);
if (this.partials.size >= this.capacity) { // exceed capacity, delete oldest
if (this.partials.size > this.capacity) { // exceed capacity, delete oldest
// eslint-disable-next-line no-unreachable-loop

@@ -90,0 +94,0 @@ for (const key of this.partials.keys()) {

import { LpPacket } from "./packet";
/** NDNLPv2 reassembler. */
export declare class Reassembler {

@@ -6,2 +7,6 @@ private readonly capacity;

private readonly partials;
/**
* Process a fragment.
* @returns fully reassembled packet, or undefined if packet is not yet complete.
*/
accept(fragment: LpPacket): LpPacket | undefined;

@@ -8,0 +13,0 @@ private getPartial;

@@ -5,2 +5,3 @@ import { __importDefault, __importStar } from "tslib";

import _cjsDefaultImport0 from "it-keepalive"; const itKeepAlive = __importDefault(_cjsDefaultImport0).default;
import _cjsDefaultImport1 from "minimalistic-assert"; const assert = __importDefault(_cjsDefaultImport1).default;
import { TT } from "./an_browser.js";

@@ -10,2 +11,11 @@ import { Fragmenter } from "./fragmenter_browser.js";

import { Reassembler } from "./reassembler_browser.js";
/**
* Map and flatten, but only do it once.
* This differs from flatMap from streaming-iterables that recursively flattens the result.
*/
async function* flatMap1(f, iterable) {
for await (const item of iterable) {
yield* f(item);
}
}
const IDLE = Encoder.encode(new LpPacket());

@@ -15,18 +25,9 @@ export class LpService {

this.mtu = Infinity;
this.rx = (iterable) => {
return this.rx_(iterable);
};
this.tx = (iterable) => {
let iterable1 = iterable;
if (this.keepAlive > 0) {
iterable1 = itKeepAlive(() => false, { timeout: this.keepAlive })(iterable);
}
return this.tx_(iterable1);
};
if (keepAlive === false || keepAlive <= 0) {
this.keepAlive = -1;
this.rx = (iterable) => flatMap1((tlv) => this.decode(tlv), iterable);
this.tx = (iterable) => flatMap1((pkt) => this.encode(pkt), this.keepAlive ?
itKeepAlive(() => false, { timeout: this.keepAlive })(iterable) :
iterable);
if (Number.isFinite(keepAlive) && keepAlive > 0) {
this.keepAlive = Math.ceil(keepAlive);
}
else {
this.keepAlive = keepAlive;
}
if (Number.isFinite(mtu)) {

@@ -38,16 +39,12 @@ this.mtu = mtu;

}
async *rx_(iterable) {
for await (const tlv of iterable) {
yield* this.decode(tlv);
}
}
*decode(tlv) {
*decode(dtlv) {
const { type, decoder, tlv } = dtlv;
try {
const { type, decoder } = tlv;
if (type !== TT.LpPacket) {
return yield this.decodeL3(tlv);
yield this.decodeL3(dtlv);
return;
}
const fragment = decoder.decode(LpPacket);
const lpp = this.reassembler.accept(fragment);
if (!lpp || !lpp.payload) {
if (!lpp?.payload) {
return;

@@ -68,3 +65,3 @@ }

catch (err) {
yield new LpService.RxError(err, tlv.tlv);
yield new LpService.RxError(err, tlv);
}

@@ -82,38 +79,23 @@ }

}
async *tx_(iterable) {
for await (const pkt of iterable) {
if (pkt === false) {
yield IDLE;
}
else {
yield* this.encode(pkt);
}
*encode(pkt) {
if (pkt === false) {
yield IDLE;
return;
}
}
*encode({ l3, token }) {
let lpp;
const { l3, token } = pkt;
const lpp = new LpPacket();
lpp.pitToken = token;
try {
switch (true) {
case l3 instanceof Interest:
case l3 instanceof Data: {
const payload = Encoder.encode(l3);
if (!token && payload.length <= this.mtu) {
return yield payload;
}
lpp = new LpPacket();
lpp.pitToken = token;
lpp.payload = payload;
break;
if (l3 instanceof Interest || l3 instanceof Data) {
const payload = Encoder.encode(l3);
if (!token && payload.length <= this.mtu) {
return yield payload;
}
case l3 instanceof Nack: {
const nack = l3;
lpp = new LpPacket();
lpp.pitToken = token;
lpp.nack = nack.header;
lpp.payload = Encoder.encode(nack.interest);
break;
}
default:
return;
lpp.payload = payload;
}
else {
assert(l3 instanceof Nack);
lpp.nack = l3.header;
lpp.payload = Encoder.encode(l3.interest);
}
}

@@ -120,0 +102,0 @@ catch (err) {

@@ -5,2 +5,3 @@ import { __importDefault, __importStar } from "tslib";

import _cjsDefaultImport0 from "it-keepalive"; const itKeepAlive = __importDefault(_cjsDefaultImport0).default;
import _cjsDefaultImport1 from "minimalistic-assert"; const assert = __importDefault(_cjsDefaultImport1).default;
import { TT } from "./an_node.js";

@@ -10,2 +11,11 @@ import { Fragmenter } from "./fragmenter_node.js";

import { Reassembler } from "./reassembler_node.js";
/**
* Map and flatten, but only do it once.
* This differs from flatMap from streaming-iterables that recursively flattens the result.
*/
async function* flatMap1(f, iterable) {
for await (const item of iterable) {
yield* f(item);
}
}
const IDLE = Encoder.encode(new LpPacket());

@@ -15,18 +25,9 @@ export class LpService {

this.mtu = Infinity;
this.rx = (iterable) => {
return this.rx_(iterable);
};
this.tx = (iterable) => {
let iterable1 = iterable;
if (this.keepAlive > 0) {
iterable1 = itKeepAlive(() => false, { timeout: this.keepAlive })(iterable);
}
return this.tx_(iterable1);
};
if (keepAlive === false || keepAlive <= 0) {
this.keepAlive = -1;
this.rx = (iterable) => flatMap1((tlv) => this.decode(tlv), iterable);
this.tx = (iterable) => flatMap1((pkt) => this.encode(pkt), this.keepAlive ?
itKeepAlive(() => false, { timeout: this.keepAlive })(iterable) :
iterable);
if (Number.isFinite(keepAlive) && keepAlive > 0) {
this.keepAlive = Math.ceil(keepAlive);
}
else {
this.keepAlive = keepAlive;
}
if (Number.isFinite(mtu)) {

@@ -38,16 +39,12 @@ this.mtu = mtu;

}
async *rx_(iterable) {
for await (const tlv of iterable) {
yield* this.decode(tlv);
}
}
*decode(tlv) {
*decode(dtlv) {
const { type, decoder, tlv } = dtlv;
try {
const { type, decoder } = tlv;
if (type !== TT.LpPacket) {
return yield this.decodeL3(tlv);
yield this.decodeL3(dtlv);
return;
}
const fragment = decoder.decode(LpPacket);
const lpp = this.reassembler.accept(fragment);
if (!lpp || !lpp.payload) {
if (!lpp?.payload) {
return;

@@ -68,3 +65,3 @@ }

catch (err) {
yield new LpService.RxError(err, tlv.tlv);
yield new LpService.RxError(err, tlv);
}

@@ -82,38 +79,23 @@ }

}
async *tx_(iterable) {
for await (const pkt of iterable) {
if (pkt === false) {
yield IDLE;
}
else {
yield* this.encode(pkt);
}
*encode(pkt) {
if (pkt === false) {
yield IDLE;
return;
}
}
*encode({ l3, token }) {
let lpp;
const { l3, token } = pkt;
const lpp = new LpPacket();
lpp.pitToken = token;
try {
switch (true) {
case l3 instanceof Interest:
case l3 instanceof Data: {
const payload = Encoder.encode(l3);
if (!token && payload.length <= this.mtu) {
return yield payload;
}
lpp = new LpPacket();
lpp.pitToken = token;
lpp.payload = payload;
break;
if (l3 instanceof Interest || l3 instanceof Data) {
const payload = Encoder.encode(l3);
if (!token && payload.length <= this.mtu) {
return yield payload;
}
case l3 instanceof Nack: {
const nack = l3;
lpp = new LpPacket();
lpp.pitToken = token;
lpp.nack = nack.header;
lpp.payload = Encoder.encode(nack.interest);
break;
}
default:
return;
lpp.payload = payload;
}
else {
assert(l3 instanceof Nack);
lpp.nack = l3.header;
lpp.payload = Encoder.encode(l3.interest);
}
}

@@ -120,0 +102,0 @@ catch (err) {

@@ -5,3 +5,3 @@ import { Data, Interest, Nack } from "@ndn/packet";

constructor({ keepAlive, mtu, reassemblerCapacity, }?: LpService.Options);
private readonly keepAlive;
private readonly keepAlive?;
private readonly mtu;

@@ -11,7 +11,5 @@ private readonly fragmenter?;

rx: (iterable: AsyncIterable<Decoder.Tlv>) => AsyncIterable<LpService.Packet | LpService.RxError>;
private rx_;
private decode;
private decodeL3;
tx: (iterable: AsyncIterable<LpService.Packet>) => AsyncIterable<Uint8Array | LpService.TxError>;
private tx_;
private encode;

@@ -22,3 +20,3 @@ }

/**
* How often to send IDLE packets if nothing else was sent, in millis.
* How often to send IDLE packets if nothing else was sent, in milliseconds.
* Set false or zero to disable keep-alive.

@@ -25,0 +23,0 @@ * @default 60000

{
"name": "@ndn/lp",
"version": "0.0.20210203",
"version": "0.0.20210930",
"description": "NDNts: NDNLP",

@@ -25,8 +25,9 @@ "keywords": [

"dependencies": {
"@ndn/packet": "0.0.20210203",
"@ndn/tlv": "0.0.20210203",
"@ndn/packet": "0.0.20210930",
"@ndn/tlv": "0.0.20210930",
"minimalistic-assert": "^1.0.1",
"it-keepalive": "^1.2.0",
"tslib": "^2.1.0"
"tslib": "^2.3.1"
},
"types": "lib/mod.d.ts"
}

@@ -8,3 +8,4 @@ # @ndn/lp

* Interest/Data/Nack packets without fragmentation.
* Fragmentation and reassembly.
* Nack.
* PIT token.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc