Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ndn/node-transport

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ndn/node-transport - npm Package Compare versions

Comparing version 0.0.20200909 to 0.0.20210203

31

lib/tcp-transport_browser.js
import { L3Face, StreamTransport } from "@ndn/l3face";
import * as net from "net";
import PCancelable from "p-cancelable";
import pTimeout from "p-timeout";
const DEFAULT_PORT = 6363;
/** TCP socket transport. */

@@ -19,21 +18,29 @@ export class TcpTransport extends StreamTransport {

(function (TcpTransport) {
function connect(arg1, port = 6363, { connectTimeout = 10000 } = {}) {
function connect(arg1, port = DEFAULT_PORT, opts = {}) {
var _a;
const connectOpts = typeof arg1 === "undefined" ? { port } :
typeof arg1 === "string" ? { host: arg1, port } :
{ connectTimeout, port, ...arg1 };
if (typeof arg1 === "object") {
connectTimeout = (_a = arg1.connectTimeout) !== null && _a !== void 0 ? _a : connectTimeout;
}
return pTimeout(new PCancelable((resolve, reject, onCancel) => {
{ host: arg1.host, port: (_a = arg1.port) !== null && _a !== void 0 ? _a : DEFAULT_PORT, family: arg1.family };
const { connectTimeout = 10000, signal, } = typeof arg1 === "object" ? arg1 : opts;
return new Promise((resolve, reject) => {
var _a;
const sock = net.connect(connectOpts);
sock.setNoDelay(true);
const fail = (err) => {
sock.destroy();
reject(err);
};
const timeout = setTimeout(() => fail(new Error("connectTimeout")), connectTimeout);
const onabort = () => fail(new Error("abort"));
(_a = signal) === null || _a === void 0 ? void 0 : _a.addEventListener("abort", () => onabort);
sock.on("error", () => undefined);
sock.once("error", reject);
sock.once("error", fail);
sock.once("connect", () => {
sock.off("error", reject);
var _a;
clearTimeout(timeout);
sock.off("error", fail);
(_a = signal) === null || _a === void 0 ? void 0 : _a.removeEventListener("abort", onabort);
resolve(new TcpTransport(sock, connectOpts));
});
onCancel(() => sock.destroy());
}), connectTimeout);
});
}

@@ -40,0 +47,0 @@ TcpTransport.connect = connect;

@@ -1,8 +0,4 @@

import { createRequire } from "module";
const require = createRequire(import.meta.url);
const { __importDefault } = require("tslib");
import { L3Face, StreamTransport } from "@ndn/l3face";
import * as net from "net";
const PCancelable = __importDefault(require("p-cancelable")).default;
const pTimeout = __importDefault(require("p-timeout")).default;
const DEFAULT_PORT = 6363;
/** TCP socket transport. */

@@ -22,21 +18,29 @@ export class TcpTransport extends StreamTransport {

(function (TcpTransport) {
function connect(arg1, port = 6363, { connectTimeout = 10000 } = {}) {
function connect(arg1, port = DEFAULT_PORT, opts = {}) {
var _a;
const connectOpts = typeof arg1 === "undefined" ? { port } :
typeof arg1 === "string" ? { host: arg1, port } :
{ connectTimeout, port, ...arg1 };
if (typeof arg1 === "object") {
connectTimeout = (_a = arg1.connectTimeout) !== null && _a !== void 0 ? _a : connectTimeout;
}
return pTimeout(new PCancelable((resolve, reject, onCancel) => {
{ host: arg1.host, port: (_a = arg1.port) !== null && _a !== void 0 ? _a : DEFAULT_PORT, family: arg1.family };
const { connectTimeout = 10000, signal, } = typeof arg1 === "object" ? arg1 : opts;
return new Promise((resolve, reject) => {
var _a;
const sock = net.connect(connectOpts);
sock.setNoDelay(true);
const fail = (err) => {
sock.destroy();
reject(err);
};
const timeout = setTimeout(() => fail(new Error("connectTimeout")), connectTimeout);
const onabort = () => fail(new Error("abort"));
(_a = signal) === null || _a === void 0 ? void 0 : _a.addEventListener("abort", () => onabort);
sock.on("error", () => undefined);
sock.once("error", reject);
sock.once("error", fail);
sock.once("connect", () => {
sock.off("error", reject);
var _a;
clearTimeout(timeout);
sock.off("error", fail);
(_a = signal) === null || _a === void 0 ? void 0 : _a.removeEventListener("abort", onabort);
resolve(new TcpTransport(sock, connectOpts));
});
onCancel(() => sock.destroy());
}), connectTimeout);
});
}

@@ -43,0 +47,0 @@ TcpTransport.connect = connect;

/// <reference types="node" />
import { L3Face, StreamTransport } from "@ndn/l3face";
import type { AbortSignal } from "abort-controller";
import * as net from "net";

@@ -15,2 +16,4 @@ /** TCP socket transport. */

connectTimeout?: number;
/** AbortSignal that allows canceling connection attempt via AbortController. */
signal?: AbortSignal | globalThis.AbortSignal;
}

@@ -17,0 +20,0 @@ /**

@@ -0,10 +1,11 @@

import { __importDefault, __importStar } from "tslib";
import * as dgram from "dgram";
import * as os from "os";
import pEvent from "p-event";
import _cjsDefaultImport0 from "p-event"; const pEvent = __importDefault(_cjsDefaultImport0).default;
const DEFAULT_UNICAST_PORT = 6363;
const DEFAULT_MULTICAST_GROUP = "224.0.23.170";
const DEFAULT_MULTICAST_PORT = 56363;
export async function openSocket({ recvBufferSize, sendBufferSize, bind = {}, }) {
async function openSocket({ family = 4, recvBufferSize, sendBufferSize, bind = {}, }) {
const sock = dgram.createSocket({
type: "udp4",
type: `udp${family}`,
reuseAddr: true,

@@ -36,2 +37,5 @@ recvBufferSize,

export async function openUnicast(opts) {
if (!opts.family && opts.host.includes(":")) {
opts.family = 6;
}
const sock = await openSocket(opts);

@@ -38,0 +42,0 @@ return connect(sock, opts);

@@ -1,13 +0,11 @@

import { createRequire } from "module";
const require = createRequire(import.meta.url);
const { __importDefault } = require("tslib");
import { __importDefault, __importStar } from "tslib";
import * as dgram from "dgram";
import * as os from "os";
const pEvent = __importDefault(require("p-event")).default;
import _cjsDefaultImport0 from "p-event"; const pEvent = __importDefault(_cjsDefaultImport0).default;
const DEFAULT_UNICAST_PORT = 6363;
const DEFAULT_MULTICAST_GROUP = "224.0.23.170";
const DEFAULT_MULTICAST_PORT = 56363;
export async function openSocket({ recvBufferSize, sendBufferSize, bind = {}, }) {
async function openSocket({ family = 4, recvBufferSize, sendBufferSize, bind = {}, }) {
const sock = dgram.createSocket({
type: "udp4",
type: `udp${family}`,
reuseAddr: true,

@@ -39,2 +37,5 @@ recvBufferSize,

export async function openUnicast(opts) {
if (!opts.family && opts.host.includes(":")) {
opts.family = 6;
}
const sock = await openSocket(opts);

@@ -41,0 +42,0 @@ return connect(sock, opts);

@@ -5,8 +5,14 @@ /// <reference types="node" />

declare type SocketOptions = Pick<dgram.SocketOptions, "recvBufferSize" | "sendBufferSize">;
export declare type OpenSocketOptions = SocketOptions & {
interface AddressFamilyOption {
/**
* IPv4 or IPv6.
* Default is IPv4, unless `host` is an IPv6 address (contains a colon).
*/
family?: 4 | 6;
}
export declare type OpenSocketOptions = SocketOptions & AddressFamilyOption & {
/** Bind options, such as local address and port. */
bind?: dgram.BindOptions;
};
export declare function openSocket({ recvBufferSize, sendBufferSize, bind, }: OpenSocketOptions): Promise<Socket>;
export interface ConnectOptions {
export interface ConnectOptions extends AddressFamilyOption {
/** Remote address. */

@@ -13,0 +19,0 @@ host: string;

@@ -0,3 +1,4 @@

import { __importDefault, __importStar } from "tslib";
import { L3Face, rxFromPacketIterable, Transport } from "@ndn/l3face";
import pEvent from "p-event";
import _cjsDefaultImport0 from "p-event"; const pEvent = __importDefault(_cjsDefaultImport0).default;
import * as udp from "./udp-helper_browser.js";

@@ -47,4 +48,4 @@ /** UDP socket transport. */

(function (UdpTransport) {
async function connect(arg1, arg2) {
const opts = typeof arg1 === "string" ? { host: arg1, port: arg2 } : arg1;
async function connect(arg1, port) {
const opts = typeof arg1 === "string" ? { host: arg1, port } : arg1;
const sock = await udp.openUnicast(opts);

@@ -51,0 +52,0 @@ return new UdpTransport(sock);

@@ -1,6 +0,4 @@

import { createRequire } from "module";
const require = createRequire(import.meta.url);
const { __importDefault } = require("tslib");
import { __importDefault, __importStar } from "tslib";
import { L3Face, rxFromPacketIterable, Transport } from "@ndn/l3face";
const pEvent = __importDefault(require("p-event")).default;
import _cjsDefaultImport0 from "p-event"; const pEvent = __importDefault(_cjsDefaultImport0).default;
import * as udp from "./udp-helper_node.js";

@@ -50,4 +48,4 @@ /** UDP socket transport. */

(function (UdpTransport) {
async function connect(arg1, arg2) {
const opts = typeof arg1 === "string" ? { host: arg1, port: arg2 } : arg1;
async function connect(arg1, port) {
const opts = typeof arg1 === "string" ? { host: arg1, port } : arg1;
const sock = await udp.openUnicast(opts);

@@ -54,0 +52,0 @@ return new UdpTransport(sock);

{
"name": "@ndn/node-transport",
"version": "0.0.20200909",
"version": "0.0.20210203",
"description": "NDNts: Low-Level Transports for Node.js",

@@ -23,9 +23,8 @@ "keywords": [

"dependencies": {
"@ndn/l3face": "0.0.20200909",
"p-cancelable": "^2.0.0",
"@ndn/l3face": "0.0.20210203",
"abort-controller": "^3.0.0",
"p-event": "^4.2.0",
"p-timeout": "^3.2.0",
"tslib": "^2.0.1"
"tslib": "^2.1.0"
},
"types": "lib/mod.d.ts"
}

@@ -11,2 +11,3 @@ # @ndn/node-transport

// other imports for examples
import { FwPacket } from "@ndn/fw";
import { L3Face, Transport } from "@ndn/l3face";

@@ -23,4 +24,4 @@ import { Data, Interest, Name } from "@ndn/packet";

* UnixTransport: Unix socket or Windows named pipe.
* TcpTransport: TCP tunnel, IPv4 only.
* UdpTransport: UDP unicast tunnel or UDP multicast group, IPv4 only.
* TcpTransport: TCP tunnel (IPv4 or IPv6).
* UdpTransport: UDP unicast tunnel (IPv4 or IPv6) or UDP multicast group (IPv4 only).

@@ -35,5 +36,4 @@ The `connect()` function of each transport creates a transport.

await useInL3Face(unix);
} catch (err) {
// This above would throw an error on Windows or if NFD is not running.
console.warn(err);
} catch (err: unknown) { // NFD is not running
console.warn("unix", err);
}

@@ -43,9 +43,32 @@

// It accepts either host+port or an options object for net.createConnection().
const tcp = await TcpTransport.connect("hobo.cs.arizona.edu", 6363);
await useInL3Face(tcp);
try {
const tcp4 = await TcpTransport.connect("hobo.cs.arizona.edu", 6363);
await useInL3Face(tcp4);
} catch (err: unknown) { // router unavailable
console.warn("tcp4", err);
}
// Select IPv4 with `family: 4` or select IPv6 with `family: 6`. Default is both.
try {
const tcp6 = await TcpTransport.connect({ host: "ndnhub.ipv6.lip6.fr", family: 6 });
await useInL3Face(tcp6);
} catch (err: unknown) { // router unavailable
console.warn("tcp6", err);
}
// UdpTransport.connect() establishes a UDP tunnel.
// It supports IPv4 only.
const udp = await UdpTransport.connect({ host: "hobo.cs.arizona.edu" });
await useInL3Face(udp);
try {
const udp4 = await UdpTransport.connect("hobo.cs.arizona.edu");
await useInL3Face(udp4);
} catch (err: unknown) { // router unavailable
console.warn("udp4", err);
}
// Select IPv6 with `type: "udp6"`. Default is IPv4 only.
try {
const udp6 = await UdpTransport.connect({ host: "ndnhub.ipv6.lip6.fr", family: 6 });
await useInL3Face(udp6);
} catch (err: unknown) { // router unavailable
console.warn("udp6", err);
}
```

@@ -61,7 +84,3 @@

multicasts.forEach(async (transport, i) => {
if (i === 0) {
await useInL3Face(transport);
} else {
transport.close();
}
await useInL3Face(transport);
});

@@ -83,3 +102,3 @@ ```

// It returns a FwFace instance (from @ndn/fw package).
const face = await UdpTransport.createFace({}, "hobo.cs.arizona.edu");
const face = await UdpTransport.createFace({}, "ndnhub.ipv6.lip6.fr");
face.addRoute(new Name("/ndn"));

@@ -117,3 +136,3 @@ face.close();

// Send five Interests.
let seq = Math.floor(Math.random() * 99999999);
let seq = Math.floor(Math.random() * 1e9);
for (let i = 0; i < 5; ++i) {

@@ -123,15 +142,14 @@ await new Promise((r) => setTimeout(r, 50));

console.log(`${transport} <I ${interest.name}`);
yield interest;
yield FwPacket.create(interest);
}
await new Promise((r) => setTimeout(r, 200));
await new Promise((r) => setTimeout(r, 500));
} }),
(async () => {
let nData = 0;
for await (const pkt of face.rx) {
if (!(pkt instanceof Data)) {
for await (const { l3 } of face.rx) {
if (!(l3 instanceof Data)) {
continue;
}
// Print incoming Data name.
const data: Data = pkt;
console.log(`${transport} >D ${data.name}`);
console.log(`${transport} >D ${l3.name}`);
if (++nData >= 5) {

@@ -138,0 +156,0 @@ return;

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc