@@ -5,25 +5,22 @@ export default eventHandler(async (event) => {
55 const signal = toWebRequest ( event ) . signal ;
66 const { rmStaleKey } = useRuntimeConfig ( event ) ;
77
8- // if (rmStaleKeyHeader !== rmStaleKey) {
9- // throw createError({
10- // status: 403,
11- // });
12- // }
8+ if ( rmStaleKeyHeader !== rmStaleKey ) {
9+ throw createError ( {
10+ status : 403 ,
11+ } ) ;
12+ }
1313
1414 const { bucket, cursor, remove } = await readBody < { bucket : 'packages' | 'templates' ; cursor : string | null ; remove : boolean } > ( event ) ;
1515
16+ setResponseHeader ( event , "Content-Type" , "application/json" ) ;
17+
1618 const result = await iterateAndDelete ( event , signal , {
1719 prefix : bucket === 'packages' ? usePackagesBucket . base : useTemplatesBucket . base ,
18- limit : 1000 ,
1920 cursor : cursor || undefined ,
2021 } , remove ) ;
2122
22- setResponseHeader ( event , "Content-Type" , "application/json" ) ;
23-
24- return {
25- result,
26- } ;
23+ return result ;
2724} ) ;
2825
2926// Helper for concurrency limiting
@@ -54,68 +51,63 @@ async function iterateAndDelete(event: H3Event, signal: AbortSignal, opts: R2Lis
5451 const binding = useBinding ( event ) ;
5552 let truncated = true ;
5653 let cursor : string | undefined = opts . cursor ;
57- let processed = 0 ;
5854 const removedItems : Array < { key : string ; uploaded : Date ; downloadedAt ?: Date } > = [ ] ;
5955 const downloadedAtBucket = useDownloadedAtBucket ( event ) ;
6056 const today = Date . parse ( new Date ( ) . toString ( ) ) ;
6157 const CONCURRENCY = 10 ;
6258
63- while ( truncated && ! signal . aborted ) {
64- if ( removedItems . length >= 100 || processed >= 10000 ) {
65- break ;
66- }
67- const next = await binding . list ( {
68- ...opts ,
69- cursor,
70- } ) ;
71- processed += next . objects . length ;
72-
73- await mapWithConcurrency ( next . objects , CONCURRENCY , async ( object ) => {
74- if ( signal . aborted ) return ;
75- const uploaded = Date . parse ( object . uploaded . toString ( ) ) ;
76- const uploadedDate = new Date ( uploaded ) ;
77- const sixMonthsAgo = new Date ( today ) ;
78- sixMonthsAgo . setMonth ( sixMonthsAgo . getMonth ( ) - 6 ) ;
79- if ( uploadedDate <= sixMonthsAgo ) {
80- removedItems . push ( {
81- key : object . key ,
82- uploaded : new Date ( object . uploaded ) ,
83- } ) ;
84- if ( remove ) {
85- await binding . delete ( object . key ) ;
86- await downloadedAtBucket . removeItem ( object . key ) ;
87- }
88- return ;
89- }
90- const downloadedAt = await downloadedAtBucket . getItem ( object . key ) ;
91- if ( ! downloadedAt ) {
92- return ;
59+ const next = await binding . list ( {
60+ ...opts ,
61+ limit : 1000 ,
62+ cursor,
63+ } ) ;
64+
65+ await mapWithConcurrency ( next . objects , CONCURRENCY , async ( object ) => {
66+ if ( signal . aborted ) return ;
67+ const uploaded = Date . parse ( object . uploaded . toString ( ) ) ;
68+ const uploadedDate = new Date ( uploaded ) ;
69+ const sixMonthsAgo = new Date ( today ) ;
70+ sixMonthsAgo . setMonth ( sixMonthsAgo . getMonth ( ) - 6 ) ;
71+ if ( uploadedDate <= sixMonthsAgo ) {
72+ removedItems . push ( {
73+ key : object . key ,
74+ uploaded : new Date ( object . uploaded ) ,
75+ } ) ;
76+ if ( remove ) {
77+ await binding . delete ( object . key ) ;
78+ await downloadedAtBucket . removeItem ( object . key ) ;
9379 }
94- const downloadedAtDate = new Date ( downloadedAt ) ;
95- const oneMonthAgo = new Date ( today ) ;
96- oneMonthAgo . setMonth ( oneMonthAgo . getMonth ( ) - 1 ) ;
97- const uploadedDate2 = new Date ( uploaded ) ;
98- if (
99- downloadedAtDate <= oneMonthAgo &&
100- uploadedDate2 <= oneMonthAgo
101- ) {
102- removedItems . push ( {
103- key : object . key ,
104- uploaded : new Date ( object . uploaded ) ,
105- downloadedAt : new Date ( downloadedAt ) ,
106- } ) ;
107- if ( remove ) {
108- await binding . delete ( object . key ) ;
109- await downloadedAtBucket . removeItem ( object . key ) ;
110- }
80+ return ;
81+ }
82+ const downloadedAt = await downloadedAtBucket . getItem ( object . key ) ;
83+ if ( ! downloadedAt ) {
84+ return ;
85+ }
86+ const downloadedAtDate = new Date ( downloadedAt ) ;
87+ const oneMonthAgo = new Date ( today ) ;
88+ oneMonthAgo . setMonth ( oneMonthAgo . getMonth ( ) - 1 ) ;
89+ const uploadedDate2 = new Date ( uploaded ) ;
90+ if (
91+ downloadedAtDate <= oneMonthAgo &&
92+ uploadedDate2 <= oneMonthAgo
93+ ) {
94+ removedItems . push ( {
95+ key : object . key ,
96+ uploaded : new Date ( object . uploaded ) ,
97+ downloadedAt : new Date ( downloadedAt ) ,
98+ } ) ;
99+ if ( remove ) {
100+ await binding . delete ( object . key ) ;
101+ await downloadedAtBucket . removeItem ( object . key ) ;
111102 }
112- } ) ;
113-
114- truncated = next . truncated ;
115- if ( next . truncated ) {
116- cursor = next . cursor ;
117103 }
104+ } ) ;
105+
106+ truncated = next . truncated ;
107+ if ( next . truncated ) {
108+ cursor = next . cursor ;
118109 }
110+
119111 return {
120112 cursor,
121113 truncated,
0 commit comments