我正在使用 sveltekit 1.0.0-next.483 运行 npm run dev -- --host 使用移动设备连接到端点时出现此错误:
typeError: immutable
at Headers.append ([..]node_modules/undici/lib/fetch/headers.js:227:13)
此错误仅发生在连接到本地网络 IP 地址的移动设备上。
我的端点:src/routes/gqlendpoint/+server.ts
const base = 'http://localhost:4000/graphql';
export async function POST( opts: { request: Request} ): Promise<Response> {
const { request } = opts;
const body = await request.json();
const response = await fetch(base, {
//credentials:"include",
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
});
return response;
}
我发现解决这种情况的唯一方法是在里面注释一行代码
node_modules/undici/lib/fetch/headers.js
// 3. If headers’s guard is "immutable", then throw a TypeError.
// 4. Otherwise, if headers’s guard is "request" and name is a
// forbidden header name, return.
// Note: undici does not implement forbidden header names
if (this[kGuard] === 'immutable') {
**//throw new TypeError('immutable')**
} else if (this[kGuard] === 'request-no-cors') {
// 5. Otherwise, if headers’s guard is "request-no-cors":
// TODO
}
这当然不是一个好的解决方案。
您必须返回新的响应正文以避免此问题,请参阅下面的示例代码。
return new Response('test example')
代替返回响应;
把这个留给未来的我(和其他人):
// return res // errors
return new Response(res.body, res) // works
在尝试制作迷你代理时遇到了这个:
const makeProxy: (proxyURL: string) => RequestHandler = proxyURL => async ({params, request}) => {
let proxyNormalized = (new URL(proxyURL)).toString()
let pathPos = request.url.indexOf(params.path)
let requestedPath = request.url.substring(pathPos)
let url = proxyNormalized + requestedPath
let headers = Array.from(request.headers.entries()).filter(e => !e[0].startsWith(":"))
console.log(headers)
let res = await fetch(url, {
body: request.body,
cache: request.cache,
credentials: request.credentials,
headers,
integrity: request.integrity,
keepalive: request.keepalive,
method: request.method,
mode: request.mode,
redirect: request.redirect,
referrer: request.referrer,
referrerPolicy: request.referrerPolicy,
signal: request.signal,
})
// this looks stupid but it doesnt work otherwise
return new Response(res.body, res)
}