@ndn/l3face
Advanced tools
Comparing version 0.0.20230121 to 0.0.20240113
import { __importDefault, __importStar } from "tslib"; | ||
import { EventEmitter } from "events"; | ||
import { Forwarder, FwPacket } from "@ndn/fw"; | ||
import { LpService } from "@ndn/lp"; | ||
import { Interest } from "@ndn/packet"; | ||
import { asDataView } from "@ndn/util"; | ||
import { asDataView, CustomEvent } from "@ndn/util"; | ||
import { abortableSource, AbortError as IteratorAbortError } from "abortable-iterator"; | ||
@@ -11,5 +10,6 @@ import { pushable } from "it-pushable"; | ||
import { consume, filter, map, pipeline } from "streaming-iterables"; | ||
import { TypedEventTarget } from "typescript-event-target"; | ||
import { Transport } from "./transport_browser.js"; | ||
/** Network layer face for sending and receiving L3 packets. */ | ||
export class L3Face extends EventEmitter { | ||
export class L3Face extends TypedEventTarget { | ||
transport; | ||
@@ -25,8 +25,10 @@ attributes; | ||
} | ||
const evt = new L3Face.StateEvent("state", newState, this.state_); | ||
this.state_ = newState; | ||
this.emit("state", newState); | ||
this.dispatchTypedEvent("state", evt); | ||
switch (newState) { | ||
case L3Face.State.UP: | ||
this.emit("up"); | ||
case L3Face.State.UP: { | ||
this.dispatchTypedEvent("up", new Event("up")); | ||
break; | ||
} | ||
case L3Face.State.DOWN: { | ||
@@ -36,9 +38,10 @@ const err = this.lastError instanceof Error ? | ||
new Error(`${this.lastError ?? "unknown error"}`); | ||
this.emit("down", err); | ||
this.dispatchTypedEvent("down", new CustomEvent("down", { detail: err })); | ||
this.lastError = undefined; | ||
break; | ||
} | ||
case L3Face.State.CLOSED: | ||
this.emit("close"); | ||
case L3Face.State.CLOSED: { | ||
this.dispatchTypedEvent("close", new Event("close")); | ||
break; | ||
} | ||
} | ||
@@ -80,7 +83,7 @@ } | ||
if (pkt instanceof LpService.RxError) { | ||
this.emit("rxerror", pkt); | ||
this.dispatchTypedEvent("rxerror", new CustomEvent("rxerror", { detail: pkt })); | ||
return false; | ||
} | ||
return true; | ||
}), map(({ l3, token: wireToken }) => { | ||
}), map(({ l3, token: wireToken, congestionMark }) => { | ||
let internalToken; | ||
@@ -96,7 +99,7 @@ if (l3 instanceof Interest) { | ||
} | ||
return FwPacket.create(l3, internalToken); | ||
return FwPacket.create(l3, internalToken, congestionMark); | ||
})); | ||
} | ||
txTransform(fwTx) { | ||
return pipeline(() => fwTx, filter((pkt) => FwPacket.isEncodable(pkt)), map(({ l3, token: internalToken }) => { | ||
return pipeline(() => fwTx, filter((pkt) => FwPacket.isEncodable(pkt)), map(({ l3, token: internalToken, congestionMark }) => { | ||
let wireToken; | ||
@@ -112,6 +115,6 @@ if (typeof internalToken === "number") { | ||
} | ||
return { l3, token: wireToken }; | ||
return { l3, token: wireToken, congestionMark }; | ||
}), this.lp.tx, filter((value) => { | ||
if (value instanceof LpService.TxError) { | ||
this.emit("txerror", value); | ||
this.dispatchTypedEvent("txerror", new CustomEvent("txerror", { detail: value })); | ||
return false; | ||
@@ -134,4 +137,3 @@ } | ||
const abort = new AbortController(); | ||
const handleStateChange = () => abort.abort(); | ||
this.once("state", handleStateChange); | ||
this.addEventListener("state", () => abort.abort(), { once: true, signal: abort.signal }); | ||
try { | ||
@@ -156,3 +158,2 @@ const txSource = abortableSource(txSourceIterable, abort.signal); | ||
abort.abort(); | ||
this.off("state", handleStateChange); | ||
} | ||
@@ -198,2 +199,12 @@ } | ||
})(State = L3Face.State || (L3Face.State = {})); | ||
class StateEvent extends Event { | ||
state; | ||
prev; | ||
constructor(type, state, prev) { | ||
super(type); | ||
this.state = state; | ||
this.prev = prev; | ||
} | ||
} | ||
L3Face.StateEvent = StateEvent; | ||
function makeCreateFace(createTransport) { | ||
@@ -200,0 +211,0 @@ return (async (opts, ...args) => { |
import { __importDefault, __importStar } from "tslib"; | ||
import { EventEmitter } from "node:events"; | ||
import { Forwarder, FwPacket } from "@ndn/fw"; | ||
import { LpService } from "@ndn/lp"; | ||
import { Interest } from "@ndn/packet"; | ||
import { asDataView } from "@ndn/util"; | ||
import { asDataView, CustomEvent } from "@ndn/util"; | ||
import { abortableSource, AbortError as IteratorAbortError } from "abortable-iterator"; | ||
@@ -11,5 +10,6 @@ import { pushable } from "it-pushable"; | ||
import { consume, filter, map, pipeline } from "streaming-iterables"; | ||
import { TypedEventTarget } from "typescript-event-target"; | ||
import { Transport } from "./transport_node.js"; | ||
/** Network layer face for sending and receiving L3 packets. */ | ||
export class L3Face extends EventEmitter { | ||
export class L3Face extends TypedEventTarget { | ||
transport; | ||
@@ -25,8 +25,10 @@ attributes; | ||
} | ||
const evt = new L3Face.StateEvent("state", newState, this.state_); | ||
this.state_ = newState; | ||
this.emit("state", newState); | ||
this.dispatchTypedEvent("state", evt); | ||
switch (newState) { | ||
case L3Face.State.UP: | ||
this.emit("up"); | ||
case L3Face.State.UP: { | ||
this.dispatchTypedEvent("up", new Event("up")); | ||
break; | ||
} | ||
case L3Face.State.DOWN: { | ||
@@ -36,9 +38,10 @@ const err = this.lastError instanceof Error ? | ||
new Error(`${this.lastError ?? "unknown error"}`); | ||
this.emit("down", err); | ||
this.dispatchTypedEvent("down", new CustomEvent("down", { detail: err })); | ||
this.lastError = undefined; | ||
break; | ||
} | ||
case L3Face.State.CLOSED: | ||
this.emit("close"); | ||
case L3Face.State.CLOSED: { | ||
this.dispatchTypedEvent("close", new Event("close")); | ||
break; | ||
} | ||
} | ||
@@ -80,7 +83,7 @@ } | ||
if (pkt instanceof LpService.RxError) { | ||
this.emit("rxerror", pkt); | ||
this.dispatchTypedEvent("rxerror", new CustomEvent("rxerror", { detail: pkt })); | ||
return false; | ||
} | ||
return true; | ||
}), map(({ l3, token: wireToken }) => { | ||
}), map(({ l3, token: wireToken, congestionMark }) => { | ||
let internalToken; | ||
@@ -96,7 +99,7 @@ if (l3 instanceof Interest) { | ||
} | ||
return FwPacket.create(l3, internalToken); | ||
return FwPacket.create(l3, internalToken, congestionMark); | ||
})); | ||
} | ||
txTransform(fwTx) { | ||
return pipeline(() => fwTx, filter((pkt) => FwPacket.isEncodable(pkt)), map(({ l3, token: internalToken }) => { | ||
return pipeline(() => fwTx, filter((pkt) => FwPacket.isEncodable(pkt)), map(({ l3, token: internalToken, congestionMark }) => { | ||
let wireToken; | ||
@@ -112,6 +115,6 @@ if (typeof internalToken === "number") { | ||
} | ||
return { l3, token: wireToken }; | ||
return { l3, token: wireToken, congestionMark }; | ||
}), this.lp.tx, filter((value) => { | ||
if (value instanceof LpService.TxError) { | ||
this.emit("txerror", value); | ||
this.dispatchTypedEvent("txerror", new CustomEvent("txerror", { detail: value })); | ||
return false; | ||
@@ -134,4 +137,3 @@ } | ||
const abort = new AbortController(); | ||
const handleStateChange = () => abort.abort(); | ||
this.once("state", handleStateChange); | ||
this.addEventListener("state", () => abort.abort(), { once: true, signal: abort.signal }); | ||
try { | ||
@@ -156,3 +158,2 @@ const txSource = abortableSource(txSourceIterable, abort.signal); | ||
abort.abort(); | ||
this.off("state", handleStateChange); | ||
} | ||
@@ -198,2 +199,12 @@ } | ||
})(State = L3Face.State || (L3Face.State = {})); | ||
class StateEvent extends Event { | ||
state; | ||
prev; | ||
constructor(type, state, prev) { | ||
super(type); | ||
this.state = state; | ||
this.prev = prev; | ||
} | ||
} | ||
L3Face.StateEvent = StateEvent; | ||
function makeCreateFace(createTransport) { | ||
@@ -200,0 +211,0 @@ return (async (opts, ...args) => { |
@@ -1,23 +0,23 @@ | ||
import { type FwFace, Forwarder, FwPacket } from "@ndn/fw"; | ||
import { Forwarder, type FwFace, FwPacket } from "@ndn/fw"; | ||
import { LpService } from "@ndn/lp"; | ||
import { type NameLike } from "@ndn/packet"; | ||
import type TypedEmitter from "typed-emitter"; | ||
import type { AsyncReturnType } from "type-fest"; | ||
import { TypedEventTarget } from "typescript-event-target"; | ||
import { Transport } from "./transport.js"; | ||
type Events = { | ||
type EventMap = { | ||
/** Emitted upon face state change. */ | ||
state: (state: L3Face.State) => void; | ||
state: L3Face.StateEvent; | ||
/** Emitted upon state becomes UP. */ | ||
up: () => void; | ||
up: Event; | ||
/** Emitted upon state becomes DOWN. */ | ||
down: (err: Error) => void; | ||
down: CustomEvent<Error>; | ||
/** Emitted upon state becomes CLOSED. */ | ||
close: () => void; | ||
close: Event; | ||
/** Emitted upon RX decoding error. */ | ||
rxerror: (err: L3Face.RxError) => void; | ||
rxerror: CustomEvent<L3Face.RxError>; | ||
/** Emitted upon TX preparation error. */ | ||
txerror: (err: L3Face.TxError) => void; | ||
txerror: CustomEvent<L3Face.TxError>; | ||
}; | ||
declare const L3Face_base: new () => TypedEmitter<Events>; | ||
/** Network layer face for sending and receiving L3 packets. */ | ||
export declare class L3Face extends L3Face_base implements FwFace.RxTx { | ||
export declare class L3Face extends TypedEventTarget<EventMap> implements FwFace.RxTx { | ||
private transport; | ||
@@ -47,2 +47,7 @@ readonly attributes: L3Face.Attributes; | ||
} | ||
class StateEvent extends Event { | ||
readonly state: State; | ||
readonly prev: State; | ||
constructor(type: string, state: State, prev: State); | ||
} | ||
interface Attributes extends Transport.Attributes { | ||
@@ -83,5 +88,5 @@ /** Whether to readvertise registered routes. */ | ||
type CreateFaceFunc<R extends Transport | Transport[], P extends any[]> = (opts: CreateFaceOptions, ...args: P) => Promise<R extends Transport[] ? FwFace[] : FwFace>; | ||
function makeCreateFace<C extends (...args: any[]) => Promise<Transport | Transport[]>>(createTransport: C): CreateFaceFunc<C extends (...args: any[]) => Promise<infer R> ? R : never, Parameters<C>>; | ||
function makeCreateFace<C extends (...args: any[]) => Promise<Transport | Transport[]>>(createTransport: C): CreateFaceFunc<AsyncReturnType<C>, Parameters<C>>; | ||
function processAddRoutes(fwFace: FwFace, addRoutes?: readonly NameLike[]): void; | ||
} | ||
export {}; |
@@ -5,2 +5,7 @@ import { Decoder } from "@ndn/tlv"; | ||
import { writeToStream } from "streaming-iterables"; | ||
/** | ||
* Parse TLVs from input stream. | ||
* @param conn input stream, such as a socket. | ||
* @returns AsyncIterable of TLVs. | ||
*/ | ||
export async function* rxFromStream(conn) { | ||
@@ -33,2 +38,7 @@ let leftover = Buffer.alloc(0); | ||
} | ||
/** | ||
* Pipe encoded packets to output stream. | ||
* @param conn output stream, such as a socket. | ||
* @returns a function that accepts AsyncIterable of Uint8Array containing encoded packets. | ||
*/ | ||
export function txToStream(conn) { | ||
@@ -40,10 +50,10 @@ return async (iterable) => { | ||
finally { | ||
conn.end(); | ||
try { | ||
conn.end(); | ||
await pEvent(conn, "finish", { timeout: 100 }); | ||
} | ||
catch { } | ||
const destroyable = conn; | ||
if (typeof destroyable.destroy === "function") { | ||
destroyable.destroy(); | ||
const socket = conn; | ||
if (typeof socket.destroy === "function") { | ||
socket.destroy(); | ||
} | ||
@@ -50,0 +60,0 @@ } |
@@ -5,2 +5,7 @@ import { Decoder } from "@ndn/tlv"; | ||
import { writeToStream } from "streaming-iterables"; | ||
/** | ||
* Parse TLVs from input stream. | ||
* @param conn input stream, such as a socket. | ||
* @returns AsyncIterable of TLVs. | ||
*/ | ||
export async function* rxFromStream(conn) { | ||
@@ -33,2 +38,7 @@ let leftover = Buffer.alloc(0); | ||
} | ||
/** | ||
* Pipe encoded packets to output stream. | ||
* @param conn output stream, such as a socket. | ||
* @returns a function that accepts AsyncIterable of Uint8Array containing encoded packets. | ||
*/ | ||
export function txToStream(conn) { | ||
@@ -40,10 +50,10 @@ return async (iterable) => { | ||
finally { | ||
conn.end(); | ||
try { | ||
conn.end(); | ||
await pEvent(conn, "finish", { timeout: 100 }); | ||
} | ||
catch { } | ||
const destroyable = conn; | ||
if (typeof destroyable.destroy === "function") { | ||
destroyable.destroy(); | ||
const socket = conn; | ||
if (typeof socket.destroy === "function") { | ||
socket.destroy(); | ||
} | ||
@@ -50,0 +60,0 @@ } |
/// <reference types="node" /> | ||
import type { Transport } from "./transport.js"; | ||
/** | ||
* Parse TLVs from input stream. | ||
* @param conn input stream, such as a socket. | ||
* @returns AsyncIterable of TLVs. | ||
*/ | ||
export declare function rxFromStream(conn: NodeJS.ReadableStream): Transport.Rx; | ||
/** | ||
* Pipe encoded packets to output stream. | ||
* @param conn output stream, such as a socket. | ||
* @returns a function that accepts AsyncIterable of Uint8Array containing encoded packets. | ||
*/ | ||
export declare function txToStream(conn: NodeJS.WritableStream): Transport.Tx; |
@@ -25,3 +25,3 @@ import type { Decoder } from "@ndn/tlv"; | ||
export declare namespace Transport { | ||
interface Attributes extends Record<string, any> { | ||
interface Attributes extends Record<string, unknown> { | ||
/** | ||
@@ -28,0 +28,0 @@ * Textual description. |
{ | ||
"name": "@ndn/l3face", | ||
"version": "0.0.20230121", | ||
"version": "0.0.20240113", | ||
"description": "NDNts: Network Layer Face", | ||
@@ -25,16 +25,17 @@ "keywords": [ | ||
"dependencies": { | ||
"@ndn/fw": "0.0.20230121", | ||
"@ndn/lp": "0.0.20230121", | ||
"@ndn/packet": "0.0.20230121", | ||
"@ndn/tlv": "0.0.20230121", | ||
"@ndn/util": "0.0.20230121", | ||
"abortable-iterator": "^4.0.2", | ||
"it-pushable": "^3.1.2", | ||
"p-event": "^5.0.1", | ||
"@ndn/fw": "0.0.20240113", | ||
"@ndn/lp": "0.0.20240113", | ||
"@ndn/packet": "0.0.20240113", | ||
"@ndn/tlv": "0.0.20240113", | ||
"@ndn/util": "0.0.20240113", | ||
"abortable-iterator": "^5.0.1", | ||
"it-pushable": "^3.2.3", | ||
"p-event": "^6.0.0", | ||
"retry": "^0.13.1", | ||
"streaming-iterables": "^7.1.0", | ||
"tslib": "^2.4.1", | ||
"typed-emitter": "^2.1.0" | ||
"streaming-iterables": "^8.0.1", | ||
"tslib": "^2.6.2", | ||
"type-fest": "^4.9.0", | ||
"typescript-event-target": "^1.1.0" | ||
}, | ||
"types": "lib/mod.d.ts" | ||
} |
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
33189
884
13
+ Addedtype-fest@^4.9.0
+ Added@ndn/fw@0.0.20240113(transitive)
+ Added@ndn/lp@0.0.20240113(transitive)
+ Added@ndn/packet@0.0.20240113(transitive)
+ Added@ndn/tlv@0.0.20240113(transitive)
+ Added@ndn/util@0.0.20240113(transitive)
+ Addedabortable-iterator@5.1.0(transitive)
+ Addedit-stream-types@2.0.2(transitive)
+ Addedp-event@6.0.1(transitive)
+ Addedp-timeout@6.1.3(transitive)
+ Addedstreaming-iterables@8.0.1(transitive)
+ Addedtype-fest@4.30.0(transitive)
+ Addedtypescript-event-target@1.1.1(transitive)
- Removedtyped-emitter@^2.1.0
- Removed@ndn/fw@0.0.20230121(transitive)
- Removed@ndn/lp@0.0.20230121(transitive)
- Removed@ndn/packet@0.0.20230121(transitive)
- Removed@ndn/tlv@0.0.20230121(transitive)
- Removed@ndn/util@0.0.20230121(transitive)
- Removedabortable-iterator@4.0.3(transitive)
- Removedit-stream-types@1.0.5(transitive)
- Removedp-event@5.0.1(transitive)
- Removedp-timeout@5.1.0(transitive)
- Removedrxjs@7.8.1(transitive)
- Removedstreaming-iterables@7.1.0(transitive)
- Removedtyped-emitter@2.1.0(transitive)
Updated@ndn/fw@0.0.20240113
Updated@ndn/lp@0.0.20240113
Updated@ndn/packet@0.0.20240113
Updated@ndn/tlv@0.0.20240113
Updated@ndn/util@0.0.20240113
Updatedabortable-iterator@^5.0.1
Updatedit-pushable@^3.2.3
Updatedp-event@^6.0.0
Updatedstreaming-iterables@^8.0.1
Updatedtslib@^2.6.2