Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions _ultimate-express/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ init();

const app = express();

app.use(express.raw({ type: "*/*" }))
app.all("*", async (req, res) => {
await init();
// @ts-ignore
req.body = ReadableStream.from(req.body)
const rs: Response = await cf.fetch(req);

res.writeHead(rs.status, rs.statusText, Object.fromEntries(rs.headers));
Expand Down
2 changes: 1 addition & 1 deletion _ultimate-express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "pnpm run build:go && tsx watch src/index.ts",
"dev": "pnpm run build:go && tsx watch index.ts",
"build": "tsc",
"build:go": "GOOS=js GOARCH=wasm go build -gcflags=all='-N -l' -gcflags='-dwarflocationlists=true' -o ./bin/app.wasm ./main.go"
},
Expand Down
16 changes: 11 additions & 5 deletions internal/stream/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,17 @@ func (rs *ReadableStream) Read(p []byte) (n int, err error) {

select {
case result := <-resultCh:
chunk := make([]byte, result.Get("byteLength").Int())
_ = js.CopyBytesToGo(chunk, result)
// The length written is always the same as the length of chunk, so it can be discarded.
// - https://pkg.go.dev/bytes#Buffer.Write
_, err := rs.buf.Write(chunk)
var err error

if result.Type().String() == "number" {
_, err = rs.buf.Write([]byte{byte(result.Int())})
} else {
chunk := make([]byte, result.Get("byteLength").Int())
_ = js.CopyBytesToGo(chunk, result)
// The length written is always the same as the length of chunk, so it can be discarded.
// - https://pkg.go.dev/bytes#Buffer.Write
_, err = rs.buf.Write(chunk)
}
if err != nil {
return 0, err
}
Expand Down