Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(worker): allow usage of js module worker #2830

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion access_audit_log_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"log"

cloudflare "github.com/cloudflare/cloudflare-go"
cloudflare "github.com/js0cha/cloudflare-go-bm"
)

func ExampleAPI_AccessAuditLogs() {
Expand Down
2 changes: 1 addition & 1 deletion argo_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"log"

cloudflare "github.com/cloudflare/cloudflare-go"
cloudflare "github.com/js0cha/cloudflare-go-bm"
)

func ExampleAPI_ArgoSmartRouting() {
Expand Down
3 changes: 0 additions & 3 deletions cmd/flarectl/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (
"os"
"strconv"
"strings"

"github.com/cloudflare/cloudflare-go"
"github.com/urfave/cli/v2"
)

func formatDNSRecord(record cloudflare.DNSRecord) []string {
Expand Down
3 changes: 0 additions & 3 deletions cmd/flarectl/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import (
"strconv"

"errors"

"github.com/cloudflare/cloudflare-go"
"github.com/urfave/cli/v2"
)

func formatAccessRule(rule cloudflare.AccessRule) []string {
Expand Down
3 changes: 1 addition & 2 deletions cmd/flarectl/flarectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package main
import (
"os"

cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/urfave/cli/v2"
cloudflare "github.com/js0cha/cloudflare-go-bm"
)

var (
Expand Down
3 changes: 1 addition & 2 deletions cmd/flarectl/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (

"errors"

cloudflare "github.com/cloudflare/cloudflare-go"
cloudflare "github.com/js0cha/cloudflare-go-bm"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli/v2"
)

func initializeAPI(c *cli.Context) error {
Expand Down
3 changes: 0 additions & 3 deletions cmd/flarectl/user_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import (
"fmt"
"os"
"strconv"

"github.com/cloudflare/cloudflare-go"
"github.com/urfave/cli/v2"
)

func formatUserAgentRule(rule cloudflare.UserAgentRule) []string {
Expand Down
3 changes: 1 addition & 2 deletions cmd/flarectl/zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
"strconv"
"strings"

cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/urfave/cli/v2"
cloudflare "github.com/js0cha/cloudflare-go-bm"
)

func zoneCerts(*cli.Context) error {
Expand Down
2 changes: 1 addition & 1 deletion dns_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"log"

cloudflare "github.com/cloudflare/cloudflare-go"
cloudflare "github.com/js0cha/cloudflare-go-bm"
)

func ExampleAPI_DNSRecords_all() {
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

cloudflare "github.com/cloudflare/cloudflare-go"
cloudflare "github.com/js0cha/cloudflare-go-bm"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion firewall_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"log"

cloudflare "github.com/cloudflare/cloudflare-go"
cloudflare "github.com/js0cha/cloudflare-go-bm"
)

func ExampleAPI_ListZoneAccessRules_all() {
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/cloudflare/cloudflare-go
module github.com/js0cha/cloudflare-go-bm

go 1.17
go 1.18

require (
github.com/bflad/tfproviderlint v0.28.1
Expand All @@ -17,7 +17,6 @@ require (
github.com/pkg/errors v0.9.1
github.com/ramya-rao-a/go-outline v0.0.0-20210608161538-9736a4bde949
github.com/stretchr/testify v1.8.0
github.com/urfave/cli/v2 v2.11.1
github.com/uudashr/gopkgs/v2 v2.1.2
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e
Expand Down Expand Up @@ -214,7 +213,6 @@ require (
github.com/ultraware/funlen v0.0.3 // indirect
github.com/ultraware/whitespace v0.0.5 // indirect
github.com/uudashr/gocognit v1.0.6 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/yagipy/maintidx v1.0.0 // indirect
github.com/yeya24/promlinter v0.2.0 // indirect
gitlab.com/bosi/decorder v0.2.2 // indirect
Expand Down
346 changes: 0 additions & 346 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion load_balancing_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"log"

cloudflare "github.com/cloudflare/cloudflare-go"
cloudflare "github.com/js0cha/cloudflare-go-bm"
)

func ExampleAPI_ListLoadBalancers() {
Expand Down
2 changes: 1 addition & 1 deletion lockdown_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"log"
"strings"

cloudflare "github.com/cloudflare/cloudflare-go"
cloudflare "github.com/js0cha/cloudflare-go-bm"
)

func ExampleAPI_ListZoneLockdowns_all() {
Expand Down
2 changes: 1 addition & 1 deletion logpush_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"log"

cloudflare "github.com/cloudflare/cloudflare-go"
cloudflare "github.com/js0cha/cloudflare-go-bm"
)

var exampleNewLogpushJob = cloudflare.LogpushJob{
Expand Down
2 changes: 1 addition & 1 deletion page_rules_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"log"

cloudflare "github.com/cloudflare/cloudflare-go"
cloudflare "github.com/js0cha/cloudflare-go-bm"
)

var exampleNewPageRule = cloudflare.PageRule{
Expand Down
2 changes: 1 addition & 1 deletion rate_limiting_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"log"

cloudflare "github.com/cloudflare/cloudflare-go"
cloudflare "github.com/js0cha/cloudflare-go-bm"
)

var exampleNewRateLimit = cloudflare.RateLimit{
Expand Down
2 changes: 1 addition & 1 deletion registrar_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"log"

cloudflare "github.com/cloudflare/cloudflare-go"
cloudflare "github.com/js0cha/cloudflare-go-bm"
)

func ExampleAPI_RegistrarDomain() {
Expand Down
1 change: 0 additions & 1 deletion tools/cmd/changelog-check/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strconv"
"strings"

"github.com/cloudflare/cloudflare-go"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
Expand Down
2 changes: 1 addition & 1 deletion user_agent_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"log"

cloudflare "github.com/cloudflare/cloudflare-go"
cloudflare "github.com/js0cha/cloudflare-go-bm"
)

func ExampleAPI_ListUserAgentRules_all() {
Expand Down
30 changes: 25 additions & 5 deletions workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type WorkerScriptParams struct {
// Bindings should be a map where the keys are the binding name, and the
// values are the binding content
Bindings map[string]WorkerBinding
// Module changes the Content-Type header to specify the script is an
// ES Module syntax script.
Module bool
}

// WorkerRoute is used to map traffic matching a URL pattern to a workers
Expand Down Expand Up @@ -655,15 +658,24 @@ func formatMultipartBody(params *WorkerScriptParams) (string, []byte, error) {
defer mpw.Close()

// Write metadata part
scriptPartName := "script"
var scriptPartName string
meta := struct {
BodyPart string `json:"body_part"`
Bindings []workerBindingMeta `json:"bindings"`
BodyPart string `json:"body_part,omitempty"`
MainModule string `json:"main_module,omitempty"`
Bindings []workerBindingMeta `json:"bindings"`
}{
BodyPart: scriptPartName,
Bindings: make([]workerBindingMeta, 0, len(params.Bindings)),
}

if params.Module {
scriptPartName = "worker.mjs"
meta.MainModule = scriptPartName
} else {
scriptPartName = "script"
meta.BodyPart = scriptPartName
}

bodyWriters := make([]workerBindingBodyWriter, 0, len(params.Bindings))
for name, b := range params.Bindings {
bindingMeta, bodyWriter, err := b.serialize(name)
Expand Down Expand Up @@ -693,8 +705,16 @@ func formatMultipartBody(params *WorkerScriptParams) (string, []byte, error) {

// Write script part
hdr = textproto.MIMEHeader{}
hdr.Set("content-disposition", fmt.Sprintf(`form-data; name="%s"`, scriptPartName))
hdr.Set("content-type", "application/javascript")

contentType := "application/javascript"
if params.Module {
contentType = "application/javascript+module"
hdr.Set("content-disposition", fmt.Sprintf(`form-data; name="%s"; filename="%[1]s"`, scriptPartName))
} else {
hdr.Set("content-disposition", fmt.Sprintf(`form-data; name="%s"`, scriptPartName))
}
hdr.Set("content-type", contentType)

pw, err = mpw.CreatePart(hdr)
if err != nil {
return "", nil, err
Expand Down
2 changes: 1 addition & 1 deletion workers_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"log"

cloudflare "github.com/cloudflare/cloudflare-go"
cloudflare "github.com/js0cha/cloudflare-go-bm"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion workers_kv_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"log"

cloudflare "github.com/cloudflare/cloudflare-go"
cloudflare "github.com/js0cha/cloudflare-go-bm"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion zone_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"log"

cloudflare "github.com/cloudflare/cloudflare-go"
cloudflare "github.com/js0cha/cloudflare-go-bm"
)

func ExampleAPI_ListZones_all() {
Expand Down
Loading