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