@constructive-io/knative-job-fn 0.2.7 → 0.3.1

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/README.md CHANGED
@@ -1 +1,59 @@
1
1
  # knative-job-fn
2
+
3
+ ---
4
+
5
+ ## Education and Tutorials
6
+
7
+ 1. 🚀 [Quickstart: Getting Up and Running](https://constructive.io/learn/quickstart)
8
+ Get started with modular databases in minutes. Install prerequisites and deploy your first module.
9
+
10
+ 2. 📦 [Modular PostgreSQL Development with Database Packages](https://constructive.io/learn/modular-postgres)
11
+ Learn to organize PostgreSQL projects with pgpm workspaces and reusable database modules.
12
+
13
+ 3. ✏️ [Authoring Database Changes](https://constructive.io/learn/authoring-database-changes)
14
+ Master the workflow for adding, organizing, and managing database changes with pgpm.
15
+
16
+ 4. 🧪 [End-to-End PostgreSQL Testing with TypeScript](https://constructive.io/learn/e2e-postgres-testing)
17
+ Master end-to-end PostgreSQL testing with ephemeral databases, RLS testing, and CI/CD automation.
18
+
19
+ 5. ⚡ [Supabase Testing](https://constructive.io/learn/supabase)
20
+ Use TypeScript-first tools to test Supabase projects with realistic RLS, policies, and auth contexts.
21
+
22
+ 6. 💧 [Drizzle ORM Testing](https://constructive.io/learn/drizzle-testing)
23
+ Run full-stack tests with Drizzle ORM, including database setup, teardown, and RLS enforcement.
24
+
25
+ 7. 🔧 [Troubleshooting](https://constructive.io/learn/troubleshooting)
26
+ Common issues and solutions for pgpm, PostgreSQL, and testing.
27
+
28
+ ## Related Constructive Tooling
29
+
30
+ ### 📦 Package Management
31
+
32
+ * [pgpm](https://github.com/constructive-io/constructive/tree/main/pgpm/pgpm): **🖥️ PostgreSQL Package Manager** for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
33
+
34
+ ### 🧪 Testing
35
+
36
+ * [pgsql-test](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-test): **📊 Isolated testing environments** with per-test transaction rollbacks—ideal for integration tests, complex migrations, and RLS simulation.
37
+ * [pgsql-seed](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-seed): **🌱 PostgreSQL seeding utilities** for CSV, JSON, SQL data loading, and pgpm deployment.
38
+ * [supabase-test](https://github.com/constructive-io/constructive/tree/main/postgres/supabase-test): **🧪 Supabase-native test harness** preconfigured for the local Supabase stack—per-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
39
+ * [graphile-test](https://github.com/constructive-io/constructive/tree/main/graphile/graphile-test): **🔐 Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
40
+ * [pg-query-context](https://github.com/constructive-io/constructive/tree/main/postgres/pg-query-context): **🔒 Session context injection** to add session-local context (e.g., `SET LOCAL`) into queries—ideal for setting `role`, `jwt.claims`, and other session settings.
41
+
42
+ ### 🧠 Parsing & AST
43
+
44
+ * [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
45
+ * [libpg-query-node](https://www.npmjs.com/package/libpg-query): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
46
+ * [pg-proto-parser](https://www.npmjs.com/package/pg-proto-parser): **📦 Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
47
+ * [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): **🏷️ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
48
+ * [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): **📝 Type definitions** for PostgreSQL AST nodes in TypeScript.
49
+ * [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): **🛠️ AST utilities** for constructing and transforming PostgreSQL syntax trees.
50
+
51
+ ## Credits
52
+
53
+ **🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
54
+
55
+ ## Disclaimer
56
+
57
+ AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
58
+
59
+ No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
package/esm/index.js ADDED
@@ -0,0 +1,236 @@
1
+ import express from 'express';
2
+ import bodyParser from 'body-parser';
3
+ import http from 'node:http';
4
+ import https from 'node:https';
5
+ import { URL } from 'node:url';
6
+ import { createLogger } from '@pgpmjs/logger';
7
+ function getHeaders(req) {
8
+ return {
9
+ 'x-worker-id': req.get('X-Worker-Id'),
10
+ 'x-job-id': req.get('X-Job-Id'),
11
+ 'x-database-id': req.get('X-Database-Id'),
12
+ 'x-callback-url': req.get('X-Callback-Url')
13
+ };
14
+ }
15
+ // Normalize callback URL so it always points at the /callback endpoint.
16
+ const normalizeCallbackUrl = (rawUrl) => {
17
+ try {
18
+ const url = new URL(rawUrl);
19
+ if (!url.pathname || url.pathname === '/') {
20
+ url.pathname = '/callback';
21
+ }
22
+ return url.toString();
23
+ }
24
+ catch {
25
+ return rawUrl;
26
+ }
27
+ };
28
+ const postJson = (urlStr, headers, body) => {
29
+ return new Promise((resolve, reject) => {
30
+ let url;
31
+ try {
32
+ url = new URL(urlStr);
33
+ }
34
+ catch (e) {
35
+ return reject(e);
36
+ }
37
+ const isHttps = url.protocol === 'https:';
38
+ const client = isHttps ? https : http;
39
+ const req = client.request({
40
+ hostname: url.hostname,
41
+ port: url.port || (isHttps ? 443 : 80),
42
+ path: url.pathname + url.search,
43
+ method: 'POST',
44
+ headers: {
45
+ 'Content-Type': 'application/json',
46
+ ...headers
47
+ }
48
+ }, (res) => {
49
+ // Drain response data but ignore contents; callback server
50
+ // only uses status for debugging.
51
+ res.on('data', () => { });
52
+ res.on('end', () => resolve());
53
+ });
54
+ req.on('error', (err) => reject(err));
55
+ req.write(JSON.stringify(body));
56
+ req.end();
57
+ });
58
+ };
59
+ const sendJobCallback = async (ctx, status, errorMessage) => {
60
+ const { callbackUrl, workerId, jobId, databaseId } = ctx;
61
+ if (!callbackUrl || !workerId || !jobId) {
62
+ return;
63
+ }
64
+ const target = normalizeCallbackUrl(callbackUrl);
65
+ const headers = {
66
+ 'X-Worker-Id': workerId,
67
+ 'X-Job-Id': jobId
68
+ };
69
+ if (databaseId) {
70
+ headers['X-Database-Id'] = databaseId;
71
+ }
72
+ const body = {
73
+ status
74
+ };
75
+ if (status === 'error') {
76
+ headers['X-Job-Error'] = 'true';
77
+ body.error = errorMessage || 'ERROR';
78
+ }
79
+ try {
80
+ logger.info('Sending job callback', {
81
+ status,
82
+ target: normalizeCallbackUrl(callbackUrl),
83
+ workerId,
84
+ jobId,
85
+ databaseId
86
+ });
87
+ await postJson(target, headers, body);
88
+ }
89
+ catch (err) {
90
+ logger.error('Failed to POST job callback', {
91
+ target,
92
+ status,
93
+ err
94
+ });
95
+ }
96
+ };
97
+ const logger = createLogger('knative-job-fn');
98
+ const createJobApp = () => {
99
+ const app = express();
100
+ app.use(bodyParser.json());
101
+ // Basic request logging for all incoming job invocations.
102
+ app.use((req, res, next) => {
103
+ try {
104
+ // Log only the headers we care about plus a shallow body snapshot
105
+ const headers = getHeaders(req);
106
+ let body;
107
+ if (req.body && typeof req.body === 'object') {
108
+ // Only log top-level keys to avoid exposing sensitive body contents.
109
+ body = { keys: Object.keys(req.body) };
110
+ }
111
+ else if (typeof req.body === 'string') {
112
+ // For string bodies, log only the length.
113
+ body = { length: req.body.length };
114
+ }
115
+ else {
116
+ body = undefined;
117
+ }
118
+ logger.info('Incoming job request', {
119
+ method: req.method,
120
+ path: req.originalUrl || req.url,
121
+ headers,
122
+ body
123
+ });
124
+ }
125
+ catch {
126
+ // best-effort logging; never block the request
127
+ }
128
+ next();
129
+ });
130
+ // Echo job headers back on responses for debugging/traceability.
131
+ app.use((req, res, next) => {
132
+ res.set({
133
+ 'Content-Type': 'application/json',
134
+ 'X-Worker-Id': req.get('X-Worker-Id'),
135
+ 'X-Database-Id': req.get('X-Database-Id'),
136
+ 'X-Job-Id': req.get('X-Job-Id')
137
+ });
138
+ next();
139
+ });
140
+ // Attach per-request context and a finish hook to send success callbacks.
141
+ app.use((req, res, next) => {
142
+ const ctx = {
143
+ callbackUrl: req.get('X-Callback-Url'),
144
+ workerId: req.get('X-Worker-Id'),
145
+ jobId: req.get('X-Job-Id'),
146
+ databaseId: req.get('X-Database-Id')
147
+ };
148
+ // Store on res.locals so the error middleware can also mark callbacks as sent.
149
+ res.locals = res.locals || {};
150
+ res.locals.jobContext = ctx;
151
+ res.locals.jobCallbackSent = false;
152
+ if (ctx.callbackUrl && ctx.workerId && ctx.jobId) {
153
+ res.on('finish', () => {
154
+ // If an error handler already sent a callback, skip.
155
+ if (res.locals.jobCallbackSent)
156
+ return;
157
+ res.locals.jobCallbackSent = true;
158
+ logger.info('Function completed', {
159
+ workerId: ctx.workerId,
160
+ jobId: ctx.jobId,
161
+ databaseId: ctx.databaseId,
162
+ statusCode: res.statusCode
163
+ });
164
+ void sendJobCallback(ctx, 'success');
165
+ });
166
+ }
167
+ next();
168
+ });
169
+ return {
170
+ post: function (...args) {
171
+ return app.post.apply(app, args);
172
+ },
173
+ listen: (port, hostOrCb, cb = () => { }) => {
174
+ // NOTE Remember that Express middleware executes in order.
175
+ // You should define error handlers last, after all other middleware.
176
+ // Otherwise, your error handler won't get called
177
+ // eslint-disable-next-line no-unused-vars
178
+ app.use(async (error, req, res, next) => {
179
+ res.set({
180
+ 'Content-Type': 'application/json',
181
+ 'X-Job-Error': true
182
+ });
183
+ // Mark job as having errored via callback, if available.
184
+ try {
185
+ const ctx = res.locals?.jobContext;
186
+ if (ctx && !res.locals.jobCallbackSent) {
187
+ res.locals.jobCallbackSent = true;
188
+ await sendJobCallback(ctx, 'error', error?.message);
189
+ }
190
+ }
191
+ catch (err) {
192
+ logger.error('Failed to send error callback', err);
193
+ }
194
+ // Log the full error context for debugging.
195
+ try {
196
+ const headers = getHeaders(req);
197
+ // Some error types (e.g. GraphQL ClientError) expose response info.
198
+ const errorDetails = {
199
+ message: error?.message,
200
+ name: error?.name,
201
+ stack: error?.stack
202
+ };
203
+ if (error?.response) {
204
+ errorDetails.response = {
205
+ status: error.response.status,
206
+ statusText: error.response.statusText,
207
+ errors: error.response.errors,
208
+ data: error.response.data
209
+ };
210
+ }
211
+ logger.error('Function error', {
212
+ headers,
213
+ path: req.originalUrl || req.url,
214
+ error: errorDetails
215
+ });
216
+ }
217
+ catch {
218
+ // never throw from the error logger
219
+ }
220
+ res.status(200).json({ message: error.message });
221
+ });
222
+ const host = typeof hostOrCb === 'string' ? hostOrCb : undefined;
223
+ const callback = typeof hostOrCb === 'function' ? hostOrCb : cb;
224
+ const onListen = () => {
225
+ callback();
226
+ };
227
+ const server = host
228
+ ? app.listen(port, host, onListen)
229
+ : app.listen(port, onListen);
230
+ return server;
231
+ }
232
+ };
233
+ };
234
+ const defaultApp = createJobApp();
235
+ export { createJobApp };
236
+ export default defaultApp;
package/index.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { Server as HttpServer } from 'http';
2
+ declare const createJobApp: () => {
3
+ post: (...args: any[]) => any;
4
+ listen: (port: any, hostOrCb?: string | (() => void), cb?: () => void) => HttpServer;
5
+ };
6
+ declare const defaultApp: {
7
+ post: (...args: any[]) => any;
8
+ listen: (port: any, hostOrCb?: string | (() => void), cb?: () => void) => HttpServer;
9
+ };
10
+ export { createJobApp };
11
+ export default defaultApp;
package/index.js ADDED
@@ -0,0 +1,242 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createJobApp = void 0;
7
+ const express_1 = __importDefault(require("express"));
8
+ const body_parser_1 = __importDefault(require("body-parser"));
9
+ const node_http_1 = __importDefault(require("node:http"));
10
+ const node_https_1 = __importDefault(require("node:https"));
11
+ const node_url_1 = require("node:url");
12
+ const logger_1 = require("@pgpmjs/logger");
13
+ function getHeaders(req) {
14
+ return {
15
+ 'x-worker-id': req.get('X-Worker-Id'),
16
+ 'x-job-id': req.get('X-Job-Id'),
17
+ 'x-database-id': req.get('X-Database-Id'),
18
+ 'x-callback-url': req.get('X-Callback-Url')
19
+ };
20
+ }
21
+ // Normalize callback URL so it always points at the /callback endpoint.
22
+ const normalizeCallbackUrl = (rawUrl) => {
23
+ try {
24
+ const url = new node_url_1.URL(rawUrl);
25
+ if (!url.pathname || url.pathname === '/') {
26
+ url.pathname = '/callback';
27
+ }
28
+ return url.toString();
29
+ }
30
+ catch {
31
+ return rawUrl;
32
+ }
33
+ };
34
+ const postJson = (urlStr, headers, body) => {
35
+ return new Promise((resolve, reject) => {
36
+ let url;
37
+ try {
38
+ url = new node_url_1.URL(urlStr);
39
+ }
40
+ catch (e) {
41
+ return reject(e);
42
+ }
43
+ const isHttps = url.protocol === 'https:';
44
+ const client = isHttps ? node_https_1.default : node_http_1.default;
45
+ const req = client.request({
46
+ hostname: url.hostname,
47
+ port: url.port || (isHttps ? 443 : 80),
48
+ path: url.pathname + url.search,
49
+ method: 'POST',
50
+ headers: {
51
+ 'Content-Type': 'application/json',
52
+ ...headers
53
+ }
54
+ }, (res) => {
55
+ // Drain response data but ignore contents; callback server
56
+ // only uses status for debugging.
57
+ res.on('data', () => { });
58
+ res.on('end', () => resolve());
59
+ });
60
+ req.on('error', (err) => reject(err));
61
+ req.write(JSON.stringify(body));
62
+ req.end();
63
+ });
64
+ };
65
+ const sendJobCallback = async (ctx, status, errorMessage) => {
66
+ const { callbackUrl, workerId, jobId, databaseId } = ctx;
67
+ if (!callbackUrl || !workerId || !jobId) {
68
+ return;
69
+ }
70
+ const target = normalizeCallbackUrl(callbackUrl);
71
+ const headers = {
72
+ 'X-Worker-Id': workerId,
73
+ 'X-Job-Id': jobId
74
+ };
75
+ if (databaseId) {
76
+ headers['X-Database-Id'] = databaseId;
77
+ }
78
+ const body = {
79
+ status
80
+ };
81
+ if (status === 'error') {
82
+ headers['X-Job-Error'] = 'true';
83
+ body.error = errorMessage || 'ERROR';
84
+ }
85
+ try {
86
+ logger.info('Sending job callback', {
87
+ status,
88
+ target: normalizeCallbackUrl(callbackUrl),
89
+ workerId,
90
+ jobId,
91
+ databaseId
92
+ });
93
+ await postJson(target, headers, body);
94
+ }
95
+ catch (err) {
96
+ logger.error('Failed to POST job callback', {
97
+ target,
98
+ status,
99
+ err
100
+ });
101
+ }
102
+ };
103
+ const logger = (0, logger_1.createLogger)('knative-job-fn');
104
+ const createJobApp = () => {
105
+ const app = (0, express_1.default)();
106
+ app.use(body_parser_1.default.json());
107
+ // Basic request logging for all incoming job invocations.
108
+ app.use((req, res, next) => {
109
+ try {
110
+ // Log only the headers we care about plus a shallow body snapshot
111
+ const headers = getHeaders(req);
112
+ let body;
113
+ if (req.body && typeof req.body === 'object') {
114
+ // Only log top-level keys to avoid exposing sensitive body contents.
115
+ body = { keys: Object.keys(req.body) };
116
+ }
117
+ else if (typeof req.body === 'string') {
118
+ // For string bodies, log only the length.
119
+ body = { length: req.body.length };
120
+ }
121
+ else {
122
+ body = undefined;
123
+ }
124
+ logger.info('Incoming job request', {
125
+ method: req.method,
126
+ path: req.originalUrl || req.url,
127
+ headers,
128
+ body
129
+ });
130
+ }
131
+ catch {
132
+ // best-effort logging; never block the request
133
+ }
134
+ next();
135
+ });
136
+ // Echo job headers back on responses for debugging/traceability.
137
+ app.use((req, res, next) => {
138
+ res.set({
139
+ 'Content-Type': 'application/json',
140
+ 'X-Worker-Id': req.get('X-Worker-Id'),
141
+ 'X-Database-Id': req.get('X-Database-Id'),
142
+ 'X-Job-Id': req.get('X-Job-Id')
143
+ });
144
+ next();
145
+ });
146
+ // Attach per-request context and a finish hook to send success callbacks.
147
+ app.use((req, res, next) => {
148
+ const ctx = {
149
+ callbackUrl: req.get('X-Callback-Url'),
150
+ workerId: req.get('X-Worker-Id'),
151
+ jobId: req.get('X-Job-Id'),
152
+ databaseId: req.get('X-Database-Id')
153
+ };
154
+ // Store on res.locals so the error middleware can also mark callbacks as sent.
155
+ res.locals = res.locals || {};
156
+ res.locals.jobContext = ctx;
157
+ res.locals.jobCallbackSent = false;
158
+ if (ctx.callbackUrl && ctx.workerId && ctx.jobId) {
159
+ res.on('finish', () => {
160
+ // If an error handler already sent a callback, skip.
161
+ if (res.locals.jobCallbackSent)
162
+ return;
163
+ res.locals.jobCallbackSent = true;
164
+ logger.info('Function completed', {
165
+ workerId: ctx.workerId,
166
+ jobId: ctx.jobId,
167
+ databaseId: ctx.databaseId,
168
+ statusCode: res.statusCode
169
+ });
170
+ void sendJobCallback(ctx, 'success');
171
+ });
172
+ }
173
+ next();
174
+ });
175
+ return {
176
+ post: function (...args) {
177
+ return app.post.apply(app, args);
178
+ },
179
+ listen: (port, hostOrCb, cb = () => { }) => {
180
+ // NOTE Remember that Express middleware executes in order.
181
+ // You should define error handlers last, after all other middleware.
182
+ // Otherwise, your error handler won't get called
183
+ // eslint-disable-next-line no-unused-vars
184
+ app.use(async (error, req, res, next) => {
185
+ res.set({
186
+ 'Content-Type': 'application/json',
187
+ 'X-Job-Error': true
188
+ });
189
+ // Mark job as having errored via callback, if available.
190
+ try {
191
+ const ctx = res.locals?.jobContext;
192
+ if (ctx && !res.locals.jobCallbackSent) {
193
+ res.locals.jobCallbackSent = true;
194
+ await sendJobCallback(ctx, 'error', error?.message);
195
+ }
196
+ }
197
+ catch (err) {
198
+ logger.error('Failed to send error callback', err);
199
+ }
200
+ // Log the full error context for debugging.
201
+ try {
202
+ const headers = getHeaders(req);
203
+ // Some error types (e.g. GraphQL ClientError) expose response info.
204
+ const errorDetails = {
205
+ message: error?.message,
206
+ name: error?.name,
207
+ stack: error?.stack
208
+ };
209
+ if (error?.response) {
210
+ errorDetails.response = {
211
+ status: error.response.status,
212
+ statusText: error.response.statusText,
213
+ errors: error.response.errors,
214
+ data: error.response.data
215
+ };
216
+ }
217
+ logger.error('Function error', {
218
+ headers,
219
+ path: req.originalUrl || req.url,
220
+ error: errorDetails
221
+ });
222
+ }
223
+ catch {
224
+ // never throw from the error logger
225
+ }
226
+ res.status(200).json({ message: error.message });
227
+ });
228
+ const host = typeof hostOrCb === 'string' ? hostOrCb : undefined;
229
+ const callback = typeof hostOrCb === 'function' ? hostOrCb : cb;
230
+ const onListen = () => {
231
+ callback();
232
+ };
233
+ const server = host
234
+ ? app.listen(port, host, onListen)
235
+ : app.listen(port, onListen);
236
+ return server;
237
+ }
238
+ };
239
+ };
240
+ exports.createJobApp = createJobApp;
241
+ const defaultApp = createJobApp();
242
+ exports.default = defaultApp;
package/package.json CHANGED
@@ -1,17 +1,20 @@
1
1
  {
2
2
  "name": "@constructive-io/knative-job-fn",
3
- "version": "0.2.7",
3
+ "version": "0.3.1",
4
4
  "description": "knative job fn",
5
5
  "author": "Constructive <developers@constructive.io>",
6
6
  "homepage": "https://github.com/constructive-io/jobs/tree/master/packages/knative-job-fn#readme",
7
7
  "license": "SEE LICENSE IN LICENSE",
8
- "main": "dist/index.js",
8
+ "main": "index.js",
9
+ "module": "esm/index.js",
10
+ "types": "index.d.ts",
9
11
  "directories": {
10
12
  "lib": "src",
11
13
  "test": "__tests__"
12
14
  },
13
15
  "publishConfig": {
14
- "access": "public"
16
+ "access": "public",
17
+ "directory": "dist"
15
18
  },
16
19
  "repository": {
17
20
  "type": "git",
@@ -21,15 +24,21 @@
21
24
  "test": "jest --passWithNoTests",
22
25
  "test:watch": "jest --watch",
23
26
  "test:debug": "node --inspect node_modules/.bin/jest --runInBand",
24
- "build": "tsc -p tsconfig.json",
25
- "build:watch": "tsc -p tsconfig.json -w"
27
+ "clean": "makage clean",
28
+ "prepack": "npm run build",
29
+ "build": "makage build",
30
+ "build:dev": "makage build --dev"
26
31
  },
27
32
  "bugs": {
28
33
  "url": "https://github.com/constructive-io/jobs/issues"
29
34
  },
35
+ "devDependencies": {
36
+ "makage": "^0.1.10"
37
+ },
30
38
  "dependencies": {
39
+ "@pgpmjs/logger": "^1.4.0",
31
40
  "body-parser": "1.19.0",
32
41
  "express": "5.2.1"
33
42
  },
34
- "gitHead": "976cc9e0e09201c7df40518a1797f4178fc21c2c"
43
+ "gitHead": "3ffd5718e86ea5fa9ca6e0930aeb510cf392f343"
35
44
  }
package/CHANGELOG.md DELETED
@@ -1,24 +0,0 @@
1
- # Change Log
2
-
3
- All notable changes to this project will be documented in this file.
4
- See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
-
6
- ## [0.2.7](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-fn@0.2.6...@constructive-io/knative-job-fn@0.2.7) (2025-12-25)
7
-
8
- **Note:** Version bump only for package @constructive-io/knative-job-fn
9
-
10
- ## [0.2.6](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-fn@0.2.5...@constructive-io/knative-job-fn@0.2.6) (2025-12-24)
11
-
12
- **Note:** Version bump only for package @constructive-io/knative-job-fn
13
-
14
- ## [0.2.5](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-fn@0.2.4...@constructive-io/knative-job-fn@0.2.5) (2025-12-19)
15
-
16
- **Note:** Version bump only for package @constructive-io/knative-job-fn
17
-
18
- ## 0.2.4 (2025-12-18)
19
-
20
- **Note:** Version bump only for package @constructive-io/knative-job-fn
21
-
22
- ## [0.2.3](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-fn@0.2.2...@constructive-io/knative-job-fn@0.2.3) (2025-12-17)
23
-
24
- **Note:** Version bump only for package @constructive-io/knative-job-fn
package/dist/index.d.ts DELETED
@@ -1,5 +0,0 @@
1
- declare const _default: {
2
- post: (...args: any[]) => any;
3
- listen: (port: any, cb?: () => void) => void;
4
- };
5
- export default _default;
package/dist/index.js DELETED
@@ -1,232 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const express_1 = __importDefault(require("express"));
7
- const body_parser_1 = __importDefault(require("body-parser"));
8
- const node_http_1 = __importDefault(require("node:http"));
9
- const node_https_1 = __importDefault(require("node:https"));
10
- const node_url_1 = require("node:url");
11
- function getHeaders(req) {
12
- return {
13
- 'x-worker-id': req.get('X-Worker-Id'),
14
- 'x-job-id': req.get('X-Job-Id'),
15
- 'x-database-id': req.get('X-Database-Id'),
16
- 'x-callback-url': req.get('X-Callback-Url')
17
- };
18
- }
19
- const app = (0, express_1.default)();
20
- app.use(body_parser_1.default.json());
21
- // Basic request logging for all incoming job invocations.
22
- app.use((req, res, next) => {
23
- try {
24
- // Log only the headers we care about plus a shallow body snapshot
25
- const headers = getHeaders(req);
26
- let body;
27
- if (req.body && typeof req.body === 'object') {
28
- // Only log top-level keys to avoid exposing sensitive body contents.
29
- body = { keys: Object.keys(req.body) };
30
- }
31
- else if (typeof req.body === 'string') {
32
- // For string bodies, log only the length.
33
- body = { length: req.body.length };
34
- }
35
- else {
36
- body = undefined;
37
- }
38
- // eslint-disable-next-line no-console
39
- console.log('[knative-job-fn] Incoming job request', {
40
- method: req.method,
41
- path: req.originalUrl || req.url,
42
- headers,
43
- body
44
- });
45
- }
46
- catch {
47
- // best-effort logging; never block the request
48
- }
49
- next();
50
- });
51
- // Echo job headers back on responses for debugging/traceability.
52
- app.use((req, res, next) => {
53
- res.set({
54
- 'Content-Type': 'application/json',
55
- 'X-Worker-Id': req.get('X-Worker-Id'),
56
- 'X-Database-Id': req.get('X-Database-Id'),
57
- 'X-Job-Id': req.get('X-Job-Id')
58
- });
59
- next();
60
- });
61
- // Normalize callback URL so it always points at the /callback endpoint.
62
- const normalizeCallbackUrl = (rawUrl) => {
63
- try {
64
- const url = new node_url_1.URL(rawUrl);
65
- if (!url.pathname || url.pathname === '/') {
66
- url.pathname = '/callback';
67
- }
68
- return url.toString();
69
- }
70
- catch {
71
- return rawUrl;
72
- }
73
- };
74
- const postJson = (urlStr, headers, body) => {
75
- return new Promise((resolve, reject) => {
76
- let url;
77
- try {
78
- url = new node_url_1.URL(urlStr);
79
- }
80
- catch (e) {
81
- return reject(e);
82
- }
83
- const isHttps = url.protocol === 'https:';
84
- const client = isHttps ? node_https_1.default : node_http_1.default;
85
- const req = client.request({
86
- hostname: url.hostname,
87
- port: url.port || (isHttps ? 443 : 80),
88
- path: url.pathname + url.search,
89
- method: 'POST',
90
- headers: {
91
- 'Content-Type': 'application/json',
92
- ...headers
93
- }
94
- }, (res) => {
95
- // Drain response data but ignore contents; callback server
96
- // only uses status for debugging.
97
- res.on('data', () => { });
98
- res.on('end', () => resolve());
99
- });
100
- req.on('error', (err) => reject(err));
101
- req.write(JSON.stringify(body));
102
- req.end();
103
- });
104
- };
105
- const sendJobCallback = async (ctx, status, errorMessage) => {
106
- const { callbackUrl, workerId, jobId, databaseId } = ctx;
107
- if (!callbackUrl || !workerId || !jobId) {
108
- return;
109
- }
110
- const target = normalizeCallbackUrl(callbackUrl);
111
- const headers = {
112
- 'X-Worker-Id': workerId,
113
- 'X-Job-Id': jobId
114
- };
115
- if (databaseId) {
116
- headers['X-Database-Id'] = databaseId;
117
- }
118
- const body = {
119
- status
120
- };
121
- if (status === 'error') {
122
- headers['X-Job-Error'] = 'true';
123
- body.error = errorMessage || 'ERROR';
124
- }
125
- try {
126
- // eslint-disable-next-line no-console
127
- console.log('[knative-job-fn] Sending job callback', {
128
- status,
129
- target: normalizeCallbackUrl(callbackUrl),
130
- workerId,
131
- jobId,
132
- databaseId
133
- });
134
- await postJson(target, headers, body);
135
- }
136
- catch (err) {
137
- // eslint-disable-next-line no-console
138
- console.error('[knative-job-fn] Failed to POST job callback', {
139
- target,
140
- status,
141
- err
142
- });
143
- }
144
- };
145
- // Attach per-request context and a finish hook to send success callbacks.
146
- app.use((req, res, next) => {
147
- const ctx = {
148
- callbackUrl: req.get('X-Callback-Url'),
149
- workerId: req.get('X-Worker-Id'),
150
- jobId: req.get('X-Job-Id'),
151
- databaseId: req.get('X-Database-Id')
152
- };
153
- // Store on res.locals so the error middleware can also mark callbacks as sent.
154
- res.locals = res.locals || {};
155
- res.locals.jobContext = ctx;
156
- res.locals.jobCallbackSent = false;
157
- if (ctx.callbackUrl && ctx.workerId && ctx.jobId) {
158
- res.on('finish', () => {
159
- // If an error handler already sent a callback, skip.
160
- if (res.locals.jobCallbackSent)
161
- return;
162
- res.locals.jobCallbackSent = true;
163
- // eslint-disable-next-line no-console
164
- console.log('[knative-job-fn] Function completed', {
165
- workerId: ctx.workerId,
166
- jobId: ctx.jobId,
167
- databaseId: ctx.databaseId,
168
- statusCode: res.statusCode
169
- });
170
- void sendJobCallback(ctx, 'success');
171
- });
172
- }
173
- next();
174
- });
175
- exports.default = {
176
- post: function (...args) {
177
- return app.post.apply(app, args);
178
- },
179
- listen: (port, cb = () => { }) => {
180
- // NOTE Remember that Express middleware executes in order.
181
- // You should define error handlers last, after all other middleware.
182
- // Otherwise, your error handler won't get called
183
- // eslint-disable-next-line no-unused-vars
184
- app.use(async (error, req, res, next) => {
185
- res.set({
186
- 'Content-Type': 'application/json',
187
- 'X-Job-Error': true
188
- });
189
- // Mark job as having errored via callback, if available.
190
- try {
191
- const ctx = res.locals?.jobContext;
192
- if (ctx && !res.locals.jobCallbackSent) {
193
- res.locals.jobCallbackSent = true;
194
- await sendJobCallback(ctx, 'error', error?.message);
195
- }
196
- }
197
- catch (err) {
198
- // eslint-disable-next-line no-console
199
- console.error('[knative-job-fn] Failed to send error callback', err);
200
- }
201
- // Log the full error context for debugging.
202
- try {
203
- const headers = getHeaders(req);
204
- // Some error types (e.g. GraphQL ClientError) expose response info.
205
- const errorDetails = {
206
- message: error?.message,
207
- name: error?.name,
208
- stack: error?.stack
209
- };
210
- if (error?.response) {
211
- errorDetails.response = {
212
- status: error.response.status,
213
- statusText: error.response.statusText,
214
- errors: error.response.errors,
215
- data: error.response.data
216
- };
217
- }
218
- // eslint-disable-next-line no-console
219
- console.error('[knative-job-fn] Function error', {
220
- headers,
221
- path: req.originalUrl || req.url,
222
- error: errorDetails
223
- });
224
- }
225
- catch {
226
- // never throw from the error logger
227
- }
228
- res.status(200).json({ message: error.message });
229
- });
230
- app.listen(port, cb);
231
- }
232
- };
package/jest.config.js DELETED
@@ -1,18 +0,0 @@
1
- /** @type {import('ts-jest').JestConfigWithTsJest} */
2
- module.exports = {
3
- preset: 'ts-jest',
4
- testEnvironment: 'node',
5
- transform: {
6
- '^.+\\.tsx?$': [
7
- 'ts-jest',
8
- {
9
- babelConfig: false,
10
- tsconfig: 'tsconfig.json',
11
- },
12
- ],
13
- },
14
- transformIgnorePatterns: [`/node_modules/*`],
15
- testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
16
- moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
17
- modulePathIgnorePatterns: ['dist/*'],
18
- };
package/src/index.ts DELETED
@@ -1,268 +0,0 @@
1
- import express from 'express';
2
- import bodyParser from 'body-parser';
3
- import http from 'node:http';
4
- import https from 'node:https';
5
- import { URL } from 'node:url';
6
-
7
- type JobCallbackStatus = 'success' | 'error';
8
-
9
- type JobContext = {
10
- callbackUrl: string | undefined;
11
- workerId: string | undefined;
12
- jobId: string | undefined;
13
- databaseId: string | undefined;
14
- };
15
-
16
- function getHeaders(req: any) {
17
- return {
18
- 'x-worker-id': req.get('X-Worker-Id'),
19
- 'x-job-id': req.get('X-Job-Id'),
20
- 'x-database-id': req.get('X-Database-Id'),
21
- 'x-callback-url': req.get('X-Callback-Url')
22
- };
23
- }
24
-
25
- const app: any = express();
26
-
27
- app.use(bodyParser.json());
28
-
29
- // Basic request logging for all incoming job invocations.
30
- app.use((req: any, res: any, next: any) => {
31
- try {
32
- // Log only the headers we care about plus a shallow body snapshot
33
- const headers = getHeaders(req);
34
-
35
- let body: any;
36
- if (req.body && typeof req.body === 'object') {
37
- // Only log top-level keys to avoid exposing sensitive body contents.
38
- body = { keys: Object.keys(req.body) };
39
- } else if (typeof req.body === 'string') {
40
- // For string bodies, log only the length.
41
- body = { length: req.body.length };
42
- } else {
43
- body = undefined;
44
- }
45
-
46
- // eslint-disable-next-line no-console
47
- console.log('[knative-job-fn] Incoming job request', {
48
- method: req.method,
49
- path: req.originalUrl || req.url,
50
- headers,
51
- body
52
- });
53
- } catch {
54
- // best-effort logging; never block the request
55
- }
56
- next();
57
- });
58
-
59
- // Echo job headers back on responses for debugging/traceability.
60
- app.use((req: any, res: any, next: any) => {
61
- res.set({
62
- 'Content-Type': 'application/json',
63
- 'X-Worker-Id': req.get('X-Worker-Id'),
64
- 'X-Database-Id': req.get('X-Database-Id'),
65
- 'X-Job-Id': req.get('X-Job-Id')
66
- });
67
- next();
68
- });
69
-
70
- // Normalize callback URL so it always points at the /callback endpoint.
71
- const normalizeCallbackUrl = (rawUrl: string): string => {
72
- try {
73
- const url = new URL(rawUrl);
74
- if (!url.pathname || url.pathname === '/') {
75
- url.pathname = '/callback';
76
- }
77
- return url.toString();
78
- } catch {
79
- return rawUrl;
80
- }
81
- };
82
-
83
- const postJson = (
84
- urlStr: string,
85
- headers: Record<string, string>,
86
- body: Record<string, unknown>
87
- ): Promise<void> => {
88
- return new Promise((resolve, reject) => {
89
- let url: URL;
90
- try {
91
- url = new URL(urlStr);
92
- } catch (e) {
93
- return reject(e);
94
- }
95
-
96
- const isHttps = url.protocol === 'https:';
97
- const client = isHttps ? https : http;
98
-
99
- const req = client.request(
100
- {
101
- hostname: url.hostname,
102
- port: url.port || (isHttps ? 443 : 80),
103
- path: url.pathname + url.search,
104
- method: 'POST',
105
- headers: {
106
- 'Content-Type': 'application/json',
107
- ...headers
108
- }
109
- },
110
- (res) => {
111
- // Drain response data but ignore contents; callback server
112
- // only uses status for debugging.
113
- res.on('data', () => {});
114
- res.on('end', () => resolve());
115
- }
116
- );
117
-
118
- req.on('error', (err) => reject(err));
119
- req.write(JSON.stringify(body));
120
- req.end();
121
- });
122
- };
123
-
124
- const sendJobCallback = async (
125
- ctx: JobContext,
126
- status: JobCallbackStatus,
127
- errorMessage?: string
128
- ) => {
129
- const { callbackUrl, workerId, jobId, databaseId } = ctx;
130
- if (!callbackUrl || !workerId || !jobId) {
131
- return;
132
- }
133
-
134
- const target = normalizeCallbackUrl(callbackUrl);
135
-
136
- const headers: Record<string, string> = {
137
- 'X-Worker-Id': workerId,
138
- 'X-Job-Id': jobId
139
- };
140
-
141
- if (databaseId) {
142
- headers['X-Database-Id'] = databaseId;
143
- }
144
-
145
- const body: Record<string, unknown> = {
146
- status
147
- };
148
-
149
- if (status === 'error') {
150
- headers['X-Job-Error'] = 'true';
151
- body.error = errorMessage || 'ERROR';
152
- }
153
-
154
- try {
155
- // eslint-disable-next-line no-console
156
- console.log('[knative-job-fn] Sending job callback', {
157
- status,
158
- target: normalizeCallbackUrl(callbackUrl),
159
- workerId,
160
- jobId,
161
- databaseId
162
- });
163
- await postJson(target, headers, body);
164
- } catch (err) {
165
- // eslint-disable-next-line no-console
166
- console.error('[knative-job-fn] Failed to POST job callback', {
167
- target,
168
- status,
169
- err
170
- });
171
- }
172
- };
173
-
174
- // Attach per-request context and a finish hook to send success callbacks.
175
- app.use((req: any, res: any, next: any) => {
176
- const ctx: JobContext = {
177
- callbackUrl: req.get('X-Callback-Url'),
178
- workerId: req.get('X-Worker-Id'),
179
- jobId: req.get('X-Job-Id'),
180
- databaseId: req.get('X-Database-Id')
181
- };
182
-
183
- // Store on res.locals so the error middleware can also mark callbacks as sent.
184
- res.locals = res.locals || {};
185
- res.locals.jobContext = ctx;
186
- res.locals.jobCallbackSent = false;
187
-
188
- if (ctx.callbackUrl && ctx.workerId && ctx.jobId) {
189
- res.on('finish', () => {
190
- // If an error handler already sent a callback, skip.
191
- if (res.locals.jobCallbackSent) return;
192
- res.locals.jobCallbackSent = true;
193
- // eslint-disable-next-line no-console
194
- console.log('[knative-job-fn] Function completed', {
195
- workerId: ctx.workerId,
196
- jobId: ctx.jobId,
197
- databaseId: ctx.databaseId,
198
- statusCode: res.statusCode
199
- });
200
- void sendJobCallback(ctx, 'success');
201
- });
202
- }
203
-
204
- next();
205
- });
206
-
207
- export default {
208
- post: function (...args: any[]) {
209
- return app.post.apply(app, args as any);
210
- },
211
- listen: (port: any, cb: () => void = () => {}) => {
212
- // NOTE Remember that Express middleware executes in order.
213
- // You should define error handlers last, after all other middleware.
214
- // Otherwise, your error handler won't get called
215
- // eslint-disable-next-line no-unused-vars
216
- app.use(async (error: any, req: any, res: any, next: any) => {
217
- res.set({
218
- 'Content-Type': 'application/json',
219
- 'X-Job-Error': true
220
- });
221
-
222
- // Mark job as having errored via callback, if available.
223
- try {
224
- const ctx: JobContext | undefined = res.locals?.jobContext;
225
- if (ctx && !res.locals.jobCallbackSent) {
226
- res.locals.jobCallbackSent = true;
227
- await sendJobCallback(ctx, 'error', error?.message);
228
- }
229
- } catch (err) {
230
- // eslint-disable-next-line no-console
231
- console.error('[knative-job-fn] Failed to send error callback', err);
232
- }
233
-
234
- // Log the full error context for debugging.
235
- try {
236
- const headers = getHeaders(req);
237
-
238
- // Some error types (e.g. GraphQL ClientError) expose response info.
239
- const errorDetails: any = {
240
- message: error?.message,
241
- name: error?.name,
242
- stack: error?.stack
243
- };
244
-
245
- if (error?.response) {
246
- errorDetails.response = {
247
- status: error.response.status,
248
- statusText: error.response.statusText,
249
- errors: error.response.errors,
250
- data: error.response.data
251
- };
252
- }
253
-
254
- // eslint-disable-next-line no-console
255
- console.error('[knative-job-fn] Function error', {
256
- headers,
257
- path: req.originalUrl || req.url,
258
- error: errorDetails
259
- });
260
- } catch {
261
- // never throw from the error logger
262
- }
263
-
264
- res.status(200).json({ message: error.message });
265
- });
266
- app.listen(port, cb);
267
- }
268
- };
package/tsconfig.esm.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "dist/esm",
5
- "module": "es2022",
6
- "rootDir": "src/",
7
- "declaration": false
8
- }
9
- }
package/tsconfig.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "dist",
5
- "rootDir": "src/"
6
- },
7
- "include": ["src/**/*.ts", "../../types/**/*.d.ts"],
8
- "exclude": ["dist", "node_modules", "**/*.spec.*", "**/*.test.*"]
9
- }