当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript PassThrough.on方法代码示例

本文整理汇总了TypeScript中stream.PassThrough.on方法的典型用法代码示例。如果您正苦于以下问题:TypeScript PassThrough.on方法的具体用法?TypeScript PassThrough.on怎么用?TypeScript PassThrough.on使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在stream.PassThrough的用法示例。


在下文中一共展示了PassThrough.on方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: createStream

 /**
  * Creates a new object Stream which emits the resource on 'data' event.
  */
 createStream(apiCall: GaxCall, request: {}, options: CallSettings):
     Transform {
   const stream = new PassThrough({objectMode: true});
   options = Object.assign({}, options, {autoPaginate: false});
   const maxResults = 'maxResults' in options ? options.maxResults : -1;
   let pushCount = 0;
   let started = false;
   function callback(err: Error|null, resources, next) {
     if (err) {
       stream.emit('error', err);
       return;
     }
     for (let i = 0; i < resources.length; ++i) {
       if (ended(stream)) {
         return;
       }
       if (resources[i] === null) {
         continue;
       }
       stream.push(resources[i]);
       pushCount++;
       if (pushCount === maxResults) {
         stream.end();
       }
     }
     if (ended(stream)) {
       return;
     }
     if (!next) {
       stream.end();
       return;
     }
     // When pageToken is specified in the original options, it will overwrite
     // the page token field in the next request. Therefore it must be cleared.
     if ('pageToken' in options) {
       delete options.pageToken;
     }
     if (stream.isPaused()) {
       request = next;
       started = false;
     } else {
       setImmediate(apiCall, next, options, callback);
     }
   }
   stream.on('resume', () => {
     if (!started) {
       started = true;
       apiCall(request, options, callback);
     }
   });
   return stream;
 }
开发者ID:googleapis,项目名称:gax-nodejs,代码行数:55,代码来源:pageDescriptor.ts

示例2: Promise

        return new Promise((resolve, reject) => {
          const arg: any = urlLib.parse(url)
          const isHttp = arg.protocol !== 'https:'
          const engine: typeof httpRequest = isHttp ? httpRequest : httpsRequest

          // Always attach certain options.
          arg.method = method
          arg.headers = request.toHeaders()
          arg.agent = options.agent
          arg.rejectUnauthorized = options.rejectUnauthorized !== false
          arg.ca = options.ca
          arg.cert = options.cert
          arg.key = options.key

          const req = engine(arg)

          // Track upload progress through a stream.
          const requestProxy = new PassThrough()
          const responseProxy = new PassThrough()

          requestProxy.on('data', function (chunk: Buffer) {
            request.uploadedBytes = request.uploadedBytes + chunk.length
          })

          requestProxy.on('end', function () {
            request.uploadedBytes = request.uploadLength
          })

          responseProxy.on('data', function (chunk: Buffer) {
            request.downloadedBytes = request.downloadedBytes + chunk.length
          })

          responseProxy.on('end', function () {
            request.downloadedBytes = request.downloadLength
          })

          // Handle the HTTP response.
          function response (res: IncomingMessage) {
            const status = res.statusCode
            const redirect = REDIRECT_STATUS[status]

            // Handle HTTP redirects.
            if (followRedirects && redirect != null && res.headers.location) {
              const newUrl = urlLib.resolve(url, res.headers.location)

              // Ignore the result of the response on redirect.
              res.resume()

              // Kill the old cookies on redirect.
              request.remove('Cookie')

              if (redirect === REDIRECT_TYPE.FOLLOW_WITH_GET) {
                // Update the "Content-Length" for updated redirection body.
                request.set('Content-Length', '0')

                return get(newUrl, 'GET')
              }

              if (redirect === REDIRECT_TYPE.FOLLOW_WITH_CONFIRMATION) {
                // Following HTTP spec by automatically redirecting with GET/HEAD.
                if (arg.method === 'GET' || arg.method === 'HEAD') {
                  return get(newUrl, method, body)
                }

                // Allow the user to confirm redirect according to HTTP spec.
                if (confirmRedirect(req, res)) {
                  return get(newUrl, method, body)
                }
              }
            }

            request.downloadLength = num(res.headers['content-length'], 0)

            // Track download progress.
            res.pipe(responseProxy)

            return Promise.resolve({
              body: responseProxy,
              status: status,
              statusText: res.statusMessage,
              headers: res.headers,
              rawHeaders: res.rawHeaders,
              url: url
            })
          }

          // Handle the response.
          req.once('response', function (message: IncomingMessage) {
            return resolve(setCookies(request, message).then(() => response(message)))
          })

          // io.js has an abort event instead of "error".
          req.once('abort', function () {
            return reject(request.error('Request aborted', 'EABORT'))
          })

          req.once('error', function (error: Error) {
            return reject(request.error(`Unable to connect to "${url}"`, 'EUNAVAILABLE', error))
          })

//.........这里部分代码省略.........
开发者ID:garethhunt,项目名称:popsicle,代码行数:101,代码来源:index.ts


注:本文中的stream.PassThrough.on方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。