Open devinpitcher opened 3 months ago
Looks like we need to make the stringification of Headers in the context of this call more robust in Miniflare. Does this code work in production?
Same error here, when uploading to a bucket. Only happens in local environment in a Next.js full-stack project.
R2, Miniflare
3.60.3 [wrangler], 3.20240610.0 [miniflare], 1.11.3 [@cloudflare/next-on-pages]
20.12.2
Ubuntu 22.04.3 LTS
When attempting to upload through env.BUCKET.put
, get [DevalueError]: Cannot stringify arbitrary non-POJOs
error.
⨯ Error [DevalueError]: Cannot stringify arbitrary non-POJOs
at flatten (file:///home/ken/dev/ai-seller/node_modules/miniflare/dist/src/index.js:3035:19)
at flatten (file:///home/ken/dev/ai-seller/node_modules/miniflare/dist/src/index.js:3062:50)
at flatten (file:///home/ken/dev/ai-seller/node_modules/miniflare/dist/src/index.js:3008:22)
at stringify (file:///home/ken/dev/ai-seller/node_modules/miniflare/dist/src/index.js:3072:17)
at stringifyWithStreams (file:///home/ken/dev/ai-seller/node_modules/miniflare/dist/src/index.js:3265:28)
at #call (file:///home/ken/dev/ai-seller/node_modules/miniflare/dist/src/index.js:6338:25)
at Proxy.put (file:///home/ken/dev/ai-seller/node_modules/miniflare/dist/src/index.js:6325:34)
at POST (webpack-internal:///(rsc)/./app/api/cos/route.ts:49:116)
at eval (webpack-internal:///(rsc)/./node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:218:43)
at eval (webpack-internal:///(rsc)/./node_modules/next/dist/esm/server/lib/trace/tracer.js:108:36)
at NoopContextManager.with (webpack-internal:///(rsc)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:361:30)
at ContextAPI.with (webpack-internal:///(rsc)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:31:58)
at NoopTracer.startActiveSpan (webpack-internal:///(rsc)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:954:34)
at ProxyTracer.startActiveSpan (webpack-internal:///(rsc)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:994:36)
at eval (webpack-internal:///(rsc)/./node_modules/next/dist/esm/server/lib/trace/tracer.js:97:103)
at NoopContextManager.with (webpack-internal:///(rsc)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:361:30)
at ContextAPI.with (webpack-internal:///(rsc)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:31:58)
at NextTracerImpl.trace (webpack-internal:///(rsc)/./node_modules/next/dist/esm/server/lib/trace/tracer.js:97:28)
at eval (webpack-internal:///(rsc)/./node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:206:91)
at AsyncLocalStorage.run (node:async_hooks:346:14)
at Object.wrap (webpack-internal:///(rsc)/./node_modules/next/dist/esm/server/async-storage/static-generation-async-storage-wrapper.js:44:24)
at eval (webpack-internal:///(rsc)/./node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:160:288)
at AsyncLocalStorage.run (node:async_hooks:346:14)
at Object.wrap (webpack-internal:///(rsc)/./node_modules/next/dist/esm/server/async-storage/request-async-storage-wrapper.js:81:24)
at eval (webpack-internal:///(rsc)/./node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:160:117)
at AsyncLocalStorage.run (node:async_hooks:346:14)
at AppRouteRouteModule.execute (webpack-internal:///(rsc)/./node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:157:56)
at AppRouteRouteModule.handle (webpack-internal:///(rsc)/./node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:280:41)
at EdgeRouteModuleWrapper.handler (webpack-internal:///(rsc)/./node_modules/next/dist/esm/server/web/edge-route-module-wrapper.js:92:44)
at eval (webpack-internal:///(rsc)/./node_modules/next/dist/esm/server/web/adapter.js:195:23)
at NoopContextManager.with (webpack-internal:///(rsc)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:361:30)
at ContextAPI.with (webpack-internal:///(rsc)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:31:58)
at NextTracerImpl.withPropagatedContext (webpack-internal:///(rsc)/./node_modules/next/dist/esm/server/lib/trace/tracer.js:63:28)
at propagator (webpack-internal:///(rsc)/./node_modules/next/dist/esm/server/web/adapter.js:63:19)
at adapter (webpack-internal:///(rsc)/./node_modules/next/dist/esm/server/web/adapter.js:176:22)
import type { NextRequest } from 'next/server'
import { getRequestContext } from '@cloudflare/next-on-pages';
export const runtime = 'edge'
export async function POST(req: NextRequest) {
const object = await getRequestContext().env.BUCKET.put('test-key', req.body, {
httpMetadata: req.headers,
})
return new Response(null, {headers: {'etag': object.httpEtag,}})
}
This code works fine in production.
Btw, how do I access a real remote R2 in local development in a full-stack Pages project, when I am supposed to use npm run dev
directly?
Looks like we need to make the stringification of Headers in the context of this call more robust in Miniflare. Does this code work in production?
This is a pretty fresh project I'm working on, have not pushed to production for testing yet. I would assume it would work in production since it's just pulling a simple JPEG from R2. Must be some sort of issue with the miniflare simulator.
I have the same problem with a full-stack Next.js app running locally with service binding. If my service call returns a string or number, it works as expected. But if the service call returns an object, it doesn't work, and I get the DevalueError: Cannot stringify POJOs with symbolic keys
I have the same problem with a full-stack Next.js app running locally with service binding. If my service call returns a string or number, it works as expected. But if the service call returns an object, it doesn't work, and I get the
DevalueError: Cannot stringify POJOs with symbolic keys
I roll-backed to wrangler@3.51
and it's now working (from wrangler@3.62
).
Same problem with an Astro CF Pages project with a local service binding. Latest wrangler I could get to work was wrangler@3.59
I'm running into this as well.
Interestingly, if I import Headers
from miniflare (like here), writeHttpMetadata
works fine. Without the import, the default Headers
interface from TypeScript's libdom will cause the mentioned Cannot stringify arbitrary non-POJOs
error.
So this seems to be a type issue.
I use Rimix Vite Plugin. and same error.
Thanks to @mlafeldt , This is my workaround.
const Headers_ =
process.env.NODE_ENV === 'development' ? (await import('miniflare')).Headers : Headers
const headers = new Headers_() as any
object.writeHttpMetadata(headers)
In local development, 'miniflare'.Headers works. In Cloudflare Workers, normal Headers works
Which Cloudflare product(s) does this pertain to?
R2, Miniflare
What version(s) of the tool(s) are you using?
3.60.3 [wrangler], 3.20240610.0 [miniflare]
What version of Node are you using?
18.18.2
What operating system and version are you using?
macOS Sonoma 14.4.1 (23E224)
Describe the Bug
Observed behavior
On a basic Next.js route handler, when attempting to
writeHttpMetadata
from an R2 Object tonew Headers()
, I get aDevalueError: Cannot stringify arbitrary non-POJOs
error.Expected behavior
The headers from the R2 object would be appended to the headers instance.
Steps to reproduce
wrangler.toml
Please provide a link to a minimal reproduction
No response
Please provide any relevant error logs
No response