Skip to content

Commit 2fc7ce9

Browse files
authored
Fix WebAssembly error when importing astro:actions in Cloudflare Vitest tests (#17943)
1 parent ba08e35 commit 2fc7ce9

13 files changed

Lines changed: 444 additions & 60 deletions

File tree

‎.changeset/huge-mangos-occur.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes a WebAssembly error when importing `astro:actions` in tests that run under `@cloudflare/vitest-pool-workers`
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import type fsMod from 'node:fs';
2+
// `es-module-lexer` compiles WebAssembly when this module is evaluated. It is
3+
// deliberately kept out of `actions/utils.js`, which the `astro:actions`
4+
// runtime graph reaches: restricted runtimes (Cloudflare Workers) disallow
5+
// wasm code generation. This module is only loaded by dev/build config code.
6+
// https://github.com/withastro/astro/issues/17906
7+
import * as eslexer from 'es-module-lexer';
8+
9+
/**
10+
* Check whether the Actions config file is present.
11+
*/
12+
export async function isActionsFilePresent(fs: typeof fsMod, srcDir: URL) {
13+
await eslexer.init;
14+
15+
const actionsFile = search(fs, srcDir);
16+
if (!actionsFile) return false;
17+
18+
let contents: string;
19+
try {
20+
contents = fs.readFileSync(actionsFile.url, 'utf-8');
21+
} catch {
22+
return false;
23+
}
24+
25+
// Check if `server` export is present.
26+
// If not, the user may have an empty `actions` file,
27+
// or may be using the `actions` file for another purpose
28+
// (possible since actions are non-breaking for v4.X).
29+
const [, exports] = eslexer.parse(contents, actionsFile.url.pathname);
30+
for (const exp of exports) {
31+
if (exp.n === 'server') {
32+
return actionsFile.filename;
33+
}
34+
}
35+
return false;
36+
}
37+
38+
function search(fs: typeof fsMod, srcDir: URL) {
39+
const filenames = [
40+
'actions.mjs',
41+
'actions.js',
42+
'actions.mts',
43+
'actions.ts',
44+
'actions/index.mjs',
45+
'actions/index.js',
46+
'actions/index.mts',
47+
'actions/index.ts',
48+
];
49+
for (const filename of filenames) {
50+
const url = new URL(filename, srcDir);
51+
if (fs.existsSync(url)) {
52+
return { filename, url };
53+
}
54+
}
55+
return undefined;
56+
}
Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type fsMod from 'node:fs';
2-
import * as eslexer from 'es-module-lexer';
31
import type { APIContext } from '../types/public/context.js';
42
import { deserializeActionResult, getActionQueryString } from './runtime/client.js';
53
import { ACTION_API_CONTEXT_SYMBOL } from './runtime/server.js';
@@ -28,54 +26,3 @@ export function createCallAction(context: ActionAPIContext): APIContext['callAct
2826
return action(input) as any;
2927
};
3028
}
31-
32-
let didInitLexer = false;
33-
34-
/**
35-
* Check whether the Actions config file is present.
36-
*/
37-
export async function isActionsFilePresent(fs: typeof fsMod, srcDir: URL) {
38-
if (!didInitLexer) await eslexer.init;
39-
40-
const actionsFile = search(fs, srcDir);
41-
if (!actionsFile) return false;
42-
43-
let contents: string;
44-
try {
45-
contents = fs.readFileSync(actionsFile.url, 'utf-8');
46-
} catch {
47-
return false;
48-
}
49-
50-
// Check if `server` export is present.
51-
// If not, the user may have an empty `actions` file,
52-
// or may be using the `actions` file for another purpose
53-
// (possible since actions are non-breaking for v4.X).
54-
const [, exports] = eslexer.parse(contents, actionsFile.url.pathname);
55-
for (const exp of exports) {
56-
if (exp.n === 'server') {
57-
return actionsFile.filename;
58-
}
59-
}
60-
return false;
61-
}
62-
63-
function search(fs: typeof fsMod, srcDir: URL) {
64-
const filenames = [
65-
'actions.mjs',
66-
'actions.js',
67-
'actions.mts',
68-
'actions.ts',
69-
'actions/index.mjs',
70-
'actions/index.js',
71-
'actions/index.mts',
72-
'actions/index.ts',
73-
];
74-
for (const filename of filenames) {
75-
const url = new URL(filename, srcDir);
76-
if (fs.existsSync(url)) {
77-
return { filename, url };
78-
}
79-
}
80-
return undefined;
81-
}

‎packages/astro/src/actions/vite-plugin-actions.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
RESOLVED_VIRTUAL_MODULE_ID,
2020
VIRTUAL_MODULE_ID,
2121
} from './consts.js';
22-
import { isActionsFilePresent } from './utils.js';
22+
import { isActionsFilePresent } from './actions-file.js';
2323
import { ASTRO_VITE_ENVIRONMENT_NAMES } from '../core/constants.js';
2424

2525
/**

‎packages/astro/src/integrations/hooks.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import colors from 'piccolore';
55
import type { InlineConfig, ViteDevServer } from 'vite';
66
import { mergeConfig as mergeViteConfig } from 'vite';
77
import astroIntegrationActionsRouteHandler from '../actions/integration.js';
8-
import { isActionsFilePresent } from '../actions/utils.js';
8+
import { isActionsFilePresent } from '../actions/actions-file.js';
99
import { CONTENT_LAYER_TYPE } from '../content/consts.js';
1010
import { globalContentLayer } from '../content/instance.js';
1111
import { globalContentConfigObserver } from '../content/utils.js';

‎packages/integrations/cloudflare/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"devalue": "^5.8.1",
6666
"prismjs": "^1.30.0",
6767
"tinyglobby": "^0.2.15",
68+
"vitest": "4.1.10",
6869
"wrangler": "^4.125.0"
6970
},
7071
"publishConfig": {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "@test/astro-cloudflare-vitest-actions",
3+
"version": "0.0.0",
4+
"private": true,
5+
"type": "module",
6+
"dependencies": {
7+
"astro": "workspace:*"
8+
},
9+
"devDependencies": {
10+
"@cloudflare/vitest-pool-workers": "0.22.0",
11+
"@vitest/runner": "4.1.10",
12+
"@vitest/snapshot": "4.1.10",
13+
"vitest": "4.1.10"
14+
}
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { ActionError } from 'astro:actions';
2+
import { expect, it } from 'vitest';
3+
4+
it('imports astro:actions in the Cloudflare vitest pool', () => {
5+
expect(ActionError).toBeTypeOf('function');
6+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
fetch() {
3+
return new Response(null, { status: 404 });
4+
},
5+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference types="vitest/config" />
2+
3+
import { cloudflareTest } from '@cloudflare/vitest-pool-workers';
4+
import { getViteConfig } from 'astro/config';
5+
6+
export default getViteConfig(
7+
{
8+
plugins: [
9+
cloudflareTest(async () => ({
10+
main: './src/testWorker.ts',
11+
wrangler: { configPath: './wrangler.jsonc' },
12+
})),
13+
],
14+
},
15+
{ configFile: false, output: 'server' },
16+
);

0 commit comments

Comments
 (0)