@flusys/nestjs-shared 7.1.0 → 8.0.0-beta.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/classes/api-controller.class.d.ts +4 -4
- package/classes/api-service.class.d.ts +7 -7
- package/classes/disabled-event-bus.class.d.ts +1 -1
- package/classes/domain-event.publisher.d.ts +1 -1
- package/classes/event-bus.registry.d.ts +1 -1
- package/classes/event-transports/event-transport.registry.d.ts +1 -1
- package/classes/event-transports/index.d.ts +2 -2
- package/classes/event-transports/kafka-transport.optional.d.ts +1 -1
- package/classes/event-transports/memory-transport.d.ts +1 -1
- package/classes/event-transports/rabbitmq-transport.optional.d.ts +1 -1
- package/classes/hybrid-event-bus.class.d.ts +1 -1
- package/classes/index.d.ts +11 -11
- package/constants/index.d.ts +2 -2
- package/decorators/api-response.decorator.d.ts +1 -1
- package/decorators/current-user.decorator.d.ts +1 -1
- package/decorators/index.d.ts +6 -6
- package/decorators/require-permission.decorator.d.ts +1 -1
- package/dtos/filter-and-pagination.dto.d.ts +1 -1
- package/dtos/index.d.ts +6 -6
- package/entities/index.d.ts +3 -3
- package/enums/index.d.ts +3 -3
- package/exceptions/index.d.ts +2 -2
- package/fesm/470.js +2713 -0
- package/fesm/509.js +793 -0
- package/fesm/667.js +606 -0
- package/fesm/67.js +808 -0
- package/fesm/720.js +914 -0
- package/{cjs/classes/event-transports/rabbitmq-transport.optional.js → fesm/chunks/123.js} +78 -55
- package/fesm/{classes/event-transports/kafka-transport.optional.js → chunks/37.js} +74 -41
- package/fesm/classes/index.js +62 -12
- package/fesm/constants/index.js +445 -17
- package/fesm/decorators/index.js +341 -6
- package/fesm/dtos/index.js +23 -6
- package/fesm/entities/index.js +336 -3
- package/fesm/enums/index.js +57 -3
- package/fesm/exceptions/index.js +280 -2
- package/fesm/guards/index.js +1003 -3
- package/fesm/index.js +1524 -14
- package/fesm/interceptors/index.js +924 -5
- package/fesm/interfaces/index.js +57 -11
- package/fesm/middlewares/index.js +33 -1
- package/fesm/modules/index.js +432 -9
- package/fesm/runtime.js +134 -0
- package/fesm/utils/index.js +1045 -8
- package/filters/global-exception.filter.d.ts +1 -1
- package/filters/index.d.ts +1 -1
- package/guards/index.d.ts +3 -3
- package/guards/jwt-auth.guard.d.ts +1 -1
- package/guards/optional-jwt-auth.guard.d.ts +1 -1
- package/guards/permission.guard.d.ts +2 -2
- package/index.d.ts +14 -14
- package/interceptors/delete-empty-id-from-body.interceptor.d.ts +1 -1
- package/interceptors/idempotency.interceptor.d.ts +2 -2
- package/interceptors/index.d.ts +5 -5
- package/interceptors/response-meta.interceptor.d.ts +1 -1
- package/interceptors/set-user-field-on-body.interceptor.d.ts +1 -1
- package/interceptors/slug.interceptor.d.ts +2 -2
- package/interfaces/ai-assistant-adapter.interface.d.ts +1 -1
- package/interfaces/api.interface.d.ts +2 -2
- package/interfaces/datasource.interface.d.ts +1 -1
- package/interfaces/event-manager-adapter.interface.d.ts +2 -2
- package/interfaces/index.d.ts +11 -11
- package/interfaces/module-config.interface.d.ts +1 -1
- package/interfaces/notification-adapter.interface.d.ts +1 -1
- package/middlewares/index.d.ts +1 -1
- package/middlewares/logger.middleware.d.ts +2 -2
- package/modules/cache/cache.module.d.ts +1 -1
- package/modules/datasource/index.d.ts +1 -1
- package/modules/datasource/multi-tenant-datasource.service.d.ts +4 -4
- package/modules/event-bus/event-bus.explorer.d.ts +2 -2
- package/modules/event-bus/event-bus.module.d.ts +2 -2
- package/modules/event-bus/module-events.provider.d.ts +1 -1
- package/modules/index.d.ts +9 -9
- package/modules/shared-permission-cache/shared-permission-cache.service.d.ts +3 -3
- package/modules/utils/utils.service.d.ts +1 -1
- package/package.json +30 -44
- package/utils/index.d.ts +8 -8
- package/utils/permission-cache-key.util.d.ts +1 -1
- package/utils/query-helpers.util.d.ts +2 -2
- package/utils/request.util.d.ts +1 -1
- package/cjs/classes/api-controller.class.js +0 -728
- package/cjs/classes/api-controller.class.spec.js +0 -684
- package/cjs/classes/api-service.class.js +0 -631
- package/cjs/classes/api-service.class.spec.js +0 -1137
- package/cjs/classes/disabled-event-bus.class.js +0 -15
- package/cjs/classes/disabled-event-bus.class.spec.js +0 -40
- package/cjs/classes/domain-event.publisher.js +0 -76
- package/cjs/classes/domain-event.publisher.spec.js +0 -234
- package/cjs/classes/event-bus.registry.js +0 -68
- package/cjs/classes/event-bus.registry.spec.js +0 -141
- package/cjs/classes/event-transports/event-transport.registry.js +0 -41
- package/cjs/classes/event-transports/event-transport.registry.spec.js +0 -31
- package/cjs/classes/event-transports/index.js +0 -23
- package/cjs/classes/event-transports/kafka-transport.optional.js +0 -151
- package/cjs/classes/event-transports/kafka-transport.optional.spec.js +0 -198
- package/cjs/classes/event-transports/memory-transport.js +0 -53
- package/cjs/classes/event-transports/memory-transport.spec.js +0 -57
- package/cjs/classes/event-transports/rabbitmq-transport.optional.spec.js +0 -255
- package/cjs/classes/handle-custome-db.js +0 -37
- package/cjs/classes/handle-custome-db.spec.js +0 -82
- package/cjs/classes/hybrid-cache.class.js +0 -96
- package/cjs/classes/hybrid-cache.class.spec.js +0 -168
- package/cjs/classes/hybrid-event-bus.class.js +0 -281
- package/cjs/classes/hybrid-event-bus.class.spec.js +0 -218
- package/cjs/classes/index.js +0 -28
- package/cjs/classes/winston.logger.class.js +0 -274
- package/cjs/classes/winston.logger.class.spec.js +0 -330
- package/cjs/constants/index.js +0 -88
- package/cjs/constants/message-keys.js +0 -68
- package/cjs/constants/permissions.js +0 -307
- package/cjs/decorators/api-response.decorator.js +0 -52
- package/cjs/decorators/api-response.decorator.spec.js +0 -126
- package/cjs/decorators/current-user.decorator.js +0 -16
- package/cjs/decorators/current-user.decorator.spec.js +0 -74
- package/cjs/decorators/index.js +0 -23
- package/cjs/decorators/log-action.decorator.js +0 -149
- package/cjs/decorators/log-action.decorator.spec.js +0 -353
- package/cjs/decorators/on-domain-event.decorator.js +0 -13
- package/cjs/decorators/on-domain-event.decorator.spec.js +0 -50
- package/cjs/decorators/public.decorator.js +0 -13
- package/cjs/decorators/public.decorator.spec.js +0 -50
- package/cjs/decorators/require-permission.decorator.js +0 -32
- package/cjs/decorators/require-permission.decorator.spec.js +0 -147
- package/cjs/dtos/delete.dto.js +0 -92
- package/cjs/dtos/filter-and-pagination.dto.js +0 -164
- package/cjs/dtos/get-by-ids.dto.js +0 -65
- package/cjs/dtos/identity-response.dto.js +0 -108
- package/cjs/dtos/index.js +0 -23
- package/cjs/dtos/pagination.dto.js +0 -67
- package/cjs/dtos/response-payload.dto.js +0 -369
- package/cjs/entities/identity.js +0 -100
- package/cjs/entities/index.js +0 -20
- package/cjs/entities/raw-type.js +0 -4
- package/cjs/entities/user-root.js +0 -189
- package/cjs/enums/index.js +0 -20
- package/cjs/enums/notification-type.enum.js +0 -17
- package/cjs/enums/participant-status.enum.js +0 -17
- package/cjs/enums/recurrence-type.enum.js +0 -18
- package/cjs/exceptions/base-app.exception.js +0 -151
- package/cjs/exceptions/base-app.exception.spec.js +0 -157
- package/cjs/exceptions/index.js +0 -19
- package/cjs/exceptions/permission.exception.js +0 -56
- package/cjs/exceptions/permission.exception.spec.js +0 -82
- package/cjs/filters/global-exception.filter.js +0 -279
- package/cjs/filters/global-exception.filter.spec.js +0 -440
- package/cjs/filters/index.js +0 -18
- package/cjs/guards/index.js +0 -20
- package/cjs/guards/jwt-auth.guard.js +0 -75
- package/cjs/guards/jwt-auth.guard.spec.js +0 -75
- package/cjs/guards/optional-jwt-auth.guard.js +0 -67
- package/cjs/guards/optional-jwt-auth.guard.spec.js +0 -55
- package/cjs/guards/permission.guard.js +0 -179
- package/cjs/guards/permission.guard.spec.js +0 -373
- package/cjs/index.js +0 -31
- package/cjs/interceptors/delete-empty-id-from-body.interceptor.js +0 -40
- package/cjs/interceptors/delete-empty-id-from-body.interceptor.spec.js +0 -107
- package/cjs/interceptors/idempotency.interceptor.js +0 -96
- package/cjs/interceptors/idempotency.interceptor.spec.js +0 -116
- package/cjs/interceptors/index.js +0 -22
- package/cjs/interceptors/response-meta.interceptor.js +0 -47
- package/cjs/interceptors/response-meta.interceptor.spec.js +0 -89
- package/cjs/interceptors/set-user-field-on-body.interceptor.js +0 -60
- package/cjs/interceptors/set-user-field-on-body.interceptor.spec.js +0 -128
- package/cjs/interceptors/slug.interceptor.js +0 -75
- package/cjs/interceptors/slug.interceptor.spec.js +0 -110
- package/cjs/interfaces/ai-assistant-adapter.interface.js +0 -24
- package/cjs/interfaces/api.interface.js +0 -4
- package/cjs/interfaces/datasource.interface.js +0 -4
- package/cjs/interfaces/event-bus.interface.js +0 -4
- package/cjs/interfaces/event-manager-adapter.interface.js +0 -11
- package/cjs/interfaces/identity.interface.js +0 -4
- package/cjs/interfaces/index.js +0 -28
- package/cjs/interfaces/logged-user-info.interface.js +0 -6
- package/cjs/interfaces/module-config.interface.js +0 -4
- package/cjs/interfaces/notification-adapter.interface.js +0 -11
- package/cjs/interfaces/permission-sync-adapter.interface.js +0 -11
- package/cjs/interfaces/permission.interface.js +0 -4
- package/cjs/middlewares/index.js +0 -18
- package/cjs/middlewares/logger.middleware.js +0 -264
- package/cjs/middlewares/logger.middleware.spec.js +0 -462
- package/cjs/modules/cache/cache.module.js +0 -39
- package/cjs/modules/cache/cache.module.spec.js +0 -48
- package/cjs/modules/datasource/index.js +0 -18
- package/cjs/modules/datasource/multi-tenant-datasource.service.js +0 -222
- package/cjs/modules/datasource/multi-tenant-datasource.service.spec.js +0 -381
- package/cjs/modules/event-bus/event-bus.explorer.js +0 -124
- package/cjs/modules/event-bus/event-bus.explorer.spec.js +0 -189
- package/cjs/modules/event-bus/event-bus.integration.spec.js +0 -253
- package/cjs/modules/event-bus/event-bus.module.js +0 -94
- package/cjs/modules/event-bus/event-bus.module.spec.js +0 -149
- package/cjs/modules/event-bus/module-events.provider.js +0 -39
- package/cjs/modules/event-bus/module-events.provider.spec.js +0 -56
- package/cjs/modules/index.js +0 -26
- package/cjs/modules/shared-permission-cache/shared-permission-cache.module.js +0 -32
- package/cjs/modules/shared-permission-cache/shared-permission-cache.service.js +0 -101
- package/cjs/modules/shared-permission-cache/shared-permission-cache.service.spec.js +0 -165
- package/cjs/modules/utils/utils.module.js +0 -32
- package/cjs/modules/utils/utils.service.js +0 -76
- package/cjs/modules/utils/utils.service.spec.js +0 -169
- package/cjs/utils/date-time.util.js +0 -94
- package/cjs/utils/date-time.util.spec.js +0 -84
- package/cjs/utils/event-pattern.util.js +0 -103
- package/cjs/utils/event-pattern.util.spec.js +0 -251
- package/cjs/utils/html-sanitizer.util.js +0 -64
- package/cjs/utils/html-sanitizer.util.spec.js +0 -76
- package/cjs/utils/index.js +0 -25
- package/cjs/utils/optional-module.util.js +0 -35
- package/cjs/utils/optional-module.util.spec.js +0 -14
- package/cjs/utils/permission-cache-key.util.js +0 -31
- package/cjs/utils/permission-cache-key.util.spec.js +0 -88
- package/cjs/utils/query-helpers.util.js +0 -60
- package/cjs/utils/query-helpers.util.spec.js +0 -209
- package/cjs/utils/request.util.js +0 -96
- package/cjs/utils/request.util.spec.js +0 -249
- package/cjs/utils/string.util.js +0 -63
- package/cjs/utils/string.util.spec.js +0 -63
- package/fesm/classes/api-controller.class.js +0 -718
- package/fesm/classes/api-controller.class.spec.js +0 -680
- package/fesm/classes/api-service.class.js +0 -621
- package/fesm/classes/api-service.class.spec.js +0 -1133
- package/fesm/classes/disabled-event-bus.class.js +0 -11
- package/fesm/classes/disabled-event-bus.class.spec.js +0 -36
- package/fesm/classes/domain-event.publisher.js +0 -63
- package/fesm/classes/domain-event.publisher.spec.js +0 -230
- package/fesm/classes/event-bus.registry.js +0 -64
- package/fesm/classes/event-bus.registry.spec.js +0 -137
- package/fesm/classes/event-transports/event-transport.registry.js +0 -35
- package/fesm/classes/event-transports/event-transport.registry.spec.js +0 -27
- package/fesm/classes/event-transports/index.js +0 -6
- package/fesm/classes/event-transports/kafka-transport.optional.spec.js +0 -194
- package/fesm/classes/event-transports/memory-transport.js +0 -49
- package/fesm/classes/event-transports/memory-transport.spec.js +0 -53
- package/fesm/classes/event-transports/rabbitmq-transport.optional.js +0 -174
- package/fesm/classes/event-transports/rabbitmq-transport.optional.spec.js +0 -251
- package/fesm/classes/handle-custome-db.js +0 -19
- package/fesm/classes/handle-custome-db.spec.js +0 -78
- package/fesm/classes/hybrid-cache.class.js +0 -81
- package/fesm/classes/hybrid-cache.class.spec.js +0 -164
- package/fesm/classes/hybrid-event-bus.class.js +0 -237
- package/fesm/classes/hybrid-event-bus.class.spec.js +0 -214
- package/fesm/classes/winston.logger.class.js +0 -228
- package/fesm/classes/winston.logger.class.spec.js +0 -327
- package/fesm/constants/message-keys.js +0 -49
- package/fesm/constants/permissions.js +0 -215
- package/fesm/decorators/api-response.decorator.js +0 -42
- package/fesm/decorators/api-response.decorator.spec.js +0 -122
- package/fesm/decorators/current-user.decorator.js +0 -18
- package/fesm/decorators/current-user.decorator.spec.js +0 -73
- package/fesm/decorators/log-action.decorator.js +0 -139
- package/fesm/decorators/log-action.decorator.spec.js +0 -349
- package/fesm/decorators/on-domain-event.decorator.js +0 -10
- package/fesm/decorators/on-domain-event.decorator.spec.js +0 -46
- package/fesm/decorators/public.decorator.js +0 -20
- package/fesm/decorators/public.decorator.spec.js +0 -46
- package/fesm/decorators/require-permission.decorator.js +0 -27
- package/fesm/decorators/require-permission.decorator.spec.js +0 -143
- package/fesm/dtos/delete.dto.js +0 -82
- package/fesm/dtos/filter-and-pagination.dto.js +0 -148
- package/fesm/dtos/get-by-ids.dto.js +0 -55
- package/fesm/dtos/identity-response.dto.js +0 -98
- package/fesm/dtos/pagination.dto.js +0 -57
- package/fesm/dtos/response-payload.dto.js +0 -336
- package/fesm/entities/identity.js +0 -90
- package/fesm/entities/raw-type.js +0 -1
- package/fesm/entities/user-root.js +0 -179
- package/fesm/enums/notification-type.enum.js +0 -7
- package/fesm/enums/participant-status.enum.js +0 -7
- package/fesm/enums/recurrence-type.enum.js +0 -8
- package/fesm/exceptions/base-app.exception.js +0 -115
- package/fesm/exceptions/base-app.exception.spec.js +0 -153
- package/fesm/exceptions/permission.exception.js +0 -35
- package/fesm/exceptions/permission.exception.spec.js +0 -78
- package/fesm/filters/global-exception.filter.js +0 -269
- package/fesm/filters/global-exception.filter.spec.js +0 -436
- package/fesm/filters/index.js +0 -1
- package/fesm/guards/jwt-auth.guard.js +0 -65
- package/fesm/guards/jwt-auth.guard.spec.js +0 -71
- package/fesm/guards/optional-jwt-auth.guard.js +0 -57
- package/fesm/guards/optional-jwt-auth.guard.spec.js +0 -51
- package/fesm/guards/permission.guard.js +0 -169
- package/fesm/guards/permission.guard.spec.js +0 -369
- package/fesm/interceptors/delete-empty-id-from-body.interceptor.js +0 -30
- package/fesm/interceptors/delete-empty-id-from-body.interceptor.spec.js +0 -103
- package/fesm/interceptors/idempotency.interceptor.js +0 -86
- package/fesm/interceptors/idempotency.interceptor.spec.js +0 -112
- package/fesm/interceptors/response-meta.interceptor.js +0 -37
- package/fesm/interceptors/response-meta.interceptor.spec.js +0 -85
- package/fesm/interceptors/set-user-field-on-body.interceptor.js +0 -39
- package/fesm/interceptors/set-user-field-on-body.interceptor.spec.js +0 -124
- package/fesm/interceptors/slug.interceptor.js +0 -65
- package/fesm/interceptors/slug.interceptor.spec.js +0 -106
- package/fesm/interfaces/ai-assistant-adapter.interface.js +0 -3
- package/fesm/interfaces/api.interface.js +0 -1
- package/fesm/interfaces/datasource.interface.js +0 -1
- package/fesm/interfaces/event-bus.interface.js +0 -1
- package/fesm/interfaces/event-manager-adapter.interface.js +0 -1
- package/fesm/interfaces/identity.interface.js +0 -1
- package/fesm/interfaces/logged-user-info.interface.js +0 -3
- package/fesm/interfaces/module-config.interface.js +0 -5
- package/fesm/interfaces/notification-adapter.interface.js +0 -1
- package/fesm/interfaces/permission-sync-adapter.interface.js +0 -1
- package/fesm/interfaces/permission.interface.js +0 -1
- package/fesm/middlewares/logger.middleware.js +0 -235
- package/fesm/middlewares/logger.middleware.spec.js +0 -459
- package/fesm/modules/cache/cache.module.js +0 -29
- package/fesm/modules/cache/cache.module.spec.js +0 -44
- package/fesm/modules/datasource/index.js +0 -1
- package/fesm/modules/datasource/multi-tenant-datasource.service.js +0 -212
- package/fesm/modules/datasource/multi-tenant-datasource.service.spec.js +0 -377
- package/fesm/modules/event-bus/event-bus.explorer.js +0 -114
- package/fesm/modules/event-bus/event-bus.explorer.spec.js +0 -185
- package/fesm/modules/event-bus/event-bus.integration.spec.js +0 -249
- package/fesm/modules/event-bus/event-bus.module.js +0 -84
- package/fesm/modules/event-bus/event-bus.module.spec.js +0 -145
- package/fesm/modules/event-bus/module-events.provider.js +0 -26
- package/fesm/modules/event-bus/module-events.provider.spec.js +0 -52
- package/fesm/modules/shared-permission-cache/shared-permission-cache.module.js +0 -22
- package/fesm/modules/shared-permission-cache/shared-permission-cache.service.js +0 -91
- package/fesm/modules/shared-permission-cache/shared-permission-cache.service.spec.js +0 -161
- package/fesm/modules/utils/utils.module.js +0 -22
- package/fesm/modules/utils/utils.service.js +0 -66
- package/fesm/modules/utils/utils.service.spec.js +0 -165
- package/fesm/utils/date-time.util.js +0 -70
- package/fesm/utils/date-time.util.spec.js +0 -80
- package/fesm/utils/event-pattern.util.js +0 -91
- package/fesm/utils/event-pattern.util.spec.js +0 -247
- package/fesm/utils/html-sanitizer.util.js +0 -69
- package/fesm/utils/html-sanitizer.util.spec.js +0 -72
- package/fesm/utils/optional-module.util.js +0 -25
- package/fesm/utils/optional-module.util.spec.js +0 -10
- package/fesm/utils/permission-cache-key.util.js +0 -10
- package/fesm/utils/permission-cache-key.util.spec.js +0 -84
- package/fesm/utils/query-helpers.util.js +0 -85
- package/fesm/utils/query-helpers.util.spec.js +0 -205
- package/fesm/utils/request.util.js +0 -81
- package/fesm/utils/request.util.spec.js +0 -245
- package/fesm/utils/string.util.js +0 -71
- package/fesm/utils/string.util.spec.js +0 -59
|
@@ -1,255 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
const _common = require("@nestjs/common");
|
|
6
|
-
const _rabbitmqtransportoptional = require("./rabbitmq-transport.optional");
|
|
7
|
-
const mockLoadOptionalModule = jest.fn();
|
|
8
|
-
jest.mock('../../utils/optional-module.util', ()=>({
|
|
9
|
-
loadOptionalModule: (...args)=>mockLoadOptionalModule(...args)
|
|
10
|
-
}));
|
|
11
|
-
function buildEvent() {
|
|
12
|
-
return {
|
|
13
|
-
id: 'event-1',
|
|
14
|
-
name: 'storage.folder.created',
|
|
15
|
-
module: 'storage',
|
|
16
|
-
entity: 'folder',
|
|
17
|
-
action: 'created',
|
|
18
|
-
payload: {
|
|
19
|
-
ids: [
|
|
20
|
-
'1'
|
|
21
|
-
]
|
|
22
|
-
},
|
|
23
|
-
actor: {},
|
|
24
|
-
occurredAt: '2026-01-01T00:00:00.000Z',
|
|
25
|
-
version: 1
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
function createChannelMock() {
|
|
29
|
-
return {
|
|
30
|
-
assertExchange: jest.fn().mockResolvedValue(undefined),
|
|
31
|
-
assertQueue: jest.fn().mockResolvedValue(undefined),
|
|
32
|
-
prefetch: jest.fn().mockResolvedValue(undefined),
|
|
33
|
-
bindQueue: jest.fn().mockResolvedValue(undefined),
|
|
34
|
-
consume: jest.fn().mockResolvedValue(undefined),
|
|
35
|
-
publish: jest.fn(),
|
|
36
|
-
ack: jest.fn(),
|
|
37
|
-
nack: jest.fn(),
|
|
38
|
-
on: jest.fn(),
|
|
39
|
-
removeAllListeners: jest.fn(),
|
|
40
|
-
close: jest.fn().mockResolvedValue(undefined)
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
/** A reconnect must land on a fresh channel, so every createChannel yields one */ function createAmqpMock() {
|
|
44
|
-
const channels = [];
|
|
45
|
-
const connection = {
|
|
46
|
-
createChannel: jest.fn(async ()=>{
|
|
47
|
-
const channel = createChannelMock();
|
|
48
|
-
channels.push(channel);
|
|
49
|
-
return channel;
|
|
50
|
-
}),
|
|
51
|
-
on: jest.fn(),
|
|
52
|
-
removeAllListeners: jest.fn(),
|
|
53
|
-
close: jest.fn().mockResolvedValue(undefined)
|
|
54
|
-
};
|
|
55
|
-
return {
|
|
56
|
-
channels,
|
|
57
|
-
connection,
|
|
58
|
-
connect: jest.fn().mockResolvedValue(connection),
|
|
59
|
-
get channel () {
|
|
60
|
-
return channels[channels.length - 1];
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
function listenerFor(target, event) {
|
|
65
|
-
const registered = target.on.mock.calls.find(([name])=>name === event);
|
|
66
|
-
if (!registered) throw new Error(`No '${event}' listener was registered`);
|
|
67
|
-
return registered[1];
|
|
68
|
-
}
|
|
69
|
-
describe('RabbitMqTransport', ()=>{
|
|
70
|
-
let amqp;
|
|
71
|
-
let warnSpy;
|
|
72
|
-
let errorSpy;
|
|
73
|
-
beforeEach(()=>{
|
|
74
|
-
amqp = createAmqpMock();
|
|
75
|
-
mockLoadOptionalModule.mockResolvedValue({
|
|
76
|
-
connect: amqp.connect
|
|
77
|
-
});
|
|
78
|
-
warnSpy = jest.spyOn(_common.Logger.prototype, 'warn').mockImplementation(()=>undefined);
|
|
79
|
-
errorSpy = jest.spyOn(_common.Logger.prototype, 'error').mockImplementation(()=>undefined);
|
|
80
|
-
});
|
|
81
|
-
afterEach(()=>{
|
|
82
|
-
warnSpy.mockRestore();
|
|
83
|
-
errorSpy.mockRestore();
|
|
84
|
-
jest.clearAllMocks();
|
|
85
|
-
});
|
|
86
|
-
it('should throw a descriptive error when amqplib is not installed', async ()=>{
|
|
87
|
-
mockLoadOptionalModule.mockRejectedValue(new Error('Cannot find module'));
|
|
88
|
-
await expect(new _rabbitmqtransportoptional.RabbitMqTransport().connect()).rejects.toThrow(_common.InternalServerErrorException);
|
|
89
|
-
});
|
|
90
|
-
it('should assert a topic exchange and a durable queue on connect', async ()=>{
|
|
91
|
-
const transport = new _rabbitmqtransportoptional.RabbitMqTransport({
|
|
92
|
-
exchange: 'ex',
|
|
93
|
-
queue: 'svc.events'
|
|
94
|
-
});
|
|
95
|
-
await transport.connect();
|
|
96
|
-
expect(amqp.channel.assertExchange).toHaveBeenCalledWith('ex', 'topic', {
|
|
97
|
-
durable: true
|
|
98
|
-
});
|
|
99
|
-
expect(amqp.channel.assertQueue).toHaveBeenCalledWith('svc.events', {
|
|
100
|
-
durable: true
|
|
101
|
-
});
|
|
102
|
-
expect(transport.isReady()).toBe(true);
|
|
103
|
-
});
|
|
104
|
-
it('should publish with the event name as the routing key', async ()=>{
|
|
105
|
-
const transport = new _rabbitmqtransportoptional.RabbitMqTransport({
|
|
106
|
-
exchange: 'ex'
|
|
107
|
-
});
|
|
108
|
-
await transport.connect();
|
|
109
|
-
const event = buildEvent();
|
|
110
|
-
await transport.publish(event);
|
|
111
|
-
expect(amqp.channel.publish).toHaveBeenCalledWith('ex', 'storage.folder.created', expect.any(Buffer), expect.objectContaining({
|
|
112
|
-
messageId: 'event-1',
|
|
113
|
-
contentType: 'application/json'
|
|
114
|
-
}));
|
|
115
|
-
});
|
|
116
|
-
it('should ignore publish before connect instead of throwing', async ()=>{
|
|
117
|
-
await expect(new _rabbitmqtransportoptional.RabbitMqTransport().publish(buildEvent())).resolves.toBeUndefined();
|
|
118
|
-
});
|
|
119
|
-
it('should translate the ** wildcard to the AMQP # wildcard when binding', async ()=>{
|
|
120
|
-
const transport = new _rabbitmqtransportoptional.RabbitMqTransport({
|
|
121
|
-
exchange: 'ex',
|
|
122
|
-
queue: 'q'
|
|
123
|
-
});
|
|
124
|
-
await transport.connect();
|
|
125
|
-
await transport.bind('storage.**');
|
|
126
|
-
expect(amqp.channel.bindQueue).toHaveBeenCalledWith('q', 'ex', 'storage.#');
|
|
127
|
-
});
|
|
128
|
-
it('should start consuming only once across several binds', async ()=>{
|
|
129
|
-
const transport = new _rabbitmqtransportoptional.RabbitMqTransport();
|
|
130
|
-
await transport.connect();
|
|
131
|
-
await transport.bind('storage.#');
|
|
132
|
-
await transport.bind('auth.#');
|
|
133
|
-
expect(amqp.channel.consume).toHaveBeenCalledTimes(1);
|
|
134
|
-
});
|
|
135
|
-
it('should hand a consumed message to its handlers and ack it', async ()=>{
|
|
136
|
-
const transport = new _rabbitmqtransportoptional.RabbitMqTransport();
|
|
137
|
-
const handler = jest.fn();
|
|
138
|
-
transport.onMessage(handler);
|
|
139
|
-
await transport.connect();
|
|
140
|
-
await transport.bind('storage.#');
|
|
141
|
-
const event = buildEvent();
|
|
142
|
-
const message = {
|
|
143
|
-
content: Buffer.from(JSON.stringify(event))
|
|
144
|
-
};
|
|
145
|
-
await amqp.channel.consume.mock.calls[0][1](message);
|
|
146
|
-
expect(handler).toHaveBeenCalledWith(event);
|
|
147
|
-
expect(amqp.channel.ack).toHaveBeenCalledWith(message);
|
|
148
|
-
});
|
|
149
|
-
it('should nack a message it cannot parse', async ()=>{
|
|
150
|
-
const transport = new _rabbitmqtransportoptional.RabbitMqTransport();
|
|
151
|
-
await transport.connect();
|
|
152
|
-
await transport.bind('storage.#');
|
|
153
|
-
const message = {
|
|
154
|
-
content: Buffer.from('not json')
|
|
155
|
-
};
|
|
156
|
-
await amqp.channel.consume.mock.calls[0][1](message);
|
|
157
|
-
expect(amqp.channel.nack).toHaveBeenCalledWith(message, false, false);
|
|
158
|
-
});
|
|
159
|
-
it('should close the channel and the connection', async ()=>{
|
|
160
|
-
const transport = new _rabbitmqtransportoptional.RabbitMqTransport();
|
|
161
|
-
await transport.connect();
|
|
162
|
-
await transport.close();
|
|
163
|
-
expect(amqp.channel.close).toHaveBeenCalled();
|
|
164
|
-
expect(amqp.connection.close).toHaveBeenCalled();
|
|
165
|
-
expect(transport.isReady()).toBe(false);
|
|
166
|
-
});
|
|
167
|
-
it('should reconnect when the channel dies while the connection is still up', async ()=>{
|
|
168
|
-
jest.useFakeTimers();
|
|
169
|
-
const transport = new _rabbitmqtransportoptional.RabbitMqTransport({
|
|
170
|
-
reconnectDelay: 10
|
|
171
|
-
});
|
|
172
|
-
await transport.connect();
|
|
173
|
-
listenerFor(amqp.channels[0], 'close')();
|
|
174
|
-
expect(transport.isReady()).toBe(false);
|
|
175
|
-
await jest.advanceTimersByTimeAsync(10);
|
|
176
|
-
expect(amqp.connect).toHaveBeenCalledTimes(2);
|
|
177
|
-
expect(transport.isReady()).toBe(true);
|
|
178
|
-
jest.useRealTimers();
|
|
179
|
-
});
|
|
180
|
-
it('should close the surviving connection rather than leak it on a channel failure', async ()=>{
|
|
181
|
-
jest.useFakeTimers();
|
|
182
|
-
const transport = new _rabbitmqtransportoptional.RabbitMqTransport({
|
|
183
|
-
reconnectDelay: 10
|
|
184
|
-
});
|
|
185
|
-
await transport.connect();
|
|
186
|
-
listenerFor(amqp.channels[0], 'close')();
|
|
187
|
-
await jest.advanceTimersByTimeAsync(10);
|
|
188
|
-
expect(amqp.connection.close).toHaveBeenCalledTimes(1);
|
|
189
|
-
jest.useRealTimers();
|
|
190
|
-
});
|
|
191
|
-
it('should schedule a single reconnect when the channel and the connection both close', async ()=>{
|
|
192
|
-
jest.useFakeTimers();
|
|
193
|
-
const transport = new _rabbitmqtransportoptional.RabbitMqTransport({
|
|
194
|
-
reconnectDelay: 10
|
|
195
|
-
});
|
|
196
|
-
await transport.connect();
|
|
197
|
-
listenerFor(amqp.channels[0], 'close')();
|
|
198
|
-
listenerFor(amqp.connection, 'close')();
|
|
199
|
-
await jest.advanceTimersByTimeAsync(10);
|
|
200
|
-
expect(amqp.connect).toHaveBeenCalledTimes(2);
|
|
201
|
-
jest.useRealTimers();
|
|
202
|
-
});
|
|
203
|
-
it('should re-bind every pattern onto the new channel after a reconnect', async ()=>{
|
|
204
|
-
jest.useFakeTimers();
|
|
205
|
-
const transport = new _rabbitmqtransportoptional.RabbitMqTransport({
|
|
206
|
-
exchange: 'ex',
|
|
207
|
-
queue: 'q',
|
|
208
|
-
reconnectDelay: 10
|
|
209
|
-
});
|
|
210
|
-
await transport.connect();
|
|
211
|
-
await transport.bind('storage.**');
|
|
212
|
-
listenerFor(amqp.channels[0], 'close')();
|
|
213
|
-
await jest.advanceTimersByTimeAsync(10);
|
|
214
|
-
expect(amqp.channels[1].bindQueue).toHaveBeenCalledWith('q', 'ex', 'storage.#');
|
|
215
|
-
expect(amqp.channels[1].consume).toHaveBeenCalledTimes(1);
|
|
216
|
-
jest.useRealTimers();
|
|
217
|
-
});
|
|
218
|
-
it('should ack an in-flight delivery on the channel it arrived on', async ()=>{
|
|
219
|
-
jest.useFakeTimers();
|
|
220
|
-
const transport = new _rabbitmqtransportoptional.RabbitMqTransport({
|
|
221
|
-
reconnectDelay: 10
|
|
222
|
-
});
|
|
223
|
-
await transport.connect();
|
|
224
|
-
await transport.bind('storage.#');
|
|
225
|
-
const deliver = amqp.channels[0].consume.mock.calls[0][1];
|
|
226
|
-
listenerFor(amqp.channels[0], 'close')();
|
|
227
|
-
await jest.advanceTimersByTimeAsync(10);
|
|
228
|
-
await deliver({
|
|
229
|
-
content: Buffer.from(JSON.stringify(buildEvent()))
|
|
230
|
-
});
|
|
231
|
-
expect(amqp.channels[0].ack).toHaveBeenCalled();
|
|
232
|
-
expect(amqp.channels[1].ack).not.toHaveBeenCalled();
|
|
233
|
-
jest.useRealTimers();
|
|
234
|
-
});
|
|
235
|
-
it('should let a later bind retry consuming after the broker rejects consume', async ()=>{
|
|
236
|
-
const transport = new _rabbitmqtransportoptional.RabbitMqTransport();
|
|
237
|
-
await transport.connect();
|
|
238
|
-
amqp.channel.consume.mockRejectedValueOnce(new Error('CHANNEL_ERROR'));
|
|
239
|
-
await expect(transport.bind('storage.#')).rejects.toThrow('CHANNEL_ERROR');
|
|
240
|
-
await transport.bind('auth.#');
|
|
241
|
-
expect(amqp.channel.consume).toHaveBeenCalledTimes(2);
|
|
242
|
-
});
|
|
243
|
-
it('should stop reconnecting once it has been closed', async ()=>{
|
|
244
|
-
jest.useFakeTimers();
|
|
245
|
-
const transport = new _rabbitmqtransportoptional.RabbitMqTransport({
|
|
246
|
-
reconnectDelay: 10
|
|
247
|
-
});
|
|
248
|
-
await transport.connect();
|
|
249
|
-
await transport.close();
|
|
250
|
-
listenerFor(amqp.channels[0], 'close')();
|
|
251
|
-
await jest.advanceTimersByTimeAsync(50);
|
|
252
|
-
expect(amqp.connect).toHaveBeenCalledTimes(1);
|
|
253
|
-
jest.useRealTimers();
|
|
254
|
-
});
|
|
255
|
-
});
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
get buildQuery () {
|
|
13
|
-
return buildQuery;
|
|
14
|
-
},
|
|
15
|
-
get getCastIntType () {
|
|
16
|
-
return getCastIntType;
|
|
17
|
-
},
|
|
18
|
-
get getJsonColumnType () {
|
|
19
|
-
return getJsonColumnType;
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
const _nestjscore = require("@flusys/nestjs-core");
|
|
23
|
-
function getJsonColumnType() {
|
|
24
|
-
const driver = _nestjscore.envConfig.getTypeOrmConfig().type;
|
|
25
|
-
if (driver === 'postgres') return 'jsonb';
|
|
26
|
-
if (driver === 'mysql') return 'json';
|
|
27
|
-
return 'simple-json';
|
|
28
|
-
}
|
|
29
|
-
function getCastIntType() {
|
|
30
|
-
const driver = _nestjscore.envConfig.getTypeOrmConfig().type;
|
|
31
|
-
return driver === 'postgres' ? 'INTEGER' : 'UNSIGNED';
|
|
32
|
-
}
|
|
33
|
-
function buildQuery(sql, dbType) {
|
|
34
|
-
if (dbType !== 'postgres') return sql;
|
|
35
|
-
let index = 0;
|
|
36
|
-
return sql.replace(/\?/g, ()=>`$${++index}`);
|
|
37
|
-
}
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
const _handlecustomedb = require("./handle-custome-db");
|
|
6
|
-
const mockGetTypeOrmConfig = jest.fn();
|
|
7
|
-
jest.mock('@flusys/nestjs-core', ()=>({
|
|
8
|
-
envConfig: {
|
|
9
|
-
getTypeOrmConfig: (...args)=>mockGetTypeOrmConfig(...args)
|
|
10
|
-
}
|
|
11
|
-
}));
|
|
12
|
-
describe('handle-custome-db', ()=>{
|
|
13
|
-
beforeEach(()=>{
|
|
14
|
-
jest.clearAllMocks();
|
|
15
|
-
});
|
|
16
|
-
describe('getJsonColumnType', ()=>{
|
|
17
|
-
it('should return jsonb for postgres', ()=>{
|
|
18
|
-
mockGetTypeOrmConfig.mockReturnValue({
|
|
19
|
-
type: 'postgres'
|
|
20
|
-
});
|
|
21
|
-
expect((0, _handlecustomedb.getJsonColumnType)()).toBe('jsonb');
|
|
22
|
-
});
|
|
23
|
-
it('should return json for mysql', ()=>{
|
|
24
|
-
mockGetTypeOrmConfig.mockReturnValue({
|
|
25
|
-
type: 'mysql'
|
|
26
|
-
});
|
|
27
|
-
expect((0, _handlecustomedb.getJsonColumnType)()).toBe('json');
|
|
28
|
-
});
|
|
29
|
-
it('should return simple-json for mariadb', ()=>{
|
|
30
|
-
mockGetTypeOrmConfig.mockReturnValue({
|
|
31
|
-
type: 'mariadb'
|
|
32
|
-
});
|
|
33
|
-
expect((0, _handlecustomedb.getJsonColumnType)()).toBe('simple-json');
|
|
34
|
-
});
|
|
35
|
-
it('should return simple-json for sqlite', ()=>{
|
|
36
|
-
mockGetTypeOrmConfig.mockReturnValue({
|
|
37
|
-
type: 'sqlite'
|
|
38
|
-
});
|
|
39
|
-
expect((0, _handlecustomedb.getJsonColumnType)()).toBe('simple-json');
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
describe('getCastIntType', ()=>{
|
|
43
|
-
it('should return INTEGER for postgres', ()=>{
|
|
44
|
-
mockGetTypeOrmConfig.mockReturnValue({
|
|
45
|
-
type: 'postgres'
|
|
46
|
-
});
|
|
47
|
-
expect((0, _handlecustomedb.getCastIntType)()).toBe('INTEGER');
|
|
48
|
-
});
|
|
49
|
-
it('should return UNSIGNED for mysql', ()=>{
|
|
50
|
-
mockGetTypeOrmConfig.mockReturnValue({
|
|
51
|
-
type: 'mysql'
|
|
52
|
-
});
|
|
53
|
-
expect((0, _handlecustomedb.getCastIntType)()).toBe('UNSIGNED');
|
|
54
|
-
});
|
|
55
|
-
it('should return UNSIGNED for mariadb', ()=>{
|
|
56
|
-
mockGetTypeOrmConfig.mockReturnValue({
|
|
57
|
-
type: 'mariadb'
|
|
58
|
-
});
|
|
59
|
-
expect((0, _handlecustomedb.getCastIntType)()).toBe('UNSIGNED');
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
describe('buildQuery', ()=>{
|
|
63
|
-
it('should leave the sql untouched for mysql', ()=>{
|
|
64
|
-
expect((0, _handlecustomedb.buildQuery)('SELECT * FROM t WHERE id = ?', 'mysql')).toBe('SELECT * FROM t WHERE id = ?');
|
|
65
|
-
});
|
|
66
|
-
it('should leave the sql untouched for mariadb', ()=>{
|
|
67
|
-
expect((0, _handlecustomedb.buildQuery)('SELECT * FROM t WHERE id = ?', 'mariadb')).toBe('SELECT * FROM t WHERE id = ?');
|
|
68
|
-
});
|
|
69
|
-
it('should convert a single ? placeholder to $1 for postgres', ()=>{
|
|
70
|
-
expect((0, _handlecustomedb.buildQuery)('SELECT * FROM t WHERE id = ?', 'postgres')).toBe('SELECT * FROM t WHERE id = $1');
|
|
71
|
-
});
|
|
72
|
-
it('should convert multiple ? placeholders in order for postgres', ()=>{
|
|
73
|
-
expect((0, _handlecustomedb.buildQuery)('WHERE a = ? AND b = ? AND c = ?', 'postgres')).toBe('WHERE a = $1 AND b = $2 AND c = $3');
|
|
74
|
-
});
|
|
75
|
-
it('should return the sql unchanged when there are no placeholders', ()=>{
|
|
76
|
-
expect((0, _handlecustomedb.buildQuery)('SELECT 1', 'postgres')).toBe('SELECT 1');
|
|
77
|
-
});
|
|
78
|
-
it('should handle an empty sql string', ()=>{
|
|
79
|
-
expect((0, _handlecustomedb.buildQuery)('', 'postgres')).toBe('');
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
});
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "HybridCache", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return HybridCache;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _config = require("@flusys/nestjs-core/config");
|
|
12
|
-
const _redis = /*#__PURE__*/ _interop_require_default(require("@keyv/redis"));
|
|
13
|
-
const _cacheable = require("cacheable");
|
|
14
|
-
const _keyv = /*#__PURE__*/ _interop_require_default(require("keyv"));
|
|
15
|
-
function _define_property(obj, key, value) {
|
|
16
|
-
if (key in obj) {
|
|
17
|
-
Object.defineProperty(obj, key, {
|
|
18
|
-
value: value,
|
|
19
|
-
enumerable: true,
|
|
20
|
-
configurable: true,
|
|
21
|
-
writable: true
|
|
22
|
-
});
|
|
23
|
-
} else {
|
|
24
|
-
obj[key] = value;
|
|
25
|
-
}
|
|
26
|
-
return obj;
|
|
27
|
-
}
|
|
28
|
-
function _interop_require_default(obj) {
|
|
29
|
-
return obj && obj.__esModule ? obj : {
|
|
30
|
-
default: obj
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
let HybridCache = class HybridCache {
|
|
34
|
-
async get(key) {
|
|
35
|
-
// Prefer memory first if available
|
|
36
|
-
if (this.memory) {
|
|
37
|
-
const value = await this.memory.get(key);
|
|
38
|
-
if (value !== undefined) return value;
|
|
39
|
-
}
|
|
40
|
-
// Fallback to redis
|
|
41
|
-
if (this.redis) {
|
|
42
|
-
const value = await this.redis.get(key);
|
|
43
|
-
if (value !== undefined && this.memory) {
|
|
44
|
-
// backfill L1 memory cache
|
|
45
|
-
await this.memory.set(key, value);
|
|
46
|
-
}
|
|
47
|
-
return value;
|
|
48
|
-
}
|
|
49
|
-
return undefined;
|
|
50
|
-
}
|
|
51
|
-
async set(key, value, ttl) {
|
|
52
|
-
if (this.memory) {
|
|
53
|
-
await this.memory.set(key, value, ttl);
|
|
54
|
-
}
|
|
55
|
-
if (this.redis) {
|
|
56
|
-
await this.redis.set(key, value, ttl);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
async del(key) {
|
|
60
|
-
if (this.memory) {
|
|
61
|
-
await this.memory.delete(key);
|
|
62
|
-
}
|
|
63
|
-
if (this.redis) {
|
|
64
|
-
await this.redis.delete(key);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
async reset() {
|
|
68
|
-
if (this.memory) {
|
|
69
|
-
await this.memory.clear();
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
async resetL2() {
|
|
73
|
-
if (this.redis) {
|
|
74
|
-
await this.redis.clear();
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
constructor(memoryTtl = 60 * 1000, memorySize = 5000){
|
|
78
|
-
_define_property(this, "memory", void 0);
|
|
79
|
-
_define_property(this, "redis", void 0);
|
|
80
|
-
// Default to 'memory' if not specified
|
|
81
|
-
const useCacheLabel = _config.envConfig.tryGetValue('USE_CACHE_LABEL', false) || 'memory';
|
|
82
|
-
if (useCacheLabel === 'memory' || useCacheLabel === 'hybrid') {
|
|
83
|
-
this.memory = new _keyv.default({
|
|
84
|
-
store: new _cacheable.CacheableMemory({
|
|
85
|
-
ttl: memoryTtl,
|
|
86
|
-
lruSize: memorySize
|
|
87
|
-
})
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
if (useCacheLabel === 'redis' || useCacheLabel === 'hybrid') {
|
|
91
|
-
this.redis = new _keyv.default({
|
|
92
|
-
store: new _redis.default(_config.envConfig.getRedisUrl())
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
};
|
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
const _hybridcacheclass = require("./hybrid-cache.class");
|
|
6
|
-
const mockTryGetValue = jest.fn();
|
|
7
|
-
const mockGetRedisUrl = jest.fn();
|
|
8
|
-
jest.mock('@flusys/nestjs-core/config', ()=>({
|
|
9
|
-
envConfig: {
|
|
10
|
-
tryGetValue: (...args)=>mockTryGetValue(...args),
|
|
11
|
-
getRedisUrl: (...args)=>mockGetRedisUrl(...args)
|
|
12
|
-
}
|
|
13
|
-
}));
|
|
14
|
-
const mockKeyvGet = jest.fn();
|
|
15
|
-
const mockKeyvSet = jest.fn();
|
|
16
|
-
const mockKeyvDelete = jest.fn();
|
|
17
|
-
const mockKeyvClear = jest.fn();
|
|
18
|
-
const KeyvCtor = jest.fn().mockImplementation(()=>({
|
|
19
|
-
get: mockKeyvGet,
|
|
20
|
-
set: mockKeyvSet,
|
|
21
|
-
delete: mockKeyvDelete,
|
|
22
|
-
clear: mockKeyvClear
|
|
23
|
-
}));
|
|
24
|
-
jest.mock('keyv', ()=>({
|
|
25
|
-
__esModule: true,
|
|
26
|
-
default: KeyvCtor
|
|
27
|
-
}));
|
|
28
|
-
jest.mock('@keyv/redis', ()=>({
|
|
29
|
-
__esModule: true,
|
|
30
|
-
default: jest.fn().mockImplementation(()=>({}))
|
|
31
|
-
}));
|
|
32
|
-
jest.mock('cacheable', ()=>({
|
|
33
|
-
CacheableMemory: jest.fn().mockImplementation(()=>({}))
|
|
34
|
-
}));
|
|
35
|
-
describe('HybridCache', ()=>{
|
|
36
|
-
beforeEach(()=>{
|
|
37
|
-
jest.clearAllMocks();
|
|
38
|
-
mockGetRedisUrl.mockReturnValue('redis://localhost:6379');
|
|
39
|
-
});
|
|
40
|
-
describe('construction', ()=>{
|
|
41
|
-
it('should default to memory-only cache when USE_CACHE_LABEL is unset', ()=>{
|
|
42
|
-
mockTryGetValue.mockReturnValue('');
|
|
43
|
-
new _hybridcacheclass.HybridCache();
|
|
44
|
-
expect(KeyvCtor).toHaveBeenCalledTimes(1);
|
|
45
|
-
});
|
|
46
|
-
it('should create only a memory store for "memory" mode', ()=>{
|
|
47
|
-
mockTryGetValue.mockReturnValue('memory');
|
|
48
|
-
new _hybridcacheclass.HybridCache();
|
|
49
|
-
expect(KeyvCtor).toHaveBeenCalledTimes(1);
|
|
50
|
-
});
|
|
51
|
-
it('should create only a redis store for "redis" mode', async ()=>{
|
|
52
|
-
mockTryGetValue.mockReturnValue('redis');
|
|
53
|
-
const cache = new _hybridcacheclass.HybridCache();
|
|
54
|
-
mockKeyvGet.mockResolvedValue('redis-value');
|
|
55
|
-
const result = await cache.get('key');
|
|
56
|
-
expect(KeyvCtor).toHaveBeenCalledTimes(1);
|
|
57
|
-
expect(result).toBe('redis-value');
|
|
58
|
-
});
|
|
59
|
-
it('should create both stores for "hybrid" mode', ()=>{
|
|
60
|
-
mockTryGetValue.mockReturnValue('hybrid');
|
|
61
|
-
new _hybridcacheclass.HybridCache();
|
|
62
|
-
expect(KeyvCtor).toHaveBeenCalledTimes(2);
|
|
63
|
-
});
|
|
64
|
-
it('should pass custom ttl and size through to the memory store options', ()=>{
|
|
65
|
-
mockTryGetValue.mockReturnValue('memory');
|
|
66
|
-
const { CacheableMemory } = require('cacheable');
|
|
67
|
-
new _hybridcacheclass.HybridCache(30_000, 100);
|
|
68
|
-
expect(CacheableMemory).toHaveBeenCalledWith({
|
|
69
|
-
ttl: 30_000,
|
|
70
|
-
lruSize: 100
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
describe('get (hybrid mode)', ()=>{
|
|
75
|
-
let cache;
|
|
76
|
-
beforeEach(()=>{
|
|
77
|
-
mockTryGetValue.mockReturnValue('hybrid');
|
|
78
|
-
cache = new _hybridcacheclass.HybridCache();
|
|
79
|
-
});
|
|
80
|
-
it('should return the memory value without touching redis when present', async ()=>{
|
|
81
|
-
mockKeyvGet.mockResolvedValueOnce('memory-value');
|
|
82
|
-
const result = await cache.get('key');
|
|
83
|
-
expect(result).toBe('memory-value');
|
|
84
|
-
expect(mockKeyvGet).toHaveBeenCalledTimes(1);
|
|
85
|
-
});
|
|
86
|
-
it('should fall back to redis and backfill memory on L1 miss', async ()=>{
|
|
87
|
-
mockKeyvGet.mockResolvedValueOnce(undefined).mockResolvedValueOnce('redis-value');
|
|
88
|
-
const result = await cache.get('key');
|
|
89
|
-
expect(result).toBe('redis-value');
|
|
90
|
-
expect(mockKeyvSet).toHaveBeenCalledWith('key', 'redis-value');
|
|
91
|
-
});
|
|
92
|
-
it('should return undefined without backfilling when redis also misses', async ()=>{
|
|
93
|
-
mockKeyvGet.mockResolvedValue(undefined);
|
|
94
|
-
const result = await cache.get('key');
|
|
95
|
-
expect(result).toBeUndefined();
|
|
96
|
-
expect(mockKeyvSet).not.toHaveBeenCalled();
|
|
97
|
-
});
|
|
98
|
-
it('should treat falsy-but-defined values (0, "", false) as cache hits', async ()=>{
|
|
99
|
-
mockKeyvGet.mockResolvedValueOnce(0);
|
|
100
|
-
const result = await cache.get('key');
|
|
101
|
-
expect(result).toBe(0);
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
describe('get (memory-only mode)', ()=>{
|
|
105
|
-
it('should return undefined when there is no redis store to fall back to', async ()=>{
|
|
106
|
-
mockTryGetValue.mockReturnValue('memory');
|
|
107
|
-
const cache = new _hybridcacheclass.HybridCache();
|
|
108
|
-
mockKeyvGet.mockResolvedValue(undefined);
|
|
109
|
-
const result = await cache.get('key');
|
|
110
|
-
expect(result).toBeUndefined();
|
|
111
|
-
});
|
|
112
|
-
});
|
|
113
|
-
describe('set', ()=>{
|
|
114
|
-
it('should write to both stores in hybrid mode with the given ttl', async ()=>{
|
|
115
|
-
mockTryGetValue.mockReturnValue('hybrid');
|
|
116
|
-
const cache = new _hybridcacheclass.HybridCache();
|
|
117
|
-
await cache.set('key', {
|
|
118
|
-
a: 1
|
|
119
|
-
}, 5000);
|
|
120
|
-
expect(mockKeyvSet).toHaveBeenNthCalledWith(1, 'key', {
|
|
121
|
-
a: 1
|
|
122
|
-
}, 5000);
|
|
123
|
-
expect(mockKeyvSet).toHaveBeenNthCalledWith(2, 'key', {
|
|
124
|
-
a: 1
|
|
125
|
-
}, 5000);
|
|
126
|
-
});
|
|
127
|
-
it('should write only to memory when redis is not configured', async ()=>{
|
|
128
|
-
mockTryGetValue.mockReturnValue('memory');
|
|
129
|
-
const cache = new _hybridcacheclass.HybridCache();
|
|
130
|
-
await cache.set('key', 'value');
|
|
131
|
-
expect(mockKeyvSet).toHaveBeenCalledTimes(1);
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
describe('del', ()=>{
|
|
135
|
-
it('should delete from both stores in hybrid mode', async ()=>{
|
|
136
|
-
mockTryGetValue.mockReturnValue('hybrid');
|
|
137
|
-
const cache = new _hybridcacheclass.HybridCache();
|
|
138
|
-
await cache.del('key');
|
|
139
|
-
expect(mockKeyvDelete).toHaveBeenCalledTimes(2);
|
|
140
|
-
});
|
|
141
|
-
it('should be a no-op safe call when no stores are configured', async ()=>{
|
|
142
|
-
mockTryGetValue.mockReturnValue('unknown-mode');
|
|
143
|
-
const cache = new _hybridcacheclass.HybridCache();
|
|
144
|
-
await expect(cache.del('key')).resolves.toBeUndefined();
|
|
145
|
-
expect(mockKeyvDelete).not.toHaveBeenCalled();
|
|
146
|
-
});
|
|
147
|
-
});
|
|
148
|
-
describe('reset / resetL2', ()=>{
|
|
149
|
-
it('reset should clear only the memory store', async ()=>{
|
|
150
|
-
mockTryGetValue.mockReturnValue('hybrid');
|
|
151
|
-
const cache = new _hybridcacheclass.HybridCache();
|
|
152
|
-
await cache.reset();
|
|
153
|
-
expect(mockKeyvClear).toHaveBeenCalledTimes(1);
|
|
154
|
-
});
|
|
155
|
-
it('resetL2 should clear only the redis store', async ()=>{
|
|
156
|
-
mockTryGetValue.mockReturnValue('hybrid');
|
|
157
|
-
const cache = new _hybridcacheclass.HybridCache();
|
|
158
|
-
await cache.resetL2();
|
|
159
|
-
expect(mockKeyvClear).toHaveBeenCalledTimes(1);
|
|
160
|
-
});
|
|
161
|
-
it('reset should be a no-op when memory is not configured', async ()=>{
|
|
162
|
-
mockTryGetValue.mockReturnValue('redis');
|
|
163
|
-
const cache = new _hybridcacheclass.HybridCache();
|
|
164
|
-
await expect(cache.reset()).resolves.toBeUndefined();
|
|
165
|
-
expect(mockKeyvClear).not.toHaveBeenCalled();
|
|
166
|
-
});
|
|
167
|
-
});
|
|
168
|
-
});
|