@tracerbench/websocket-message-transport
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -1,2 +0,5 @@ | ||
export { default } from "./openWebSocket"; | ||
import { AttachMessageTransport } from "@tracerbench/message-transport"; | ||
import { RaceCancellation } from "race-cancellation"; | ||
export declare type CloseWebSocket = () => void; | ||
export default function openWebSocket(url: string, raceCancellation?: RaceCancellation): Promise<[AttachMessageTransport, CloseWebSocket]>; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var openWebSocket_1 = require("./openWebSocket"); | ||
exports.default = openWebSocket_1.default; | ||
const race_cancellation_1 = require("race-cancellation"); | ||
const NodeWebSocket = require("ws"); | ||
async function openWebSocket(url, raceCancellation) { | ||
const ws = new NodeWebSocket(url); | ||
let lastError; | ||
ws.on("error", (err) => { | ||
lastError = err; | ||
}); | ||
await race_cancellation_1.disposablePromise((resolve, reject) => { | ||
ws.on("open", resolve); | ||
ws.on("close", onClose); | ||
function onClose() { | ||
let message = `Failed to open ${url}`; | ||
if (lastError !== undefined) { | ||
message += `: ${lastError.stack}`; | ||
} | ||
reject(new Error(message)); | ||
} | ||
return () => { | ||
ws.removeListener("open", resolve); | ||
ws.removeListener("close", onClose); | ||
}; | ||
}, raceCancellation); | ||
return [ | ||
(onMessage, onClose) => { | ||
// we should be open still when attach is called | ||
// but double check here | ||
if (ws.readyState !== ws.OPEN) { | ||
setImmediate(handleClose); | ||
} | ||
let called = false; | ||
function handleClose() { | ||
if (called) { | ||
return; | ||
} | ||
called = true; | ||
ws.removeListener("message", onMessage); | ||
ws.removeListener("close", handleClose); | ||
onClose(lastError); | ||
} | ||
ws.on("message", onMessage); | ||
ws.on("close", handleClose); | ||
return message => ws.send(message); | ||
}, | ||
() => ws.close(), | ||
]; | ||
} | ||
exports.default = openWebSocket; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@tracerbench/websocket-message-transport", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"license": "BSD-2-Clause", | ||
@@ -13,4 +13,4 @@ "author": "Kris Selden <kris.selden@gmail.com>", | ||
"scripts": { | ||
"build": "tsc", | ||
"build:clean": "rm -rf dist && yarn run build", | ||
"build": "tsc -b", | ||
"clean": "rm -rf dist tsconfig.tsbuildinfo", | ||
"lint": "tslint -p tsconfig.json", | ||
@@ -20,15 +20,15 @@ "prepare": "yarn run build" | ||
"dependencies": { | ||
"@tracerbench/message-transport": "^1.0.1", | ||
"@tracerbench/message-transport": "^1.1.0", | ||
"race-cancellation": "^0.4.1", | ||
"ws": "^7.0.1" | ||
"ws": "^7.1.2" | ||
}, | ||
"devDependencies": { | ||
"@types/ws": "*", | ||
"@types/ws": "^6.0.3", | ||
"prettier": "^1.18.2", | ||
"tslint": "^5.18.0", | ||
"tslint": "^5.20.0", | ||
"tslint-config-prettier": "^1.18.0", | ||
"tslint-plugin-prettier": "^2.0.1", | ||
"typescript": "^3.5.2" | ||
"typescript": "^3.6.4" | ||
}, | ||
"gitHead": "e37b27bd4de7c6c49ad7ad8593263e67ad6018a0" | ||
"gitHead": "5952575b4e979ed608d48972982dc6881edf6ce9" | ||
} |
@@ -1,1 +0,59 @@ | ||
export { default } from "./openWebSocket"; | ||
import { AttachMessageTransport } from "@tracerbench/message-transport"; | ||
import { disposablePromise, RaceCancellation } from "race-cancellation"; | ||
import NodeWebSocket = require("ws"); | ||
export type CloseWebSocket = () => void; | ||
export default async function openWebSocket( | ||
url: string, | ||
raceCancellation?: RaceCancellation, | ||
): Promise<[AttachMessageTransport, CloseWebSocket]> { | ||
const ws = new NodeWebSocket(url); | ||
let lastError: Error | undefined; | ||
ws.on("error", (err: Error) => { | ||
lastError = err; | ||
}); | ||
await disposablePromise((resolve, reject) => { | ||
ws.on("open", resolve); | ||
ws.on("close", onClose); | ||
function onClose() { | ||
let message = `Failed to open ${url}`; | ||
if (lastError !== undefined) { | ||
message += `: ${lastError.stack}`; | ||
} | ||
reject(new Error(message)); | ||
} | ||
return () => { | ||
ws.removeListener("open", resolve); | ||
ws.removeListener("close", onClose); | ||
}; | ||
}, raceCancellation); | ||
return [ | ||
(onMessage, onClose) => { | ||
// we should be open still when attach is called | ||
// but double check here | ||
if (ws.readyState !== ws.OPEN) { | ||
setImmediate(handleClose); | ||
} | ||
let called = false; | ||
function handleClose() { | ||
if (called) { | ||
return; | ||
} | ||
called = true; | ||
ws.removeListener("message", onMessage); | ||
ws.removeListener("close", handleClose); | ||
onClose(lastError); | ||
} | ||
ws.on("message", onMessage); | ||
ws.on("close", handleClose); | ||
return message => ws.send(message); | ||
}, | ||
() => ws.close(), | ||
]; | ||
} |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
7943
8
106
1
Updatedws@^7.1.2