bini-env 1.0.5 → 1.0.7
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/LICENSE +21 -0
- package/README.md +253 -0
- package/dist/index.d.mts +37 -0
- package/dist/index.d.ts +18 -22
- package/dist/index.js +206 -102
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +235 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +53 -20
- package/Readme.md +0 -135
- package/dist/index.cjs +0 -178
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -41
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Binidu Ranasinghe
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
# bini-env
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+

|
|
9
|
+
|
|
10
|
+
**Zero-config environment variable system + Vite plugin for Bini.js**
|
|
11
|
+
Loads `.env` in development, uses host-provided variables in production, and works across Node.js, Bun, Deno, and edge runtimes — without leaking secrets or adding runtime cost.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## ⚠️ Before You Use This
|
|
16
|
+
|
|
17
|
+
This library **does NOT magically make env vars safe**.
|
|
18
|
+
|
|
19
|
+
* Anything exposed to the client (`import.meta.env`) is **public**.
|
|
20
|
+
* Only server-side code (`getEnv`, `requireEnv`) can safely access secrets.
|
|
21
|
+
* Misconfigured prefixes = **data leak**.
|
|
22
|
+
|
|
23
|
+
If you don’t understand this, stop and fix that first.
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## ✨ Features
|
|
28
|
+
|
|
29
|
+
* **Universal API** — `getEnv()` / `requireEnv()` work across runtimes
|
|
30
|
+
* **Zero-config `.env` loading** in development
|
|
31
|
+
* **Strict production behavior** — no file reads, no dotenv
|
|
32
|
+
* **Prefix control** — supports `BINI_`, `VITE_`, or custom
|
|
33
|
+
* **Tree-shakeable** — no dead code in client bundles
|
|
34
|
+
* **Edge-safe** — no static dotenv import
|
|
35
|
+
* **Typed** — full TypeScript support
|
|
36
|
+
* **Fast** — single load in dev, zero overhead in prod
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## 📦 Installation
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pnpm add bini-env
|
|
44
|
+
# or
|
|
45
|
+
npm install bini-env
|
|
46
|
+
# or
|
|
47
|
+
yarn add bini-env
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## 🚀 Quick Start (Don’t Overthink It)
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
// vite.config.ts
|
|
56
|
+
import { defineConfig } from 'vite';
|
|
57
|
+
import { biniEnv } from 'bini-env';
|
|
58
|
+
|
|
59
|
+
export default defineConfig({
|
|
60
|
+
plugins: [biniEnv()]
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Done.
|
|
65
|
+
|
|
66
|
+
If this doesn’t work, your project setup is broken — not the plugin.
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## 🔐 Environment Rules (Read This Twice)
|
|
71
|
+
|
|
72
|
+
> ✅ **Good news:** Both `BINI_` and `VITE_` prefixes work out of the box — no extra config needed.
|
|
73
|
+
|
|
74
|
+
### Client (PUBLIC)
|
|
75
|
+
|
|
76
|
+
```env
|
|
77
|
+
BINI_PUBLIC_API_URL=https://api.example.com
|
|
78
|
+
VITE_ANALYTICS_ID=UA-XXXX
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Accessible via:
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import.meta.env.BINI_PUBLIC_API_URL
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
👉 **Never put secrets here. Ever.**
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
### Server (PRIVATE)
|
|
92
|
+
|
|
93
|
+
```env
|
|
94
|
+
SMTP_PASS=super_secret
|
|
95
|
+
DATABASE_URL=postgres://...
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
import { requireEnv } from 'bini-env';
|
|
100
|
+
|
|
101
|
+
const pass = requireEnv('SMTP_PASS');
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
👉 If this leaks, it’s your fault, not the library’s.
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## 🧠 How It Actually Works
|
|
109
|
+
|
|
110
|
+
| Mode | Behavior |
|
|
111
|
+
| ---------------- | ---------------------------------------- |
|
|
112
|
+
| Dev (`vite dev`) | Loads `.env` once via dynamic dotenv |
|
|
113
|
+
| Preview | Same as dev |
|
|
114
|
+
| Production | Uses `process.env` / host injection only |
|
|
115
|
+
| Edge/Deno | Uses native `Deno.env` |
|
|
116
|
+
|
|
117
|
+
**No runtime branching in client bundles. No hidden magic.**
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## ⚙️ Plugin Options
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
biniEnv({
|
|
125
|
+
enabled: true,
|
|
126
|
+
clearViteHeader: true,
|
|
127
|
+
logo: 'ß',
|
|
128
|
+
envPrefix: ['BINI_', 'VITE_']
|
|
129
|
+
});
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Critical Detail
|
|
133
|
+
|
|
134
|
+
If you change `envPrefix`, you are changing what gets exposed to the browser.
|
|
135
|
+
|
|
136
|
+
Break this → you leak secrets.
|
|
137
|
+
|
|
138
|
+
---
|
|
139
|
+
|
|
140
|
+
## 📚 API
|
|
141
|
+
|
|
142
|
+
### `getEnv(key)`
|
|
143
|
+
|
|
144
|
+
Returns `string | undefined`.
|
|
145
|
+
|
|
146
|
+
Safe fallback reader across:
|
|
147
|
+
|
|
148
|
+
* `Deno.env`
|
|
149
|
+
* `process.env`
|
|
150
|
+
* `import.meta.env`
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
### `requireEnv(key)`
|
|
155
|
+
|
|
156
|
+
Same as `getEnv` but throws:
|
|
157
|
+
|
|
158
|
+
```txt
|
|
159
|
+
[bini-env] Missing required environment variable: "SMTP_PASS"
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Use this for anything critical.
|
|
163
|
+
|
|
164
|
+
---
|
|
165
|
+
|
|
166
|
+
### `biniEnv(options)`
|
|
167
|
+
|
|
168
|
+
Vite plugin.
|
|
169
|
+
|
|
170
|
+
If your plugin order is wrong and things break, that’s on your config.
|
|
171
|
+
|
|
172
|
+
---
|
|
173
|
+
|
|
174
|
+
## 📂 Env File Resolution Order
|
|
175
|
+
|
|
176
|
+
1. `.env.local`
|
|
177
|
+
2. `.env.[mode].local`
|
|
178
|
+
3. `.env.[mode]`
|
|
179
|
+
4. `.env`
|
|
180
|
+
|
|
181
|
+
Loaded once. Cached. No repeated disk reads.
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
## ⚡ Performance
|
|
186
|
+
|
|
187
|
+
| Metric | Dev | Prod |
|
|
188
|
+
| ------------- | --------- | ----------- |
|
|
189
|
+
| File Reads | 1–5 | 0 |
|
|
190
|
+
| Runtime Cost | ~5ms once | 0 |
|
|
191
|
+
| Bundle Impact | Minimal | Tree-shaken |
|
|
192
|
+
|
|
193
|
+
If you see overhead in production, you did something wrong.
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
## 🔥 Common Failure Modes
|
|
198
|
+
|
|
199
|
+
### 1. “Env is undefined”
|
|
200
|
+
|
|
201
|
+
You forgot the prefix.
|
|
202
|
+
|
|
203
|
+
### 2. “Works in dev, broken in prod”
|
|
204
|
+
|
|
205
|
+
You relied on `.env` in production.
|
|
206
|
+
|
|
207
|
+
### 3. “Secrets leaked”
|
|
208
|
+
|
|
209
|
+
You exposed them via prefix.
|
|
210
|
+
|
|
211
|
+
### 4. “Types not found”
|
|
212
|
+
|
|
213
|
+
Add:
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
/// <reference types="vite/client" />
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
---
|
|
220
|
+
|
|
221
|
+
## 🧪 Reality Check
|
|
222
|
+
|
|
223
|
+
This library is intentionally simple.
|
|
224
|
+
|
|
225
|
+
If you need:
|
|
226
|
+
|
|
227
|
+
* secret rotation
|
|
228
|
+
* encrypted envs
|
|
229
|
+
* runtime validation schemas
|
|
230
|
+
|
|
231
|
+
That’s **your job**, not this package.
|
|
232
|
+
|
|
233
|
+
---
|
|
234
|
+
|
|
235
|
+
## 🤝 Contributing
|
|
236
|
+
|
|
237
|
+
PRs welcome — but:
|
|
238
|
+
|
|
239
|
+
* No bloat
|
|
240
|
+
* No magic
|
|
241
|
+
* No runtime cost
|
|
242
|
+
|
|
243
|
+
If it slows startup or increases bundle size, it’s getting rejected.
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
## 📄 License
|
|
248
|
+
|
|
249
|
+
MIT © Bini.js Team
|
|
250
|
+
|
|
251
|
+
---
|
|
252
|
+
|
|
253
|
+
**Ship fast. Leak nothing. Blame config, not tooling.**
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
export { Plugin } from 'vite';
|
|
3
|
+
|
|
4
|
+
interface BiniEnvPluginOptions {
|
|
5
|
+
readonly enabled?: boolean;
|
|
6
|
+
readonly clearViteHeader?: boolean;
|
|
7
|
+
readonly logo?: string;
|
|
8
|
+
readonly envPrefix?: string | string[];
|
|
9
|
+
}
|
|
10
|
+
interface DetectedEnvFile {
|
|
11
|
+
readonly name: string;
|
|
12
|
+
readonly path: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Read an environment variable with caching for performance.
|
|
16
|
+
* Cache is cleared on module reload in development only.
|
|
17
|
+
*/
|
|
18
|
+
declare function getEnv(key: string): string | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Optimized requireEnv with cached error messages
|
|
21
|
+
*/
|
|
22
|
+
declare function requireEnv(key: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* Load .env file with memoization and race condition prevention
|
|
25
|
+
*/
|
|
26
|
+
declare function loadEnv(projectRoot?: string): Promise<void>;
|
|
27
|
+
declare function detectEnvFiles(projectRoot?: string): DetectedEnvFile[];
|
|
28
|
+
/**
|
|
29
|
+
* Production-ready Vite plugin with performance optimizations:
|
|
30
|
+
* - Lazy loading
|
|
31
|
+
* - Caching
|
|
32
|
+
* - Minimal bundle impact
|
|
33
|
+
* - No runtime overhead in production
|
|
34
|
+
*/
|
|
35
|
+
declare function biniEnv(options?: Readonly<BiniEnvPluginOptions>): Plugin;
|
|
36
|
+
|
|
37
|
+
export { type BiniEnvPluginOptions, type DetectedEnvFile, biniEnv, detectEnvFiles, getEnv, loadEnv, requireEnv };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,40 +2,36 @@ import { Plugin } from 'vite';
|
|
|
2
2
|
export { Plugin } from 'vite';
|
|
3
3
|
|
|
4
4
|
interface BiniEnvPluginOptions {
|
|
5
|
-
/**
|
|
6
|
-
* Enable/disable the plugin
|
|
7
|
-
* @default true
|
|
8
|
-
* @readonly
|
|
9
|
-
*/
|
|
10
5
|
readonly enabled?: boolean;
|
|
11
|
-
/**
|
|
12
|
-
* Remove Vite's default header
|
|
13
|
-
* @default true
|
|
14
|
-
* @readonly
|
|
15
|
-
*/
|
|
16
6
|
readonly clearViteHeader?: boolean;
|
|
17
|
-
/**
|
|
18
|
-
* Custom logo/text to display
|
|
19
|
-
* @default 'ß'
|
|
20
|
-
* @readonly
|
|
21
|
-
*/
|
|
22
7
|
readonly logo?: string;
|
|
8
|
+
readonly envPrefix?: string | string[];
|
|
23
9
|
}
|
|
24
10
|
interface DetectedEnvFile {
|
|
25
11
|
readonly name: string;
|
|
26
12
|
readonly path: string;
|
|
27
13
|
}
|
|
28
14
|
/**
|
|
29
|
-
*
|
|
15
|
+
* Read an environment variable with caching for performance.
|
|
16
|
+
* Cache is cleared on module reload in development only.
|
|
30
17
|
*/
|
|
31
|
-
declare function
|
|
18
|
+
declare function getEnv(key: string): string | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Optimized requireEnv with cached error messages
|
|
21
|
+
*/
|
|
22
|
+
declare function requireEnv(key: string): string;
|
|
32
23
|
/**
|
|
33
|
-
*
|
|
24
|
+
* Load .env file with memoization and race condition prevention
|
|
34
25
|
*/
|
|
35
|
-
declare function
|
|
26
|
+
declare function loadEnv(projectRoot?: string): Promise<void>;
|
|
27
|
+
declare function detectEnvFiles(projectRoot?: string): DetectedEnvFile[];
|
|
36
28
|
/**
|
|
37
|
-
*
|
|
29
|
+
* Production-ready Vite plugin with performance optimizations:
|
|
30
|
+
* - Lazy loading
|
|
31
|
+
* - Caching
|
|
32
|
+
* - Minimal bundle impact
|
|
33
|
+
* - No runtime overhead in production
|
|
38
34
|
*/
|
|
39
|
-
declare function
|
|
35
|
+
declare function biniEnv(options?: Readonly<BiniEnvPluginOptions>): Plugin;
|
|
40
36
|
|
|
41
|
-
export { type BiniEnvPluginOptions, type DetectedEnvFile,
|
|
37
|
+
export { type BiniEnvPluginOptions, type DetectedEnvFile, biniEnv, detectEnvFiles, getEnv, loadEnv, requireEnv };
|