當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。