Skip to content

Latest commit

 

History

History

env

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

@tupik/env

Environment validated at startup for Tupik: typed variables, secrets that never reach logs and Cloudflare bindings checked by shape. If something is missing or wrong, the app does not start. Zero dependencies, no node:* imports.

Install

npm install @tupik/env

Node, Bun

Validate before serve: a bad environment throws one EnvError listing every problem (names only, never values).

import { defineEnv, number, oneOf, secret, url } from '@tupik/env';
import { serve } from '@tupik/node';

const env = defineEnv({
  JWT_SECRET: secret({ minLength: 32 }),
  PUBLIC_URL: url(),
  MODE: oneOf(['development', 'production']),
  PORT: number({ integer: true, default: 3000 }),
}).parse(); // process.env or Bun.env

serve(app, { port: env.PORT });

Cloudflare Workers

import type { ExecutionContextLike } from '@tupik/core';
import { binding, defineEnv, secret, withEnv } from '@tupik/env';
import { app } from './app.js'; // your createApp(...)

const env = defineEnv({ JWT_SECRET: secret({ minLength: 32 }), DB: binding.d1(), CACHE: binding.kv() });

// Type `ctx` (ExecutionContextLike, or ExecutionContext from @cloudflare/workers-types): left out, withEnv infers
// `unknown`, which app.fetch does not accept in strict mode.
export default { fetch: withEnv(env, (request, vars, ctx: ExecutionContextLike) => app.fetch(request, vars, ctx)) };

withEnv validates once per isolate on the first request. An invalid env logs the problems once and answers a generic 500 application/problem+json: the app never runs half-configured.

Key API

Export Purpose
defineEnv(shape) .parse(source?) (throws EnvError), .safeParse(source?), .shape; InferEnv<typeof env>
string, secret, url, number, boolean, oneOf Builders with optional / default (secret has no default); any synchronous Standard Schema also works
binding.d1/kv/r2/queue/durableObject/rateLimit/service() Duck-typed bindings; binding.d1<D1Database>() for the real type
withEnv(definition, handler, { log? }) Workers fetch guarded by the env

JSON.stringify(env) and console.log(env) show secrets as '[redacted]' and bindings as '[binding]'. A secret read directly (env.JWT_SECRET) is a plain string: do not log it.

License

MIT