-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.ts
More file actions
232 lines (209 loc) · 8.29 KB
/
Copy pathprotocol.ts
File metadata and controls
232 lines (209 loc) · 8.29 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { isPluginProcessProxy } from './error.js';
export const PLUGIN_PROCESS_PROTOCOL = 1 as const;
const MAX_PAYLOAD_DEPTH = 32;
const MAX_PAYLOAD_BYTES = 1024 * 1024;
export interface PluginProcessErrorPayload {
code: string;
message: string;
retryable?: boolean;
retryAfterMs?: number;
}
export interface PluginProcessRequest {
protocol: typeof PLUGIN_PROCESS_PROTOCOL;
generation: string;
kind: 'request';
requestId: string;
method: string;
payload: unknown;
}
export interface PluginProcessResponseSuccess {
protocol: typeof PLUGIN_PROCESS_PROTOCOL;
generation: string;
kind: 'response';
requestId: string;
ok: true;
payload: unknown;
}
export interface PluginProcessResponseFailure {
protocol: typeof PLUGIN_PROCESS_PROTOCOL;
generation: string;
kind: 'response';
requestId: string;
ok: false;
error: PluginProcessErrorPayload;
}
export type PluginProcessResponse = PluginProcessResponseSuccess | PluginProcessResponseFailure;
export interface PluginProcessEvent {
protocol: typeof PLUGIN_PROCESS_PROTOCOL;
generation: string;
kind: 'event';
event: string;
payload: unknown;
}
export type PluginProcessEnvelope = PluginProcessRequest | PluginProcessResponse | PluginProcessEvent;
export function assertPluginProcessPayload<T>(input: T): T {
walkPayload(input, 0, new WeakSet<object>());
const serialized = JSON.stringify(input);
if (typeof serialized !== 'string' || Buffer.byteLength(serialized, 'utf8') > MAX_PAYLOAD_BYTES) {
throw new TypeError('Plugin process payload exceeds 1 MiB');
}
return input;
}
export function parsePluginProcessEnvelope(input: unknown, expectedGeneration: string): PluginProcessEnvelope {
assertNotProxy(input, 'Plugin process envelope');
if (!isPlainObject(input)) {
throw new TypeError('Plugin process envelope must be a plain object');
}
const common = ['protocol', 'generation', 'kind'] as const;
if (!hasExactFields(input, common)
&& !hasExactFields(input, [...common, 'requestId', 'method', 'payload'])
&& !hasExactFields(input, [...common, 'requestId', 'ok', 'payload'])
&& !hasExactFields(input, [...common, 'requestId', 'ok', 'error'])
&& !hasExactFields(input, [...common, 'event', 'payload'])) {
throw new TypeError('Plugin process envelope has unknown or missing fields');
}
if (input.protocol !== PLUGIN_PROCESS_PROTOCOL) {
throw new TypeError('Plugin process protocol version is not supported');
}
if (input.generation !== expectedGeneration) {
throw new TypeError('Plugin process envelope generation is stale');
}
switch (input.kind) {
case 'request':
if (!hasExactFields(input, [...common, 'requestId', 'method', 'payload'])
|| !isNonEmptyString(input.requestId) || !isNonEmptyString(input.method)) {
throw new TypeError('Plugin process request is invalid');
}
assertPluginProcessPayload(input.payload);
return input as unknown as PluginProcessRequest;
case 'response':
if (input.ok === true && hasExactFields(input, [...common, 'requestId', 'ok', 'payload'])
&& isNonEmptyString(input.requestId)) {
assertPluginProcessPayload(input.payload);
return input as unknown as PluginProcessResponseSuccess;
}
if (input.ok === false && hasExactFields(input, [...common, 'requestId', 'ok', 'error'])
&& isNonEmptyString(input.requestId) && isPluginProcessErrorPayload(input.error)) {
return input as unknown as PluginProcessResponseFailure;
}
throw new TypeError('Plugin process response is invalid');
case 'event':
if (!hasExactFields(input, [...common, 'event', 'payload']) || !isNonEmptyString(input.event)) {
throw new TypeError('Plugin process event is invalid');
}
assertPluginProcessPayload(input.payload);
return input as unknown as PluginProcessEvent;
default:
throw new TypeError('Plugin process envelope kind is invalid');
}
}
function walkPayload(input: unknown, depth: number, seen: WeakSet<object>): void {
if (depth > MAX_PAYLOAD_DEPTH) {
throw new TypeError('Plugin process payload exceeds maximum depth');
}
if (input === null || typeof input === 'string' || typeof input === 'boolean') {
return;
}
if (typeof input === 'number') {
if (!Number.isFinite(input)) {
throw new TypeError('Plugin process payload numbers must be finite');
}
return;
}
if (typeof input !== 'object') {
throw new TypeError('Plugin process payload must be structured-clone-compatible');
}
assertNotProxy(input, 'Plugin process payload');
if (seen.has(input)) {
throw new TypeError('Plugin process payload cannot contain cycles');
}
seen.add(input);
try {
if (Array.isArray(input)) {
validateArray(input, depth, seen);
return;
}
if (!isPlainObject(input)) {
throw new TypeError('Plugin process payload objects must have a plain or null prototype');
}
for (const key of ownEnumerableDataKeys(input)) {
const descriptor = Object.getOwnPropertyDescriptor(input, key);
walkPayload(descriptor?.value, depth + 1, seen);
}
} finally {
seen.delete(input);
}
}
function validateArray(input: unknown[], depth: number, seen: WeakSet<object>): void {
if (Object.getPrototypeOf(input) !== Array.prototype) {
throw new TypeError('Plugin process payload arrays must have the Array prototype');
}
const keys = Reflect.ownKeys(input);
for (const key of keys) {
if (key === 'length') {
continue;
}
if (typeof key !== 'string' || !isArrayIndex(key, input.length)) {
throw new TypeError('Plugin process payload arrays cannot have custom properties');
}
}
for (let index = 0; index < input.length; index += 1) {
const descriptor = Object.getOwnPropertyDescriptor(input, String(index));
if (!descriptor) {
throw new TypeError('Plugin process payload arrays cannot contain holes');
}
if (!descriptor.enumerable || !Object.hasOwn(descriptor, 'value')) {
throw new TypeError('Plugin process payload arrays must have enumerable value entries');
}
walkPayload(descriptor.value, depth + 1, seen);
}
}
function isPluginProcessErrorPayload(input: unknown): input is PluginProcessErrorPayload {
if (!isPlainObject(input)) {
return false;
}
const allowed = ['code', 'message', 'retryable', 'retryAfterMs'];
const keys = ownEnumerableDataKeys(input);
if (!keys.includes('code') || !keys.includes('message') || keys.some((key) => !allowed.includes(key))) {
return false;
}
return isNonEmptyString(input.code)
&& typeof input.message === 'string'
&& (input.retryable === undefined || typeof input.retryable === 'boolean')
&& (input.retryAfterMs === undefined || (typeof input.retryAfterMs === 'number'
&& Number.isFinite(input.retryAfterMs) && input.retryAfterMs >= 0));
}
function isPlainObject(input: unknown): input is Record<string, unknown> {
if (input === null || typeof input !== 'object' || isPluginProcessProxy(input) || Array.isArray(input)) {
return false;
}
const prototype = Object.getPrototypeOf(input);
return prototype === Object.prototype || prototype === null;
}
function hasExactFields(input: Record<string, unknown>, expected: readonly string[]): boolean {
const keys = ownEnumerableDataKeys(input);
return keys.length === expected.length && expected.every((key) => keys.includes(key));
}
function ownEnumerableDataKeys(input: Record<string, unknown>): string[] {
const keys = Reflect.ownKeys(input);
if (keys.some((key) => typeof key !== 'string')) {
throw new TypeError('Plugin process data cannot contain symbol fields');
}
for (const key of keys) {
const descriptor = Object.getOwnPropertyDescriptor(input, key);
if (!descriptor || !descriptor.enumerable || !Object.hasOwn(descriptor, 'value')) {
throw new TypeError('Plugin process data must have enumerable value fields');
}
}
return keys as string[];
}
function isNonEmptyString(input: unknown): input is string {
return typeof input === 'string' && input.length > 0;
}
function assertNotProxy(input: unknown, label: string): void {
if (isPluginProcessProxy(input)) throw new TypeError(`${label} cannot be a Proxy`);
}
function isArrayIndex(key: string, length: number): boolean {
const index = Number(key);
return Number.isInteger(index) && index >= 0 && index < length && String(index) === key;
}