11import type { IncomingMessage , ServerResponse } from "node:http" ;
22
33import { storeBundleJson } from "../_lib/blob-store.ts" ;
4- import { buildBundleUrls , getEnv , readBody , setCors , validateBundlePayload } from "../_lib/share-utils.ts" ;
4+ import { buildCanonicalRequest , buildRequestLike } from "../_lib/request-like.ts" ;
5+ import { buildCorsHeaders , rateLimitPublishRequest , validateTrustedOrigin , verifyShareBotProtection } from "../_lib/publish-security.ts" ;
6+ import { buildBundleUrls , getEnv , readBody , validateBundlePayload } from "../_lib/share-utils.ts" ;
57
68interface LegacyApiRequest extends IncomingMessage {
79 method ?: string ;
@@ -21,48 +23,91 @@ function formatPublishError(error: unknown): string {
2123 return message ;
2224}
2325
26+ function applyHeaders ( res : LegacyApiResponse , headers : Record < string , string > ) : void {
27+ for ( const [ name , value ] of Object . entries ( headers ) ) {
28+ res . setHeader ( name , value ) ;
29+ }
30+ }
31+
32+ function json ( res : LegacyApiResponse , body : unknown , status = 200 , headers : Record < string , string > = { } ) : void {
33+ applyHeaders ( res , {
34+ ...headers ,
35+ "Content-Type" : "application/json" ,
36+ } ) ;
37+ res . status ( status ) . end ( JSON . stringify ( body ) ) ;
38+ }
39+
2440export default async function handler ( req : LegacyApiRequest , res : LegacyApiResponse ) : Promise < void > {
25- setCors ( res ) ;
41+ const request = buildCanonicalRequest ( {
42+ pathname : "/v1/bundles" ,
43+ method : req . method ,
44+ headers : req . headers ,
45+ } ) ;
46+
47+ applyHeaders ( res , buildCorsHeaders ( request ) ) ;
2648 if ( req . method === "OPTIONS" ) {
2749 res . status ( 204 ) . end ( ) ;
2850 return ;
2951 }
52+
53+ const originCheck = validateTrustedOrigin ( request ) ;
54+ if ( ! originCheck . ok ) {
55+ json ( res , { message : originCheck . message } , originCheck . status ) ;
56+ return ;
57+ }
58+
59+ const rateLimit = rateLimitPublishRequest ( request ) ;
60+ if ( ! rateLimit . ok ) {
61+ json (
62+ res ,
63+ { message : "Publishing is temporarily rate limited." } ,
64+ 429 ,
65+ { "X-Retry-After" : String ( rateLimit . retryAfterSeconds ) } ,
66+ ) ;
67+ return ;
68+ }
69+
70+ const botProtection = await verifyShareBotProtection ( request ) ;
71+ if ( ! botProtection . ok ) {
72+ json ( res , { message : botProtection . message } , botProtection . status ) ;
73+ return ;
74+ }
75+
3076 if ( req . method !== "POST" ) {
31- res . status ( 405 ) . json ( { message : "Method not allowed" } ) ;
77+ json ( res , { message : "Method not allowed" } , 405 ) ;
3278 return ;
3379 }
3480
3581 const maxBytes = Number . parseInt ( getEnv ( "MAX_BYTES" , "5242880" ) , 10 ) ;
3682
3783 const contentType = String ( req . headers [ "content-type" ] ?? "" ) . toLowerCase ( ) ;
3884 if ( ! contentType . includes ( "application/json" ) ) {
39- res . status ( 415 ) . json ( { message : "Expected application/json" } ) ;
85+ json ( res , { message : "Expected application/json" } , 415 ) ;
4086 return ;
4187 }
4288
4389 const raw = await readBody ( req ) ;
4490 if ( ! raw || raw . length === 0 ) {
45- res . status ( 400 ) . json ( { message : "Body is required" } ) ;
91+ json ( res , { message : "Body is required" } , 400 ) ;
4692 return ;
4793 }
4894 if ( raw . length > maxBytes ) {
49- res . status ( 413 ) . json ( { message : "Bundle exceeds upload limit" , maxBytes } ) ;
95+ json ( res , { message : "Bundle exceeds upload limit" , maxBytes } , 413 ) ;
5096 return ;
5197 }
5298
5399 const rawJson = raw . toString ( "utf8" ) ;
54100 const validation = validateBundlePayload ( rawJson ) ;
55101 if ( ! validation . ok ) {
56- res . status ( 422 ) . json ( { message : validation . message } ) ;
102+ json ( res , { message : validation . message } , 422 ) ;
57103 return ;
58104 }
59105
60106 try {
61107 const { id } = await storeBundleJson ( rawJson ) ;
62- const urls = buildBundleUrls ( req as unknown as import ( "../_lib/types" ) . RequestLike , id ) ;
63- res . setHeader ( "Content-Type" , "application/json" ) ;
64- res . status ( 200 ) . end ( JSON . stringify ( { url : urls . shareUrl } ) ) ;
108+ const urls = buildBundleUrls ( buildRequestLike ( { headers : request . headers } ) , id ) ;
109+ json ( res , { url : urls . shareUrl } ) ;
65110 } catch ( e ) {
66- res . status ( 500 ) . json ( { message : formatPublishError ( e ) } ) ;
111+ json ( res , { message : formatPublishError ( e ) } , 500 ) ;
67112 }
68113}
0 commit comments