@ndn/packet
Advanced tools
Comparing version 0.0.20230121 to 0.0.20240113
export declare const TT: { | ||
Name: number; | ||
GenericNameComponent: number; | ||
ImplicitSha256DigestComponent: number; | ||
ParametersSha256DigestComponent: number; | ||
Interest: number; | ||
CanBePrefix: number; | ||
MustBeFresh: number; | ||
ForwardingHint: number; | ||
Nonce: number; | ||
InterestLifetime: number; | ||
HopLimit: number; | ||
AppParameters: number; | ||
ISigInfo: number; | ||
ISigValue: number; | ||
Data: number; | ||
MetaInfo: number; | ||
ContentType: number; | ||
FreshnessPeriod: number; | ||
FinalBlock: number; | ||
Content: number; | ||
DSigInfo: number; | ||
DSigValue: number; | ||
SigType: number; | ||
KeyLocator: number; | ||
KeyDigest: number; | ||
SigNonce: number; | ||
SigTime: number; | ||
SigSeqNum: number; | ||
Nack: number; | ||
NackReason: number; | ||
readonly Name: 7; | ||
readonly GenericNameComponent: 8; | ||
readonly ImplicitSha256DigestComponent: 1; | ||
readonly ParametersSha256DigestComponent: 2; | ||
readonly Interest: 5; | ||
readonly CanBePrefix: 33; | ||
readonly MustBeFresh: 18; | ||
readonly ForwardingHint: 30; | ||
readonly Nonce: 10; | ||
readonly InterestLifetime: 12; | ||
readonly HopLimit: 34; | ||
readonly AppParameters: 36; | ||
readonly ISigInfo: 44; | ||
readonly ISigValue: 46; | ||
readonly Data: 6; | ||
readonly MetaInfo: 20; | ||
readonly ContentType: 24; | ||
readonly FreshnessPeriod: 25; | ||
readonly FinalBlock: 26; | ||
readonly Content: 21; | ||
readonly DSigInfo: 22; | ||
readonly DSigValue: 23; | ||
readonly SigType: 27; | ||
readonly KeyLocator: 28; | ||
readonly KeyDigest: 29; | ||
readonly SigNonce: 38; | ||
readonly SigTime: 40; | ||
readonly SigSeqNum: 42; | ||
readonly Nack: 800; | ||
readonly NackReason: 801; | ||
}; | ||
export declare const SigType: { | ||
Sha256: number; | ||
Sha256WithRsa: number; | ||
Sha256WithEcdsa: number; | ||
HmacWithSha256: number; | ||
Ed25519: number; | ||
Null: number; | ||
readonly Sha256: 0; | ||
readonly Sha256WithRsa: 1; | ||
readonly Sha256WithEcdsa: 3; | ||
readonly HmacWithSha256: 4; | ||
readonly Ed25519: 5; | ||
readonly Null: 200; | ||
}; | ||
export declare const NackReason: { | ||
Congestion: number; | ||
Duplicate: number; | ||
NoRoute: number; | ||
readonly Congestion: 50; | ||
readonly Duplicate: 100; | ||
readonly NoRoute: 150; | ||
}; |
import { Encoder, EvDecoder, NNI } from "@ndn/tlv"; | ||
import { sha256 } from "@ndn/util"; | ||
import { constrain, sha256 } from "@ndn/util"; | ||
import { TT } from "./an_browser.js"; | ||
@@ -35,6 +35,6 @@ import { definePublicFields, FIELDS } from "./impl-public-fields_browser.js"; | ||
get contentType() { return this.contentType_; } | ||
set contentType(v) { this.contentType_ = NNI.constrain(v, "ContentType"); } | ||
set contentType(v) { this.contentType_ = constrain(v, "ContentType"); } | ||
contentType_ = 0; | ||
get freshnessPeriod() { return this.freshnessPeriod_; } | ||
set freshnessPeriod(v) { this.freshnessPeriod_ = NNI.constrain(v, "FreshnessPeriod"); } | ||
set freshnessPeriod(v) { this.freshnessPeriod_ = constrain(Math.trunc(v), "FreshnessPeriod"); } | ||
freshnessPeriod_ = 0; | ||
@@ -123,13 +123,15 @@ finalBlockId; | ||
TT.MetaInfo, Encoder.OmitEmpty, | ||
contentType > 0 ? [TT.ContentType, NNI(contentType)] : undefined, | ||
freshnessPeriod > 0 ? [TT.FreshnessPeriod, NNI(freshnessPeriod)] : undefined, | ||
contentType > 0 && [TT.ContentType, NNI(contentType)], | ||
freshnessPeriod > 0 && [TT.FreshnessPeriod, NNI(freshnessPeriod)], | ||
finalBlockId && [TT.FinalBlock, finalBlockId], | ||
], | ||
content.byteLength > 0 ? [TT.Content, content] : undefined, | ||
content.length > 0 && [TT.Content, content], | ||
sigInfo.encodeAs(TT.DSigInfo), | ||
]; | ||
} | ||
/** Return the implicit digest if it's already computed. */ | ||
getImplicitDigest() { | ||
return this[FIELDS].topTlvDigest; | ||
} | ||
/** Compute the implicit digest. */ | ||
async computeImplicitDigest() { | ||
@@ -147,2 +149,3 @@ let digest = this.getImplicitDigest(); | ||
} | ||
/** Return the full name if the implicit digest is already computed. */ | ||
getFullName() { | ||
@@ -155,2 +158,3 @@ const digest = this.getImplicitDigest(); | ||
} | ||
/** Compute the full name (name plus implicit digest). */ | ||
async computeFullName() { | ||
@@ -162,6 +166,8 @@ await this.computeImplicitDigest(); | ||
* Determine if a Data can satisfy an Interest. | ||
* @param isCacheLookup if true, Data with zero FreshnessPeriod cannot satisfy Interest with MustBeFresh; | ||
* if false, this check does not apply. | ||
* @returns a Promise that will be resolved with the result. | ||
*/ | ||
async canSatisfy(interest) { | ||
if (interest.mustBeFresh && this.freshnessPeriod <= 0) { | ||
async canSatisfy(interest, { isCacheLookup = false } = {}) { | ||
if (isCacheLookup && interest.mustBeFresh && this.freshnessPeriod <= 0) { | ||
return false; | ||
@@ -168,0 +174,0 @@ } |
import { Encoder, EvDecoder, NNI } from "@ndn/tlv"; | ||
import { sha256 } from "@ndn/util"; | ||
import { constrain, sha256 } from "@ndn/util"; | ||
import { TT } from "./an_node.js"; | ||
@@ -35,6 +35,6 @@ import { definePublicFields, FIELDS } from "./impl-public-fields_node.js"; | ||
get contentType() { return this.contentType_; } | ||
set contentType(v) { this.contentType_ = NNI.constrain(v, "ContentType"); } | ||
set contentType(v) { this.contentType_ = constrain(v, "ContentType"); } | ||
contentType_ = 0; | ||
get freshnessPeriod() { return this.freshnessPeriod_; } | ||
set freshnessPeriod(v) { this.freshnessPeriod_ = NNI.constrain(v, "FreshnessPeriod"); } | ||
set freshnessPeriod(v) { this.freshnessPeriod_ = constrain(Math.trunc(v), "FreshnessPeriod"); } | ||
freshnessPeriod_ = 0; | ||
@@ -123,13 +123,15 @@ finalBlockId; | ||
TT.MetaInfo, Encoder.OmitEmpty, | ||
contentType > 0 ? [TT.ContentType, NNI(contentType)] : undefined, | ||
freshnessPeriod > 0 ? [TT.FreshnessPeriod, NNI(freshnessPeriod)] : undefined, | ||
contentType > 0 && [TT.ContentType, NNI(contentType)], | ||
freshnessPeriod > 0 && [TT.FreshnessPeriod, NNI(freshnessPeriod)], | ||
finalBlockId && [TT.FinalBlock, finalBlockId], | ||
], | ||
content.byteLength > 0 ? [TT.Content, content] : undefined, | ||
content.length > 0 && [TT.Content, content], | ||
sigInfo.encodeAs(TT.DSigInfo), | ||
]; | ||
} | ||
/** Return the implicit digest if it's already computed. */ | ||
getImplicitDigest() { | ||
return this[FIELDS].topTlvDigest; | ||
} | ||
/** Compute the implicit digest. */ | ||
async computeImplicitDigest() { | ||
@@ -147,2 +149,3 @@ let digest = this.getImplicitDigest(); | ||
} | ||
/** Return the full name if the implicit digest is already computed. */ | ||
getFullName() { | ||
@@ -155,2 +158,3 @@ const digest = this.getImplicitDigest(); | ||
} | ||
/** Compute the full name (name plus implicit digest). */ | ||
async computeFullName() { | ||
@@ -162,6 +166,8 @@ await this.computeImplicitDigest(); | ||
* Determine if a Data can satisfy an Interest. | ||
* @param isCacheLookup if true, Data with zero FreshnessPeriod cannot satisfy Interest with MustBeFresh; | ||
* if false, this check does not apply. | ||
* @returns a Promise that will be resolved with the result. | ||
*/ | ||
async canSatisfy(interest) { | ||
if (interest.mustBeFresh && this.freshnessPeriod <= 0) { | ||
async canSatisfy(interest, { isCacheLookup = false } = {}) { | ||
if (isCacheLookup && interest.mustBeFresh && this.freshnessPeriod <= 0) { | ||
return false; | ||
@@ -168,0 +174,0 @@ } |
import { type Decoder, Encoder } from "@ndn/tlv"; | ||
import type { Except } from "type-fest"; | ||
import { FIELDS } from "./impl-public-fields.js"; | ||
import type { Interest } from "./interest.js"; | ||
import { type NameLike, Component, Name } from "./name/mod.js"; | ||
import { type Signer, type Verifier, LLSign, LLVerify } from "./security/signing.js"; | ||
import { Component, Name, type NameLike } from "./name/mod.js"; | ||
import { LLSign, LLVerify, type Signer, type Verifier } from "./security/signing.js"; | ||
import { SigInfo } from "./sig-info.js"; | ||
@@ -33,3 +34,3 @@ declare class Fields { | ||
} | ||
interface PublicFields extends Omit<Fields, "signedPortion" | "topTlv" | "topTlvDigest"> { | ||
interface PublicFields extends Except<Fields, "signedPortion" | "topTlv" | "topTlvDigest"> { | ||
} | ||
@@ -54,11 +55,17 @@ /** Data packet. */ | ||
private encodeSignedPortion; | ||
/** Return the implicit digest if it's already computed. */ | ||
getImplicitDigest(): Uint8Array | undefined; | ||
/** Compute the implicit digest. */ | ||
computeImplicitDigest(): Promise<Uint8Array>; | ||
/** Return the full name if the implicit digest is already computed. */ | ||
getFullName(): Name | undefined; | ||
/** Compute the full name (name plus implicit digest). */ | ||
computeFullName(): Promise<Name>; | ||
/** | ||
* Determine if a Data can satisfy an Interest. | ||
* @param isCacheLookup if true, Data with zero FreshnessPeriod cannot satisfy Interest with MustBeFresh; | ||
* if false, this check does not apply. | ||
* @returns a Promise that will be resolved with the result. | ||
*/ | ||
canSatisfy(interest: Interest): Promise<boolean>; | ||
canSatisfy(interest: Interest, { isCacheLookup }?: Data.CanSatisfyOptions): Promise<boolean>; | ||
[LLSign.OP](sign: LLSign): Promise<void>; | ||
@@ -82,3 +89,13 @@ [LLVerify.OP](verify: LLVerify): Promise<void>; | ||
type CtorArg = NameLike | CtorTag | typeof FinalBlock | Uint8Array; | ||
/** Data.canSatisfy options. */ | ||
interface CanSatisfyOptions { | ||
/** | ||
* Whether the Interest-Data matching is in the context of cache lookup. | ||
* If true, Data with zero FreshnessPeriod cannot satisfy Interest with MustBeFresh. | ||
* If false, this check does not apply. | ||
* @default false | ||
*/ | ||
isCacheLookup?: boolean; | ||
} | ||
} | ||
export {}; |
import { type Decoder, Encoder } from "@ndn/tlv"; | ||
import { type NameLike, Name } from "./name/mod.js"; | ||
import { Name, type NameLike } from "./name/mod.js"; | ||
/** ForwardingHint in Interest. */ | ||
@@ -4,0 +4,0 @@ export declare class FwHint { |
import { Encoder, EvDecoder, NNI } from "@ndn/tlv"; | ||
import { assert, sha256 } from "@ndn/util"; | ||
import { assert, constrain, sha256 } from "@ndn/util"; | ||
import { TT } from "./an_browser.js"; | ||
@@ -38,9 +38,9 @@ import { FwHint } from "./fwhint_browser.js"; | ||
get nonce() { return this.nonce_; } | ||
set nonce(v) { this.nonce_ = v && NNI.constrain(v, "Nonce", 0xFFFFFFFF); } | ||
set nonce(v) { this.nonce_ = v && constrain(v, "Nonce", 0xFFFFFFFF); } | ||
nonce_; | ||
get lifetime() { return this.lifetime_; } | ||
set lifetime(v) { this.lifetime_ = NNI.constrain(v, "InterestLifetime"); } | ||
set lifetime(v) { this.lifetime_ = constrain(Math.trunc(v), "InterestLifetime"); } | ||
lifetime_ = Interest.DefaultLifetime; | ||
get hopLimit() { return this.hopLimit_; } | ||
set hopLimit(v) { this.hopLimit_ = NNI.constrain(v, "HopLimit", HOPLIMIT_MAX); } | ||
set hopLimit(v) { this.hopLimit_ = constrain(v, "HopLimit", HOPLIMIT_MAX); } | ||
hopLimit_ = HOPLIMIT_MAX; | ||
@@ -47,0 +47,0 @@ appParameters; |
import { Encoder, EvDecoder, NNI } from "@ndn/tlv"; | ||
import { assert, sha256 } from "@ndn/util"; | ||
import { assert, constrain, sha256 } from "@ndn/util"; | ||
import { TT } from "./an_node.js"; | ||
@@ -38,9 +38,9 @@ import { FwHint } from "./fwhint_node.js"; | ||
get nonce() { return this.nonce_; } | ||
set nonce(v) { this.nonce_ = v && NNI.constrain(v, "Nonce", 0xFFFFFFFF); } | ||
set nonce(v) { this.nonce_ = v && constrain(v, "Nonce", 0xFFFFFFFF); } | ||
nonce_; | ||
get lifetime() { return this.lifetime_; } | ||
set lifetime(v) { this.lifetime_ = NNI.constrain(v, "InterestLifetime"); } | ||
set lifetime(v) { this.lifetime_ = constrain(Math.trunc(v), "InterestLifetime"); } | ||
lifetime_ = Interest.DefaultLifetime; | ||
get hopLimit() { return this.hopLimit_; } | ||
set hopLimit(v) { this.hopLimit_ = NNI.constrain(v, "HopLimit", HOPLIMIT_MAX); } | ||
set hopLimit(v) { this.hopLimit_ = constrain(v, "HopLimit", HOPLIMIT_MAX); } | ||
hopLimit_ = HOPLIMIT_MAX; | ||
@@ -47,0 +47,0 @@ appParameters; |
import { type Decoder, Encoder } from "@ndn/tlv"; | ||
import type { Except } from "type-fest"; | ||
import { FwHint } from "./fwhint.js"; | ||
import { FIELDS } from "./impl-public-fields.js"; | ||
import { type NameLike, Name } from "./name/mod.js"; | ||
import { type Signer, type Verifier, LLSign, LLVerify } from "./security/signing.js"; | ||
import { Name, type NameLike } from "./name/mod.js"; | ||
import { LLSign, LLVerify, type Signer, type Verifier } from "./security/signing.js"; | ||
import { SigInfo } from "./sig-info.js"; | ||
@@ -28,3 +29,3 @@ declare class Fields { | ||
} | ||
interface PublicFields extends Omit<Fields, "paramsPortion" | "signedPortion"> { | ||
interface PublicFields extends Except<Fields, "paramsPortion" | "signedPortion"> { | ||
} | ||
@@ -31,0 +32,0 @@ /** Interest packet. */ |
@@ -44,8 +44,9 @@ import { Encoder, EvDecoder } from "@ndn/tlv"; | ||
function mustGetName(kl) { | ||
if (!kl || !kl.name) { | ||
const name = kl?.name; | ||
if (!name) { | ||
throw new Error("KeyLocator does not have name"); | ||
} | ||
return kl.name; | ||
return name; | ||
} | ||
KeyLocator.mustGetName = mustGetName; | ||
})(KeyLocator || (KeyLocator = {})); |
@@ -44,8 +44,9 @@ import { Encoder, EvDecoder } from "@ndn/tlv"; | ||
function mustGetName(kl) { | ||
if (!kl || !kl.name) { | ||
const name = kl?.name; | ||
if (!name) { | ||
throw new Error("KeyLocator does not have name"); | ||
} | ||
return kl.name; | ||
return name; | ||
} | ||
KeyLocator.mustGetName = mustGetName; | ||
})(KeyLocator || (KeyLocator = {})); |
import { type Decoder, Encoder } from "@ndn/tlv"; | ||
import { type NameLike, Name } from "./name/mod.js"; | ||
import { Name, type NameLike } from "./name/mod.js"; | ||
/** KeyLocator in SigInfo. */ | ||
@@ -4,0 +4,0 @@ export declare class KeyLocator { |
import { EvDecoder, NNI } from "@ndn/tlv"; | ||
import { constrain } from "@ndn/util"; | ||
import { NackReason, TT } from "./an_browser.js"; | ||
@@ -8,3 +9,3 @@ const EVD = new EvDecoder("NackHeader", TT.Nack) | ||
get reason() { return this.reason_; } | ||
set reason(v) { this.reason_ = NNI.constrain(v, "Reason"); } | ||
set reason(v) { this.reason_ = constrain(v, "NackReason"); } | ||
reason_ = 0; | ||
@@ -11,0 +12,0 @@ static decodeFrom(decoder) { |
import { EvDecoder, NNI } from "@ndn/tlv"; | ||
import { constrain } from "@ndn/util"; | ||
import { NackReason, TT } from "./an_node.js"; | ||
@@ -8,3 +9,3 @@ const EVD = new EvDecoder("NackHeader", TT.Nack) | ||
get reason() { return this.reason_; } | ||
set reason(v) { this.reason_ = NNI.constrain(v, "Reason"); } | ||
set reason(v) { this.reason_ = constrain(v, "NackReason"); } | ||
reason_ = 0; | ||
@@ -11,0 +12,0 @@ static decodeFrom(decoder) { |
import { __importDefault, __importStar } from "tslib"; | ||
import { Decoder, Encoder } from "@ndn/tlv"; | ||
import { fromHex, fromUtf8, toHex, toUtf8 } from "@ndn/util"; | ||
import { constrain, fromHex, fromUtf8, toHex, toUtf8 } from "@ndn/util"; | ||
import _cjsDefaultImport0 from "buffer-compare"; const bufferCompare = __importDefault(_cjsDefaultImport0).default; | ||
import { TT } from "../an_browser.js"; | ||
function checkType(t) { | ||
return Number.isInteger(t) && t >= 0x01 && t <= 0xFFFF; | ||
} | ||
function assertType(t) { | ||
if (!checkType(t)) { | ||
throw new Error(`Component TLV-TYPE ${t} out of range`); | ||
} | ||
return constrain(t, "Component TLV-TYPE", 0x01, 0xFFFF); | ||
} | ||
@@ -21,2 +16,4 @@ const CHAR_ENCODE = {}; | ||
const CODEPOINT_PERIOD = ".".codePointAt(0); | ||
const encoderHeadroom = 10; | ||
const FROM = Symbol("Component.from"); | ||
/** | ||
@@ -40,9 +37,11 @@ * Name component. | ||
if (posEqual >= 1) { | ||
const iType = Number.parseInt(input, 10); | ||
if (checkType(iType)) { | ||
type = iType; | ||
try { | ||
type = assertType(Number.parseInt(input, 10)); | ||
posValue = posEqual + 1; | ||
} | ||
catch { } | ||
} | ||
const value = new Uint8Array(input.length - posValue); | ||
const maxLength = input.length - posValue; | ||
const encoder = new Encoder(encoderHeadroom + maxLength); | ||
const value = encoder.prependRoom(maxLength); | ||
let length = 0; | ||
@@ -65,3 +64,3 @@ let hasNonPeriods = false; | ||
} | ||
return new Component(type, value.subarray(0, length)); | ||
return new Component(type, FROM, encoder, length); | ||
} | ||
@@ -82,3 +81,3 @@ /** Whole TLV. */ | ||
} | ||
constructor(arg1 = TT.GenericNameComponent, value = new Uint8Array()) { | ||
constructor(arg1 = TT.GenericNameComponent, value, encoder, length) { | ||
if (arg1 instanceof Uint8Array) { | ||
@@ -88,10 +87,21 @@ this.tlv = arg1; | ||
({ type: this.type, value: this.value } = decoder.read()); | ||
decoder.throwUnlessEof(); | ||
} | ||
else { | ||
this.type = arg1; | ||
if (typeof value === "string") { | ||
value = toUtf8(value); | ||
let tailroom = 0; | ||
if (value === FROM) { | ||
tailroom = encoder.size - length; | ||
} | ||
this.tlv = Encoder.encode([this.type, value], 10 + value.length); | ||
this.value = this.tlv.subarray(this.tlv.length - value.length); | ||
else { | ||
if (typeof value === "string") { | ||
value = toUtf8(value); | ||
} | ||
length = value?.length ?? 0; | ||
encoder = new Encoder(encoderHeadroom + length); | ||
encoder.encode(value); | ||
} | ||
this.value = encoder.slice(0, length); | ||
encoder?.prependTypeLength(this.type, length); | ||
this.tlv = encoder.slice(0, encoder.size - tailroom); | ||
} | ||
@@ -98,0 +108,0 @@ assertType(this.type); |
import { __importDefault, __importStar } from "tslib"; | ||
import { Decoder, Encoder } from "@ndn/tlv"; | ||
import { fromHex, fromUtf8, toHex, toUtf8 } from "@ndn/util"; | ||
import { constrain, fromHex, fromUtf8, toHex, toUtf8 } from "@ndn/util"; | ||
import _cjsDefaultImport0 from "buffer-compare"; const bufferCompare = __importDefault(_cjsDefaultImport0).default; | ||
import { TT } from "../an_node.js"; | ||
function checkType(t) { | ||
return Number.isInteger(t) && t >= 0x01 && t <= 0xFFFF; | ||
} | ||
function assertType(t) { | ||
if (!checkType(t)) { | ||
throw new Error(`Component TLV-TYPE ${t} out of range`); | ||
} | ||
return constrain(t, "Component TLV-TYPE", 0x01, 0xFFFF); | ||
} | ||
@@ -21,2 +16,4 @@ const CHAR_ENCODE = {}; | ||
const CODEPOINT_PERIOD = ".".codePointAt(0); | ||
const encoderHeadroom = 10; | ||
const FROM = Symbol("Component.from"); | ||
/** | ||
@@ -40,9 +37,11 @@ * Name component. | ||
if (posEqual >= 1) { | ||
const iType = Number.parseInt(input, 10); | ||
if (checkType(iType)) { | ||
type = iType; | ||
try { | ||
type = assertType(Number.parseInt(input, 10)); | ||
posValue = posEqual + 1; | ||
} | ||
catch { } | ||
} | ||
const value = new Uint8Array(input.length - posValue); | ||
const maxLength = input.length - posValue; | ||
const encoder = new Encoder(encoderHeadroom + maxLength); | ||
const value = encoder.prependRoom(maxLength); | ||
let length = 0; | ||
@@ -65,3 +64,3 @@ let hasNonPeriods = false; | ||
} | ||
return new Component(type, value.subarray(0, length)); | ||
return new Component(type, FROM, encoder, length); | ||
} | ||
@@ -82,3 +81,3 @@ /** Whole TLV. */ | ||
} | ||
constructor(arg1 = TT.GenericNameComponent, value = new Uint8Array()) { | ||
constructor(arg1 = TT.GenericNameComponent, value, encoder, length) { | ||
if (arg1 instanceof Uint8Array) { | ||
@@ -88,10 +87,21 @@ this.tlv = arg1; | ||
({ type: this.type, value: this.value } = decoder.read()); | ||
decoder.throwUnlessEof(); | ||
} | ||
else { | ||
this.type = arg1; | ||
if (typeof value === "string") { | ||
value = toUtf8(value); | ||
let tailroom = 0; | ||
if (value === FROM) { | ||
tailroom = encoder.size - length; | ||
} | ||
this.tlv = Encoder.encode([this.type, value], 10 + value.length); | ||
this.value = this.tlv.subarray(this.tlv.length - value.length); | ||
else { | ||
if (typeof value === "string") { | ||
value = toUtf8(value); | ||
} | ||
length = value?.length ?? 0; | ||
encoder = new Encoder(encoderHeadroom + length); | ||
encoder.encode(value); | ||
} | ||
this.value = encoder.slice(0, length); | ||
encoder?.prependTypeLength(this.type, length); | ||
this.tlv = encoder.slice(0, encoder.size - tailroom); | ||
} | ||
@@ -98,0 +108,0 @@ assertType(this.type); |
import { Decoder, Encoder } from "@ndn/tlv"; | ||
import type { NamingConvention } from "./convention.js"; | ||
declare const FROM: unique symbol; | ||
/** Name component or component URI. */ | ||
@@ -32,2 +33,3 @@ export type ComponentLike = Component | string; | ||
constructor(tlv: Uint8Array); | ||
constructor(type: number, isFrom: typeof FROM, encoder: Encoder, length: number); | ||
/** Get URI string. */ | ||
@@ -56,1 +58,2 @@ toString(): string; | ||
} | ||
export {}; |
@@ -8,1 +8,2 @@ export * from "./algo_browser.js"; | ||
export * from "./name-map_browser.js"; | ||
export * from "./struct-field_browser.js"; |
@@ -8,1 +8,2 @@ export * from "./algo_node.js"; | ||
export * from "./name-map_node.js"; | ||
export * from "./struct-field_node.js"; |
@@ -8,1 +8,2 @@ export * from "./algo.js"; | ||
export * from "./name-map.js"; | ||
export * from "./struct-field.js"; |
@@ -15,36 +15,31 @@ import { Decoder, Encoder } from "@ndn/tlv"; | ||
/** List of name components. */ | ||
comps; | ||
comps = []; | ||
constructor(arg1, parseComponent = Component.from) { | ||
switch (true) { | ||
case arg1 instanceof Name: { | ||
const other = arg1; | ||
this.comps = other.comps; | ||
this.value_ = other.value_; | ||
break; | ||
} | ||
case typeof arg1 === "string": { | ||
const uri = arg1; | ||
this.comps = uri.replace(/^(?:ndn:)?\/*/, "").split("/") | ||
.filter((comp) => comp !== "").map(parseComponent); | ||
this.valueEncoderBufSize = uri.length + 4 * this.comps.length; | ||
break; | ||
} | ||
case Array.isArray(arg1): | ||
this.comps = Array.from(arg1, Component.from); | ||
break; | ||
case arg1 instanceof Uint8Array: { | ||
this.value_ = arg1; | ||
const comps = []; | ||
const decoder = new Decoder(this.value_); | ||
while (!decoder.eof) { | ||
comps.push(decoder.decode(Component)); | ||
if (arg1 === undefined) { | ||
this.valueEncoderBufSize = 0; | ||
} | ||
else if (arg1 instanceof Name) { | ||
this.comps = arg1.comps; | ||
this.value_ = arg1.value_; | ||
this.uri_ = arg1.uri_; | ||
this.hex_ = arg1.hex_; | ||
} | ||
else if (typeof arg1 === "string") { | ||
for (const comp of arg1.replace(/^(?:ndn:)?\/*/, "").split("/")) { | ||
if (comp !== "") { | ||
this.comps.push(parseComponent(comp)); | ||
} | ||
this.comps = comps; | ||
break; | ||
} | ||
default: // undefined | ||
this.comps = []; | ||
this.valueEncoderBufSize = 0; | ||
break; | ||
this.valueEncoderBufSize = arg1.length + 4 * this.comps.length; | ||
} | ||
else if (Array.isArray(arg1)) { | ||
this.comps = Array.from(arg1, Component.from); | ||
} | ||
else if (arg1 instanceof Uint8Array) { | ||
this.value_ = arg1; | ||
const decoder = new Decoder(this.value_); | ||
while (!decoder.eof) { | ||
this.comps.push(decoder.decode(Component)); | ||
} | ||
} | ||
} | ||
@@ -61,5 +56,3 @@ valueEncoderBufSize; | ||
get value() { | ||
if (!this.value_) { | ||
this.value_ = Encoder.encode(this.comps, this.valueEncoderBufSize ?? 256); | ||
} | ||
this.value_ ??= Encoder.encode(this.comps, this.valueEncoderBufSize ?? 256); | ||
return this.value_; | ||
@@ -102,7 +95,10 @@ } | ||
append(...args) { | ||
let suffix; | ||
if (args.length === 2 && | ||
typeof args[0].create === "function") { | ||
return this.append(args[0].create(args[1])); | ||
suffix = [args[0].create(args[1])]; | ||
} | ||
const suffix = args; | ||
else { | ||
suffix = args; | ||
} | ||
return new Name([...this.comps, ...suffix]); | ||
@@ -164,2 +160,3 @@ } | ||
(function (Name) { | ||
/** Determine if obj is Name or Name URI. */ | ||
function isNameLike(obj) { | ||
@@ -169,2 +166,6 @@ return obj instanceof Name || typeof obj === "string"; | ||
Name.isNameLike = isNameLike; | ||
/** | ||
* Create Name from Name or Name URI. | ||
* This is more efficient than new Name(input) if input is already a Name. | ||
*/ | ||
function from(input) { | ||
@@ -171,0 +172,0 @@ return input instanceof Name ? input : new Name(input); |
@@ -15,36 +15,31 @@ import { Decoder, Encoder } from "@ndn/tlv"; | ||
/** List of name components. */ | ||
comps; | ||
comps = []; | ||
constructor(arg1, parseComponent = Component.from) { | ||
switch (true) { | ||
case arg1 instanceof Name: { | ||
const other = arg1; | ||
this.comps = other.comps; | ||
this.value_ = other.value_; | ||
break; | ||
} | ||
case typeof arg1 === "string": { | ||
const uri = arg1; | ||
this.comps = uri.replace(/^(?:ndn:)?\/*/, "").split("/") | ||
.filter((comp) => comp !== "").map(parseComponent); | ||
this.valueEncoderBufSize = uri.length + 4 * this.comps.length; | ||
break; | ||
} | ||
case Array.isArray(arg1): | ||
this.comps = Array.from(arg1, Component.from); | ||
break; | ||
case arg1 instanceof Uint8Array: { | ||
this.value_ = arg1; | ||
const comps = []; | ||
const decoder = new Decoder(this.value_); | ||
while (!decoder.eof) { | ||
comps.push(decoder.decode(Component)); | ||
if (arg1 === undefined) { | ||
this.valueEncoderBufSize = 0; | ||
} | ||
else if (arg1 instanceof Name) { | ||
this.comps = arg1.comps; | ||
this.value_ = arg1.value_; | ||
this.uri_ = arg1.uri_; | ||
this.hex_ = arg1.hex_; | ||
} | ||
else if (typeof arg1 === "string") { | ||
for (const comp of arg1.replace(/^(?:ndn:)?\/*/, "").split("/")) { | ||
if (comp !== "") { | ||
this.comps.push(parseComponent(comp)); | ||
} | ||
this.comps = comps; | ||
break; | ||
} | ||
default: // undefined | ||
this.comps = []; | ||
this.valueEncoderBufSize = 0; | ||
break; | ||
this.valueEncoderBufSize = arg1.length + 4 * this.comps.length; | ||
} | ||
else if (Array.isArray(arg1)) { | ||
this.comps = Array.from(arg1, Component.from); | ||
} | ||
else if (arg1 instanceof Uint8Array) { | ||
this.value_ = arg1; | ||
const decoder = new Decoder(this.value_); | ||
while (!decoder.eof) { | ||
this.comps.push(decoder.decode(Component)); | ||
} | ||
} | ||
} | ||
@@ -61,5 +56,3 @@ valueEncoderBufSize; | ||
get value() { | ||
if (!this.value_) { | ||
this.value_ = Encoder.encode(this.comps, this.valueEncoderBufSize ?? 256); | ||
} | ||
this.value_ ??= Encoder.encode(this.comps, this.valueEncoderBufSize ?? 256); | ||
return this.value_; | ||
@@ -102,7 +95,10 @@ } | ||
append(...args) { | ||
let suffix; | ||
if (args.length === 2 && | ||
typeof args[0].create === "function") { | ||
return this.append(args[0].create(args[1])); | ||
suffix = [args[0].create(args[1])]; | ||
} | ||
const suffix = args; | ||
else { | ||
suffix = args; | ||
} | ||
return new Name([...this.comps, ...suffix]); | ||
@@ -164,2 +160,3 @@ } | ||
(function (Name) { | ||
/** Determine if obj is Name or Name URI. */ | ||
function isNameLike(obj) { | ||
@@ -169,2 +166,6 @@ return obj instanceof Name || typeof obj === "string"; | ||
Name.isNameLike = isNameLike; | ||
/** | ||
* Create Name from Name or Name URI. | ||
* This is more efficient than new Name(input) if input is already a Name. | ||
*/ | ||
function from(input) { | ||
@@ -171,0 +172,0 @@ return input instanceof Name ? input : new Name(input); |
import { Decoder, Encoder } from "@ndn/tlv"; | ||
import { type ComponentLike, Component } from "./component.js"; | ||
import { Component, type ComponentLike } from "./component.js"; | ||
import type { NamingConvention } from "./convention.js"; | ||
@@ -61,3 +61,8 @@ /** Name or Name URI. */ | ||
export declare namespace Name { | ||
/** Determine if obj is Name or Name URI. */ | ||
function isNameLike(obj: any): obj is NameLike; | ||
/** | ||
* Create Name from Name or Name URI. | ||
* This is more efficient than new Name(input) if input is already a Name. | ||
*/ | ||
function from(input: NameLike): Name; | ||
@@ -64,0 +69,0 @@ /** Name compare result. */ |
import { __importDefault, __importStar } from "tslib"; | ||
import { assert, toHex } from "@ndn/util"; | ||
import { assert, evict, toHex } from "@ndn/util"; | ||
import _cjsDefaultImport0 from "mnemonist/default-weak-map.js"; const DefaultWeakMap = __importDefault(_cjsDefaultImport0).default; | ||
@@ -7,11 +7,2 @@ import { Interest } from "../interest_browser.js"; | ||
import { LLSign, LLVerify, Signer } from "./signing_browser.js"; | ||
function evict(capacity, container) { | ||
assert(capacity >= 0); | ||
for (const key of container.keys()) { | ||
if (container.size <= capacity) { | ||
break; | ||
} | ||
container.delete(key); | ||
} | ||
} | ||
/** Validation policy for SigInfo fields in signed Interest. */ | ||
@@ -25,3 +16,3 @@ export class SignedInterestPolicy { | ||
let opts = {}; | ||
if (typeof arg1.check === "function") { | ||
if (typeof arg1?.check === "function") { | ||
rules.unshift(arg1); | ||
@@ -32,4 +23,4 @@ } | ||
} | ||
const { trackedKeys = 256, } = opts; | ||
this.trackedKeys = trackedKeys; | ||
assert(rules.length > 0, "no rules"); | ||
this.trackedKeys = opts.trackedKeys ?? 256; | ||
this.rules = rules; | ||
@@ -85,3 +76,3 @@ } | ||
switch (prop) { | ||
case LLSign.OP: | ||
case LLSign.OP: { | ||
return (signer) => { | ||
@@ -91,3 +82,4 @@ this.update(interest); | ||
}; | ||
case LLVerify.OP: | ||
} | ||
case LLVerify.OP: { | ||
return async (verify) => { | ||
@@ -98,2 +90,3 @@ const save = this.check(interest); | ||
}; | ||
} | ||
} | ||
@@ -232,3 +225,3 @@ return Reflect.get(target, prop, receiver); | ||
* | ||
* This rule assigns a random SigNonce of `minNonceLength` octets that does not duplicate | ||
* This rule assigns a random SigNonce of `nonceLength` octets that does not duplicate | ||
* last `trackedNonces` values. | ||
@@ -235,0 +228,0 @@ * |
import { __importDefault, __importStar } from "tslib"; | ||
import { assert, toHex } from "@ndn/util"; | ||
import { assert, evict, toHex } from "@ndn/util"; | ||
import _cjsDefaultImport0 from "mnemonist/default-weak-map.js"; const DefaultWeakMap = __importDefault(_cjsDefaultImport0).default; | ||
@@ -7,11 +7,2 @@ import { Interest } from "../interest_node.js"; | ||
import { LLSign, LLVerify, Signer } from "./signing_node.js"; | ||
function evict(capacity, container) { | ||
assert(capacity >= 0); | ||
for (const key of container.keys()) { | ||
if (container.size <= capacity) { | ||
break; | ||
} | ||
container.delete(key); | ||
} | ||
} | ||
/** Validation policy for SigInfo fields in signed Interest. */ | ||
@@ -25,3 +16,3 @@ export class SignedInterestPolicy { | ||
let opts = {}; | ||
if (typeof arg1.check === "function") { | ||
if (typeof arg1?.check === "function") { | ||
rules.unshift(arg1); | ||
@@ -32,4 +23,4 @@ } | ||
} | ||
const { trackedKeys = 256, } = opts; | ||
this.trackedKeys = trackedKeys; | ||
assert(rules.length > 0, "no rules"); | ||
this.trackedKeys = opts.trackedKeys ?? 256; | ||
this.rules = rules; | ||
@@ -85,3 +76,3 @@ } | ||
switch (prop) { | ||
case LLSign.OP: | ||
case LLSign.OP: { | ||
return (signer) => { | ||
@@ -91,3 +82,4 @@ this.update(interest); | ||
}; | ||
case LLVerify.OP: | ||
} | ||
case LLVerify.OP: { | ||
return async (verify) => { | ||
@@ -98,2 +90,3 @@ const save = this.check(interest); | ||
}; | ||
} | ||
} | ||
@@ -232,3 +225,3 @@ return Reflect.get(target, prop, receiver); | ||
* | ||
* This rule assigns a random SigNonce of `minNonceLength` octets that does not duplicate | ||
* This rule assigns a random SigNonce of `nonceLength` octets that does not duplicate | ||
* last `trackedNonces` values. | ||
@@ -235,0 +228,0 @@ * |
import { Interest } from "../interest.js"; | ||
import { SigInfo } from "../sig-info.js"; | ||
import { type Verifier, Signer } from "./signing.js"; | ||
import { Signer, type Verifier } from "./signing.js"; | ||
/** Validation policy for SigInfo fields in signed Interest. */ | ||
@@ -101,3 +101,3 @@ export declare class SignedInterestPolicy { | ||
* | ||
* This rule assigns a random SigNonce of `minNonceLength` octets that does not duplicate | ||
* This rule assigns a random SigNonce of `nonceLength` octets that does not duplicate | ||
* last `trackedNonces` values. | ||
@@ -115,3 +115,3 @@ * | ||
* | ||
* Minimum is 0. Setting to 0 is generally a bad idea because it would require consumer and | ||
* Minimum is 0. However, setting to 0 is inadvisable because it would require consumer and | ||
* producer to have precisely synchronized clocks. | ||
@@ -118,0 +118,0 @@ * @default 60000 |
@@ -40,5 +40,3 @@ import { type Decoder, type EncodableObj, Extensible, ExtensionRegistry } from "@ndn/tlv"; | ||
function Time(v?: number): CtorTag; | ||
function SeqNum(v: bigint): { | ||
[ctorAssign](si: SigInfo): void; | ||
}; | ||
function SeqNum(v: bigint): CtorTag; | ||
type CtorArg = SigInfo | number | KeyLocator.CtorArg | CtorTag; | ||
@@ -45,0 +43,0 @@ const registerExtension: <R>(ext: import("@ndn/tlv").Extension<SigInfo, R>) => void; |
{ | ||
"name": "@ndn/packet", | ||
"version": "0.0.20230121", | ||
"version": "0.0.20240113", | ||
"description": "NDNts: Network Layer Packets", | ||
@@ -25,9 +25,10 @@ "keywords": [ | ||
"dependencies": { | ||
"@ndn/tlv": "0.0.20230121", | ||
"@ndn/util": "0.0.20230121", | ||
"@ndn/tlv": "0.0.20240113", | ||
"@ndn/util": "0.0.20240113", | ||
"buffer-compare": "^1.1.1", | ||
"mnemonist": "^0.39.5", | ||
"tslib": "^2.4.1" | ||
"mnemonist": "^0.39.7", | ||
"tslib": "^2.6.2", | ||
"type-fest": "^4.9.0" | ||
}, | ||
"types": "lib/mod.d.ts" | ||
} |
@@ -5,3 +5,3 @@ # @ndn/packet | ||
This package implements **Name**, **Interest**, and **Data** types as specified in [NDN Packet Format v0.3](https://named-data.net/doc/NDN-packet-spec/0.3/). | ||
This package implements **Name**, **Interest**, and **Data** types as specified in [NDN Packet Format v0.3](https://docs.named-data.net/NDN-packet-spec/0.3/). | ||
@@ -14,3 +14,3 @@ ```ts | ||
import { fromUtf8, toUtf8 } from "@ndn/util"; | ||
import { strict as assert } from "node:assert"; | ||
import assert from "node:assert/strict"; | ||
``` | ||
@@ -155,3 +155,3 @@ | ||
const interestWire = Encoder.encode(interest); | ||
const interest2 = new Decoder(interestWire).decode(Interest); | ||
const interest2 = Decoder.decode(interestWire, Interest); | ||
assert.equal(interest2.name.toString(), "/8=A"); | ||
@@ -179,3 +179,3 @@ | ||
// And then decode it. | ||
const data2 = new Decoder(dataWire).decode(Data); | ||
const data2 = Decoder.decode(dataWire, Data); | ||
@@ -226,5 +226,5 @@ // Data signature should be verified. | ||
assert.equal(await data.canSatisfy(interest3), false); | ||
const data3 = new Decoder(dataWire).decode(Data); | ||
const data3 = Decoder.decode(dataWire, Data); | ||
const interestWithFullName = new Interest(fullName); | ||
assert.equal(await data.canSatisfy(interestWithFullName), true); | ||
``` |
@@ -6,3 +6,3 @@ import { TT, Name, Component, ImplicitDigest, AltUri, Interest, Data, digestSigning } from "@ndn/packet"; | ||
import { fromUtf8, toUtf8 } from "@ndn/util"; | ||
import { strict as assert } from "node:assert"; | ||
import assert from "node:assert/strict"; | ||
@@ -135,3 +135,3 @@ // Name components are immutable. Once it's created, you can never change it. | ||
const interestWire = Encoder.encode(interest); | ||
const interest2 = new Decoder(interestWire).decode(Interest); | ||
const interest2 = Decoder.decode(interestWire, Interest); | ||
assert.equal(interest2.name.toString(), "/8=A"); | ||
@@ -155,3 +155,3 @@ | ||
// And then decode it. | ||
const data2 = new Decoder(dataWire).decode(Data); | ||
const data2 = Decoder.decode(dataWire, Data); | ||
@@ -194,4 +194,4 @@ // Data signature should be verified. | ||
assert.equal(await data.canSatisfy(interest3), false); | ||
const data3 = new Decoder(dataWire).decode(Data); | ||
const data3 = Decoder.decode(dataWire, Data); | ||
const interestWithFullName = new Interest(fullName); | ||
assert.equal(await data.canSatisfy(interestWithFullName), true); |
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
169927
67
4515
6
+ Addedtype-fest@^4.9.0
+ Added@ndn/tlv@0.0.20240113(transitive)
+ Added@ndn/util@0.0.20240113(transitive)
+ Addedstreaming-iterables@8.0.1(transitive)
+ Addedtype-fest@4.27.1(transitive)
- Removed@ndn/tlv@0.0.20230121(transitive)
- Removed@ndn/util@0.0.20230121(transitive)
Updated@ndn/tlv@0.0.20240113
Updated@ndn/util@0.0.20240113
Updatedmnemonist@^0.39.7
Updatedtslib@^2.6.2