@kne/fastify-task 1.1.1 → 1.1.2
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 +77 -2
- package/libs/controllers/main.js +2 -2
- package/libs/services/main.js +30 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -41,6 +41,55 @@ npm i --save @kne/fastify-task
|
|
|
41
41
|
| failed | 执行失败 |
|
|
42
42
|
| canceled | 已取消 |
|
|
43
43
|
|
|
44
|
+
### 签名验证
|
|
45
|
+
|
|
46
|
+
当任务的 `context.secret` 设置了密钥时,外部调用需要提供 HMAC-SHA256 签名进行验证。
|
|
47
|
+
|
|
48
|
+
#### 签名生成方法
|
|
49
|
+
|
|
50
|
+
```javascript
|
|
51
|
+
const crypto = require('node:crypto');
|
|
52
|
+
|
|
53
|
+
function generateSignature({ secret, id, data }) {
|
|
54
|
+
// data 可以是字符串或对象
|
|
55
|
+
const dataStr = typeof data === 'string' ? data : JSON.stringify(data);
|
|
56
|
+
const dataToSign = `${id}|${dataStr}`;
|
|
57
|
+
const hmac = crypto.createHmac('sha256', secret);
|
|
58
|
+
hmac.update(dataToSign);
|
|
59
|
+
return hmac.digest('hex');
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
#### 各接口签名数据格式
|
|
64
|
+
|
|
65
|
+
| 接口 | data 格式 |
|
|
66
|
+
|----------------------|----------------------------------|
|
|
67
|
+
| processNext | `result` 字符串(JSON格式结果) |
|
|
68
|
+
| logWithSignature | `{ data, message }` 对象 |
|
|
69
|
+
| callbackWithSignature| `{ code, data, message }` 对象 |
|
|
70
|
+
|
|
71
|
+
#### 示例
|
|
72
|
+
|
|
73
|
+
```javascript
|
|
74
|
+
// processNext 签名
|
|
75
|
+
const result = JSON.stringify({ code: 0, data: { output: 'done' } });
|
|
76
|
+
const signature = generateSignature({ secret: 'your-secret', id: 'task-1', data: result });
|
|
77
|
+
|
|
78
|
+
// logWithSignature 签名
|
|
79
|
+
const signature = generateSignature({
|
|
80
|
+
secret: 'your-secret',
|
|
81
|
+
id: 'task-1',
|
|
82
|
+
data: { data: { key: 'value' }, message: '日志消息' }
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// callbackWithSignature 签名
|
|
86
|
+
const signature = generateSignature({
|
|
87
|
+
secret: 'your-secret',
|
|
88
|
+
id: 'task-1',
|
|
89
|
+
data: { code: 0, data: { result: 'done' }, message: '成功' }
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
44
93
|
### 示例
|
|
45
94
|
|
|
46
95
|
### API
|
|
@@ -211,7 +260,20 @@ npm i --save @kne/fastify-task
|
|
|
211
260
|
| id | string | 否 | 单个任务ID |
|
|
212
261
|
| taskIds | string[] | 否 | 任务ID数组 |
|
|
213
262
|
|
|
214
|
-
#### log -
|
|
263
|
+
#### log - 记录日志(内部调用)
|
|
264
|
+
|
|
265
|
+
> 内部调用方法,不需要签名验证
|
|
266
|
+
|
|
267
|
+
| 参数名 | 类型 | 必填 | 说明 |
|
|
268
|
+
|-----------|--------|----|-----------------|
|
|
269
|
+
| id | string | 是 | 任务ID |
|
|
270
|
+
| taskId | string | 是 | 任务ID(id的别名) |
|
|
271
|
+
| data | object | 否 | 日志数据 |
|
|
272
|
+
| message | string | 否 | 日志消息 |
|
|
273
|
+
|
|
274
|
+
#### logWithSignature - 记录日志(外部调用)
|
|
275
|
+
|
|
276
|
+
> 外部调用方法,需要签名验证(HTTP API使用)
|
|
215
277
|
|
|
216
278
|
| 参数名 | 类型 | 必填 | 说明 |
|
|
217
279
|
|-----------|--------|----|----------------------------------------|
|
|
@@ -221,7 +283,20 @@ npm i --save @kne/fastify-task
|
|
|
221
283
|
| message | string | 否 | 日志消息 |
|
|
222
284
|
| signature | string | 否 | HMAC-SHA256签名(当任务设置了context.secret时必填) |
|
|
223
285
|
|
|
224
|
-
#### callback -
|
|
286
|
+
#### callback - 任务回调(内部调用)
|
|
287
|
+
|
|
288
|
+
> 内部调用方法,不需要签名验证
|
|
289
|
+
|
|
290
|
+
| 参数名 | 类型 | 必填 | 说明 |
|
|
291
|
+
|-----------|--------|----|-----------|
|
|
292
|
+
| id | string | 是 | 任务ID |
|
|
293
|
+
| code | number | 是 | 状态码,0为成功 |
|
|
294
|
+
| data | object | 否 | 回调数据 |
|
|
295
|
+
| message | string | 否 | 回调消息 |
|
|
296
|
+
|
|
297
|
+
#### callbackWithSignature - 任务回调(外部调用)
|
|
298
|
+
|
|
299
|
+
> 外部调用方法,需要签名验证(HTTP API使用)
|
|
225
300
|
|
|
226
301
|
| 参数名 | 类型 | 必填 | 说明 |
|
|
227
302
|
|-----------|--------|----|----------------------------------------|
|
package/libs/controllers/main.js
CHANGED
|
@@ -228,7 +228,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
228
228
|
}
|
|
229
229
|
},
|
|
230
230
|
async request => {
|
|
231
|
-
return await services.
|
|
231
|
+
return await services.logWithSignature(Object.assign({}, request.body, { taskId: request.query.taskId }));
|
|
232
232
|
}
|
|
233
233
|
);
|
|
234
234
|
|
|
@@ -269,7 +269,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
269
269
|
}
|
|
270
270
|
},
|
|
271
271
|
async request => {
|
|
272
|
-
return await services.
|
|
272
|
+
return await services.callbackWithSignature(Object.assign({}, request.body, { taskId: request.query.taskId }));
|
|
273
273
|
}
|
|
274
274
|
);
|
|
275
275
|
});
|
package/libs/services/main.js
CHANGED
|
@@ -439,17 +439,13 @@ module.exports = fp(async (fastify, options) => {
|
|
|
439
439
|
}
|
|
440
440
|
};
|
|
441
441
|
|
|
442
|
-
const log = async ({ id, taskId, data, message = ''
|
|
442
|
+
const log = async ({ id, taskId, data, message = '' }) => {
|
|
443
443
|
const targetId = id || taskId;
|
|
444
444
|
const task = await detail({ id: targetId });
|
|
445
445
|
if (!task) {
|
|
446
446
|
throw new Error('任务不存在');
|
|
447
447
|
}
|
|
448
448
|
|
|
449
|
-
if (task.context?.secret && !verifySignature({ secret: task.context.secret, id: targetId, data: { data, message }, signature })) {
|
|
450
|
-
throw new Error('签名验证失败');
|
|
451
|
-
}
|
|
452
|
-
|
|
453
449
|
const currentOptions = task.options || {};
|
|
454
450
|
const logs = (currentOptions.logs || []).slice(0);
|
|
455
451
|
logs.splice(0, 0, {
|
|
@@ -470,21 +466,28 @@ module.exports = fp(async (fastify, options) => {
|
|
|
470
466
|
return task;
|
|
471
467
|
};
|
|
472
468
|
|
|
473
|
-
const
|
|
474
|
-
const
|
|
469
|
+
const logWithSignature = async ({ id, taskId, data, message = '', signature }) => {
|
|
470
|
+
const targetId = id || taskId;
|
|
471
|
+
const task = await detail({ id: targetId });
|
|
475
472
|
if (!task) {
|
|
476
473
|
throw new Error('任务不存在');
|
|
477
474
|
}
|
|
478
475
|
|
|
479
|
-
if (task.context?.secret && !verifySignature({ secret: task.context.secret, id, data: {
|
|
476
|
+
if (task.context?.secret && !verifySignature({ secret: task.context.secret, id: targetId, data: { data, message }, signature })) {
|
|
480
477
|
throw new Error('签名验证失败');
|
|
481
478
|
}
|
|
482
479
|
|
|
480
|
+
return log({ id: targetId, data, message });
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
const callback = async ({ id, code, data, message }) => {
|
|
484
|
+
const task = await detail({ id });
|
|
485
|
+
if (!task) {
|
|
486
|
+
throw new Error('任务不存在');
|
|
487
|
+
}
|
|
488
|
+
|
|
483
489
|
const result = { code, data, message };
|
|
484
|
-
|
|
485
|
-
const logMessage = '回调结果';
|
|
486
|
-
const logSignature = task.context?.secret ? generateSignature({ secret: task.context.secret, id, data: { data: logData, message: logMessage } }) : undefined;
|
|
487
|
-
await log({ taskId: id, message: logMessage, data: logData, signature: logSignature });
|
|
490
|
+
await log({ id, message: '回调结果', data: JSON.stringify(result) });
|
|
488
491
|
const input = Object.assign(
|
|
489
492
|
{},
|
|
490
493
|
{ id },
|
|
@@ -502,6 +505,19 @@ module.exports = fp(async (fastify, options) => {
|
|
|
502
505
|
await complete(input);
|
|
503
506
|
};
|
|
504
507
|
|
|
508
|
+
const callbackWithSignature = async ({ id, code, data, message, signature }) => {
|
|
509
|
+
const task = await detail({ id });
|
|
510
|
+
if (!task) {
|
|
511
|
+
throw new Error('任务不存在');
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
if (task.context?.secret && !verifySignature({ secret: task.context.secret, id, data: { code, data, message }, signature })) {
|
|
515
|
+
throw new Error('签名验证失败');
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
return callback({ id, code, data, message });
|
|
519
|
+
};
|
|
520
|
+
|
|
505
521
|
Object.assign(fastify[options.name].services, {
|
|
506
522
|
create,
|
|
507
523
|
detail,
|
|
@@ -512,7 +528,9 @@ module.exports = fp(async (fastify, options) => {
|
|
|
512
528
|
resetAll,
|
|
513
529
|
retry,
|
|
514
530
|
log,
|
|
531
|
+
logWithSignature,
|
|
515
532
|
callback,
|
|
533
|
+
callbackWithSignature,
|
|
516
534
|
executor,
|
|
517
535
|
processNext,
|
|
518
536
|
processSystemTask,
|