@stacks/api-toolkit 1.12.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.
Files changed (85) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +74 -0
  3. package/bin/api-toolkit-git-info.js +25 -0
  4. package/dist/fastify/cache.d.ts +31 -0
  5. package/dist/fastify/cache.js +63 -0
  6. package/dist/fastify/cache.js.map +1 -0
  7. package/dist/fastify/fastify.d.ts +16 -0
  8. package/dist/fastify/fastify.js +46 -0
  9. package/dist/fastify/fastify.js.map +1 -0
  10. package/dist/fastify/index.d.ts +4 -0
  11. package/dist/fastify/index.js +21 -0
  12. package/dist/fastify/index.js.map +1 -0
  13. package/dist/fastify/openapi.d.ts +13 -0
  14. package/dist/fastify/openapi.js +23 -0
  15. package/dist/fastify/openapi.js.map +1 -0
  16. package/dist/fastify/schemas.d.ts +9 -0
  17. package/dist/fastify/schemas.js +16 -0
  18. package/dist/fastify/schemas.js.map +1 -0
  19. package/dist/helpers/events.d.ts +52 -0
  20. package/dist/helpers/events.js +93 -0
  21. package/dist/helpers/events.js.map +1 -0
  22. package/dist/helpers/index.d.ts +7 -0
  23. package/dist/helpers/index.js +25 -0
  24. package/dist/helpers/index.js.map +1 -0
  25. package/dist/helpers/is-debugging.d.ts +1 -0
  26. package/dist/helpers/is-debugging.js +15 -0
  27. package/dist/helpers/is-debugging.js.map +1 -0
  28. package/dist/helpers/iterators.d.ts +27 -0
  29. package/dist/helpers/iterators.js +74 -0
  30. package/dist/helpers/iterators.js.map +1 -0
  31. package/dist/helpers/serialize-error.d.ts +20 -0
  32. package/dist/helpers/serialize-error.js +135 -0
  33. package/dist/helpers/serialize-error.js.map +1 -0
  34. package/dist/helpers/time.d.ts +54 -0
  35. package/dist/helpers/time.js +121 -0
  36. package/dist/helpers/time.js.map +1 -0
  37. package/dist/helpers/values.d.ts +68 -0
  38. package/dist/helpers/values.js +165 -0
  39. package/dist/helpers/values.js.map +1 -0
  40. package/dist/helpers/worker-thread-init.d.ts +1 -0
  41. package/dist/helpers/worker-thread-init.js +67 -0
  42. package/dist/helpers/worker-thread-init.js.map +1 -0
  43. package/dist/helpers/worker-thread-manager.d.ts +53 -0
  44. package/dist/helpers/worker-thread-manager.js +148 -0
  45. package/dist/helpers/worker-thread-manager.js.map +1 -0
  46. package/dist/index.d.ts +7 -0
  47. package/dist/index.js +24 -0
  48. package/dist/index.js.map +1 -0
  49. package/dist/logger/index.d.ts +20 -0
  50. package/dist/logger/index.js +14 -0
  51. package/dist/logger/index.js.map +1 -0
  52. package/dist/postgres/base-pg-store.d.ts +68 -0
  53. package/dist/postgres/base-pg-store.js +109 -0
  54. package/dist/postgres/base-pg-store.js.map +1 -0
  55. package/dist/postgres/connection.d.ts +62 -0
  56. package/dist/postgres/connection.js +126 -0
  57. package/dist/postgres/connection.js.map +1 -0
  58. package/dist/postgres/errors.d.ts +5 -0
  59. package/dist/postgres/errors.js +71 -0
  60. package/dist/postgres/errors.js.map +1 -0
  61. package/dist/postgres/index.d.ts +5 -0
  62. package/dist/postgres/index.js +22 -0
  63. package/dist/postgres/index.js.map +1 -0
  64. package/dist/postgres/migrations.d.ts +47 -0
  65. package/dist/postgres/migrations.js +134 -0
  66. package/dist/postgres/migrations.js.map +1 -0
  67. package/dist/postgres/types.d.ts +14 -0
  68. package/dist/postgres/types.js +48 -0
  69. package/dist/postgres/types.js.map +1 -0
  70. package/dist/profiler/index.d.ts +2 -0
  71. package/dist/profiler/index.js +19 -0
  72. package/dist/profiler/index.js.map +1 -0
  73. package/dist/profiler/inspector-util.d.ts +29 -0
  74. package/dist/profiler/inspector-util.js +268 -0
  75. package/dist/profiler/inspector-util.js.map +1 -0
  76. package/dist/profiler/server.d.ts +6 -0
  77. package/dist/profiler/server.js +186 -0
  78. package/dist/profiler/server.js.map +1 -0
  79. package/dist/server-version/index.d.ts +8 -0
  80. package/dist/server-version/index.js +33 -0
  81. package/dist/server-version/index.js.map +1 -0
  82. package/dist/shutdown-handler/index.d.ts +17 -0
  83. package/dist/shutdown-handler/index.js +82 -0
  84. package/dist/shutdown-handler/index.js.map +1 -0
  85. package/package.json +73 -0
@@ -0,0 +1,68 @@
1
+ export declare const isDevEnv: boolean;
2
+ export declare const isTestEnv: boolean;
3
+ export declare const isProdEnv: boolean;
4
+ /**
5
+ * Digests a string value into a SHA256 hash.
6
+ * @param content - String input
7
+ * @returns Hashed value
8
+ */
9
+ export declare function sha256(content: string): string;
10
+ /**
11
+ * Parses a boolean string using conventions from CLI arguments, URL query params, and environmental
12
+ * variables. If the input is defined but empty string then true is returned. If the input is
13
+ * undefined or null than false is returned. For example, if the input comes from a CLI arg like
14
+ * `--enable_thing` or URL query param like `?enable_thing`, then this function expects to receive a
15
+ * defined but empty string, and returns true. Otherwise, it checks or values like `true`, `1`,
16
+ * `on`, `yes` (and the inverses). Throws if an unexpected input value is provided.
17
+ */
18
+ export declare function parseBoolean(val: string | undefined | null): boolean;
19
+ /**
20
+ * Encodes a buffer as a `0x` prefixed lower-case hex string. Returns an empty string if the buffer
21
+ * is zero length.
22
+ */
23
+ export declare function bufferToHex(buff: Buffer, prefix?: boolean): string;
24
+ /**
25
+ * Decodes a `0x` prefixed hex string to a buffer.
26
+ * @param hex - A hex string with a `0x` prefix.
27
+ */
28
+ export declare function hexToBuffer(hex: string): Buffer;
29
+ /**
30
+ * Decodes a hex string to a Buffer, trims the 0x-prefix if exists.
31
+ * If already a buffer, returns the input immediately.
32
+ */
33
+ export declare function coerceToBuffer(hex: string | Buffer | ArrayBufferView): Buffer;
34
+ /**
35
+ * Converts a hex string into a UTF-8 string.
36
+ * @param hex - Hex string
37
+ * @returns UTF-8 string
38
+ */
39
+ export declare function hexToUtf8String(hex: string): string;
40
+ /**
41
+ * Converts a number to a hex string.
42
+ * @param number - Number
43
+ * @param paddingBytes - Padding bytes
44
+ * @returns Hex string
45
+ */
46
+ export declare function numberToHex(number: number, paddingBytes?: number): string;
47
+ /**
48
+ * Checks if a string has `0x` prefix.
49
+ * @param val - Hex string
50
+ * @returns Boolean
51
+ */
52
+ export declare const has0xPrefix: (val: string) => boolean;
53
+ /**
54
+ * Converts a string to an enum value.
55
+ * @param enumType - The enum type
56
+ * @param value - The string value to convert
57
+ * @returns Enum item or undefined
58
+ */
59
+ export declare function toEnumValue<T>(enm: {
60
+ [s: string]: T;
61
+ }, value: string): T | undefined;
62
+ /**
63
+ * Unwraps a value that may be undefined or null.
64
+ * @param val - The value to unwrap
65
+ * @param onNullish - Callback to throw an error if the value is null or undefined
66
+ * @returns The unwrapped value
67
+ */
68
+ export declare function unwrap<T>(val: T | null, onNullish?: () => string): Exclude<T, undefined | null>;
@@ -0,0 +1,165 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.has0xPrefix = exports.isProdEnv = exports.isTestEnv = exports.isDevEnv = void 0;
4
+ exports.sha256 = sha256;
5
+ exports.parseBoolean = parseBoolean;
6
+ exports.bufferToHex = bufferToHex;
7
+ exports.hexToBuffer = hexToBuffer;
8
+ exports.coerceToBuffer = coerceToBuffer;
9
+ exports.hexToUtf8String = hexToUtf8String;
10
+ exports.numberToHex = numberToHex;
11
+ exports.toEnumValue = toEnumValue;
12
+ exports.unwrap = unwrap;
13
+ const node_crypto_1 = require("node:crypto");
14
+ const types_1 = require("node:util/types");
15
+ exports.isDevEnv = process.env.NODE_ENV === 'development';
16
+ exports.isTestEnv = process.env.NODE_ENV === 'test';
17
+ exports.isProdEnv = process.env.NODE_ENV === 'production' ||
18
+ process.env.NODE_ENV === 'prod' ||
19
+ !process.env.NODE_ENV ||
20
+ (!exports.isTestEnv && !exports.isDevEnv);
21
+ /**
22
+ * Digests a string value into a SHA256 hash.
23
+ * @param content - String input
24
+ * @returns Hashed value
25
+ */
26
+ function sha256(content) {
27
+ return (0, node_crypto_1.createHash)('sha256').update(content).digest('hex');
28
+ }
29
+ /**
30
+ * Parses a boolean string using conventions from CLI arguments, URL query params, and environmental
31
+ * variables. If the input is defined but empty string then true is returned. If the input is
32
+ * undefined or null than false is returned. For example, if the input comes from a CLI arg like
33
+ * `--enable_thing` or URL query param like `?enable_thing`, then this function expects to receive a
34
+ * defined but empty string, and returns true. Otherwise, it checks or values like `true`, `1`,
35
+ * `on`, `yes` (and the inverses). Throws if an unexpected input value is provided.
36
+ */
37
+ function parseBoolean(val) {
38
+ if (typeof val === 'undefined' || val === null) {
39
+ return false;
40
+ }
41
+ switch (val.trim().toLowerCase()) {
42
+ case '':
43
+ case 'true':
44
+ case '1':
45
+ case 'on':
46
+ case 'yes':
47
+ return true;
48
+ case 'false':
49
+ case '0':
50
+ case 'off':
51
+ case 'no':
52
+ return false;
53
+ default:
54
+ throw new Error(`Cannot parse boolean`);
55
+ }
56
+ }
57
+ /**
58
+ * Encodes a buffer as a `0x` prefixed lower-case hex string. Returns an empty string if the buffer
59
+ * is zero length.
60
+ */
61
+ function bufferToHex(buff, prefix = true) {
62
+ return buff.length === 0 ? '' : (prefix ? '0x' : '') + buff.toString('hex');
63
+ }
64
+ /**
65
+ * Decodes a `0x` prefixed hex string to a buffer.
66
+ * @param hex - A hex string with a `0x` prefix.
67
+ */
68
+ function hexToBuffer(hex) {
69
+ if (hex.length === 0) {
70
+ return Buffer.alloc(0);
71
+ }
72
+ if (!hex.startsWith('0x')) {
73
+ throw new Error(`Hex string is missing the "0x" prefix`);
74
+ }
75
+ if (hex.length % 2 !== 0) {
76
+ throw new Error(`Hex string is an odd number of digits`);
77
+ }
78
+ return Buffer.from(hex.substring(2), 'hex');
79
+ }
80
+ /**
81
+ * Decodes a hex string to a Buffer, trims the 0x-prefix if exists.
82
+ * If already a buffer, returns the input immediately.
83
+ */
84
+ function coerceToBuffer(hex) {
85
+ if (typeof hex === 'string') {
86
+ if (hex.startsWith('0x')) {
87
+ hex = hex.substring(2);
88
+ }
89
+ if (hex.length % 2 !== 0) {
90
+ throw new Error(`Hex string is an odd number of characters`);
91
+ }
92
+ if (!/^[0-9a-fA-F]*$/.test(hex)) {
93
+ throw new Error(`Hex string contains non-hexadecimal characters`);
94
+ }
95
+ return Buffer.from(hex, 'hex');
96
+ }
97
+ else if (Buffer.isBuffer(hex)) {
98
+ return hex;
99
+ }
100
+ else if ((0, types_1.isArrayBufferView)(hex)) {
101
+ return Buffer.from(hex.buffer, hex.byteOffset, hex.byteLength);
102
+ }
103
+ else {
104
+ throw new Error(`Cannot convert to Buffer, unexpected type: ${hex.constructor.name}`);
105
+ }
106
+ }
107
+ /**
108
+ * Converts a hex string into a UTF-8 string.
109
+ * @param hex - Hex string
110
+ * @returns UTF-8 string
111
+ */
112
+ function hexToUtf8String(hex) {
113
+ const buffer = hexToBuffer(hex);
114
+ return buffer.toString('utf8');
115
+ }
116
+ /**
117
+ * Converts a number to a hex string.
118
+ * @param number - Number
119
+ * @param paddingBytes - Padding bytes
120
+ * @returns Hex string
121
+ */
122
+ function numberToHex(number, paddingBytes = 4) {
123
+ let result = number.toString(16);
124
+ if (result.length % 2 > 0) {
125
+ result = '0' + result;
126
+ }
127
+ if (paddingBytes && result.length / 2 < paddingBytes) {
128
+ result = '00'.repeat(paddingBytes - result.length / 2) + result;
129
+ }
130
+ return '0x' + result;
131
+ }
132
+ /**
133
+ * Checks if a string has `0x` prefix.
134
+ * @param val - Hex string
135
+ * @returns Boolean
136
+ */
137
+ const has0xPrefix = (val) => val.substring(0, 2).toLowerCase() === '0x';
138
+ exports.has0xPrefix = has0xPrefix;
139
+ /**
140
+ * Converts a string to an enum value.
141
+ * @param enumType - The enum type
142
+ * @param value - The string value to convert
143
+ * @returns Enum item or undefined
144
+ */
145
+ function toEnumValue(enm, value) {
146
+ return Object.values(enm).includes(value)
147
+ ? value
148
+ : undefined;
149
+ }
150
+ /**
151
+ * Unwraps a value that may be undefined or null.
152
+ * @param val - The value to unwrap
153
+ * @param onNullish - Callback to throw an error if the value is null or undefined
154
+ * @returns The unwrapped value
155
+ */
156
+ function unwrap(val, onNullish) {
157
+ if (val === undefined) {
158
+ throw new Error(onNullish?.() ?? 'value is undefined');
159
+ }
160
+ if (val === null) {
161
+ throw new Error(onNullish?.() ?? 'value is null');
162
+ }
163
+ return val;
164
+ }
165
+ //# sourceMappingURL=values.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"values.js","sourceRoot":"","sources":["../../src/helpers/values.ts"],"names":[],"mappings":";;;AAgBA,wBAEC;AAUD,oCAmBC;AAMD,kCAEC;AAMD,kCAWC;AAMD,wCAmBC;AAOD,0CAGC;AAQD,kCASC;AAeD,kCAIC;AAQD,wBAQC;AA/JD,6CAAyC;AACzC,2CAAoD;AAEvC,QAAA,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,aAAa,CAAC;AAClD,QAAA,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM,CAAC;AAC5C,QAAA,SAAS,GACpB,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY;IACrC,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM;IAC/B,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ;IACrB,CAAC,CAAC,iBAAS,IAAI,CAAC,gBAAQ,CAAC,CAAC;AAE5B;;;;GAIG;AACH,SAAgB,MAAM,CAAC,OAAe;IACpC,OAAO,IAAA,wBAAU,EAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC5D,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,YAAY,CAAC,GAA8B;IACzD,IAAI,OAAO,GAAG,KAAK,WAAW,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;QAC/C,OAAO,KAAK,CAAC;IACf,CAAC;IACD,QAAQ,GAAG,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,EAAE,CAAC;QACjC,KAAK,EAAE,CAAC;QACR,KAAK,MAAM,CAAC;QACZ,KAAK,GAAG,CAAC;QACT,KAAK,IAAI,CAAC;QACV,KAAK,KAAK;YACR,OAAO,I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QACrD,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,YAAY,GAAG,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC;IAClE,CAAC;IACD,OAAO,IAAI,GAAG,MAAM,CAAC;AACvB,CAAC;AAED;;;;GAIG;AACI,MAAM,WAAW,GAAG,CAAC,GAAW,EAAE,EAAE,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,KAAK,IAAI,CAAC;AAA1E,QAAA,WAAW,eAA+D;AAEvF;;;;;GAKG;AACH,SAAgB,WAAW,CAAI,GAAuB,EAAE,KAAa;IACnE,OAAQ,MAAM,CAAC,MAAM,CAAC,GAAG,CAAyB,CAAC,QAAQ,CAAC,KAAK,CAAC;QAChE,CAAC,CAAE,KAAsB;QACzB,CAAC,CAAC,SAAS,CAAC;AAChB,CAAC;AAED;;;;;GAKG;AACH,SAAgB,MAAM,CAAI,GAAa,EAAE,SAAwB;IAC/D,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACtB,MAAM,IAAI,KAAK,CAAC,SAAS,EAAE,EAAE,IAAI,oBAAoB,CAAC,CAAC;IACzD,CAAC;IACD,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;QACjB,MAAM,IAAI,KAAK,CAAC,SAAS,EAAE,EAAE,IAAI,eAAe,CAAC,CAAC;IACpD,CAAC;IACD,OAAO,GAAmC,CAAC;AAC7C,CAAC"}
@@ -0,0 +1 @@
1
+ export declare const filename: string;
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.filename = void 0;
4
+ const WorkerThreads = require("node:worker_threads");
5
+ const serialize_error_1 = require("./serialize-error");
6
+ // Minimal worker thread initialization code. This file is the entry point for worker threads
7
+ // and is responsible for setting up the worker environment and handling messages from the main thread.
8
+ // Imports should be kept to a minimum to avoid in worker thread init overhead and memory usage.
9
+ exports.filename = __filename;
10
+ /**
11
+ * Invokes a function that may return a value or a promise, and passes the result
12
+ * to a callback in a consistent format. Handles both synchronous and asynchronous cases,
13
+ * ensuring type safety and avoiding unnecessary async transitions for sync functions.
14
+ */
15
+ function getMaybePromiseResult(fn, cb) {
16
+ try {
17
+ const maybePromise = fn();
18
+ if (maybePromise instanceof Promise) {
19
+ maybePromise.then(ok => cb({ ok }), (err) => cb({ err }));
20
+ }
21
+ else {
22
+ cb({ ok: maybePromise });
23
+ }
24
+ }
25
+ catch (err) {
26
+ cb({ err });
27
+ }
28
+ }
29
+ // Check if this file is being run in a worker thread. If so, it will set up the worker environment.
30
+ if (!WorkerThreads.isMainThread && WorkerThreads.workerData?.workerFile) {
31
+ const { workerFile } = WorkerThreads.workerData;
32
+ // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
33
+ const workerModule = require(workerFile);
34
+ const parentPort = WorkerThreads.parentPort;
35
+ // Determine if the worker module `processTask` function is a default export or a named export.
36
+ const processTask = 'default' in workerModule ? workerModule.default.processTask : workerModule.processTask;
37
+ parentPort.on('messageerror', err => {
38
+ console.error(`Worker thread message error`, err);
39
+ });
40
+ parentPort.on('message', (message) => {
41
+ const msg = message;
42
+ getMaybePromiseResult(() => processTask(...msg.req), result => {
43
+ try {
44
+ let reply;
45
+ if (result.ok) {
46
+ reply = {
47
+ msgId: msg.msgId,
48
+ resp: result.ok,
49
+ };
50
+ }
51
+ else {
52
+ const error = (0, serialize_error_1.isErrorLike)(result.err) ? (0, serialize_error_1.serializeError)(result.err) : result.err;
53
+ reply = {
54
+ msgId: msg.msgId,
55
+ error,
56
+ };
57
+ }
58
+ parentPort.postMessage(reply);
59
+ }
60
+ catch (err) {
61
+ console.error(`Critical bug in work task processing`, err);
62
+ }
63
+ });
64
+ });
65
+ parentPort.postMessage('ready');
66
+ }
67
+ //# sourceMappingURL=worker-thread-init.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker-thread-init.js","sourceRoot":"","sources":["../../src/helpers/worker-th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,CAAC;IACpD,CAAC,CAAC,CAAC;IACH,UAAU,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,OAAgB,EAAE,EAAE;QAC5C,MAAM,GAAG,GAAG,OAAkC,CAAC;QAC/C,qBAAqB,CACnB,GAAG,EAAE,CAAC,WAAW,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,EAC7B,MAAM,CAAC,EAAE;YACP,IAAI,CAAC;gBACH,IAAI,KAAsC,CAAC;gBAC3C,IAAI,MAAM,CAAC,EAAE,EAAE,CAAC;oBACd,KAAK,GAAG;wBACN,KAAK,EAAE,GAAG,CAAC,KAAK;wBAChB,IAAI,EAAE,MAAM,CAAC,EAAE;qBAChB,CAAC;gBACJ,CAAC;qBAAM,CAAC;oBACN,MAAM,KAAK,GAAG,IAAA,6BAAW,EAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAA,gCAAc,EAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC;oBAChF,KAAK,GAAG;wBACN,KAAK,EAAE,GAAG,CAAC,KAAK;wBAChB,KAAK;qBACN,CAAC;gBACJ,CAAC;gBACD,UAAU,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;YAChC,CAAC;YAAC,OAAO,GAAY,EAAE,CAAC;gBACtB,OAAO,CAAC,KAAK,CAAC,sCAAsC,EAAE,GAAG,CAAC,CAAC;YAC7D,CAAC;QACH,CAAC,CACF,CAAC;IACJ,CAAC,CAAC,CAAC;IACH,UAAU,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;AAClC,CAAC"}
@@ -0,0 +1,53 @@
1
+ import { EventEmitter } from 'node:events';
2
+ export type WorkerDataInterface = {
3
+ workerFile: string;
4
+ };
5
+ export type WorkerReqMsg<TArgs extends unknown[]> = {
6
+ msgId: number;
7
+ req: TArgs;
8
+ };
9
+ export type WorkerRespMsg<TResp, TErr> = {
10
+ msgId: number;
11
+ } & ({
12
+ resp: TResp;
13
+ error?: null;
14
+ } | {
15
+ resp?: null;
16
+ error: TErr;
17
+ });
18
+ export type WorkerPoolModuleInterface<TArgs extends unknown[], TResp> = {
19
+ workerModule: NodeJS.Module;
20
+ processTask: (...args: TArgs) => Promise<TResp> | TResp;
21
+ } | {
22
+ default: {
23
+ workerModule: NodeJS.Module;
24
+ processTask: (...args: TArgs) => Promise<TResp> | TResp;
25
+ };
26
+ };
27
+ export declare class WorkerThreadManager<TArgs extends unknown[], TResp> {
28
+ private readonly workers;
29
+ private readonly idleWorkers;
30
+ private readonly jobQueue;
31
+ private readonly msgRequests;
32
+ private lastMsgId;
33
+ readonly workerCount: number;
34
+ readonly workerFile: string;
35
+ private readonly abortControlller;
36
+ readonly events: EventEmitter<{
37
+ workersReady: [];
38
+ }>;
39
+ get idleWorkerCount(): number;
40
+ get busyWorkerCount(): number;
41
+ get queuedJobCount(): number;
42
+ static init<TArgs extends unknown[], TResp>(workerModule: WorkerPoolModuleInterface<TArgs, TResp>, opts?: {
43
+ workerCount?: number;
44
+ }): Promise<WorkerThreadManager<TArgs, TResp>>;
45
+ constructor(workerModule: WorkerPoolModuleInterface<TArgs, TResp>, opts?: {
46
+ workerCount?: number;
47
+ });
48
+ exec(...args: TArgs): Promise<TResp>;
49
+ createWorkerPool(): void;
50
+ private setupWorkerHandler;
51
+ private assignJobs;
52
+ close(): Promise<void>;
53
+ }
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WorkerThreadManager = void 0;
4
+ const WorkerThreads = require("node:worker_threads");
5
+ const os = require("node:os");
6
+ const node_events_1 = require("node:events");
7
+ const time_1 = require("./time");
8
+ const serialize_error_1 = require("./serialize-error");
9
+ const worker_thread_init_1 = require("./worker-thread-init");
10
+ class WorkerThreadManager {
11
+ workers = new Set();
12
+ idleWorkers = [];
13
+ jobQueue = [];
14
+ msgRequests = new Map();
15
+ lastMsgId = 0;
16
+ workerCount;
17
+ workerFile;
18
+ abortControlller = new AbortController();
19
+ events = new node_events_1.EventEmitter();
20
+ get idleWorkerCount() {
21
+ return this.idleWorkers.length;
22
+ }
23
+ get busyWorkerCount() {
24
+ return this.workerCount - this.idleWorkers.length;
25
+ }
26
+ get queuedJobCount() {
27
+ return this.jobQueue.length;
28
+ }
29
+ static init(workerModule, opts = {}) {
30
+ const workerManager = new WorkerThreadManager(workerModule, opts);
31
+ return new Promise(resolve => {
32
+ workerManager.events.once('workersReady', () => {
33
+ resolve(workerManager);
34
+ });
35
+ });
36
+ }
37
+ constructor(workerModule, opts = {}) {
38
+ if (!WorkerThreads.isMainThread) {
39
+ throw new Error(`${this.constructor.name} must be instantiated in the main thread`);
40
+ }
41
+ if ('default' in workerModule) {
42
+ this.workerFile = workerModule.default.workerModule.filename;
43
+ }
44
+ else {
45
+ this.workerFile = workerModule.workerModule.filename;
46
+ }
47
+ this.workerCount = opts.workerCount ?? os.cpus().length;
48
+ this.createWorkerPool();
49
+ }
50
+ exec(...args) {
51
+ this.abortControlller.signal.throwIfAborted();
52
+ if (this.lastMsgId >= Number.MAX_SAFE_INTEGER) {
53
+ this.lastMsgId = 0;
54
+ }
55
+ const msgId = this.lastMsgId++;
56
+ const replyWaiter = (0, time_1.waiter)();
57
+ this.msgRequests.set(msgId, replyWaiter);
58
+ const reqMsg = {
59
+ msgId,
60
+ req: args,
61
+ };
62
+ this.jobQueue.push(reqMsg);
63
+ this.assignJobs();
64
+ return replyWaiter;
65
+ }
66
+ createWorkerPool() {
67
+ let workersReady = 0;
68
+ for (let i = 0; i < this.workerCount; i++) {
69
+ const workerData = {
70
+ workerFile: this.workerFile,
71
+ };
72
+ const workerOpt = {
73
+ workerData,
74
+ };
75
+ const hasTsSource = worker_thread_init_1.filename.endsWith('.ts') || this.workerFile.endsWith('.ts');
76
+ if (hasTsSource) {
77
+ if (process.env.NODE_ENV !== 'test') {
78
+ throw new Error('Worker threads are being created with ts-node outside of a test environment');
79
+ }
80
+ workerOpt.execArgv = ['-r', 'ts-node/register/transpile-only'];
81
+ }
82
+ const worker = new WorkerThreads.Worker(worker_thread_init_1.filename, workerOpt);
83
+ worker.unref();
84
+ this.workers.add(worker);
85
+ worker.on('error', err => {
86
+ console.error(`Worker error`, err);
87
+ });
88
+ worker.on('messageerror', err => {
89
+ console.error(`Worker message error`, err);
90
+ });
91
+ worker.once('message', (message) => {
92
+ if (message !== 'ready') {
93
+ throw new Error(`Unexpected first msg from worker thread: ${JSON.stringify(message)}`);
94
+ }
95
+ this.setupWorkerHandler(worker);
96
+ this.idleWorkers.push(worker);
97
+ this.assignJobs();
98
+ workersReady++;
99
+ if (workersReady === this.workerCount) {
100
+ this.events.emit('workersReady');
101
+ }
102
+ });
103
+ }
104
+ (0, node_events_1.addAbortListener)(this.abortControlller.signal, () => {
105
+ for (const replyWaiter of this.msgRequests.values()) {
106
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
107
+ replyWaiter.reject(this.abortControlller.signal.reason);
108
+ }
109
+ this.msgRequests.clear();
110
+ });
111
+ }
112
+ setupWorkerHandler(worker) {
113
+ worker.on('message', (message) => {
114
+ this.idleWorkers.push(worker);
115
+ this.assignJobs();
116
+ const msg = message;
117
+ const replyWaiter = this.msgRequests.get(msg.msgId);
118
+ if (replyWaiter) {
119
+ if (msg.error) {
120
+ const error = (0, serialize_error_1.isErrorLike)(msg.error) ? (0, serialize_error_1.deserializeError)(msg.error) : msg.error;
121
+ replyWaiter.reject(error);
122
+ }
123
+ else if (msg.resp) {
124
+ replyWaiter.resolve(msg.resp);
125
+ }
126
+ this.msgRequests.delete(msg.msgId);
127
+ }
128
+ else {
129
+ console.error('Received unexpected message from worker', msg);
130
+ }
131
+ });
132
+ }
133
+ assignJobs() {
134
+ while (this.idleWorkers.length > 0 && this.jobQueue.length > 0) {
135
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
136
+ const worker = this.idleWorkers.shift();
137
+ const job = this.jobQueue.shift();
138
+ worker.postMessage(job);
139
+ }
140
+ }
141
+ async close() {
142
+ this.abortControlller.abort();
143
+ await Promise.all([...this.workers].map(worker => worker.terminate()));
144
+ this.workers.clear();
145
+ }
146
+ }
147
+ exports.WorkerThreadManager = WorkerThreadManager;
148
+ //# sourceMappingURL=worker-thread-manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker-thread-manager.js","sourceRoot":"","sources":["../../src/helpers/worker-thread-manager.ts"],"names":[],"mappings":";;;AAAA,qDAAqD;AACrD,8BAA8B;AAC9B,6CAA6D;AAC7D,iCAAwC;AACxC,uDAAkE;AAClE,6DAA4E;AAoC5E,MAAa,mBAAmB;IACb,OAAO,GAAG,IAAI,GAAG,EAAwB,CAAC;IAC1C,WAAW,GAA2B,EAAE,CAAC;IAEzC,QAAQ,GAA0B,EAAE,CAAC;IACrC,WAAW,GAA+B,IAAI,GAAG,EAAE,CAAC;IAC7D,SAAS,GAAG,CAAC,CAAC;IAEb,WAAW,CAAS;IACpB,UAAU,CAAS;IAEX,gBAAgB,GAAG,IAAI,eAAe,EAAE,CAAC;IAEjD,MAAM,GAAG,IAAI,0BAAY,EAE9B,CAAC;IAEL,IAAI,eAAe;QACjB,OAAO,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC;IACjC,CAAC;IAED,IAAI,eAAe;QACjB,OAAO,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC;IACpD,CAAC;IAED,IAAI,cAAc;QAChB,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;IAC9B,CAAC;IAEM,MAAM,CAAC,IAAI,CAChB,YAAqD,EACrD,OAAiC,EAAE;QAEnC,MAAM,aAAa,GAAG,IAAI,mBAAmB,CAAC,YAAY,EAAE,IAAI,CAAC,CAAC;QAClE,OAAO,IAAI,OAAO,CAAoC,OAAO,CAAC,EAAE;YAC9D,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,GAAG,EAAE;gBAC7C,OAAO,CAAC,aAAa,CAAC,CAAC;YACzB,C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
5
+ export * from './profiler';
6
+ export * from './server-version';
7
+ export * from './shutdown-handler';
package/dist/index.js ADDED
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./fastify"), exports);
18
+ __exportStar(require("./helpers"), exports);
19
+ __exportStar(require("./logger"), exports);
20
+ __exportStar(require("./postgres"), exports);
21
+ __exportStar(require("./profiler"), exports);
22
+ __exportStar(require("./server-version"), exports);
23
+ __exportStar(require("./shutdown-handler"), exports);
24
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAA0B;AAC1B,4CAA0B;AAC1B,2CAAyB;AACzB,6CAA2B;AAC3B,6CAA2B;AAC3B,mDAAiC;AACjC,qDAAmC"}
@@ -0,0 +1,20 @@
1
+ export declare const PINO_LOGGER_CONFIG: {
2
+ name: string;
3
+ level: string;
4
+ timestamp: () => string;
5
+ formatters: {
6
+ level: (label: string, number: number) => {
7
+ level: string;
8
+ };
9
+ };
10
+ };
11
+ export declare const logger: import("pino").Logger<{
12
+ name: string;
13
+ level: string;
14
+ timestamp: () => string;
15
+ formatters: {
16
+ level: (label: string, number: number) => {
17
+ level: string;
18
+ };
19
+ };
20
+ }>;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.logger = exports.PINO_LOGGER_CONFIG = void 0;
4
+ const pino_1 = require("pino");
5
+ exports.PINO_LOGGER_CONFIG = {
6
+ name: process.env.APPLICATION_NAME ?? 'api',
7
+ level: process.env.LOG_LEVEL ?? (process.env.NODE_ENV === 'production' ? 'info' : 'debug'),
8
+ timestamp: pino_1.default.stdTimeFunctions.isoTime,
9
+ formatters: {
10
+ level: (label, number) => ({ level: label }),
11
+ },
12
+ };
13
+ exports.logger = (0, pino_1.default)(exports.PINO_LOGGER_CONFIG);
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/logger/index.ts"],"names":[],"mappings":";;;AAAA,+BAAwB;AAEX,QAAA,kBAAkB,GAAG;IAChC,IAAI,EAAE,OAAO,CAAC,GAAG,CAAC,gBAAgB,IAAI,KAAK;IAC3C,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC;IAC1F,SAAS,EAAE,cAAI,CAAC,gBAAgB,CAAC,OAAO;IACxC,UAAU,EAAE;QACV,KAAK,EAAE,CAAC,KAAa,EAAE,MAAc,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;KAC7D;CACF,CAAC;AACW,QAAA,MAAM,GAAG,IAAA,cAAI,EAAC,0BAAkB,CAAC,CAAC"}