Skip to content

Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close in Node Pipeline stream #60

@gotham8x

Description

@gotham8x

Thanks for a good work on open source.

I am using node-server to run a very simple web page, but I encounter the error Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close quite often. After investigating alternative servers, I discovered that miniflare is also aware of this issue and they don't use pipeline(..) in their implementation. You can see their implementation at https://github.com/cloudflare/miniflare/blob/7e4d906e19cc69cd3446512bfeb7f8aee3a2bda7/packages/http-server/src/index.ts#L215-L241.

Although miniflare works well, it is quite heavy. In order to have a stable and smooth runtime, I think we should follow miniflare's approach in this implementation.


My code

async function mainFetch(req: Request, _env: Record<string, string>): Promise<Response> {
  const u = new URL(req.url);
  if (u.pathname === '/favicon.ico') {
   // seems like issue went here
    return fetch('https://my-internal-site/favicon.ico');
  }
  return new Response('OK');
}

serve(
  {
    fetch(r) {
      return mainFetch(r, {});
    },
    port: 3535,
  },
  (info) => console.log(`http://localhost:${info.port}`)
);

Run the complied code and access the webpage.

$ node dist/index.cjs 
http://localhost:3535
Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close
    at new NodeError (node:internal/errors:399:5)
    at ServerResponse.onclose (node:internal/streams/end-of-stream:154:30)
    at ServerResponse.emit (node:events:525:35)
    at emitCloseNT (node:_http_server:965:10)
    at Socket.onServerResponseClose (node:_http_server:277:5)
    at Socket.emit (node:events:525:35)
    at TCP.<anonymous> (node:net:322:12) {
  code: 'ERR_STREAM_PREMATURE_CLOSE'
}

CC: @yusukebe

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions