Repository navigation
Expand file tree
/
Copy pathstreaming_example_test.go
More file actions
169 lines (160 loc) · 4.41 KB
/
Copy pathstreaming_example_test.go
File metadata and controls
169 lines (160 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package edge_test
import (
"bytes"
"compress/gzip"
"context"
"encoding/json/jsontext"
"encoding/json/v2"
"fmt"
"io"
"net/http"
"github.com/aws/aws-lambda-go/events"
"github.com/asteroid-computing/go-lambda-edge"
)
func ExampleStreamingAdapter_Handle() {
adapter, err := edge.NewStreaming(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Protected applications authenticate and authorize before this point.
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
controller := http.NewResponseController(w)
for _, message := range []string{"ready", "done"} {
if r.Context().Err() != nil {
return
}
if _, err := fmt.Fprintf(w, "data: %s\n\n", message); err != nil {
return
}
if err := controller.Flush(); err != nil {
return
}
}
}))
if err != nil {
panic(err)
}
// A Lambda main registers lambda.Start(adapter.Handle).
// This direct call demonstrates reader ownership locally;
// it makes no AWS calls.
stream, err := adapter.Handle(context.Background(), jsontext.Value(`{"httpMethod":"GET","path":"/","requestContext":{"apiId":"example"}}`))
if err != nil {
panic(err)
}
fmt.Print(string(readExampleStream(stream)))
// Output:
// data: ready
//
// data: done
}
func ExampleStreamingAdapter_HandleV1() {
adapter, err := edge.NewStreaming(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/x-ndjson")
for _, value := range []string{"ready", "done"} {
if r.Context().Err() != nil {
return
}
if err := json.MarshalWrite(w, struct {
State string `json:"state"`
}{value}); err != nil {
return
}
if _, err := io.WriteString(w, "\n"); err != nil {
return
}
if err := http.NewResponseController(w).Flush(); err != nil {
return
}
}
}))
if err != nil {
panic(err)
}
// A Lambda main may register lambda.Start(adapter.HandleV1).
// Input envelope decoding then belongs to the SDK;
// output metadata still uses JSON v2 in edge.
stream, err := adapter.HandleV1(context.Background(), exampleRESTEvent())
if err != nil {
panic(err)
}
fmt.Print(string(readExampleStream(stream)))
// Output:
// {"state":"ready"}
// {"state":"done"}
}
func ExampleNewStreaming_binary() {
adapter, err := edge.NewStreaming(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/octet-stream")
_, _ = w.Write([]byte{0, 255, 1})
}))
if err != nil {
panic(err)
}
stream, err := adapter.HandleV1(context.Background(), exampleRESTEvent())
if err != nil {
panic(err)
}
fmt.Printf("%v\n", readExampleStream(stream))
// Output: [0 255 1]
}
func ExampleNewStreaming_gzip() {
adapter, err := edge.NewStreaming(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Content-Encoding", "gzip")
// The example always emits gzip;
// a real route negotiates Accept-Encoding.
compressed := gzip.NewWriter(w)
defer compressed.Close()
for _, chunk := range []string{"first\n", "last\n"} {
if r.Context().Err() != nil {
return
}
if _, err := io.WriteString(compressed, chunk); err != nil {
return
}
// Flush the encoder first, then the underlying HTTP response.
if err := compressed.Flush(); err != nil {
return
}
if err := http.NewResponseController(w).Flush(); err != nil {
return
}
}
}))
if err != nil {
panic(err)
}
stream, err := adapter.HandleV1(context.Background(), exampleRESTEvent())
if err != nil {
panic(err)
}
reader, err := gzip.NewReader(bytes.NewReader(readExampleStream(stream)))
if err != nil {
panic(err)
}
defer reader.Close()
data, err := io.ReadAll(reader)
if err != nil {
panic(err)
}
fmt.Print(string(data))
// Output:
// first
// last
}
func exampleRESTEvent() events.APIGatewayProxyRequest {
return events.APIGatewayProxyRequest{HTTPMethod: "GET", Path: "/", RequestContext: events.APIGatewayProxyRequestContext{APIID: "example"}}
}
// This collector is only for the small local examples.
// Lambda's runtime consumes the reader incrementally;
// do not collect the stream in a production wrapper.
func readExampleStream(stream io.ReadCloser) []byte {
defer stream.Close()
wire, err := io.ReadAll(stream)
if err != nil {
panic(err)
}
_, body, ok := bytes.Cut(wire, make([]byte, 8))
if !ok {
panic("missing stream delimiter")
}
return body
}