@ndn/ws-transport
Advanced tools
Comparing version 0.0.20220501 to 0.0.20230121
@@ -1,1 +0,1 @@ | ||
export * from "./ws-transport"; | ||
export * from "./ws-transport.js"; |
@@ -0,7 +1,12 @@ | ||
import { __importDefault, __importStar } from "tslib"; | ||
import { L3Face, rxFromPacketIterable, Transport } from "@ndn/l3face"; | ||
import { pEventIterator } from "p-event"; | ||
import { map } from "streaming-iterables"; | ||
import _cjsDefaultImport0 from "event-iterator"; const EventIterator = __importDefault(_cjsDefaultImport0).default; | ||
import { makeWebSocket } from "./ws_browser.js"; | ||
/** WebSocket transport. */ | ||
export class WsTransport extends Transport { | ||
sock; | ||
opts; | ||
rx; | ||
highWaterMark; | ||
lowWaterMark; | ||
constructor(sock, opts) { | ||
@@ -11,20 +16,14 @@ super({ describe: `WebSocket(${sock.url})` }); | ||
this.opts = opts; | ||
this.tx = async (iterable) => { | ||
try { | ||
for await (const pkt of iterable) { | ||
if (this.sock.readyState !== this.sock.OPEN) { | ||
throw new Error(`unexpected WebSocket.readyState ${this.sock.readyState}`); | ||
} | ||
this.sock.send(pkt); | ||
if (this.sock.bufferedAmount > this.highWaterMark) { | ||
await this.waitForTxBuffer(); | ||
} | ||
} | ||
} | ||
finally { | ||
this.close(); | ||
} | ||
}; | ||
sock.binaryType = "arraybuffer"; | ||
this.rx = rxFromPacketIterable(map((evt) => new Uint8Array(evt instanceof ArrayBuffer ? evt : evt.data), pEventIterator(sock, "message", { resolutionEvents: ["close"] }))); | ||
this.rx = rxFromPacketIterable(new EventIterator(({ push, stop }) => { | ||
const handleMessage = (evt) => { | ||
push(new Uint8Array(evt instanceof ArrayBuffer ? evt : evt.data)); | ||
}; | ||
sock.addEventListener("message", handleMessage); | ||
sock.addEventListener("close", stop); | ||
return () => { | ||
sock.removeEventListener("message", handleMessage); | ||
sock.removeEventListener("close", stop); | ||
}; | ||
})); | ||
this.highWaterMark = opts.highWaterMark ?? 1024 * 1024; | ||
@@ -37,2 +36,18 @@ this.lowWaterMark = opts.lowWaterMark ?? 16 * 1024; | ||
get mtu() { return Infinity; } | ||
tx = async (iterable) => { | ||
try { | ||
for await (const pkt of iterable) { | ||
if (this.sock.readyState !== this.sock.OPEN) { | ||
throw new Error(`unexpected WebSocket.readyState ${this.sock.readyState}`); | ||
} | ||
this.sock.send(pkt); | ||
if (this.sock.bufferedAmount > this.highWaterMark) { | ||
await this.waitForTxBuffer(); | ||
} | ||
} | ||
} | ||
finally { | ||
this.close(); | ||
} | ||
}; | ||
waitForTxBuffer() { | ||
@@ -66,7 +81,9 @@ return new Promise((resolve) => { | ||
} | ||
let timeout; // eslint-disable-line prefer-const | ||
const fail = (err) => { | ||
clearTimeout(timeout); | ||
sock.close(); | ||
reject(err); | ||
}; | ||
const timeout = setTimeout(() => fail(new Error("connectTimeout")), connectTimeout); | ||
timeout = setTimeout(() => fail(new Error("connectTimeout")), connectTimeout); | ||
const onabort = () => fail(new Error("abort")); | ||
@@ -73,0 +90,0 @@ signal?.addEventListener("abort", onabort); |
@@ -0,7 +1,12 @@ | ||
import { __importDefault, __importStar } from "tslib"; | ||
import { L3Face, rxFromPacketIterable, Transport } from "@ndn/l3face"; | ||
import { pEventIterator } from "p-event"; | ||
import { map } from "streaming-iterables"; | ||
import _cjsDefaultImport0 from "event-iterator"; const EventIterator = __importDefault(_cjsDefaultImport0).default; | ||
import { makeWebSocket } from "./ws_node.js"; | ||
/** WebSocket transport. */ | ||
export class WsTransport extends Transport { | ||
sock; | ||
opts; | ||
rx; | ||
highWaterMark; | ||
lowWaterMark; | ||
constructor(sock, opts) { | ||
@@ -11,20 +16,14 @@ super({ describe: `WebSocket(${sock.url})` }); | ||
this.opts = opts; | ||
this.tx = async (iterable) => { | ||
try { | ||
for await (const pkt of iterable) { | ||
if (this.sock.readyState !== this.sock.OPEN) { | ||
throw new Error(`unexpected WebSocket.readyState ${this.sock.readyState}`); | ||
} | ||
this.sock.send(pkt); | ||
if (this.sock.bufferedAmount > this.highWaterMark) { | ||
await this.waitForTxBuffer(); | ||
} | ||
} | ||
} | ||
finally { | ||
this.close(); | ||
} | ||
}; | ||
sock.binaryType = "arraybuffer"; | ||
this.rx = rxFromPacketIterable(map((evt) => new Uint8Array(evt instanceof ArrayBuffer ? evt : evt.data), pEventIterator(sock, "message", { resolutionEvents: ["close"] }))); | ||
this.rx = rxFromPacketIterable(new EventIterator(({ push, stop }) => { | ||
const handleMessage = (evt) => { | ||
push(new Uint8Array(evt instanceof ArrayBuffer ? evt : evt.data)); | ||
}; | ||
sock.addEventListener("message", handleMessage); | ||
sock.addEventListener("close", stop); | ||
return () => { | ||
sock.removeEventListener("message", handleMessage); | ||
sock.removeEventListener("close", stop); | ||
}; | ||
})); | ||
this.highWaterMark = opts.highWaterMark ?? 1024 * 1024; | ||
@@ -37,2 +36,18 @@ this.lowWaterMark = opts.lowWaterMark ?? 16 * 1024; | ||
get mtu() { return Infinity; } | ||
tx = async (iterable) => { | ||
try { | ||
for await (const pkt of iterable) { | ||
if (this.sock.readyState !== this.sock.OPEN) { | ||
throw new Error(`unexpected WebSocket.readyState ${this.sock.readyState}`); | ||
} | ||
this.sock.send(pkt); | ||
if (this.sock.bufferedAmount > this.highWaterMark) { | ||
await this.waitForTxBuffer(); | ||
} | ||
} | ||
} | ||
finally { | ||
this.close(); | ||
} | ||
}; | ||
waitForTxBuffer() { | ||
@@ -66,7 +81,9 @@ return new Promise((resolve) => { | ||
} | ||
let timeout; // eslint-disable-line prefer-const | ||
const fail = (err) => { | ||
clearTimeout(timeout); | ||
sock.close(); | ||
reject(err); | ||
}; | ||
const timeout = setTimeout(() => fail(new Error("connectTimeout")), connectTimeout); | ||
timeout = setTimeout(() => fail(new Error("connectTimeout")), connectTimeout); | ||
const onabort = () => fail(new Error("abort")); | ||
@@ -73,0 +90,0 @@ signal?.addEventListener("abort", onabort); |
{ | ||
"name": "@ndn/ws-transport", | ||
"version": "0.0.20220501", | ||
"version": "0.0.20230121", | ||
"description": "NDNts: WebSocket Transport", | ||
@@ -25,11 +25,9 @@ "keywords": [ | ||
"dependencies": { | ||
"@ndn/l3face": "0.0.20220501", | ||
"@types/ws": "^8.5.3", | ||
"p-event": "^5.0.1", | ||
"streaming-iterables": "^7.0.2", | ||
"tslib": "^2.4.0", | ||
"ws": "^8.5.0" | ||
"@ndn/l3face": "0.0.20230121", | ||
"@types/ws": "^8.5.4", | ||
"event-iterator": "^2.0.0", | ||
"tslib": "^2.4.1", | ||
"ws": "^8.12.0" | ||
}, | ||
"types": "lib/mod.d.ts", | ||
"readme": "# @ndn/ws-transport\n\nThis package is part of [NDNts](https://yoursunny.com/p/NDNts/), Named Data Networking libraries for the modern web.\n\nThis package implements a WebSocket transport.\nIt works in both Node and browser.\n\nYou can create a forwarder face that uses WebSocket transport with `WsTransport.createFace()` function.\nTo create a WebSocket transport without wrapping into L3Face, use `WsTransport.connect()` function.\n\n```ts\nimport { WsTransport } from \"@ndn/ws-transport\";\n\n// other imports for examples\nimport { Endpoint } from \"@ndn/endpoint\";\nimport { Data, Interest, Name } from \"@ndn/packet\";\n\nif (process.env.CI) { process.exit(0); }\n\n// Create a WebSocket face.\n// Unless otherwise specified, the face is added to the default Forwarder instance.\n// You may set an alternate Forwarder instance in the first argument.\n//\n// A route for \"/\" prefix is added automatically.\n// You may customize the route prefixes via addRoutes property in the first argument.\nconst uplink = await WsTransport.createFace({}, \"wss://hobo.cs.arizona.edu/ws/\");\n\n// Construct an Endpoint on the default Forwarder instance.\nconst endpoint = new Endpoint();\n\n// We can now send Interests and retrieve Data.\nlet seq = Math.trunc(Math.random() * 1e8);\nfor (let i = 0; i < 5; ++i) {\n try {\n const interest = new Interest(`/ndn/edu/arizona/ping/NDNts/${seq++}`);\n console.log(`<I ${interest.name}`);\n const data = await endpoint.consume(interest);\n console.log(`>D ${data.name}`);\n } catch (err: unknown) {\n console.warn(err);\n }\n}\n\n// In case a socket error occurs, the transport will attempt to reconnect automatically,\n// although packets transmitted during that time would be lost. The logic of reconnecting\n// is implemented in L3Face class from @ndn/l3face package.\n\n// When the face is no longer needed, close it.\nuplink.close();\n```\n" | ||
"types": "lib/mod.d.ts" | ||
} |
Sorry, the diff of this file is not supported yet
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
5
287
14568
+ Addedevent-iterator@^2.0.0
+ Added@ndn/fw@0.0.20230121(transitive)
+ Added@ndn/l3face@0.0.20230121(transitive)
+ Added@ndn/lp@0.0.20230121(transitive)
+ Added@ndn/packet@0.0.20230121(transitive)
+ Added@ndn/tlv@0.0.20230121(transitive)
+ Added@ndn/util@0.0.20230121(transitive)
+ Addedevent-iterator@2.0.0(transitive)
+ Addedhirestime@7.0.4(transitive)
+ Addedit-pushable@3.2.3(transitive)
+ Addedp-defer@4.0.1(transitive)
- Removedp-event@^5.0.1
- Removedstreaming-iterables@^7.0.2
- Removed@ndn/fw@0.0.20220501(transitive)
- Removed@ndn/l3face@0.0.20220501(transitive)
- Removed@ndn/lp@0.0.20220501(transitive)
- Removed@ndn/packet@0.0.20220501(transitive)
- Removed@ndn/tlv@0.0.20220501(transitive)
- Removed@ndn/util@0.0.20220501(transitive)
- Removedhirestime@6.1.0(transitive)
- Removedit-pushable@2.0.2(transitive)
Updated@ndn/l3face@0.0.20230121
Updated@types/ws@^8.5.4
Updatedtslib@^2.4.1
Updatedws@^8.12.0