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

feat: implement adapter.emulate #11732

Merged
merged 18 commits into from
Mar 12, 2024
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
29 changes: 29 additions & 0 deletions packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { execSync } from 'node:child_process';
import esbuild from 'esbuild';
import toml from '@iarna/toml';
import { fileURLToPath } from 'node:url';
import { getPlatformProxy } from 'wrangler';

/**
* @typedef {{
Expand Down Expand Up @@ -140,6 +141,34 @@ export default function ({ config = 'wrangler.toml' } = {}) {
const bucket_dir = `${site.bucket}${builder.config.kit.paths.base}`;
builder.writeClient(bucket_dir);
builder.writePrerendered(bucket_dir);
},

async emulate() {
const proxy = await getPlatformProxy();
const platform = /** @type {App.Platform} */ ({
env: proxy.env,
context: proxy.ctx,
caches: proxy.caches,
cf: proxy.cf
});

/** @type {Record<string, any>} */
const env = {};
const prerender_platform = /** @type {App.Platform} */ (/** @type {unknown} */ ({ env }));

for (const key in proxy.env) {
Object.defineProperty(env, key, {
get: () => {
throw new Error(`Cannot access platform.env.${key} in a prerenderable route`);
}
});
}

return {
platform: ({ prerender }) => {
return prerender ? prerender_platform : platform;
}
};
}
};
}
Expand Down
5 changes: 3 additions & 2 deletions packages/adapter-cloudflare-workers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"scripts": {
"lint": "prettier --check .",
"format": "pnpm lint --write",
"check": "tsc"
"check": "tsc --skipLibCheck"
},
"dependencies": {
"@cloudflare/workers-types": "^4.20231121.0",
Expand All @@ -41,6 +41,7 @@
"typescript": "^5.3.3"
},
"peerDependencies": {
"@sveltejs/kit": "^2.0.0"
"@sveltejs/kit": "^2.0.0",
"wrangler": "^3.28.4"
}
}
6 changes: 3 additions & 3 deletions packages/adapter-cloudflare/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Cache, CacheStorage, IncomingRequestCfProperties } from '@cloudflare/workers-types';
import { CacheStorage, IncomingRequestCfProperties } from '@cloudflare/workers-types';

declare global {
namespace App {
export interface Platform {
context?: {
context: {
waitUntil(promise: Promise<any>): void;
};
caches?: CacheStorage & { default: Cache };
caches: CacheStorage;
cf?: IncomingRequestCfProperties;
}
}
Expand Down
28 changes: 28 additions & 0 deletions packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { writeFileSync } from 'node:fs';
import * as path from 'node:path';
import { fileURLToPath } from 'node:url';
import * as esbuild from 'esbuild';
import { getPlatformProxy } from 'wrangler';

// list from https://developers.cloudflare.com/workers/runtime-apis/nodejs/
const compatible_node_modules = [
Expand Down Expand Up @@ -124,6 +125,33 @@ export default function (options = {}) {
}`
);
}
},
async emulate() {
const proxy = await getPlatformProxy();
const platform = /** @type {App.Platform} */ ({
env: proxy.env,
context: proxy.ctx,
caches: proxy.caches,
cf: proxy.cf
});

/** @type {Record<string, any>} */
const env = {};
const prerender_platform = /** @type {App.Platform} */ (/** @type {unknown} */ ({ env }));

for (const key in proxy.env) {
Object.defineProperty(env, key, {
get: () => {
throw new Error(`Cannot access platform.env.${key} in a prerenderable route`);
}
});
}

return {
platform: ({ prerender }) => {
return prerender ? prerender_platform : platform;
}
};
}
};
}
Expand Down
3 changes: 2 additions & 1 deletion packages/adapter-cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"typescript": "^5.3.3"
},
"peerDependencies": {
"@sveltejs/kit": "^2.0.0"
"@sveltejs/kit": "^2.0.0",
"wrangler": "^3.28.4"
},
"publishConfig": {
"access": "public"
Expand Down
Loading
Loading