本文整理汇总了TypeScript中stream.Duplex类的典型用法代码示例。如果您正苦于以下问题:TypeScript Duplex类的具体用法?TypeScript Duplex怎么用?TypeScript Duplex使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Duplex类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: removeInternalSocketHandlers
/**
* Removes internal event listeners on the underlying Socket.
*/
private removeInternalSocketHandlers() {
// Pauses data flow of the socket (this is internally resumed after 'established' is emitted)
this._socket.pause();
this._socket.removeListener('data', this._onDataReceived);
this._socket.removeListener('close', this._onClose);
this._socket.removeListener('error', this._onError);
this._socket.removeListener('connect', this.onConnect);
}
示例2: sendSocks5CommandRequest
/**
* Sends Socks v5 final handshake request.
*/
private sendSocks5CommandRequest() {
const buff = new SmartBuffer();
buff.writeUInt8(0x05);
buff.writeUInt8(SocksCommand[this._options.command]);
buff.writeUInt8(0x00);
// ipv4, ipv6, domain?
if (net.isIPv4(this._options.destination.host)) {
buff.writeUInt8(Socks5HostType.IPv4);
buff.writeBuffer(ip.toBuffer(this._options.destination.host));
} else if (net.isIPv6(this._options.destination.host)) {
buff.writeUInt8(Socks5HostType.IPv6);
buff.writeBuffer(ip.toBuffer(this._options.destination.host));
} else {
buff.writeUInt8(Socks5HostType.Hostname);
buff.writeUInt8(this._options.destination.host.length);
buff.writeString(this._options.destination.host);
}
buff.writeUInt16BE(this._options.destination.port);
this._nextRequiredPacketBufferSize =
SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseHeader;
this._socket.write(buff.toBuffer());
this.state = SocksClientState.SentFinalHandshake;
}
示例3: sendSocks4InitialHandshake
/**
* Sends initial Socks v4 handshake request.
*/
private sendSocks4InitialHandshake() {
const userId = this._options.proxy.userId || '';
const buff = new SmartBuffer();
buff.writeUInt8(0x04);
buff.writeUInt8(SocksCommand[this._options.command]);
buff.writeUInt16BE(this._options.destination.port);
// Socks 4 (IPv4)
if (net.isIPv4(this._options.destination.host)) {
buff.writeBuffer(ip.toBuffer(this._options.destination.host));
buff.writeStringNT(userId);
// Socks 4a (hostname)
} else {
buff.writeUInt8(0x00);
buff.writeUInt8(0x00);
buff.writeUInt8(0x00);
buff.writeUInt8(0x01);
buff.writeStringNT(userId);
buff.writeStringNT(this._options.destination.host);
}
this._nextRequiredPacketBufferSize =
SOCKS_INCOMING_PACKET_SIZES.Socks4Response;
this._socket.write(buff.toBuffer());
}
示例4:
const invocationCallback = (errorValue, successValue) => {
connection.end(JSON.stringify({
result: successValue,
errorMessage: errorValue && (errorValue.message || errorValue),
errorDetails: errorValue && (errorValue.stack || null)
}));
};
示例5: function
client.on('message', function(data) {
log.debug('c->s ', JSON.stringify(data));
if (data['a']=='sub' || data['a']=='bs') {
if (data['a']=='sub') {
// User is subscribing to a new document
log.debug("Got new sub");
document = data['d'];
} else { // data['a']=='bs'
var collectionDocumentVersionMap = data['s'];
var numCollections = Object.keys(collectionDocumentVersionMap).length;
if (numCollections != 1) {
log.error({message:"Zero or more than one collection not expected",value:numCollections});
client.stop();
return;
}
var cName = Object.keys(collectionDocumentVersionMap)[0];
var numDocuments = Object.keys(collectionDocumentVersionMap[cName]).length;
if (numDocuments != 1) {
log.error({message:"Zero or more than one document not expected",value:numDocuments});
client.stop();
return;
}
var docName = Object.keys(collectionDocumentVersionMap[cName])[0];
document = docName;
}
mongoStore.get(sessionId, function(err, session) {
if (err) {
log.error(err);
client.stop();
return;
}
if (!session) {
log.error({message:"Tried to get session that doesn't exist",value:rawSessionCookie});
client.stop();
return;
}
var userId = session.passport.user;
if (!userId) {
log.error({message:"Tried to get userId that doesn't exist",value:session});
client.stop();
return;
}
AuthHelper.userIdCanAccessPageId(userId, document, function(canAccess) {
if (!canAccess) {
client.stop();
return;
}
pageConnectionMap[document] = pageConnectionMap[document] ?
pageConnectionMap[document]+1 :
1;
log.info(pageConnectionMap[document] + " CLIENTS CONNECTED TO " + document);
stream.push(data);
});
});
} else {
stream.push(data);
}
});
示例6: Error
const invocationCallback = (errorValue, successValue) => {
if (hasInvokedCallback) {
throw new Error('Cannot supply more than one result. The callback has already been invoked,'
+ ' or the result stream has already been accessed');
}
hasInvokedCallback = true;
connection.end(JSON.stringify({
result: successValue,
errorMessage: errorValue && (errorValue.message || errorValue),
errorDetails: errorValue && (errorValue.stack || null)
}));
};
示例7: sendSocks5UserPassAuthentication
/**
* Sends Socks v5 user & password auth handshake.
*
* Note: No auth and user/pass are currently supported.
*/
private sendSocks5UserPassAuthentication() {
const userId = this._options.proxy.userId || '';
const password = this._options.proxy.password || '';
const buff = new SmartBuffer();
buff.writeUInt8(0x01);
buff.writeUInt8(Buffer.byteLength(userId));
buff.writeString(userId);
buff.writeUInt8(Buffer.byteLength(password));
buff.writeString(password);
this._nextRequiredPacketBufferSize =
SOCKS_INCOMING_PACKET_SIZES.Socks5UserPassAuthenticationResponse;
this._socket.write(buff.toBuffer());
this.state = SocksClientState.SentAuthentication;
}
示例8: _closeSocket
/**
* Closes and destroys the underlying Socket. Emits an error event.
* @param err { String } An error string to include in error event.
*/
private _closeSocket(err: string) {
// Make sure only one 'error' event is fired for the lifetime of this SocksClient instance.
if (this.state !== SocksClientState.Error) {
// Set internal state to Error.
this.state = SocksClientState.Error;
// Destroy Socket
this._socket.destroy();
// Remove internal listeners
this.removeInternalSocketHandlers();
// Fire 'error' event.
this.emit('error', new SocksClientError(err, this._options));
}
}
示例9: sendSocks5InitialHandshake
/**
* Sends initial Socks v5 handshake request.
*/
private sendSocks5InitialHandshake() {
const buff = new SmartBuffer();
buff.writeUInt8(0x05);
// We should only tell the proxy we support user/pass auth if auth info is actually provided.
// Note: As of Tor v0.3.5.7+, if user/pass auth is an option from the client, by default it will always take priority.
if (this._options.proxy.userId || this._options.proxy.password) {
buff.writeUInt8(2);
buff.writeUInt8(Socks5Auth.NoAuth);
buff.writeUInt8(Socks5Auth.UserPass);
} else {
buff.writeUInt8(1);
buff.writeUInt8(Socks5Auth.NoAuth);
}
this._nextRequiredPacketBufferSize =
SOCKS_INCOMING_PACKET_SIZES.Socks5InitialHandshakeResponse;
this._socket.write(buff.toBuffer());
this.state = SocksClientState.SentInitialHandshake;
}