question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`TypeError: at normalizeAndValidateHeaderValue` with data proxy in cloudflare workers/miniflare

See original GitHub issue

Bug description

Client is bugged in my closed source project but also in this open source project I found it so using that as an example.

When using Prisma Data Proxy in Cloudflare Workers with prisma 3.10.0 and miniflare 2.3.0 (also using remix 1.2.3 but probably not related) an error occurs when making any request since the transactionId header is passed as undefined in prisma causing a header normalization error (since undefined isn’t a valid header).

Workaround

I’ve managed to fix the error locally by modifying line 93 col 13582 of node_modules/@prisma/client/runtime/proxy.js to be const headers={...n};if(typeof t=="string"&&r){headers.transactionId= t}return{inTx:typeof t=="number"&&r?!0:void 0,headers} instead of return{inTx:typeof t=="number"&&r?!0:void 0,headers:{transactionId:typeof t=="string"&&r?t:void 0,...n} since in the bugged code the transactionId header will be undefined if not present, thus causing the normalizeAndValidateHeaderValue error.

How to reproduce

  1. Go to https://github.com/marcomafessolli/remix-prisma-cloudflare-workers/tree/d86bb949016ce35f805ab113f4df89f3e96b3644 (hard link for current point in time of master branch)
  2. Git clone that project
  3. Follow the steps to run the project (add an .env, npm i, PRISMA_CLIENT_ENGINE_TYPE=dataproxy npx prisma generate and create your prisma data proxy project if not already using one)
  4. Run npm run dev
  5. Open the root index page for the website (only one page exists).
  6. See error

Expected behavior

TypeError:
[dev:*worker]     at normalizeAndValidateHeaderValue (`<PATH>/remix-prisma-cloudflare-workers/node_modules/undici/lib/fetch/headers.js:42:11`)
[dev:*worker]     at HeadersList.append (`<PATH>/remix-prisma-cloudflare-workers/node_modules/undici/lib/fetch/headers.js:98:29`)
[dev:*worker]     at fill (`<PATH>/remix-prisma-cloudflare-workers/node_modules/undici/lib/fetch/headers.js:86:15`)
[dev:*worker]     at new Request (`<PATH>/remix-prisma-cloudflare-workers/node_modules/undici/lib/fetch/request.js:433:9`)
[dev:*worker]     at new Request (`<PATH>/remix-prisma-cloudflare-workers/node_modules/@miniflare/core/src/standards/http.ts:407:13`)
[dev:*worker]     at upgradingFetch (`<PATH>/remix-prisma-cloudflare-workers/node_modules/@miniflare/web-sockets/src/fetch.ts:20:19`)
[dev:*worker]     at WebSocketPlugin.<anonymous> (`<PATH>/remix-prisma-cloudflare-workers/node_modules/@miniflare/core/src/standards/http.ts:895:21`)
[dev:*worker]     at fetch (`<PATH>/remix-prisma-cloudflare-workers/node_modules/@miniflare/web-sockets/src/plugin.ts:51:28`)
[dev:*worker]     at request (`<PATH>/remix-prisma-cloudflare-workers/node_modules/@prisma/client/runtime/proxy.js:25:12296`)
[dev:*worker]     at Object.requestInternal (`<PATH>/remix-prisma-cloudflare-workers/node_modules/@prisma/client/runtime/proxy.js:27:285`)

Prisma information

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["mongoDb", "dataProxy"]
}

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

model User {
  id    String  @id @default(auto()) @map("_id") @db.ObjectId
  email String  @unique
  name  String?
}

model Log {
  id      String @id @default(auto()) @map("_id") @db.ObjectId
  level   Level
  message String
  meta    Json
}

enum Level {
  Info
  Warn
  Error
}

Environment & setup

OS: Windows using Fedora 35 via wsl2 Database: PostgreSQL Miniflare: 2.3.0 Prisma: 3.10.0

Prisma Version

3.10.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:7
  • Comments:17 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
millspcommented, Jun 7, 2022

Hey everyone, hey @kettanaito 👋. We have released this fix a few minutes ago in 3.15.0. I can confirm that I could get the reproduction working again, and the warnings around eval are also gone. tl;dr;

  • Instead of PRISMA_CLIENT_ENGINE_TYPE, use prisma generate --data-proxy to enable the Data Proxy
  • import { PrismaClient } from '@prisma/client/edge' for a Workers-compatible Prisma Client
  • Please be aware that .env aren’t bundled into the generated Prisma Client any longer (see docs)
  • You can also remove any aliasing from your build scripts

Thanks everyone, and your feedback is welcome.

2reactions
millspcommented, Jun 10, 2022

You should be able to fully remove this alias and just replace your @prisma/client import with @prisma/client/edge.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Changelog - Miniflare
Throw TypeError when calling Fetcher#fetch with an illegal this to match the behaviour of the Workers runtime. ​2.8.2. ​Fixes. Allow WebSocket client connection ......
Read more >
Prisma 3.15.0 Release - GitClear
Sveltekit + Cloudflare adapter - "PrismaClient is unable to be run in the browser." TypeError: at normalizeAndValidateHeaderValue with data ...
Read more >
Quickstarts · Cloudflare Workers docs
A complete starter template using Sunder, TypeScript, Miniflare, esbuild, Jest, and Sass. Uses Workers Sites for static assets. Copy. npx ...
Read more >
Cloudflare Worker TypeError: One-time-use body
I'm trying to use a Cloudflare Worker to proxy a POST request to another server. It is throwing a JS exception – by...
Read more >
Cloudflare workers | Everything I know - My Knowledge Wiki
Can build and release websites fully on Cloudflare stack. Wrangler & Miniflare are useful (nice template for it).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found