"use strict";var be=Object.create;var W=Object.defineProperty;var xe=Object.getOwnPropertyDescriptor;var He=Object.getOwnPropertyNames;var ke=Object.getPrototypeOf,Se=Object.prototype.hasOwnProperty;var Ie=(n,r)=>{for(var e in r)W(n,e,{get:r[e],enumerable:!0})},se=(n,r,e,t)=>{if(r&&typeof r=="object"||typeof r=="function")for(let s of He(r))!Se.call(n,s)&&s!==e&&W(n,s,{get:()=>r[s],enumerable:!(t=xe(r,s))||t.enumerable});return n};var q=(n,r,e)=>(e=n!=null?be(ke(n)):{},se(r||!n||!n.__esModule?W(e,"default",{value:n,enumerable:!0}):e,n)),Te=n=>se(W({},"__esModule",{value:!0}),n);var $e={};Ie($e,{AsyncIterReader:()=>R,BaseAsyncIterReader:()=>p,BaseSerializerBuffer:()=>P,CDXAndRecordIndexer:()=>ee,CDXIndexer:()=>B,CRLF:()=>D,CRLFCRLF:()=>J,DEFAULT_CDX_FIELDS:()=>me,DEFAULT_FIELDS:()=>Re,DEFAULT_LEGACY_CDX_FIELDS:()=>Ae,HeadersMultiMap:()=>f,Indexer:()=>E,LimitReader:()=>m,NoConcatInflator:()=>x,SerializerInMemBuffer:()=>U,StatusAndHeaders:()=>b,StatusAndHeadersParser:()=>k,UTFToLatin1:()=>V,WARCParser:()=>I,WARCRecord:()=>S,WARCSerializer:()=>Z,WARC_1_0:()=>X,WARC_1_1:()=>pe,appendRequestQuery:()=>ne,binaryToString:()=>M,concatChunks:()=>_,getSurt:()=>N,indexOfDoubleCRLF:()=>ce,isRequestHeader:()=>Ce,jsonToQueryParams:()=>ie,jsonToQueryString:()=>z,latin1ToUTF:()=>G,mfdToQueryParams:()=>ae,mfdToQueryString:()=>oe,multiValueHeader:()=>Le,postToGetUrl:()=>$,readToDoubleCRLF:()=>ue,rxEscape:()=>Ue,splitChunk:()=>w});module.exports=Te($e);var le=q(require("pako"),1);function M(n){let r;typeof n=="string"?r=n:n?.length?r=n.reduce((e,t)=>(e+=String.fromCharCode(t),e),""):n?r=n.toString():r="";try{return"__wb_post_data="+btoa(r)}catch{return"__wb_post_data="}}function Ue(n){return n.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function N(n){try{if(!n.startsWith("https:")&&!n.startsWith("http:"))return n;n=n.replace(/^(https?:\/\/)www\d*\./,"$1");let r=n.toLowerCase(),e=new URL(r),s=e.hostname.split(".").reverse().join(",");if(e.port&&(s+=":"+e.port),s+=")",s+=e.pathname,e.search){let i=e.search.slice(1).split("&");i.sort(),s+="?"+i.join("&")}return s}catch{return n}}function $(n){let{method:r,headers:e,postData:t=""}=n;if(r==="GET")return!1;let i=(d=>{let c=d.get("content-type");if(c)return c;if(!(d instanceof Headers)){for(let[u,A]of d.entries())if(u&&u.toLowerCase()==="content-type")return A}return""})(e),a=i.split(";")[0];function o(d){return d instanceof Uint8Array&&(d=new TextDecoder().decode(d)),d}let l="";switch(a){case"application/x-www-form-urlencoded":l=o(t);break;case"application/json":l=z(o(t));break;case"text/plain":try{l=z(o(t),!1)}catch{l=M(t)}break;case"multipart/form-data":{if(!i)throw new Error("utils cannot call postToGetURL when missing content-type header");l=oe(o(t),i);break}default:l=M(t)}return l!=null?(n.url=ne(n.url,decodeURI(l),n.method),n.method="GET",n.requestBody=l,!0):!1}function ne(n,r,e){if(!e)return n;let t=n.indexOf("?")>0?"&":"?";return`${n}${t}__wb_method=${e}&${r}`}function ie(n,r=!0){if(typeof n=="string")try{n=JSON.parse(n)}catch{n={}}let e=new URLSearchParams,t={},s=a=>e.has(a)?(a in t||(t[a]=1),a+"."+ ++t[a]+"_"):a,i=(a,o="")=>{let l="";if(typeof a=="object"&&!(a instanceof Array))try{for(let[d,c]of Object.entries(a))i(c,d)}catch{a===null&&(l="null")}else if(a instanceof Array)for(let d=0;de+=String.fromCharCode(t)),e}function G(n){let r=new Uint8Array(n.length);for(let e=0;e{t.done||!t.value?e.close():e.enqueue(t.value)})}})}async readFully(){return await n.readFully(this)}async readline(r=0){let e=await this.readlineRaw(r);return e?de.decode(e):""}async*iterLines(r=0){let e=null;for(;e=await this.readline(r);)yield e}};function We(n){return!!(n&&Symbol.iterator in Object(n))}function ve(n){return!!(n&&Symbol.asyncIterator in Object(n))}var R=class n extends p{constructor(e,t="gzip",s=!1){super();this.compressed=t,this.opts={raw:t==="deflateRaw"},this.inflator=t?new x(this.opts,this):null;let i;if(ve(e))i=e;else if(typeof e=="object"&&"read"in e&&typeof e.read=="function")i=n.fromReadable(e);else if(e instanceof ReadableStream)i=n.fromReadable(e.getReader());else if(We(e))i=n.fromIter(e);else throw new TypeError("Invalid Stream Source");s?this._sourceIter=this.dechunk(i):this._sourceIter=i[Symbol.asyncIterator](),this.lastValue=null,this.errored=!1,this._savedChunk=null,this._rawOffset=0,this._readOffset=0,this.numChunks=0}async _loadNext(){let e=await this._sourceIter.next();return e.done?null:e.value}async*dechunk(e){let t=e instanceof n?e:new n(e,null),s=-1,i=!0;for(;s!=0;){let a=await t.readlineRaw(64),o=new Uint8Array;if(s=a?parseInt(de.decode(a),16):0,!s||s>2**32){if(Number.isNaN(s)||s>2**32){i||(this.errored=!0),yield a;break}}else if(o=await t.readSize(s),o.length!=s){i?yield a:this.errored=!0,yield o;break}let l=await t.readSize(2);if(l[0]!=13||l[1]!=10){i?yield a:this.errored=!0,yield o,yield l;break}else{if(i=!1,!o||s===0)return;yield o}}yield*t}unread(e){e.length&&(this._readOffset-=e.length,this._savedChunk&&console.log("Already have chunk!"),this._savedChunk=e)}async _next(){if(this._savedChunk){let t=this._savedChunk;return this._savedChunk=null,t}if(this.compressed){let t=this._getNextChunk();if(t)return t}let e=await this._loadNext();for(;this.compressed&&e;){this._push(e);let t=this._getNextChunk(e);if(t)return t;e=await this._loadNext()}return e}_push(e){if(!this.inflator)throw new Error("AsyncIterReader cannot call _push when this.compressed is null");this.lastValue=e,this.inflator.ended&&(this.inflator=new x(this.opts,this)),this.inflator.push(e),this.inflator.err&&this.inflator.ended&&this.compressed==="deflate"&&!this.opts.raw&&this.numChunks===0&&(this.opts.raw=!0,this.compressed="deflateRaw",this.inflator=new x(this.opts,this),this.inflator.push(e))}_getNextChunk(e){if(!this.inflator)throw new Error("AsyncIterReader cannot call _getNextChunk when this.compressed is null");for(;;){if(this.inflator.chunks.length>0)return this.numChunks++,this.inflator.chunks.shift();if(this.inflator.ended){if(this.inflator.err!==0)return this.compressed=null,e;let t=this.inflator.strm.avail_in;if(t&&this.lastValue){this._push(this.lastValue.slice(-t));continue}}return null}}async*[Symbol.asyncIterator](){let e=null;for(;e=await this._next();)this._readOffset+=e.length,yield e}async readlineRaw(e){let t=[],s=0,i=-1,a=null;for await(let o of this){if(e&&s+o.byteLength>e){a=o,i=e-s-1;let l=o.slice(0,i+1).indexOf(10);l>=0&&(i=l);break}if(i=o.indexOf(10),i>=0){a=o;break}t.push(o),s+=o.byteLength}if(a){let[o,l]=w(a,i+1);t.push(o),s+=o.byteLength,this.unread(l)}else if(!t.length)return null;return _(t,s)}async readFully(){return(await this._readOrSkip())[1]}async readSize(e){return(await this._readOrSkip(e))[1]}async skipSize(e){return(await this._readOrSkip(e,!0))[0]}async _readOrSkip(e=-1,t=!1){let s=[],i=0;for await(let a of this){if(e>=0)if(a.length>e){let[o,l]=w(a,e);t||s.push(o),i+=o.byteLength,this.unread(l);break}else if(a.length===e){t||s.push(a),i+=a.byteLength,e=0;break}else e-=a.length;t||s.push(a),i+=a.byteLength}return t?[i,new Uint8Array]:[i,_(s,i)]}getReadOffset(){return this._readOffset}getRawOffset(){return this.compressed?this._rawOffset:this._readOffset}getRawLength(e){return this.compressed?this.inflator.strm.total_in:this._readOffset-e}static fromReadable(e){return{async*[Symbol.asyncIterator](){let s=null;for(;(s=await e.read())&&!s.done;)yield s.value}}}static fromIter(e){return{async*[Symbol.asyncIterator](){for(let s of e)yield s}}}},m=class extends p{constructor(e,t,s=0){super();this.sourceIter=e,this.length=t,this.limit=t,this.skip=s}setLimitSkip(e,t=0){this.limit=e,this.skip=t}async*[Symbol.asyncIterator](){if(!(this.limit<=0))for await(let e of this.sourceIter){if(this.skip>0)if(e.length>=this.skip){let[,t]=w(e,this.skip);e=t,this.skip=0}else{this.skip-=e.length;continue}if(e.length>this.limit){let[t,s]=w(e,this.limit);e=t,this.sourceIter.unread&&this.sourceIter.unread(s)}if(e.length&&(this.limit-=e.length,yield e),this.limit<=0)break}}async readlineRaw(e){if(this.limit<=0)return null;let t=await this.sourceIter.readlineRaw(e?Math.min(e,this.limit):this.limit);return this.limit-=t?.length||0,t}async skipFully(){let e=this.limit;for(;this.limit>0;)this.limit-=await this.sourceIter.skipSize(this.limit);return e}};var D=new Uint8Array([13,10]),J=new Uint8Array([13,10,13,10]),Fe=new TextDecoder("utf-8"),b=class{constructor({statusline:r,headers:e,reencodeHeaders:t}){this.statusline=r,this.headers=e,this.reencodeHeaders=t}toString(){let r=[this.statusline],e=this.headers instanceof Headers;for(let[t,s]of this.headers)e&&this.reencodeHeaders?.has(t)?r.push(`${t}: ${G(s)}`):r.push(`${t}: ${s}`);return r.join(`\r `)+`\r `}async*iterSerialize(r){yield r.encode(this.statusline),yield D;for(let[e,t]of this.headers)yield r.encode(`${e}: ${t}\r `)}_parseResponseStatusLine(){let r=De(this.statusline," ",2);this._protocol=r[0]??"",this._statusCode=r.length>1?Number(r[1]):"",this._statusText=r.length>2?r[2]:""}get statusCode(){return this._statusCode===void 0&&this._parseResponseStatusLine(),this._statusCode}get protocol(){return this._protocol===void 0&&this._parseResponseStatusLine(),this._protocol}get statusText(){return this._statusText===void 0&&this._parseResponseStatusLine(),this._statusText}_parseRequestStatusLine(){let r=this.statusline.split(" ",2);this._method=r[0]??"",this._requestPath=r.length>1?r[1]:""}get method(){return this._method===void 0&&this._parseRequestStatusLine(),this._method}get requestPath(){return this._requestPath===void 0&&this._parseRequestStatusLine(),this._requestPath}},k=class{constructor(){this.reencodeHeaders=new Set}async parse(r,{headersClass:e,firstLine:t}={headersClass:f}){let s=t||await r.readline();if(!s)return null;let i=s.trimEnd();if(!i)return null;let a=new e,o=await ue(r),l=0,d,c,u,A="",h;for(;l=0&&d0&&s.push(t.slice(e).join(r)),s}async function ce(n,r){let e=0;for(let t=0;t=n.length){let{value:i}=await r.next();if(!i)break;let a=new Uint8Array(i.length+n.length);a.set(n,0),a.set(i,n.length),n=a}if(n[s+1]===10&&n[s+2]===13&&n[s+3]===10)return[s+3,n];e=s+1}return[-1,n]}async function ue(n){let r=[],e=0,t=0,s=null,i=n[Symbol.asyncIterator]();for await(let a of i){if([t,a]=await ce(a,i),t>=0){s=a;break}r.push(a),e+=a.byteLength}if(s){let[a,o]=w(s,t+1);r.push(a),e+=a.byteLength,n.unread(o)}else if(!r.length)return"";return Fe.decode(_(r,e))}var fe=q(require("uuid-random"),1);var Pe=new TextDecoder("utf-8"),Ee=new TextEncoder,pe="WARC/1.1",X="WARC/1.0",Be="http://netpreserve.org/warc/1.0/revisit/identical-payload-digest",Oe="http://netpreserve.org/warc/1.1/revisit/identical-payload-digest",qe={warcinfo:"application/warc-fields",response:"application/http; msgtype=response",revisit:"application/http; msgtype=response",request:"application/http; msgtype=request",metadata:"application/warc-fields"},S=class n extends p{constructor({warcHeaders:e,reader:t}){super();this._offset=0;this._length=0;this._urlkey="";this.warcHeaders=e,this._reader=t,this._contentReader=null,this.payload=null,this.httpHeaders=null,this.consumed="",this.fixUp()}static create({url:e,date:t,type:s,warcHeaders:i={},filename:a="",httpHeaders:o={},statusline:l="HTTP/1.1 200 OK",warcVersion:d=X,keepHeadersCase:c=!0,refersToUrl:u=void 0,refersToDate:A=void 0}={},h){function te(C){let _e=C;return d===X&&(C=C.split(".")[0],C.charAt(_e.length-1)!="Z"&&(C+="Z")),C}t=te(t||new Date().toISOString());let y=new f(i);if(s==="warcinfo")a&&y.set("WARC-Filename",a);else if(e)try{y.set("WARC-Target-URI",new URL(e).href)}catch{y.set("WARC-Target-URI",e)}y.set("WARC-Date",t),s&&y.set("WARC-Type",s),s==="revisit"&&(y.set("WARC-Profile",d===pe?Oe:Be),u&&(y.set("WARC-Refers-To-Target-URI",u),y.set("WARC-Refers-To-Date",te(A||new Date().toISOString()))));let H=new b({statusline:d,headers:y});H.headers.get("WARC-Record-ID")||H.headers.set("WARC-Record-ID",``),H.headers.get("Content-Type")||H.headers.set("Content-Type",s&&qe[s]||"application/octet-stream"),h||(h=he());let re=new n({warcHeaders:H,reader:h}),L=null,O=!1;switch(s){case"response":case"request":case"revisit":c?(L=new f(o),O=!L.size):(L=new Headers(o),O=!Object.entries(o).length),(!O||s!=="revisit")&&(re.httpHeaders=new b({statusline:l,headers:L}));break}return re}static createWARCInfo(e={},t){async function*s(){for(let[i,a]of Object.entries(t))yield Ee.encode(`${i}: ${a}\r `)}return e.type="warcinfo",n.create(e,s())}getResponseInfo(){let e=this.httpHeaders;return e?{headers:e.headers,status:e.statusCode,statusText:e.statusText}:null}fixUp(){let e=this.warcHeaders.headers.get("WARC-Target-URI");e&&e.startsWith("<")&&e.endsWith(">")&&this.warcHeaders.headers.set("WARC-Target-URI",e.slice(1,-1))}async readFully(e=!1){if(this.httpHeaders){if(this.payload&&!this.payload.length)return this.payload;if(this._contentReader&&!e)throw new TypeError("WARC Record decoding already started, but requesting raw payload");if(e&&this.consumed==="raw"&&this.payload)return await this._createDecodingReader([this.payload]).readFully()}return this.payload?this.payload:(e?(this.payload=await super.readFully(),this.consumed="content"):(this.payload=await p.readFully(this._reader),this.consumed="raw"),this.payload)}get reader(){if(this.payload&&!this.payload.length)return he();if(this._contentReader)throw new TypeError("WARC Record decoding already started, but requesting raw payload");return this._reader}get contentReader(){return this.httpHeaders?(this._contentReader||(this._contentReader=this._createDecodingReader(this._reader)),this._contentReader):this._reader}_createDecodingReader(e){if(!this.httpHeaders)throw new Error("WARCRecord cannot call _createDecodingReader when this.httpHeaders === null");let t=this.httpHeaders.headers.get("Content-Encoding"),s=this.httpHeaders.headers.get("Transfer-Encoding"),i=s==="chunked";return!t&&!i&&(t=s),new R(e,t,i)}async readlineRaw(e){if(this.consumed)throw new Error("Record already consumed.. Perhaps a promise was not awaited?");if(this.contentReader instanceof p)return this.contentReader.readlineRaw(e);throw new Error("WARCRecord cannot call readlineRaw on this.contentReader if it does not extend BaseAsyncIterReader")}async contentText(){let e=await this.readFully(!0);return Pe.decode(e)}async*[Symbol.asyncIterator](){for await(let e of this.contentReader)if(yield e,this.consumed)throw new Error("Record already consumed.. Perhaps a promise was not awaited?");this.consumed="content"}async skipFully(){if(!this.consumed){if(this._reader instanceof m){let e=await this._reader.skipFully();return this.consumed="skipped",e}throw new Error("WARCRecord cannot call skipFully on this._reader if it is not a LimitReader")}}warcHeader(e){return this.warcHeaders.headers.get(e)}get warcType(){return this.warcHeaders.headers.get("WARC-Type")}get warcTargetURI(){return this.warcHeaders.headers.get("WARC-Target-URI")}get warcDate(){return this.warcHeaders.headers.get("WARC-Date")}get warcRefersToTargetURI(){return this.warcHeaders.headers.get("WARC-Refers-To-Target-URI")}get warcRefersToDate(){return this.warcHeaders.headers.get("WARC-Refers-To-Date")}get warcPayloadDigest(){return this.warcHeaders.headers.get("WARC-Payload-Digest")}get warcBlockDigest(){return this.warcHeaders.headers.get("WARC-Block-Digest")}get warcContentType(){return this.warcHeaders.headers.get("Content-Type")}get warcContentLength(){return Number(this.warcHeaders.headers.get("Content-Length"))}get warcConcurrentTo(){if(this.warcHeaders.headers instanceof f)return this.warcHeaders.headers.getMultiple("WARC-Concurrent-To");{let e=this.warcHeaders.headers.get("WARC-Concurrent-To");return e?e.split(","):[]}}};async function*he(){}var ye=new TextDecoder,Q=new Uint8Array([]),I=class n{static async parse(r,e){return new n(r,e).parse()}static iterRecords(r,e){return new n(r,e)[Symbol.asyncIterator]()}constructor(r,{keepHeadersCase:e=!1,parseHttp:t=!0}={}){this._offset=0,this._warcHeadersLength=0,this._headersClass=e?f:Headers,this._parseHttp=t,r instanceof R?this._reader=r:this._reader=new R(r),this._record=null}async readToNextRecord(){if(!this._reader||!this._record)return Q;await this._record.skipFully(),this._reader.compressed&&(this._offset=this._reader.getRawOffset());let r=await this._reader.readlineRaw(),e=0;if(!r)r=Q;else{if(e=r.byteLength-1,e===9&&ye.decode(r).startsWith("WARC/"))return r;for(;e>0;){let t=r[e-1];if(t!==10&&t!==13)break;e--}e&&console.warn(`Content-Length Too Small: Record not followed by newline, Remainder Length: ${e}, Offset: ${this._reader.getRawOffset()-r.byteLength}`)}if(this._reader.compressed)await this._reader.skipSize(2),r=Q;else{for(r=await this._reader.readlineRaw();r&&r.byteLength===2;)r=await this._reader.readlineRaw();this._offset=this._reader.getRawOffset(),r&&(this._offset-=r.length)}return r}_initRecordReader(r){return new m(this._reader,Number(r.headers.get("Content-Length")||0))}async parse(){let r=await this.readToNextRecord(),e=r?ye.decode(r):"",t=new k,s=await t.parse(this._reader,{firstLine:e,headersClass:this._headersClass});if(!s)return null;this._warcHeadersLength=this._reader.getReadOffset();let i=new S({warcHeaders:s,reader:this._initRecordReader(s)});if(this._record=i,this._parseHttp)switch(i.warcType){case"response":case"request":await this._addHttpHeaders(i,t);break;case"revisit":i.warcContentLength>0&&await this._addHttpHeaders(i,t);break}return i}get offset(){return this._offset}get recordLength(){return this._reader.getRawLength(this._offset)}async*[Symbol.asyncIterator](){let r=null;for(;(r=await this.parse())!==null;)yield r;this._record=null}async _addHttpHeaders(r,e){let t=await e.parse(this._reader,{headersClass:this._headersClass});r.httpHeaders=t;let s=this._reader.getReadOffset()-this._warcHeadersLength;r.reader instanceof m&&r.reader.setLimitSkip(r.warcContentLength-s)}};function K(n){if(n instanceof Int8Array||n instanceof Uint8Array||n instanceof Uint8ClampedArray)return new DataView(n.buffer,n.byteOffset,n.byteLength);if(n instanceof ArrayBuffer)return new DataView(n);throw new TypeError("Expected `data` to be an ArrayBuffer, Buffer, Int8Array, Uint8Array or Uint8ClampedArray")}var Me="ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",ze="0123456789ABCDEFGHIJKLMNOPQRSTUV",Ne="0123456789ABCDEFGHJKMNPQRSTVWXYZ";function Y(n,r,e){e=e||{};let t,s;switch(r){case"RFC3548":case"RFC4648":t=Me,s=!0;break;case"RFC4648-HEX":t=ze,s=!0;break;case"Crockford":t=Ne,s=!1;break;default:throw new Error("Unknown base32 variant: "+r)}let i=e.padding!==void 0?e.padding:s,a=K(n),o=0,l=0,d="";for(let c=0;c=5;)d+=t[l>>>o-5&31],o-=5;if(o>0&&(d+=t[l<<5-o&31]),i)for(;d.length%8!==0;)d+="=";return d}var we=q(require("pako"),1),T=require("hash-wasm");var ge=new TextEncoder,P=class{},U=class extends P{constructor(){super(...arguments);this.buffers=[]}write(e){this.buffers.push(e)}async*readAll(){for(let e of this.buffers)yield e}},Z=class n extends p{constructor(e,t={},s=new U){super();this.gzip=!1;this.digestAlgo="";this.digestAlgoPrefix="";this.digestBase32=!1;this.preferPako=!1;this._alreadyDigested=!1;this.blockHasher=null;this.payloadHasher=null;this.httpHeadersBuff=null;this.warcHeadersBuff=null;this.gzip=!!t.gzip,this.record=e;let i=t.digest||{};this.digestAlgo=i.algo||"sha-256",this.digestAlgoPrefix=i.prefix||"sha256:",this.digestBase32=!!i.base32,this.preferPako=!!t.preferPako,n.noComputeDigest(e)&&(this.digestAlgo=""),this.externalBuffer=s}static async serialize(e,t,s=new U){return await new n(e,t,s).readFully()}static noComputeDigest(e){return e.warcType==="revisit"||e.warcType==="warcinfo"||e.warcPayloadDigest&&e.warcBlockDigest}async*[Symbol.asyncIterator](){if(!this.gzip){yield*this.generateRecord();return}if("CompressionStream"in globalThis&&!this.preferPako){let e=new globalThis.CompressionStream("gzip");yield*this.streamCompress(e)}else yield*this.pakoCompress()}async readlineRaw(e){return null}async*pakoCompress(){let e=new we.default.Deflate({gzip:!0}),t=null;for await(let s of this.generateRecord())for(t&&t.length>0&&e.push(t),t=s;e.chunks.length;)yield e.chunks.shift();t&&e.push(t,!0),yield e.result}async*streamCompress(e){let t=this.generateRecord();new ReadableStream({async pull(o){let l=await t.next();l.done?o.close():o.enqueue(l.value)}}).pipeThrough(e);let i=null,a=e.readable.getReader();for(;(i=await a.read())&&!i.done;)yield i.value}async newHasher(){switch(this.digestAlgo){case"sha-256":return(0,T.createSHA256)();case"sha-1":return(0,T.createSHA1)();case"":return null;default:return(0,T.createSHA256)()}}getDigest(e){return this.digestAlgoPrefix+(this.digestBase32?Y(e.digest("binary"),"RFC4648"):e.digest("hex"))}async digestRecord(){let e=this.record;if(this._alreadyDigested)return Number(e.warcHeaders.headers.get("Content-Length"));let t=await this.newHasher(),s=await this.newHasher(),i=0;e.httpHeaders&&(this.httpHeadersBuff=ge.encode(e.httpHeaders.toString()+`\r `),i+=this.httpHeadersBuff.length,t?.update(this.httpHeadersBuff));for await(let a of e.reader)t?.update(a),s?.update(a),this.externalBuffer.write(a),i+=a.length;return s&&e.warcHeaders.headers.set("WARC-Payload-Digest",this.getDigest(s)),t&&e.warcHeaders.headers.set("WARC-Block-Digest",this.getDigest(t)),e.warcHeaders.headers.set("Content-Length",i.toString()),this.warcHeadersBuff=ge.encode(e.warcHeaders.toString()),this._alreadyDigested=!0,i}async*generateRecord(){if(await this.digestRecord(),this.warcHeadersBuff&&(yield this.warcHeadersBuff),yield D,this.httpHeadersBuff&&(yield this.httpHeadersBuff),this.externalBuffer)for await(let e of this.externalBuffer.readAll())yield e;yield J}};var Re=["offset","warc-type","warc-target-uri"],j=class{constructor(r={},e=Re){this.opts=r,r.fields?(this.fields=r.fields,this.reqFields=this.fields.filter(t=>Ce(t))):(this.fields=e,this.reqFields=[]),this.parseHttp=!1}serialize(r){return JSON.stringify(r)+` `}write(r,e){e.write(this.serialize(r))}async writeAll(r,e){for await(let t of this.iterIndex(r))this.write(t,e)}async*iterIndex(r){let e={strictHeaders:!0,parseHttp:this.parseHttp};for(let{filename:t,reader:s}of r){let i=new I(s,e);yield*this.iterRecords(i,t)}}async*iterRecords(r,e){for await(let t of r){await t.skipFully();let s=this.indexRecord(t,r,e);s&&(yield s)}}indexRecord(r,e,t){if(this.filterRecord&&!this.filterRecord(r))return null;let s={},{offset:i,recordLength:a}=e,o={offset:i,length:a,filename:t};for(let l of this.fields)l in o?s[l]=o[l]:this.setField(l,r,s);return s}setField(r,e,t){let s=this.getField(r,e);s!==null&&(t[r]=s)}getField(r,e){if(r.startsWith("req."))if(e.warcType==="request")r=r.slice(4);else return null;if(r==="http:status")return e.httpHeaders&&(e.warcType==="response"||e.warcType==="revisit")?e.httpHeaders.statusCode:null;if(r.startsWith("http:")){if(e.httpHeaders){let t=e.httpHeaders.headers,s=r.slice(5),i=t.get(s);if(t instanceof Map){let a=s.toLowerCase();for(let o of t.keys())a===o.toLowerCase()&&(i=t.get(o))}return i}return null}return e.warcHeaders.headers.get(r)||null}},E=class extends j{constructor(r,e){super(r,e);for(let t of this.fields)if(t.startsWith("http:")){this.parseHttp=!0;break}}},me="urlkey,timestamp,url,mime,status,digest,length,offset,filename".split(","),Ae="urlkey,timestamp,url,mime,status,digest,redirect,meta,length,offset,filename".split(","),B=class extends E{constructor(e){super(e,me);switch(this.includeAll=!!e?.all,this.overrideIndexForAll=!!e?.all,this.parseHttp=!0,this.noSurt=!!e?.noSurt,this._lastRecord=null,e?.format){case"cdxj":this.serialize=this.serializeCDXJ;break;case"cdx":this.serialize=this.serializeCDX11;break;case"json":default:break}}async*iterRecords(e,t){this._lastRecord=null;for await(let i of e){await i.readFully();let a=this.indexRecord(i,e,t);a&&(yield a)}let s=this.indexRecord(null,e,t);s&&(yield s)}filterRecord(e){if(this.includeAll)return!0;let t=e.warcType;return!(t==="request"||t==="warcinfo"||(t==="metadata"||t==="resource")&&e.warcContentType==="application/warc-fields")}indexRecord(e,t,s){if(this.overrideIndexForAll)return e?super.indexRecord(e,t,s):null;let i=this._lastRecord;if(this._lastRecord=e,e&&(e._offset=t.offset,e._length=t.recordLength),!i)return null;if(!e||i.warcTargetURI!=e.warcTargetURI)return this.indexRecordPair(i,null,t,s);let a=e.warcType,o=i.warcType;return a==="request"&&(o==="response"||o==="revisit")?(this._lastRecord=null,this.indexRecordPair(i,e,t,s)):(a==="response"||a==="revisit")&&o==="request"?(this._lastRecord=null,this.indexRecordPair(e,i,t,s)):this.indexRecordPair(i,null,t,s)}indexRecordPair(e,t,s,i){let a,o,l=e.warcTargetURI||"";if(t?.httpHeaders&&t.httpHeaders.method!=="GET"){let c={url:l,method:t.httpHeaders.method,headers:t.httpHeaders.headers,postData:t.payload};a=c.method,$(c)&&(o=c.requestBody,l=c.url)}e._urlkey=l;let d=super.indexRecord(e,s,i);if(d&&(e._offset!==void 0&&(d.offset=e._offset,d.length=e._length),a&&(d.method=a),o&&(d.requestBody=o),t&&this.reqFields.length))for(let c of this.reqFields)this.setField(c,t,d);return d}serializeCDXJ(e){let{urlkey:t,timestamp:s}=e;return delete e.urlkey,delete e.timestamp,`${t} ${s} ${JSON.stringify(e,(a,o)=>["offset","length","status"].includes(a)?o==null?"":""+o:o)} `}serializeCDX11(e){let t=[];for(let s of Ae)t.push(e[s]!=null?e[s]:"-");return t.join(" ")+` `}getField(e,t){let s=null;switch(e){case"urlkey":return s=t._urlkey||t.warcTargetURI||null,this.noSurt||s===null?s:N(s);case"timestamp":return s=t.warcDate??"",s.replace(/[-:T]/g,"").slice(0,14);case"url":return t.warcTargetURI;case"mime":switch(t.warcType){case"revisit":return"warc/revisit";case"response":case"request":e="http:content-type";break;default:return t.warcContentType}return s=super.getField(e,t),s?s.toString().split(";",1)[0]?.trim():null;case"status":return super.getField("http:status",t);case"referrer":return super.getField("req.http:referer",t);case"digest":return s=t.warcPayloadDigest,s?s.split(":",2)[1]:null;default:return super.getField(e,t)}}},ee=class extends B{constructor(r){super(r),this.overrideIndexForAll=!1}indexRecordPair(r,e,t,s){let i=super.indexRecordPair(r,e,t,s);return i&&{cdx:i,record:r,reqRecord:e}}};function Ce(n){return n.startsWith("req.")||n.toLowerCase()==="referrer"}0&&(module.exports={AsyncIterReader,BaseAsyncIterReader,BaseSerializerBuffer,CDXAndRecordIndexer,CDXIndexer,CRLF,CRLFCRLF,DEFAULT_CDX_FIELDS,DEFAULT_FIELDS,DEFAULT_LEGACY_CDX_FIELDS,HeadersMultiMap,Indexer,LimitReader,NoConcatInflator,SerializerInMemBuffer,StatusAndHeaders,StatusAndHeadersParser,UTFToLatin1,WARCParser,WARCRecord,WARCSerializer,WARC_1_0,WARC_1_1,appendRequestQuery,binaryToString,concatChunks,getSurt,indexOfDoubleCRLF,isRequestHeader,jsonToQueryParams,jsonToQueryString,latin1ToUTF,mfdToQueryParams,mfdToQueryString,multiValueHeader,postToGetUrl,readToDoubleCRLF,rxEscape,splitChunk});