@drmhse/authos-vue 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/dist/index.mjs ADDED
@@ -0,0 +1,511 @@
1
+ import { AUTH_OS_INJECTION_KEY, useAuthOS } from './chunk-OGRUDANK.mjs';
2
+ export { AUTH_OS_INJECTION_KEY, useAuthOS } from './chunk-OGRUDANK.mjs';
3
+ import './chunk-6DZX6EAA.mjs';
4
+ import { defineComponent, reactive, provide, onMounted, onUnmounted, h, ref, computed, inject } from 'vue';
5
+ import { SsoClient, BrowserStorage, MemoryStorage, SsoApiError } from '@drmhse/sso-sdk';
6
+ export { AuthErrorCodes, BrowserStorage, MemoryStorage, SsoApiError } from '@drmhse/sso-sdk';
7
+
8
+ function createAuthOS(options) {
9
+ const getStorage = () => {
10
+ if (options.storage) return options.storage;
11
+ try {
12
+ if (typeof window !== "undefined" && window.localStorage) {
13
+ return new BrowserStorage();
14
+ }
15
+ } catch {
16
+ }
17
+ return new MemoryStorage();
18
+ };
19
+ const client = new SsoClient({
20
+ baseURL: options.baseUrl,
21
+ storage: getStorage()
22
+ });
23
+ const state = reactive({
24
+ user: null,
25
+ isAuthenticated: false,
26
+ isLoading: true,
27
+ currentOrganization: null,
28
+ organizations: []
29
+ });
30
+ const context = {
31
+ client,
32
+ state
33
+ };
34
+ return {
35
+ install(app) {
36
+ client.onAuthStateChange(async (isAuthenticated) => {
37
+ state.isAuthenticated = isAuthenticated;
38
+ state.isLoading = false;
39
+ if (isAuthenticated) {
40
+ try {
41
+ const profile = await client.user.getProfile();
42
+ state.user = profile;
43
+ } catch {
44
+ state.user = null;
45
+ }
46
+ try {
47
+ const orgs = await client.organizations.list();
48
+ state.organizations = orgs;
49
+ if (orgs.length > 0 && !state.currentOrganization) {
50
+ state.currentOrganization = orgs[0];
51
+ }
52
+ } catch {
53
+ state.organizations = [];
54
+ }
55
+ } else {
56
+ state.user = null;
57
+ state.currentOrganization = null;
58
+ state.organizations = [];
59
+ }
60
+ });
61
+ app.provide(AUTH_OS_INJECTION_KEY, context);
62
+ app.config.globalProperties.$authOS = context;
63
+ }
64
+ };
65
+ }
66
+ function useUser() {
67
+ const context = inject(AUTH_OS_INJECTION_KEY);
68
+ if (!context) {
69
+ return {
70
+ user: ref(null),
71
+ isLoading: computed(() => false)
72
+ };
73
+ }
74
+ const user = computed(() => context.state.user);
75
+ const isLoading = computed(() => context.state.isLoading);
76
+ return {
77
+ user,
78
+ isLoading
79
+ };
80
+ }
81
+ function useOrganization() {
82
+ const context = inject(AUTH_OS_INJECTION_KEY);
83
+ if (!context) {
84
+ return {
85
+ currentOrganization: ref(null),
86
+ organizations: ref([]),
87
+ switchOrganization: async () => null,
88
+ isSwitching: ref(false)
89
+ };
90
+ }
91
+ const currentOrganization = computed(() => context.state.currentOrganization);
92
+ const organizations = computed(() => context.state.organizations);
93
+ const isSwitching = ref(false);
94
+ async function switchOrganization(slug) {
95
+ if (!context) return;
96
+ isSwitching.value = true;
97
+ try {
98
+ const orgResponse = await context.client.organizations.get(slug);
99
+ context.state.currentOrganization = orgResponse;
100
+ return orgResponse;
101
+ } finally {
102
+ isSwitching.value = false;
103
+ }
104
+ }
105
+ return {
106
+ currentOrganization,
107
+ organizations,
108
+ switchOrganization,
109
+ isSwitching
110
+ };
111
+ }
112
+ var AuthOSProvider = defineComponent({
113
+ name: "AuthOSProvider",
114
+ props: {
115
+ baseUrl: {
116
+ type: String,
117
+ required: true
118
+ },
119
+ storage: {
120
+ type: Object,
121
+ default: void 0
122
+ },
123
+ client: {
124
+ type: Object,
125
+ default: void 0
126
+ }
127
+ },
128
+ setup(props, { slots }) {
129
+ const getStorage = () => {
130
+ if (props.storage) return props.storage;
131
+ if (typeof window !== "undefined") return new BrowserStorage();
132
+ return new MemoryStorage();
133
+ };
134
+ const client = props.client ?? new SsoClient({
135
+ baseURL: props.baseUrl,
136
+ storage: getStorage()
137
+ });
138
+ const state = reactive({
139
+ user: null,
140
+ isAuthenticated: false,
141
+ isLoading: true,
142
+ currentOrganization: null,
143
+ organizations: []
144
+ });
145
+ const context = { client, state };
146
+ provide(AUTH_OS_INJECTION_KEY, context);
147
+ let unsubscribe;
148
+ onMounted(() => {
149
+ unsubscribe = client.onAuthStateChange(async (isAuthenticated) => {
150
+ state.isAuthenticated = isAuthenticated;
151
+ state.isLoading = false;
152
+ if (isAuthenticated) {
153
+ try {
154
+ const profile = await client.user.getProfile();
155
+ state.user = profile;
156
+ } catch {
157
+ state.user = null;
158
+ }
159
+ try {
160
+ const orgs = await client.organizations.list();
161
+ state.organizations = orgs;
162
+ if (orgs.length > 0 && !state.currentOrganization) {
163
+ state.currentOrganization = orgs[0];
164
+ }
165
+ } catch {
166
+ state.organizations = [];
167
+ }
168
+ } else {
169
+ state.user = null;
170
+ state.currentOrganization = null;
171
+ state.organizations = [];
172
+ }
173
+ });
174
+ });
175
+ onUnmounted(() => {
176
+ unsubscribe?.();
177
+ });
178
+ return () => slots.default ? slots.default() : h("div");
179
+ }
180
+ });
181
+ var MFA_PREAUTH_EXPIRY = 300;
182
+ var SignIn = defineComponent({
183
+ name: "SignIn",
184
+ props: {
185
+ onSuccess: {
186
+ type: Function,
187
+ default: void 0
188
+ },
189
+ onError: {
190
+ type: Function,
191
+ default: void 0
192
+ }
193
+ },
194
+ emits: ["success", "error"],
195
+ setup(props, { slots, emit }) {
196
+ const { client } = useAuthOS();
197
+ const email = ref("");
198
+ const password = ref("");
199
+ const mfaCode = ref("");
200
+ const preauthToken = ref("");
201
+ const step = ref("credentials");
202
+ const error = ref(null);
203
+ const isSubmitting = ref(false);
204
+ async function submit() {
205
+ error.value = null;
206
+ isSubmitting.value = true;
207
+ try {
208
+ if (step.value === "credentials") {
209
+ const result = await client.auth.login({
210
+ email: email.value,
211
+ password: password.value
212
+ });
213
+ if (result.expires_in === MFA_PREAUTH_EXPIRY) {
214
+ preauthToken.value = result.access_token;
215
+ step.value = "mfa";
216
+ } else {
217
+ emit("success");
218
+ props.onSuccess?.();
219
+ }
220
+ } else {
221
+ await client.auth.verifyMfa(preauthToken.value, mfaCode.value);
222
+ emit("success");
223
+ props.onSuccess?.();
224
+ }
225
+ } catch (err) {
226
+ const message = err instanceof SsoApiError ? err.message : "Login failed";
227
+ error.value = message;
228
+ const e = err instanceof Error ? err : new Error(message);
229
+ emit("error", e);
230
+ props.onError?.(e);
231
+ } finally {
232
+ isSubmitting.value = false;
233
+ }
234
+ }
235
+ return () => {
236
+ const slotProps = {
237
+ email: email.value,
238
+ password: password.value,
239
+ mfaCode: mfaCode.value,
240
+ step: step.value,
241
+ error: error.value,
242
+ isSubmitting: isSubmitting.value,
243
+ updateEmail: (v) => email.value = v,
244
+ updatePassword: (v) => password.value = v,
245
+ updateMfaCode: (v) => mfaCode.value = v,
246
+ submit
247
+ };
248
+ if (slots.default) {
249
+ return slots.default(slotProps);
250
+ }
251
+ return h("form", { onSubmit: (e) => {
252
+ e.preventDefault();
253
+ submit();
254
+ } }, [
255
+ step.value === "credentials" ? [
256
+ h("input", {
257
+ type: "email",
258
+ value: email.value,
259
+ placeholder: "Email",
260
+ onInput: (e) => email.value = e.target.value
261
+ }),
262
+ h("input", {
263
+ type: "password",
264
+ value: password.value,
265
+ placeholder: "Password",
266
+ onInput: (e) => password.value = e.target.value
267
+ })
268
+ ] : h("input", {
269
+ type: "text",
270
+ value: mfaCode.value,
271
+ placeholder: "MFA Code",
272
+ onInput: (e) => mfaCode.value = e.target.value
273
+ }),
274
+ error.value && h("p", { style: "color: red" }, error.value),
275
+ h("button", { type: "submit", disabled: isSubmitting.value }, isSubmitting.value ? "Signing in..." : "Sign In")
276
+ ]);
277
+ };
278
+ }
279
+ });
280
+ var SignUp = defineComponent({
281
+ name: "SignUp",
282
+ props: {
283
+ onSuccess: {
284
+ type: Function,
285
+ default: void 0
286
+ },
287
+ onError: {
288
+ type: Function,
289
+ default: void 0
290
+ }
291
+ },
292
+ emits: ["success", "error"],
293
+ setup(props, { slots, emit }) {
294
+ const { client } = useAuthOS();
295
+ const email = ref("");
296
+ const password = ref("");
297
+ const error = ref(null);
298
+ const isSubmitting = ref(false);
299
+ async function submit() {
300
+ error.value = null;
301
+ isSubmitting.value = true;
302
+ try {
303
+ await client.auth.register({
304
+ email: email.value,
305
+ password: password.value
306
+ });
307
+ emit("success");
308
+ props.onSuccess?.();
309
+ } catch (err) {
310
+ const message = err instanceof SsoApiError ? err.message : "Registration failed";
311
+ error.value = message;
312
+ const e = err instanceof Error ? err : new Error(message);
313
+ emit("error", e);
314
+ props.onError?.(e);
315
+ } finally {
316
+ isSubmitting.value = false;
317
+ }
318
+ }
319
+ return () => {
320
+ const slotProps = {
321
+ email: email.value,
322
+ password: password.value,
323
+ error: error.value,
324
+ isSubmitting: isSubmitting.value,
325
+ updateEmail: (v) => email.value = v,
326
+ updatePassword: (v) => password.value = v,
327
+ submit
328
+ };
329
+ if (slots.default) {
330
+ return slots.default(slotProps);
331
+ }
332
+ return h("form", { onSubmit: (e) => {
333
+ e.preventDefault();
334
+ submit();
335
+ } }, [
336
+ h("input", {
337
+ type: "email",
338
+ value: email.value,
339
+ placeholder: "Email",
340
+ onInput: (e) => email.value = e.target.value
341
+ }),
342
+ h("input", {
343
+ type: "password",
344
+ value: password.value,
345
+ placeholder: "Password",
346
+ onInput: (e) => password.value = e.target.value
347
+ }),
348
+ error.value && h("p", { style: "color: red" }, error.value),
349
+ h("button", { type: "submit", disabled: isSubmitting.value }, isSubmitting.value ? "Creating account..." : "Sign Up")
350
+ ]);
351
+ };
352
+ }
353
+ });
354
+ var OrganizationSwitcher = defineComponent({
355
+ name: "OrganizationSwitcher",
356
+ props: {
357
+ onSwitch: {
358
+ type: Function,
359
+ default: void 0
360
+ }
361
+ },
362
+ emits: ["switch"],
363
+ setup(props, { slots, emit }) {
364
+ const { currentOrganization, organizations, switchOrganization, isSwitching } = useOrganization();
365
+ async function switchTo(slug) {
366
+ await switchOrganization(slug);
367
+ const org = organizations.value.find((o) => o.organization.slug === slug);
368
+ if (org) {
369
+ emit("switch", org);
370
+ props.onSwitch?.(org);
371
+ }
372
+ }
373
+ return () => {
374
+ const slotProps = {
375
+ currentOrganization: currentOrganization.value,
376
+ organizations: organizations.value,
377
+ isSwitching: isSwitching.value,
378
+ switchTo
379
+ };
380
+ if (slots.default) {
381
+ return slots.default(slotProps);
382
+ }
383
+ return h(
384
+ "div",
385
+ { "data-authos-orgswitcher": "", "data-state": organizations.value.length > 0 ? "ready" : "empty" },
386
+ organizations.value.length > 0 ? h(
387
+ "select",
388
+ {
389
+ value: currentOrganization.value?.organization.slug ?? "",
390
+ disabled: isSwitching.value,
391
+ onChange: (e) => switchTo(e.target.value)
392
+ },
393
+ organizations.value.map(
394
+ (org) => h("option", { key: org.organization.id, value: org.organization.slug }, org.organization.name)
395
+ )
396
+ ) : "No organizations"
397
+ );
398
+ };
399
+ }
400
+ });
401
+ var UserButton = defineComponent({
402
+ name: "UserButton",
403
+ props: {
404
+ onLogout: {
405
+ type: Function,
406
+ default: void 0
407
+ }
408
+ },
409
+ emits: ["logout"],
410
+ setup(props, { slots, emit }) {
411
+ const { user, isLoading } = useUser();
412
+ const { client } = useAuthOS();
413
+ const isLoggingOut = ref(false);
414
+ async function logout() {
415
+ isLoggingOut.value = true;
416
+ try {
417
+ await client.auth.logout();
418
+ emit("logout");
419
+ props.onLogout?.();
420
+ } finally {
421
+ isLoggingOut.value = false;
422
+ }
423
+ }
424
+ return () => {
425
+ const slotProps = {
426
+ user: user.value,
427
+ isLoading: isLoading.value,
428
+ isLoggingOut: isLoggingOut.value,
429
+ logout
430
+ };
431
+ if (slots.default) {
432
+ return slots.default(slotProps);
433
+ }
434
+ if (isLoading.value) {
435
+ return h("div", { "data-authos-userbutton": "", "data-state": "loading" }, "Loading...");
436
+ }
437
+ if (!user.value) {
438
+ return h("div", { "data-authos-userbutton": "", "data-state": "signed-out" }, "Not signed in");
439
+ }
440
+ return h("div", { "data-authos-userbutton": "", "data-state": "signed-in", style: "display: flex; align-items: center; gap: 8px;" }, [
441
+ h("span", user.value.email),
442
+ h(
443
+ "button",
444
+ {
445
+ onClick: logout,
446
+ disabled: isLoggingOut.value
447
+ },
448
+ isLoggingOut.value ? "Logging out..." : "Logout"
449
+ )
450
+ ]);
451
+ };
452
+ }
453
+ });
454
+ var Protect = defineComponent({
455
+ name: "Protect",
456
+ props: {
457
+ permission: {
458
+ type: String,
459
+ default: void 0
460
+ },
461
+ permissions: {
462
+ type: Array,
463
+ default: void 0
464
+ },
465
+ requireAll: {
466
+ type: Boolean,
467
+ default: false
468
+ },
469
+ fallback: {
470
+ type: [Object, Function],
471
+ default: void 0
472
+ }
473
+ },
474
+ setup(props, { slots }) {
475
+ const { user, isLoading } = useUser();
476
+ const hasAccess = computed(() => {
477
+ if (isLoading.value || !user.value) {
478
+ return false;
479
+ }
480
+ const userPermissions = user.value.permissions ?? [];
481
+ if (props.permission) {
482
+ return userPermissions.includes(props.permission);
483
+ }
484
+ if (props.permissions && props.permissions.length > 0) {
485
+ if (props.requireAll) {
486
+ return props.permissions.every((p) => userPermissions.includes(p));
487
+ }
488
+ return props.permissions.some((p) => userPermissions.includes(p));
489
+ }
490
+ return true;
491
+ });
492
+ return () => {
493
+ if (isLoading.value) {
494
+ return h("div", { "data-authos-protect": "", "data-state": "loading" });
495
+ }
496
+ if (hasAccess.value) {
497
+ return h("div", { "data-authos-protect": "", "data-state": "allowed" }, slots.default?.());
498
+ }
499
+ if (props.fallback) {
500
+ const fallbackContent = typeof props.fallback === "function" ? props.fallback() : props.fallback;
501
+ return h("div", { "data-authos-protect": "", "data-state": "denied" }, [fallbackContent]);
502
+ }
503
+ if (slots.fallback) {
504
+ return h("div", { "data-authos-protect": "", "data-state": "denied" }, slots.fallback());
505
+ }
506
+ return h("div", { "data-authos-protect": "", "data-state": "denied" });
507
+ };
508
+ }
509
+ });
510
+
511
+ export { AuthOSProvider, OrganizationSwitcher, Protect, SignIn, SignUp, UserButton, createAuthOS, useOrganization, useUser };
@@ -0,0 +1,3 @@
1
+ export { Cu as parseJSON5, Au as stringifyJSON5 } from './chunk-VCTSSS2F.mjs';
2
+ import './chunk-JLL4L3HM.mjs';
3
+ import './chunk-6DZX6EAA.mjs';
@@ -0,0 +1,3 @@
1
+ export { h as parseJSONC, d as stringifyJSONC } from './chunk-SW2YRXFK.mjs';
2
+ import './chunk-JLL4L3HM.mjs';
3
+ import './chunk-6DZX6EAA.mjs';
@@ -0,0 +1,173 @@
1
+ import { require_node, require_node_fetch_native_DhEqb06g } from './chunk-XLWXQZHO.mjs';
2
+ import { __commonJS, __require } from './chunk-6DZX6EAA.mjs';
3
+
4
+ // node_modules/node-fetch-native/dist/chunks/multipart-parser.cjs
5
+ var require_multipart_parser = __commonJS({
6
+ "node_modules/node-fetch-native/dist/chunks/multipart-parser.cjs"(exports$1) {
7
+ var y = Object.defineProperty;
8
+ var c = (R, o) => y(R, "name", { value: o, configurable: true });
9
+ var node = require_node();
10
+ __require("http"), __require("https"), __require("zlib"), __require("stream"), __require("buffer"), __require("util"), require_node_fetch_native_DhEqb06g(), __require("url"), __require("net"), __require("fs"), __require("path");
11
+ var s = 0;
12
+ var S = { START_BOUNDARY: s++, HEADER_FIELD_START: s++, HEADER_FIELD: s++, HEADER_VALUE_START: s++, HEADER_VALUE: s++, HEADER_VALUE_ALMOST_DONE: s++, HEADERS_ALMOST_DONE: s++, PART_DATA_START: s++, PART_DATA: s++, END: s++ };
13
+ var f = 1;
14
+ var F = { PART_BOUNDARY: f, LAST_BOUNDARY: f *= 2 };
15
+ var LF = 10;
16
+ var CR = 13;
17
+ var SPACE = 32;
18
+ var HYPHEN = 45;
19
+ var COLON = 58;
20
+ var A = 97;
21
+ var Z = 122;
22
+ var lower = c((R) => R | 32, "lower");
23
+ var noop = c(() => {
24
+ }, "noop");
25
+ var g = class g {
26
+ constructor(o) {
27
+ this.index = 0, this.flags = 0, this.onHeaderEnd = noop, this.onHeaderField = noop, this.onHeadersEnd = noop, this.onHeaderValue = noop, this.onPartBegin = noop, this.onPartData = noop, this.onPartEnd = noop, this.boundaryChars = {}, o = `\r
28
+ --` + o;
29
+ const t = new Uint8Array(o.length);
30
+ for (let n = 0; n < o.length; n++) t[n] = o.charCodeAt(n), this.boundaryChars[t[n]] = true;
31
+ this.boundary = t, this.lookbehind = new Uint8Array(this.boundary.length + 8), this.state = S.START_BOUNDARY;
32
+ }
33
+ write(o) {
34
+ let t = 0;
35
+ const n = o.length;
36
+ let E = this.index, { lookbehind: l, boundary: h, boundaryChars: H, index: e, state: a, flags: d } = this;
37
+ const b = this.boundary.length, m = b - 1, O = o.length;
38
+ let r, P;
39
+ const u = c((D) => {
40
+ this[D + "Mark"] = t;
41
+ }, "mark"), i = c((D) => {
42
+ delete this[D + "Mark"];
43
+ }, "clear"), T = c((D, p, _, N) => {
44
+ (p === void 0 || p !== _) && this[D](N && N.subarray(p, _));
45
+ }, "callback"), L = c((D, p) => {
46
+ const _ = D + "Mark";
47
+ _ in this && (p ? (T(D, this[_], t, o), delete this[_]) : (T(D, this[_], o.length, o), this[_] = 0));
48
+ }, "dataCallback");
49
+ for (t = 0; t < n; t++) switch (r = o[t], a) {
50
+ case S.START_BOUNDARY:
51
+ if (e === h.length - 2) {
52
+ if (r === HYPHEN) d |= F.LAST_BOUNDARY;
53
+ else if (r !== CR) return;
54
+ e++;
55
+ break;
56
+ } else if (e - 1 === h.length - 2) {
57
+ if (d & F.LAST_BOUNDARY && r === HYPHEN) a = S.END, d = 0;
58
+ else if (!(d & F.LAST_BOUNDARY) && r === LF) e = 0, T("onPartBegin"), a = S.HEADER_FIELD_START;
59
+ else return;
60
+ break;
61
+ }
62
+ r !== h[e + 2] && (e = -2), r === h[e + 2] && e++;
63
+ break;
64
+ case S.HEADER_FIELD_START:
65
+ a = S.HEADER_FIELD, u("onHeaderField"), e = 0;
66
+ case S.HEADER_FIELD:
67
+ if (r === CR) {
68
+ i("onHeaderField"), a = S.HEADERS_ALMOST_DONE;
69
+ break;
70
+ }
71
+ if (e++, r === HYPHEN) break;
72
+ if (r === COLON) {
73
+ if (e === 1) return;
74
+ L("onHeaderField", true), a = S.HEADER_VALUE_START;
75
+ break;
76
+ }
77
+ if (P = lower(r), P < A || P > Z) return;
78
+ break;
79
+ case S.HEADER_VALUE_START:
80
+ if (r === SPACE) break;
81
+ u("onHeaderValue"), a = S.HEADER_VALUE;
82
+ case S.HEADER_VALUE:
83
+ r === CR && (L("onHeaderValue", true), T("onHeaderEnd"), a = S.HEADER_VALUE_ALMOST_DONE);
84
+ break;
85
+ case S.HEADER_VALUE_ALMOST_DONE:
86
+ if (r !== LF) return;
87
+ a = S.HEADER_FIELD_START;
88
+ break;
89
+ case S.HEADERS_ALMOST_DONE:
90
+ if (r !== LF) return;
91
+ T("onHeadersEnd"), a = S.PART_DATA_START;
92
+ break;
93
+ case S.PART_DATA_START:
94
+ a = S.PART_DATA, u("onPartData");
95
+ case S.PART_DATA:
96
+ if (E = e, e === 0) {
97
+ for (t += m; t < O && !(o[t] in H); ) t += b;
98
+ t -= m, r = o[t];
99
+ }
100
+ if (e < h.length) h[e] === r ? (e === 0 && L("onPartData", true), e++) : e = 0;
101
+ else if (e === h.length) e++, r === CR ? d |= F.PART_BOUNDARY : r === HYPHEN ? d |= F.LAST_BOUNDARY : e = 0;
102
+ else if (e - 1 === h.length) if (d & F.PART_BOUNDARY) {
103
+ if (e = 0, r === LF) {
104
+ d &= ~F.PART_BOUNDARY, T("onPartEnd"), T("onPartBegin"), a = S.HEADER_FIELD_START;
105
+ break;
106
+ }
107
+ } else d & F.LAST_BOUNDARY && r === HYPHEN ? (T("onPartEnd"), a = S.END, d = 0) : e = 0;
108
+ if (e > 0) l[e - 1] = r;
109
+ else if (E > 0) {
110
+ const D = new Uint8Array(l.buffer, l.byteOffset, l.byteLength);
111
+ T("onPartData", 0, E, D), E = 0, u("onPartData"), t--;
112
+ }
113
+ break;
114
+ case S.END:
115
+ break;
116
+ default:
117
+ throw new Error(`Unexpected state entered: ${a}`);
118
+ }
119
+ L("onHeaderField"), L("onHeaderValue"), L("onPartData"), this.index = e, this.state = a, this.flags = d;
120
+ }
121
+ end() {
122
+ if (this.state === S.HEADER_FIELD_START && this.index === 0 || this.state === S.PART_DATA && this.index === this.boundary.length) this.onPartEnd();
123
+ else if (this.state !== S.END) throw new Error("MultipartParser.end(): stream ended unexpectedly");
124
+ }
125
+ };
126
+ c(g, "MultipartParser");
127
+ var MultipartParser = g;
128
+ function _fileName(R) {
129
+ const o = R.match(/\bfilename=("(.*?)"|([^()<>@,;:\\"/[\]?={}\s\t]+))($|;\s)/i);
130
+ if (!o) return;
131
+ const t = o[2] || o[3] || "";
132
+ let n = t.slice(t.lastIndexOf("\\") + 1);
133
+ return n = n.replace(/%22/g, '"'), n = n.replace(/&#(\d{4});/g, (E, l) => String.fromCharCode(l)), n;
134
+ }
135
+ c(_fileName, "_fileName");
136
+ async function toFormData(R, o) {
137
+ if (!/multipart/i.test(o)) throw new TypeError("Failed to fetch");
138
+ const t = o.match(/boundary=(?:"([^"]+)"|([^;]+))/i);
139
+ if (!t) throw new TypeError("no or bad content-type header, no multipart boundary");
140
+ const n = new MultipartParser(t[1] || t[2]);
141
+ let E, l, h, H, e, a;
142
+ const d = [], b = new node.FormData(), m = c((i) => {
143
+ h += u.decode(i, { stream: true });
144
+ }, "onPartData"), O = c((i) => {
145
+ d.push(i);
146
+ }, "appendToFile"), r = c(() => {
147
+ const i = new node.File(d, a, { type: e });
148
+ b.append(H, i);
149
+ }, "appendFileToFormData"), P = c(() => {
150
+ b.append(H, h);
151
+ }, "appendEntryToFormData"), u = new TextDecoder("utf-8");
152
+ u.decode(), n.onPartBegin = function() {
153
+ n.onPartData = m, n.onPartEnd = P, E = "", l = "", h = "", H = "", e = "", a = null, d.length = 0;
154
+ }, n.onHeaderField = function(i) {
155
+ E += u.decode(i, { stream: true });
156
+ }, n.onHeaderValue = function(i) {
157
+ l += u.decode(i, { stream: true });
158
+ }, n.onHeaderEnd = function() {
159
+ if (l += u.decode(), E = E.toLowerCase(), E === "content-disposition") {
160
+ const i = l.match(/\bname=("([^"]*)"|([^()<>@,;:\\"/[\]?={}\s\t]+))/i);
161
+ i && (H = i[2] || i[3] || ""), a = _fileName(l), a && (n.onPartData = O, n.onPartEnd = r);
162
+ } else E === "content-type" && (e = l);
163
+ l = "", E = "";
164
+ };
165
+ for await (const i of R) n.write(i);
166
+ return n.end(), b;
167
+ }
168
+ c(toFormData, "toFormData"), exports$1.toFormData = toFormData;
169
+ }
170
+ });
171
+ var multipartParserZINPIK62 = require_multipart_parser();
172
+
173
+ export { multipartParserZINPIK62 as default };