Skip to content

Commit 8a164fc

Browse files
committed
remove symlink to nitro-v2
1 parent 033061f commit 8a164fc

File tree

6 files changed

+180
-2
lines changed

6 files changed

+180
-2
lines changed

workbench/nitro-v2/server/api/trigger.post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineEventHandler, getRequestURL, readRawBody } from 'h3';
22
import { start } from 'workflow/api';
33
import { hydrateWorkflowArguments } from 'workflow/internal/serialization';
4-
import { allWorkflows } from '../_workflows.js';
4+
import { allWorkflows } from '../../_workflows.js';
55

66
export default defineEventHandler(async (event) => {
77
const url = getRequestURL(event);

workbench/nuxt/server/api

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// THIS FILE IS JUST FOR TESTING HMR AS AN ENTRY NEEDS
2+
// TO IMPORT THE WORKFLOWS TO DISCOVER THEM AND WATCH
3+
4+
import * as workflows from '../../workflows/3_streams.js';
5+
6+
export default async ({ req }: { req: Request }) => {
7+
console.log(workflows);
8+
return Response.json('hello world');
9+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { defineEventHandler, readRawBody } from 'h3';
2+
import { getHookByToken, resumeHook } from 'workflow/api';
3+
4+
export default defineEventHandler(async (event) => {
5+
const body = await readRawBody(event);
6+
const { token, data } = JSON.parse(body as string);
7+
8+
let hook: Awaited<ReturnType<typeof getHookByToken>>;
9+
try {
10+
hook = await getHookByToken(token);
11+
console.log('hook', hook);
12+
} catch (error) {
13+
console.log('error during getHookByToken', error);
14+
// TODO: `WorkflowAPIError` is not exported, so for now
15+
// we'll return 404 assuming it's the "invalid" token test case
16+
return Response.json(null, { status: 404 });
17+
}
18+
19+
await resumeHook(hook.token, {
20+
...data,
21+
// @ts-expect-error metadata is not typed
22+
customData: hook.metadata?.customData,
23+
});
24+
25+
return Response.json(hook);
26+
});
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { defineEventHandler, getRequestURL } from 'h3';
2+
import { getRun } from 'workflow/api';
3+
4+
export default defineEventHandler(async (event) => {
5+
const url = getRequestURL(event);
6+
const runId = url.searchParams.get('runId');
7+
if (!runId) {
8+
return new Response('No runId provided', { status: 400 });
9+
}
10+
11+
const outputStreamParam = url.searchParams.get('output-stream');
12+
if (outputStreamParam) {
13+
const namespace = outputStreamParam === '1' ? undefined : outputStreamParam;
14+
const run = getRun(runId);
15+
const stream = run.getReadable({
16+
namespace,
17+
});
18+
// Add JSON framing to the stream, wrapping binary data in base64
19+
const streamWithFraming = new TransformStream({
20+
transform(chunk, controller) {
21+
const data =
22+
chunk instanceof Uint8Array
23+
? { data: Buffer.from(chunk).toString('base64') }
24+
: chunk;
25+
controller.enqueue(`${JSON.stringify(data)}\n`);
26+
},
27+
});
28+
return new Response(stream.pipeThrough(streamWithFraming), {
29+
headers: {
30+
'Content-Type': 'application/octet-stream',
31+
},
32+
});
33+
}
34+
35+
try {
36+
const run = getRun(runId);
37+
const returnValue = await run.returnValue;
38+
console.log('Return value:', returnValue);
39+
return returnValue instanceof ReadableStream
40+
? new Response(returnValue, {
41+
headers: {
42+
'Content-Type': 'application/octet-stream',
43+
},
44+
})
45+
: Response.json(returnValue);
46+
} catch (error) {
47+
if (error instanceof Error) {
48+
if (error.name === 'WorkflowRunNotCompletedError') {
49+
return Response.json(
50+
{
51+
...error,
52+
name: error.name,
53+
message: error.message,
54+
},
55+
{ status: 202 }
56+
);
57+
}
58+
59+
if (error.name === 'WorkflowRunFailedError') {
60+
return Response.json(
61+
{
62+
...error,
63+
name: error.name,
64+
message: error.message,
65+
},
66+
{ status: 400 }
67+
);
68+
}
69+
}
70+
71+
console.error(
72+
'Unexpected error while getting workflow return value:',
73+
error
74+
);
75+
return Response.json(
76+
{
77+
error: 'Internal server error',
78+
},
79+
{ status: 500 }
80+
);
81+
}
82+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { defineEventHandler, getRequestURL, readRawBody } from 'h3';
2+
import { start } from 'workflow/api';
3+
import { hydrateWorkflowArguments } from 'workflow/internal/serialization';
4+
import { allWorkflows } from '../../_workflows.js';
5+
6+
export default defineEventHandler(async (event) => {
7+
const url = getRequestURL(event);
8+
9+
const workflowFile =
10+
url.searchParams.get('workflowFile') || 'workflows/99_e2e.ts';
11+
if (!workflowFile) {
12+
return new Response('No workflowFile query parameter provided', {
13+
status: 400,
14+
});
15+
}
16+
const workflows = allWorkflows[workflowFile as keyof typeof allWorkflows];
17+
if (!workflows) {
18+
return new Response(`Workflow file "${workflowFile}" not found`, {
19+
status: 400,
20+
});
21+
}
22+
23+
const workflowFn = url.searchParams.get('workflowFn') || 'simple';
24+
if (!workflowFn) {
25+
return new Response('No workflow query parameter provided', {
26+
status: 400,
27+
});
28+
}
29+
const workflow = workflows[workflowFn as keyof typeof workflows];
30+
if (!workflow) {
31+
return new Response(`Workflow "${workflowFn}" not found`, { status: 400 });
32+
}
33+
34+
let args: any[] = [];
35+
36+
// Args from query string
37+
const argsParam = url.searchParams.get('args');
38+
if (argsParam) {
39+
args = argsParam.split(',').map((arg) => {
40+
const num = parseFloat(arg);
41+
return Number.isNaN(num) ? arg.trim() : num;
42+
});
43+
} else {
44+
// Args from body
45+
const body = await readRawBody(event);
46+
if (body) {
47+
args = hydrateWorkflowArguments(JSON.parse(body), globalThis);
48+
} else {
49+
args = [42];
50+
}
51+
}
52+
console.log(`Starting "${workflowFn}" workflow with args: ${args}`);
53+
54+
try {
55+
const run = await start(workflow as any, args as any);
56+
console.log('Run:', run);
57+
return Response.json(run);
58+
} catch (err) {
59+
console.error(`Failed to start!!`, err);
60+
throw err;
61+
}
62+
});

0 commit comments

Comments
 (0)