Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// return a promise we resolve as soon as the connection is secured
const ret = createDeferredPromise();
// try connecting
const onConnection = () => {
debug("successfully created socket for origin " + origin.toString());
sock.removeListener("error", onError);
ret.resolve(new SocketWrapper(sock));
};
const onError = (e: Error) => {
debug("socket creation for origin " + origin.toString() + " failed: " + e);
sock.removeListener("connected", onConnection);
ret.reject(e.message);
};
const sock = dtls
.createSocket(dtlsOpts)
.once("connected", onConnection)
.once("error", onError)
;
return ret;
default:
throw new Error(`protocol type "${origin.protocol}" is not supported`);
}
}