@uploadfilm/sdk 0.1.0 → 0.2.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/README.md +26 -6
- package/dist/copy/types.d.ts +29 -2
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/dist/player.js +1 -1
- package/dist/player.mjs +1 -1
- package/dist/server/media/client.d.ts +34 -1
- package/dist/server/media/protocol.d.ts +242 -4
- package/dist/server/r2/client.d.ts +18 -4
- package/dist/server/storage/chunked.d.ts +6 -2
- package/dist/server/storage/s3.d.ts +51 -0
- package/dist/server/testing/fake-r2.d.ts +22 -2
- package/dist/server/transfer/handler.d.ts +50 -0
- package/dist/server/transfer/protocol.d.ts +53 -4
- package/dist/server-media.js +1 -1
- package/dist/server-media.mjs +1 -1
- package/dist/server-testing.js +1 -1
- package/dist/server-testing.mjs +1 -1
- package/dist/server.js +1 -1
- package/dist/server.mjs +1 -1
- package/dist/transfer/client.d.ts +13 -2
- package/dist/transfer/engine.d.ts +22 -2
- package/dist/transfer/growing/bytes.d.ts +17 -0
- package/dist/transfer/growing/index.d.ts +12 -0
- package/dist/transfer/growing/parts.d.ts +157 -0
- package/dist/transfer/growing/retry.d.ts +38 -0
- package/dist/transfer/growing/store.d.ts +66 -0
- package/dist/transfer/growing/upload.d.ts +126 -0
- package/dist/transfer/index.d.ts +1 -0
- package/dist/transfer/protocol.d.ts +5 -0
- package/dist/transfer/storage.d.ts +28 -3
- package/dist/workers/cli.mjs +115 -0
- package/dist/workers/media/config.mjs +53 -0
- package/dist/workers/media/container/Dockerfile +19 -0
- package/dist/workers/media/container/FFMPEG.md +129 -0
- package/dist/workers/media/container/runner.mjs +2690 -0
- package/dist/workers/media/index.mjs +1011 -0
- package/dist/workers/media/init/config.ts +52 -0
- package/dist/workers/media/init/worker.ts +8 -0
- package/dist/workers/media/init/wrangler.toml +44 -0
- package/dist/workers/media/runner.mjs +2656 -0
- package/dist/workers/media/types/build/entry/config.d.mts +6 -0
- package/dist/workers/media/types/build/entry/index.d.mts +43 -0
- package/dist/workers/media/types/build/entry/program.d.mts +13 -0
- package/dist/workers/media/types/build/entry/runner.d.mts +12 -0
- package/dist/workers/media/types/build/entry/settings.d.mts +40 -0
- package/dist/workers/media/types/src/config.d.mts +135 -0
- package/dist/workers/media/types/src/jobs.d.mts +73 -0
- package/dist/workers/media/types/src/runner/container.d.mts +32 -0
- package/dist/workers/media/types/src/runner/ffmpeg.d.mts +227 -0
- package/dist/workers/media/types/src/runner/job.d.mts +107 -0
- package/dist/workers/media/types/src/runner/node.d.mts +71 -0
- package/dist/workers/media/types/src/runner/plugins.d.mts +45 -0
- package/dist/workers/media/types/src/serve.d.mts +107 -0
- package/dist/workers/media/types/src/steps/audio.d.mts +33 -0
- package/dist/workers/media/types/src/steps/edit.d.mts +62 -0
- package/dist/workers/media/types/src/steps/frameCheck.d.mts +44 -0
- package/dist/workers/media/types/src/steps/gate.d.mts +18 -0
- package/dist/workers/media/types/src/steps/paths.d.mts +1 -0
- package/dist/workers/media/types/src/steps/prep.d.mts +60 -0
- package/dist/workers/media/types/src/steps/render.d.mts +40 -0
- package/dist/workers/media/types/src/steps/runner.d.mts +30 -0
- package/dist/workers/media/types/src/steps/transcribe.d.mts +89 -0
- package/dist/workers/media/types/src/storyboard/index.d.mts +61 -0
- package/dist/workers/media/types/src/transcoder.d.mts +30 -0
- package/dist/workers/media/types/src/worker.d.mts +30 -0
- package/dist/workers/zip/index.mjs +589 -0
- package/dist/workers/zip/init/config.ts +22 -0
- package/dist/workers/zip/init/worker.ts +5 -0
- package/dist/workers/zip/init/wrangler.toml +25 -0
- package/dist/workers/zip/types/build/entry/index.d.mts +5 -0
- package/dist/workers/zip/types/src/config.d.mts +35 -0
- package/dist/workers/zip/types/src/serve.d.mts +86 -0
- package/dist/workers/zip/types/src/worker.d.mts +12 -0
- package/package.json +51 -3
|
@@ -13,6 +13,8 @@ export type TransferOp =
|
|
|
13
13
|
/**
|
|
14
14
|
* Server performs CreateMultipartUpload and returns the uploadId. `size` lets the server enforce plans (immutable
|
|
15
15
|
* kinds must give it). `sha256` (hex, optional) lets a dedupe hook recognise content that is already stored.
|
|
16
|
+
* `checksumAlgorithm: "SHA256"` (integrity "sha256"): the upload is created with SHA-256 checksums; a handler whose
|
|
17
|
+
* storage can't check them on parts (R2: `checksums: { multipart: false }`) refuses it (`ChecksumUnsupported`).
|
|
16
18
|
*/
|
|
17
19
|
{
|
|
18
20
|
op: "create";
|
|
@@ -21,19 +23,33 @@ export type TransferOp =
|
|
|
21
23
|
size?: number;
|
|
22
24
|
counts?: boolean;
|
|
23
25
|
sha256?: string;
|
|
26
|
+
checksumAlgorithm?: "SHA256";
|
|
24
27
|
}
|
|
25
28
|
/**
|
|
26
29
|
* Presigned PUT for one part (body sent by the client). With `contentMd5` (base64 MD5 of the body) the URL signs
|
|
27
30
|
* Content-MD5, so storage rejects a body that changed in transit (BadDigest) instead of storing it.
|
|
28
31
|
*/
|
|
32
|
+
/**
|
|
33
|
+
* `size` (optional): the part's exact length. Signed into the URL as Content-Length (storage refuses another length)
|
|
34
|
+
* and given to the handler's `signParts` budget hook. A part signed ahead of its bytes (growing uploads) has none.
|
|
35
|
+
*/
|
|
36
|
+
/**
|
|
37
|
+
* `checksumSha256` (base64 SHA-256 of the body, integrity "sha256"): signed as `x-amz-checksum-sha256`, so storage
|
|
38
|
+
* checks the body against it and answers it back (the PUT's receipt).
|
|
39
|
+
*/
|
|
29
40
|
| {
|
|
30
41
|
op: "part";
|
|
31
42
|
key: string;
|
|
32
43
|
uploadId: string;
|
|
33
44
|
partNumber: number;
|
|
34
45
|
contentMd5?: string;
|
|
46
|
+
size?: number;
|
|
47
|
+
checksumSha256?: string;
|
|
35
48
|
}
|
|
36
|
-
/**
|
|
49
|
+
/**
|
|
50
|
+
* Server performs CompleteMultipartUpload and returns the final size (HEAD). A part's `checksumSha256` (base64) is
|
|
51
|
+
* sent as its ChecksumSHA256 (every part has one, or none does).
|
|
52
|
+
*/
|
|
37
53
|
| {
|
|
38
54
|
op: "complete";
|
|
39
55
|
key: string;
|
|
@@ -41,6 +57,7 @@ export type TransferOp =
|
|
|
41
57
|
parts: {
|
|
42
58
|
n: number;
|
|
43
59
|
etag: string;
|
|
60
|
+
checksumSha256?: string;
|
|
44
61
|
}[];
|
|
45
62
|
}
|
|
46
63
|
/** Server aborts a multipart upload. */
|
|
@@ -49,7 +66,7 @@ export type TransferOp =
|
|
|
49
66
|
key: string;
|
|
50
67
|
uploadId: string;
|
|
51
68
|
}
|
|
52
|
-
/** Presigned single PUT (small files, posters). `contentMd5` as for parts; `sha256` as for create. */
|
|
69
|
+
/** Presigned single PUT (small files, posters). `contentMd5`, `checksumSha256` as for parts; `sha256` as for create. */
|
|
53
70
|
| {
|
|
54
71
|
op: "put";
|
|
55
72
|
key: string;
|
|
@@ -58,6 +75,7 @@ export type TransferOp =
|
|
|
58
75
|
size?: number;
|
|
59
76
|
counts?: boolean;
|
|
60
77
|
sha256?: string;
|
|
78
|
+
checksumSha256?: string;
|
|
61
79
|
}
|
|
62
80
|
/** Server HEADs the key: size or null. */
|
|
63
81
|
| {
|
|
@@ -80,6 +98,16 @@ export type TransferOp =
|
|
|
80
98
|
| {
|
|
81
99
|
op: "listUploads";
|
|
82
100
|
key: string;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* A presigned GET of the key, with the stored object's size and ETag (what to pin the read to with If-Match). Only
|
|
104
|
+
* when the product's `readable` hook allows reading that kind for the caller (fail closed: refused by default, code
|
|
105
|
+
* `ReadRefused`). The engine reads back an object that was at the key before its first write, to prove by SHA-256
|
|
106
|
+
* that it is the file (the MD5 ETag isn't collision resistant); downloads can use it too (Reville gap 9).
|
|
107
|
+
*/
|
|
108
|
+
| {
|
|
109
|
+
op: "get";
|
|
110
|
+
key: string;
|
|
83
111
|
};
|
|
84
112
|
export type TransferOpName = TransferOp["op"];
|
|
85
113
|
/** Ops that write (or lead to a write of) the key. */
|
|
@@ -93,11 +121,19 @@ export type TransferResult = {
|
|
|
93
121
|
headers: Record<string, string>;
|
|
94
122
|
expiresAt: string;
|
|
95
123
|
}
|
|
96
|
-
/**
|
|
124
|
+
/**
|
|
125
|
+
* complete, head. `etag` is storage's ETag without quotes: MD5 hex for a single PUT, md5(part md5s)-N for multipart.
|
|
126
|
+
* complete `recovered: true`: the upload was already gone (a completion whose answer was lost, or not ours) and this
|
|
127
|
+
* is a HEAD of what is stored, not a stitch of the parts named.
|
|
128
|
+
* `checksumSha256`: the SHA-256 storage holds for the object (base64; "<b64>-<parts>" for an S3 composite multipart
|
|
129
|
+
* checksum), when it reports one.
|
|
130
|
+
*/
|
|
97
131
|
| {
|
|
98
132
|
ok: true;
|
|
99
133
|
size: number | null;
|
|
100
134
|
etag: string | null;
|
|
135
|
+
recovered?: true;
|
|
136
|
+
checksumSha256?: string;
|
|
101
137
|
}
|
|
102
138
|
/**
|
|
103
139
|
* create, put, when the server's dedupe hook found the same content already stored and copied it to the key
|
|
@@ -111,13 +147,17 @@ export type TransferResult = {
|
|
|
111
147
|
} | {
|
|
112
148
|
ok: true;
|
|
113
149
|
}
|
|
114
|
-
/**
|
|
150
|
+
/**
|
|
151
|
+
* listParts. `etag`: storage's part ETag without quotes (the part's MD5 hex on R2). `checksumSha256`: the part's
|
|
152
|
+
* (base64), when storage reports one (S3 with SHA-256 checksums; never R2).
|
|
153
|
+
*/
|
|
115
154
|
| {
|
|
116
155
|
ok: true;
|
|
117
156
|
parts: {
|
|
118
157
|
n: number;
|
|
119
158
|
etag: string;
|
|
120
159
|
size: number;
|
|
160
|
+
checksumSha256?: string;
|
|
121
161
|
}[];
|
|
122
162
|
}
|
|
123
163
|
/** listUploads: the multipart uploads open on the key (`initiated`: ISO time, when storage reports it). */
|
|
@@ -127,6 +167,15 @@ export type TransferResult = {
|
|
|
127
167
|
uploadId: string;
|
|
128
168
|
initiated?: string;
|
|
129
169
|
}[];
|
|
170
|
+
}
|
|
171
|
+
/** get: the URL, its lifetime and the stored object's size/ETag. Nothing stored: the head shape with size null. */
|
|
172
|
+
| {
|
|
173
|
+
ok: true;
|
|
174
|
+
url: string;
|
|
175
|
+
headers: Record<string, string>;
|
|
176
|
+
expiresAt: string;
|
|
177
|
+
size: number;
|
|
178
|
+
etag: string | null;
|
|
130
179
|
} | {
|
|
131
180
|
ok: false;
|
|
132
181
|
error: string;
|
package/dist/server-media.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const t=[{short:360,kbps:700,peak:1e3},{short:720,kbps:2800,peak:4e3},{short:1080,kbps:5500,peak:8e3}];const e=["ladder","poster","storyboard","faststart","audio","transcript","frame-check"],r=["audio","transcript","frame-check"],s=t=>t.some(t=>r.includes(t)),o=t=>t.includes("audio")||t.includes("transcript");function n(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 a=n("steps/1"),i=(t,e)=>t.includes("storyboard")&&!!e?.storyboard,c="audio/audio.json",u="transcript.vtt",d="transcript.json",p="frame-check.json";const l=/^[a-z0-9]{1,8}$/;function h(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(!l.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}const f=4096,m=604800,y=t=>"number"==typeof t&&Number.isFinite(t)&&t>0&&t<=m;function x(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 g=(t,e)=>t.ladder===e,b=t=>Math.round(1e3*t)/1e3;function S(t,e,r){let s=0;for(const o of r)s+=Math.max(0,Math.min(e,o.end)-Math.max(t,o.start));return s}function w(t,e,r,s){const o=e-t;if(o<=0)return r.some(e=>t>=e.start&&t<=e.end);const n=S(t,e,r);return n>0&&n>=.8*o&&o-n<=s}function v(t){const e=Math.max(0,Math.round(1e3*t)),r=Math.floor(e/36e5),s=Math.floor(e/6e4)%60,o=Math.floor(e/1e3)%60,n=(t,e=2)=>String(t).padStart(e,"0");return`${n(r)}:${n(s)}:${n(o)}.${n(e%1e3,3)}`}const A=t=>t.replace(/\r\n?/g,"\n").replace(/\n\s*\n/g,"\n").replace(/-->/g,"→").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").trim();const M=new TextEncoder;new TextDecoder;function E(t){const e="string"==typeof t?M.encode(t):t,r=new Uint8Array(e.length);return r.set(e),r}async function $(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",E(t),{name:"HMAC",hash:"SHA-256"},!1,["sign"]);return new Uint8Array(await crypto.subtle.sign("HMAC",r,E(e)))}(t,`${e}\n${r}`))}class k 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 T(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 N(r),r}const j=T("storyboard/1");function D(t,e,r){return"number"==typeof t&&Number.isInteger(t)&&t>=e&&t<=r}function N(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"),D(t.maxFrames,1,1e4)||e("maxFrames"),(!D(t.size,16,1024)||t.size%2)&&e("size"),D(t.cols,1,32)&&D(t.rows,1,32)||e("cols/rows"),(!D(t.quality,1,100)||!D(t.minQuality,1,100)||t.minQuality>t.quality)&&e("quality/minQuality"),D(t.maxSheetBytes,1e3,1e8)&&D(t.maxBytes,t.maxSheetBytes,1e9)||e("maxSheetBytes/maxBytes")}const _=t=>Math.max(2,2*Math.round(t/2));const F="storyboard",R="storyboard/storyboard.vtt",O="storyboard/storyboard.json",C=t=>`storyboard/sb-${String(t+1).padStart(3,"0")}.webp`;function I(t){return t===O?"storyboard-index":t===R?"storyboard-vtt":/^storyboard\/sb-\d{3,4}\.webp$/.test(t)?"storyboard":null}const B=(t,e)=>Math.ceil(e/(t.cols*t.rows));function U(t,e){const r=t.cols*t.rows,s=[];for(let o=0;o<e;o++){const n=o%r,a=H(o*t.interval),i=H((o+1)*t.interval),c=o===e-1?Math.max(i,H(t.seconds)):i;s.push({start:a,end:c,sheet:Math.floor(o/r),x:n%t.cols*t.width,y:Math.floor(n/t.cols)*t.height,w:t.width,h:t.height})}return s}function H(t){return Math.round(1e3*t)/1e3}function V(t){const e=Math.round(1e3*t),r=Math.floor(e/36e5),s=Math.floor(e/6e4)%60,o=Math.floor(e/1e3)%60,n=(t,e=2)=>String(t).padStart(e,"0");return`${n(r)}:${n(s)}:${n(o)}.${n(e%1e3,3)}`}function W(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&&D(e.cols,1,32)&&D(e.rows,1,32)&&D(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"===I(t))&&e.vtt===R}exports.AUDIO_INDEX=c,exports.DEFAULT_RUNGS=t,exports.DEFAULT_STEP_SETTINGS=a,exports.DEFAULT_STORYBOARD_SETTINGS=j,exports.EXPECT_HEADER="x-media-expect-duration",exports.EXTRA_STEPS=r,exports.FRAME_CHECK_FILE=p,exports.MAX_BODY=f,exports.MAX_EXPECT_DURATION_S=m,exports.MediaJobError=k,exports.RUNNER_OUTPUT_REVISION=2,exports.SDK_ENTRY="server/media",exports.SILENT_SHARE=.8,exports.STEPS=e,exports.STORYBOARD_DIR=F,exports.STORYBOARD_INDEX=O,exports.STORYBOARD_VTT=R,exports.TRANSCRIPT_JSON=d,exports.TRANSCRIPT_VTT=u,exports.VERSION_HEADER="x-media-version",exports.audioChunkName=t=>`audio/a-${String(t).padStart(3,"0")}.ogg`,exports.buildTranscript=function(t,e){const r=[];let s=0,o=t.language;for(const{chunk:n,result:a}of t.results){o??=a.transcription_info?.language;const i=n.end-n.start;for(const o of a.segments??[]){const a=String(o.text??"").trim();if(!a||!Number.isFinite(o.start)||!Number.isFinite(o.end))continue;const c=b(n.start+Math.max(0,Math.min(o.start,i))),u=b(n.start+Math.max(0,Math.min(Math.max(o.end,o.start),i)));if(w(c,u,t.silences,e.edgeS)){s++;continue}const d=(o.words??[]).filter(t=>t&&"string"==typeof t.word&&Number.isFinite(t.start)&&Number.isFinite(t.end)).map(t=>({word:t.word.trim(),start:b(n.start+Math.max(0,t.start)),end:b(n.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,...o?{language:o}:{},seconds:b(t.seconds),segments:r,text:r.map(t=>t.text).join(" "),dropped:s,silentChunks:t.silentChunks??0}},exports.checkStoryboardSettings=N,exports.chunkSilent=(t,e)=>t.end-t.start-S(t.start,t.end,e)<=.05,exports.completionFile=function(t){return t.includes("ladder")?"master.m3u8":t.includes("faststart")?"faststart.mp4":t.includes("storyboard")?"storyboard/storyboard.json":t.includes("poster")||!s(t)?"poster.jpg":o(t)?c:p},exports.contentTypeOf=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",exports.hasExtraSteps=s,exports.inSilence=w,exports.isStoryboardIndex=W,exports.jobBody=x,exports.jobVersion=(t,e,r=a)=>{const o="string"==typeof r?{version:r}:r;return s(e)||i(e,o)?`${t}+${o.version}`:t},exports.keyframeExpr=(t,e)=>`expr:gte(t,if(lt(n_forced,2),n_forced*${t},(n_forced-1)*${e}))`,exports.ladderFile=h,exports.ladderSettings=function(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}},exports.mediaJobClient=function(t){const e=()=>"function"==typeof t.secret?t.secret():t.secret;return{sign:async(r,s)=>$(e(),(e=>"job"===e?t.jobDomain:t.doneDomain)(r),s),async request(r){if(void 0!==r.expectDurationS&&!y(r.expectDurationS))throw new Error(`media job: bad expectDurationS ${String(r.expectDurationS)}`);const s=x(t.issuedAt&&void 0===r.issuedAt?{...r,issuedAt:Math.floor((t.now??Date.now)()/1e3)}:r);if(s.length>f)throw new Error("media job: request too big");let o;try{o=await(t.fetch??fetch)(`${t.url.replace(/\/$/,"")}/jobs`,{method:"POST",body:s,headers:{"content-type":"application/json",[t.signatureHeader]:await $(e(),t.jobDomain,s)},signal:AbortSignal.timeout(t.timeoutMs??3e4)})}catch(t){const e="TimeoutError"===t?.name||"AbortError"===t?.name;throw new k(`media worker: ${e?"no answer in time":"unreachable"}: ${t?.message??String(t)}`,{reached:!1,timedOut:e,cause:t})}if(!o.ok)throw new k(`media worker: ${o.status} ${(await o.text().catch(()=>"")).slice(0,200)}`,{reached:!0,status:o.status})},async readReport(r,s){if("string"!=typeof r||r.length>f||!function(t,e){if("string"!=typeof t||"string"!=typeof e||t.length!==e.length)return!1;let r=0;for(let s=0;s<t.length;s++)r|=t.charCodeAt(s)^e.charCodeAt(s);return 0===r}(s,await $(e(),t.doneDomain,r)))return{ok:!1,status:403,body:"bad signature"};let o;try{o=JSON.parse(r)}catch{return{ok:!1,status:400,body:"bad json"}}return o&&"object"==typeof o?{ok:!0,report:o}:{ok:!1,status:400,body:"bad report"}}}},exports.mediaStepFiles=function(t){return{audioIndex:`${t}audio/audio.json`,transcriptVtt:`${t}transcript.vtt`,transcriptJson:`${t}transcript.json`,frameCheck:`${t}frame-check.json`}},exports.outputFile=function(t,e,r){if(e.includes("ladder")){const e=h(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(o(e)){if(t===c)return"audio-index";if(/^audio\/a-\d{3,4}\.ogg$/.test(t))return"audio"}if(e.includes("transcript")){if(t===u)return"transcript";if(t===d)return"transcript-json"}return e.includes("frame-check")&&t===p?"frame-check":null},exports.parseSteps=function(t,r=e){if(!Array.isArray(t)||!t.length||t.length>2*e.length)return null;for(const e of t)if("string"!=typeof e||!r.includes(e))return null;return e.filter(e=>t.includes(e))},exports.planStoryboard=function(t,e=j){const r=t.width>0?t.width:16,s=t.height>0?t.height:9,o=Number.isFinite(t.seconds)&&t.seconds>0?t.seconds:0,n=e.size/Math.max(r,s),a=Math.ceil(1e3*Math.max(e.intervalS,o/e.maxFrames))/1e3,i=o?Math.min(e.maxFrames,Math.max(1,Math.ceil(o/a-1e-9))):1;return{interval:a,maxFrames:e.maxFrames,expected:i,width:_(r*n),height:_(s*n),cols:e.cols,rows:e.rows,seconds:o}},exports.readAudioIndex=function(t){let e;try{e=JSON.parse(t)}catch{return null}if(!e||"object"!=typeof e)return null;const r=e,s=t=>"number"==typeof t&&Number.isFinite(t)&&t>=0;if(1!==r.format||!Array.isArray(r.chunks)||!Array.isArray(r.silences)||!s(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)||!s(t.start)||!s(t.end)||t.end<t.start)return null;for(const t of r.silences)if(!t||!s(t.start)||!s(t.end)||t.end<t.start)return null;return r},exports.readFrameCheck=function(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},exports.readTranscript=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},exports.refuseStale=function(t,e){return"done"===t.state&&!g(t,e)},exports.rungsFor=function(t,e,r,s){const o=t=>Math.max(2,2*Math.round(t/2));if(s&&Math.min(e,r)>s){const t=s/Math.min(e,r);e=o(e*t),r=o(r*t)}const n=Math.min(e,r),a=[];for(const s of t){if(s.short>=.9*n)break;const t=s.short/n;a.push({width:o(e*t),height:o(r*t),kbps:s.kbps,peak:s.peak,top:!1})}const i=[...t].reverse().find(t=>t.short<=1.1*n)??t[0];return a.push({width:o(e),height:o(r),kbps:i.kbps,peak:i.peak,top:!0}),a},exports.silentSeconds=S,exports.sourceHash=async function(t,e,r){const s=new Uint8Array(await crypto.subtle.digest("SHA-256",(new TextEncoder).encode(`${t}#${e}${r?`#${r}`:""}`)));let o="";for(let t=0;t<s.length;t++)o+=s[t].toString(16).padStart(2,"0");return o},exports.stampMatches=g,exports.stepSettings=n,exports.storyboardCues=U,exports.storyboardDescriptor=function(t,e){/\/$/.test(t)||(t+="/");const r={vtt:t+R,index:t+O};if(!e)return r;if(!W(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)}},exports.storyboardFile=I,exports.storyboardIndex=function(t,e,r,s){const o=B(t,e);return{version:r,interval:t.interval,cols:t.cols,rows:t.rows,width:t.width,height:t.height,count:e,seconds:H(t.seconds),sheets:Array.from({length:o},(t,e)=>C(e)),vtt:R,bytes:s}},exports.storyboardSettings=T,exports.storyboardSheetCount=B,exports.storyboardSheetName=C,exports.storyboardV2=i,exports.storyboardVtt=function(t,e){const r=["WEBVTT",""];for(const s of U(t,e))r.push(`${V(s.start)} --\x3e ${V(s.end)}`,`${C(s.sheet).slice(11)}#xywh=${s.x},${s.y},${s.w},${s.h}`,"");return r.join("\n")},exports.toWebVtt=function(t){const e=["WEBVTT",""];return t.segments.forEach((t,r)=>{const s=t.end>t.start?t.end:t.start+.5;e.push(String(r+1),`${v(t.start)} --\x3e ${v(s)}`,A(t.text),"")}),e.join("\n")},exports.validExpectDuration=y,exports.vttText=A,exports.vttTime=v,exports.wantsAudio=o;
|
|
1
|
+
"use strict";const t=[{short:360,kbps:700,peak:1e3},{short:720,kbps:2800,peak:4e3},{short:1080,kbps:5500,peak:8e3}];const e=["ladder","poster","storyboard","faststart","audio","transcript","frame-check"],r=["prep","render"],s=[...e,...r],o=["audio","transcript","frame-check"],n=t=>t.some(t=>o.includes(t)||r.includes(t)),a=t=>t.some(t=>r.includes(t)),i=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}}:{},...e.prep?{prep:{...e.prep}}:{},...e.render?{render:{...e.render}}:{}}}const p=u("steps/1"),c=(t,e)=>t.includes("storyboard")&&!!e?.storyboard,d="audio/audio.json",h="transcript.vtt",f="transcript.json",m="frame-check.json";const l=/^[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(!l.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}const b=4096,y=604800,g=t=>"number"==typeof t&&Number.isFinite(t)&&t>0&&t<=y;function S(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}:{},...void 0!==t.render?{render:L(t.render)}:{}})}const M=(t,e)=>t.ladder===e,w="edit/media.mp4",E="edit/peaks.bin",v="edit/prep.json",A=t=>`edit/sb-${String(t).padStart(3,"0")}.jpg`,T="render/video.mp4",j="render/poster.jpg",$="render/result.json",R={maxShort:1080,maxSeconds:14400,preset:"veryfast",crf:22,audioKbps:128,columns:10,rows:10,tileLongSide:320,minIntervalS:2,maxFrames:400,jpegQ:5,peaksRate:100,processTimeoutS:6600},k={maxShort:1080,maxParts:200,maxSources:50,maxSeconds:14400,minPartMs:50,preset:"veryfast",crf:21,audioKbps:128,posterWidth:1280,partTimeoutS:5400},N={parts:500,sources:100,seconds:86400,frame:8192,fps:120},_=(t,e)=>"number"==typeof t&&Number.isFinite(t)?t:e;function D(t){const e={...k,...t?.render},r=(t,e,r,s)=>Math.min(s,Math.max(r,Math.round(_(t,e))));return{maxShort:-2&r(e.maxShort,1080,144,4320),maxParts:r(e.maxParts,200,1,N.parts),maxSources:r(e.maxSources,50,1,N.sources),maxSeconds:r(e.maxSeconds,14400,1,N.seconds),minPartMs:r(e.minPartMs,50,1,6e4),preset:/^[a-z]{1,12}$/.test(String(e.preset))?String(e.preset):k.preset,crf:r(e.crf,21,0,51),audioKbps:r(e.audioKbps,128,32,512),posterWidth:-2&r(e.posterWidth,1280,64,4096),partTimeoutS:r(e.partTimeoutS,5400,10,86400)}}const F=(t,e)=>2*Math.ceil(Math.max(0,t)*e),I=(t,e,r)=>"number"==typeof t&&Number.isSafeInteger(t)&&t>=e&&t<=r,P=(t,e)=>Object.keys(t).every(t=>e.includes(t)),O=t=>!!t&&"object"==typeof t&&!Array.isArray(t);function L(t){const e=t=>({x:t.x,y:t.y,width:t.width,height:t.height}),r=t.editList.output;return{sources:t.sources.map(t=>({src:t.src,etag:t.etag,bytes:t.bytes})),editList:{version:t.editList.version,output:{width:r.width,height:r.height,fps:r.fps,...r.crop?{crop:e(r.crop)}:{},...void 0!==r.maxShort?{maxShort:r.maxShort}:{}},parts:t.editList.parts.map(t=>({source:t.source,inMs:t.inMs,outMs:t.outMs,...t.muted?{muted:!0}:{},...t.crop?{crop:e(t.crop)}:{}}))}}}const C=/^(?!\/)(?!.*(?:^|\/)\.\.(?:\/|$))[\x21-\x7e]{1,1024}$/;function B(t,e,r){return O(t)&&P(t,["x","y","width","height"])&&I(t.x,0,e)&&I(t.y,0,r)&&I(t.width,2,e)&&I(t.height,2,r)?t.x+t.width>e||t.y+t.height>r?"crop outside the frame":{x:t.x,y:t.y,width:t.width,height:t.height}:"bad crop"}function U(t,e=k){const r=t=>({error:t});if(!O(t)||!P(t,["sources","editList"]))return r("bad render");const s=t.sources;if(!Array.isArray(s)||!s.length||s.length>Math.min(e.maxSources,N.sources))return r("bad sources");const o=[];for(const t of s){if(!O(t)||!P(t,["src","etag","bytes"])||"string"!=typeof t.src||!C.test(t.src))return r("bad source");if("string"!=typeof t.etag||!/^[\x21-\x7e]{1,80}$/.test(t.etag)||!I(t.bytes,1,Number.MAX_SAFE_INTEGER))return r("bad source");o.push({src:t.src,etag:t.etag,bytes:t.bytes})}const n=t.editList;if(!O(n)||!P(n,["version","output","parts"]))return r("bad editList");if(1!==n.version)return r("unknown editList version");const a=n.output;if(!O(a)||!P(a,["width","height","fps","crop","maxShort"]))return r("bad output");if(!I(a.width,16,N.frame)||!I(a.height,16,N.frame))return r("bad output frame");if("number"!=typeof a.fps||!(a.fps>=1&&a.fps<=N.fps)||Math.round(1e3*a.fps)!==1e3*a.fps)return r("bad fps");let i;if(void 0!==a.crop){const t=B(a.crop,a.width,a.height);if("string"==typeof t)return r(`output: ${t}`);if(t.width<16||t.height<16)return r("output: crop too small");i=t}if(void 0!==a.maxShort&&!I(a.maxShort,144,4320))return r("bad maxShort");const u=n.parts;if(!Array.isArray(u)||!u.length||u.length>Math.min(e.maxParts,N.parts))return r("bad parts");const p=[],c=new Set;let d=0;for(let t=0;t<u.length;t++){const s=u[t];if(!O(s)||!P(s,["source","inMs","outMs","muted","crop"]))return r(`part ${t}: bad shape`);if(!I(s.source,0,o.length-1))return r(`part ${t}: bad source`);const n=1e3*N.seconds;if(!I(s.inMs,0,n)||!I(s.outMs,1,n)||s.outMs-s.inMs<Math.max(e.minPartMs,Math.ceil(1e3/a.fps)))return r(`part ${t}: bad times`);if(void 0!==s.muted&&!0!==s.muted)return r(`part ${t}: bad muted`);let i;if(void 0!==s.crop){const e=B(s.crop,N.frame,N.frame);if("string"==typeof e)return r(`part ${t}: ${e}`);i=e}c.add(s.source),d+=s.outMs-s.inMs,p.push({source:s.source,inMs:s.inMs,outMs:s.outMs,...s.muted?{muted:!0}:{},...i?{crop:i}:{}})}if(c.size!==o.length)return r("a source no part uses");if(d>1e3*e.maxSeconds)return r("too long");return{job:{sources:o,editList:{version:1,output:{width:a.width,height:a.height,fps:a.fps,...i?{crop:i}:{},...void 0!==a.maxShort?{maxShort:a.maxShort}:{}},parts:p}}}}const W=t=>t.parts.reduce((t,e)=>t+e.outMs-e.inMs,0);function H(t){let e=0;const r=t.output.fps;return t.parts.map(t=>{const s=Math.ceil(e*r/1e3-1e-9);return e+=t.outMs-t.inMs,Math.max(1,Math.ceil(e*r/1e3-1e-9)-s)})}const J=48e3;function V(t){let e=0;return t.parts.map(t=>{const r=Math.round(e*J/1e3);return e+=t.outMs-t.inMs,Math.round(e*J/1e3)-r})}function K(t,e=k){const r=t=>Math.max(2,2*Math.floor(t/2)),s=t.output.crop?t.output.crop.width:t.output.width,o=t.output.crop?t.output.crop.height:t.output.height,n=Math.min(e.maxShort,t.output.maxShort??1/0),a=Math.min(1,n/Math.min(s,o));return{width:r(s*a),height:r(o*a)}}const X=t=>Math.round(1e3*t)/1e3;function z(t,e,r){let s=0;for(const o of r)s+=Math.max(0,Math.min(e,o.end)-Math.max(t,o.start));return s}function G(t,e,r,s){const o=e-t;if(o<=0)return r.some(e=>t>=e.start&&t<=e.end);const n=z(t,e,r);return n>0&&n>=.8*o&&o-n<=s}function Q(t){const e=Math.max(0,Math.round(1e3*t)),r=Math.floor(e/36e5),s=Math.floor(e/6e4)%60,o=Math.floor(e/1e3)%60,n=(t,e=2)=>String(t).padStart(e,"0");return`${n(r)}:${n(s)}:${n(o)}.${n(e%1e3,3)}`}const Y=t=>t.replace(/\r\n?/g,"\n").replace(/\n\s*\n/g,"\n").replace(/-->/g,"→").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").trim();const q=new TextEncoder;new TextDecoder;function Z(t){const e="string"==typeof t?q.encode(t):t,r=new Uint8Array(e.length);return r.set(e),r}async function tt(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",Z(t),{name:"HMAC",hash:"SHA-256"},!1,["sign"]);return new Uint8Array(await crypto.subtle.sign("HMAC",r,Z(e)))}(t,`${e}\n${r}`))}class et extends Error{reached;status;timedOut;busy;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,this.busy=e.busy??!1,void 0!==e.status&&(this.status=e.status)}}const rt=/no container instance|too many containers|max(imum)? instances|container.{0,40}(capacity|unavailable)/i;function st(t,e,r){return!(t>=200&&t<300)&&(429===t||503===t&&null!==r||rt.test(e))}function ot(t,e){if(null===t||""===t.trim())return null;const r=Number(t);if(Number.isFinite(r))return r>=0?1e3*r:null;const s=Date.parse(t);return Number.isFinite(s)?Math.max(0,s-e):null}function nt(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 ut(r),r}const at=nt("storyboard/1");function it(t,e,r){return"number"==typeof t&&Number.isInteger(t)&&t>=e&&t<=r}function ut(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"),it(t.maxFrames,1,1e4)||e("maxFrames"),(!it(t.size,16,1024)||t.size%2)&&e("size"),it(t.cols,1,32)&&it(t.rows,1,32)||e("cols/rows"),(!it(t.quality,1,100)||!it(t.minQuality,1,100)||t.minQuality>t.quality)&&e("quality/minQuality"),it(t.maxSheetBytes,1e3,1e8)&&it(t.maxBytes,t.maxSheetBytes,1e9)||e("maxSheetBytes/maxBytes")}const pt=t=>Math.max(2,2*Math.round(t/2));const ct="storyboard",dt="storyboard/storyboard.vtt",ht="storyboard/storyboard.json",ft=t=>`storyboard/sb-${String(t+1).padStart(3,"0")}.webp`;function mt(t){return t===ht?"storyboard-index":t===dt?"storyboard-vtt":/^storyboard\/sb-\d{3,4}\.webp$/.test(t)?"storyboard":null}const lt=(t,e)=>Math.ceil(e/(t.cols*t.rows));function xt(t,e){const r=t.cols*t.rows,s=[];for(let o=0;o<e;o++){const n=o%r,a=bt(o*t.interval),i=bt((o+1)*t.interval),u=o===e-1?Math.max(i,bt(t.seconds)):i;s.push({start:a,end:u,sheet:Math.floor(o/r),x:n%t.cols*t.width,y:Math.floor(n/t.cols)*t.height,w:t.width,h:t.height})}return s}function bt(t){return Math.round(1e3*t)/1e3}function yt(t){const e=Math.round(1e3*t),r=Math.floor(e/36e5),s=Math.floor(e/6e4)%60,o=Math.floor(e/1e3)%60,n=(t,e=2)=>String(t).padStart(e,"0");return`${n(r)}:${n(s)}:${n(o)}.${n(e%1e3,3)}`}function gt(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&&it(e.cols,1,32)&&it(e.rows,1,32)&&it(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"===mt(t))&&e.vtt===dt}exports.ALL_STEPS=s,exports.AUDIO_INDEX=d,exports.DEFAULT_PREP=R,exports.DEFAULT_RENDER=k,exports.DEFAULT_RUNGS=t,exports.DEFAULT_STEP_SETTINGS=p,exports.DEFAULT_STORYBOARD_SETTINGS=at,exports.EDIT_LIST_VERSION=1,exports.EDIT_RESULT_FORMAT=1,exports.EDIT_STEPS=r,exports.EXPECT_HEADER="x-media-expect-duration",exports.EXTRA_STEPS=o,exports.FRAME_CHECK_FILE=m,exports.MAX_BODY=b,exports.MAX_EXPECT_DURATION_S=y,exports.MAX_RENDER_BODY=65536,exports.MediaJobError=et,exports.PREP_MEDIA=w,exports.PREP_PEAKS=E,exports.PREP_RESULT=v,exports.RENDER_LIMITS=N,exports.RENDER_POSTER=j,exports.RENDER_RESULT=$,exports.RENDER_SAMPLE_RATE=J,exports.RENDER_VIDEO=T,exports.RUNNER_OUTPUT_REVISION=2,exports.SDK_ENTRY="server/media",exports.SILENT_SHARE=.8,exports.STEPS=e,exports.STORYBOARD_DIR=ct,exports.STORYBOARD_INDEX=ht,exports.STORYBOARD_VTT=dt,exports.TRANSCRIPT_JSON=f,exports.TRANSCRIPT_VTT=h,exports.VERSION_HEADER="x-media-version",exports.audioChunkName=t=>`audio/a-${String(t).padStart(3,"0")}.ogg`,exports.buildTranscript=function(t,e){const r=[];let s=0,o=t.language;for(const{chunk:n,result:a}of t.results){o??=a.transcription_info?.language;const i=n.end-n.start;for(const o of a.segments??[]){const a=String(o.text??"").trim();if(!a||!Number.isFinite(o.start)||!Number.isFinite(o.end))continue;const u=X(n.start+Math.max(0,Math.min(o.start,i))),p=X(n.start+Math.max(0,Math.min(Math.max(o.end,o.start),i)));if(G(u,p,t.silences,e.edgeS)){s++;continue}const c=(o.words??[]).filter(t=>t&&"string"==typeof t.word&&Number.isFinite(t.start)&&Number.isFinite(t.end)).map(t=>({word:t.word.trim(),start:X(n.start+Math.max(0,t.start)),end:X(n.start+Math.max(0,t.end))})).filter(e=>e.word&&!G(e.start,e.end,t.silences,0));r.push({start:u,end:Math.max(u,p),text:a,...c.length?{words:c}:{}})}}return r.sort((t,e)=>t.start-e.start),{format:1,model:t.model,...o?{language:o}:{},seconds:X(t.seconds),segments:r,text:r.map(t=>t.text).join(" "),dropped:s,silentChunks:t.silentChunks??0}},exports.checkStoryboardSettings=ut,exports.chunkSilent=(t,e)=>t.end-t.start-z(t.start,t.end,e)<=.05,exports.completionFile=function(t){return t.includes("prep")?v:t.includes("render")?$:t.includes("ladder")?"master.m3u8":t.includes("faststart")?"faststart.mp4":t.includes("storyboard")?"storyboard/storyboard.json":t.includes("poster")||!n(t)?"poster.jpg":i(t)?d:m},exports.contentTypeOf=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(".bin")?"application/octet-stream":t.endsWith(".json")?"application/json":t.endsWith(".ogg")?"audio/ogg":t.endsWith(".vtt")?"text/vtt; charset=utf-8":"video/mp4",exports.editListMs=W,exports.filmstripInterval=(t,e)=>Math.max(e.minIntervalS,Math.ceil(Math.max(0,t)/e.maxFrames)),exports.hasExtraSteps=n,exports.inSilence=G,exports.isEditJob=a,exports.isStoryboardIndex=gt,exports.jobBody=S,exports.jobVersion=(t,e,r=p)=>{const s="string"==typeof r?{version:r}:r;return n(e)||c(e,s)?`${t}+${s.version}`:t},exports.keyframeExpr=(t,e)=>`expr:gte(t,if(lt(n_forced,2),n_forced*${t},(n_forced-1)*${e}))`,exports.ladderFile=x,exports.ladderSettings=function(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}},exports.mediaJobClient=function(t){const e=()=>"function"==typeof t.secret?t.secret():t.secret,r=()=>t.fetch??fetch,s=t.sleep??(t=>new Promise(e=>setTimeout(e,t))),o=t.random??Math.random,n=()=>(t.now??Date.now)(),a=D(t.stepSettings),i=!1===t.busy?null:{tries:Math.max(1,Math.floor(t.busy?.tries??5)),baseMs:Math.max(0,t.busy?.baseMs??500),maxMs:Math.max(0,t.busy?.maxMs??8e3)},u=t.maxStartsPerSecond&&t.maxStartsPerSecond>0?1e3/t.maxStartsPerSecond:0;let p=0;const c=async(t=1/0)=>{const e=n(),r=Math.max(e,u?p:0);return!(r>=t)&&(u&&(p=r+u),r>e&&await s(r-e),n()<t)};return{sign:async(r,s)=>tt(e(),(e=>"job"===e?t.jobDomain:t.doneDomain)(r),s),async request(u){if(void 0!==u.expectDurationS&&!g(u.expectDurationS))throw new Error(`media job: bad expectDurationS ${String(u.expectDurationS)}`);const p=()=>S(t.issuedAt&&void 0===u.issuedAt?{...u,issuedAt:Math.floor(n()/1e3)}:u),d=1===u.steps?.length&&"render"===u.steps[0];if(d||void 0!==u.render){const t=U(u.render,a);if("error"in t)throw new Error(`media job: bad render: ${t.error}`);if(!d)throw new Error('media job: a render needs steps ["render"]')}if(p().length>(d?65536:b))throw new Error("media job: request too big");const h=t.timeoutMs??3e4,f=i?.tries??1;await c();const m=n()+h;for(let a=1;;a++){const u=p();let d;try{d=await r()(`${t.url.replace(/\/$/,"")}/jobs`,{method:"POST",body:u,headers:{"content-type":"application/json",[t.signatureHeader]:await tt(e(),t.jobDomain,u)},signal:AbortSignal.timeout(Math.max(1,Math.min(h,m-n())))})}catch(t){const e="TimeoutError"===t?.name||"AbortError"===t?.name;throw new et(`media worker: ${e?"no answer in time":"unreachable"}: ${t?.message??String(t)}`,{reached:!1,timedOut:e,cause:t})}if(d.ok)return;const l=await d.text().catch(()=>""),x=st(d.status,l,d.headers.get("retry-after"));if(x&&i&&a<f){const t=Math.min(i.maxMs,i.baseMs*2**(a-1)),e=Math.max(ot(d.headers.get("retry-after"),n())??0,Math.floor(o()*t));if(n()+e<m&&(await s(e),n()<m&&await c(m)))continue}throw new et(`media worker: ${d.status} ${l.slice(0,200)}`,{reached:!0,status:d.status,busy:x})}},async readReport(r,s){if("string"!=typeof r||r.length>b||!function(t,e){if("string"!=typeof t||"string"!=typeof e||t.length!==e.length)return!1;let r=0;for(let s=0;s<t.length;s++)r|=t.charCodeAt(s)^e.charCodeAt(s);return 0===r}(s,await tt(e(),t.doneDomain,r)))return{ok:!1,status:403,body:"bad signature"};let o;try{o=JSON.parse(r)}catch{return{ok:!1,status:400,body:"bad json"}}return o&&"object"==typeof o?{ok:!0,report:o}:{ok:!1,status:400,body:"bad report"}}}},exports.mediaStepFiles=function(t){return{audioIndex:`${t}audio/audio.json`,transcriptVtt:`${t}transcript.vtt`,transcriptJson:`${t}transcript.json`,frameCheck:`${t}frame-check.json`}},exports.outputFile=function(t,e,r){if(e.includes("prep")){if(t===w)return"edit-media";if(t===E)return"edit-peaks";if(t===v)return"edit-result";if(/^edit\/sb-(?:\d{3}|[1-9]\d{3})\.jpg$/.test(t))return"edit-filmstrip"}if(e.includes("render")){if(t===T)return"render-video";if(t===j)return"render-poster";if(t===$)return"render-result"}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(i(e)){if(t===d)return"audio-index";if(/^audio\/a-\d{3,4}\.ogg$/.test(t))return"audio"}if(e.includes("transcript")){if(t===h)return"transcript";if(t===f)return"transcript-json"}return e.includes("frame-check")&&t===m?"frame-check":null},exports.parseRenderJob=U,exports.parseSteps=function(t,r=e){if(!Array.isArray(t)||!t.length||t.length>2*e.length)return null;for(const e of t)if("string"!=typeof e||!r.includes(e))return null;const o=s.filter(e=>t.includes(e));return a(o)&&1!==o.length?null:o},exports.peaksBytes=F,exports.planStoryboard=function(t,e=at){const r=t.width>0?t.width:16,s=t.height>0?t.height:9,o=Number.isFinite(t.seconds)&&t.seconds>0?t.seconds:0,n=e.size/Math.max(r,s),a=Math.ceil(1e3*Math.max(e.intervalS,o/e.maxFrames))/1e3,i=o?Math.min(e.maxFrames,Math.max(1,Math.ceil(o/a-1e-9))):1;return{interval:a,maxFrames:e.maxFrames,expected:i,width:pt(r*n),height:pt(s*n),cols:e.cols,rows:e.rows,seconds:o}},exports.prepResultProblem=function(t,e,r){if(!O(t)||1!==t.format||"boolean"!=typeof t.converted||"boolean"!=typeof t.hasAudio)return"prep.json: bad shape";if(t.media!==(t.converted?w:null))return"prep.json: media";if(!(I(t.durationMs,1,1e3*e.maxSeconds+6e4)&&I(t.width,2,N.frame)&&I(t.height,2,N.frame)&&I(t.bytes,1,Number.MAX_SAFE_INTEGER)))return"prep.json: bad numbers";if("number"!=typeof t.fps||!(t.fps>0&&t.fps<=1e3))return"prep.json: bad fps";if(Math.min(t.width,t.height)>Math.min(e.maxShort,r?.maxShort??1/0))return"prep.json: over the cap";if(r&&!t.converted&&t.bytes!==r.bytes)return"prep.json: not the source's size";const s=t.filmstrip;if(!(O(s)&&s.columns===e.columns&&s.rows===e.rows&&I(s.count,1,e.maxFrames)&&I(s.interval,1,1e6)&&I(s.tileWidth,1,4096)&&I(s.tileHeight,1,4096)&&Array.isArray(s.sheets)))return"prep.json: bad filmstrip";if(s.sheets.length!==Math.ceil(s.count/(e.columns*e.rows))||s.sheets.some((t,e)=>t!==A(e)))return"prep.json: bad sheets";if(null!==t.peaks){const r=t.peaks;if(!O(r)||r.file!==E||r.rate!==e.peaksRate||!I(r.buckets,1,F(e.maxSeconds+60,e.peaksRate)/2))return"prep.json: bad peaks";if(!t.hasAudio)return"prep.json: peaks without sound"}else if(t.hasAudio)return"prep.json: sound without peaks";return null},exports.prepSettingsOf=function(t){const e={...R,...t?.prep},r=(t,e,r,s)=>Math.min(s,Math.max(r,Math.round(_(t,e))));return{maxShort:-2&r(e.maxShort,1080,144,4320),maxSeconds:r(e.maxSeconds,14400,1,N.seconds),preset:/^[a-z]{1,12}$/.test(String(e.preset))?String(e.preset):R.preset,crf:r(e.crf,22,0,51),audioKbps:r(e.audioKbps,128,32,512),columns:r(e.columns,10,1,20),rows:r(e.rows,10,1,20),tileLongSide:-2&r(e.tileLongSide,320,32,1280),minIntervalS:r(e.minIntervalS,2,1,3600),maxFrames:r(e.maxFrames,400,1,4e3),jpegQ:r(e.jpegQ,5,2,31),peaksRate:r(e.peaksRate,100,1,1e3),processTimeoutS:r(e.processTimeoutS,6600,10,86400)}},exports.prepSheetName=A,exports.readAudioIndex=function(t){let e;try{e=JSON.parse(t)}catch{return null}if(!e||"object"!=typeof e)return null;const r=e,s=t=>"number"==typeof t&&Number.isFinite(t)&&t>=0;if(1!==r.format||!Array.isArray(r.chunks)||!Array.isArray(r.silences)||!s(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)||!s(t.start)||!s(t.end)||t.end<t.start)return null;for(const t of r.silences)if(!t||!s(t.start)||!s(t.end)||t.end<t.start)return null;return r},exports.readFrameCheck=function(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},exports.readTranscript=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},exports.refuseStale=function(t,e){return"done"===t.state&&!M(t,e)},exports.renderBody=L,exports.renderFrame=K,exports.renderFrames=H,exports.renderHash=async function(t){const e=new Uint8Array(await crypto.subtle.digest("SHA-256",(new TextEncoder).encode(JSON.stringify(L(t)))));let r="";for(let t=0;t<e.length;t++)r+=e[t].toString(16).padStart(2,"0");return r},exports.renderKey=function(t){const e=JSON.stringify(L(t));let r=0xcbf29ce484222325n;for(let t=0;t<e.length;t++)r=0x100000001b3n*(r^BigInt(e.charCodeAt(t)))&0xffffffffffffffffn;return r.toString(16).padStart(16,"0")},exports.renderResultProblem=function(t,e,r){if(!O(t)||1!==t.format)return"result.json: bad shape";const s=H(e).reduce((t,e)=>t+e,0),o=V(e).reduce((t,e)=>t+e,0),n=K(e,r);return t.frames!==s||t.samples!==o||t.fps!==e.output.fps||t.parts!==e.parts.length?"result.json: not this edit list":t.width!==n.width||t.height!==n.height?"result.json: not this frame":!I(t.durationMs,1,1e3*N.seconds)||Math.abs(t.durationMs-W(e))>Math.ceil(1e3/e.output.fps)?"result.json: not within one frame of the edit list":I(t.bytes,1,Number.MAX_SAFE_INTEGER)?null:"result.json: bad bytes"},exports.renderSamples=V,exports.renderSettingsOf=D,exports.rungsFor=function(t,e,r,s){const o=t=>Math.max(2,2*Math.round(t/2));if(s&&Math.min(e,r)>s){const t=s/Math.min(e,r);e=o(e*t),r=o(r*t)}const n=Math.min(e,r),a=[];for(const s of t){if(s.short>=.9*n)break;const t=s.short/n;a.push({width:o(e*t),height:o(r*t),kbps:s.kbps,peak:s.peak,top:!1})}const i=[...t].reverse().find(t=>t.short<=1.1*n)??t[0];return a.push({width:o(e),height:o(r),kbps:i.kbps,peak:i.peak,top:!0}),a},exports.silentSeconds=z,exports.sourceHash=async function(t,e,r){const s=new Uint8Array(await crypto.subtle.digest("SHA-256",(new TextEncoder).encode(`${t}#${e}${r?`#${r}`:""}`)));let o="";for(let t=0;t<s.length;t++)o+=s[t].toString(16).padStart(2,"0");return o},exports.stampMatches=M,exports.stepSettings=u,exports.storyboardCues=xt,exports.storyboardDescriptor=function(t,e){/\/$/.test(t)||(t+="/");const r={vtt:t+dt,index:t+ht};if(!e)return r;if(!gt(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)}},exports.storyboardFile=mt,exports.storyboardIndex=function(t,e,r,s){const o=lt(t,e);return{version:r,interval:t.interval,cols:t.cols,rows:t.rows,width:t.width,height:t.height,count:e,seconds:bt(t.seconds),sheets:Array.from({length:o},(t,e)=>ft(e)),vtt:dt,bytes:s}},exports.storyboardSettings=nt,exports.storyboardSheetCount=lt,exports.storyboardSheetName=ft,exports.storyboardV2=c,exports.storyboardVtt=function(t,e){const r=["WEBVTT",""];for(const s of xt(t,e))r.push(`${yt(s.start)} --\x3e ${yt(s.end)}`,`${ft(s.sheet).slice(11)}#xywh=${s.x},${s.y},${s.w},${s.h}`,"");return r.join("\n")},exports.toWebVtt=function(t){const e=["WEBVTT",""];return t.segments.forEach((t,r)=>{const s=t.end>t.start?t.end:t.start+.5;e.push(String(r+1),`${Q(t.start)} --\x3e ${Q(s)}`,Y(t.text),"")}),e.join("\n")},exports.validExpectDuration=g,exports.vttText=Y,exports.vttTime=Q,exports.wantsAudio=i;
|
package/dist/server-media.mjs
CHANGED
|
@@ -1 +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};
|
|
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 s=(t,e)=>`expr:gte(t,if(lt(n_forced,2),n_forced*${t},(n_forced-1)*${e}))`;function n(t,e,r,s){const n=t=>Math.max(2,2*Math.round(t/2));if(s&&Math.min(e,r)>s){const t=s/Math.min(e,r);e=n(e*t),r=n(r*t)}const o=Math.min(e,r),a=[];for(const s of t){if(s.short>=.9*o)break;const t=s.short/o;a.push({width:n(e*t),height:n(r*t),kbps:s.kbps,peak:s.peak,top:!1})}const i=[...t].reverse().find(t=>t.short<=1.1*o)??t[0];return a.push({width:n(e),height:n(r),kbps:i.kbps,peak:i.peak,top:!0}),a}const o=["ladder","poster","storyboard","faststart","audio","transcript","frame-check"],a=["prep","render"],i=[...o,...a],u=["audio","transcript","frame-check"],c=t=>t.some(t=>u.includes(t)||a.includes(t)),d=t=>t.some(t=>a.includes(t)),p=t=>t.includes("audio")||t.includes("transcript");function h(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}}:{},...e.prep?{prep:{...e.prep}}:{},...e.render?{render:{...e.render}}:{}}}const f=h("steps/1"),m=(t,e)=>t.includes("storyboard")&&!!e?.storyboard,l=(t,e,r=f)=>{const s="string"==typeof r?{version:r}:r;return c(e)||m(e,s)?`${t}+${s.version}`:t},g="audio/audio.json",y="transcript.vtt",b="transcript.json",x="frame-check.json",M=t=>`audio/a-${String(t).padStart(3,"0")}.ogg`;function S(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;const r=i.filter(e=>t.includes(e));return d(r)&&1!==r.length?null:r}const w=/^[a-z0-9]{1,8}$/;function v(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 $(t,e,r){if(e.includes("prep")){if(t===L)return"edit-media";if(t===O)return"edit-peaks";if(t===C)return"edit-result";if(/^edit\/sb-(?:\d{3}|[1-9]\d{3})\.jpg$/.test(t))return"edit-filmstrip"}if(e.includes("render")){if(t===I)return"render-video";if(t===_)return"render-poster";if(t===z)return"render-result"}if(e.includes("ladder")){const e=v(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(p(e)){if(t===g)return"audio-index";if(/^audio\/a-\d{3,4}\.ogg$/.test(t))return"audio"}if(e.includes("transcript")){if(t===y)return"transcript";if(t===b)return"transcript-json"}return e.includes("frame-check")&&t===x?"frame-check":null}function j(t){return t.includes("prep")?C:t.includes("render")?z:t.includes("ladder")?"master.m3u8":t.includes("faststart")?"faststart.mp4":t.includes("storyboard")?"storyboard/storyboard.json":t.includes("poster")||!c(t)?"poster.jpg":p(t)?g:x}const k=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(".bin")?"application/octet-stream":t.endsWith(".json")?"application/json":t.endsWith(".ogg")?"audio/ogg":t.endsWith(".vtt")?"text/vtt; charset=utf-8":"video/mp4";async function A(t,e,r){const s=new Uint8Array(await crypto.subtle.digest("SHA-256",(new TextEncoder).encode(`${t}#${e}${r?`#${r}`:""}`)));let n="";for(let t=0;t<s.length;t++)n+=s[t].toString(16).padStart(2,"0");return n}const E=4096,N=604800,F=t=>"number"==typeof t&&Number.isFinite(t)&&t>0&&t<=N;function T(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}:{},...void 0!==t.render?{render:st(t.render)}:{}})}const D=(t,e)=>t.ladder===e,W="x-media-expect-duration",P="x-media-version",L="edit/media.mp4",O="edit/peaks.bin",C="edit/prep.json",B=t=>`edit/sb-${String(t).padStart(3,"0")}.jpg`,I="render/video.mp4",_="render/poster.jpg",z="render/result.json",R=1,J={maxShort:1080,maxSeconds:14400,preset:"veryfast",crf:22,audioKbps:128,columns:10,rows:10,tileLongSide:320,minIntervalS:2,maxFrames:400,jpegQ:5,peaksRate:100,processTimeoutS:6600},H={maxShort:1080,maxParts:200,maxSources:50,maxSeconds:14400,minPartMs:50,preset:"veryfast",crf:21,audioKbps:128,posterWidth:1280,partTimeoutS:5400},K={parts:500,sources:100,seconds:86400,frame:8192,fps:120},Q=65536,q=(t,e)=>"number"==typeof t&&Number.isFinite(t)?t:e;function G(t){const e={...J,...t?.prep},r=(t,e,r,s)=>Math.min(s,Math.max(r,Math.round(q(t,e))));return{maxShort:-2&r(e.maxShort,1080,144,4320),maxSeconds:r(e.maxSeconds,14400,1,K.seconds),preset:/^[a-z]{1,12}$/.test(String(e.preset))?String(e.preset):J.preset,crf:r(e.crf,22,0,51),audioKbps:r(e.audioKbps,128,32,512),columns:r(e.columns,10,1,20),rows:r(e.rows,10,1,20),tileLongSide:-2&r(e.tileLongSide,320,32,1280),minIntervalS:r(e.minIntervalS,2,1,3600),maxFrames:r(e.maxFrames,400,1,4e3),jpegQ:r(e.jpegQ,5,2,31),peaksRate:r(e.peaksRate,100,1,1e3),processTimeoutS:r(e.processTimeoutS,6600,10,86400)}}function U(t){const e={...H,...t?.render},r=(t,e,r,s)=>Math.min(s,Math.max(r,Math.round(q(t,e))));return{maxShort:-2&r(e.maxShort,1080,144,4320),maxParts:r(e.maxParts,200,1,K.parts),maxSources:r(e.maxSources,50,1,K.sources),maxSeconds:r(e.maxSeconds,14400,1,K.seconds),minPartMs:r(e.minPartMs,50,1,6e4),preset:/^[a-z]{1,12}$/.test(String(e.preset))?String(e.preset):H.preset,crf:r(e.crf,21,0,51),audioKbps:r(e.audioKbps,128,32,512),posterWidth:-2&r(e.posterWidth,1280,64,4096),partTimeoutS:r(e.partTimeoutS,5400,10,86400)}}const V=(t,e)=>Math.max(e.minIntervalS,Math.ceil(Math.max(0,t)/e.maxFrames)),X=(t,e)=>2*Math.ceil(Math.max(0,t)*e),Y=(t,e,r)=>"number"==typeof t&&Number.isSafeInteger(t)&&t>=e&&t<=r,Z=(t,e)=>Object.keys(t).every(t=>e.includes(t)),tt=t=>!!t&&"object"==typeof t&&!Array.isArray(t);function et(t,e,r){if(!tt(t)||1!==t.format||"boolean"!=typeof t.converted||"boolean"!=typeof t.hasAudio)return"prep.json: bad shape";if(t.media!==(t.converted?L:null))return"prep.json: media";if(!(Y(t.durationMs,1,1e3*e.maxSeconds+6e4)&&Y(t.width,2,K.frame)&&Y(t.height,2,K.frame)&&Y(t.bytes,1,Number.MAX_SAFE_INTEGER)))return"prep.json: bad numbers";if("number"!=typeof t.fps||!(t.fps>0&&t.fps<=1e3))return"prep.json: bad fps";if(Math.min(t.width,t.height)>Math.min(e.maxShort,r?.maxShort??1/0))return"prep.json: over the cap";if(r&&!t.converted&&t.bytes!==r.bytes)return"prep.json: not the source's size";const s=t.filmstrip;if(!(tt(s)&&s.columns===e.columns&&s.rows===e.rows&&Y(s.count,1,e.maxFrames)&&Y(s.interval,1,1e6)&&Y(s.tileWidth,1,4096)&&Y(s.tileHeight,1,4096)&&Array.isArray(s.sheets)))return"prep.json: bad filmstrip";if(s.sheets.length!==Math.ceil(s.count/(e.columns*e.rows))||s.sheets.some((t,e)=>t!==B(e)))return"prep.json: bad sheets";if(null!==t.peaks){const r=t.peaks;if(!tt(r)||r.file!==O||r.rate!==e.peaksRate||!Y(r.buckets,1,X(e.maxSeconds+60,e.peaksRate)/2))return"prep.json: bad peaks";if(!t.hasAudio)return"prep.json: peaks without sound"}else if(t.hasAudio)return"prep.json: sound without peaks";return null}const rt=1;function st(t){const e=t=>({x:t.x,y:t.y,width:t.width,height:t.height}),r=t.editList.output;return{sources:t.sources.map(t=>({src:t.src,etag:t.etag,bytes:t.bytes})),editList:{version:t.editList.version,output:{width:r.width,height:r.height,fps:r.fps,...r.crop?{crop:e(r.crop)}:{},...void 0!==r.maxShort?{maxShort:r.maxShort}:{}},parts:t.editList.parts.map(t=>({source:t.source,inMs:t.inMs,outMs:t.outMs,...t.muted?{muted:!0}:{},...t.crop?{crop:e(t.crop)}:{}}))}}}const nt=/^(?!\/)(?!.*(?:^|\/)\.\.(?:\/|$))[\x21-\x7e]{1,1024}$/;function ot(t,e,r){return tt(t)&&Z(t,["x","y","width","height"])&&Y(t.x,0,e)&&Y(t.y,0,r)&&Y(t.width,2,e)&&Y(t.height,2,r)?t.x+t.width>e||t.y+t.height>r?"crop outside the frame":{x:t.x,y:t.y,width:t.width,height:t.height}:"bad crop"}function at(t,e=H){const r=t=>({error:t});if(!tt(t)||!Z(t,["sources","editList"]))return r("bad render");const s=t.sources;if(!Array.isArray(s)||!s.length||s.length>Math.min(e.maxSources,K.sources))return r("bad sources");const n=[];for(const t of s){if(!tt(t)||!Z(t,["src","etag","bytes"])||"string"!=typeof t.src||!nt.test(t.src))return r("bad source");if("string"!=typeof t.etag||!/^[\x21-\x7e]{1,80}$/.test(t.etag)||!Y(t.bytes,1,Number.MAX_SAFE_INTEGER))return r("bad source");n.push({src:t.src,etag:t.etag,bytes:t.bytes})}const o=t.editList;if(!tt(o)||!Z(o,["version","output","parts"]))return r("bad editList");if(1!==o.version)return r("unknown editList version");const a=o.output;if(!tt(a)||!Z(a,["width","height","fps","crop","maxShort"]))return r("bad output");if(!Y(a.width,16,K.frame)||!Y(a.height,16,K.frame))return r("bad output frame");if("number"!=typeof a.fps||!(a.fps>=1&&a.fps<=K.fps)||Math.round(1e3*a.fps)!==1e3*a.fps)return r("bad fps");let i;if(void 0!==a.crop){const t=ot(a.crop,a.width,a.height);if("string"==typeof t)return r(`output: ${t}`);if(t.width<16||t.height<16)return r("output: crop too small");i=t}if(void 0!==a.maxShort&&!Y(a.maxShort,144,4320))return r("bad maxShort");const u=o.parts;if(!Array.isArray(u)||!u.length||u.length>Math.min(e.maxParts,K.parts))return r("bad parts");const c=[],d=new Set;let p=0;for(let t=0;t<u.length;t++){const s=u[t];if(!tt(s)||!Z(s,["source","inMs","outMs","muted","crop"]))return r(`part ${t}: bad shape`);if(!Y(s.source,0,n.length-1))return r(`part ${t}: bad source`);const o=1e3*K.seconds;if(!Y(s.inMs,0,o)||!Y(s.outMs,1,o)||s.outMs-s.inMs<Math.max(e.minPartMs,Math.ceil(1e3/a.fps)))return r(`part ${t}: bad times`);if(void 0!==s.muted&&!0!==s.muted)return r(`part ${t}: bad muted`);let i;if(void 0!==s.crop){const e=ot(s.crop,K.frame,K.frame);if("string"==typeof e)return r(`part ${t}: ${e}`);i=e}d.add(s.source),p+=s.outMs-s.inMs,c.push({source:s.source,inMs:s.inMs,outMs:s.outMs,...s.muted?{muted:!0}:{},...i?{crop:i}:{}})}if(d.size!==n.length)return r("a source no part uses");if(p>1e3*e.maxSeconds)return r("too long");return{job:{sources:n,editList:{version:1,output:{width:a.width,height:a.height,fps:a.fps,...i?{crop:i}:{},...void 0!==a.maxShort?{maxShort:a.maxShort}:{}},parts:c}}}}const it=t=>t.parts.reduce((t,e)=>t+e.outMs-e.inMs,0);function ut(t){let e=0;const r=t.output.fps;return t.parts.map(t=>{const s=Math.ceil(e*r/1e3-1e-9);return e+=t.outMs-t.inMs,Math.max(1,Math.ceil(e*r/1e3-1e-9)-s)})}const ct=48e3;function dt(t){let e=0;return t.parts.map(t=>{const r=Math.round(e*ct/1e3);return e+=t.outMs-t.inMs,Math.round(e*ct/1e3)-r})}function pt(t,e=H){const r=t=>Math.max(2,2*Math.floor(t/2)),s=t.output.crop?t.output.crop.width:t.output.width,n=t.output.crop?t.output.crop.height:t.output.height,o=Math.min(e.maxShort,t.output.maxShort??1/0),a=Math.min(1,o/Math.min(s,n));return{width:r(s*a),height:r(n*a)}}function ht(t,e,r){if(!tt(t)||1!==t.format)return"result.json: bad shape";const s=ut(e).reduce((t,e)=>t+e,0),n=dt(e).reduce((t,e)=>t+e,0),o=pt(e,r);return t.frames!==s||t.samples!==n||t.fps!==e.output.fps||t.parts!==e.parts.length?"result.json: not this edit list":t.width!==o.width||t.height!==o.height?"result.json: not this frame":!Y(t.durationMs,1,1e3*K.seconds)||Math.abs(t.durationMs-it(e))>Math.ceil(1e3/e.output.fps)?"result.json: not within one frame of the edit list":Y(t.bytes,1,Number.MAX_SAFE_INTEGER)?null:"result.json: bad bytes"}async function ft(t){const e=new Uint8Array(await crypto.subtle.digest("SHA-256",(new TextEncoder).encode(JSON.stringify(st(t)))));let r="";for(let t=0;t<e.length;t++)r+=e[t].toString(16).padStart(2,"0");return r}function mt(t){const e=JSON.stringify(st(t));let r=0xcbf29ce484222325n;for(let t=0;t<e.length;t++)r=0x100000001b3n*(r^BigInt(e.charCodeAt(t)))&0xffffffffffffffffn;return r.toString(16).padStart(16,"0")}const lt=.8,gt=t=>Math.round(1e3*t)/1e3;function yt(t,e,r){let s=0;for(const n of r)s+=Math.max(0,Math.min(e,n.end)-Math.max(t,n.start));return s}function bt(t,e,r,s){const n=e-t;if(n<=0)return r.some(e=>t>=e.start&&t<=e.end);const o=yt(t,e,r);return o>0&&o>=.8*n&&n-o<=s}const xt=(t,e)=>t.end-t.start-yt(t.start,t.end,e)<=.05;function Mt(t,e){const r=[];let s=0,n=t.language;for(const{chunk:o,result:a}of t.results){n??=a.transcription_info?.language;const i=o.end-o.start;for(const n of a.segments??[]){const a=String(n.text??"").trim();if(!a||!Number.isFinite(n.start)||!Number.isFinite(n.end))continue;const u=gt(o.start+Math.max(0,Math.min(n.start,i))),c=gt(o.start+Math.max(0,Math.min(Math.max(n.end,n.start),i)));if(bt(u,c,t.silences,e.edgeS)){s++;continue}const d=(n.words??[]).filter(t=>t&&"string"==typeof t.word&&Number.isFinite(t.start)&&Number.isFinite(t.end)).map(t=>({word:t.word.trim(),start:gt(o.start+Math.max(0,t.start)),end:gt(o.start+Math.max(0,t.end))})).filter(e=>e.word&&!bt(e.start,e.end,t.silences,0));r.push({start:u,end:Math.max(u,c),text:a,...d.length?{words:d}:{}})}}return r.sort((t,e)=>t.start-e.start),{format:1,model:t.model,...n?{language:n}:{},seconds:gt(t.seconds),segments:r,text:r.map(t=>t.text).join(" "),dropped:s,silentChunks:t.silentChunks??0}}function St(t){const e=Math.max(0,Math.round(1e3*t)),r=Math.floor(e/36e5),s=Math.floor(e/6e4)%60,n=Math.floor(e/1e3)%60,o=(t,e=2)=>String(t).padStart(e,"0");return`${o(r)}:${o(s)}:${o(n)}.${o(e%1e3,3)}`}const wt=t=>t.replace(/\r\n?/g,"\n").replace(/\n\s*\n/g,"\n").replace(/-->/g,"→").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").trim();function vt(t){const e=["WEBVTT",""];return t.segments.forEach((t,r)=>{const s=t.end>t.start?t.end:t.start+.5;e.push(String(r+1),`${St(t.start)} --\x3e ${St(s)}`,wt(t.text),"")}),e.join("\n")}function $t(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 jt(t){let e;try{e=JSON.parse(t)}catch{return null}if(!e||"object"!=typeof e)return null;const r=e,s=t=>"number"==typeof t&&Number.isFinite(t)&&t>=0;if(1!==r.format||!Array.isArray(r.chunks)||!Array.isArray(r.silences)||!s(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)||!s(t.start)||!s(t.end)||t.end<t.start)return null;for(const t of r.silences)if(!t||!s(t.start)||!s(t.end)||t.end<t.start)return null;return r}function kt(t){return{audioIndex:`${t}audio/audio.json`,transcriptVtt:`${t}transcript.vtt`,transcriptJson:`${t}transcript.json`,frameCheck:`${t}frame-check.json`}}function At(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 Et=new TextEncoder;new TextDecoder;function Nt(t){const e="string"==typeof t?Et.encode(t):t,r=new Uint8Array(e.length);return r.set(e),r}async function Ft(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",Nt(t),{name:"HMAC",hash:"SHA-256"},!1,["sign"]);return new Uint8Array(await crypto.subtle.sign("HMAC",r,Nt(e)))}(t,`${e}\n${r}`))}class Tt extends Error{reached;status;timedOut;busy;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,this.busy=e.busy??!1,void 0!==e.status&&(this.status=e.status)}}const Dt=/no container instance|too many containers|max(imum)? instances|container.{0,40}(capacity|unavailable)/i;function Wt(t,e,r){return!(t>=200&&t<300)&&(429===t||503===t&&null!==r||Dt.test(e))}function Pt(t,e){if(null===t||""===t.trim())return null;const r=Number(t);if(Number.isFinite(r))return r>=0?1e3*r:null;const s=Date.parse(t);return Number.isFinite(s)?Math.max(0,s-e):null}function Lt(t){const e=()=>"function"==typeof t.secret?t.secret():t.secret,r=()=>t.fetch??fetch,s=t.sleep??(t=>new Promise(e=>setTimeout(e,t))),n=t.random??Math.random,o=()=>(t.now??Date.now)(),a=U(t.stepSettings),i=!1===t.busy?null:{tries:Math.max(1,Math.floor(t.busy?.tries??5)),baseMs:Math.max(0,t.busy?.baseMs??500),maxMs:Math.max(0,t.busy?.maxMs??8e3)},u=t.maxStartsPerSecond&&t.maxStartsPerSecond>0?1e3/t.maxStartsPerSecond:0;let c=0;const d=async(t=1/0)=>{const e=o(),r=Math.max(e,u?c:0);return!(r>=t)&&(u&&(c=r+u),r>e&&await s(r-e),o()<t)};return{sign:async(r,s)=>Ft(e(),(e=>"job"===e?t.jobDomain:t.doneDomain)(r),s),async request(u){if(void 0!==u.expectDurationS&&!F(u.expectDurationS))throw new Error(`media job: bad expectDurationS ${String(u.expectDurationS)}`);const c=()=>T(t.issuedAt&&void 0===u.issuedAt?{...u,issuedAt:Math.floor(o()/1e3)}:u),p=1===u.steps?.length&&"render"===u.steps[0];if(p||void 0!==u.render){const t=at(u.render,a);if("error"in t)throw new Error(`media job: bad render: ${t.error}`);if(!p)throw new Error('media job: a render needs steps ["render"]')}if(c().length>(p?65536:E))throw new Error("media job: request too big");const h=t.timeoutMs??3e4,f=i?.tries??1;await d();const m=o()+h;for(let a=1;;a++){const u=c();let p;try{p=await r()(`${t.url.replace(/\/$/,"")}/jobs`,{method:"POST",body:u,headers:{"content-type":"application/json",[t.signatureHeader]:await Ft(e(),t.jobDomain,u)},signal:AbortSignal.timeout(Math.max(1,Math.min(h,m-o())))})}catch(t){const e="TimeoutError"===t?.name||"AbortError"===t?.name;throw new Tt(`media worker: ${e?"no answer in time":"unreachable"}: ${t?.message??String(t)}`,{reached:!1,timedOut:e,cause:t})}if(p.ok)return;const l=await p.text().catch(()=>""),g=Wt(p.status,l,p.headers.get("retry-after"));if(g&&i&&a<f){const t=Math.min(i.maxMs,i.baseMs*2**(a-1)),e=Math.max(Pt(p.headers.get("retry-after"),o())??0,Math.floor(n()*t));if(o()+e<m&&(await s(e),o()<m&&await d(m)))continue}throw new Tt(`media worker: ${p.status} ${l.slice(0,200)}`,{reached:!0,status:p.status,busy:g})}},async readReport(r,s){if("string"!=typeof r||r.length>E||!function(t,e){if("string"!=typeof t||"string"!=typeof e||t.length!==e.length)return!1;let r=0;for(let s=0;s<t.length;s++)r|=t.charCodeAt(s)^e.charCodeAt(s);return 0===r}(s,await Ft(e(),t.doneDomain,r)))return{ok:!1,status:403,body:"bad signature"};let n;try{n=JSON.parse(r)}catch{return{ok:!1,status:400,body:"bad json"}}return n&&"object"==typeof n?{ok:!0,report:n}:{ok:!1,status:400,body:"bad report"}}}}function Ot(t,e){return"done"===t.state&&!D(t,e)}function Ct(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 _t(r),r}const Bt=Ct("storyboard/1");function It(t,e,r){return"number"==typeof t&&Number.isInteger(t)&&t>=e&&t<=r}function _t(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"),It(t.maxFrames,1,1e4)||e("maxFrames"),(!It(t.size,16,1024)||t.size%2)&&e("size"),It(t.cols,1,32)&&It(t.rows,1,32)||e("cols/rows"),(!It(t.quality,1,100)||!It(t.minQuality,1,100)||t.minQuality>t.quality)&&e("quality/minQuality"),It(t.maxSheetBytes,1e3,1e8)&&It(t.maxBytes,t.maxSheetBytes,1e9)||e("maxSheetBytes/maxBytes")}const zt=t=>Math.max(2,2*Math.round(t/2));function Rt(t,e=Bt){const r=t.width>0?t.width:16,s=t.height>0?t.height:9,n=Number.isFinite(t.seconds)&&t.seconds>0?t.seconds:0,o=e.size/Math.max(r,s),a=Math.ceil(1e3*Math.max(e.intervalS,n/e.maxFrames))/1e3,i=n?Math.min(e.maxFrames,Math.max(1,Math.ceil(n/a-1e-9))):1;return{interval:a,maxFrames:e.maxFrames,expected:i,width:zt(r*o),height:zt(s*o),cols:e.cols,rows:e.rows,seconds:n}}const Jt="storyboard",Ht="storyboard/storyboard.vtt",Kt="storyboard/storyboard.json",Qt=t=>`storyboard/sb-${String(t+1).padStart(3,"0")}.webp`;function qt(t){return t===Kt?"storyboard-index":t===Ht?"storyboard-vtt":/^storyboard\/sb-\d{3,4}\.webp$/.test(t)?"storyboard":null}const Gt=(t,e)=>Math.ceil(e/(t.cols*t.rows));function Ut(t,e){const r=t.cols*t.rows,s=[];for(let n=0;n<e;n++){const o=n%r,a=Vt(n*t.interval),i=Vt((n+1)*t.interval),u=n===e-1?Math.max(i,Vt(t.seconds)):i;s.push({start:a,end:u,sheet:Math.floor(n/r),x:o%t.cols*t.width,y:Math.floor(o/t.cols)*t.height,w:t.width,h:t.height})}return s}function Vt(t){return Math.round(1e3*t)/1e3}function Xt(t){const e=Math.round(1e3*t),r=Math.floor(e/36e5),s=Math.floor(e/6e4)%60,n=Math.floor(e/1e3)%60,o=(t,e=2)=>String(t).padStart(e,"0");return`${o(r)}:${o(s)}:${o(n)}.${o(e%1e3,3)}`}function Yt(t,e){const r=["WEBVTT",""];for(const s of Ut(t,e))r.push(`${Xt(s.start)} --\x3e ${Xt(s.end)}`,`${Qt(s.sheet).slice(11)}#xywh=${s.x},${s.y},${s.w},${s.h}`,"");return r.join("\n")}function Zt(t,e,r,s){const n=Gt(t,e);return{version:r,interval:t.interval,cols:t.cols,rows:t.rows,width:t.width,height:t.height,count:e,seconds:Vt(t.seconds),sheets:Array.from({length:n},(t,e)=>Qt(e)),vtt:Ht,bytes:s}}function te(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&&It(e.cols,1,32)&&It(e.rows,1,32)&&It(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"===qt(t))&&e.vtt===Ht}function ee(t,e){/\/$/.test(t)||(t+="/");const r={vtt:t+Ht,index:t+Kt};if(!e)return r;if(!te(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 re="server/media";export{i as ALL_STEPS,g as AUDIO_INDEX,J as DEFAULT_PREP,H as DEFAULT_RENDER,t as DEFAULT_RUNGS,f as DEFAULT_STEP_SETTINGS,Bt as DEFAULT_STORYBOARD_SETTINGS,rt as EDIT_LIST_VERSION,R as EDIT_RESULT_FORMAT,a as EDIT_STEPS,W as EXPECT_HEADER,u as EXTRA_STEPS,x as FRAME_CHECK_FILE,E as MAX_BODY,N as MAX_EXPECT_DURATION_S,Q as MAX_RENDER_BODY,Tt as MediaJobError,L as PREP_MEDIA,O as PREP_PEAKS,C as PREP_RESULT,K as RENDER_LIMITS,_ as RENDER_POSTER,z as RENDER_RESULT,ct as RENDER_SAMPLE_RATE,I as RENDER_VIDEO,e as RUNNER_OUTPUT_REVISION,re as SDK_ENTRY,lt as SILENT_SHARE,o as STEPS,Jt as STORYBOARD_DIR,Kt as STORYBOARD_INDEX,Ht as STORYBOARD_VTT,b as TRANSCRIPT_JSON,y as TRANSCRIPT_VTT,P as VERSION_HEADER,M as audioChunkName,Mt as buildTranscript,_t as checkStoryboardSettings,xt as chunkSilent,j as completionFile,k as contentTypeOf,it as editListMs,V as filmstripInterval,c as hasExtraSteps,bt as inSilence,d as isEditJob,te as isStoryboardIndex,T as jobBody,l as jobVersion,s as keyframeExpr,v as ladderFile,r as ladderSettings,Lt as mediaJobClient,kt as mediaStepFiles,$ as outputFile,at as parseRenderJob,S as parseSteps,X as peaksBytes,Rt as planStoryboard,et as prepResultProblem,G as prepSettingsOf,B as prepSheetName,jt as readAudioIndex,At as readFrameCheck,$t as readTranscript,Ot as refuseStale,st as renderBody,pt as renderFrame,ut as renderFrames,ft as renderHash,mt as renderKey,ht as renderResultProblem,dt as renderSamples,U as renderSettingsOf,n as rungsFor,yt as silentSeconds,A as sourceHash,D as stampMatches,h as stepSettings,Ut as storyboardCues,ee as storyboardDescriptor,qt as storyboardFile,Zt as storyboardIndex,Ct as storyboardSettings,Gt as storyboardSheetCount,Qt as storyboardSheetName,m as storyboardV2,Yt as storyboardVtt,vt as toWebVtt,F as validExpectDuration,wt as vttText,St as vttTime,p as wantsAudio};
|
package/dist/server-testing.js
CHANGED
|
@@ -1 +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;
|
|
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;s3Checksums=!1;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}`)}const l=new Set(Object.keys(i.headers).map(e=>e.toLowerCase()));for(const t of Object.keys(e.headers))if(t.startsWith("x-amz-checksum-")&&!l.has(t))return u(`unsigned header ${t}`);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]);const f=e.headers["x-amz-checksum-sha256"];if(null!==i&&void 0!==f&&!this.s3Checksums)return await this.body(e).catch(()=>null),h(501),this.s3err(t,501,"NotImplemented",`Header 'x-amz-checksum-sha256' with value '${String(f)}' not implemented`);if(void 0!==f&&void 0!==e.headers["content-md5"]&&!this.s3Checksums)return await this.body(e).catch(()=>null),h(400),this.s3err(t,400,"InvalidRequest","You can only specify one non-default checksum at a time.");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 y=await this.body(e);if(this.offline)return void e.socket.destroy();"corruptInTransit"===g?.action&&y.length&&(y=Buffer.from(y),y[y.length>>1]^=255);const k=s.check(y.length,y);if(k)return this.counters.rejectedSignature++,h(k.status),this.s3err(t,k.status,k.code,k.message);const b=e.headers["content-md5"];if(b&&r(y).toString("base64")!==b)return h(400),this.s3err(t,400,"BadDigest");const S=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,y);if(!S)return h(400),this.s3err(t,400,"BadDigest");const x=Object.keys(S).length?{checksums:S}:{};"storeCorrupted"===g?.action&&y.length&&(y=Buffer.from(y),y[0]^=255);const $=n(y);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?y:Buffer.alloc(0),etag:$,size:y.length,...x}),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:y,etag:$,size:y.length,contentType:e.headers["content-type"],...Object.keys(r).length?{meta:r}:{},...x,...this.stamp()}),this.log({kind:c,key:a})}if(await this.post(g,e))return void(l.outcome="dropped");h(200);const A={};for(const[e,t]of Object.entries(S))A[`x-amz-checksum-${e}`]=t;t.writeHead(200,"noEtag"===g?.action?{"content-length":0,...A}:{etag:`"${$}"`,"content-length":0,...A}),t.end()}createUpload(e,s,a){const r=t.randomBytes(12).toString("hex"),n=this.s3Checksums&&a?{checksumAlgorithm:a.toUpperCase()}:{};return this.uploads.set(r,{key:e,contentType:s,parts:new Map,created:this.now(),...n}),this.log({kind:"create",key:e,detail:r}),r}completeUpload(e,s,a,r={}){const n=this.liveUpload(s);if(!n||n.key!==e)return{ok:!1,code:"NoSuchUpload",status:404};if(!a.length||a.length>u)return{ok:!1,code:"MalformedXML",status:400};const o=[...a].map(e=>({n:Number(e.n),etag:String(e.etag),checksumSha256:e.checksumSha256})).sort((e,t)=>e.n-t.n),l="SHA256"===n.checksumAlgorithm,d=[];for(let e=1;e<o.length;e++)if(o[e].n===o[e-1].n)return{ok:!1,code:"InvalidPartOrder",status:400};const h=[],g=[];let p=0,m=-1;for(let e=0;e<o.length;e++){const t=o[e],s=n.parts.get(t.n);if(!s||s.etag!==t.etag.replace(/"/g,"").toLowerCase())return{ok:!1,code:"InvalidPart",status:400};if(e<o.length-1){if(s.size<c)return{ok:!1,code:"EntityTooSmall",status:400};if(-1===m)m=s.size;else if(s.size!==m)return{ok:!1,code:"InvalidPart",status:400}}else if(-1!==m&&s.size>m)return{ok:!1,code:"InvalidPart",status:400};if(l){const e=s.checksums?.sha256;if(!e||t.checksumSha256!==e)return{ok:!1,code:"InvalidPart",status:400};d.push(Buffer.from(e,"base64"))}h.push(s.body),g.push(s.etag),p+=s.size}if(r.ifNoneMatch&&this.objects.has(e))return this.uploads.delete(s),{ok:!1,code:"PreconditionFailed",status:412};const f=i(g);let y=Buffer.concat(h);r.truncate&&(y=y.subarray(0,Math.max(0,y.length-1)),p=Math.max(0,p-1));const k=l?{checksums:{sha256:`${t.createHash("sha256").update(Buffer.concat(d)).digest("base64")}-${d.length}`}}:{},w={body:y,etag:f,size:p,contentType:n.contentType,...k,...this.stamp()};return this.objects.set(e,w),this.uploads.delete(s),this.log({kind:"complete",key:e,detail:`parts=${o.length}`}),{ok:!0,object:w}}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 t.startsWith("x-amz-checksum-")?i(`unsigned header ${t}`):{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]};const y="ENABLED"===String(a.headers["x-amz-checksum-mode"]??"").toUpperCase();if(!f&&(y||i&&!this.s3Checksums))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=a.headers["x-amz-checksum-algorithm"],t=this.createUpload(s,a.headers["content-type"],"string"==typeof e?e:void 0);return h(`<InitiateMultipartUploadResult><Bucket>${p(this.s3.bucket)}</Bucket><Key>${p(s)}</Key><UploadId>${t}</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=>{return{n:Number(/<PartNumber>(\d+)<\/PartNumber>/.exec(e[1])?.[1]),etag:m(/<ETag>([^<]*)<\/ETag>/.exec(e[1])?.[1]??""),checksumSha256:(t=/<ChecksumSHA256>([^<]*)<\/ChecksumSHA256>/.exec(e[1])?.[1],void 0===t?void 0:m(t))};var t}),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,l=i.object.checksums?.sha256;return h(`<CompleteMultipartUploadResult><Bucket>${p(this.s3.bucket)}</Bucket><Key>${p(s)}</Key><ETag>"${u}"</ETag>${l?`<ChecksumSHA256>${p(l)}</ChecksumSHA256>`:""}</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>${this.s3Checksums&&t.checksums?.sha256?`<ChecksumSHA256>${t.checksums.sha256}</ChecksumSHA256>`:""}</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;
|