@zh-moody/safe-env 0.3.4 → 0.3.5
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/chunk-5LT4H5GX.cjs +1 -0
- package/dist/chunk-CYBE3YNH.cjs +8 -0
- package/dist/chunk-D6WM53FN.cjs +1 -0
- package/dist/chunk-FBIIFJ67.js +1 -0
- package/dist/chunk-I7AUKTXE.js +1 -0
- package/dist/chunk-MZ3R3VUV.js +8 -0
- package/dist/fs-browser.cjs +1 -1
- package/dist/fs-browser.js +1 -1
- package/dist/fs-node.cjs +1 -1
- package/dist/fs-node.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +13 -16
- package/dist/index.d.ts +13 -16
- package/dist/index.js +1 -1
- package/dist/{types-CFipqnoe.d.cts → types-C7kDNgjd.d.cts} +17 -15
- package/dist/{types-CFipqnoe.d.ts → types-C7kDNgjd.d.ts} +17 -15
- package/dist/vite.cjs +2 -1
- package/dist/vite.d.cts +2 -12
- package/dist/vite.d.ts +2 -12
- package/dist/vite.js +2 -1
- package/package.json +1 -1
- package/dist/chunk-MODJPD2X.cjs +0 -1
- package/dist/chunk-UONNQTFU.js +0 -3
- package/dist/chunk-WXZ32K3G.cjs +0 -3
- package/dist/chunk-WZTPUQ3S.js +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});function l(e){let s={},r=e.split(/\r?\n/);for(let o of r){let i=o.trim();if(!i||i.startsWith("#"))continue;let n=i.indexOf("=");if(n==-1)continue;let c=i.slice(0,n).trim(),t=i.slice(n+1).trim();(t.startsWith('"')&&t.endsWith('"')||t.startsWith("'")&&t.endsWith("'"))&&(t=t.slice(1,-1)),s[c]=t}return s}exports.a = l;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _chunkD6WM53FNcjs = require('./chunk-D6WM53FN.cjs');var T="development",I= exports.b ="serve",_= exports.c ="build",$= exports.d ="VITE_DEV_SERVER",K= exports.e ="VITE_";var F={r:"\x1B[31m",g:"\x1B[32m",y:"\x1B[33m",b:"\x1B[1m",res:"\x1B[0m",d:"\x1B[2m",cy:"\x1B[36m"},k=typeof window<"u"&&typeof window.document<"u",g=(s,i)=>{let r=String(s),n=()=>r.length+(r.match(/[^\x00-\xff]/g)||[]).length;for(;n()>i;)r=r.slice(0,-1);return r+" ".repeat(i-n())};function v(s,i=!0){let r=Math.max(80,(typeof process<"u"?process.stdout.columns:80)||80)-10,n=Math.floor(r*.3),y=Math.floor(r*.5),e=i?F:{r:"",g:"",y:"",b:"",res:"",d:"",cy:""},a="";return a+=`
|
|
2
|
+
${e.r}${e.b}\u274C SafeEnv Validation Failed${e.res}
|
|
3
|
+
`,a+=` ${e.b}${g("Key",n)} \u2502 ${g("Error",y)} \u2502 Value${e.res}
|
|
4
|
+
`,a+=e.d+"\u2500".repeat(r+5)+e.res+`
|
|
5
|
+
`,s.forEach(d=>{let E=d.value===void 0?"undefined":d.isSecret?"********":`"${d.value}"`,o=d.value===void 0?e.d:d.isSecret?e.y:e.cy;a+=` ${e.y}${g(d.key,n)}${e.res} \u2502 ${e.r}${g(d.error,y)}${e.res} \u2502 ${o}${E}${e.res}
|
|
6
|
+
`}),a+=e.d+"\u2500".repeat(r+5)+e.res+`
|
|
7
|
+
`,a+=` ${e.g}\u{1F4A1} Tip: Check your .env files or schema definitions.${e.res}
|
|
8
|
+
`,a}function O(s){console.group("%c \u274C SafeEnv Validation Failed ","background: #fee2e2; color: #b91c1c; font-weight: bold; padding: 4px; border-radius: 2px;");let i=s.reduce((r,n)=>(r[n.key]={"Error Message":n.error,"Current Value":n.value===void 0?"undefined":n.isSecret?"********":n.value},r),{});console.table(i),console.log("%c \u{1F4A1} Tip: Check your .env files or schema definitions. ","color: #059669; font-style: italic;"),console.groupEnd()}function S(s){k?O(s):console.error(v(s,!0))}var x={};function R(){return new Proxy({},{get:()=>{}})}function A(s,i={}){let{loadProcessEnv:r=!0,prefix:n="",cwd:y,useCache:e=!0}=i,a=typeof window<"u",d=typeof process<"u"&&(!!process.env.VITE||!!process.env[$]),E=a||d||"source"in i,o;if(e&&Object.keys(x).length>0&&!("source"in i))o=x;else if("source"in i)o=i.source||{},e&&Object.keys(o).length>0&&Object.assign(x,o);else if(typeof process<"u"&&!a)try{let t=i.mode||process.env.NODE_ENV||T,{loadDotEnv:c}=_chunkD6WM53FNcjs.a.call(void 0, "./fs-node.cjs"),l={};for(let m of[".env",`.env.${t}`,".env.local",`.env.${t}.local`])l={...l,...c(m,y)};o={...l,...r?process.env:{}},e&&Object.keys(o).length>0&&Object.assign(x,o)}catch (e2){o={}}else o={};if(E&&Object.keys(o).length===0)return{};let b={},u=[];for(let t in s){let c=s[t],l=n&&!t.startsWith(n)?n+t:t,m=c.sourceKey||(o[l]!==void 0?l:o[t]!==void 0?t:l),p=o[m];try{if(p===void 0||p===""&&c.default!==void 0){if(c.required&&p===void 0)throw new Error("Missing required field");b[t]=c.default}else{let f=c.parse(p);if(f!==void 0&&c.metadata){let{min:h,max:D,validate:w}=c.metadata;if(typeof f=="number"){if(h!==void 0&&f<h)throw new Error(`Below min ${h}`);if(D!==void 0&&f>D)throw new Error(`Above max ${D}`)}if(w&&!w.fn(f))throw new Error(w.message)}b[t]=f}}catch(f){u.push({key:m,error:f.message,value:p,isSecret:_optionalChain([c, 'access', _2 => _2.metadata, 'optionalAccess', _3 => _3.isSecret])})}}if(u.length>0){if(i.throwOnError){let t=new Error(v(u,!0));throw t.plainMessage=v(u,!1),t}return S(u),typeof process<"u"&&process.exit&&!E&&process.exit(1),R()}return Object.freeze(b)}exports.a = T; exports.b = I; exports.c = _; exports.d = $; exports.e = K; exports.f = v; exports.g = S; exports.h = A;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var d=(a=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(a,{get:(b,c)=>(typeof require<"u"?require:b)[c]}):a)(function(a){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+a+'" is not supported')});exports.a = d;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function l(e){let s={},r=e.split(/\r?\n/);for(let o of r){let i=o.trim();if(!i||i.startsWith("#"))continue;let n=i.indexOf("=");if(n==-1)continue;let c=i.slice(0,n).trim(),t=i.slice(n+1).trim();(t.startsWith('"')&&t.endsWith('"')||t.startsWith("'")&&t.endsWith("'"))&&(t=t.slice(1,-1)),s[c]=t}return s}export{l as a};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var d=(a=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(a,{get:(b,c)=>(typeof require<"u"?require:b)[c]}):a)(function(a){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+a+'" is not supported')});export{d as a};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import{a as V}from"./chunk-I7AUKTXE.js";var T="development",I="serve",_="build",$="VITE_DEV_SERVER",K="VITE_";var F={r:"\x1B[31m",g:"\x1B[32m",y:"\x1B[33m",b:"\x1B[1m",res:"\x1B[0m",d:"\x1B[2m",cy:"\x1B[36m"},k=typeof window<"u"&&typeof window.document<"u",g=(s,i)=>{let r=String(s),n=()=>r.length+(r.match(/[^\x00-\xff]/g)||[]).length;for(;n()>i;)r=r.slice(0,-1);return r+" ".repeat(i-n())};function v(s,i=!0){let r=Math.max(80,(typeof process<"u"?process.stdout.columns:80)||80)-10,n=Math.floor(r*.3),y=Math.floor(r*.5),e=i?F:{r:"",g:"",y:"",b:"",res:"",d:"",cy:""},a="";return a+=`
|
|
2
|
+
${e.r}${e.b}\u274C SafeEnv Validation Failed${e.res}
|
|
3
|
+
`,a+=` ${e.b}${g("Key",n)} \u2502 ${g("Error",y)} \u2502 Value${e.res}
|
|
4
|
+
`,a+=e.d+"\u2500".repeat(r+5)+e.res+`
|
|
5
|
+
`,s.forEach(d=>{let E=d.value===void 0?"undefined":d.isSecret?"********":`"${d.value}"`,o=d.value===void 0?e.d:d.isSecret?e.y:e.cy;a+=` ${e.y}${g(d.key,n)}${e.res} \u2502 ${e.r}${g(d.error,y)}${e.res} \u2502 ${o}${E}${e.res}
|
|
6
|
+
`}),a+=e.d+"\u2500".repeat(r+5)+e.res+`
|
|
7
|
+
`,a+=` ${e.g}\u{1F4A1} Tip: Check your .env files or schema definitions.${e.res}
|
|
8
|
+
`,a}function O(s){console.group("%c \u274C SafeEnv Validation Failed ","background: #fee2e2; color: #b91c1c; font-weight: bold; padding: 4px; border-radius: 2px;");let i=s.reduce((r,n)=>(r[n.key]={"Error Message":n.error,"Current Value":n.value===void 0?"undefined":n.isSecret?"********":n.value},r),{});console.table(i),console.log("%c \u{1F4A1} Tip: Check your .env files or schema definitions. ","color: #059669; font-style: italic;"),console.groupEnd()}function S(s){k?O(s):console.error(v(s,!0))}var x={};function R(){return new Proxy({},{get:()=>{}})}function A(s,i={}){let{loadProcessEnv:r=!0,prefix:n="",cwd:y,useCache:e=!0}=i,a=typeof window<"u",d=typeof process<"u"&&(!!process.env.VITE||!!process.env[$]),E=a||d||"source"in i,o;if(e&&Object.keys(x).length>0&&!("source"in i))o=x;else if("source"in i)o=i.source||{},e&&Object.keys(o).length>0&&Object.assign(x,o);else if(typeof process<"u"&&!a)try{let t=i.mode||process.env.NODE_ENV||T,{loadDotEnv:c}=V("./fs-node.cjs"),l={};for(let m of[".env",`.env.${t}`,".env.local",`.env.${t}.local`])l={...l,...c(m,y)};o={...l,...r?process.env:{}},e&&Object.keys(o).length>0&&Object.assign(x,o)}catch{o={}}else o={};if(E&&Object.keys(o).length===0)return{};let b={},u=[];for(let t in s){let c=s[t],l=n&&!t.startsWith(n)?n+t:t,m=c.sourceKey||(o[l]!==void 0?l:o[t]!==void 0?t:l),p=o[m];try{if(p===void 0||p===""&&c.default!==void 0){if(c.required&&p===void 0)throw new Error("Missing required field");b[t]=c.default}else{let f=c.parse(p);if(f!==void 0&&c.metadata){let{min:h,max:D,validate:w}=c.metadata;if(typeof f=="number"){if(h!==void 0&&f<h)throw new Error(`Below min ${h}`);if(D!==void 0&&f>D)throw new Error(`Above max ${D}`)}if(w&&!w.fn(f))throw new Error(w.message)}b[t]=f}}catch(f){u.push({key:m,error:f.message,value:p,isSecret:c.metadata?.isSecret})}}if(u.length>0){if(i.throwOnError){let t=new Error(v(u,!0));throw t.plainMessage=v(u,!1),t}return S(u),typeof process<"u"&&process.exit&&!E&&process.exit(1),R()}return Object.freeze(b)}export{T as a,I as b,_ as c,$ as d,K as e,v as f,S as g,A as h};
|
package/dist/fs-browser.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});function r(n=".env",t){return{}}exports.loadDotEnv = r;
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});require('./chunk-D6WM53FN.cjs');function r(n=".env",t){return{}}exports.loadDotEnv = r;
|
package/dist/fs-browser.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function r(n=".env",t){return{}}export{r as loadDotEnv};
|
|
1
|
+
import"./chunk-I7AUKTXE.js";function r(n=".env",t){return{}}export{r as loadDotEnv};
|
package/dist/fs-node.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _chunk5LT4H5GXcjs = require('./chunk-5LT4H5GX.cjs');require('./chunk-D6WM53FN.cjs');var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);var _path = require('path'); var _path2 = _interopRequireDefault(_path);function p(e=".env",n){try{let r=_path2.default.resolve(n||process.cwd(),e);if(_fs2.default.existsSync(r))return _chunk5LT4H5GXcjs.a.call(void 0, _fs2.default.readFileSync(r,"utf-8"))}catch (e2){}return{}}exports.loadDotEnv = p;
|
package/dist/fs-node.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{a as t}from"./chunk-FBIIFJ67.js";import"./chunk-I7AUKTXE.js";import o from"fs";import s from"path";function p(e=".env",n){try{let r=s.resolve(n||process.cwd(),e);if(o.existsSync(r))return t(o.readFileSync(r,"utf-8"))}catch{}return{}}export{p as loadDotEnv};
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunk5LT4H5GXcjs = require('./chunk-5LT4H5GX.cjs');var _chunkCYBE3YNHcjs = require('./chunk-CYBE3YNH.cjs');require('./chunk-D6WM53FN.cjs');function a(i,e,n,s=[]){return{type:i,default:e,required:e===void 0,parse:n,metadata:s.length?{options:s}:{},from(t){return this.sourceKey=t,this},validate(t,r="Custom validation failed"){return this.metadata={...this.metadata,validate:{fn:t,message:r}},this},min(t){return this.metadata={...this.metadata,min:t},this},max(t){return this.metadata={...this.metadata,max:t},this},transform(t){let r=this.parse;return this.parse=o=>t(r(o)),this},secret(){return this.metadata={...this.metadata,isSecret:!0},this},url(){return this.validate(t=>{try{return new URL(String(t)),!0}catch (e2){return!1}},"Invalid URL format")},email(){return this.validate(t=>/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/.test(String(t)),"Invalid email format")},regex(t,r="Value does not match pattern"){return this.validate(o=>t.test(String(o)),r)},description(t){return this.metadata={...this.metadata,description:t},this}}}var p={string:i=>a("string",i,e=>String(e)),number:i=>a("number",i,e=>{let n=Number(e);if(isNaN(n))throw new Error(`Invalid number: ${e}`);return n}),boolean:i=>a("boolean",i,e=>{if(typeof e=="boolean")return e;if(e===void 0||e==="")return!1;let n=String(e).toLowerCase().trim();if(["true","1","yes","on"].includes(n))return!0;if(["false","0","no","off"].includes(n))return!1;throw new Error(`Invalid boolean: ${e}`)}),enum:(i,e)=>a("enum",e,n=>{if(!i.includes(n))throw new Error(`Value "${n}" is not one of: ${i.join(", ")}`);return n},i),array:(i,e=",")=>a("array",i,n=>Array.isArray(n)?n:typeof n!="string"?[]:n.split(e).map(s=>s.trim()).filter(Boolean))};exports.BUILD = _chunkCYBE3YNHcjs.c; exports.DEV = _chunkCYBE3YNHcjs.a; exports.SERVE = _chunkCYBE3YNHcjs.b; exports.VITE_DEV_FLAG = _chunkCYBE3YNHcjs.d; exports.VITE_PREFIX = _chunkCYBE3YNHcjs.e; exports.formatErrorReport = _chunkCYBE3YNHcjs.f; exports.parseDotEnv = _chunk5LT4H5GXcjs.a; exports.reportErrors = _chunkCYBE3YNHcjs.g; exports.s = p; exports.safeEnv = _chunkCYBE3YNHcjs.h;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,28 +1,25 @@
|
|
|
1
|
-
import { F as FieldDefinition, S as Schema, I as InferSchema, E as EnvError } from './types-
|
|
2
|
-
export { B as BaseType } from './types-
|
|
1
|
+
import { F as FieldDefinition, S as Schema, a as SafeEnvOptions, I as InferSchema, E as EnvError } from './types-C7kDNgjd.cjs';
|
|
2
|
+
export { B as BUILD, b as BaseType, D as DEV, c as SERVE, V as VITE_DEV_FLAG, d as VITE_PREFIX } from './types-C7kDNgjd.cjs';
|
|
3
3
|
|
|
4
4
|
declare const s: {
|
|
5
|
-
string: (
|
|
6
|
-
number: (
|
|
7
|
-
boolean: (
|
|
8
|
-
enum: <T extends string>(
|
|
9
|
-
array: (
|
|
5
|
+
string: (d?: string) => FieldDefinition<string>;
|
|
6
|
+
number: (d?: number) => FieldDefinition<number>;
|
|
7
|
+
boolean: (d?: boolean) => FieldDefinition<boolean>;
|
|
8
|
+
enum: <T extends string>(o: T[], d?: T) => FieldDefinition<T>;
|
|
9
|
+
array: (d?: string[], sep?: string) => FieldDefinition<string[]>;
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
prefix?: string;
|
|
17
|
-
cwd?: string;
|
|
18
|
-
}
|
|
19
|
-
declare function safeEnv<T extends Schema>(schema: T, options?: SafeEnvOptions): Readonly<InferSchema<T>>;
|
|
12
|
+
declare function safeEnv<T extends Schema>(schema: T, options?: SafeEnvOptions & {
|
|
13
|
+
throwOnError?: boolean;
|
|
14
|
+
useCache?: boolean;
|
|
15
|
+
}): Readonly<InferSchema<T>>;
|
|
20
16
|
|
|
21
17
|
/***
|
|
22
18
|
* 将 .env 内容字符串解析为对象
|
|
23
19
|
* */
|
|
24
20
|
declare function parseDotEnv(content: string): Record<string, string>;
|
|
25
21
|
|
|
22
|
+
declare function formatErrorReport(errors: EnvError[], useColor?: boolean): string;
|
|
26
23
|
declare function reportErrors(errors: EnvError[]): void;
|
|
27
24
|
|
|
28
|
-
export { EnvError, FieldDefinition, InferSchema, Schema, parseDotEnv, reportErrors, s, safeEnv };
|
|
25
|
+
export { EnvError, FieldDefinition, InferSchema, SafeEnvOptions, Schema, formatErrorReport, parseDotEnv, reportErrors, s, safeEnv };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,28 +1,25 @@
|
|
|
1
|
-
import { F as FieldDefinition, S as Schema, I as InferSchema, E as EnvError } from './types-
|
|
2
|
-
export { B as BaseType } from './types-
|
|
1
|
+
import { F as FieldDefinition, S as Schema, a as SafeEnvOptions, I as InferSchema, E as EnvError } from './types-C7kDNgjd.js';
|
|
2
|
+
export { B as BUILD, b as BaseType, D as DEV, c as SERVE, V as VITE_DEV_FLAG, d as VITE_PREFIX } from './types-C7kDNgjd.js';
|
|
3
3
|
|
|
4
4
|
declare const s: {
|
|
5
|
-
string: (
|
|
6
|
-
number: (
|
|
7
|
-
boolean: (
|
|
8
|
-
enum: <T extends string>(
|
|
9
|
-
array: (
|
|
5
|
+
string: (d?: string) => FieldDefinition<string>;
|
|
6
|
+
number: (d?: number) => FieldDefinition<number>;
|
|
7
|
+
boolean: (d?: boolean) => FieldDefinition<boolean>;
|
|
8
|
+
enum: <T extends string>(o: T[], d?: T) => FieldDefinition<T>;
|
|
9
|
+
array: (d?: string[], sep?: string) => FieldDefinition<string[]>;
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
prefix?: string;
|
|
17
|
-
cwd?: string;
|
|
18
|
-
}
|
|
19
|
-
declare function safeEnv<T extends Schema>(schema: T, options?: SafeEnvOptions): Readonly<InferSchema<T>>;
|
|
12
|
+
declare function safeEnv<T extends Schema>(schema: T, options?: SafeEnvOptions & {
|
|
13
|
+
throwOnError?: boolean;
|
|
14
|
+
useCache?: boolean;
|
|
15
|
+
}): Readonly<InferSchema<T>>;
|
|
20
16
|
|
|
21
17
|
/***
|
|
22
18
|
* 将 .env 内容字符串解析为对象
|
|
23
19
|
* */
|
|
24
20
|
declare function parseDotEnv(content: string): Record<string, string>;
|
|
25
21
|
|
|
22
|
+
declare function formatErrorReport(errors: EnvError[], useColor?: boolean): string;
|
|
26
23
|
declare function reportErrors(errors: EnvError[]): void;
|
|
27
24
|
|
|
28
|
-
export { EnvError, FieldDefinition, InferSchema, Schema, parseDotEnv, reportErrors, s, safeEnv };
|
|
25
|
+
export { EnvError, FieldDefinition, InferSchema, SafeEnvOptions, Schema, formatErrorReport, parseDotEnv, reportErrors, s, safeEnv };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as
|
|
1
|
+
import{a as u}from"./chunk-FBIIFJ67.js";import{a as l,b as f,c as m,d as h,e as y,f as g,g as D,h as T}from"./chunk-MZ3R3VUV.js";import"./chunk-I7AUKTXE.js";function a(i,e,n,s=[]){return{type:i,default:e,required:e===void 0,parse:n,metadata:s.length?{options:s}:{},from(t){return this.sourceKey=t,this},validate(t,r="Custom validation failed"){return this.metadata={...this.metadata,validate:{fn:t,message:r}},this},min(t){return this.metadata={...this.metadata,min:t},this},max(t){return this.metadata={...this.metadata,max:t},this},transform(t){let r=this.parse;return this.parse=o=>t(r(o)),this},secret(){return this.metadata={...this.metadata,isSecret:!0},this},url(){return this.validate(t=>{try{return new URL(String(t)),!0}catch{return!1}},"Invalid URL format")},email(){return this.validate(t=>/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/.test(String(t)),"Invalid email format")},regex(t,r="Value does not match pattern"){return this.validate(o=>t.test(String(o)),r)},description(t){return this.metadata={...this.metadata,description:t},this}}}var p={string:i=>a("string",i,e=>String(e)),number:i=>a("number",i,e=>{let n=Number(e);if(isNaN(n))throw new Error(`Invalid number: ${e}`);return n}),boolean:i=>a("boolean",i,e=>{if(typeof e=="boolean")return e;if(e===void 0||e==="")return!1;let n=String(e).toLowerCase().trim();if(["true","1","yes","on"].includes(n))return!0;if(["false","0","no","off"].includes(n))return!1;throw new Error(`Invalid boolean: ${e}`)}),enum:(i,e)=>a("enum",e,n=>{if(!i.includes(n))throw new Error(`Value "${n}" is not one of: ${i.join(", ")}`);return n},i),array:(i,e=",")=>a("array",i,n=>Array.isArray(n)?n:typeof n!="string"?[]:n.split(e).map(s=>s.trim()).filter(Boolean))};export{m as BUILD,l as DEV,f as SERVE,h as VITE_DEV_FLAG,y as VITE_PREFIX,g as formatErrorReport,u as parseDotEnv,D as reportErrors,p as s,T as safeEnv};
|
|
@@ -1,20 +1,15 @@
|
|
|
1
|
+
declare const DEV = "development";
|
|
2
|
+
declare const SERVE = "serve";
|
|
3
|
+
declare const BUILD = "build";
|
|
4
|
+
declare const VITE_DEV_FLAG = "VITE_DEV_SERVER";
|
|
5
|
+
declare const VITE_PREFIX = "VITE_";
|
|
1
6
|
type BaseType = "string" | "number" | "boolean" | "enum" | "array";
|
|
2
7
|
interface FieldDefinition<T = any, D extends string = string> {
|
|
3
8
|
type: BaseType;
|
|
4
9
|
default?: T;
|
|
5
10
|
required: boolean;
|
|
6
11
|
sourceKey?: string;
|
|
7
|
-
metadata?:
|
|
8
|
-
min?: number;
|
|
9
|
-
max?: number;
|
|
10
|
-
options?: T[];
|
|
11
|
-
description?: string;
|
|
12
|
-
isSecret?: boolean;
|
|
13
|
-
validate?: {
|
|
14
|
-
fn: (val: T) => boolean;
|
|
15
|
-
message: string;
|
|
16
|
-
};
|
|
17
|
-
};
|
|
12
|
+
metadata?: any;
|
|
18
13
|
parse: (val: any) => T;
|
|
19
14
|
from: (key: string) => FieldDefinition<T, D>;
|
|
20
15
|
validate: (fn: (val: T) => boolean, message?: string) => FieldDefinition<T, D>;
|
|
@@ -35,9 +30,16 @@ interface EnvError {
|
|
|
35
30
|
isSecret?: boolean;
|
|
36
31
|
}
|
|
37
32
|
type InferSchema<T> = {
|
|
38
|
-
[K in keyof T]: T[K] extends FieldDefinition<infer U,
|
|
39
|
-
readonly __description?: D;
|
|
40
|
-
} : never;
|
|
33
|
+
[K in keyof T]: T[K] extends FieldDefinition<infer U, any> ? U : never;
|
|
41
34
|
};
|
|
35
|
+
interface SafeEnvOptions {
|
|
36
|
+
mode?: string;
|
|
37
|
+
loadProcessEnv?: boolean;
|
|
38
|
+
source?: Record<string, any>;
|
|
39
|
+
prefix?: string;
|
|
40
|
+
cwd?: string;
|
|
41
|
+
/** @internal */ manualSource?: boolean;
|
|
42
|
+
/** @internal */ devMode?: boolean;
|
|
43
|
+
}
|
|
42
44
|
|
|
43
|
-
export
|
|
45
|
+
export { BUILD as B, DEV as D, type EnvError as E, type FieldDefinition as F, type InferSchema as I, type Schema as S, VITE_DEV_FLAG as V, type SafeEnvOptions as a, type BaseType as b, SERVE as c, VITE_PREFIX as d };
|
|
@@ -1,20 +1,15 @@
|
|
|
1
|
+
declare const DEV = "development";
|
|
2
|
+
declare const SERVE = "serve";
|
|
3
|
+
declare const BUILD = "build";
|
|
4
|
+
declare const VITE_DEV_FLAG = "VITE_DEV_SERVER";
|
|
5
|
+
declare const VITE_PREFIX = "VITE_";
|
|
1
6
|
type BaseType = "string" | "number" | "boolean" | "enum" | "array";
|
|
2
7
|
interface FieldDefinition<T = any, D extends string = string> {
|
|
3
8
|
type: BaseType;
|
|
4
9
|
default?: T;
|
|
5
10
|
required: boolean;
|
|
6
11
|
sourceKey?: string;
|
|
7
|
-
metadata?:
|
|
8
|
-
min?: number;
|
|
9
|
-
max?: number;
|
|
10
|
-
options?: T[];
|
|
11
|
-
description?: string;
|
|
12
|
-
isSecret?: boolean;
|
|
13
|
-
validate?: {
|
|
14
|
-
fn: (val: T) => boolean;
|
|
15
|
-
message: string;
|
|
16
|
-
};
|
|
17
|
-
};
|
|
12
|
+
metadata?: any;
|
|
18
13
|
parse: (val: any) => T;
|
|
19
14
|
from: (key: string) => FieldDefinition<T, D>;
|
|
20
15
|
validate: (fn: (val: T) => boolean, message?: string) => FieldDefinition<T, D>;
|
|
@@ -35,9 +30,16 @@ interface EnvError {
|
|
|
35
30
|
isSecret?: boolean;
|
|
36
31
|
}
|
|
37
32
|
type InferSchema<T> = {
|
|
38
|
-
[K in keyof T]: T[K] extends FieldDefinition<infer U,
|
|
39
|
-
readonly __description?: D;
|
|
40
|
-
} : never;
|
|
33
|
+
[K in keyof T]: T[K] extends FieldDefinition<infer U, any> ? U : never;
|
|
41
34
|
};
|
|
35
|
+
interface SafeEnvOptions {
|
|
36
|
+
mode?: string;
|
|
37
|
+
loadProcessEnv?: boolean;
|
|
38
|
+
source?: Record<string, any>;
|
|
39
|
+
prefix?: string;
|
|
40
|
+
cwd?: string;
|
|
41
|
+
/** @internal */ manualSource?: boolean;
|
|
42
|
+
/** @internal */ devMode?: boolean;
|
|
43
|
+
}
|
|
42
44
|
|
|
43
|
-
export
|
|
45
|
+
export { BUILD as B, DEV as D, type EnvError as E, type FieldDefinition as F, type InferSchema as I, type Schema as S, VITE_DEV_FLAG as V, type SafeEnvOptions as a, type BaseType as b, SERVE as c, VITE_PREFIX as d };
|
package/dist/vite.cjs
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkCYBE3YNHcjs = require('./chunk-CYBE3YNH.cjs');require('./chunk-D6WM53FN.cjs');var _vite = require('vite');function y(v,f={}){let n=null,o=!1;return{name:"vite-plugin-safe-env",configResolved(e){o=e.command===_chunkCYBE3YNHcjs.b,process.env[_chunkCYBE3YNHcjs.d]=o?"true":"";let{envDir:t=e.root,prefix:r=e.envPrefix||_chunkCYBE3YNHcjs.e}=f,c=_vite.loadEnv.call(void 0, e.mode,t,r);try{_chunkCYBE3YNHcjs.h.call(void 0, v,{source:c,prefix:Array.isArray(r)?r[0]:r,loadProcessEnv:!1,throwOnError:!0}),n=null}catch(s){n=s,o?e.logger.error(s.message):(console.error(s.message),console.error(`\x1B[31m[safe-env] Fatal: Environment validation failed during build. Exiting...\x1B[0m
|
|
2
|
+
`),process.exit(1))}},transform(e,t){if(o&&n&&e.includes("@zh-moody/safe-env")){let r=new Error(n.plainMessage||n.message);throw r.stack="",r}return null}}}exports.viteSafeEnv = y;
|
package/dist/vite.d.cts
CHANGED
|
@@ -1,19 +1,9 @@
|
|
|
1
1
|
import { Plugin } from 'vite';
|
|
2
|
-
import { S as Schema } from './types-
|
|
2
|
+
import { S as Schema } from './types-C7kDNgjd.cjs';
|
|
3
3
|
|
|
4
|
-
interface ViteSafeEnvOptions {
|
|
5
|
-
/**
|
|
6
|
-
* 环境变量目录,默认为项目根目录
|
|
7
|
-
*/
|
|
8
|
-
envDir?: string;
|
|
9
|
-
/**
|
|
10
|
-
* 环境变量前缀,默认是 VITE_
|
|
11
|
-
*/
|
|
12
|
-
prefix?: string | string[];
|
|
13
|
-
}
|
|
14
4
|
/**
|
|
15
5
|
* Vite 插件:在构建或开发启动时校验环境变量
|
|
16
6
|
*/
|
|
17
|
-
declare function viteSafeEnv(schema: Schema, options?:
|
|
7
|
+
declare function viteSafeEnv(schema: Schema, options?: any): Plugin;
|
|
18
8
|
|
|
19
9
|
export { viteSafeEnv };
|
package/dist/vite.d.ts
CHANGED
|
@@ -1,19 +1,9 @@
|
|
|
1
1
|
import { Plugin } from 'vite';
|
|
2
|
-
import { S as Schema } from './types-
|
|
2
|
+
import { S as Schema } from './types-C7kDNgjd.js';
|
|
3
3
|
|
|
4
|
-
interface ViteSafeEnvOptions {
|
|
5
|
-
/**
|
|
6
|
-
* 环境变量目录,默认为项目根目录
|
|
7
|
-
*/
|
|
8
|
-
envDir?: string;
|
|
9
|
-
/**
|
|
10
|
-
* 环境变量前缀,默认是 VITE_
|
|
11
|
-
*/
|
|
12
|
-
prefix?: string | string[];
|
|
13
|
-
}
|
|
14
4
|
/**
|
|
15
5
|
* Vite 插件:在构建或开发启动时校验环境变量
|
|
16
6
|
*/
|
|
17
|
-
declare function viteSafeEnv(schema: Schema, options?:
|
|
7
|
+
declare function viteSafeEnv(schema: Schema, options?: any): Plugin;
|
|
18
8
|
|
|
19
9
|
export { viteSafeEnv };
|
package/dist/vite.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
import{b as
|
|
1
|
+
import{b as a,d as i,e as l,h as m}from"./chunk-MZ3R3VUV.js";import"./chunk-I7AUKTXE.js";import{loadEnv as p}from"vite";function y(v,f={}){let n=null,o=!1;return{name:"vite-plugin-safe-env",configResolved(e){o=e.command===a,process.env[i]=o?"true":"";let{envDir:t=e.root,prefix:r=e.envPrefix||l}=f,c=p(e.mode,t,r);try{m(v,{source:c,prefix:Array.isArray(r)?r[0]:r,loadProcessEnv:!1,throwOnError:!0}),n=null}catch(s){n=s,o?e.logger.error(s.message):(console.error(s.message),console.error(`\x1B[31m[safe-env] Fatal: Environment validation failed during build. Exiting...\x1B[0m
|
|
2
|
+
`),process.exit(1))}},transform(e,t){if(o&&n&&e.includes("@zh-moody/safe-env")){let r=new Error(n.plainMessage||n.message);throw r.stack="",r}return null}}}export{y as viteSafeEnv};
|
package/package.json
CHANGED
package/dist/chunk-MODJPD2X.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);var _path = require('path'); var _path2 = _interopRequireDefault(_path);function o(s){let i={},r=s.split(/\r?\n/);for(let f of r){let n=f.trim();if(!n||n.startsWith("#"))continue;let e=n.indexOf("=");if(e==-1)continue;let l=n.slice(0,e).trim(),t=n.slice(e+1).trim();(t.startsWith('"')&&t.endsWith('"')||t.startsWith("'")&&t.endsWith("'"))&&(t=t.slice(1,-1)),i[l]=t}return i}function g(s=".env",i){try{let r=_path2.default.resolve(i||process.cwd(),s);if(_fs2.default.existsSync(r))return o(_fs2.default.readFileSync(r,"utf-8"))}catch (e2){}return{}}exports.a = o; exports.b = g;
|
package/dist/chunk-UONNQTFU.js
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import{b as y}from"./chunk-WZTPUQ3S.js";function $(m){let e={r:"\x1B[31m",g:"\x1B[32m",y:"\x1B[33m",b:"\x1B[1m",res:"\x1B[0m",d:"\x1B[2m",cy:"\x1B[36m"},d=Math.max(80,process.stdout.columns||80)-10,f=Math.floor(d*.3),u=Math.floor(d*.5),l=(r,a)=>{let t=String(r),o=()=>t.length+(t.match(/[^\x00-\xff]/g)||[]).length;for(;o()>a;)t=t.slice(0,-1);return t+" ".repeat(a-o())};console.error(`
|
|
2
|
-
${e.r}${e.b}\u274C SafeEnv Validation Failed${e.res}`),console.error(` ${e.b}${l("Key",f)} \u2502 ${l("Error",u)} \u2502 Value${e.res}`),console.error(e.d+"\u2500".repeat(d+5)+e.res),m.forEach(r=>{let a=r.isSecret?"********":r.value===void 0?"undefined":`"${r.value}"`,t=r.isSecret?e.y:r.value===void 0?e.d:e.cy;console.error(` ${e.y}${l(r.key,f)}${e.res} \u2502 ${e.r}${l(r.error,u)}${e.res} \u2502 ${t}${a}${e.res}`)}),console.error(e.d+"\u2500".repeat(d+5)+e.res),console.error(` ${e.g}\u{1F4A1} Tip: Check your .env files or schema definitions.${e.res}
|
|
3
|
-
`)}function b(m,e={}){let{loadProcessEnv:d=!0,source:f,prefix:u="",cwd:l}=e,r;if(f!==void 0)r=f||{};else{let o=e.mode||(typeof process<"u"?process.env.NODE_ENV:"development"),s=[".env",`.env.${o}`,".env.local",`.env.${o}.local`],i={};for(let c of s)i={...i,...y(c,l)};r={...i,...d&&typeof process<"u"?process.env:{}}}let a={},t=[];for(let o in m){let s=m[o],i=s.sourceKey;if(!i){let n=u+o;r[n]!==void 0?i=n:r[o]!==void 0?i=o:i=u?n:o}let c=r[i];try{let n;if(c===void 0||c===""&&s.default!==void 0){if(s.required&&c===void 0)throw new Error("Required field missing");n=s.default}else n=s.parse(c);if(n!==void 0&&s.metadata){let{min:p,max:v,validate:E}=s.metadata;if(typeof n=="number"){if(p!==void 0&&n<p)throw new Error(`Below min ${p}`);if(v!==void 0&&n>v)throw new Error(`Above max ${v}`)}if(E&&!E.fn(n))throw new Error(E.message)}a[o]=n}catch(n){t.push({key:i,error:n.message,value:c,isSecret:s.metadata?.isSecret})}}if(t.length>0){$(t);let o=typeof process<"u"&&!!process.exit,s=typeof process<"u"&&process.env.NODE_ENV==="test";if(o&&!f&&!s)process.exit(1);else throw new Error("SafeEnv: Configuration validation failed.")}return Object.freeze(a)}export{$ as a,b};
|
package/dist/chunk-WXZ32K3G.cjs
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _chunkMODJPD2Xcjs = require('./chunk-MODJPD2X.cjs');function $(m){let e={r:"\x1B[31m",g:"\x1B[32m",y:"\x1B[33m",b:"\x1B[1m",res:"\x1B[0m",d:"\x1B[2m",cy:"\x1B[36m"},d=Math.max(80,process.stdout.columns||80)-10,f=Math.floor(d*.3),u=Math.floor(d*.5),l=(r,a)=>{let t=String(r),o=()=>t.length+(t.match(/[^\x00-\xff]/g)||[]).length;for(;o()>a;)t=t.slice(0,-1);return t+" ".repeat(a-o())};console.error(`
|
|
2
|
-
${e.r}${e.b}\u274C SafeEnv Validation Failed${e.res}`),console.error(` ${e.b}${l("Key",f)} \u2502 ${l("Error",u)} \u2502 Value${e.res}`),console.error(e.d+"\u2500".repeat(d+5)+e.res),m.forEach(r=>{let a=r.isSecret?"********":r.value===void 0?"undefined":`"${r.value}"`,t=r.isSecret?e.y:r.value===void 0?e.d:e.cy;console.error(` ${e.y}${l(r.key,f)}${e.res} \u2502 ${e.r}${l(r.error,u)}${e.res} \u2502 ${t}${a}${e.res}`)}),console.error(e.d+"\u2500".repeat(d+5)+e.res),console.error(` ${e.g}\u{1F4A1} Tip: Check your .env files or schema definitions.${e.res}
|
|
3
|
-
`)}function b(m,e={}){let{loadProcessEnv:d=!0,source:f,prefix:u="",cwd:l}=e,r;if(f!==void 0)r=f||{};else{let o=e.mode||(typeof process<"u"?process.env.NODE_ENV:"development"),s=[".env",`.env.${o}`,".env.local",`.env.${o}.local`],i={};for(let c of s)i={...i,..._chunkMODJPD2Xcjs.b.call(void 0, c,l)};r={...i,...d&&typeof process<"u"?process.env:{}}}let a={},t=[];for(let o in m){let s=m[o],i=s.sourceKey;if(!i){let n=u+o;r[n]!==void 0?i=n:r[o]!==void 0?i=o:i=u?n:o}let c=r[i];try{let n;if(c===void 0||c===""&&s.default!==void 0){if(s.required&&c===void 0)throw new Error("Required field missing");n=s.default}else n=s.parse(c);if(n!==void 0&&s.metadata){let{min:p,max:v,validate:E}=s.metadata;if(typeof n=="number"){if(p!==void 0&&n<p)throw new Error(`Below min ${p}`);if(v!==void 0&&n>v)throw new Error(`Above max ${v}`)}if(E&&!E.fn(n))throw new Error(E.message)}a[o]=n}catch(n){t.push({key:i,error:n.message,value:c,isSecret:_optionalChain([s, 'access', _ => _.metadata, 'optionalAccess', _2 => _2.isSecret])})}}if(t.length>0){$(t);let o=typeof process<"u"&&!!process.exit,s=typeof process<"u"&&process.env.NODE_ENV==="test";if(o&&!f&&!s)process.exit(1);else throw new Error("SafeEnv: Configuration validation failed.")}return Object.freeze(a)}exports.a = $; exports.b = b;
|
package/dist/chunk-WZTPUQ3S.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import c from"fs";import a from"path";function o(s){let i={},r=s.split(/\r?\n/);for(let f of r){let n=f.trim();if(!n||n.startsWith("#"))continue;let e=n.indexOf("=");if(e==-1)continue;let l=n.slice(0,e).trim(),t=n.slice(e+1).trim();(t.startsWith('"')&&t.endsWith('"')||t.startsWith("'")&&t.endsWith("'"))&&(t=t.slice(1,-1)),i[l]=t}return i}function g(s=".env",i){try{let r=a.resolve(i||process.cwd(),s);if(c.existsSync(r))return o(c.readFileSync(r,"utf-8"))}catch{}return{}}export{o as a,g as b};
|