opencode-gemini-app 0.0.0-stage → 0.1.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/DISCLAIMER.md +17 -0
- package/LICENSE +21 -0
- package/README.md +46 -2
- package/extensions/opencode/auth.js +324 -0
- package/extensions/opencode/fetch.js +494 -0
- package/extensions/opencode/index.js +27 -0
- package/extensions/opencode/models.js +84 -0
- package/extensions/pi/auth.js +215 -0
- package/native-wire/consumer-tool-codec.mjs +239 -0
- package/native-wire/oauth-loopback.mjs +175 -0
- package/native-wire/protobuf.mjs +133 -0
- package/native-wire/robin-current.mjs +172 -0
- package/package.json +75 -4
- package/src/gateway/automatic-recovery.mjs +70 -0
- package/src/gateway/image-download.mjs +110 -0
- package/src/gateway/image-upload.mjs +262 -0
- package/src/gateway/native-diagnostic.mjs +190 -0
- package/src/gateway/native-evidence.mjs +114 -0
- package/src/gateway/native-response-limits.mjs +8 -0
- package/src/gateway/native-search-citations.mjs +196 -0
- package/src/gateway/native-search-diagnostic.mjs +282 -0
- package/src/gateway/native-search-metadata.mjs +749 -0
- package/src/gateway/native-transport.mjs +433 -0
- package/src/gateway/oauth-client-defaults.mjs +12 -0
- package/src/gateway/oauth-identity.mjs +67 -0
- package/src/gateway/outbound-proxy.mjs +320 -0
- package/src/gateway/output-recovery.mjs +48 -0
- package/src/gateway/quota-failover.mjs +14 -0
- package/src/gateway/quota-transport.mjs +37 -0
- package/src/gateway/quota.mjs +181 -0
- package/src/gateway/soft-error.mjs +25 -0
- package/src/gateway/structured-output.mjs +157 -0
- package/src/gateway/subscription.mjs +51 -0
- package/src/gateway/terms.mjs +126 -0
- package/src/image-input.mjs +137 -0
- package/src/image-url.mjs +21 -0
- package/src/native-provider.mjs +806 -0
- package/src/openai-contract.mjs +576 -0
- package/src/openai-usage.mjs +66 -0
- package/src/prompt-tool-schema.mjs +391 -0
- package/src/responses-tool-namespaces.mjs +84 -0
- package/src/tool-protocol.mjs +366 -0
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
import { decodeGrpcMessage } from "./native-evidence.mjs";
|
|
2
|
+
/** Product-only native HTTP2 transport. No App, IPC, research ledger or retry.
|
|
3
|
+
* Prompt tools finish one generation; caller results are new checked API turns.
|
|
4
|
+
*/
|
|
5
|
+
import http2 from "node:http2";
|
|
6
|
+
import net from "node:net";
|
|
7
|
+
import crypto from "node:crypto";
|
|
8
|
+
import { buildConsumerToolInitial } from "../../native-wire/consumer-tool-codec.mjs";
|
|
9
|
+
import {
|
|
10
|
+
lengthDelimited as m,
|
|
11
|
+
varintField as v,
|
|
12
|
+
} from "../../native-wire/protobuf.mjs";
|
|
13
|
+
import {
|
|
14
|
+
GrpcMessageReader,
|
|
15
|
+
frameGrpcMessage,
|
|
16
|
+
NATIVE_VERSION,
|
|
17
|
+
GATEWAY_USER_AGENT,
|
|
18
|
+
} from "../../native-wire/robin-current.mjs";
|
|
19
|
+
import { fault } from "./oauth-client-defaults.mjs";
|
|
20
|
+
import { PROMPT_MAX_BYTES } from "../openai-contract.mjs";
|
|
21
|
+
import { NATIVE_RESPONSE_LIMITS as RESPONSE_LIMITS } from './native-response-limits.mjs';
|
|
22
|
+
import { observeNative } from './native-diagnostic.mjs';
|
|
23
|
+
import { connectHttp2 } from './outbound-proxy.mjs';
|
|
24
|
+
export const DEFAULT_HTTP2_PING_INTERVAL_MS = 15_000;
|
|
25
|
+
export const DEFAULT_HTTP2_PING_TIMEOUT_MS = 30_000;
|
|
26
|
+
export const MIN_CONNECT_ATTEMPT_TIMEOUT_MS = 5000;
|
|
27
|
+
// Node v22 defaults Happy Eyeballs per-family attempt timeout to 250ms, which aborts
|
|
28
|
+
// IPv4 connects over high-RTT VPN tunnels when IPv6 returns immediate EHOSTUNREACH.
|
|
29
|
+
export function ensureConnectAttemptTimeout(minMs = MIN_CONNECT_ATTEMPT_TIMEOUT_MS) {
|
|
30
|
+
if (
|
|
31
|
+
typeof net.getDefaultAutoSelectFamilyAttemptTimeout === "function" &&
|
|
32
|
+
typeof net.setDefaultAutoSelectFamilyAttemptTimeout === "function" &&
|
|
33
|
+
net.getDefaultAutoSelectFamilyAttemptTimeout() < minMs
|
|
34
|
+
) {
|
|
35
|
+
net.setDefaultAutoSelectFamilyAttemptTimeout(minMs);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
ensureConnectAttemptTimeout();
|
|
39
|
+
export const NATIVE_ORIGIN = "https://robinfrontend-pa.googleapis.com";
|
|
40
|
+
export const NATIVE_RPC =
|
|
41
|
+
"/assistant.mobile.robin.RobinService/ProcessQuery";
|
|
42
|
+
// ProcessQuery compatibility snapshot shared with native-wire/robin-current.mjs.
|
|
43
|
+
// A protocol constant, not a runtime App dependency or copied device identity.
|
|
44
|
+
export const NATIVE_COMPATIBILITY_VERSION = NATIVE_VERSION;
|
|
45
|
+
export function openDirectTurn(
|
|
46
|
+
token,
|
|
47
|
+
{
|
|
48
|
+
messages,
|
|
49
|
+
images = [],
|
|
50
|
+
modeCategory = 3,
|
|
51
|
+
thinkingLevel = 2,
|
|
52
|
+
smartModelSelection = false,
|
|
53
|
+
temporaryChat = true,
|
|
54
|
+
continuation,
|
|
55
|
+
timeoutMs = 180000,
|
|
56
|
+
pingIntervalMs = DEFAULT_HTTP2_PING_INTERVAL_MS,
|
|
57
|
+
pingTimeoutMs = DEFAULT_HTTP2_PING_TIMEOUT_MS,
|
|
58
|
+
signal,
|
|
59
|
+
allowNativeTools = false,
|
|
60
|
+
emitHeaders = false,
|
|
61
|
+
diagnostic,
|
|
62
|
+
} = {},
|
|
63
|
+
{ connect = connectHttp2 } = {},
|
|
64
|
+
) {
|
|
65
|
+
if (
|
|
66
|
+
typeof token !== "string" ||
|
|
67
|
+
!token ||
|
|
68
|
+
token.length > 65536 ||
|
|
69
|
+
/\s/.test(token)
|
|
70
|
+
)
|
|
71
|
+
throw fault("NATIVE_CREDENTIAL_REQUIRED", 503);
|
|
72
|
+
if (
|
|
73
|
+
!Array.isArray(messages) ||
|
|
74
|
+
messages.length !== 1 ||
|
|
75
|
+
messages[0]?.role !== "user" ||
|
|
76
|
+
typeof messages[0].content !== "string" ||
|
|
77
|
+
!messages[0].content
|
|
78
|
+
)
|
|
79
|
+
throw fault("INVALID_NATIVE_INPUT");
|
|
80
|
+
if (!Number.isInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 600000)
|
|
81
|
+
throw fault("INVALID_TIMEOUT");
|
|
82
|
+
if (signal?.aborted) throw fault("REQUEST_CANCELLED", 499);
|
|
83
|
+
const text = messages[0].content;
|
|
84
|
+
if (Buffer.byteLength(text) > PROMPT_MAX_BYTES) throw fault("PROMPT_TOO_LARGE", 413);
|
|
85
|
+
const initial = buildConsumerToolInitial({
|
|
86
|
+
userText: text,
|
|
87
|
+
images,
|
|
88
|
+
modeCategory,
|
|
89
|
+
thinkingLevel,
|
|
90
|
+
smartModelSelection,
|
|
91
|
+
temporaryChat,
|
|
92
|
+
continuation,
|
|
93
|
+
allowNativeTools,
|
|
94
|
+
clientMetadata: {
|
|
95
|
+
clientType: 21,
|
|
96
|
+
languageCode: "en",
|
|
97
|
+
interfaceLanguageCode: "en",
|
|
98
|
+
clientVersion: NATIVE_COMPATIBILITY_VERSION,
|
|
99
|
+
clientInstanceId: crypto.randomUUID(),
|
|
100
|
+
triggerType: 1,
|
|
101
|
+
uiSource: 1,
|
|
102
|
+
},
|
|
103
|
+
}, { maxInputBytes: PROMPT_MAX_BYTES });
|
|
104
|
+
const wire = Buffer.concat(
|
|
105
|
+
[
|
|
106
|
+
initial,
|
|
107
|
+
Buffer.concat([v(1, 1), m(1007, m(1, Buffer.from(text)))]),
|
|
108
|
+
Buffer.concat([v(1, 1), m(1007, m(2, Buffer.alloc(0)))]),
|
|
109
|
+
].map(frameGrpcMessage),
|
|
110
|
+
);
|
|
111
|
+
if (wire.length > 16 * 1024 * 1024) throw fault("PROMPT_TOO_LARGE", 413);
|
|
112
|
+
const note = (name, values) => observeNative(diagnostic, 'event', name, values);
|
|
113
|
+
note('transport_open', { mode: modeCategory, thinking: thinkingLevel, timeoutMs, bytes: wire.length });
|
|
114
|
+
let client,
|
|
115
|
+
socket,
|
|
116
|
+
connectionEnd = null,
|
|
117
|
+
streamCloseCode = null,
|
|
118
|
+
request,
|
|
119
|
+
timer,
|
|
120
|
+
pingTimer = null,
|
|
121
|
+
pingAckTimer = null,
|
|
122
|
+
pingInFlight = false,
|
|
123
|
+
lastInboundAt = Date.now(),
|
|
124
|
+
closed = false,
|
|
125
|
+
ended = false,
|
|
126
|
+
error = null,
|
|
127
|
+
waiting = null,
|
|
128
|
+
httpStatus = null,
|
|
129
|
+
grpcStatus = null,
|
|
130
|
+
grpcMessage = null,
|
|
131
|
+
http2ResetCode = null,
|
|
132
|
+
received = 0,
|
|
133
|
+
queued = 0;
|
|
134
|
+
const items = [],
|
|
135
|
+
reader = new GrpcMessageReader({ maxBytes: RESPONSE_LIMITS.frameBytes });
|
|
136
|
+
const detachSocketListeners = () => {
|
|
137
|
+
const current = socket;
|
|
138
|
+
socket = null;
|
|
139
|
+
if (!current) return;
|
|
140
|
+
try { current.removeListener?.('end', socketEnd); } catch {}
|
|
141
|
+
try { current.removeListener?.('close', socketClose); } catch {}
|
|
142
|
+
};
|
|
143
|
+
const connectionEnded = (kind) => {
|
|
144
|
+
if (closed || ended || connectionEnd) return;
|
|
145
|
+
// Node's socketOnClose() locally closes unfinished streams with CANCEL.
|
|
146
|
+
// Capture the code BEFORE that handler so its synthetic 8 is not peer RST evidence.
|
|
147
|
+
const code = request?.rstCode;
|
|
148
|
+
connectionEnd = { rstCode: Number.isInteger(code) ? code : 0 };
|
|
149
|
+
note(`peer_connection_${kind}`, { rstCode: connectionEnd.rstCode });
|
|
150
|
+
if (kind === 'close') detachSocketListeners();
|
|
151
|
+
};
|
|
152
|
+
const socketEnd = () => connectionEnded('end');
|
|
153
|
+
const socketClose = () => connectionEnded('close');
|
|
154
|
+
const stopPing = () => {
|
|
155
|
+
if (pingTimer) {
|
|
156
|
+
clearInterval(pingTimer);
|
|
157
|
+
pingTimer = null;
|
|
158
|
+
}
|
|
159
|
+
if (pingAckTimer) {
|
|
160
|
+
clearTimeout(pingAckTimer);
|
|
161
|
+
pingAckTimer = null;
|
|
162
|
+
}
|
|
163
|
+
pingInFlight = false;
|
|
164
|
+
};
|
|
165
|
+
const stop = () => {
|
|
166
|
+
clearTimeout(timer);
|
|
167
|
+
stopPing();
|
|
168
|
+
signal?.removeEventListener("abort", abort);
|
|
169
|
+
detachSocketListeners();
|
|
170
|
+
note('local_stream_close', { requestedCode: ended ? 0 : 8, rstCode: request?.rstCode });
|
|
171
|
+
try {
|
|
172
|
+
request?.close(ended ? http2.constants.NGHTTP2_NO_ERROR : http2.constants.NGHTTP2_CANCEL);
|
|
173
|
+
} catch {}
|
|
174
|
+
const session = client;
|
|
175
|
+
const destroy = () => { note('local_session_destroy'); try { session?.destroy(); } catch {} };
|
|
176
|
+
if (typeof session?.close !== "function") { destroy(); return; }
|
|
177
|
+
// Graceful session shutdown flushes RST_STREAM before closing the socket.
|
|
178
|
+
// A hard bound still prevents a peer from holding cleanup open forever.
|
|
179
|
+
const cleanup = setTimeout(destroy, 250); cleanup.unref();
|
|
180
|
+
session.once?.("close", () => clearTimeout(cleanup));
|
|
181
|
+
try { session.close(); } catch { clearTimeout(cleanup); destroy(); }
|
|
182
|
+
};
|
|
183
|
+
const push = (event) => {
|
|
184
|
+
if (closed) return;
|
|
185
|
+
if (waiting) {
|
|
186
|
+
const resolve = waiting.resolve;
|
|
187
|
+
waiting = null;
|
|
188
|
+
resolve({ value: event, done: false });
|
|
189
|
+
} else {
|
|
190
|
+
const size = event.bytes?.length ?? 0;
|
|
191
|
+
if (queued + size > RESPONSE_LIMITS.queuedBytes) return fail("NATIVE_RECEIVE_LIMIT");
|
|
192
|
+
queued += size;
|
|
193
|
+
items.push(event);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
const fail = (code, status = 502) => {
|
|
197
|
+
if (closed) return;
|
|
198
|
+
note('transport_failure', { code });
|
|
199
|
+
error = fault(code, status);
|
|
200
|
+
closed = true;
|
|
201
|
+
if (waiting) {
|
|
202
|
+
waiting.reject(error);
|
|
203
|
+
waiting = null;
|
|
204
|
+
}
|
|
205
|
+
stop();
|
|
206
|
+
};
|
|
207
|
+
const abort = () => { note('local_signal_abort'); fail("REQUEST_CANCELLED", 499); };
|
|
208
|
+
const events = {
|
|
209
|
+
[Symbol.asyncIterator]() {
|
|
210
|
+
return this;
|
|
211
|
+
},
|
|
212
|
+
next() {
|
|
213
|
+
if (items.length) {
|
|
214
|
+
const value = items.shift();
|
|
215
|
+
queued -= value.bytes?.length ?? 0;
|
|
216
|
+
return Promise.resolve({ value, done: false });
|
|
217
|
+
}
|
|
218
|
+
if (error) return Promise.reject(error);
|
|
219
|
+
if (ended || closed) return Promise.resolve({ done: true });
|
|
220
|
+
if (waiting) return Promise.reject(fault("NATIVE_READER_BUSY", 502));
|
|
221
|
+
return new Promise((resolve, reject) => {
|
|
222
|
+
waiting = { resolve, reject };
|
|
223
|
+
});
|
|
224
|
+
},
|
|
225
|
+
return() {
|
|
226
|
+
cancel();
|
|
227
|
+
return Promise.resolve({ done: true });
|
|
228
|
+
},
|
|
229
|
+
};
|
|
230
|
+
const cancel = () => {
|
|
231
|
+
if (closed) return;
|
|
232
|
+
note('local_cancel');
|
|
233
|
+
closed = true;
|
|
234
|
+
items.length = 0;
|
|
235
|
+
queued = 0;
|
|
236
|
+
if (waiting) {
|
|
237
|
+
waiting.resolve({ done: true });
|
|
238
|
+
waiting = null;
|
|
239
|
+
}
|
|
240
|
+
stop();
|
|
241
|
+
};
|
|
242
|
+
const rejectPeerReset = () => {
|
|
243
|
+
// Sample before our own stop()/cancel(); local cleanup must not become upstream evidence.
|
|
244
|
+
if (closed) return false;
|
|
245
|
+
const code = request?.rstCode;
|
|
246
|
+
if (!Number.isInteger(code) || code <= 0 || code > 0xffffffff) return false;
|
|
247
|
+
if (connectionEnd?.rstCode === 0) {
|
|
248
|
+
streamCloseCode = code;
|
|
249
|
+
fail("NATIVE_STREAM_CLOSED_EARLY");
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
http2ResetCode = code;
|
|
253
|
+
note('peer_reset', { rstCode: code });
|
|
254
|
+
fail("NATIVE_STREAM_RESET");
|
|
255
|
+
return true;
|
|
256
|
+
};
|
|
257
|
+
const status = (headers) => {
|
|
258
|
+
if (headers["grpc-message"] !== undefined)
|
|
259
|
+
grpcMessage = decodeGrpcMessage(headers["grpc-message"]);
|
|
260
|
+
if (headers[":status"] !== undefined)
|
|
261
|
+
httpStatus = Number(headers[":status"]);
|
|
262
|
+
if (headers["grpc-status"] !== undefined) {
|
|
263
|
+
const parsed = Number(headers["grpc-status"]);
|
|
264
|
+
if (!Number.isInteger(parsed) || parsed < 0 || parsed > 16)
|
|
265
|
+
return fail("NATIVE_INVALID_STATUS");
|
|
266
|
+
grpcStatus = parsed;
|
|
267
|
+
}
|
|
268
|
+
if (httpStatus === 429 || grpcStatus === 8)
|
|
269
|
+
return fail("NATIVE_QUOTA_EXHAUSTED", 429);
|
|
270
|
+
if ([401, 403].includes(httpStatus) || [7, 16].includes(grpcStatus))
|
|
271
|
+
return fail(
|
|
272
|
+
"NATIVE_PERMISSION_OR_AUTH",
|
|
273
|
+
[401, 403].includes(httpStatus) ? httpStatus : 403,
|
|
274
|
+
);
|
|
275
|
+
if (httpStatus === 503) {
|
|
276
|
+
if (grpcStatus !== null && grpcStatus !== 0 && grpcStatus !== 14)
|
|
277
|
+
return fail("NATIVE_TURN_FAILED");
|
|
278
|
+
return fail("NATIVE_UPSTREAM_UNAVAILABLE", 503);
|
|
279
|
+
}
|
|
280
|
+
if (httpStatus !== null && httpStatus !== 200)
|
|
281
|
+
return fail("NATIVE_HTTP_REJECTED");
|
|
282
|
+
};
|
|
283
|
+
timer = setTimeout(() => { note('transport_deadline'); fail("NATIVE_TRANSPORT_TIMEOUT"); }, timeoutMs);
|
|
284
|
+
timer.unref();
|
|
285
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
286
|
+
try {
|
|
287
|
+
client = connect(NATIVE_ORIGIN);
|
|
288
|
+
try {
|
|
289
|
+
socket = client.socket;
|
|
290
|
+
socket?.setKeepAlive?.(true, 15_000);
|
|
291
|
+
socket?.prependOnceListener?.('end', socketEnd);
|
|
292
|
+
socket?.prependOnceListener?.('close', socketClose);
|
|
293
|
+
} catch {
|
|
294
|
+
detachSocketListeners();
|
|
295
|
+
}
|
|
296
|
+
client.on("error", () => fail("NATIVE_CONNECTION_FAILED"));
|
|
297
|
+
if (diagnostic) client.on('goaway', code => note('peer_goaway', { goawayCode: code }));
|
|
298
|
+
if (Number.isInteger(pingIntervalMs) && pingIntervalMs > 0 && typeof client.ping === "function") {
|
|
299
|
+
let pingSeq = 0, activePingSeq = 0;
|
|
300
|
+
pingTimer = setInterval(() => {
|
|
301
|
+
if (closed || ended || client.closed || client.destroyed || client.connecting || pingInFlight) return;
|
|
302
|
+
const seq = ++pingSeq;
|
|
303
|
+
activePingSeq = seq;
|
|
304
|
+
const pingStartedAt = Date.now();
|
|
305
|
+
pingInFlight = true;
|
|
306
|
+
let pingQueued = true;
|
|
307
|
+
let sent = false;
|
|
308
|
+
try {
|
|
309
|
+
sent = client.ping((err) => {
|
|
310
|
+
if (!pingQueued || seq !== activePingSeq) return;
|
|
311
|
+
if (pingAckTimer) {
|
|
312
|
+
clearTimeout(pingAckTimer);
|
|
313
|
+
pingAckTimer = null;
|
|
314
|
+
}
|
|
315
|
+
pingInFlight = false;
|
|
316
|
+
if (closed || ended) return;
|
|
317
|
+
if (err) {
|
|
318
|
+
if (err.code === 'ERR_HTTP2_PING_CANCEL') return;
|
|
319
|
+
note('transport_ping_timeout');
|
|
320
|
+
fail('NATIVE_CONNECTION_FAILED');
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
lastInboundAt = Date.now();
|
|
324
|
+
});
|
|
325
|
+
} catch {
|
|
326
|
+
pingQueued = false;
|
|
327
|
+
if (activePingSeq === seq) activePingSeq = 0;
|
|
328
|
+
pingInFlight = false;
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
if (sent === false) {
|
|
332
|
+
pingQueued = false;
|
|
333
|
+
if (activePingSeq === seq) activePingSeq = 0;
|
|
334
|
+
pingInFlight = false;
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
if (Number.isInteger(pingTimeoutMs) && pingTimeoutMs > 0 && !closed && !ended && pingInFlight && activePingSeq === seq) {
|
|
338
|
+
const armAckWatchdog = (delayMs) => {
|
|
339
|
+
pingAckTimer = setTimeout(() => {
|
|
340
|
+
pingAckTimer = null;
|
|
341
|
+
if (closed || ended || !pingInFlight || activePingSeq !== seq) return;
|
|
342
|
+
const idleSinceInbound = Date.now() - lastInboundAt;
|
|
343
|
+
if (lastInboundAt >= pingStartedAt && idleSinceInbound < pingTimeoutMs) {
|
|
344
|
+
armAckWatchdog(Math.max(1, pingTimeoutMs - idleSinceInbound));
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
note('transport_ping_timeout');
|
|
348
|
+
fail('NATIVE_CONNECTION_FAILED');
|
|
349
|
+
}, delayMs);
|
|
350
|
+
pingAckTimer.unref();
|
|
351
|
+
};
|
|
352
|
+
armAckWatchdog(pingTimeoutMs);
|
|
353
|
+
}
|
|
354
|
+
}, pingIntervalMs);
|
|
355
|
+
pingTimer.unref();
|
|
356
|
+
}
|
|
357
|
+
request = client.request({
|
|
358
|
+
":method": "POST",
|
|
359
|
+
":path": NATIVE_RPC,
|
|
360
|
+
authorization: "Bearer " + token,
|
|
361
|
+
"content-type": "application/grpc",
|
|
362
|
+
"grpc-timeout": `${timeoutMs}m`,
|
|
363
|
+
te: "trailers",
|
|
364
|
+
"user-agent": GATEWAY_USER_AGENT,
|
|
365
|
+
});
|
|
366
|
+
request.on("response", headers => {
|
|
367
|
+
lastInboundAt = Date.now();
|
|
368
|
+
note('peer_headers', { httpStatus: Number(headers[':status']), grpcStatus: Number(headers['grpc-status']) });
|
|
369
|
+
status(headers);
|
|
370
|
+
if (emitHeaders && !closed && httpStatus === 200) push({ type: 'headers', httpStatus });
|
|
371
|
+
});
|
|
372
|
+
request.on("trailers", headers => { lastInboundAt = Date.now(); note('peer_trailers', { grpcStatus: Number(headers['grpc-status']) }); status(headers); });
|
|
373
|
+
request.on("error", () => {
|
|
374
|
+
note('stream_error', { rstCode: request?.rstCode });
|
|
375
|
+
if (!rejectPeerReset()) fail("NATIVE_STREAM_FAILED");
|
|
376
|
+
});
|
|
377
|
+
request.on("close", () => {
|
|
378
|
+
note('stream_close', { rstCode: request?.rstCode });
|
|
379
|
+
if (!ended && !closed && !rejectPeerReset())
|
|
380
|
+
fail("NATIVE_STREAM_CLOSED_EARLY");
|
|
381
|
+
});
|
|
382
|
+
request.on("data", (chunk) => {
|
|
383
|
+
if (closed) return;
|
|
384
|
+
lastInboundAt = Date.now();
|
|
385
|
+
note('peer_data', { bytes: chunk.length });
|
|
386
|
+
try {
|
|
387
|
+
received += chunk.length;
|
|
388
|
+
if (received > RESPONSE_LIMITS.wireBytes) return fail("NATIVE_RECEIVE_LIMIT");
|
|
389
|
+
for (const bytes of reader.push(chunk)) {
|
|
390
|
+
observeNative(diagnostic, 'frame', bytes);
|
|
391
|
+
push({ type: "frame", bytes });
|
|
392
|
+
}
|
|
393
|
+
} catch {
|
|
394
|
+
fail("NATIVE_FRAME_REJECTED");
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
request.on("end", () => {
|
|
398
|
+
lastInboundAt = Date.now();
|
|
399
|
+
note('stream_end', { rstCode: request?.rstCode });
|
|
400
|
+
if (closed) return;
|
|
401
|
+
// Node may emit readable 'end' for RST_STREAM(CANCEL). That is not a successful end.
|
|
402
|
+
if (rejectPeerReset()) return;
|
|
403
|
+
try {
|
|
404
|
+
reader.finish();
|
|
405
|
+
push({ type: "end", httpStatus, grpcStatus });
|
|
406
|
+
ended = true;
|
|
407
|
+
clearTimeout(timer);
|
|
408
|
+
stopPing();
|
|
409
|
+
} catch {
|
|
410
|
+
fail("NATIVE_FRAME_REJECTED");
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
note('local_half_close', { bytes: wire.length });
|
|
414
|
+
request.end(wire);
|
|
415
|
+
} catch {
|
|
416
|
+
fail("NATIVE_CONNECTION_FAILED");
|
|
417
|
+
}
|
|
418
|
+
return {
|
|
419
|
+
events,
|
|
420
|
+
cancel,
|
|
421
|
+
getSafeStatus: () => ({
|
|
422
|
+
httpStatus,
|
|
423
|
+
grpcStatus,
|
|
424
|
+
...(grpcMessage ? { grpcMessage } : {}),
|
|
425
|
+
...(http2ResetCode !== null ? { http2ResetCode } : {}),
|
|
426
|
+
...(connectionEnd ? { connectionEndObserved: true } : {}),
|
|
427
|
+
...(streamCloseCode !== null ? { streamCloseCode } : {}),
|
|
428
|
+
ended,
|
|
429
|
+
receivedBytes: received,
|
|
430
|
+
requestBytes: wire.length,
|
|
431
|
+
}),
|
|
432
|
+
};
|
|
433
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Default public OAuth desktop client configuration and lightweight error helper.
|
|
2
|
+
export function fault(code, status = 400) {
|
|
3
|
+
return Object.assign(new Error(code), { code, status });
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export const DEFAULT_OAUTH_PROFILE = Object.freeze({
|
|
7
|
+
context: 2,
|
|
8
|
+
enableFst: true,
|
|
9
|
+
clientId: "839509190550-l232s7lupkp6f57818eshd7u4sl4920h.apps.googleusercontent.com",
|
|
10
|
+
clientSecret: "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",
|
|
11
|
+
scopes: Object.freeze(["openid","https://www.googleapis.com/auth/userinfo.email","https://www.googleapis.com/auth/userinfo.profile","https://www.googleapis.com/auth/assistant","https://www.googleapis.com/auth/client_channel","https://www.googleapis.com/auth/gemini","https://www.googleapis.com/auth/calendar","https://www.googleapis.com/auth/drive","https://www.googleapis.com/auth/photos.image.readonly","https://www.googleapis.com/auth/photospicker.mediaitems.readonly","https://www.googleapis.com/auth/oauth_integrations","https://www.googleapis.com/auth/experimentsandconfigs","https://www.googleapis.com/auth/cclog","https://www.googleapis.com/auth/supportcontent","https://www.googleapis.com/auth/mobile_user_preferences","https://www.googleapis.com/auth/notifications","https://www.googleapis.com/auth/cloud-platform","https://www.googleapis.com/auth/aicode"]),
|
|
12
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { fault } from "./oauth-client-defaults.mjs";
|
|
2
|
+
import { fetchWithProxy } from "./outbound-proxy.mjs";
|
|
3
|
+
|
|
4
|
+
export function validateIdentity(data) {
|
|
5
|
+
if (
|
|
6
|
+
!data ||
|
|
7
|
+
data.email_verified !== true ||
|
|
8
|
+
typeof data.email !== "string" ||
|
|
9
|
+
data.email.length > 254 ||
|
|
10
|
+
!/^[^\s@\x00-\x1f\x7f]+@[^\s@\x00-\x1f\x7f]+\.[^\s@\x00-\x1f\x7f]+$/.test(
|
|
11
|
+
data.email,
|
|
12
|
+
) ||
|
|
13
|
+
typeof data.sub !== "string" ||
|
|
14
|
+
!/^[A-Za-z0-9_-]{1,255}$/.test(data.sub)
|
|
15
|
+
)
|
|
16
|
+
throw fault("GOOGLE_VERIFIED_EMAIL_REQUIRED", 502);
|
|
17
|
+
return { email: data.email, subject: data.sub };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Fixed Google endpoint; never follow redirects or disclose response bodies.
|
|
21
|
+
export async function identifyGoogleAccount(
|
|
22
|
+
tokens,
|
|
23
|
+
{ fetchImpl = fetchWithProxy, timeoutMs = 8000 } = {},
|
|
24
|
+
) {
|
|
25
|
+
if (
|
|
26
|
+
typeof tokens?.accessToken !== "string" ||
|
|
27
|
+
!tokens.accessToken ||
|
|
28
|
+
/[\r\n]/.test(tokens.accessToken)
|
|
29
|
+
)
|
|
30
|
+
throw fault("GOOGLE_IDENTITY_UNAVAILABLE", 502);
|
|
31
|
+
try {
|
|
32
|
+
const response = await fetchImpl(
|
|
33
|
+
"https://www.googleapis.com/oauth2/v3/userinfo",
|
|
34
|
+
{
|
|
35
|
+
headers: { Authorization: `Bearer ${tokens.accessToken}` },
|
|
36
|
+
redirect: "error",
|
|
37
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
38
|
+
},
|
|
39
|
+
);
|
|
40
|
+
if (!response.ok) {
|
|
41
|
+
await response.body?.cancel();
|
|
42
|
+
throw fault("GOOGLE_IDENTITY_UNAVAILABLE", 502);
|
|
43
|
+
}
|
|
44
|
+
const reader = response.body?.getReader();
|
|
45
|
+
if (!reader) throw fault("GOOGLE_IDENTITY_UNAVAILABLE", 502);
|
|
46
|
+
const chunks = [];
|
|
47
|
+
let length = 0;
|
|
48
|
+
try {
|
|
49
|
+
while (true) {
|
|
50
|
+
const { value, done } = await reader.read();
|
|
51
|
+
if (done) break;
|
|
52
|
+
length += value.byteLength;
|
|
53
|
+
if (length > 32768) {
|
|
54
|
+
await reader.cancel();
|
|
55
|
+
throw fault("GOOGLE_IDENTITY_UNAVAILABLE", 502);
|
|
56
|
+
}
|
|
57
|
+
chunks.push(Buffer.from(value));
|
|
58
|
+
}
|
|
59
|
+
} finally {
|
|
60
|
+
reader.releaseLock();
|
|
61
|
+
}
|
|
62
|
+
return validateIdentity(JSON.parse(Buffer.concat(chunks).toString("utf8")));
|
|
63
|
+
} catch (error) {
|
|
64
|
+
if (error.code === "GOOGLE_VERIFIED_EMAIL_REQUIRED") throw error;
|
|
65
|
+
throw fault("GOOGLE_IDENTITY_UNAVAILABLE", 502);
|
|
66
|
+
}
|
|
67
|
+
}
|