• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6const h2 = require('http2');
7const tls = require('tls');
8const fixtures = require('../common/fixtures');
9const { Duplex } = require('stream');
10
11const server = h2.createSecureServer({
12  key: fixtures.readKey('agent1-key.pem'),
13  cert: fixtures.readKey('agent1-cert.pem')
14});
15
16class JSSocket extends Duplex {
17  constructor(socket) {
18    super({ emitClose: true });
19    socket.on('close', () => this.destroy());
20    socket.on('data', (data) => this.push(data));
21    this.socket = socket;
22  }
23
24  _write(data, encoding, callback) {
25    this.socket.write(data, encoding, callback);
26  }
27
28  _read(size) {
29  }
30
31  _final(cb) {
32    cb();
33  }
34}
35
36server.listen(0, common.mustCall(function() {
37  const socket = tls.connect({
38    rejectUnauthorized: false,
39    host: 'localhost',
40    port: this.address().port,
41    ALPNProtocols: ['h2']
42  }, () => {
43    const proxy = new JSSocket(socket);
44    const client = h2.connect(`https://localhost:${this.address().port}`, {
45      createConnection: () => proxy
46    });
47    const req = client.request();
48
49    server.on('request', () => {
50      socket.destroy();
51    });
52
53    req.on('close', common.mustCall(() => {
54      client.close();
55      server.close();
56    }));
57  });
58}));
59