All files / src/internal/server index.js

97.78% Statements 663/678
96.96% Branches 128/132
94.59% Functions 35/37
97.78% Lines 663/678

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 6791x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2549x 2549x 2549x 1x 1x 1x 1x 1x 1x 171x 171x 171x 171x 171x 1x 1x 1x 1x 1x 1x 1x 1x 142x 142x 142x 142x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 86x     86x 45x 45x 5x 5x 5x 5x 5x 45x 86x 17x 17x 2x 2x 2x 2x 2x 17x 86x 86x 86x 86x 86x 1x 1x 84x 84x 84x 84x 1x 1x 1x 1x 1x 1x 1x 1x 1x 113x 113x 113x 113x 113x 99x 94x 94x 99x 99x 94x 94x 99x 99x 113x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2549x 2549x 2549x 2549x 2549x 2549x 2549x 1x 1x 1x 2549x 2549x 2549x 2549x 2549x 1x 1x 2526x 2526x 2549x 2526x 2526x 2526x 2549x 2549x 2549x 2549x 1x 1x 1x 1x 1x 1x 1x 1x 8781x 8781x 8781x 8781x 8781x 8781x 8781x 8781x 8781x 57x 57x 57x 57x 57x 8781x 8781x 8781x 1x 1x 1x 1x 1x 1x 1x 32x 32x 32x 32x 32x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1483x 1483x 1483x 1483x 1x 1x 1x 1x 1x 1x 1x 1x 1x 26x 26x 24x 26x 2x 2x 26x 26x 24x 26x 2x 2x 26x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 177x 177x 177x 177x 177x 283x 283x 327x 327x 303x 303x 327x 283x 177x 177x 177x 9x 8x 8x 8x 9x 1x 1x 9x 177x 177x 8x 8x 8x     8x 177x 177x 6x 6x 6x     6x 177x 177x 177x 177x 177x 288x 286x 267x 267x 288x 288x 288x 177x 177x 177x 1x 1x 1x 1x 1x 1x 48x 48x 48x 48x 48x 87x 87x 145x 145x 87x 48x 48x 1x 1x 1x 1x 1x 1x 211x 211x 1x 1x 69x 69x 69x 69x 69x 69x 1x 1x 1x 34x 34x 34x 1x 1x 1x 1x 1x 1x 15x 15x 15x 25x 25x 25x 25x 18x 18x 15x 18x 18x 16x 18x 2x 2x 18x 15x 15x 1x 1x 1x 1x 1x 1x 1x 1x 1x 359x 359x 359x 357x 357x 357x 359x 112x 112x 245x 359x 359x 359x 359x 359x 359x 359x 359x 359x 1x 1x 1x 1x 1x 1x 359x 2x 2x 359x 1x 1x 1x 1x 1x 1x 1x 1x 1x 44x 44x 44x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 1x 1x 1x 151x 241x 241x 151x 1x 1x 1x 1x 1x 1x 1x 1x 2054x 2054x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 555x 37x 29x 29x 555x 518x 518x 555x 1x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 37x 16x 16x 37x 17x 17x 1x 1x 1x 1x 1x 1x 35x 35x 35x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 2118x 4298x 4298x 4298x 4298x 4298x 2046x 4298x 34x 34x 4298x 2118x 1x 1x 1x 1x 1x 1x 1x 1x 119x 119x 92x 92x 92x 92x 119x 27x 27x 119x 1x 1x 1x 1x 1x 745x 745x 745x 745x 1x 1x 1x 1x 1x 1x               1x 1x 1x 1x 1x 1x 1x 8x 8x 1x 1x 1x 1x 1x 1x 16x 16x 16x 14x 14x 16x 16x 16x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { is_promise, noop } from '../shared/utils.js';
import { subscribe_to_store } from '../../store/utils.js';
import {
	UNINITIALIZED,
	DOMBooleanAttributes,
	RawTextElements,
	disallowed_paragraph_contents,
	interactive_elements,
	is_tag_valid_with_parent
} from '../../constants.js';
import { DEV } from 'esm-env';
import { current_component, pop, push } from './context.js';
import { BLOCK_CLOSE, BLOCK_OPEN } from './hydration.js';
 
/**
 * @typedef {{
 * 	tag: string;
 * 	parent: null | Element;
 * }} Element
 */
 
/**
 * @typedef {{
 * 	head: string;
 * 	html: string;
 * }} RenderOutput
 */
 
/**
 * @typedef {{
 * 	out: string;
 * 	anchor: number;
 * 	head: {
 * 		title: string;
 * 		out: string;
 * 		anchor: number;
 * 	};
 * }} Payload
 */
 
const ATTR_REGEX = /[&"]/g;
const CONTENT_REGEX = /[&<]/g;
// https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
// https://infra.spec.whatwg.org/#noncharacter
const INVALID_ATTR_NAME_CHAR_REGEX =
	/[\s'">/=\u{FDD0}-\u{FDEF}\u{FFFE}\u{FFFF}\u{1FFFE}\u{1FFFF}\u{2FFFE}\u{2FFFF}\u{3FFFE}\u{3FFFF}\u{4FFFE}\u{4FFFF}\u{5FFFE}\u{5FFFF}\u{6FFFE}\u{6FFFF}\u{7FFFE}\u{7FFFF}\u{8FFFE}\u{8FFFF}\u{9FFFE}\u{9FFFF}\u{AFFFE}\u{AFFFF}\u{BFFFE}\u{BFFFF}\u{CFFFE}\u{CFFFF}\u{DFFFE}\u{DFFFF}\u{EFFFE}\u{EFFFF}\u{FFFFE}\u{FFFFF}\u{10FFFE}\u{10FFFF}]/u;
 
export const VoidElements = new Set([
	'area',
	'base',
	'br',
	'col',
	'embed',
	'hr',
	'img',
	'input',
	'keygen',
	'link',
	'menuitem',
	'meta',
	'param',
	'source',
	'track',
	'wbr'
]);
 
/**
 * @type {Element | null}
 */
let current_element = null;
 
/** @returns {Payload} */
function create_payload() {
	return { out: '', head: { title: '', out: '', anchor: 0 }, anchor: 0 };
}
 
/**
 * @param {Payload} to_copy
 * @returns {Payload}
 */
export function copy_payload(to_copy) {
	return {
		...to_copy,
		head: { ...to_copy.head }
	};
}
 
/**
 * Assigns second payload to first
 * @param {Payload} p1
 * @param {Payload} p2
 * @returns {void}
 */
export function assign_payload(p1, p2) {
	p1.out = p2.out;
	p1.head = p2.head;
	p1.anchor = p2.anchor;
}
 
/**
 * @param {Payload} payload
 * @param {string} message
 */
function error_on_client(payload, message) {
	message =
		`Svelte SSR validation error:\n\n${message}\n\n` +
		'Ensure your components render valid HTML as the browser will try to repair invalid HTML, ' +
		'which may result in content being shifted around and will likely result in a hydration mismatch.';
	// eslint-disable-next-line no-console
	console.error(message);
	payload.head.out += `<script>console.error(\`${message}\`)</script>`;
}
 
/**
 * @param {string} tag
 * @param {Payload} payload
 */
export function push_element(tag, payload) {
	if (current_element !== null && !is_tag_valid_with_parent(tag, current_element.tag)) {
		error_on_client(payload, `<${tag}> is invalid inside <${current_element.tag}>`);
	}
	if (interactive_elements.has(tag)) {
		let element = current_element;
		while (element !== null) {
			if (interactive_elements.has(element.tag)) {
				error_on_client(payload, `<${tag}> is invalid inside <${element.tag}>`);
			}
			element = element.parent;
		}
	}
	if (disallowed_paragraph_contents.includes(tag)) {
		let element = current_element;
		while (element !== null) {
			if (element.tag === 'p') {
				error_on_client(payload, `<${tag}> is invalid inside <p>`);
			}
			element = element.parent;
		}
	}
	current_element = {
		tag,
		parent: current_element
	};
}
 
export function pop_element() {
	if (current_element !== null) {
		current_element = current_element.parent;
	}
}
 
/**
 * @param {Payload} payload
 * @param {string} tag
 * @param {() => void} attributes_fn
 * @param {() => void} children_fn
 * @returns {void}
 */
export function element(payload, tag, attributes_fn, children_fn) {
	payload.out += `<${tag} `;
	attributes_fn();
	payload.out += `>`;
 
	if (!VoidElements.has(tag)) {
		if (!RawTextElements.includes(tag)) {
			payload.out += BLOCK_OPEN;
		}
		children_fn();
		if (!RawTextElements.includes(tag)) {
			payload.out += BLOCK_CLOSE;
		}
		payload.out += `</${tag}>`;
	}
}
 
/**
 * Array of `onDestroy` callbacks that should be called at the end of the server render function
 * @type {Function[]}
 */
export let on_destroy = [];
 
/**
 * @param {typeof import('svelte').SvelteComponent} component
 * @param {{ props: Record<string, any>; context?: Map<any, any> }} options
 * @returns {RenderOutput}
 */
export function render(component, options) {
	const payload = create_payload();
 
	const prev_on_destroy = on_destroy;
	on_destroy = [];
	payload.out += BLOCK_OPEN;
 
	if (options.context) {
		push();
		/** @type {import('#server').Component} */ (current_component).c = options.context;
	}
 
	// @ts-expect-error
	component(payload, options.props, {}, {});
 
	if (options.context) {
		pop();
	}
 
	payload.out += BLOCK_CLOSE;
	for (const cleanup of on_destroy) cleanup();
	on_destroy = prev_on_destroy;
 
	return {
		head: payload.head.out || payload.head.title ? payload.head.out + payload.head.title : '',
		html: payload.out
	};
}
 
/**
 * @template V
 * @param {V} value
 * @param {any} is_attr
 * @returns {string}
 */
export function escape(value, is_attr = false) {
	const str = String(value ?? '');
 
	const pattern = is_attr ? ATTR_REGEX : CONTENT_REGEX;
	pattern.lastIndex = 0;
 
	let escaped = '';
	let last = 0;
 
	while (pattern.test(str)) {
		const i = pattern.lastIndex - 1;
		const ch = str[i];
		escaped += str.substring(last, i) + (ch === '&' ? '&amp;' : ch === '"' ? '&quot;' : '&lt;');
		last = i + 1;
	}
 
	return escaped + str.substring(last);
}
 
/**
 * @param {Payload} payload
 * @param {(head_payload: Payload['head']) => void} fn
 * @returns {void}
 */
export function head(payload, fn) {
	const head_payload = payload.head;
	payload.head.out += BLOCK_OPEN;
	fn(head_payload);
	payload.head.out += BLOCK_CLOSE;
}
 
/**
 * @template V
 * @param {string} name
 * @param {V} value
 * @param {boolean} boolean
 * @returns {string}
 */
export function attr(name, value, boolean) {
	if (value == null || (!value && boolean) || (value === '' && name === 'class')) return '';
	const assignment = boolean ? '' : `="${escape(value, true)}"`;
	return ` ${name}${assignment}`;
}
 
/**
 * @param {Payload} payload
 * @param {boolean} is_html
 * @param {Record<string, string>} props
 * @param {() => void} component
 * @returns {void}
 */
export function css_props(payload, is_html, props, component) {
	const styles = style_object_to_string(props);
	if (is_html) {
		payload.out += `<div style="display: contents; ${styles}"><!--[-->`;
	} else {
		payload.out += `<g style="${styles}"><!--[-->`;
	}
	component();
	if (is_html) {
		payload.out += `<!--]--></div>`;
	} else {
		payload.out += `<!--]--></g>`;
	}
}
 
/**
 * @param {Record<string, unknown>[]} attrs
 * @param {boolean} lowercase_attributes
 * @param {boolean} is_svg
 * @param {string} class_hash
 * @param {{ styles: Record<string, string> | null; classes: string }} [additional]
 * @returns {string}
 */
export function spread_attributes(attrs, lowercase_attributes, is_svg, class_hash, additional) {
	/** @type {Record<string, unknown>} */
	const merged_attrs = {};
	let key;
 
	for (let i = 0; i < attrs.length; i++) {
		const obj = attrs[i];
		for (key in obj) {
			// omit functions
			if (typeof obj[key] !== 'function') {
				merged_attrs[key] = obj[key];
			}
		}
	}
 
	const styles = additional?.styles;
	if (styles) {
		if ('style' in merged_attrs) {
			merged_attrs.style = style_object_to_string(
				merge_styles(/** @type {string} */ (merged_attrs.style), styles)
			);
		} else {
			merged_attrs.style = style_object_to_string(styles);
		}
	}
 
	if (class_hash) {
		if ('class' in merged_attrs) {
			merged_attrs.class += ` ${class_hash}`;
		} else {
			merged_attrs.class = class_hash;
		}
	}
	const classes = additional?.classes;
	if (classes) {
		if ('class' in merged_attrs) {
			merged_attrs.class += ` ${classes}`;
		} else {
			merged_attrs.class = classes;
		}
	}
 
	let attr_str = '';
	let name;
 
	for (name in merged_attrs) {
		if (INVALID_ATTR_NAME_CHAR_REGEX.test(name)) continue;
		if (lowercase_attributes) {
			name = name.toLowerCase();
		}
		const is_boolean = !is_svg && DOMBooleanAttributes.includes(name);
		attr_str += attr(name, merged_attrs[name], is_boolean);
	}
 
	return attr_str;
}
 
/**
 * @param {Record<string, unknown>[]} props
 * @returns {Record<string, unknown>}
 */
export function spread_props(props) {
	/** @type {Record<string, unknown>} */
	const merged_props = {};
	let key;
 
	for (let i = 0; i < props.length; i++) {
		const obj = props[i];
		for (key in obj) {
			merged_props[key] = obj[key];
		}
	}
	return merged_props;
}
 
/**
 * @param {unknown} value
 * @returns {string}
 */
export function stringify(value) {
	return typeof value === 'string' ? value : value == null ? '' : value + '';
}
 
/** @param {Record<string, string>} style_object */
function style_object_to_string(style_object) {
	return Object.keys(style_object)
		.filter(/** @param {any} key */ (key) => style_object[key])
		.map(/** @param {any} key */ (key) => `${key}: ${escape(style_object[key], true)};`)
		.join(' ');
}
 
/** @param {Record<string, string>} style_object */
export function add_styles(style_object) {
	const styles = style_object_to_string(style_object);
	return styles ? ` style="${styles}"` : '';
}
 
/**
 * @param {string} style_attribute
 * @param {Record<string, string>} style_directive
 */
export function merge_styles(style_attribute, style_directive) {
	/** @type {Record<string, string>} */
	const style_object = {};
	for (const individual_style of style_attribute.split(';')) {
		const colon_index = individual_style.indexOf(':');
		const name = individual_style.slice(0, colon_index).trim();
		const value = individual_style.slice(colon_index + 1).trim();
		if (!name) continue;
		style_object[name] = value;
	}
	for (const name in style_directive) {
		const value = style_directive[name];
		if (value) {
			style_object[name] = value;
		} else {
			delete style_object[name];
		}
	}
	return style_object;
}
 
/**
 * @template V
 * @param {Record<string, [any, any, any]>} store_values
 * @param {string} store_name
 * @param {import('#shared').Store<V> | null | undefined} store
 * @returns {V}
 */
export function store_get(store_values, store_name, store) {
	if (DEV) {
		validate_store(store, store_name.slice(1));
	}
 
	// it could be that someone eagerly updates the store in the instance script, so
	// we should only reuse the store value in the template
	if (store_name in store_values && store_values[store_name][0] === store) {
		return store_values[store_name][2];
	}
 
	store_values[store_name]?.[1](); // if store was switched, unsubscribe from old store
	store_values[store_name] = [store, null, undefined];
	const unsub = subscribe_to_store(
		store,
		/** @param {any} v */ (v) => (store_values[store_name][2] = v)
	);
	store_values[store_name][1] = unsub;
	return store_values[store_name][2];
}
 
/**
 * @param {any} store
 * @param {string} name
 */
export function validate_store(store, name) {
	if (store != null && typeof store.subscribe !== 'function') {
		throw new Error(`'${name}' is not a store with a 'subscribe' method`);
	}
}
 
/**
 * Sets the new value of a store and returns that value.
 * @template V
 * @param {import('#shared').Store<V>} store
 * @param {V} value
 * @returns {V}
 */
export function store_set(store, value) {
	store.set(value);
	return value;
}
 
/**
 * Updates a store with a new value.
 * @template V
 * @param {Record<string, [any, any, any]>} store_values
 * @param {string} store_name
 * @param {import('#shared').Store<V>} store
 * @param {any} expression
 */
export function mutate_store(store_values, store_name, store, expression) {
	store_set(store, store_get(store_values, store_name, store));
	return expression;
}
 
/**
 * @param {Record<string, [any, any, any]>} store_values
 * @param {string} store_name
 * @param {import('#shared').Store<number>} store
 * @param {1 | -1} [d]
 * @returns {number}
 */
export function update_store(store_values, store_name, store, d = 1) {
	let store_value = store_get(store_values, store_name, store);
	store.set(store_value + d);
	return store_value;
}
 
/**
 * @param {Record<string, [any, any, any]>} store_values
 * @param {string} store_name
 * @param {import('#shared').Store<number>} store
 * @param {1 | -1} [d]
 * @returns {number}
 */
export function update_store_pre(store_values, store_name, store, d = 1) {
	const value = store_get(store_values, store_name, store) + d;
	store.set(value);
	return value;
}
 
/** @param {Record<string, [any, any, any]>} store_values */
export function unsubscribe_stores(store_values) {
	for (const store_name in store_values) {
		store_values[store_name][1]();
	}
}
 
/**
 * @template V
 * @param {V} value
 * @param {() => V} fallback lazy because could contain side effects
 * @returns {V}
 */
export function value_or_fallback(value, fallback) {
	return value === undefined ? fallback() : value;
}
 
/**
 * @template V
 * @param {V} value
 * @param {() => Promise<V>} fallback lazy because could contain side effects
 * @returns {Promise<V>}
 */
export async function value_or_fallback_async(value, fallback) {
	return value === undefined ? fallback() : value;
}
 
/**
 * @param {Payload} payload
 * @param {void | ((payload: Payload, props: Record<string, unknown>) => void)} slot_fn
 * @param {Record<string, unknown>} slot_props
 * @param {null | (() => void)} fallback_fn
 * @returns {void}
 */
export function slot(payload, slot_fn, slot_props, fallback_fn) {
	if (slot_fn === undefined) {
		if (fallback_fn !== null) {
			fallback_fn();
		}
	} else {
		slot_fn(payload, slot_props);
	}
}
 
/**
 * @param {Record<string, unknown>} props
 * @param {string[]} rest
 * @returns {Record<string, unknown>}
 */
export function rest_props(props, rest) {
	/** @type {Record<string, unknown>} */
	const rest_props = {};
	let key;
	for (key in props) {
		if (!rest.includes(key)) {
			rest_props[key] = props[key];
		}
	}
	return rest_props;
}
 
/**
 * @param {Record<string, unknown>} props
 * @returns {Record<string, unknown>}
 */
export function sanitize_props(props) {
	const { children, $$slots, ...sanitized } = props;
	return sanitized;
}
 
/**
 * @param {Record<string, any>} props
 * @returns {Record<string, any>}
 */
export function sanitize_slots(props) {
	const sanitized = { ...props.$$slots };
	if (props.children) sanitized.default = props.children;
	return sanitized;
}
 
/**
 * Legacy mode: If the prop has a fallback and is bound in the
 * parent component, propagate the fallback value upwards.
 * @param {Record<string, unknown>} props_parent
 * @param {Record<string, unknown>} props_now
 */
export function bind_props(props_parent, props_now) {
	for (const key in props_now) {
		const initial_value = props_parent[key];
		const value = props_now[key];
		if (
			initial_value === undefined &&
			value !== undefined &&
			Object.getOwnPropertyDescriptor(props_parent, key)?.set
		) {
			props_parent[key] = value;
		}
	}
}
 
/**
 * @template V
 * @param {Promise<V>} promise
 * @param {null | (() => void)} pending_fn
 * @param {(value: V) => void} then_fn
 * @returns {void}
 */
function await_block(promise, pending_fn, then_fn) {
	if (is_promise(promise)) {
		promise.then(null, noop);
		if (pending_fn !== null) {
			pending_fn();
		}
	} else if (then_fn !== null) {
		then_fn(promise);
	}
}
 
export { await_block as await };
 
/** @param {any} array_like_or_iterator */
export function ensure_array_like(array_like_or_iterator) {
	return array_like_or_iterator?.length !== undefined
		? array_like_or_iterator
		: Array.from(array_like_or_iterator);
}
 
/**
 * @param {number} timeout
 * @returns {() => void}
 * */
export function loop_guard(timeout) {
	const start = Date.now();
	return () => {
		if (Date.now() - start > timeout) {
			throw new Error('Infinite loop detected');
		}
	};
}
 
/**
 * @param {any[]} args
 * @param {Function} [inspect]
 */
// eslint-disable-next-line no-console
export function inspect(args, inspect = console.log) {
	inspect('init', ...args);
}
 
/**
 * @template V
 * @param {() => V} get_value
 */
export function once(get_value) {
	let value = /** @type {V} */ (UNINITIALIZED);
	return () => {
		if (value === UNINITIALIZED) {
			value = get_value();
		}
		return value;
	};
}
 
export { push, pop } from './context.js';
 
export {
	add_snippet_symbol,
	validate_component,
	validate_dynamic_element_tag,
	validate_snippet,
	validate_void_dynamic_element
} from '../shared/validate.js';