@anchan828/nest-cloud-run-queue-worker 3.1.1 → 3.1.3

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.
@@ -1,6 +1,6 @@
1
1
  export declare const QUEUE_WORKER_MODULE_OPTIONS = "QUEUE_WORKER_MODULE_OPTIONS";
2
2
  export declare const QUEUE_WORKER_DECORATOR = "QUEUE_WORKER_DECORATOR";
3
3
  export declare const QUEUE_WORKER_PROCESS_DECORATOR = "QUEUE_WORKER_PROCESS_DECORATOR";
4
- export declare const ERROR_INVALID_MESSAGE_FORMAT = "Invalid message format.";
5
- export declare const ERROR_QUEUE_WORKER_NAME_NOT_FOUND = "QueueWorker name not found.";
4
+ export declare const ERROR_INVALID_MESSAGE_FORMAT = "Invalid message format. The message must be an object that has a 'name' property.";
5
+ export declare const ERROR_QUEUE_WORKER_NAME_NOT_FOUND = "Worker name not found.";
6
6
  export declare const ERROR_WORKER_NOT_FOUND: (name: string) => string;
package/dist/constants.js CHANGED
@@ -5,7 +5,7 @@ exports.QUEUE_WORKER_MODULE_OPTIONS = "QUEUE_WORKER_MODULE_OPTIONS";
5
5
  exports.QUEUE_WORKER_DECORATOR = "QUEUE_WORKER_DECORATOR";
6
6
  exports.QUEUE_WORKER_PROCESS_DECORATOR = "QUEUE_WORKER_PROCESS_DECORATOR";
7
7
  // errors
8
- exports.ERROR_INVALID_MESSAGE_FORMAT = "Invalid message format.";
9
- exports.ERROR_QUEUE_WORKER_NAME_NOT_FOUND = "QueueWorker name not found.";
10
- const ERROR_WORKER_NOT_FOUND = (name) => `QueueWorker '${name}' not found.`;
8
+ exports.ERROR_INVALID_MESSAGE_FORMAT = "Invalid message format. The message must be an object that has a 'name' property.";
9
+ exports.ERROR_QUEUE_WORKER_NAME_NOT_FOUND = "Worker name not found.";
10
+ const ERROR_WORKER_NOT_FOUND = (name) => `Worker '${name}' not found.`;
11
11
  exports.ERROR_WORKER_NOT_FOUND = ERROR_WORKER_NOT_FOUND;
@@ -16,6 +16,13 @@ export interface QueueWorkerModuleOptions extends ModuleOptions {
16
16
  * @memberof QueueWorkerModuleOptions
17
17
  */
18
18
  maxRetryAttempts?: number;
19
+ /**
20
+ * extra config
21
+ *
22
+ * @type {QueueWorkerExtraConfig}
23
+ * @memberof QueueWorkerModuleOptions
24
+ */
25
+ extraConfig?: QueueWorkerExtraConfig;
19
26
  /**
20
27
  * Define a Route for the controller.
21
28
  * Default: POST /
@@ -40,6 +47,9 @@ export interface QueueWorkerProcessorMetadata extends QueueWorkerProcessDecorato
40
47
  processorName: string;
41
48
  processor: QueueWorkerProcessor;
42
49
  }
50
+ export type QueueWorkerExtraConfig = {
51
+ parseReviver?: (key: string, value: any) => any;
52
+ };
43
53
  export interface QueueWorkerDecoratorArgs {
44
54
  names: QueueWorkerName[];
45
55
  /**
package/dist/util.d.ts CHANGED
@@ -5,7 +5,7 @@ import { QueueWorkerDecodedMessage, QueueWorkerRawMessage } from "./interfaces";
5
5
  * JSON.parse has receiver for Date.parse.
6
6
  * @param json
7
7
  */
8
- export declare const parseJSON: <T>(json: string) => T;
8
+ export declare const parseJSON: <T>(json: string, reviver?: (key: string, value: any) => any) => T;
9
9
  /**
10
10
  * sort array by priority prop
11
11
  *
@@ -24,5 +24,10 @@ export declare function sortByPriority<T extends {
24
24
  * @param {string} value
25
25
  * @return {*} {boolean}
26
26
  */
27
- export declare function isBase64(value?: string | null | Message): value is string;
28
- export declare function decodeMessage<T = any>(message: QueueWorkerRawMessage | Message): QueueWorkerDecodedMessage<T>;
27
+ export declare function isBase64<T = any>(value?: string | null | Message<T>): value is string;
28
+ export declare function decodeMessage<T = any>(message: QueueWorkerRawMessage<T> | Message, reviver?: (key: string, value: any) => any): QueueWorkerDecodedMessage<T>;
29
+ export declare function isDecodedMessage<T = any>(message: QueueWorkerRawMessage<T> | QueueWorkerDecodedMessage<T> | Message<T>): message is QueueWorkerDecodedMessage<T>;
30
+ export declare function isTaskMessage<T>(message?: QueueWorkerRawMessage<T> | Message<T> | null): message is Message<T> & {
31
+ headers?: Record<string, string>;
32
+ };
33
+ export declare function isMessage<T>(message?: QueueWorkerRawMessage<T> | Message<T> | null): message is Message<T>;
package/dist/util.js CHANGED
@@ -4,6 +4,9 @@ exports.parseJSON = void 0;
4
4
  exports.sortByPriority = sortByPriority;
5
5
  exports.isBase64 = isBase64;
6
6
  exports.decodeMessage = decodeMessage;
7
+ exports.isDecodedMessage = isDecodedMessage;
8
+ exports.isTaskMessage = isTaskMessage;
9
+ exports.isMessage = isMessage;
7
10
  const common_1 = require("@nestjs/common");
8
11
  const constants_1 = require("./constants");
9
12
  const dateRegExp = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
@@ -12,18 +15,19 @@ const dateRegExp = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
12
15
  * JSON.parse has receiver for Date.parse.
13
16
  * @param json
14
17
  */
15
- const parseJSON = (json) => {
16
- return JSON.parse(json, (_, value) => {
17
- if (typeof value === "string" && value.length === 24 && dateRegExp.test(value)) {
18
- const date = new Date(value);
19
- if (+date === +date) {
20
- return date;
21
- }
22
- }
23
- return value;
24
- });
18
+ const parseJSON = (json, reviver) => {
19
+ return JSON.parse(json, (key, value) => reviver ? reviver(key, parseDate(key, value)) : parseDate(key, value));
25
20
  };
26
21
  exports.parseJSON = parseJSON;
22
+ function parseDate(key, value) {
23
+ if (typeof value === "string" && value.length === 24 && dateRegExp.test(value)) {
24
+ const date = new Date(value);
25
+ if (+date === +date) {
26
+ return date;
27
+ }
28
+ }
29
+ return value;
30
+ }
27
31
  /**
28
32
  * sort array by priority prop
29
33
  *
@@ -60,15 +64,26 @@ function isBase64(value) {
60
64
  const firstPaddingChar = value.indexOf("=");
61
65
  return (firstPaddingChar === -1 || firstPaddingChar === len - 1 || (firstPaddingChar === len - 2 && value[len - 1] === "="));
62
66
  }
63
- function decodeMessage(message) {
67
+ function decodeMessage(message, reviver) {
64
68
  let data;
65
69
  if (isBase64(message.data)) {
66
70
  // pubsub
67
- data = decodeData(message.data);
71
+ data = decodeData(message.data, reviver);
72
+ }
73
+ else if (isTaskMessage(message)) {
74
+ // tasks
75
+ data = { data: JSON.parse(JSON.stringify(message.data), reviver), name: message.name };
68
76
  }
69
77
  else {
70
- // tasks / http
71
- data = message;
78
+ // http / raw
79
+ const _message = isMessage(message) ? message : isMessage(message.data) ? message.data : undefined;
80
+ if (!_message) {
81
+ throw new common_1.BadRequestException(constants_1.ERROR_INVALID_MESSAGE_FORMAT);
82
+ }
83
+ data = _message;
84
+ }
85
+ if (!data) {
86
+ throw new common_1.BadRequestException(constants_1.ERROR_INVALID_MESSAGE_FORMAT);
72
87
  }
73
88
  return {
74
89
  data,
@@ -77,10 +92,36 @@ function decodeMessage(message) {
77
92
  raw: message,
78
93
  };
79
94
  }
95
+ function isDecodedMessage(message) {
96
+ return "raw" in message;
97
+ }
98
+ function isTaskMessage(message) {
99
+ if (!message) {
100
+ return false;
101
+ }
102
+ const keys = Object.keys(message);
103
+ return keys.length <= 3 && keys.includes("name") && keys.includes("headers");
104
+ }
105
+ function isMessage(message) {
106
+ if (!message) {
107
+ return false;
108
+ }
109
+ const keys = Object.keys(message);
110
+ return keys.length <= 3 && keys.includes("name");
111
+ }
80
112
  function getMessageId(raw) {
81
- return raw.messageId ?? raw.headers?.["x-cloudtasks-taskname"] ?? "";
113
+ if (!raw) {
114
+ return "";
115
+ }
116
+ if ("messageId" in raw) {
117
+ return raw.messageId ?? "";
118
+ }
119
+ if ("headers" in raw && raw.headers?.["x-cloudtasks-taskname"]) {
120
+ return raw.headers["x-cloudtasks-taskname"];
121
+ }
122
+ return "";
82
123
  }
83
- function decodeData(data) {
124
+ function decodeData(data, reviver) {
84
125
  if (!data) {
85
126
  throw new common_1.BadRequestException(constants_1.ERROR_INVALID_MESSAGE_FORMAT);
86
127
  }
@@ -95,7 +136,7 @@ function decodeData(data) {
95
136
  }
96
137
  try {
97
138
  if (typeof data === "string") {
98
- return (0, exports.parseJSON)(data);
139
+ return (0, exports.parseJSON)(data, reviver);
99
140
  }
100
141
  return data;
101
142
  }
@@ -20,5 +20,4 @@ export declare class QueueWorkerService {
20
20
  getWorkers<T = any>(meessage: QueueWorkerRawMessage<T>): Worker<T>[];
21
21
  getWorkers<T = any>(meessage: Message<T>): Worker<T>[];
22
22
  getWorkers<T = any>(meessage: QueueWorkerDecodedMessage<T>): Worker<T>[];
23
- private isDecodedMessage;
24
23
  }
@@ -38,7 +38,9 @@ let QueueWorkerService = class QueueWorkerService {
38
38
  _QueueWorkerService__allWorkers.set(this, void 0);
39
39
  }
40
40
  async execute(meessage) {
41
- const decodedMessage = this.isDecodedMessage(meessage) ? meessage : (0, util_1.decodeMessage)(meessage);
41
+ const decodedMessage = (0, util_1.isDecodedMessage)(meessage)
42
+ ? meessage
43
+ : (0, util_1.decodeMessage)(meessage, this.options.extraConfig?.parseReviver);
42
44
  if (this.options.throwModuleError && !decodedMessage.data.name) {
43
45
  throw new common_1.BadRequestException(constants_1.ERROR_QUEUE_WORKER_NAME_NOT_FOUND);
44
46
  }
@@ -53,7 +55,9 @@ let QueueWorkerService = class QueueWorkerService {
53
55
  return results;
54
56
  }
55
57
  getWorkers(meessage) {
56
- const decodedMessage = this.isDecodedMessage(meessage) ? meessage : (0, util_1.decodeMessage)(meessage);
58
+ const decodedMessage = (0, util_1.isDecodedMessage)(meessage)
59
+ ? meessage
60
+ : (0, util_1.decodeMessage)(meessage, this.options.extraConfig?.parseReviver);
57
61
  if (!decodedMessage.data.name) {
58
62
  return [];
59
63
  }
@@ -61,9 +65,6 @@ let QueueWorkerService = class QueueWorkerService {
61
65
  .filter((worker) => decodedMessage.data.name === worker.name)
62
66
  .map((metadata) => new worker_1.Worker(decodedMessage, metadata, this.options));
63
67
  }
64
- isDecodedMessage(message) {
65
- return "raw" in message;
66
- }
67
68
  };
68
69
  exports.QueueWorkerService = QueueWorkerService;
69
70
  _QueueWorkerService__allWorkers = new WeakMap();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anchan828/nest-cloud-run-queue-worker",
3
- "version": "3.1.1",
3
+ "version": "3.1.3",
4
4
  "description": "> TODO: description",
5
5
  "homepage": "https://github.com/anchan828/nest-cloud-run-queue/tree/master/packages/worker#readme",
6
6
  "bugs": {
@@ -33,7 +33,7 @@
33
33
  "watch": "tsc -w"
34
34
  },
35
35
  "dependencies": {
36
- "@anchan828/nest-cloud-run-queue-common": "^3.1.1"
36
+ "@anchan828/nest-cloud-run-queue-common": "^3.1.3"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@nestjs/common": "10.3.10",
@@ -46,5 +46,5 @@
46
46
  "access": "public"
47
47
  },
48
48
  "packageManager": "npm@10.8.2",
49
- "gitHead": "aa316926176b40816565704e5372ceb0d3938543"
49
+ "gitHead": "c33cfa67028e79e9bf101108d7392e73ba26edca"
50
50
  }