-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement
getCloudflareContext
for production (#31)
* implement `getCloudflareContext` for production --------- Co-authored-by: Victor Berchet <[email protected]>
- Loading branch information
1 parent
9758666
commit af15fd1
Showing
44 changed files
with
181 additions
and
59 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
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
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 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,53 @@ | ||
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, | ||
> = { | ||
/** | ||
* the worker's [bindings](https://developers.cloudflare.com/workers/runtime-apis/bindings/) | ||
*/ | ||
env: CloudflareEnv; | ||
/** | ||
* the request's [cf properties](https://developers.cloudflare.com/workers/runtime-apis/request/#the-cf-property-requestinitcfproperties) | ||
*/ | ||
cf: CfProperties; | ||
/** | ||
* the current [execution context](https://developers.cloudflare.com/workers/runtime-apis/context) | ||
*/ | ||
ctx: Context; | ||
}; | ||
|
||
// Note: this symbol needs to be kept in sync with the one used in `src/cli/templates/worker.ts` | ||
const cloudflareContextSymbol = Symbol.for("__cloudflare-context__"); | ||
|
||
/** | ||
* Utility to get the current Cloudflare context | ||
* | ||
* Throws an error if the context could not be retrieved | ||
* | ||
* @returns the cloudflare context | ||
*/ | ||
export async function getCloudflareContext< | ||
CfProperties extends Record<string, unknown> = IncomingRequestCfProperties, | ||
Context = ExecutionContext, | ||
>(): Promise<CloudflareContext<CfProperties, Context>> { | ||
const cloudflareContext = ( | ||
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 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"; |
11 changes: 0 additions & 11 deletions
11
packages/cloudflare/src/build/patches/investigated/copy-package.ts
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
14 changes: 14 additions & 0 deletions
14
packages/cloudflare/src/cli/build/patches/investigated/copy-package-cli-files.ts
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,14 @@ | ||
import { Config } from "../../../config"; | ||
import { cpSync } from "node:fs"; | ||
import path from "node:path"; | ||
|
||
/** | ||
* Copies the template files present in the cloudflare adapter package into the standalone node_modules folder | ||
*/ | ||
export function copyPackageCliFiles(packageDistDir: string, config: Config) { | ||
console.log("# copyPackageTemplateFiles"); | ||
const sourceDir = path.join(packageDistDir, "cli"); | ||
const destinationDir = path.join(config.paths.internalPackage, "cli"); | ||
|
||
cpSync(sourceDir, destinationDir, { recursive: true }); | ||
} |
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
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 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 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 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 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 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 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 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.
66 changes: 42 additions & 24 deletions
66
packages/cloudflare/src/templates/worker.ts → ...es/cloudflare/src/cli/templates/worker.ts
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 |
---|---|---|
@@ -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]; |