New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

envsafe

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

envsafe - npm Package Compare versions

Comparing version 1.2.0-1 to 2.0.0

39

dist/envsafe.cjs.development.js

@@ -217,3 +217,3 @@ 'use strict';

if (typeof window !== 'undefined' && ((_window = window) === null || _window === void 0 ? void 0 : _window.alert)) {
if (typeof window !== 'undefined' && ((_window = window) == null ? void 0 : _window.alert)) {
window.alert(text);

@@ -230,9 +230,9 @@ }

validator = _ref.validator,
key = _ref.key,
validateKey = _ref.validateKey;
key = _ref.key;
var usingDevDefault = env.NODE_ENV !== 'production';
var raw = (_validator$input = validator.input) !== null && _validator$input !== void 0 ? _validator$input : env[key];
var raw = (_validator$input = validator.input) != null ? _validator$input : env[key];
if (validateKey && !validateKey(key)) {
throw new InvalidEnvError("Invalid environment key \"" + key + "\"");
if (!validator.allowEmpty && raw === '') {
// treat empty env vars as `undefined`
raw = undefined;
}

@@ -268,4 +268,3 @@

_ref2$strict = _ref2.strict,
strict = _ref2$strict === void 0 ? false : _ref2$strict,
validateKey = _ref2.validateKey;
strict = _ref2$strict === void 0 ? false : _ref2$strict;

@@ -282,4 +281,3 @@ var errors = {};

validator: validator,
key: key,
validateKey: validateKey
key: key
});

@@ -387,22 +385,2 @@ output[key] = resolved;

function nextjsWebpackPlugin(validators, webpack, env) {
if (env === void 0) {
env = process.env;
}
var cleanEnv = envsafe(validators, {
validateKey: function validateKey(key) {
if (!key.startsWith('NEXT_PUBLIC_')) {
throw new InvalidEnvError("Not prefixed with \"NEXT_PUBLIC_\"");
}
return true;
},
env: env
});
return new webpack.DefinePlugin({
'process.envsafe': JSON.stringify(cleanEnv)
});
}
exports.InvalidEnvError = InvalidEnvError;

@@ -419,3 +397,2 @@ exports.MissingEnvError = MissingEnvError;

exports.makeValidator = makeValidator;
exports.nextjsWebpackPlugin = nextjsWebpackPlugin;
exports.num = num;

@@ -422,0 +399,0 @@ exports.port = port;

@@ -1,2 +0,2 @@

"use strict";function e(){return(e=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function t(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t}function r(e){return(r=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function n(e,t){return(n=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function o(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(e){return!1}}function i(e,t,r){return(i=o()?Reflect.construct:function(e,t,r){var o=[null];o.push.apply(o,t);var i=new(Function.bind.apply(e,o));return r&&n(i,r.prototype),i}).apply(null,arguments)}function u(e){var t="function"==typeof Map?new Map:void 0;return(u=function(e){if(null===e||-1===Function.toString.call(e).indexOf("[native code]"))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,o)}function o(){return i(e,arguments,r(this).constructor)}return o.prototype=Object.create(e.prototype,{constructor:{value:o,enumerable:!1,writable:!0,configurable:!0}}),n(o,e)})(e)}function a(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}Object.defineProperty(exports,"__esModule",{value:!0});var c=function(e){function r(t){var n;return n=e.call(this,t)||this,Object.setPrototypeOf(a(n),r.prototype),n}return t(r,e),r}(u(TypeError)),s=function(e,t){return new c("Invalid "+e+' input: "'+t+'"')},f=function(e){function r(t){var n;return n=e.call(this,t)||this,Object.setPrototypeOf(a(n),r.prototype),n}return t(r,e),r}(u(ReferenceError)),p=["length","inspect","hasOwnProperty","toJSON","asymmetricMatch","nodeType","$$typeof","constructor","@@__IMMUTABLE_ITERABLE__@@","@@__IMMUTABLE_RECORD__@@","_isMockFunction","then","__esModule"];function l(e,t){var r=Object.freeze?Object.freeze(e):e;return global.Proxy?new Proxy(r,{get:function(e,n){if("string"!=typeof n||p.includes(n))return r[n];if(!r.hasOwnProperty(n)){if(t.hasOwnProperty(n))throw new ReferenceError("[envsafe] Env var "+String(n)+" was accessed but not validated. This var is set in the environment; please add an envsafe validator for it.");throw new ReferenceError('[envsafe] Env var "'+String(n)+'" not found')}return r[n]}}):r}function v(e){for(var t=e.errors,r=[],n=[],o=0,i=Object.keys(t);o<i.length;o++){var u=i[o],a=t[u];a instanceof f?n.push(" "+u+": "+(a.message||"required")):r.push(" "+u+": "+(a.message||"invalid"))}return r.length&&r.unshift("❌ Invalid environment variables:"),n.length&&n.unshift("💨 Missing environment variables:"),["========================================"].concat(r,n,["========================================"]).join("\n")}function d(e){var t,r=v(e);throw console.error(r),"undefined"!=typeof window&&(null===(t=window)||void 0===t?void 0:t.alert)&&window.alert(r),new TypeError(r)}function h(e){var t,r=e.env,n=e.validator,o=e.key,i=e.validateKey,u="production"!==r.NODE_ENV,a=null!==(t=n.input)&&void 0!==t?t:r[o];if(i&&!i(o))throw new c('Invalid environment key "'+o+'"');if(void 0===a&&u&&void 0!==n.devDefault&&(a=n.devDefault),void 0===a&&void 0!==n.default&&(a=n.default),void 0===a)throw new f("Missing value");var s=n._parse(a);if(n.choices&&!n.choices.includes(s))throw new c('Value "'+s+'" not in choices ['+n.choices+"]");return s}function y(e,t){var r=void 0===t?{}:t,n=r.reporter,o=void 0===n?d:n,i=r.env,u=void 0===i?process.env:i,a=r.strict,c=void 0!==a&&a,s=r.validateKey,f={},p={};for(var v in e){var y=e[v];try{var w=h({env:u,validator:y,key:v,validateKey:s});p[v]=w}catch(e){f[v]=e}}return Object.keys(f).length&&o({errors:f,output:p,env:u}),c?l(p,u):p}var w=/^[^@\s]+@[^@\s]+\.[^@\s]+$/;function _(t){return function(r){return void 0===r&&(r={}),e({},r,{_parse:t})}}var O=_((function(e){switch(e){case!0:case"true":case"t":case"1":return!0;case!1:case"false":case"f":case"0":return!1;default:throw s("str",e)}})),b=_((function(e){if("string"!=typeof e)throw s("str",e);return e})),g=_((function(e){if(!w.test(e))throw s("email",e);return e})),x=_((function(e){var t=+e;if(Number.isNaN(t))throw s("num",e);return t})),E=_((function(e){var t=+e;if(Number.isNaN(t)||""+t!=""+e||t%1!=0||t<1||t>65535)throw s("port",e);return t})),j=_((function(e){try{return new URL(e),e}catch(t){throw s("url",e)}})),m=_((function(e){try{return"string"!=typeof e?e:JSON.parse(e)}catch(t){throw s("json",e)}}));exports.InvalidEnvError=c,exports.MissingEnvError=f,exports.bool=O,exports.defaultReporter=d,exports.defaultReporterText=v,exports.email=g,exports.envsafe=y,exports.freezeObject=l,exports.invalidEnvError=s,exports.json=m,exports.makeValidator=_,exports.nextjsWebpackPlugin=function(e,t,r){void 0===r&&(r=process.env);var n=y(e,{validateKey:function(e){if(!e.startsWith("NEXT_PUBLIC_"))throw new c('Not prefixed with "NEXT_PUBLIC_"');return!0},env:r});return new t.DefinePlugin({"process.envsafe":JSON.stringify(n)})},exports.num=x,exports.port=E,exports.str=b,exports.url=j;
"use strict";function t(){return(t=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function e(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e}function r(t){return(r=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function n(t,e){return(n=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function o(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}function i(t,e,r){return(i=o()?Reflect.construct:function(t,e,r){var o=[null];o.push.apply(o,e);var i=new(Function.bind.apply(t,o));return r&&n(i,r.prototype),i}).apply(null,arguments)}function u(t){var e="function"==typeof Map?new Map:void 0;return(u=function(t){if(null===t||-1===Function.toString.call(t).indexOf("[native code]"))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,o)}function o(){return i(t,arguments,r(this).constructor)}return o.prototype=Object.create(t.prototype,{constructor:{value:o,enumerable:!1,writable:!0,configurable:!0}}),n(o,t)})(t)}function c(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}Object.defineProperty(exports,"__esModule",{value:!0});var s=function(t){function r(e){var n;return n=t.call(this,e)||this,Object.setPrototypeOf(c(n),r.prototype),n}return e(r,t),r}(u(TypeError)),a=function(t,e){return new s("Invalid "+t+' input: "'+e+'"')},f=function(t){function r(e){var n;return n=t.call(this,e)||this,Object.setPrototypeOf(c(n),r.prototype),n}return e(r,t),r}(u(ReferenceError)),p=["length","inspect","hasOwnProperty","toJSON","asymmetricMatch","nodeType","$$typeof","constructor","@@__IMMUTABLE_ITERABLE__@@","@@__IMMUTABLE_RECORD__@@","_isMockFunction","then","__esModule"];function l(t,e){var r=Object.freeze?Object.freeze(t):t;return global.Proxy?new Proxy(r,{get:function(t,n){if("string"!=typeof n||p.includes(n))return r[n];if(!r.hasOwnProperty(n)){if(e.hasOwnProperty(n))throw new ReferenceError("[envsafe] Env var "+String(n)+" was accessed but not validated. This var is set in the environment; please add an envsafe validator for it.");throw new ReferenceError('[envsafe] Env var "'+String(n)+'" not found')}return r[n]}}):r}function v(t){for(var e=t.errors,r=[],n=[],o=0,i=Object.keys(e);o<i.length;o++){var u=i[o],c=e[u];c instanceof f?n.push(" "+u+": "+(c.message||"required")):r.push(" "+u+": "+(c.message||"invalid"))}return r.length&&r.unshift("❌ Invalid environment variables:"),n.length&&n.unshift("💨 Missing environment variables:"),["========================================"].concat(r,n,["========================================"]).join("\n")}function d(t){var e,r=v(t);throw console.error(r),"undefined"!=typeof window&&(null==(e=window)?void 0:e.alert)&&window.alert(r),new TypeError(r)}function h(t){var e,r=t.env,n=t.validator,o=null!=(e=n.input)?e:r[t.key];if(n.allowEmpty||""!==o||(o=void 0),void 0===o&&"production"!==r.NODE_ENV&&void 0!==n.devDefault&&(o=n.devDefault),void 0===o&&void 0!==n.default&&(o=n.default),void 0===o)throw new f("Missing value");var i=n._parse(o);if(n.choices&&!n.choices.includes(i))throw new s('Value "'+i+'" not in choices ['+n.choices+"]");return i}var y=/^[^@\s]+@[^@\s]+\.[^@\s]+$/;function w(e){return function(r){return void 0===r&&(r={}),t({},r,{_parse:e})}}var O=w((function(t){switch(t){case!0:case"true":case"t":case"1":return!0;case!1:case"false":case"f":case"0":return!1;default:throw a("str",t)}})),b=w((function(t){if("string"!=typeof t)throw a("str",t);return t})),_=w((function(t){if(!y.test(t))throw a("email",t);return t})),g=w((function(t){var e=+t;if(Number.isNaN(e))throw a("num",t);return e})),E=w((function(t){var e=+t;if(Number.isNaN(e)||""+e!=""+t||e%1!=0||e<1||e>65535)throw a("port",t);return e})),x=w((function(t){try{return new URL(t),t}catch(e){throw a("url",t)}})),j=w((function(t){try{return"string"!=typeof t?t:JSON.parse(t)}catch(e){throw a("json",t)}}));exports.InvalidEnvError=s,exports.MissingEnvError=f,exports.bool=O,exports.defaultReporter=d,exports.defaultReporterText=v,exports.email=_,exports.envsafe=function(t,e){var r=void 0===e?{}:e,n=r.reporter,o=void 0===n?d:n,i=r.env,u=void 0===i?process.env:i,c=r.strict,s=void 0!==c&&c,a={},f={};for(var p in t){var v=t[p];try{var y=h({env:u,validator:v,key:p});f[p]=y}catch(t){a[p]=t}}return Object.keys(a).length&&o({errors:a,output:f,env:u}),s?l(f,u):f},exports.freezeObject=l,exports.invalidEnvError=a,exports.json=j,exports.makeValidator=w,exports.num=g,exports.port=E,exports.str=b,exports.url=x;
//# sourceMappingURL=envsafe.cjs.production.min.js.map
import { EnvsafeOpts, Validators } from './types';
export declare function envsafe<TCleanEnv>(validators: Validators<TCleanEnv>, { reporter, env, strict, validateKey, }?: EnvsafeOpts<TCleanEnv>): Readonly<TCleanEnv>;
export declare function envsafe<TCleanEnv>(validators: Validators<TCleanEnv>, { reporter, env, strict, }?: EnvsafeOpts<TCleanEnv>): Readonly<TCleanEnv>;

@@ -213,3 +213,3 @@ function _extends() {

if (typeof window !== 'undefined' && ((_window = window) === null || _window === void 0 ? void 0 : _window.alert)) {
if (typeof window !== 'undefined' && ((_window = window) == null ? void 0 : _window.alert)) {
window.alert(text);

@@ -226,9 +226,9 @@ }

validator = _ref.validator,
key = _ref.key,
validateKey = _ref.validateKey;
key = _ref.key;
var usingDevDefault = env.NODE_ENV !== 'production';
var raw = (_validator$input = validator.input) !== null && _validator$input !== void 0 ? _validator$input : env[key];
var raw = (_validator$input = validator.input) != null ? _validator$input : env[key];
if (validateKey && !validateKey(key)) {
throw new InvalidEnvError("Invalid environment key \"" + key + "\"");
if (!validator.allowEmpty && raw === '') {
// treat empty env vars as `undefined`
raw = undefined;
}

@@ -264,4 +264,3 @@

_ref2$strict = _ref2.strict,
strict = _ref2$strict === void 0 ? false : _ref2$strict,
validateKey = _ref2.validateKey;
strict = _ref2$strict === void 0 ? false : _ref2$strict;

@@ -278,4 +277,3 @@ var errors = {};

validator: validator,
key: key,
validateKey: validateKey
key: key
});

@@ -383,23 +381,3 @@ output[key] = resolved;

function nextjsWebpackPlugin(validators, webpack, env) {
if (env === void 0) {
env = process.env;
}
var cleanEnv = envsafe(validators, {
validateKey: function validateKey(key) {
if (!key.startsWith('NEXT_PUBLIC_')) {
throw new InvalidEnvError("Not prefixed with \"NEXT_PUBLIC_\"");
}
return true;
},
env: env
});
return new webpack.DefinePlugin({
'process.envsafe': JSON.stringify(cleanEnv)
});
}
export { InvalidEnvError, MissingEnvError, bool, defaultReporter, defaultReporterText, email, envsafe, freezeObject, invalidEnvError, json, makeValidator, nextjsWebpackPlugin, num, port, str, url };
export { InvalidEnvError, MissingEnvError, bool, defaultReporter, defaultReporterText, email, envsafe, freezeObject, invalidEnvError, json, makeValidator, num, port, str, url };
//# sourceMappingURL=envsafe.esm.js.map

@@ -7,2 +7,1 @@ export * from './envsafe';

export * from './freezeObject';
export * from './plugins/webpack';
export interface Spec<TValue> {
/**
* An Array that lists the admissable parsed values for the env var.
* An Array that lists the admissible parsed values for the env var.
*/

@@ -11,3 +11,3 @@ choices?: ReadonlyArray<TValue>;

/**
* A fallback value to use only when NODE_ENV is not 'production'.
* A fallback value to use only when `NODE_ENV` is not `production`.
* This is handy for env vars that are required for production environments, but optional for development and testing.

@@ -17,2 +17,8 @@ */

/**
* Do not treat an empty string as a missing variable.
* It will then not be defaulted over and error if it isn't allowed.
* @default false
*/
allowEmpty?: boolean;
/**
* A string that describes the env var.

@@ -46,3 +52,2 @@ */

export declare type ReporterFn<TCleanEnv> = (opts: ReporterOpts<TCleanEnv>) => void;
export declare type KeyValidator = (key: string) => boolean;
export declare type EnvsafeOpts<TCleanEnv> = {

@@ -64,6 +69,2 @@ /**

strict?: boolean;
/**
* Used in webpack plugins to validate that the key is in an allowlist of keys
*/
validateKey?: (key: string) => true;
};

@@ -70,0 +71,0 @@ export declare type Validators<TCleanEnv> = {

{
"version": "1.2.0-1",
"version": "2.0.0",
"license": "MIT",

@@ -37,9 +37,8 @@ "main": "dist/index.js",

"devDependencies": {
"@types/webpack": "^4.41.22",
"husky": "4.3.0",
"husky": "4.3.6",
"np": "*",
"tsdx": "0.13.3",
"tslib": "2.0.1",
"typescript": "4.0.2"
"tsdx": "0.14.1",
"tslib": "2.0.3",
"typescript": "4.1.3"
}
}

@@ -113,2 +113,3 @@ [![Maintainability](https://api.codeclimate.com/v1/badges/c17614f0d80b810e47a6/maintainability)](https://codeclimate.com/github/KATT/envsafe/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/c17614f0d80b810e47a6/test_coverage)](https://codeclimate.com/github/KATT/envsafe/test_coverage)

| `input` | `string` | As some environments don't allow you to dynamically read env vars, we can manually put it in as well. [Example](https://github.com/KATT/envsafe/blob/f74736a24560f22cee5694d48546a247a47425bd/examples/next.js/utils/browserEnv.js#L6-L8) |
| `allowEmpty` | `boolean` | Default behavior is `false` which treats empty strings as the value is missing; if explicit empty strings are OK, pass in `true`. |

@@ -115,0 +116,0 @@ These values below are not used by the library and only for description of the variables.

@@ -10,3 +10,2 @@ import { InvalidEnvError, MissingEnvError } from './errors';

Validators,
KeyValidator,
} from './types';

@@ -18,3 +17,2 @@

key,
validateKey,
}: {

@@ -24,3 +22,2 @@ env: Environment;

key: string;
validateKey?: KeyValidator;
}): TValue {

@@ -30,5 +27,5 @@ const usingDevDefault = env.NODE_ENV !== 'production';

let raw: string | TValue | undefined = validator.input ?? env[key];
if (validateKey && !validateKey(key)) {
throw new InvalidEnvError(`Invalid environment key "${key}"`);
if (!validator.allowEmpty && raw === '') {
// treat empty env vars as `undefined`
raw = undefined;
}

@@ -67,3 +64,2 @@

strict = false,
validateKey,
}: EnvsafeOpts<TCleanEnv> = {},

@@ -77,3 +73,3 @@ ): Readonly<TCleanEnv> {

try {
const resolved = getValueOrThrow({ env, validator, key, validateKey });
const resolved = getValueOrThrow({ env, validator, key });
output[key] = resolved;

@@ -80,0 +76,0 @@ } catch (err) {

@@ -7,2 +7,1 @@ export * from './envsafe';

export * from './freezeObject';
export * from './plugins/webpack';
export interface Spec<TValue> {
/**
* An Array that lists the admissable parsed values for the env var.
* An Array that lists the admissible parsed values for the env var.
*/

@@ -11,3 +11,3 @@ choices?: ReadonlyArray<TValue>;

/**
* A fallback value to use only when NODE_ENV is not 'production'.
* A fallback value to use only when `NODE_ENV` is not `production`.
* This is handy for env vars that are required for production environments, but optional for development and testing.

@@ -17,2 +17,8 @@ */

/**
* Do not treat an empty string as a missing variable.
* It will then not be defaulted over and error if it isn't allowed.
* @default false
*/
allowEmpty?: boolean;
/**
* A string that describes the env var.

@@ -51,3 +57,3 @@ */

export type ReporterFn<TCleanEnv> = (opts: ReporterOpts<TCleanEnv>) => void;
export type KeyValidator = (key: string) => boolean;
export type EnvsafeOpts<TCleanEnv> = {

@@ -69,6 +75,2 @@ /**

strict?: boolean;
/**
* Used in webpack plugins to validate that the key is in an allowlist of keys
*/
validateKey?: (key: string) => true;
};

@@ -75,0 +77,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc