Error: write EPIPE when using wrangler dev to run cloudflare worker using itty-router
Asked Answered
H

1

5

I am trying to test run a cloudflare worker locally using wrangler dev (although I have the same issue when running npm start), and I keep getting an EPIPE error. The error message and code are below. I am on Ubunutu 22.04 if that is relevant. Is this a bug I should report (and, if so, to which project?), or am I doing something wrong?

Error

username@machine:~/worker$ wrangler dev
 โ›…๏ธ wrangler 3.0.0
------------------
wrangler dev now uses local mode by default, powered by ๐Ÿ”ฅ Miniflare and ๐Ÿ‘ท workerd.
To run an edge preview session for your Worker, use wrangler dev --remote
Using vars defined in .dev.vars
Your worker has access to the following bindings:
- R2 Buckets:
  - R2_BUCKET: bucket
โŽ” Starting local server...
[mf:wrn] The latest compatibility date supported by the installed Cloudflare Workers Runtime is "2023-05-12",
but you've requested "2023-05-18". Falling back to "2023-05-12"...
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ [b] open a browser, [d] open Devtools, [l] turn off local mode, [c] clear console, [x] to exit                                           โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
/home/username/.nvm/versions/node/v20.1.0/lib/node_modules/wrangler/wrangler-dist/cli.js:30632
            throw a;
            ^

Error: write EPIPE
    at afterWriteDispatched (node:internal/stream_base_commons:160:15)
    at writeGeneric (node:internal/stream_base_commons:151:3)
    at Socket._writeGeneric (node:net:946:11)
    at Socket._write (node:net:958:8)
    at writeOrBuffer (node:internal/streams/writable:399:12)
    at _write (node:internal/streams/writable:340:10)
    at Writable.write (node:internal/streams/writable:344:10)
    at Runtime.updateConfig (/home/username/.nvm/versions/node/v20.1.0/lib/node_modules/wrangler/node_modules/miniflare/dist/src/index.js:5120:26)
    at async #assembleAndUpdateConfig (/home/username/.nvm/versions/node/v20.1.0/lib/node_modules/wrangler/node_modules/miniflare/dist/src/index.js:9130:23)
    at async #init (/home/username/.nvm/versions/node/v20.1.0/lib/node_modules/wrangler/node_modules/miniflare/dist/src/index.js:8894:5)
Emitted 'error' event on Socket instance at:
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  errno: -32,
  code: 'EPIPE',
  syscall: 'write'
}

Node.js v20.1.0

Code

import { Router, RouterType, IRequest } from 'itty-router'

export interface Env {
    R2_BUCKET: R2Bucket;
}

const router: RouterType = Router();

const get_static = async (req: IRequest, env: Env): Promise<Response> => {
    const url = new URL(req.url);
    const key = url.pathname.slice(1);
    const object = await env.R2_BUCKET.get(key);
    if (object == null) {
        return new Response('404, not found!', { status: 404 });
    }
    const headers = new Headers();
    object.writeHttpMetadata(headers);
    headers.set('etag', object.etag);
    return new Response(object.body, { headers });
}

router.all('*', get_static);

export default {
    async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
       return await router.handle(request, env, ctx);
    },
};

I do not get the error when running the hello world worker script, but once I import itty-router and try to run, the error occurs. I expected the worker to run a local server normally, but the error shown above results instead.

Hornbill answered 18/5, 2023 at 14:46 Comment(0)
H
6

My issue was fixed by installing libc++1 via apt install libc++1 per the advice in the github issue.

Hornbill answered 19/5, 2023 at 1:55 Comment(1)
This was useful, thanks. I also had to upgrade my wsl2 debian version to 12 - ubuntu wsl2 users will need to upgrade to 22 as well. โ€“ Callie

© 2022 - 2024 โ€” McMap. All rights reserved.