UNPKG

@trifrost/core

Version:

Blazingly fast, runtime-agnostic server framework for modern edge and node environments

66 lines (65 loc) 1.83 kB
// eslint-disable-next-line @typescript-eslint/triple-slash-reference /// <reference path="../atomic.d.ts" /> import { ScriptEngine } from './Engine'; import { env as ogEnv } from '../ctx/env'; import { state as ogState } from '../ctx/state'; import { nonce } from '../ctx/nonce'; import { Script as ogScript } from './Script'; import { Module as ogModule } from './Module'; let active_engine = null; export function setActiveScriptEngine(engine) { active_engine = engine; return engine; } export function getActiveScriptEngine() { return active_engine; } /** * MARK: Script Factory */ export function createScript(config) { let mountPath = null; const isAtomic = 'atomic' in config && config.atomic === true; if (!active_engine) setActiveScriptEngine(new ScriptEngine()); if (isAtomic) active_engine.setAtomic(true); const modMap = config.modules ?? {}; const env = (key) => ogEnv(key); const state = (key) => ogState(key); const Script = (props) => { if (!active_engine) setActiveScriptEngine(new ScriptEngine()); if (!active_engine.known_modules_rgx) active_engine?.setModules(modMap); return ogScript(props); }; const root = () => { if (mountPath) active_engine.setMountPath(mountPath); active_engine.setRoot(true); }; const setMountPath = (path) => { mountPath = typeof path === 'string' ? path : null; }; return { Script, script: { env, state, nonce, root, isAtomic, setMountPath, }, }; } /** * MARK: Module Factory */ export function createModule(config = {}) { function Module(props) { return ogModule(props); } return { Module }; }