-
Notifications
You must be signed in to change notification settings - Fork 72
implement getCloudflareContext
for production
#31
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b753375
implement `getCloudflareContext` for production
dario-piotrowicz 6ea8d74
implement solution using ALS
dario-piotrowicz a83fba6
add ts-doc comment to getCloudflareContext function
dario-piotrowicz e5aa81a
Update examples/api/app/api/hello/route.ts
dario-piotrowicz 2f5b4f7
update cloudflare context ts-docs comments as suggested
dario-piotrowicz b7c71a0
update various paths
dario-piotrowicz 7801c7a
add comment
dario-piotrowicz 271b0d5
remove "object containing" from comments
dario-piotrowicz 63646cd
update comments
dario-piotrowicz 6a18a09
update comment
dario-piotrowicz 75caef0
use any
dario-piotrowicz 5b57287
improve cf typing
dario-piotrowicz 61cbd7f
add comments regarding keeping the cloudflare-context symbols in sync
dario-piotrowicz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,5 @@ | ||
// Generated by Wrangler by running `wrangler types --env-interface CloudflareEnv` | ||
|
||
interface CloudflareEnv { | ||
hello: "Hello World from the cloudflare context!"; | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,50 @@ | ||
import "server-only"; | ||
|
||
declare global { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
interface CloudflareEnv {} | ||
} | ||
|
||
export type CloudflareContext< | ||
CfProperties extends Record<string, unknown> = IncomingRequestCfProperties, | ||
Context = ExecutionContext, | ||
> = { | ||
/** | ||
* object containing the worker's bindings ([docs](https://developers.cloudflare.com/workers/runtime-apis/bindings/)) | ||
dario-piotrowicz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
*/ | ||
env: CloudflareEnv; | ||
/** | ||
* object containing information regarding the current request ([docs](https://developers.cloudflare.com/workers/runtime-apis/request/#the-cf-property-requestinitcfproperties)) | ||
*/ | ||
cf: CfProperties; | ||
/** | ||
* the current execution context object ([docs](https://developers.cloudflare.com/workers/runtime-apis/context)) | ||
*/ | ||
ctx: Context; | ||
}; | ||
|
||
const cloudflareContextSymbol = Symbol.for("__cloudflare-context__"); | ||
|
||
/** | ||
* Utility to get the current Cloudflare context | ||
dario-piotrowicz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @returns the cloudflare context | ||
*/ | ||
export async function getCloudflareContext< | ||
CfProperties extends Record<string, unknown> = IncomingRequestCfProperties, | ||
Context = ExecutionContext, | ||
>(): Promise<CloudflareContext<CfProperties, Context>> { | ||
const cloudflareContext = ( | ||
dario-piotrowicz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
globalThis as unknown as { | ||
[cloudflareContextSymbol]: CloudflareContext<CfProperties, Context> | undefined; | ||
} | ||
)[cloudflareContextSymbol]; | ||
|
||
if (!cloudflareContext) { | ||
// TODO: cloudflareContext should always be present in production/preview, if not it means that this | ||
// is running under `next dev`, in this case use `getPlatformProxy` to return local proxies | ||
throw new Error("Cloudflare context is not defined!"); | ||
} | ||
|
||
return cloudflareContext; | ||
} |
This file contains hidden or 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 @@ | ||
export * from "./get-cloudflare-context"; |
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ages/cloudflare/src/build/build-worker.ts → .../cloudflare/src/cli/build/build-worker.ts
This file contains hidden or 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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...uild/patches/investigated/copy-package.ts → ...uild/patches/investigated/copy-package.ts
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...build/patches/investigated/patch-cache.ts → ...build/patches/investigated/patch-cache.ts
This file contains hidden or 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...gated/update-webpack-chunks-file/index.ts → ...gated/update-webpack-chunks-file/index.ts
This file contains hidden or 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...es/to-investigate/inline-eval-manifest.ts → ...es/to-investigate/inline-eval-manifest.ts
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...hes/to-investigate/inline-next-require.ts → ...hes/to-investigate/inline-next-require.ts
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
.../patches/to-investigate/patch-find-dir.ts → .../patches/to-investigate/patch-find-dir.ts
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...patches/to-investigate/patch-read-file.ts → ...patches/to-investigate/patch-read-file.ts
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...d/patches/to-investigate/wrangler-deps.ts → ...d/patches/to-investigate/wrangler-deps.ts
This file contains hidden or 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
64 changes: 41 additions & 23 deletions
64
packages/cloudflare/src/templates/worker.ts → ...es/cloudflare/src/cli/templates/worker.ts
This file contains hidden or 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 hidden or 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,16 +1,27 @@ | ||
import { cp } from "fs/promises"; | ||
import { defineConfig } from "tsup"; | ||
|
||
export default defineConfig({ | ||
entry: ["src/index.ts", "src/cache-handler.ts"], | ||
outDir: "dist", | ||
const cliConfig = defineConfig({ | ||
entry: ["src/cli/index.ts", "src/cli/cache-handler.ts"], | ||
outDir: "dist/cli", | ||
dts: false, | ||
format: ["esm"], | ||
platform: "node", | ||
external: ["esbuild"], | ||
onSuccess: async () => { | ||
await cp(`${__dirname}/src/templates`, `${__dirname}/dist/templates`, { | ||
await cp(`${__dirname}/src/cli/templates`, `${__dirname}/dist/cli/templates`, { | ||
recursive: true, | ||
}); | ||
}, | ||
}); | ||
|
||
const apiConfig = defineConfig({ | ||
entry: ["src/api"], | ||
outDir: "dist/api", | ||
dts: true, | ||
format: ["esm"], | ||
platform: "node", | ||
external: ["server-only"], | ||
}); | ||
|
||
export default [cliConfig, apiConfig]; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.