mirror of
https://github.com/denoland/deno.git
synced 2025-07-09 22:35:10 +00:00

Issue: https://github.com/denoland/deno/issues/20594
We need to implement JSStreamSocket from nodejs in
deno for rustls to terminate TLS on a JavaScript stream.
c46b2b9da3/lib/internal/js_stream_socket.js (L49)
Co-authored-by: Divy Srivastava <dj.srivastava23@gmail.com>
51 lines
1.1 KiB
JavaScript
51 lines
1.1 KiB
JavaScript
import { Duplex } from 'node:stream';
|
|
import { connect } from 'node:tls';
|
|
import { Socket } from 'node:net';
|
|
|
|
class SocketWrapper extends Duplex {
|
|
constructor(options) {
|
|
super(options);
|
|
this.socket = new Socket();
|
|
this[Deno.internal.rid] = this.socket.rid;
|
|
}
|
|
|
|
_write(chunk, encoding, callback) {
|
|
this.socket.write(chunk, encoding, callback);
|
|
}
|
|
|
|
_read(size) {
|
|
}
|
|
|
|
connect(port, host) {
|
|
this.socket.connect(port, host);
|
|
this.socket.on('data', (data) => this.push(data));
|
|
this.socket.on('end', () => this.push(null));
|
|
}
|
|
}
|
|
|
|
const socketWrapper = new SocketWrapper();
|
|
|
|
socketWrapper.connect(443, 'example.com');
|
|
|
|
const tlsSocket = connect({
|
|
socket: socketWrapper,
|
|
rejectUnauthorized: false
|
|
});
|
|
|
|
tlsSocket.on('secureConnect', () => {
|
|
console.log('TLS connection established');
|
|
tlsSocket.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n');
|
|
});
|
|
|
|
tlsSocket.on('data', (data) => {
|
|
console.log('Received:', data.length);
|
|
tlsSocket.end();
|
|
});
|
|
|
|
tlsSocket.on('end', () => {
|
|
console.log('TLS connection closed');
|
|
});
|
|
|
|
tlsSocket.on('error', (error) => {
|
|
console.error('Error:', error);
|
|
});
|