-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from syumai/return-fetch-body-directly
return fetch body directly
- Loading branch information
Showing
10 changed files
with
83 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +0,0 @@ | ||
github.com/syumai/tinyutil v0.3.0 h1:sgWeE8oQyequIRLNeHZgR1PddpY4mxcdkfMgx2m53IE= | ||
github.com/syumai/tinyutil v0.3.0/go.mod h1:/owCyUs1bh6tKxH7K1Ze3M/zZtZ+vGrj3h82fgNHDFI= | ||
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,16 +7,30 @@ import ( | |
"syscall/js" | ||
) | ||
|
||
// streamReaderToReader implements io.Reader sourced from ReadableStreamDefaultReader. | ||
type RawJSBodyWriter interface { | ||
WriteRawJSBody(body js.Value) | ||
} | ||
|
||
// readableStreamToReadCloser implements io.Reader sourced from ReadableStreamDefaultReader. | ||
// - ReadableStreamDefaultReader: https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader | ||
// - This implementation is based on: https://deno.land/[email protected]/streams/conversion.ts#L76 | ||
type streamReaderToReader struct { | ||
type readableStreamToReadCloser struct { | ||
buf bytes.Buffer | ||
streamReader js.Value | ||
stream js.Value | ||
streamReader *js.Value | ||
} | ||
|
||
var ( | ||
_ io.ReadCloser = (*readableStreamToReadCloser)(nil) | ||
_ io.WriterTo = (*readableStreamToReadCloser)(nil) | ||
) | ||
|
||
// Read reads bytes from ReadableStreamDefaultReader. | ||
func (sr *streamReaderToReader) Read(p []byte) (n int, err error) { | ||
func (sr *readableStreamToReadCloser) Read(p []byte) (n int, err error) { | ||
if sr.streamReader == nil { | ||
r := sr.stream.Call("getReader") | ||
sr.streamReader = &r | ||
} | ||
if sr.buf.Len() == 0 { | ||
promise := sr.streamReader.Call("read") | ||
resultCh := make(chan js.Value) | ||
|
@@ -56,10 +70,31 @@ func (sr *streamReaderToReader) Read(p []byte) (n int, err error) { | |
return sr.buf.Read(p) | ||
} | ||
|
||
// ConvertStreamReaderToReader converts ReadableStreamDefaultReader to io.Reader. | ||
func ConvertStreamReaderToReader(sr js.Value) io.Reader { | ||
return &streamReaderToReader{ | ||
streamReader: sr, | ||
func (sr *readableStreamToReadCloser) Close() error { | ||
if sr.streamReader == nil { | ||
return nil | ||
} | ||
sr.streamReader.Call("close") | ||
return nil | ||
} | ||
|
||
// readerWrapper is wrapper to disable readableStreamToReadCloser's WriteTo method. | ||
type readerWrapper struct { | ||
io.Reader | ||
} | ||
|
||
func (sr *readableStreamToReadCloser) WriteTo(w io.Writer) (n int64, err error) { | ||
if w, ok := w.(RawJSBodyWriter); ok { | ||
w.WriteRawJSBody(sr.stream) | ||
return 0, nil | ||
} | ||
return io.Copy(w, &readerWrapper{sr}) | ||
} | ||
|
||
// ConvertReadableStreamToReadCloser converts ReadableStream to io.ReadCloser. | ||
func ConvertReadableStreamToReadCloser(stream js.Value) io.ReadCloser { | ||
return &readableStreamToReadCloser{ | ||
stream: stream, | ||
} | ||
} | ||
|
||
|