I think it streams instead of buffers by default, based on the fact that waiting until fully buffere
I think it streams instead of buffers by default, based on the fact that waiting until fully buffered is an enterprise feature.

*.example.com/* or */* worker route, AAAA *.example.com 100:: Proxied record)api.example.com/*: service none (for normal origins)*/* to match all or *.example.com/* to match all subdomains besides apex, service: Worker*.workers.example.com)

fetch subrequest isn't going to use up any CPUdo stuff with reader?the event stream might not finish within 30sIt's 30 seconds of CPU time, none of this is particularly heavy compute.
const response = await ... and then return response, couldn't you?
*.example.com/**.example.com/*api.example.com/**.workers.example.comdo stuff with readerconst response = await ...return response const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${env.OPENAI_API_KEY}`,
},
body,
})
.then((res) => response.body)
.then((body) => {
const reader = body.getReader()
// do stuff with reader
})const startTime = Date.now()
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${env.OPENAI_API_KEY}`,
},
body,
})
if (!response.ok) {
console.log(
`Unexpected response from OpenAI API: ${response.status} ${response.statusText}`
)
return
}
if (!response.body) {
console.log("No response body")
return
}
const reader = response.body.getReader()
// Get first byte
let chunk = await reader.read()
// Log first byte time
const firstByteTime = Date.now() - startTime
// Read the rest of the response
while (!chunk.done) {
chunk = await reader.read()
} const totalTime = Date.now() - startTime
console.log("Time to first byte: " + firstByteTime + "ms")
console.log("Total response time: " + totalTime + "ms")
// Log response time
const client = new Client(env.DB_URL + "?sslmode=require")
await client.connect()
const text =
"INSERT INTO response_times(model, date, ttfb, duration) VALUES($1, $2, $3, $4) RETURNING *"
const values = [
"gpt-4",
new Date(event.scheduledTime),
firstByteTime,
totalTime,
]
try {
const res = await client.query(text, values)
console.log(res.rows[0])
} catch (err) {
console.error(err)
} finally {
ctx.waitUntil(client.end())
}