New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@ndn/packet

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ndn/packet - npm Package Compare versions

Comparing version

to
0.0.20210930

0

lib/an.d.ts

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

4

lib/data_browser.js

@@ -17,3 +17,3 @@ import { Encoder, EvDecoder, NNI } from "@ndn/tlv";

let isFinalBlock = false;
args.forEach((arg) => {
for (const arg of args) {
if (Name.isNameLike(arg)) {

@@ -37,3 +37,3 @@ this.name = new Name(arg);

}
});
}
this.isFinalBlock = isFinalBlock;

@@ -40,0 +40,0 @@ }

@@ -17,3 +17,3 @@ import { Encoder, EvDecoder, NNI } from "@ndn/tlv";

let isFinalBlock = false;
args.forEach((arg) => {
for (const arg of args) {
if (Name.isNameLike(arg)) {

@@ -37,3 +37,3 @@ this.name = new Name(arg);

}
});
}
this.isFinalBlock = isFinalBlock;

@@ -40,0 +40,0 @@ }

@@ -0,0 +0,0 @@ import { Decoder, Encoder } from "@ndn/tlv";

import { Decoder, Encoder, EvDecoder, NNI, toHex } from "@ndn/tlv";
import { TT } from "./an_browser.js";
import { Name } from "./name/mod_browser.js";
const EVD = new EvDecoder("Delegation", TT.Delegation)
const DelEVD = new EvDecoder("Delegation", TT.Delegation)
.add(TT.Preference, (t, { nni }) => t.preference = nni)
.add(TT.Name, (t, { decoder }) => t.name = decoder.decode(Name));
const DelsEVD = new EvDecoder("FwHint")
.add(TT.Delegation, (t, { decoder }) => t.push(decoder.decode(FwHint.Delegation)), { repeat: true });
/** ForwardingHint in Interest. */

@@ -13,3 +15,3 @@ export class FwHint {

for (const del of arg) {
this.m.set(toHex(del.name.value), del);
this.add(del);
}

@@ -20,10 +22,12 @@ }

}
else if (Name.isNameLike(arg)) {
this.add(new FwHint.Delegation(arg));
}
}
static decodeValue(value) {
const dels = [];
for (const decoder = new Decoder(value); !decoder.eof;) {
dels.push(decoder.decode(FwHint.Delegation));
}
return new FwHint(dels);
return new FwHint(DelsEVD.decodeValue([], new Decoder(value)));
}
add(del) {
this.m.set(toHex(del.name.value), del);
}
get delegations() {

@@ -44,3 +48,3 @@ return Array.from(this.m.values()).sort((a, b) => a.preference - b.preference);

static decodeFrom(decoder) {
return EVD.decode(new Delegation(), decoder);
return DelEVD.decode(new Delegation(), decoder);
}

@@ -47,0 +51,0 @@ encodeTo(encoder) {

import { Decoder, Encoder, EvDecoder, NNI, toHex } from "@ndn/tlv";
import { TT } from "./an_node.js";
import { Name } from "./name/mod_node.js";
const EVD = new EvDecoder("Delegation", TT.Delegation)
const DelEVD = new EvDecoder("Delegation", TT.Delegation)
.add(TT.Preference, (t, { nni }) => t.preference = nni)
.add(TT.Name, (t, { decoder }) => t.name = decoder.decode(Name));
const DelsEVD = new EvDecoder("FwHint")
.add(TT.Delegation, (t, { decoder }) => t.push(decoder.decode(FwHint.Delegation)), { repeat: true });
/** ForwardingHint in Interest. */

@@ -13,3 +15,3 @@ export class FwHint {

for (const del of arg) {
this.m.set(toHex(del.name.value), del);
this.add(del);
}

@@ -20,10 +22,12 @@ }

}
else if (Name.isNameLike(arg)) {
this.add(new FwHint.Delegation(arg));
}
}
static decodeValue(value) {
const dels = [];
for (const decoder = new Decoder(value); !decoder.eof;) {
dels.push(decoder.decode(FwHint.Delegation));
}
return new FwHint(dels);
return new FwHint(DelsEVD.decodeValue([], new Decoder(value)));
}
add(del) {
this.m.set(toHex(del.name.value), del);
}
get delegations() {

@@ -44,3 +48,3 @@ return Array.from(this.m.values()).sort((a, b) => a.preference - b.preference);

static decodeFrom(decoder) {
return EVD.decode(new Delegation(), decoder);
return DelEVD.decode(new Delegation(), decoder);
}

@@ -47,0 +51,0 @@ encodeTo(encoder) {

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

constructor(copy?: FwHint);
constructor(name: NameLike);
constructor(delegations: readonly FwHint.Delegation[]);
private add;
get delegations(): readonly FwHint.Delegation[];

@@ -10,0 +12,0 @@ private readonly m;

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

this.hopLimit_ = HOPLIMIT_MAX;
args.forEach((arg) => {
for (const arg of args) {
if (Name.isNameLike(arg)) {

@@ -46,3 +46,3 @@ this.name = new Name(arg);

}
});
}
}

@@ -82,3 +82,3 @@ get nonce() { return this.nonce_; }

}
if (typeof t.sigInfo === "undefined") {
if (t.sigInfo === undefined) {
throw new Error("ISigInfo missing in signed Interest");

@@ -118,3 +118,2 @@ }

encodeTo(encoder) {
var _a;
const f = this[FIELDS];

@@ -127,3 +126,3 @@ if (f.name.length === 0) {

}
encoder.prependTlv(TT.Interest, f.name, f.canBePrefix ? [TT.CanBePrefix] : undefined, f.mustBeFresh ? [TT.MustBeFresh] : undefined, f.fwHint, [TT.Nonce, NNI((_a = f.nonce) !== null && _a !== void 0 ? _a : Interest.generateNonce(), { len: 4 })], f.lifetime === Interest.DefaultLifetime ?
encoder.prependTlv(TT.Interest, f.name, f.canBePrefix ? [TT.CanBePrefix] : undefined, f.mustBeFresh ? [TT.MustBeFresh] : undefined, f.fwHint, [TT.Nonce, NNI(f.nonce ?? Interest.generateNonce(), { len: 4 })], f.lifetime === Interest.DefaultLifetime ?
undefined : [TT.InterestLifetime, NNI(f.lifetime)], f.hopLimit === HOPLIMIT_MAX ?

@@ -162,7 +161,7 @@ undefined : [TT.HopLimit, NNI(f.hopLimit, { len: 1 })], ...this.encodeParamsPortion());

const f = this[FIELDS];
if (typeof f.appParameters === "undefined") {
if (f.appParameters === undefined) {
return;
}
const params = f.paramsPortion;
if (typeof params === "undefined") {
if (params === undefined) {
throw new Error("parameters portion is empty");

@@ -266,15 +265,15 @@ }

return (interest) => {
if (typeof canBePrefix !== "undefined") {
if (canBePrefix !== undefined) {
interest.canBePrefix = canBePrefix;
}
if (typeof mustBeFresh !== "undefined") {
if (mustBeFresh !== undefined) {
interest.mustBeFresh = mustBeFresh;
}
if (typeof fwHint !== "undefined") {
if (fwHint !== undefined) {
interest.fwHint = fwHint;
}
if (typeof lifetime !== "undefined") {
if (lifetime !== undefined) {
interest.lifetime = lifetime;
}
if (typeof hopLimit !== "undefined") {
if (hopLimit !== undefined) {
interest.hopLimit = hopLimit;

@@ -281,0 +280,0 @@ }

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

this.hopLimit_ = HOPLIMIT_MAX;
args.forEach((arg) => {
for (const arg of args) {
if (Name.isNameLike(arg)) {

@@ -46,3 +46,3 @@ this.name = new Name(arg);

}
});
}
}

@@ -82,3 +82,3 @@ get nonce() { return this.nonce_; }

}
if (typeof t.sigInfo === "undefined") {
if (t.sigInfo === undefined) {
throw new Error("ISigInfo missing in signed Interest");

@@ -118,3 +118,2 @@ }

encodeTo(encoder) {
var _a;
const f = this[FIELDS];

@@ -127,3 +126,3 @@ if (f.name.length === 0) {

}
encoder.prependTlv(TT.Interest, f.name, f.canBePrefix ? [TT.CanBePrefix] : undefined, f.mustBeFresh ? [TT.MustBeFresh] : undefined, f.fwHint, [TT.Nonce, NNI((_a = f.nonce) !== null && _a !== void 0 ? _a : Interest.generateNonce(), { len: 4 })], f.lifetime === Interest.DefaultLifetime ?
encoder.prependTlv(TT.Interest, f.name, f.canBePrefix ? [TT.CanBePrefix] : undefined, f.mustBeFresh ? [TT.MustBeFresh] : undefined, f.fwHint, [TT.Nonce, NNI(f.nonce ?? Interest.generateNonce(), { len: 4 })], f.lifetime === Interest.DefaultLifetime ?
undefined : [TT.InterestLifetime, NNI(f.lifetime)], f.hopLimit === HOPLIMIT_MAX ?

@@ -162,7 +161,7 @@ undefined : [TT.HopLimit, NNI(f.hopLimit, { len: 1 })], ...this.encodeParamsPortion());

const f = this[FIELDS];
if (typeof f.appParameters === "undefined") {
if (f.appParameters === undefined) {
return;
}
const params = f.paramsPortion;
if (typeof params === "undefined") {
if (params === undefined) {
throw new Error("parameters portion is empty");

@@ -266,15 +265,15 @@ }

return (interest) => {
if (typeof canBePrefix !== "undefined") {
if (canBePrefix !== undefined) {
interest.canBePrefix = canBePrefix;
}
if (typeof mustBeFresh !== "undefined") {
if (mustBeFresh !== undefined) {
interest.mustBeFresh = mustBeFresh;
}
if (typeof fwHint !== "undefined") {
if (fwHint !== undefined) {
interest.fwHint = fwHint;
}
if (typeof lifetime !== "undefined") {
if (lifetime !== undefined) {
interest.lifetime = lifetime;
}
if (typeof hopLimit !== "undefined") {
if (hopLimit !== undefined) {
interest.hopLimit = hopLimit;

@@ -281,0 +280,0 @@ }

@@ -0,0 +0,0 @@ import { Decoder, Encoder } from "@ndn/tlv";

@@ -39,3 +39,3 @@ import { Encoder, EvDecoder } from "@ndn/tlv";

function mustGetName(kl) {
const name = kl === null || kl === void 0 ? void 0 : kl.name;
const name = kl?.name;
if (!name) {

@@ -42,0 +42,0 @@ throw new Error("KeyLocator does not have name");

@@ -39,3 +39,3 @@ import { Encoder, EvDecoder } from "@ndn/tlv";

function mustGetName(kl) {
const name = kl === null || kl === void 0 ? void 0 : kl.name;
const name = kl?.name;
if (!name) {

@@ -42,0 +42,0 @@ throw new Error("KeyLocator does not have name");

@@ -0,0 +0,0 @@ import { Decoder, Encoder } from "@ndn/tlv";

@@ -0,0 +0,0 @@ export * from "./name/mod";

@@ -0,0 +0,0 @@ import { Decoder, Encoder } from "@ndn/tlv";

@@ -7,3 +7,3 @@ import { toHex } from "@ndn/tlv";

*/
export function lpm(name, get) {
export function* lpm(name, get) {
const prefixes = [""];

@@ -19,6 +19,5 @@ let s = "";

if (entry) {
return entry;
yield entry;
}
}
return undefined;
}

@@ -7,3 +7,3 @@ import { toHex } from "@ndn/tlv";

*/
export function lpm(name, get) {
export function* lpm(name, get) {
const prefixes = [""];

@@ -19,6 +19,5 @@ let s = "";

if (entry) {
return entry;
yield entry;
}
}
return undefined;
}

@@ -7,2 +7,2 @@ import type { Name } from "./name";

*/
export declare function lpm<Entry>(name: Name, get: (prefixHex: string) => Entry | undefined): Entry | undefined;
export declare function lpm<Entry>(name: Name, get: (prefixHex: string) => Entry | undefined): Iterable<Entry>;

@@ -30,5 +30,3 @@ import { TT } from "../an_browser.js";

/** Print name in alternate URI syntax. */
this.ofName = (name) => {
return `/${name.comps.map((comp) => this.ofComponent(comp)).join("/")}`;
};
this.ofName = (name) => `/${name.comps.map((comp) => this.ofComponent(comp)).join("/")}`;
/** Parse component from alternate URI syntax */

@@ -45,5 +43,3 @@ this.parseComponent = (input) => {

/** Parse name from alternate URI syntax. */
this.parseName = (input) => {
return new Name(input, this.parseComponent);
};
this.parseName = (input) => new Name(input, this.parseComponent);
}

@@ -50,0 +46,0 @@ }

@@ -30,5 +30,3 @@ import { TT } from "../an_node.js";

/** Print name in alternate URI syntax. */
this.ofName = (name) => {
return `/${name.comps.map((comp) => this.ofComponent(comp)).join("/")}`;
};
this.ofName = (name) => `/${name.comps.map((comp) => this.ofComponent(comp)).join("/")}`;
/** Parse component from alternate URI syntax */

@@ -45,5 +43,3 @@ this.parseComponent = (input) => {

/** Parse name from alternate URI syntax. */
this.parseName = (input) => {
return new Name(input, this.parseComponent);
};
this.parseName = (input) => new Name(input, this.parseComponent);
}

@@ -50,0 +46,0 @@ }

@@ -0,0 +0,0 @@ import { Component } from "./component";

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

function checkType(t) {
return !Number.isNaN(t) && t >= 0x01 && t <= 0xFFFF;
return Number.isFinite(t) && t >= 0x01 && t <= 0xFFFF;
}

@@ -16,6 +16,6 @@ function assertType(t) {

const UNESCAPED = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
const a = new Array(256);
const a = [];
for (let ch = 0x00; ch <= 0xFF; ++ch) {
const s = String.fromCharCode(ch);
a[ch] = UNESCAPED.includes(s) ? s : `%${ch.toString(16).padStart(2, "0").toUpperCase()}`;
a.push(UNESCAPED.includes(s) ? s : `%${ch.toString(16).padStart(2, "0").toUpperCase()}`);
}

@@ -80,3 +80,3 @@ return a;

let iType;
if (typeof sValue === "undefined") {
if (sValue === undefined) {
[sType, sValue] = ["", sType];

@@ -113,6 +113,6 @@ }

let hasNonPeriods = false;
this.value.forEach((ch) => {
for (const ch of this.value) {
hasNonPeriods || (hasNonPeriods = ch !== CHARCODE_PERIOD);
b += CHAR_ENCODE[ch];
});
}
if (!hasNonPeriods) {

@@ -119,0 +119,0 @@ b += "...";

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

function checkType(t) {
return !Number.isNaN(t) && t >= 0x01 && t <= 0xFFFF;
return Number.isFinite(t) && t >= 0x01 && t <= 0xFFFF;
}

@@ -16,6 +16,6 @@ function assertType(t) {

const UNESCAPED = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
const a = new Array(256);
const a = [];
for (let ch = 0x00; ch <= 0xFF; ++ch) {
const s = String.fromCharCode(ch);
a[ch] = UNESCAPED.includes(s) ? s : `%${ch.toString(16).padStart(2, "0").toUpperCase()}`;
a.push(UNESCAPED.includes(s) ? s : `%${ch.toString(16).padStart(2, "0").toUpperCase()}`);
}

@@ -80,3 +80,3 @@ return a;

let iType;
if (typeof sValue === "undefined") {
if (sValue === undefined) {
[sType, sValue] = ["", sType];

@@ -113,6 +113,6 @@ }

let hasNonPeriods = false;
this.value.forEach((ch) => {
for (const ch of this.value) {
hasNonPeriods || (hasNonPeriods = ch !== CHARCODE_PERIOD);
b += CHAR_ENCODE[ch];
});
}
if (!hasNonPeriods) {

@@ -119,0 +119,0 @@ b += "...";

@@ -0,0 +0,0 @@ import { Decoder, Encoder } from "@ndn/tlv";

@@ -0,0 +0,0 @@ import type { Component } from "./component";

@@ -40,4 +40,3 @@ import { fromHex, toHex } from "@ndn/tlv";

strip(name) {
var _a;
if ((_a = name.get(-1)) === null || _a === void 0 ? void 0 : _a.is(this)) {
if (name.get(-1)?.is(this)) {
return name.getPrefix(-1);

@@ -44,0 +43,0 @@ }

@@ -40,4 +40,3 @@ import { fromHex, toHex } from "@ndn/tlv";

strip(name) {
var _a;
if ((_a = name.get(-1)) === null || _a === void 0 ? void 0 : _a.is(this)) {
if (name.get(-1)?.is(this)) {
return name.getPrefix(-1);

@@ -44,0 +43,0 @@ }

@@ -0,0 +0,0 @@ import { Component } from "./component";

@@ -0,0 +0,0 @@ export * from "./algo";

@@ -26,3 +26,3 @@ import { Decoder, Encoder } from "@ndn/tlv";

case Array.isArray(arg1):
this.comps = arg1.map(Component.from);
this.comps = Array.from(arg1, Component.from);
break;

@@ -64,3 +64,3 @@ case arg1 instanceof Uint8Array: {

const comp = this.get(i);
if (typeof comp === "undefined") {
if (comp === undefined) {
throw new Error(`component ${i} out of range`);

@@ -85,4 +85,3 @@ }

typeof args[0].create === "function") {
const convention = args[0];
return this.append(convention.create(args[1]));
return this.append(args[0].create(args[1]));
}

@@ -89,0 +88,0 @@ const suffix = args;

@@ -26,3 +26,3 @@ import { Decoder, Encoder } from "@ndn/tlv";

case Array.isArray(arg1):
this.comps = arg1.map(Component.from);
this.comps = Array.from(arg1, Component.from);
break;

@@ -64,3 +64,3 @@ case arg1 instanceof Uint8Array: {

const comp = this.get(i);
if (typeof comp === "undefined") {
if (comp === undefined) {
throw new Error(`component ${i} out of range`);

@@ -85,4 +85,3 @@ }

typeof args[0].create === "function") {
const convention = args[0];
return this.append(convention.create(args[1]));
return this.append(args[0].create(args[1]));
}

@@ -89,0 +88,0 @@ const suffix = args;

@@ -0,0 +0,0 @@ import { Decoder, Encoder } from "@ndn/tlv";

@@ -0,0 +0,0 @@ import type { Data } from "../data";

export declare function randBytes(size: number): Uint8Array;
export declare function sha256(input: Uint8Array): Promise<Uint8Array>;
export declare function timingSafeEqual(a: Uint8Array, b: Uint8Array): boolean;
export declare function randBytes(size: number): Uint8Array;
export declare function sha256(input: Uint8Array): Promise<Uint8Array>;
export declare function timingSafeEqual(a: Uint8Array, b: Uint8Array): boolean;

@@ -1,2 +0,2 @@

import { createHash, randomBytes, timingSafeEqual as nodeTimingSafeEqual } from "crypto";
import { createHash, randomBytes, timingSafeEqual as nodeTimingSafeEqual } from "node:crypto";
export function randBytes(size) {

@@ -3,0 +3,0 @@ return randomBytes(size);

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

constructor(arg1, ...rules) {
var _a;
this.owned = new DefaultWeakMap(() => ({}));

@@ -29,3 +28,3 @@ this.records = new Map();

else {
opts = (_a = arg1) !== null && _a !== void 0 ? _a : {};
opts = arg1 ?? {};
}

@@ -51,3 +50,2 @@ const { trackedKeys = 256, } = opts;

check({ sigInfo }) {
var _a;
if (!sigInfo) {

@@ -57,8 +55,7 @@ throw new Error("SignedInterestPolicy rejects unsigned Interest");

const key = (() => {
var _a, _b;
const klName = (_a = sigInfo.keyLocator) === null || _a === void 0 ? void 0 : _a.name;
const klName = sigInfo.keyLocator?.name;
if (klName) {
return `N:${toHex(klName.value)}`;
}
const klDigest = (_b = sigInfo.keyLocator) === null || _b === void 0 ? void 0 : _b.digest;
const klDigest = sigInfo.keyLocator?.digest;
if (klDigest) {

@@ -69,3 +66,3 @@ return `D:${toHex(klDigest)}`;

})();
const state = (_a = this.records.get(key)) !== null && _a !== void 0 ? _a : {};
const state = this.records.get(key) ?? {};
const saves = [];

@@ -152,3 +149,2 @@ for (const rule of this.rules) {

update(si, state) {
var _a;
let nonceHex;

@@ -158,12 +154,11 @@ do {

nonceHex = toHex(si.nonce);
} while ((_a = state.nonces) === null || _a === void 0 ? void 0 : _a.has(nonceHex));
} while (state.nonces?.has(nonceHex));
this.recordNonce(state, nonceHex);
}
check(si, state) {
var _a;
if (typeof si.nonce === "undefined" || si.nonce.length < this.minNonceLength) {
if (si.nonce === undefined || si.nonce.length < this.minNonceLength) {
throw new Error("SigNonce is absent or too short");
}
const nonceHex = toHex(si.nonce);
if ((_a = state.nonces) === null || _a === void 0 ? void 0 : _a.has(nonceHex)) {
if (state.nonces?.has(nonceHex)) {
throw new Error("SigNonce is duplicate");

@@ -174,4 +169,3 @@ }

recordNonce(state, nonceHex) {
var _a;
(_a = state.nonces) !== null && _a !== void 0 ? _a : (state.nonces = new Set());
state.nonces ?? (state.nonces = new Set());
state.nonces.add(nonceHex);

@@ -182,17 +176,18 @@ evict(this.trackedNonces, state.nonces);

class SequencedRuleBase {
constructor(field, name) {
constructor(field, name, max) {
this.field = field;
this.name = name;
this.max = max;
}
check(si, state) {
const value = si[this.field];
if (typeof value !== "number") { // TODO adjust if SigSeqNum is decoded as bignum
if (value === undefined) {
throw new Error(`${this.name} is absent`);
}
if (typeof state[this.field] === "number" && value <= state[this.field]) {
const prev = state[this.field];
if (prev !== undefined && value <= prev) {
throw new Error(`${this.name} reordering detected`);
}
return () => {
var _a;
state[this.field] = Math.max((_a = state[this.field]) !== null && _a !== void 0 ? _a : 0, value);
state[this.field] = this.max(value, state[this.field]);
};

@@ -203,3 +198,3 @@ }

constructor({ maxClockOffset = 60000, }) {
super("time", "SigTime");
super("time", "SigTime", (value, prev = 0) => Math.max(value, prev));
assert(maxClockOffset >= 0);

@@ -209,4 +204,3 @@ this.maxClockOffset = maxClockOffset;

update(si, state) {
var _a;
si.time = Math.max(Date.now(), 1 + ((_a = state.time) !== null && _a !== void 0 ? _a : 0));
si.time = Math.max(Date.now(), 1 + (state.time ?? 0));
state.time = si.time;

@@ -224,10 +218,9 @@ }

class SeqNumRule extends SequencedRuleBase {
constructor({ initialSeqNum = 0, }) {
super("seqNum", "SigSeqNum");
this.initialSeqNum = initialSeqNum;
constructor({ initialSeqNum = 0n, }) {
super("seqNum", "SigSeqNum", (value, prev = 0n) => value > prev ? value : prev);
this.beforeInitialSeqNum = initialSeqNum - 1n;
}
update(si, state) {
var _a;
si.seqNum = 1 + ((_a = state.seqNum) !== null && _a !== void 0 ? _a : (this.initialSeqNum - 1));
state.seqNum = si.seqNum;
state.seqNum ?? (state.seqNum = this.beforeInitialSeqNum);
si.seqNum = ++state.seqNum;
}

@@ -234,0 +227,0 @@ }

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

constructor(arg1, ...rules) {
var _a;
this.owned = new DefaultWeakMap(() => ({}));

@@ -29,3 +28,3 @@ this.records = new Map();

else {
opts = (_a = arg1) !== null && _a !== void 0 ? _a : {};
opts = arg1 ?? {};
}

@@ -51,3 +50,2 @@ const { trackedKeys = 256, } = opts;

check({ sigInfo }) {
var _a;
if (!sigInfo) {

@@ -57,8 +55,7 @@ throw new Error("SignedInterestPolicy rejects unsigned Interest");

const key = (() => {
var _a, _b;
const klName = (_a = sigInfo.keyLocator) === null || _a === void 0 ? void 0 : _a.name;
const klName = sigInfo.keyLocator?.name;
if (klName) {
return `N:${toHex(klName.value)}`;
}
const klDigest = (_b = sigInfo.keyLocator) === null || _b === void 0 ? void 0 : _b.digest;
const klDigest = sigInfo.keyLocator?.digest;
if (klDigest) {

@@ -69,3 +66,3 @@ return `D:${toHex(klDigest)}`;

})();
const state = (_a = this.records.get(key)) !== null && _a !== void 0 ? _a : {};
const state = this.records.get(key) ?? {};
const saves = [];

@@ -152,3 +149,2 @@ for (const rule of this.rules) {

update(si, state) {
var _a;
let nonceHex;

@@ -158,12 +154,11 @@ do {

nonceHex = toHex(si.nonce);
} while ((_a = state.nonces) === null || _a === void 0 ? void 0 : _a.has(nonceHex));
} while (state.nonces?.has(nonceHex));
this.recordNonce(state, nonceHex);
}
check(si, state) {
var _a;
if (typeof si.nonce === "undefined" || si.nonce.length < this.minNonceLength) {
if (si.nonce === undefined || si.nonce.length < this.minNonceLength) {
throw new Error("SigNonce is absent or too short");
}
const nonceHex = toHex(si.nonce);
if ((_a = state.nonces) === null || _a === void 0 ? void 0 : _a.has(nonceHex)) {
if (state.nonces?.has(nonceHex)) {
throw new Error("SigNonce is duplicate");

@@ -174,4 +169,3 @@ }

recordNonce(state, nonceHex) {
var _a;
(_a = state.nonces) !== null && _a !== void 0 ? _a : (state.nonces = new Set());
state.nonces ?? (state.nonces = new Set());
state.nonces.add(nonceHex);

@@ -182,17 +176,18 @@ evict(this.trackedNonces, state.nonces);

class SequencedRuleBase {
constructor(field, name) {
constructor(field, name, max) {
this.field = field;
this.name = name;
this.max = max;
}
check(si, state) {
const value = si[this.field];
if (typeof value !== "number") { // TODO adjust if SigSeqNum is decoded as bignum
if (value === undefined) {
throw new Error(`${this.name} is absent`);
}
if (typeof state[this.field] === "number" && value <= state[this.field]) {
const prev = state[this.field];
if (prev !== undefined && value <= prev) {
throw new Error(`${this.name} reordering detected`);
}
return () => {
var _a;
state[this.field] = Math.max((_a = state[this.field]) !== null && _a !== void 0 ? _a : 0, value);
state[this.field] = this.max(value, state[this.field]);
};

@@ -203,3 +198,3 @@ }

constructor({ maxClockOffset = 60000, }) {
super("time", "SigTime");
super("time", "SigTime", (value, prev = 0) => Math.max(value, prev));
assert(maxClockOffset >= 0);

@@ -209,4 +204,3 @@ this.maxClockOffset = maxClockOffset;

update(si, state) {
var _a;
si.time = Math.max(Date.now(), 1 + ((_a = state.time) !== null && _a !== void 0 ? _a : 0));
si.time = Math.max(Date.now(), 1 + (state.time ?? 0));
state.time = si.time;

@@ -224,10 +218,9 @@ }

class SeqNumRule extends SequencedRuleBase {
constructor({ initialSeqNum = 0, }) {
super("seqNum", "SigSeqNum");
this.initialSeqNum = initialSeqNum;
constructor({ initialSeqNum = 0n, }) {
super("seqNum", "SigSeqNum", (value, prev = 0n) => value > prev ? value : prev);
this.beforeInitialSeqNum = initialSeqNum - 1n;
}
update(si, state) {
var _a;
si.seqNum = 1 + ((_a = state.seqNum) !== null && _a !== void 0 ? _a : (this.initialSeqNum - 1));
state.seqNum = si.seqNum;
state.seqNum ?? (state.seqNum = this.beforeInitialSeqNum);
si.seqNum = ++state.seqNum;
}

@@ -234,0 +227,0 @@ }

@@ -44,3 +44,3 @@ import { Interest } from "../interest";

time?: number;
seqNum?: number;
seqNum?: bigint;
}

@@ -143,5 +143,5 @@ interface Rule {

* Initial sequence number.
* @default 0
* @default 0n
*/
initialSeqNum?: number;
initialSeqNum?: bigint;
}

@@ -148,0 +148,0 @@ /**

@@ -23,5 +23,4 @@ import { SigType } from "../an_browser.js";

function putSigInfo(pkt, sigType, keyLocator) {
var _a;
(_a = pkt.sigInfo) !== null && _a !== void 0 ? _a : (pkt.sigInfo = new SigInfo());
if (typeof sigType !== "undefined") {
pkt.sigInfo ?? (pkt.sigInfo = new SigInfo());
if (sigType !== undefined) {
pkt.sigInfo.type = sigType;

@@ -47,4 +46,3 @@ }

function checkSigType(pkt, expectedSigType) {
var _a;
if (((_a = pkt.sigInfo) === null || _a === void 0 ? void 0 : _a.type) !== expectedSigType) {
if (pkt.sigInfo?.type !== expectedSigType) {
throw new Error(`packet does not have SigType ${expectedSigType}`);

@@ -51,0 +49,0 @@ }

@@ -23,5 +23,4 @@ import { SigType } from "../an_node.js";

function putSigInfo(pkt, sigType, keyLocator) {
var _a;
(_a = pkt.sigInfo) !== null && _a !== void 0 ? _a : (pkt.sigInfo = new SigInfo());
if (typeof sigType !== "undefined") {
pkt.sigInfo ?? (pkt.sigInfo = new SigInfo());
if (sigType !== undefined) {
pkt.sigInfo.type = sigType;

@@ -47,4 +46,3 @@ }

function checkSigType(pkt, expectedSigType) {
var _a;
if (((_a = pkt.sigInfo) === null || _a === void 0 ? void 0 : _a.type) !== expectedSigType) {
if (pkt.sigInfo?.type !== expectedSigType) {
throw new Error(`packet does not have SigType ${expectedSigType}`);

@@ -51,0 +49,0 @@ }

@@ -0,0 +0,0 @@ import { KeyLocator } from "../key-locator";

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

.add(TT.SigTime, (t, { nni }) => t.time = nni)
.add(TT.SigSeqNum, (t, { nni }) => t.seqNum = nni)
.add(TT.SigSeqNum, (t, { nniBig }) => t.seqNum = nniBig)
.setUnknown(EXTENSIONS.decodeUnknown);

@@ -32,3 +32,3 @@ /** SignatureInfo on Interest or Data. */

this.type = SigType.Null;
this[_a] = Extensible.newRecords();
this[_a] = EXTENSIONS;
const klArgs = [];

@@ -44,3 +44,3 @@ for (const arg of args) {

Object.assign(this, arg);
this[Extensible.TAG] = { ...arg[Extensible.TAG] };
Extensible.cloneRecord(this, arg);
}

@@ -71,5 +71,3 @@ else if (arg[ctorAssign]) {

encodeTo(encoder, tt) {
encoder.prependTlv(tt, [TT.SigType, NNI(this.type)], this.keyLocator, [TT.SigNonce, Encoder.OmitEmpty, this.nonce], [TT.SigTime, Encoder.OmitEmpty,
typeof this.time === "undefined" ? undefined : NNI(this.time)], [TT.SigSeqNum, Encoder.OmitEmpty,
typeof this.seqNum === "undefined" ? undefined : NNI(this.seqNum)], ...EXTENSIONS.encode(this));
encoder.prependTlv(tt, [TT.SigType, NNI(this.type)], this.keyLocator, [TT.SigNonce, Encoder.OmitEmpty, this.nonce], this.time === undefined ? undefined : [TT.SigTime, NNI(this.time)], this.seqNum === undefined ? undefined : [TT.SigSeqNum, NNI(this.seqNum)], ...EXTENSIONS.encode(this));
}

@@ -76,0 +74,0 @@ }

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

.add(TT.SigTime, (t, { nni }) => t.time = nni)
.add(TT.SigSeqNum, (t, { nni }) => t.seqNum = nni)
.add(TT.SigSeqNum, (t, { nniBig }) => t.seqNum = nniBig)
.setUnknown(EXTENSIONS.decodeUnknown);

@@ -32,3 +32,3 @@ /** SignatureInfo on Interest or Data. */

this.type = SigType.Null;
this[_a] = Extensible.newRecords();
this[_a] = EXTENSIONS;
const klArgs = [];

@@ -44,3 +44,3 @@ for (const arg of args) {

Object.assign(this, arg);
this[Extensible.TAG] = { ...arg[Extensible.TAG] };
Extensible.cloneRecord(this, arg);
}

@@ -71,5 +71,3 @@ else if (arg[ctorAssign]) {

encodeTo(encoder, tt) {
encoder.prependTlv(tt, [TT.SigType, NNI(this.type)], this.keyLocator, [TT.SigNonce, Encoder.OmitEmpty, this.nonce], [TT.SigTime, Encoder.OmitEmpty,
typeof this.time === "undefined" ? undefined : NNI(this.time)], [TT.SigSeqNum, Encoder.OmitEmpty,
typeof this.seqNum === "undefined" ? undefined : NNI(this.seqNum)], ...EXTENSIONS.encode(this));
encoder.prependTlv(tt, [TT.SigType, NNI(this.type)], this.keyLocator, [TT.SigNonce, Encoder.OmitEmpty, this.nonce], this.time === undefined ? undefined : [TT.SigTime, NNI(this.time)], this.seqNum === undefined ? undefined : [TT.SigSeqNum, NNI(this.seqNum)], ...EXTENSIONS.encode(this));
}

@@ -76,0 +74,0 @@ }

@@ -1,2 +0,2 @@

import { Decoder, EncodableObj, Extensible } from "@ndn/tlv";
import { Decoder, EncodableObj, Extensible, ExtensionRegistry } from "@ndn/tlv";
import { KeyLocator } from "./key-locator";

@@ -10,4 +10,4 @@ /** SignatureInfo on Interest or Data. */

time?: number;
seqNum?: number;
[Extensible.TAG]: Map<number, unknown>;
seqNum?: bigint;
readonly [Extensible.TAG]: ExtensionRegistry<SigInfo>;
/**

@@ -41,3 +41,3 @@ * Construct from flexible arguments.

function Time(v?: number): CtorTag;
function SeqNum(v: number): {
function SeqNum(v: bigint): {
[ctorAssign](si: SigInfo): void;

@@ -44,0 +44,0 @@ };

{
"name": "@ndn/packet",
"version": "0.0.20210203",
"version": "0.0.20210930",
"description": "NDNts: Network Layer Packets",

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

"dependencies": {
"@ndn/tlv": "0.0.20210203",
"@ndn/tlv": "0.0.20210930",
"buffer-compare": "^1.1.1",
"mnemonist": "^0.38.1",
"mnemonist": "^0.38.4",
"minimalistic-assert": "^1.0.1",
"tslib": "^2.1.0"
"tslib": "^2.3.1"
},
"types": "lib/mod.d.ts"
}

@@ -12,3 +12,4 @@ # @ndn/packet

import { Decoder, Encoder, fromUtf8, toUtf8 } from "@ndn/tlv";
import { strict as assert } from "assert";
import { strict as assert } from "node:assert";
(async () => {

@@ -84,3 +85,3 @@ ```

// It would return 'undefined' if the component does not exist, so we have to check.
if (typeof name1comp1 === "undefined") {
if (name1comp1 === undefined) {
assert.fail(); // This isn't supposed to happen for this name, though.

@@ -212,3 +213,3 @@ } else {

assert.equal(digest2, digest);
assert(typeof fullName2 !== "undefined");
assert(fullName2 !== undefined);
assert.equal(fullName2.toString(), fullName.toString());

@@ -215,0 +216,0 @@