silgi 0.24.15 → 0.24.17
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/dist/cli/build/prepare.mjs +43 -0
- package/dist/cli/{dev.mjs → commands/dev.mjs} +4 -43
- package/dist/cli/commands/env.mjs +40 -0
- package/dist/cli/{install.mjs → commands/install.mjs} +3 -37
- package/dist/cli/commands/prepare.mjs +66 -0
- package/dist/cli/{prepare.mjs → commands/run.mjs} +9 -82
- package/dist/cli/config/defaults.mjs +129 -0
- package/dist/cli/config/index.mjs +2 -16
- package/dist/cli/config/loader.mjs +97 -0
- package/dist/cli/config/resolvers/compatibility.mjs +90 -0
- package/dist/cli/config/resolvers/imports.mjs +96 -0
- package/dist/cli/config/resolvers/paths.mjs +167 -0
- package/dist/cli/config/resolvers/storage.mjs +25 -0
- package/dist/cli/config/resolvers/url.mjs +7 -0
- package/dist/cli/config/types.mjs +191 -0
- package/dist/cli/core/app.mjs +92 -0
- package/dist/cli/core/devServer.mjs +8 -0
- package/dist/cli/core/env.mjs +76 -0
- package/dist/cli/core/installPackage.mjs +31 -0
- package/dist/cli/core/prepare.mjs +7 -0
- package/dist/cli/core/runtimeConfig.mjs +14 -0
- package/dist/cli/core/scan.mjs +39 -0
- package/dist/cli/core/silgi.mjs +125 -0
- package/dist/cli/core/templates.mjs +31 -0
- package/dist/cli/framework/emptyFramework.mjs +9 -0
- package/dist/cli/framework/h3.mjs +89 -0
- package/dist/cli/framework/index.mjs +8 -0
- package/dist/cli/framework/nitro.mjs +106 -0
- package/dist/cli/framework/nuxt.mjs +9 -0
- package/dist/cli/index.mjs +6 -9
- package/dist/cli/module/exportScan.mjs +130 -0
- package/dist/cli/module/install.mjs +59 -0
- package/dist/cli/module/scan.mjs +227 -0
- package/dist/cli/scan/commands.mjs +47 -0
- package/dist/cli/scan/prepareConfigs.mjs +46 -0
- package/dist/cli/scan/prepareCoreFile.mjs +166 -0
- package/dist/cli/scan/prepareFramework.mjs +73 -0
- package/dist/cli/scan/prepareSchema.mjs +177 -0
- package/dist/cli/scan/prepareServerFiles.mjs +119 -0
- package/dist/cli/scan/scanExportFile.mjs +155 -0
- package/dist/cli/scan/writeCoreFile.mjs +34 -0
- package/dist/cli/scan/writeScanFiles.mjs +30 -0
- package/dist/cli/scan/writeTypesAndFiles.mjs +109 -0
- package/dist/cli/utils/build-uri-map.mjs +39 -0
- package/dist/cli/utils/common.mjs +13 -0
- package/dist/cli/{compatibility.mjs → utils/compatibility.mjs} +1 -1
- package/dist/cli/utils/generateRouterDTS.mjs +80 -0
- package/dist/cli/utils/ignore.mjs +46 -0
- package/dist/cli/utils/parser.mjs +168 -0
- package/dist/cli/utils/readScanFile.mjs +79 -0
- package/dist/cli/utils/storage.mjs +25 -0
- package/dist/core/createSilgi.mjs +76 -0
- package/dist/core/error.mjs +227 -0
- package/dist/core/index.mjs +8 -0
- package/dist/core/routeRules.mjs +288 -0
- package/dist/core/silgi.mjs +128 -0
- package/dist/core/silgiApp.mjs +25 -0
- package/dist/core/storage.mjs +15 -0
- package/dist/core/unctx.mjs +27 -0
- package/dist/core/uris/uri.mjs +33 -0
- package/dist/core/uris/utils.mjs +130 -0
- package/dist/core/utils/event.mjs +15 -0
- package/dist/core/utils/merge.mjs +29 -0
- package/dist/core/utils/runtime.mjs +22 -0
- package/dist/core/utils/schema.mjs +5 -0
- package/dist/core/utils/service.mjs +5 -0
- package/dist/core/utils/shared.mjs +5 -0
- package/dist/core/utils/storage.mjs +72 -0
- package/dist/index.mjs +20 -793
- package/dist/kit/errors.mjs +11 -0
- package/dist/kit/esm.mjs +14 -0
- package/dist/kit/fs.mjs +30 -0
- package/dist/kit/gen.mjs +44 -0
- package/dist/kit/hash.mjs +8 -0
- package/dist/kit/index.mjs +15 -528
- package/dist/kit/isFramework.mjs +25 -0
- package/dist/kit/logger.mjs +7 -0
- package/dist/kit/module.mjs +73 -0
- package/dist/kit/parseServices.mjs +57 -0
- package/dist/kit/path.mjs +34 -0
- package/dist/kit/preset.mjs +6 -0
- package/dist/kit/resolve.mjs +80 -0
- package/dist/kit/template.mjs +49 -0
- package/dist/kit/useRequest.mjs +91 -0
- package/dist/kit/utils.mjs +43 -0
- package/dist/package.json.mjs +5 -0
- package/dist/types/index.d.mts +1 -1
- package/package.json +1 -1
- package/dist/_chunks/silgiApp.mjs +0 -2682
- package/dist/cli/types.mjs +0 -772
- /package/dist/cli/{init.mjs → commands/init.mjs} +0 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { defu } from 'defu';
|
|
2
|
+
import { tryUseSilgiCLI, useSilgiCLI } from 'silgi';
|
|
3
|
+
import { checkSilgiCompatibility } from '../cli/utils/compatibility.mjs';
|
|
4
|
+
|
|
5
|
+
function defineSilgiModule(definition) {
|
|
6
|
+
if (definition) {
|
|
7
|
+
return _defineSilgiModule(definition);
|
|
8
|
+
}
|
|
9
|
+
return {
|
|
10
|
+
with: (definition2) => _defineSilgiModule(definition2)
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function _defineSilgiModule(definition) {
|
|
14
|
+
if (typeof definition === "function") {
|
|
15
|
+
return _defineSilgiModule({ setup: definition });
|
|
16
|
+
}
|
|
17
|
+
const module = defu(definition, { meta: {} });
|
|
18
|
+
module.meta.configKey ||= module.meta.name;
|
|
19
|
+
async function getOptions(inlineOptions, silgi = useSilgiCLI()) {
|
|
20
|
+
const nuxtConfigOptionsKey = module.meta.configKey || module.meta.name;
|
|
21
|
+
const nuxtConfigOptions = nuxtConfigOptionsKey && nuxtConfigOptionsKey in silgi.options ? silgi.options[nuxtConfigOptionsKey] : {};
|
|
22
|
+
const optionsDefaults = typeof module.defaults === "function" ? await module.defaults(silgi) : module.defaults ?? {};
|
|
23
|
+
const options = defu(inlineOptions, nuxtConfigOptions, optionsDefaults);
|
|
24
|
+
return Promise.resolve(options);
|
|
25
|
+
}
|
|
26
|
+
async function silgiNormalizedModule(inlineOptions, silgi = tryUseSilgiCLI()) {
|
|
27
|
+
if (!silgi) {
|
|
28
|
+
throw new TypeError("Cannot use module outside of Silgi context");
|
|
29
|
+
}
|
|
30
|
+
const uniqueKey = module.meta.configKey || module.meta.name;
|
|
31
|
+
if (uniqueKey && !silgi.options.isPreparingModules) {
|
|
32
|
+
silgi._requiredModules ||= {};
|
|
33
|
+
if (silgi._requiredModules[uniqueKey]) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
silgi._requiredModules[uniqueKey] = true;
|
|
37
|
+
}
|
|
38
|
+
if (module.meta.compatibility) {
|
|
39
|
+
const issues = await checkSilgiCompatibility(module.meta.compatibility, silgi);
|
|
40
|
+
if (issues.length) {
|
|
41
|
+
silgi.logger.warn(`Module \`${module.meta.name}\` is disabled due to incompatibility issues:
|
|
42
|
+
${issues.toString()}`);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const _options = await getOptions(inlineOptions, silgi);
|
|
47
|
+
if (module.hooks) {
|
|
48
|
+
silgi.hooks.addHooks(module.hooks);
|
|
49
|
+
}
|
|
50
|
+
const start = performance.now();
|
|
51
|
+
const res = await module.setup?.call(null, _options, silgi) ?? {};
|
|
52
|
+
const perf = performance.now() - start;
|
|
53
|
+
const setupTime = Math.round(perf * 100) / 100;
|
|
54
|
+
if (setupTime > 5e3 && uniqueKey !== "@silgi/telemetry") {
|
|
55
|
+
silgi.logger.warn(`Slow module \`${uniqueKey || "<no name>"}\` took \`${setupTime}ms\` to setup.`);
|
|
56
|
+
} else if (silgi.options.debug) {
|
|
57
|
+
silgi.logger.info(`Module \`${uniqueKey || "<no name>"}\` took \`${setupTime}ms\` to setup.`);
|
|
58
|
+
}
|
|
59
|
+
if (res === false) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return defu(res, {
|
|
63
|
+
timings: {
|
|
64
|
+
setup: setupTime
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
silgiNormalizedModule.getMeta = () => Promise.resolve(module.meta);
|
|
69
|
+
silgiNormalizedModule.getOptions = getOptions;
|
|
70
|
+
return silgiNormalizedModule;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export { _defineSilgiModule, defineSilgiModule };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { defu } from 'defu';
|
|
2
|
+
import { withLeadingSlash } from 'ufo';
|
|
3
|
+
|
|
4
|
+
function serviceParseModule(params) {
|
|
5
|
+
return params;
|
|
6
|
+
}
|
|
7
|
+
const parseRouteRules = serviceParseModule(({
|
|
8
|
+
node,
|
|
9
|
+
basePath,
|
|
10
|
+
silgi,
|
|
11
|
+
pathLength
|
|
12
|
+
}) => {
|
|
13
|
+
const routeRules = node.routeRules;
|
|
14
|
+
if (!routeRules)
|
|
15
|
+
return;
|
|
16
|
+
let removeMethod = "";
|
|
17
|
+
removeMethod = withLeadingSlash(basePath);
|
|
18
|
+
if (pathLength === 1 || pathLength === 2 || pathLength === 3) {
|
|
19
|
+
if (routeRules.doubleSplat === void 0) {
|
|
20
|
+
routeRules.doubleSplat = true;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (routeRules.splat)
|
|
24
|
+
removeMethod = `${removeMethod}/*`;
|
|
25
|
+
else if (routeRules.doubleSplat)
|
|
26
|
+
removeMethod = `${removeMethod}/**`;
|
|
27
|
+
removeMethod = removeMethod.replace(/\/{2,}/g, "/");
|
|
28
|
+
delete routeRules.splat;
|
|
29
|
+
delete routeRules.doubleSplat;
|
|
30
|
+
silgi.routeRules.addRule(removeMethod, routeRules);
|
|
31
|
+
});
|
|
32
|
+
function parseServices(silgi, pathLength = [4, 3, 2, 1], servicesObject, currentPath = []) {
|
|
33
|
+
const modulesURIs = {};
|
|
34
|
+
silgi.options.serviceParseModules.push(parseRouteRules);
|
|
35
|
+
const pathLengths = Array.isArray(pathLength) ? pathLength : [pathLength];
|
|
36
|
+
function traverse(node, path = []) {
|
|
37
|
+
if (!node || typeof node !== "object")
|
|
38
|
+
return;
|
|
39
|
+
if (pathLengths.includes(path.length)) {
|
|
40
|
+
const basePath = path.join("/");
|
|
41
|
+
for (const handler of silgi.options.serviceParseModules) {
|
|
42
|
+
handler({ node, basePath, silgi, modulesURIs, pathLength: path.length });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
for (const key in node) {
|
|
46
|
+
if (!["_type", "fields"].includes(key)) {
|
|
47
|
+
traverse(node[key], [...path, key]);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
traverse(servicesObject || silgi.services, currentPath);
|
|
52
|
+
silgi.options.serviceParseModules = [];
|
|
53
|
+
silgi.modulesURIs = defu(modulesURIs, silgi.modulesURIs);
|
|
54
|
+
return modulesURIs;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { parseRouteRules, parseServices, serviceParseModule };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import consola from 'consola';
|
|
2
|
+
import { colors } from 'consola/utils';
|
|
3
|
+
import { getProperty } from 'dot-prop';
|
|
4
|
+
import { relative, resolve } from 'pathe';
|
|
5
|
+
|
|
6
|
+
function prettyPath(p, highlight = true) {
|
|
7
|
+
p = relative(process.cwd(), p);
|
|
8
|
+
return highlight ? colors.cyan(p) : p;
|
|
9
|
+
}
|
|
10
|
+
function resolveSilgiPath(path, silgiCLIOptions, base) {
|
|
11
|
+
if (typeof path !== "string") {
|
|
12
|
+
throw new TypeError(`Invalid path: ${path}`);
|
|
13
|
+
}
|
|
14
|
+
path = _compilePathTemplate(path)(silgiCLIOptions);
|
|
15
|
+
for (const base2 in silgiCLIOptions.alias) {
|
|
16
|
+
if (path.startsWith(base2)) {
|
|
17
|
+
path = silgiCLIOptions.alias[base2] + path.slice(base2.length);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return resolve(base || silgiCLIOptions.srcDir, path);
|
|
21
|
+
}
|
|
22
|
+
function _compilePathTemplate(contents) {
|
|
23
|
+
return (params) => contents.replace(/\{\{ ?([\w.]+) ?\}\}/g, (_, match) => {
|
|
24
|
+
const val = getProperty(params, match);
|
|
25
|
+
if (!val) {
|
|
26
|
+
consola.warn(
|
|
27
|
+
`cannot resolve template param '${match}' in ${contents.slice(0, 20)}`
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
return val || `${match}`;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export { prettyPath, resolveSilgiPath };
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { existsSync, promises } from 'node:fs';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
import { resolvePath as resolvePath$1 } from 'mlly';
|
|
4
|
+
import { dirname, resolve, normalize, isAbsolute, join } from 'pathe';
|
|
5
|
+
import { resolveAlias as resolveAlias$1 } from 'pathe/utils';
|
|
6
|
+
import { tryUseSilgiCLI } from 'silgi';
|
|
7
|
+
|
|
8
|
+
async function resolvePath(path, opts = {}) {
|
|
9
|
+
const _path = path;
|
|
10
|
+
path = normalize(path);
|
|
11
|
+
if (isAbsolute(path)) {
|
|
12
|
+
if (existsSync(path) && !await isDirectory(path)) {
|
|
13
|
+
return path;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const cwd = opts.cwd || process.cwd();
|
|
17
|
+
const extensions = opts.extensions || [".ts", ".mjs", ".cjs", ".json"];
|
|
18
|
+
const modulesDir = opts.modulesDir || [];
|
|
19
|
+
path = resolveAlias(path);
|
|
20
|
+
if (!isAbsolute(path)) {
|
|
21
|
+
path = resolve(cwd, path);
|
|
22
|
+
}
|
|
23
|
+
let _isDir = false;
|
|
24
|
+
if (existsSync(path)) {
|
|
25
|
+
_isDir = await isDirectory(path);
|
|
26
|
+
if (!_isDir) {
|
|
27
|
+
return path;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
for (const ext of extensions) {
|
|
31
|
+
const pathWithExt = path + ext;
|
|
32
|
+
if (existsSync(pathWithExt)) {
|
|
33
|
+
return pathWithExt;
|
|
34
|
+
}
|
|
35
|
+
const pathWithIndex = join(path, `index${ext}`);
|
|
36
|
+
if (_isDir && existsSync(pathWithIndex)) {
|
|
37
|
+
return pathWithIndex;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const resolveModulePath = await resolvePath$1(_path, { url: [cwd, ...modulesDir] }).catch(() => null);
|
|
41
|
+
if (resolveModulePath) {
|
|
42
|
+
return resolveModulePath;
|
|
43
|
+
}
|
|
44
|
+
return opts.fallbackToOriginal ? _path : path;
|
|
45
|
+
}
|
|
46
|
+
async function isDirectory(path) {
|
|
47
|
+
return (await promises.lstat(path)).isDirectory();
|
|
48
|
+
}
|
|
49
|
+
function resolveAlias(path, alias) {
|
|
50
|
+
alias ||= tryUseSilgiCLI()?.options.alias || {};
|
|
51
|
+
return resolveAlias$1(path, alias || {});
|
|
52
|
+
}
|
|
53
|
+
function createResolver(base) {
|
|
54
|
+
if (!base) {
|
|
55
|
+
throw new Error("`base` argument is missing for createResolver(base)!");
|
|
56
|
+
}
|
|
57
|
+
base = base.toString();
|
|
58
|
+
if (base.startsWith("file://")) {
|
|
59
|
+
base = dirname(fileURLToPath(base));
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
resolve: (...path) => resolve(base, ...path),
|
|
63
|
+
resolvePath: (path, opts) => resolvePath(path, { cwd: base, ...opts })
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
async function resolveSilgiModule(base, paths) {
|
|
67
|
+
const resolved = [];
|
|
68
|
+
const resolver = createResolver(base);
|
|
69
|
+
for (const path of paths) {
|
|
70
|
+
if (path.startsWith(base)) {
|
|
71
|
+
resolved.push(path.split("/index.ts")[0]);
|
|
72
|
+
} else {
|
|
73
|
+
const resolvedPath = await resolver.resolvePath(path);
|
|
74
|
+
resolved.push(resolvedPath.slice(0, resolvedPath.lastIndexOf(path) + path.length));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return resolved;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export { createResolver, resolveAlias, resolvePath, resolveSilgiModule };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { parse, basename, resolve } from 'pathe';
|
|
3
|
+
import { useSilgiCLI } from 'silgi';
|
|
4
|
+
import { hash } from 'silgi/kit';
|
|
5
|
+
import { filterInPlace } from './utils.mjs';
|
|
6
|
+
|
|
7
|
+
function addTemplate(_template) {
|
|
8
|
+
const silgi = useSilgiCLI();
|
|
9
|
+
const template = normalizeTemplate(_template);
|
|
10
|
+
filterInPlace(silgi.options.build.templates, (p) => normalizeTemplate(p).dst !== template.dst);
|
|
11
|
+
silgi.options.build.templates.push(template);
|
|
12
|
+
return template;
|
|
13
|
+
}
|
|
14
|
+
function normalizeTemplate(template, buildDir) {
|
|
15
|
+
if (!template) {
|
|
16
|
+
throw new Error(`Invalid template: ${JSON.stringify(template)}`);
|
|
17
|
+
}
|
|
18
|
+
if (typeof template === "string") {
|
|
19
|
+
template = { src: template };
|
|
20
|
+
} else {
|
|
21
|
+
template = { ...template };
|
|
22
|
+
}
|
|
23
|
+
if (template.src) {
|
|
24
|
+
if (!existsSync(template.src)) {
|
|
25
|
+
throw new Error(`Template not found: ${template.src}`);
|
|
26
|
+
}
|
|
27
|
+
if (!template.filename) {
|
|
28
|
+
const srcPath = parse(template.src);
|
|
29
|
+
template.filename = template.fileName || `${basename(srcPath.dir)}.${srcPath.name}.${hash(template.src)}${srcPath.ext}`;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (!template.src && !template.getContents) {
|
|
33
|
+
throw new Error(`Invalid template. Either \`getContents\` or \`src\` should be provided: ${JSON.stringify(template)}`);
|
|
34
|
+
}
|
|
35
|
+
if (!template.filename) {
|
|
36
|
+
throw new Error(`Invalid template. \`filename\` must be provided: ${JSON.stringify(template)}`);
|
|
37
|
+
}
|
|
38
|
+
if (template.filename.endsWith(".d.ts")) {
|
|
39
|
+
template.write = true;
|
|
40
|
+
}
|
|
41
|
+
if (!template.dst) {
|
|
42
|
+
const silgi = useSilgiCLI();
|
|
43
|
+
const dir = template.where === ".silgi" ? silgi.options.build.dir : template.where === "server" ? silgi.options.silgi.serverDir : template.where === "client" ? silgi.options.silgi.clientDir : silgi.options.silgi.serverDir;
|
|
44
|
+
template.dst = resolve(buildDir ?? dir, template.filename);
|
|
45
|
+
}
|
|
46
|
+
return template;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { addTemplate, normalizeTemplate };
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { useSilgi } from 'silgi';
|
|
2
|
+
|
|
3
|
+
function useRequest(event) {
|
|
4
|
+
const silgi = useSilgi();
|
|
5
|
+
if (silgi.options.present === "nuxt" || silgi.options.present === "nitro" || silgi.options.present === "h3") {
|
|
6
|
+
return event.node.req;
|
|
7
|
+
}
|
|
8
|
+
return event;
|
|
9
|
+
}
|
|
10
|
+
function useResponse(event) {
|
|
11
|
+
const silgi = useSilgi();
|
|
12
|
+
if (silgi.options.present === "nuxt" || silgi.options.present === "nitro" || silgi.options.present === "h3") {
|
|
13
|
+
return event.node.res;
|
|
14
|
+
}
|
|
15
|
+
return event;
|
|
16
|
+
}
|
|
17
|
+
function getIpAddress(event) {
|
|
18
|
+
const silgi = useSilgi();
|
|
19
|
+
if (silgi.options.present === "nuxt" || silgi.options.present === "nitro" || silgi.options.present === "h3") {
|
|
20
|
+
const _ipAddress = ipAddress(event.node.req);
|
|
21
|
+
return _ipAddress;
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
function ipAddress(req) {
|
|
26
|
+
const headers = [
|
|
27
|
+
"cf-connecting-ip",
|
|
28
|
+
// Cloudflare
|
|
29
|
+
"true-client-ip",
|
|
30
|
+
// Akamai and Cloudflare
|
|
31
|
+
"x-real-ip",
|
|
32
|
+
// Nginx proxy/FastCGI
|
|
33
|
+
"x-forwarded-for",
|
|
34
|
+
// Standard proxy header
|
|
35
|
+
"x-client-ip",
|
|
36
|
+
// Apache
|
|
37
|
+
"x-forwarded",
|
|
38
|
+
// Alternative format
|
|
39
|
+
"forwarded",
|
|
40
|
+
// RFC 7239 compliant format
|
|
41
|
+
"forwarded-for",
|
|
42
|
+
// RFC 7239 variant
|
|
43
|
+
"x-appengine-user-ip",
|
|
44
|
+
// Google App Engine
|
|
45
|
+
"x-cluster-client-ip",
|
|
46
|
+
// GCP load balancer
|
|
47
|
+
"fastly-client-ip",
|
|
48
|
+
// Fastly CDN
|
|
49
|
+
"x-azure-clientip",
|
|
50
|
+
// Azure
|
|
51
|
+
"x-original-forwarded-for",
|
|
52
|
+
// Secondary forwarded-for header
|
|
53
|
+
"x-lb-forwarded-for",
|
|
54
|
+
// Load balancer specific
|
|
55
|
+
"cdn-loop"
|
|
56
|
+
// Used by some CDNs
|
|
57
|
+
];
|
|
58
|
+
for (const header of headers) {
|
|
59
|
+
const value = req.headers[header];
|
|
60
|
+
if (!value)
|
|
61
|
+
continue;
|
|
62
|
+
let ip;
|
|
63
|
+
if (Array.isArray(value)) {
|
|
64
|
+
ip = value[0];
|
|
65
|
+
} else if (header === "x-forwarded-for" || header === "forwarded-for" || header === "x-original-forwarded-for") {
|
|
66
|
+
ip = value.split(",")[0];
|
|
67
|
+
} else if (header === "forwarded") {
|
|
68
|
+
const match = value.match(/for=([^;]+)/);
|
|
69
|
+
ip = match ? match[1].trim().replace(/^\[|\]$/g, "") : "";
|
|
70
|
+
} else {
|
|
71
|
+
ip = value;
|
|
72
|
+
}
|
|
73
|
+
const cleanIp = ip.trim();
|
|
74
|
+
if (cleanIp && isValidIp(cleanIp)) {
|
|
75
|
+
return cleanIp;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const socketIp = req.socket.remoteAddress ?? "";
|
|
79
|
+
return socketIp && isValidIp(socketIp) ? socketIp : "";
|
|
80
|
+
}
|
|
81
|
+
function isValidIp(ip) {
|
|
82
|
+
if (ip === "::1" || ip === "localhost" || ip === "127.0.0.1")
|
|
83
|
+
return true;
|
|
84
|
+
if (/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(ip))
|
|
85
|
+
return true;
|
|
86
|
+
if (/^[0-9a-f:]+$/i.test(ip))
|
|
87
|
+
return true;
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export { getIpAddress, ipAddress, useRequest, useResponse };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { relative } from 'pathe';
|
|
2
|
+
import { useSilgiCLI } from 'silgi';
|
|
3
|
+
|
|
4
|
+
const RELATIVE_RE = /^([^.])/;
|
|
5
|
+
function relativeWithDot(from, to) {
|
|
6
|
+
return relative(from, to).replace(RELATIVE_RE, "./$1") || ".";
|
|
7
|
+
}
|
|
8
|
+
function toArray(value) {
|
|
9
|
+
return Array.isArray(value) ? value : [value];
|
|
10
|
+
}
|
|
11
|
+
function filterInPlace(array, predicate) {
|
|
12
|
+
for (let i = array.length; i--; i >= 0) {
|
|
13
|
+
if (!predicate(array[i], i, array)) {
|
|
14
|
+
array.splice(i, 1);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return array;
|
|
18
|
+
}
|
|
19
|
+
const MODE_RE = /\.(server|client)(\.\w+)*$/;
|
|
20
|
+
function hasSilgiModule(moduleKey, silgi = useSilgiCLI()) {
|
|
21
|
+
return silgi.scanModules.some(({ meta }) => meta.configKey === moduleKey) || Object.keys(silgi.scanModules).includes(moduleKey);
|
|
22
|
+
}
|
|
23
|
+
function hasInstalledModule(moduleKey, silgi = useSilgiCLI()) {
|
|
24
|
+
const find = silgi.scanModules.find(({ meta }) => meta.configKey === moduleKey);
|
|
25
|
+
return find?.installed ?? false;
|
|
26
|
+
}
|
|
27
|
+
const baseHeaderBannerComment = [
|
|
28
|
+
"// DO NOT EDIT THIS FILE",
|
|
29
|
+
"// This file is generated by Silgi",
|
|
30
|
+
"/* eslint-disable */",
|
|
31
|
+
"/* prettier-ignore */",
|
|
32
|
+
"/* tslint:disable */"
|
|
33
|
+
];
|
|
34
|
+
function processFilePath(src) {
|
|
35
|
+
const silgi = useSilgiCLI();
|
|
36
|
+
if (silgi.options.typescript.removeFileExtension) {
|
|
37
|
+
src = src.replace(/\.ts$/, "");
|
|
38
|
+
return src;
|
|
39
|
+
}
|
|
40
|
+
return src;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { MODE_RE, baseHeaderBannerComment, filterInPlace, hasInstalledModule, hasSilgiModule, processFilePath, relativeWithDot, toArray };
|
package/dist/types/index.d.mts
CHANGED
|
@@ -578,7 +578,7 @@ interface SilgiCLIOptions extends PresetOptions {
|
|
|
578
578
|
commandType: CommandType;
|
|
579
579
|
routeRules: SilgiRouteRules$1;
|
|
580
580
|
environments: DotenvOptions$1[];
|
|
581
|
-
activeEnvironment: string;
|
|
581
|
+
activeEnvironment: 'dev' | 'docker' | 'staging' | 'testing' | '.env' | (string & {});
|
|
582
582
|
envOptions: EnvOptions$1;
|
|
583
583
|
runtimeConfig: SilgiRuntimeConfig$1 & {
|
|
584
584
|
[key: string]: any;
|