@uploadfilm/sdk 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +4 -0
- package/README.md +509 -2
- package/dist/ai/embed.d.ts +58 -0
- package/dist/ai/fake.d.ts +41 -0
- package/dist/ai/http.d.ts +17 -0
- package/dist/ai/index.d.ts +27 -0
- package/dist/ai/languages.d.ts +2 -0
- package/dist/ai/ollama.d.ts +23 -0
- package/dist/ai/openaiCompatible.d.ts +54 -0
- package/dist/ai/passages.d.ts +28 -0
- package/dist/ai/transcribe.d.ts +84 -0
- package/dist/ai/types.d.ts +141 -0
- package/dist/ai/usage.d.ts +66 -0
- package/dist/ai/whisperJson.d.ts +15 -0
- package/dist/ai/workersAi.d.ts +56 -0
- package/dist/ai.js +1 -0
- package/dist/ai.mjs +1 -0
- package/dist/cards/files.d.ts +91 -0
- package/dist/cards/index.d.ts +15 -0
- package/dist/cards/layouts.d.ts +121 -0
- package/dist/cards/probe.d.ts +69 -0
- package/dist/cards/proxies.d.ts +70 -0
- package/dist/cards/select.d.ts +68 -0
- package/dist/cards/volumes.d.ts +170 -0
- package/dist/cards/xml.d.ts +80 -0
- package/dist/cards.js +1 -0
- package/dist/cards.mjs +1 -0
- package/dist/copy/checksums.d.ts +66 -0
- package/dist/copy/engine.d.ts +24 -0
- package/dist/copy/errors.d.ts +109 -0
- package/dist/copy/index.d.ts +15 -0
- package/dist/copy/steps.d.ts +28 -0
- package/dist/copy/sweep.d.ts +24 -0
- package/dist/copy/types.d.ts +336 -0
- package/dist/embed/player.js +2 -0
- package/dist/host/adapters.d.ts +107 -0
- package/dist/host/detect.d.ts +68 -0
- package/dist/host/encoders.d.ts +34 -0
- package/dist/host/environment.d.ts +53 -0
- package/dist/host/index.d.ts +20 -0
- package/dist/host/linux.d.ts +94 -0
- package/dist/host/mac.d.ts +47 -0
- package/dist/host/network.d.ts +54 -0
- package/dist/host/node.d.ts +13 -0
- package/dist/host/policy.d.ts +101 -0
- package/dist/host/tuner.d.ts +48 -0
- package/dist/host/types.d.ts +118 -0
- package/dist/host-node.js +1 -0
- package/dist/host-node.mjs +1 -0
- package/dist/host.js +1 -0
- package/dist/host.mjs +1 -0
- package/dist/images/index.d.ts +11 -0
- package/dist/images/settings.d.ts +43 -0
- package/dist/images/types.d.ts +83 -0
- package/dist/images/url.d.ts +65 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/dist/player/adapters/hlsjs.d.ts +28 -0
- package/dist/player/bandwidthMemory.d.ts +28 -0
- package/dist/player/captions/index.d.ts +55 -0
- package/dist/player/embed/code.d.ts +195 -0
- package/dist/player/embed/constants.d.ts +4 -0
- package/dist/player/embed/controller.d.ts +62 -0
- package/dist/player/embed/element.d.ts +32 -0
- package/dist/player/embed/frame.d.ts +65 -0
- package/dist/player/embed/global.d.ts +14 -0
- package/dist/player/embed/index.d.ts +15 -0
- package/dist/player/embed/loader.d.ts +1 -0
- package/dist/player/embed/oembed.d.ts +74 -0
- package/dist/player/embed/protocol.d.ts +64 -0
- package/dist/player/engine.d.ts +215 -0
- package/dist/player/index.d.ts +18 -0
- package/dist/player/start.d.ts +62 -0
- package/dist/player/store.d.ts +10 -0
- package/dist/player/storyboard/index.d.ts +161 -0
- package/dist/player/stream.d.ts +206 -0
- package/dist/player/tracks.d.ts +41 -0
- package/dist/player.js +1 -0
- package/dist/player.mjs +1 -0
- package/dist/proxies/args.d.ts +19 -0
- package/dist/proxies/budget.d.ts +57 -0
- package/dist/proxies/camera.d.ts +104 -0
- package/dist/proxies/formats.d.ts +36 -0
- package/dist/proxies/index.d.ts +18 -0
- package/dist/proxies/jobs.d.ts +98 -0
- package/dist/proxies/node.d.ts +32 -0
- package/dist/proxies/plan.d.ts +89 -0
- package/dist/proxies/probe.d.ts +39 -0
- package/dist/proxies/render.d.ts +104 -0
- package/dist/proxies/safety.d.ts +45 -0
- package/dist/proxies/types.d.ts +83 -0
- package/dist/proxies/upload.d.ts +14 -0
- package/dist/react/BigPlay.d.ts +26 -0
- package/dist/react/Player.d.ts +58 -0
- package/dist/react/embed/index.d.ts +45 -0
- package/dist/react/examples/CustomControls.d.ts +8 -0
- package/dist/react/images/Image.d.ts +16 -0
- package/dist/react/images/index.d.ts +4 -0
- package/dist/react/images/useImage.d.ts +47 -0
- package/dist/react/index.d.ts +17 -0
- package/dist/react/storyboard/index.d.ts +58 -0
- package/dist/react/theme.d.ts +37 -0
- package/dist/react/useAdapterErrors.d.ts +8 -0
- package/dist/react/usePlaybackStart.d.ts +18 -0
- package/dist/react/usePlayer.d.ts +28 -0
- package/dist/react/useReducedMotion.d.ts +2 -0
- package/dist/react/useTimedTrack.d.ts +22 -0
- package/dist/react.js +1 -0
- package/dist/react.mjs +1 -0
- package/dist/server/auth/apiKeys.d.ts +30 -0
- package/dist/server/auth/cache.d.ts +21 -0
- package/dist/server/auth/deviceTokens.d.ts +38 -0
- package/dist/server/auth/envelope.d.ts +15 -0
- package/dist/server/auth/grants.d.ts +76 -0
- package/dist/server/auth/index.d.ts +19 -0
- package/dist/server/auth/jwt.d.ts +65 -0
- package/dist/server/auth/keyring.d.ts +31 -0
- package/dist/server/auth/kv.d.ts +12 -0
- package/dist/server/auth/repository.d.ts +45 -0
- package/dist/server/auth/revocations.d.ts +21 -0
- package/dist/server/auth/scope.d.ts +4 -0
- package/dist/server/auth/serviceAuth.d.ts +24 -0
- package/dist/server/auth/signedUrls.d.ts +28 -0
- package/dist/server/embed/index.d.ts +36 -0
- package/dist/server/embed/lifecycle.d.ts +53 -0
- package/dist/server/embed/playerScript.d.ts +4 -0
- package/dist/server/images/index.d.ts +15 -0
- package/dist/server/images/job.d.ts +80 -0
- package/dist/server/images/layout.d.ts +24 -0
- package/dist/server/images/params.d.ts +90 -0
- package/dist/server/images/route.d.ts +101 -0
- package/dist/server/images/sign.d.ts +55 -0
- package/dist/server/index.d.ts +42 -0
- package/dist/server/media/client.d.ts +66 -0
- package/dist/server/media/frameCheck.d.ts +62 -0
- package/dist/server/media/index.d.ts +10 -0
- package/dist/server/media/protocol.d.ts +318 -0
- package/dist/server/media/storyboard.d.ts +144 -0
- package/dist/server/media/transcript.d.ts +143 -0
- package/dist/server/pipeline/index.d.ts +12 -0
- package/dist/server/pipeline/match.d.ts +10 -0
- package/dist/server/pipeline/pipeline.d.ts +182 -0
- package/dist/server/pipeline/queues.d.ts +108 -0
- package/dist/server/pipeline/sources.d.ts +54 -0
- package/dist/server/r2/client.d.ts +150 -0
- package/dist/server/r2/crc32.d.ts +24 -0
- package/dist/server/r2/encoding.d.ts +35 -0
- package/dist/server/r2/errors.d.ts +9 -0
- package/dist/server/r2/integrity.d.ts +25 -0
- package/dist/server/r2/parts.d.ts +46 -0
- package/dist/server/r2/sign.d.ts +47 -0
- package/dist/server/redis/index.d.ts +39 -0
- package/dist/server/state/memory.d.ts +18 -0
- package/dist/server/state/r2.d.ts +73 -0
- package/dist/server/state/types.d.ts +57 -0
- package/dist/server/storage/chunked.d.ts +21 -0
- package/dist/server/storage/index.d.ts +12 -0
- package/dist/server/storage/r2.d.ts +96 -0
- package/dist/server/storage/s3.d.ts +93 -0
- package/dist/server/testing/fake-r2.d.ts +390 -0
- package/dist/server/testing/index.d.ts +10 -0
- package/dist/server/testing/kv-contract.d.ts +14 -0
- package/dist/server/transfer/handler.d.ts +167 -0
- package/dist/server/transfer/keys.d.ts +45 -0
- package/dist/server/transfer/protocol.d.ts +141 -0
- package/dist/server/zip/client.d.ts +32 -0
- package/dist/server/zip/generations.d.ts +83 -0
- package/dist/server/zip/index.d.ts +12 -0
- package/dist/server/zip/planner.d.ts +94 -0
- package/dist/server/zip/sign.d.ts +22 -0
- package/dist/server-media.js +1 -0
- package/dist/server-media.mjs +1 -0
- package/dist/server-redis.js +1 -0
- package/dist/server-redis.mjs +1 -0
- package/dist/server-testing.js +1 -0
- package/dist/server-testing.mjs +1 -0
- package/dist/server.js +1 -0
- package/dist/server.mjs +1 -0
- package/dist/storage/browserFile.d.ts +19 -0
- package/dist/storage/index.d.ts +12 -0
- package/dist/storage/local.d.ts +9 -0
- package/dist/storage/presigned.d.ts +22 -0
- package/dist/sync/ffmpeg.d.ts +44 -0
- package/dist/sync/index.d.ts +17 -0
- package/dist/sync/match.d.ts +28 -0
- package/dist/sync/probe.d.ts +30 -0
- package/dist/sync/timecode.d.ts +50 -0
- package/dist/sync/transcript.d.ts +35 -0
- package/dist/sync/types.d.ts +172 -0
- package/dist/sync/waveform.d.ts +89 -0
- package/dist/sync.js +1 -0
- package/dist/sync.mjs +1 -0
- package/dist/transfer/claims.d.ts +41 -0
- package/dist/transfer/client.d.ts +256 -0
- package/dist/transfer/download.d.ts +192 -0
- package/dist/transfer/engine.d.ts +382 -0
- package/dist/transfer/errors.d.ts +50 -0
- package/dist/transfer/index.d.ts +39 -0
- package/dist/transfer/integrity.d.ts +93 -0
- package/dist/transfer/limits.d.ts +34 -0
- package/dist/transfer/node.d.ts +63 -0
- package/dist/transfer/pool.d.ts +83 -0
- package/dist/transfer/protocol.d.ts +167 -0
- package/dist/transfer/signer.d.ts +29 -0
- package/dist/transfer/storage.d.ts +110 -0
- package/dist/transfer/timing.d.ts +132 -0
- package/dist/transfer/transports/electronNet.d.ts +62 -0
- package/dist/transfer/transports/fetch.d.ts +15 -0
- package/dist/transfer/transports/retryAfter.d.ts +12 -0
- package/dist/transfer/transports/undici.d.ts +72 -0
- package/dist/transfer/tuner.d.ts +56 -0
- package/dist/zip/index.d.ts +13 -0
- package/dist/zip/layout.d.ts +129 -0
- package/dist/zip/names.d.ts +34 -0
- package/dist/zip/plan.d.ts +115 -0
- package/dist/zip/sw.d.ts +7 -0
- package/package.json +265 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const t=[{short:360,kbps:700,peak:1e3},{short:720,kbps:2800,peak:4e3},{short:1080,kbps:5500,peak:8e3}],e=2;function r(e,r={}){return{version:e,rungs:t,audioKbps:128,firstSegmentS:1,segmentS:4,keyframeS:2,segmentName:"seg",copyMaxGopS:4,copyMaxBps:12e6,preset:"veryfast",cappedPreset:"superfast",...r}}const n=(t,e)=>`expr:gte(t,if(lt(n_forced,2),n_forced*${t},(n_forced-1)*${e}))`;function s(t,e,r,n){const s=t=>Math.max(2,2*Math.round(t/2));if(n&&Math.min(e,r)>n){const t=n/Math.min(e,r);e=s(e*t),r=s(r*t)}const o=Math.min(e,r),a=[];for(const n of t){if(n.short>=.9*o)break;const t=n.short/o;a.push({width:s(e*t),height:s(r*t),kbps:n.kbps,peak:n.peak,top:!1})}const i=[...t].reverse().find(t=>t.short<=1.1*o)??t[0];return a.push({width:s(e),height:s(r),kbps:i.kbps,peak:i.peak,top:!0}),a}const o=["ladder","poster","storyboard","faststart","audio","transcript","frame-check"],a=["audio","transcript","frame-check"],i=t=>t.some(t=>a.includes(t)),c=t=>t.includes("audio")||t.includes("transcript");function u(t,e={}){return{version:t,audio:{kbps:24,chunkS:600,...e.audio},vad:{noiseDb:-40,minSilenceS:.6,edgeS:.3,...e.vad},transcript:{model:"@cf/openai/whisper-large-v3-turbo",...e.transcript},frameCheck:{gapFactor:1.5,jitter:.1,maxErrorLines:20,...e.frameCheck},...e.storyboard?{storyboard:{...e.storyboard}}:{}}}const d=u("steps/1"),l=(t,e)=>t.includes("storyboard")&&!!e?.storyboard,f=(t,e,r=d)=>{const n="string"==typeof r?{version:r}:r;return i(e)||l(e,n)?`${t}+${n.version}`:t},h="audio/audio.json",p="transcript.vtt",m="transcript.json",y="frame-check.json",g=t=>`audio/a-${String(t).padStart(3,"0")}.ogg`;function b(t,e=o){if(!Array.isArray(t)||!t.length||t.length>2*o.length)return null;for(const r of t)if("string"!=typeof r||!e.includes(r))return null;return o.filter(e=>t.includes(e))}const w=/^[a-z0-9]{1,8}$/;function x(t,e){if("master.m3u8"===t)return"playlist";const r=new RegExp(`^v(\\d{1,2})\\/(index\\.m3u8|init(?:_\\d{1,2})?\\.mp4|(?:${function(t){for(const e of t)if(!w.test(e))throw new Error(`bad segment name: ${e}`);return t.join("|")}(e)})-\\d{5,6}\\.m4s)$`).exec(t);return r?"index.m3u8"===r[2]?"playlist":r[2].startsWith("init")?"init":"segment":null}function S(t,e,r){if(e.includes("ladder")){const e=x(t,r);if(e)return e}if(e.includes("poster")&&"poster.jpg"===t)return"poster";if(e.includes("storyboard")){if("storyboard/storyboard.json"===t)return"storyboard-index";if(/^storyboard\/sb-\d{3,4}\.(?:jpg|webp)$/.test(t)||"storyboard/storyboard.vtt"===t)return"storyboard"}if(e.includes("faststart")&&"faststart.mp4"===t)return"faststart";if(c(e)){if(t===h)return"audio-index";if(/^audio\/a-\d{3,4}\.ogg$/.test(t))return"audio"}if(e.includes("transcript")){if(t===p)return"transcript";if(t===m)return"transcript-json"}return e.includes("frame-check")&&t===y?"frame-check":null}function v(t){return t.includes("ladder")?"master.m3u8":t.includes("faststart")?"faststart.mp4":t.includes("storyboard")?"storyboard/storyboard.json":t.includes("poster")||!i(t)?"poster.jpg":c(t)?h:y}const $=t=>t.endsWith(".m3u8")?"application/vnd.apple.mpegurl":t.endsWith(".m4s")?"video/iso.segment":t.endsWith(".jpg")?"image/jpeg":t.endsWith(".webp")?"image/webp":t.endsWith(".json")?"application/json":t.endsWith(".ogg")?"audio/ogg":t.endsWith(".vtt")?"text/vtt; charset=utf-8":"video/mp4";async function M(t,e,r){const n=new Uint8Array(await crypto.subtle.digest("SHA-256",(new TextEncoder).encode(`${t}#${e}${r?`#${r}`:""}`)));let s="";for(let t=0;t<n.length;t++)s+=n[t].toString(16).padStart(2,"0");return s}const k=4096,j=604800,A=t=>"number"==typeof t&&Number.isFinite(t)&&t>0&&t<=j;function E(t){return JSON.stringify({id:t.id,attempt:t.attempt,src:t.src,etag:t.etag,bytes:t.bytes,prefix:t.prefix,done:t.done,...void 0!==t.maxShort?{maxShort:t.maxShort}:{},...void 0!==t.steps?{steps:t.steps}:{},...void 0!==t.issuedAt?{issuedAt:t.issuedAt}:{},...void 0!==t.expectDurationS?{expectDurationS:t.expectDurationS}:{}})}const F=(t,e)=>t.ladder===e,N="x-media-expect-duration",D="x-media-version",C=.8,B=t=>Math.round(1e3*t)/1e3;function O(t,e,r){let n=0;for(const s of r)n+=Math.max(0,Math.min(e,s.end)-Math.max(t,s.start));return n}function W(t,e,r,n){const s=e-t;if(s<=0)return r.some(e=>t>=e.start&&t<=e.end);const o=O(t,e,r);return o>0&&o>=.8*s&&s-o<=n}const T=(t,e)=>t.end-t.start-O(t.start,t.end,e)<=.05;function J(t,e){const r=[];let n=0,s=t.language;for(const{chunk:o,result:a}of t.results){s??=a.transcription_info?.language;const i=o.end-o.start;for(const s of a.segments??[]){const a=String(s.text??"").trim();if(!a||!Number.isFinite(s.start)||!Number.isFinite(s.end))continue;const c=B(o.start+Math.max(0,Math.min(s.start,i))),u=B(o.start+Math.max(0,Math.min(Math.max(s.end,s.start),i)));if(W(c,u,t.silences,e.edgeS)){n++;continue}const d=(s.words??[]).filter(t=>t&&"string"==typeof t.word&&Number.isFinite(t.start)&&Number.isFinite(t.end)).map(t=>({word:t.word.trim(),start:B(o.start+Math.max(0,t.start)),end:B(o.start+Math.max(0,t.end))})).filter(e=>e.word&&!W(e.start,e.end,t.silences,0));r.push({start:c,end:Math.max(c,u),text:a,...d.length?{words:d}:{}})}}return r.sort((t,e)=>t.start-e.start),{format:1,model:t.model,...s?{language:s}:{},seconds:B(t.seconds),segments:r,text:r.map(t=>t.text).join(" "),dropped:n,silentChunks:t.silentChunks??0}}function q(t){const e=Math.max(0,Math.round(1e3*t)),r=Math.floor(e/36e5),n=Math.floor(e/6e4)%60,s=Math.floor(e/1e3)%60,o=(t,e=2)=>String(t).padStart(e,"0");return`${o(r)}:${o(n)}:${o(s)}.${o(e%1e3,3)}`}const z=t=>t.replace(/\r\n?/g,"\n").replace(/\n\s*\n/g,"\n").replace(/-->/g,"→").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").trim();function H(t){const e=["WEBVTT",""];return t.segments.forEach((t,r)=>{const n=t.end>t.start?t.end:t.start+.5;e.push(String(r+1),`${q(t.start)} --\x3e ${q(n)}`,z(t.text),"")}),e.join("\n")}function _(t){let e;try{e=JSON.parse(t)}catch{return null}if(!e||"object"!=typeof e)return null;const r=e;if(1!==r.format||"string"!=typeof r.model||!Array.isArray(r.segments))return null;for(const t of r.segments)if(!t||"number"!=typeof t.start||"number"!=typeof t.end||"string"!=typeof t.text)return null;return r}function Q(t){let e;try{e=JSON.parse(t)}catch{return null}if(!e||"object"!=typeof e)return null;const r=e,n=t=>"number"==typeof t&&Number.isFinite(t)&&t>=0;if(1!==r.format||!Array.isArray(r.chunks)||!Array.isArray(r.silences)||!n(r.seconds))return null;for(const t of r.chunks)if(!t||"string"!=typeof t.file||!/^audio\/a-\d{3,4}\.ogg$/.test(t.file)||!n(t.start)||!n(t.end)||t.end<t.start)return null;for(const t of r.silences)if(!t||!n(t.start)||!n(t.end)||t.end<t.start)return null;return r}function U(t){return{audioIndex:`${t}audio/audio.json`,transcriptVtt:`${t}transcript.vtt`,transcriptJson:`${t}transcript.json`,frameCheck:`${t}frame-check.json`}}function V(t){let e;try{e=JSON.parse(t)}catch{return null}if(!e||"object"!=typeof e)return null;const r=e;return 1===r.format&&"boolean"==typeof r.ok&&"number"==typeof r.frames&&Array.isArray(r.gaps)&&Array.isArray(r.decodeErrors)?r:null}const I=new TextEncoder;new TextDecoder;function K(t){const e="string"==typeof t?I.encode(t):t,r=new Uint8Array(e.length);return r.set(e),r}async function P(t,e,r){if(function(t){if("string"!=typeof t||t.length<32)throw new Error("secret missing or too short")}(t),"string"!=typeof e||e.includes("\n"))throw new Error("bad HMAC domain");return(t=>{let e="";for(let r=0;r<t.length;r++)e+=t[r].toString(16).padStart(2,"0");return e})(await async function(t,e){const r=await crypto.subtle.importKey("raw",K(t),{name:"HMAC",hash:"SHA-256"},!1,["sign"]);return new Uint8Array(await crypto.subtle.sign("HMAC",r,K(e)))}(t,`${e}\n${r}`))}class R extends Error{reached;status;timedOut;constructor(t,e){super(t,void 0===e.cause?void 0:{cause:e.cause}),this.name="MediaJobError",this.reached=e.reached,this.timedOut=e.timedOut??!1,void 0!==e.status&&(this.status=e.status)}}function G(t){const e=()=>"function"==typeof t.secret?t.secret():t.secret;return{sign:async(r,n)=>P(e(),(e=>"job"===e?t.jobDomain:t.doneDomain)(r),n),async request(r){if(void 0!==r.expectDurationS&&!A(r.expectDurationS))throw new Error(`media job: bad expectDurationS ${String(r.expectDurationS)}`);const n=E(t.issuedAt&&void 0===r.issuedAt?{...r,issuedAt:Math.floor((t.now??Date.now)()/1e3)}:r);if(n.length>k)throw new Error("media job: request too big");let s;try{s=await(t.fetch??fetch)(`${t.url.replace(/\/$/,"")}/jobs`,{method:"POST",body:n,headers:{"content-type":"application/json",[t.signatureHeader]:await P(e(),t.jobDomain,n)},signal:AbortSignal.timeout(t.timeoutMs??3e4)})}catch(t){const e="TimeoutError"===t?.name||"AbortError"===t?.name;throw new R(`media worker: ${e?"no answer in time":"unreachable"}: ${t?.message??String(t)}`,{reached:!1,timedOut:e,cause:t})}if(!s.ok)throw new R(`media worker: ${s.status} ${(await s.text().catch(()=>"")).slice(0,200)}`,{reached:!0,status:s.status})},async readReport(r,n){if("string"!=typeof r||r.length>k||!function(t,e){if("string"!=typeof t||"string"!=typeof e||t.length!==e.length)return!1;let r=0;for(let n=0;n<t.length;n++)r|=t.charCodeAt(n)^e.charCodeAt(n);return 0===r}(n,await P(e(),t.doneDomain,r)))return{ok:!1,status:403,body:"bad signature"};let s;try{s=JSON.parse(r)}catch{return{ok:!1,status:400,body:"bad json"}}return s&&"object"==typeof s?{ok:!0,report:s}:{ok:!1,status:400,body:"bad report"}}}}function L(t,e){return"done"===t.state&&!F(t,e)}function X(t,e={}){const r={version:t,intervalS:2,maxFrames:200,size:160,cols:5,rows:5,quality:60,minQuality:20,maxSheetBytes:1e5,maxBytes:1e6,...e};return tt(r),r}const Y=X("storyboard/1");function Z(t,e,r){return"number"==typeof t&&Number.isInteger(t)&&t>=e&&t<=r}function tt(t){const e=t=>{throw new Error(`storyboard settings: bad ${t}`)};"string"==typeof t.version&&t.version||e("version"),"number"==typeof t.intervalS&&t.intervalS>=.1&&t.intervalS<=3600||e("intervalS"),Z(t.maxFrames,1,1e4)||e("maxFrames"),(!Z(t.size,16,1024)||t.size%2)&&e("size"),Z(t.cols,1,32)&&Z(t.rows,1,32)||e("cols/rows"),(!Z(t.quality,1,100)||!Z(t.minQuality,1,100)||t.minQuality>t.quality)&&e("quality/minQuality"),Z(t.maxSheetBytes,1e3,1e8)&&Z(t.maxBytes,t.maxSheetBytes,1e9)||e("maxSheetBytes/maxBytes")}const et=t=>Math.max(2,2*Math.round(t/2));function rt(t,e=Y){const r=t.width>0?t.width:16,n=t.height>0?t.height:9,s=Number.isFinite(t.seconds)&&t.seconds>0?t.seconds:0,o=e.size/Math.max(r,n),a=Math.ceil(1e3*Math.max(e.intervalS,s/e.maxFrames))/1e3,i=s?Math.min(e.maxFrames,Math.max(1,Math.ceil(s/a-1e-9))):1;return{interval:a,maxFrames:e.maxFrames,expected:i,width:et(r*o),height:et(n*o),cols:e.cols,rows:e.rows,seconds:s}}const nt="storyboard",st="storyboard/storyboard.vtt",ot="storyboard/storyboard.json",at=t=>`storyboard/sb-${String(t+1).padStart(3,"0")}.webp`;function it(t){return t===ot?"storyboard-index":t===st?"storyboard-vtt":/^storyboard\/sb-\d{3,4}\.webp$/.test(t)?"storyboard":null}const ct=(t,e)=>Math.ceil(e/(t.cols*t.rows));function ut(t,e){const r=t.cols*t.rows,n=[];for(let s=0;s<e;s++){const o=s%r,a=dt(s*t.interval),i=dt((s+1)*t.interval),c=s===e-1?Math.max(i,dt(t.seconds)):i;n.push({start:a,end:c,sheet:Math.floor(s/r),x:o%t.cols*t.width,y:Math.floor(o/t.cols)*t.height,w:t.width,h:t.height})}return n}function dt(t){return Math.round(1e3*t)/1e3}function lt(t){const e=Math.round(1e3*t),r=Math.floor(e/36e5),n=Math.floor(e/6e4)%60,s=Math.floor(e/1e3)%60,o=(t,e=2)=>String(t).padStart(e,"0");return`${o(r)}:${o(n)}:${o(s)}.${o(e%1e3,3)}`}function ft(t,e){const r=["WEBVTT",""];for(const n of ut(t,e))r.push(`${lt(n.start)} --\x3e ${lt(n.end)}`,`${at(n.sheet).slice(11)}#xywh=${n.x},${n.y},${n.w},${n.h}`,"");return r.join("\n")}function ht(t,e,r,n){const s=ct(t,e);return{version:r,interval:t.interval,cols:t.cols,rows:t.rows,width:t.width,height:t.height,count:e,seconds:dt(t.seconds),sheets:Array.from({length:s},(t,e)=>at(e)),vtt:st,bytes:n}}function pt(t){const e=t;return!!e&&"object"==typeof e&&"string"==typeof e.version&&[e.interval,e.width,e.height,e.seconds,e.bytes].every(t=>"number"==typeof t&&Number.isFinite(t)&&t>=0)&&e.interval>0&&Z(e.cols,1,32)&&Z(e.rows,1,32)&&Z(e.count,1,1e5)&&Array.isArray(e.sheets)&&e.sheets.length===Math.ceil(e.count/(e.cols*e.rows))&&e.sheets.every(t=>"string"==typeof t&&"storyboard"===it(t))&&e.vtt===st}function mt(t,e){/\/$/.test(t)||(t+="/");const r={vtt:t+st,index:t+ot};if(!e)return r;if(!pt(e))throw new Error("storyboard: not a storyboard index");return{...r,interval:e.interval,cols:e.cols,rows:e.rows,width:e.width,height:e.height,count:e.count,seconds:e.seconds,sheets:e.sheets.map(e=>t+e)}}const yt="server/media";export{h as AUDIO_INDEX,t as DEFAULT_RUNGS,d as DEFAULT_STEP_SETTINGS,Y as DEFAULT_STORYBOARD_SETTINGS,N as EXPECT_HEADER,a as EXTRA_STEPS,y as FRAME_CHECK_FILE,k as MAX_BODY,j as MAX_EXPECT_DURATION_S,R as MediaJobError,e as RUNNER_OUTPUT_REVISION,yt as SDK_ENTRY,C as SILENT_SHARE,o as STEPS,nt as STORYBOARD_DIR,ot as STORYBOARD_INDEX,st as STORYBOARD_VTT,m as TRANSCRIPT_JSON,p as TRANSCRIPT_VTT,D as VERSION_HEADER,g as audioChunkName,J as buildTranscript,tt as checkStoryboardSettings,T as chunkSilent,v as completionFile,$ as contentTypeOf,i as hasExtraSteps,W as inSilence,pt as isStoryboardIndex,E as jobBody,f as jobVersion,n as keyframeExpr,x as ladderFile,r as ladderSettings,G as mediaJobClient,U as mediaStepFiles,S as outputFile,b as parseSteps,rt as planStoryboard,Q as readAudioIndex,V as readFrameCheck,_ as readTranscript,L as refuseStale,s as rungsFor,O as silentSeconds,M as sourceHash,F as stampMatches,u as stepSettings,ut as storyboardCues,mt as storyboardDescriptor,it as storyboardFile,ht as storyboardIndex,X as storyboardSettings,ct as storyboardSheetCount,at as storyboardSheetName,l as storyboardV2,ft as storyboardVtt,H as toWebVtt,A as validExpectDuration,z as vttText,q as vttTime,c as wantsAudio};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var e=require("@uploadfilm/sdk/server");const t="local own = KEYS[1] .. '\0uf-own'\nlocal ok\nif ARGV[2] ~= '' then ok = redis.call('SET', KEYS[1], ARGV[1], 'NX', 'PX', ARGV[2]) else ok = redis.call('SET', KEYS[1], ARGV[1], 'NX') end\nif ok then\n redis.call('SET', own, ARGV[3], 'PX', 60000)\n return 1\nend\nif redis.call('GET', own) == ARGV[3] then return 1 end\nreturn 0",r=()=>`${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}${Math.random().toString(36).slice(2)}`;const n=t=>{const r="string"==typeof t?Number(t):t;if("number"!=typeof r||!Number.isFinite(r))throw new e.StateError(`unexpected Redis reply ${String(t)}`);return r};exports.SDK_ENTRY="server/redis",exports.createRedisStore=function(i,o={}){const s=o.kind??function(t){const r=t;if("function"==typeof r.defineCommand)return"ioredis";if("function"==typeof r.exec&&"opts"in r)return"upstash";throw new e.StateError("createRedisStore: pass an ioredis or @upstash/redis client, or say which with { kind }")}(i),a=o.prefix??"";if("upstash"===s&&!1!==(l=i,l.opts)?.automaticDeserialization)throw new e.StateError("createRedisStore: create the @upstash/redis client with { automaticDeserialization: false } so values come back as stored");var l;const c=i,d=i,u=(e,t,r)=>"ioredis"===s?c.eval(e,1,a+t,...r):d.eval(e,[a+t],r);return{get:async t=>(t=>{if(null==t)return null;if("string"!=typeof t)throw new e.StateError("Redis returned a non-string value (is automaticDeserialization off?)");return t})(await("ioredis"===s?c.get(a+t):d.get(a+t))),async set(i,o,l={}){e.checkValue(o);const f=e.ttlMsOf(l);let S;if("ioredis"===s){if(l.ifAbsent)return 1===n(await u(t,i,[o,void 0===f?"":String(f),r()]));const e=[];void 0!==f&&e.push("PX",f),S=await c.set(a+i,o,...e)}else{if(l.ifAbsent)return 1===n(await u(t,i,[o,void 0===f?"":String(f),r()]));S=await d.set(a+i,o,void 0!==f?{px:f}:void 0)}return"OK"===S},async del(e){await("ioredis"===s?c.del(a+e):d.del(a+e))},async ttl(e){const t=n(await("ioredis"===s?c.pttl(a+e):d.pttl(a+e)));return-2===t?null:-1===t?1/0:t},async incr(t,r=1,i){e.checkBy(r);const o=e.ttlMsOf(i),s=n(await u("local cur = redis.call('GET', KEYS[1])\nlocal existed = cur ~= false\nlocal n = tonumber(ARGV[1])\nif existed and string.match(cur, '^-?%d+$') then\n local c = tonumber(cur)\n if c + n > 9007199254740991 or c + n < -9007199254740991 or c > 9007199254740991 or c < -9007199254740991 then\n return redis.error_reply('ERR increment would overflow')\n end\nelseif not existed and (n > 9007199254740991 or n < -9007199254740991) then\n return redis.error_reply('ERR increment would overflow')\nend\nredis.call('INCRBY', KEYS[1], ARGV[1])\nif not existed and tonumber(ARGV[2]) > 0 then redis.call('PEXPIRE', KEYS[1], ARGV[2]) end\nreturn redis.call('GET', KEYS[1])",t,[String(r),String(o??0)]));if(!Number.isSafeInteger(s))throw new e.StateError(`counter out of the safe-integer range: ${s}`);return s},async compareAndSet(t,r,i,o){e.checkValue(i);const s=e.ttlMsOf(o),a=await u("local cur = redis.call('GET', KEYS[1])\nif (ARGV[1] == '0' and cur == false) or (ARGV[1] == '1' and cur == ARGV[2]) then\n if ARGV[4] ~= '' then redis.call('SET', KEYS[1], ARGV[3], 'PX', ARGV[4]) else redis.call('SET', KEYS[1], ARGV[3]) end\n return 1\nend\nreturn 0",t,[null===r?"0":"1",r??"",i,void 0===s?"":String(s)]);return 1===n(a)}}};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{StateError as e,checkValue as n,ttlMsOf as t,checkBy as r}from"@uploadfilm/sdk/server";const i="server/redis",o="local own = KEYS[1] .. '\0uf-own'\nlocal ok\nif ARGV[2] ~= '' then ok = redis.call('SET', KEYS[1], ARGV[1], 'NX', 'PX', ARGV[2]) else ok = redis.call('SET', KEYS[1], ARGV[1], 'NX') end\nif ok then\n redis.call('SET', own, ARGV[3], 'PX', 60000)\n return 1\nend\nif redis.call('GET', own) == ARGV[3] then return 1 end\nreturn 0",s=()=>`${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}${Math.random().toString(36).slice(2)}`;const a=n=>{const t="string"==typeof n?Number(n):n;if("number"!=typeof t||!Number.isFinite(t))throw new e(`unexpected Redis reply ${String(n)}`);return t};function l(i,l={}){const c=l.kind??function(n){const t=n;if("function"==typeof t.defineCommand)return"ioredis";if("function"==typeof t.exec&&"opts"in t)return"upstash";throw new e("createRedisStore: pass an ioredis or @upstash/redis client, or say which with { kind }")}(i),d=l.prefix??"";if("upstash"===c&&!1!==(u=i,u.opts)?.automaticDeserialization)throw new e("createRedisStore: create the @upstash/redis client with { automaticDeserialization: false } so values come back as stored");var u;const f=i,w=i,h=(e,n,t)=>"ioredis"===c?f.eval(e,1,d+n,...t):w.eval(e,[d+n],t);return{get:async n=>(n=>{if(null==n)return null;if("string"!=typeof n)throw new e("Redis returned a non-string value (is automaticDeserialization off?)");return n})(await("ioredis"===c?f.get(d+n):w.get(d+n))),async set(e,r,i={}){n(r);const l=t(i);let u;if("ioredis"===c){if(i.ifAbsent)return 1===a(await h(o,e,[r,void 0===l?"":String(l),s()]));const n=[];void 0!==l&&n.push("PX",l),u=await f.set(d+e,r,...n)}else{if(i.ifAbsent)return 1===a(await h(o,e,[r,void 0===l?"":String(l),s()]));u=await w.set(d+e,r,void 0!==l?{px:l}:void 0)}return"OK"===u},async del(e){await("ioredis"===c?f.del(d+e):w.del(d+e))},async ttl(e){const n=a(await("ioredis"===c?f.pttl(d+e):w.pttl(d+e)));return-2===n?null:-1===n?1/0:n},async incr(n,i=1,o){r(i);const s=t(o),l=a(await h("local cur = redis.call('GET', KEYS[1])\nlocal existed = cur ~= false\nlocal n = tonumber(ARGV[1])\nif existed and string.match(cur, '^-?%d+$') then\n local c = tonumber(cur)\n if c + n > 9007199254740991 or c + n < -9007199254740991 or c > 9007199254740991 or c < -9007199254740991 then\n return redis.error_reply('ERR increment would overflow')\n end\nelseif not existed and (n > 9007199254740991 or n < -9007199254740991) then\n return redis.error_reply('ERR increment would overflow')\nend\nredis.call('INCRBY', KEYS[1], ARGV[1])\nif not existed and tonumber(ARGV[2]) > 0 then redis.call('PEXPIRE', KEYS[1], ARGV[2]) end\nreturn redis.call('GET', KEYS[1])",n,[String(i),String(s??0)]));if(!Number.isSafeInteger(l))throw new e(`counter out of the safe-integer range: ${l}`);return l},async compareAndSet(e,r,i,o){n(i);const s=t(o),l=await h("local cur = redis.call('GET', KEYS[1])\nif (ARGV[1] == '0' and cur == false) or (ARGV[1] == '1' and cur == ARGV[2]) then\n if ARGV[4] ~= '' then redis.call('SET', KEYS[1], ARGV[3], 'PX', ARGV[4]) else redis.call('SET', KEYS[1], ARGV[3]) end\n return 1\nend\nreturn 0",e,[null===r?"0":"1",r??"",i,void 0===s?"":String(s)]);return 1===a(l)}}}export{i as SDK_ENTRY,l as createRedisStore};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var e=require("node:http"),t=require("node:crypto"),s=require("node:zlib"),a=require("node:assert/strict");const r=e=>t.createHash("md5").update(e).digest(),n=e=>r(e).toString("hex");function i(e){return`${r(Buffer.concat(e.map(e=>Buffer.from(e.replace(/"/g,""),"hex")))).toString("hex")}-${e.length}`}const o=e=>!!e&&/^(text\/|application\/(json|xml|javascript|x-javascript|ld\+json|xhtml\+xml|rss\+xml|atom\+xml|manifest\+json|x-subrip)|image\/svg\+xml)/i.test(e),c=5242880,u=1e4,l=["InvalidArgument","Multiple ranges are not supported: send one byte range per request, as bytes=first-last, bytes=first- or bytes=-n"],d=globalThis.setTimeout,h=e=>new Promise(t=>d(t,e)),g=e=>encodeURIComponent(e).replace(/[!'()*]/g,e=>`%${e.charCodeAt(0).toString(16).toUpperCase()}`),p=e=>e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'"),m=e=>e.replace(/"/g,'"').replace(/'/g,"'").replace(/</g,"<").replace(/>/g,">").replace(/&/g,"&"),f=e=>t.createHash("sha256").update(e).digest("hex"),y=new Map;function k(e,t){let s=y.get(e);if(!s){s=new Uint32Array(256);for(let t=0;t<256;t++){let a=t;for(let t=0;t<8;t++)a=1&a?a>>>1^e:a>>>1;s[t]=a>>>0}y.set(e,s)}let a=-1>>>0;for(let e=0;e<t.length;e++)a=s[255&(a^t[e])]^a>>>8;const r=Buffer.alloc(4);return r.writeUInt32BE(~a>>>0),r.toString("base64")}const w={crc32:e=>k(3988292384,e),crc32c:e=>k(2197175160,e),sha1:e=>t.createHash("sha1").update(e).digest("base64"),sha256:e=>t.createHash("sha256").update(e).digest("base64")};const b=(e,t)=>e.split(",").some(e=>{const s=e.trim();return"*"===s||s.replace(/^W\//,"").replace(/"/g,"")===t});class S{objects=new Map;uploads=new Map;events=[];puts=[];counters={requests:0,faults:0,rejectedSignature:0,partPuts:0,objectPuts:0,s3Calls:0};offline=!1;refused=new Map;scale=1;weakEtags=!0;enforceSignatures=!0;keepBodies=!0;publicBase="";tokenMount="/s3/";presignTtlMs=36e5;now=()=>Date.now();gzipText=!0;allowChunkedPuts=!1;expiryAfterBody=!1;uploadMaxAgeMs=0;aborted=new Map;s3;server;sockets=new Set;faults=[];gates=new Map;nextFaultId=1;presigned=new Map;base="";port=0;constructor(e={}){this.s3=e.s3}async start(t=0,s="127.0.0.1"){return this.server=e.createServer((e,t)=>{this.handle(e,t)}),this.server.keepAliveTimeout=5e3,this.server.requestTimeout=0,this.server.headersTimeout=0,this.server.on("connection",e=>{this.sockets.add(e),e.on("close",()=>this.sockets.delete(e))}),await new Promise(e=>this.server.listen(t,s,()=>e())),this.port=this.server.address().port,this.base=`http://${s}:${this.port}`,this}async close(){for(const e of this.sockets)e.destroy();await new Promise(e=>this.server?this.server.close(()=>e()):e())}get endpoint(){return this.base}fault(e){let t;const s=new Promise(e=>t=e),a={...e,id:this.nextFaultId++,hits:0,seen:0,remove:()=>{this.faults=this.faults.filter(e=>e!==a)},release:()=>t()};return this.gates.set(a,s),this.faults.push(a),a}clearFaults(){this.faults=[]}outage(e){if(this.offline=e,this.log({kind:e?"outage-start":"outage-end"}),e)for(const e of this.sockets)e.destroy()}refusedAttempts(e,t){return this.refused.get(`${e}#${t}`)??0}abortUpload(e){this.uploads.delete(e)}liveUpload(e){if(!e)return;const t=this.uploads.get(e);if(t&&this.uploadMaxAgeMs>0)if(void 0===t.created)t.created=this.now();else if(this.now()-t.created>this.uploadMaxAgeMs)return this.uploads.delete(e),void this.log({kind:"lifecycle-abort",key:t.key,detail:e});return t}targetMatches(e,t){return e===t||"storage"===e&&("partPut"===t||"objectPut"===t)||"s3:*"===e&&t.startsWith("s3:")}pick(e){for(const t of this.faults){if((Array.isArray(t.target)?t.target:[t.target]).some(t=>this.targetMatches(t,e.target))&&((!t.match||t.match(e))&&(t.seen++,!(t.after&&t.seen<=t.after||void 0!==t.times&&t.hits>=t.times))))return t.hits++,this.counters.faults++,this.log({kind:"fault",target:e.target,key:e.key,n:e.partNumber,fault:t.action,status:t.status}),t}return null}log(e){this.events.push({t:Date.now(),...e}),this.events.length>5e4&&this.events.splice(0,1e4)}object(e){return this.objects.get(e)}sha256(e){const s=this.objects.get(e);return s?t.createHash("sha256").update(s.body).digest("hex"):null}headInfo(e){const t=this.objects.get(e);if(!t)return{size:null,etag:null};const s=this.weakEtags&&o(t.contentType)&&t.size>1024;return{size:t.size,etag:s?`W/${t.etag}`:t.etag}}partPutsOf(e,t){return this.puts.filter(s=>"partPut"===s.target&&s.uploadId===e&&(void 0===t||s.n===t)&&200===s.outcome)}stamp(){return this.s3?{lastModified:this.now()}:{}}seed(e,t,s,a){const r=Buffer.isBuffer(t)?t:Buffer.from(t);this.objects.set(e,{body:r,etag:n(r),size:r.length,contentType:s,meta:a,lastModified:this.now()})}presign(e,s,a={}){const r=t.randomBytes(12).toString("hex"),n=Date.now()+this.presignTtlMs*this.scale;this.presigned.set(r,{key:e,headers:s,expiresAt:n,...a});const i=this.publicBase||this.base,o=new URLSearchParams;return void 0!==a.partNumber&&o.set("partNumber",String(a.partNumber)),void 0!==a.uploadId&&o.set("uploadId",a.uploadId),o.set("X-Uf-Signature",r),{url:`${i}${this.tokenMount}${encodeURIComponent(e)}?${o}`,expiresAt:new Date(Date.now()+this.presignTtlMs).toISOString()}}presignGet(e,s=3e5){const a=t.randomBytes(12).toString("hex");return this.presigned.set(a,{key:e,headers:{},expiresAt:Date.now()+s*this.scale,get:!0}),`${this.publicBase||this.base}${this.tokenMount}${encodeURIComponent(e)}?X-Uf-Signature=${a}`}expireToken(e,t){const s=this.presigned.get(new URL(e).searchParams.get("X-Uf-Signature")??"");s&&(s.expiresAt=t)}async handle(e,t){this.counters.requests++;const s=new URL(e.url,this.base);if(this.offline)this.refuse(e,s);else try{return s.pathname.startsWith(this.tokenMount)?await this.storage(e,t,s):this.s3&&(s.pathname===`/${this.s3.bucket}`||s.pathname.startsWith(`/${this.s3.bucket}/`))?await this.s3api(e,t,s):await this.route(e,t,s)}catch(s){this.log({kind:"error",detail:String(s?.stack??s)}),t.headersSent?e.socket.destroy():this.json(t,500,{error:String(s)})}}async route(e,t,s){await this.body(e).catch(()=>null),this.json(t,404,{error:"not found"})}bump(e){this.refused.set(e,(this.refused.get(e)??0)+1)}async refuse(e,t){if(t.pathname.startsWith(this.tokenMount)){const e=decodeURIComponent(t.pathname.slice(this.tokenMount.length)),s=t.searchParams.get("partNumber");this.bump(s?`${e}#${s}`:`${e}#put`)}else this.bump(t.pathname);e.socket.destroy()}json(e,t,s){const a=JSON.stringify(s);e.writeHead(t,{"content-type":"application/json","content-length":Buffer.byteLength(a)}),e.end(a)}s3err(e,t,s,a=s){if("HEAD"===e.req?.method)return e.writeHead(t,{"content-type":"application/xml"}),void e.end();const r=`<?xml version="1.0" encoding="UTF-8"?><Error><Code>${s}</Code><Message>${a}</Message></Error>`;e.writeHead(t,{"content-type":"application/xml","content-length":Buffer.byteLength(r)}),e.end(r)}xml(e,t,s,a={}){e.writeHead(t,{"content-type":"application/xml","content-length":Buffer.byteLength(s),...a}),e.end(s)}body(e){const t=e.__trickleMs;return new Promise(t?(s,a)=>{const r=[];e.pause(),e.on("end",()=>s(Buffer.concat(r))),e.on("error",a),e.on("aborted",()=>a(new Error("aborted")));const n=()=>{if(e.destroyed)return;let s,a=0;for(;a<65536&&null!==(s=e.read());)r.push(s),a+=s.length;e.readableEnded||d(n,t)};n()}:(t,s)=>{const a=[];e.on("data",e=>a.push(e)),e.on("end",()=>t(Buffer.concat(a))),e.on("error",s),e.on("aborted",()=>s(new Error("aborted")))})}async pre(e,t,s,a){if(!e)return"apply";switch(e.action){case"drop":return t.socket.destroy(),"handled";case"reset":{const e=Number(t.headers["content-length"]||0),s=Math.floor(e/2);let a=0;return t.on("data",e=>{a+=e.length,a>=s&&t.socket.destroy()}),0===e&&t.socket.destroy(),"handled"}case"status":{await this.body(t).catch(()=>null);const r=e.status??503;return a?this.s3err(s,r,e.code??(503===r?"ServiceUnavailable":500===r?"InternalError":"Injected")):this.json(s,r,{error:"injected",...e.code?{code:e.code}:{}}),"handled"}case"delay":return await h(e.ms??1e3),"apply";case"trickle":return t.__trickleMs=e.ms??50,"apply";case"stall":return t.pause(),e.ms?(await h(e.ms),t.resume(),"apply"):"handled";default:return"apply"}}async post(e,t){if(!e)return!1;if("dropResponse"===e.action)return t.socket.destroy(),!0;if("gate"===e.action&&await this.gates.get(e),"holdResponse"===e.action){if(!e.ms)return!0;await h(e.ms)}return!1}checkSignature(e,t,s,a,r=null!==a){if(!this.enforceSignatures)return null;const n=t.searchParams.get("X-Uf-Signature")??"",i=this.presigned.get(n),o=t.searchParams.get("partNumber"),c=t.searchParams.get("uploadId"),u=e=>({status:403,code:"SignatureDoesNotMatch",message:e});if(!i)return u("no such signature");if(i.key!==s||String(i.partNumber??"")!==(o??"")||(i.uploadId??"")!==(c??""))return u("URL does not match its signature");if((!r||this.expiryAfterBody)&&Date.now()>i.expiresAt)return{status:403,code:"ExpiredRequest",message:"Request has expired"};for(const[t,s]of Object.entries(i.headers)){const r=e.headers[t];if("content-length"!==t){if(void 0===r)return u(`signed header ${t} missing`);if(String(r)!==s)return u(`signed header ${t} changed`)}else if(null!==a&&String(a)!==s)return u(`signed content-length ${s}, body ${a}`)}return null}putTarget(e,t){return t?"partPut":"objectPut"}async storage(e,t,s){const a=decodeURIComponent(s.pathname.slice(this.tokenMount.length)),r=s.searchParams.get("partNumber"),n=s.searchParams.get("uploadId")??void 0;if("GET"===e.method){const r=this.pick({target:"storage:get",key:a}),n=this.presigned.get(s.searchParams.get("X-Uf-Signature")??"");if(this.enforceSignatures&&(!n||!n.get||n.key!==a))return this.s3err(t,403,"SignatureDoesNotMatch");if(this.enforceSignatures&&Date.now()>n.expiresAt)return this.s3err(t,403,"ExpiredRequest","Request has expired");if(!this.midBodyFault(r)&&"handled"===await this.pre(r,e,t,!0))return;const i=this.objects.get(a),o=i?this.readAnswer(i,e,s,!1,r):{status:404,error:"NoSuchKey"};if(await this.post(r,e))return;return o.error?this.s3err(t,o.status,o.error,o.message):this.sendRead(e,t,o,r)}if("PUT"!==e.method)return this.s3err(t,405,"MethodNotAllowed");const i=this.putTarget(a,r);return this.storePut(e,t,{key:a,pn:r,uploadId:n,target:i,check:t=>this.checkSignature(e,s,a,t),expire:()=>{const e=this.presigned.get(s.searchParams.get("X-Uf-Signature")??"");e&&(e.expiresAt=0)}})}async storePut(e,t,s){const{key:a,pn:i,uploadId:o,target:c}=s,l={t:Date.now(),target:c,key:a,n:i?Number(i):void 0,uploadId:o,outcome:"pending",bytes:0,md5:!!e.headers["content-md5"]};this.puts.push(l);const d=e.push.bind(e);e.push=(e,t)=>(e&&(l.bytes+=e.length),d(e,t)),t.on("close",()=>{"pending"===l.outcome&&(l.outcome="closed",l.clientClosedAt=Date.now())});const h=e=>(l.outcome=e,l.doneAt=Date.now()),g=this.pick({target:c,key:a,partNumber:l.n,uploadId:o});"expire"===g?.action&&s.expire();const p=s.check(null);if(p)return this.counters.rejectedSignature++,await this.body(e).catch(()=>null),h(p.status),this.s3err(t,p.status,p.code,p.message);const m=!this.allowChunkedPuts&&void 0===e.headers["content-length"]&&/chunked/i.test(String(e.headers["transfer-encoding"]??""))?[411,"MissingContentLength"]:null!==i&&(!/^\d+$/.test(i)||Number(i)<1||Number(i)>u)?[400,"InvalidArgument"]:null;if(m)return await this.body(e).catch(()=>null),h(m[0]),this.s3err(t,m[0],m[1]);if("noSuchUpload"===g?.action&&o&&this.uploads.delete(o),!g||"drop"!==g.action&&"reset"!==g.action||(l.outcome="dropped"),"handled"===await this.pre(g,e,t,!0))return void(t.headersSent&&h(t.statusCode));let f=await this.body(e);if(this.offline)return void e.socket.destroy();"corruptInTransit"===g?.action&&f.length&&(f=Buffer.from(f),f[f.length>>1]^=255);const y=s.check(f.length,f);if(y)return this.counters.rejectedSignature++,h(y.status),this.s3err(t,y.status,y.code,y.message);const k=e.headers["content-md5"];if(k&&r(f).toString("base64")!==k)return h(400),this.s3err(t,400,"BadDigest");const b=function(e,t){const s={};for(const[a,r]of Object.entries(w)){const n=e.headers[`x-amz-checksum-${a}`];if("string"==typeof n){if(r(t)!==n.trim())return null;s[a]=n.trim()}}return s}(e,f);if(!b)return h(400),this.s3err(t,400,"BadDigest");const S=Object.keys(b).length?{checksums:b}:{};"storeCorrupted"===g?.action&&f.length&&(f=Buffer.from(f),f[0]^=255);const x=n(f);if(this.counters[`${c}s`]=(this.counters[`${c}s`]??0)+1,i){const e=this.liveUpload(o);if(!e||e.key!==a)return h(404),this.s3err(t,404,"NoSuchUpload");e.parts.set(Number(i),{body:this.keepBodies?f:Buffer.alloc(0),etag:x,size:f.length,...S}),this.log({kind:"partPut",key:a,n:Number(i)})}else{if("*"===e.headers["if-none-match"]&&this.objects.has(a))return h(412),this.s3err(t,412,"PreconditionFailed");const r={};if(s.s3){const s=e.headers["if-match"];if(void 0!==s&&`"${this.objects.get(a)?.etag}"`!==s.replace(/^W\//,""))return h(412),this.s3err(t,412,"PreconditionFailed");for(const[t,s]of Object.entries(e.headers))t.startsWith("x-amz-meta-")&&"string"==typeof s&&(r[t.slice(11)]=s)}this.objects.set(a,{body:f,etag:x,size:f.length,contentType:e.headers["content-type"],...Object.keys(r).length?{meta:r}:{},...S,...this.stamp()}),this.log({kind:c,key:a})}await this.post(g,e)?l.outcome="dropped":(h(200),t.writeHead(200,"noEtag"===g?.action?{"content-length":0}:{etag:`"${x}"`,"content-length":0}),t.end())}createUpload(e,s){const a=t.randomBytes(12).toString("hex");return this.uploads.set(a,{key:e,contentType:s,parts:new Map,created:this.now()}),this.log({kind:"create",key:e,detail:a}),a}completeUpload(e,t,s,a={}){const r=this.liveUpload(t);if(!r||r.key!==e)return{ok:!1,code:"NoSuchUpload",status:404};if(!s.length||s.length>u)return{ok:!1,code:"MalformedXML",status:400};const n=[...s].map(e=>({n:Number(e.n),etag:String(e.etag)})).sort((e,t)=>e.n-t.n);for(let e=1;e<n.length;e++)if(n[e].n===n[e-1].n)return{ok:!1,code:"InvalidPartOrder",status:400};const o=[],l=[];let d=0,h=-1;for(let e=0;e<n.length;e++){const t=n[e],s=r.parts.get(t.n);if(!s||s.etag!==t.etag.replace(/"/g,"").toLowerCase())return{ok:!1,code:"InvalidPart",status:400};if(e<n.length-1){if(s.size<c)return{ok:!1,code:"EntityTooSmall",status:400};if(-1===h)h=s.size;else if(s.size!==h)return{ok:!1,code:"InvalidPart",status:400}}else if(-1!==h&&s.size>h)return{ok:!1,code:"InvalidPart",status:400};o.push(s.body),l.push(s.etag),d+=s.size}if(a.ifNoneMatch&&this.objects.has(e))return this.uploads.delete(t),{ok:!1,code:"PreconditionFailed",status:412};const g=i(l);let p=Buffer.concat(o);a.truncate&&(p=p.subarray(0,Math.max(0,p.length-1)),d=Math.max(0,d-1));const m={body:p,etag:g,size:d,contentType:r.contentType,...this.stamp()};return this.objects.set(e,m),this.uploads.delete(t),this.log({kind:"complete",key:e,detail:`parts=${n.length}`}),{ok:!0,object:m}}checkSigV4(e,s,a,r){const n=null===a||this.expiryAfterBody;if(!this.enforceSignatures||!this.s3)return null;const i=e=>({status:403,code:"SignatureDoesNotMatch",message:e}),o=s.searchParams,c=null!==o.get("X-Amz-Algorithm");let u,l,d,h,p;if(c){if("AWS4-HMAC-SHA256"!==o.get("X-Amz-Algorithm"))return i("algorithm");u=o.get("X-Amz-Credential")??"",l=o.get("X-Amz-SignedHeaders")??"",d=o.get("X-Amz-Signature")??"",h=o.get("X-Amz-Date")??"",p="UNSIGNED-PAYLOAD";const e=Number(o.get("X-Amz-Expires"));if(!Number.isInteger(e)||e<1||e>604800)return{status:400,code:"AuthorizationQueryParametersError",message:"X-Amz-Expires"};const t=Date.parse(`${h.slice(0,4)}-${h.slice(4,6)}-${h.slice(6,8)}T${h.slice(9,11)}:${h.slice(11,13)}:${h.slice(13,15)}Z`);if(!Number.isFinite(t))return i("date");if(n&&this.now()>t+1e3*e*this.scale)return{status:403,code:"ExpiredRequest",message:"Request has expired"};if(n&&t>this.now()+9e5)return{status:403,code:"RequestTimeTooSkewed",message:"Request is not valid yet"}}else{const t=String(e.headers.authorization??""),s=/^AWS4-HMAC-SHA256 Credential=([^,]+), ?SignedHeaders=([^,]+), ?Signature=([0-9a-f]{64})$/.exec(t);if(!s)return{status:403,code:"AccessDenied",message:"no SigV4 Authorization header"};if([,u,l,d]=s,h=String(e.headers["x-amz-date"]??""),p=String(e.headers["x-amz-content-sha256"]??""),!p)return{status:400,code:"InvalidRequest",message:"x-amz-content-sha256 missing"};if(r&&"UNSIGNED-PAYLOAD"!==p&&f(r)!==p)return{status:400,code:"XAmzContentSHA256Mismatch",message:"payload hash"}}const[m,y,k,w,b]=u.split("/");if(m!==this.s3.accessKeyId)return{status:403,code:"InvalidAccessKeyId",message:"unknown access key"};if(k!==(this.s3.region??"auto")||"s3"!==w||"aws4_request"!==b||y!==h.slice(0,8))return i("credential scope");const S=l.split(";");if(!S.includes("host")||[...S].sort().join(";")!==l)return i("signed headers");for(const t of Object.keys(e.headers))if(t.startsWith("x-amz-")&&"x-amz-content-sha256"!==t&&!S.includes(t))return{status:403,code:"AccessDenied",message:`unsigned header ${t}`};const x=[];for(const t of S){if("content-length"===t&&null!==a&&String(a)!==String(e.headers["content-length"]??a))return i("content-length");const s=e.headers[t];if(void 0===s)return i(`signed header ${t} missing`);x.push(`${t}:${String(s).trim().replace(/\s+/g," ")}`)}const $=(e.url??"/").split("?")[0],A=((e.url??"").includes("?")?(e.url??"").slice((e.url??"").indexOf("?")+1):"").split("&").filter(Boolean).map(e=>{const t=e.indexOf("="),s=e=>decodeURIComponent(e.replace(/\+/g," "));return[s(t<0?e:e.slice(0,t)),s(t<0?"":e.slice(t+1))]}).filter(([e])=>"X-Amz-Signature"!==e).map(([e,t])=>[g(e),g(t)]).sort(([e,t],[s,a])=>e<s?-1:e>s?1:t<a?-1:t>a?1:0),v=[e.method,$,A.map(([e,t])=>`${e}=${t}`).join("&"),x.join("\n")+"\n",l,p].join("\n"),q=["AWS4-HMAC-SHA256",h,`${y}/${k}/s3/aws4_request`,f(v)].join("\n"),M=(e,s)=>t.createHmac("sha256",e).update(s).digest(),T=M(M(M(M(`AWS4${this.s3.secretAccessKey}`,y),k),"s3"),"aws4_request");if(t.createHmac("sha256",T).update(q).digest("hex")!==d)return i("signature");if(!c){const e=Date.parse(`${h.slice(0,4)}-${h.slice(4,6)}-${h.slice(6,8)}T${h.slice(9,11)}:${h.slice(11,13)}:${h.slice(13,15)}Z`);if(!Number.isFinite(e)||n&&Math.abs(this.now()-e)>9e5)return{status:403,code:"RequestTimeTooSkewed",message:"date"}}return null}s3Key(e){return e.pathname.slice(this.s3.bucket.length+2).split("/").map(e=>decodeURIComponent(e)).join("/")}etagHeader(e,t){const s=String(t.headers["accept-encoding"]??""),a=this.weakEtags&&o(e.contentType)&&e.size>1024&&""!==s&&"identity"!==s;return{weak:a,etag:`${a?"W/":""}"${e.etag}"`}}async s3api(e,t,s){this.counters.s3Calls++;const a=this.s3Key(s),r=s.searchParams,n=e.method??"GET",i=r.get("partNumber"),o=r.get("uploadId")??void 0;if("PUT"===n&&a&&!e.headers["x-amz-copy-source"]){const r=this.putTarget(a,i);let n=!1;return this.storePut(e,t,{key:a,pn:i,uploadId:o,target:r,check:(t,a)=>n&&this.enforceSignatures&&(null===t||this.expiryAfterBody)?{status:403,code:"ExpiredRequest",message:"Request has expired"}:this.checkSigV4(e,s,t,a),expire:()=>n=!0,s3:!0})}const c=a?"POST"===n&&r.has("delete")?"deleteObjects":"POST"===n&&r.has("uploads")?"create":"POST"===n&&o?"complete":"DELETE"===n&&o?"abort":"GET"===n&&o?"listParts":"HEAD"===n?"head":"GET"===n?"get":"DELETE"===n?"delete":"PUT"===n?"copy":"unknown":"GET"===n&&r.has("uploads")?"listUploads":"GET"===n?"list":"POST"===n&&r.has("delete")?"deleteObjects":"bucket",u=`s3:${c}`,l=this.pick({target:u,key:a,partNumber:i?Number(i):void 0,uploadId:o}),d=this.checkSigV4(e,s,null);if(d)return this.counters.rejectedSignature++,await this.body(e).catch(()=>null),this.s3err(t,d.status,d.code,d.message);if("noSuchUpload"===l?.action&&o&&this.uploads.delete(o),("get"!==c||!this.midBodyFault(l))&&"handled"===await this.pre(l,e,t,!0))return;const h=await this.body(e),g=this.checkSigV4(e,s,h.length,h);if(g)return this.counters.rejectedSignature++,this.s3err(t,g.status,g.code,g.message);const p=await this.s3apply(c,a,e,s,h,l);if(!await this.post(l,e)){if(p.error)return this.s3err(t,p.status,p.error,p.message);if("get"===c||"head"===c)return this.sendRead(e,t,p,l);t.writeHead(p.status,{"content-length":p.body?p.body.length:0,...p.body?{"content-type":"application/xml"}:{},...p.headers}),t.end("HEAD"===n?void 0:p.body)}}midBodyFault(e){return!!e&&("reset"===e.action||"stall"===e.action)}async sendRead(e,t,s,a){const r={...s.headers};s.noLength?delete r["content-length"]:void 0===r["content-length"]&&(r["content-length"]=s.body?s.body.length:0),t.writeHead(s.status,r);const n="HEAD"===e.method||304===s.status?void 0:s.body;if(!n||!this.midBodyFault(a))return void t.end(n);const i=n.subarray(0,n.length>>1);if("reset"===a.action)return i.length?void t.write(i,()=>e.socket.destroy()):void e.socket.destroy();t.write(i),a.ms&&(await h(a.ms),t.destroyed||t.end(n.subarray(i.length)))}readAnswer(e,a,r,i,o){const{weak:c,etag:u}=this.etagHeader(e,a),d="wrongEtag"===o?.action?`"${n(t.randomBytes(8))}"`:u,h=a.headers["if-match"];if(void 0!==h&&!b(h,e.etag))return{status:412,error:"PreconditionFailed"};const g=a.headers["if-none-match"];if(void 0!==g&&b(g,e.etag))return{status:304,headers:{etag:d},noLength:!0};const p={etag:d,"content-type":e.contentType??"application/octet-stream","accept-ranges":"bytes"};void 0!==e.lastModified&&(p["last-modified"]=new Date(e.lastModified).toUTCString());for(const[t,s]of Object.entries(e.meta??{}))p[`x-amz-meta-${t}`]=s;const m=r.searchParams;for(const[e,t]of[["response-content-disposition","content-disposition"],["response-content-type","content-type"],["response-cache-control","cache-control"],["response-content-language","content-language"],["response-expires","expires"]]){const s=m.get(e);null!==s&&/^[\t\x20-\x7e]*$/.test(s)&&(p[t]=s)}const f=function(e,t){if(!e)return null;const s=/^\s*bytes\s*=\s*(.+)$/i.exec(e);if(!s)return null;if(s[1].includes(","))return 400;const a=/^\s*(\d*)\s*-\s*(\d*)\s*$/.exec(s[1]);if(!a||""===a[1]&&""===a[2])return null;if(""===a[1]){const e=Number(a[2]);return 0===e||0===t?416:{start:Math.max(0,t-e),end:t-1}}const r=Number(a[1]),n=""===a[2]?t-1:Math.min(Number(a[2]),t-1);return""!==a[2]&&Number(a[2])<r?null:r>=t?416:{start:r,end:n}}(a.headers.range,e.size);if(416===f)return{status:416,error:"InvalidRange",message:"The requested range is not satisfiable"};if(400===f)return{status:400,error:l[0],message:l[1]};if("ENABLED"===String(a.headers["x-amz-checksum-mode"]??"").toUpperCase()&&!f)for(const[t,s]of Object.entries(e.checksums??{}))p[`x-amz-checksum-${t}`]=s;if(f){const t=e.body.subarray(f.start,f.end+1);return p["content-range"]=`bytes ${f.start}-${f.end}/${e.size}`,p["content-length"]=f.end-f.start+1,{status:206,headers:p,body:i?void 0:t}}if(c){if(i)return{status:200,headers:p,noLength:!0};if(this.gzipText&&/\bgzip\b/i.test(String(a.headers["accept-encoding"]??"")))return{status:200,headers:{...p,"content-encoding":"gzip"},body:s.gzipSync(e.body),noLength:!0}}return p["content-length"]=e.size,{status:200,headers:p,body:i?void 0:e.body}}async s3apply(e,s,a,i,o,c){const l=i.searchParams,d=l.get("uploadId")??"",h=e=>({status:200,body:Buffer.from(`<?xml version="1.0" encoding="UTF-8"?>${e}`)}),g=()=>h(`<Error><Code>${c?.code??"InternalError"}</Code><Message>We encountered an internal error. Please try again.</Message></Error>`);switch(e){case"create":{const e=this.createUpload(s,a.headers["content-type"]);return h(`<InitiateMultipartUploadResult><Bucket>${p(this.s3.bucket)}</Bucket><Key>${p(s)}</Key><UploadId>${e}</UploadId></InitiateMultipartUploadResult>`)}case"complete":{const e=this.liveUpload(d);if(!e||e.key!==s)return{status:404,error:"NoSuchUpload"};if("error200"===c?.action)return g();const r=[...o.toString("utf8").matchAll(/<Part>([\s\S]*?)<\/Part>/g)].map(e=>({n:Number(/<PartNumber>(\d+)<\/PartNumber>/.exec(e[1])?.[1]),etag:m(/<ETag>([^<]*)<\/ETag>/.exec(e[1])?.[1]??"")})),i=this.completeUpload(s,d,r,{truncate:"truncate"===c?.action,ifNoneMatch:"*"===a.headers["if-none-match"]});if(!i.ok)return{status:i.status,error:i.code};const u="wrongEtag"===c?.action?n(t.randomBytes(8))+"-1":i.object.etag;return h(`<CompleteMultipartUploadResult><Bucket>${p(this.s3.bucket)}</Bucket><Key>${p(s)}</Key><ETag>"${u}"</ETag></CompleteMultipartUploadResult>`)}case"abort":{const e=this.liveUpload(d);return e&&e.key===s?(this.uploads.delete(d),this.aborted.set(d,s),{status:204}):this.aborted.get(d)===s?{status:204}:{status:404,error:"NoSuchUpload"}}case"listParts":{const e=this.liveUpload(d);if(!e||e.key!==s)return{status:404,error:"NoSuchUpload"};const t=Math.min(1e3,Number(l.get("max-parts")??1e3)||1e3),a=Number(l.get("part-number-marker")??0),r=[...e.parts].sort((e,t)=>e[0]-t[0]).filter(([e])=>e>a),n=r.slice(0,t),i=r.length>t;return h(`<ListPartsResult><UploadId>${d}</UploadId>${n.map(([e,t])=>`<Part><PartNumber>${e}</PartNumber><ETag>"${t.etag}"</ETag><Size>${t.size}</Size></Part>`).join("")}<IsTruncated>${i}</IsTruncated>${i?`<NextPartNumberMarker>${n[n.length-1][0]}</NextPartNumberMarker>`:""}</ListPartsResult>`)}case"head":case"get":{const t=this.objects.get(s);return t?this.readAnswer(t,a,i,"head"===e,c):{status:404,error:"NoSuchKey"}}case"delete":return this.objects.delete(s),{status:204};case"copy":{const e=String(a.headers["x-amz-copy-source"]),t=`/${this.s3.bucket}/`;if(!e.startsWith(t))return{status:400,error:"InvalidArgument"};const r=e.slice(t.length).split("/").map(decodeURIComponent).join("/"),i=this.objects.get(r);if(!i)return{status:404,error:"NoSuchKey"};const o=a.headers["x-amz-copy-source-if-match"];if(void 0!==o&&o.replace(/"/g,"")!==i.etag)return{status:412,error:"PreconditionFailed"};const p=l.get("partNumber");if(null!==p&&(!/^\d+$/.test(p)||Number(p)<1||Number(p)>u))return{status:400,error:"InvalidArgument"};if("error200"===c?.action)return g();if(p){const e=this.liveUpload(d);if(!e||e.key!==s)return{status:404,error:"NoSuchUpload"};const t=/^bytes=(\d+)-(\d+)$/.exec(String(a.headers["x-amz-copy-source-range"]??"")),r=t?i.body.subarray(Number(t[1]),Number(t[2])+1):i.body,o=n(r);return e.parts.set(Number(p),{body:this.keepBodies?r:Buffer.alloc(0),etag:o,size:r.length}),h(`<CopyPartResult><ETag>"${o}"</ETag></CopyPartResult>`)}if("*"===a.headers["cf-copy-destination-if-none-match"]&&this.objects.has(s))return{status:412,error:"PreconditionFailed"};const m="REPLACE"===a.headers["x-amz-metadata-directive"];return this.objects.set(s,{body:i.body,etag:i.etag,size:i.size,contentType:m?a.headers["content-type"]:i.contentType,...this.stamp()}),this.log({kind:"copy",key:s,detail:r}),h(`<CopyObjectResult><ETag>"${i.etag}"</ETag></CopyObjectResult>`)}case"list":{const e=l.get("prefix")??"",t=Math.min(1e3,Number(l.get("max-keys")??1e3)||1e3),s=l.get("continuation-token")?Buffer.from(l.get("continuation-token"),"base64url").toString():"",a=[...this.objects.keys()].filter(t=>t.startsWith(e)&&t>s).sort(),r=a.slice(0,t),n=a.length>t;return h(`<ListBucketResult><Name>${p(this.s3.bucket)}</Name><Prefix>${p(e)}</Prefix><KeyCount>${r.length}</KeyCount>${r.map(e=>{const t=this.objects.get(e);return`<Contents><Key>${p(e)}</Key><LastModified>${new Date(t.lastModified??0).toISOString()}</LastModified><ETag>"${t.etag}"</ETag><Size>${t.size}</Size></Contents>`}).join("")}<IsTruncated>${n}</IsTruncated>${n?`<NextContinuationToken>${Buffer.from(r[r.length-1]).toString("base64url")}</NextContinuationToken>`:""}</ListBucketResult>`)}case"listUploads":{const e=l.get("prefix")??"",t=Math.min(1e3,Number(l.get("max-uploads")??1e3)||1e3),s=l.get("key-marker")??"",a=l.get("upload-id-marker")??"",r=[...this.uploads].filter(([t,s])=>s.key.startsWith(e)&&this.liveUpload(t)).map(([e,t])=>({id:e,key:t.key,created:t.created})).sort((e,t)=>e.key<t.key?-1:e.key>t.key?1:e.id<t.id?-1:1).filter(e=>!s||e.key>s||e.key===s&&""!==a&&e.id>a),n=r.slice(0,t),i=r.length>t,o=n[n.length-1];return h(`<ListMultipartUploadsResult>${n.map(e=>`<Upload><Key>${p(e.key)}</Key><UploadId>${e.id}</UploadId><Initiated>${new Date(e.created??0).toISOString()}</Initiated></Upload>`).join("")}<IsTruncated>${i}</IsTruncated>${i?`<NextKeyMarker>${p(o.key)}</NextKeyMarker><NextUploadIdMarker>${o.id}</NextUploadIdMarker>`:""}</ListMultipartUploadsResult>`)}case"deleteObjects":{const e=a.headers["content-md5"];if(void 0!==e&&r(o).toString("base64")!==e)return{status:400,error:"BadDigest"};const t=[...o.toString("utf8").matchAll(/<Object><Key>([^<]*)<\/Key><\/Object>/g)].map(e=>m(e[1]));if(t.length>1e3)return{status:400,error:"MalformedXML"};for(const e of t)this.objects.delete(e);return h("<DeleteResult></DeleteResult>")}default:return{status:405,error:"MethodNotAllowed"}}}}const x=["","x","123","-7","0","true","null",'{"a": 1}',"[1,2]",'"quoted"'," spaced ","line\nbreak","tab\there","日本語 ✓ 🎬","a".repeat(1e4)],$=(e,t)=>(a.equal(typeof e.ttl,"function","store has ttl"),e.ttl(t)),A=e=>(a.equal(typeof e.incr,"function","store has incr"),a.equal(typeof e.compareAndSet,"function","store has compareAndSet"),e);exports.FakeR2=S,exports.MIN_PART=c,exports.SDK_ENTRY="server/testing",exports.createFakeR2=async function(e={}){const t={bucket:e.bucket??"test-bucket",accessKeyId:e.accessKeyId??"AKIDFAKER2",secretAccessKey:e.secretAccessKey??"fake-r2-secret",region:e.region??"auto"},s=new S({s3:t});return await s.start(e.port??0),Object.assign(s,{config:{endpoint:s.endpoint,bucket:t.bucket,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey,region:t.region}})},exports.isTextLike=o,exports.keyValueStoreContract=function(){const e=[],t=(t,s)=>e.push({name:t,run:s});return t("values come back byte for byte (no JSON coercion)",async e=>{const{store:t}=await e.make();a.equal(await t.get("missing"),null);for(const[e,s]of x.entries())a.equal(await t.set(`k${e}`,s),!0),a.equal(await t.get(`k${e}`),s,`value ${JSON.stringify(s.slice(0,20))}`)}),t("keys are opaque strings",async e=>{const{store:t}=await e.make();for(const e of["a:b","p/x/../y","日本","with space","*","{tag}","a".repeat(500)])await t.set(e,e),a.equal(await t.get(e),e);a.equal(await t.get("p/y"),null)}),t("set overwrites; ifAbsent only writes a missing key",async e=>{const{store:t}=await e.make();a.equal(await t.set("k","1"),!0),a.equal(await t.set("k","2"),!0),a.equal(await t.get("k"),"2"),a.equal(await t.set("k","3",{ifAbsent:!0}),!1),a.equal(await t.get("k"),"2"),a.equal(await t.set("n","1",{ifAbsent:!0}),!0),a.equal(await t.get("n"),"1")}),t("del removes the key; deleting a missing key is fine",async e=>{const{store:t}=await e.make();await t.del("k"),await t.set("k","v"),await t.del("k"),a.equal(await t.get("k"),null),a.equal(await t.set("k","w",{ifAbsent:!0}),!0)}),t("ttl: null when missing, Infinity without expiry, the remaining ms with one",async e=>{const{store:t}=await e.make();a.equal(await $(t,"k"),null),await t.set("k","v"),a.equal(await $(t,"k"),1/0),await t.set("t","v",{ttlMs:6e4});const s=await $(t,"t");a.ok(s>5e4&&s<=6e4,`ttl ${s}`)}),t("a key past its TTL is gone for every operation",async e=>{const{store:t,advance:s}=await e.make(),r=e.shortTtlMs??200;await t.set("k","v",{ttlMs:r}),a.equal(await t.get("k"),"v"),await s(2*r+50),a.equal(await t.get("k"),null),a.equal(await $(t,"k"),null),await t.del("k"),a.equal(await t.set("k","w",{ifAbsent:!0}),!0)}),t("TTLs in seconds (the auth kit's unit): { ttlSeconds } on set, a number on incr / compareAndSet",async e=>{const t=A((await e.make()).store);await t.set("a","v",{ttlSeconds:60});const s=await t.ttl("a");a.ok(s>5e4&&s<=6e4,`ttl ${s}`),await t.incr("n",1,60),a.ok(await t.ttl("n")<=6e4&&await t.ttl("n")>5e4),a.equal(await t.compareAndSet("c",null,"x",60),!0),a.ok(await t.ttl("c")<=6e4&&await t.ttl("c")>5e4),await a.rejects(t.set("b","v",{ttlMs:1e3,ttlSeconds:1}),/not both/)}),t("a plain set clears an earlier TTL (as Redis SET does)",async e=>{const{store:t}=await e.make();await t.set("k","v",{ttlMs:6e4}),await t.set("k","w"),a.equal(await $(t,"k"),1/0)}),t("bad TTLs and non-string values are refused",async e=>{const{store:t}=await e.make();for(const e of[0,-1,1.5,NaN,1/0])await a.rejects(t.set("k","v",{ttlMs:e}),/ttlMs/);await a.rejects(t.set("k",1),/string/),a.equal(await t.get("k"),null)}),t("incr: counts from 0, TTL only when it creates the key, refuses non-integers",async e=>{const t=A((await e.make()).store);a.equal(await t.incr("c"),1),a.equal(await t.incr("c",5),6),a.equal(await t.incr("c",-10),-4),a.equal(await t.ttl("c"),1/0),a.equal(await t.incr("w",1,{ttlMs:6e4}),1);const s=await t.ttl("w");a.ok(s>0&&s<=6e4),await t.incr("w",1,{ttlMs:1e6}),a.ok(await t.ttl("w")<=6e4),await t.set("s","abc"),await a.rejects(t.incr("s")),await t.set("f","1.5"),await a.rejects(t.incr("f")),a.equal(await t.get("s"),"abc")}),t("incr refuses to leave the safe-integer range, and writes nothing then",async e=>{const t=A((await e.make()).store);await t.set("c","9007199254740990"),a.equal(await t.incr("c"),9007199254740991),await a.rejects(t.incr("c")),a.equal(await t.get("c"),"9007199254740991"),await t.set("d","-9007199254740990"),a.equal(await t.incr("d",-1),-9007199254740991),await a.rejects(t.incr("d",-1)),await a.rejects(t.incr("e",2**53)),a.equal(await t.get("e"),null)}),t("incr after expiry starts again (a new window)",async e=>{const{store:t,advance:s}=await e.make(),r=A(t),n=e.shortTtlMs??200;await r.incr("w",3,{ttlMs:n}),await s(2*n+50),a.equal(await r.incr("w",1,{ttlMs:n}),1)}),t("compareAndSet: from missing, from a value, never on a mismatch",async e=>{const t=A((await e.make()).store);a.equal(await t.compareAndSet("k","x","y"),!1),a.equal(await t.get("k"),null),a.equal(await t.compareAndSet("k",null,"a"),!0),a.equal(await t.compareAndSet("k",null,"b"),!1),a.equal(await t.compareAndSet("k","b","c"),!1),a.equal(await t.compareAndSet("k","a","c",{ttlMs:6e4}),!0),a.equal(await t.get("k"),"c"),a.ok(await t.ttl("k")<=6e4),a.equal(await t.compareAndSet("k","c","",{}),!0),a.equal(await t.get("k"),""),a.equal(await t.compareAndSet("k","","d"),!0,"the empty string is a value, not missing")}),t("atomic under concurrency: counters add up, exactly one ifAbsent / compareAndSet wins",async e=>{const t=A((await e.make()).store),s=await Promise.all(Array.from({length:40},()=>t.incr("c")));a.equal(await t.get("c"),String(40)),a.deepEqual([...s].sort((e,t)=>e-t),Array.from({length:40},(e,t)=>t+1));const r=await Promise.all(Array.from({length:40},(e,s)=>t.set("once",String(s),{ifAbsent:!0})));a.equal(r.filter(Boolean).length,1);const n=await Promise.all(Array.from({length:40},(e,s)=>t.compareAndSet("lock",null,String(s))));a.equal(n.filter(Boolean).length,1);let i=await t.get("lock");for(let e=0;e<10;e++){const e=await Promise.all(Array.from({length:8},(e,s)=>t.compareAndSet("lock",i,`${i}.${s}`)));a.equal(e.filter(Boolean).length,1),i=await t.get("lock")}}),t("random operation sequences match a model (no TTLs)",async e=>{const{store:t}=await e.make(),s=A(t),r=new Map;let n=12139;const i=e=>(n=1103515245*n+12345&2147483647)%e,o=["a","b","c","d"];for(let e=0;e<400;e++){const e=o[i(o.length)],t=String(i(5));switch(i(6)){case 0:a.equal(await s.get(e),r.get(e)??null);break;case 1:a.equal(await s.set(e,t),!0),r.set(e,t);break;case 2:{const n=!r.has(e);a.equal(await s.set(e,t,{ifAbsent:!0}),n),n&&r.set(e,t);break}case 3:await s.del(e),r.delete(e);break;case 4:{const t=r.get(e),n=void 0===t?0:Number(t);a.equal(await s.incr(e,2),n+2),r.set(e,String(n+2));break}case 5:{const n=i(2)?r.get(e)??null:"zz",o=(r.get(e)??null)===n;a.equal(await s.compareAndSet(e,n,t),o),o&&r.set(e,t);break}}}}),e},exports.md5hex=n,exports.multipartEtag=i;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import e from"node:http";import{createHash as t,randomBytes as s,createHmac as a}from"node:crypto";import{gzipSync as r}from"node:zlib";import n from"node:assert/strict";const i=e=>t("md5").update(e).digest(),o=e=>i(e).toString("hex");function c(e){return`${i(Buffer.concat(e.map(e=>Buffer.from(e.replace(/"/g,""),"hex")))).toString("hex")}-${e.length}`}const u=e=>!!e&&/^(text\/|application\/(json|xml|javascript|x-javascript|ld\+json|xhtml\+xml|rss\+xml|atom\+xml|manifest\+json|x-subrip)|image\/svg\+xml)/i.test(e),l=5242880,d=1e4,h=["InvalidArgument","Multiple ranges are not supported: send one byte range per request, as bytes=first-last, bytes=first- or bytes=-n"],g=globalThis.setTimeout,p=e=>new Promise(t=>g(t,e)),m=e=>encodeURIComponent(e).replace(/[!'()*]/g,e=>`%${e.charCodeAt(0).toString(16).toUpperCase()}`),f=e=>e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'"),y=e=>e.replace(/"/g,'"').replace(/'/g,"'").replace(/</g,"<").replace(/>/g,">").replace(/&/g,"&"),k=e=>t("sha256").update(e).digest("hex"),w=new Map;function b(e,t){let s=w.get(e);if(!s){s=new Uint32Array(256);for(let t=0;t<256;t++){let a=t;for(let t=0;t<8;t++)a=1&a?a>>>1^e:a>>>1;s[t]=a>>>0}w.set(e,s)}let a=-1>>>0;for(let e=0;e<t.length;e++)a=s[255&(a^t[e])]^a>>>8;const r=Buffer.alloc(4);return r.writeUInt32BE(~a>>>0),r.toString("base64")}const S={crc32:e=>b(3988292384,e),crc32c:e=>b(2197175160,e),sha1:e=>t("sha1").update(e).digest("base64"),sha256:e=>t("sha256").update(e).digest("base64")};const $=(e,t)=>e.split(",").some(e=>{const s=e.trim();return"*"===s||s.replace(/^W\//,"").replace(/"/g,"")===t});class x{objects=new Map;uploads=new Map;events=[];puts=[];counters={requests:0,faults:0,rejectedSignature:0,partPuts:0,objectPuts:0,s3Calls:0};offline=!1;refused=new Map;scale=1;weakEtags=!0;enforceSignatures=!0;keepBodies=!0;publicBase="";tokenMount="/s3/";presignTtlMs=36e5;now=()=>Date.now();gzipText=!0;allowChunkedPuts=!1;expiryAfterBody=!1;uploadMaxAgeMs=0;aborted=new Map;s3;server;sockets=new Set;faults=[];gates=new Map;nextFaultId=1;presigned=new Map;base="";port=0;constructor(e={}){this.s3=e.s3}async start(t=0,s="127.0.0.1"){return this.server=e.createServer((e,t)=>{this.handle(e,t)}),this.server.keepAliveTimeout=5e3,this.server.requestTimeout=0,this.server.headersTimeout=0,this.server.on("connection",e=>{this.sockets.add(e),e.on("close",()=>this.sockets.delete(e))}),await new Promise(e=>this.server.listen(t,s,()=>e())),this.port=this.server.address().port,this.base=`http://${s}:${this.port}`,this}async close(){for(const e of this.sockets)e.destroy();await new Promise(e=>this.server?this.server.close(()=>e()):e())}get endpoint(){return this.base}fault(e){let t;const s=new Promise(e=>t=e),a={...e,id:this.nextFaultId++,hits:0,seen:0,remove:()=>{this.faults=this.faults.filter(e=>e!==a)},release:()=>t()};return this.gates.set(a,s),this.faults.push(a),a}clearFaults(){this.faults=[]}outage(e){if(this.offline=e,this.log({kind:e?"outage-start":"outage-end"}),e)for(const e of this.sockets)e.destroy()}refusedAttempts(e,t){return this.refused.get(`${e}#${t}`)??0}abortUpload(e){this.uploads.delete(e)}liveUpload(e){if(!e)return;const t=this.uploads.get(e);if(t&&this.uploadMaxAgeMs>0)if(void 0===t.created)t.created=this.now();else if(this.now()-t.created>this.uploadMaxAgeMs)return this.uploads.delete(e),void this.log({kind:"lifecycle-abort",key:t.key,detail:e});return t}targetMatches(e,t){return e===t||"storage"===e&&("partPut"===t||"objectPut"===t)||"s3:*"===e&&t.startsWith("s3:")}pick(e){for(const t of this.faults){if((Array.isArray(t.target)?t.target:[t.target]).some(t=>this.targetMatches(t,e.target))&&((!t.match||t.match(e))&&(t.seen++,!(t.after&&t.seen<=t.after||void 0!==t.times&&t.hits>=t.times))))return t.hits++,this.counters.faults++,this.log({kind:"fault",target:e.target,key:e.key,n:e.partNumber,fault:t.action,status:t.status}),t}return null}log(e){this.events.push({t:Date.now(),...e}),this.events.length>5e4&&this.events.splice(0,1e4)}object(e){return this.objects.get(e)}sha256(e){const s=this.objects.get(e);return s?t("sha256").update(s.body).digest("hex"):null}headInfo(e){const t=this.objects.get(e);if(!t)return{size:null,etag:null};const s=this.weakEtags&&u(t.contentType)&&t.size>1024;return{size:t.size,etag:s?`W/${t.etag}`:t.etag}}partPutsOf(e,t){return this.puts.filter(s=>"partPut"===s.target&&s.uploadId===e&&(void 0===t||s.n===t)&&200===s.outcome)}stamp(){return this.s3?{lastModified:this.now()}:{}}seed(e,t,s,a){const r=Buffer.isBuffer(t)?t:Buffer.from(t);this.objects.set(e,{body:r,etag:o(r),size:r.length,contentType:s,meta:a,lastModified:this.now()})}presign(e,t,a={}){const r=s(12).toString("hex"),n=Date.now()+this.presignTtlMs*this.scale;this.presigned.set(r,{key:e,headers:t,expiresAt:n,...a});const i=this.publicBase||this.base,o=new URLSearchParams;return void 0!==a.partNumber&&o.set("partNumber",String(a.partNumber)),void 0!==a.uploadId&&o.set("uploadId",a.uploadId),o.set("X-Uf-Signature",r),{url:`${i}${this.tokenMount}${encodeURIComponent(e)}?${o}`,expiresAt:new Date(Date.now()+this.presignTtlMs).toISOString()}}presignGet(e,t=3e5){const a=s(12).toString("hex");return this.presigned.set(a,{key:e,headers:{},expiresAt:Date.now()+t*this.scale,get:!0}),`${this.publicBase||this.base}${this.tokenMount}${encodeURIComponent(e)}?X-Uf-Signature=${a}`}expireToken(e,t){const s=this.presigned.get(new URL(e).searchParams.get("X-Uf-Signature")??"");s&&(s.expiresAt=t)}async handle(e,t){this.counters.requests++;const s=new URL(e.url,this.base);if(this.offline)this.refuse(e,s);else try{return s.pathname.startsWith(this.tokenMount)?await this.storage(e,t,s):this.s3&&(s.pathname===`/${this.s3.bucket}`||s.pathname.startsWith(`/${this.s3.bucket}/`))?await this.s3api(e,t,s):await this.route(e,t,s)}catch(s){this.log({kind:"error",detail:String(s?.stack??s)}),t.headersSent?e.socket.destroy():this.json(t,500,{error:String(s)})}}async route(e,t,s){await this.body(e).catch(()=>null),this.json(t,404,{error:"not found"})}bump(e){this.refused.set(e,(this.refused.get(e)??0)+1)}async refuse(e,t){if(t.pathname.startsWith(this.tokenMount)){const e=decodeURIComponent(t.pathname.slice(this.tokenMount.length)),s=t.searchParams.get("partNumber");this.bump(s?`${e}#${s}`:`${e}#put`)}else this.bump(t.pathname);e.socket.destroy()}json(e,t,s){const a=JSON.stringify(s);e.writeHead(t,{"content-type":"application/json","content-length":Buffer.byteLength(a)}),e.end(a)}s3err(e,t,s,a=s){if("HEAD"===e.req?.method)return e.writeHead(t,{"content-type":"application/xml"}),void e.end();const r=`<?xml version="1.0" encoding="UTF-8"?><Error><Code>${s}</Code><Message>${a}</Message></Error>`;e.writeHead(t,{"content-type":"application/xml","content-length":Buffer.byteLength(r)}),e.end(r)}xml(e,t,s,a={}){e.writeHead(t,{"content-type":"application/xml","content-length":Buffer.byteLength(s),...a}),e.end(s)}body(e){const t=e.__trickleMs;return new Promise(t?(s,a)=>{const r=[];e.pause(),e.on("end",()=>s(Buffer.concat(r))),e.on("error",a),e.on("aborted",()=>a(new Error("aborted")));const n=()=>{if(e.destroyed)return;let s,a=0;for(;a<65536&&null!==(s=e.read());)r.push(s),a+=s.length;e.readableEnded||g(n,t)};n()}:(t,s)=>{const a=[];e.on("data",e=>a.push(e)),e.on("end",()=>t(Buffer.concat(a))),e.on("error",s),e.on("aborted",()=>s(new Error("aborted")))})}async pre(e,t,s,a){if(!e)return"apply";switch(e.action){case"drop":return t.socket.destroy(),"handled";case"reset":{const e=Number(t.headers["content-length"]||0),s=Math.floor(e/2);let a=0;return t.on("data",e=>{a+=e.length,a>=s&&t.socket.destroy()}),0===e&&t.socket.destroy(),"handled"}case"status":{await this.body(t).catch(()=>null);const r=e.status??503;return a?this.s3err(s,r,e.code??(503===r?"ServiceUnavailable":500===r?"InternalError":"Injected")):this.json(s,r,{error:"injected",...e.code?{code:e.code}:{}}),"handled"}case"delay":return await p(e.ms??1e3),"apply";case"trickle":return t.__trickleMs=e.ms??50,"apply";case"stall":return t.pause(),e.ms?(await p(e.ms),t.resume(),"apply"):"handled";default:return"apply"}}async post(e,t){if(!e)return!1;if("dropResponse"===e.action)return t.socket.destroy(),!0;if("gate"===e.action&&await this.gates.get(e),"holdResponse"===e.action){if(!e.ms)return!0;await p(e.ms)}return!1}checkSignature(e,t,s,a,r=null!==a){if(!this.enforceSignatures)return null;const n=t.searchParams.get("X-Uf-Signature")??"",i=this.presigned.get(n),o=t.searchParams.get("partNumber"),c=t.searchParams.get("uploadId"),u=e=>({status:403,code:"SignatureDoesNotMatch",message:e});if(!i)return u("no such signature");if(i.key!==s||String(i.partNumber??"")!==(o??"")||(i.uploadId??"")!==(c??""))return u("URL does not match its signature");if((!r||this.expiryAfterBody)&&Date.now()>i.expiresAt)return{status:403,code:"ExpiredRequest",message:"Request has expired"};for(const[t,s]of Object.entries(i.headers)){const r=e.headers[t];if("content-length"!==t){if(void 0===r)return u(`signed header ${t} missing`);if(String(r)!==s)return u(`signed header ${t} changed`)}else if(null!==a&&String(a)!==s)return u(`signed content-length ${s}, body ${a}`)}return null}putTarget(e,t){return t?"partPut":"objectPut"}async storage(e,t,s){const a=decodeURIComponent(s.pathname.slice(this.tokenMount.length)),r=s.searchParams.get("partNumber"),n=s.searchParams.get("uploadId")??void 0;if("GET"===e.method){const r=this.pick({target:"storage:get",key:a}),n=this.presigned.get(s.searchParams.get("X-Uf-Signature")??"");if(this.enforceSignatures&&(!n||!n.get||n.key!==a))return this.s3err(t,403,"SignatureDoesNotMatch");if(this.enforceSignatures&&Date.now()>n.expiresAt)return this.s3err(t,403,"ExpiredRequest","Request has expired");if(!this.midBodyFault(r)&&"handled"===await this.pre(r,e,t,!0))return;const i=this.objects.get(a),o=i?this.readAnswer(i,e,s,!1,r):{status:404,error:"NoSuchKey"};if(await this.post(r,e))return;return o.error?this.s3err(t,o.status,o.error,o.message):this.sendRead(e,t,o,r)}if("PUT"!==e.method)return this.s3err(t,405,"MethodNotAllowed");const i=this.putTarget(a,r);return this.storePut(e,t,{key:a,pn:r,uploadId:n,target:i,check:t=>this.checkSignature(e,s,a,t),expire:()=>{const e=this.presigned.get(s.searchParams.get("X-Uf-Signature")??"");e&&(e.expiresAt=0)}})}async storePut(e,t,s){const{key:a,pn:r,uploadId:n,target:c}=s,u={t:Date.now(),target:c,key:a,n:r?Number(r):void 0,uploadId:n,outcome:"pending",bytes:0,md5:!!e.headers["content-md5"]};this.puts.push(u);const l=e.push.bind(e);e.push=(e,t)=>(e&&(u.bytes+=e.length),l(e,t)),t.on("close",()=>{"pending"===u.outcome&&(u.outcome="closed",u.clientClosedAt=Date.now())});const h=e=>(u.outcome=e,u.doneAt=Date.now()),g=this.pick({target:c,key:a,partNumber:u.n,uploadId:n});"expire"===g?.action&&s.expire();const p=s.check(null);if(p)return this.counters.rejectedSignature++,await this.body(e).catch(()=>null),h(p.status),this.s3err(t,p.status,p.code,p.message);const m=!this.allowChunkedPuts&&void 0===e.headers["content-length"]&&/chunked/i.test(String(e.headers["transfer-encoding"]??""))?[411,"MissingContentLength"]:null!==r&&(!/^\d+$/.test(r)||Number(r)<1||Number(r)>d)?[400,"InvalidArgument"]:null;if(m)return await this.body(e).catch(()=>null),h(m[0]),this.s3err(t,m[0],m[1]);if("noSuchUpload"===g?.action&&n&&this.uploads.delete(n),!g||"drop"!==g.action&&"reset"!==g.action||(u.outcome="dropped"),"handled"===await this.pre(g,e,t,!0))return void(t.headersSent&&h(t.statusCode));let f=await this.body(e);if(this.offline)return void e.socket.destroy();"corruptInTransit"===g?.action&&f.length&&(f=Buffer.from(f),f[f.length>>1]^=255);const y=s.check(f.length,f);if(y)return this.counters.rejectedSignature++,h(y.status),this.s3err(t,y.status,y.code,y.message);const k=e.headers["content-md5"];if(k&&i(f).toString("base64")!==k)return h(400),this.s3err(t,400,"BadDigest");const w=function(e,t){const s={};for(const[a,r]of Object.entries(S)){const n=e.headers[`x-amz-checksum-${a}`];if("string"==typeof n){if(r(t)!==n.trim())return null;s[a]=n.trim()}}return s}(e,f);if(!w)return h(400),this.s3err(t,400,"BadDigest");const b=Object.keys(w).length?{checksums:w}:{};"storeCorrupted"===g?.action&&f.length&&(f=Buffer.from(f),f[0]^=255);const $=o(f);if(this.counters[`${c}s`]=(this.counters[`${c}s`]??0)+1,r){const e=this.liveUpload(n);if(!e||e.key!==a)return h(404),this.s3err(t,404,"NoSuchUpload");e.parts.set(Number(r),{body:this.keepBodies?f:Buffer.alloc(0),etag:$,size:f.length,...b}),this.log({kind:"partPut",key:a,n:Number(r)})}else{if("*"===e.headers["if-none-match"]&&this.objects.has(a))return h(412),this.s3err(t,412,"PreconditionFailed");const r={};if(s.s3){const s=e.headers["if-match"];if(void 0!==s&&`"${this.objects.get(a)?.etag}"`!==s.replace(/^W\//,""))return h(412),this.s3err(t,412,"PreconditionFailed");for(const[t,s]of Object.entries(e.headers))t.startsWith("x-amz-meta-")&&"string"==typeof s&&(r[t.slice(11)]=s)}this.objects.set(a,{body:f,etag:$,size:f.length,contentType:e.headers["content-type"],...Object.keys(r).length?{meta:r}:{},...b,...this.stamp()}),this.log({kind:c,key:a})}await this.post(g,e)?u.outcome="dropped":(h(200),t.writeHead(200,"noEtag"===g?.action?{"content-length":0}:{etag:`"${$}"`,"content-length":0}),t.end())}createUpload(e,t){const a=s(12).toString("hex");return this.uploads.set(a,{key:e,contentType:t,parts:new Map,created:this.now()}),this.log({kind:"create",key:e,detail:a}),a}completeUpload(e,t,s,a={}){const r=this.liveUpload(t);if(!r||r.key!==e)return{ok:!1,code:"NoSuchUpload",status:404};if(!s.length||s.length>d)return{ok:!1,code:"MalformedXML",status:400};const n=[...s].map(e=>({n:Number(e.n),etag:String(e.etag)})).sort((e,t)=>e.n-t.n);for(let e=1;e<n.length;e++)if(n[e].n===n[e-1].n)return{ok:!1,code:"InvalidPartOrder",status:400};const i=[],o=[];let u=0,h=-1;for(let e=0;e<n.length;e++){const t=n[e],s=r.parts.get(t.n);if(!s||s.etag!==t.etag.replace(/"/g,"").toLowerCase())return{ok:!1,code:"InvalidPart",status:400};if(e<n.length-1){if(s.size<l)return{ok:!1,code:"EntityTooSmall",status:400};if(-1===h)h=s.size;else if(s.size!==h)return{ok:!1,code:"InvalidPart",status:400}}else if(-1!==h&&s.size>h)return{ok:!1,code:"InvalidPart",status:400};i.push(s.body),o.push(s.etag),u+=s.size}if(a.ifNoneMatch&&this.objects.has(e))return this.uploads.delete(t),{ok:!1,code:"PreconditionFailed",status:412};const g=c(o);let p=Buffer.concat(i);a.truncate&&(p=p.subarray(0,Math.max(0,p.length-1)),u=Math.max(0,u-1));const m={body:p,etag:g,size:u,contentType:r.contentType,...this.stamp()};return this.objects.set(e,m),this.uploads.delete(t),this.log({kind:"complete",key:e,detail:`parts=${n.length}`}),{ok:!0,object:m}}checkSigV4(e,t,s,r){const n=null===s||this.expiryAfterBody;if(!this.enforceSignatures||!this.s3)return null;const i=e=>({status:403,code:"SignatureDoesNotMatch",message:e}),o=t.searchParams,c=null!==o.get("X-Amz-Algorithm");let u,l,d,h,g;if(c){if("AWS4-HMAC-SHA256"!==o.get("X-Amz-Algorithm"))return i("algorithm");u=o.get("X-Amz-Credential")??"",l=o.get("X-Amz-SignedHeaders")??"",d=o.get("X-Amz-Signature")??"",h=o.get("X-Amz-Date")??"",g="UNSIGNED-PAYLOAD";const e=Number(o.get("X-Amz-Expires"));if(!Number.isInteger(e)||e<1||e>604800)return{status:400,code:"AuthorizationQueryParametersError",message:"X-Amz-Expires"};const t=Date.parse(`${h.slice(0,4)}-${h.slice(4,6)}-${h.slice(6,8)}T${h.slice(9,11)}:${h.slice(11,13)}:${h.slice(13,15)}Z`);if(!Number.isFinite(t))return i("date");if(n&&this.now()>t+1e3*e*this.scale)return{status:403,code:"ExpiredRequest",message:"Request has expired"};if(n&&t>this.now()+9e5)return{status:403,code:"RequestTimeTooSkewed",message:"Request is not valid yet"}}else{const t=String(e.headers.authorization??""),s=/^AWS4-HMAC-SHA256 Credential=([^,]+), ?SignedHeaders=([^,]+), ?Signature=([0-9a-f]{64})$/.exec(t);if(!s)return{status:403,code:"AccessDenied",message:"no SigV4 Authorization header"};if([,u,l,d]=s,h=String(e.headers["x-amz-date"]??""),g=String(e.headers["x-amz-content-sha256"]??""),!g)return{status:400,code:"InvalidRequest",message:"x-amz-content-sha256 missing"};if(r&&"UNSIGNED-PAYLOAD"!==g&&k(r)!==g)return{status:400,code:"XAmzContentSHA256Mismatch",message:"payload hash"}}const[p,f,y,w,b]=u.split("/");if(p!==this.s3.accessKeyId)return{status:403,code:"InvalidAccessKeyId",message:"unknown access key"};if(y!==(this.s3.region??"auto")||"s3"!==w||"aws4_request"!==b||f!==h.slice(0,8))return i("credential scope");const S=l.split(";");if(!S.includes("host")||[...S].sort().join(";")!==l)return i("signed headers");for(const t of Object.keys(e.headers))if(t.startsWith("x-amz-")&&"x-amz-content-sha256"!==t&&!S.includes(t))return{status:403,code:"AccessDenied",message:`unsigned header ${t}`};const $=[];for(const t of S){if("content-length"===t&&null!==s&&String(s)!==String(e.headers["content-length"]??s))return i("content-length");const a=e.headers[t];if(void 0===a)return i(`signed header ${t} missing`);$.push(`${t}:${String(a).trim().replace(/\s+/g," ")}`)}const x=(e.url??"/").split("?")[0],A=((e.url??"").includes("?")?(e.url??"").slice((e.url??"").indexOf("?")+1):"").split("&").filter(Boolean).map(e=>{const t=e.indexOf("="),s=e=>decodeURIComponent(e.replace(/\+/g," "));return[s(t<0?e:e.slice(0,t)),s(t<0?"":e.slice(t+1))]}).filter(([e])=>"X-Amz-Signature"!==e).map(([e,t])=>[m(e),m(t)]).sort(([e,t],[s,a])=>e<s?-1:e>s?1:t<a?-1:t>a?1:0),v=[e.method,x,A.map(([e,t])=>`${e}=${t}`).join("&"),$.join("\n")+"\n",l,g].join("\n"),q=["AWS4-HMAC-SHA256",h,`${f}/${y}/s3/aws4_request`,k(v)].join("\n"),M=(e,t)=>a("sha256",e).update(t).digest(),T=M(M(M(M(`AWS4${this.s3.secretAccessKey}`,f),y),"s3"),"aws4_request");if(a("sha256",T).update(q).digest("hex")!==d)return i("signature");if(!c){const e=Date.parse(`${h.slice(0,4)}-${h.slice(4,6)}-${h.slice(6,8)}T${h.slice(9,11)}:${h.slice(11,13)}:${h.slice(13,15)}Z`);if(!Number.isFinite(e)||n&&Math.abs(this.now()-e)>9e5)return{status:403,code:"RequestTimeTooSkewed",message:"date"}}return null}s3Key(e){return e.pathname.slice(this.s3.bucket.length+2).split("/").map(e=>decodeURIComponent(e)).join("/")}etagHeader(e,t){const s=String(t.headers["accept-encoding"]??""),a=this.weakEtags&&u(e.contentType)&&e.size>1024&&""!==s&&"identity"!==s;return{weak:a,etag:`${a?"W/":""}"${e.etag}"`}}async s3api(e,t,s){this.counters.s3Calls++;const a=this.s3Key(s),r=s.searchParams,n=e.method??"GET",i=r.get("partNumber"),o=r.get("uploadId")??void 0;if("PUT"===n&&a&&!e.headers["x-amz-copy-source"]){const r=this.putTarget(a,i);let n=!1;return this.storePut(e,t,{key:a,pn:i,uploadId:o,target:r,check:(t,a)=>n&&this.enforceSignatures&&(null===t||this.expiryAfterBody)?{status:403,code:"ExpiredRequest",message:"Request has expired"}:this.checkSigV4(e,s,t,a),expire:()=>n=!0,s3:!0})}const c=a?"POST"===n&&r.has("delete")?"deleteObjects":"POST"===n&&r.has("uploads")?"create":"POST"===n&&o?"complete":"DELETE"===n&&o?"abort":"GET"===n&&o?"listParts":"HEAD"===n?"head":"GET"===n?"get":"DELETE"===n?"delete":"PUT"===n?"copy":"unknown":"GET"===n&&r.has("uploads")?"listUploads":"GET"===n?"list":"POST"===n&&r.has("delete")?"deleteObjects":"bucket",u=`s3:${c}`,l=this.pick({target:u,key:a,partNumber:i?Number(i):void 0,uploadId:o}),d=this.checkSigV4(e,s,null);if(d)return this.counters.rejectedSignature++,await this.body(e).catch(()=>null),this.s3err(t,d.status,d.code,d.message);if("noSuchUpload"===l?.action&&o&&this.uploads.delete(o),("get"!==c||!this.midBodyFault(l))&&"handled"===await this.pre(l,e,t,!0))return;const h=await this.body(e),g=this.checkSigV4(e,s,h.length,h);if(g)return this.counters.rejectedSignature++,this.s3err(t,g.status,g.code,g.message);const p=await this.s3apply(c,a,e,s,h,l);if(!await this.post(l,e)){if(p.error)return this.s3err(t,p.status,p.error,p.message);if("get"===c||"head"===c)return this.sendRead(e,t,p,l);t.writeHead(p.status,{"content-length":p.body?p.body.length:0,...p.body?{"content-type":"application/xml"}:{},...p.headers}),t.end("HEAD"===n?void 0:p.body)}}midBodyFault(e){return!!e&&("reset"===e.action||"stall"===e.action)}async sendRead(e,t,s,a){const r={...s.headers};s.noLength?delete r["content-length"]:void 0===r["content-length"]&&(r["content-length"]=s.body?s.body.length:0),t.writeHead(s.status,r);const n="HEAD"===e.method||304===s.status?void 0:s.body;if(!n||!this.midBodyFault(a))return void t.end(n);const i=n.subarray(0,n.length>>1);if("reset"===a.action)return i.length?void t.write(i,()=>e.socket.destroy()):void e.socket.destroy();t.write(i),a.ms&&(await p(a.ms),t.destroyed||t.end(n.subarray(i.length)))}readAnswer(e,t,a,n,i){const{weak:c,etag:u}=this.etagHeader(e,t),l="wrongEtag"===i?.action?`"${o(s(8))}"`:u,d=t.headers["if-match"];if(void 0!==d&&!$(d,e.etag))return{status:412,error:"PreconditionFailed"};const g=t.headers["if-none-match"];if(void 0!==g&&$(g,e.etag))return{status:304,headers:{etag:l},noLength:!0};const p={etag:l,"content-type":e.contentType??"application/octet-stream","accept-ranges":"bytes"};void 0!==e.lastModified&&(p["last-modified"]=new Date(e.lastModified).toUTCString());for(const[t,s]of Object.entries(e.meta??{}))p[`x-amz-meta-${t}`]=s;const m=a.searchParams;for(const[e,t]of[["response-content-disposition","content-disposition"],["response-content-type","content-type"],["response-cache-control","cache-control"],["response-content-language","content-language"],["response-expires","expires"]]){const s=m.get(e);null!==s&&/^[\t\x20-\x7e]*$/.test(s)&&(p[t]=s)}const f=function(e,t){if(!e)return null;const s=/^\s*bytes\s*=\s*(.+)$/i.exec(e);if(!s)return null;if(s[1].includes(","))return 400;const a=/^\s*(\d*)\s*-\s*(\d*)\s*$/.exec(s[1]);if(!a||""===a[1]&&""===a[2])return null;if(""===a[1]){const e=Number(a[2]);return 0===e||0===t?416:{start:Math.max(0,t-e),end:t-1}}const r=Number(a[1]),n=""===a[2]?t-1:Math.min(Number(a[2]),t-1);return""!==a[2]&&Number(a[2])<r?null:r>=t?416:{start:r,end:n}}(t.headers.range,e.size);if(416===f)return{status:416,error:"InvalidRange",message:"The requested range is not satisfiable"};if(400===f)return{status:400,error:h[0],message:h[1]};if("ENABLED"===String(t.headers["x-amz-checksum-mode"]??"").toUpperCase()&&!f)for(const[t,s]of Object.entries(e.checksums??{}))p[`x-amz-checksum-${t}`]=s;if(f){const t=e.body.subarray(f.start,f.end+1);return p["content-range"]=`bytes ${f.start}-${f.end}/${e.size}`,p["content-length"]=f.end-f.start+1,{status:206,headers:p,body:n?void 0:t}}if(c){if(n)return{status:200,headers:p,noLength:!0};if(this.gzipText&&/\bgzip\b/i.test(String(t.headers["accept-encoding"]??"")))return{status:200,headers:{...p,"content-encoding":"gzip"},body:r(e.body),noLength:!0}}return p["content-length"]=e.size,{status:200,headers:p,body:n?void 0:e.body}}async s3apply(e,t,a,r,n,c){const u=r.searchParams,l=u.get("uploadId")??"",h=e=>({status:200,body:Buffer.from(`<?xml version="1.0" encoding="UTF-8"?>${e}`)}),g=()=>h(`<Error><Code>${c?.code??"InternalError"}</Code><Message>We encountered an internal error. Please try again.</Message></Error>`);switch(e){case"create":{const e=this.createUpload(t,a.headers["content-type"]);return h(`<InitiateMultipartUploadResult><Bucket>${f(this.s3.bucket)}</Bucket><Key>${f(t)}</Key><UploadId>${e}</UploadId></InitiateMultipartUploadResult>`)}case"complete":{const e=this.liveUpload(l);if(!e||e.key!==t)return{status:404,error:"NoSuchUpload"};if("error200"===c?.action)return g();const r=[...n.toString("utf8").matchAll(/<Part>([\s\S]*?)<\/Part>/g)].map(e=>({n:Number(/<PartNumber>(\d+)<\/PartNumber>/.exec(e[1])?.[1]),etag:y(/<ETag>([^<]*)<\/ETag>/.exec(e[1])?.[1]??"")})),i=this.completeUpload(t,l,r,{truncate:"truncate"===c?.action,ifNoneMatch:"*"===a.headers["if-none-match"]});if(!i.ok)return{status:i.status,error:i.code};const u="wrongEtag"===c?.action?o(s(8))+"-1":i.object.etag;return h(`<CompleteMultipartUploadResult><Bucket>${f(this.s3.bucket)}</Bucket><Key>${f(t)}</Key><ETag>"${u}"</ETag></CompleteMultipartUploadResult>`)}case"abort":{const e=this.liveUpload(l);return e&&e.key===t?(this.uploads.delete(l),this.aborted.set(l,t),{status:204}):this.aborted.get(l)===t?{status:204}:{status:404,error:"NoSuchUpload"}}case"listParts":{const e=this.liveUpload(l);if(!e||e.key!==t)return{status:404,error:"NoSuchUpload"};const s=Math.min(1e3,Number(u.get("max-parts")??1e3)||1e3),a=Number(u.get("part-number-marker")??0),r=[...e.parts].sort((e,t)=>e[0]-t[0]).filter(([e])=>e>a),n=r.slice(0,s),i=r.length>s;return h(`<ListPartsResult><UploadId>${l}</UploadId>${n.map(([e,t])=>`<Part><PartNumber>${e}</PartNumber><ETag>"${t.etag}"</ETag><Size>${t.size}</Size></Part>`).join("")}<IsTruncated>${i}</IsTruncated>${i?`<NextPartNumberMarker>${n[n.length-1][0]}</NextPartNumberMarker>`:""}</ListPartsResult>`)}case"head":case"get":{const s=this.objects.get(t);return s?this.readAnswer(s,a,r,"head"===e,c):{status:404,error:"NoSuchKey"}}case"delete":return this.objects.delete(t),{status:204};case"copy":{const e=String(a.headers["x-amz-copy-source"]),s=`/${this.s3.bucket}/`;if(!e.startsWith(s))return{status:400,error:"InvalidArgument"};const r=e.slice(s.length).split("/").map(decodeURIComponent).join("/"),n=this.objects.get(r);if(!n)return{status:404,error:"NoSuchKey"};const i=a.headers["x-amz-copy-source-if-match"];if(void 0!==i&&i.replace(/"/g,"")!==n.etag)return{status:412,error:"PreconditionFailed"};const p=u.get("partNumber");if(null!==p&&(!/^\d+$/.test(p)||Number(p)<1||Number(p)>d))return{status:400,error:"InvalidArgument"};if("error200"===c?.action)return g();if(p){const e=this.liveUpload(l);if(!e||e.key!==t)return{status:404,error:"NoSuchUpload"};const s=/^bytes=(\d+)-(\d+)$/.exec(String(a.headers["x-amz-copy-source-range"]??"")),r=s?n.body.subarray(Number(s[1]),Number(s[2])+1):n.body,i=o(r);return e.parts.set(Number(p),{body:this.keepBodies?r:Buffer.alloc(0),etag:i,size:r.length}),h(`<CopyPartResult><ETag>"${i}"</ETag></CopyPartResult>`)}if("*"===a.headers["cf-copy-destination-if-none-match"]&&this.objects.has(t))return{status:412,error:"PreconditionFailed"};const m="REPLACE"===a.headers["x-amz-metadata-directive"];return this.objects.set(t,{body:n.body,etag:n.etag,size:n.size,contentType:m?a.headers["content-type"]:n.contentType,...this.stamp()}),this.log({kind:"copy",key:t,detail:r}),h(`<CopyObjectResult><ETag>"${n.etag}"</ETag></CopyObjectResult>`)}case"list":{const e=u.get("prefix")??"",t=Math.min(1e3,Number(u.get("max-keys")??1e3)||1e3),s=u.get("continuation-token")?Buffer.from(u.get("continuation-token"),"base64url").toString():"",a=[...this.objects.keys()].filter(t=>t.startsWith(e)&&t>s).sort(),r=a.slice(0,t),n=a.length>t;return h(`<ListBucketResult><Name>${f(this.s3.bucket)}</Name><Prefix>${f(e)}</Prefix><KeyCount>${r.length}</KeyCount>${r.map(e=>{const t=this.objects.get(e);return`<Contents><Key>${f(e)}</Key><LastModified>${new Date(t.lastModified??0).toISOString()}</LastModified><ETag>"${t.etag}"</ETag><Size>${t.size}</Size></Contents>`}).join("")}<IsTruncated>${n}</IsTruncated>${n?`<NextContinuationToken>${Buffer.from(r[r.length-1]).toString("base64url")}</NextContinuationToken>`:""}</ListBucketResult>`)}case"listUploads":{const e=u.get("prefix")??"",t=Math.min(1e3,Number(u.get("max-uploads")??1e3)||1e3),s=u.get("key-marker")??"",a=u.get("upload-id-marker")??"",r=[...this.uploads].filter(([t,s])=>s.key.startsWith(e)&&this.liveUpload(t)).map(([e,t])=>({id:e,key:t.key,created:t.created})).sort((e,t)=>e.key<t.key?-1:e.key>t.key?1:e.id<t.id?-1:1).filter(e=>!s||e.key>s||e.key===s&&""!==a&&e.id>a),n=r.slice(0,t),i=r.length>t,o=n[n.length-1];return h(`<ListMultipartUploadsResult>${n.map(e=>`<Upload><Key>${f(e.key)}</Key><UploadId>${e.id}</UploadId><Initiated>${new Date(e.created??0).toISOString()}</Initiated></Upload>`).join("")}<IsTruncated>${i}</IsTruncated>${i?`<NextKeyMarker>${f(o.key)}</NextKeyMarker><NextUploadIdMarker>${o.id}</NextUploadIdMarker>`:""}</ListMultipartUploadsResult>`)}case"deleteObjects":{const e=a.headers["content-md5"];if(void 0!==e&&i(n).toString("base64")!==e)return{status:400,error:"BadDigest"};const t=[...n.toString("utf8").matchAll(/<Object><Key>([^<]*)<\/Key><\/Object>/g)].map(e=>y(e[1]));if(t.length>1e3)return{status:400,error:"MalformedXML"};for(const e of t)this.objects.delete(e);return h("<DeleteResult></DeleteResult>")}default:return{status:405,error:"MethodNotAllowed"}}}}async function A(e={}){const t={bucket:e.bucket??"test-bucket",accessKeyId:e.accessKeyId??"AKIDFAKER2",secretAccessKey:e.secretAccessKey??"fake-r2-secret",region:e.region??"auto"},s=new x({s3:t});return await s.start(e.port??0),Object.assign(s,{config:{endpoint:s.endpoint,bucket:t.bucket,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey,region:t.region}})}const v=["","x","123","-7","0","true","null",'{"a": 1}',"[1,2]",'"quoted"'," spaced ","line\nbreak","tab\there","日本語 ✓ 🎬","a".repeat(1e4)],q=(e,t)=>(n.equal(typeof e.ttl,"function","store has ttl"),e.ttl(t)),M=e=>(n.equal(typeof e.incr,"function","store has incr"),n.equal(typeof e.compareAndSet,"function","store has compareAndSet"),e);function T(){const e=[],t=(t,s)=>e.push({name:t,run:s});return t("values come back byte for byte (no JSON coercion)",async e=>{const{store:t}=await e.make();n.equal(await t.get("missing"),null);for(const[e,s]of v.entries())n.equal(await t.set(`k${e}`,s),!0),n.equal(await t.get(`k${e}`),s,`value ${JSON.stringify(s.slice(0,20))}`)}),t("keys are opaque strings",async e=>{const{store:t}=await e.make();for(const e of["a:b","p/x/../y","日本","with space","*","{tag}","a".repeat(500)])await t.set(e,e),n.equal(await t.get(e),e);n.equal(await t.get("p/y"),null)}),t("set overwrites; ifAbsent only writes a missing key",async e=>{const{store:t}=await e.make();n.equal(await t.set("k","1"),!0),n.equal(await t.set("k","2"),!0),n.equal(await t.get("k"),"2"),n.equal(await t.set("k","3",{ifAbsent:!0}),!1),n.equal(await t.get("k"),"2"),n.equal(await t.set("n","1",{ifAbsent:!0}),!0),n.equal(await t.get("n"),"1")}),t("del removes the key; deleting a missing key is fine",async e=>{const{store:t}=await e.make();await t.del("k"),await t.set("k","v"),await t.del("k"),n.equal(await t.get("k"),null),n.equal(await t.set("k","w",{ifAbsent:!0}),!0)}),t("ttl: null when missing, Infinity without expiry, the remaining ms with one",async e=>{const{store:t}=await e.make();n.equal(await q(t,"k"),null),await t.set("k","v"),n.equal(await q(t,"k"),1/0),await t.set("t","v",{ttlMs:6e4});const s=await q(t,"t");n.ok(s>5e4&&s<=6e4,`ttl ${s}`)}),t("a key past its TTL is gone for every operation",async e=>{const{store:t,advance:s}=await e.make(),a=e.shortTtlMs??200;await t.set("k","v",{ttlMs:a}),n.equal(await t.get("k"),"v"),await s(2*a+50),n.equal(await t.get("k"),null),n.equal(await q(t,"k"),null),await t.del("k"),n.equal(await t.set("k","w",{ifAbsent:!0}),!0)}),t("TTLs in seconds (the auth kit's unit): { ttlSeconds } on set, a number on incr / compareAndSet",async e=>{const t=M((await e.make()).store);await t.set("a","v",{ttlSeconds:60});const s=await t.ttl("a");n.ok(s>5e4&&s<=6e4,`ttl ${s}`),await t.incr("n",1,60),n.ok(await t.ttl("n")<=6e4&&await t.ttl("n")>5e4),n.equal(await t.compareAndSet("c",null,"x",60),!0),n.ok(await t.ttl("c")<=6e4&&await t.ttl("c")>5e4),await n.rejects(t.set("b","v",{ttlMs:1e3,ttlSeconds:1}),/not both/)}),t("a plain set clears an earlier TTL (as Redis SET does)",async e=>{const{store:t}=await e.make();await t.set("k","v",{ttlMs:6e4}),await t.set("k","w"),n.equal(await q(t,"k"),1/0)}),t("bad TTLs and non-string values are refused",async e=>{const{store:t}=await e.make();for(const e of[0,-1,1.5,NaN,1/0])await n.rejects(t.set("k","v",{ttlMs:e}),/ttlMs/);await n.rejects(t.set("k",1),/string/),n.equal(await t.get("k"),null)}),t("incr: counts from 0, TTL only when it creates the key, refuses non-integers",async e=>{const t=M((await e.make()).store);n.equal(await t.incr("c"),1),n.equal(await t.incr("c",5),6),n.equal(await t.incr("c",-10),-4),n.equal(await t.ttl("c"),1/0),n.equal(await t.incr("w",1,{ttlMs:6e4}),1);const s=await t.ttl("w");n.ok(s>0&&s<=6e4),await t.incr("w",1,{ttlMs:1e6}),n.ok(await t.ttl("w")<=6e4),await t.set("s","abc"),await n.rejects(t.incr("s")),await t.set("f","1.5"),await n.rejects(t.incr("f")),n.equal(await t.get("s"),"abc")}),t("incr refuses to leave the safe-integer range, and writes nothing then",async e=>{const t=M((await e.make()).store);await t.set("c","9007199254740990"),n.equal(await t.incr("c"),9007199254740991),await n.rejects(t.incr("c")),n.equal(await t.get("c"),"9007199254740991"),await t.set("d","-9007199254740990"),n.equal(await t.incr("d",-1),-9007199254740991),await n.rejects(t.incr("d",-1)),await n.rejects(t.incr("e",2**53)),n.equal(await t.get("e"),null)}),t("incr after expiry starts again (a new window)",async e=>{const{store:t,advance:s}=await e.make(),a=M(t),r=e.shortTtlMs??200;await a.incr("w",3,{ttlMs:r}),await s(2*r+50),n.equal(await a.incr("w",1,{ttlMs:r}),1)}),t("compareAndSet: from missing, from a value, never on a mismatch",async e=>{const t=M((await e.make()).store);n.equal(await t.compareAndSet("k","x","y"),!1),n.equal(await t.get("k"),null),n.equal(await t.compareAndSet("k",null,"a"),!0),n.equal(await t.compareAndSet("k",null,"b"),!1),n.equal(await t.compareAndSet("k","b","c"),!1),n.equal(await t.compareAndSet("k","a","c",{ttlMs:6e4}),!0),n.equal(await t.get("k"),"c"),n.ok(await t.ttl("k")<=6e4),n.equal(await t.compareAndSet("k","c","",{}),!0),n.equal(await t.get("k"),""),n.equal(await t.compareAndSet("k","","d"),!0,"the empty string is a value, not missing")}),t("atomic under concurrency: counters add up, exactly one ifAbsent / compareAndSet wins",async e=>{const t=M((await e.make()).store),s=await Promise.all(Array.from({length:40},()=>t.incr("c")));n.equal(await t.get("c"),String(40)),n.deepEqual([...s].sort((e,t)=>e-t),Array.from({length:40},(e,t)=>t+1));const a=await Promise.all(Array.from({length:40},(e,s)=>t.set("once",String(s),{ifAbsent:!0})));n.equal(a.filter(Boolean).length,1);const r=await Promise.all(Array.from({length:40},(e,s)=>t.compareAndSet("lock",null,String(s))));n.equal(r.filter(Boolean).length,1);let i=await t.get("lock");for(let e=0;e<10;e++){const e=await Promise.all(Array.from({length:8},(e,s)=>t.compareAndSet("lock",i,`${i}.${s}`)));n.equal(e.filter(Boolean).length,1),i=await t.get("lock")}}),t("random operation sequences match a model (no TTLs)",async e=>{const{store:t}=await e.make(),s=M(t),a=new Map;let r=12139;const i=e=>(r=1103515245*r+12345&2147483647)%e,o=["a","b","c","d"];for(let e=0;e<400;e++){const e=o[i(o.length)],t=String(i(5));switch(i(6)){case 0:n.equal(await s.get(e),a.get(e)??null);break;case 1:n.equal(await s.set(e,t),!0),a.set(e,t);break;case 2:{const r=!a.has(e);n.equal(await s.set(e,t,{ifAbsent:!0}),r),r&&a.set(e,t);break}case 3:await s.del(e),a.delete(e);break;case 4:{const t=a.get(e),r=void 0===t?0:Number(t);n.equal(await s.incr(e,2),r+2),a.set(e,String(r+2));break}case 5:{const r=i(2)?a.get(e)??null:"zz",o=(a.get(e)??null)===r;n.equal(await s.compareAndSet(e,r,t),o),o&&a.set(e,t);break}}}}),e}const N="server/testing";export{x as FakeR2,l as MIN_PART,N as SDK_ENTRY,A as createFakeR2,u as isTextLike,T as keyValueStoreContract,o as md5hex,c as multipartEtag};
|