Skip to content

Commit d9816e0

Browse files
authored
Merge pull request #2 from Darckfast/dev
feat: more reworks, perf changes and testing
2 parents f3e501e + e3b6825 commit d9816e0

75 files changed

Lines changed: 1896 additions & 1344 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/pull_request.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ jobs:
1717

1818
- uses: pnpm/action-setup@v4
1919
name: Install pnpm
20-
with:
21-
version: 10
2220

2321
- uses: actions/setup-node@v4
2422
with:
2523
node-version: 22
24+
- run: echo "$(go env GOROOT)/lib/wasm" >> $GITHUB_PATH
25+
- run: pnpm i
2626

2727
- name: Test Go
2828
shell: bash
29-
run: PATH='$PATH:$(go env GOROOT)/lib/wasm' pnpm run test:go
29+
run: pnpm run test:go
3030

3131
- name: Test Worker
3232
shell: bash

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_modules
44
.wrangler
55
.dev.vars
66
.env
7+
.vite

cloudflare/cache/client.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import (
55
"net/http"
66
"syscall/js"
77

8+
jsclass "github.com/syumai/workers/internal/class"
89
jshttp "github.com/syumai/workers/internal/http"
9-
jsutil "github.com/syumai/workers/internal/utils"
1010
)
1111

1212
type Cache struct {
1313
instance js.Value
1414
}
1515

1616
func (c *Cache) Open(namespace string) error {
17-
v, err := jsutil.AwaitPromise(jsutil.RuntimeCache.Call("open", namespace))
17+
v, err := jsclass.Await(jsclass.Caches.Call("open", namespace))
1818
if err != nil {
1919
return err
2020
}
@@ -25,7 +25,7 @@ func (c *Cache) Open(namespace string) error {
2525

2626
func New() *Cache {
2727
return &Cache{
28-
instance: jsutil.RuntimeCache.Get("default"),
28+
instance: jsclass.Caches.Get("default"),
2929
}
3030
}
3131

@@ -36,7 +36,9 @@ func New() *Cache {
3636
// - Cache-Control instructs not to cache or if the response is too large.
3737
// docs: https://developers.cloudflare.com/workers/runtime-apis/cache/#put
3838
func (c *Cache) Put(req *http.Request, res *http.Response) error {
39-
_, err := jsutil.AwaitPromise(c.instance.Call("put", jshttp.ToJSRequest(req), jshttp.ToJSResponse(res)))
39+
r := jshttp.ToJSRequest(req)
40+
rs := jshttp.ToJSResponse(res)
41+
_, err := jsclass.Await(c.instance.Call("put", r, rs))
4042
if err != nil {
4143
return err
4244
}
@@ -45,47 +47,41 @@ func (c *Cache) Put(req *http.Request, res *http.Response) error {
4547

4648
var ErrCacheNotFound = errors.New("cache not found")
4749

48-
// MatchOptions represents the options of the Match method.
4950
type MatchOptions struct {
50-
// IgnoreMethod - Consider the request method a GET regardless of its actual value.
5151
IgnoreMethod bool
5252
}
5353

54-
// toJS converts MatchOptions to JS object.
5554
func (opts *MatchOptions) toJS() js.Value {
5655
if opts == nil {
5756
return js.Undefined()
5857
}
59-
obj := jsutil.NewObject()
58+
obj := jsclass.Object.New()
6059
obj.Set("ignoreMethod", opts.IgnoreMethod)
6160
return obj
6261
}
6362

6463
// Match returns the response object keyed to that request.
6564
// docs: https://developers.cloudflare.com/workers/runtime-apis/cache/#match
6665
func (c *Cache) Match(req *http.Request, opts *MatchOptions) (*http.Response, error) {
67-
res, err := jsutil.AwaitPromise(c.instance.Call("match", jshttp.ToJSRequest(req), opts.toJS()))
66+
res, err := jsclass.Await(c.instance.Call("match", jshttp.ToJSRequest(req), opts.toJS()))
6867
if err != nil {
6968
return nil, err
7069
}
7170
if res.IsUndefined() {
7271
return nil, ErrCacheNotFound
7372
}
74-
return jshttp.ToResponse(res)
73+
return jshttp.ToResponse(res), nil
7574
}
7675

77-
// DeleteOptions represents the options of the Delete method.
7876
type DeleteOptions struct {
79-
// IgnoreMethod - Consider the request method a GET regardless of its actual value.
8077
IgnoreMethod bool
8178
}
8279

83-
// toJS converts DeleteOptions to JS object.
8480
func (opts *DeleteOptions) toJS() js.Value {
8581
if opts == nil {
8682
return js.Undefined()
8783
}
88-
obj := jsutil.NewObject()
84+
obj := jsclass.Object.New()
8985
obj.Set("ignoreMethod", opts.IgnoreMethod)
9086
return obj
9187
}
@@ -94,7 +90,7 @@ func (opts *DeleteOptions) toJS() js.Value {
9490
// This method only purges content of the cache in the data center that the Worker was invoked.
9591
// Returns ErrCacheNotFount if the response was not cached.
9692
func (c *Cache) Delete(req *http.Request, opts *DeleteOptions) error {
97-
res, err := jsutil.AwaitPromise(c.instance.Call("delete", jshttp.ToJSRequest(req), opts.toJS()))
93+
res, err := jsclass.Await(c.instance.Call("delete", jshttp.ToJSRequest(req), opts.toJS()))
9894
if err != nil {
9995
return err
10096
}

cloudflare/cron/scheduler.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
package cron
22

33
import (
4-
"fmt"
4+
"errors"
55
"syscall/js"
66

7-
jsutil "github.com/syumai/workers/internal/utils"
7+
"github.com/syumai/workers/cloudflare/env"
8+
jsclass "github.com/syumai/workers/internal/class"
89
)
910

1011
type Task func(evt *CronEvent) error
1112

1213
var scheduledTask Task = func(_ *CronEvent) error {
13-
return fmt.Errorf("no scheduled implemented")
14+
return errors.New("no scheduled implemented")
1415
}
1516

16-
func runScheduler(eventObj js.Value, envObj js.Value, ctxObj js.Value) error {
17-
jsutil.RuntimeEnv = envObj
18-
jsutil.RuntimeExcutionContext = ctxObj
19-
event := NewEvent(eventObj)
17+
func runScheduler(jsEvent js.Value, envObj js.Value, ctxObj js.Value) error {
18+
jsclass.Env = envObj
19+
jsclass.ExcutionContext = ctxObj
20+
event := NewEvent(jsEvent)
21+
env.LoadEnvs()
2022

2123
return scheduledTask(event)
2224
}
@@ -33,17 +35,19 @@ func init() {
3335
resolve := pArgs[0]
3436
reject := pArgs[1]
3537

36-
err := runScheduler(controllerObj, envObj, ctxObj)
38+
go func() {
39+
err := runScheduler(controllerObj, envObj, ctxObj)
3740

38-
if err != nil {
39-
reject.Invoke(jsutil.Error(err.Error()))
40-
} else {
41-
resolve.Invoke(js.Undefined())
42-
}
41+
if err != nil {
42+
reject.Invoke(jsclass.ToJSError(err))
43+
} else {
44+
resolve.Invoke(js.Undefined())
45+
}
46+
}()
4347
return nil
4448
})
4549

46-
return jsutil.NewPromise(cb)
50+
return jsclass.Promise.New(cb)
4751
})
4852

4953
js.Global().Get("cf").Set("scheduled", runSchedulerCallback)

cloudflare/ctx/fetch_event.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ package contx
33
import (
44
"syscall/js"
55

6-
jsutil "github.com/syumai/workers/internal/utils"
6+
jsclass "github.com/syumai/workers/internal/class"
77
)
88

99
// WaitUntil extends the lifetime of the "fetch" event.
1010
// It accepts an asynchronous task which the Workers runtime will execute before the handler terminates but without blocking the response.
1111
// see: https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#waituntil
1212
func WaitUntil(task func()) {
13-
exCtx := jsutil.RuntimeExcutionContext
14-
exCtx.Call("waitUntil", jsutil.NewPromise(js.FuncOf(func(this js.Value, pArgs []js.Value) any {
13+
jsclass.ExcutionContext.Call("waitUntil", jsclass.Promise.New(js.FuncOf(func(this js.Value, pArgs []js.Value) any {
1514
resolve := pArgs[0]
1615
go func() {
1716
task()
@@ -25,11 +24,10 @@ func WaitUntil(task func()) {
2524
// Instead, the request forwards to the origin server as if it had not gone through the worker.
2625
// see: https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#passthroughonexception
2726
func PassThroughOnException() {
28-
exCtx := jsutil.RuntimeExcutionContext
29-
jsutil.AwaitPromise(jsutil.NewPromise(js.FuncOf(func(this js.Value, pArgs []js.Value) any {
27+
jsclass.Await(jsclass.Promise.New(js.FuncOf(func(this js.Value, pArgs []js.Value) any {
3028
resolve := pArgs[0]
3129
go func() {
32-
exCtx.Call("passThroughOnException")
30+
jsclass.ExcutionContext.Call("passThroughOnException")
3331
resolve.Invoke(js.Undefined())
3432
}()
3533
return js.Undefined()

cloudflare/d1/connector.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"database/sql/driver"
66
"syscall/js"
77

8-
jsutil "github.com/syumai/workers/internal/utils"
8+
jsclass "github.com/syumai/workers/internal/class"
99
)
1010

1111
type Connector struct {
@@ -19,7 +19,7 @@ var (
1919
// OpenConnector returns Connector of D1.
2020
// This method checks DB existence. If DB was not found, this function returns error.
2121
func OpenConnector(name string) (driver.Connector, error) {
22-
v := jsutil.RuntimeEnv.Get(name)
22+
v := jsclass.Env.Get(name)
2323
if v.IsUndefined() {
2424
return nil, ErrDatabaseNotFound
2525
}

cloudflare/d1/rows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"sync"
99
"syscall/js"
1010

11-
jsutil "github.com/syumai/workers/internal/utils"
11+
jsclass "github.com/syumai/workers/internal/class"
1212
)
1313

1414
type rows struct {
@@ -64,7 +64,7 @@ func convertRowColumnValueToAny(v js.Value) (driver.Value, error) {
6464
return v.String(), nil
6565
case js.TypeObject:
6666
// handle BLOB type (ArrayBuffer).
67-
src := jsutil.Uint8ArrayClass.New(v)
67+
src := jsclass.Uint8Array.New(v)
6868
dst := make([]byte, src.Length())
6969
n := js.CopyBytesToGo(dst, src)
7070
if n != len(dst) {

cloudflare/d1/stmt.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"errors"
77
"syscall/js"
88

9-
jsutil "github.com/syumai/workers/internal/utils"
9+
jsclass "github.com/syumai/workers/internal/class"
1010
)
1111

1212
type stmt struct {
@@ -39,7 +39,7 @@ func (s *stmt) ExecContext(_ context.Context, args []driver.NamedValue) (driver.
3939
argValues := make([]any, len(args))
4040
for i, arg := range args {
4141
if src, ok := arg.Value.([]byte); ok {
42-
dst := jsutil.Uint8ArrayClass.New(len(src))
42+
dst := jsclass.Uint8Array.New(len(src))
4343
if n := js.CopyBytesToJS(dst, src); n != len(src) {
4444
return nil, errors.New("incomplete copy into Uint8Array")
4545
}
@@ -49,7 +49,7 @@ func (s *stmt) ExecContext(_ context.Context, args []driver.NamedValue) (driver.
4949
}
5050
}
5151
resultPromise := s.stmtObj.Call("bind", argValues...).Call("run")
52-
resultObj, err := jsutil.AwaitPromise(resultPromise)
52+
resultObj, err := jsclass.Await(resultPromise)
5353
if err != nil {
5454
return nil, err
5555
}
@@ -66,7 +66,7 @@ func (s *stmt) QueryContext(_ context.Context, args []driver.NamedValue) (driver
6666
argValues := make([]any, len(args))
6767
for i, arg := range args {
6868
if src, ok := arg.Value.([]byte); ok {
69-
dst := jsutil.Uint8ArrayClass.New(len(src))
69+
dst := jsclass.Uint8Array.New(len(src))
7070
if n := js.CopyBytesToJS(dst, src); n != len(src) {
7171
return nil, errors.New("incomplete copy into Uint8Array")
7272
}
@@ -76,7 +76,7 @@ func (s *stmt) QueryContext(_ context.Context, args []driver.NamedValue) (driver
7676
}
7777
}
7878
resultPromise := s.stmtObj.Call("bind", argValues...).Call("raw", map[string]any{"columnNames": true})
79-
rowsArray, err := jsutil.AwaitPromise(resultPromise)
79+
rowsArray, err := jsclass.Await(resultPromise)
8080
if err != nil {
8181
return nil, err
8282
}
@@ -92,7 +92,7 @@ func (s *stmt) QueryContext(_ context.Context, args []driver.NamedValue) (driver
9292
colsArray := rowsArray.Call("shift")
9393
colsLen := colsArray.Length()
9494
cols := make([]string, colsLen)
95-
for i := 0; i < colsLen; i++ {
95+
for i := range colsLen {
9696
cols[i] = colsArray.Index(i).String()
9797
}
9898
return &rows{

cloudflare/durable_objects/container.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
jsclass "github.com/syumai/workers/internal/class"
77
jshttp "github.com/syumai/workers/internal/http"
8-
jsutil "github.com/syumai/workers/internal/utils"
98
)
109

1110
type Container struct {
@@ -21,11 +20,11 @@ func (s *Container) ContainerFetch(req *http.Request) (*http.Response, error) {
2120
return nil, err
2221
}
2322

24-
return jshttp.ToResponse(jsRes)
23+
return jshttp.ToResponse(jsRes), nil
2524
}
2625

2726
func GetContainer(binding string, id string) (*Container, error) {
28-
inst := jsutil.RuntimeEnv.Get(binding)
27+
inst := jsclass.Env.Get(binding)
2928
donamespace := &DurableObjectNamespace{instance: inst}
3029
objId := donamespace.IdFromName(id)
3130
obj, err := donamespace.Get(objId)

cloudflare/durable_objects/stub.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
package durableobjects
22

33
import (
4-
"fmt"
4+
"errors"
55
"net/http"
66
"syscall/js"
77

88
jsclass "github.com/syumai/workers/internal/class"
99
jshttp "github.com/syumai/workers/internal/http"
10-
jsutil "github.com/syumai/workers/internal/utils"
10+
jstry "github.com/syumai/workers/internal/try"
1111
)
1212

1313
type DurableObjectNamespace struct {
1414
instance js.Value
1515
}
1616

1717
func NewDurableObjectNamespace(varName string) (*DurableObjectNamespace, error) {
18-
inst := jsutil.RuntimeEnv.Get(varName)
18+
inst := jsclass.Env.Get(varName)
1919
if inst.IsUndefined() {
20-
return nil, fmt.Errorf("%s is undefined", varName)
20+
return nil, errors.New("%s is undefined" + varName)
2121
}
2222
return &DurableObjectNamespace{instance: inst}, nil
2323
}
@@ -28,7 +28,7 @@ func (ns *DurableObjectNamespace) IdFromName(name string) *DurableObjectId {
2828
}
2929

3030
func (ns *DurableObjectNamespace) IdFromString(id string) (*DurableObjectId, error) {
31-
idStr, err := jsutil.TryCatch(js.FuncOf(func(_ js.Value, args []js.Value) any {
31+
idStr, err := jstry.TryCatch(js.FuncOf(func(_ js.Value, args []js.Value) any {
3232
return ns.instance.Call("idFromString", id)
3333
}))
3434

@@ -51,7 +51,7 @@ func (ns *DurableObjectNamespace) Jurisdiction(jur string) *DurableObjectNamespa
5151

5252
func (ns *DurableObjectNamespace) Get(id *DurableObjectId) (*DurableObjectStub, error) {
5353
if id == nil || id.val.IsUndefined() {
54-
return nil, fmt.Errorf("invalid UniqueGlobalId")
54+
return nil, errors.New("invalid UniqueGlobalId")
5555
}
5656
stub := ns.instance.Call("get", id.val)
5757
return &DurableObjectStub{val: stub}, nil
@@ -74,7 +74,7 @@ func (s *DurableObjectStub) Fetch(req *http.Request) (*http.Response, error) {
7474
return nil, err
7575
}
7676

77-
return jshttp.ToResponse(jsRes)
77+
return jshttp.ToResponse(jsRes), nil
7878
}
7979

8080
func (s *DurableObjectStub) Call(funcName string) (any, error) {

0 commit comments

Comments
 (0)