forked from syumai/workers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchevent.go
39 lines (35 loc) · 1.4 KB
/
fetchevent.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
package cloudflare
import (
"context"
"github.com/tinyredglasses/workers/jsutil"
"syscall/js"
"github.com/tinyredglasses/workers/cloudflare/internal/cfruntimecontext"
)
// WaitUntil extends the lifetime of the "fetch" event.
// It accepts an asynchronous task which the Workers runtime will execute before the handler terminates but without blocking the response.
// see: https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#waituntil
func WaitUntil(ctx context.Context, task func()) {
exCtx := cfruntimecontext.MustGetExecutionContext(ctx)
exCtx.Call("waitUntil", jsutil.NewPromise(js.FuncOf(func(this js.Value, pArgs []js.Value) any {
resolve := pArgs[0]
go func() {
task()
resolve.Invoke(js.Undefined())
}()
return js.Undefined()
})))
}
// PassThroughOnException prevents a runtime error response when the Worker script throws an unhandled exception.
// Instead, the request forwards to the origin server as if it had not gone through the worker.
// see: https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#passthroughonexception
func PassThroughOnException(ctx context.Context) {
exCtx := cfruntimecontext.MustGetExecutionContext(ctx)
jsutil.AwaitPromise(jsutil.NewPromise(js.FuncOf(func(this js.Value, pArgs []js.Value) any {
resolve := pArgs[0]
go func() {
exCtx.Call("passThroughOnException")
resolve.Invoke(js.Undefined())
}()
return js.Undefined()
})))
}