This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
timecraft.go
246 lines (199 loc) · 5.63 KB
/
timecraft.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package format
import (
"bytes"
"crypto/sha256"
"encoding"
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/google/uuid"
)
type UUID = uuid.UUID
type Hash struct {
Algorithm, Digest string
}
func SHA256(b []byte) Hash {
digest := sha256.Sum256(b)
return Hash{
Algorithm: "sha256",
Digest: hex.EncodeToString(digest[:]),
}
}
func ParseHash(s string) Hash {
algorithm, digest, ok := strings.Cut(s, ":")
if !ok {
algorithm, digest = "sha256", s
}
return Hash{Algorithm: algorithm, Digest: digest}
}
func (h Hash) Short() string {
s := h.Digest
if len(s) > 12 {
s = s[:12]
}
return s
}
func (h Hash) String() string {
return h.Algorithm + ":" + h.Digest
}
func (h Hash) MarshalText() ([]byte, error) {
return []byte(h.String()), nil
}
func (h *Hash) UnmarshalText(b []byte) error {
algorithm, digest, ok := strings.Cut(string(b), ":")
if !ok {
return fmt.Errorf("malformed hash: %q", b)
}
h.Algorithm, h.Digest = algorithm, digest
return nil
}
var (
_ encoding.TextMarshaler = Hash{}
_ encoding.TextUnmarshaler = (*Hash)(nil)
)
type MediaType string
const (
TypeDescriptor MediaType = "application/vnd.oci.descriptor.v1+json"
TypeTimecraftRuntime MediaType = "application/vnd.timecraft.runtime.v1+json"
TypeTimecraftConfig MediaType = "application/vnd.timecraft.config.v1+json"
TypeTimecraftProcess MediaType = "application/vnd.timecraft.process.v1+json"
TypeTimecraftProfile MediaType = "application/vnd.timecraft.profile.v1+pprof"
TypeTimecraftManifest MediaType = "application/vnd.timecraft.manifest.v1+json"
TypeTimecraftModule MediaType = "application/vnd.timecraft.module.v1+wasm"
)
func (m MediaType) String() string { return string(m) }
type Resource interface {
ContentType() MediaType
}
type ResourceMarshaler interface {
Resource
MarshalResource() ([]byte, error)
}
type ResourceUnmarshaler interface {
Resource
UnmarshalResource([]byte) error
}
type Descriptor struct {
MediaType MediaType `json:"mediaType" yaml:"mediaType"`
Digest Hash `json:"digest" yaml:"digest"`
Size int64 `json:"size" yaml:"size"`
URLs []string `json:"urls,omitempty" yaml:"urls,omitempty"`
Annotations map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty"`
}
func (d *Descriptor) ContentType() MediaType {
return TypeDescriptor
}
func (d *Descriptor) MarshalResource() ([]byte, error) {
return jsonEncode(d)
}
func (d *Descriptor) UnmarshalResource(b []byte) error {
return jsonDecode(b, d)
}
type Module struct {
Code []byte
}
func (m *Module) ContentType() MediaType {
return TypeTimecraftModule
}
func (m *Module) MarshalResource() ([]byte, error) {
return m.Code, nil
}
func (m *Module) UnmarshalResource(b []byte) error {
m.Code = b
return nil
}
type Runtime struct {
Runtime string `json:"runtime" yaml:"runtime"`
Version string `json:"version" yaml:"version"`
}
func (r *Runtime) ContentType() MediaType {
return TypeTimecraftRuntime
}
func (r *Runtime) MarshalResource() ([]byte, error) {
return jsonEncode(r)
}
func (r *Runtime) UnmarshalResource(b []byte) error {
return jsonDecode(b, r)
}
type Config struct {
Runtime *Descriptor `json:"runtime" yaml:"runtime"`
Modules []*Descriptor `json:"modules" yaml:"modules"`
Args []string `json:"args" yaml:"args"`
Env []string `json:"env,omitempty" yaml:"env,omitempty"`
}
func (c *Config) ContentType() MediaType {
return TypeTimecraftConfig
}
func (c *Config) MarshalResource() ([]byte, error) {
return jsonEncode(c)
}
func (c *Config) UnmarshalResource(b []byte) error {
return jsonDecode(b, c)
}
type Process struct {
ID UUID `json:"id" yaml:"id"`
StartTime time.Time `json:"startTime" yaml:"startTime"`
Config *Descriptor `json:"config" yaml:"config"`
}
func (p *Process) ContentType() MediaType {
return TypeTimecraftProcess
}
func (p *Process) MarshalResource() ([]byte, error) {
return jsonEncode(p)
}
func (p *Process) UnmarshalResource(b []byte) error {
return jsonDecode(b, p)
}
func jsonEncode(value any) ([]byte, error) {
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
err := enc.Encode(value)
return buf.Bytes(), err
}
func jsonDecode(b []byte, value any) error {
return json.Unmarshal(b, &value)
}
type Manifest struct {
ProcessID UUID `json:"id,omitempty" yaml:"id,omitempty"`
StartTime time.Time `json:"startTime" yaml:"startTime"`
Process *Descriptor `json:"process" yaml:"process"`
Segments []LogSegment `json:"segments,omitempty" yaml:"segments,omitempty"`
}
func (m *Manifest) ContentType() MediaType {
return TypeTimecraftManifest
}
func (m *Manifest) MarshalResource() ([]byte, error) {
return jsonEncode(m)
}
func (m *Manifest) UnmarshalResource(b []byte) error {
return jsonDecode(b, m)
}
type LogSegment struct {
Number int `json:"number" yaml:"number"`
Size int64 `json:"size" yaml:"size"`
CreatedAt time.Time `json:"createdAt" yaml:"createdAt"`
}
type Record struct {
ID string
Process *Descriptor
Offset int64
Size int64
Time time.Time
Function string
}
var (
_ ResourceMarshaler = (*Descriptor)(nil)
_ ResourceMarshaler = (*Module)(nil)
_ ResourceMarshaler = (*Runtime)(nil)
_ ResourceMarshaler = (*Config)(nil)
_ ResourceMarshaler = (*Manifest)(nil)
_ ResourceUnmarshaler = (*Descriptor)(nil)
_ ResourceUnmarshaler = (*Module)(nil)
_ ResourceUnmarshaler = (*Runtime)(nil)
_ ResourceUnmarshaler = (*Config)(nil)
_ ResourceUnmarshaler = (*Manifest)(nil)
)