Skip to content

Commit 7150c8b

Browse files
authored
fix: fixed stream copy when using express like lib (http/https) (#33)
1 parent 5b4bc6e commit 7150c8b

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

_ultimate-express/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ init();
66

77
const app = express();
88

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

1416
res.writeHead(rs.status, rs.statusText, Object.fromEntries(rs.headers));

_ultimate-express/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "module",
77
"scripts": {
88
"test": "echo \"Error: no test specified\" && exit 1",
9-
"dev": "pnpm run build:go && tsx watch src/index.ts",
9+
"dev": "pnpm run build:go && tsx watch index.ts",
1010
"build": "tsc",
1111
"build:go": "GOOS=js GOARCH=wasm go build -gcflags=all='-N -l' -gcflags='-dwarflocationlists=true' -o ./bin/app.wasm ./main.go"
1212
},

internal/stream/stream.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,17 @@ func (rs *ReadableStream) Read(p []byte) (n int, err error) {
5656

5757
select {
5858
case result := <-resultCh:
59-
chunk := make([]byte, result.Get("byteLength").Int())
60-
_ = js.CopyBytesToGo(chunk, result)
61-
// The length written is always the same as the length of chunk, so it can be discarded.
62-
// - https://pkg.go.dev/bytes#Buffer.Write
63-
_, err := rs.buf.Write(chunk)
59+
var err error
60+
61+
if result.Type().String() == "number" {
62+
_, err = rs.buf.Write([]byte{byte(result.Int())})
63+
} else {
64+
chunk := make([]byte, result.Get("byteLength").Int())
65+
_ = js.CopyBytesToGo(chunk, result)
66+
// The length written is always the same as the length of chunk, so it can be discarded.
67+
// - https://pkg.go.dev/bytes#Buffer.Write
68+
_, err = rs.buf.Write(chunk)
69+
}
6470
if err != nil {
6571
return 0, err
6672
}

0 commit comments

Comments
 (0)