Skip to content

Instantly share code, notes, and snippets.

@some-say
Forked from paambaati/got.js
Created March 1, 2023 00:18
Show Gist options
  • Select an option

  • Save some-say/1423e03b7af1b650b4f68b55aa41aae1 to your computer and use it in GitHub Desktop.

Select an option

Save some-say/1423e03b7af1b650b4f68b55aa41aae1 to your computer and use it in GitHub Desktop.

Revisions

  1. @paambaati paambaati revised this gist Dec 24, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion got.js
    Original file line number Diff line number Diff line change
    @@ -59,7 +59,7 @@ const got = gotExtend({
    };
    const session = await http2.connect(options.href, randomConnectSettings);
    session.unref();
    options.request = http2.request(options.href, {session});
    options.request = http2.request({...options, session});
    }

    options.createConnection = () => socket;
  2. @paambaati paambaati revised this gist Dec 24, 2018. No changes.
  3. @paambaati paambaati revised this gist Dec 24, 2018. 1 changed file with 9 additions and 3 deletions.
    12 changes: 9 additions & 3 deletions got.js
    Original file line number Diff line number Diff line change
    @@ -51,9 +51,15 @@ const got = gotExtend({
    };

    const {alpnProtocol, socket} = await resolveALPN(socketOptions);

    if (alpnProtocol === 'h2') {
    options.request = http2.request;
    const randomConnectSettings = {
    headerTableSize: 65536,
    maxConcurrentStreams: 1000,
    initialWindowSize: 6291456,
    };
    const session = await http2.connect(options.href, randomConnectSettings);
    session.unref();
    options.request = http2.request(options.href, {session});
    }

    options.createConnection = () => socket;
    @@ -69,4 +75,4 @@ const got = gotExtend({

    const {body: h1body} = await got('https://httpbin.org/headers', {json: true});
    console.log('HTTP1', h1body);
    })();
    })();
  4. @szmarczak szmarczak revised this gist Dec 23, 2018. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions got.js
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,6 @@ const {extend: gotExtend} = require('got');
    const http2 = require('http2-wrapper');
    const resolveALPN = require('resolve-alpn');

    const h2got = gotExtend({request: http2.request});

    // Taken from https://github.com/nodejs/node/blob/d4c91f28148af8a6c1a95392e5c88cb93d4b61c6/lib/_http_agent.js
    //
    // throws
  5. @szmarczak szmarczak created this gist Dec 23, 2018.
    74 changes: 74 additions & 0 deletions got.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    const {extend: gotExtend} = require('got');
    const http2 = require('http2-wrapper');
    const resolveALPN = require('resolve-alpn');

    const h2got = gotExtend({request: http2.request});

    // Taken from https://github.com/nodejs/node/blob/d4c91f28148af8a6c1a95392e5c88cb93d4b61c6/lib/_http_agent.js
    //
    // throws
    // tls.connect({host: 'httpbin.org', port: 443});
    //
    // doesn't throw
    // tls.connect({host: 'httpbin.org', port: 443, servername: 'httpbin.org'});
    const calculateServerName = options => {
    let servername = options.host;
    const hostHeader = options.headers.host;

    if (hostHeader) {
    // abc => abc
    // abc:123 => abc
    // [::1] => ::1
    // [::1]:123 => ::1
    if (hostHeader.startsWith('[')) {
    const index = hostHeader.indexOf(']');
    if (index === -1) {
    // Leading '[', but no ']'. Need to do something...
    servername = hostHeader;
    } else {
    servername = hostHeader.substr(1, index - 1);
    }
    } else {
    servername = hostHeader.split(':', 1)[0];
    }
    }

    return servername;
    };

    const got = gotExtend({
    hooks: {
    beforeRequest: [
    async options => {
    if (options.protocol === 'https:' && !options.request && !options.createConnection) {
    const socketOptions = {
    ...options,
    host: options.hostname || options.host,
    path: options.socketPath,
    port: options.port || 443,
    session: options.socketSession,
    servername: options.servername || calculateServerName(options),
    ALPNProtocols: ['h2', 'http/1.1'],
    resolveSocket: true
    };

    const {alpnProtocol, socket} = await resolveALPN(socketOptions);

    if (alpnProtocol === 'h2') {
    options.request = http2.request;
    }

    options.createConnection = () => socket;
    }
    }
    ]
    }
    });

    (async () => {
    const {body: h2body} = await got('https://nghttp2.org/httpbin/headers', {json: true});
    console.log('HTTP2', h2body);

    const {body: h1body} = await got('https://httpbin.org/headers', {json: true});
    console.log('HTTP1', h1body);
    })();