Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support stream in fetch pkg #93

Merged
merged 2 commits into from
Feb 1, 2024
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
3 changes: 3 additions & 0 deletions _examples/stream-large-file/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build
node_modules
.wrangler
12 changes: 12 additions & 0 deletions _examples/stream-large-file/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.PHONY: dev
dev:
wrangler dev

.PHONY: build
build:
go run ../../cmd/workers-assets-gen
tinygo build -o ./build/app.wasm -target wasm -no-debug ./...

.PHONY: deploy
deploy:
wrangler deploy
18 changes: 18 additions & 0 deletions _examples/stream-large-file/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# stream-large-file

## Development

### Requirements

This project requires these tools to be installed globally.

* wrangler
* tinygo

### Commands

```
make dev # run dev server
make build # build Go Wasm binary
make deploy # deploy worker
```
7 changes: 7 additions & 0 deletions _examples/stream-large-file/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/syumai/workers/_examples/stream

go 1.21.3

require github.com/syumai/workers v0.0.0

replace github.com/syumai/workers => ../../
2 changes: 2 additions & 0 deletions _examples/stream-large-file/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/syumai/workers v0.1.0 h1:z5QfQR2X+PCKzom7RodpI5J4D5YF7NT7Qwzb9AM9dgY=
github.com/syumai/workers v0.1.0/go.mod h1:alXIDhTyeTwSzh0ZgQ3cb9HQPyyYfIejupE4Z3efr14=
26 changes: 26 additions & 0 deletions _examples/stream-large-file/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"fmt"
"io"
"net/http"

"github.com/syumai/workers"
"github.com/syumai/workers/cloudflare/fetch"
)

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
cli := fetch.NewClient().HTTPClient(fetch.RedirectModeFollow)
resp, err := cli.Get("http://tyo.download.datapacket.com/1000mb.bin")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "err: %v", err)
return
}
defer resp.Body.Close()

io.Copy(w, resp.Body)
})
workers.Serve(nil)
}
6 changes: 6 additions & 0 deletions _examples/stream-large-file/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name = "stream-large-file"
main = "./build/worker.mjs"
compatibility_date = "2023-12-01"

[build]
command = "make build"
20 changes: 17 additions & 3 deletions cloudflare/fetch/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fetch
import (
"errors"
"net/http"
"strconv"
"syscall/js"

"github.com/syumai/workers/internal/jshttp"
Expand All @@ -15,8 +16,8 @@ func fetch(namespace js.Value, req *http.Request, init *RequestInit) (*http.Resp
if namespace.IsUndefined() {
return nil, errors.New("fetch function not found")
}
fetchObj := namespace.Get("fetch")
promise := fetchObj.Invoke(
fetchFunc := namespace.Get("fetch")
promise := fetchFunc.Invoke(
// The Request object to fetch.
// Docs: https://developers.cloudflare.com/workers/runtime-apis/request
jshttp.ToJSRequest(req),
Expand All @@ -30,5 +31,18 @@ func fetch(namespace js.Value, req *http.Request, init *RequestInit) (*http.Resp
return nil, err
}

return jshttp.ToResponse(jsRes)
// Create TransformStream
ts := js.Global().Get("IdentityTransformStream").New()
readable := ts.Get("readable")
writable := ts.Get("writable")
jsRes.Get("body").Call("pipeTo", writable)

// Create response
res := new(http.Response)
res.StatusCode = jsRes.Get("status").Int()
res.Status = strconv.Itoa(res.StatusCode) + " " + jsRes.Get("statusText").String()
res.Header = jshttp.ToHeader(jsRes.Get("headers"))
res.Body = jsutil.ConvertReadableStreamToReadCloser(readable)

return res, nil
}
Loading