altcha 0.7.0 → 0.9.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.
@@ -0,0 +1 @@
1
+ declare module 'altcha/upload';
@@ -0,0 +1,458 @@
1
+ var V = Object.defineProperty;
2
+ var K = (t) => {
3
+ throw TypeError(t);
4
+ };
5
+ var J = (t, n, e) => n in t ? V(t, n, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[n] = e;
6
+ var p = (t, n, e) => J(t, typeof n != "symbol" ? n + "" : n, e), v = (t, n, e) => n.has(t) || K("Cannot " + e);
7
+ var h = (t, n, e) => (v(t, n, "read from private field"), e ? e.call(t) : n.get(t)), g = (t, n, e) => n.has(t) ? K("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) => (v(t, n, "access private method"), e);
9
+ const b = {
10
+ generateKey: X,
11
+ exportKey: Q,
12
+ importKey: W,
13
+ decrypt: ee,
14
+ encrypt: Z
15
+ };
16
+ async function X(t = 256) {
17
+ return crypto.subtle.generateKey({
18
+ name: "AES-GCM",
19
+ length: t
20
+ }, !0, ["encrypt", "decrypt"]);
21
+ }
22
+ async function Q(t) {
23
+ return new Uint8Array(await crypto.subtle.exportKey("raw", t));
24
+ }
25
+ async function W(t) {
26
+ return crypto.subtle.importKey("raw", t, {
27
+ name: "AES-GCM"
28
+ }, !0, ["encrypt", "decrypt"]);
29
+ }
30
+ async function Z(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 ee(t, n, e) {
41
+ return new Uint8Array(await crypto.subtle.decrypt({
42
+ name: "AES-GCM",
43
+ iv: e
44
+ }, t, n));
45
+ }
46
+ function te(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 S(t, n = !1) {
50
+ const e = btoa(String.fromCharCode(...t));
51
+ return n ? e.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "") : e;
52
+ }
53
+ function x(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 L(t) {
61
+ return te(t.split(/\r?\n/).filter((n) => !n.startsWith("-----")).join(""));
62
+ }
63
+ const d = "RSA-OAEP", w = "SHA-256", ne = 2048, re = new Uint8Array([1, 0, 1]), ie = {
64
+ generateKeyPair: se,
65
+ encrypt: oe,
66
+ decrypt: ae,
67
+ exportPrivateKey: C,
68
+ exportPrivateKeyPem: le,
69
+ exportPublicKey: U,
70
+ exportPublicKeyPem: ce,
71
+ exportPublicKeyFromPrivateKey: ue,
72
+ importPrivateKey: z,
73
+ importPrivateKeyPem: pe,
74
+ importPublicKey: I,
75
+ importPublicKeyPem: N
76
+ };
77
+ async function se() {
78
+ return crypto.subtle.generateKey({
79
+ name: d,
80
+ modulusLength: ne,
81
+ publicExponent: re,
82
+ hash: w
83
+ }, !0, ["encrypt", "decrypt"]);
84
+ }
85
+ async function oe(t, n) {
86
+ return new Uint8Array(await crypto.subtle.encrypt({
87
+ name: d
88
+ }, t, n));
89
+ }
90
+ async function ae(t, n) {
91
+ return new Uint8Array(await crypto.subtle.decrypt({
92
+ name: d
93
+ }, t, n));
94
+ }
95
+ async function U(t) {
96
+ return new Uint8Array(await crypto.subtle.exportKey("spki", t));
97
+ }
98
+ async function C(t) {
99
+ return new Uint8Array(await crypto.subtle.exportKey("pkcs8", t));
100
+ }
101
+ async function ce(t) {
102
+ return `-----BEGIN PUBLIC KEY-----
103
+ ` + x(S(await U(t)), 64) + "-----END PUBLIC KEY-----";
104
+ }
105
+ async function le(t) {
106
+ return `-----BEGIN PRIVATE KEY-----
107
+ ` + x(S(await C(t)), 64) + "-----END PRIVATE KEY-----";
108
+ }
109
+ async function I(t) {
110
+ return crypto.subtle.importKey("spki", t, {
111
+ name: d,
112
+ hash: w
113
+ }, !0, ["encrypt"]);
114
+ }
115
+ async function N(t) {
116
+ return I(L(t));
117
+ }
118
+ async function z(t) {
119
+ return crypto.subtle.importKey("pkcs8", t, {
120
+ name: d,
121
+ hash: w
122
+ }, !0, ["decrypt"]);
123
+ }
124
+ async function pe(t) {
125
+ return z(L(t));
126
+ }
127
+ async function ue(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: d,
132
+ hash: w
133
+ }, !0, ["encrypt"]);
134
+ return U(e);
135
+ }
136
+ const ye = new Uint8Array([1, 0, 1]), de = 256, he = 16;
137
+ async function fe(t, n, e = {}) {
138
+ const { aesIVLength: i = he, aesKeyLength: r = de } = e, o = await b.generateKey(r), { encrypted: c, iv: s } = await b.encrypt(o, n, i), l = await ie.encrypt(t, await b.exportKey(o));
139
+ return new Uint8Array([
140
+ ...ye,
141
+ ...new Uint8Array([l.length]),
142
+ ...new Uint8Array([s.length]),
143
+ ...l,
144
+ ...s,
145
+ ...c
146
+ ]);
147
+ }
148
+ class A {
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(A, "pluginName");
193
+ var f, m, a, _, j, k, E, H, R, q, P, O, B, G;
194
+ class T extends A {
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
+ g(this, a);
203
+ p(this, "pendingFiles", []);
204
+ p(this, "uploadHandles", []);
205
+ p(this, "elForm");
206
+ g(this, f, u(this, a, R).bind(this));
207
+ g(this, m, u(this, a, q).bind(this));
208
+ this.elForm = this.context.el.closest("form"), this.elForm && (this.elForm.addEventListener("change", h(this, f)), this.elForm.addEventListener("submit", h(this, m), {
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", h(this, f)), this.elForm.removeEventListener("submit", h(this, m)));
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, a, P).call(this, u(this, a, j).call(this, r)), this.pendingFiles.length)
235
+ return e();
236
+ };
237
+ await e(), this.pendingFiles.length === 0 && (u(this, a, _).call(this), (i = this.elForm) == null || i.requestSubmit());
238
+ }
239
+ }
240
+ f = new WeakMap(), m = new WeakMap(), a = new WeakSet(), /**
241
+ * Adds hidden input fields to the form containing the file IDs of uploaded files.
242
+ */
243
+ _ = function() {
244
+ var i, r, o;
245
+ const e = this.uploadHandles.reduce(
246
+ (c, s) => (c[s.fieldName] || (c[s.fieldName] = []), s.fileId && c[s.fieldName].push(s.fileId), c),
247
+ {}
248
+ );
249
+ for (const c in e) {
250
+ const s = document.createElement("input");
251
+ s.name = c, s.type = "hidden", s.value = e[c].join(","), (r = (i = this.elForm) == null ? void 0 : i.querySelector(`[name="${c}"]`)) == null || r.setAttribute("disabled", "disabled"), (o = this.elForm) == null || o.appendChild(s);
252
+ }
253
+ }, /**
254
+ * Creates an upload handle for the specified pending file.
255
+ *
256
+ * @param {[string, File]} pendingFile - The field name and file to be uploaded.
257
+ * @returns {UploadHandle} The created upload handle.
258
+ * @throws Will throw an error if the upload handle cannot be created.
259
+ */
260
+ j = function(e) {
261
+ const i = this.pendingFiles.findIndex(
262
+ ([o, c]) => o === e[0] && c === e[1]
263
+ );
264
+ if (i < 0)
265
+ throw new Error("Cannot create upload handle.");
266
+ const r = new me(e[0], e[1]);
267
+ return this.uploadHandles.push(r), this.pendingFiles.splice(i, 1), u(this, a, k).call(this, r), u(this, a, E).call(this), r;
268
+ }, /**
269
+ * Dispatches a custom event when a file upload starts.
270
+ *
271
+ * @param {UploadHandle} handle - The upload handle associated with the file upload.
272
+ */
273
+ k = function(e) {
274
+ this.context.dispatch("upload", { handle: e });
275
+ }, /**
276
+ * Dispatches a custom event to track the progress of ongoing file uploads.
277
+ */
278
+ E = function() {
279
+ const e = this.pendingFiles.reduce((r, [o, c]) => r + c.size, 0) + this.uploadHandles.reduce((r, { uploadSize: o }) => r + o, 0), i = this.uploadHandles.reduce(
280
+ (r, { loaded: o }) => r + o,
281
+ 0
282
+ );
283
+ this.context.dispatch("uploadprogress", {
284
+ bytesLoaded: i,
285
+ bytesTotal: e,
286
+ pendingFiles: this.pendingFiles,
287
+ uploadHandles: this.uploadHandles
288
+ });
289
+ }, /**
290
+ * Retrieves the upload URL from the form's attributes.
291
+ *
292
+ * @returns {string | null} The upload URL, or null if not found.
293
+ */
294
+ H = function() {
295
+ if (this.elForm) {
296
+ const e = this.elForm.getAttribute("action");
297
+ return this.elForm.getAttribute("data-upload-url") || e + "/file";
298
+ }
299
+ return null;
300
+ }, /**
301
+ * Handles the form's change event, adding files to the pending files list.
302
+ *
303
+ * @param {Event} ev - The change event.
304
+ */
305
+ R = function(e) {
306
+ const i = e.target;
307
+ if (i && i.type === "file") {
308
+ const r = i.files;
309
+ if (r != null && r.length)
310
+ for (const o of r)
311
+ this.addFile(i.name, o);
312
+ }
313
+ }, /**
314
+ * Handles the form's submit event, preventing submission until all pending files are uploaded.
315
+ *
316
+ * @param {SubmitEvent} ev - The submit event.
317
+ */
318
+ q = function(e) {
319
+ this.pendingFiles.length && (e.preventDefault(), e.stopPropagation(), this.uploadPendingFiles());
320
+ }, P = async function(e, i) {
321
+ const r = u(this, a, H).call(this);
322
+ if (!r)
323
+ throw new Error("Upload url not specified.");
324
+ const o = {
325
+ "content-type": "application/json"
326
+ };
327
+ i && (o.authorization = "Altcha payload=" + i);
328
+ const c = await fetch(r, {
329
+ body: JSON.stringify({
330
+ name: e.file.name || "file",
331
+ size: e.file.size,
332
+ type: e.file.type || "application/octet-stream"
333
+ }),
334
+ credentials: "include",
335
+ headers: o,
336
+ method: "POST"
337
+ });
338
+ if (c.status === 401)
339
+ return u(this, a, O).call(this, c, e);
340
+ if (c.status !== 200)
341
+ throw new Error(`Unexpected server response ${c.status}.`);
342
+ const s = await c.json();
343
+ let l = e.file;
344
+ if (s.encrypted && s.encryptionPublicKey) {
345
+ const y = await N(s.encryptionPublicKey), M = await new Response(
346
+ new ReadableStream({
347
+ async start(F) {
348
+ const $ = e.file.stream().getReader();
349
+ for (; ; ) {
350
+ const { done: Y, value: D } = await $.read();
351
+ if (Y)
352
+ break;
353
+ F.enqueue(D);
354
+ }
355
+ F.close();
356
+ }
357
+ })
358
+ ).arrayBuffer();
359
+ l = await fe(y, new Uint8Array(M));
360
+ }
361
+ return e.uploadSize = l instanceof Uint8Array ? l.byteLength : e.file.size, await u(this, a, G).call(this, s.uploadUrl, e, l, {
362
+ "content-type": e.file.type || "application/octet-stream"
363
+ }), s.finalizeUrl && await u(this, a, B).call(this, s.finalizeUrl, e.uploadSize), e.fileId = s.fileId, e.resolve({
364
+ encrypted: s.encrypted,
365
+ fileId: s.fileId
366
+ }), e.promise;
367
+ }, O = async function(e, i) {
368
+ var r;
369
+ try {
370
+ const o = e.headers.get("www-authenticate"), c = (r = o == null ? void 0 : o.match(/challenge=(.*),/)) == null ? void 0 : r[1];
371
+ if (!c)
372
+ throw new Error(
373
+ "Unable to retrieve altcha challenge from www-authenticate header."
374
+ );
375
+ const s = JSON.parse(c);
376
+ if (s && "challenge" in s) {
377
+ const { solution: l } = await this.context.solve(s);
378
+ if (l && "number" in l)
379
+ return u(this, a, P).call(this, i, btoa(
380
+ JSON.stringify({
381
+ ...s,
382
+ number: l.number
383
+ })
384
+ ));
385
+ throw new Error("Invalid challenge solution.");
386
+ }
387
+ } catch (o) {
388
+ throw this.context.log(o), new Error("Unable to solve altcha challenge for upload.");
389
+ }
390
+ }, B = async function(e, i) {
391
+ const r = await fetch(e, {
392
+ body: JSON.stringify({
393
+ uploadedBytes: i
394
+ }),
395
+ headers: {
396
+ "content-type": "application/json"
397
+ },
398
+ method: "POST"
399
+ });
400
+ if (r.status > 204)
401
+ throw new Error(`Unexpected server response ${r.status}.`);
402
+ return !0;
403
+ }, G = async function(e, i, r, o = {}) {
404
+ return new Promise((c, s) => {
405
+ const l = new XMLHttpRequest();
406
+ i.controller.signal.addEventListener("abort", () => {
407
+ l.abort();
408
+ }), l.upload.addEventListener("progress", (y) => {
409
+ i.setProgress(y.loaded), u(this, a, E).call(this);
410
+ }), l.addEventListener("error", (y) => {
411
+ s(new Error("Upload failed."));
412
+ }), l.addEventListener("load", () => {
413
+ c(void 0);
414
+ }), l.open("PUT", e);
415
+ for (const y in o)
416
+ l.setRequestHeader(y, o[y]);
417
+ l.send(r);
418
+ });
419
+ }, p(T, "pluginName", "upload");
420
+ class me {
421
+ /**
422
+ * Creates an instance of UploadHandle.
423
+ *
424
+ * @param {string} fieldName - The name of the field associated with the file upload.
425
+ * @param {File} file - The file to be uploaded.
426
+ */
427
+ constructor(n, e) {
428
+ p(this, "controller", new AbortController());
429
+ p(this, "promise");
430
+ p(this, "fileId");
431
+ p(this, "loaded", 0);
432
+ p(this, "progress", 0);
433
+ p(this, "uploadSize", 0);
434
+ p(this, "resolve");
435
+ p(this, "reject");
436
+ this.fieldName = n, this.file = e, this.uploadSize = this.file.size, this.promise = new Promise((i, r) => {
437
+ this.resolve = i, this.reject = r;
438
+ });
439
+ }
440
+ /**
441
+ * Aborts the file upload by invoking the AbortController's abort method.
442
+ */
443
+ abort() {
444
+ this.controller.abort();
445
+ }
446
+ /**
447
+ * Updates the progress of the file upload.
448
+ *
449
+ * @param {number} loaded - The number of bytes that have been uploaded.
450
+ */
451
+ setProgress(n) {
452
+ this.loaded = n, this.progress = this.file.size && n ? Math.min(1, n / this.file.size) : 0;
453
+ }
454
+ }
455
+ A.register(T);
456
+ export {
457
+ T as PluginUpload
458
+ };
@@ -0,0 +1,4 @@
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 me=Object.defineProperty;var j=c=>{throw TypeError(c)};var ge=(c,a,d)=>a in c?me(c,a,{enumerable:!0,configurable:!0,writable:!0,value:d}):c[a]=d;var y=(c,a,d)=>ge(c,typeof a!="symbol"?a+"":a,d),_=(c,a,d)=>a.has(c)||j("Cannot "+d);var b=(c,a,d)=>(_(c,a,"read from private field"),d?d.call(c):a.get(c)),P=(c,a,d)=>a.has(c)?j("Cannot add the same private member more than once"):a instanceof WeakSet?a.add(c):a.set(c,d);var h=(c,a,d)=>(_(c,a,"access private method"),d);var g,w,l,k,H,R,K,O,q,M,v,B,G,$;const a={generateKey:d,exportKey:Y,importKey:D,decrypt:J,encrypt:V};async function d(t=256){return crypto.subtle.generateKey({name:"AES-GCM",length:t},!0,["encrypt","decrypt"])}async function Y(t){return new Uint8Array(await crypto.subtle.exportKey("raw",t))}async function D(t){return crypto.subtle.importKey("raw",t,{name:"AES-GCM"},!0,["encrypt","decrypt"])}async function V(t,n,e=16){const i=crypto.getRandomValues(new Uint8Array(e));return{encrypted:new Uint8Array(await crypto.subtle.encrypt({name:"AES-GCM",iv:i},t,n)),iv:i}}async function J(t,n,e){return new Uint8Array(await crypto.subtle.decrypt({name:"AES-GCM",iv:e},t,n))}function X(t,n=!1){return n&&(t=t.replace(/_/g,"/").replace(/-/g,"+")+"=".repeat(3-(3+t.length)%4)),Uint8Array.from(atob(t),e=>e.charCodeAt(0))}function S(t,n=!1){const e=btoa(String.fromCharCode(...t));return n?e.replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,""):e}function x(t,n=80){let e="";for(;t.length>0;)e+=t.slice(0,n)+`
2
+ `,t=t.slice(n);return e}function L(t){return X(t.split(/\r?\n/).filter(n=>!n.startsWith("-----")).join(""))}const m="RSA-OAEP",E="SHA-256",Q=2048,W=new Uint8Array([1,0,1]),Z={generateKeyPair:ee,encrypt:te,decrypt:ne,exportPrivateKey:C,exportPrivateKeyPem:ie,exportPublicKey:U,exportPublicKeyPem:re,exportPublicKeyFromPrivateKey:oe,importPrivateKey:N,importPrivateKeyPem:se,importPublicKey:I,importPublicKeyPem:T};async function ee(){return crypto.subtle.generateKey({name:m,modulusLength:Q,publicExponent:W,hash:E},!0,["encrypt","decrypt"])}async function te(t,n){return new Uint8Array(await crypto.subtle.encrypt({name:m},t,n))}async function ne(t,n){return new Uint8Array(await crypto.subtle.decrypt({name:m},t,n))}async function U(t){return new Uint8Array(await crypto.subtle.exportKey("spki",t))}async function C(t){return new Uint8Array(await crypto.subtle.exportKey("pkcs8",t))}async function re(t){return`-----BEGIN PUBLIC KEY-----
3
+ `+x(S(await U(t)),64)+"-----END PUBLIC KEY-----"}async function ie(t){return`-----BEGIN PRIVATE KEY-----
4
+ `+x(S(await C(t)),64)+"-----END PRIVATE KEY-----"}async function I(t){return crypto.subtle.importKey("spki",t,{name:m,hash:E},!0,["encrypt"])}async function T(t){return I(L(t))}async function N(t){return crypto.subtle.importKey("pkcs8",t,{name:m,hash:E},!0,["decrypt"])}async function se(t){return N(L(t))}async function oe(t){const n=await crypto.subtle.exportKey("jwk",t);delete n.d,delete n.dp,delete n.dq,delete n.q,delete n.qi,n.key_ops=["encrypt"];const e=await crypto.subtle.importKey("jwk",n,{name:m,hash:E},!0,["encrypt"]);return U(e)}const ae=new Uint8Array([1,0,1]),ce=256,le=16;async function pe(t,n,e={}){const{aesIVLength:i=le,aesKeyLength:r=ce}=e,o=await a.generateKey(r),{encrypted:p,iv:s}=await a.encrypt(o,n,i),u=await Z.encrypt(t,await a.exportKey(o));return new Uint8Array([...ae,...new Uint8Array([u.length]),...new Uint8Array([s.length]),...u,...s,...p])}class A{constructor(n){this.context=n}static register(n){typeof globalThis.altchaPlugins!="object"&&(globalThis.altchaPlugins=[]),globalThis.altchaPlugins.includes(n)||globalThis.altchaPlugins.push(n)}destroy(){}onErrorChange(n){}onStateChange(n){}}y(A,"pluginName");class F extends A{constructor(e){super(e);P(this,l);y(this,"pendingFiles",[]);y(this,"uploadHandles",[]);y(this,"elForm");P(this,g,h(this,l,q).bind(this));P(this,w,h(this,l,M).bind(this));this.elForm=this.context.el.closest("form"),this.elForm&&(this.elForm.addEventListener("change",b(this,g)),this.elForm.addEventListener("submit",b(this,w),{capture:!0}))}addFile(e,i){this.pendingFiles.find(([r,o])=>r===e&&o===i)||this.pendingFiles.push([e,i])}destroy(){this.elForm&&(this.elForm.removeEventListener("change",b(this,g)),this.elForm.removeEventListener("submit",b(this,w)))}async uploadPendingFiles(){var i;const e=async()=>{const r=this.pendingFiles[0];if(r&&await h(this,l,v).call(this,h(this,l,H).call(this,r)),this.pendingFiles.length)return e()};await e(),this.pendingFiles.length===0&&(h(this,l,k).call(this),(i=this.elForm)==null||i.requestSubmit())}}g=new WeakMap,w=new WeakMap,l=new WeakSet,k=function(){var i,r,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(","),(r=(i=this.elForm)==null?void 0:i.querySelector(`[name="${p}"]`))==null||r.setAttribute("disabled","disabled"),(o=this.elForm)==null||o.appendChild(s)}},H=function(e){const i=this.pendingFiles.findIndex(([o,p])=>o===e[0]&&p===e[1]);if(i<0)throw new Error("Cannot create upload handle.");const r=new ue(e[0],e[1]);return this.uploadHandles.push(r),this.pendingFiles.splice(i,1),h(this,l,R).call(this,r),h(this,l,K).call(this),r},R=function(e){this.context.dispatch("upload",{handle:e})},K=function(){const e=this.pendingFiles.reduce((r,[o,p])=>r+p.size,0)+this.uploadHandles.reduce((r,{uploadSize:o})=>r+o,0),i=this.uploadHandles.reduce((r,{loaded:o})=>r+o,0);this.context.dispatch("uploadprogress",{bytesLoaded:i,bytesTotal:e,pendingFiles:this.pendingFiles,uploadHandles:this.uploadHandles})},O=function(){if(this.elForm){const e=this.elForm.getAttribute("action");return this.elForm.getAttribute("data-upload-url")||e+"/file"}return null},q=function(e){const i=e.target;if(i&&i.type==="file"){const r=i.files;if(r!=null&&r.length)for(const o of r)this.addFile(i.name,o)}},M=function(e){this.pendingFiles.length&&(e.preventDefault(),e.stopPropagation(),this.uploadPendingFiles())},v=async function(e,i){const r=h(this,l,O).call(this);if(!r)throw new Error("Upload url not specified.");const o={"content-type":"application/json"};i&&(o.authorization="Altcha payload="+i);const p=await fetch(r,{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 u=e.file;if(s.encrypted&&s.encryptionPublicKey){const f=await T(s.encryptionPublicKey),de=await new Response(new ReadableStream({async start(z){const ye=e.file.stream().getReader();for(;;){const{done:he,value:fe}=await ye.read();if(he)break;z.enqueue(fe)}z.close()}})).arrayBuffer();u=await pe(f,new Uint8Array(de))}return e.uploadSize=u instanceof Uint8Array?u.byteLength:e.file.size,await h(this,l,$).call(this,s.uploadUrl,e,u,{"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,i){var r;try{const o=e.headers.get("www-authenticate"),p=(r=o==null?void 0:o.match(/challenge=(.*),/))==null?void 0:r[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:u}=await this.context.solve(s);if(u&&"number"in u)return h(this,l,v).call(this,i,btoa(JSON.stringify({...s,number:u.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,i){const r=await fetch(e,{body:JSON.stringify({uploadedBytes:i}),headers:{"content-type":"application/json"},method:"POST"});if(r.status>204)throw new Error(`Unexpected server response ${r.status}.`);return!0},$=async function(e,i,r,o={}){return new Promise((p,s)=>{const u=new XMLHttpRequest;i.controller.signal.addEventListener("abort",()=>{u.abort()}),u.upload.addEventListener("progress",f=>{i.setProgress(f.loaded),h(this,l,K).call(this)}),u.addEventListener("error",f=>{s(new Error("Upload failed."))}),u.addEventListener("load",()=>{p(void 0)}),u.open("PUT",e);for(const f in o)u.setRequestHeader(f,o[f]);u.send(r)})},y(F,"pluginName","upload");class ue{constructor(n,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=n,this.file=e,this.uploadSize=this.file.size,this.promise=new Promise((i,r)=>{this.resolve=i,this.reject=r})}abort(){this.controller.abort()}setProgress(n){this.loaded=n,this.progress=this.file.size&&n?Math.min(1,n/this.file.size):0}}A.register(F),c.PluginUpload=F,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "altcha",
3
3
  "description": "GDPR compliant, self-hosted CAPTCHA alternative.",
4
- "version": "0.7.0",
4
+ "version": "0.9.0",
5
5
  "license": "MIT",
6
6
  "author": {
7
7
  "name": "Daniel Regeci",
@@ -28,7 +28,8 @@
28
28
  ],
29
29
  "files": [
30
30
  "dist",
31
- "dist_external"
31
+ "dist_external",
32
+ "dist_plugins"
32
33
  ],
33
34
  "main": "./dist/altcha.umd.cjs",
34
35
  "module": "./dist/altcha.js",
@@ -41,20 +42,33 @@
41
42
  "import": "./dist_external/altcha.css",
42
43
  "require": "./dist_external/altcha.css"
43
44
  },
44
- "./external/altcha.js": {
45
+ "./analytics": {
46
+ "import": "./dist_plugins/analytics.js",
47
+ "require": "./dist_plugins/analytics.umd.cjs"
48
+ },
49
+ "./obfuscation": {
50
+ "import": "./dist_plugins/obfuscation.js",
51
+ "require": "./dist_plugins/obfuscation.umd.cjs"
52
+ },
53
+ "./upload": {
54
+ "import": "./dist_plugins/upload.js",
55
+ "require": "./dist_plugins/upload.umd.cjs"
56
+ },
57
+ "./altcha.ext": {
45
58
  "import": "./dist_external/altcha.js",
46
59
  "require": "./dist_external/altcha.umd.cjs"
47
60
  },
48
- "./external/worker.js": {
61
+ "./worker": {
49
62
  "import": "./dist_external/worker.js",
50
63
  "require": "./dist_external/worker.js"
51
64
  }
52
65
  },
53
66
  "scripts": {
54
67
  "dev": "vite",
55
- "build": "npm run build:bundle && npm run build:external",
68
+ "build": "npm run build:bundle && npm run build:external && npm run build:plugins",
56
69
  "build:bundle": "rimraf dist && vite build && cp src/declarations.d.ts dist/altcha.d.ts",
57
- "build:external": "rimraf dist_external && BUILD_EXTERNAL=1 vite build && cp src/declarations.d.ts dist_external/altcha.d.ts",
70
+ "build:external": "rimraf dist_external && vite build -c vite.external.config.ts && cp src/declarations.d.ts dist_external/altcha.d.ts",
71
+ "build:plugins": "rimraf dist_plugins && find src/plugins -type f -name '*.ts' | xargs -I {} vite build -c vite.plugins.config.ts -- {}",
58
72
  "preview": "vite preview",
59
73
  "check": "svelte-check --tsconfig ./tsconfig.json",
60
74
  "format": "prettier --write ./src/**/*",
@@ -63,23 +77,26 @@
63
77
  "prepare": "husky"
64
78
  },
65
79
  "devDependencies": {
66
- "@playwright/test": "^1.44.1",
67
- "@sveltejs/vite-plugin-svelte": "^3.0.2",
68
- "@tsconfig/svelte": "^5.0.0",
69
- "@types/node": "^20.11.19",
70
- "body-parser": "^1.20.2",
71
- "cors": "^2.8.5",
72
- "express": "^4.18.2",
73
- "husky": "^9.0.11",
80
+ "@playwright/test": "^1.46.1",
81
+ "@sveltejs/vite-plugin-svelte": "^3.1.1",
82
+ "@tsconfig/svelte": "^5.0.4",
83
+ "@types/node": "^20.15.0",
84
+ "husky": "^9.1.4",
74
85
  "prettier": "3.2.5",
75
- "prettier-plugin-svelte": "^3.2.3",
86
+ "prettier-plugin-svelte": "^3.2.6",
76
87
  "rollup-plugin-css-only": "^4.5.2",
77
- "sass": "^1.71.1",
78
- "svelte": "^4.2.11",
79
- "svelte-check": "^3.4.6",
80
- "tslib": "^2.6.0",
81
- "typescript": "^5.0.2",
82
- "vite": "^5.3.0",
88
+ "sass": "^1.77.8",
89
+ "svelte": "^4.2.18",
90
+ "svelte-check": "^3.8.5",
91
+ "tslib": "^2.6.3",
92
+ "typescript": "^5.5.4",
93
+ "vite": "^5.4.1",
83
94
  "vitest": "^0.34.6"
84
- }
95
+ },
96
+ "dependencies": {
97
+ "@altcha/crypto": "^0.0.1"
98
+ },
99
+ "optionalDependencies": {
100
+ "@rollup/rollup-linux-x64-gnu": "4.18.0"
101
+ }
85
102
  }