silgi 0.12.4 → 0.14.0

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.
@@ -1,4 +1,4 @@
1
- const version = "0.12.4";
1
+ const version = "0.14.0";
2
2
  const peerDependencies = {
3
3
  "@fastify/deepmerge": "^2.0.2",
4
4
  "@nuxt/kit": "^3.15.3",
@@ -27,6 +27,7 @@ const SilgiCLIDefaults = {
27
27
  version: "0.0.1"
28
28
  }
29
29
  },
30
+ storages: [],
30
31
  devServer: {
31
32
  watch: []
32
33
  },
@@ -1387,7 +1387,6 @@ function useCLIRuntimeConfig(silgi) {
1387
1387
  const _sharedRuntimeConfig = initRuntimeConfig(silgi.options.envOptions, silgi.options.runtimeConfig);
1388
1388
  silgi.options.runtimeConfig = _sharedRuntimeConfig;
1389
1389
  silgi.hook("prepare:configs.ts", (data) => {
1390
- data.storage = silgi.options.storage;
1391
1390
  data.runtimeConfig = _sharedRuntimeConfig;
1392
1391
  silgi.options.envOptions = silgi.options.envOptions;
1393
1392
  });
@@ -1756,7 +1755,11 @@ async function prepareFramework(silgi) {
1756
1755
  const importItems = {
1757
1756
  "silgi/types": {
1758
1757
  import: [
1759
- { name: "SilgiRuntimeContext", type: true }
1758
+ {
1759
+ name: "SilgiRuntimeContext",
1760
+ type: true,
1761
+ key: "SilgiRuntimeContext"
1762
+ }
1760
1763
  ],
1761
1764
  from: "silgi/types"
1762
1765
  }
@@ -1781,7 +1784,11 @@ async function createDTSFramework(silgi) {
1781
1784
  const importItems = {
1782
1785
  "silgi/types": {
1783
1786
  import: [
1784
- { name: "SilgiRuntimeContext", type: true }
1787
+ {
1788
+ name: "SilgiRuntimeContext",
1789
+ type: true,
1790
+ key: "SilgiRuntimeContext"
1791
+ }
1785
1792
  ],
1786
1793
  from: "silgi/types"
1787
1794
  }
@@ -1996,11 +2003,14 @@ async function prepareSchema(silgi) {
1996
2003
  "",
1997
2004
  silgi.options.typescript.generateRuntimeConfigTypes ? generateTypes(
1998
2005
  await resolveSchema(
1999
- Object.fromEntries(
2000
- Object.entries(silgi.options.runtimeConfig).filter(
2001
- ([key]) => !["app", "nitro", "nuxt"].includes(key)
2002
- )
2003
- )
2006
+ {
2007
+ ...Object.fromEntries(
2008
+ Object.entries(silgi.options.runtimeConfig).filter(
2009
+ ([key]) => !["app", "nitro", "nuxt"].includes(key)
2010
+ )
2011
+ ),
2012
+ ...silgi.options.storages
2013
+ }
2004
2014
  ),
2005
2015
  {
2006
2016
  interfaceName: "SilgiRuntimeConfigExtends",
@@ -5,7 +5,6 @@ import { getContext } from 'unctx';
5
5
  import { Buffer } from 'node:buffer';
6
6
  import { klona } from 'klona';
7
7
  import { createStorage as createStorage$1, builtinDrivers, prefixStorage } from 'unstorage';
8
- import memoryDriver from 'unstorage/drivers/memory';
9
8
 
10
9
  const silgiCtx = getContext("silgi");
11
10
  function useSilgi() {
@@ -423,7 +422,6 @@ async function createStorage(silgi) {
423
422
  for (const [path, opts] of Object.entries(mounts)) {
424
423
  if (opts.driver) {
425
424
  const driver = await import(builtinDrivers[opts.driver] || opts.driver).then((r) => r.default || r);
426
- storage.mount("/memory:cache", memoryDriver());
427
425
  storage.mount(path, driver(opts));
428
426
  } else {
429
427
  silgi.logger.warn(`No \`driver\` set for storage mount point "${path}".`);
@@ -2,6 +2,7 @@ import { SilgiCLI, ModuleOptionsCustom, ModuleDefinition, SilgiModule, SilgiPres
2
2
  import { Buffer } from 'node:buffer';
3
3
  import * as consola from 'consola';
4
4
  import { ConsolaOptions } from 'consola';
5
+ import { IncomingMessage } from 'node:http';
5
6
 
6
7
  declare function hasError(type: SilgiCLI['errors'][0]['type'], silgi?: SilgiCLI): boolean;
7
8
 
@@ -93,6 +94,17 @@ declare function addTemplate<T>(_template: SilgiTemplate<T> | string): ResolvedS
93
94
  */
94
95
  declare function normalizeTemplate<T>(template: SilgiTemplate<T> | string, buildDir?: string): ResolvedSilgiTemplate<T>;
95
96
 
97
+ declare function useRequest(event: SilgiEvents): false | (IncomingMessage & {
98
+ originalUrl?: string;
99
+ });
100
+ declare function getIpAddress(event: SilgiEvents): string | false;
101
+ /**
102
+ * Extract the client's IP address from request headers with high accuracy
103
+ * @param req - The incoming HTTP request
104
+ * @returns The client's IP address or empty string if not found
105
+ */
106
+ declare function ipAddress(req: IncomingMessage): string;
107
+
96
108
  declare function relativeWithDot(from: string, to: string): string;
97
109
  /** @since 3.9.0 */
98
110
  declare function toArray<T>(value: T | T[]): T[];
@@ -113,4 +125,4 @@ declare const MODE_RE: RegExp;
113
125
  declare function hasSilgiModule(moduleKey: string, silgi?: SilgiCLI): boolean;
114
126
  declare function hasInstalledModule(moduleKey: string, silgi?: SilgiCLI): boolean;
115
127
 
116
- export { MODE_RE, addTemplate, applyEnv, createResolver, defineSilgiModule, defineSilgiPreset, filterInPlace, hasError, hasInstalledModule, hasSilgiModule, hash, initRuntimeConfig, isDirectory, isH3, isNitro, isNuxt, normalizeTemplate, prettyPath, relativeWithDot, resolveAlias, resolvePath, resolveSilgiModule, resolveSilgiPath, toArray, tryResolveModule, useLogger, useSilgiRuntimeConfig, writeFile };
128
+ export { MODE_RE, addTemplate, applyEnv, createResolver, defineSilgiModule, defineSilgiPreset, filterInPlace, getIpAddress, hasError, hasInstalledModule, hasSilgiModule, hash, initRuntimeConfig, ipAddress, isDirectory, isH3, isNitro, isNuxt, normalizeTemplate, prettyPath, relativeWithDot, resolveAlias, resolvePath, resolveSilgiModule, resolveSilgiPath, toArray, tryResolveModule, useLogger, useRequest, useSilgiRuntimeConfig, writeFile };
@@ -2,6 +2,7 @@ import { SilgiCLI, ModuleOptionsCustom, ModuleDefinition, SilgiModule, SilgiPres
2
2
  import { Buffer } from 'node:buffer';
3
3
  import * as consola from 'consola';
4
4
  import { ConsolaOptions } from 'consola';
5
+ import { IncomingMessage } from 'node:http';
5
6
 
6
7
  declare function hasError(type: SilgiCLI['errors'][0]['type'], silgi?: SilgiCLI): boolean;
7
8
 
@@ -93,6 +94,17 @@ declare function addTemplate<T>(_template: SilgiTemplate<T> | string): ResolvedS
93
94
  */
94
95
  declare function normalizeTemplate<T>(template: SilgiTemplate<T> | string, buildDir?: string): ResolvedSilgiTemplate<T>;
95
96
 
97
+ declare function useRequest(event: SilgiEvents): false | (IncomingMessage & {
98
+ originalUrl?: string;
99
+ });
100
+ declare function getIpAddress(event: SilgiEvents): string | false;
101
+ /**
102
+ * Extract the client's IP address from request headers with high accuracy
103
+ * @param req - The incoming HTTP request
104
+ * @returns The client's IP address or empty string if not found
105
+ */
106
+ declare function ipAddress(req: IncomingMessage): string;
107
+
96
108
  declare function relativeWithDot(from: string, to: string): string;
97
109
  /** @since 3.9.0 */
98
110
  declare function toArray<T>(value: T | T[]): T[];
@@ -113,4 +125,4 @@ declare const MODE_RE: RegExp;
113
125
  declare function hasSilgiModule(moduleKey: string, silgi?: SilgiCLI): boolean;
114
126
  declare function hasInstalledModule(moduleKey: string, silgi?: SilgiCLI): boolean;
115
127
 
116
- export { MODE_RE, addTemplate, applyEnv, createResolver, defineSilgiModule, defineSilgiPreset, filterInPlace, hasError, hasInstalledModule, hasSilgiModule, hash, initRuntimeConfig, isDirectory, isH3, isNitro, isNuxt, normalizeTemplate, prettyPath, relativeWithDot, resolveAlias, resolvePath, resolveSilgiModule, resolveSilgiPath, toArray, tryResolveModule, useLogger, useSilgiRuntimeConfig, writeFile };
128
+ export { MODE_RE, addTemplate, applyEnv, createResolver, defineSilgiModule, defineSilgiPreset, filterInPlace, getIpAddress, hasError, hasInstalledModule, hasSilgiModule, hash, initRuntimeConfig, ipAddress, isDirectory, isH3, isNitro, isNuxt, normalizeTemplate, prettyPath, relativeWithDot, resolveAlias, resolvePath, resolveSilgiModule, resolveSilgiPath, toArray, tryResolveModule, useLogger, useRequest, useSilgiRuntimeConfig, writeFile };
@@ -413,4 +413,85 @@ function normalizeTemplate(template, buildDir) {
413
413
  return template;
414
414
  }
415
415
 
416
- export { MODE_RE, addTemplate, applyEnv, createResolver, defineSilgiModule, defineSilgiPreset, filterInPlace, hasInstalledModule, hasSilgiModule, hash, initRuntimeConfig, isDirectory$1 as isDirectory, isH3, isNitro, isNuxt, normalizeTemplate, prettyPath, relativeWithDot, resolveAlias, resolvePath, resolveSilgiModule, resolveSilgiPath, toArray, tryResolveModule, useLogger, useSilgiRuntimeConfig, writeFile };
416
+ function useRequest(event) {
417
+ const silgi = useSilgi();
418
+ if (silgi.options.present === "nuxt" || silgi.options.present === "nitro" || silgi.options.present === "h3") {
419
+ return event.node.req;
420
+ }
421
+ return false;
422
+ }
423
+ function getIpAddress(event) {
424
+ const silgi = useSilgi();
425
+ if (silgi.options.present === "nuxt" || silgi.options.present === "nitro" || silgi.options.present === "h3") {
426
+ const _ipAddress = ipAddress(event.node.req);
427
+ return _ipAddress;
428
+ }
429
+ return false;
430
+ }
431
+ function ipAddress(req) {
432
+ const headers = [
433
+ "cf-connecting-ip",
434
+ // Cloudflare
435
+ "true-client-ip",
436
+ // Akamai and Cloudflare
437
+ "x-real-ip",
438
+ // Nginx proxy/FastCGI
439
+ "x-forwarded-for",
440
+ // Standard proxy header
441
+ "x-client-ip",
442
+ // Apache
443
+ "x-forwarded",
444
+ // Alternative format
445
+ "forwarded",
446
+ // RFC 7239 compliant format
447
+ "forwarded-for",
448
+ // RFC 7239 variant
449
+ "x-appengine-user-ip",
450
+ // Google App Engine
451
+ "x-cluster-client-ip",
452
+ // GCP load balancer
453
+ "fastly-client-ip",
454
+ // Fastly CDN
455
+ "x-azure-clientip",
456
+ // Azure
457
+ "x-original-forwarded-for",
458
+ // Secondary forwarded-for header
459
+ "x-lb-forwarded-for",
460
+ // Load balancer specific
461
+ "cdn-loop"
462
+ // Used by some CDNs
463
+ ];
464
+ for (const header of headers) {
465
+ const value = req.headers[header];
466
+ if (!value)
467
+ continue;
468
+ let ip;
469
+ if (Array.isArray(value)) {
470
+ ip = value[0];
471
+ } else if (header === "x-forwarded-for" || header === "forwarded-for" || header === "x-original-forwarded-for") {
472
+ ip = value.split(",")[0];
473
+ } else if (header === "forwarded") {
474
+ const match = value.match(/for=([^;]+)/);
475
+ ip = match ? match[1].trim().replace(/^\[|\]$/g, "") : "";
476
+ } else {
477
+ ip = value;
478
+ }
479
+ const cleanIp = ip.trim();
480
+ if (cleanIp && isValidIp(cleanIp)) {
481
+ return cleanIp;
482
+ }
483
+ }
484
+ const socketIp = req.socket.remoteAddress ?? "";
485
+ return socketIp && isValidIp(socketIp) ? socketIp : "";
486
+ }
487
+ function isValidIp(ip) {
488
+ if (ip === "::1" || ip === "localhost" || ip === "127.0.0.1")
489
+ return true;
490
+ if (/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(ip))
491
+ return true;
492
+ if (/^[0-9a-f:]+$/i.test(ip))
493
+ return true;
494
+ return false;
495
+ }
496
+
497
+ export { MODE_RE, addTemplate, applyEnv, createResolver, defineSilgiModule, defineSilgiPreset, filterInPlace, getIpAddress, hasInstalledModule, hasSilgiModule, hash, initRuntimeConfig, ipAddress, isDirectory$1 as isDirectory, isH3, isNitro, isNuxt, normalizeTemplate, prettyPath, relativeWithDot, resolveAlias, resolvePath, resolveSilgiModule, resolveSilgiPath, toArray, tryResolveModule, useLogger, useRequest, useSilgiRuntimeConfig, writeFile };
@@ -1,4 +1,4 @@
1
- const version = "0.12.4";
1
+ const version = "0.14.0";
2
2
  const peerDependencies = {
3
3
  "@fastify/deepmerge": "^2.0.2",
4
4
  "@nuxt/kit": "^3.15.3",
@@ -1,4 +1,4 @@
1
- const version = "0.12.4";
1
+ const version = "0.14.0";
2
2
  const peerDependencies = {
3
3
  "@fastify/deepmerge": "^2.0.2",
4
4
  "@nuxt/kit": "^3.15.3",
@@ -11,7 +11,8 @@ const nitro = defineSilgiPreset(
11
11
  },
12
12
  envOptions: {
13
13
  prefix: "NITRO_"
14
- }
14
+ },
15
+ storages: ["data", "/assets"]
15
16
  },
16
17
  {
17
18
  name: "nitro",
@@ -12,7 +12,8 @@ const nuxt = defineSilgiPreset(
12
12
  envOptions: {
13
13
  prefix: "NITRO_",
14
14
  altPrefix: "NUXT_"
15
- }
15
+ },
16
+ storages: ["data", "/assets"]
16
17
  },
17
18
  {
18
19
  name: "nuxt",
@@ -569,6 +569,7 @@ interface SilgiCLIOptions extends PresetOptions {
569
569
  runtimeConfig: SilgiRuntimeConfig$1 & {
570
570
  [key: string]: any;
571
571
  };
572
+ storages: string[];
572
573
  namespaces: string[];
573
574
  hooks: NestedHooks<SilgiCLIHooks$1>;
574
575
  plugins: {
@@ -902,7 +903,7 @@ interface SilgiStorageBase {
902
903
  type StorageKeyGenerator<TInput> = (input: TInput) => string | Promise<string>;
903
904
  interface StorageConfig<TInput> {
904
905
  options: TransactionOptions;
905
- base: '/memory:cache' | keyof SilgiStorageBase;
906
+ base: 'memory' | keyof SilgiStorageBase;
906
907
  key?: StorageKeyGenerator<TInput>;
907
908
  scope?: 'request' | 'global';
908
909
  }
@@ -569,6 +569,7 @@ interface SilgiCLIOptions extends PresetOptions {
569
569
  runtimeConfig: SilgiRuntimeConfig$1 & {
570
570
  [key: string]: any;
571
571
  };
572
+ storages: string[];
572
573
  namespaces: string[];
573
574
  hooks: NestedHooks<SilgiCLIHooks$1>;
574
575
  plugins: {
@@ -902,7 +903,7 @@ interface SilgiStorageBase {
902
903
  type StorageKeyGenerator<TInput> = (input: TInput) => string | Promise<string>;
903
904
  interface StorageConfig<TInput> {
904
905
  options: TransactionOptions;
905
- base: '/memory:cache' | keyof SilgiStorageBase;
906
+ base: 'memory' | keyof SilgiStorageBase;
906
907
  key?: StorageKeyGenerator<TInput>;
907
908
  scope?: 'request' | 'global';
908
909
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "silgi",
3
3
  "type": "module",
4
- "version": "0.12.4",
4
+ "version": "0.14.0",
5
5
  "private": false,
6
6
  "sideEffects": false,
7
7
  "exports": {