@fantasticfour/dotenvx-next 0.1.3 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +157 -0
- package/dist/chunk-CKQMccvm.cjs +28 -0
- package/dist/index.cjs +6 -0
- package/dist/index.d.cts +2 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +2 -1
- package/dist/next-env-compat.cjs +29 -11858
- package/dist/next-env-compat.cjs.map +1 -1
- package/dist/next-env-compat.d.cts +9 -1
- package/dist/next-env-compat.d.cts.map +1 -0
- package/dist/next-env-compat.d.mts +9 -0
- package/dist/next-env-compat.d.mts.map +1 -0
- package/dist/next-env-compat.mjs +31 -0
- package/dist/next-env-compat.mjs.map +1 -0
- package/dist/plugin.cjs +29 -53
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.d.cts +2 -7
- package/dist/plugin.d.cts.map +1 -1
- package/dist/plugin.d.mts +2 -7
- package/dist/plugin.d.mts.map +1 -1
- package/dist/plugin.mjs +25 -52
- package/dist/plugin.mjs.map +1 -1
- package/dist/turbopack-inject.cjs +95 -4
- package/dist/turbopack-inject.cjs.map +1 -0
- package/dist/turbopack-inject.d.cts +4 -4
- package/dist/turbopack-inject.d.cts.map +1 -1
- package/dist/turbopack-inject.d.mts +4 -4
- package/dist/turbopack-inject.d.mts.map +1 -1
- package/dist/turbopack-inject.mjs +12 -14
- package/dist/turbopack-inject.mjs.map +1 -1
- package/dist/webpack-plugin-BmC1uBZp.mjs +47 -0
- package/dist/webpack-plugin-BmC1uBZp.mjs.map +1 -0
- package/dist/webpack-plugin.cjs +3 -11
- package/dist/webpack-plugin.cjs.map +1 -1
- package/dist/webpack-plugin.d.cts +7 -10
- package/dist/webpack-plugin.d.cts.map +1 -1
- package/dist/webpack-plugin.d.mts +7 -10
- package/dist/webpack-plugin.d.mts.map +1 -1
- package/dist/webpack-plugin.mjs +1 -51
- package/package.json +18 -19
- package/dist/chunk-y_GBKt04.mjs +0 -5
- package/dist/init.cjs +0 -16
- package/dist/init.cjs.map +0 -1
- package/dist/init.d.cts +0 -5
- package/dist/init.d.cts.map +0 -1
- package/dist/init.d.mts +0 -5
- package/dist/init.d.mts.map +0 -1
- package/dist/init.mjs +0 -18
- package/dist/init.mjs.map +0 -1
- package/dist/turbopack-inject-BgCxanwa.cjs +0 -139
- package/dist/turbopack-inject-BgCxanwa.cjs.map +0 -1
- package/dist/webpack-plugin.mjs.map +0 -1
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
//#region \0rolldown/runtime.js
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __copyProps = (to, from, except, desc) => {
|
|
9
|
-
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
-
key = keys[i];
|
|
11
|
-
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
-
get: ((k) => from[k]).bind(null, key),
|
|
13
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
-
value: mod,
|
|
20
|
-
enumerable: true
|
|
21
|
-
}) : target, mod));
|
|
22
|
-
//#endregion
|
|
23
|
-
let node_fs = require("node:fs");
|
|
24
|
-
node_fs = __toESM(node_fs, 1);
|
|
25
|
-
let node_path = require("node:path");
|
|
26
|
-
node_path = __toESM(node_path, 1);
|
|
27
|
-
//#region src/turbopack-inject.ts
|
|
28
|
-
/**
|
|
29
|
-
* Turbopack runtime injection — patches fs.writeFile/fs.writeFileSync to intercept
|
|
30
|
-
* turbopack's build output and prepend the dotenvx init bundle into runtime files.
|
|
31
|
-
*
|
|
32
|
-
* Turbopack writes `[turbopack]_runtime.js` directly (bypassing webpack), so we
|
|
33
|
-
* can't use the processAssets hook. Instead we intercept fs writes and inject when
|
|
34
|
-
* export-detail.json is written (signals compilation is complete).
|
|
35
|
-
*/
|
|
36
|
-
function debugLog(...args) {
|
|
37
|
-
if (!process.env.DEBUG_DOTENVX_NEXT) return;
|
|
38
|
-
console.log("[dotenvx-next]", ...args);
|
|
39
|
-
}
|
|
40
|
-
let injectedTurbopackRuntime = false;
|
|
41
|
-
function isInjectedTurbopackRuntime() {
|
|
42
|
-
return injectedTurbopackRuntime;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Inject the dotenvx init bundle into turbopack runtime files.
|
|
46
|
-
* Called after export-detail.json is written (turbopack compilation complete).
|
|
47
|
-
*/
|
|
48
|
-
function injectDotenvxInitIntoTurbopackRuntime(nextDirPath, initSource) {
|
|
49
|
-
if (injectedTurbopackRuntime) return;
|
|
50
|
-
const serverRuntimeFiles = [];
|
|
51
|
-
const edgeWrapperFiles = [];
|
|
52
|
-
const walkDir = (dir) => {
|
|
53
|
-
if (!node_fs.default.existsSync(dir)) return;
|
|
54
|
-
for (const entry of node_fs.default.readdirSync(dir, { withFileTypes: true })) if (entry.isDirectory()) walkDir(node_path.default.join(dir, entry.name));
|
|
55
|
-
else if (entry.name === "[turbopack]_runtime.js") serverRuntimeFiles.push(node_path.default.join(dir, entry.name));
|
|
56
|
-
else if (entry.name.includes("edge-wrapper") && entry.name.endsWith(".js")) edgeWrapperFiles.push(node_path.default.join(dir, entry.name));
|
|
57
|
-
};
|
|
58
|
-
walkDir(nextDirPath);
|
|
59
|
-
debugLog(`turbopack runtime injection: found ${serverRuntimeFiles.length} server runtime files,`, `${edgeWrapperFiles.length} edge wrapper files`);
|
|
60
|
-
if (!serverRuntimeFiles.length) return;
|
|
61
|
-
injectedTurbopackRuntime = true;
|
|
62
|
-
const strippedSource = initSource.replace(/\s*\/\/# sourceMappingURL=\S+\s*$/m, "");
|
|
63
|
-
const iifeWrap = (src) => `(function(exports,module){${src}\n})({},{exports:{}});`;
|
|
64
|
-
const wrappedInit = iifeWrap(strippedSource);
|
|
65
|
-
/**
|
|
66
|
-
* Insert wrappedInit into source respecting ESM constraints.
|
|
67
|
-
* ESM files require all top-level `import` declarations to appear before any
|
|
68
|
-
* other statements, so we must insert after the last import line rather than
|
|
69
|
-
* prepending to the file.
|
|
70
|
-
*/
|
|
71
|
-
const insertIntoSource = (origSource) => {
|
|
72
|
-
const lines = origSource.split("\n");
|
|
73
|
-
let lastImportIdx = -1;
|
|
74
|
-
for (let i = 0; i < lines.length; i++) if (/^import\s/.test(lines[i])) lastImportIdx = i;
|
|
75
|
-
if (lastImportIdx >= 0) {
|
|
76
|
-
lines.splice(lastImportIdx + 1, 0, wrappedInit);
|
|
77
|
-
return lines.join("\n");
|
|
78
|
-
}
|
|
79
|
-
return [wrappedInit, origSource].join("\n");
|
|
80
|
-
};
|
|
81
|
-
for (const runtimeFile of serverRuntimeFiles) {
|
|
82
|
-
const origSource = node_fs.default.readFileSync(runtimeFile, "utf8");
|
|
83
|
-
node_fs.default.writeFileSync(runtimeFile, insertIntoSource(origSource));
|
|
84
|
-
debugLog(`injected dotenvx init into turbopack server runtime: ${runtimeFile}`);
|
|
85
|
-
}
|
|
86
|
-
for (const wrapperFile of edgeWrapperFiles) {
|
|
87
|
-
const origSource = node_fs.default.readFileSync(wrapperFile, "utf8");
|
|
88
|
-
node_fs.default.writeFileSync(wrapperFile, insertIntoSource(origSource));
|
|
89
|
-
debugLog(`injected dotenvx init into turbopack edge wrapper: ${wrapperFile}`);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Patch global fs methods to detect when turbopack compilation completes,
|
|
94
|
-
* then trigger runtime injection.
|
|
95
|
-
*/
|
|
96
|
-
function activateTurbopackInjection(initSource) {
|
|
97
|
-
debugLog("activating turbopack fs intercept");
|
|
98
|
-
const origWriteFileFn = node_fs.default.promises.writeFile;
|
|
99
|
-
node_fs.default.promises.writeFile = async function dotenvxPatchedWriteFile(...args) {
|
|
100
|
-
const filePath = args[0].toString();
|
|
101
|
-
debugLog("fs.promises.writeFile:", filePath);
|
|
102
|
-
if (!injectedTurbopackRuntime && filePath.endsWith("/.next/export-detail.json")) injectDotenvxInitIntoTurbopackRuntime(filePath.substring(0, filePath.lastIndexOf("/")), initSource);
|
|
103
|
-
return origWriteFileFn.apply(node_fs.default.promises, args);
|
|
104
|
-
};
|
|
105
|
-
const origWriteFileSyncFn = node_fs.default.writeFileSync;
|
|
106
|
-
node_fs.default.writeFileSync = function dotenvxPatchedWriteFileSync(...args) {
|
|
107
|
-
const filePath = args[0].toString();
|
|
108
|
-
debugLog("fs.writeFileSync:", filePath);
|
|
109
|
-
if (!injectedTurbopackRuntime && filePath.endsWith("/.next/export-detail.json")) injectDotenvxInitIntoTurbopackRuntime(filePath.substring(0, filePath.lastIndexOf("/")), initSource);
|
|
110
|
-
origWriteFileSyncFn.apply(node_fs.default, args);
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
//#endregion
|
|
114
|
-
Object.defineProperty(exports, "__toESM", {
|
|
115
|
-
enumerable: true,
|
|
116
|
-
get: function() {
|
|
117
|
-
return __toESM;
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
Object.defineProperty(exports, "activateTurbopackInjection", {
|
|
121
|
-
enumerable: true,
|
|
122
|
-
get: function() {
|
|
123
|
-
return activateTurbopackInjection;
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
Object.defineProperty(exports, "injectDotenvxInitIntoTurbopackRuntime", {
|
|
127
|
-
enumerable: true,
|
|
128
|
-
get: function() {
|
|
129
|
-
return injectDotenvxInitIntoTurbopackRuntime;
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
Object.defineProperty(exports, "isInjectedTurbopackRuntime", {
|
|
133
|
-
enumerable: true,
|
|
134
|
-
get: function() {
|
|
135
|
-
return isInjectedTurbopackRuntime;
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
//# sourceMappingURL=turbopack-inject-BgCxanwa.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"turbopack-inject-BgCxanwa.cjs","names":["fs","path"],"sources":["../src/turbopack-inject.ts"],"sourcesContent":["/**\n * Turbopack runtime injection — patches fs.writeFile/fs.writeFileSync to intercept\n * turbopack's build output and prepend the dotenvx init bundle into runtime files.\n *\n * Turbopack writes `[turbopack]_runtime.js` directly (bypassing webpack), so we\n * can't use the processAssets hook. Instead we intercept fs writes and inject when\n * export-detail.json is written (signals compilation is complete).\n */\n\nimport fs from 'node:fs';\nimport path from 'node:path';\n\nfunction debugLog(...args: unknown[]): void {\n if (!process.env.DEBUG_DOTENVX_NEXT) return;\n console.log('[dotenvx-next]', ...args);\n}\n\nlet injectedTurbopackRuntime = false;\n\nexport function isInjectedTurbopackRuntime(): boolean {\n return injectedTurbopackRuntime;\n}\n\n/**\n * Inject the dotenvx init bundle into turbopack runtime files.\n * Called after export-detail.json is written (turbopack compilation complete).\n */\nexport function injectDotenvxInitIntoTurbopackRuntime(\n nextDirPath: string,\n initSource: string\n): void {\n if (injectedTurbopackRuntime) return;\n\n const serverRuntimeFiles: string[] = [];\n const edgeWrapperFiles: string[] = [];\n\n const walkDir = (dir: string): void => {\n if (!fs.existsSync(dir)) return;\n for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {\n if (entry.isDirectory()) {\n walkDir(path.join(dir, entry.name));\n } else if (entry.name === '[turbopack]_runtime.js') {\n serverRuntimeFiles.push(path.join(dir, entry.name));\n } else if (\n entry.name.includes('edge-wrapper') &&\n entry.name.endsWith('.js')\n ) {\n edgeWrapperFiles.push(path.join(dir, entry.name));\n }\n }\n };\n\n walkDir(nextDirPath);\n\n debugLog(\n `turbopack runtime injection: found ${serverRuntimeFiles.length} server runtime files,`,\n `${edgeWrapperFiles.length} edge wrapper files`\n );\n\n if (!serverRuntimeFiles.length) {\n // Runtime files may not exist yet — turbopack (Rust) writes them directly.\n // We'll retry on subsequent fs writes until they appear.\n return;\n }\n\n // Mark as done so we don't retry\n injectedTurbopackRuntime = true;\n\n // Strip sourcemap comment — if it appears on the last line before the closing\n // `})({},{exports:{}});`, it would comment out the IIFE closer and cause\n // \"Unexpected end of input\" at parse time.\n const strippedSource = initSource.replace(\n /\\s*\\/\\/# sourceMappingURL=\\S+\\s*$/m,\n ''\n );\n\n // Wrap in IIFE to avoid symbol collisions (CJS bundle uses exports.X = ...)\n const iifeWrap = (src: string): string =>\n `(function(exports,module){${src}\\n})({},{exports:{}});`;\n\n const wrappedInit = iifeWrap(strippedSource);\n\n /**\n * Insert wrappedInit into source respecting ESM constraints.\n * ESM files require all top-level `import` declarations to appear before any\n * other statements, so we must insert after the last import line rather than\n * prepending to the file.\n */\n const insertIntoSource = (origSource: string): string => {\n const lines = origSource.split('\\n');\n // Find the last line that starts with 'import ' (top-level import declaration)\n let lastImportIdx = -1;\n for (let i = 0; i < lines.length; i++) {\n if (/^import\\s/.test(lines[i])) {\n lastImportIdx = i;\n }\n }\n if (lastImportIdx >= 0) {\n // Insert after the last import line\n lines.splice(lastImportIdx + 1, 0, wrappedInit);\n return lines.join('\\n');\n }\n // CJS or no imports — prepend as before\n return [wrappedInit, origSource].join('\\n');\n };\n\n for (const runtimeFile of serverRuntimeFiles) {\n const origSource = fs.readFileSync(runtimeFile, 'utf8');\n fs.writeFileSync(runtimeFile, insertIntoSource(origSource));\n debugLog(\n `injected dotenvx init into turbopack server runtime: ${runtimeFile}`\n );\n }\n\n for (const wrapperFile of edgeWrapperFiles) {\n const origSource = fs.readFileSync(wrapperFile, 'utf8');\n fs.writeFileSync(wrapperFile, insertIntoSource(origSource));\n debugLog(\n `injected dotenvx init into turbopack edge wrapper: ${wrapperFile}`\n );\n }\n}\n\n/**\n * Patch global fs methods to detect when turbopack compilation completes,\n * then trigger runtime injection.\n */\nexport function activateTurbopackInjection(initSource: string): void {\n debugLog('activating turbopack fs intercept');\n\n const origWriteFileFn = fs.promises.writeFile;\n fs.promises.writeFile = async function dotenvxPatchedWriteFile(\n ...args: Parameters<typeof fs.promises.writeFile>\n ): ReturnType<typeof fs.promises.writeFile> {\n const filePath = args[0].toString();\n debugLog('fs.promises.writeFile:', filePath);\n\n if (\n !injectedTurbopackRuntime &&\n filePath.endsWith('/.next/export-detail.json')\n ) {\n const nextDirPath = filePath.substring(0, filePath.lastIndexOf('/'));\n injectDotenvxInitIntoTurbopackRuntime(nextDirPath, initSource);\n }\n\n return origWriteFileFn.apply(fs.promises, args);\n };\n\n const origWriteFileSyncFn = fs.writeFileSync;\n fs.writeFileSync = function dotenvxPatchedWriteFileSync(\n ...args: Parameters<typeof fs.writeFileSync>\n ): void {\n const filePath = args[0].toString();\n debugLog('fs.writeFileSync:', filePath);\n\n if (\n !injectedTurbopackRuntime &&\n filePath.endsWith('/.next/export-detail.json')\n ) {\n const nextDirPath = filePath.substring(0, filePath.lastIndexOf('/'));\n injectDotenvxInitIntoTurbopackRuntime(nextDirPath, initSource);\n }\n\n origWriteFileSyncFn.apply(fs, args);\n };\n}\n"],"mappings":"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"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"webpack-plugin.mjs","names":[],"sources":["../src/webpack-plugin.ts"],"sourcesContent":["/**\n * DotenvxWebpackPlugin — injects the dotenvx init bundle into webpack runtime files.\n *\n * We prepend the self-contained init bundle as raw JS so it runs before any\n * module code (before Prisma Pool, etc. can evaluate at import time).\n *\n * The __DOTENVX_FILES__ placeholder in the init source is replaced via direct\n * string substitution before injection — no DefinePlugin needed.\n */\n\n// webpack types come bundled with next — do not add webpack as a separate dep\ntype WebpackCompiler = import('webpack').Compiler;\ntype WebpackCompilation = import('webpack').Compilation;\n\nconst PLUGIN_NAME = 'DotenvxNextWebpackPlugin';\n\n// These are the webpack runtime asset names where we inject\nconst SERVER_RUNTIME_ASSETS = [\n 'webpack-runtime.js',\n '../webpack-runtime.js',\n 'webpack-api-runtime.js',\n '../webpack-api-runtime.js',\n];\n\nconst EDGE_RUNTIME_ASSETS = [\n 'edge-runtime-webpack.js',\n 'webpack-runtime.js',\n '../webpack-runtime.js',\n];\n\nexport interface DotenvxWebpackPluginOptions {\n /** Resolved list of env file paths to pass to dotenvx at runtime */\n files: string[];\n /** Pre-compiled init bundle source (CJS) to prepend into runtime */\n initSource: string;\n}\n\n/**\n * Replace the __DOTENVX_FILES__ placeholder in the init bundle with the actual\n * file list. The placeholder is a string literal in the source that gets swapped\n * with a JSON-encoded array string, so JSON.parse() in the init code yields string[].\n */\nfunction substituteFiles(initSource: string, files: string[]): string {\n // The init source contains: const filesJson: string = '__DOTENVX_FILES__';\n // We replace the placeholder string literal with the actual JSON value.\n return initSource.replace(\n /'__DOTENVX_FILES__'/g,\n JSON.stringify(JSON.stringify(files)),\n );\n}\n\nexport class DotenvxWebpackPlugin {\n private readonly options: DotenvxWebpackPluginOptions;\n\n constructor(options: DotenvxWebpackPluginOptions) {\n this.options = options;\n }\n\n apply(compiler: WebpackCompiler): void {\n const { files, initSource } = this.options;\n\n // Substitute the file list into the init bundle once, before any hooks run\n const patchedInitSource = substituteFiles(initSource, files);\n\n // Wrap in IIFE to avoid symbol collisions (CJS bundle uses exports.X = ...)\n const wrappedInit = `(function(exports,module){${patchedInitSource}})({},{exports:{}});`;\n\n // Use webpack from the compiler's context — same instance Next.js uses\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n const webpack = require('webpack') as typeof import('webpack');\n\n compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation: WebpackCompilation) => {\n compilation.hooks.processAssets.tap(\n {\n name: PLUGIN_NAME,\n stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,\n },\n () => {\n // Determine compilation target from compiler options\n const target = compiler.options.target;\n const isEdge = Array.isArray(target)\n ? target.some((t) => typeof t === 'string' && t.includes('webworker'))\n : typeof target === 'string' && target.includes('webworker');\n\n // Only inject into server-side compilations (not client bundles)\n const runtimeNames = isEdge ? EDGE_RUNTIME_ASSETS : SERVER_RUNTIME_ASSETS;\n\n for (const assetName of runtimeNames) {\n if (!compilation.getAsset(assetName)) continue;\n\n compilation.updateAsset(assetName, (origSource) => {\n const origSourceStr = origSource.source().toString();\n const updatedSourceStr = [wrappedInit, origSourceStr].join('\\n');\n return new webpack.sources.RawSource(updatedSourceStr);\n });\n }\n },\n );\n });\n }\n}\n"],"mappings":";;AAcA,MAAM,cAAc;AAGpB,MAAM,wBAAwB;CAC5B;CACA;CACA;CACA;CACD;AAED,MAAM,sBAAsB;CAC1B;CACA;CACA;CACD;;;;;;AAcD,SAAS,gBAAgB,YAAoB,OAAyB;AAGpE,QAAO,WAAW,QAChB,wBACA,KAAK,UAAU,KAAK,UAAU,MAAM,CAAC,CACtC;;AAGH,IAAa,uBAAb,MAAkC;CAGhC,YAAY,SAAsC;AAChD,OAAK,UAAU;;CAGjB,MAAM,UAAiC;EACrC,MAAM,EAAE,OAAO,eAAe,KAAK;EAMnC,MAAM,cAAc,6BAHM,gBAAgB,YAAY,MAGY,CAAC;EAInE,MAAM,UAAA,UAAkB,UAAU;AAElC,WAAS,MAAM,gBAAgB,IAAI,cAAc,gBAAoC;AACnF,eAAY,MAAM,cAAc,IAC9B;IACE,MAAM;IACN,OAAO,QAAQ,YAAY;IAC5B,QACK;IAEJ,MAAM,SAAS,SAAS,QAAQ;IAMhC,MAAM,gBALS,MAAM,QAAQ,OAAO,GAChC,OAAO,MAAM,MAAM,OAAO,MAAM,YAAY,EAAE,SAAS,YAAY,CAAC,GACpE,OAAO,WAAW,YAAY,OAAO,SAAS,YAAY,IAGhC,sBAAsB;AAEpD,SAAK,MAAM,aAAa,cAAc;AACpC,SAAI,CAAC,YAAY,SAAS,UAAU,CAAE;AAEtC,iBAAY,YAAY,YAAY,eAAe;MAEjD,MAAM,mBAAmB,CAAC,aADJ,WAAW,QAAQ,CAAC,UACU,CAAC,CAAC,KAAK,KAAK;AAChE,aAAO,IAAI,QAAQ,QAAQ,UAAU,iBAAiB;OACtD;;KAGP;IACD"}
|