-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstream.go
More file actions
33 lines (26 loc) · 762 Bytes
/
Copy pathstream.go
File metadata and controls
33 lines (26 loc) · 762 Bytes
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
package fasthttp
import (
"errors"
"os"
"github.com/grafana/sobek"
"go.k6.io/k6/js/common"
)
type FileStream struct {
*os.File
}
func (s *FileStream) Close() error {
// prevent file from being closed so it can stream for multiple requests as fasthttp calls Close()
// after sending body
return nil
}
func (mi *ModuleInstance) FileStream(call sobek.ConstructorCall, rt *sobek.Runtime) *sobek.Object {
if len(call.Arguments) != 1 {
common.Throw(rt, errors.New("one arg required of file path for stream"))
}
f, err := os.Open(call.Argument(0).String())
if err != nil {
mi.vu.State().Logger.WithError(err).Errorf("Failed to open file %s", call.Argument(0).String())
common.Throw(rt, err)
}
return rt.ToValue(&FileStream{f}).ToObject(rt)
}