Skip to content

Commit 07e15a2

Browse files
committed
refactor(supaworker): Improve edge function name extraction and type handling
Extracted function name extraction logic into a separate function Updated type signature for handler to use unknown instead of any Simplified Deno.serve request handling by using extracted function
1 parent 5a50e74 commit 07e15a2

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

pkgs/supaworker/src/index.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@ import { Worker, WorkerConfig } from './Worker.ts';
22
import spawnNewEdgeFunction from './spawnNewEdgeFunction.ts';
33
import { Json } from './types.ts';
44

5+
/**
6+
* Extracts the edge function name from the request URL
7+
*/
8+
function extractFunctionName(req: Request) {
9+
return new URL(req.url).pathname.replace(/^\/+|\/+$/g, '');
10+
}
11+
512
export type SupaworkerConfig = Omit<WorkerConfig, 'connectionString'>;
613

714
export class Supaworker {
815
private static wasCalled = false;
916

1017
static start<MessagePayload extends Json = Json>(
11-
handler: (message: MessagePayload) => Promise<any> | any,
18+
handler: (message: MessagePayload) => Promise<unknown> | unknown,
1219
config: SupaworkerConfig = {}
1320
) {
1421
if (this.wasCalled) {
@@ -46,11 +53,8 @@ export class Supaworker {
4653
// @ts-ignore: TODO: fix the types
4754
EdgeRuntime.waitUntil(new Promise(() => {}));
4855

49-
Deno.serve((_req) => {
50-
const edgeFunctionName = new URL(_req.url).pathname.replace(
51-
/^\/+|\/+$/g,
52-
''
53-
);
56+
Deno.serve((req) => {
57+
const edgeFunctionName = extractFunctionName(req);
5458

5559
worker.startOnlyOnce({
5660
edgeFunctionName,

0 commit comments

Comments
 (0)