@queuebase/node 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-W3S5SQJS.js +45 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +15 -1
- package/dist/client.js.map +1 -1
- package/dist/handler.cjs +69 -0
- package/dist/handler.d.cts +8 -0
- package/dist/handler.d.ts +6 -4
- package/dist/handler.js +6 -41
- package/dist/index.cjs +158 -0
- package/dist/index.d.cts +16 -0
- package/dist/index.d.ts +15 -6
- package/dist/index.js +89 -11
- package/package.json +8 -5
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// src/handler.ts
|
|
2
|
+
import { WEBHOOK_HEADERS, processJobCallback } from "@queuebase/core";
|
|
3
|
+
function readBody(req) {
|
|
4
|
+
const existing = req.body;
|
|
5
|
+
if (existing !== void 0) {
|
|
6
|
+
return Promise.resolve(typeof existing === "string" ? existing : existing.toString("utf-8"));
|
|
7
|
+
}
|
|
8
|
+
if (req.readableEnded) {
|
|
9
|
+
return Promise.reject(
|
|
10
|
+
new Error("Request body already consumed. Use express.raw() or avoid body-parsing middleware on this route.")
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
const chunks = [];
|
|
15
|
+
req.on("data", (chunk) => chunks.push(chunk));
|
|
16
|
+
req.on("end", () => resolve(Buffer.concat(chunks).toString("utf-8")));
|
|
17
|
+
req.on("error", reject);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
function createNodeHandler(router, options) {
|
|
21
|
+
return async (req, res) => {
|
|
22
|
+
if (req.method !== "POST") {
|
|
23
|
+
res.writeHead(405, { "Content-Type": "text/plain" });
|
|
24
|
+
res.end("Method not allowed");
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
let body;
|
|
28
|
+
try {
|
|
29
|
+
body = await readBody(req);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
const message = error instanceof Error ? error.message : "Failed to read request body";
|
|
32
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
33
|
+
res.end(JSON.stringify({ success: false, error: message }));
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const signatureHeader = req.headers[WEBHOOK_HEADERS.SIGNATURE.toLowerCase()] ?? null;
|
|
37
|
+
const result = await processJobCallback(router, { body, signatureHeader }, options);
|
|
38
|
+
res.writeHead(result.status, { "Content-Type": "application/json" });
|
|
39
|
+
res.end(JSON.stringify(result.body));
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export {
|
|
44
|
+
createNodeHandler
|
|
45
|
+
};
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,iBAAiB,EAGlB,MAAM,iBAAiB,CAAC;AAEzB,UAAU,eAAe;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,wBAAgB,YAAY,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,EACrE,MAAM,EAAE,CAAC,EACT,MAAM,EAAE,eAAe,GACtB,iBAAiB,CAAC,CAAC,CAAC,CAwDtB"}
|
package/dist/client.js
CHANGED
|
@@ -27,7 +27,21 @@ export function createClient(router, config) {
|
|
|
27
27
|
throw new Error(`Failed to enqueue job: ${error}`);
|
|
28
28
|
}
|
|
29
29
|
const result = (await response.json());
|
|
30
|
-
return {
|
|
30
|
+
return {
|
|
31
|
+
jobId: result.jobId,
|
|
32
|
+
getStatus: async () => {
|
|
33
|
+
const res = await fetch(`${config.apiUrl}/jobs/${result.jobId}`, {
|
|
34
|
+
headers: {
|
|
35
|
+
...(config.apiKey ? { Authorization: `Bearer ${config.apiKey}` } : {}),
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
if (!res.ok) {
|
|
39
|
+
const error = await res.text().catch(() => 'Unknown error');
|
|
40
|
+
throw new Error(`Failed to get job status: ${error}`);
|
|
41
|
+
}
|
|
42
|
+
return (await res.json());
|
|
43
|
+
},
|
|
44
|
+
};
|
|
31
45
|
},
|
|
32
46
|
_def: jobDef,
|
|
33
47
|
};
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAaA,MAAM,UAAU,YAAY,CAC1B,MAAS,EACT,MAAuB;IAEvB,MAAM,MAAM,GAAG,EAA0B,CAAC;IAE1C,KAAK,MAAM,CAAC,IAAI,EAAE,UAAU,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QACxD,MAAM,MAAM,GAAG,UAAU,CAAC;QAEzB,MAAkC,CAAC,IAAI,CAAC,GAAG;YAC1C,OAAO,EAAE,KAAK,EAAE,KAAc,EAAE,OAAwB,EAAE,EAAE;gBAC1D,MAAM,cAAc,GAAG,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;gBAEjD,MAAM,aAAa,GAAG;oBACpB,GAAG,MAAM,CAAC,QAAQ;oBAClB,GAAG,OAAO;iBACX,CAAC;gBAEF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,MAAM,CAAC,MAAM,eAAe,EAAE;oBAC5D,MAAM,EAAE,MAAM;oBACd,OAAO,EAAE;wBACP,cAAc,EAAE,kBAAkB;wBAClC,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,UAAU,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;qBACvE;oBACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC;wBACnB,IAAI;wBACJ,OAAO,EAAE,cAAc;wBACvB,WAAW,EAAE,MAAM,CAAC,WAAW;wBAC/B,OAAO,EAAE,aAAa;qBACvB,CAAC;iBACH,CAAC,CAAC;gBAEH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;oBACjB,MAAM,KAAK,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,eAAe,CAAC,CAAC;oBACjE,MAAM,IAAI,KAAK,CAAC,0BAA0B,KAAK,EAAE,CAAC,CAAC;gBACrD,CAAC;gBAED,MAAM,MAAM,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAsB,CAAC;gBAC5D,OAAO;oBACL,KAAK,EAAE,MAAM,CAAC,KAAK;oBACnB,SAAS,EAAE,KAAK,IAAI,EAAE;wBACpB,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,GAAG,MAAM,CAAC,MAAM,SAAS,MAAM,CAAC,KAAK,EAAE,EAAE;4BAC/D,OAAO,EAAE;gCACP,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,UAAU,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;6BACvE;yBACF,CAAC,CAAC;wBACH,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;4BACZ,MAAM,KAAK,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,eAAe,CAAC,CAAC;4BAC5D,MAAM,IAAI,KAAK,CAAC,6BAA6B,KAAK,EAAE,CAAC,CAAC;wBACxD,CAAC;wBACD,OAAO,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAAsB,CAAC;oBACjD,CAAC;iBACF,CAAC;YACJ,CAAC;YACD,IAAI,EAAE,MAAM;SACb,CAAC;IACJ,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/dist/handler.cjs
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/handler.ts
|
|
21
|
+
var handler_exports = {};
|
|
22
|
+
__export(handler_exports, {
|
|
23
|
+
createNodeHandler: () => createNodeHandler
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(handler_exports);
|
|
26
|
+
var import_core = require("@queuebase/core");
|
|
27
|
+
function readBody(req) {
|
|
28
|
+
const existing = req.body;
|
|
29
|
+
if (existing !== void 0) {
|
|
30
|
+
return Promise.resolve(typeof existing === "string" ? existing : existing.toString("utf-8"));
|
|
31
|
+
}
|
|
32
|
+
if (req.readableEnded) {
|
|
33
|
+
return Promise.reject(
|
|
34
|
+
new Error("Request body already consumed. Use express.raw() or avoid body-parsing middleware on this route.")
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
const chunks = [];
|
|
39
|
+
req.on("data", (chunk) => chunks.push(chunk));
|
|
40
|
+
req.on("end", () => resolve(Buffer.concat(chunks).toString("utf-8")));
|
|
41
|
+
req.on("error", reject);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function createNodeHandler(router, options) {
|
|
45
|
+
return async (req, res) => {
|
|
46
|
+
if (req.method !== "POST") {
|
|
47
|
+
res.writeHead(405, { "Content-Type": "text/plain" });
|
|
48
|
+
res.end("Method not allowed");
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
let body;
|
|
52
|
+
try {
|
|
53
|
+
body = await readBody(req);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
const message = error instanceof Error ? error.message : "Failed to read request body";
|
|
56
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
57
|
+
res.end(JSON.stringify({ success: false, error: message }));
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const signatureHeader = req.headers[import_core.WEBHOOK_HEADERS.SIGNATURE.toLowerCase()] ?? null;
|
|
61
|
+
const result = await (0, import_core.processJobCallback)(router, { body, signatureHeader }, options);
|
|
62
|
+
res.writeHead(result.status, { "Content-Type": "application/json" });
|
|
63
|
+
res.end(JSON.stringify(result.body));
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
67
|
+
0 && (module.exports = {
|
|
68
|
+
createNodeHandler
|
|
69
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { AnyJobDefinition } from '@queuebase/core';
|
|
3
|
+
|
|
4
|
+
declare function createNodeHandler(router: Record<string, AnyJobDefinition>, options?: {
|
|
5
|
+
webhookSecret?: string;
|
|
6
|
+
}): (req: IncomingMessage, res: ServerResponse) => Promise<void>;
|
|
7
|
+
|
|
8
|
+
export { createNodeHandler };
|
package/dist/handler.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { AnyJobDefinition } from '@queuebase/core';
|
|
3
|
+
|
|
4
|
+
declare function createNodeHandler(router: Record<string, AnyJobDefinition>, options?: {
|
|
4
5
|
webhookSecret?: string;
|
|
5
6
|
}): (req: IncomingMessage, res: ServerResponse) => Promise<void>;
|
|
6
|
-
|
|
7
|
+
|
|
8
|
+
export { createNodeHandler };
|
package/dist/handler.js
CHANGED
|
@@ -1,41 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
if (req.readableEnded) {
|
|
9
|
-
return Promise.reject(new Error('Request body already consumed. Use express.raw() or avoid body-parsing middleware on this route.'));
|
|
10
|
-
}
|
|
11
|
-
return new Promise((resolve, reject) => {
|
|
12
|
-
const chunks = [];
|
|
13
|
-
req.on('data', (chunk) => chunks.push(chunk));
|
|
14
|
-
req.on('end', () => resolve(Buffer.concat(chunks).toString('utf-8')));
|
|
15
|
-
req.on('error', reject);
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
export function createNodeHandler(router, options) {
|
|
19
|
-
return async (req, res) => {
|
|
20
|
-
if (req.method !== 'POST') {
|
|
21
|
-
res.writeHead(405, { 'Content-Type': 'text/plain' });
|
|
22
|
-
res.end('Method not allowed');
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
let body;
|
|
26
|
-
try {
|
|
27
|
-
body = await readBody(req);
|
|
28
|
-
}
|
|
29
|
-
catch (error) {
|
|
30
|
-
const message = error instanceof Error ? error.message : 'Failed to read request body';
|
|
31
|
-
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
32
|
-
res.end(JSON.stringify({ success: false, error: message }));
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
const signatureHeader = req.headers[WEBHOOK_HEADERS.SIGNATURE.toLowerCase()] ?? null;
|
|
36
|
-
const result = await processJobCallback(router, { body, signatureHeader }, options);
|
|
37
|
-
res.writeHead(result.status, { 'Content-Type': 'application/json' });
|
|
38
|
-
res.end(JSON.stringify(result.body));
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
//# sourceMappingURL=handler.js.map
|
|
1
|
+
import {
|
|
2
|
+
createNodeHandler
|
|
3
|
+
} from "./chunk-W3S5SQJS.js";
|
|
4
|
+
export {
|
|
5
|
+
createNodeHandler
|
|
6
|
+
};
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
createClient: () => createClient,
|
|
24
|
+
createJobRouter: () => import_core2.createJobRouter,
|
|
25
|
+
createNodeHandler: () => createNodeHandler,
|
|
26
|
+
defineConfig: () => defineConfig,
|
|
27
|
+
job: () => import_core2.job,
|
|
28
|
+
processJobCallback: () => import_core3.processJobCallback,
|
|
29
|
+
resolveConfig: () => resolveConfig
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(src_exports);
|
|
32
|
+
var import_core2 = require("@queuebase/core");
|
|
33
|
+
var import_core3 = require("@queuebase/core");
|
|
34
|
+
|
|
35
|
+
// src/config.ts
|
|
36
|
+
var DEFAULT_DEV_API_URL = "http://localhost:3847";
|
|
37
|
+
var DEFAULT_PROD_API_URL = "https://api.queuebase.io";
|
|
38
|
+
function defineConfig(config) {
|
|
39
|
+
return {
|
|
40
|
+
jobsDir: config.jobsDir ?? "./src/jobs",
|
|
41
|
+
callbackUrl: config.callbackUrl ?? process.env.QUEUEBASE_CALLBACK_URL,
|
|
42
|
+
apiKey: config.apiKey ?? process.env.QUEUEBASE_API_KEY,
|
|
43
|
+
apiUrl: config.apiUrl ?? process.env.QUEUEBASE_API_URL ?? (process.env.NODE_ENV === "production" ? DEFAULT_PROD_API_URL : DEFAULT_DEV_API_URL)
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function resolveConfig(config) {
|
|
47
|
+
const isDev = process.env.NODE_ENV !== "production";
|
|
48
|
+
return {
|
|
49
|
+
jobsDir: config.jobsDir,
|
|
50
|
+
callbackUrl: process.env.QUEUEBASE_CALLBACK_URL ?? config.callbackUrl,
|
|
51
|
+
apiKey: process.env.QUEUEBASE_API_KEY ?? config.apiKey,
|
|
52
|
+
apiUrl: process.env.QUEUEBASE_API_URL ?? config.apiUrl ?? (isDev ? DEFAULT_DEV_API_URL : DEFAULT_PROD_API_URL)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/client.ts
|
|
57
|
+
function createClient(router, config) {
|
|
58
|
+
const client = {};
|
|
59
|
+
for (const [name, definition] of Object.entries(router)) {
|
|
60
|
+
const jobDef = definition;
|
|
61
|
+
client[name] = {
|
|
62
|
+
enqueue: async (input, options) => {
|
|
63
|
+
const validatedInput = jobDef.input.parse(input);
|
|
64
|
+
const mergedOptions = {
|
|
65
|
+
...jobDef.defaults,
|
|
66
|
+
...options
|
|
67
|
+
};
|
|
68
|
+
const response = await fetch(`${config.apiUrl}/jobs/enqueue`, {
|
|
69
|
+
method: "POST",
|
|
70
|
+
headers: {
|
|
71
|
+
"Content-Type": "application/json",
|
|
72
|
+
...config.apiKey ? { Authorization: `Bearer ${config.apiKey}` } : {}
|
|
73
|
+
},
|
|
74
|
+
body: JSON.stringify({
|
|
75
|
+
name,
|
|
76
|
+
payload: validatedInput,
|
|
77
|
+
callbackUrl: config.callbackUrl,
|
|
78
|
+
options: mergedOptions
|
|
79
|
+
})
|
|
80
|
+
});
|
|
81
|
+
if (!response.ok) {
|
|
82
|
+
const error = await response.text().catch(() => "Unknown error");
|
|
83
|
+
throw new Error(`Failed to enqueue job: ${error}`);
|
|
84
|
+
}
|
|
85
|
+
const result = await response.json();
|
|
86
|
+
return {
|
|
87
|
+
jobId: result.jobId,
|
|
88
|
+
getStatus: async () => {
|
|
89
|
+
const res = await fetch(`${config.apiUrl}/jobs/${result.jobId}`, {
|
|
90
|
+
headers: {
|
|
91
|
+
...config.apiKey ? { Authorization: `Bearer ${config.apiKey}` } : {}
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
if (!res.ok) {
|
|
95
|
+
const error = await res.text().catch(() => "Unknown error");
|
|
96
|
+
throw new Error(`Failed to get job status: ${error}`);
|
|
97
|
+
}
|
|
98
|
+
return await res.json();
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
},
|
|
102
|
+
_def: jobDef
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
return client;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/handler.ts
|
|
109
|
+
var import_core = require("@queuebase/core");
|
|
110
|
+
function readBody(req) {
|
|
111
|
+
const existing = req.body;
|
|
112
|
+
if (existing !== void 0) {
|
|
113
|
+
return Promise.resolve(typeof existing === "string" ? existing : existing.toString("utf-8"));
|
|
114
|
+
}
|
|
115
|
+
if (req.readableEnded) {
|
|
116
|
+
return Promise.reject(
|
|
117
|
+
new Error("Request body already consumed. Use express.raw() or avoid body-parsing middleware on this route.")
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
return new Promise((resolve, reject) => {
|
|
121
|
+
const chunks = [];
|
|
122
|
+
req.on("data", (chunk) => chunks.push(chunk));
|
|
123
|
+
req.on("end", () => resolve(Buffer.concat(chunks).toString("utf-8")));
|
|
124
|
+
req.on("error", reject);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
function createNodeHandler(router, options) {
|
|
128
|
+
return async (req, res) => {
|
|
129
|
+
if (req.method !== "POST") {
|
|
130
|
+
res.writeHead(405, { "Content-Type": "text/plain" });
|
|
131
|
+
res.end("Method not allowed");
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
let body;
|
|
135
|
+
try {
|
|
136
|
+
body = await readBody(req);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
const message = error instanceof Error ? error.message : "Failed to read request body";
|
|
139
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
140
|
+
res.end(JSON.stringify({ success: false, error: message }));
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const signatureHeader = req.headers[import_core.WEBHOOK_HEADERS.SIGNATURE.toLowerCase()] ?? null;
|
|
144
|
+
const result = await (0, import_core.processJobCallback)(router, { body, signatureHeader }, options);
|
|
145
|
+
res.writeHead(result.status, { "Content-Type": "application/json" });
|
|
146
|
+
res.end(JSON.stringify(result.body));
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
150
|
+
0 && (module.exports = {
|
|
151
|
+
createClient,
|
|
152
|
+
createJobRouter,
|
|
153
|
+
createNodeHandler,
|
|
154
|
+
defineConfig,
|
|
155
|
+
job,
|
|
156
|
+
processJobCallback,
|
|
157
|
+
resolveConfig
|
|
158
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { QueuebaseConfig, AnyJobDefinition, CallableJobRouter } from '@queuebase/core';
|
|
2
|
+
export { AnyJobDefinition, EnqueueOptions, HandlerRequest, HandlerResponse, InferJobInput, InferJobOutput, JobContext, JobDefinition, JobRouter, createJobRouter, job, processJobCallback } from '@queuebase/core';
|
|
3
|
+
export { createNodeHandler } from './handler.cjs';
|
|
4
|
+
import 'node:http';
|
|
5
|
+
|
|
6
|
+
declare function defineConfig(config: Partial<QueuebaseConfig>): QueuebaseConfig;
|
|
7
|
+
declare function resolveConfig(config: QueuebaseConfig): QueuebaseConfig;
|
|
8
|
+
|
|
9
|
+
interface TransportConfig {
|
|
10
|
+
apiUrl: string;
|
|
11
|
+
apiKey?: string;
|
|
12
|
+
callbackUrl: string;
|
|
13
|
+
}
|
|
14
|
+
declare function createClient<T extends Record<string, AnyJobDefinition>>(router: T, config: TransportConfig): CallableJobRouter<T>;
|
|
15
|
+
|
|
16
|
+
export { createClient, defineConfig, resolveConfig };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
export { processJobCallback } from '@queuebase/core';
|
|
4
|
-
export { defineConfig, resolveConfig } from './config.js';
|
|
5
|
-
export { createClient } from './client.js';
|
|
1
|
+
import { QueuebaseConfig, AnyJobDefinition, CallableJobRouter } from '@queuebase/core';
|
|
2
|
+
export { AnyJobDefinition, EnqueueOptions, HandlerRequest, HandlerResponse, InferJobInput, InferJobOutput, JobContext, JobDefinition, JobRouter, createJobRouter, job, processJobCallback } from '@queuebase/core';
|
|
6
3
|
export { createNodeHandler } from './handler.js';
|
|
7
|
-
|
|
4
|
+
import 'node:http';
|
|
5
|
+
|
|
6
|
+
declare function defineConfig(config: Partial<QueuebaseConfig>): QueuebaseConfig;
|
|
7
|
+
declare function resolveConfig(config: QueuebaseConfig): QueuebaseConfig;
|
|
8
|
+
|
|
9
|
+
interface TransportConfig {
|
|
10
|
+
apiUrl: string;
|
|
11
|
+
apiKey?: string;
|
|
12
|
+
callbackUrl: string;
|
|
13
|
+
}
|
|
14
|
+
declare function createClient<T extends Record<string, AnyJobDefinition>>(router: T, config: TransportConfig): CallableJobRouter<T>;
|
|
15
|
+
|
|
16
|
+
export { createClient, defineConfig, resolveConfig };
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,89 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
//
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
//
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import {
|
|
2
|
+
createNodeHandler
|
|
3
|
+
} from "./chunk-W3S5SQJS.js";
|
|
4
|
+
|
|
5
|
+
// src/index.ts
|
|
6
|
+
import { job, createJobRouter } from "@queuebase/core";
|
|
7
|
+
import { processJobCallback } from "@queuebase/core";
|
|
8
|
+
|
|
9
|
+
// src/config.ts
|
|
10
|
+
var DEFAULT_DEV_API_URL = "http://localhost:3847";
|
|
11
|
+
var DEFAULT_PROD_API_URL = "https://api.queuebase.io";
|
|
12
|
+
function defineConfig(config) {
|
|
13
|
+
return {
|
|
14
|
+
jobsDir: config.jobsDir ?? "./src/jobs",
|
|
15
|
+
callbackUrl: config.callbackUrl ?? process.env.QUEUEBASE_CALLBACK_URL,
|
|
16
|
+
apiKey: config.apiKey ?? process.env.QUEUEBASE_API_KEY,
|
|
17
|
+
apiUrl: config.apiUrl ?? process.env.QUEUEBASE_API_URL ?? (process.env.NODE_ENV === "production" ? DEFAULT_PROD_API_URL : DEFAULT_DEV_API_URL)
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function resolveConfig(config) {
|
|
21
|
+
const isDev = process.env.NODE_ENV !== "production";
|
|
22
|
+
return {
|
|
23
|
+
jobsDir: config.jobsDir,
|
|
24
|
+
callbackUrl: process.env.QUEUEBASE_CALLBACK_URL ?? config.callbackUrl,
|
|
25
|
+
apiKey: process.env.QUEUEBASE_API_KEY ?? config.apiKey,
|
|
26
|
+
apiUrl: process.env.QUEUEBASE_API_URL ?? config.apiUrl ?? (isDev ? DEFAULT_DEV_API_URL : DEFAULT_PROD_API_URL)
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// src/client.ts
|
|
31
|
+
function createClient(router, config) {
|
|
32
|
+
const client = {};
|
|
33
|
+
for (const [name, definition] of Object.entries(router)) {
|
|
34
|
+
const jobDef = definition;
|
|
35
|
+
client[name] = {
|
|
36
|
+
enqueue: async (input, options) => {
|
|
37
|
+
const validatedInput = jobDef.input.parse(input);
|
|
38
|
+
const mergedOptions = {
|
|
39
|
+
...jobDef.defaults,
|
|
40
|
+
...options
|
|
41
|
+
};
|
|
42
|
+
const response = await fetch(`${config.apiUrl}/jobs/enqueue`, {
|
|
43
|
+
method: "POST",
|
|
44
|
+
headers: {
|
|
45
|
+
"Content-Type": "application/json",
|
|
46
|
+
...config.apiKey ? { Authorization: `Bearer ${config.apiKey}` } : {}
|
|
47
|
+
},
|
|
48
|
+
body: JSON.stringify({
|
|
49
|
+
name,
|
|
50
|
+
payload: validatedInput,
|
|
51
|
+
callbackUrl: config.callbackUrl,
|
|
52
|
+
options: mergedOptions
|
|
53
|
+
})
|
|
54
|
+
});
|
|
55
|
+
if (!response.ok) {
|
|
56
|
+
const error = await response.text().catch(() => "Unknown error");
|
|
57
|
+
throw new Error(`Failed to enqueue job: ${error}`);
|
|
58
|
+
}
|
|
59
|
+
const result = await response.json();
|
|
60
|
+
return {
|
|
61
|
+
jobId: result.jobId,
|
|
62
|
+
getStatus: async () => {
|
|
63
|
+
const res = await fetch(`${config.apiUrl}/jobs/${result.jobId}`, {
|
|
64
|
+
headers: {
|
|
65
|
+
...config.apiKey ? { Authorization: `Bearer ${config.apiKey}` } : {}
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
if (!res.ok) {
|
|
69
|
+
const error = await res.text().catch(() => "Unknown error");
|
|
70
|
+
throw new Error(`Failed to get job status: ${error}`);
|
|
71
|
+
}
|
|
72
|
+
return await res.json();
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
},
|
|
76
|
+
_def: jobDef
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return client;
|
|
80
|
+
}
|
|
81
|
+
export {
|
|
82
|
+
createClient,
|
|
83
|
+
createJobRouter,
|
|
84
|
+
createNodeHandler,
|
|
85
|
+
defineConfig,
|
|
86
|
+
job,
|
|
87
|
+
processJobCallback,
|
|
88
|
+
resolveConfig
|
|
89
|
+
};
|
package/package.json
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@queuebase/node",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
|
-
"import": "./dist/index.js"
|
|
8
|
+
"import": "./dist/index.js",
|
|
9
|
+
"require": "./dist/index.cjs"
|
|
9
10
|
},
|
|
10
11
|
"./handler": {
|
|
11
12
|
"types": "./dist/handler.d.ts",
|
|
12
|
-
"import": "./dist/handler.js"
|
|
13
|
+
"import": "./dist/handler.js",
|
|
14
|
+
"require": "./dist/handler.cjs"
|
|
13
15
|
}
|
|
14
16
|
},
|
|
15
17
|
"files": [
|
|
@@ -20,16 +22,17 @@
|
|
|
20
22
|
},
|
|
21
23
|
"dependencies": {
|
|
22
24
|
"zod": "^3.24.1",
|
|
23
|
-
"@queuebase/core": "1.
|
|
25
|
+
"@queuebase/core": "1.2.0"
|
|
24
26
|
},
|
|
25
27
|
"devDependencies": {
|
|
26
28
|
"@types/node": "^22.10.5",
|
|
29
|
+
"tsup": "^8.4.0",
|
|
27
30
|
"typescript": "^5.7.2",
|
|
28
31
|
"vitest": "^2.1.8",
|
|
29
32
|
"@queuebase/tsconfig": "0.0.0"
|
|
30
33
|
},
|
|
31
34
|
"scripts": {
|
|
32
|
-
"build": "
|
|
35
|
+
"build": "tsup",
|
|
33
36
|
"dev": "tsc -b --watch",
|
|
34
37
|
"clean": "rm -rf dist .turbo *.tsbuildinfo",
|
|
35
38
|
"typecheck": "tsc --noEmit",
|