vitral 0.1.0 → 0.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.
Files changed (63) hide show
  1. package/README.md +250 -217
  2. package/dist/angular.d.ts +16 -4
  3. package/dist/angular.js +26 -5
  4. package/dist/auto.d.ts +2 -0
  5. package/dist/auto.js +13 -0
  6. package/dist/capture-JYFERAPL.js +42 -0
  7. package/dist/chunk-3YXI3G34.js +181 -0
  8. package/dist/chunk-AH7KNMBW.js +20 -0
  9. package/dist/chunk-AKNNHQ2H.js +45 -0
  10. package/dist/chunk-DXORSF4X.js +42 -0
  11. package/dist/chunk-GNHCZRQP.js +79 -0
  12. package/dist/chunk-GNPFWISJ.js +80 -0
  13. package/dist/chunk-I3VSKSAZ.js +634 -0
  14. package/dist/chunk-IHTUM6LT.js +152 -0
  15. package/dist/chunk-SDB5AZEI.js +51 -0
  16. package/dist/chunk-TD4S3IHA.js +201 -0
  17. package/dist/chunk-ZAC6QQI6.js +64 -0
  18. package/dist/cli.d.ts +1 -0
  19. package/dist/cli.js +138 -0
  20. package/dist/contract-46EZFQPQ.js +17 -0
  21. package/dist/dedupe-S7BHQMOZ.js +82 -0
  22. package/dist/devtools.d.ts +12 -0
  23. package/dist/devtools.js +86 -0
  24. package/dist/element.d.ts +60 -2
  25. package/dist/element.js +10 -2
  26. package/dist/errors-kjBv6IH_.d.ts +17 -0
  27. package/dist/events-D5gs7rff.d.ts +6 -0
  28. package/dist/expose-JUOUN5N5.js +15 -0
  29. package/dist/host-slot-Cz6EiGsB.d.ts +6 -0
  30. package/dist/iframe.d.ts +28 -0
  31. package/dist/iframe.js +239 -0
  32. package/dist/index.d.ts +144 -147
  33. package/dist/index.js +40 -10
  34. package/dist/navigate-BRG1WJUM.d.ts +9 -0
  35. package/dist/otel.d.ts +44 -0
  36. package/dist/otel.js +27 -0
  37. package/dist/overrides-NZAIUGK5.js +15 -0
  38. package/dist/preact.d.ts +30 -7
  39. package/dist/preact.js +38 -9
  40. package/dist/react.d.ts +46 -7
  41. package/dist/react.js +97 -42
  42. package/dist/routes.d.ts +56 -0
  43. package/dist/routes.js +144 -0
  44. package/dist/sentry.d.ts +25 -0
  45. package/dist/sentry.js +11 -0
  46. package/dist/shadow-6HELCHCJ.js +98 -0
  47. package/dist/slot-DqXTwGbs.d.ts +36 -0
  48. package/dist/solid.d.ts +29 -6
  49. package/dist/solid.js +32 -8
  50. package/dist/sri-JMX6JLQP.js +116 -0
  51. package/dist/state-BDdCwc21.d.ts +4 -0
  52. package/dist/svelte.d.ts +19 -3
  53. package/dist/svelte.js +11 -4
  54. package/dist/testing.d.ts +3 -2
  55. package/dist/testing.js +9 -3
  56. package/dist/types-Czvm-siZ.d.ts +327 -0
  57. package/dist/vite.d.ts +66 -4
  58. package/dist/vite.js +1044 -25
  59. package/dist/vue.d.ts +86 -7
  60. package/dist/vue.js +74 -46
  61. package/package.json +181 -11
  62. package/dist/chunk-IZK6ITIJ.js +0 -96
  63. package/dist/chunk-TTL52BF2.js +0 -547
@@ -1,96 +0,0 @@
1
- import {
2
- createSlot,
3
- defineRemote
4
- } from "./chunk-TTL52BF2.js";
5
-
6
- // src/element.ts
7
- var Base = typeof HTMLElement === "undefined" ? class {
8
- } : HTMLElement;
9
- var VitralRemoteElement = class extends Base {
10
- static {
11
- this.observedAttributes = ["name", "props", "isolate"];
12
- }
13
- #slot = null;
14
- #props = {};
15
- get props() {
16
- return this.#props;
17
- }
18
- set props(value) {
19
- this.#props = value ?? {};
20
- this.#slot?.update(this.#props);
21
- }
22
- get status() {
23
- return this.#slot?.status ?? "loading";
24
- }
25
- retry() {
26
- this.#slot?.retry();
27
- }
28
- connectedCallback() {
29
- if (this.#slot) return;
30
- const name = this.getAttribute("name");
31
- if (!name) return;
32
- if (this.hasAttribute("props") && !Object.keys(this.#props).length) this.#props = parseProps(this.getAttribute("props"));
33
- this.#slot = createSlot(this, {
34
- name,
35
- props: this.#props,
36
- isolate: this.getAttribute("isolate") || void 0,
37
- onStatus: (status, error) => {
38
- this.setAttribute("data-vitral-status", status);
39
- for (const el of this.querySelectorAll(':scope > [slot="fallback"]')) el.hidden = status !== "loading";
40
- for (const el of this.querySelectorAll(':scope > [slot="error"]')) el.hidden = status !== "error";
41
- if (status === "ready") this.dispatchEvent(new CustomEvent("vitral-ready", { bubbles: true }));
42
- if (status === "error") this.dispatchEvent(new CustomEvent("vitral-error", { bubbles: true, detail: { error } }));
43
- }
44
- });
45
- }
46
- disconnectedCallback() {
47
- queueMicrotask(() => {
48
- if (!this.isConnected) {
49
- this.#slot?.destroy();
50
- this.#slot = null;
51
- }
52
- });
53
- }
54
- attributeChangedCallback(attr, _old, value) {
55
- if (attr === "props") return void (this.props = parseProps(value));
56
- if (!this.#slot) return void (this.isConnected && this.connectedCallback());
57
- if (attr === "name" && value) this.#slot.set({ name: value });
58
- if (attr === "isolate") this.#slot.set({ isolate: value || void 0 });
59
- }
60
- };
61
- function parseProps(value) {
62
- if (!value) return {};
63
- try {
64
- return JSON.parse(value);
65
- } catch {
66
- console.error("[vitral] atributo props n\xE3o \xE9 JSON v\xE1lido:", value);
67
- return {};
68
- }
69
- }
70
- function defineVitralElement(tag = "vitral-remote") {
71
- if (typeof customElements !== "undefined" && !customElements.get(tag)) {
72
- customElements.define(tag, class extends VitralRemoteElement {
73
- });
74
- }
75
- }
76
- function elementRemote(tag, setup) {
77
- return defineRemote({
78
- mount(el, props, ctx) {
79
- setup?.();
80
- const node = document.createElement(tag);
81
- node.vitral = ctx;
82
- Object.assign(node, props);
83
- el.appendChild(node);
84
- return {
85
- update: (next) => Object.assign(node, next),
86
- unmount: () => node.remove()
87
- };
88
- }
89
- });
90
- }
91
-
92
- export {
93
- VitralRemoteElement,
94
- defineVitralElement,
95
- elementRemote
96
- };
@@ -1,547 +0,0 @@
1
- // src/component.ts
2
- var TEMPLATE = /* @__PURE__ */ Symbol("vitral.html");
3
- function html(strings, ...values) {
4
- return { [TEMPLATE]: true, strings, values };
5
- }
6
- function unsafeHTML(markup) {
7
- return html(Object.assign([markup], { raw: [markup] }));
8
- }
9
- var isTemplate = (v) => typeof v === "object" && v !== null && TEMPLATE in v;
10
- var escape = (s) => s.replace(/[&<>"']/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[c]);
11
- var EVENT_ATTR = /\s+on([a-z][\w:-]*)=(["']?)$/i;
12
- var HANDLER_PREFIX = "data-vitral-h";
13
- function toMarkup(value, handlers) {
14
- if (value == null || value === false) return "";
15
- if (Array.isArray(value)) return value.map((v) => toMarkup(v, handlers)).join("");
16
- if (!isTemplate(value)) return escape(String(value));
17
- const strings = [...value.strings];
18
- let out = "";
19
- for (let i = 0; i < strings.length; i++) {
20
- let s = strings[i];
21
- if (i < value.values.length) {
22
- const v = value.values[i];
23
- const event = typeof v === "function" ? EVENT_ATTR.exec(s) : null;
24
- if (event) {
25
- s = s.slice(0, s.length - event[0].length) + ` ${HANDLER_PREFIX}${handlers.length}="${event[1].toLowerCase()}"`;
26
- handlers.push(v);
27
- if (event[2] && strings[i + 1]?.startsWith(event[2])) strings[i + 1] = strings[i + 1].slice(1);
28
- out += s;
29
- } else {
30
- out += s + toMarkup(v, handlers);
31
- }
32
- } else {
33
- out += s;
34
- }
35
- }
36
- return out;
37
- }
38
- function render(view, el) {
39
- if (typeof Node !== "undefined" && view instanceof Node) {
40
- el.replaceChildren(view);
41
- return;
42
- }
43
- const handlers = [];
44
- el.innerHTML = toMarkup(view, handlers);
45
- if (!handlers.length) return;
46
- for (const node of el.querySelectorAll("*")) {
47
- for (const attr of [...node.attributes]) {
48
- if (!attr.name.startsWith(HANDLER_PREFIX)) continue;
49
- node.addEventListener(attr.value, handlers[Number(attr.name.slice(HANDLER_PREFIX.length))]);
50
- node.removeAttribute(attr.name);
51
- }
52
- }
53
- }
54
- function component(options) {
55
- const opts = typeof options === "function" ? { render: options } : options;
56
- return defineRemote({
57
- mount(el, props, ctx) {
58
- let current = props;
59
- let queued = false;
60
- let alive = true;
61
- const draw = () => {
62
- queued = false;
63
- if (alive) render(opts.render(current, state2, ctx) ?? "", el);
64
- };
65
- const initial = typeof opts.state === "function" ? opts.state() : { ...opts.state ?? {} };
66
- const state2 = new Proxy(initial, {
67
- set(target, key, value) {
68
- if (!Object.is(Reflect.get(target, key), value)) {
69
- Reflect.set(target, key, value);
70
- if (!queued) {
71
- queued = true;
72
- queueMicrotask(draw);
73
- }
74
- }
75
- return true;
76
- }
77
- });
78
- draw();
79
- const cleanup = opts.mounted?.(el, state2, ctx);
80
- return {
81
- update(next) {
82
- current = next;
83
- draw();
84
- },
85
- unmount() {
86
- alive = false;
87
- cleanup?.();
88
- el.replaceChildren();
89
- }
90
- };
91
- }
92
- });
93
- }
94
-
95
- // src/index.ts
96
- var RemoteError = class extends Error {
97
- constructor(code, remote, message, cause) {
98
- super(`[vitral] ${remote}: ${message}`);
99
- this.code = code;
100
- this.remote = remote;
101
- this.cause = cause;
102
- this.name = "RemoteError";
103
- }
104
- };
105
- var KEY = /* @__PURE__ */ Symbol.for("vitral.state.v1");
106
- var g = globalThis;
107
- function state() {
108
- return g[KEY] ??= {
109
- config: { timeout: 15e3, retries: 1, isolate: "none", allowedOrigins: null },
110
- entries: /* @__PURE__ */ new Map(),
111
- remotesSource: null,
112
- modules: /* @__PURE__ */ new Map(),
113
- events: createEventBus()
114
- };
115
- }
116
- function configure(config) {
117
- const s = state();
118
- const { remotes, allowedOrigins, ...rest } = config;
119
- Object.assign(s.config, rest);
120
- if (allowedOrigins) s.config.allowedOrigins = allowedOrigins;
121
- if (typeof remotes === "string") {
122
- const url = absolute(remotes);
123
- s.remotesSource = fetchJson(url, s.config.timeout, "remotes", "no-cache").then(
124
- (map) => registerAll(map, url)
125
- );
126
- } else if (remotes) {
127
- registerAll(remotes);
128
- }
129
- }
130
- function register(name, entry) {
131
- const s = state();
132
- const e = typeof entry === "string" ? { url: entry } : entry;
133
- if (!e.url && !e.module) throw new RemoteError("NOT_REGISTERED", name, "informe url ou module");
134
- s.entries.set(name, e.url ? { ...e, url: absolute(e.url) } : e);
135
- s.modules.delete(name);
136
- }
137
- function registerAll(map, base) {
138
- for (const [name, entry] of Object.entries(map)) {
139
- const e = typeof entry === "string" ? { url: entry } : entry;
140
- register(name, e.url ? { ...e, url: absolute(e.url, base) } : e);
141
- }
142
- }
143
- function loadRemote(name) {
144
- return loadResolved(name).then((r) => r.mod);
145
- }
146
- function preload(name) {
147
- loadResolved(name).catch(() => {
148
- });
149
- }
150
- function loadResolved(name) {
151
- const s = state();
152
- let p = s.modules.get(name);
153
- if (!p) {
154
- p = (async () => {
155
- if (s.remotesSource) await s.remotesSource;
156
- const entry = s.entries.get(name);
157
- if (!entry) throw new RemoteError("NOT_REGISTERED", name, "remote n\xE3o registrado. Use configure({ remotes }).");
158
- if (entry.module) return loadLocal(name, entry);
159
- const resolved = await resolveEntry(name, entry);
160
- const mod = await withRetry(s.config.retries, () => importModule(name, resolved));
161
- return { mod, resolved };
162
- })().catch((e) => {
163
- throw e instanceof RemoteError ? e : new RemoteError("LOAD_FAILED", name, String(e?.message ?? e), e);
164
- });
165
- p.catch(() => s.modules.get(name) === p && s.modules.delete(name));
166
- s.modules.set(name, p);
167
- }
168
- return p;
169
- }
170
- async function loadLocal(name, entry) {
171
- let mod = entry.module;
172
- if (typeof mod === "function") mod = await mod();
173
- mod = mod?.default ?? mod;
174
- if (typeof mod?.mount !== "function") throw new RemoteError("INVALID_MODULE", name, "o m\xF3dulo n\xE3o exporta mount()");
175
- const resolved = { entry: typeof location !== "undefined" ? location.href : "http://localhost/", css: entry.css ?? [], isolate: entry.isolate };
176
- return { mod, resolved };
177
- }
178
- async function resolveEntry(name, entry) {
179
- checkOrigin(name, entry.url);
180
- const isManifest = new URL(entry.url).pathname.endsWith(".json");
181
- if (!isManifest) {
182
- return { entry: entry.url, css: (entry.css ?? []).map((c) => absolute(c, entry.url)), integrity: entry.integrity, isolate: entry.isolate };
183
- }
184
- const m = await fetchJson(entry.url, state().config.timeout, name, "no-cache");
185
- if (!m || typeof m.entry !== "string") throw new RemoteError("INVALID_MODULE", name, `manifesto inv\xE1lido em ${entry.url}`);
186
- const resolved = {
187
- entry: absolute(m.entry, entry.url),
188
- css: [...m.css ?? [], ...entry.css ?? []].map((c) => absolute(c, entry.url)),
189
- // A integridade declarada pelo host vence a do manifesto.
190
- integrity: entry.integrity ?? m.integrity,
191
- isolate: entry.isolate,
192
- scope: m.scope
193
- };
194
- checkOrigin(name, resolved.entry);
195
- return resolved;
196
- }
197
- async function importModule(name, r) {
198
- const { timeout } = state().config;
199
- let url = r.entry;
200
- let revoke = null;
201
- if (r.integrity) {
202
- const code = await fetchText(url, timeout, name);
203
- await verifyIntegrity(name, code, r.integrity);
204
- url = revoke = URL.createObjectURL(new Blob([code], { type: "text/javascript" }));
205
- }
206
- try {
207
- const ns = await withTimeout(timeout, name, import(
208
- /* @vite-ignore */
209
- /* webpackIgnore: true */
210
- url
211
- ));
212
- const mod = ns.default ?? ns;
213
- if (typeof mod?.mount !== "function") {
214
- throw new RemoteError("INVALID_MODULE", name, "o m\xF3dulo n\xE3o exporta mount(). Use defineRemote() no remote.");
215
- }
216
- return mod;
217
- } catch (e) {
218
- if (e instanceof RemoteError) throw e;
219
- throw new RemoteError("LOAD_FAILED", name, `falha ao importar ${r.entry}`, e);
220
- } finally {
221
- if (revoke) URL.revokeObjectURL(revoke);
222
- }
223
- }
224
- async function mountRemote(target, name, props, options = {}) {
225
- const { mod, resolved } = await loadResolved(name);
226
- const isolate = options.isolate ?? resolved.isolate ?? state().config.isolate;
227
- let shadowRoot = null;
228
- const el = document.createElement("div");
229
- el.setAttribute("data-vitral-root", name);
230
- if (resolved.scope) el.setAttribute("data-vitral-scope", resolved.scope);
231
- el.style.display = "contents";
232
- if (isolate === "shadow") {
233
- shadowRoot = target.shadowRoot ?? target.attachShadow({ mode: "open" });
234
- shadowRoot.appendChild(el);
235
- } else {
236
- target.appendChild(el);
237
- }
238
- await injectCss(resolved.css, shadowRoot);
239
- const scoped = scopedEvents();
240
- const ctx = {
241
- name,
242
- baseUrl: new URL(".", resolved.entry).href,
243
- events: scoped.bus,
244
- shadowRoot,
245
- reportError(e) {
246
- const err = e instanceof RemoteError ? e : new RemoteError("RENDER_FAILED", name, String(e?.message ?? e), e);
247
- if (options.onError) options.onError(err);
248
- else console.error(err);
249
- },
250
- navigate
251
- };
252
- let mounted;
253
- try {
254
- mounted = await mod.mount(el, props, ctx);
255
- } catch (e) {
256
- scoped.dispose();
257
- el.remove();
258
- throw new RemoteError("MOUNT_FAILED", name, "erro ao montar", e);
259
- }
260
- let alive = true;
261
- return {
262
- update(next) {
263
- if (alive) mounted.update?.(next);
264
- },
265
- unmount() {
266
- if (!alive) return;
267
- alive = false;
268
- try {
269
- mounted.unmount();
270
- } finally {
271
- scoped.dispose();
272
- el.remove();
273
- }
274
- }
275
- };
276
- }
277
- var loadedCss = /* @__PURE__ */ new WeakMap();
278
- var cssText = /* @__PURE__ */ new Map();
279
- var hoistedProps = /* @__PURE__ */ new Set();
280
- function injectCss(urls, root) {
281
- if (root && typeof CSSStyleSheet !== "undefined" && "replaceSync" in CSSStyleSheet.prototype) {
282
- return injectShadowCss(urls, root);
283
- }
284
- const owner = root ?? document;
285
- let seen = loadedCss.get(owner);
286
- if (!seen) loadedCss.set(owner, seen = /* @__PURE__ */ new Map());
287
- const parent = root ?? document.head;
288
- return Promise.all(
289
- urls.map((href) => {
290
- let p = seen.get(href);
291
- if (!p) {
292
- p = new Promise((resolve) => {
293
- const link = document.createElement("link");
294
- link.rel = "stylesheet";
295
- link.href = href;
296
- link.crossOrigin = "anonymous";
297
- link.onload = link.onerror = () => resolve();
298
- parent.appendChild(link);
299
- });
300
- seen.set(href, p);
301
- }
302
- return p;
303
- })
304
- );
305
- }
306
- function injectShadowCss(urls, root) {
307
- return Promise.all(urls.map(loadSheet)).then((sheets) => {
308
- const add = sheets.filter((s) => !!s && !root.adoptedStyleSheets.includes(s));
309
- root.adoptedStyleSheets = [...root.adoptedStyleSheets, ...add];
310
- });
311
- }
312
- function loadSheet(href) {
313
- let p = cssText.get(href);
314
- if (!p) {
315
- p = fetch(href, { credentials: "omit" }).then((r) => r.ok ? r.text() : "").then((text) => {
316
- const props = [];
317
- const rest = text.replace(/@property\s+(--[\w-]+)\s*\{[^}]*\}/g, (rule, prop) => {
318
- if (!hoistedProps.has(prop)) {
319
- hoistedProps.add(prop);
320
- props.push(rule);
321
- }
322
- return "";
323
- });
324
- if (props.length) {
325
- const style = document.createElement("style");
326
- style.setAttribute("data-vitral-properties", "");
327
- style.textContent = props.join("\n");
328
- document.head.appendChild(style);
329
- }
330
- const sheet = new CSSStyleSheet();
331
- sheet.replaceSync(rest.replace(/url\(\s*(['"]?)(?![a-z]+:|\/|#)([^'")]+)\1\s*\)/gi, (_, q, u) => `url(${q}${new URL(u, href).href}${q})`));
332
- return sheet;
333
- }).catch(() => null);
334
- cssText.set(href, p);
335
- }
336
- return p;
337
- }
338
- function createSlot(target, options) {
339
- let { name, isolate } = options;
340
- let props = options.props ?? {};
341
- let handle = null;
342
- let holder = null;
343
- let token = 0;
344
- let status = "loading";
345
- const setStatus = (s, e = null) => {
346
- status = s;
347
- if (holder) holder.style.display = s === "error" ? "none" : "";
348
- options.onStatus?.(s, e);
349
- };
350
- const teardown = () => {
351
- handle?.unmount();
352
- handle = null;
353
- holder?.remove();
354
- holder = null;
355
- };
356
- const mount = () => {
357
- const my = ++token;
358
- teardown();
359
- holder = document.createElement("div");
360
- holder.setAttribute("data-vitral-slot", name);
361
- target.appendChild(holder);
362
- setStatus("loading");
363
- const fail = (e) => {
364
- if (my !== token) return;
365
- setStatus("error", e instanceof RemoteError ? e : new RemoteError("MOUNT_FAILED", name, String(e), e));
366
- };
367
- const initial = props;
368
- mountRemote(holder, name, props, { isolate, onError: fail }).then((h) => {
369
- if (my !== token) return h.unmount();
370
- handle = h;
371
- if (props !== initial) h.update?.(props);
372
- setStatus("ready");
373
- }, fail);
374
- };
375
- mount();
376
- return {
377
- update(next) {
378
- props = next;
379
- handle?.update?.(next);
380
- },
381
- set(next) {
382
- const changed = next.name !== void 0 && next.name !== name || "isolate" in next && next.isolate !== isolate;
383
- if (next.name !== void 0) name = next.name;
384
- if ("isolate" in next) isolate = next.isolate;
385
- if (changed) mount();
386
- },
387
- retry: mount,
388
- destroy() {
389
- token++;
390
- teardown();
391
- },
392
- get status() {
393
- return status;
394
- }
395
- };
396
- }
397
- function defineRemote(remote) {
398
- return remote;
399
- }
400
- function createEventBus() {
401
- const handlers = /* @__PURE__ */ new Map();
402
- const lastValues = /* @__PURE__ */ new Map();
403
- return {
404
- emit(type, payload) {
405
- lastValues.set(type, payload);
406
- for (const h of [...handlers.get(type) ?? []]) {
407
- try {
408
- h(payload);
409
- } catch (e) {
410
- console.error(`[vitral] handler de "${type}" falhou`, e);
411
- }
412
- }
413
- },
414
- on(type, handler) {
415
- let set = handlers.get(type);
416
- if (!set) handlers.set(type, set = /* @__PURE__ */ new Set());
417
- set.add(handler);
418
- return () => set.delete(handler);
419
- },
420
- last(type) {
421
- return lastValues.get(type);
422
- }
423
- };
424
- }
425
- var events = {
426
- emit: (t, p) => state().events.emit(t, p),
427
- on: (t, h) => state().events.on(t, h),
428
- last: (t) => state().events.last(t)
429
- };
430
- function scopedEvents() {
431
- const offs = /* @__PURE__ */ new Set();
432
- return {
433
- bus: {
434
- emit: (t, p) => events.emit(t, p),
435
- last: (t) => events.last(t),
436
- on(t, h) {
437
- const off = events.on(t, h);
438
- const wrapped = () => (offs.delete(wrapped), off());
439
- offs.add(wrapped);
440
- return wrapped;
441
- }
442
- },
443
- dispose() {
444
- for (const off of [...offs]) off();
445
- }
446
- };
447
- }
448
- function navigate(url, options = {}) {
449
- const target = new URL(url, location.href);
450
- if (target.origin !== location.origin) {
451
- location.assign(target.href);
452
- return;
453
- }
454
- if (options.replace) history.replaceState(history.state, "", target.href);
455
- else history.pushState(history.state, "", target.href);
456
- dispatchEvent(new PopStateEvent("popstate", { state: history.state }));
457
- events.emit("vitral:navigate", target.pathname + target.search + target.hash);
458
- }
459
- function pageOrigin() {
460
- return typeof location !== "undefined" ? location.origin : null;
461
- }
462
- function absolute(url, base) {
463
- return new URL(url, base ?? (typeof location !== "undefined" ? location.href : void 0)).href;
464
- }
465
- function checkOrigin(name, url) {
466
- const s = state();
467
- const origin = new URL(url).origin;
468
- let allowed = s.config.allowedOrigins;
469
- if (!allowed) {
470
- allowed = [pageOrigin(), ...[...s.entries.values()].filter((e) => e.url).map((e) => new URL(e.url).origin)].filter(Boolean);
471
- }
472
- if (allowed.includes("*") || allowed.includes(origin) || url.startsWith("blob:")) return;
473
- throw new RemoteError("ORIGIN_BLOCKED", name, `origem ${origin} n\xE3o est\xE1 em allowedOrigins`);
474
- }
475
- async function withRetry(retries, fn) {
476
- for (let i = 0; ; i++) {
477
- try {
478
- return await fn();
479
- } catch (e) {
480
- const retryable = e instanceof RemoteError && (e.code === "LOAD_FAILED" || e.code === "TIMEOUT");
481
- if (!retryable || i >= retries) throw e;
482
- await new Promise((r) => setTimeout(r, 200 * 2 ** i));
483
- }
484
- }
485
- }
486
- function withTimeout(ms, name, p) {
487
- return new Promise((resolve, reject) => {
488
- const t = setTimeout(() => reject(new RemoteError("TIMEOUT", name, `n\xE3o respondeu em ${ms}ms`)), ms);
489
- p.then(
490
- (v) => (clearTimeout(t), resolve(v)),
491
- (e) => (clearTimeout(t), reject(e))
492
- );
493
- });
494
- }
495
- async function fetchText(url, timeout, name, cache) {
496
- const ctrl = new AbortController();
497
- const t = setTimeout(() => ctrl.abort(), timeout);
498
- try {
499
- const res = await fetch(url, { signal: ctrl.signal, credentials: "omit", cache });
500
- if (!res.ok) throw new RemoteError("LOAD_FAILED", name, `HTTP ${res.status} em ${url}`);
501
- return await res.text();
502
- } catch (e) {
503
- if (e instanceof RemoteError) throw e;
504
- if (ctrl.signal.aborted) throw new RemoteError("TIMEOUT", name, `n\xE3o respondeu em ${timeout}ms`);
505
- throw new RemoteError("LOAD_FAILED", name, `falha ao baixar ${url}`, e);
506
- } finally {
507
- clearTimeout(t);
508
- }
509
- }
510
- async function fetchJson(url, timeout, name, cache) {
511
- const text = await fetchText(url, timeout, name, cache);
512
- try {
513
- return JSON.parse(text);
514
- } catch (e) {
515
- throw new RemoteError("INVALID_MODULE", name, `JSON inv\xE1lido em ${url}`, e);
516
- }
517
- }
518
- async function verifyIntegrity(name, code, integrity) {
519
- const [algo, expected] = integrity.split("-", 2);
520
- const algos = { sha256: "SHA-256", sha384: "SHA-384", sha512: "SHA-512" };
521
- if (!algos[algo] || !expected) throw new RemoteError("INTEGRITY", name, `formato de integrity inv\xE1lido: ${integrity}`);
522
- const digest = await crypto.subtle.digest(algos[algo], new TextEncoder().encode(code));
523
- const actual = btoa(String.fromCharCode(...new Uint8Array(digest)));
524
- if (actual !== expected) throw new RemoteError("INTEGRITY", name, "o hash do c\xF3digo n\xE3o confere com integrity");
525
- }
526
- function __reset() {
527
- delete g[KEY];
528
- }
529
-
530
- export {
531
- html,
532
- unsafeHTML,
533
- render,
534
- component,
535
- RemoteError,
536
- configure,
537
- register,
538
- loadRemote,
539
- preload,
540
- mountRemote,
541
- createSlot,
542
- defineRemote,
543
- createEventBus,
544
- events,
545
- navigate,
546
- __reset
547
- };