{"name":"@endo/env-options","version":"1.1.0","private":null,"description":"Reading environment options.","keywords":[],"author":{"name":"Endo contributors"},"license":"Apache-2.0","homepage":"https://github.com/endojs/endo/tree/master/packages/env-options#readme","repository":{"type":"git","url":"git+https://github.com/endojs/endo.git"},"bugs":{"url":"https://github.com/endojs/endo/issues"},"type":"module","main":"./index.js","module":"./index.js","browser":null,"unpkg":null,"types":null,"exports":{".":"./index.js","./package.json":"./package.json"},"scripts":{"build":"exit 0","lint":"yarn lint:types && yarn lint:eslint","lint-fix":"eslint --fix .","lint:eslint":"eslint .","lint:types":"tsc","build:types":"tsc --build tsconfig.build.json","clean:types":"git clean -f '*.d.ts*'","test":"exit 0"},"devDependencies":{"ava":"^5.3.0","babel-eslint":"^10.0.3","eslint":"^8.46.0","eslint-config-airbnb-base":"^15.0.0","eslint-config-prettier":"^8.8.0","eslint-plugin-eslint-comments":"^3.1.2","eslint-plugin-import":"^2.29.0","prettier":"^3.0.0","typescript":"~5.3.3"},"publishConfig":{"access":"public"},"eslintConfig":{"extends":["plugin:@endo/internal"],"rules":{"@endo/no-polymorphic-call":"error"},"overrides":[{"files":["test/**/*.js","demos/**/*.js","scripts/**/*.js"],"rules":{"@endo/no-polymorphic-call":"off"}}]},"prettier":null,"ava":{"files":["test/**/test-*.js"],"timeout":"2m"},"typeCoverage":{"atLeast":74.44},"gitHead":"373f9eebab66c94ed42350473c90fb25e6054f0a","_id":"@endo/env-options@1.1.0","_nodeVersion":"18.19.0","_npmVersion":"lerna/5.6.2/node@v18.19.0+x64 (darwin)","dist":{"integrity":"sha512-5D1AcZnoeaymT0yzccceyu1fbMJ7vqyrOluFC+ebVh7pDLcF5EqP27lyBdw1/GKSM60u1MOn9h9YVxmNC/Gi4A==","shasum":"468d18c6cab6aaa5be733ba067750624ba6bac2b","tarball":"http://registry-npm.ywwl.com/@endo/env-options/download/@endo/env-options-1.1.0.tgz","fileCount":9,"unpackedSize":28044,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQD49B56MHaeHl4W56xnb3V5ICZ12i41MRymUV2bH9FydAIgWqYScRa9xQTlmdkInPhQMRDlh5LwQmcubFukUgJtGsw="}],"size":10344,"noattachment":false,"key":"/@endo/env-options/-/@endo/env-options-1.1.0.tgz"},"_npmUser":{"name":"kriskowal","email":"kris.kowal@cixar.com"},"directories":{},"maintainers":[{"name":"mhofman","email":"mathieu+npmjs@agoric.com"},{"name":"kriskowal","email":"kris.kowal@cixar.com"},{"name":"michaelfig","email":"michael@fig.org"},{"name":"erights","email":"erights@gmail.com"},{"name":"warner","email":"warner-npm@lothar.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/env-options_1.1.0_1705538017821_0.5138638371421753"},"_hasShrinkwrap":false,"readme":"# Parameterizing Modules with Environment Options\n\nJavaScript module semantics resist attempts to parameterize a module's\ninitialization behavior. A module initializes in order according to\nthe path by which it is first imported, and then the initialized module\nis reused by all the other times it is imported. Compartments give us\nthe opportunity to bind the same import name to different imported\nmodules, depending on the package/compartment doing the import. Compartments\nalso address the difficulty of parameterizing a module's initialization\nlogic, but not in a pleasant manner.\n\nA pleasant parameterization would be for a static module to be function-like\nwith explicit parameters, and for the parameterization to be like\ncalling the static module with parameters in order to derive from it a\nmodule instance. Compartments instead lets us parameterize the meaning\nof a module instance derived from a static module according to the\nthree namespaces provided by the JavaScript semantics, affecting the\nmeaning of a module instance.\n   * The global variable namespaces.\n      * The global scope, aliased to properties of the global object.\n        This is necessarily compartment-wide. In our\n        recommened usage pattern of one compartment per package,\n        each global would be package-wide. (See LavaMoat)\n      * The global lexical scope. The SES-shim compartments support\n        these both compartment-wide as well as per-module. But it is\n        not yet clear what we will propose in the Compartment proposal.\n   * The import namespace.\n   * The host hooks.\n\nThis `@endo/env-options` package follows the Node precedent for\nfinding Unix environment variable settings: looking for a\nglobal `process` object holding an `env` object,\noptionally holding a property with the same name as the option,\nwhose value is the configuration setting of that option.\n\n```js\nimport { getEnvironmentOption } from '@endo/env-options';\nconst FooBarOption = getEnvironmentOption('FOO_BAR', 'absent');\n```\n\nThe first argument to `getEnvironmentOption` is the name of the option.\nThe value of `FooBarOption` would then be the value of\n`globalThis.process.env.FOO_BAR`, if present.\nIf value is either absent or `undefined`, the second argument,\nsuch as `'absent'`, would be used instead.\n\nIn either case, reflecting Unix environment variable expectations,\nthe resulting setting must be a string.\nThis restriction also helps ensure that this channel is used only to pass data,\nnot authority beyond the ability to read this global state.\n\n```js\nconst ENABLED =\n  getEnvironmentOption('TRACK_TURNS', 'disabled', ['enabled']) === 'enabled';\n```\n\n`getEnvironmentOption` also takes an optional third argument, which if present\nis an exhaustive list of allowed strings other than the default. If present\nand the actual environment option is neither the default nor one of these\nallowed strings, then an error is thrown explaining the problem.\n\n```js\nconst DEBUG_VALUES = getEnvironmentOptionsList('DEBUG');\nconst DEBUG_AGORIC = environmentOptionsListHas('DEBUG', 'agoric');\n```\n\nAnother common convention is for the value of an option to be a\ncomma (`','`) separated list of strings. `getEnvironmentOptionsList` will\nreturn this list, or an empty list if the option is absent.\n`environmentOptionsListHas` will test if this list contains a specific\nvalue, or return false if the option is absent.\n\n(Compat note: https://github.com/Agoric/agoric-sdk/issues/8096 explains that\nfor `DEBUG` specifically, some existing uses split on colon (`':'`) rather\nthan comma. Once these are fixed, then these uses can be switched to use\n`getEnvironmentOptionsList` or `environmentOptionsListHas`.)\n\n## Tracking used option names\n\nThe `'@endo/env-options'` module also exports a lower-level\n`makeEnvironmentCaptor` that you can apply to whatever object you wish to treat\nas a global(having a \"process\" property with its own \"env\" record),\nsuch as the global of another compartment. It returns an entagled\npair of a `getEnvironmentOption` function as above, and a\n`getCapturedEnvironmentOptionNames` function that returns an array of\nthe option names used by that `getEnvironmentOption` function. This is\nuseful to give feedback about\nwhich environment variables were actually read, for diagnostic purposes.\nFor example, the\nses-shim `lockdown` once contained code such as the following, to explain which\nenvironment variables were read to provide `lockdown` settings.\n\n```js\nimport { makeEnvironmentCaptor } from '@endo/env-options';\nconst {\n  getEnvironmentOption,\n  getEnvironmentOptionsList,\n  environmentOptionsListHas,\n  getCapturedEnvironmentOptionNames,\n} = makeEnvironmentCaptor(globalThis);\n...\nconst capturedEnvironmentOptionNames = getCapturedEnvironmentOptionNames();\nif (capturedEnvironmentOptionNames.length > 0) {\n  console.warn(\n    `SES Lockdown using options from environment variables ${enJoin(\n      arrayMap(capturedEnvironmentOptionNames, q),\n      'and',\n    )}`,\n  );\n}\n```\n\n# Note of test migration\n\nTo reduce cyclic dependencies, the tests of this module have been moved to\n@endo/ses-ava. Doing `yarn test` here currently does nothing.\n","publish_time":1705538018045,"_cnpm_publish_time":1705538018045,"dist-tags":{"latest":"1.1.11"}}