Home / File/ utils.ts — astro Source File

utils.ts — astro Source File

Architecture documentation for utils.ts, a typescript file in the astro codebase. 6 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 6 imports 5 functions

Entity Profile

Dependency Diagram

graph LR
  0ac2db55_946e_5ee0_0aac_261f857d2b76["utils.ts"]
  28857b9f_4720_3f29_4abb_a7eec34dcca5["../types/public/context.js"]
  0ac2db55_946e_5ee0_0aac_261f857d2b76 --> 28857b9f_4720_3f29_4abb_a7eec34dcca5
  b0bf0fd2_1ce5_745d_58fc_02deee5192bf["../actions/runtime/client.js"]
  0ac2db55_946e_5ee0_0aac_261f857d2b76 --> b0bf0fd2_1ce5_745d_58fc_02deee5192bf
  4a188169_db01_767e_98b1_f9cf67180716["../actions/runtime/server.js"]
  0ac2db55_946e_5ee0_0aac_261f857d2b76 --> 4a188169_db01_767e_98b1_f9cf67180716
  a1711a94_f06c_214e_28b9_0d6031277c04["../../actions/runtime/types.js"]
  0ac2db55_946e_5ee0_0aac_261f857d2b76 --> a1711a94_f06c_214e_28b9_0d6031277c04
  e16a223b_37f3_6b25_1ee1_2b7bcb9d9415["node:fs"]
  0ac2db55_946e_5ee0_0aac_261f857d2b76 --> e16a223b_37f3_6b25_1ee1_2b7bcb9d9415
  58b96617_ab14_83d2_3aac_1c7f1ca9b66c["es-module-lexer"]
  0ac2db55_946e_5ee0_0aac_261f857d2b76 --> 58b96617_ab14_83d2_3aac_1c7f1ca9b66c
  style 0ac2db55_946e_5ee0_0aac_261f857d2b76 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type fsMod from 'node:fs';
import * as eslexer from 'es-module-lexer';
import type { APIContext } from '../types/public/context.js';
import { deserializeActionResult, getActionQueryString } from './runtime/client.js';
import { ACTION_API_CONTEXT_SYMBOL } from './runtime/server.js';
import type { ActionAPIContext, ActionsLocals } from './runtime/types.js';

export function hasActionPayload(locals: APIContext['locals']): locals is ActionsLocals {
	return '_actionPayload' in locals;
}

export function createGetActionResult(locals: APIContext['locals']): APIContext['getActionResult'] {
	return (actionFn): any => {
		if (
			!hasActionPayload(locals) ||
			actionFn.toString() !== getActionQueryString(locals._actionPayload.actionName)
		) {
			return undefined;
		}
		return deserializeActionResult(locals._actionPayload.actionResult);
	};
}

export function createCallAction(context: ActionAPIContext): APIContext['callAction'] {
	return (baseAction, input) => {
		Reflect.set(context, ACTION_API_CONTEXT_SYMBOL, true);
		const action = baseAction.bind(context);
		return action(input) as any;
	};
}

let didInitLexer = false;

/**
 * Check whether the Actions config file is present.
 */
export async function isActionsFilePresent(fs: typeof fsMod, srcDir: URL) {
	if (!didInitLexer) await eslexer.init;

	const actionsFile = search(fs, srcDir);
	if (!actionsFile) return false;

	let contents: string;
	try {
		contents = fs.readFileSync(actionsFile.url, 'utf-8');
	} catch {
		return false;
	}

	// Check if `server` export is present.
	// If not, the user may have an empty `actions` file,
	// or may be using the `actions` file for another purpose
	// (possible since actions are non-breaking for v4.X).
	const [, exports] = eslexer.parse(contents, actionsFile.url.pathname);
	for (const exp of exports) {
		if (exp.n === 'server') {
			return actionsFile.filename;
		}
	}
	return false;
}

function search(fs: typeof fsMod, srcDir: URL) {
	const filenames = [
		'actions.mjs',
		'actions.js',
		'actions.mts',
		'actions.ts',
		'actions/index.mjs',
		'actions/index.js',
		'actions/index.mts',
		'actions/index.ts',
	];
	for (const filename of filenames) {
		const url = new URL(filename, srcDir);
		if (fs.existsSync(url)) {
			return { filename, url };
		}
	}
	return undefined;
}

Domain

Subdomains

Dependencies

  • ../../actions/runtime/types.js
  • ../actions/runtime/client.js
  • ../actions/runtime/server.js
  • ../types/public/context.js
  • es-module-lexer
  • node:fs

Frequently Asked Questions

What does utils.ts do?
utils.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in utils.ts?
utils.ts defines 5 function(s): createCallAction, createGetActionResult, hasActionPayload, isActionsFilePresent, search.
What does utils.ts depend on?
utils.ts imports 6 module(s): ../../actions/runtime/types.js, ../actions/runtime/client.js, ../actions/runtime/server.js, ../types/public/context.js, es-module-lexer, node:fs.
Where is utils.ts in the architecture?
utils.ts is located at packages/astro/src/actions/utils.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/actions).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free