|
1 | 1 | import { DirSource, FileMetadata, FileSource, SourcePart } from './types.js' |
2 | 2 | import { getFileName } from './utils.js' |
3 | 3 |
|
4 | | -function s3list(bucket: string, prefix: string) { |
| 4 | +async function s3list(bucket: string, prefix: string) { |
5 | 5 | const url = `https://${bucket}.s3.amazonaws.com/?list-type=2&prefix=${prefix}&delimiter=/` |
6 | | - return fetch(url) |
7 | | - .then(res => { |
8 | | - if (!res.ok) throw new Error(`${res.status} ${res.statusText}`) |
9 | | - return res.text() |
10 | | - }) |
11 | | - .then(text => { |
12 | | - const results = [] |
| 6 | + const result = await fetch(url) |
| 7 | + if (!result.ok) { |
| 8 | + throw new Error(`${result.status} ${result.statusText}`) |
| 9 | + } |
| 10 | + const text = await result.text() |
| 11 | + const results = [] |
13 | 12 |
|
14 | | - // Parse regular objects (files and explicit directories) |
15 | | - const contentsRegex = /<Contents>(.*?)<\/Contents>/gs |
16 | | - const contentsMatches = text.match(contentsRegex) ?? [] |
| 13 | + // Parse regular objects (files and explicit directories) |
| 14 | + const contentsRegex = /<Contents>(.*?)<\/Contents>/gs |
| 15 | + const contentsMatches = text.match(contentsRegex) ?? [] |
17 | 16 |
|
18 | | - for (const match of contentsMatches) { |
19 | | - const keyMatch = /<Key>(.*?)<\/Key>/.exec(match) |
20 | | - const lastModifiedMatch = /<LastModified>(.*?)<\/LastModified>/.exec(match) |
21 | | - const sizeMatch = /<Size>(.*?)<\/Size>/.exec(match) |
22 | | - const eTagMatch = /<ETag>"(.*?)"<\/ETag>/.exec(match) ?? /<ETag>"(.*?)"<\/ETag>/.exec(match) |
| 17 | + for (const match of contentsMatches) { |
| 18 | + const keyMatch = /<Key>(.*?)<\/Key>/.exec(match) |
| 19 | + const lastModifiedMatch = /<LastModified>(.*?)<\/LastModified>/.exec(match) |
| 20 | + const sizeMatch = /<Size>(.*?)<\/Size>/.exec(match) |
| 21 | + const eTagMatch = /<ETag>"(.*?)"<\/ETag>/.exec(match) ?? /<ETag>"(.*?)"<\/ETag>/.exec(match) |
23 | 22 |
|
24 | | - if (!keyMatch || !lastModifiedMatch) continue |
| 23 | + if (!keyMatch || !lastModifiedMatch) continue |
25 | 24 |
|
26 | | - const key = keyMatch[1] |
27 | | - const lastModified = lastModifiedMatch[1] |
28 | | - const size = sizeMatch ? parseInt(sizeMatch[1] ?? '', 10) : undefined |
29 | | - const eTag = eTagMatch ? eTagMatch[1] : undefined |
| 25 | + const key = keyMatch[1] |
| 26 | + const lastModified = lastModifiedMatch[1] |
| 27 | + const size = sizeMatch ? parseInt(sizeMatch[1] ?? '', 10) : undefined |
| 28 | + const eTag = eTagMatch ? eTagMatch[1] : undefined |
30 | 29 |
|
31 | | - results.push({ key, lastModified, size, eTag }) |
32 | | - } |
| 30 | + results.push({ key, lastModified, size, eTag }) |
| 31 | + } |
33 | 32 |
|
34 | | - // Parse CommonPrefixes (virtual directories) |
35 | | - const prefixRegex = /<CommonPrefixes>(.*?)<\/CommonPrefixes>/gs |
36 | | - const prefixMatches = text.match(prefixRegex) ?? [] |
37 | | - |
38 | | - for (const match of prefixMatches) { |
39 | | - const prefixMatch = /<Prefix>(.*?)<\/Prefix>/.exec(match) |
40 | | - if (!prefixMatch) continue |
41 | | - |
42 | | - const key = prefixMatch[1] |
43 | | - results.push({ |
44 | | - key, |
45 | | - lastModified: new Date().toISOString(), // No lastModified for CommonPrefixes |
46 | | - size: 0, |
47 | | - isCommonPrefix: true, |
48 | | - }) |
49 | | - } |
| 33 | + // Parse CommonPrefixes (virtual directories) |
| 34 | + const prefixRegex = /<CommonPrefixes>(.*?)<\/CommonPrefixes>/gs |
| 35 | + const prefixMatches = text.match(prefixRegex) ?? [] |
| 36 | + |
| 37 | + for (const match of prefixMatches) { |
| 38 | + const prefixMatch = /<Prefix>(.*?)<\/Prefix>/.exec(match) |
| 39 | + if (!prefixMatch) continue |
50 | 40 |
|
51 | | - return results |
| 41 | + const key = prefixMatch[1] |
| 42 | + results.push({ |
| 43 | + key, |
| 44 | + lastModified: new Date().toISOString(), // No lastModified for CommonPrefixes |
| 45 | + size: 0, |
| 46 | + isCommonPrefix: true, |
52 | 47 | }) |
| 48 | + } |
| 49 | + |
| 50 | + return results |
53 | 51 | } |
54 | 52 |
|
55 | 53 | function getSourceParts(sourceId: string): SourcePart[] { |
|
0 commit comments