forked from jlalmes/trpc-chrome
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathchrome.ts
133 lines (114 loc) · 4.8 KB
/
chrome.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
130
131
132
133
import { AnyProcedure, AnyRouter, TRPCError } from '@trpc/server';
import { Unsubscribable, isObservable } from '@trpc/server/observable';
import { getErrorShape } from '@trpc/server/shared';
import { isTRPCRequestWithId } from '../shared/trpcMessage';
import type { TRPCChromeResponse } from '../types';
import type { CreateHandlerOptions } from './base';
import { getErrorFromUnknown } from './errors';
export type CreateChromeContextOptions = {
req: chrome.runtime.Port;
res: undefined;
};
type ChromeOptions = {
chrome?: typeof chrome;
};
type ChromeContextOptions = { req: chrome.runtime.Port; res: undefined };
export const createChromeHandler = <TRouter extends AnyRouter>(
opts: CreateHandlerOptions<TRouter, ChromeContextOptions, ChromeOptions>,
) => {
const { router, createContext, onError, chrome = global.chrome } = opts;
if (!chrome) {
console.warn("Skipping chrome handler creation: 'opts.chrome' not defined");
return;
}
chrome.runtime.onConnect.addListener((port) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const { transformer } = router._def._config;
const subscriptions = new Map<number | string, Unsubscribable>();
const listeners: (() => void)[] = [];
const cleanup = () => listeners.forEach((unsub) => unsub());
port.onDisconnect.addListener(cleanup);
listeners.push(() => port.onDisconnect.removeListener(cleanup));
const onMessage = async (message: unknown) => {
if (!port || !isTRPCRequestWithId(message)) return;
const { trpc } = message;
const sendResponse = (response: TRPCChromeResponse['trpc']) => {
port.postMessage({
trpc: { id: trpc.id, jsonrpc: trpc.jsonrpc, ...response },
} as TRPCChromeResponse);
};
if (trpc.method === 'subscription.stop') {
subscriptions.get(trpc.id)?.unsubscribe();
subscriptions.delete(trpc.id);
return sendResponse({ result: { type: 'stopped' } });
}
const { method, params, id } = trpc;
const ctx = await createContext?.({ req: port, res: undefined });
const handleError = (cause: unknown) => {
const error = getErrorFromUnknown(cause);
onError?.({
error,
type: method,
path: params.path,
input: params.input,
ctx,
req: port,
});
sendResponse({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
error: getErrorShape({
config: router._def._config,
error,
type: method,
path: params.path,
input: params.input,
ctx,
}),
});
};
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
const input = transformer.input.deserialize(trpc.params.input);
const caller = router.createCaller(ctx);
const procedureFn = trpc.params.path
.split('.')
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-explicit-any
.reduce((acc, segment) => acc[segment], caller as any) as AnyProcedure;
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const result = await procedureFn(input);
if (trpc.method !== 'subscription') {
return sendResponse({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
result: { type: 'data', data: transformer.output.serialize(result) },
});
}
if (!isObservable(result)) {
throw new TRPCError({
message: `Subscription ${params.path} did not return an observable`,
code: 'INTERNAL_SERVER_ERROR',
});
}
const subscription = result.subscribe({
next: (data) => {
const serializedData = transformer.output.serialize(data);
sendResponse({ result: { type: 'data', data: serializedData } });
},
error: handleError,
complete: () => sendResponse({ result: { type: 'stopped' } }),
});
if (subscriptions.has(id)) {
subscription.unsubscribe();
sendResponse({ result: { type: 'stopped' } });
throw new TRPCError({ message: `Duplicate id ${id}`, code: 'BAD_REQUEST' });
}
listeners.push(() => subscription.unsubscribe());
subscriptions.set(id, subscription);
sendResponse({ result: { type: 'started' } });
} catch (cause) {
handleError(cause);
}
};
port.onMessage.addListener(onMessage);
listeners.push(() => port.onMessage.removeListener(onMessage));
});
};