From cea45d1443f507eb0ed747658c7e0f3eedecab1b Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Sat, 23 Sep 2023 09:43:33 +0200 Subject: [PATCH] feat(cloudflare): add D1 database binding This addition allows using D1 databases within the Astro dev server. --- .changeset/hungry-mails-boil.md | 5 + packages/integrations/cloudflare/.gitignore | 3 +- packages/integrations/cloudflare/package.json | 1 + packages/integrations/cloudflare/src/index.ts | 32 ++++++- .../cloudflare/src/utils/parser.ts | 18 +++- .../integrations/cloudflare/test/cf.test.js | 17 +++- .../test/fixtures/cf/src/pages/d1.astro | 21 ++++ .../cloudflare/test/fixtures/cf/wrangler.toml | 12 +++ pnpm-lock.yaml | 95 ++++++++++++++++--- 9 files changed, 181 insertions(+), 23 deletions(-) create mode 100644 .changeset/hungry-mails-boil.md create mode 100644 packages/integrations/cloudflare/test/fixtures/cf/src/pages/d1.astro diff --git a/.changeset/hungry-mails-boil.md b/.changeset/hungry-mails-boil.md new file mode 100644 index 0000000000000..d856ee39786eb --- /dev/null +++ b/.changeset/hungry-mails-boil.md @@ -0,0 +1,5 @@ +--- +'@astrojs/cloudflare': minor +--- + +Introduce support for local D1 bindings. Enhance development experience by allowing direct integration with `astro dev`. diff --git a/packages/integrations/cloudflare/.gitignore b/packages/integrations/cloudflare/.gitignore index 58b200bf60faf..0134f82dfeae6 100644 --- a/packages/integrations/cloudflare/.gitignore +++ b/packages/integrations/cloudflare/.gitignore @@ -1,2 +1,3 @@ # Astro cloudflare directory mode creates a function directory -functions \ No newline at end of file +functions +.mf diff --git a/packages/integrations/cloudflare/package.json b/packages/integrations/cloudflare/package.json index b7590f4eb2f07..a348920da5765 100644 --- a/packages/integrations/cloudflare/package.json +++ b/packages/integrations/cloudflare/package.json @@ -36,6 +36,7 @@ "dependencies": { "@astrojs/underscore-redirects": "workspace:*", "@cloudflare/workers-types": "^4.20230821.0", + "miniflare": "^3.20230918.0", "@iarna/toml": "^2.2.5", "@miniflare/cache": "^2.14.1", "@miniflare/shared": "^2.14.1", diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index d2d67600dcff3..79fe2e306c296 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -6,18 +6,19 @@ import { NoOpLog } from '@miniflare/shared'; import { MemoryStorage } from '@miniflare/storage-memory'; import { AstroError } from 'astro/errors'; import esbuild from 'esbuild'; +import { Miniflare } from 'miniflare'; import * as fs from 'node:fs'; import * as os from 'node:os'; import { dirname, relative, sep } from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; import glob from 'tiny-glob'; -import { getCFObject } from './utils/getCFObject.js'; -import { getEnvVars } from './utils/parser.js'; +import { deduplicatePatterns } from './utils/deduplicatePatterns.js'; import { getAdapter } from './utils/getAdapter.js'; -import { wasmModuleLoader } from './utils/wasm-module-loader.js'; +import { getCFObject } from './utils/getCFObject.js'; +import { getD1Bindings, getEnvVars } from './utils/parser.js'; import { prependForwardSlash } from './utils/prependForwardSlash.js'; -import { deduplicatePatterns } from './utils/deduplicatePatterns.js'; import { rewriteWasmImportPath } from './utils/rewriteWasmImportPath.js'; +import { wasmModuleLoader } from './utils/wasm-module-loader.js'; export type { AdvancedRuntime } from './entrypoints/server.advanced.js'; export type { DirectoryRuntime } from './entrypoints/server.directory.js'; @@ -69,6 +70,7 @@ class StorageFactory { export default function createIntegration(args?: Options): AstroIntegration { let _config: AstroConfig; let _buildConfig: BuildConfig; + let _mf: Miniflare; let _entryPoints = new Map(); const SERVER_BUILD_FOLDER = '/$server_build/'; @@ -122,6 +124,21 @@ export default function createIntegration(args?: Options): AstroIntegration { try { const cf = await getCFObject(runtimeMode); const vars = await getEnvVars(); + const D1Bindings = await getD1Bindings(); + let bindingsEnv = new Object({}); + + // monkey patch to get rid of PWD error + process.env.PWD = process.cwd(); + _mf = new Miniflare({ + modules: true, + script: '', + d1Databases: D1Bindings, + d1Persist: true, + }); + for (const D1Binding of D1Bindings) { + const db = await _mf.getD1Database(D1Binding); + Reflect.set(bindingsEnv, D1Binding, db); + } const clientLocalsSymbol = Symbol.for('astro.locals'); Reflect.set(req, clientLocalsSymbol, { @@ -136,6 +153,7 @@ export default function createIntegration(args?: Options): AstroIntegration { // will be fetched from git dynamically once we support mocking of bindings CF_PAGES_COMMIT_SHA: 'TBA', CF_PAGES_URL: `http://${req.headers.host}`, + ...bindingsEnv, ...vars, }, cf: cf, @@ -157,6 +175,12 @@ export default function createIntegration(args?: Options): AstroIntegration { }); } }, + 'astro:server:done': async () => { + if (_mf) { + await _mf.ready; + await _mf.dispose(); + } + }, 'astro:build:setup': ({ vite, target }) => { if (target === 'server') { vite.resolve ||= {}; diff --git a/packages/integrations/cloudflare/src/utils/parser.ts b/packages/integrations/cloudflare/src/utils/parser.ts index e9a9cdd00a1bc..463655918efbe 100644 --- a/packages/integrations/cloudflare/src/utils/parser.ts +++ b/packages/integrations/cloudflare/src/utils/parser.ts @@ -119,7 +119,8 @@ function getVarsForDev(config: any, configPath: string | undefined): any { return config.vars; } } -export async function getEnvVars() { + +function parseConfig() { let rawConfig; const configPath = findWranglerToml(process.cwd(), false); // false = args.experimentalJsonConfig if (!configPath) { @@ -129,6 +130,21 @@ export async function getEnvVars() { if (configPath?.endsWith('toml')) { rawConfig = parseTOML(fs.readFileSync(configPath).toString(), configPath); } + return { rawConfig, configPath }; +} + +export async function getEnvVars() { + const { rawConfig, configPath } = parseConfig(); const vars = getVarsForDev(rawConfig, configPath); return vars; } + +export async function getD1Bindings() { + const { rawConfig } = parseConfig(); + if (!rawConfig) return []; + if (!rawConfig?.d1_databases) return []; + const bindings = (rawConfig?.d1_databases as []).map( + (binding: { binding: string }) => binding.binding + ); + return bindings; +} diff --git a/packages/integrations/cloudflare/test/cf.test.js b/packages/integrations/cloudflare/test/cf.test.js index 78a18dcdf5927..87e718868cf70 100644 --- a/packages/integrations/cloudflare/test/cf.test.js +++ b/packages/integrations/cloudflare/test/cf.test.js @@ -54,11 +54,6 @@ describe('Astro Cloudflare Runtime', () => { adapter: cloudflare({ runtime: 'local', }), - image: { - service: { - entrypoint: 'astro/assets/services/noop', - }, - }, }); process.chdir('./test/fixtures/cf'); devServer = await fixture.startDevServer(); @@ -76,4 +71,16 @@ describe('Astro Cloudflare Runtime', () => { expect($('#hasRuntime').text()).to.equal('true'); expect($('#hasCache').text()).to.equal('true'); }); + + it('adds D1 mocking', async () => { + expect(await fixture.pathExists('../.mf/d1')).to.be.true; + + let res = await fixture.fetch('/d1'); + expect(res.status).to.equal(200); + let html = await res.text(); + let $ = cheerio.load(html); + expect($('#hasDB').text()).to.equal('true'); + expect($('#hasPRODDB').text()).to.equal('true'); + expect($('#hasACCESS').text()).to.equal('true'); + }); }); diff --git a/packages/integrations/cloudflare/test/fixtures/cf/src/pages/d1.astro b/packages/integrations/cloudflare/test/fixtures/cf/src/pages/d1.astro new file mode 100644 index 0000000000000..a28940e9fe461 --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/cf/src/pages/d1.astro @@ -0,0 +1,21 @@ +--- +const runtime = Astro.locals.runtime; +const db = runtime.env?.D1; +await db.exec("CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)"); +await db.exec("INSERT INTO test (name) VALUES ('true')"); +const result = await db.prepare("SELECT * FROM test").all(); +--- + + + + + + + D1 + + +
{!!runtime.env?.D1}
+
{!!runtime.env?.D1_PROD}
+
{!!result.results[0].name}
+ + diff --git a/packages/integrations/cloudflare/test/fixtures/cf/wrangler.toml b/packages/integrations/cloudflare/test/fixtures/cf/wrangler.toml index ba0fa64c4b09c..6d330f332be5d 100644 --- a/packages/integrations/cloudflare/test/fixtures/cf/wrangler.toml +++ b/packages/integrations/cloudflare/test/fixtures/cf/wrangler.toml @@ -2,3 +2,15 @@ name = "test" [vars] COOL = "ME" + +[[d1_databases]] +binding = "D1" # Should match preview_database_id, i.e. available in your Worker on env.DB +database_name = "" +database_id = "" +preview_database_id = "D1" # Required for Pages local development + +[[d1_databases]] +binding = "D1_PROD" # Should match preview_database_id +database_name = "" +database_id = "" +preview_database_id = "D1_PROD" # Required for Pages local development diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 94c742651cc01..d82daf77ae62e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3646,6 +3646,9 @@ importers: find-up: specifier: ^6.3.0 version: 6.3.0 + miniflare: + specifier: ^3.20230918.0 + version: 3.20230918.0 tiny-glob: specifier: ^0.2.9 version: 0.2.9 @@ -7041,6 +7044,15 @@ packages: dev: true optional: true + /@cloudflare/workerd-darwin-64@1.20230904.0: + resolution: {integrity: sha512-/GDlmxAFbDtrQwP4zOXFbqOfaPvkDxdsCoEa+KEBcAl5uR98+7WW5/b8naBHX+t26uS7p4bLlImM8J5F1ienRQ==} + engines: {node: '>=16'} + cpu: [x64] + os: [darwin] + requiresBuild: true + dev: false + optional: true + /@cloudflare/workerd-darwin-arm64@1.20230814.1: resolution: {integrity: sha512-U2mcgi+AiuI/4EY5Wk/GmygiNoCNw/V2mcHmxESqe4r6XbJYOzBdEsjnqJ05rqd0JlEM8m64jRtE6/qBnQHygg==} engines: {node: '>=16'} @@ -7050,6 +7062,15 @@ packages: dev: true optional: true + /@cloudflare/workerd-darwin-arm64@1.20230904.0: + resolution: {integrity: sha512-x8WXNc2xnDqr5y1iirnNdyx8GZY3rL5xiF7ebK3mKQeB+jFjkhO71yuPTkDCzUWtOvw1Wfd4jbwy4wxacMX4mQ==} + engines: {node: '>=16'} + cpu: [arm64] + os: [darwin] + requiresBuild: true + dev: false + optional: true + /@cloudflare/workerd-linux-64@1.20230814.1: resolution: {integrity: sha512-Q4kITXLTCuG2i2Z01fbb5AjVRRIf3+lS4ZVsFbTbIwtcOOG4Ozcw7ee7tKsFES7hFqR4Eg9gMG4/aS0mmi+L2g==} engines: {node: '>=16'} @@ -7059,6 +7080,15 @@ packages: dev: true optional: true + /@cloudflare/workerd-linux-64@1.20230904.0: + resolution: {integrity: sha512-V58xyMS3oDpKO8Dpdh0r0BXm99OzoGgvWe9ufttVraj/1NTMGELwb6i9ySb8k3F1J9m/sO26+TV7pQc/bGC1VQ==} + engines: {node: '>=16'} + cpu: [x64] + os: [linux] + requiresBuild: true + dev: false + optional: true + /@cloudflare/workerd-linux-arm64@1.20230814.1: resolution: {integrity: sha512-BX5SaksXw+pkREVw3Rw2eSNXplqZw+14CcwW/5x/4oq/C6yn5qCvKxJfM7pukJGMI4wkJPOYops7B3g27FB/HA==} engines: {node: '>=16'} @@ -7068,6 +7098,15 @@ packages: dev: true optional: true + /@cloudflare/workerd-linux-arm64@1.20230904.0: + resolution: {integrity: sha512-VrDaW+pjb5IAKEnNWtEaFiG377kXKmk5Fu0Era4W+jKzPON2BW/qRb/4LNHXQ4yxg/2HLm7RiUTn7JZtt1qO6A==} + engines: {node: '>=16'} + cpu: [arm64] + os: [linux] + requiresBuild: true + dev: false + optional: true + /@cloudflare/workerd-windows-64@1.20230814.1: resolution: {integrity: sha512-GWHqfyhsG/1wm2W8afkYX3q3fWXUWWD8NGtHfAs6ZVTHdW3mmYyMhKR0lc6ptBwz5i5aXRlP2S+CxxxwwDbKpw==} engines: {node: '>=16'} @@ -7077,6 +7116,15 @@ packages: dev: true optional: true + /@cloudflare/workerd-windows-64@1.20230904.0: + resolution: {integrity: sha512-/R/dE8uy+8J2YeXfDhI8/Bg7YUirdbbjH5/l/Vv00ZRE0lC3nPLcYeyBXSwXIQ6/Xht3gN+lksLQgKd0ZWRd+Q==} + engines: {node: '>=16'} + cpu: [x64] + os: [win32] + requiresBuild: true + dev: false + optional: true + /@cloudflare/workers-types@4.20230821.0: resolution: {integrity: sha512-lVQSyr5E4CEkQw7WIdsrMTj+kHjsm28mJ0B5AhNFByKR+16KTFsU/RW/nGLKHHW2jxT5lvYI+HjNQMzC9QR8Ng==} dev: false @@ -9804,7 +9852,6 @@ packages: resolution: {integrity: sha512-xvsWESUJn0JN421Xb9MQw6AsMHRCUknCe0Wjlxvjud80mU4E6hQf1A6NzQKcYNmYw62MfzEtXc+badstZP3JpQ==} dependencies: printable-characters: 1.0.42 - dev: true /assertion-error@1.1.0: resolution: {integrity: sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==} @@ -10273,7 +10320,6 @@ packages: tslib: 2.6.2 transitivePeerDependencies: - supports-color - dev: true /ccount@2.0.1: resolution: {integrity: sha512-eyrF0jiFpY+3drT6383f1qhkbGsLSifNAjA61IUjZjmLCWjItY6LB9ft9YhoDgwfmclB2zhu51Lc7+95b8NRAg==} @@ -10806,7 +10852,6 @@ packages: /data-uri-to-buffer@2.0.2: resolution: {integrity: sha512-ND9qDTLc6diwj+Xe5cdAgVTbLVdXbtxTJRXRhli8Mowuaan+0EJOtdqJ0QCHNSSPyoXGx9HX2/VMnKeC34AChA==} - dev: true /data-uri-to-buffer@4.0.1: resolution: {integrity: sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==} @@ -11689,7 +11734,6 @@ packages: /exit-hook@2.2.1: resolution: {integrity: sha512-eNTPlAD67BmP31LDINZ3U7HSF8l57TxOY2PmBJ1shpCvpnxBF93mWCE8YHBnXs8qiUZJc9WDcWIeC3a2HIAMfw==} engines: {node: '>=6'} - dev: true /expand-template@2.0.3: resolution: {integrity: sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg==} @@ -12053,7 +12097,6 @@ packages: dependencies: data-uri-to-buffer: 2.0.2 source-map: 0.6.1 - dev: true /get-stream@6.0.1: resolution: {integrity: sha512-ts6Wi+2j3jQjqi70w5AlN8DFnkSwC+MqmxEzdEALB2qXZYV3X/b1CTfgPLGJNMeAWxdPfU8FO1ms3NUfaHCPYg==} @@ -12106,7 +12149,6 @@ packages: /glob-to-regexp@0.4.1: resolution: {integrity: sha512-lkX1HJXwyMcprw/5YUZc2s7DrpAiHB21/V+E1rHUrVNokkvB6bqMzT0VfV6/86ZNabt1k14YOIaT7nDvOX3Iiw==} - dev: true /glob@7.1.6: resolution: {integrity: sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==} @@ -14235,6 +14277,28 @@ packages: - utf-8-validate dev: true + /miniflare@3.20230918.0: + resolution: {integrity: sha512-Dd29HB7ZlT1CXB2tPH8nW6fBOOXi/m7qFZHjKm2jGS+1OaGfrv0PkT5UspWW5jQi8rWI87xtordAUiIJkwWqRw==} + engines: {node: '>=16.13'} + dependencies: + acorn: 8.10.0 + acorn-walk: 8.2.0 + capnp-ts: 0.7.0 + exit-hook: 2.2.1 + glob-to-regexp: 0.4.1 + source-map-support: 0.5.21 + stoppable: 1.1.0 + undici: 5.23.0 + workerd: 1.20230904.0 + ws: 8.13.0 + youch: 3.2.3 + zod: 3.21.1 + transitivePeerDependencies: + - bufferutil + - supports-color + - utf-8-validate + dev: false + /minimatch@3.1.2: resolution: {integrity: sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==} dependencies: @@ -14387,7 +14451,6 @@ packages: /mustache@4.2.0: resolution: {integrity: sha512-71ippSywq5Yb7/tVYyGbkBggbU8H3u5Rz56fH60jGFgr8uHwxs+aSKeqmluIVzM0m0kB7xQjKS6qPfd0b2ZoqQ==} hasBin: true - dev: true /mz@2.7.0: resolution: {integrity: sha512-z81GNO7nnYMEhrGh9LeymoE4+Yr0Wn5McHIZMK5cfQCl+NDX08sCZgUc9/6MHni9IWuFLm1Z3HTCXu2z9fN62Q==} @@ -15522,7 +15585,6 @@ packages: /printable-characters@1.0.42: resolution: {integrity: sha512-dKp+C4iXWK4vVYZmYSd0KBH5F/h1HoZRsbJ82AVKRO3PEo8L4lBS/vLwhVtpwwuYcoIsVY+1JYKR268yn480uQ==} - dev: true /prismjs@1.29.0: resolution: {integrity: sha512-Kx/1w86q/epKcmte75LNrEoT+lX8pBpavuAbvJWRXar7Hz8jrtF+e3vY751p0R8H9HdArwaCTNDDzHg/ScJK1Q==} @@ -16566,7 +16628,6 @@ packages: dependencies: as-table: 1.0.55 get-source: 2.0.12 - dev: true /statuses@2.0.1: resolution: {integrity: sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ==} @@ -16586,7 +16647,6 @@ packages: /stoppable@1.1.0: resolution: {integrity: sha512-KXDYZ9dszj6bzvnEMRYvxgeTHU74QBFL54XKtP3nyMuJ81CFYtABZ3bAzL2EdFUaEwJOBOgENyFj3R7oTzDyyw==} engines: {node: '>=4', npm: '>=6'} - dev: true /stream-parser@0.3.1: resolution: {integrity: sha512-bJ/HgKq41nlKvlhccD5kaCr/P+Hu0wPNKPJOH7en+YrJu/9EgqUF+88w5Jb6KNcjOFMhfX4B2asfeAtIGuHObQ==} @@ -18436,6 +18496,19 @@ packages: '@cloudflare/workerd-windows-64': 1.20230814.1 dev: true + /workerd@1.20230904.0: + resolution: {integrity: sha512-t9znszH0rQGK4mJGvF9L3nN0qKEaObAGx0JkywFtAwH8OkSn+YfQbHNZE+YsJ4qa1hOz1DCNEk08UDFRBaYq4g==} + engines: {node: '>=16'} + hasBin: true + requiresBuild: true + optionalDependencies: + '@cloudflare/workerd-darwin-64': 1.20230904.0 + '@cloudflare/workerd-darwin-arm64': 1.20230904.0 + '@cloudflare/workerd-linux-64': 1.20230904.0 + '@cloudflare/workerd-linux-arm64': 1.20230904.0 + '@cloudflare/workerd-windows-64': 1.20230904.0 + dev: false + /workerpool@6.2.1: resolution: {integrity: sha512-ILEIE97kDZvF9Wb9f6h5aXK4swSlKGUcOEGiIYb2OOu/IrDU9iwj0fD//SsA6E5ibwJxpEvhullJY4Sl4GcpAw==} dev: true @@ -18506,7 +18579,6 @@ packages: optional: true utf-8-validate: optional: true - dev: true /xml-name-validator@4.0.0: resolution: {integrity: sha512-ICP2e+jsHvAj2E2lIHxa5tjXRlKDJo4IdvPvCXbXQGdzSfmSpNVyIKMvoZHjDY9DP0zV17iI85o90vRFXNccRw==} @@ -18656,7 +18728,6 @@ packages: cookie: 0.5.0 mustache: 4.2.0 stacktracey: 2.1.8 - dev: true /zod@3.21.1: resolution: {integrity: sha512-+dTu2m6gmCbO9Ahm4ZBDapx2O6ZY9QSPXst2WXjcznPMwf2YNpn3RevLx4KkZp1OPW/ouFcoBtBzFz/LeY69oA==}