UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

1 lines 2.15 kB
function normalizeChannelCors(e){if(e===void 0||e===!1)return;if(e===!0)return{};if(typeof e!=`object`||!e||Array.isArray(e))throw Error(`Expected channel cors to be a boolean or a serializable CORS options object.`);let t={};if(e.origin!==void 0&&(t.origin=normalizeOrigin(e.origin)),e.methods!==void 0&&(t.methods=normalizeWildcardOrStringList(e.methods,`methods`)),e.allowHeaders!==void 0&&(t.allowHeaders=normalizeWildcardOrStringList(e.allowHeaders,`allowHeaders`)),e.exposeHeaders!==void 0&&(t.exposeHeaders=normalizeWildcardOrStringList(e.exposeHeaders,`exposeHeaders`)),e.credentials!==void 0){if(typeof e.credentials!=`boolean`)throw Error(`Expected channel cors.credentials to be a boolean.`);t.credentials=e.credentials}return e.maxAge!==void 0&&(t.maxAge=normalizeMaxAge(e.maxAge)),e.preflight!==void 0&&(t.preflight=normalizePreflight(e.preflight)),t}function normalizeOrigin(e){if(e===`*`||e===`null`)return e;if(!Array.isArray(e))throw Error(`Expected channel cors.origin to be "*", "null", or an array of origins.`);return e.map(e=>{if(typeof e!=`string`||e.length===0)throw Error(`Expected channel cors.origin entries to be non-empty strings.`);return e})}function normalizeWildcardOrStringList(e,t){if(e===`*`)return e;if(!Array.isArray(e))throw Error(`Expected channel cors.${t} to be "*" or an array of strings.`);return e.map(e=>{if(typeof e!=`string`||e.length===0)throw Error(`Expected channel cors.${t} entries to be non-empty strings.`);return e})}function normalizeMaxAge(e){if(e===!1)return!1;if(typeof e==`number`){if(!Number.isFinite(e)||e<0)throw Error(`Expected channel cors.maxAge to be a non-negative finite number.`);return String(e)}if(typeof e==`string`&&e.length>0)return e;throw Error(`Expected channel cors.maxAge to be false, a string, or a number.`)}function normalizePreflight(e){if(typeof e!=`object`||!e||Array.isArray(e))throw Error(`Expected channel cors.preflight to be an object.`);let t=e.statusCode;if(t===void 0)return{};if(!Number.isInteger(t)||t<100||t>599)throw Error(`Expected channel cors.preflight.statusCode to be an HTTP status code.`);return{statusCode:t}}export{normalizeChannelCors};