Skip to content

Commit

Permalink
added typings and adjusted some return vars
Browse files Browse the repository at this point in the history
  • Loading branch information
AirBorne04 authored and AirBorne04 committed Oct 25, 2022
1 parent b90925a commit 022bf01
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
5 changes: 4 additions & 1 deletion packages/integrations/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
"homepage": "https://docs.astro.build/en/guides/integrations-guide/cloudflare/",
"exports": {
".": "./dist/index.js",
"./runtime": "./dist/runtime.js",
"./runtime": {
"import": "./dist/runtime.js",
"types": "./dist/runtime.d.ts"
},
"./server.advanced.js": "./dist/server.advanced.js",
"./server.directory.js": "./dist/server.directory.js",
"./package.json": "./package.json"
Expand Down
25 changes: 19 additions & 6 deletions packages/integrations/cloudflare/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
export type WorkerRuntime<T> = {
name: 'cloudflare';
env: T;
waitUntil(promise: Promise<any>): void;
passThroughOnException(): void;
};

export function getRuntime(request: Request): any {
return Reflect.get(
request,
Symbol.for('runtime')
);
}
export type PagesRuntime<T, U> = {
name: 'cloudflare';
env: T;
functionPath: string;
params: Record<string, string>;
data: U;
waitUntil(promise: Promise<any>): void;
next(request: Request): void;
};

export function getRuntime<T, U>(request: Request): WorkerRuntime<T> | PagesRuntime<T, U> {
return Reflect.get(request, Symbol.for('runtime'));
}
10 changes: 2 additions & 8 deletions packages/integrations/cloudflare/src/server.advanced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import { App } from 'astro/app';

type Env = {
ASSETS: { fetch: (req: Request) => Promise<Response> };
name: string;
name: string;
};

export function createExports(manifest: SSRManifest) {
const app = new App(manifest, false);

const fetch = async (request: Request, env: Env, context: any) => {

const { origin, pathname } = new URL(request.url);
env.name = "cloudflare";

// static assets
if (manifest.assets.has(pathname)) {
Expand All @@ -29,11 +27,7 @@ export function createExports(manifest: SSRManifest) {
Symbol.for('astro.clientAddress'),
request.headers.get('cf-connecting-ip')
);
Reflect.set(
request,
Symbol.for('runtime'),
{ env, ...context }
);
Reflect.set(request, Symbol.for('runtime'), { env, name: 'cloudflare', ...context });
let response = await app.render(request, routeData);

if (app.setCookieHeaders) {
Expand Down
14 changes: 6 additions & 8 deletions packages/integrations/cloudflare/src/server.directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ export function createExports(manifest: SSRManifest) {
const onRequest = async ({
request,
next,
...runtimeEnv
...runtimeEnv
}: {
request: Request;
next: (request: Request) => void;
} & Record<string, unknown>) => {
const { origin, pathname } = new URL(request.url);
runtimeEnv.name = "cloudflare";

// static assets
if (manifest.assets.has(pathname)) {
const assetRequest = new Request(`${origin}/static${pathname}`, request);
Expand All @@ -30,11 +28,11 @@ export function createExports(manifest: SSRManifest) {
Symbol.for('astro.clientAddress'),
request.headers.get('cf-connecting-ip')
);
Reflect.set(
request,
Symbol.for('runtime'),
runtimeEnv
);
Reflect.set(request, Symbol.for('runtime'), {
...runtimeEnv,
name: 'cloudflare',
next,
});
let response = await app.render(request, routeData);

if (app.setCookieHeaders) {
Expand Down

0 comments on commit 022bf01

Please sign in to comment.