@systemfsoftware/agent-platform-cloudflare 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/dist/Alarm-CbhflIzz.mjs +1931 -0
- package/dist/Alarm-CbhflIzz.mjs.map +1 -0
- package/dist/Alarm-Nz1gIjfv.d.mts +252 -0
- package/dist/Alarm.d.mts +3 -0
- package/dist/Alarm.mjs +3 -0
- package/dist/BrowserCredentials-BWC4RqRn.d.mts +116 -0
- package/dist/BrowserCredentials-CktBoPgE.mjs +140 -0
- package/dist/BrowserCredentials-CktBoPgE.mjs.map +1 -0
- package/dist/BrowserCredentials.d.mts +2 -0
- package/dist/BrowserCredentials.mjs +1 -0
- package/dist/BrowserRestCapture.d.mts +14 -0
- package/dist/BrowserRestCapture.mjs +195 -0
- package/dist/BrowserRestCapture.mjs.map +1 -0
- package/dist/BrowserSession-5bdpqPIT.d.mts +152 -0
- package/dist/BrowserSession-Ce8cWN1A.mjs +673 -0
- package/dist/BrowserSession-Ce8cWN1A.mjs.map +1 -0
- package/dist/BrowserSession.d.mts +3 -0
- package/dist/BrowserSession.mjs +4 -0
- package/dist/BrowserUse-Bsi830Yv.mjs +263 -0
- package/dist/BrowserUse-Bsi830Yv.mjs.map +1 -0
- package/dist/BrowserUse-DF8afbNt.d.mts +59 -0
- package/dist/BrowserUse.d.mts +2 -0
- package/dist/BrowserUse.mjs +1 -0
- package/dist/CloudflareAiGateway-By8C8c5z.mjs +79 -0
- package/dist/CloudflareAiGateway-By8C8c5z.mjs.map +1 -0
- package/dist/CloudflareAiGateway-ukN34014.d.mts +63 -0
- package/dist/CloudflareAiGateway.d.mts +2 -0
- package/dist/CloudflareAiGateway.mjs +1 -0
- package/dist/CloudflareBindings-Dylkir84.mjs +123 -0
- package/dist/CloudflareBindings-Dylkir84.mjs.map +1 -0
- package/dist/CloudflareBindings-E1Sty9Vq.d.mts +134 -0
- package/dist/CloudflareBindings.d.mts +2 -0
- package/dist/CloudflareBindings.mjs +1 -0
- package/dist/CloudflareBrowser-CmiQpslZ.d.mts +41 -0
- package/dist/CloudflareBrowser-DmMCFeIQ.mjs +397 -0
- package/dist/CloudflareBrowser-DmMCFeIQ.mjs.map +1 -0
- package/dist/CloudflareBrowser.d.mts +3 -0
- package/dist/CloudflareBrowser.mjs +1 -0
- package/dist/CloudflareCodeMode-BMva6dCs.mjs +627 -0
- package/dist/CloudflareCodeMode-BMva6dCs.mjs.map +1 -0
- package/dist/CloudflareCodeMode-CeGXZtHv.d.mts +43 -0
- package/dist/CloudflareCodeMode.d.mts +2 -0
- package/dist/CloudflareCodeMode.mjs +1 -0
- package/dist/CloudflareConfig-BFWgEyhH.d.mts +102 -0
- package/dist/CloudflareConfig-rBlbPSIZ.mjs +125 -0
- package/dist/CloudflareConfig-rBlbPSIZ.mjs.map +1 -0
- package/dist/CloudflareConfig.d.mts +2 -0
- package/dist/CloudflareConfig.mjs +1 -0
- package/dist/CloudflareMemory-Bzp32ZpR.d.mts +120 -0
- package/dist/CloudflareMemory-DejIVKBT.mjs +207 -0
- package/dist/CloudflareMemory-DejIVKBT.mjs.map +1 -0
- package/dist/CloudflareMemory.d.mts +2 -0
- package/dist/CloudflareMemory.mjs +1 -0
- package/dist/CloudflareScheduling-BleqfC44.mjs +395 -0
- package/dist/CloudflareScheduling-BleqfC44.mjs.map +1 -0
- package/dist/CloudflareScheduling-vZ7iSR57.d.mts +54 -0
- package/dist/CloudflareScheduling.d.mts +2 -0
- package/dist/CloudflareScheduling.mjs +1 -0
- package/dist/CloudflareSubscriptions-IHSo19MU.d.mts +91 -0
- package/dist/CloudflareSubscriptions-S3fZF-Jo.mjs +529 -0
- package/dist/CloudflareSubscriptions-S3fZF-Jo.mjs.map +1 -0
- package/dist/CloudflareSubscriptions.d.mts +2 -0
- package/dist/CloudflareSubscriptions.mjs +1 -0
- package/dist/CloudflareThreadClient-BUv9Te1s.d.mts +2141 -0
- package/dist/CloudflareThreadClient-K7QJHYpK.mjs +496 -0
- package/dist/CloudflareThreadClient-K7QJHYpK.mjs.map +1 -0
- package/dist/CloudflareThreadClient.d.mts +3 -0
- package/dist/CloudflareThreadClient.mjs +3 -0
- package/dist/InteractiveBrowser-CMbA7YPP.d.mts +228 -0
- package/dist/InteractiveBrowser-e8ZvxC1C.mjs +2013 -0
- package/dist/InteractiveBrowser-e8ZvxC1C.mjs.map +1 -0
- package/dist/InteractiveBrowser.d.mts +2 -0
- package/dist/InteractiveBrowser.mjs +1 -0
- package/dist/LiveText-CfpcZaGb.d.mts +91 -0
- package/dist/LiveText-DaF0tN_p.mjs +148 -0
- package/dist/LiveText-DaF0tN_p.mjs.map +1 -0
- package/dist/LiveText.d.mts +2 -0
- package/dist/LiveText.mjs +1 -0
- package/dist/R2ToolResultStorage.d.mts +27 -0
- package/dist/R2ToolResultStorage.mjs +84 -0
- package/dist/R2ToolResultStorage.mjs.map +1 -0
- package/dist/ThreadObject-B2-PqnpT.d.mts +390 -0
- package/dist/ThreadObject-D1pCeNrq.mjs +1225 -0
- package/dist/ThreadObject-D1pCeNrq.mjs.map +1 -0
- package/dist/ThreadObject.d.mts +2 -0
- package/dist/ThreadObject.mjs +1 -0
- package/dist/WakeScheduler-67UeXBYK.d.mts +22 -0
- package/dist/WakeScheduler-DV4J4TRe.mjs +69 -0
- package/dist/WakeScheduler-DV4J4TRe.mjs.map +1 -0
- package/dist/WakeScheduler.d.mts +2 -0
- package/dist/WakeScheduler.mjs +1 -0
- package/dist/boundary-YrOnE2zO.mjs +41 -0
- package/dist/boundary-YrOnE2zO.mjs.map +1 -0
- package/dist/browser-failure-CWBBnf1j.mjs +70 -0
- package/dist/browser-failure-CWBBnf1j.mjs.map +1 -0
- package/dist/index.d.mts +17 -0
- package/dist/index.mjs +17 -0
- package/dist/prepared-admission-DngoYN74.mjs +57 -0
- package/dist/prepared-admission-DngoYN74.mjs.map +1 -0
- package/dist/rolldown-runtime-D3KUYPsf.mjs +12 -0
- package/package.json +134 -3
- package/src/Alarm.ts +3402 -0
- package/src/BrowserCredentials.ts +181 -0
- package/src/BrowserRestCapture.ts +383 -0
- package/src/BrowserSession.ts +993 -0
- package/src/BrowserUse.ts +438 -0
- package/src/CloudflareAiGateway.ts +176 -0
- package/src/CloudflareBindings.ts +225 -0
- package/src/CloudflareBrowser.ts +9 -0
- package/src/CloudflareCodeMode.ts +896 -0
- package/src/CloudflareConfig.ts +142 -0
- package/src/CloudflareMemory.ts +418 -0
- package/src/CloudflareScheduling.ts +736 -0
- package/src/CloudflareSubscriptions.ts +1032 -0
- package/src/CloudflareThreadClient.ts +980 -0
- package/src/InteractiveBrowser.ts +3300 -0
- package/src/LiveText.ts +145 -0
- package/src/R2ToolResultStorage.ts +131 -0
- package/src/ThreadObject.ts +1348 -0
- package/src/WakeScheduler.ts +102 -0
- package/src/index.ts +16 -0
- package/src/internal/boundary.ts +53 -0
- package/src/internal/browser-binding.ts +483 -0
- package/src/internal/browser-credentials.ts +481 -0
- package/src/internal/browser-failure.ts +145 -0
- package/src/internal/browser-file-selection.ts +146 -0
- package/src/internal/browser-quick-action.ts +666 -0
- package/src/internal/browser-readonly-live-view.ts +121 -0
- package/src/internal/browser-session-lifecycle.ts +200 -0
- package/src/internal/crypto.ts +44 -0
- package/src/internal/due-queue.ts +452 -0
- package/src/internal/host-protocol-error.ts +23 -0
- package/src/internal/layers.ts +1181 -0
- package/src/internal/live-text-observation.ts +8 -0
- package/src/internal/live-text-protocol.ts +59 -0
- package/src/internal/live-text.ts +339 -0
- package/src/internal/message-delivery.ts +149 -0
- package/src/internal/page-function.ts +12 -0
- package/src/internal/prepared-admission.ts +83 -0
- package/src/internal/progress-wait.ts +121 -0
- package/src/internal/transport.ts +53 -0
- package/README.md +0 -3
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
import * as Cause from 'effect/Cause'
|
|
2
|
+
import * as Context from 'effect/Context'
|
|
3
|
+
import * as Effect from 'effect/Effect'
|
|
4
|
+
import * as Redacted from 'effect/Redacted'
|
|
5
|
+
import * as Schema from 'effect/Schema'
|
|
6
|
+
import type { Frame, Page } from 'puppeteer-core/lib/esm/puppeteer/puppeteer-core-browser.js'
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
BrowserCredentialAccess,
|
|
10
|
+
type BrowserCredentialMaterial,
|
|
11
|
+
CardCredential,
|
|
12
|
+
type CredentialAccessRequest,
|
|
13
|
+
CredentialBinding,
|
|
14
|
+
type CredentialDispatch,
|
|
15
|
+
type CredentialFieldRole,
|
|
16
|
+
CredentialFillError,
|
|
17
|
+
CredentialFillResult,
|
|
18
|
+
CredentialForm,
|
|
19
|
+
CredentialOrigin,
|
|
20
|
+
CredentialTarget,
|
|
21
|
+
FillCredentialRequest,
|
|
22
|
+
LoginCredential,
|
|
23
|
+
} from '../BrowserCredentials'
|
|
24
|
+
|
|
25
|
+
/** Supplied only under the attachment lock, with the remaining command/session budget. */
|
|
26
|
+
export class BrowserSessionPage extends Context.Service<
|
|
27
|
+
BrowserSessionPage,
|
|
28
|
+
{
|
|
29
|
+
readonly page: Page
|
|
30
|
+
readonly commandTimeoutMillis: number
|
|
31
|
+
}
|
|
32
|
+
>()('@effect-agent/platform-cloudflare/internal/BrowserSessionPage') {}
|
|
33
|
+
|
|
34
|
+
const TargetReply = Schema.Union([
|
|
35
|
+
Schema.Struct({
|
|
36
|
+
action: Schema.String.check(Schema.isMaxLength(2048)),
|
|
37
|
+
shape: Schema.String.check(Schema.isMaxLength(128 * 1024)),
|
|
38
|
+
}),
|
|
39
|
+
Schema.Struct({ reason: Schema.Literals(['stale-target', 'unsupported']) }),
|
|
40
|
+
])
|
|
41
|
+
|
|
42
|
+
const WriteReply = Schema.Literals([
|
|
43
|
+
'filled',
|
|
44
|
+
'stale-target',
|
|
45
|
+
'unsupported-before-write',
|
|
46
|
+
'unsupported',
|
|
47
|
+
])
|
|
48
|
+
|
|
49
|
+
// One invocation-local isolated-world handle. Values never leave the page through this handle.
|
|
50
|
+
// This program neither discovers controls nor classifies roles nor observes page content.
|
|
51
|
+
const prepareFields = `(() => {
|
|
52
|
+
const doc = document;
|
|
53
|
+
let fields = [];
|
|
54
|
+
let form;
|
|
55
|
+
let snapshots = [];
|
|
56
|
+
const describe = el => {
|
|
57
|
+
if (!el || !el.isConnected || el.ownerDocument !== doc || doc !== document) return null;
|
|
58
|
+
const native = el instanceof HTMLTextAreaElement ||
|
|
59
|
+
(el instanceof HTMLSelectElement && !el.multiple) ||
|
|
60
|
+
(el instanceof HTMLInputElement && ['text','email','password','tel','number','month','search','url','date','time','week','datetime-local'].includes(el.type));
|
|
61
|
+
if (!native || el.matches(':disabled') || el.readOnly || !el.form) return null;
|
|
62
|
+
const values = [el.form.action, el.form.method, el.form.enctype, el.name, el.type, el.autocomplete ?? ''];
|
|
63
|
+
if (values.some(value => value.length > 2048)) return null;
|
|
64
|
+
return JSON.stringify(values);
|
|
65
|
+
};
|
|
66
|
+
const validate = () => fields.length > 0 && fields.every((el, index) =>
|
|
67
|
+
el.form === form && describe(el) !== null && describe(el) === snapshots[index]);
|
|
68
|
+
return {
|
|
69
|
+
prepare(selectors, roles) {
|
|
70
|
+
try {
|
|
71
|
+
fields = selectors.map(selector => {
|
|
72
|
+
const matches = doc.querySelectorAll(selector);
|
|
73
|
+
return matches.length === 1 ? matches[0] : null;
|
|
74
|
+
});
|
|
75
|
+
} catch { return {reason: 'stale-target'}; }
|
|
76
|
+
if (fields.some(el => !el) || new Set(fields).size !== fields.length) return {reason: 'stale-target'};
|
|
77
|
+
if (fields.some((el, index) => roles[index] === 'password' && el.type !== 'password')) return {reason: 'unsupported'};
|
|
78
|
+
snapshots = fields.map(describe);
|
|
79
|
+
form = fields[0].form;
|
|
80
|
+
if (!form || snapshots.some(value => value === null) || fields.some(el => el.form !== form)) return {reason: 'unsupported'};
|
|
81
|
+
return {action: form.action, shape: JSON.stringify(snapshots)};
|
|
82
|
+
},
|
|
83
|
+
target() { return validate() ? {action: form.action, shape: JSON.stringify(snapshots)} : {reason: 'stale-target'}; },
|
|
84
|
+
node(index) { return index === 0 ? doc.documentElement : index === 1 ? form : fields[index - 2]; },
|
|
85
|
+
fill(index, value) {
|
|
86
|
+
if (!validate()) return 'stale-target';
|
|
87
|
+
const el = fields[index];
|
|
88
|
+
if (el instanceof HTMLSelectElement) {
|
|
89
|
+
const options = [...el.options].filter(option => option.value === value);
|
|
90
|
+
if (options.length !== 1 || options[0].matches(':disabled')) return 'unsupported-before-write';
|
|
91
|
+
}
|
|
92
|
+
let prototype = Object.getPrototypeOf(el);
|
|
93
|
+
let setter;
|
|
94
|
+
while (prototype && !setter) { setter = Object.getOwnPropertyDescriptor(prototype, 'value')?.set; prototype = Object.getPrototypeOf(prototype); }
|
|
95
|
+
if (!setter) return 'unsupported-before-write';
|
|
96
|
+
setter.call(el, value);
|
|
97
|
+
if (el.value !== value) return 'unsupported';
|
|
98
|
+
el.dispatchEvent(new Event('input', {bubbles: true}));
|
|
99
|
+
el.dispatchEvent(new Event('change', {bubbles: true}));
|
|
100
|
+
return 'filled';
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
})()`
|
|
104
|
+
|
|
105
|
+
const secretFor = (material: BrowserCredentialMaterial, role: typeof CredentialFieldRole.Type) => {
|
|
106
|
+
if (material._tag === 'LoginCredential') {
|
|
107
|
+
return role === 'username'
|
|
108
|
+
? material.username
|
|
109
|
+
: role === 'password'
|
|
110
|
+
? material.password
|
|
111
|
+
: undefined
|
|
112
|
+
}
|
|
113
|
+
switch (role) {
|
|
114
|
+
case 'card-name':
|
|
115
|
+
return material.name
|
|
116
|
+
case 'card-number':
|
|
117
|
+
return material.number
|
|
118
|
+
case 'card-expiry':
|
|
119
|
+
return material.expiry
|
|
120
|
+
case 'card-expiry-month':
|
|
121
|
+
return material.expiryMonth
|
|
122
|
+
case 'card-expiry-year':
|
|
123
|
+
return material.expiryYear
|
|
124
|
+
case 'card-security-code':
|
|
125
|
+
return material.securityCode
|
|
126
|
+
case 'username':
|
|
127
|
+
case 'password':
|
|
128
|
+
return undefined
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Acquire exact native controls for this Scope; no credential material is resolved. */
|
|
133
|
+
const openForm = Effect.fnUntraced(function*(input: CredentialForm) {
|
|
134
|
+
const { page } = yield* BrowserSessionPage
|
|
135
|
+
|
|
136
|
+
const fail = (reason: CredentialFillError['reason']) =>
|
|
137
|
+
CredentialFillError.make({
|
|
138
|
+
reason,
|
|
139
|
+
dispatch: 'not-dispatched',
|
|
140
|
+
filled: 0,
|
|
141
|
+
cleanup: 'not-requested',
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
const remote = <A>(run: (signal: AbortSignal) => Promise<A>) =>
|
|
145
|
+
Effect.tryPromise({ try: run, catch: () => fail('provider') })
|
|
146
|
+
|
|
147
|
+
const release = (handles: ReadonlyArray<{ dispose(): Promise<void> }>) =>
|
|
148
|
+
Effect.promise(async () => {
|
|
149
|
+
await Promise.all(handles.map((handle) => handle.dispose().catch(() => {})))
|
|
150
|
+
}).pipe(Effect.interruptible, Effect.timeoutOption('1 second'), Effect.asVoid)
|
|
151
|
+
|
|
152
|
+
const acquire = <A extends { dispose(): Promise<void> }>(run: () => Promise<ReadonlyArray<A>>) =>
|
|
153
|
+
Effect.acquireRelease(
|
|
154
|
+
remote(async (signal) => {
|
|
155
|
+
const handles = await run()
|
|
156
|
+
|
|
157
|
+
if (signal.aborted) {
|
|
158
|
+
await Promise.all(handles.map((handle) => handle.dispose().catch(() => {})))
|
|
159
|
+
|
|
160
|
+
return undefined
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return handles
|
|
164
|
+
}).pipe(
|
|
165
|
+
Effect.filterOrFail(
|
|
166
|
+
(handles) => handles !== undefined,
|
|
167
|
+
() => fail('provider'),
|
|
168
|
+
),
|
|
169
|
+
Effect.interruptible,
|
|
170
|
+
),
|
|
171
|
+
release,
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
const origin = Effect.fnUntraced(function*(value: string) {
|
|
175
|
+
const url = yield* Effect.try({
|
|
176
|
+
try: () => new URL(value),
|
|
177
|
+
catch: () => fail('denied'),
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
if (url.username || url.password) return yield* fail('denied')
|
|
181
|
+
|
|
182
|
+
return yield* Schema.decodeEffect(CredentialOrigin)(url.origin).pipe(
|
|
183
|
+
Effect.mapError(() => fail('denied')),
|
|
184
|
+
)
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
const request = yield* Schema.decodeEffect(CredentialForm, {
|
|
188
|
+
onExcessProperty: 'error',
|
|
189
|
+
})(input).pipe(Effect.mapError(() => fail('denied')))
|
|
190
|
+
|
|
191
|
+
const roles = request.fields.map((field) => field.role)
|
|
192
|
+
|
|
193
|
+
if (
|
|
194
|
+
new Set(roles).size !== roles.length ||
|
|
195
|
+
roles.some(
|
|
196
|
+
(role) => (request.kind === 'login') !== (role === 'username' || role === 'password'),
|
|
197
|
+
)
|
|
198
|
+
) {
|
|
199
|
+
return yield* fail('denied')
|
|
200
|
+
}
|
|
201
|
+
let frame: Frame = page.mainFrame()
|
|
202
|
+
|
|
203
|
+
for (const selector of request.frame ?? []) {
|
|
204
|
+
const matches = yield* acquire(() => frame.$$(selector))
|
|
205
|
+
const element = matches[0]
|
|
206
|
+
|
|
207
|
+
if (matches.length !== 1 || element === undefined) return yield* fail('stale-target')
|
|
208
|
+
const child = yield* remote(() => element.contentFrame())
|
|
209
|
+
|
|
210
|
+
if (child === null) return yield* fail('unsupported')
|
|
211
|
+
frame = child
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const handles = yield* acquire(async () => [
|
|
215
|
+
await frame.isolatedRealm().evaluateHandle(prepareFields),
|
|
216
|
+
])
|
|
217
|
+
|
|
218
|
+
const held = handles[0]
|
|
219
|
+
|
|
220
|
+
if (held === undefined) return yield* fail('provider')
|
|
221
|
+
|
|
222
|
+
const inspect = (raw: unknown) =>
|
|
223
|
+
Schema.decodeUnknownEffect(TargetReply)(raw).pipe(
|
|
224
|
+
Effect.mapError(() => fail('provider')),
|
|
225
|
+
Effect.filterOrFail(
|
|
226
|
+
(reply) => 'action' in reply,
|
|
227
|
+
(reply) => fail('reason' in reply ? reply.reason : 'provider'),
|
|
228
|
+
),
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
const prepared = yield* remote(() =>
|
|
232
|
+
held.evaluate(
|
|
233
|
+
(state, selectors, roles) => {
|
|
234
|
+
if (typeof state !== 'object' || state === null) return null
|
|
235
|
+
|
|
236
|
+
return Reflect.apply(Reflect.get(state, 'prepare'), state, [selectors, roles])
|
|
237
|
+
},
|
|
238
|
+
request.fields.map((field) => field.selector),
|
|
239
|
+
roles,
|
|
240
|
+
)
|
|
241
|
+
).pipe(Effect.flatMap(inspect))
|
|
242
|
+
|
|
243
|
+
const target = CredentialTarget.make({
|
|
244
|
+
topOrigin: yield* origin(page.url()),
|
|
245
|
+
frameOrigin: yield* origin(frame.url()),
|
|
246
|
+
recipientOrigin: yield* origin(prepared.action),
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
const validate = Effect.gen(function*() {
|
|
250
|
+
if (
|
|
251
|
+
frame.detached ||
|
|
252
|
+
(yield* origin(page.url())) !== target.topOrigin ||
|
|
253
|
+
(yield* origin(frame.url())) !== target.frameOrigin
|
|
254
|
+
) {
|
|
255
|
+
return yield* fail('stale-target')
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const current = yield* remote(() =>
|
|
259
|
+
held.evaluate((state) => {
|
|
260
|
+
if (typeof state !== 'object' || state === null) return null
|
|
261
|
+
|
|
262
|
+
return Reflect.apply(Reflect.get(state, 'target'), state, [])
|
|
263
|
+
})
|
|
264
|
+
).pipe(Effect.flatMap(inspect))
|
|
265
|
+
|
|
266
|
+
if ((yield* origin(current.action)) !== target.recipientOrigin) {
|
|
267
|
+
return yield* fail('stale-target')
|
|
268
|
+
}
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
const binding = Effect.gen(function*() {
|
|
272
|
+
const nodes: Array<number> = []
|
|
273
|
+
|
|
274
|
+
for (let index = 0; index <= request.fields.length + 1; index++) {
|
|
275
|
+
const handles = yield* acquire(async () => [
|
|
276
|
+
await held.evaluateHandle((state, index) => {
|
|
277
|
+
if (typeof state !== 'object' || state === null) return null
|
|
278
|
+
|
|
279
|
+
return Reflect.apply(Reflect.get(state, 'node'), state, [index])
|
|
280
|
+
}, index),
|
|
281
|
+
])
|
|
282
|
+
|
|
283
|
+
const element = handles[0]?.asElement()
|
|
284
|
+
|
|
285
|
+
if (element === null || element === undefined) return yield* fail('stale-target')
|
|
286
|
+
nodes.push(yield* remote(() => element.backendNodeId()))
|
|
287
|
+
}
|
|
288
|
+
yield* validate
|
|
289
|
+
|
|
290
|
+
const digest = yield* remote(() => crypto.subtle.digest('SHA-256', new TextEncoder().encode(prepared.shape)))
|
|
291
|
+
|
|
292
|
+
return CredentialBinding.make({
|
|
293
|
+
version: 1,
|
|
294
|
+
pageId: page.mainFrame()._id,
|
|
295
|
+
frameId: frame._id,
|
|
296
|
+
target,
|
|
297
|
+
nodes,
|
|
298
|
+
shape: Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, '0')).join(
|
|
299
|
+
'',
|
|
300
|
+
),
|
|
301
|
+
})
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
return { held, target, validate, binding }
|
|
305
|
+
})
|
|
306
|
+
|
|
307
|
+
/** Bind a host-owned request to this exact document and fields without reading their values. */
|
|
308
|
+
export const prepareCredential = Effect.fnUntraced(
|
|
309
|
+
function*(input: CredentialForm) {
|
|
310
|
+
const form = yield* openForm(input)
|
|
311
|
+
|
|
312
|
+
return yield* form.binding
|
|
313
|
+
},
|
|
314
|
+
Effect.scoped,
|
|
315
|
+
Effect.withTracerEnabled(false),
|
|
316
|
+
)
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Fill explicit native fields on an existing page without submitting. The owning browser session
|
|
320
|
+
* serializes operations and closes on timeout, interruption or uncertain dispatch; this function releases
|
|
321
|
+
* only its local handles. Ordinary native observations remain available, including credential echoes.
|
|
322
|
+
*/
|
|
323
|
+
export const fillCredential = Effect.fnUntraced(function*(
|
|
324
|
+
input: FillCredentialRequest,
|
|
325
|
+
): Effect.fn.Return<
|
|
326
|
+
CredentialFillResult,
|
|
327
|
+
CredentialFillError,
|
|
328
|
+
BrowserCredentialAccess | BrowserSessionPage
|
|
329
|
+
> {
|
|
330
|
+
const { commandTimeoutMillis } = yield* BrowserSessionPage
|
|
331
|
+
let dispatch: typeof CredentialDispatch.Type = 'not-dispatched'
|
|
332
|
+
let filled = 0
|
|
333
|
+
|
|
334
|
+
const fail = (reason: CredentialFillError['reason']) =>
|
|
335
|
+
CredentialFillError.make({ reason, dispatch, filled, cleanup: 'not-requested' })
|
|
336
|
+
|
|
337
|
+
const remote = <A>(run: (signal: AbortSignal) => Promise<A>) =>
|
|
338
|
+
Effect.tryPromise({
|
|
339
|
+
try: async (signal) => {
|
|
340
|
+
try {
|
|
341
|
+
return { ok: true as const, value: await run(signal) }
|
|
342
|
+
} catch {
|
|
343
|
+
return { ok: false as const }
|
|
344
|
+
}
|
|
345
|
+
},
|
|
346
|
+
catch: () => fail('provider'),
|
|
347
|
+
}).pipe(
|
|
348
|
+
Effect.flatMap((result) => result.ok ? Effect.succeed(result.value) : Effect.fail(fail('provider'))),
|
|
349
|
+
)
|
|
350
|
+
|
|
351
|
+
return yield* Effect.gen(function*() {
|
|
352
|
+
const request = yield* Schema.decodeEffect(FillCredentialRequest, {
|
|
353
|
+
onExcessProperty: 'error',
|
|
354
|
+
})(input).pipe(Effect.mapError(() => fail('denied')))
|
|
355
|
+
|
|
356
|
+
const form = yield* openForm(
|
|
357
|
+
CredentialForm.make({
|
|
358
|
+
kind: request.kind,
|
|
359
|
+
fields: request.fields,
|
|
360
|
+
...(request.frame === undefined ? {} : { frame: request.frame }),
|
|
361
|
+
}),
|
|
362
|
+
)
|
|
363
|
+
|
|
364
|
+
const { held, target } = form
|
|
365
|
+
const validate = form.validate.pipe(Effect.mapError((error) => fail(error.reason)))
|
|
366
|
+
const roles = request.fields.map((field) => field.role)
|
|
367
|
+
|
|
368
|
+
if (request.binding !== undefined) {
|
|
369
|
+
const actual = yield* form.binding
|
|
370
|
+
const expected = request.binding
|
|
371
|
+
|
|
372
|
+
if (
|
|
373
|
+
actual.pageId !== expected.pageId ||
|
|
374
|
+
actual.frameId !== expected.frameId ||
|
|
375
|
+
actual.shape !== expected.shape ||
|
|
376
|
+
actual.nodes.length !== expected.nodes.length ||
|
|
377
|
+
actual.nodes.some((node, index) => node !== expected.nodes[index]) ||
|
|
378
|
+
actual.target.topOrigin !== expected.target.topOrigin ||
|
|
379
|
+
actual.target.frameOrigin !== expected.target.frameOrigin ||
|
|
380
|
+
actual.target.recipientOrigin !== expected.target.recipientOrigin
|
|
381
|
+
) {
|
|
382
|
+
return yield* fail('stale-target')
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
const access = yield* BrowserCredentialAccess
|
|
386
|
+
|
|
387
|
+
const authorization: CredentialAccessRequest = {
|
|
388
|
+
credential: request.credential,
|
|
389
|
+
kind: request.kind,
|
|
390
|
+
target,
|
|
391
|
+
roles,
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const authorize = Effect.suspend(() => access.authorize(authorization)).pipe(
|
|
395
|
+
Effect.mapError((error) => fail(error.reason)),
|
|
396
|
+
)
|
|
397
|
+
|
|
398
|
+
yield* authorize
|
|
399
|
+
yield* validate
|
|
400
|
+
|
|
401
|
+
const raw = yield* access
|
|
402
|
+
.resolve(authorization)
|
|
403
|
+
.pipe(Effect.mapError((error) => fail(error.reason)))
|
|
404
|
+
|
|
405
|
+
const material = yield* Schema.decodeEffect(
|
|
406
|
+
request.kind === 'login' ? LoginCredential : CardCredential,
|
|
407
|
+
)(raw).pipe(Effect.mapError(() => fail('resolver')))
|
|
408
|
+
|
|
409
|
+
for (const role of roles) {
|
|
410
|
+
if (secretFor(material, role) === undefined) return yield* fail('missing-credential')
|
|
411
|
+
}
|
|
412
|
+
for (const [index, role] of roles.entries()) {
|
|
413
|
+
yield* authorize
|
|
414
|
+
yield* validate
|
|
415
|
+
const value = secretFor(material, role)
|
|
416
|
+
|
|
417
|
+
if (value === undefined) return yield* fail('missing-credential')
|
|
418
|
+
const previous = dispatch
|
|
419
|
+
|
|
420
|
+
const result = yield* remote((signal) => {
|
|
421
|
+
if (signal.aborted) return Promise.reject(fail('provider'))
|
|
422
|
+
dispatch = 'possibly-dispatched'
|
|
423
|
+
|
|
424
|
+
return held.evaluate(
|
|
425
|
+
(state, field, secret) => {
|
|
426
|
+
if (typeof state !== 'object' || state === null) return 'stale-target'
|
|
427
|
+
|
|
428
|
+
return Reflect.apply(Reflect.get(state, 'fill'), state, [field, secret])
|
|
429
|
+
},
|
|
430
|
+
index,
|
|
431
|
+
Redacted.value(value),
|
|
432
|
+
)
|
|
433
|
+
}).pipe(
|
|
434
|
+
Effect.flatMap(Schema.decodeUnknownEffect(WriteReply)),
|
|
435
|
+
Effect.mapError(() => fail('provider')),
|
|
436
|
+
)
|
|
437
|
+
|
|
438
|
+
if (result === 'stale-target' || result === 'unsupported-before-write') {
|
|
439
|
+
dispatch = previous
|
|
440
|
+
|
|
441
|
+
return yield* fail(result === 'stale-target' ? 'stale-target' : 'unsupported')
|
|
442
|
+
}
|
|
443
|
+
if (result === 'unsupported') return yield* fail('unsupported')
|
|
444
|
+
dispatch = 'dispatched'
|
|
445
|
+
filled++
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
return CredentialFillResult.make({ dispatch: 'dispatched', filled })
|
|
449
|
+
}).pipe(
|
|
450
|
+
Effect.scoped,
|
|
451
|
+
Effect.timeoutOrElse({
|
|
452
|
+
duration: commandTimeoutMillis,
|
|
453
|
+
orElse: () => Effect.fail(fail('timeout')),
|
|
454
|
+
}),
|
|
455
|
+
Effect.catchCause((cause) => {
|
|
456
|
+
if (Cause.hasInterrupts(cause)) return Effect.interrupt
|
|
457
|
+
if (
|
|
458
|
+
cause.reasons.some(
|
|
459
|
+
(reason) =>
|
|
460
|
+
reason._tag === 'Die' ||
|
|
461
|
+
(reason._tag === 'Fail' && !Schema.is(CredentialFillError)(reason.error)),
|
|
462
|
+
)
|
|
463
|
+
) {
|
|
464
|
+
return Effect.fail(fail('provider'))
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
const expected = cause.reasons.find(
|
|
468
|
+
(reason) => reason._tag === 'Fail' && Schema.is(CredentialFillError)(reason.error),
|
|
469
|
+
)
|
|
470
|
+
|
|
471
|
+
return Effect.fail(
|
|
472
|
+
fail(
|
|
473
|
+
expected?._tag === 'Fail' && Schema.is(CredentialFillError)(expected.error)
|
|
474
|
+
? expected.error.reason
|
|
475
|
+
: 'provider',
|
|
476
|
+
),
|
|
477
|
+
)
|
|
478
|
+
}),
|
|
479
|
+
Effect.withTracerEnabled(false),
|
|
480
|
+
)
|
|
481
|
+
})
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import * as FailureDiagnostic from '@systemfsoftware/agent/failure-diagnostic'
|
|
2
|
+
import * as Cause from 'effect/Cause'
|
|
3
|
+
import * as Context from 'effect/Context'
|
|
4
|
+
import * as Data from 'effect/Data'
|
|
5
|
+
import * as Effect from 'effect/Effect'
|
|
6
|
+
import * as ErrorReporter from 'effect/ErrorReporter'
|
|
7
|
+
import * as Predicate from 'effect/Predicate'
|
|
8
|
+
import * as Schema from 'effect/Schema'
|
|
9
|
+
|
|
10
|
+
/** Translate typed failures without dropping cleanup reasons during actual cancellation. */
|
|
11
|
+
export const mapBrowserError =
|
|
12
|
+
<E, E2>(map: (error: E) => E2, mapDefect?: (defect: unknown) => unknown) =>
|
|
13
|
+
<A, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E2, R> =>
|
|
14
|
+
Effect.uninterruptibleMask((restore) =>
|
|
15
|
+
restore(effect).pipe(
|
|
16
|
+
Effect.catchCause((cause) => {
|
|
17
|
+
const mapped = Cause.map(cause, map)
|
|
18
|
+
|
|
19
|
+
return Effect.failCause(
|
|
20
|
+
mapDefect === undefined
|
|
21
|
+
? mapped
|
|
22
|
+
: Cause.fromReasons(
|
|
23
|
+
mapped.reasons.map((reason) =>
|
|
24
|
+
Cause.isDieReason(reason)
|
|
25
|
+
? Cause.makeDieReason(mapDefect(reason.defect)).annotate(
|
|
26
|
+
Context.makeUnsafe(new Map(reason.annotations)),
|
|
27
|
+
)
|
|
28
|
+
: reason
|
|
29
|
+
),
|
|
30
|
+
),
|
|
31
|
+
)
|
|
32
|
+
}),
|
|
33
|
+
)
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
/** Browser stage and classification. Original causes stay private and out of public JSON. */
|
|
37
|
+
export class BrowserRunFailure extends Data.TaggedError('BrowserRunFailure')<{
|
|
38
|
+
readonly operation: string
|
|
39
|
+
readonly reason:
|
|
40
|
+
| 'provider'
|
|
41
|
+
| 'timeout'
|
|
42
|
+
| 'malformed'
|
|
43
|
+
| 'configuration'
|
|
44
|
+
| 'authorization'
|
|
45
|
+
| 'rate-limited'
|
|
46
|
+
| 'pending'
|
|
47
|
+
readonly status?: number
|
|
48
|
+
readonly cause?: BrowserRunFailure | { readonly name: string }
|
|
49
|
+
}> {
|
|
50
|
+
readonly [ErrorReporter.severity] = 'Error' as const
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** A public projection of an already reported private failure must not produce another issue. */
|
|
54
|
+
export const reportedBrowserError = <A extends object>(error: A): A =>
|
|
55
|
+
Object.assign(error, { [ErrorReporter.ignore]: true })
|
|
56
|
+
|
|
57
|
+
export const inheritBrowserReport = <A extends object>(original: unknown, error: A): A =>
|
|
58
|
+
ErrorReporter.isIgnored(original) ? reportedBrowserError(error) : error
|
|
59
|
+
|
|
60
|
+
const reasonSchema = Schema.Literals([
|
|
61
|
+
'provider',
|
|
62
|
+
'timeout',
|
|
63
|
+
'malformed',
|
|
64
|
+
'configuration',
|
|
65
|
+
'authorization',
|
|
66
|
+
'rate-limited',
|
|
67
|
+
'pending',
|
|
68
|
+
])
|
|
69
|
+
|
|
70
|
+
const statusSchema = Schema.Int.check(Schema.isBetween({ minimum: 100, maximum: 599 }))
|
|
71
|
+
|
|
72
|
+
const failureNames = new Set([
|
|
73
|
+
'TypeError',
|
|
74
|
+
'RangeError',
|
|
75
|
+
'SyntaxError',
|
|
76
|
+
'TimeoutError',
|
|
77
|
+
'SchemaError',
|
|
78
|
+
'ProtocolError',
|
|
79
|
+
'TargetCloseError',
|
|
80
|
+
'ConnectionClosedError',
|
|
81
|
+
])
|
|
82
|
+
|
|
83
|
+
/** Construct a reporter-safe copy, never the error carrying the private original cause. */
|
|
84
|
+
const projectBrowserFailure = (operation: string, cause?: unknown): BrowserRunFailure => {
|
|
85
|
+
const source = cause instanceof BrowserRunFailure ? cause.cause : cause
|
|
86
|
+
|
|
87
|
+
return new BrowserRunFailure({
|
|
88
|
+
operation: cause instanceof BrowserRunFailure ? cause.operation : operation,
|
|
89
|
+
reason: cause instanceof BrowserRunFailure
|
|
90
|
+
? cause.reason
|
|
91
|
+
: Predicate.isObject(cause) && 'reason' in cause && Schema.is(reasonSchema)(cause.reason)
|
|
92
|
+
? cause.reason
|
|
93
|
+
: 'provider',
|
|
94
|
+
...(Predicate.isObject(cause) && 'status' in cause && Schema.is(statusSchema)(cause.status)
|
|
95
|
+
? { status: cause.status }
|
|
96
|
+
: {}),
|
|
97
|
+
...(source instanceof BrowserRunFailure
|
|
98
|
+
? { cause: projectBrowserFailure(operation, source) }
|
|
99
|
+
: Predicate.isObject(source) &&
|
|
100
|
+
'name' in source &&
|
|
101
|
+
typeof source.name === 'string' &&
|
|
102
|
+
failureNames.has(source.name)
|
|
103
|
+
? { cause: { name: source.name } }
|
|
104
|
+
: {}),
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export const browserFailure = (operation: string, cause?: unknown): BrowserRunFailure => {
|
|
109
|
+
if (cause instanceof BrowserRunFailure) return cause
|
|
110
|
+
|
|
111
|
+
const failure = projectBrowserFailure(operation, cause)
|
|
112
|
+
|
|
113
|
+
return cause === undefined ? failure : FailureDiagnostic.withPrivateCause(failure, cause)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Keep mixed defects and interruptions distinct while discarding all foreign diagnostic content. */
|
|
117
|
+
export const reportBrowserCause = <E>(
|
|
118
|
+
operation: string,
|
|
119
|
+
cause: Cause.Cause<E>,
|
|
120
|
+
): Effect.Effect<void> =>
|
|
121
|
+
Effect.suspend(() =>
|
|
122
|
+
ErrorReporter.report(
|
|
123
|
+
Cause.fromReasons(
|
|
124
|
+
cause.reasons
|
|
125
|
+
.filter((reason) => !Cause.isFailReason(reason) || !ErrorReporter.isIgnored(reason.error))
|
|
126
|
+
.map((reason) => {
|
|
127
|
+
switch (reason._tag) {
|
|
128
|
+
case 'Fail':
|
|
129
|
+
return Cause.makeFailReason(projectBrowserFailure(operation, reason.error))
|
|
130
|
+
case 'Die':
|
|
131
|
+
return Cause.makeDieReason(projectBrowserFailure(operation, reason.defect))
|
|
132
|
+
case 'Interrupt':
|
|
133
|
+
return reason
|
|
134
|
+
}
|
|
135
|
+
}),
|
|
136
|
+
),
|
|
137
|
+
)
|
|
138
|
+
).pipe(
|
|
139
|
+
// Reporting is best effort; a defective sink must not replace the operation's failure.
|
|
140
|
+
Effect.catchCause((cause) =>
|
|
141
|
+
Cause.hasInterrupts(cause)
|
|
142
|
+
? Effect.failCause(Cause.fromReasons<never>(cause.reasons.filter(Cause.isInterruptReason)))
|
|
143
|
+
: Effect.void
|
|
144
|
+
),
|
|
145
|
+
)
|