-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from syumai/add-runtime-context
add runtime context
- Loading branch information
Showing
18 changed files
with
204 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
package cloudflare | ||
|
||
import "github.com/syumai/workers/internal/jsutil" | ||
import ( | ||
"context" | ||
) | ||
|
||
// Getenv gets a value of an environment variable. | ||
// - https://developers.cloudflare.com/workers/platform/environment-variables/ | ||
// - Technically, this function is just an alias for js.Global().Get(env_name).String(). | ||
func Getenv(name string) string { | ||
return jsutil.Global.Get(name).String() | ||
// - This function panics when a runtime context is not found. | ||
func Getenv(ctx context.Context, name string) string { | ||
return getRuntimeContextEnv(ctx).Get(name).String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"syscall/js" | ||
|
||
"github.com/syumai/workers/internal/runtimecontext" | ||
) | ||
|
||
/** | ||
* The type definition of RuntimeContext for Cloudflare Worker expects: | ||
* ```ts | ||
* type RuntimeContext { | ||
* env: Env; | ||
* ctx: ExecutionContext; | ||
* } | ||
* ``` | ||
* This type is based on the type definition of ExportedHandlerFetchHandler. | ||
* - see: https://github.com/cloudflare/workers-types/blob/c8d9533caa4415c2156d2cf1daca75289d01ae70/index.d.ts#LL564 | ||
*/ | ||
|
||
// getRuntimeContextEnv gets object which holds environment variables bound to Cloudflare worker. | ||
// - see: https://github.com/cloudflare/workers-types/blob/c8d9533caa4415c2156d2cf1daca75289d01ae70/index.d.ts#L566 | ||
func getRuntimeContextEnv(ctx context.Context) js.Value { | ||
runtimeCtxValue := runtimecontext.MustExtract(ctx) | ||
return runtimeCtxValue.Get("env") | ||
} | ||
|
||
// getExecutionContext gets ExecutionContext object from context. | ||
// - see: https://github.com/cloudflare/workers-types/blob/c8d9533caa4415c2156d2cf1daca75289d01ae70/index.d.ts#L567 | ||
// - see also: https://github.com/cloudflare/workers-types/blob/c8d9533caa4415c2156d2cf1daca75289d01ae70/index.d.ts#L554 | ||
func getExecutionContext(ctx context.Context) js.Value { | ||
runtimeCtxValue := runtimecontext.MustExtract(ctx) | ||
return runtimeCtxValue.Get("ctx") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.