@mstuercke/api-utils 5.2.0 → 5.2.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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mstuercke/api-utils",
|
|
3
|
-
"version": "5.2.
|
|
3
|
+
"version": "5.2.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"files": [
|
|
@@ -53,12 +53,12 @@
|
|
|
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.377",
|
|
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.377",
|
|
62
62
|
"@types/aws-lambda": "^8.10.138",
|
|
63
63
|
"@types/bun": "^1.1.3",
|
|
64
64
|
"@types/path-to-regexp": "^1.7.0",
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import {PostToConnectionCommand} from '@aws-sdk/client-apigatewaymanagementapi';
|
|
2
2
|
import {AnyTRPCRouter, callTRPCProcedure, inferRouterContext} from '@trpc/server';
|
|
3
|
-
import {
|
|
4
|
-
import {isAsyncIterable} from '@trpc/server/unstable-core-do-not-import';
|
|
3
|
+
import type {Observable} from '@trpc/server/observable';
|
|
5
4
|
import {APIGatewayProxyEvent, APIGatewayProxyResult} from 'aws-lambda';
|
|
6
5
|
import {getApiGatewayManagementApiClient} from '../getApiGatewayManagementApiClient';
|
|
7
6
|
import {getConnection} from '../getConnection';
|
|
@@ -48,20 +47,49 @@ export const awsLambdaTrpcSubscriptionRequestHandler = <Router extends AnyTRPCRo
|
|
|
48
47
|
result: {type: 'started'},
|
|
49
48
|
});
|
|
50
49
|
|
|
51
|
-
const
|
|
50
|
+
const sub = (await callTRPCProcedure({
|
|
52
51
|
procedures: router._def.procedures,
|
|
53
52
|
path: request.params.path,
|
|
54
53
|
type: 'subscription',
|
|
55
54
|
ctx: await createContext?.(event) || {},
|
|
56
55
|
getRawInput: async () => request.params.input,
|
|
57
|
-
}))
|
|
56
|
+
})) as Observable<unknown, unknown>;
|
|
57
|
+
|
|
58
|
+
const messageQueue: TrpcSubscriptionResponse[] = [];
|
|
59
|
+
sub.subscribe({
|
|
60
|
+
next: (data: unknown) => {
|
|
61
|
+
messageQueue.push({
|
|
62
|
+
id: request.id,
|
|
63
|
+
result: {
|
|
64
|
+
type: 'data',
|
|
65
|
+
data: data,
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
error: (err) => {
|
|
70
|
+
console.error(err);
|
|
71
|
+
messageQueue.push({
|
|
72
|
+
id: request.id,
|
|
73
|
+
result: {type: 'stopped'},
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
complete: () => {
|
|
77
|
+
messageQueue.push({
|
|
78
|
+
id: request.id,
|
|
79
|
+
result: {type: 'stopped'},
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
let stopped = false;
|
|
85
|
+
while (!stopped) {
|
|
86
|
+
const message = messageQueue.shift();
|
|
87
|
+
if (!message)
|
|
88
|
+
continue;
|
|
89
|
+
|
|
90
|
+
await sendMessage(message);
|
|
58
91
|
|
|
59
|
-
|
|
60
|
-
await handleAsyncGenerator(procedure, request, sendMessage, stopConnection);
|
|
61
|
-
} else if (isObservable(procedure)) {
|
|
62
|
-
await handleObservable(procedure, request, sendMessage);
|
|
63
|
-
} else {
|
|
64
|
-
throw 'Unable to determine subscription type';
|
|
92
|
+
stopped = message.result.type === 'stopped';
|
|
65
93
|
}
|
|
66
94
|
}
|
|
67
95
|
} catch (err) {
|
|
@@ -75,70 +103,3 @@ export const awsLambdaTrpcSubscriptionRequestHandler = <Router extends AnyTRPCRo
|
|
|
75
103
|
};
|
|
76
104
|
};
|
|
77
105
|
};
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const handleObservable = async (
|
|
81
|
-
observable: Observable<unknown, unknown>,
|
|
82
|
-
request: TrpcSubscriptionRequest,
|
|
83
|
-
sendMessage: (message: TrpcSubscriptionResponse) => Promise<void>,
|
|
84
|
-
) => {
|
|
85
|
-
const messageQueue: TrpcSubscriptionResponse[] = [];
|
|
86
|
-
observable.subscribe({
|
|
87
|
-
next: (data: unknown) => {
|
|
88
|
-
messageQueue.push({
|
|
89
|
-
id: request.id,
|
|
90
|
-
result: {
|
|
91
|
-
type: 'data',
|
|
92
|
-
data: data,
|
|
93
|
-
},
|
|
94
|
-
});
|
|
95
|
-
},
|
|
96
|
-
error: (err) => {
|
|
97
|
-
console.error(err);
|
|
98
|
-
messageQueue.push({
|
|
99
|
-
id: request.id,
|
|
100
|
-
result: {type: 'stopped'},
|
|
101
|
-
});
|
|
102
|
-
},
|
|
103
|
-
complete: () => {
|
|
104
|
-
messageQueue.push({
|
|
105
|
-
id: request.id,
|
|
106
|
-
result: {type: 'stopped'},
|
|
107
|
-
});
|
|
108
|
-
},
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
let stopped = false;
|
|
112
|
-
while (!stopped) {
|
|
113
|
-
const message = messageQueue.shift();
|
|
114
|
-
if (!message)
|
|
115
|
-
continue;
|
|
116
|
-
|
|
117
|
-
await sendMessage(message);
|
|
118
|
-
|
|
119
|
-
stopped = message.result.type === 'stopped';
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
const handleAsyncGenerator = async (
|
|
124
|
-
generator: AsyncIterable<unknown>,
|
|
125
|
-
request: TrpcSubscriptionRequest,
|
|
126
|
-
sendMessage: (message: TrpcSubscriptionResponse) => Promise<void>,
|
|
127
|
-
stopConnection: () => Promise<void>
|
|
128
|
-
) => {
|
|
129
|
-
try {
|
|
130
|
-
for await (const data of generator) {
|
|
131
|
-
await sendMessage({
|
|
132
|
-
id: request.id,
|
|
133
|
-
result: {
|
|
134
|
-
type: 'data',
|
|
135
|
-
data: data,
|
|
136
|
-
},
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
} catch (err) {
|
|
140
|
-
console.error(err);
|
|
141
|
-
} finally {
|
|
142
|
-
await stopConnection();
|
|
143
|
-
}
|
|
144
|
-
};
|