@@ -12,47 +12,32 @@ const props = z.object({
1212
1313const { name } = props .parse (Astro .props );
1414
15- const worker = `
15+ const worker = ` import { Buffer } from 'node:buffer';
1616export interface Env {
1717 AI: Ai;
1818}
19-
2019const URL = "https://pub-dbcf9f0bd3af47ca9d40971179ee62de.r2.dev/02f6edc0-1f7b-4272-bd17-f05335104725/audio.mp3";
21-
2220export default {
2321 async fetch(request, env, ctx): Promise<Response> {
2422 const mp3 = await fetch(URL);
2523 if (!mp3.ok) {
26- throw new Error('Response error from mp3');
27- }
28-
24+ throw new Error(\` Response status: \$ {mp3.status}\` );
25+ }
2926 const mp3Buffer = await mp3.arrayBuffer();
30-
31- // Convert ArrayBuffer to Base64 using a loop
32- const base64 = arrayBufferToBase64(mp3Buffer);
33-
27+ const base64 = Buffer.from(mp3Buffer, 'binary').toString("base64");
3428 try {
35- const res = await env.AI.run("audio-analysis ", {
36- audio: base64
29+ const res = await env.AI.run("${ name } ", {
30+ " audio" : base64
3731 });
3832 return Response.json(res);
39- } catch (e) {
40- console.log("AI Service Error:", e);
4133 }
42-
43- return new Response(JSON.stringify({ message: "Fallback response" }));
34+ catch (e) {
35+ console.log(JSON.stringify(env.AI));
36+ }
37+ return new Response(JSON.stringify({ho:"to"}));
4438 },
45- };
46-
47- // Utility to convert ArrayBuffer to Base64 using a loop
48- function arrayBufferToBase64(buffer: ArrayBuffer): string {
49- const bytes = new Uint8Array(buffer);
50- let binary = '';
51- for (let i = 0; i < bytes.length; i++) {
52- binary += String.fromCharCode(bytes[i]);
53- }
54- return btoa(binary);
55- } ` ;
39+ } satisfies ExportedHandler<Env>
40+ ` ;
5641---
5742
5843<Details header =" Workers - TypeScript" >
0 commit comments