@ndn/packet
Version:
NDNts: Network Layer Packets
65 lines (64 loc) • 2.19 kB
JavaScript
import { fromHex, toHex } from "@ndn/util";
import { TT } from "../an_browser.js";
import { Component } from "./component_browser.js";
const DIGEST_LENGTH = 32;
class DigestComp {
tt;
altUriPrefix;
altUriRegex;
constructor(tt, altUriPrefix) {
this.tt = tt;
this.altUriPrefix = altUriPrefix;
this.altUriRegex = new RegExp(`^${altUriPrefix}=([0-9a-fA-F]{${2 * DIGEST_LENGTH}})$`);
}
match(comp) {
return comp.type === this.tt && comp.length === DIGEST_LENGTH;
}
create(v) {
if (v.length !== DIGEST_LENGTH) {
throw new Error(`digest length must be ${DIGEST_LENGTH}`);
}
return new Component(this.tt, v);
}
parse(comp) {
return comp.value;
}
toAltUri(comp) {
return `${this.altUriPrefix}=${toHex(comp.value).toLowerCase()}`;
}
fromAltUri(input) {
const m = this.altUriRegex.exec(input);
if (!m) {
return undefined;
}
return new Component(this.tt, fromHex(m[1]));
}
}
class ImplicitDigestComp extends DigestComp {
/** Remove ImplicitDigest if present at last component. */
strip(name) {
if (name.get(-1)?.is(this)) {
return name.getPrefix(-1);
}
return name;
}
}
/** ImplicitSha256DigestComponent. */
export const ImplicitDigest = new ImplicitDigestComp(TT.ImplicitSha256DigestComponent, "sha256digest");
class ParamsDigestPlaceHolder extends Component {
}
class ParamsDigestComp extends DigestComp {
/** ParamsDigest placeholder during Interest encoding. */
PLACEHOLDER = new ParamsDigestPlaceHolder(this.tt, "placeholder");
/** Determine if comp is a ParamsDigest placeholder. */
isPlaceholder(comp) {
return comp instanceof ParamsDigestPlaceHolder;
}
/** Find ParamsDigest or placeholder in name. */
findIn(name, matchPlaceholder = true) {
return name.comps.findIndex((comp) => this.match(comp) ||
(matchPlaceholder && this.isPlaceholder(comp)));
}
}
/** ParametersSha256DigestComponent */
export const ParamsDigest = new ParamsDigestComp(TT.ParametersSha256DigestComponent, "params-sha256");