altcha 2.2.3 → 2.3.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.
@@ -1,475 +0,0 @@
1
- var J = Object.defineProperty;
2
- var v = (t) => {
3
- throw TypeError(t);
4
- };
5
- var X = (t, n, e) => n in t ? J(t, n, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[n] = e;
6
- var p = (t, n, e) => X(t, typeof n != "symbol" ? n + "" : n, e), S = (t, n, e) => n.has(t) || v("Cannot " + e);
7
- var f = (t, n, e) => (S(t, n, "read from private field"), e ? e.call(t) : n.get(t)), w = (t, n, e) => n.has(t) ? v("Cannot add the same private member more than once") : n instanceof WeakSet ? n.add(t) : n.set(t, e);
8
- var u = (t, n, e) => (S(t, n, "access private method"), e);
9
- const E = {
10
- generateKey: Q,
11
- exportKey: W,
12
- importKey: Z,
13
- decrypt: te,
14
- encrypt: ee
15
- };
16
- async function Q(t = 256) {
17
- return crypto.subtle.generateKey({
18
- name: "AES-GCM",
19
- length: t
20
- }, !0, ["encrypt", "decrypt"]);
21
- }
22
- async function W(t) {
23
- return new Uint8Array(await crypto.subtle.exportKey("raw", t));
24
- }
25
- async function Z(t) {
26
- return crypto.subtle.importKey("raw", t, {
27
- name: "AES-GCM"
28
- }, !0, ["encrypt", "decrypt"]);
29
- }
30
- async function ee(t, n, e = 16) {
31
- const i = crypto.getRandomValues(new Uint8Array(e));
32
- return {
33
- encrypted: new Uint8Array(await crypto.subtle.encrypt({
34
- name: "AES-GCM",
35
- iv: i
36
- }, t, n)),
37
- iv: i
38
- };
39
- }
40
- async function te(t, n, e) {
41
- return new Uint8Array(await crypto.subtle.decrypt({
42
- name: "AES-GCM",
43
- iv: e
44
- }, t, n));
45
- }
46
- function ne(t, n = !1) {
47
- return n && (t = t.replace(/_/g, "/").replace(/-/g, "+") + "=".repeat(3 - (3 + t.length) % 4)), Uint8Array.from(atob(t), (e) => e.charCodeAt(0));
48
- }
49
- function x(t, n = !1) {
50
- const e = btoa(String.fromCharCode(...t));
51
- return n ? e.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "") : e;
52
- }
53
- function L(t, n = 80) {
54
- let e = "";
55
- for (; t.length > 0; )
56
- e += t.slice(0, n) + `
57
- `, t = t.slice(n);
58
- return e;
59
- }
60
- function C(t) {
61
- return ne(t.split(/\r?\n/).filter((n) => !n.startsWith("-----")).join(""));
62
- }
63
- const h = "RSA-OAEP", b = "SHA-256", re = 2048, ie = new Uint8Array([1, 0, 1]), se = {
64
- generateKeyPair: oe,
65
- encrypt: ae,
66
- decrypt: ce,
67
- exportPrivateKey: I,
68
- exportPrivateKeyPem: pe,
69
- exportPublicKey: F,
70
- exportPublicKeyPem: le,
71
- exportPublicKeyFromPrivateKey: de,
72
- importPrivateKey: T,
73
- importPrivateKeyPem: ue,
74
- importPublicKey: N,
75
- importPublicKeyPem: z
76
- };
77
- async function oe() {
78
- return crypto.subtle.generateKey({
79
- name: h,
80
- modulusLength: re,
81
- publicExponent: ie,
82
- hash: b
83
- }, !0, ["encrypt", "decrypt"]);
84
- }
85
- async function ae(t, n) {
86
- return new Uint8Array(await crypto.subtle.encrypt({
87
- name: h
88
- }, t, n));
89
- }
90
- async function ce(t, n) {
91
- return new Uint8Array(await crypto.subtle.decrypt({
92
- name: h
93
- }, t, n));
94
- }
95
- async function F(t) {
96
- return new Uint8Array(await crypto.subtle.exportKey("spki", t));
97
- }
98
- async function I(t) {
99
- return new Uint8Array(await crypto.subtle.exportKey("pkcs8", t));
100
- }
101
- async function le(t) {
102
- return `-----BEGIN PUBLIC KEY-----
103
- ` + L(x(await F(t)), 64) + "-----END PUBLIC KEY-----";
104
- }
105
- async function pe(t) {
106
- return `-----BEGIN PRIVATE KEY-----
107
- ` + L(x(await I(t)), 64) + "-----END PRIVATE KEY-----";
108
- }
109
- async function N(t) {
110
- return crypto.subtle.importKey("spki", t, {
111
- name: h,
112
- hash: b
113
- }, !0, ["encrypt"]);
114
- }
115
- async function z(t) {
116
- return N(C(t));
117
- }
118
- async function T(t) {
119
- return crypto.subtle.importKey("pkcs8", t, {
120
- name: h,
121
- hash: b
122
- }, !0, ["decrypt"]);
123
- }
124
- async function ue(t) {
125
- return T(C(t));
126
- }
127
- async function de(t) {
128
- const n = await crypto.subtle.exportKey("jwk", t);
129
- delete n.d, delete n.dp, delete n.dq, delete n.q, delete n.qi, n.key_ops = ["encrypt"];
130
- const e = await crypto.subtle.importKey("jwk", n, {
131
- name: h,
132
- hash: b
133
- }, !0, ["encrypt"]);
134
- return F(e);
135
- }
136
- const ye = new Uint8Array([1, 0, 1]), he = 256, fe = 16;
137
- async function me(t, n, e = {}) {
138
- const { aesIVLength: i = fe, aesKeyLength: r = he } = e, o = await E.generateKey(r), { encrypted: a, iv: s } = await E.encrypt(o, n, i), l = await se.encrypt(t, await E.exportKey(o));
139
- return new Uint8Array([
140
- ...ye,
141
- ...new Uint8Array([l.length]),
142
- ...new Uint8Array([s.length]),
143
- ...l,
144
- ...s,
145
- ...a
146
- ]);
147
- }
148
- class P {
149
- /**
150
- * Constructs a new instance of the Plugin.
151
- *
152
- * @param {PluginContext} context - The context provided to the plugin, containing necessary configurations and dependencies.
153
- */
154
- constructor(n) {
155
- this.context = n;
156
- }
157
- /**
158
- * Registers a plugin class in the global `altchaPlugins` array.
159
- * Ensures the plugin is added only once.
160
- *
161
- * @param {new(context: PluginContext) => Plugin} cls - The plugin class to register.
162
- */
163
- static register(n) {
164
- typeof globalThis.altchaPlugins != "object" && (globalThis.altchaPlugins = []), globalThis.altchaPlugins.includes(n) || globalThis.altchaPlugins.push(n);
165
- }
166
- /**
167
- * Clean up resources when the plugin is destroyed.
168
- * Override this method in subclasses to implement custom destruction logic.
169
- */
170
- destroy() {
171
- }
172
- /**
173
- * Callback triggered when an error changes.
174
- * Override this method in subclasses to handle error state changes.
175
- *
176
- * @param {string | null} err - The error message or `null` if there's no error.
177
- */
178
- onErrorChange(n) {
179
- }
180
- /**
181
- * Callback triggered when the plugin state changes.
182
- * Override this method in subclasses to handle state changes.
183
- *
184
- * @param {State} state - The new state of the plugin.
185
- */
186
- onStateChange(n) {
187
- }
188
- }
189
- /**
190
- * A distinct name of the plugin. Every plugin must have it's own name.
191
- */
192
- p(P, "pluginName");
193
- var m, g, c, R, j, k, U, H, q, O, A, $, B, G;
194
- class _ extends P {
195
- /**
196
- * Constructor initializes the plugin, setting up event listeners on the form.
197
- *
198
- * @param {PluginContext} context - Plugin context providing access to the element, configuration, and utility methods.
199
- */
200
- constructor(e) {
201
- super(e);
202
- w(this, c);
203
- p(this, "pendingFiles", []);
204
- p(this, "uploadHandles", []);
205
- p(this, "elForm");
206
- w(this, m, u(this, c, q).bind(this));
207
- w(this, g, u(this, c, O).bind(this));
208
- this.elForm = this.context.el.closest("form"), this.elForm && (this.elForm.addEventListener("change", f(this, m)), this.elForm.addEventListener("submit", f(this, g), {
209
- capture: !0
210
- }));
211
- }
212
- /**
213
- * Adds a file to the pending files list for upload.
214
- *
215
- * @param {string} fieldName - The field name associated with the file input.
216
- * @param {File} file - The file to be uploaded.
217
- */
218
- addFile(e, i) {
219
- this.pendingFiles.find(([r, o]) => r === e && o === i) || this.pendingFiles.push([e, i]);
220
- }
221
- /**
222
- * Cleans up event listeners and other resources when the plugin is destroyed.
223
- */
224
- destroy() {
225
- this.elForm && (this.elForm.removeEventListener("change", f(this, m)), this.elForm.removeEventListener("submit", f(this, g)));
226
- }
227
- /**
228
- * Uploads all pending files in the list.
229
- */
230
- async uploadPendingFiles() {
231
- var i;
232
- const e = async () => {
233
- const r = this.pendingFiles[0];
234
- if (r && await u(this, c, A).call(this, u(this, c, j).call(this, r)), this.pendingFiles.length)
235
- return e();
236
- };
237
- try {
238
- await e();
239
- } catch (r) {
240
- return this.context.log("upload failed", r), this.context.dispatch("uploaderror", {
241
- error: r
242
- }), !1;
243
- }
244
- this.pendingFiles.length === 0 && (u(this, c, R).call(this), (i = this.elForm) == null || i.requestSubmit());
245
- }
246
- }
247
- m = new WeakMap(), g = new WeakMap(), c = new WeakSet(), /**
248
- * Adds hidden input fields to the form containing the file IDs of uploaded files.
249
- */
250
- R = function() {
251
- var i, r, o;
252
- const e = this.uploadHandles.reduce(
253
- (a, s) => (a[s.fieldName] || (a[s.fieldName] = []), s.fileId && a[s.fieldName].push(s.fileId), a),
254
- {}
255
- );
256
- for (const a in e) {
257
- const s = document.createElement("input");
258
- s.name = a, s.type = "hidden", s.value = e[a].join(","), (r = (i = this.elForm) == null ? void 0 : i.querySelector(`[name="${a}"]`)) == null || r.setAttribute("disabled", "disabled"), (o = this.elForm) == null || o.appendChild(s);
259
- }
260
- }, /**
261
- * Creates an upload handle for the specified pending file.
262
- *
263
- * @param {[string, File]} pendingFile - The field name and file to be uploaded.
264
- * @returns {UploadHandle} The created upload handle.
265
- * @throws Will throw an error if the upload handle cannot be created.
266
- */
267
- j = function(e) {
268
- const i = this.pendingFiles.findIndex(
269
- ([o, a]) => o === e[0] && a === e[1]
270
- );
271
- if (i < 0)
272
- throw new Error("Cannot create upload handle.");
273
- const r = new ge(e[0], e[1]);
274
- return this.uploadHandles.push(r), this.pendingFiles.splice(i, 1), u(this, c, k).call(this, r), u(this, c, U).call(this), r;
275
- }, /**
276
- * Dispatches a custom event when a file upload starts.
277
- *
278
- * @param {UploadHandle} handle - The upload handle associated with the file upload.
279
- */
280
- k = function(e) {
281
- this.context.dispatch("upload", { handle: e });
282
- }, /**
283
- * Dispatches a custom event to track the progress of ongoing file uploads.
284
- */
285
- U = function() {
286
- const e = this.pendingFiles.reduce((r, [o, a]) => r + a.size, 0) + this.uploadHandles.reduce((r, { uploadSize: o }) => r + o, 0), i = this.uploadHandles.reduce(
287
- (r, { loaded: o }) => r + o,
288
- 0
289
- );
290
- this.context.dispatch("uploadprogress", {
291
- bytesLoaded: i,
292
- bytesTotal: e,
293
- pendingFiles: this.pendingFiles,
294
- uploadHandles: this.uploadHandles
295
- });
296
- }, /**
297
- * Retrieves the upload URL from the form's attributes.
298
- *
299
- * @returns {string | null} The upload URL, or null if not found.
300
- */
301
- H = function() {
302
- if (this.elForm) {
303
- const e = this.elForm.getAttribute("action"), i = this.elForm.getAttribute("data-upload-url");
304
- if (i)
305
- return i;
306
- const r = new URL(e || location.origin);
307
- return r.pathname = r.pathname + "/file", r.toString();
308
- }
309
- return null;
310
- }, /**
311
- * Handles the form's change event, adding files to the pending files list.
312
- *
313
- * @param {Event} ev - The change event.
314
- */
315
- q = function(e) {
316
- const i = e.target;
317
- if (i && i.type === "file") {
318
- const r = i.files;
319
- if (r != null && r.length)
320
- for (const o of r)
321
- this.addFile(i.name, o);
322
- }
323
- }, /**
324
- * Handles the form's submit event, preventing submission until all pending files are uploaded.
325
- *
326
- * @param {SubmitEvent} ev - The submit event.
327
- */
328
- O = function(e) {
329
- const i = e.target;
330
- i != null && i.hasAttribute(
331
- "data-code-challenge-form"
332
- ) || this.pendingFiles.length && (e.preventDefault(), e.stopPropagation(), this.uploadPendingFiles());
333
- }, A = async function(e, i) {
334
- const r = u(this, c, H).call(this);
335
- if (!r)
336
- throw new Error("Upload url not specified.");
337
- const o = {
338
- "content-type": "application/json"
339
- };
340
- i && (o.authorization = "Altcha payload=" + i);
341
- const a = await fetch(r, {
342
- body: JSON.stringify({
343
- name: e.file.name || "file",
344
- size: e.file.size,
345
- type: e.file.type || "application/octet-stream"
346
- }),
347
- credentials: "include",
348
- headers: o,
349
- method: "POST"
350
- });
351
- if (a.status === 401)
352
- return u(this, c, $).call(this, a, e);
353
- if (a.status !== 200)
354
- throw new Error(`Unexpected server response ${a.status}.`);
355
- const s = await a.json();
356
- let l = e.file;
357
- if (s.encrypted && s.encryptionPublicKey) {
358
- const d = await z(s.encryptionPublicKey), M = await new Response(
359
- new ReadableStream({
360
- async start(K) {
361
- const Y = e.file.stream().getReader();
362
- for (; ; ) {
363
- const { done: D, value: V } = await Y.read();
364
- if (D)
365
- break;
366
- K.enqueue(V);
367
- }
368
- K.close();
369
- }
370
- })
371
- ).arrayBuffer();
372
- l = await me(d, new Uint8Array(M));
373
- }
374
- return e.uploadSize = l instanceof Uint8Array ? l.byteLength : e.file.size, await u(this, c, G).call(this, s.uploadUrl, e, l, {
375
- "content-type": e.file.type || "application/octet-stream"
376
- }), s.finalizeUrl && await u(this, c, B).call(this, s.finalizeUrl, e.uploadSize), e.fileId = s.fileId, e.resolve({
377
- encrypted: s.encrypted,
378
- fileId: s.fileId
379
- }), e.promise;
380
- }, $ = async function(e, i) {
381
- var r;
382
- try {
383
- const o = e.headers.get("www-authenticate"), a = (r = o == null ? void 0 : o.match(/challenge=(.*),/)) == null ? void 0 : r[1];
384
- if (!a)
385
- throw new Error(
386
- "Unable to retrieve altcha challenge from www-authenticate header."
387
- );
388
- const s = JSON.parse(a);
389
- if (s && "challenge" in s) {
390
- const { solution: l } = await this.context.solve(s);
391
- if (l && "number" in l)
392
- return u(this, c, A).call(this, i, btoa(
393
- JSON.stringify({
394
- ...s,
395
- number: l.number
396
- })
397
- ));
398
- throw new Error("Invalid challenge solution.");
399
- }
400
- } catch (o) {
401
- throw this.context.log(o), new Error("Unable to solve altcha challenge for upload.");
402
- }
403
- }, B = async function(e, i) {
404
- const r = await fetch(e, {
405
- body: JSON.stringify({
406
- uploadedBytes: i
407
- }),
408
- headers: {
409
- "content-type": "application/json"
410
- },
411
- method: "POST"
412
- });
413
- if (r.status > 204)
414
- throw new Error(`Unexpected server response ${r.status}.`);
415
- return !0;
416
- }, G = async function(e, i, r, o = {}) {
417
- var a;
418
- return e = new URL(
419
- e,
420
- ((a = this.elForm) == null ? void 0 : a.getAttribute("action")) || location.origin
421
- ).toString(), new Promise((s, l) => {
422
- const d = new XMLHttpRequest();
423
- i.controller.signal.addEventListener("abort", () => {
424
- d.abort();
425
- }), d.upload.addEventListener("progress", (y) => {
426
- i.setProgress(y.loaded), u(this, c, U).call(this);
427
- }), d.addEventListener("error", (y) => {
428
- l(new Error("Upload failed."));
429
- }), d.addEventListener("load", () => {
430
- d.status >= 400 ? l(new Error(`Server responded with ${d.status}`)) : s(void 0);
431
- }), d.open("PUT", e);
432
- for (const y in o)
433
- d.setRequestHeader(y, o[y]);
434
- d.send(r);
435
- });
436
- }, p(_, "pluginName", "upload");
437
- class ge {
438
- /**
439
- * Creates an instance of UploadHandle.
440
- *
441
- * @param {string} fieldName - The name of the field associated with the file upload.
442
- * @param {File} file - The file to be uploaded.
443
- */
444
- constructor(n, e) {
445
- p(this, "controller", new AbortController());
446
- p(this, "promise");
447
- p(this, "fileId");
448
- p(this, "loaded", 0);
449
- p(this, "progress", 0);
450
- p(this, "uploadSize", 0);
451
- p(this, "resolve");
452
- p(this, "reject");
453
- this.fieldName = n, this.file = e, this.uploadSize = this.file.size, this.promise = new Promise((i, r) => {
454
- this.resolve = i, this.reject = r;
455
- });
456
- }
457
- /**
458
- * Aborts the file upload by invoking the AbortController's abort method.
459
- */
460
- abort() {
461
- this.controller.abort();
462
- }
463
- /**
464
- * Updates the progress of the file upload.
465
- *
466
- * @param {number} loaded - The number of bytes that have been uploaded.
467
- */
468
- setProgress(n) {
469
- this.loaded = n, this.progress = this.file.size && n ? Math.min(1, n / this.file.size) : 0;
470
- }
471
- }
472
- P.register(_);
473
- export {
474
- _ as PluginUpload
475
- };
@@ -1,4 +0,0 @@
1
- (function(c,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(c=typeof globalThis<"u"?globalThis:c||self,a(c["[name]"]={}))})(this,function(c){"use strict";var ge=Object.defineProperty;var _=c=>{throw TypeError(c)};var we=(c,a,u)=>a in c?ge(c,a,{enumerable:!0,configurable:!0,writable:!0,value:u}):c[a]=u;var y=(c,a,u)=>we(c,typeof a!="symbol"?a+"":a,u),R=(c,a,u)=>a.has(c)||_("Cannot "+u);var E=(c,a,u)=>(R(c,a,"read from private field"),u?u.call(c):a.get(c)),P=(c,a,u)=>a.has(c)?_("Cannot add the same private member more than once"):a instanceof WeakSet?a.add(c):a.set(c,u);var h=(c,a,u)=>(R(c,a,"access private method"),u);var w,b,l,k,H,O,v,q,M,$,S,B,G,Y;const a={generateKey:u,exportKey:D,importKey:V,decrypt:X,encrypt:J};async function u(t=256){return crypto.subtle.generateKey({name:"AES-GCM",length:t},!0,["encrypt","decrypt"])}async function D(t){return new Uint8Array(await crypto.subtle.exportKey("raw",t))}async function V(t){return crypto.subtle.importKey("raw",t,{name:"AES-GCM"},!0,["encrypt","decrypt"])}async function J(t,i,e=16){const r=crypto.getRandomValues(new Uint8Array(e));return{encrypted:new Uint8Array(await crypto.subtle.encrypt({name:"AES-GCM",iv:r},t,i)),iv:r}}async function X(t,i,e){return new Uint8Array(await crypto.subtle.decrypt({name:"AES-GCM",iv:e},t,i))}function Q(t,i=!1){return i&&(t=t.replace(/_/g,"/").replace(/-/g,"+")+"=".repeat(3-(3+t.length)%4)),Uint8Array.from(atob(t),e=>e.charCodeAt(0))}function x(t,i=!1){const e=btoa(String.fromCharCode(...t));return i?e.replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,""):e}function L(t,i=80){let e="";for(;t.length>0;)e+=t.slice(0,i)+`
2
- `,t=t.slice(i);return e}function C(t){return Q(t.split(/\r?\n/).filter(i=>!i.startsWith("-----")).join(""))}const m="RSA-OAEP",U="SHA-256",W=2048,Z=new Uint8Array([1,0,1]),ee={generateKeyPair:te,encrypt:ne,decrypt:re,exportPrivateKey:I,exportPrivateKeyPem:se,exportPublicKey:A,exportPublicKeyPem:ie,exportPublicKeyFromPrivateKey:ae,importPrivateKey:z,importPrivateKeyPem:oe,importPublicKey:T,importPublicKeyPem:N};async function te(){return crypto.subtle.generateKey({name:m,modulusLength:W,publicExponent:Z,hash:U},!0,["encrypt","decrypt"])}async function ne(t,i){return new Uint8Array(await crypto.subtle.encrypt({name:m},t,i))}async function re(t,i){return new Uint8Array(await crypto.subtle.decrypt({name:m},t,i))}async function A(t){return new Uint8Array(await crypto.subtle.exportKey("spki",t))}async function I(t){return new Uint8Array(await crypto.subtle.exportKey("pkcs8",t))}async function ie(t){return`-----BEGIN PUBLIC KEY-----
3
- `+L(x(await A(t)),64)+"-----END PUBLIC KEY-----"}async function se(t){return`-----BEGIN PRIVATE KEY-----
4
- `+L(x(await I(t)),64)+"-----END PRIVATE KEY-----"}async function T(t){return crypto.subtle.importKey("spki",t,{name:m,hash:U},!0,["encrypt"])}async function N(t){return T(C(t))}async function z(t){return crypto.subtle.importKey("pkcs8",t,{name:m,hash:U},!0,["decrypt"])}async function oe(t){return z(C(t))}async function ae(t){const i=await crypto.subtle.exportKey("jwk",t);delete i.d,delete i.dp,delete i.dq,delete i.q,delete i.qi,i.key_ops=["encrypt"];const e=await crypto.subtle.importKey("jwk",i,{name:m,hash:U},!0,["encrypt"]);return A(e)}const ce=new Uint8Array([1,0,1]),le=256,pe=16;async function ue(t,i,e={}){const{aesIVLength:r=pe,aesKeyLength:n=le}=e,o=await a.generateKey(n),{encrypted:p,iv:s}=await a.encrypt(o,i,r),d=await ee.encrypt(t,await a.exportKey(o));return new Uint8Array([...ce,...new Uint8Array([d.length]),...new Uint8Array([s.length]),...d,...s,...p])}class F{constructor(i){this.context=i}static register(i){typeof globalThis.altchaPlugins!="object"&&(globalThis.altchaPlugins=[]),globalThis.altchaPlugins.includes(i)||globalThis.altchaPlugins.push(i)}destroy(){}onErrorChange(i){}onStateChange(i){}}y(F,"pluginName");class K extends F{constructor(e){super(e);P(this,l);y(this,"pendingFiles",[]);y(this,"uploadHandles",[]);y(this,"elForm");P(this,w,h(this,l,M).bind(this));P(this,b,h(this,l,$).bind(this));this.elForm=this.context.el.closest("form"),this.elForm&&(this.elForm.addEventListener("change",E(this,w)),this.elForm.addEventListener("submit",E(this,b),{capture:!0}))}addFile(e,r){this.pendingFiles.find(([n,o])=>n===e&&o===r)||this.pendingFiles.push([e,r])}destroy(){this.elForm&&(this.elForm.removeEventListener("change",E(this,w)),this.elForm.removeEventListener("submit",E(this,b)))}async uploadPendingFiles(){var r;const e=async()=>{const n=this.pendingFiles[0];if(n&&await h(this,l,S).call(this,h(this,l,H).call(this,n)),this.pendingFiles.length)return e()};try{await e()}catch(n){return this.context.log("upload failed",n),this.context.dispatch("uploaderror",{error:n}),!1}this.pendingFiles.length===0&&(h(this,l,k).call(this),(r=this.elForm)==null||r.requestSubmit())}}w=new WeakMap,b=new WeakMap,l=new WeakSet,k=function(){var r,n,o;const e=this.uploadHandles.reduce((p,s)=>(p[s.fieldName]||(p[s.fieldName]=[]),s.fileId&&p[s.fieldName].push(s.fileId),p),{});for(const p in e){const s=document.createElement("input");s.name=p,s.type="hidden",s.value=e[p].join(","),(n=(r=this.elForm)==null?void 0:r.querySelector(`[name="${p}"]`))==null||n.setAttribute("disabled","disabled"),(o=this.elForm)==null||o.appendChild(s)}},H=function(e){const r=this.pendingFiles.findIndex(([o,p])=>o===e[0]&&p===e[1]);if(r<0)throw new Error("Cannot create upload handle.");const n=new de(e[0],e[1]);return this.uploadHandles.push(n),this.pendingFiles.splice(r,1),h(this,l,O).call(this,n),h(this,l,v).call(this),n},O=function(e){this.context.dispatch("upload",{handle:e})},v=function(){const e=this.pendingFiles.reduce((n,[o,p])=>n+p.size,0)+this.uploadHandles.reduce((n,{uploadSize:o})=>n+o,0),r=this.uploadHandles.reduce((n,{loaded:o})=>n+o,0);this.context.dispatch("uploadprogress",{bytesLoaded:r,bytesTotal:e,pendingFiles:this.pendingFiles,uploadHandles:this.uploadHandles})},q=function(){if(this.elForm){const e=this.elForm.getAttribute("action"),r=this.elForm.getAttribute("data-upload-url");if(r)return r;const n=new URL(e||location.origin);return n.pathname=n.pathname+"/file",n.toString()}return null},M=function(e){const r=e.target;if(r&&r.type==="file"){const n=r.files;if(n!=null&&n.length)for(const o of n)this.addFile(r.name,o)}},$=function(e){const r=e.target;r!=null&&r.hasAttribute("data-code-challenge-form")||this.pendingFiles.length&&(e.preventDefault(),e.stopPropagation(),this.uploadPendingFiles())},S=async function(e,r){const n=h(this,l,q).call(this);if(!n)throw new Error("Upload url not specified.");const o={"content-type":"application/json"};r&&(o.authorization="Altcha payload="+r);const p=await fetch(n,{body:JSON.stringify({name:e.file.name||"file",size:e.file.size,type:e.file.type||"application/octet-stream"}),credentials:"include",headers:o,method:"POST"});if(p.status===401)return h(this,l,B).call(this,p,e);if(p.status!==200)throw new Error(`Unexpected server response ${p.status}.`);const s=await p.json();let d=e.file;if(s.encrypted&&s.encryptionPublicKey){const f=await N(s.encryptionPublicKey),ye=await new Response(new ReadableStream({async start(j){const he=e.file.stream().getReader();for(;;){const{done:fe,value:me}=await he.read();if(fe)break;j.enqueue(me)}j.close()}})).arrayBuffer();d=await ue(f,new Uint8Array(ye))}return e.uploadSize=d instanceof Uint8Array?d.byteLength:e.file.size,await h(this,l,Y).call(this,s.uploadUrl,e,d,{"content-type":e.file.type||"application/octet-stream"}),s.finalizeUrl&&await h(this,l,G).call(this,s.finalizeUrl,e.uploadSize),e.fileId=s.fileId,e.resolve({encrypted:s.encrypted,fileId:s.fileId}),e.promise},B=async function(e,r){var n;try{const o=e.headers.get("www-authenticate"),p=(n=o==null?void 0:o.match(/challenge=(.*),/))==null?void 0:n[1];if(!p)throw new Error("Unable to retrieve altcha challenge from www-authenticate header.");const s=JSON.parse(p);if(s&&"challenge"in s){const{solution:d}=await this.context.solve(s);if(d&&"number"in d)return h(this,l,S).call(this,r,btoa(JSON.stringify({...s,number:d.number})));throw new Error("Invalid challenge solution.")}}catch(o){throw this.context.log(o),new Error("Unable to solve altcha challenge for upload.")}},G=async function(e,r){const n=await fetch(e,{body:JSON.stringify({uploadedBytes:r}),headers:{"content-type":"application/json"},method:"POST"});if(n.status>204)throw new Error(`Unexpected server response ${n.status}.`);return!0},Y=async function(e,r,n,o={}){var p;return e=new URL(e,((p=this.elForm)==null?void 0:p.getAttribute("action"))||location.origin).toString(),new Promise((s,d)=>{const f=new XMLHttpRequest;r.controller.signal.addEventListener("abort",()=>{f.abort()}),f.upload.addEventListener("progress",g=>{r.setProgress(g.loaded),h(this,l,v).call(this)}),f.addEventListener("error",g=>{d(new Error("Upload failed."))}),f.addEventListener("load",()=>{f.status>=400?d(new Error(`Server responded with ${f.status}`)):s(void 0)}),f.open("PUT",e);for(const g in o)f.setRequestHeader(g,o[g]);f.send(n)})},y(K,"pluginName","upload");class de{constructor(i,e){y(this,"controller",new AbortController);y(this,"promise");y(this,"fileId");y(this,"loaded",0);y(this,"progress",0);y(this,"uploadSize",0);y(this,"resolve");y(this,"reject");this.fieldName=i,this.file=e,this.uploadSize=this.file.size,this.promise=new Promise((r,n)=>{this.resolve=r,this.reject=n})}abort(){this.controller.abort()}setProgress(i){this.loaded=i,this.progress=this.file.size&&i?Math.min(1,i/this.file.size):0}}F.register(K),c.PluginUpload=K,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});