@autofleet/rabbit 4.1.1 → 5.0.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/README.md +8 -0
- package/dist/chunk-CUT6urMc.cjs +30 -0
- package/dist/index-B6pk4Ot8.d.cts +216 -0
- package/dist/index-K1ijaudV.d.ts +216 -0
- package/dist/index.cjs +1119 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -132
- package/dist/index.js +1111 -1112
- package/dist/index.js.map +1 -1
- package/dist/mock/index.cjs +20 -0
- package/dist/mock/index.cjs.map +1 -0
- package/dist/mock/index.d.cts +18 -0
- package/dist/mock/index.d.ts +16 -12
- package/dist/mock/index.js +18 -18
- package/dist/mock/index.js.map +1 -1
- package/dist/mock/vitest.cjs +21 -0
- package/dist/mock/vitest.cjs.map +1 -0
- package/dist/mock/vitest.d.cts +18 -0
- package/dist/mock/vitest.d.ts +16 -11
- package/dist/mock/vitest.js +18 -17
- package/dist/mock/vitest.js.map +1 -1
- package/package.json +48 -9
- package/.nvmrc +0 -1
- package/dist/lib/celery.d.ts +0 -9
- package/dist/lib/celery.js +0 -54
- package/dist/lib/celery.js.map +0 -1
- package/dist/lib/consts.d.ts +0 -27
- package/dist/lib/consts.js +0 -31
- package/dist/lib/consts.js.map +0 -1
- package/dist/lib/rabbitError.d.ts +0 -3
- package/dist/lib/rabbitError.js +0 -10
- package/dist/lib/rabbitError.js.map +0 -1
- package/dist/lib/redis.d.ts +0 -8
- package/dist/lib/redis.js +0 -6
- package/dist/lib/redis.js.map +0 -1
- package/dist/lib/types.d.ts +0 -57
- package/dist/lib/types.js +0 -12
- package/dist/lib/types.js.map +0 -1
- package/dist/lib/utils.d.ts +0 -14
- package/dist/lib/utils.js +0 -29
- package/dist/lib/utils.js.map +0 -1
- package/dist/logger.d.ts +0 -2
- package/dist/logger.js +0 -9
- package/dist/logger.js.map +0 -1
- package/src/index.ts +0 -1401
- package/src/lib/celery.ts +0 -89
- package/src/lib/consts.ts +0 -32
- package/src/lib/rabbitError.ts +0 -6
- package/src/lib/redis.ts +0 -11
- package/src/lib/types.ts +0 -77
- package/src/lib/utils.ts +0 -42
- package/src/logger.ts +0 -5
- package/src/mock/index.ts +0 -25
- package/src/mock/vitest.ts +0 -24
- package/src/redis-lock.d.ts +0 -6
- package/tsconfig.build.json +0 -5
- package/tsconfig.json +0 -16
- package/vitest.config.ts +0 -17
package/src/lib/celery.ts
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from 'node:crypto';
|
|
2
|
-
import logger from '../logger';
|
|
3
|
-
// Environment configuration
|
|
4
|
-
const config = {
|
|
5
|
-
host: process.env.RABBITMQ_SERVICE_HOST || 'localhost',
|
|
6
|
-
username: process.env.RABBITMQ_USERNAME || 'guest',
|
|
7
|
-
password: process.env.RABBITMQ_PASSWORD || 'guest',
|
|
8
|
-
} as const;
|
|
9
|
-
|
|
10
|
-
// Type definitions
|
|
11
|
-
interface TaskData {
|
|
12
|
-
[key: string]: any;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
interface TaskMessage {
|
|
16
|
-
task: string;
|
|
17
|
-
id: string;
|
|
18
|
-
args: TaskData[];
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
interface PublishPayload {
|
|
22
|
-
properties: {
|
|
23
|
-
// eslint-disable-next-line camelcase
|
|
24
|
-
delivery_mode: number;
|
|
25
|
-
// eslint-disable-next-line camelcase
|
|
26
|
-
content_type: string;
|
|
27
|
-
};
|
|
28
|
-
// eslint-disable-next-line camelcase
|
|
29
|
-
routing_key: string;
|
|
30
|
-
payload: string;
|
|
31
|
-
// eslint-disable-next-line camelcase
|
|
32
|
-
payload_encoding: string;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
interface PublishResponse {
|
|
36
|
-
routed: boolean;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
interface SendTaskOptions {
|
|
40
|
-
taskName: string;
|
|
41
|
-
queueName: string;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
async function sendCeleryTaskViaHttp(
|
|
45
|
-
data: TaskData,
|
|
46
|
-
{ taskName, queueName }: SendTaskOptions,
|
|
47
|
-
): Promise<void> {
|
|
48
|
-
const apiUrl = `http://${config.host}:15672/api/exchanges/%2f/amq.default/publish`;
|
|
49
|
-
|
|
50
|
-
const message: TaskMessage = {
|
|
51
|
-
task: taskName,
|
|
52
|
-
id: randomUUID(),
|
|
53
|
-
args: [data],
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const payload: PublishPayload = {
|
|
57
|
-
properties: {
|
|
58
|
-
delivery_mode: 2,
|
|
59
|
-
content_type: 'application/json',
|
|
60
|
-
},
|
|
61
|
-
routing_key: queueName,
|
|
62
|
-
payload: JSON.stringify(message),
|
|
63
|
-
payload_encoding: 'string',
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
try {
|
|
67
|
-
const response = await fetch(apiUrl, {
|
|
68
|
-
method: 'POST',
|
|
69
|
-
headers: {
|
|
70
|
-
'Content-Type': 'application/json',
|
|
71
|
-
Authorization: `Basic ${Buffer.from(`${config.username}:${config.password}`).toString('base64')}`,
|
|
72
|
-
},
|
|
73
|
-
body: JSON.stringify(payload),
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
if (response.ok) {
|
|
77
|
-
const result: PublishResponse = await response.json();
|
|
78
|
-
logger.info('Successfully published message:', result);
|
|
79
|
-
} else {
|
|
80
|
-
logger.error(`Failed to publish message. Status code: ${response.status}`);
|
|
81
|
-
logger.error(`Response: ${await response.text()}`);
|
|
82
|
-
}
|
|
83
|
-
} catch (error) {
|
|
84
|
-
logger.error('Error sending request:', error instanceof Error ? error.message : String(error));
|
|
85
|
-
throw error;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export { sendCeleryTaskViaHttp, TaskData, SendTaskOptions };
|
package/src/lib/consts.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type { BackoffOptions } from 'exponential-backoff';
|
|
2
|
-
import type { ConsumeOptions } from './types';
|
|
3
|
-
|
|
4
|
-
export const DEFAULT_DEAD_TTL_TWO_DAYS = 60_000 * 60 * 12;
|
|
5
|
-
export const DEFAULT_LOCK_TIMEOUT = 1000 * 5;
|
|
6
|
-
export const RETRY_HEADER = 'x-retry-count';
|
|
7
|
-
export const TRACING_HEADER = 'x-trace-id';
|
|
8
|
-
export const USER_TRACING_HEADER = 'x-af-user-id';
|
|
9
|
-
export const AUTOMATION_ID_HEADER = 'x-af-automation-id';
|
|
10
|
-
export const USER_OBJECT = 'userObject';
|
|
11
|
-
export const DEFAULT_USE_CONSUME_WITH_LOCK = false;
|
|
12
|
-
export const DEFAULT_OPTIONS = {
|
|
13
|
-
limit: 1,
|
|
14
|
-
retries: 1,
|
|
15
|
-
deadMessageTtl: DEFAULT_DEAD_TTL_TWO_DAYS,
|
|
16
|
-
lockTimeout: DEFAULT_LOCK_TIMEOUT,
|
|
17
|
-
useConsumeWithLock: DEFAULT_USE_CONSUME_WITH_LOCK,
|
|
18
|
-
auditContext: null,
|
|
19
|
-
enableRabbitTrace: false,
|
|
20
|
-
} satisfies ConsumeOptions;
|
|
21
|
-
|
|
22
|
-
export const ASSERT_VHOST_EXPONENTIAL_BACKOFF_DEV_ENV_CONFIG = {
|
|
23
|
-
startingDelay: 500,
|
|
24
|
-
timeMultiple: 4,
|
|
25
|
-
numOfAttempts: 5,
|
|
26
|
-
} satisfies BackoffOptions;
|
|
27
|
-
|
|
28
|
-
export const ASSERT_VHOST_EXPONENTIAL_BACKOFF_PROD_CONFIG = {
|
|
29
|
-
startingDelay: 1,
|
|
30
|
-
timeMultiple: 1,
|
|
31
|
-
numOfAttempts: 5,
|
|
32
|
-
} satisfies BackoffOptions;
|
package/src/lib/rabbitError.ts
DELETED
package/src/lib/redis.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { createClient } from 'redis';
|
|
2
|
-
|
|
3
|
-
export type RedisConfig = {
|
|
4
|
-
host: string;
|
|
5
|
-
port: number | undefined;
|
|
6
|
-
prefix?: string;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const getRedisInstance = (config: RedisConfig): ReturnType<typeof createClient> => createClient({ socket: config });
|
|
10
|
-
|
|
11
|
-
export default getRedisInstance;
|
package/src/lib/types.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import type { AmqpConnectionManager } from 'amqp-connection-manager';
|
|
2
|
-
import type { ConsumeMessage, Options, Replies } from 'amqplib';
|
|
3
|
-
|
|
4
|
-
export interface ExchangesCache {
|
|
5
|
-
[key: string]: Replies.AssertExchange | undefined;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export interface QueuesCache {
|
|
9
|
-
[key: string]: Replies.AssertQueue | undefined;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface QueueSetupPromisesDictionary {
|
|
13
|
-
[key: string]: Promise<Replies.AssertQueue> | undefined;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface AssertExchangePromisesDictionary {
|
|
17
|
-
[key: string]: Promise<Replies.AssertExchange> | undefined;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export type CustomMessageHeaders = {
|
|
21
|
-
redisTimestampValidationKey?: string;
|
|
22
|
-
}
|
|
23
|
-
export type ConsumeMessageOrNull = ConsumeMessage | null;
|
|
24
|
-
|
|
25
|
-
export interface ConsumeOptions {
|
|
26
|
-
retries?: number;
|
|
27
|
-
deadMessageTtl?: number;
|
|
28
|
-
messageTtl?: number;
|
|
29
|
-
limit?: number;
|
|
30
|
-
lockTimeout?: number;
|
|
31
|
-
useConsumeWithLock?: boolean;
|
|
32
|
-
auditContext?: any;
|
|
33
|
-
enableRabbitTrace?: boolean;
|
|
34
|
-
// TODO: [QUORUM-PHASE-3] Redundant because all the queues are quorum queues
|
|
35
|
-
isQuorumQueue?: boolean;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface NackOptions {
|
|
39
|
-
skipRetry?: boolean;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export type Message = Omit<ConsumeMessage, 'content'> & { content: any; };
|
|
43
|
-
|
|
44
|
-
export type CallbackFunction = (msg: Message, ack: () => Promise<void>, nack: (ignored: ConsumeMessageOrNull, nackOptions?: NackOptions) => Promise<void>) => Promise<void>;
|
|
45
|
-
|
|
46
|
-
export type newChannelOpts = {
|
|
47
|
-
name?: string;
|
|
48
|
-
onClose?: null | ((args: any | null) => void);
|
|
49
|
-
};
|
|
50
|
-
export type assertChannelOpts = {
|
|
51
|
-
channelName?: string;
|
|
52
|
-
force?: boolean;
|
|
53
|
-
}
|
|
54
|
-
export type AfConsumer = {
|
|
55
|
-
queue: string;
|
|
56
|
-
callback: CallbackFunction;
|
|
57
|
-
options: ConsumeOptions | undefined;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const HA_PROMOTE_ON_FAILURE = 'ha-promote-on-failure';
|
|
61
|
-
|
|
62
|
-
const HA_PROMOTE_ON_SHUTDOWN = 'ha-promote-on-shutdown';
|
|
63
|
-
|
|
64
|
-
export const CONSUMER_DEFAULT_OPTIONS: Options.Consume = {
|
|
65
|
-
arguments: {
|
|
66
|
-
[HA_PROMOTE_ON_FAILURE]: 'always',
|
|
67
|
-
[HA_PROMOTE_ON_SHUTDOWN]: 'always',
|
|
68
|
-
},
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
export type ConnectionData = {
|
|
72
|
-
amqpConnection: AmqpConnectionManager | null;
|
|
73
|
-
creatingConnection: boolean;
|
|
74
|
-
connectionCreatedEventName: string;
|
|
75
|
-
connectionFailedEventName: string;
|
|
76
|
-
blockReconnect: boolean;
|
|
77
|
-
};
|
package/src/lib/utils.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import type { ConfirmChannel, Replies } from 'amqplib';
|
|
2
|
-
import type { ChannelWrapper } from 'amqp-connection-manager';
|
|
3
|
-
import type { BackoffOptions } from 'exponential-backoff';
|
|
4
|
-
import { ASSERT_VHOST_EXPONENTIAL_BACKOFF_PROD_CONFIG, ASSERT_VHOST_EXPONENTIAL_BACKOFF_DEV_ENV_CONFIG } from './consts';
|
|
5
|
-
|
|
6
|
-
const { PROJECT_ID } = process.env;
|
|
7
|
-
|
|
8
|
-
export const assertExchangeFanout = async (
|
|
9
|
-
c: ChannelWrapper | ConfirmChannel,
|
|
10
|
-
exchangeName: string,
|
|
11
|
-
): Promise<Replies.AssertExchange> => c.assertExchange(exchangeName, 'fanout');
|
|
12
|
-
|
|
13
|
-
export const rand = (): number => Math.floor(Math.random() * 100_000);
|
|
14
|
-
|
|
15
|
-
interface PromiseWithResolvers<T> {
|
|
16
|
-
promise: Promise<T>;
|
|
17
|
-
resolve: (value: T | PromiseLike<T>) => void;
|
|
18
|
-
reject: (reason?: any) => void;
|
|
19
|
-
}
|
|
20
|
-
interface PromiseCls extends PromiseConstructor {
|
|
21
|
-
withResolvers<T>(): PromiseWithResolvers<T>;
|
|
22
|
-
}
|
|
23
|
-
/** This is polyfill for `Promise.withResolvers` which exists only on Node v22 and onwards */
|
|
24
|
-
export const createDeferredPromise = <T = void>(): PromiseWithResolvers<T> => {
|
|
25
|
-
if ((Promise as PromiseCls).withResolvers) {
|
|
26
|
-
return (Promise as PromiseCls).withResolvers<T>();
|
|
27
|
-
}
|
|
28
|
-
let resolve!: PromiseWithResolvers<T>['resolve'];
|
|
29
|
-
let reject!: PromiseWithResolvers<T>['reject'];
|
|
30
|
-
const promise = new Promise<T>((res, rej) => {
|
|
31
|
-
resolve = res;
|
|
32
|
-
reject = rej;
|
|
33
|
-
});
|
|
34
|
-
return { promise, resolve, reject };
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
const isDevEnv = (): boolean => ['af-experiment-manager', 'dev1-experiment-manager'].includes(PROJECT_ID || '');
|
|
38
|
-
|
|
39
|
-
export const getAssertVhostExponentialBackoffConfig = (): BackoffOptions => (
|
|
40
|
-
isDevEnv()
|
|
41
|
-
? ASSERT_VHOST_EXPONENTIAL_BACKOFF_DEV_ENV_CONFIG
|
|
42
|
-
: ASSERT_VHOST_EXPONENTIAL_BACKOFF_PROD_CONFIG);
|
package/src/logger.ts
DELETED
package/src/mock/index.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
// eslint-disable-next-line import/no-unresolved
|
|
2
|
-
import 'jest';
|
|
3
|
-
import type { IAfRabbitMq } from '../index';
|
|
4
|
-
|
|
5
|
-
class RabbitMq implements IAfRabbitMq {
|
|
6
|
-
ack: any = jest.fn();
|
|
7
|
-
|
|
8
|
-
nack: any = jest.fn();
|
|
9
|
-
|
|
10
|
-
assertChannel: any = jest.fn();
|
|
11
|
-
|
|
12
|
-
assertExchange: any = jest.fn();
|
|
13
|
-
|
|
14
|
-
assertQueue: any = jest.fn();
|
|
15
|
-
|
|
16
|
-
consume: any = jest.fn();
|
|
17
|
-
|
|
18
|
-
consumeFromExchange: any = jest.fn();
|
|
19
|
-
|
|
20
|
-
publish: any = jest.fn();
|
|
21
|
-
|
|
22
|
-
sendToQueue: any = jest.fn();
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export default RabbitMq;
|
package/src/mock/vitest.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { vi } from 'vitest';
|
|
2
|
-
import { IAfRabbitMq } from '../index';
|
|
3
|
-
|
|
4
|
-
class RabbitMq implements IAfRabbitMq {
|
|
5
|
-
ack = vi.fn();
|
|
6
|
-
|
|
7
|
-
nack = vi.fn();
|
|
8
|
-
|
|
9
|
-
assertChannel = vi.fn();
|
|
10
|
-
|
|
11
|
-
assertExchange = vi.fn();
|
|
12
|
-
|
|
13
|
-
assertQueue = vi.fn();
|
|
14
|
-
|
|
15
|
-
consume = vi.fn();
|
|
16
|
-
|
|
17
|
-
consumeFromExchange = vi.fn();
|
|
18
|
-
|
|
19
|
-
publish = vi.fn();
|
|
20
|
-
|
|
21
|
-
sendToQueue = vi.fn();
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export default RabbitMq;
|
package/src/redis-lock.d.ts
DELETED
package/tsconfig.build.json
DELETED
package/tsconfig.json
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "es2020",
|
|
4
|
-
"module": "commonjs",
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"outDir": "./dist",
|
|
7
|
-
"strict": true,
|
|
8
|
-
"esModuleInterop": true,
|
|
9
|
-
"experimentalDecorators": true,
|
|
10
|
-
"emitDecoratorMetadata": true,
|
|
11
|
-
"skipLibCheck": true,
|
|
12
|
-
"sourceMap": true,
|
|
13
|
-
},
|
|
14
|
-
"exclude": ["node_modules"],
|
|
15
|
-
"include": ["src/", "vitest.config.ts"],
|
|
16
|
-
}
|
package/vitest.config.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
// eslint-disable-next-line import/no-unresolved
|
|
2
|
-
import { defineConfig } from 'vitest/config';
|
|
3
|
-
|
|
4
|
-
export default defineConfig({
|
|
5
|
-
test: {
|
|
6
|
-
coverage: {
|
|
7
|
-
provider: 'v8',
|
|
8
|
-
include: ['src/**/*.ts'],
|
|
9
|
-
exclude: ['src/example.ts', 'src/mock/*.ts'],
|
|
10
|
-
reporter: 'text',
|
|
11
|
-
thresholds: {
|
|
12
|
-
// TO DO: Increase the coverage threshold back to 75 in phase 3
|
|
13
|
-
lines: 65,
|
|
14
|
-
},
|
|
15
|
-
},
|
|
16
|
-
},
|
|
17
|
-
});
|