How to use the fetch-h2.StreamBody function in fetch-h2

To help you get started, we’ve selected a few fetch-h2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zeit / now / src / util / agent.js View on Github external
const { body } = opts;
    if (this._agent) {
      opts.agent = this._agent;
    }

    if (body && typeof body === 'object' && typeof body.pipe !== 'function') {
      opts.headers['Content-Type'] = 'application/json';
      opts.body = new JsonBody(body);
    }

    if (
      body &&
      typeof body === 'object' &&
      typeof body.pipe === 'function'
    ) {
      opts.body = new StreamBody(body);
    }

    const handleCompleted = async res => {
      currentContext.ongoingFetches--;
      if (
        currentContext !== this._currContext &&
        currentContext.ongoingFetches <= 0
      ) {
        // We've completely moved on to a new socket
        // close the old one

        // TODO: Fix race condition:
        // If the response is a stream, and the server is still streaming data
        // we should check if the stream has closed before disconnecting
        // hasCompleted CAN technically be called before the res body stream is closed
        debug('Closing old socket');
github zeit / now / src / providers / sh / util / agent.js View on Github external
const { body } = opts
    if (this._agent) {
      opts.agent = this._agent
    }

    if (body && typeof body === 'object' && typeof body.pipe !== 'function') {
      opts.headers['Content-Type'] = 'application/json'
      if (shouldUseHttp2(this)) {
        opts.body = new JsonBody(body)
      } else {
        opts.body = JSON.stringify(body)
      }
    }

    if(shouldUseHttp2(this) && body && typeof body === 'object' && typeof body.pipe === 'function') {
      opts.body = new StreamBody(body)
    }

    if (!shouldUseHttp2(this) && opts.body && typeof body.pipe !== 'function') {
      opts.headers['Content-Length'] = Buffer.byteLength(opts.body)
    }

    const handleCompleted = async (res) => {
      currentContext.ongoingFetches--;
      if(currentContext !== this._currContext && currentContext.ongoingFetches <= 0) {
        // We've completely moved on to a new socket
        // close the old one

        // TODO: Fix race condition:
        // If the response is a stream, and the server is still streaming data
        // we should check if the stream has closed before disconnecting
        // hasCompleted CAN technically be called before the res body stream is closed

fetch-h2

HTTP/1+2 Fetch API client for Node.js

MIT
Latest version published 3 years ago

Package Health Score

50 / 100
Full package analysis