@ndn/nfdmgmt
This package is part of NDNts, Named Data Networking libraries for the modern web.
This package implements basic support for NFD Management protocol.
In particular, it enables prefix registration on NFD.
import { enableNfdPrefixReg, signInterest02 } from "@ndn/nfdmgmt";
import { Endpoint } from "@ndn/endpoint";
import { Forwarder, FwFace } from "@ndn/fw";
import { EcPrivateKey } from "@ndn/keychain";
import { UnixTransport } from "@ndn/node-transport";
import { Data, Interest, Name } from "@ndn/packet";
import { fromUtf8, toUtf8 } from "@ndn/tlv";
import { strict as assert } from "assert";
(async () => {
2014 Signed Interest
While @ndn/keychain
package only supports 2019 Signed Interest format, NFD Management protocol is using 2014 Signed Interest format.
signInterest02
function provides basic support for that format.
const [privateKey] = await EcPrivateKey.generate("/K", "P-256");
const interest = new Interest("/I");
await signInterest02(interest, { signer: privateKey });
assert.equal(interest.name.length, 5);
console.log(`${interest.name}`);
NFD Prefix Registration
const fwC = Forwarder.create();
const fwP = Forwarder.create();
let uplinkC: FwFace;
try {
uplinkC = await UnixTransport.createFace({ fw: fwC }, "/run/nfd.sock");
} catch {
console.warn("NFD not running");
return;
}
uplinkC.addRoute(new Name("/"));
const uplinkP = await UnixTransport.createFace({ fw: fwP }, "/run/nfd.sock");
enableNfdPrefixReg(uplinkP, { signer: privateKey });
const producer = new Endpoint({ fw: fwP }).produce("/P",
async () => {
console.log("producing");
return new Data("/P", Data.FreshnessPeriod(1000), toUtf8("NDNts + NFD"));
});
await new Promise((r) => setTimeout(r, 500));
const data = await new Endpoint({ fw: fwC }).consume(
new Interest("/P", Interest.MustBeFresh),
);
const payloadText = fromUtf8(data.content);
console.log("received", `${data.name} ${payloadText}`);
assert.equal(payloadText, "NDNts + NFD");
uplinkC.close();
uplinkP.close();
producer.close();
})();