Hyperlinkv0.8.0-beta.28

NodeHttpPlatform

Effect<
  {
    readonly fileResponse: (
      path: string,
      options?: ServerResponse.Options.WithContent & {
        readonly bytesToRead?: SizeInput | undefined
        readonly chunkSize?: SizeInput | undefined
        readonly offset?: SizeInput | undefined
      }
    ) => Effect<ServerResponse.HttpServerResponse, PlatformError>
    readonly fileWebResponse: (
      file: HttpBody.FileLike,
      options?: ServerResponse.Options.WithContent & {
        readonly bytesToRead?: SizeInput | undefined
        readonly chunkSize?: SizeInput | undefined
        readonly offset?: SizeInput | undefined
      }
    ) => Effect<ServerResponse.HttpServerResponse>
  },
  never,
  EtagImpl.Generator | FileSystem
>

Creates the Node HttpPlatform, serving file responses from Node readable streams and adding MIME type and content-length headers when needed.

constructors
export const make = Platform.make({
  fileResponse(path, status, statusText, headers, start, end, contentLength) {
    const stream = contentLength === 0
      ? Readable.from([])
      : Fs.createReadStream(path, { start, end: end === undefined ? undefined : end - 1 })
    return ServerResponse.raw(stream, {
      headers: {
        ...headers,
        "content-type": headers["content-type"] ?? Mime.getType(path) ?? "application/octet-stream",
        "content-length": contentLength.toString()
      },
      status,
      statusText
    })
  },
  fileWebResponse(file, status, statusText, headers, _options) {
    return ServerResponse.raw(Readable.fromWeb(file.stream() as any), {
      headers: Headers.merge(
        headers,
        Headers.fromRecordUnsafe({
          "content-type": headers["content-type"] ?? Mime.getType(file.name) ?? "application/octet-stream",
          "content-length": file.size.toString()
        })
      ),
      status,
      statusText
    })
  }
})
Referenced by 1 symbols