@trifrost/core
Version:
Blazingly fast, runtime-agnostic server framework for modern edge and node environments
69 lines (68 loc) • 2.46 kB
JavaScript
;
/// <reference lib="dom" />
Object.defineProperty(exports, "__esModule", { value: true });
exports.SCRIPT_MARKER = void 0;
exports.Script = Script;
const nonce_1 = require("../ctx/nonce");
const use_1 = require("./use");
const util_1 = require("./util");
exports.SCRIPT_MARKER = '__TRIFROST_HYDRATED_SCRIPT__';
const RGX_DATA_SCRIPT = /<\/script>/gi;
function Script(options) {
if (!options || Object.prototype.toString.call(options) !== '[object Object]')
return null;
/* Source */
if (typeof options.src === 'string' && options.src.length) {
const { src, async, defer, type = 'text/javascript' } = options;
/* Formulate props */
const props = { type };
props.src = src;
/* Add async */
if (async === true)
props.async = true;
/* Add defer */
if (defer)
props.defer = true;
/* Nonce */
let n_nonce = options.nonce || null;
if (!n_nonce)
n_nonce = (0, nonce_1.nonce)();
if (typeof n_nonce === 'string' && n_nonce.length)
props.nonce = n_nonce;
return { type: 'script', props, key: null };
}
/* If at this point we dont have a function, do nothing */
if (typeof options.children !== 'function')
return null;
const raw = options.children.toString().trim();
/* If pure (no args), execute eagerly (inline) as we dont need to atomify the method */
if (options.children.length === 0) {
const open_idx = raw.indexOf('{');
const close_idx = raw.lastIndexOf('}');
const body = open_idx >= 0 && close_idx >= 0 ? (0, util_1.atomicMinify)(raw.slice(open_idx + 1, close_idx)) : '';
if (!body)
return null;
return {
type: 'script',
props: {
dangerouslySetInnerHTML: {
__html: '(function(){' + body + '})();',
},
nonce: options.nonce || (0, nonce_1.nonce)(),
},
key: null,
};
}
else {
const engine = (0, use_1.getActiveScriptEngine)();
if (!engine)
return null;
/* Get data */
const data = options.data ? JSON.stringify(options.data).replace(RGX_DATA_SCRIPT, '<\\/script>') : null;
return {
type: exports.SCRIPT_MARKER,
props: engine.register(raw, data),
key: null,
};
}
}