Skip to content

Commit f9285ce

Browse files
committed
wip: resolver
1 parent 797cdbe commit f9285ce

15 files changed

+64
-62
lines changed

packages/api-sync-system/src/resolver/createEventHandlerPlugin.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,15 @@ export const createEventHandlerPlugin = (params: ICreateEventHandlerPluginParams
3535
console.log("Resolver handler started.");
3636
console.log(
3737
JSON.stringify({
38-
records: event.Records
38+
event
3939
})
4040
);
41+
/**
42+
* Just end
43+
*/
44+
if (event.Records) {
45+
return;
46+
}
4147
try {
4248
const fetcher = createFetcher({
4349
maxRetries: 10,

packages/api-sync-system/src/sync/attachToDynamoDbDocument.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ export const decorateClientWithHandler = (
2525
* Let's check for the handler and then skip attaching.
2626
*/
2727
// @ts-expect-error
28-
if (client.__webinyHandler) {
28+
if (client.__webinyHandler?.id === handler.id) {
2929
return client;
3030
}
31+
3132
const originalSend = client.send;
3233
const originalPut = client.put;
3334
const originalBatchWrite = client.batchWrite;
@@ -68,7 +69,7 @@ export const decorateClientWithHandler = (
6869
};
6970

7071
export const attachToDynamoDbDocument = ({ handler }: IAttachToDynamoDbDocumentParams): void => {
71-
decorateDocumentClient(client => {
72+
return decorateDocumentClient(client => {
7273
return decorateClientWithHandler({
7374
handler,
7475
client

packages/api-sync-system/src/sync/createSendDataToEventBridgeOnRequestEnd.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { IHandler } from "./types.js";
22
import { convertException } from "@webiny/utils";
33
import { createOnRequestResponse, createOnRequestTimeout } from "@webiny/handler";
4+
import type { Request as FastifyRequest } from "@webiny/handler/types";
45

56
const execute = async (handler: IHandler) => {
67
try {
@@ -15,12 +16,26 @@ const execute = async (handler: IHandler) => {
1516
}
1617
};
1718

19+
const isOptionsRequest = (request: FastifyRequest): boolean => {
20+
return request.method.toUpperCase() === "OPTIONS";
21+
};
22+
1823
export const createSendDataToEventBridgeOnRequestEnd = (handler: IHandler) => {
1924
return [
20-
createOnRequestResponse(async () => {
25+
createOnRequestResponse(async request => {
26+
if (isOptionsRequest(request)) {
27+
console.log("Skipping OPTIONS request - response.");
28+
return;
29+
}
30+
console.log("Executing on request end.");
2131
return execute(handler);
2232
}),
23-
createOnRequestTimeout(async () => {
33+
createOnRequestTimeout(async request => {
34+
if (isOptionsRequest(request)) {
35+
console.log("Skipping OPTIONS request - timeout.");
36+
return;
37+
}
38+
console.log("Executing on request timeout.");
2439
return execute(handler);
2540
})
2641
];

packages/api-sync-system/src/sync/handler/Handler.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { PutEventsCommand } from "@webiny/aws-sdk/client-eventbridge";
1414
import { convertException } from "@webiny/utils";
1515
import type { IDetail } from "./types.js";
1616
import { SQS_EVENT_NAME } from "~/constants.js";
17+
import { generateAlphaNumericId } from "@webiny/utils/generateId.js";
1718

1819
export interface IHandlerEventBus {
1920
name: string;
@@ -28,6 +29,7 @@ export interface IHandlerParams {
2829
}
2930

3031
export class Handler implements IHandler {
32+
public readonly id = generateAlphaNumericId();
3133
private readonly system: ISystem;
3234
private readonly client: Pick<EventBridgeClient, "send">;
3335
private commands: ICommandValue[] = [];

packages/api-sync-system/src/sync/handler/HandlerConverter.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,6 @@ export class HandlerConverter implements IHandlerConverter {
3232
return converter.convert(command);
3333
}
3434
}
35-
/**
36-
* We ALWAYS log unknown commands as we do not know what user will do with the DynamoDB client.
37-
*/
38-
console.error(`Unknown command: ${command.constructor?.name || "unknown"}`);
39-
console.log(
40-
JSON.stringify({
41-
command
42-
})
43-
);
4435

4536
return this._default;
4637
}

packages/api-sync-system/src/sync/handler/converter/BatchGetCommandConverter.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

packages/api-sync-system/src/sync/handler/converter/GetCommandConverter.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

packages/api-sync-system/src/sync/handler/converter/commands/NullCommandValue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export class NullCommandValue implements ICommandValue {
44
/**
55
* Does not matter what will be here, we will not use it.
66
*/
7-
public readonly command = "put";
7+
public readonly command = "null";
88

99
public getItems(): null {
1010
return null;

packages/api-sync-system/src/sync/requestPlugin.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export const createSyncSystemHandlerOnRequestPlugin = (
2121
return createHandlerOnRequest(async (_, __, context) => {
2222
const { data: manifest, error } = await getManifest(params);
2323
if (!manifest?.sync?.region || error) {
24+
console.log(
25+
JSON.stringify({
26+
error,
27+
noManifest: manifest
28+
})
29+
);
2430
return;
2531
}
2632

packages/api-sync-system/src/sync/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export interface ISystem {
6161
}
6262

6363
export interface IHandler {
64+
readonly id: string;
6465
flush(): Promise<void>;
6566
add(input: IDynamoDbCommand): void;
6667
}

packages/api-sync-system/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Context as BaseContext } from "@webiny/handler-aws/types";
22

33
export type CommandType = "put" | "delete";
44
export type ExtendedCommandType = "put" | "delete" | "update";
5-
export type AllCommandType = ExtendedCommandType | "batchWrite";
5+
export type AllCommandType = ExtendedCommandType | "batchWrite" | "null";
66

77
export type DynamoDBTableType = "regular" | "elasticsearch" | "log" | "unknown";
88

packages/aws-sdk/src/client-dynamodb/getDocumentClient.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,17 @@ export const getDocumentClient = (input?: DynamoDBClientConfig): DynamoDBDocumen
3535
const config = input || DEFAULT_CONFIG;
3636
const key = createKey(config);
3737
if (documentClients[key]) {
38+
console.log({
39+
existingKey: key
40+
});
3841
return applyDecoration(documentClients[key]);
3942
}
4043
const client = new DynamoDBClient(config);
4144

4245
const documentClient = DynamoDBDocument.from(client, documentClientConfig);
46+
console.log({
47+
newKey: key
48+
});
4349

4450
return (documentClients[key] = applyDecoration(documentClient));
4551
};
@@ -48,13 +54,21 @@ export const getDocumentClient = (input?: DynamoDBClientConfig): DynamoDBDocumen
4854
*/
4955
const applyDecoration = (client: DynamoDBDocument): DynamoDBDocument => {
5056
if (!decorateDocumentClientCallable) {
57+
console.log("No decoration function provided.");
5158
return client;
5259
}
60+
console.log("applying decoration");
5361
// @ts-expect-error
5462
client.__decoratedByWebiny = true;
5563
return decorateDocumentClientCallable(client);
5664
};
5765

5866
export const decorateDocumentClient = (cb: IDecorateDocumentClientCallable): void => {
5967
decorateDocumentClientCallable = cb;
68+
/**
69+
* Decorate already existing clients.
70+
*/
71+
for (const key in documentClients) {
72+
cb(documentClients[key]);
73+
}
6074
};

packages/handler/src/fastify.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,22 +408,22 @@ export const createHandler = (params: CreateHandlerParams) => {
408408
/**
409409
* We need to output the benchmark results at the end of the request in both response and timeout cases
410410
*/
411-
app.addHook("onResponse", async () => {
411+
app.addHook("onResponse", async (request, reply) => {
412412
const plugins = app.webiny.plugins.byType<OnRequestResponsePlugin>(
413413
OnRequestResponsePlugin.type
414414
);
415415
for (const plugin of plugins) {
416-
await plugin.exec();
416+
await plugin.exec(request, reply);
417417
}
418418
await context.benchmark.output();
419419
});
420420

421-
app.addHook("onTimeout", async () => {
421+
app.addHook("onTimeout", async (request, reply) => {
422422
const plugins = app.webiny.plugins.byType<OnRequestTimeoutPlugin>(
423423
OnRequestTimeoutPlugin.type
424424
);
425425
for (const plugin of plugins) {
426-
await plugin.exec();
426+
await plugin.exec(request, reply);
427427
}
428428
await context.benchmark.output();
429429
});

packages/handler/src/plugins/OnRequestResponsePlugin.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Plugin } from "@webiny/plugins";
2+
import type { Reply as FastifyReply, Request as FastifyRequest } from "~/types.js";
23

34
export interface IOnRequestResponsePluginCallable {
4-
(): Promise<void>;
5+
(request: FastifyRequest, reply: FastifyReply): Promise<void>;
56
}
67

78
export class OnRequestResponsePlugin extends Plugin {
@@ -14,8 +15,8 @@ export class OnRequestResponsePlugin extends Plugin {
1415
this.cb = cb;
1516
}
1617

17-
public async exec(): Promise<void> {
18-
return this.cb();
18+
public async exec(request: FastifyRequest, reply: FastifyReply): Promise<void> {
19+
return this.cb(request, reply);
1920
}
2021
}
2122

packages/handler/src/plugins/OnRequestTimeoutPlugin.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Plugin } from "@webiny/plugins";
2+
import type { Reply as FastifyReply, Request as FastifyRequest } from "~/types.js";
23

34
export interface IOnRequestTimeoutPluginCallable {
4-
(): Promise<void>;
5+
(request: FastifyRequest, reply: FastifyReply): Promise<void>;
56
}
67

78
export class OnRequestTimeoutPlugin extends Plugin {
@@ -14,8 +15,8 @@ export class OnRequestTimeoutPlugin extends Plugin {
1415
this.cb = cb;
1516
}
1617

17-
public async exec(): Promise<void> {
18-
return this.cb();
18+
public async exec(request: FastifyRequest, reply: FastifyReply): Promise<void> {
19+
return this.cb(request, reply);
1920
}
2021
}
2122

0 commit comments

Comments
 (0)