/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/@plant/http-adapter@1.4.0/src/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
const{and:and}=require("@plant/flow"),{URL:URL}=require("url"),isObject=require("lodash.isobject"),streams=require("./deps/web-streams-polyfill"),{ReadableStream:WebReadableStream}=streams,TypedArray=Object.getPrototypeOf(Uint8Array),INTERNAL_SERVER_ERROR_MSG="Internal server error";function getResolvedNetworkProps(e){const{remoteAddress:t,remotePort:r}=e.connection;let o,n=t,s=r,a=e.connection.encrypted;if(e.headers[":authority"]?o=e.headers[":authority"]:e.headers.host&&(o=e.headers.host),"::ffff:127.0.0.1"===t){const t=e.headers["x-forwarded-for"],r=e.headers["x-forwarded-host"];t&&(n=t,s="0"),r&&(o=r),a="1"===e.headers["x-forwarded-proto"]}return[o,{address:n,port:s},a]}function createRequest(e,{host:t,encrypted:r},{Request:o,Headers:n}){const s=new URL(`${r?"https":"http"}://${t}${e.url}`);return new o({method:e.method.toLowerCase(),url:s,headers:new n(e.headers,n.MODE_IMMUTABLE),body:new WebReadableStream({start(t){e.resume(),e.on("data",(e=>{t.enqueue(e)})),e.on("end",(()=>{t.close()}))},cancel(){e.drain()}})})}async function writeResponseToWritableStream(e,t){const{body:r}=t;try{if(isObject(r))if("function"==typeof r.getReader)await writeWebReadableStreamToWritableStream(e,r);else if(r instanceof Buffer||r instanceof String)e.write(r);else{if(!(r instanceof TypedArray))throw new TypeError("Invalid body type");writeUint8ArrayToNodeStream(e,r)}else{if("string"!=typeof r)throw new TypeError("Invalid body type");e.write(r)}}finally{e.end()}}async function writeWebReadableStreamToWritableStream(e,t){let r,o;function n(e){o=e}try{if(t.locked)throw new Error("Source stream is locked");for(r=t.getReader(),e.on("error",n);;){const{value:t,done:n}=await r.read();if(o)throw o;if(n||e.closed)break;"string"==typeof t||t instanceof Buffer?e.write(t):writeUint8ArrayToNodeStream(e,t)}}finally{void 0!==r&&(r.cancel(),r.releaseLock()),t.cancel(),e.removeListener("error",n)}}function writeUint8ArrayToNodeStream(e,t){const{prototype:r}=t.constructor;Object.setPrototypeOf(t,Buffer.prototype);try{e.write(t)}finally{Object.setPrototypeOf(t,r)}}function createSocket(e,t,r,{Socket:o,Peer:n,URI:s}){let a;a=t&&t.pushAllowed?function(e){return new Promise((function(r,o){t.pushStream({":path":e.url.pathname+e.url.search},(function(t,n){if(t)o(t);else{const t={":status":e.status};e.headers.forEach((function(e,r){t[r]=e})),n.respond(t),writeResponseToWritableStream(n,e).catch((e=>{if("ERR_HTTP2_STREAM_ERROR"!==e.code||!e.message.includes("NGHTTP2_REFUSED_STREAM"))throw e})).then(r,o).finally((()=>n.close()))}}))}))}:null;const d=new o({peer:new n({uri:new s({protocol:"tcp:",hostname:r.address,post:r.port})}),onEnd(){e.ended||e.end()},onPush:a});let c,i;return c=()=>{d.end(),i()},i=()=>{e.removeListener("abort",c),e.removeListener("close",c)},e.on("abort",c),e.on("close",c),d.on("destroy",i),d}function createResponse(e,{url:t},{Response:r,Headers:o}){return new r({url:t,status:e.statusCode,headers:new o(e.headers),body:null})}function handleRequest(e,t,r,o){const[n,s,a]=getResolvedNetworkProps(e),d=createRequest(e,{host:n,encrypted:a},r),c=createResponse(t,{url:d.url},r),i=createSocket(e.socket,e.stream,s,r);return e.socket.setMaxListeners(1/0),o({httpReq:e,httpRes:t,req:d,res:c,socket:i}).then((async()=>{if(!e.ended){null===c.body&&c.setStatus(404).text("Nothing found"),t.statusCode=c.status;for(const e of c.headers.keys())t.setHeader(e,c.headers.raw(e));return writeResponseToWritableStream(t,c)}i.destroy()}))}function createRequestHandler(e,{handlers:t=[],debug:r=!1}={}){const o=and(...t,contextWithout(["httpReq","httpRes"]),e),{Headers:n,Peer:s,Request:a,Response:d,Socket:c,URI:i}=e.constructor;return function(e,t){handleRequest(e,t,{Headers:n,Socket:c,Peer:s,URI:i,Request:a,Response:d},o).catch(handleRequestError.bind(this,e,t,r))}}function handleRequestError(e,t,r,o){if(!t.headersSent){const e=r?`Error: ${o.message}`:"Internal server error";t.statusCode=500,t.setHeader("content-type","text/plain"),t.setHeader("content-length",e.length),t.write(e)}t.end(),this.emit("error",o)}function contextWithout(e){return(t,r)=>r(without(e,t))}function without(e,t){const r={...t};for(const t of e)delete r[t];return r}exports.createRequestHandler=createRequestHandler,exports.INTERNAL_SERVER_ERROR_MSG="Internal server error";
//# sourceMappingURL=/sm/8f9680de1135726ecd5980c7db576e22d42ad5d61ecbdc226d008487daf7cad2.map