@ac-kit/browser 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/CHANGELOG.md +44 -0
- package/LICENSE +28 -0
- package/README.md +187 -2
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/indexeddb/_indexed-db-request.d.ts +16 -0
- package/dist/indexeddb/_indexed-db-request.d.ts.map +1 -0
- package/dist/indexeddb/_indexed-db-request.js +45 -0
- package/dist/indexeddb/_indexed-db-request.js.map +1 -0
- package/dist/indexeddb/indexed-db-error.d.ts +9 -0
- package/dist/indexeddb/indexed-db-error.d.ts.map +1 -0
- package/dist/indexeddb/indexed-db-error.js +12 -0
- package/dist/indexeddb/indexed-db-error.js.map +1 -0
- package/dist/indexeddb/indexed-db-records.d.ts +95 -0
- package/dist/indexeddb/indexed-db-records.d.ts.map +1 -0
- package/dist/indexeddb/indexed-db-records.js +121 -0
- package/dist/indexeddb/indexed-db-records.js.map +1 -0
- package/dist/indexeddb/open-indexed-db.d.ts +68 -0
- package/dist/indexeddb/open-indexed-db.d.ts.map +1 -0
- package/dist/indexeddb/open-indexed-db.js +102 -0
- package/dist/indexeddb/open-indexed-db.js.map +1 -0
- package/dist/web-midi/web-midi-access.d.ts +54 -0
- package/dist/web-midi/web-midi-access.d.ts.map +1 -0
- package/dist/web-midi/web-midi-access.js +78 -0
- package/dist/web-midi/web-midi-access.js.map +1 -0
- package/dist/web-midi/web-midi-inputs.d.ts +81 -0
- package/dist/web-midi/web-midi-inputs.d.ts.map +1 -0
- package/dist/web-midi/web-midi-inputs.js +107 -0
- package/dist/web-midi/web-midi-inputs.js.map +1 -0
- package/dist/xhr/send-with-progress.d.ts +96 -0
- package/dist/xhr/send-with-progress.d.ts.map +1 -0
- package/dist/xhr/send-with-progress.js +132 -0
- package/dist/xhr/send-with-progress.js.map +1 -0
- package/package.json +64 -3
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Callable, CallableNoArgs } from "@ac-kit/core";
|
|
2
|
+
/** What an upgrade from one version of a database to the next is given. */
|
|
3
|
+
export type IndexedDbUpgrade = {
|
|
4
|
+
/** The database, its object stores to create, change or delete. */
|
|
5
|
+
readonly database: IDBDatabase;
|
|
6
|
+
/** The upgrade's own transaction, to reach the stores that exist already. */
|
|
7
|
+
readonly transaction: IDBTransaction;
|
|
8
|
+
/** The version the database had: `0` when it is created. */
|
|
9
|
+
readonly oldVersion: number;
|
|
10
|
+
readonly newVersion: number;
|
|
11
|
+
};
|
|
12
|
+
/** What {@link openIndexedDb} opens, and how. */
|
|
13
|
+
export type OpenIndexedDbInput = {
|
|
14
|
+
/** The page's or worker's `indexedDB`, or a stand-in in tests. */
|
|
15
|
+
readonly factory: IDBFactory | undefined;
|
|
16
|
+
readonly name: string;
|
|
17
|
+
/** The schema's version, a positive integer raised with each change. */
|
|
18
|
+
readonly version: number;
|
|
19
|
+
/**
|
|
20
|
+
* Brings the database from `oldVersion` to `newVersion`, synchronously, when
|
|
21
|
+
* it is older or new: create the stores and indexes each version added. What
|
|
22
|
+
* it throws aborts the upgrade, and the opening fails.
|
|
23
|
+
*/
|
|
24
|
+
readonly upgrade: Callable<[upgrade: IndexedDbUpgrade]>;
|
|
25
|
+
/**
|
|
26
|
+
* Told that another page holds the database open at an older version: the
|
|
27
|
+
* opening waits until it closes, which the person may need asking for.
|
|
28
|
+
*/
|
|
29
|
+
readonly onBlocked?: CallableNoArgs;
|
|
30
|
+
/**
|
|
31
|
+
* Told that another page opens the database at a newer version. The database
|
|
32
|
+
* is closed first, so that page's upgrade can run; this page has to reload,
|
|
33
|
+
* or open it again at the newer version, to keep using it.
|
|
34
|
+
*/
|
|
35
|
+
readonly onVersionChange?: CallableNoArgs;
|
|
36
|
+
/** Gives up opening: the opening rejects with its reason. */
|
|
37
|
+
readonly signal?: AbortSignal;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Opens the IndexedDB database `name` at `version`, creating or upgrading it
|
|
41
|
+
* through `upgrade` first. Reach for it to keep structured data across page
|
|
42
|
+
* loads; {@link createIndexedDbRecords} reads and writes one of its object
|
|
43
|
+
* stores, and {@link openIndexedDbRecords} does both for a database of one
|
|
44
|
+
* store.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* const database = await openIndexedDb({
|
|
48
|
+
* factory: indexedDB,
|
|
49
|
+
* name: "practice",
|
|
50
|
+
* version: 2,
|
|
51
|
+
* upgrade: ({ database, oldVersion }) => {
|
|
52
|
+
* if (oldVersion < 1) {
|
|
53
|
+
* database.createObjectStore("songs");
|
|
54
|
+
* }
|
|
55
|
+
* if (oldVersion < 2) {
|
|
56
|
+
* database.createObjectStore("sessions");
|
|
57
|
+
* }
|
|
58
|
+
* },
|
|
59
|
+
* });
|
|
60
|
+
*
|
|
61
|
+
* @throws {IndexedDbError} When `factory` is absent (some private modes), the
|
|
62
|
+
* database cannot be opened — the version is lower than the stored one, or
|
|
63
|
+
* storage is refused — or `upgrade` threw, which is its `cause`.
|
|
64
|
+
* @throws {RangeError} When `version` is not a positive integer.
|
|
65
|
+
* @throws `signal`'s reason when it aborts first.
|
|
66
|
+
*/
|
|
67
|
+
export declare function openIndexedDb(input: OpenIndexedDbInput): Promise<IDBDatabase>;
|
|
68
|
+
//# sourceMappingURL=open-indexed-db.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"open-indexed-db.d.ts","sourceRoot":"","sources":["../../src/indexeddb/open-indexed-db.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAI7D,2EAA2E;AAC3E,MAAM,MAAM,gBAAgB,GAAG;IAC9B,mEAAmE;IACnE,QAAQ,CAAC,QAAQ,EAAE,WAAW,CAAC;IAC/B,6EAA6E;IAC7E,QAAQ,CAAC,WAAW,EAAE,cAAc,CAAC;IACrC,4DAA4D;IAC5D,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF,iDAAiD;AACjD,MAAM,MAAM,kBAAkB,GAAG;IAChC,kEAAkE;IAClE,QAAQ,CAAC,OAAO,EAAE,UAAU,GAAG,SAAS,CAAC;IACzC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,wEAAwE;IACxE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB;;;;OAIG;IACH,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,OAAO,EAAE,gBAAgB,CAAC,CAAC,CAAC;IACxD;;;OAGG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,cAAc,CAAC;IACpC;;;;OAIG;IACH,QAAQ,CAAC,eAAe,CAAC,EAAE,cAAc,CAAC;IAC1C,6DAA6D;IAC7D,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;CAC9B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,kBAAkB,GAAG,OAAO,CAAC,WAAW,CAAC,CA4E7E"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { IndexedDbError } from "./indexed-db-error.js";
|
|
2
|
+
/**
|
|
3
|
+
* Opens the IndexedDB database `name` at `version`, creating or upgrading it
|
|
4
|
+
* through `upgrade` first. Reach for it to keep structured data across page
|
|
5
|
+
* loads; {@link createIndexedDbRecords} reads and writes one of its object
|
|
6
|
+
* stores, and {@link openIndexedDbRecords} does both for a database of one
|
|
7
|
+
* store.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const database = await openIndexedDb({
|
|
11
|
+
* factory: indexedDB,
|
|
12
|
+
* name: "practice",
|
|
13
|
+
* version: 2,
|
|
14
|
+
* upgrade: ({ database, oldVersion }) => {
|
|
15
|
+
* if (oldVersion < 1) {
|
|
16
|
+
* database.createObjectStore("songs");
|
|
17
|
+
* }
|
|
18
|
+
* if (oldVersion < 2) {
|
|
19
|
+
* database.createObjectStore("sessions");
|
|
20
|
+
* }
|
|
21
|
+
* },
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* @throws {IndexedDbError} When `factory` is absent (some private modes), the
|
|
25
|
+
* database cannot be opened — the version is lower than the stored one, or
|
|
26
|
+
* storage is refused — or `upgrade` threw, which is its `cause`.
|
|
27
|
+
* @throws {RangeError} When `version` is not a positive integer.
|
|
28
|
+
* @throws `signal`'s reason when it aborts first.
|
|
29
|
+
*/
|
|
30
|
+
export function openIndexedDb(input) {
|
|
31
|
+
const { factory, name, version, upgrade, signal } = input;
|
|
32
|
+
if (!Number.isSafeInteger(version) || version < 1) {
|
|
33
|
+
return Promise.reject(new RangeError(`An IndexedDB version is a positive integer: ${version}`));
|
|
34
|
+
}
|
|
35
|
+
if (factory === undefined) {
|
|
36
|
+
return Promise.reject(new IndexedDbError("This browser has no IndexedDB"));
|
|
37
|
+
}
|
|
38
|
+
if (signal?.aborted === true) {
|
|
39
|
+
return Promise.reject(signal.reason);
|
|
40
|
+
}
|
|
41
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
42
|
+
let isAbandoned = false;
|
|
43
|
+
let upgradeError;
|
|
44
|
+
const handleAbort = () => {
|
|
45
|
+
isAbandoned = true;
|
|
46
|
+
reject(signal?.reason);
|
|
47
|
+
};
|
|
48
|
+
signal?.addEventListener("abort", handleAbort, { once: true });
|
|
49
|
+
let opening;
|
|
50
|
+
try {
|
|
51
|
+
opening = factory.open(name, version);
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
signal?.removeEventListener("abort", handleAbort);
|
|
55
|
+
return Promise.reject(new IndexedDbError(`Cannot open the IndexedDB database "${name}"`, {
|
|
56
|
+
cause: error,
|
|
57
|
+
}));
|
|
58
|
+
}
|
|
59
|
+
opening.addEventListener("upgradeneeded", (event) => {
|
|
60
|
+
const transaction = opening.transaction;
|
|
61
|
+
if (isAbandoned || transaction === null) {
|
|
62
|
+
transaction?.abort();
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
upgrade({
|
|
67
|
+
database: opening.result,
|
|
68
|
+
transaction,
|
|
69
|
+
oldVersion: event.oldVersion,
|
|
70
|
+
newVersion: event.newVersion ?? version,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
upgradeError = error;
|
|
75
|
+
transaction.abort();
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
opening.addEventListener("blocked", () => {
|
|
79
|
+
input.onBlocked?.();
|
|
80
|
+
});
|
|
81
|
+
opening.addEventListener("success", () => {
|
|
82
|
+
signal?.removeEventListener("abort", handleAbort);
|
|
83
|
+
const database = opening.result;
|
|
84
|
+
if (isAbandoned) {
|
|
85
|
+
database.close();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
database.addEventListener("versionchange", () => {
|
|
89
|
+
database.close();
|
|
90
|
+
input.onVersionChange?.();
|
|
91
|
+
});
|
|
92
|
+
resolve(database);
|
|
93
|
+
});
|
|
94
|
+
opening.addEventListener("error", () => {
|
|
95
|
+
signal?.removeEventListener("abort", handleAbort);
|
|
96
|
+
reject(new IndexedDbError(`Cannot open the IndexedDB database "${name}"`, {
|
|
97
|
+
cause: upgradeError ?? opening.error,
|
|
98
|
+
}));
|
|
99
|
+
});
|
|
100
|
+
return promise;
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=open-indexed-db.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"open-indexed-db.js","sourceRoot":"","sources":["../../src/indexeddb/open-indexed-db.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAyCvD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,U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AC;IACnB,CAAC,CAAC,CAAC;IACH,OAAO,CAAC,gBAAgB,CAAC,OAAO,EAAE,GAAG,EAAE;QACtC,MAAM,EAAE,mBAAmB,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;QAClD,MAAM,CACL,IAAI,cAAc,CAAC,uCAAuC,IAAI,GAAG,EAAE;YAClE,KAAK,EAAE,YAAY,IAAI,OAAO,CAAC,KAAK;SACpC,CAAC,CACF,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a browser offers of Web MIDI: its `navigator`, or a stand-in. Both
|
|
3
|
+
* members are optional, as a browser may lack either.
|
|
4
|
+
*/
|
|
5
|
+
export type WebMidiNavigator<Access> = {
|
|
6
|
+
readonly requestMIDIAccess?: (options?: MIDIOptions) => Promise<Access>;
|
|
7
|
+
readonly permissions?: Pick<Permissions, "query">;
|
|
8
|
+
};
|
|
9
|
+
/** What to ask the browser for, beyond the ports' plain messages. */
|
|
10
|
+
export type WebMidiAccessOptions = {
|
|
11
|
+
/**
|
|
12
|
+
* System Exclusive messages too, which can reprogram a device and so need a
|
|
13
|
+
* stronger permission. Default: `false`.
|
|
14
|
+
*/
|
|
15
|
+
readonly sysex?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Software synthesizers and virtual ports too, where the browser lists them.
|
|
18
|
+
* Default: `false`.
|
|
19
|
+
*/
|
|
20
|
+
readonly software?: boolean;
|
|
21
|
+
};
|
|
22
|
+
/** The browser has no Web MIDI, as Safari, or none in this context. */
|
|
23
|
+
export declare class WebMidiUnsupportedError extends Error {
|
|
24
|
+
constructor(options?: ErrorOptions);
|
|
25
|
+
}
|
|
26
|
+
/** The user, or the browser's settings, refused the MIDI devices. */
|
|
27
|
+
export declare class WebMidiDeniedError extends Error {
|
|
28
|
+
constructor(options?: ErrorOptions);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Whether the page may reach the MIDI devices without asking: `"granted"`;
|
|
32
|
+
* `"prompt"` when the browser would ask, or cannot say; `"denied"` when it
|
|
33
|
+
* refused; `"unsupported"` without Web MIDI. Reach for it to decide whether to
|
|
34
|
+
* show a button asking for access before {@link requestWebMidiAccess} prompts.
|
|
35
|
+
*
|
|
36
|
+
* @param navigator The page's `navigator`, or a stand-in.
|
|
37
|
+
* @param options `sysex` asks about the stronger permission.
|
|
38
|
+
*/
|
|
39
|
+
export declare function queryWebMidiPermission(navigator: WebMidiNavigator<unknown>, options?: Pick<WebMidiAccessOptions, "sysex">): Promise<PermissionState | "unsupported">;
|
|
40
|
+
/**
|
|
41
|
+
* Asks the browser for its MIDI devices; it may ask the user first, and answers
|
|
42
|
+
* once they did.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* const access = await requestWebMidiAccess(navigator);
|
|
46
|
+
* using inputs = new WebMidiInputs(access);
|
|
47
|
+
*
|
|
48
|
+
* @param navigator The page's `navigator`, or a stand-in.
|
|
49
|
+
* @returns The browser's `MIDIAccess`.
|
|
50
|
+
* @throws {WebMidiUnsupportedError} Without Web MIDI.
|
|
51
|
+
* @throws {WebMidiDeniedError} When it is refused.
|
|
52
|
+
*/
|
|
53
|
+
export declare function requestWebMidiAccess<Access>(navigator: WebMidiNavigator<Access>, options?: WebMidiAccessOptions): Promise<Access>;
|
|
54
|
+
//# sourceMappingURL=web-midi-access.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web-midi-access.d.ts","sourceRoot":"","sources":["../../src/web-midi/web-midi-access.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,gBAAgB,CAAC,MAAM,IAAI;IACtC,QAAQ,CAAC,iBAAiB,CAAC,EAAE,CAAC,OAAO,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IACxE,QAAQ,CAAC,WAAW,CAAC,EAAE,IAAI,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;CAClD,CAAC;AAEF,qEAAqE;AACrE,MAAM,MAAM,oBAAoB,GAAG;IAClC;;;OAGG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC;IACzB;;;OAGG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;CAC5B,CAAC;AAEF,uEAAuE;AACvE,qBAAa,uBAAwB,SAAQ,KAAK;IACjD,YAAY,OAAO,CAAC,EAAE,YAAY,EAGjC;CACD;AAED,qEAAqE;AACrE,qBAAa,kBAAmB,SAAQ,KAAK;IAC5C,YAAY,OAAO,CAAC,EAAE,YAAY,EAGjC;CACD;AAED;;;;;;;;GAQG;AACH,wBAAsB,sBAAsB,CAC3C,SAAS,EAAE,gBAAgB,CAAC,OAAO,CAAC,EACpC,OAAO,CAAC,EAAE,IAAI,CAAC,oBAAoB,EAAE,OAAO,CAAC,GAC3C,OAAO,CAAC,eAAe,GAAG,aAAa,CAAC,CAkB1C;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,oBAAoB,CAAC,MAAM,EAChD,SAAS,EAAE,gBAAgB,CAAC,MAAM,CAAC,EACnC,OAAO,CAAC,EAAE,oBAAoB,GAC5B,OAAO,CAAC,MAAM,CAAC,CAoBjB"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/** The browser has no Web MIDI, as Safari, or none in this context. */
|
|
2
|
+
export class WebMidiUnsupportedError extends Error {
|
|
3
|
+
constructor(options) {
|
|
4
|
+
super("This browser cannot reach MIDI devices", options);
|
|
5
|
+
this.name = "WebMidiUnsupportedError";
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
/** The user, or the browser's settings, refused the MIDI devices. */
|
|
9
|
+
export class WebMidiDeniedError extends Error {
|
|
10
|
+
constructor(options) {
|
|
11
|
+
super("Access to the MIDI devices was refused", options);
|
|
12
|
+
this.name = "WebMidiDeniedError";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Whether the page may reach the MIDI devices without asking: `"granted"`;
|
|
17
|
+
* `"prompt"` when the browser would ask, or cannot say; `"denied"` when it
|
|
18
|
+
* refused; `"unsupported"` without Web MIDI. Reach for it to decide whether to
|
|
19
|
+
* show a button asking for access before {@link requestWebMidiAccess} prompts.
|
|
20
|
+
*
|
|
21
|
+
* @param navigator The page's `navigator`, or a stand-in.
|
|
22
|
+
* @param options `sysex` asks about the stronger permission.
|
|
23
|
+
*/
|
|
24
|
+
export async function queryWebMidiPermission(navigator, options) {
|
|
25
|
+
if (navigator.requestMIDIAccess === undefined) {
|
|
26
|
+
return "unsupported";
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
// The Web MIDI specification's `MidiPermissionDescriptor`, which adds
|
|
30
|
+
// `sysex` to the plain descriptor.
|
|
31
|
+
const descriptor = {
|
|
32
|
+
name: "midi",
|
|
33
|
+
sysex: options?.sysex ?? false,
|
|
34
|
+
};
|
|
35
|
+
const status = await navigator.permissions?.query(descriptor);
|
|
36
|
+
return status?.state ?? "prompt";
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
// A browser that cannot query this permission asks when requested.
|
|
40
|
+
return "prompt";
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Asks the browser for its MIDI devices; it may ask the user first, and answers
|
|
45
|
+
* once they did.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* const access = await requestWebMidiAccess(navigator);
|
|
49
|
+
* using inputs = new WebMidiInputs(access);
|
|
50
|
+
*
|
|
51
|
+
* @param navigator The page's `navigator`, or a stand-in.
|
|
52
|
+
* @returns The browser's `MIDIAccess`.
|
|
53
|
+
* @throws {WebMidiUnsupportedError} Without Web MIDI.
|
|
54
|
+
* @throws {WebMidiDeniedError} When it is refused.
|
|
55
|
+
*/
|
|
56
|
+
export async function requestWebMidiAccess(navigator, options) {
|
|
57
|
+
if (navigator.requestMIDIAccess === undefined) {
|
|
58
|
+
throw new WebMidiUnsupportedError();
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
return await navigator.requestMIDIAccess({
|
|
62
|
+
sysex: options?.sysex ?? false,
|
|
63
|
+
software: options?.software ?? false,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
if (error instanceof DOMException) {
|
|
68
|
+
if (error.name === "SecurityError" || error.name === "NotAllowedError") {
|
|
69
|
+
throw new WebMidiDeniedError({ cause: error });
|
|
70
|
+
}
|
|
71
|
+
if (error.name === "NotSupportedError") {
|
|
72
|
+
throw new WebMidiUnsupportedError({ cause: error });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=web-midi-access.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web-midi-access.js","sourceRoot":"","sources":["../../src/web-midi/web-midi-access.ts"],"names":[],"mappings":"AAuBA,uEAAuE;AACvE,MAAM,OAAO,uBAAwB,SAAQ,KAAK;IACjD,YAAY,OAAsB;QACjC,KAAK,CAAC,wCAAwC,EAAE,OAAO,CAAC,CAAC;QACzD,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAC;IACvC,CAAC;CACD;AAED,qEAAqE;AACrE,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAC5C,YAAY,OAAsB;QACjC,KAAK,CAAC,wCAAwC,EAAE,OAAO,CAAC,CAAC;QACzD,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IAClC,CAAC;CACD;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAC3C,SAAoC,EACpC,OAA6C;IAE7C,IAAI,SAAS,CAAC,iBAAiB,KAAK,SAAS,EAAE,CAAC;QAC/C,OAAO,aAAa,CAAC;IACtB,CAAC;IACD,IAAI,CAAC;QACJ,sEAAsE;QACtE,mCAAmC;QACnC,MAA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/async";
|
|
2
|
+
/**
|
|
3
|
+
* What names a MIDI port from one session to the next, where its id may change:
|
|
4
|
+
* its name and its manufacturer's, empty when the browser gives none.
|
|
5
|
+
*/
|
|
6
|
+
export type MidiPortIdentity = {
|
|
7
|
+
readonly name: string;
|
|
8
|
+
readonly manufacturer: string;
|
|
9
|
+
};
|
|
10
|
+
/** An input port, as {@link WebMidiInputs} lists it. */
|
|
11
|
+
export type MidiInputPortInfo = MidiPortIdentity & {
|
|
12
|
+
/** The browser's id for the port, in this session only. */
|
|
13
|
+
readonly id: string;
|
|
14
|
+
readonly isConnected: boolean;
|
|
15
|
+
};
|
|
16
|
+
/** The parts of a Web MIDI `MIDIInput` {@link WebMidiInputs} uses. */
|
|
17
|
+
export type WebMidiInputPort = {
|
|
18
|
+
readonly id: string;
|
|
19
|
+
readonly name: string | null;
|
|
20
|
+
readonly manufacturer: string | null;
|
|
21
|
+
readonly state: MIDIPortDeviceState;
|
|
22
|
+
addEventListener(type: "midimessage", listener: (event: Event) => void, options?: AddEventListenerOptions): void;
|
|
23
|
+
close?(): Promise<unknown>;
|
|
24
|
+
};
|
|
25
|
+
/** The parts of a Web MIDI `MIDIAccess` {@link WebMidiInputs} uses. */
|
|
26
|
+
export type WebMidiAccess = {
|
|
27
|
+
readonly inputs: ReadonlyMap<string, WebMidiInputPort>;
|
|
28
|
+
addEventListener(type: "statechange", listener: () => void, options?: AddEventListenerOptions): void;
|
|
29
|
+
};
|
|
30
|
+
/** Events of a {@link WebMidiInputs}. */
|
|
31
|
+
export type WebMidiInputsEvents = {
|
|
32
|
+
/** A port came, went, or changed state: read `ports` again. */
|
|
33
|
+
ports: [];
|
|
34
|
+
/** The port followed was connected, or lost. */
|
|
35
|
+
connection: [isConnected: boolean];
|
|
36
|
+
/**
|
|
37
|
+
* The bytes of one `midimessage` event of the port followed, as the browser
|
|
38
|
+
* delivered them — one whole MIDI message, a SysEx included — with its
|
|
39
|
+
* `timeStamp`, in milliseconds on the page's `performance.now()` time line,
|
|
40
|
+
* and the port it came from. Decode them with `@ac-kit/format-midi`'s
|
|
41
|
+
* `MidiMessageDecoder`, one per port.
|
|
42
|
+
*/
|
|
43
|
+
message: [data: Uint8Array, time: number, port: MidiInputPortInfo];
|
|
44
|
+
};
|
|
45
|
+
/** Whether two ports are the same device, as their names tell. */
|
|
46
|
+
export declare function isSameMidiPort(port: MidiPortIdentity, other: MidiPortIdentity): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* The input ports of a Web MIDI access, and the one followed by its identity:
|
|
49
|
+
* listened to while connected, given up when it goes, and taken up again when
|
|
50
|
+
* it comes back, whatever its id then — the way a person expects "my keyboard"
|
|
51
|
+
* to be remembered across reloads and replugs. Each message is handed over as
|
|
52
|
+
* the bytes the browser delivered, with the time it arrived; this class reads
|
|
53
|
+
* no MIDI syntax.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* using inputs = new WebMidiInputs(await requestWebMidiAccess(navigator));
|
|
57
|
+
* inputs.subscribe("message", (data, time) => console.log(data, time));
|
|
58
|
+
* inputs.follow({ name: "Keystation 49", manufacturer: "M-Audio" });
|
|
59
|
+
*/
|
|
60
|
+
export declare class WebMidiInputs extends EventDispatcherMapBase<WebMidiInputsEvents> implements Disposable {
|
|
61
|
+
private readonly access;
|
|
62
|
+
private readonly stopping;
|
|
63
|
+
private followed;
|
|
64
|
+
private attached;
|
|
65
|
+
constructor(access: WebMidiAccess);
|
|
66
|
+
/** Every input port the browser knows, connected or not. O(ports). */
|
|
67
|
+
get ports(): MidiInputPortInfo[];
|
|
68
|
+
/** Whether the port followed is connected and listened to. */
|
|
69
|
+
get isConnected(): boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Follows the port of `identity`, connected now or later; none when
|
|
72
|
+
* `undefined`. The first of several ports with the same identity is taken.
|
|
73
|
+
*/
|
|
74
|
+
follow(identity: MidiPortIdentity | undefined): void;
|
|
75
|
+
/** Stops listening, to the ports and the access, and closes the port. */
|
|
76
|
+
[Symbol.dispose](): void;
|
|
77
|
+
private refresh;
|
|
78
|
+
private attach;
|
|
79
|
+
private detach;
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=web-midi-inputs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web-midi-inputs.d.ts","sourceRoot":"","sources":["../../src/web-midi/web-midi-inputs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAEvD;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC9B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEF,wDAAwD;AACxD,MAAM,MAAM,iBAAiB,GAAG,gBAAgB,GAAG;IAClD,2DAA2D;IAC3D,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;CAC9B,CAAC;AAEF,sEAAsE;AACtE,MAAM,MAAM,gBAAgB,GAAG;IAC9B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,QAAQ,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IACrC,QAAQ,CAAC,KAAK,EAAE,mBAAmB,CAAC;IACpC,gBAAgB,CACf,IAAI,EAAE,aAAa,EACnB,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,EAChC,OAAO,CAAC,EAAE,uBAAuB,GAC/B,IAAI,CAAC;IACR,KAAK,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;CAC3B,CAAC;AAEF,uEAAuE;AACvE,MAAM,MAAM,aAAa,GAAG;IAC3B,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;IACvD,gBAAgB,CACf,IAAI,EAAE,aAAa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|
+
export function isSameMidiPort(port, other) {
|
|
4
|
+
return port.name === other.name && port.manufacturer === other.manufacturer;
|
|
5
|
+
}
|
|
6
|
+
function isMidiMessageEvent_(event) {
|
|
7
|
+
return "data" in event;
|
|
8
|
+
}
|
|
9
|
+
function portInfo_(port) {
|
|
10
|
+
return {
|
|
11
|
+
id: port.id,
|
|
12
|
+
name: port.name ?? "",
|
|
13
|
+
manufacturer: port.manufacturer ?? "",
|
|
14
|
+
isConnected: port.state === "connected",
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The input ports of a Web MIDI access, and the one followed by its identity:
|
|
19
|
+
* listened to while connected, given up when it goes, and taken up again when
|
|
20
|
+
* it comes back, whatever its id then — the way a person expects "my keyboard"
|
|
21
|
+
* to be remembered across reloads and replugs. Each message is handed over as
|
|
22
|
+
* the bytes the browser delivered, with the time it arrived; this class reads
|
|
23
|
+
* no MIDI syntax.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* using inputs = new WebMidiInputs(await requestWebMidiAccess(navigator));
|
|
27
|
+
* inputs.subscribe("message", (data, time) => console.log(data, time));
|
|
28
|
+
* inputs.follow({ name: "Keystation 49", manufacturer: "M-Audio" });
|
|
29
|
+
*/
|
|
30
|
+
export class WebMidiInputs extends EventDispatcherMapBase {
|
|
31
|
+
access;
|
|
32
|
+
stopping = new AbortController();
|
|
33
|
+
followed;
|
|
34
|
+
attached;
|
|
35
|
+
constructor(access) {
|
|
36
|
+
super();
|
|
37
|
+
this.access = access;
|
|
38
|
+
access.addEventListener("statechange", () => this.refresh(), {
|
|
39
|
+
signal: this.stopping.signal,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/** Every input port the browser knows, connected or not. O(ports). */
|
|
43
|
+
get ports() {
|
|
44
|
+
return Array.from(this.access.inputs.values(), portInfo_);
|
|
45
|
+
}
|
|
46
|
+
/** Whether the port followed is connected and listened to. */
|
|
47
|
+
get isConnected() {
|
|
48
|
+
return this.attached !== undefined;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Follows the port of `identity`, connected now or later; none when
|
|
52
|
+
* `undefined`. The first of several ports with the same identity is taken.
|
|
53
|
+
*/
|
|
54
|
+
follow(identity) {
|
|
55
|
+
this.followed = identity;
|
|
56
|
+
this.attach();
|
|
57
|
+
}
|
|
58
|
+
/** Stops listening, to the ports and the access, and closes the port. */
|
|
59
|
+
[Symbol.dispose]() {
|
|
60
|
+
this.stopping.abort();
|
|
61
|
+
this.detach();
|
|
62
|
+
}
|
|
63
|
+
refresh() {
|
|
64
|
+
this.attach();
|
|
65
|
+
this.dispatch("ports", []);
|
|
66
|
+
}
|
|
67
|
+
attach() {
|
|
68
|
+
const { followed, attached } = this;
|
|
69
|
+
const isWanted = (port) => port.state === "connected" &&
|
|
70
|
+
followed !== undefined &&
|
|
71
|
+
isSameMidiPort(portInfo_(port), followed);
|
|
72
|
+
// A port gone from the list is lost, whatever its state says.
|
|
73
|
+
const candidates = Array.from(this.access.inputs.values()).filter(isWanted);
|
|
74
|
+
if (attached !== undefined && candidates.includes(attached.port)) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const wasConnected = this.isConnected;
|
|
78
|
+
this.detach();
|
|
79
|
+
const [found] = candidates;
|
|
80
|
+
if (found !== undefined) {
|
|
81
|
+
const detach = new AbortController();
|
|
82
|
+
found.addEventListener("midimessage", (event) => {
|
|
83
|
+
if (isMidiMessageEvent_(event) && event.data !== null) {
|
|
84
|
+
this.dispatch("message", [
|
|
85
|
+
event.data,
|
|
86
|
+
event.timeStamp,
|
|
87
|
+
portInfo_(found),
|
|
88
|
+
]);
|
|
89
|
+
}
|
|
90
|
+
}, { signal: detach.signal });
|
|
91
|
+
this.attached = { port: found, detach };
|
|
92
|
+
}
|
|
93
|
+
if (wasConnected !== this.isConnected) {
|
|
94
|
+
this.dispatch("connection", [this.isConnected]);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
detach() {
|
|
98
|
+
const { attached } = this;
|
|
99
|
+
if (attached === undefined) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
this.attached = undefined;
|
|
103
|
+
attached.detach.abort();
|
|
104
|
+
attached.port.close?.().catch(() => { });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=web-midi-inputs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web-midi-inputs.js","sourceRoot":"","sources":["../../src/web-midi/web-midi-inputs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AA0DvD,kEAAkE;AAClE,MAAM,UAAU,cAAc,CAC7B,IAAsB,EACtB,KAAuB;IAEvB,OAAO,IAAI,CAAC,IAAI,KAAK,KAAK,CAAC,IAAI,IAAI,IAAI,CAAC,YAAY,KAAK,KAAK,CAAC,YAAY,CAAC;AAC7E,CAAC;AAED,SAAS,mBAAmB,CAAC,KAAY;IACxC,OAAO,MAAM,IAAI,KAAK,CAAC;AACxB,CAAC;AAED,SAAS,SAAS,CAAC,IAAsB;IACxC,OAAO;QACN,EAAE,EAAE,IAAI,CAAC,EAAE;QACX,IAAI,EAAE,IAAI,CAAC,IAAI,IAAI,EAAE;QACrB,YAAY,EAAE,IAAI,CAAC,YAAY,IAAI,EAAE;QACrC,WAAW,EAAE,IAAI,CAAC,KAAK,KAAK,WAAW;KACvC,CAAC;AACH,CAAC;AAOD;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,aACZ,SAAQ,sBAA2C;IAOtB,MAAM;IAJlB,QAAQ,GAAG,IAAI,eAAe,EAAE,CAAC;IAC1C,QAAQ,CAA+B;IACvC,QAAQ,CAAwB;IAExC,YAA6B,MAAqB;QACjD,KAAK,EAAE,CAAC;sBADoB,MAAM;QAElC,MAAM,CAAC,gBAAgB,CAAC,aAAa,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE;YAC5D,MA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@
|
|
|
1
|
+
import type { Callable } from "@ac-kit/core";
|
|
2
|
+
/** How much of a body crossed so far. */
|
|
3
|
+
export type HttpProgress = {
|
|
4
|
+
/** Bytes sent, or received, so far. */
|
|
5
|
+
readonly loaded: number;
|
|
6
|
+
/** The body's whole length, when it is known. */
|
|
7
|
+
readonly total: number | undefined;
|
|
8
|
+
};
|
|
9
|
+
/** What a response's body is read as, by `responseType`. */
|
|
10
|
+
export type ProgressResponseBodies = {
|
|
11
|
+
text: string;
|
|
12
|
+
bytes: Uint8Array<ArrayBuffer>;
|
|
13
|
+
blob: Blob;
|
|
14
|
+
};
|
|
15
|
+
/** A request to send with {@link sendWithProgress}. */
|
|
16
|
+
export type ProgressRequest<ResponseType extends keyof ProgressResponseBodies = "text"> = {
|
|
17
|
+
readonly method: string;
|
|
18
|
+
/** Absolute, or relative to the page's URL. */
|
|
19
|
+
readonly url: string | URL;
|
|
20
|
+
readonly headers?: HeadersInit;
|
|
21
|
+
/**
|
|
22
|
+
* Sent with its length declared. A `FormData`, `URLSearchParams` or string
|
|
23
|
+
* gets its `Content-Type` unless `headers` sets one.
|
|
24
|
+
*/
|
|
25
|
+
readonly body?: XMLHttpRequestBodyInit | null;
|
|
26
|
+
/**
|
|
27
|
+
* Whether a cross-origin request carries the page's cookies and HTTP
|
|
28
|
+
* authentication (`"include"`), or only a same-origin one does
|
|
29
|
+
* (`"same-origin"`, the default, as `fetch`'s). `XMLHttpRequest` cannot leave
|
|
30
|
+
* them off a same-origin request.
|
|
31
|
+
*/
|
|
32
|
+
readonly credentials?: "same-origin" | "include";
|
|
33
|
+
/**
|
|
34
|
+
* How the response body is read: `"text"` (the default), `"bytes"` or
|
|
35
|
+
* `"blob"`.
|
|
36
|
+
*/
|
|
37
|
+
readonly responseType?: ResponseType;
|
|
38
|
+
/** Aborting it cancels the request, which rejects with its reason. */
|
|
39
|
+
readonly signal?: AbortSignal;
|
|
40
|
+
/**
|
|
41
|
+
* Told how much of the body was sent, as it goes; never called without a
|
|
42
|
+
* body. The last call has `loaded` equal to the body's length.
|
|
43
|
+
*/
|
|
44
|
+
readonly onUploadProgress?: Callable<[progress: HttpProgress]>;
|
|
45
|
+
/** Told how much of the response body arrived, as it goes. */
|
|
46
|
+
readonly onDownloadProgress?: Callable<[progress: HttpProgress]>;
|
|
47
|
+
};
|
|
48
|
+
/** The response to a {@link sendWithProgress} request, whatever its status. */
|
|
49
|
+
export type ProgressResponse<Body> = {
|
|
50
|
+
readonly status: number;
|
|
51
|
+
readonly statusText: string;
|
|
52
|
+
readonly headers: Headers;
|
|
53
|
+
/** The URL the response came from, after redirects. */
|
|
54
|
+
readonly url: string;
|
|
55
|
+
readonly body: Body;
|
|
56
|
+
};
|
|
57
|
+
/** The constructor of the page's `XMLHttpRequest`, or a stand-in. */
|
|
58
|
+
export type XMLHttpRequestConstructor = new () => XMLHttpRequest;
|
|
59
|
+
/**
|
|
60
|
+
* The request failed before a response: the network, CORS, or the browser
|
|
61
|
+
* itself ended it. A `TypeError`, as `fetch`'s own failure.
|
|
62
|
+
*/
|
|
63
|
+
export declare class HttpNetworkError extends TypeError {
|
|
64
|
+
constructor(options?: ErrorOptions);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Sends a request from a page through `XMLHttpRequest`, telling how much of its
|
|
68
|
+
* body was sent and of its response received as they go: for a file upload a
|
|
69
|
+
* person watches. `fetch` reports no upload progress, and streams a request
|
|
70
|
+
* body only without its length; prefer it for anything else. The browser
|
|
71
|
+
* follows redirects and keeps its usual cookie and CORS rules.
|
|
72
|
+
*
|
|
73
|
+
* The response body is held whole in memory, read as `responseType` says.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* const response = await sendWithProgress(XMLHttpRequest, {
|
|
77
|
+
* method: "POST",
|
|
78
|
+
* url: "/songs",
|
|
79
|
+
* body: file,
|
|
80
|
+
* onUploadProgress: ({ loaded, total }) =>
|
|
81
|
+
* bar.update(loaded / (total ?? loaded)),
|
|
82
|
+
* signal,
|
|
83
|
+
* });
|
|
84
|
+
*
|
|
85
|
+
* @param XMLHttpRequestClass The page's `XMLHttpRequest` (a worker's too), or a
|
|
86
|
+
* stand-in in tests.
|
|
87
|
+
* @returns The response, whatever its status.
|
|
88
|
+
* @throws {HttpNetworkError} When the request fails before a response.
|
|
89
|
+
* @throws {TypeError} When `request.headers` holds an invalid field.
|
|
90
|
+
* @throws A `DOMException` the browser throws for a method, URL or field it
|
|
91
|
+
* refuses (`SyntaxError`, `SecurityError`); nothing is sent then.
|
|
92
|
+
* @throws When `request.signal` aborts, its reason; a `DOMException` named
|
|
93
|
+
* `"AbortError"` when the browser aborts it on its own.
|
|
94
|
+
*/
|
|
95
|
+
export declare function sendWithProgress<ResponseType extends keyof ProgressResponseBodies = "text">(XMLHttpRequestClass: XMLHttpRequestConstructor, request: ProgressRequest<ResponseType>): Promise<ProgressResponse<ProgressResponseBodies[ResponseType]>>;
|
|
96
|
+
//# sourceMappingURL=send-with-progress.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"send-with-progress.d.ts","sourceRoot":"","sources":["../../src/xhr/send-with-progress.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C,yCAAyC;AACzC,MAAM,MAAM,YAAY,GAAG;IAC1B,uCAAuC;IACvC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,iDAAiD;IACjD,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;CACnC,CAAC;AAEF,4DAA4D;AAC5D,MAAM,MAAM,sBAAsB,GAAG;IACpC,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC;IAC/B,IAAI,EAAE,IAAI,CAAC;CACX,CAAC;AAEF,uDAAuD;AACvD,MAAM,MAAM,eAAe,CAC1B,YAAY,SAAS,MAAM,sBAAsB,GAAG,MAAM,IACvD;IACH,QAA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