-
Notifications
You must be signed in to change notification settings - Fork 1
/
transport_module_config.go
120 lines (100 loc) · 3.39 KB
/
transport_module_config.go
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
package water
import (
"errors"
"fmt"
"os"
"runtime"
)
// TransportModuleConfig defines the configuration file used by
// the WebAssembly Transport Module.
//
// It is optional to WATER, but may be mandatory according to the
// WebAssembly Transport Module implementation.
type TransportModuleConfig interface {
// AsFile returns the TransportModuleConfig as a file, which
// then can be loaded into the WebAssembly Transport Module.
//
// If the returned error is nil, the *os.File MUST be valid
// and in a readable state.
//
// It is recommended to use AsBytes instead, which does not
// leave any temporary files on the local file system.
AsFile() (*os.File, error)
// AsBytes returns the TransportModuleConfig as a byte slice.
AsBytes() []byte
}
// transportModuleConfigFile could be used to provide a config file
// on the local file system to the WebAssembly Transport Module by
// specifying the path to the config file.
//
// Implements TransportModuleConfig.
type transportModuleConfigFile string
// TransportModuleConfigFromFile creates a TransportModuleConfig from
// the given file path.
func TransportModuleConfigFromFile(filePath string) (TransportModuleConfig, error) {
// Try opening the file to ensure it exists and is readable.
_, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("water: failed to open WATM config file: %w", err)
}
return transportModuleConfigFile(filePath), nil
}
// AsFile implements TransportModuleConfig.
func (c transportModuleConfigFile) AsFile() (*os.File, error) {
if string(c) == "" {
return nil, errors.New("transport module config file path is empty")
}
f, err := os.Open(string(c))
if err != nil {
return nil, fmt.Errorf("failed to open transport module config file: %w", err)
}
return f, nil
}
// AsBytes implements TransportModuleConfig.
func (c transportModuleConfigFile) AsBytes() []byte {
if string(c) == "" {
return nil
}
buf, err := os.ReadFile(string(c))
if err != nil {
return nil
}
return buf
}
type transportModuleConfigBytes []byte
// TransportModuleConfigFromBytes creates a TransportModuleConfig from
// the given byte slice.
func TransportModuleConfigFromBytes(configBytes []byte) TransportModuleConfig {
return transportModuleConfigBytes(configBytes)
}
// AsFile implements TransportModuleConfig.
func (c transportModuleConfigBytes) AsFile() (*os.File, error) {
if len(c) == 0 {
return nil, errors.New("transport module config bytes is empty")
}
f, err := os.CreateTemp("", "water-wasm-config-*.json")
if err != nil {
return nil, fmt.Errorf("failed to create temp file for transport module config: %w", err)
}
if _, err := f.Write(c); err != nil {
return nil, fmt.Errorf("failed to write transport module config to temp file: %w", err)
}
// reset the file pointer to the beginning of the file
if _, err := f.Seek(0, 0); err != nil {
return nil, fmt.Errorf("failed to seek to the beginning of the temp file: %w", err)
}
runtime.SetFinalizer(f, func(tmpFile *os.File) {
tmpFile.Close()
// Remove the temp file from local file system when collected.
//
// This does NOT guarantee the temp file will always be removed
// from the local file system before the program exits. However,
// it is still better than nothing when concurrency is high.
os.Remove(tmpFile.Name())
})
return f, nil
}
// AsBytes implements TransportModuleConfig.
func (c transportModuleConfigBytes) AsBytes() []byte {
return c
}