Created
July 15, 2025 06:22
-
-
Save yusukebe/a56aa39162c9856fec73f112038c926d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { serve } from '@hono/node-server' | |
import { Hono } from 'hono' | |
const app = new Hono() | |
app.get('/reproduce-flush', async (c) => { | |
let transformController | |
let bufferedChunks = ['chunk1', 'chunk2'] | |
const stream = new ReadableStream({ | |
start(controller) { | |
controller.enqueue('initial data') | |
controller.close() | |
} | |
}) | |
const transformStream = new TransformStream({ | |
start(controller) { | |
transformController = controller | |
// flushBufferedChunksをシミュレート | |
const flushBufferedChunks = () => { | |
bufferedChunks.forEach((chunk) => { | |
try { | |
controller.enqueue(chunk) // ここでERR_INVALID_STATEが発生 | |
} catch (error) { | |
console.error('flushBufferedChunks error:', error.message) | |
} | |
}) | |
} | |
// 遅延実行(Wakuの実装をシミュレート) | |
setTimeout(flushBufferedChunks, 100) | |
}, | |
transform(chunk, controller) { | |
// 即座にストリームをクローズしてしまう | |
controller.enqueue(chunk) | |
controller.terminate() // または何らかの理由でクローズ | |
} | |
}) | |
const transformedStream = stream.pipeThrough(transformStream) | |
return c.body(transformedStream, 200) | |
}) | |
serve( | |
{ | |
fetch: app.fetch, | |
port: 3000 | |
}, | |
(info) => { | |
console.log(`Server is running on http://localhost:${info.port}`) | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment