66 "compress/gzip"
77 "crypto/sha256"
88 "encoding/gob"
9+ "encoding/json"
910 "fmt"
1011 "go/build"
1112 "io"
@@ -14,6 +15,7 @@ import (
1415 "path/filepath"
1516 "time"
1617
18+ "github.com/gopherjs/gopherjs/build/cache/pack"
1719 log "github.com/sirupsen/logrus"
1820)
1921
@@ -26,6 +28,8 @@ import (
2628type Cacheable interface {
2729 Write (encode func (any ) error ) error
2830 Read (decode func (any ) error ) error
31+ PackEncode (e * pack.Encoder ) error
32+ PackDecode (d * pack.Decoder ) error
2933}
3034
3135// Cache defines methods to store and load cacheable objects.
@@ -206,23 +210,54 @@ func (bc *BuildCache) Load(c Cacheable, importPath string, srcModTime time.Time)
206210 return true
207211}
208212
209- func (bc * BuildCache ) serialize (c Cacheable , buildTime time.Time , w io.Writer ) (err error ) {
213+ var encoding = `json`
214+
215+ func (bc * BuildCache ) serialize (c Cacheable , buildTime time.Time , w io.Writer ) error {
216+ switch encoding {
217+ case `json` :
218+ return bc .jsonSerialize (c , buildTime , w )
219+ case `gob` :
220+ return bc .gobSerialize (c , buildTime , w )
221+ case `zipGob` :
222+ return bc .zipGobSerialize (c , buildTime , w )
223+ default :
224+ panic (fmt .Errorf (`unknown encoding for serialization: %q` , encoding ))
225+ }
226+ }
227+
228+ func (bc * BuildCache ) deserialize (c Cacheable , srcModTime time.Time , r io.Reader ) (buildTime time.Time , old bool , err error ) {
229+ switch encoding {
230+ case `json` :
231+ return bc .jsonDeserialize (c , srcModTime , r )
232+ case `gob` :
233+ return bc .gobDeserialize (c , srcModTime , r )
234+ case `zipGob` :
235+ return bc .zipGobDeserialize (c , srcModTime , r )
236+ default :
237+ panic (fmt .Errorf (`unknown encoding for serialization: %q` , encoding ))
238+ }
239+ }
240+
241+ func (bc * BuildCache ) zipGobSerialize (c Cacheable , buildTime time.Time , w io.Writer ) (err error ) {
210242 zw := gzip .NewWriter (w )
211243 defer func () {
212244 // This close flushes the gzip but does not close the given writer.
213245 if closeErr := zw .Close (); err == nil {
214246 err = closeErr
215247 }
216248 }()
249+ return bc .gobSerialize (c , buildTime , zw )
250+ }
217251
218- ge := gob .NewEncoder (zw )
252+ func (bc * BuildCache ) gobSerialize (c Cacheable , buildTime time.Time , w io.Writer ) error {
253+ ge := gob .NewEncoder (w )
219254 if err := ge .Encode (buildTime ); err != nil {
220255 return err
221256 }
222257 return c .Write (ge .Encode )
223258}
224259
225- func (bc * BuildCache ) deserialize (c Cacheable , srcModTime time.Time , r io.Reader ) (buildTime time.Time , old bool , err error ) {
260+ func (bc * BuildCache ) zipGobDeserialize (c Cacheable , srcModTime time.Time , r io.Reader ) (buildTime time.Time , old bool , err error ) {
226261 zr , err := gzip .NewReader (r )
227262 if err != nil {
228263 return buildTime , false , err
@@ -233,8 +268,11 @@ func (bc *BuildCache) deserialize(c Cacheable, srcModTime time.Time, r io.Reader
233268 err = closeErr
234269 }
235270 }()
271+ return bc .gobDeserialize (c , srcModTime , zr )
272+ }
236273
237- gd := gob .NewDecoder (zr )
274+ func (bc * BuildCache ) gobDeserialize (c Cacheable , srcModTime time.Time , r io.Reader ) (buildTime time.Time , old bool , err error ) {
275+ gd := gob .NewDecoder (r )
238276 if err := gd .Decode (& buildTime ); err != nil {
239277 return buildTime , false , err
240278 }
@@ -244,6 +282,25 @@ func (bc *BuildCache) deserialize(c Cacheable, srcModTime time.Time, r io.Reader
244282 return buildTime , false , c .Read (gd .Decode )
245283}
246284
285+ func (bc * BuildCache ) jsonSerialize (c Cacheable , buildTime time.Time , w io.Writer ) (err error ) {
286+ encode := json .NewEncoder (w ).Encode
287+ if err := encode (& buildTime ); err != nil {
288+ return err
289+ }
290+ return c .Write (encode )
291+ }
292+
293+ func (bc * BuildCache ) jsonDeserialize (c Cacheable , srcModTime time.Time , r io.Reader ) (buildTime time.Time , old bool , err error ) {
294+ decode := json .NewDecoder (r ).Decode
295+ if err := decode (& buildTime ); err != nil {
296+ return buildTime , false , err
297+ }
298+ if srcModTime .After (buildTime ) {
299+ return buildTime , true , nil // Package is out-of-date, cache miss.
300+ }
301+ return buildTime , false , c .Read (decode )
302+ }
303+
247304// commonKey returns a part of the cache key common for all artifacts generated
248305// under a given BuildCache configuration.
249306func (bc * BuildCache ) commonKey () string {
0 commit comments