npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ndn/endpoint

v0.0.20240630

Published

NDNts: Client Endpoint

Downloads

15

Readme

@ndn/endpoint

This package is part of NDNts, Named Data Networking libraries for the modern web.

This package implements the endpoint concept, consisting of consume and produce functions. These are the basic abstractions through which an application can communicate with the NDN network.

The endpoint concept is similar to a "client face" in other NDN libraries, with the enhancement that it handles these details automatically:

  • [X] Outgoing packets are signed and incoming packets are verified, if trust schema is provided.
  • [X] Outgoing Interests are retransmitted periodically, if retransmission policy is specified.
  • [X] Outgoing Data buffer, if enabled, allows the producer to reply to one Interest with multiple Data (e.g. segments), or push generated Data without receiving an Interest. Data will be sent automatically upon Interest arrival.
  • [X] The underlying transport is reconnected upon failure, if transport failure policy is specified (implemented in @ndn/l3face package).
  • [X] Prefix registrations are refreshed periodically or upon transport reconnection (implemented in @ndn/nfdmgmt package).
import { consume, produce } from "@ndn/endpoint";

// other imports for examples
import { generateSigningKey } from "@ndn/keychain";
import { Data, digestSigning } from "@ndn/packet";
import { fromUtf8, toUtf8 } from "@ndn/util";

// Generate a key pair for the demo.
const [signer, verifier] = await generateSigningKey("/identity");

Producer

The produce() standalone function creates a producer. It accepts three parameters:

  1. A name prefix that the producer should listen on.
  2. A handler function that produces the Data in reply to an Interest.
  3. Additional options.
using producer = produce("/P", async (interest) => {
  console.log(`Producer is handling Interest ${interest.name}`);
  return new Data(interest.name, toUtf8("served by NDNts"));
}, {
  concurrency: 16, // allow concurrent calls to the handler function
  dataSigner: signer, // enable automatic signing
});

The return value of produce() function is an object that implements Producer interface. This interface contains accessors and methods for observing and controlling the producer.

The object implements Disposable interface. With using keyword (TypeScript only), the producer is closed when the variable goes out of scope. Alternatively, you can invoke producer[Symbol.dispose]() explicitly.

Consumer

The consume() standalone function creates a consumer to receive a single Data packet. It accepts two parameters:

  1. An Interest or Interest name.
  2. Additional options.
const consumer1 = consume("/P/1", {
  retx: 2, // enable retransmission
  verifier, // enable automatic verification
});
try {
  const data1 = await consumer1;
  console.log(`Consumer receives Data ${data1.name} with content "${
    fromUtf8(data1.content)}" after ${consumer1.nRetx} retransmissions`);
} catch (err: unknown) {
  console.log("Consumer error", err);
}

The return value of consume() function is an object that implements ConsumerContext interface. This interface contains accessors and methods for observing and controlling the consumer.

Most importantly, the return value is a Promise that resolves to the retrieved Data or rejects upon error (including timeout). Thus, you can simply await consume(..) to obtain the Data.

try {
  const data2 = await consume("/P/2", { retx: 2, verifier });
  console.log(`Consumer receives Data ${data2.name}`);
} catch (err: unknown) {
  console.log("Consumer error", err);
}