zephyr-agent
Version:
Zephyr plugin agent
137 lines (136 loc) • 5.9 kB
JavaScript
import "node:module";
import { __webpack_require__ } from "../../rslib-runtime~0.mjs";
import { createRequire as __rspack_createRequire } from "node:module";
const __rspack_createRequire_require = __rspack_createRequire(import.meta.url);
__webpack_require__.add({
"crypto?c60e" (module) {
module.exports = __rspack_createRequire_require("crypto");
}
});
const VIRTUAL_SPECIFIER = 'env:vars';
const ZE_REGEX_SIMPLE = /\bprocess\.env\.(ZE_PUBLIC_[A-Z0-9_]+)/g;
const ZE_REGEX_QUOTED = /\bprocess\.env\[["'`](ZE_PUBLIC_[A-Z0-9_]+)["'`]\]/g;
const ZE_REGEX_IMPORT_META_SIMPLE = /\bimport\.meta\.env\.(ZE_PUBLIC_[A-Z0-9_]+)/g;
const ZE_REGEX_IMPORT_META_QUOTED = /\bimport\.meta\.env\[["'`](ZE_PUBLIC_[A-Z0-9_]+)["'`]\]/g;
const ZE_DESTRUCT_DECL = /(const|let|var)\s*\{([^}]+)\}\s*=\s*(process\.env|import\.meta\.env)/g;
const ZE_DESTRUCT_ASSIGN = /\{([^}]+)\}\s*=\s*(process\.env|import\.meta\.env)/g;
function extractDestructuredKeys(raw) {
return raw.split(',').map((s)=>s.trim()).filter(Boolean).map((frag)=>{
const colonIdx = frag.indexOf(':');
const eqIdx = frag.indexOf('=');
let base = frag;
if (-1 !== colonIdx) base = frag.slice(0, colonIdx).trim();
else if (-1 !== eqIdx) base = frag.slice(0, eqIdx).trim();
return base;
});
}
function detectEnvReads(source) {
const names = new Set();
const regs = [
ZE_REGEX_SIMPLE,
ZE_REGEX_QUOTED,
ZE_REGEX_IMPORT_META_SIMPLE,
ZE_REGEX_IMPORT_META_QUOTED
];
for (const r of regs)source.replace(r, (_m, name)=>{
names.add(String(name));
return _m;
});
source.replace(ZE_DESTRUCT_DECL, (_m, _decl, inner)=>{
for (const k of extractDestructuredKeys(String(inner)))if (k.startsWith('ZE_PUBLIC_')) names.add(k);
return _m;
});
source.replace(ZE_DESTRUCT_ASSIGN, (_m, inner)=>{
for (const k of extractDestructuredKeys(String(inner)))if (k.startsWith('ZE_PUBLIC_')) names.add(k);
return _m;
});
return names;
}
function rewriteEnvReadsToVirtualModule(source, specifier = VIRTUAL_SPECIFIER) {
const used = detectEnvReads(source);
if (0 === used.size) return {
code: source,
used
};
let code = source.replace(ZE_REGEX_SIMPLE, (_m, name)=>`__ZE_MANIFEST__.zeVars.${name}`).replace(ZE_REGEX_QUOTED, (_m, name)=>`__ZE_MANIFEST__.zeVars.${name}`).replace(ZE_REGEX_IMPORT_META_SIMPLE, (_m, name)=>`__ZE_MANIFEST__.zeVars.${name}`).replace(ZE_REGEX_IMPORT_META_QUOTED, (_m, name)=>`__ZE_MANIFEST__.zeVars.${name}`);
code = code.replace(ZE_DESTRUCT_DECL, (m, decl, inner)=>{
const keys = extractDestructuredKeys(String(inner));
if (!keys.some((k)=>k.startsWith('ZE_PUBLIC_'))) return m;
return `${decl} {${inner}} = __ZE_MANIFEST__.zeVars`;
});
code = code.replace(ZE_DESTRUCT_ASSIGN, (m, inner)=>{
const keys = extractDestructuredKeys(String(inner));
if (!keys.some((k)=>k.startsWith('ZE_PUBLIC_'))) return m;
return `{${inner}} = __ZE_MANIFEST__.zeVars`;
});
const escaped = specifier.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const importRe = new RegExp(`from\\s+['"]${escaped}['"];?|require\\(['"]${escaped}['"]\\)`, 'm');
if (!importRe.test(code)) code = `import __ZE_MANIFEST__ from '${specifier}' with { type: 'json' };\n` + code;
return {
code,
used
};
}
function buildImportMap(specifier, url) {
return JSON.stringify({
imports: {
[specifier]: url
}
}, null, 0);
}
function injectImportMap(html, importMapJson, opts) {
const scriptTag = `<script type="importmap">${importMapJson}</script>`;
if (html.includes(scriptTag)) return html;
const injectTo = opts?.injectTo ?? 'head-prepend';
if ('body' === injectTo) return html.replace('</body>', `${scriptTag}</body>`);
return html.replace('</head>', `${scriptTag}</head>`);
}
function buildModulePreload(url) {
return `<link rel="modulepreload" href="${url}">`;
}
function injectBeforeHeadClose(html, snippet) {
if (html.includes(snippet)) return html;
return html.replace('</head>', `${snippet}</head>`);
}
function buildEnvJsonAsset(env) {
const safe = {};
for (const [k, v] of Object.entries(env))if (k.startsWith('ZE_PUBLIC_')) {
if ('string' == typeof v) safe[k] = v;
}
const source = JSON.stringify(safe);
let h = 5381;
for(let i = 0; i < source.length; i++)h = (h << 5) + h ^ source.charCodeAt(i);
const short = (h >>> 0).toString(36);
return {
fileName: `ze-env.${short}.json`,
source
};
}
function generateManifestContent(envVars, dependencies = {}) {
const publicVars = {};
for (const [key, value] of Object.entries(envVars))if (key.startsWith('ZE_PUBLIC_')) publicVars[key] = value;
const manifest = {
version: '1.0.0',
timestamp: new Date().toISOString(),
dependencies: dependencies,
zeVars: publicVars
};
return JSON.stringify(manifest, null, 2);
}
function calculateManifestHash(content) {
try {
const crypto = __webpack_require__("crypto?c60e");
return crypto.createHash('sha256').update(content).digest('hex');
} catch {
let h = 5381;
for(let i = 0; i < content.length; i++)h = (h << 5) + h ^ content.charCodeAt(i);
return (h >>> 0).toString(36);
}
}
function collectZEPublicVars(env) {
const vars = {};
for (const [key, value] of Object.entries(env))if (key.startsWith('ZE_PUBLIC_') && 'string' == typeof value) vars[key] = value;
return vars;
}
export { VIRTUAL_SPECIFIER, buildEnvJsonAsset, buildImportMap, buildModulePreload, calculateManifestHash, collectZEPublicVars, detectEnvReads, generateManifestContent, injectBeforeHeadClose, injectImportMap, rewriteEnvReadsToVirtualModule };
//# sourceMappingURL=env-var-rewrites.mjs.map