Skip to content

Commit 3cc034b

Browse files
committed
refactor(supaworker): Restructure Supaworker class with improved method organization
Refactored Supaworker implementation by: - Breaking down constructor logic into separate private methods - Extracting function name extraction method - Adding static method for ensuring single class initialization - Improving error handling for connection string retrieval - Simplifying worker initialization and setup
1 parent 07e15a2 commit 3cc034b

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

pkgs/supaworker/src/index.ts

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@ 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-
125
export type SupaworkerConfig = Omit<WorkerConfig, 'connectionString'>;
136

147
export class Supaworker {
@@ -18,29 +11,52 @@ export class Supaworker {
1811
handler: (message: MessagePayload) => Promise<unknown> | unknown,
1912
config: SupaworkerConfig = {}
2013
) {
14+
this.ensureFirstCall();
15+
const connectionString = this.getConnectionString();
16+
17+
const worker = this.initializeWorker(handler, {
18+
...config,
19+
connectionString,
20+
});
21+
22+
this.setupShutdownHandler(worker);
23+
this.setupRequestHandler(worker);
24+
}
25+
26+
private static ensureFirstCall() {
2127
if (this.wasCalled) {
2228
throw new Error('Supaworker can only be called once');
2329
}
2430
this.wasCalled = true;
31+
}
2532

33+
private static getConnectionString(): string {
2634
// @ts-ignore - TODO: fix the types
27-
const DB_POOL_URL = Deno.env.get('DB_POOL_URL');
28-
29-
if (!DB_POOL_URL) {
35+
const connectionString = Deno.env.get('DB_POOL_URL');
36+
if (!connectionString) {
3037
throw new Error('DB_POOL_URL is not set');
3138
}
39+
return connectionString;
40+
}
3241

33-
const worker = new Worker<MessagePayload>(
42+
private static initializeWorker<MessagePayload extends Json>(
43+
handler: (message: MessagePayload) => Promise<unknown> | unknown,
44+
config: WorkerConfig
45+
): Worker<MessagePayload> {
46+
return new Worker<MessagePayload>(
3447
async (message) => {
3548
await handler(message);
3649
},
3750
{
38-
connectionString: DB_POOL_URL,
3951
queueName: config.queueName || 'tasks',
4052
...config,
4153
}
4254
);
55+
}
4356

57+
private static setupShutdownHandler<MessagePayload extends Json>(
58+
worker: Worker<MessagePayload>
59+
) {
4460
globalThis.onbeforeunload = () => {
4561
worker.stop();
4662

@@ -52,9 +68,13 @@ export class Supaworker {
5268
// use waitUntil to prevent the function from exiting
5369
// @ts-ignore: TODO: fix the types
5470
EdgeRuntime.waitUntil(new Promise(() => {}));
71+
}
5572

56-
Deno.serve((req) => {
57-
const edgeFunctionName = extractFunctionName(req);
73+
private static setupRequestHandler<MessagePayload extends Json>(
74+
worker: Worker<MessagePayload>
75+
) {
76+
Deno.serve({}, (req) => {
77+
const edgeFunctionName = this.extractFunctionName(req);
5878

5979
worker.startOnlyOnce({
6080
edgeFunctionName,
@@ -67,4 +87,9 @@ export class Supaworker {
6787
});
6888
});
6989
}
90+
91+
private static extractFunctionName(req: Request): string {
92+
return new URL(req.url).pathname.replace(/^\/+|\/+$/g, '');
93+
}
7094
}
95+

0 commit comments

Comments
 (0)