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


TypeScript PassThrough.once方法代码示例

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


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

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