-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy.go
366 lines (296 loc) · 7.21 KB
/
proxy.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package inkscape
import (
"bytes"
"context"
"errors"
"fmt"
"log"
"os"
"os/exec"
"strings"
"time"
"github.com/galihrivanto/runner"
)
const (
defaultCmdName = "inkscape"
shellModeBanner = "Inkscape interactive shell mode"
quitCommand = "quit"
)
// defines common errors in library
var (
ErrCommandNotAvailable = errors.New("inkscape not available")
ErrCommandNotReady = errors.New("inkscape not ready")
ErrCommandExecCanceled = errors.New("command execution canceled")
)
// bytes.Buffer pool
var bufferPool = NewSizedBufferPool(5, 1024*1024)
type chanWriter struct {
out chan []byte
}
func (w *chanWriter) Write(data []byte) (int, error) {
// look like the buffer being reused internally by the exec.Command
// so we can't directly read the buffer in another goroutine while still being used in exec.Command goroutine
// copy to be written buffer and pass it into channel
bufferToWrite := make([]byte, len(data))
written := copy(bufferToWrite, data)
w.out <- bufferToWrite
return written, nil
}
// Proxy runs inkscape instance in background and
// provides mechanism to interfacing with running
// instance via stdin
type Proxy struct {
options Options
logger *log.Logger
// context and cancellation
ctx context.Context
cancel context.CancelFunc
// limiter to allow one command processed at time
requestLimiter chan struct{}
// queue of request
requestQueue chan []byte
// output
stdout chan []byte
stderr chan []byte
}
func (p *Proxy) debug(args ...interface{}) {
if p.options.verbose {
p.logger.Println(args...)
}
}
// runBackground run inkscape instance in background
func (p *Proxy) runBackground(ctx context.Context, commandPath string, vars ...string) error {
args := []string{
"--shell",
}
if len(vars) > 0 {
args = append(args, vars...)
}
cmd := exec.CommandContext(ctx, commandPath, args...)
// pipe stderr
stderrC := make(chan []byte)
defer close(stderrC)
cmd.Stderr = &chanWriter{out: stderrC}
// pipe stdout
stdoutC := make(chan []byte)
defer close(stdoutC)
cmd.Stdout = &chanWriter{out: stdoutC}
// pipe stdin
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
defer stdin.Close()
// start command and wait it close
p.debug("run in background")
if err := cmd.Start(); err != nil {
return err
}
// make first command available
// after received prompt
wait:
for {
bytesOut := <-stdoutC
bytesOut = bytes.TrimSpace(bytesOut)
parts := bytes.Split(bytesOut, []byte("\n"))
for _, part := range parts {
if isPrompt(part) {
break wait
}
}
}
select {
case p.requestLimiter <- struct{}{}:
default:
// discard
}
// handle command and output
for {
select {
case <-ctx.Done():
return cmd.Wait()
case command := <-p.requestQueue:
p.debug("write command ", string(command))
if _, err := stdin.Write(command); err != nil {
p.stderr <- []byte(err.Error())
}
case bytesErr := <-stderrC:
if len(bytesErr) == 0 {
break
}
// only skip warning when option suppressWarning are true
if p.options.suppressWarning {
if bytes.Contains(bytesErr, []byte("WARNING")) {
break
}
}
p.stderr <- bytes.TrimSpace(bytesErr)
case bytesOut := <-stdoutC:
if len(bytesOut) == 0 {
break
}
p.stdout <- bytes.TrimSpace(bytesOut)
}
}
}
// Run start inkscape proxy
func (p *Proxy) Run(args ...string) error {
commandPath, err := exec.LookPath(p.options.commandName)
if err != nil {
return ErrCommandNotAvailable
}
p.debug(commandPath)
p.ctx, p.cancel = context.WithCancel(context.Background())
go func() {
runner.RunWithRetry(
p.ctx,
func(ctx context.Context) error {
return p.runBackground(ctx, commandPath, args...)
},
runner.NewExponentialBackoffRetry(p.options.maxRetry),
)
}()
// print inkscape version
res, _ := p.RawCommands(Version())
fmt.Println(string(res))
return nil
}
// Close satisfy io.Closer interface
func (p *Proxy) Close() error {
// send quit command
_, err := p.sendCommand(context.Background(), []byte(quitCommand), false)
p.cancel()
close(p.requestLimiter)
close(p.requestQueue)
close(p.stderr)
close(p.stdout)
return err
}
func (p *Proxy) sendCommand(ctx context.Context, b []byte, waitPrompt ...bool) ([]byte, error) {
wait := true
if len(waitPrompt) > 0 {
wait = waitPrompt[0]
}
// wait available
p.debug("wait prompt available")
<-p.requestLimiter
defer func() {
// make it available again
p.requestLimiter <- struct{}{}
}()
p.debug("send command to stdin ", string(b))
// drain old err and out
drain(p.stderr)
drain(p.stdout)
// append new line
if !bytes.HasSuffix(b, []byte{'\n'}) {
b = append(b, '\n')
}
p.requestQueue <- b
var (
output []byte
err error
)
// immediate return
if !wait {
<-time.After(time.Second)
return []byte{}, nil
}
waitLoop:
for {
select {
// wait till context canceled, early return
case <-ctx.Done():
return output, ErrCommandExecCanceled
// wait until received prompt
case bytesOut := <-p.stdout:
p.debug(string(bytesOut))
parts := bytes.Split(bytesOut, []byte("\n"))
for _, part := range parts {
if isPrompt(part) {
break waitLoop
}
}
output = append(output, bytesOut...)
}
}
// drain error channel
errLoop:
for {
select {
case bytesErr := <-p.stderr:
if len(bytesErr) > 0 {
p.debug(string(bytesErr))
err = fmt.Errorf("%s", string(bytesErr))
}
default:
break errLoop
}
}
return output, err
}
// RawCommands send inkscape shell commands
func (p *Proxy) RawCommands(args ...string) ([]byte, error) {
return p.RawCommandsContext(context.Background(), args...)
}
// RawCommandsContext send inkscape shell commands that are bounded into specific context
func (p *Proxy) RawCommandsContext(ctx context.Context, args ...string) ([]byte, error) {
buffer := bufferPool.Get()
defer bufferPool.Put(buffer)
// construct command buffer
buffer.WriteString(strings.Join(args, ";"))
res, err := p.sendCommand(ctx, buffer.Bytes())
return res, err
}
// Svg2Pdf convert svg input file to output pdf file
func (p *Proxy) Svg2Pdf(svgIn, pdfOut string) error {
return p.Svg2PdfContext(context.Background(), svgIn, pdfOut)
}
// Svg2PdfContext convert svg input file to output pdf file that are bounded into specific context
func (p *Proxy) Svg2PdfContext(ctx context.Context, svgIn, pdfOut string) error {
res, err := p.RawCommandsContext(
ctx,
FileOpen(svgIn),
ExportFileName(pdfOut),
ExportDo(),
FileClose(),
)
if err != nil {
return err
}
p.debug("result", string(res))
return nil
}
// NewProxy create new inkscape proxy instance
func NewProxy(opts ...Option) *Proxy {
// default value
options := Options{
commandName: defaultCmdName,
maxRetry: 5,
verbose: false,
suppressWarning: true,
}
// merge options
options = mergeOptions(options, opts...)
return &Proxy{
options: options,
stdout: make(chan []byte, 100),
stderr: make(chan []byte, 100),
logger: log.New(os.Stdout, "[debug]", log.Lshortfile),
// limit request to one request at time
requestLimiter: make(chan struct{}, 1),
requestQueue: make(chan []byte, 100),
}
}
func isPrompt(data []byte) bool {
return bytes.Equal(data, []byte(">"))
}
func drain(c chan []byte) {
for {
select {
case <-c:
default:
return
}
}
}