UNPKG

eve

Version:

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

1 lines 1.18 kB
import{BlockList,isIP}from"node:net";function createRuntimeIpAllowList(e){let t=new BlockList;for(let n of e){let e=n.trim();if(e.length===0)throw Error(`Expected every network.ipAllowList entry to be non-empty.`);if(e.includes(`*`))throw Error(`Expected "${e}" to use exact IP or CIDR syntax instead of "*".`);let r=e.indexOf(`/`);if(r===-1){let n=normalizeIpAddress(e),r=getIpFamily(n);t.addAddress(n,r);continue}let i=normalizeIpAddress(e.slice(0,r)),a=Number.parseInt(e.slice(r+1),10),o=getIpFamily(i),s=o===`ipv4`?32:128;if(!Number.isInteger(a)||a<0||a>s)throw Error(`Expected "${e}" to use a valid ${o} CIDR prefix length.`);t.addSubnet(i,a,o)}return{blockList:t,entries:[...e]}}function isRuntimeIpAllowed(e,t){let n=normalizeIpAddress(e),r=getIpFamily(n);return t.blockList.check(n,r)}function normalizeIpAddress(e){let t=e.trim().replace(/^\[(.*)\]$/,`$1`),n=t.indexOf(`%`),r=n===-1?t:t.slice(0,n);if(r.startsWith(`::ffff:`)){let e=r.slice(7);if(isIP(e)===4)return e}return r}function getIpFamily(e){switch(isIP(e)){case 4:return`ipv4`;case 6:return`ipv6`;default:throw Error(`Expected "${e}" to be a valid IP address.`)}}export{createRuntimeIpAllowList,isRuntimeIpAllowed};