Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit f85a251

Browse files
fix: make sure logs are strings not buffers
1 parent 89b517d commit f85a251

File tree

11 files changed

+137
-2
lines changed

11 files changed

+137
-2
lines changed

examples/worker/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export default {
22
async fetch() {
3+
throw new Error('xxx');
34
return new Response('Hello World!');
45
},
56
};

packages/vite-plugin-cloudflare/src/miniflare-options.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,11 @@ export function getMiniflareOptions(
159159
return {
160160
log: logger,
161161
handleRuntimeStdio(stdout, stderr) {
162-
stdout.forEach((data) => logger.info(data));
163-
stderr.forEach((error) => logger.error(error));
162+
const decoder = new TextDecoder();
163+
stdout.forEach((data) => logger.info(decoder.decode(data)));
164+
stderr.forEach((error) =>
165+
logger.logWithLevel(LogLevel.ERROR, decoder.decode(error)),
166+
);
164167
},
165168
...getPersistence(normalizedPluginConfig.persistPath),
166169
workers: workers.map((workerOptions) => {
@@ -273,6 +276,9 @@ class ViteMiniflareLogger extends Log {
273276
}
274277

275278
override logWithLevel(level: LogLevel, message: string) {
279+
if (/^Ready on http/.test(message)) {
280+
level = LogLevel.DEBUG;
281+
}
276282
switch (level) {
277283
case LogLevel.ERROR:
278284
return this.logger.error(message);

pnpm-lock.yaml

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import * as path from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
import { cloudflare } from '@flarelabs-net/vite-plugin-cloudflare';
4+
import * as vite from 'vite';
5+
import { beforeEach, describe, expect, test } from 'vitest';
6+
import { getWorker, MockLogger, UNKNOWN_HOST } from '../test-helpers/src/utils';
7+
import type { FetchableDevEnvironment } from '../test-helpers/src/utils';
8+
9+
const root = fileURLToPath(new URL('.', import.meta.url));
10+
let server: vite.ViteDevServer;
11+
let customLogger: MockLogger;
12+
let worker: FetchableDevEnvironment;
13+
14+
describe('service bindings', async () => {
15+
beforeEach(async ({ onTestFinished }) => {
16+
customLogger = new MockLogger();
17+
server = await vite.createServer({
18+
customLogger,
19+
plugins: [
20+
cloudflare({
21+
workers: {
22+
worker: {
23+
main: path.join(root, 'worker/index.ts'),
24+
wranglerConfig: path.join(root, 'worker/wrangler.toml'),
25+
},
26+
},
27+
persistTo: false,
28+
}),
29+
],
30+
});
31+
worker = getWorker(server);
32+
onTestFinished(() => server.close());
33+
});
34+
35+
test('returns a response from the worker', async () => {
36+
const response = await worker.dispatchFetch(new Request(UNKNOWN_HOST));
37+
const result = await response.json();
38+
39+
expect(result).toEqual({ status: 'OK' });
40+
});
41+
42+
test('forwards console logs from the worker', async () => {
43+
await worker.dispatchFetch(new Request(UNKNOWN_HOST));
44+
expect(customLogger.getLogs('info')).toMatchInlineSnapshot(`
45+
"console log
46+
"
47+
`);
48+
expect(customLogger.getLogs('error')).toMatchInlineSnapshot(`
49+
"console warn
50+
console error
51+
"
52+
`);
53+
});
54+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "@tests/integration-basic-functionality",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"check:types": "tsc --build",
7+
"test": "vitest run"
8+
},
9+
"devDependencies": {
10+
"@cloudflare/workers-types": "catalog:default",
11+
"@flarelabs-net/vite-plugin-cloudflare": "workspace:*",
12+
"@vite-plugin-cloudflare/typescript-config": "workspace:*",
13+
"typescript": "catalog:default",
14+
"vite": "catalog:default",
15+
"vitest": "catalog:default",
16+
"wrangler": "catalog:default"
17+
}
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": "@vite-plugin-cloudflare/typescript-config/worker.json",
3+
"include": ["**/*.ts"],
4+
"exclude": ["*.test.ts"]
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "@vite-plugin-cloudflare/typescript-config/base.json",
3+
"include": ["*.test.ts"]
4+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"files": [],
3+
"references": [
4+
{ "path": "./tsconfig.integration.json" },
5+
{ "path": "./tsconfig.fixtures.json" }
6+
]
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default {
2+
async fetch(request) {
3+
console.log('console log');
4+
console.warn('console warn');
5+
console.error('console error');
6+
return Response.json({ status: 'OK' });
7+
},
8+
} satisfies ExportedHandler;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
compatibility_date = "2024-09-09"

0 commit comments

Comments
 (0)