-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
63 lines (49 loc) · 1.37 KB
/
main.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
package main
import (
"bytes"
"io"
"github.com/andybalholm/brotli"
"github.com/realPy/hogosuru"
"github.com/realPy/hogosuru/base/stream"
"github.com/realPy/hogosuru/base/typedarray"
)
type BrotliEncoder struct {
controller stream.TransformStreamDefaultController
b *bytes.Buffer
reader *brotli.Reader
}
func main() {
var optimalGoBuffer [8 * 1024 * 1024]byte
var outputdec [8 * 1024 * 1024]byte
hogosuru.Init()
var enc *BrotliEncoder
ts, _ := stream.NewTransformStream(func(controller stream.TransformStreamDefaultController) {
buffer := bytes.NewBuffer([]byte{})
enc = &BrotliEncoder{controller: controller, b: buffer, reader: brotli.NewReader(buffer)}
},
func(chunk interface{}, controller stream.TransformStreamDefaultController) {
if buffer, ok := chunk.(typedarray.Uint8Array); ok {
size, _ := buffer.Length()
buffer.CopyBytes(optimalGoBuffer[:size])
enc.b.Write(optimalGoBuffer[:size])
for {
if n, err := enc.reader.Read(outputdec[:]); err == nil {
decarray, _ := typedarray.NewUint8Array(n)
decarray.CopyFromBytes(outputdec[:n])
controller.Enqueue(decarray.BaseObject)
} else {
if err == io.EOF {
break
}
break
}
}
}
},
func(controller stream.TransformStreamDefaultController) {
controller.Terminate()
},
)
ts.Export("hogosuru_brotli_decoder")
select {}
}