-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathpackager.go
76 lines (69 loc) · 2.55 KB
/
packager.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
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package webpackager
import (
"net/http"
"net/url"
"time"
"github.com/google/webpackager/resource"
"golang.org/x/xerrors"
)
// Packager implements the control flow of Web Packager.
type Packager struct {
Config
}
// NewPackager creates and initializes a new Packager with the provided
// Config. It panics when config.ExchangeFactory is nil.
func NewPackager(config Config) *Packager {
config.populateDefaults()
return &Packager{config}
}
// Run runs the process to obtain the signed exchange for url: fetches the
// content from the server, processes it, and produces the signed exchange
// from it. Run also takes care of subresources (external resources
// referenced from the fetched content), such as stylesheets, provided they
// are good for preloading.
//
// The process stops when it encounters any error with processing the main
// resource (specified by url), but keeps running and produces the signed
// exchange for the main resource if it just fails with the subresources.
// In either case, Run returns a multierror.Error (hashicorp/go-multierror),
// which consists of webpackager.Errors.
//
// Run does not run the process when ResourceCache already has an entry for
// url.
func (pkg *Packager) Run(url *url.URL, sxgDate time.Time) (*resource.Resource, error) {
req, err := newGetRequest(url)
if err != nil {
return nil, err
}
return pkg.RunForRequest(req, sxgDate)
}
// RunForRequest is like Run, but takes an http.Request instead of a URL
// thus provides more flexibility to the caller.
//
// RunForRequest uses req directly: RequestTweaker mutates req; FetchClient
// sends req to retrieve the HTTP response.
func (pkg *Packager) RunForRequest(req *http.Request, sxgDate time.Time) (*resource.Resource, error) {
runner, err := newTaskRunner(pkg, sxgDate)
if err != nil {
return nil, xerrors.Errorf("packaging: %w", err)
}
r := resource.NewResource(req.URL)
runner.run(nil, req, r)
if err != nil {
return nil, xerrors.Errorf("processing: %w", err)
}
return r, runner.err()
}