@mstuercke/api-utils 5.2.1 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mstuercke/api-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"files": [
|
|
@@ -53,17 +53,17 @@
|
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"@aws-sdk/client-apigatewaymanagementapi": "^3.438.0",
|
|
56
|
-
"@trpc/server": "^11.0.0-rc.
|
|
56
|
+
"@trpc/server": "^11.0.0-rc.390",
|
|
57
57
|
"@types/aws-lambda": "^8.10.138"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
60
|
"@mstuercke/bun-utils": "6.0.2",
|
|
61
|
-
"@trpc/server": "^11.0.0-rc.
|
|
61
|
+
"@trpc/server": "^11.0.0-rc.390",
|
|
62
62
|
"@types/aws-lambda": "^8.10.138",
|
|
63
63
|
"@types/bun": "^1.1.3",
|
|
64
64
|
"@types/path-to-regexp": "^1.7.0",
|
|
65
65
|
"@types/react": "^18.3.3",
|
|
66
|
-
"aws-sdk-client-mock": "^4.0.
|
|
66
|
+
"aws-sdk-client-mock": "^4.0.1",
|
|
67
67
|
"typescript": "^5.4.5",
|
|
68
68
|
"zod": "^3.23.8"
|
|
69
69
|
},
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {PostToConnectionCommand} from '@aws-sdk/client-apigatewaymanagementapi';
|
|
2
2
|
import {AnyTRPCRouter, callTRPCProcedure, inferRouterContext} from '@trpc/server';
|
|
3
|
-
import
|
|
4
|
-
import {APIGatewayProxyEvent, APIGatewayProxyResult} from 'aws-lambda';
|
|
3
|
+
import {APIGatewayProxyEvent, APIGatewayProxyResult, Context} from 'aws-lambda';
|
|
5
4
|
import {getApiGatewayManagementApiClient} from '../getApiGatewayManagementApiClient';
|
|
6
5
|
import {getConnection} from '../getConnection';
|
|
7
6
|
import {TrpcSubscriptionRequest} from './TrpcSubscriptionRequest';
|
|
8
7
|
import {TrpcSubscriptionResponse} from './TrpcSubscriptionResponse';
|
|
8
|
+
import {isObservable} from './isObservable';
|
|
9
9
|
|
|
10
10
|
export const awsLambdaTrpcSubscriptionRequestHandler = <Router extends AnyTRPCRouter>(opts: {
|
|
11
11
|
router: Router
|
|
@@ -13,7 +13,8 @@ export const awsLambdaTrpcSubscriptionRequestHandler = <Router extends AnyTRPCRo
|
|
|
13
13
|
}) => {
|
|
14
14
|
const {router, createContext} = opts;
|
|
15
15
|
|
|
16
|
-
return async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
|
|
16
|
+
return async (event: APIGatewayProxyEvent, context: Context): Promise<APIGatewayProxyResult> => {
|
|
17
|
+
context.callbackWaitsForEmptyEventLoop = false;
|
|
17
18
|
const connection = getConnection(event);
|
|
18
19
|
|
|
19
20
|
if (!event.body || event.body === '[]')
|
|
@@ -37,26 +38,32 @@ export const awsLambdaTrpcSubscriptionRequestHandler = <Router extends AnyTRPCRo
|
|
|
37
38
|
};
|
|
38
39
|
|
|
39
40
|
try {
|
|
41
|
+
const messageQueue: TrpcSubscriptionResponse[] = [];
|
|
42
|
+
|
|
40
43
|
if (request.method === 'subscription.stop') {
|
|
41
44
|
await stopConnection();
|
|
42
45
|
}
|
|
43
46
|
|
|
44
47
|
if (request.method === 'subscription') {
|
|
45
|
-
|
|
48
|
+
messageQueue.push({
|
|
46
49
|
id: request.id,
|
|
47
50
|
result: {type: 'started'},
|
|
48
51
|
});
|
|
49
52
|
|
|
50
|
-
const
|
|
53
|
+
const procedure = (await callTRPCProcedure({
|
|
51
54
|
procedures: router._def.procedures,
|
|
52
55
|
path: request.params.path,
|
|
53
56
|
type: 'subscription',
|
|
54
57
|
ctx: await createContext?.(event) || {},
|
|
55
58
|
getRawInput: async () => request.params.input,
|
|
56
|
-
}))
|
|
59
|
+
}));
|
|
60
|
+
|
|
61
|
+
if (!isObservable(procedure)) {
|
|
62
|
+
console.error(`observable`, procedure);
|
|
63
|
+
throw `Procedure is not an observable!`;
|
|
64
|
+
}
|
|
57
65
|
|
|
58
|
-
|
|
59
|
-
sub.subscribe({
|
|
66
|
+
procedure.subscribe({
|
|
60
67
|
next: (data: unknown) => {
|
|
61
68
|
messageQueue.push({
|
|
62
69
|
id: request.id,
|
|
@@ -83,6 +90,8 @@ export const awsLambdaTrpcSubscriptionRequestHandler = <Router extends AnyTRPCRo
|
|
|
83
90
|
|
|
84
91
|
let stopped = false;
|
|
85
92
|
while (!stopped) {
|
|
93
|
+
if (messageQueue.length === 0)
|
|
94
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
86
95
|
const message = messageQueue.shift();
|
|
87
96
|
if (!message)
|
|
88
97
|
continue;
|