當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript Writable.pipe方法代碼示例

本文整理匯總了TypeScript中stream.Writable.pipe方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Writable.pipe方法的具體用法?TypeScript Writable.pipe怎麽用?TypeScript Writable.pipe使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在stream.Writable的用法示例。


在下文中一共展示了Writable.pipe方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: accepts

export function httpRespond<E>({req, res, type, value}: HttpParams<E>, callback?: ErrCallback) {
	assert.instanceOf(type, AbstractType)
	if (callback === undefined) callback = _ => {}
	assert.instanceOf(callback, Function)
	assert.instanceOf(req, http.IncomingMessage)
	assert.instanceOf(res, http.OutgoingMessage)
	try {
		res.setHeader('Content-Type', 'application/octet-stream')
		res.setHeader('sig', type.getSignature())
		const acceptsGzip = accepts(req).encoding(['gzip'])
		let outStream: Writable
		if (acceptsGzip) {
			res.setHeader('Content-Encoding', 'gzip')
			outStream = zlib.createGzip() //pipe into a zip stream to decrease size of response
		}
		else outStream = res
		function writeEndCallback(err: Error | null) {
			if (err) callback!(err)
			else if (!acceptsGzip) callback!(null)
		}
		if (req.headers.sig && req.headers.sig === type.getSignature()) { //if client already has type, only value needs to be sent
			writeValue({type, value, outStream}, writeEndCallback)
		}
		else writeTypeAndValue({type, value, outStream}, writeEndCallback) //otherwise, type and value need to be sent
		if (acceptsGzip) { //don't pipe until writing begins
			outStream.pipe(res)
				.on('finish', () => callback!(null))
		}
	}
	catch (err) { callback(err) }
}
開發者ID:calebsander,項目名稱:structure-bytes,代碼行數:31,代碼來源:io.ts

示例2: writeHead

    const fetchCallback = (err: any, v8res: any, resBody: V8ResponseBody) => {
      if (cbCalled) {
        return // this can't happen twice
      }
      cbCalled = true

      if (err) {
        log.error("error from fetch callback:", err)

        writeHead(rt, res, 500)
        res.end("Error: " + err)
        return reject(err)
      }

      // tslint:disable-next-line:forin
      for (let n in v8res.headers) {
        try {
          n = n.trim()
          if (/^server$/i.test(n)) {
            continue
          }

          const val = v8res.headers[n]

          res.setHeader(n, val)
        } catch (err) {
          log.error("error setting header", err)
        }
      }

      for (const n of hopHeaders) {
        res.removeHeader(n)
      }

      let dst: Writable = res
      let contentEncoding = res.getHeader("content-encoding")
      if (contentEncoding && contentEncoding instanceof Array) {
        contentEncoding = contentEncoding.join(",")
      }
      if (typeof contentEncoding === "string" && contentEncoding.includes("skip")) {
        // skip is a magical content-encoding header that both skips gzip, and clears the header
        res.removeHeader("content-encoding")
      }
      let contentType = res.getHeader("content-type")
      let acceptEncoding = req.headers["accept-encoding"]
      if (acceptEncoding && acceptEncoding instanceof Array) {
        acceptEncoding = acceptEncoding.join(", ")
      }

      // gzip if no encoding
      if (!contentEncoding && contentType && acceptEncoding && acceptEncoding.includes("gzip")) {
        if (contentType && contentType instanceof Array) {
          contentType = contentType.join(", ")
        } else {
          contentType = contentType.toString()
        }
        // only gzip text
        if (
          contentType.includes("text/") ||
          contentType.includes("application/javascript") ||
          contentType.includes("application/x-javascript") ||
          contentType.includes("application/json")
        ) {
          res.removeHeader("Content-Length")
          res.setHeader("Content-Encoding", "gzip")
          dst = zlib.createGzip({
            level: 2
          })
          dst.pipe(res)
        }
      }

      writeHead(rt, res, v8res.status)

      handleResponse(rt, resBody, res, dst)
        .then(() => {
          const dataOut = req.connection.bytesWritten - startBytes
          rt.reportUsage("http", {
            data_out: dataOut
          })
          if (!res.finished) {
            res.end()
          } // we are done. triggers 'finish' event
          resolve(dataOut)
        })
        .catch(reject)
    }
開發者ID:dianpeng,項目名稱:fly,代碼行數:87,代碼來源:server.ts


注:本文中的stream.Writable.pipe方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。