@bitblit/ratchet-epsilon-common 6.0.146-alpha → 6.0.148-alpha
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/lib/config/cron/abstract-cron-entry.d.ts +1 -1
- package/package.json +11 -10
- package/src/background/background-dynamo-log-table-handler.ts +44 -0
- package/src/background/background-entry.ts +4 -0
- package/src/background/background-execution-event-type.ts +9 -0
- package/src/background/background-execution-event.ts +9 -0
- package/src/background/background-execution-listener.ts +6 -0
- package/src/background/background-handler.ts +352 -0
- package/src/background/background-http-adapter-handler.ts +166 -0
- package/src/background/background-meta-response-internal.ts +5 -0
- package/src/background/background-process-handling.ts +6 -0
- package/src/background/background-process-log-table-entry.ts +11 -0
- package/src/background/background-queue-response-internal.ts +9 -0
- package/src/background/background-validator.ts +105 -0
- package/src/background/epsilon-background-process-error.ts +110 -0
- package/src/background/internal-background-entry.ts +10 -0
- package/src/background/manager/abstract-background-manager.ts +120 -0
- package/src/background/manager/aws-large-payload-s3-sqs-sns-background-manager.ts +87 -0
- package/src/background/manager/aws-sqs-sns-background-manager.ts +201 -0
- package/src/background/manager/background-manager-like.ts +44 -0
- package/src/background/manager/background-manager.spec.ts +89 -0
- package/src/background/manager/single-thread-local-background-manager.ts +58 -0
- package/src/background/s3-background-transaction-logger.ts +65 -0
- package/src/build/ratchet-epsilon-common-info.ts +19 -0
- package/src/built-in/background/echo-processor.ts +17 -0
- package/src/built-in/background/log-and-enqueue-echo-processor.ts +14 -0
- package/src/built-in/background/log-message-background-error-processor.ts +10 -0
- package/src/built-in/background/no-op-processor.ts +12 -0
- package/src/built-in/background/retry-processor.ts +51 -0
- package/src/built-in/background/sample-delay-processor.ts +15 -0
- package/src/built-in/background/sample-input-validated-processor-data.ts +4 -0
- package/src/built-in/background/sample-input-validated-processor.ts +14 -0
- package/src/built-in/built-in-trace-id-generators.ts +22 -0
- package/src/built-in/daemon/daemon-authorizer-function.ts +4 -0
- package/src/built-in/daemon/daemon-config.ts +9 -0
- package/src/built-in/daemon/daemon-group-selection-function.ts +3 -0
- package/src/built-in/daemon/daemon-handler.ts +87 -0
- package/src/built-in/daemon/daemon-process-state-list.ts +9 -0
- package/src/built-in/http/apollo/apollo-util.ts +43 -0
- package/src/built-in/http/apollo/default-epsilon-apollo-context.ts +11 -0
- package/src/built-in/http/apollo/epsilon-apollo-context-builder-options.ts +5 -0
- package/src/built-in/http/apollo/epsilon-lambda-apollo-context-function-argument.ts +6 -0
- package/src/built-in/http/apollo/epsilon-lambda-apollo-options.ts +11 -0
- package/src/built-in/http/apollo-filter.ts +151 -0
- package/src/built-in/http/built-in-auth-filters.ts +73 -0
- package/src/built-in/http/built-in-authorizers.ts +22 -0
- package/src/built-in/http/built-in-filters.spec.ts +26 -0
- package/src/built-in/http/built-in-filters.ts +300 -0
- package/src/built-in/http/built-in-handlers.ts +85 -0
- package/src/built-in/http/log-level-manipulation-filter.ts +26 -0
- package/src/built-in/http/run-handler-as-filter.spec.ts +67 -0
- package/src/built-in/http/run-handler-as-filter.ts +102 -0
- package/src/cli/ratchet-cli-handler.ts +23 -0
- package/src/cli/run-background-process-from-command-line.ts +32 -0
- package/src/config/background/background-aws-config.ts +8 -0
- package/src/config/background/background-config.ts +15 -0
- package/src/config/background/background-error-processor.ts +5 -0
- package/src/config/background/background-processor.ts +14 -0
- package/src/config/background/background-transaction-log.ts +9 -0
- package/src/config/background/background-transaction-logger.ts +6 -0
- package/src/config/cron/abstract-cron-entry.ts +17 -0
- package/src/config/cron/cron-background-entry.ts +17 -0
- package/src/config/cron/cron-config.ts +10 -0
- package/src/config/dynamo-db-config.ts +6 -0
- package/src/config/epsilon-config.ts +30 -0
- package/src/config/epsilon-lambda-event-handler.ts +12 -0
- package/src/config/epsilon-logger-config.ts +23 -0
- package/src/config/espilon-server-mode.ts +10 -0
- package/src/config/generic-aws-event-handler-function.ts +1 -0
- package/src/config/http/authorizer-function.ts +9 -0
- package/src/config/http/epsilon-authorization-context.ts +5 -0
- package/src/config/http/epsilon-cors-approach.ts +7 -0
- package/src/config/http/extended-api-gateway-event.ts +8 -0
- package/src/config/http/filter-chain-context.ts +15 -0
- package/src/config/http/filter-function.ts +3 -0
- package/src/config/http/handler-function.ts +4 -0
- package/src/config/http/http-config.ts +27 -0
- package/src/config/http/http-processing-config.ts +23 -0
- package/src/config/http/mapped-http-processing-config.ts +12 -0
- package/src/config/http/null-returned-object-handling.ts +7 -0
- package/src/config/inter-api/inter-api-aws-config.ts +5 -0
- package/src/config/inter-api/inter-api-config.ts +7 -0
- package/src/config/inter-api/inter-api-process-mapping.ts +11 -0
- package/src/config/local-server/local-server-event-logging-style.ts +8 -0
- package/src/config/local-server/local-server-http-method-handling.ts +7 -0
- package/src/config/local-server/local-server-options.ts +12 -0
- package/src/config/logging-trace-id-generator.ts +3 -0
- package/src/config/no-handlers-found-error.ts +6 -0
- package/src/config/open-api/open-api-document-components.ts +4 -0
- package/src/config/open-api/open-api-document.ts +7 -0
- package/src/config/s3-config.ts +8 -0
- package/src/config/sns-config.ts +7 -0
- package/src/config/sqs-config.ts +7 -0
- package/src/epsilon-build-properties.ts +21 -0
- package/src/epsilon-constants.ts +62 -0
- package/src/epsilon-global-handler.ts +238 -0
- package/src/epsilon-instance.ts +20 -0
- package/src/epsilon-logging-extension-processor.ts +19 -0
- package/src/http/auth/api-gateway-adapter-authentication-handler.ts +95 -0
- package/src/http/auth/auth0-web-token-manipulator.ts +69 -0
- package/src/http/auth/basic-auth-token.ts +7 -0
- package/src/http/auth/google-web-token-manipulator.spec.ts +15 -0
- package/src/http/auth/google-web-token-manipulator.ts +80 -0
- package/src/http/auth/jwt-ratchet-local-web-token-manipulator.ts +37 -0
- package/src/http/auth/local-web-token-manipulator.spec.ts +34 -0
- package/src/http/auth/local-web-token-manipulator.ts +114 -0
- package/src/http/auth/web-token-manipulator.ts +9 -0
- package/src/http/error/bad-gateway.ts +11 -0
- package/src/http/error/bad-request-error.ts +11 -0
- package/src/http/error/conflict-error.ts +12 -0
- package/src/http/error/forbidden-error.ts +12 -0
- package/src/http/error/gateway-timeout.ts +12 -0
- package/src/http/error/method-not-allowed-error.ts +12 -0
- package/src/http/error/misconfigured-error.ts +12 -0
- package/src/http/error/not-found-error.ts +12 -0
- package/src/http/error/not-implemented.ts +12 -0
- package/src/http/error/request-timeout-error.ts +12 -0
- package/src/http/error/service-unavailable.ts +12 -0
- package/src/http/error/too-many-requests-error.ts +12 -0
- package/src/http/error/unauthorized-error.ts +12 -0
- package/src/http/event-util.spec.ts +190 -0
- package/src/http/event-util.ts +272 -0
- package/src/http/response-util.spec.ts +117 -0
- package/src/http/response-util.ts +164 -0
- package/src/http/route/epsilon-router.ts +9 -0
- package/src/http/route/extended-auth-response-context.ts +7 -0
- package/src/http/route/route-and-parse.ts +8 -0
- package/src/http/route/route-mapping.ts +21 -0
- package/src/http/route/route-validator-config.ts +5 -0
- package/src/http/route/router-util.spec.ts +33 -0
- package/src/http/route/router-util.ts +314 -0
- package/src/http/web-handler.spec.ts +99 -0
- package/src/http/web-handler.ts +157 -0
- package/src/http/web-v2-handler.ts +34 -0
- package/src/inter-api/inter-api-entry.ts +8 -0
- package/src/inter-api/inter-api-util.spec.ts +77 -0
- package/src/inter-api/inter-api-util.ts +71 -0
- package/src/inter-api-manager.ts +75 -0
- package/src/lambda-event-handler/cron-epsilon-lambda-event-handler.spec.ts +130 -0
- package/src/lambda-event-handler/cron-epsilon-lambda-event-handler.ts +132 -0
- package/src/lambda-event-handler/dynamo-epsilon-lambda-event-handler.ts +42 -0
- package/src/lambda-event-handler/generic-sns-epsilon-lambda-event-handler.ts +38 -0
- package/src/lambda-event-handler/generic-sqs-epsilon-lambda-event-handler.ts +43 -0
- package/src/lambda-event-handler/inter-api-epsilon-lambda-event-handler.ts +33 -0
- package/src/lambda-event-handler/s3-epsilon-lambda-event-handler.ts +50 -0
- package/src/local-container-server.ts +128 -0
- package/src/local-server.spec.ts +16 -0
- package/src/local-server.ts +426 -0
- package/src/open-api-util/open-api-doc-modifications.ts +9 -0
- package/src/open-api-util/open-api-doc-modifier.spec.ts +22 -0
- package/src/open-api-util/open-api-doc-modifier.ts +90 -0
- package/src/open-api-util/yaml-combiner.spec.ts +26 -0
- package/src/open-api-util/yaml-combiner.ts +35 -0
- package/src/sample/sample-server-components-with-apollo.ts +87 -0
- package/src/sample/sample-server-components.ts +183 -0
- package/src/sample/sample-server-static-files.ts +614 -0
- package/src/sample/test-error-server.ts +140 -0
- package/src/util/aws-util.ts +89 -0
- package/src/util/context-global-data.ts +13 -0
- package/src/util/context-util.ts +156 -0
- package/src/util/cron-util.spec.ts +190 -0
- package/src/util/cron-util.ts +86 -0
- package/src/util/epsilon-config-parser.ts +90 -0
- package/src/util/epsilon-server-util.spec.ts +18 -0
- package/src/util/epsilon-server-util.ts +16 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { BackgroundEntry } from '../background-entry.js';
|
|
2
|
+
import { InternalBackgroundEntry } from '../internal-background-entry.js';
|
|
3
|
+
import { Subject } from 'rxjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Classes implementing this interface handle all submission of work to the background processing system.
|
|
7
|
+
*
|
|
8
|
+
* Note that these do NOT validate the input, they just passes it along. This is
|
|
9
|
+
* because it creates a circular reference to the processors if we try since they
|
|
10
|
+
* define the type and validation.
|
|
11
|
+
*/
|
|
12
|
+
export interface BackgroundManagerLike {
|
|
13
|
+
// Used in info dumps and logging
|
|
14
|
+
backgroundManagerName: string;
|
|
15
|
+
// If the class defines this, other classes can register to be notified the instant anything arrives in the queue
|
|
16
|
+
immediateProcessQueue?(): Subject<InternalBackgroundEntry<any>>;
|
|
17
|
+
// Wraps up an entry
|
|
18
|
+
createEntry<T>(type: string, data?: T): BackgroundEntry<T>;
|
|
19
|
+
// Wraps up an entry and adds internal control structures
|
|
20
|
+
wrapEntryForInternal<T>(
|
|
21
|
+
entry: BackgroundEntry<T>,
|
|
22
|
+
overrideTraceId?: string,
|
|
23
|
+
overrideTraceDepth?: number,
|
|
24
|
+
): Promise<InternalBackgroundEntry<T>>;
|
|
25
|
+
// Helper method to simplify building a request and add it to the queue
|
|
26
|
+
addEntryToQueueByParts<T>(type: string, data?: T, fireStartMessage?: boolean): Promise<string>;
|
|
27
|
+
// Add an entry to the queue for background processing, and optionally fire a start message after it is added
|
|
28
|
+
addEntryToQueue<T>(entry: BackgroundEntry<T>, fireStartMessage?: boolean): Promise<string>;
|
|
29
|
+
// Helper method to add more than one entry, and optionally fire a start message after all are added
|
|
30
|
+
addEntriesToQueue(entries: BackgroundEntry<any>[], fireStartMessage?: boolean): Promise<string[]>;
|
|
31
|
+
// Helper method to simplify building a request and then immediately run it in the background processor
|
|
32
|
+
fireImmediateProcessRequestByParts<T>(type: string, data?: T): Promise<string>;
|
|
33
|
+
// Immediately run a request in the background processor
|
|
34
|
+
fireImmediateProcessRequest<T>(entry: BackgroundEntry<T>): Promise<string>;
|
|
35
|
+
// Tell the processor to try reading the queue and start processing if anything is found
|
|
36
|
+
fireStartProcessingRequest(): Promise<string>;
|
|
37
|
+
// Check approximately how many entries are currently waiting in the queue
|
|
38
|
+
fetchApproximateNumberOfQueueEntries(): Promise<number>;
|
|
39
|
+
// Read a single entry from the background queue
|
|
40
|
+
takeEntryFromBackgroundQueue(): Promise<InternalBackgroundEntry<any>[]>;
|
|
41
|
+
// If defined, allows the entry to be modified immediately prior to submission to the processors
|
|
42
|
+
// Allows global behavior like reading a large body from S3 for example
|
|
43
|
+
modifyPayloadPreProcess?<T>(entry: InternalBackgroundEntry<T>): Promise<InternalBackgroundEntry<T>>;
|
|
44
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { Logger } from '@bitblit/ratchet-common/logger/logger';
|
|
2
|
+
import { GetQueueAttributesCommand, GetQueueAttributesResult, SQSClient } from '@aws-sdk/client-sqs';
|
|
3
|
+
//import { ModelValidator } from '@bitblit/ratchet-misc/model-validator/model-validator';
|
|
4
|
+
import { AwsSqsSnsBackgroundManager } from './aws-sqs-sns-background-manager.js';
|
|
5
|
+
import { BackgroundConfig } from '../../config/background/background-config.js';
|
|
6
|
+
import { EchoProcessor } from '../../built-in/background/echo-processor.js';
|
|
7
|
+
import { NoOpProcessor } from '../../built-in/background/no-op-processor.js';
|
|
8
|
+
import { BackgroundAwsConfig } from '../../config/background/background-aws-config.js';
|
|
9
|
+
import { SNSClient } from '@aws-sdk/client-sns';
|
|
10
|
+
import { mockClient } from 'aws-sdk-client-mock';
|
|
11
|
+
import { beforeEach, describe, expect, test } from 'vitest';
|
|
12
|
+
|
|
13
|
+
let mockSqs;
|
|
14
|
+
let mockSns;
|
|
15
|
+
|
|
16
|
+
describe('#createEntry', function () {
|
|
17
|
+
mockSns = mockClient(SNSClient);
|
|
18
|
+
mockSqs = mockClient(SQSClient);
|
|
19
|
+
|
|
20
|
+
let backgroundMgr: AwsSqsSnsBackgroundManager;
|
|
21
|
+
const fakeAccountNumber: string = '123456789012';
|
|
22
|
+
let _backgroundConfig: BackgroundConfig;
|
|
23
|
+
let backgroundAwsConfig: BackgroundAwsConfig;
|
|
24
|
+
//const fakeModelValidator: ModelValidator = new ModelValidator({ BackgroundBuiltInSampleInputValidatedProcessor: {} });
|
|
25
|
+
|
|
26
|
+
const echoProcessor: EchoProcessor = new EchoProcessor();
|
|
27
|
+
const noOpProcessor: NoOpProcessor = new NoOpProcessor();
|
|
28
|
+
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
mockSqs.reset();
|
|
31
|
+
mockSns.reset();
|
|
32
|
+
|
|
33
|
+
_backgroundConfig = {
|
|
34
|
+
processors: [echoProcessor, noOpProcessor],
|
|
35
|
+
httpSubmissionPath: '/background/',
|
|
36
|
+
implyTypeFromPathSuffix: true,
|
|
37
|
+
httpMetaEndpoint: '/background-meta',
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
backgroundAwsConfig = {
|
|
41
|
+
queueUrl: 'https://fake-sqs.fake-availability-zone.test.com/' + fakeAccountNumber + '/fakeQueue.fifo',
|
|
42
|
+
notificationArn: 'arn:aws:sns:fake-availability-zone:' + fakeAccountNumber + ':fakeSnsTopicName',
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
backgroundMgr = new AwsSqsSnsBackgroundManager(backgroundAwsConfig, mockSqs, mockSns);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('Should return queue attributes', async () => {
|
|
49
|
+
mockSqs.on(GetQueueAttributesCommand).resolves({
|
|
50
|
+
Attributes: {
|
|
51
|
+
ApproximateNumberOfMessages: 1,
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const queueAttr: GetQueueAttributesResult = await backgroundMgr.fetchCurrentQueueAttributes();
|
|
56
|
+
const msgCount: number = await backgroundMgr.fetchApproximateNumberOfQueueEntries();
|
|
57
|
+
Logger.info('Got : %j', queueAttr);
|
|
58
|
+
Logger.info('Msg: %d', msgCount);
|
|
59
|
+
expect(queueAttr).toBeTruthy();
|
|
60
|
+
expect(msgCount).toEqual(1);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('Should round-trip guids with prefix no slash', async () => {
|
|
64
|
+
const prefix: string = 'test';
|
|
65
|
+
const guid: string = AwsSqsSnsBackgroundManager.generateBackgroundGuid();
|
|
66
|
+
expect(guid).toBeTruthy();
|
|
67
|
+
const path: string = AwsSqsSnsBackgroundManager.backgroundGuidToPath(prefix, guid);
|
|
68
|
+
const outGuid: string = AwsSqsSnsBackgroundManager.pathToBackgroundGuid(prefix, path);
|
|
69
|
+
expect(outGuid).toEqual(guid);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('Should round-trip guids with prefix with slash', async () => {
|
|
73
|
+
const prefix: string = 'test/';
|
|
74
|
+
const guid: string = AwsSqsSnsBackgroundManager.generateBackgroundGuid();
|
|
75
|
+
expect(guid).toBeTruthy();
|
|
76
|
+
const path: string = AwsSqsSnsBackgroundManager.backgroundGuidToPath(prefix, guid);
|
|
77
|
+
const outGuid: string = AwsSqsSnsBackgroundManager.pathToBackgroundGuid(prefix, path);
|
|
78
|
+
expect(outGuid).toEqual(guid);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('Should round-trip guids with no prefix', async () => {
|
|
82
|
+
const prefix: string = null;
|
|
83
|
+
const guid: string = AwsSqsSnsBackgroundManager.generateBackgroundGuid();
|
|
84
|
+
expect(guid).toBeTruthy();
|
|
85
|
+
const path: string = AwsSqsSnsBackgroundManager.backgroundGuidToPath(prefix, guid);
|
|
86
|
+
const outGuid: string = AwsSqsSnsBackgroundManager.pathToBackgroundGuid(prefix, path);
|
|
87
|
+
expect(outGuid).toEqual(guid);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Subject } from 'rxjs';
|
|
2
|
+
import { Logger } from '@bitblit/ratchet-common/logger/logger';
|
|
3
|
+
import { BackgroundEntry } from '../background-entry.js';
|
|
4
|
+
import { InternalBackgroundEntry } from '../internal-background-entry.js';
|
|
5
|
+
import { AbstractBackgroundManager } from './abstract-background-manager.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Handles all submission of work to the background processing system.
|
|
9
|
+
*
|
|
10
|
+
* Note that this does NOT validate the input, it just passes it along. This is
|
|
11
|
+
* because it creates a circular reference to the processors if we try since they
|
|
12
|
+
* define the type and validation.
|
|
13
|
+
*/
|
|
14
|
+
export class SingleThreadLocalBackgroundManager extends AbstractBackgroundManager {
|
|
15
|
+
private _localBus: Subject<InternalBackgroundEntry<any>> = new Subject<InternalBackgroundEntry<any>>();
|
|
16
|
+
|
|
17
|
+
public readonly backgroundManagerName: string = 'SingleThreadLocalBackgroundManager';
|
|
18
|
+
|
|
19
|
+
// Super constructor automatically called
|
|
20
|
+
public immediateProcessQueue?(): Subject<InternalBackgroundEntry<any>> {
|
|
21
|
+
return this._localBus;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public async addEntryToQueue<T>(entry: BackgroundEntry<T>, fireStartMessage?: boolean): Promise<string> {
|
|
25
|
+
const wrapped: InternalBackgroundEntry<T> = await this.wrapEntryForInternal(entry);
|
|
26
|
+
const rval: string = wrapped.guid;
|
|
27
|
+
Logger.info('Add entry to queue (local) : %j : Start : %s', entry, fireStartMessage);
|
|
28
|
+
this._localBus.next(wrapped);
|
|
29
|
+
return rval;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public async fireImmediateProcessRequest<T>(entry: BackgroundEntry<T>): Promise<string> {
|
|
33
|
+
let rval: string = null;
|
|
34
|
+
const wrapped: InternalBackgroundEntry<T> = await this.wrapEntryForInternal(entry);
|
|
35
|
+
rval = wrapped.guid;
|
|
36
|
+
Logger.info('Fire immediately (local) : %j ', entry);
|
|
37
|
+
this._localBus.next(wrapped);
|
|
38
|
+
return rval;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public async fireStartProcessingRequest(): Promise<string> {
|
|
42
|
+
let rval: string = null;
|
|
43
|
+
Logger.info('Fire start processing request (local, ignored)');
|
|
44
|
+
rval = 'NO-OP';
|
|
45
|
+
return rval;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public async fetchApproximateNumberOfQueueEntries(): Promise<number> {
|
|
49
|
+
let rval: number = null;
|
|
50
|
+
rval = 0; // No queue when running locally
|
|
51
|
+
return rval;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public async takeEntryFromBackgroundQueue(): Promise<InternalBackgroundEntry<any>[]> {
|
|
55
|
+
Logger.info('Called takeEntryFromBackgroundQueue on SingleThreadedLocal - returning empty');
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { BackgroundTransactionLogger } from '../config/background/background-transaction-logger.js';
|
|
2
|
+
import { BackgroundTransactionLog } from '../config/background/background-transaction-log.js';
|
|
3
|
+
import { AbstractBackgroundManager } from './manager/abstract-background-manager.js';
|
|
4
|
+
import { Logger } from '@bitblit/ratchet-common/logger/logger';
|
|
5
|
+
import { ErrorRatchet } from '@bitblit/ratchet-common/lang/error-ratchet';
|
|
6
|
+
import { S3Client } from '@aws-sdk/client-s3';
|
|
7
|
+
import { S3CacheRatchet } from '@bitblit/ratchet-aws/s3/s3-cache-ratchet';
|
|
8
|
+
|
|
9
|
+
export class S3BackgroundTransactionLogger implements BackgroundTransactionLogger {
|
|
10
|
+
private s3TransactionLogCacheRatchet: S3CacheRatchet;
|
|
11
|
+
|
|
12
|
+
constructor(private cfg: BackgroundS3TransactionLoggingConfig) {
|
|
13
|
+
const err: string[] = S3BackgroundTransactionLogger.validateConfig(cfg);
|
|
14
|
+
if (err.length) {
|
|
15
|
+
ErrorRatchet.throwFormattedErr('Invalid S3BackgroundTransactionLogger config : %j', err);
|
|
16
|
+
}
|
|
17
|
+
this.s3TransactionLogCacheRatchet = new S3CacheRatchet(this.cfg.s3, this.cfg.bucket);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public async logTransaction(txLog: BackgroundTransactionLog): Promise<void> {
|
|
21
|
+
if (txLog) {
|
|
22
|
+
if (txLog.request?.guid) {
|
|
23
|
+
await this.s3TransactionLogCacheRatchet.writeObjectToCacheFile(
|
|
24
|
+
AbstractBackgroundManager.backgroundGuidToPath(this.cfg.prefix, txLog.request.guid),
|
|
25
|
+
txLog,
|
|
26
|
+
);
|
|
27
|
+
} else {
|
|
28
|
+
Logger.warn('Could not write transaction record - no guid defined : %j', txLog);
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
Logger.silly('Skipping write of null log');
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public async readTransactionLog(txGuid: string): Promise<BackgroundTransactionLog> {
|
|
36
|
+
const path: string = AbstractBackgroundManager.backgroundGuidToPath(this.cfg.prefix, txGuid);
|
|
37
|
+
const log = await this.s3TransactionLogCacheRatchet.fetchCacheFileAsObject<BackgroundTransactionLog>(path);
|
|
38
|
+
return log;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public static validateConfig(cfg: BackgroundS3TransactionLoggingConfig): string[] {
|
|
42
|
+
const rval: string[] = [];
|
|
43
|
+
if (cfg) {
|
|
44
|
+
if (!cfg.s3) {
|
|
45
|
+
rval.push('You must supply an S3 object');
|
|
46
|
+
}
|
|
47
|
+
if (!cfg.bucket) {
|
|
48
|
+
rval.push('You must supply a bucket');
|
|
49
|
+
}
|
|
50
|
+
if (!cfg.timeToLiveDays) {
|
|
51
|
+
rval.push('You must supply a timeToLiveDays');
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
rval.push('No config defined');
|
|
55
|
+
}
|
|
56
|
+
return rval;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface BackgroundS3TransactionLoggingConfig {
|
|
61
|
+
s3: S3Client;
|
|
62
|
+
bucket: string;
|
|
63
|
+
timeToLiveDays: number;
|
|
64
|
+
prefix?: string;
|
|
65
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { BuildInformation } from '@bitblit/ratchet-common/build/build-information';
|
|
2
|
+
|
|
3
|
+
export class RatchetEpsilonCommonInfo {
|
|
4
|
+
// Empty constructor prevents instantiation
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
6
|
+
private constructor() {}
|
|
7
|
+
|
|
8
|
+
public static buildInformation(): BuildInformation {
|
|
9
|
+
const val: BuildInformation = {
|
|
10
|
+
version: 'LOCAL-SNAPSHOT',
|
|
11
|
+
hash: 'LOCAL-HASH',
|
|
12
|
+
branch: 'LOCAL-BRANCH',
|
|
13
|
+
tag: 'LOCAL-TAG',
|
|
14
|
+
timeBuiltISO: 'LOCAL-TIME-ISO',
|
|
15
|
+
notes: 'LOCAL-NOTES',
|
|
16
|
+
};
|
|
17
|
+
return val;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Logger } from '@bitblit/ratchet-common/logger/logger';
|
|
2
|
+
import { BackgroundProcessor } from '../../config/background/background-processor.js';
|
|
3
|
+
import { ErrorRatchet } from '@bitblit/ratchet-common/lang/error-ratchet';
|
|
4
|
+
import { StringRatchet } from '@bitblit/ratchet-common/lang/string-ratchet';
|
|
5
|
+
import { BackgroundManagerLike } from '../../background/manager/background-manager-like.js';
|
|
6
|
+
|
|
7
|
+
export class EchoProcessor implements BackgroundProcessor<any> {
|
|
8
|
+
public static readonly ECHO_PROCESSOR_TYPE_NAME: string = 'EpsilonEcho';
|
|
9
|
+
public readonly typeName: string = EchoProcessor.ECHO_PROCESSOR_TYPE_NAME;
|
|
10
|
+
|
|
11
|
+
public async handleEvent(data: any, _mgr?: BackgroundManagerLike): Promise<void> {
|
|
12
|
+
Logger.info('Echo processing : %j', data);
|
|
13
|
+
if (data && StringRatchet.trimToNull(data['error'])) {
|
|
14
|
+
ErrorRatchet.throwFormattedErr('Forced error : %s', data['error']);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Logger } from '@bitblit/ratchet-common/logger/logger';
|
|
2
|
+
import { BackgroundProcessor } from '../../config/background/background-processor.js';
|
|
3
|
+
import { EchoProcessor } from './echo-processor.js';
|
|
4
|
+
import { BackgroundManagerLike } from '../../background/manager/background-manager-like.js';
|
|
5
|
+
|
|
6
|
+
export class LogAndEnqueueEchoProcessor implements BackgroundProcessor<any> {
|
|
7
|
+
public readonly typeName: string = 'EpsilonLogAndEnqueueEcho';
|
|
8
|
+
|
|
9
|
+
public async handleEvent(data: any, cfg: BackgroundManagerLike): Promise<void> {
|
|
10
|
+
Logger.info('LogAndEnqueueEchoProcessor : %j', data);
|
|
11
|
+
await cfg.fireImmediateProcessRequestByParts(EchoProcessor.ECHO_PROCESSOR_TYPE_NAME, { upstream: data });
|
|
12
|
+
Logger.info('Completed : LogAndEnqueueEchoProcessor');
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Logger } from '@bitblit/ratchet-common/logger/logger';
|
|
2
|
+
import { BackgroundErrorProcessor } from '../../config/background/background-error-processor.js';
|
|
3
|
+
import { InternalBackgroundEntry } from '../../background/internal-background-entry.js';
|
|
4
|
+
|
|
5
|
+
// This is just here to test error processor logic (errors are logged anyway)
|
|
6
|
+
export class LogMessageBackgroundErrorProcessor implements BackgroundErrorProcessor {
|
|
7
|
+
public async handleError(submission: InternalBackgroundEntry<any>, error: Error): Promise<void> {
|
|
8
|
+
Logger.error('-------- ERROR PROCESSED : %j : %s----', submission, error);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BackgroundProcessor } from '../../config/background/background-processor.js';
|
|
2
|
+
import { Logger } from '@bitblit/ratchet-common/logger/logger';
|
|
3
|
+
import { BackgroundManagerLike } from '../../background/manager/background-manager-like.js';
|
|
4
|
+
|
|
5
|
+
export class NoOpProcessor implements BackgroundProcessor<any> {
|
|
6
|
+
public readonly typeName: string = 'EpsilonNoOp';
|
|
7
|
+
|
|
8
|
+
public async handleEvent(_data: any, _mgr?: BackgroundManagerLike): Promise<void> {
|
|
9
|
+
// Does nothing
|
|
10
|
+
Logger.silly('Hit the no-op proc');
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { BackgroundProcessor } from '../../config/background/background-processor.js';
|
|
2
|
+
import { BackgroundManagerLike } from '../../background/manager/background-manager-like.js';
|
|
3
|
+
import { Logger } from '@bitblit/ratchet-common/logger/logger';
|
|
4
|
+
import { PromiseRatchet } from '@bitblit/ratchet-common/lang/promise-ratchet';
|
|
5
|
+
import { StringRatchet } from '@bitblit/ratchet-common/lang/string-ratchet';
|
|
6
|
+
import { NumberRatchet } from '@bitblit/ratchet-common/lang/number-ratchet';
|
|
7
|
+
import { DurationRatchet } from '@bitblit/ratchet-common/lang/duration-ratchet';
|
|
8
|
+
|
|
9
|
+
export class RetryProcessor implements BackgroundProcessor<any> {
|
|
10
|
+
private static readonly RETRY_FIELD_NAME: string = '___RetryProcessorTryNumber';
|
|
11
|
+
|
|
12
|
+
constructor(
|
|
13
|
+
private delegate: BackgroundProcessor<any>,
|
|
14
|
+
private opts: RetryProcessorOptions,
|
|
15
|
+
) {}
|
|
16
|
+
|
|
17
|
+
public get typeName(): string {
|
|
18
|
+
return StringRatchet.trimToEmpty(this.opts?.typePrefix) + this.delegate.typeName + StringRatchet.trimToEmpty(this.opts?.typeSuffix);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public async handleEvent(data: any, mgr: BackgroundManagerLike): Promise<void> {
|
|
22
|
+
const tryNumber: number =
|
|
23
|
+
data && data[RetryProcessor.RETRY_FIELD_NAME] ? NumberRatchet.safeNumber(data[RetryProcessor.RETRY_FIELD_NAME]) : 1;
|
|
24
|
+
const dataCopy: any = data ? Object.assign({}, data) : null;
|
|
25
|
+
//eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
26
|
+
delete dataCopy[RetryProcessor.RETRY_FIELD_NAME];
|
|
27
|
+
Logger.info('RetryProcessor : %s : Try %d of %d', this.delegate.typeName, tryNumber, this.opts.retryCount);
|
|
28
|
+
try {
|
|
29
|
+
await this.delegate.handleEvent(dataCopy, mgr);
|
|
30
|
+
} catch (err) {
|
|
31
|
+
Logger.error('Failed to process : %s', err, err);
|
|
32
|
+
if (tryNumber < this.opts.retryCount) {
|
|
33
|
+
const waitTimeMS: number = tryNumber * this.opts.baseDelayMS;
|
|
34
|
+
Logger.info('Firing automatic retry after a wait of %s', DurationRatchet.formatMsDuration(waitTimeMS));
|
|
35
|
+
await PromiseRatchet.wait(waitTimeMS);
|
|
36
|
+
const wrapped: any = dataCopy || {};
|
|
37
|
+
wrapped[RetryProcessor.RETRY_FIELD_NAME] = tryNumber + 1;
|
|
38
|
+
await mgr.fireImmediateProcessRequestByParts(this.typeName, wrapped);
|
|
39
|
+
} else {
|
|
40
|
+
Logger.error('That was the last try - giving up');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface RetryProcessorOptions {
|
|
47
|
+
retryCount?: number;
|
|
48
|
+
baseDelayMS?: number;
|
|
49
|
+
typePrefix?: string;
|
|
50
|
+
typeSuffix?: string;
|
|
51
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Logger } from '@bitblit/ratchet-common/logger/logger';
|
|
2
|
+
import { PromiseRatchet } from '@bitblit/ratchet-common/lang/promise-ratchet';
|
|
3
|
+
import { BackgroundProcessor } from '../../config/background/background-processor.js';
|
|
4
|
+
import { BackgroundManagerLike } from '../../background/manager/background-manager-like.js';
|
|
5
|
+
|
|
6
|
+
export class SampleDelayProcessor implements BackgroundProcessor<any> {
|
|
7
|
+
public readonly typeName: string = 'EpsilonSampleDelay';
|
|
8
|
+
|
|
9
|
+
public async handleEvent(_data: any, _mgr?: BackgroundManagerLike): Promise<void> {
|
|
10
|
+
const delayMS: number = Math.floor(Math.random() * 5000);
|
|
11
|
+
Logger.info('Running sample processor for %d', delayMS);
|
|
12
|
+
await PromiseRatchet.wait(delayMS);
|
|
13
|
+
Logger.info('Sample processor complete');
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Logger } from '@bitblit/ratchet-common/logger/logger';
|
|
2
|
+
import { BackgroundProcessor } from '../../config/background/background-processor.js';
|
|
3
|
+
import { SampleInputValidatedProcessorData } from './sample-input-validated-processor-data.js';
|
|
4
|
+
import { BackgroundManagerLike } from '../../background/manager/background-manager-like.js';
|
|
5
|
+
|
|
6
|
+
export class SampleInputValidatedProcessor implements BackgroundProcessor<SampleInputValidatedProcessorData> {
|
|
7
|
+
public readonly typeName: string = 'EpsilonSampleInputValidated';
|
|
8
|
+
|
|
9
|
+
public async handleEvent(data: SampleInputValidatedProcessorData, _mgr?: BackgroundManagerLike): Promise<void> {
|
|
10
|
+
Logger.info('Running SampleInputValidatedProcessor, data was : %j', data);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public readonly dataSchemaName: string = 'BackgroundSampleInputValidatedProcessorData';
|
|
14
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { StringRatchet } from '@bitblit/ratchet-common/lang/string-ratchet';
|
|
2
|
+
import { ContextUtil } from '../util/context-util.js';
|
|
3
|
+
|
|
4
|
+
export class BuiltInTraceIdGenerators {
|
|
5
|
+
public static fullAwsRequestId(): string {
|
|
6
|
+
return ContextUtil.defaultedCurrentRequestId();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
public static shortAwsRequestId(): string {
|
|
10
|
+
let rval: string = BuiltInTraceIdGenerators.fullAwsRequestId();
|
|
11
|
+
if (rval.length > 10) {
|
|
12
|
+
let idx: number = rval.lastIndexOf('-');
|
|
13
|
+
idx = idx === -1 ? rval.length - 10 : idx;
|
|
14
|
+
rval = rval.substring(idx);
|
|
15
|
+
}
|
|
16
|
+
return rval;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public static fixedLengthHex(length: number = 10): string {
|
|
20
|
+
return StringRatchet.createRandomHexString(length);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { DaemonProcessState } from '@bitblit/ratchet-aws-node-only/daemon/daemon-process-state';
|
|
2
|
+
import { ExtendedAPIGatewayEvent } from '../../config/http/extended-api-gateway-event.js';
|
|
3
|
+
|
|
4
|
+
export type DaemonAuthorizerFunction = (evt: ExtendedAPIGatewayEvent, proc: DaemonProcessState) => Promise<boolean>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { DaemonAuthorizerFunction } from './daemon-authorizer-function.js';
|
|
2
|
+
import { DaemonGroupSelectionFunction } from './daemon-group-selection-function.js';
|
|
3
|
+
|
|
4
|
+
export interface DaemonConfig {
|
|
5
|
+
authorizer?: DaemonAuthorizerFunction;
|
|
6
|
+
groupSelector?: DaemonGroupSelectionFunction;
|
|
7
|
+
fetchDaemonStatusPathParameter?: string;
|
|
8
|
+
fetchDaemonStatusByPublicTokenPathParameter?: string;
|
|
9
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Logger } from '@bitblit/ratchet-common/logger/logger';
|
|
2
|
+
import { DaemonProcessState } from '@bitblit/ratchet-aws-node-only/daemon/daemon-process-state';
|
|
3
|
+
import { DaemonLike } from '@bitblit/ratchet-aws-node-only/daemon/daemon-like';
|
|
4
|
+
import { DaemonAuthorizerFunction } from './daemon-authorizer-function.js';
|
|
5
|
+
import { ExtendedAPIGatewayEvent } from '../../config/http/extended-api-gateway-event.js';
|
|
6
|
+
import { NotFoundError } from '../../http/error/not-found-error.js';
|
|
7
|
+
import { DaemonProcessStateList } from './daemon-process-state-list.js';
|
|
8
|
+
import { DaemonConfig } from './daemon-config.js';
|
|
9
|
+
import { StringRatchet } from '@bitblit/ratchet-common/lang/string-ratchet';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A helper class to simplify adding Ratchet "Daemon" handling to your application
|
|
13
|
+
*/
|
|
14
|
+
export class DaemonHandler {
|
|
15
|
+
public static readonly ALLOW_EVERYTHING_AUTHORIZER: DaemonAuthorizerFunction = async (
|
|
16
|
+
_evt: ExtendedAPIGatewayEvent,
|
|
17
|
+
_proc: DaemonProcessState,
|
|
18
|
+
) => {
|
|
19
|
+
return true;
|
|
20
|
+
};
|
|
21
|
+
private config: DaemonConfig;
|
|
22
|
+
//private groupSelectionFunction: DaemonGroupSelectionFunction;
|
|
23
|
+
//private authorizer: DaemonAuthorizerFunction;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Initialize the Router
|
|
27
|
+
*/
|
|
28
|
+
constructor(
|
|
29
|
+
private daemon: DaemonLike,
|
|
30
|
+
private inConfig?: DaemonConfig,
|
|
31
|
+
) {
|
|
32
|
+
this.config = inConfig || {};
|
|
33
|
+
this.config.authorizer = this.config.authorizer || DaemonHandler.ALLOW_EVERYTHING_AUTHORIZER;
|
|
34
|
+
this.config.groupSelector = this.config.groupSelector || ((_evt: ExtendedAPIGatewayEvent) => Promise.resolve(daemon.defaultGroup));
|
|
35
|
+
this.config.fetchDaemonStatusByPublicTokenPathParameter =
|
|
36
|
+
StringRatchet.trimToNull(this.config.fetchDaemonStatusByPublicTokenPathParameter) || 'publicToken';
|
|
37
|
+
this.config.fetchDaemonStatusPathParameter = StringRatchet.trimToNull(this.config.fetchDaemonStatusPathParameter) || 'key';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// If you are going to map this function, be sure that your Daemon is setup with a JwtRatchet...
|
|
41
|
+
public async fetchDaemonStatusByPublicToken(evt: ExtendedAPIGatewayEvent): Promise<DaemonProcessState> {
|
|
42
|
+
// TODO: verify has access to this key
|
|
43
|
+
const publicToken: string = evt.pathParameters[this.config.fetchDaemonStatusByPublicTokenPathParameter];
|
|
44
|
+
Logger.info('Fetching daemon status for token: %s', publicToken);
|
|
45
|
+
|
|
46
|
+
let rval: DaemonProcessState = await this.daemon.statFromPublicToken(publicToken);
|
|
47
|
+
const canRead: boolean = rval ? await this.config.authorizer(evt, rval) : false;
|
|
48
|
+
rval = canRead ? rval : null;
|
|
49
|
+
if (rval === null) {
|
|
50
|
+
throw new NotFoundError('No such token : ' + publicToken);
|
|
51
|
+
}
|
|
52
|
+
return rval;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public async fetchDaemonStatus(evt: ExtendedAPIGatewayEvent): Promise<DaemonProcessState> {
|
|
56
|
+
// TODO: verify has access to this key
|
|
57
|
+
const daemonKey: string = evt.pathParameters[this.config.fetchDaemonStatusPathParameter];
|
|
58
|
+
Logger.info('Fetching daemon status for : %s', daemonKey);
|
|
59
|
+
|
|
60
|
+
let rval: DaemonProcessState = await this.daemon.stat(daemonKey);
|
|
61
|
+
const canRead: boolean = rval ? await this.config.authorizer(evt, rval) : false;
|
|
62
|
+
rval = canRead ? rval : null;
|
|
63
|
+
if (rval === null) {
|
|
64
|
+
throw new NotFoundError('No such key : ' + daemonKey);
|
|
65
|
+
}
|
|
66
|
+
return rval;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public async listDaemonStatus(evt: ExtendedAPIGatewayEvent): Promise<DaemonProcessStateList> {
|
|
70
|
+
const group: string = await this.config.groupSelector(evt);
|
|
71
|
+
const keys: DaemonProcessState[] = await this.daemon.list(group);
|
|
72
|
+
const allowed: DaemonProcessState[] = [];
|
|
73
|
+
for (const kVal of keys) {
|
|
74
|
+
//for (let i = 0; i < keys.length; i++) {
|
|
75
|
+
const canRead: boolean = await this.config.authorizer(evt, kVal);
|
|
76
|
+
if (canRead) {
|
|
77
|
+
allowed.push(kVal);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// Token here for future expansion into pagination, not implemented yet
|
|
81
|
+
const rval: DaemonProcessStateList = {
|
|
82
|
+
results: allowed,
|
|
83
|
+
nextToken: null,
|
|
84
|
+
};
|
|
85
|
+
return rval;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { BaseContext } from '@apollo/server';
|
|
2
|
+
import { EpsilonLambdaApolloContextFunctionArgument } from './epsilon-lambda-apollo-context-function-argument.js';
|
|
3
|
+
import { EventUtil } from '../../../http/event-util.js';
|
|
4
|
+
import { DefaultEpsilonApolloContext } from './default-epsilon-apollo-context.js';
|
|
5
|
+
import { Logger } from '@bitblit/ratchet-common/logger/logger';
|
|
6
|
+
import { StringRatchet } from '@bitblit/ratchet-common/lang/string-ratchet';
|
|
7
|
+
import { JwtTokenBase } from '@bitblit/ratchet-common/jwt/jwt-token-base';
|
|
8
|
+
import { ExpiredJwtHandling } from '@bitblit/ratchet-common/jwt/expired-jwt-handling';
|
|
9
|
+
import { EpsilonApolloContextBuilderOptions } from './epsilon-apollo-context-builder-options';
|
|
10
|
+
|
|
11
|
+
export class ApolloUtil {
|
|
12
|
+
// Prevent instantiation
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
14
|
+
private constructor() {}
|
|
15
|
+
|
|
16
|
+
//const defaultContextFn: ContextFunction<[EpsilonLambdaApolloContextFunctionArgument], any> = async () => ({});
|
|
17
|
+
|
|
18
|
+
public static async emptyContext<T extends BaseContext>(): Promise<T> {
|
|
19
|
+
return {} as T;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public static async defaultEpsilonApolloContext(
|
|
23
|
+
args: EpsilonLambdaApolloContextFunctionArgument,
|
|
24
|
+
opts?: EpsilonApolloContextBuilderOptions,
|
|
25
|
+
): Promise<DefaultEpsilonApolloContext<any>> {
|
|
26
|
+
const authTokenSt: string = EventUtil.extractBearerTokenFromEvent(args.lambdaEvent);
|
|
27
|
+
let token: JwtTokenBase = null;
|
|
28
|
+
if (StringRatchet.trimToNull(authTokenSt) && opts?.jwtRatchet) {
|
|
29
|
+
Logger.debug('Proc token : %s', StringRatchet.obscure(authTokenSt));
|
|
30
|
+
token = await opts.jwtRatchet.decodeToken(authTokenSt, ExpiredJwtHandling.RETURN_NULL);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const rval: DefaultEpsilonApolloContext<any> = {
|
|
34
|
+
user: token,
|
|
35
|
+
bearerTokenString: authTokenSt,
|
|
36
|
+
headers: args.lambdaEvent.headers,
|
|
37
|
+
functionName: args.lambdaContext.functionName,
|
|
38
|
+
lambdaEvent: args.lambdaEvent,
|
|
39
|
+
lambdaContext: args.lambdaContext,
|
|
40
|
+
};
|
|
41
|
+
return rval;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { APIGatewayEvent, Context } from 'aws-lambda';
|
|
2
|
+
import { JwtTokenBase } from '@bitblit/ratchet-common/jwt/jwt-token-base';
|
|
3
|
+
|
|
4
|
+
export interface DefaultEpsilonApolloContext<T extends JwtTokenBase> {
|
|
5
|
+
user?: T;
|
|
6
|
+
bearerTokenString?: string;
|
|
7
|
+
headers: Record<string, string>;
|
|
8
|
+
functionName: string;
|
|
9
|
+
lambdaEvent: APIGatewayEvent;
|
|
10
|
+
lambdaContext: Context;
|
|
11
|
+
}
|