Skip to content
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

refactors runtime #196

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions packages/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,20 @@
"dependencies": {
"@astrojs/underscore-redirects": "^0.3.3",
"@cloudflare/workers-types": "^4.20240320.1",
"miniflare": "^3.20240314.0",
"@iarna/toml": "^2.2.5",
"dotenv": "^16.3.1",
"miniflare": "^3.20240320.0",
"esbuild": "^0.19.5",
"find-up": "^6.3.0",
"tiny-glob": "^0.2.9"
"tiny-glob": "^0.2.9",
"wrangler": "^3.36.0"
},
"peerDependencies": {
"astro": "^4.2.0"
},
"devDependencies": {
"execa": "^8.0.1",
"fast-glob": "^3.3.2",
"@types/iarna__toml": "^2.0.2",
"strip-ansi": "^7.1.0",
"astro": "^4.5.8",
"cheerio": "1.0.0-rc.12",
"wrangler": "^3.36.0",
"@astrojs/test-utils": "workspace:*",
"astro-scripts": "workspace:*"
},
Expand Down
69 changes: 25 additions & 44 deletions packages/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { AstroConfig, AstroIntegration, RouteData } from 'astro';
import type { LocalPagesRuntime, LocalWorkersRuntime, RUNTIME } from './utils/local-runtime.js';

import * as fs from 'node:fs';
import * as os from 'node:os';
Expand All @@ -9,10 +8,10 @@ import { createRedirectsFromAstroRoutes } from '@astrojs/underscore-redirects';
import { AstroError } from 'astro/errors';
import esbuild from 'esbuild';
import glob from 'tiny-glob';
import { getPlatformProxy } from 'wrangler';
import { getAdapter } from './getAdapter.js';
import { deduplicatePatterns } from './utils/deduplicatePatterns.js';
import { prepareImageConfig } from './utils/image-config.js';
import { getLocalRuntime, getRuntimeConfig } from './utils/local-runtime.js';
import { prependForwardSlash } from './utils/prependForwardSlash.js';
import { rewriteWasmImportPath } from './utils/rewriteWasmImportPath.js';
import { patchSharpBundle } from './utils/sharpBundlePatch.js';
Expand All @@ -37,21 +36,18 @@ export type Options = {
exclude?: string[];
};
/**
* @deprecated Removed in v10. Configure bindings in `wrangler.toml`. Leveraging Cloudflare's API simplifies setup and ensures full compatibility with Wrangler configurations. Use `platformProxy` instead.
* Proxy configuration for the platform.
*/
runtime?:
| { mode: 'off' }
| {
mode: Extract<RUNTIME, { type: 'pages' }>['mode'];
type: Extract<RUNTIME, { type: 'pages' }>['type'];
persistTo?: Extract<RUNTIME, { type: 'pages' }>['persistTo'];
bindings?: Extract<RUNTIME, { type: 'pages' }>['bindings'];
}
| {
mode: Extract<RUNTIME, { type: 'workers' }>['mode'];
type: Extract<RUNTIME, { type: 'workers' }>['type'];
persistTo?: Extract<RUNTIME, { type: 'workers' }>['persistTo'];
};
platformProxy?: {
/** Toggle the proxy. Default `undefined`, which equals to `false`. */
enabled?: boolean;
/** Path to the configuration file. Default `wrangler.toml`. */
configPath?: string;
/** Enable experimental support for JSON configuration. Default `false`. */
experimentalJsonConfig?: boolean;
/** Configuration persistence settings. Default '.wrangler/state/v3' */
persist?: boolean | { path: string };
};
wasmModuleImports?: boolean;
};

Expand All @@ -66,12 +62,9 @@ interface BuildConfig {
export default function createIntegration(args?: Options): AstroIntegration {
let _config: AstroConfig;
let _buildConfig: BuildConfig;
let _localRuntime: LocalPagesRuntime | LocalWorkersRuntime;

const SERVER_BUILD_FOLDER = '/$server_build/';

const runtimeMode = getRuntimeConfig(args?.runtime);

return {
name: '@astrojs/cloudflare',
hooks: {
Expand Down Expand Up @@ -112,41 +105,29 @@ export default function createIntegration(args?: Options): AstroIntegration {
);
}
},
'astro:server:setup': ({ server, logger }) => {
if (runtimeMode.mode === 'local') {
server.middlewares.use(async function middleware(req, res, next) {
_localRuntime = getLocalRuntime(_config, runtimeMode, logger);
'astro:server:setup': async ({ server, logger }) => {
if (args?.platformProxy?.enabled === true) {
const platformProxy = await getPlatformProxy({
configPath: args.platformProxy.configPath ?? 'wrangler.toml',
experimentalJsonConfig: args.platformProxy.experimentalJsonConfig ?? false,
persist: args.platformProxy.persist ?? true,
});

const bindings = await _localRuntime.getBindings();
const secrets = await _localRuntime.getSecrets();
const caches = await _localRuntime.getCaches();
const cf = await _localRuntime.getCF();
const clientLocalsSymbol = Symbol.for('astro.locals');

const clientLocalsSymbol = Symbol.for('astro.locals');
server.middlewares.use(async function middleware(req, res, next) {
Reflect.set(req, clientLocalsSymbol, {
runtime: {
env: {
CF_PAGES_URL: `http://${req.headers.host}`,
...bindings,
...secrets,
},
cf: cf,
caches: caches,
waitUntil: (_promise: Promise<any>) => {
return;
},
env: platformProxy.env,
cf: platformProxy.cf,
caches: platformProxy.caches,
ctx: platformProxy.ctx,
},
});
next();
});
}
},
'astro:server:done': async ({ logger }) => {
if (_localRuntime) {
logger.info('Cleaning up the local Cloudflare runtime.');
await _localRuntime.dispose();
}
},
'astro:build:setup': ({ vite, target }) => {
if (target === 'server') {
vite.resolve ||= {};
Expand Down
139 changes: 0 additions & 139 deletions packages/cloudflare/src/tmp-types.d.ts

This file was deleted.

Loading
Loading