@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
3 lines (2 loc) • 2.99 kB
JavaScript
import { createRequire as __piCreateRequire } from "node:module"; const require = __piCreateRequire(import.meta.url); { const __piWorkerThreads = require("node:worker_threads"); if (typeof __piWorkerThreads.markAsUncloneable !== "function") { __piWorkerThreads.markAsUncloneable = () => {}; } }
function normalizeHeaders(headers){return headers?headers instanceof Headers?Object.fromEntries(headers.entries()):Array.isArray(headers)?Object.fromEntries(headers):{...headers}:{}}function createFetchWithInit(baseFetch=fetch,baseInit){return baseInit?async(url,init)=>{let mergedInit={...baseInit,...init,headers:init?.headers?{...normalizeHeaders(baseInit.headers),...normalizeHeaders(init.headers)}:baseInit.headers};return baseFetch(url,mergedInit)}:baseFetch}var MAX_REDIRECTS=5,REDIRECT_STATUSES=[301,302,303,307,308];function isWithinOrigin(from,to){return from.protocol===to.protocol&&from.hostname===to.hostname&&from.port===to.port?!0:from.hostname===to.hostname&&from.protocol==="http:"&&from.port===""&&to.protocol==="https:"&&to.port===""}function redirectTarget(response,requestUrl){let location=REDIRECT_STATUSES.includes(response.status)?response.headers.get("location"):null;try{return location?new URL(location,requestUrl):void 0}catch{return}}function followWithinOrigin(baseFetch,url,init,response,followed=0){let target=redirectTarget(response,url),keepsMethod=response.status===307||response.status===308||(init?.method??"GET").toUpperCase()==="GET";if(!target||followed===MAX_REDIRECTS||!keepsMethod)return;let from=new URL(url);if(!((target.username||target.password)&&(target.username!==from.username||target.password!==from.password)||!isWithinOrigin(from,target)))return Promise.resolve(response.body?.cancel()).then(()=>baseFetch(target,{...init,redirect:"manual"})).then(next=>followWithinOrigin(baseFetch,target,init,next,followed+1)??next)}var followingFetches=new WeakSet;function fetchLeavingRedirects(baseFetch){let following=(url,init)=>(baseFetch??fetch)(url,init);return followingFetches.add(following),following}function fetchWithinOrigin(baseFetch=fetch){return followingFetches.has(baseFetch)?baseFetch:(url,init)=>init?.redirect==="error"||init?.redirect==="manual"?baseFetch(url,init):Promise.resolve(baseFetch(url,{...init,redirect:"manual"})).then(response=>followWithinOrigin(baseFetch,url,init,response)??response)}function unfollowedRedirect(response,requestUrl){if(response.type==="opaqueredirect")return"Redirect not followed: this runtime does not expose the redirect target";let from=response.url||requestUrl,target=redirectTarget(response,from);if(target)return target.username=target.password=target.search=target.hash="",target.protocol==="http:"&&new URL(from).protocol==="https:"?(target.protocol="https:",`Redirect to plain http not followed; try ${target.href} instead`):`Redirect to ${target.href} not followed`}export{normalizeHeaders,createFetchWithInit,followWithinOrigin,fetchLeavingRedirects,fetchWithinOrigin,unfollowedRedirect};