/
externalProtocol.ts
209 lines (178 loc) · 6.39 KB
/
externalProtocol.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright (C) 2021 PixieBrix, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { uuidv4 } from "@/types/helpers";
import {
HandlerEntry,
HandlerOptions,
isErrorResponse,
SerializableResponse,
toErrorResponse,
} from "@/messaging/protocol";
import pTimeout from "p-timeout";
import oneMutation from "one-mutation";
import { isContentScript, isExtensionContext } from "webext-detect-page";
import { deserializeError } from "serialize-error";
import { ContentScriptActionError } from "@/contentScript/backgroundProtocol";
import { PIXIEBRIX_READY_ATTRIBUTE } from "@/contentScript/context";
import { expectContentScript } from "@/utils/expectContext";
// Context for this protocol:
// - Implemented and explained in https://github.com/pixiebrix/pixiebrix-extension/pull/1019
// - It's only one-way from the app to the content scripts
// - The communication happens via posted messages
// - Messages **are received by both ends**, so they use the `type` property as a differentiator
// - The app sends messages with a `type` property
// - The content script responds without `type` property
// TODO: The content script should load within 200ms, but when the devtools are open it could take 4-5 seconds.
// Find out why https://github.com/pixiebrix/pixiebrix-extension/pull/1019#discussion_r684894579
const POLL_READY_TIMEOUT =
process.env.ENVIRONMENT === "development" ? 6000 : 2000;
// TODO: Some handlers could take longer, so it needs to be refactored.
// https://github.com/pixiebrix/pixiebrix-extension/issues/1015
const RESPONSE_TIMEOUT_MS = 2000;
const MESSAGE_PREFIX = "@@pixiebrix/external/";
interface Message<R = unknown> {
type: string;
payload: R;
meta: {
nonce: string;
};
}
interface Response<R = unknown> {
payload: R;
meta: {
nonce: string;
};
}
const contentScriptHandlers = new Map<string, HandlerEntry>();
async function waitExtensionLoaded(): Promise<void> {
if (document.documentElement.hasAttribute(PIXIEBRIX_READY_ATTRIBUTE)) {
return;
}
await pTimeout(
oneMutation(document.documentElement, {
attributes: true,
attributeFilter: [PIXIEBRIX_READY_ATTRIBUTE],
}),
POLL_READY_TIMEOUT,
`The extension did not load within ${POLL_READY_TIMEOUT / 1000}s`
);
}
/**
* The message is also received by the same context, so it must detect that and
* ignore its own messages. Currently it uses the `type` property as a differentiator:
* The lack of it means it's a response from the content script
*/
function sendMessageToOtherSide(message: Message | Response) {
document.defaultView.postMessage(message, document.defaultView.origin);
}
/** Content script handler for messages from app */
async function onContentScriptReceiveMessage(
event: MessageEvent<Message>
): Promise<void> {
expectContentScript();
if (event.source !== document.defaultView) {
// The message comes from other views (PB does not send these)
return;
}
const { type, meta, payload } = event.data;
if (!type || !Array.isArray(payload)) {
// It might be a response that `onContentScriptReceiveMessage` itself sent
return;
}
const { handler, options } = contentScriptHandlers.get(type) ?? {};
if (!handler) {
// Handler not registered, it might be handled elsewhere
return;
}
if (!options.asyncResponse) {
void handler(...payload).catch((error: unknown) => {
console.warn(`${type}: ${meta.nonce}: Notification error`, error);
});
}
const response: Response = {
meta,
payload: null,
};
try {
response.payload = await handler(...payload);
console.debug(`${type}: ${meta.nonce}: Handler success`);
} catch (error: unknown) {
response.payload = toErrorResponse(type, error);
console.warn(`${type}: ${meta.nonce}: Handler error`, error);
}
sendMessageToOtherSide(response);
}
/** Set up listener for specific message via nonce */
async function oneResponse<R>(nonce: string): Promise<R> {
return new Promise((resolve) => {
function onMessage(event: MessageEvent) {
// Responses *must not* have a `type`, but just a `nonce` and `payload`
if (!event.data?.type && event.data?.meta?.nonce === nonce) {
document.defaultView.removeEventListener("message", onMessage);
resolve(event.data.payload);
}
}
document.defaultView.addEventListener("message", onMessage);
});
}
export function liftExternal<
TArguments extends unknown[],
R extends SerializableResponse
>(
type: string,
method: (...args: TArguments) => Promise<R>,
options: HandlerOptions = {}
): (...args: TArguments) => Promise<R> {
const fullType = `${MESSAGE_PREFIX}${type}`;
// Set defaults
options = {
asyncResponse: true,
...options,
};
if (isContentScript()) {
// Register global handler; Automatically deduplicated
document.defaultView.addEventListener(
"message",
onContentScriptReceiveMessage
);
contentScriptHandlers.set(fullType, { handler: method, options });
console.debug(`${fullType}: Installed content script handler`);
return method;
}
return async (...args: TArguments) => {
if (isExtensionContext()) {
throw new ContentScriptActionError("Expected call from external page");
}
await waitExtensionLoaded();
const nonce = uuidv4();
const message: Message = {
type: fullType,
payload: args,
meta: { nonce },
};
console.debug(`${fullType}: Sending from app`, message);
sendMessageToOtherSide(message);
// Communication is completely asynchronous; This sets up response for the specific nonce
const payload = await pTimeout(oneResponse<R>(nonce), RESPONSE_TIMEOUT_MS);
if (isErrorResponse(payload)) {
throw deserializeError(payload.$$error);
}
return payload;
};
}
/** @deprecated The registration now happens automatically */
export default () => {};