/
handleIPCMessage.ts
129 lines (118 loc) · 3.14 KB
/
handleIPCMessage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { callProcedure, TRPCError } from '@trpc/server';
import type { AnyRouter, inferRouterContext } from '@trpc/server';
import type { TRPCResponseMessage } from '@trpc/server/rpc';
import type { IpcMainEvent } from 'electron';
import { isObservable, Unsubscribable } from '@trpc/server/observable';
import { transformTRPCResponse } from '@trpc/server/shared';
import { getTRPCErrorFromUnknown } from './utils';
import { CreateContextOptions } from './types';
import { ELECTRON_TRPC_CHANNEL } from '../constants';
import { ETRPCRequest } from '../types';
import debugFactory from 'debug';
const debug = debugFactory('electron-trpc:main:handleIPCMessage');
export async function handleIPCMessage<TRouter extends AnyRouter>({
router,
createContext,
internalId,
message,
event,
subscriptions,
}: {
router: TRouter;
createContext?: (opts: CreateContextOptions) => Promise<inferRouterContext<TRouter>>;
internalId: string;
message: ETRPCRequest;
event: IpcMainEvent;
subscriptions: Map<string, Unsubscribable>;
}) {
if (message.method === 'subscription.stop') {
const subscription = subscriptions.get(internalId);
if (!subscription) {
return;
}
subscription.unsubscribe();
subscriptions.delete(internalId);
return;
}
const { type, input: serializedInput, path, id } = message.operation;
const input = serializedInput
? router._def._config.transformer.input.deserialize(serializedInput)
: undefined;
const ctx = (await createContext?.({ event })) ?? {};
const respond = (response: TRPCResponseMessage) => {
if (event.sender.isDestroyed()) return;
event.reply(ELECTRON_TRPC_CHANNEL, transformTRPCResponse(router._def._config, response));
};
try {
const result = await callProcedure({
ctx,
path,
procedures: router._def.procedures,
rawInput: input,
type,
});
if (type !== 'subscription') {
respond({
id,
result: {
type: 'data',
data: result,
},
});
return;
} else {
if (!isObservable(result)) {
throw new TRPCError({
message: `Subscription ${path} did not return an observable`,
code: 'INTERNAL_SERVER_ERROR',
});
}
}
const subscription = result.subscribe({
next(data) {
respond({
id,
result: {
type: 'data',
data,
},
});
},
error(err) {
const error = getTRPCErrorFromUnknown(err);
respond({
id,
error: router.getErrorShape({
error,
type,
path,
input,
ctx,
}),
});
},
complete() {
respond({
id,
result: {
type: 'stopped',
},
});
},
});
debug('Creating subscription', internalId);
subscriptions.set(internalId, subscription);
} catch (cause) {
const error: TRPCError = getTRPCErrorFromUnknown(cause);
return respond({
id,
error: router.getErrorShape({
error,
type,
path,
input,
ctx,
}),
});
}
}