@reset-framework/sdk 1.2.2 → 1.2.4

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/src/client.js CHANGED
@@ -1,932 +1,932 @@
1
- import {
2
- createResetTransport,
3
- getResetRuntime,
4
- invoke,
5
- invokeRaw,
6
- isResetRuntimeAvailable
7
- } from "./transport.js"
8
- import { createEventApi } from "./events.js"
9
- import { ResetProtocolError } from "./errors.js"
10
-
11
- function isObjectLike(value) {
12
- return typeof value === "object" && value !== null && !Array.isArray(value)
13
- }
14
-
15
- function requireObject(value, command) {
16
- if (!isObjectLike(value)) {
17
- throw new ResetProtocolError(
18
- `Reset runtime returned an invalid payload for '${command}'.`,
19
- { command }
20
- )
21
- }
22
-
23
- return value
24
- }
25
-
26
- function requireStringField(record, field, command) {
27
- const value = record[field]
28
-
29
- if (typeof value !== "string" || value.trim() === "") {
30
- throw new ResetProtocolError(
31
- `Reset runtime returned an invalid '${field}' field for '${command}'.`,
32
- { command }
33
- )
34
- }
35
-
36
- return value
37
- }
38
-
39
- function requireBooleanField(record, field, command) {
40
- const value = record[field]
41
-
42
- if (typeof value !== "boolean") {
43
- throw new ResetProtocolError(
44
- `Reset runtime returned an invalid '${field}' field for '${command}'.`,
45
- { command }
46
- )
47
- }
48
-
49
- return value
50
- }
51
-
52
- function requireNumberField(record, field, command) {
53
- const value = record[field]
54
-
55
- if (typeof value !== "number" || Number.isNaN(value)) {
56
- throw new ResetProtocolError(
57
- `Reset runtime returned an invalid '${field}' field for '${command}'.`,
58
- { command }
59
- )
60
- }
61
-
62
- return value
63
- }
64
-
65
- function optionalStringField(record, field, fallback, command) {
66
- const value = record[field]
67
-
68
- if (value === undefined || value === null) {
69
- return fallback
70
- }
71
-
72
- if (typeof value !== "string") {
73
- throw new ResetProtocolError(
74
- `Reset runtime returned an invalid '${field}' field for '${command}'.`,
75
- { command }
76
- )
77
- }
78
-
79
- return value
80
- }
81
-
82
- function normalizeStringArray(value, field, command) {
83
- if (!Array.isArray(value)) {
84
- throw new ResetProtocolError(
85
- `Reset runtime returned an invalid '${field}' field for '${command}'.`,
86
- { command }
87
- )
88
- }
89
-
90
- return Object.freeze(
91
- value.map((entry) => {
92
- if (typeof entry !== "string") {
93
- throw new ResetProtocolError(
94
- `Reset runtime returned an invalid '${field}' field for '${command}'.`,
95
- { command }
96
- )
97
- }
98
-
99
- return entry
100
- })
101
- )
102
- }
103
-
104
- function normalizeAppInfo(payload, command) {
105
- const record = requireObject(payload, command)
106
-
107
- return Object.freeze({
108
- id: requireStringField(record, "id", command),
109
- name: requireStringField(record, "name", command),
110
- slug: requireStringField(record, "slug", command),
111
- version: requireStringField(record, "version", command),
112
- windowTitle: requireStringField(record, "windowTitle", command)
113
- })
114
- }
115
-
116
- function normalizeRuntimeInfo(payload, command) {
117
- const record = requireObject(payload, command)
118
-
119
- return Object.freeze({
120
- platform: requireStringField(record, "platform", command),
121
- arch: requireStringField(record, "arch", command),
122
- frameworkVersion: requireStringField(record, "frameworkVersion", command),
123
- bridgeVersion: requireStringField(record, "bridgeVersion", command),
124
- debug: requireBooleanField(record, "debug", command)
125
- })
126
- }
127
-
128
- function normalizeCapabilities(payload, command) {
129
- const record = requireObject(payload, command)
130
- const commands = Array.isArray(record.commands) ? record.commands : []
131
- const permissions = Array.isArray(record.permissions) ? record.permissions : []
132
-
133
- return Object.freeze({
134
- commands: Object.freeze(commands.map((entry) => Object.freeze({ ...entry }))),
135
- permissions: Object.freeze([...permissions])
136
- })
137
- }
138
-
139
- function normalizeProtocolLaunch(payload, command) {
140
- const record = requireObject(payload, command)
141
-
142
- return Object.freeze({
143
- id: requireStringField(record, "id", command),
144
- url: requireStringField(record, "url", command),
145
- scheme: requireStringField(record, "scheme", command),
146
- host: optionalStringField(record, "host", "", command),
147
- path: optionalStringField(record, "path", "", command),
148
- query: optionalStringField(record, "query", "", command),
149
- fragment: optionalStringField(record, "fragment", "", command),
150
- source: requireStringField(record, "source", command),
151
- coldStart: requireBooleanField(record, "coldStart", command)
152
- })
153
- }
154
-
155
- function normalizeProtocolLaunchEnvelope(payload, command) {
156
- const record = requireObject(payload, command)
157
- const launch = record.launch
158
-
159
- if (launch === null) {
160
- return null
161
- }
162
-
163
- return normalizeProtocolLaunch(launch, command)
164
- }
165
-
166
- function normalizeProtocolLaunchList(payload, command) {
167
- const record = requireObject(payload, command)
168
- const launches = record.launches
169
-
170
- if (!Array.isArray(launches)) {
171
- throw new ResetProtocolError(
172
- `Reset runtime returned an invalid 'launches' field for '${command}'.`,
173
- { command }
174
- )
175
- }
176
-
177
- return Object.freeze(launches.map((entry) => normalizeProtocolLaunch(entry, command)))
178
- }
179
-
180
- function normalizeProtocolInfo(payload, command) {
181
- const record = requireObject(payload, command)
182
-
183
- return Object.freeze({
184
- supported: requireBooleanField(record, "supported", command),
185
- module: requireStringField(record, "module", command),
186
- platform: requireStringField(record, "platform", command),
187
- configuredSchemes: normalizeStringArray(record.configuredSchemes ?? [], "configuredSchemes", command),
188
- bundledSchemes: normalizeStringArray(record.bundledSchemes ?? [], "bundledSchemes", command),
189
- eventName: requireStringField(record, "eventName", command),
190
- pendingCount: requireNumberField(record, "pendingCount", command),
191
- eventStreamActive: requireBooleanField(record, "eventStreamActive", command),
192
- runtimeRegistration: requireBooleanField(record, "runtimeRegistration", command)
193
- })
194
- }
195
-
196
- function normalizeBackendInfo(payload, command) {
197
- const record = requireObject(payload, command)
198
- const runtime = isObjectLike(record.runtime) ? record.runtime : {}
199
-
200
- return Object.freeze({
201
- supported: requireBooleanField(record, "supported", command),
202
- configured: requireBooleanField(record, "configured", command),
203
- running: requireBooleanField(record, "running", command),
204
- available: requireBooleanField(record, "available", command),
205
- protocol: requireStringField(record, "protocol", command),
206
- pid: typeof record.pid === "number" ? record.pid : null,
207
- entryPath: optionalStringField(record, "entryPath", "", command),
208
- runnerPath: optionalStringField(record, "runnerPath", "", command),
209
- executablePath: optionalStringField(record, "executablePath", "", command),
210
- workingDirectory: optionalStringField(record, "workingDirectory", "", command),
211
- methods: normalizeStringArray(record.methods ?? [], "methods", command),
212
- runtime: Object.freeze({
213
- name: optionalStringField(runtime, "name", "", command),
214
- version: optionalStringField(runtime, "version", "", command),
215
- platform: optionalStringField(runtime, "platform", "", command),
216
- arch: optionalStringField(runtime, "arch", "", command)
217
- }),
218
- lastError: optionalStringField(record, "lastError", "", command)
219
- })
220
- }
221
-
222
- function createCommandApi(transport) {
223
- return Object.freeze({
224
- invoke(command, payload = {}) {
225
- return transport.invoke(command, payload)
226
- },
227
- invokeRaw(command, payload = {}) {
228
- return transport.invokeRaw(command, payload)
229
- }
230
- })
231
- }
232
-
233
- function createAppApi(transport) {
234
- return Object.freeze({
235
- async getId() {
236
- const payload = requireObject(await transport.invoke("app.getId"), "app.getId")
237
- return requireStringField(payload, "id", "app.getId")
238
- },
239
- async getName() {
240
- const payload = requireObject(await transport.invoke("app.getName"), "app.getName")
241
- return requireStringField(payload, "name", "app.getName")
242
- },
243
- async getSlug() {
244
- const payload = requireObject(await transport.invoke("app.getSlug"), "app.getSlug")
245
- return requireStringField(payload, "slug", "app.getSlug")
246
- },
247
- async getVersion() {
248
- const payload = requireObject(await transport.invoke("app.getVersion"), "app.getVersion")
249
- return requireStringField(payload, "version", "app.getVersion")
250
- },
251
- async getInfo() {
252
- return normalizeAppInfo(await transport.invoke("app.getInfo"), "app.getInfo")
253
- },
254
- show() {
255
- return transport.invoke("app.show")
256
- },
257
- hide() {
258
- return transport.invoke("app.hide")
259
- },
260
- quit() {
261
- return transport.invoke("app.quit")
262
- },
263
- relaunch() {
264
- return transport.invoke("app.relaunch")
265
- }
266
- })
267
- }
268
-
269
- function createRuntimeApi(transport) {
270
- return Object.freeze({
271
- async getPlatform() {
272
- const payload = requireObject(await transport.invoke("runtime.getPlatform"), "runtime.getPlatform")
273
- return requireStringField(payload, "platform", "runtime.getPlatform")
274
- },
275
- async getArchitecture() {
276
- const payload = requireObject(await transport.invoke("runtime.getArchitecture"), "runtime.getArchitecture")
277
- return requireStringField(payload, "arch", "runtime.getArchitecture")
278
- },
279
- async getFrameworkVersion() {
280
- const payload = requireObject(
281
- await transport.invoke("runtime.getFrameworkVersion"),
282
- "runtime.getFrameworkVersion"
283
- )
284
- return requireStringField(payload, "version", "runtime.getFrameworkVersion")
285
- },
286
- async getBridgeVersion() {
287
- const payload = requireObject(
288
- await transport.invoke("runtime.getBridgeVersion"),
289
- "runtime.getBridgeVersion"
290
- )
291
- return requireStringField(payload, "version", "runtime.getBridgeVersion")
292
- },
293
- async getInfo() {
294
- return normalizeRuntimeInfo(await transport.invoke("runtime.getInfo"), "runtime.getInfo")
295
- },
296
- async getCapabilities() {
297
- return normalizeCapabilities(
298
- await transport.invoke("runtime.getCapabilities"),
299
- "runtime.getCapabilities"
300
- )
301
- }
302
- })
303
- }
304
-
305
- function createWindowApi(transport) {
306
- return Object.freeze({
307
- getCurrent() {
308
- return transport.invoke("window.getCurrent")
309
- },
310
- getInfo() {
311
- return transport.invoke("window.getInfo")
312
- },
313
- list() {
314
- return transport.invoke("window.list")
315
- },
316
- show() {
317
- return transport.invoke("window.show")
318
- },
319
- hide() {
320
- return transport.invoke("window.hide")
321
- },
322
- focus() {
323
- return transport.invoke("window.focus")
324
- },
325
- close() {
326
- return transport.invoke("window.close")
327
- },
328
- minimize() {
329
- return transport.invoke("window.minimize")
330
- },
331
- maximize() {
332
- return transport.invoke("window.maximize")
333
- },
334
- center() {
335
- return transport.invoke("window.center")
336
- },
337
- setTitle(title) {
338
- return transport.invoke("window.setTitle", { title })
339
- },
340
- setSize(width, height) {
341
- return transport.invoke("window.setSize", { width, height })
342
- },
343
- setResizable(resizable) {
344
- return transport.invoke("window.setResizable", { resizable })
345
- }
346
- })
347
- }
348
-
349
- function createDialogApi(transport) {
350
- return Object.freeze({
351
- openFile(options = {}) {
352
- return transport.invoke("dialog.openFile", options)
353
- },
354
- saveFile(options = {}) {
355
- return transport.invoke("dialog.saveFile", options)
356
- },
357
- message(title, message) {
358
- return transport.invoke("dialog.message", { title, message })
359
- },
360
- confirm(title, message) {
361
- return transport.invoke("dialog.confirm", { title, message })
362
- }
363
- })
364
- }
365
-
366
- function createFsApi(transport) {
367
- return Object.freeze({
368
- async readTextFile(path) {
369
- const payload = requireObject(await transport.invoke("fs.readTextFile", { path }), "fs.readTextFile")
370
- return requireStringField(payload, "text", "fs.readTextFile")
371
- },
372
- writeTextFile(path, text, options = {}) {
373
- return transport.invoke("fs.writeTextFile", {
374
- path,
375
- text,
376
- ...options
377
- })
378
- },
379
- async exists(path) {
380
- const payload = requireObject(await transport.invoke("fs.exists", { path }), "fs.exists")
381
- return requireBooleanField(payload, "exists", "fs.exists")
382
- },
383
- mkdir(path, options = {}) {
384
- return transport.invoke("fs.mkdir", {
385
- path,
386
- ...options
387
- })
388
- },
389
- remove(path, options = {}) {
390
- return transport.invoke("fs.remove", {
391
- path,
392
- ...options
393
- })
394
- },
395
- rename(from, to) {
396
- return transport.invoke("fs.rename", { from, to })
397
- },
398
- async readDir(path) {
399
- const payload = requireObject(await transport.invoke("fs.readDir", { path }), "fs.readDir")
400
- return Array.isArray(payload.entries) ? payload.entries : []
401
- }
402
- })
403
- }
404
-
405
- function createPathApi(transport) {
406
- return Object.freeze({
407
- async join(...segments) {
408
- const payload = requireObject(await transport.invoke("path.join", { segments }), "path.join")
409
- return requireStringField(payload, "path", "path.join")
410
- },
411
- async basename(path) {
412
- const payload = requireObject(await transport.invoke("path.basename", { path }), "path.basename")
413
- return requireStringField(payload, "name", "path.basename")
414
- },
415
- async dirname(path) {
416
- const payload = requireObject(await transport.invoke("path.dirname", { path }), "path.dirname")
417
- return requireStringField(payload, "path", "path.dirname")
418
- },
419
- async resolve(path, base) {
420
- const payload = requireObject(await transport.invoke("path.resolve", { path, base }), "path.resolve")
421
- return requireStringField(payload, "path", "path.resolve")
422
- },
423
- async getHomeDir() {
424
- const payload = requireObject(await transport.invoke("path.getHomeDir"), "path.getHomeDir")
425
- return requireStringField(payload, "path", "path.getHomeDir")
426
- },
427
- async getTempDir() {
428
- const payload = requireObject(await transport.invoke("path.getTempDir"), "path.getTempDir")
429
- return requireStringField(payload, "path", "path.getTempDir")
430
- },
431
- async getAppDataDir() {
432
- const payload = requireObject(await transport.invoke("path.getAppDataDir"), "path.getAppDataDir")
433
- return requireStringField(payload, "path", "path.getAppDataDir")
434
- },
435
- async getAppConfigDir() {
436
- const payload = requireObject(await transport.invoke("path.getAppConfigDir"), "path.getAppConfigDir")
437
- return requireStringField(payload, "path", "path.getAppConfigDir")
438
- },
439
- async getCacheDir() {
440
- const payload = requireObject(await transport.invoke("path.getCacheDir"), "path.getCacheDir")
441
- return requireStringField(payload, "path", "path.getCacheDir")
442
- },
443
- getInfo() {
444
- return transport.invoke("path.getInfo")
445
- }
446
- })
447
- }
448
-
449
- function createShellApi(transport) {
450
- return Object.freeze({
451
- openExternal(url) {
452
- return transport.invoke("shell.openExternal", { url })
453
- },
454
- openPath(path) {
455
- return transport.invoke("shell.openPath", { path })
456
- },
457
- showItemInFolder(path) {
458
- return transport.invoke("shell.showItemInFolder", { path })
459
- }
460
- })
461
- }
462
-
463
- function createClipboardApi(transport) {
464
- return Object.freeze({
465
- async readText() {
466
- const payload = requireObject(await transport.invoke("clipboard.readText"), "clipboard.readText")
467
- return requireStringField(payload, "text", "clipboard.readText")
468
- },
469
- writeText(text) {
470
- return transport.invoke("clipboard.writeText", { text })
471
- }
472
- })
473
- }
474
-
475
- function createNotificationApi(transport) {
476
- return Object.freeze({
477
- async isSupported() {
478
- const payload = requireObject(
479
- await transport.invoke("notification.isSupported"),
480
- "notification.isSupported"
481
- )
482
- return requireBooleanField(payload, "supported", "notification.isSupported")
483
- },
484
- requestPermission() {
485
- return transport.invoke("notification.requestPermission")
486
- },
487
- show(options) {
488
- return transport.invoke("notification.show", options)
489
- }
490
- })
491
- }
492
-
493
- function createScreenApi(transport) {
494
- return Object.freeze({
495
- getCursorPosition() {
496
- return transport.invoke("screen.getCursorPosition")
497
- },
498
- getPrimaryDisplay() {
499
- return transport.invoke("screen.getPrimaryDisplay")
500
- },
501
- async getDisplays() {
502
- const payload = requireObject(await transport.invoke("screen.getDisplays"), "screen.getDisplays")
503
- return Array.isArray(payload.displays) ? payload.displays : []
504
- }
505
- })
506
- }
507
-
508
- function createStorageApi(transport) {
509
- return Object.freeze({
510
- async get(key) {
511
- const payload = requireObject(await transport.invoke("storage.get", { key }), "storage.get")
512
- return payload.value
513
- },
514
- set(key, value) {
515
- return transport.invoke("storage.set", { key, value })
516
- },
517
- remove(key) {
518
- return transport.invoke("storage.remove", { key })
519
- },
520
- clear() {
521
- return transport.invoke("storage.clear")
522
- },
523
- async getAll() {
524
- const payload = requireObject(await transport.invoke("storage.getAll"), "storage.getAll")
525
- return requireObject(payload.entries ?? {}, "storage.getAll")
526
- }
527
- })
528
- }
529
-
530
- function createWebViewApi(transport) {
531
- return Object.freeze({
532
- getInfo() {
533
- return transport.invoke("webview.getInfo")
534
- },
535
- reload() {
536
- return transport.invoke("webview.reload")
537
- },
538
- goBack() {
539
- return transport.invoke("webview.goBack")
540
- },
541
- goForward() {
542
- return transport.invoke("webview.goForward")
543
- },
544
- navigate(options) {
545
- return transport.invoke("webview.navigate", options)
546
- },
547
- setZoomFactor(factor) {
548
- return transport.invoke("webview.setZoomFactor", { factor })
549
- }
550
- })
551
- }
552
-
553
- function createCryptoApi(transport) {
554
- return Object.freeze({
555
- getInfo() {
556
- return transport.invoke("crypto.getInfo")
557
- },
558
- randomBytes(size, options = {}) {
559
- return transport.invoke("crypto.randomBytes", {
560
- size,
561
- encoding: options.encoding
562
- })
563
- },
564
- async randomUuid() {
565
- const payload = requireObject(await transport.invoke("crypto.randomUuid"), "crypto.randomUuid")
566
- return requireStringField(payload, "uuid", "crypto.randomUuid")
567
- }
568
- })
569
- }
570
-
571
- function createProcessApi(transport) {
572
- return Object.freeze({
573
- async getPid() {
574
- const payload = requireObject(await transport.invoke("process.getPid"), "process.getPid")
575
- return requireNumberField(payload, "pid", "process.getPid")
576
- },
577
- async getCwd() {
578
- const payload = requireObject(await transport.invoke("process.getCwd"), "process.getCwd")
579
- return requireStringField(payload, "path", "process.getCwd")
580
- },
581
- getInfo() {
582
- return transport.invoke("process.getInfo")
583
- }
584
- })
585
- }
586
-
587
- function createPowerApi(transport) {
588
- return Object.freeze({
589
- getInfo() {
590
- return transport.invoke("power.getInfo")
591
- }
592
- })
593
- }
594
-
595
- function createMenuApi(transport) {
596
- return Object.freeze({
597
- getInfo() {
598
- return transport.invoke("menu.getInfo")
599
- },
600
- setApplicationMenu(items) {
601
- return transport.invoke("menu.setApplicationMenu", { items })
602
- },
603
- clearApplicationMenu() {
604
- return transport.invoke("menu.clearApplicationMenu")
605
- }
606
- })
607
- }
608
-
609
- function createTrayApi(transport) {
610
- return Object.freeze({
611
- getInfo() {
612
- return transport.invoke("tray.getInfo")
613
- },
614
- create(options = {}) {
615
- return transport.invoke("tray.create", options)
616
- },
617
- setTitle(title) {
618
- return transport.invoke("tray.setTitle", { title })
619
- },
620
- setTooltip(tooltip) {
621
- return transport.invoke("tray.setTooltip", { tooltip })
622
- },
623
- setMenu(items) {
624
- return transport.invoke("tray.setMenu", { items })
625
- },
626
- destroy() {
627
- return transport.invoke("tray.destroy")
628
- }
629
- })
630
- }
631
-
632
- function createShortcutApi(transport) {
633
- return Object.freeze({
634
- getInfo() {
635
- return transport.invoke("shortcut.getInfo")
636
- },
637
- register(accelerator) {
638
- return transport.invoke("shortcut.register", { accelerator })
639
- },
640
- unregister(accelerator) {
641
- return transport.invoke("shortcut.unregister", { accelerator })
642
- },
643
- unregisterAll() {
644
- return transport.invoke("shortcut.unregisterAll")
645
- }
646
- })
647
- }
648
-
649
- function createProtocolApi(transport, events) {
650
- return Object.freeze({
651
- async getInfo() {
652
- return normalizeProtocolInfo(await transport.invoke("protocol.getInfo"), "protocol.getInfo")
653
- },
654
- async activate() {
655
- const payload = requireObject(await transport.invoke("protocol.activate"), "protocol.activate")
656
- return Object.freeze({
657
- active: requireBooleanField(payload, "active", "protocol.activate")
658
- })
659
- },
660
- async getCurrentLaunch() {
661
- return normalizeProtocolLaunchEnvelope(
662
- await transport.invoke("protocol.getCurrentLaunch"),
663
- "protocol.getCurrentLaunch"
664
- )
665
- },
666
- async getPendingLaunches() {
667
- return normalizeProtocolLaunchList(
668
- await transport.invoke("protocol.getPendingLaunches"),
669
- "protocol.getPendingLaunches"
670
- )
671
- },
672
- async consumePendingLaunches() {
673
- return normalizeProtocolLaunchList(
674
- await transport.invoke("protocol.consumePendingLaunches"),
675
- "protocol.consumePendingLaunches"
676
- )
677
- },
678
- async listen(handler, options = {}) {
679
- if (typeof handler !== "function") {
680
- throw new TypeError("Reset protocol listener must be a function.")
681
- }
682
-
683
- const consumePending = options.consumePending !== false
684
- const seenLaunches = new Set()
685
- const deliver = (payload) => {
686
- const launch = normalizeProtocolLaunch(payload, "protocol.open")
687
- if (seenLaunches.has(launch.id)) {
688
- return
689
- }
690
-
691
- seenLaunches.add(launch.id)
692
- handler(launch)
693
- }
694
-
695
- const dispose = events.listen("protocol.open", deliver)
696
- await transport.invoke("protocol.activate")
697
-
698
- if (consumePending) {
699
- const pending = await transport.invoke("protocol.consumePendingLaunches")
700
- for (const launch of normalizeProtocolLaunchList(pending, "protocol.consumePendingLaunches")) {
701
- if (seenLaunches.has(launch.id)) {
702
- continue
703
- }
704
-
705
- seenLaunches.add(launch.id)
706
- handler(launch)
707
- }
708
- }
709
-
710
- return dispose
711
- },
712
- register(options = {}) {
713
- return transport.invoke("protocol.register", options)
714
- },
715
- unregister(options = {}) {
716
- return transport.invoke("protocol.unregister", options)
717
- }
718
- })
719
- }
720
-
721
- function createUpdaterApi(transport) {
722
- return Object.freeze({
723
- getInfo() {
724
- return transport.invoke("updater.getInfo")
725
- },
726
- check(options = {}) {
727
- return transport.invoke("updater.check", options)
728
- },
729
- download(options = {}) {
730
- return transport.invoke("updater.download", options)
731
- },
732
- install(options = {}) {
733
- return transport.invoke("updater.install", options)
734
- }
735
- })
736
- }
737
-
738
- function createNetApi(transport) {
739
- return Object.freeze({
740
- request(options) {
741
- return transport.invoke("net.request", options)
742
- }
743
- })
744
- }
745
-
746
- function createBackendApi(transport, events) {
747
- const normalizeEventName = (eventName) => {
748
- if (typeof eventName !== "string" || eventName.trim() === "") {
749
- throw new TypeError("Reset backend event name must be a non-empty string.")
750
- }
751
-
752
- return eventName.startsWith("backend.") ? eventName : `backend.${eventName}`
753
- }
754
-
755
- const invokeBackend = (method, params = {}) =>
756
- transport.invoke("backend.invoke", {
757
- method,
758
- params
759
- })
760
-
761
- const processApi = Object.freeze({
762
- spawn(options) {
763
- return invokeBackend("process.spawn", options)
764
- },
765
- run(options) {
766
- return invokeBackend("process.run", options)
767
- },
768
- write(processId, data) {
769
- return invokeBackend("process.write", { processId, data })
770
- },
771
- kill(processId, options = {}) {
772
- return invokeBackend("process.kill", {
773
- processId,
774
- ...options
775
- })
776
- },
777
- list() {
778
- return invokeBackend("process.list", {})
779
- }
780
- })
781
-
782
- return Object.freeze({
783
- async getInfo() {
784
- return normalizeBackendInfo(await transport.invoke("backend.getInfo"), "backend.getInfo")
785
- },
786
- async isAvailable() {
787
- const info = await transport.invoke("backend.getInfo")
788
- return normalizeBackendInfo(info, "backend.getInfo").available
789
- },
790
- invoke(method, params = {}) {
791
- return invokeBackend(method, params)
792
- },
793
- listen(eventName, handler) {
794
- return events.listen(normalizeEventName(eventName), handler)
795
- },
796
- once(eventName, handler) {
797
- return events.once(normalizeEventName(eventName), handler)
798
- },
799
- process: processApi
800
- })
801
- }
802
-
803
- function scheduleFrontendReadySignal(transport) {
804
- const target = typeof globalThis === "object" ? globalThis.window : undefined
805
-
806
- if (typeof target === "undefined") {
807
- return
808
- }
809
-
810
- if (target.__resetFrontendReadySignalScheduled) {
811
- return
812
- }
813
-
814
- target.__resetFrontendReadySignalScheduled = true
815
-
816
- let attempts = 0
817
- const maxAttempts = 20
818
-
819
- const trySignal = () => {
820
- if (transport.isAvailable()) {
821
- transport.invokeRaw("runtime.signalFrontendReady", {}).catch(() => {})
822
- return
823
- }
824
-
825
- attempts += 1
826
- if (attempts >= maxAttempts) {
827
- return
828
- }
829
-
830
- setTimeout(trySignal, 100)
831
- }
832
-
833
- queueMicrotask(trySignal)
834
- }
835
-
836
- export function createResetClient(source) {
837
- const transport = createResetTransport(source)
838
- const commands = createCommandApi(transport)
839
- const app = createAppApi(transport)
840
- const runtime = createRuntimeApi(transport)
841
- const events = createEventApi(transport)
842
- const windowApi = createWindowApi(transport)
843
- const dialog = createDialogApi(transport)
844
- const fs = createFsApi(transport)
845
- const path = createPathApi(transport)
846
- const shell = createShellApi(transport)
847
- const clipboard = createClipboardApi(transport)
848
- const notification = createNotificationApi(transport)
849
- const screen = createScreenApi(transport)
850
- const storage = createStorageApi(transport)
851
- const webview = createWebViewApi(transport)
852
- const crypto = createCryptoApi(transport)
853
- const process = createProcessApi(transport)
854
- const power = createPowerApi(transport)
855
- const menu = createMenuApi(transport)
856
- const tray = createTrayApi(transport)
857
- const shortcut = createShortcutApi(transport)
858
- const protocol = createProtocolApi(transport, events)
859
- const updater = createUpdaterApi(transport)
860
- const net = createNetApi(transport)
861
- const backend = createBackendApi(transport, events)
862
- const client = Object.freeze({
863
- isAvailable() {
864
- return transport.isAvailable()
865
- },
866
- getRuntime() {
867
- return transport.getRuntime()
868
- },
869
- commands,
870
- app,
871
- runtime,
872
- events,
873
- window: windowApi,
874
- dialog,
875
- fs,
876
- path,
877
- shell,
878
- clipboard,
879
- notification,
880
- screen,
881
- storage,
882
- webview,
883
- crypto,
884
- process,
885
- power,
886
- menu,
887
- tray,
888
- shortcut,
889
- protocol,
890
- updater,
891
- net,
892
- backend
893
- })
894
-
895
- scheduleFrontendReadySignal(transport)
896
-
897
- return client
898
- }
899
-
900
- export {
901
- createResetTransport,
902
- getResetRuntime,
903
- invoke,
904
- invokeRaw,
905
- isResetRuntimeAvailable
906
- } from "./transport.js"
907
-
908
- export const reset = createResetClient()
909
- export const commands = reset.commands
910
- export const app = reset.app
911
- export const runtime = reset.runtime
912
- export const events = reset.events
913
- export const window = reset.window
914
- export const dialog = reset.dialog
915
- export const fs = reset.fs
916
- export const path = reset.path
917
- export const shell = reset.shell
918
- export const clipboard = reset.clipboard
919
- export const notification = reset.notification
920
- export const screen = reset.screen
921
- export const storage = reset.storage
922
- export const webview = reset.webview
923
- export const crypto = reset.crypto
924
- export const process = reset.process
925
- export const power = reset.power
926
- export const menu = reset.menu
927
- export const tray = reset.tray
928
- export const shortcut = reset.shortcut
929
- export const protocol = reset.protocol
930
- export const updater = reset.updater
931
- export const net = reset.net
932
- export const backend = reset.backend
1
+ import {
2
+ createResetTransport,
3
+ getResetRuntime,
4
+ invoke,
5
+ invokeRaw,
6
+ isResetRuntimeAvailable
7
+ } from "./transport.js"
8
+ import { createEventApi } from "./events.js"
9
+ import { ResetProtocolError } from "./errors.js"
10
+
11
+ function isObjectLike(value) {
12
+ return typeof value === "object" && value !== null && !Array.isArray(value)
13
+ }
14
+
15
+ function requireObject(value, command) {
16
+ if (!isObjectLike(value)) {
17
+ throw new ResetProtocolError(
18
+ `Reset runtime returned an invalid payload for '${command}'.`,
19
+ { command }
20
+ )
21
+ }
22
+
23
+ return value
24
+ }
25
+
26
+ function requireStringField(record, field, command) {
27
+ const value = record[field]
28
+
29
+ if (typeof value !== "string" || value.trim() === "") {
30
+ throw new ResetProtocolError(
31
+ `Reset runtime returned an invalid '${field}' field for '${command}'.`,
32
+ { command }
33
+ )
34
+ }
35
+
36
+ return value
37
+ }
38
+
39
+ function requireBooleanField(record, field, command) {
40
+ const value = record[field]
41
+
42
+ if (typeof value !== "boolean") {
43
+ throw new ResetProtocolError(
44
+ `Reset runtime returned an invalid '${field}' field for '${command}'.`,
45
+ { command }
46
+ )
47
+ }
48
+
49
+ return value
50
+ }
51
+
52
+ function requireNumberField(record, field, command) {
53
+ const value = record[field]
54
+
55
+ if (typeof value !== "number" || Number.isNaN(value)) {
56
+ throw new ResetProtocolError(
57
+ `Reset runtime returned an invalid '${field}' field for '${command}'.`,
58
+ { command }
59
+ )
60
+ }
61
+
62
+ return value
63
+ }
64
+
65
+ function optionalStringField(record, field, fallback, command) {
66
+ const value = record[field]
67
+
68
+ if (value === undefined || value === null) {
69
+ return fallback
70
+ }
71
+
72
+ if (typeof value !== "string") {
73
+ throw new ResetProtocolError(
74
+ `Reset runtime returned an invalid '${field}' field for '${command}'.`,
75
+ { command }
76
+ )
77
+ }
78
+
79
+ return value
80
+ }
81
+
82
+ function normalizeStringArray(value, field, command) {
83
+ if (!Array.isArray(value)) {
84
+ throw new ResetProtocolError(
85
+ `Reset runtime returned an invalid '${field}' field for '${command}'.`,
86
+ { command }
87
+ )
88
+ }
89
+
90
+ return Object.freeze(
91
+ value.map((entry) => {
92
+ if (typeof entry !== "string") {
93
+ throw new ResetProtocolError(
94
+ `Reset runtime returned an invalid '${field}' field for '${command}'.`,
95
+ { command }
96
+ )
97
+ }
98
+
99
+ return entry
100
+ })
101
+ )
102
+ }
103
+
104
+ function normalizeAppInfo(payload, command) {
105
+ const record = requireObject(payload, command)
106
+
107
+ return Object.freeze({
108
+ id: requireStringField(record, "id", command),
109
+ name: requireStringField(record, "name", command),
110
+ slug: requireStringField(record, "slug", command),
111
+ version: requireStringField(record, "version", command),
112
+ windowTitle: requireStringField(record, "windowTitle", command)
113
+ })
114
+ }
115
+
116
+ function normalizeRuntimeInfo(payload, command) {
117
+ const record = requireObject(payload, command)
118
+
119
+ return Object.freeze({
120
+ platform: requireStringField(record, "platform", command),
121
+ arch: requireStringField(record, "arch", command),
122
+ frameworkVersion: requireStringField(record, "frameworkVersion", command),
123
+ bridgeVersion: requireStringField(record, "bridgeVersion", command),
124
+ debug: requireBooleanField(record, "debug", command)
125
+ })
126
+ }
127
+
128
+ function normalizeCapabilities(payload, command) {
129
+ const record = requireObject(payload, command)
130
+ const commands = Array.isArray(record.commands) ? record.commands : []
131
+ const permissions = Array.isArray(record.permissions) ? record.permissions : []
132
+
133
+ return Object.freeze({
134
+ commands: Object.freeze(commands.map((entry) => Object.freeze({ ...entry }))),
135
+ permissions: Object.freeze([...permissions])
136
+ })
137
+ }
138
+
139
+ function normalizeProtocolLaunch(payload, command) {
140
+ const record = requireObject(payload, command)
141
+
142
+ return Object.freeze({
143
+ id: requireStringField(record, "id", command),
144
+ url: requireStringField(record, "url", command),
145
+ scheme: requireStringField(record, "scheme", command),
146
+ host: optionalStringField(record, "host", "", command),
147
+ path: optionalStringField(record, "path", "", command),
148
+ query: optionalStringField(record, "query", "", command),
149
+ fragment: optionalStringField(record, "fragment", "", command),
150
+ source: requireStringField(record, "source", command),
151
+ coldStart: requireBooleanField(record, "coldStart", command)
152
+ })
153
+ }
154
+
155
+ function normalizeProtocolLaunchEnvelope(payload, command) {
156
+ const record = requireObject(payload, command)
157
+ const launch = record.launch
158
+
159
+ if (launch === null) {
160
+ return null
161
+ }
162
+
163
+ return normalizeProtocolLaunch(launch, command)
164
+ }
165
+
166
+ function normalizeProtocolLaunchList(payload, command) {
167
+ const record = requireObject(payload, command)
168
+ const launches = record.launches
169
+
170
+ if (!Array.isArray(launches)) {
171
+ throw new ResetProtocolError(
172
+ `Reset runtime returned an invalid 'launches' field for '${command}'.`,
173
+ { command }
174
+ )
175
+ }
176
+
177
+ return Object.freeze(launches.map((entry) => normalizeProtocolLaunch(entry, command)))
178
+ }
179
+
180
+ function normalizeProtocolInfo(payload, command) {
181
+ const record = requireObject(payload, command)
182
+
183
+ return Object.freeze({
184
+ supported: requireBooleanField(record, "supported", command),
185
+ module: requireStringField(record, "module", command),
186
+ platform: requireStringField(record, "platform", command),
187
+ configuredSchemes: normalizeStringArray(record.configuredSchemes ?? [], "configuredSchemes", command),
188
+ bundledSchemes: normalizeStringArray(record.bundledSchemes ?? [], "bundledSchemes", command),
189
+ eventName: requireStringField(record, "eventName", command),
190
+ pendingCount: requireNumberField(record, "pendingCount", command),
191
+ eventStreamActive: requireBooleanField(record, "eventStreamActive", command),
192
+ runtimeRegistration: requireBooleanField(record, "runtimeRegistration", command)
193
+ })
194
+ }
195
+
196
+ function normalizeBackendInfo(payload, command) {
197
+ const record = requireObject(payload, command)
198
+ const runtime = isObjectLike(record.runtime) ? record.runtime : {}
199
+
200
+ return Object.freeze({
201
+ supported: requireBooleanField(record, "supported", command),
202
+ configured: requireBooleanField(record, "configured", command),
203
+ running: requireBooleanField(record, "running", command),
204
+ available: requireBooleanField(record, "available", command),
205
+ protocol: requireStringField(record, "protocol", command),
206
+ pid: typeof record.pid === "number" ? record.pid : null,
207
+ entryPath: optionalStringField(record, "entryPath", "", command),
208
+ runnerPath: optionalStringField(record, "runnerPath", "", command),
209
+ executablePath: optionalStringField(record, "executablePath", "", command),
210
+ workingDirectory: optionalStringField(record, "workingDirectory", "", command),
211
+ methods: normalizeStringArray(record.methods ?? [], "methods", command),
212
+ runtime: Object.freeze({
213
+ name: optionalStringField(runtime, "name", "", command),
214
+ version: optionalStringField(runtime, "version", "", command),
215
+ platform: optionalStringField(runtime, "platform", "", command),
216
+ arch: optionalStringField(runtime, "arch", "", command)
217
+ }),
218
+ lastError: optionalStringField(record, "lastError", "", command)
219
+ })
220
+ }
221
+
222
+ function createCommandApi(transport) {
223
+ return Object.freeze({
224
+ invoke(command, payload = {}) {
225
+ return transport.invoke(command, payload)
226
+ },
227
+ invokeRaw(command, payload = {}) {
228
+ return transport.invokeRaw(command, payload)
229
+ }
230
+ })
231
+ }
232
+
233
+ function createAppApi(transport) {
234
+ return Object.freeze({
235
+ async getId() {
236
+ const payload = requireObject(await transport.invoke("app.getId"), "app.getId")
237
+ return requireStringField(payload, "id", "app.getId")
238
+ },
239
+ async getName() {
240
+ const payload = requireObject(await transport.invoke("app.getName"), "app.getName")
241
+ return requireStringField(payload, "name", "app.getName")
242
+ },
243
+ async getSlug() {
244
+ const payload = requireObject(await transport.invoke("app.getSlug"), "app.getSlug")
245
+ return requireStringField(payload, "slug", "app.getSlug")
246
+ },
247
+ async getVersion() {
248
+ const payload = requireObject(await transport.invoke("app.getVersion"), "app.getVersion")
249
+ return requireStringField(payload, "version", "app.getVersion")
250
+ },
251
+ async getInfo() {
252
+ return normalizeAppInfo(await transport.invoke("app.getInfo"), "app.getInfo")
253
+ },
254
+ show() {
255
+ return transport.invoke("app.show")
256
+ },
257
+ hide() {
258
+ return transport.invoke("app.hide")
259
+ },
260
+ quit() {
261
+ return transport.invoke("app.quit")
262
+ },
263
+ relaunch() {
264
+ return transport.invoke("app.relaunch")
265
+ }
266
+ })
267
+ }
268
+
269
+ function createRuntimeApi(transport) {
270
+ return Object.freeze({
271
+ async getPlatform() {
272
+ const payload = requireObject(await transport.invoke("runtime.getPlatform"), "runtime.getPlatform")
273
+ return requireStringField(payload, "platform", "runtime.getPlatform")
274
+ },
275
+ async getArchitecture() {
276
+ const payload = requireObject(await transport.invoke("runtime.getArchitecture"), "runtime.getArchitecture")
277
+ return requireStringField(payload, "arch", "runtime.getArchitecture")
278
+ },
279
+ async getFrameworkVersion() {
280
+ const payload = requireObject(
281
+ await transport.invoke("runtime.getFrameworkVersion"),
282
+ "runtime.getFrameworkVersion"
283
+ )
284
+ return requireStringField(payload, "version", "runtime.getFrameworkVersion")
285
+ },
286
+ async getBridgeVersion() {
287
+ const payload = requireObject(
288
+ await transport.invoke("runtime.getBridgeVersion"),
289
+ "runtime.getBridgeVersion"
290
+ )
291
+ return requireStringField(payload, "version", "runtime.getBridgeVersion")
292
+ },
293
+ async getInfo() {
294
+ return normalizeRuntimeInfo(await transport.invoke("runtime.getInfo"), "runtime.getInfo")
295
+ },
296
+ async getCapabilities() {
297
+ return normalizeCapabilities(
298
+ await transport.invoke("runtime.getCapabilities"),
299
+ "runtime.getCapabilities"
300
+ )
301
+ }
302
+ })
303
+ }
304
+
305
+ function createWindowApi(transport) {
306
+ return Object.freeze({
307
+ getCurrent() {
308
+ return transport.invoke("window.getCurrent")
309
+ },
310
+ getInfo() {
311
+ return transport.invoke("window.getInfo")
312
+ },
313
+ list() {
314
+ return transport.invoke("window.list")
315
+ },
316
+ show() {
317
+ return transport.invoke("window.show")
318
+ },
319
+ hide() {
320
+ return transport.invoke("window.hide")
321
+ },
322
+ focus() {
323
+ return transport.invoke("window.focus")
324
+ },
325
+ close() {
326
+ return transport.invoke("window.close")
327
+ },
328
+ minimize() {
329
+ return transport.invoke("window.minimize")
330
+ },
331
+ maximize() {
332
+ return transport.invoke("window.maximize")
333
+ },
334
+ center() {
335
+ return transport.invoke("window.center")
336
+ },
337
+ setTitle(title) {
338
+ return transport.invoke("window.setTitle", { title })
339
+ },
340
+ setSize(width, height) {
341
+ return transport.invoke("window.setSize", { width, height })
342
+ },
343
+ setResizable(resizable) {
344
+ return transport.invoke("window.setResizable", { resizable })
345
+ }
346
+ })
347
+ }
348
+
349
+ function createDialogApi(transport) {
350
+ return Object.freeze({
351
+ openFile(options = {}) {
352
+ return transport.invoke("dialog.openFile", options)
353
+ },
354
+ saveFile(options = {}) {
355
+ return transport.invoke("dialog.saveFile", options)
356
+ },
357
+ message(title, message) {
358
+ return transport.invoke("dialog.message", { title, message })
359
+ },
360
+ confirm(title, message) {
361
+ return transport.invoke("dialog.confirm", { title, message })
362
+ }
363
+ })
364
+ }
365
+
366
+ function createFsApi(transport) {
367
+ return Object.freeze({
368
+ async readTextFile(path) {
369
+ const payload = requireObject(await transport.invoke("fs.readTextFile", { path }), "fs.readTextFile")
370
+ return requireStringField(payload, "text", "fs.readTextFile")
371
+ },
372
+ writeTextFile(path, text, options = {}) {
373
+ return transport.invoke("fs.writeTextFile", {
374
+ path,
375
+ text,
376
+ ...options
377
+ })
378
+ },
379
+ async exists(path) {
380
+ const payload = requireObject(await transport.invoke("fs.exists", { path }), "fs.exists")
381
+ return requireBooleanField(payload, "exists", "fs.exists")
382
+ },
383
+ mkdir(path, options = {}) {
384
+ return transport.invoke("fs.mkdir", {
385
+ path,
386
+ ...options
387
+ })
388
+ },
389
+ remove(path, options = {}) {
390
+ return transport.invoke("fs.remove", {
391
+ path,
392
+ ...options
393
+ })
394
+ },
395
+ rename(from, to) {
396
+ return transport.invoke("fs.rename", { from, to })
397
+ },
398
+ async readDir(path) {
399
+ const payload = requireObject(await transport.invoke("fs.readDir", { path }), "fs.readDir")
400
+ return Array.isArray(payload.entries) ? payload.entries : []
401
+ }
402
+ })
403
+ }
404
+
405
+ function createPathApi(transport) {
406
+ return Object.freeze({
407
+ async join(...segments) {
408
+ const payload = requireObject(await transport.invoke("path.join", { segments }), "path.join")
409
+ return requireStringField(payload, "path", "path.join")
410
+ },
411
+ async basename(path) {
412
+ const payload = requireObject(await transport.invoke("path.basename", { path }), "path.basename")
413
+ return requireStringField(payload, "name", "path.basename")
414
+ },
415
+ async dirname(path) {
416
+ const payload = requireObject(await transport.invoke("path.dirname", { path }), "path.dirname")
417
+ return requireStringField(payload, "path", "path.dirname")
418
+ },
419
+ async resolve(path, base) {
420
+ const payload = requireObject(await transport.invoke("path.resolve", { path, base }), "path.resolve")
421
+ return requireStringField(payload, "path", "path.resolve")
422
+ },
423
+ async getHomeDir() {
424
+ const payload = requireObject(await transport.invoke("path.getHomeDir"), "path.getHomeDir")
425
+ return requireStringField(payload, "path", "path.getHomeDir")
426
+ },
427
+ async getTempDir() {
428
+ const payload = requireObject(await transport.invoke("path.getTempDir"), "path.getTempDir")
429
+ return requireStringField(payload, "path", "path.getTempDir")
430
+ },
431
+ async getAppDataDir() {
432
+ const payload = requireObject(await transport.invoke("path.getAppDataDir"), "path.getAppDataDir")
433
+ return requireStringField(payload, "path", "path.getAppDataDir")
434
+ },
435
+ async getAppConfigDir() {
436
+ const payload = requireObject(await transport.invoke("path.getAppConfigDir"), "path.getAppConfigDir")
437
+ return requireStringField(payload, "path", "path.getAppConfigDir")
438
+ },
439
+ async getCacheDir() {
440
+ const payload = requireObject(await transport.invoke("path.getCacheDir"), "path.getCacheDir")
441
+ return requireStringField(payload, "path", "path.getCacheDir")
442
+ },
443
+ getInfo() {
444
+ return transport.invoke("path.getInfo")
445
+ }
446
+ })
447
+ }
448
+
449
+ function createShellApi(transport) {
450
+ return Object.freeze({
451
+ openExternal(url) {
452
+ return transport.invoke("shell.openExternal", { url })
453
+ },
454
+ openPath(path) {
455
+ return transport.invoke("shell.openPath", { path })
456
+ },
457
+ showItemInFolder(path) {
458
+ return transport.invoke("shell.showItemInFolder", { path })
459
+ }
460
+ })
461
+ }
462
+
463
+ function createClipboardApi(transport) {
464
+ return Object.freeze({
465
+ async readText() {
466
+ const payload = requireObject(await transport.invoke("clipboard.readText"), "clipboard.readText")
467
+ return requireStringField(payload, "text", "clipboard.readText")
468
+ },
469
+ writeText(text) {
470
+ return transport.invoke("clipboard.writeText", { text })
471
+ }
472
+ })
473
+ }
474
+
475
+ function createNotificationApi(transport) {
476
+ return Object.freeze({
477
+ async isSupported() {
478
+ const payload = requireObject(
479
+ await transport.invoke("notification.isSupported"),
480
+ "notification.isSupported"
481
+ )
482
+ return requireBooleanField(payload, "supported", "notification.isSupported")
483
+ },
484
+ requestPermission() {
485
+ return transport.invoke("notification.requestPermission")
486
+ },
487
+ show(options) {
488
+ return transport.invoke("notification.show", options)
489
+ }
490
+ })
491
+ }
492
+
493
+ function createScreenApi(transport) {
494
+ return Object.freeze({
495
+ getCursorPosition() {
496
+ return transport.invoke("screen.getCursorPosition")
497
+ },
498
+ getPrimaryDisplay() {
499
+ return transport.invoke("screen.getPrimaryDisplay")
500
+ },
501
+ async getDisplays() {
502
+ const payload = requireObject(await transport.invoke("screen.getDisplays"), "screen.getDisplays")
503
+ return Array.isArray(payload.displays) ? payload.displays : []
504
+ }
505
+ })
506
+ }
507
+
508
+ function createStorageApi(transport) {
509
+ return Object.freeze({
510
+ async get(key) {
511
+ const payload = requireObject(await transport.invoke("storage.get", { key }), "storage.get")
512
+ return payload.value
513
+ },
514
+ set(key, value) {
515
+ return transport.invoke("storage.set", { key, value })
516
+ },
517
+ remove(key) {
518
+ return transport.invoke("storage.remove", { key })
519
+ },
520
+ clear() {
521
+ return transport.invoke("storage.clear")
522
+ },
523
+ async getAll() {
524
+ const payload = requireObject(await transport.invoke("storage.getAll"), "storage.getAll")
525
+ return requireObject(payload.entries ?? {}, "storage.getAll")
526
+ }
527
+ })
528
+ }
529
+
530
+ function createWebViewApi(transport) {
531
+ return Object.freeze({
532
+ getInfo() {
533
+ return transport.invoke("webview.getInfo")
534
+ },
535
+ reload() {
536
+ return transport.invoke("webview.reload")
537
+ },
538
+ goBack() {
539
+ return transport.invoke("webview.goBack")
540
+ },
541
+ goForward() {
542
+ return transport.invoke("webview.goForward")
543
+ },
544
+ navigate(options) {
545
+ return transport.invoke("webview.navigate", options)
546
+ },
547
+ setZoomFactor(factor) {
548
+ return transport.invoke("webview.setZoomFactor", { factor })
549
+ }
550
+ })
551
+ }
552
+
553
+ function createCryptoApi(transport) {
554
+ return Object.freeze({
555
+ getInfo() {
556
+ return transport.invoke("crypto.getInfo")
557
+ },
558
+ randomBytes(size, options = {}) {
559
+ return transport.invoke("crypto.randomBytes", {
560
+ size,
561
+ encoding: options.encoding
562
+ })
563
+ },
564
+ async randomUuid() {
565
+ const payload = requireObject(await transport.invoke("crypto.randomUuid"), "crypto.randomUuid")
566
+ return requireStringField(payload, "uuid", "crypto.randomUuid")
567
+ }
568
+ })
569
+ }
570
+
571
+ function createProcessApi(transport) {
572
+ return Object.freeze({
573
+ async getPid() {
574
+ const payload = requireObject(await transport.invoke("process.getPid"), "process.getPid")
575
+ return requireNumberField(payload, "pid", "process.getPid")
576
+ },
577
+ async getCwd() {
578
+ const payload = requireObject(await transport.invoke("process.getCwd"), "process.getCwd")
579
+ return requireStringField(payload, "path", "process.getCwd")
580
+ },
581
+ getInfo() {
582
+ return transport.invoke("process.getInfo")
583
+ }
584
+ })
585
+ }
586
+
587
+ function createPowerApi(transport) {
588
+ return Object.freeze({
589
+ getInfo() {
590
+ return transport.invoke("power.getInfo")
591
+ }
592
+ })
593
+ }
594
+
595
+ function createMenuApi(transport) {
596
+ return Object.freeze({
597
+ getInfo() {
598
+ return transport.invoke("menu.getInfo")
599
+ },
600
+ setApplicationMenu(items) {
601
+ return transport.invoke("menu.setApplicationMenu", { items })
602
+ },
603
+ clearApplicationMenu() {
604
+ return transport.invoke("menu.clearApplicationMenu")
605
+ }
606
+ })
607
+ }
608
+
609
+ function createTrayApi(transport) {
610
+ return Object.freeze({
611
+ getInfo() {
612
+ return transport.invoke("tray.getInfo")
613
+ },
614
+ create(options = {}) {
615
+ return transport.invoke("tray.create", options)
616
+ },
617
+ setTitle(title) {
618
+ return transport.invoke("tray.setTitle", { title })
619
+ },
620
+ setTooltip(tooltip) {
621
+ return transport.invoke("tray.setTooltip", { tooltip })
622
+ },
623
+ setMenu(items) {
624
+ return transport.invoke("tray.setMenu", { items })
625
+ },
626
+ destroy() {
627
+ return transport.invoke("tray.destroy")
628
+ }
629
+ })
630
+ }
631
+
632
+ function createShortcutApi(transport) {
633
+ return Object.freeze({
634
+ getInfo() {
635
+ return transport.invoke("shortcut.getInfo")
636
+ },
637
+ register(accelerator) {
638
+ return transport.invoke("shortcut.register", { accelerator })
639
+ },
640
+ unregister(accelerator) {
641
+ return transport.invoke("shortcut.unregister", { accelerator })
642
+ },
643
+ unregisterAll() {
644
+ return transport.invoke("shortcut.unregisterAll")
645
+ }
646
+ })
647
+ }
648
+
649
+ function createProtocolApi(transport, events) {
650
+ return Object.freeze({
651
+ async getInfo() {
652
+ return normalizeProtocolInfo(await transport.invoke("protocol.getInfo"), "protocol.getInfo")
653
+ },
654
+ async activate() {
655
+ const payload = requireObject(await transport.invoke("protocol.activate"), "protocol.activate")
656
+ return Object.freeze({
657
+ active: requireBooleanField(payload, "active", "protocol.activate")
658
+ })
659
+ },
660
+ async getCurrentLaunch() {
661
+ return normalizeProtocolLaunchEnvelope(
662
+ await transport.invoke("protocol.getCurrentLaunch"),
663
+ "protocol.getCurrentLaunch"
664
+ )
665
+ },
666
+ async getPendingLaunches() {
667
+ return normalizeProtocolLaunchList(
668
+ await transport.invoke("protocol.getPendingLaunches"),
669
+ "protocol.getPendingLaunches"
670
+ )
671
+ },
672
+ async consumePendingLaunches() {
673
+ return normalizeProtocolLaunchList(
674
+ await transport.invoke("protocol.consumePendingLaunches"),
675
+ "protocol.consumePendingLaunches"
676
+ )
677
+ },
678
+ async listen(handler, options = {}) {
679
+ if (typeof handler !== "function") {
680
+ throw new TypeError("Reset protocol listener must be a function.")
681
+ }
682
+
683
+ const consumePending = options.consumePending !== false
684
+ const seenLaunches = new Set()
685
+ const deliver = (payload) => {
686
+ const launch = normalizeProtocolLaunch(payload, "protocol.open")
687
+ if (seenLaunches.has(launch.id)) {
688
+ return
689
+ }
690
+
691
+ seenLaunches.add(launch.id)
692
+ handler(launch)
693
+ }
694
+
695
+ const dispose = events.listen("protocol.open", deliver)
696
+ await transport.invoke("protocol.activate")
697
+
698
+ if (consumePending) {
699
+ const pending = await transport.invoke("protocol.consumePendingLaunches")
700
+ for (const launch of normalizeProtocolLaunchList(pending, "protocol.consumePendingLaunches")) {
701
+ if (seenLaunches.has(launch.id)) {
702
+ continue
703
+ }
704
+
705
+ seenLaunches.add(launch.id)
706
+ handler(launch)
707
+ }
708
+ }
709
+
710
+ return dispose
711
+ },
712
+ register(options = {}) {
713
+ return transport.invoke("protocol.register", options)
714
+ },
715
+ unregister(options = {}) {
716
+ return transport.invoke("protocol.unregister", options)
717
+ }
718
+ })
719
+ }
720
+
721
+ function createUpdaterApi(transport) {
722
+ return Object.freeze({
723
+ getInfo() {
724
+ return transport.invoke("updater.getInfo")
725
+ },
726
+ check(options = {}) {
727
+ return transport.invoke("updater.check", options)
728
+ },
729
+ download(options = {}) {
730
+ return transport.invoke("updater.download", options)
731
+ },
732
+ install(options = {}) {
733
+ return transport.invoke("updater.install", options)
734
+ }
735
+ })
736
+ }
737
+
738
+ function createNetApi(transport) {
739
+ return Object.freeze({
740
+ request(options) {
741
+ return transport.invoke("net.request", options)
742
+ }
743
+ })
744
+ }
745
+
746
+ function createBackendApi(transport, events) {
747
+ const normalizeEventName = (eventName) => {
748
+ if (typeof eventName !== "string" || eventName.trim() === "") {
749
+ throw new TypeError("Reset backend event name must be a non-empty string.")
750
+ }
751
+
752
+ return eventName.startsWith("backend.") ? eventName : `backend.${eventName}`
753
+ }
754
+
755
+ const invokeBackend = (method, params = {}) =>
756
+ transport.invoke("backend.invoke", {
757
+ method,
758
+ params
759
+ })
760
+
761
+ const processApi = Object.freeze({
762
+ spawn(options) {
763
+ return invokeBackend("process.spawn", options)
764
+ },
765
+ run(options) {
766
+ return invokeBackend("process.run", options)
767
+ },
768
+ write(processId, data) {
769
+ return invokeBackend("process.write", { processId, data })
770
+ },
771
+ kill(processId, options = {}) {
772
+ return invokeBackend("process.kill", {
773
+ processId,
774
+ ...options
775
+ })
776
+ },
777
+ list() {
778
+ return invokeBackend("process.list", {})
779
+ }
780
+ })
781
+
782
+ return Object.freeze({
783
+ async getInfo() {
784
+ return normalizeBackendInfo(await transport.invoke("backend.getInfo"), "backend.getInfo")
785
+ },
786
+ async isAvailable() {
787
+ const info = await transport.invoke("backend.getInfo")
788
+ return normalizeBackendInfo(info, "backend.getInfo").available
789
+ },
790
+ invoke(method, params = {}) {
791
+ return invokeBackend(method, params)
792
+ },
793
+ listen(eventName, handler) {
794
+ return events.listen(normalizeEventName(eventName), handler)
795
+ },
796
+ once(eventName, handler) {
797
+ return events.once(normalizeEventName(eventName), handler)
798
+ },
799
+ process: processApi
800
+ })
801
+ }
802
+
803
+ function scheduleFrontendReadySignal(transport) {
804
+ const target = typeof globalThis === "object" ? globalThis.window : undefined
805
+
806
+ if (typeof target === "undefined") {
807
+ return
808
+ }
809
+
810
+ if (target.__resetFrontendReadySignalScheduled) {
811
+ return
812
+ }
813
+
814
+ target.__resetFrontendReadySignalScheduled = true
815
+
816
+ let attempts = 0
817
+ const maxAttempts = 20
818
+
819
+ const trySignal = () => {
820
+ if (transport.isAvailable()) {
821
+ transport.invokeRaw("runtime.signalFrontendReady", {}).catch(() => {})
822
+ return
823
+ }
824
+
825
+ attempts += 1
826
+ if (attempts >= maxAttempts) {
827
+ return
828
+ }
829
+
830
+ setTimeout(trySignal, 100)
831
+ }
832
+
833
+ queueMicrotask(trySignal)
834
+ }
835
+
836
+ export function createResetClient(source) {
837
+ const transport = createResetTransport(source)
838
+ const commands = createCommandApi(transport)
839
+ const app = createAppApi(transport)
840
+ const runtime = createRuntimeApi(transport)
841
+ const events = createEventApi(transport)
842
+ const windowApi = createWindowApi(transport)
843
+ const dialog = createDialogApi(transport)
844
+ const fs = createFsApi(transport)
845
+ const path = createPathApi(transport)
846
+ const shell = createShellApi(transport)
847
+ const clipboard = createClipboardApi(transport)
848
+ const notification = createNotificationApi(transport)
849
+ const screen = createScreenApi(transport)
850
+ const storage = createStorageApi(transport)
851
+ const webview = createWebViewApi(transport)
852
+ const crypto = createCryptoApi(transport)
853
+ const process = createProcessApi(transport)
854
+ const power = createPowerApi(transport)
855
+ const menu = createMenuApi(transport)
856
+ const tray = createTrayApi(transport)
857
+ const shortcut = createShortcutApi(transport)
858
+ const protocol = createProtocolApi(transport, events)
859
+ const updater = createUpdaterApi(transport)
860
+ const net = createNetApi(transport)
861
+ const backend = createBackendApi(transport, events)
862
+ const client = Object.freeze({
863
+ isAvailable() {
864
+ return transport.isAvailable()
865
+ },
866
+ getRuntime() {
867
+ return transport.getRuntime()
868
+ },
869
+ commands,
870
+ app,
871
+ runtime,
872
+ events,
873
+ window: windowApi,
874
+ dialog,
875
+ fs,
876
+ path,
877
+ shell,
878
+ clipboard,
879
+ notification,
880
+ screen,
881
+ storage,
882
+ webview,
883
+ crypto,
884
+ process,
885
+ power,
886
+ menu,
887
+ tray,
888
+ shortcut,
889
+ protocol,
890
+ updater,
891
+ net,
892
+ backend
893
+ })
894
+
895
+ scheduleFrontendReadySignal(transport)
896
+
897
+ return client
898
+ }
899
+
900
+ export {
901
+ createResetTransport,
902
+ getResetRuntime,
903
+ invoke,
904
+ invokeRaw,
905
+ isResetRuntimeAvailable
906
+ } from "./transport.js"
907
+
908
+ export const reset = createResetClient()
909
+ export const commands = reset.commands
910
+ export const app = reset.app
911
+ export const runtime = reset.runtime
912
+ export const events = reset.events
913
+ export const window = reset.window
914
+ export const dialog = reset.dialog
915
+ export const fs = reset.fs
916
+ export const path = reset.path
917
+ export const shell = reset.shell
918
+ export const clipboard = reset.clipboard
919
+ export const notification = reset.notification
920
+ export const screen = reset.screen
921
+ export const storage = reset.storage
922
+ export const webview = reset.webview
923
+ export const crypto = reset.crypto
924
+ export const process = reset.process
925
+ export const power = reset.power
926
+ export const menu = reset.menu
927
+ export const tray = reset.tray
928
+ export const shortcut = reset.shortcut
929
+ export const protocol = reset.protocol
930
+ export const updater = reset.updater
931
+ export const net = reset.net
932
+ export const backend = reset.backend