当前位置: 首页>>代码示例>>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;未经允许,请勿转载。