99 * OF ANY KIND, either express or implied. See the License for the specific language
1010 * governing permissions and limitations under the License.
1111 */
12+ import processQueue from '@adobe/helix-shared-process-queue' ;
1213import getObject from '../object/get.js' ;
1314import listObjects from '../object/list.js' ;
1415
16+ const MAX_VERSIONS = 500 ;
17+ const CONCURRENCY = 50 ;
18+
1519export async function listObjectVersions ( env , { bucket, org, key } ) {
1620 const current = await getObject ( env , { bucket, org, key } , true ) ;
1721 if ( current . status === 404 || ! current . metadata . id ) {
1822 return 404 ;
1923 }
20- const resp = await listObjects ( env , { bucket, org, key : `.da-versions/${ current . metadata . id } ` } , 500 ) ;
21- const promises = await Promise . all ( JSON . parse ( resp . body ) . map ( async ( entry ) => {
24+ const resp = await listObjects ( env , { bucket, org, key : `.da-versions/${ current . metadata . id } ` } , MAX_VERSIONS ) ;
25+ if ( resp . status !== 200 ) {
26+ return resp ;
27+ }
28+ const list = JSON . parse ( resp . body ) ;
29+
30+ const versions = await processQueue ( list , async ( entry ) => {
2231 const entryResp = await getObject ( env , {
2332 bucket,
2433 org,
2534 key : `.da-versions/${ current . metadata . id } /${ entry . name } .${ entry . ext } ` ,
2635 } , true ) ;
36+
37+ if ( entryResp . status !== 200 || ! entryResp . metadata ) {
38+ // Some requests might fail for unknown reasons (system busy, etc.)
39+ return undefined ;
40+ }
41+
2742 const timestamp = parseInt ( entryResp . metadata . timestamp || '0' , 10 ) ;
2843 const users = JSON . parse ( entryResp . metadata . users || '[{"email":"anonymous"}]' ) ;
2944 const { label, path } = entryResp . metadata ;
@@ -38,11 +53,14 @@ export async function listObjectVersions(env, { bucket, org, key }) {
3853 } ;
3954 }
4055 return { users, timestamp, path } ;
41- } ) ) ;
56+ } , CONCURRENCY ) ;
57+
58+ // Filter out undefined entries (failed requests)
59+ const filteredVersions = versions . filter ( ( version ) => version !== undefined ) ;
4260
4361 return {
4462 status : resp . status ,
4563 contentType : resp . contentType ,
46- body : JSON . stringify ( promises ) ,
64+ body : JSON . stringify ( filteredVersions ) ,
4765 } ;
4866}
0 commit comments