-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
310 lines (271 loc) · 6.94 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
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
package main
import (
"archive/tar"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"sync"
"github.com/cdnjs/tools/compress"
"github.com/cdnjs/tools/packages"
"github.com/cdnjs/tools/sri"
"github.com/pkg/errors"
)
var (
// these file extensions will be uploaded to KV
// but not compressed
doNotCompress = map[string]bool{
".woff2": true,
}
// we calculate SRIs for these file extensions
calculateSRI = map[string]bool{
".js": true,
".css": true,
}
)
const (
INPUT = "/input"
OUTPUT = "/output"
WORKSPACE = "/tmp/work"
)
func main() {
ctx := context.Background()
config, err := readConfig()
if err != nil {
log.Fatalf("could not read config: %s", err)
}
if err := os.MkdirAll(WORKSPACE, 0700); err != nil {
log.Fatalf("could not create workspace: %s", err)
}
if err := extractInput(*config.Autoupdate.Source); err != nil {
log.Fatalf("failed to extract input: %s", err)
}
if err := optimizePackage(ctx, config); err != nil {
log.Fatalf("failed to optimize files: %s", err)
}
log.Printf("processed %s\n", *config.Name)
}
type optimizeJob struct {
Ctx context.Context
Optimization *packages.Optimization
File string
Dest string
}
func (j optimizeJob) clone() optimizeJob {
return optimizeJob{
Ctx: j.Ctx,
Optimization: j.Optimization,
File: j.File,
Dest: j.Dest,
}
}
func (j optimizeJob) emitFromWorkspace(src string) {
dest := path.Join(OUTPUT, j.Dest)
if err := os.MkdirAll(path.Dir(dest), 0755); err != nil {
log.Fatalf("could not create dest dir: %s", err)
}
ext := path.Ext(src)
if _, ok := calculateSRI[ext]; ok {
outSRI := fmt.Sprintf("%s.sri", dest)
if _, err := os.Stat(outSRI); err == nil {
log.Printf("file %s already exists at the output\n", outSRI)
} else {
sri.CalculateFileSRI(src, outSRI)
log.Printf("sri %s -> %s\n", src, outSRI)
}
}
if _, ok := doNotCompress[ext]; !ok {
outBr := fmt.Sprintf("%s.br", dest)
if _, err := os.Stat(outBr); err == nil {
log.Printf("file %s already exists at the output\n", outBr)
} else {
compress.Brotli11CLI(j.Ctx, src, outBr)
log.Printf("br %s -> %s\n", src, outBr)
}
outGz := fmt.Sprintf("%s.gz", dest)
if _, err := os.Stat(outGz); err == nil {
log.Printf("file %s already exists at the output\n", outGz)
} else {
compress.Gzip9Native(j.Ctx, src, outGz)
log.Printf("gz %s -> %s\n", src, outGz)
}
} else {
if err := copyFile(src, dest); err != nil {
log.Fatalf("failed to copy file: %s", err)
}
log.Printf("copy %s -> %s\n", src, dest)
}
}
func (j optimizeJob) emit(name string) {
src := path.Join(WORKSPACE, name)
j.emitFromWorkspace(src)
}
func removePackageDir(path string) string {
if len(path) < 8 {
return path
}
if path[0:8] == "package/" {
return path[8:]
}
return path
}
func removeFirstDir(path string) string {
parts := strings.Split(path, "/")
return strings.Replace(path, parts[0]+"/", "", 1)
}
func readConfig() (*packages.Package, error) {
file := path.Join(INPUT, "config.json")
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, errors.Wrap(err, "could not read file")
}
config := new(packages.Package)
if err := json.Unmarshal(data, config); err != nil {
return nil, errors.Wrap(err, "could not parse config")
}
return config, nil
}
func extractInput(source string) error {
gzipStream, err := os.Open(path.Join(INPUT, "new-version.tgz"))
if err != nil {
return errors.Wrap(err, "could not open input")
}
uncompressedStream, err := gzip.NewReader(gzipStream)
if err != nil {
return errors.Wrap(err, "could not create reader")
}
tarReader := tar.NewReader(uncompressedStream)
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
log.Fatalf("ExtractTarGz: Next() failed: %s", err.Error())
}
target := header.Name
if source == "npm" {
// remove package folder
target = removePackageDir(header.Name)
}
if source == "git" {
// remove package folder
target = removeFirstDir(header.Name)
}
switch header.Typeflag {
case tar.TypeDir:
// ignore dirs
case tar.TypeReg:
if err := os.MkdirAll(path.Join(WORKSPACE, filepath.Dir(target)), 0755); err != nil {
return errors.Wrap(err, "ExtractTarGz: Mkdir() failed")
}
outFile, err := os.Create(path.Join(WORKSPACE, target))
if err != nil {
return errors.Wrap(err, "ExtractTarGz: Create() failed")
}
defer outFile.Close()
if _, err := io.Copy(outFile, tarReader); err != nil {
return errors.Wrap(err, "ExtractTarGz: Copy() failed")
}
default:
log.Printf(
"ExtractTarGz: uknown type: %x in %s\n",
header.Typeflag,
header.Name)
}
}
return nil
}
func optimizeWorker(wg *sync.WaitGroup, jobs <-chan optimizeJob) {
for j := range jobs {
intputFile := path.Join(WORKSPACE, j.File)
ext := path.Ext(j.File)
switch ext {
case ".jpg", ".jpeg":
if j.Optimization.Jpg() {
compress.Jpeg(j.Ctx, intputFile)
}
case ".png":
if j.Optimization.Png() {
compress.Png(j.Ctx, intputFile)
}
case ".js":
if j.Optimization.Js() {
if out := compress.Js(j.Ctx, intputFile); out != nil {
j := j.clone()
j.Dest = strings.Replace(j.Dest, ".js", ".min.js", 1)
j.emitFromWorkspace(*out)
}
}
case ".css":
if j.Optimization.Css() {
if out := compress.CSS(j.Ctx, intputFile); out != nil {
j := j.clone()
j.Dest = strings.Replace(j.Dest, ".css", ".min.css", 1)
j.emitFromWorkspace(*out)
}
}
}
j.emit(j.File)
wg.Done()
}
}
// Optimizes/minifies package's files on disk for a particular package version.
func optimizePackage(ctx context.Context, config *packages.Package) error {
log.Printf("optimizing files (Js %t, Css %t, Png %t, Jpg %t)\n",
config.Optimization.Js(),
config.Optimization.Css(),
config.Optimization.Png(),
config.Optimization.Jpg())
files := config.NpmFilesFrom(WORKSPACE)
cpuCount := runtime.NumCPU()
jobs := make(chan optimizeJob, cpuCount)
var wg sync.WaitGroup
wg.Add(len(files))
for w := 1; w <= cpuCount; w++ {
go optimizeWorker(&wg, jobs)
}
for _, file := range files {
jobs <- optimizeJob{
Ctx: ctx,
Optimization: config.Optimization,
File: file.From,
Dest: file.To,
}
}
close(jobs)
wg.Wait()
return nil
}
func copyFile(src, dest string) error {
if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil {
return errors.Wrap(err, "could not create dir")
}
srcFile, err := os.Open(src)
if err != nil {
return errors.Wrap(err, "could not open source file")
}
defer srcFile.Close()
destFile, err := os.Create(dest)
if err != nil {
return errors.Wrap(err, "could not open dest file")
}
defer destFile.Close()
_, err = io.Copy(destFile, srcFile) // check first var for number of bytes copied
if err != nil {
return errors.Wrap(err, "could not copy")
}
err = destFile.Sync()
if err != nil {
return errors.Wrap(err, "could not sync")
}
return nil
}