-
Notifications
You must be signed in to change notification settings - Fork 2
S3 browse demo #238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
S3 browse demo #238
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4aaf5ea
working s3 browser demo
bleakley 80c0d85
parse breadcrumbs for file view also
bleakley 6d7bced
remove pointless throw
bleakley 7996faa
fix lint
bleakley 5a8b9ba
undo lint fix
bleakley 7c48ba7
fix build
bleakley c1c73e5
fix lint and test
bleakley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,127 @@ | ||
| import { FileSource } from './types.js' | ||
| import { DirSource, FileMetadata, FileSource, SourcePart } from './types.js' | ||
| import { getFileName } from './utils.js' | ||
|
|
||
| export function getHttpSource(sourceId: string, options?: {requestInit?: RequestInit}): FileSource | undefined { | ||
| function s3list(bucket: string, prefix: string) { | ||
| const url = `https://${bucket}.s3.amazonaws.com/?list-type=2&prefix=${prefix}&delimiter=/` | ||
| return fetch(url) | ||
| .then(res => { | ||
| if (!res.ok) throw new Error(`${res.status} ${res.statusText}`) | ||
| return res.text() | ||
| }) | ||
| .then(text => { | ||
| const results = [] | ||
|
|
||
| // Parse regular objects (files and explicit directories) | ||
| const contentsRegex = /<Contents>(.*?)<\/Contents>/gs | ||
| const contentsMatches = text.match(contentsRegex) ?? [] | ||
|
|
||
| for (const match of contentsMatches) { | ||
| const keyMatch = /<Key>(.*?)<\/Key>/.exec(match) | ||
| const lastModifiedMatch = /<LastModified>(.*?)<\/LastModified>/.exec(match) | ||
| const sizeMatch = /<Size>(.*?)<\/Size>/.exec(match) | ||
| const eTagMatch = /<ETag>"(.*?)"<\/ETag>/.exec(match) ?? /<ETag>"(.*?)"<\/ETag>/.exec(match) | ||
|
|
||
| if (!keyMatch || !lastModifiedMatch) continue | ||
|
|
||
| const key = keyMatch[1] | ||
| const lastModified = lastModifiedMatch[1] | ||
| const size = sizeMatch ? parseInt(sizeMatch[1] ?? '', 10) : undefined | ||
| const eTag = eTagMatch ? eTagMatch[1] : undefined | ||
|
|
||
| results.push({ key, lastModified, size, eTag }) | ||
| } | ||
|
|
||
| // Parse CommonPrefixes (virtual directories) | ||
| const prefixRegex = /<CommonPrefixes>(.*?)<\/CommonPrefixes>/gs | ||
| const prefixMatches = text.match(prefixRegex) ?? [] | ||
|
|
||
| for (const match of prefixMatches) { | ||
| const prefixMatch = /<Prefix>(.*?)<\/Prefix>/.exec(match) | ||
| if (!prefixMatch) continue | ||
|
|
||
| const key = prefixMatch[1] | ||
| results.push({ | ||
| key, | ||
| lastModified: new Date().toISOString(), // No lastModified for CommonPrefixes | ||
| size: 0, | ||
| isCommonPrefix: true, | ||
| }) | ||
| } | ||
|
|
||
| return results | ||
| }) | ||
| } | ||
|
|
||
| function getSourceParts(sourceId: string): SourcePart[] { | ||
| const [protocol, rest] = sourceId.split('://', 2) | ||
| const parts = rest | ||
| ? [`${protocol}://${rest.split('/', 1)[0]}`, ...rest.split('/').slice(1)] | ||
| : sourceId.split('/') | ||
| const sourceParts = [ | ||
| ...parts.map((part, depth) => { | ||
| const slashSuffix = depth === parts.length - 1 ? '' : '/' | ||
| return { | ||
| text: part + slashSuffix, | ||
| sourceId: parts.slice(0, depth + 1).join('/') + slashSuffix, | ||
| } | ||
| }), | ||
| ] | ||
| if (sourceParts[sourceParts.length - 1]?.text === '') { | ||
| sourceParts.pop() | ||
| } | ||
| return sourceParts | ||
| } | ||
|
|
||
| export function getHttpSource(sourceId: string, options?: {requestInit?: RequestInit}): FileSource | DirSource | undefined { | ||
| if (!URL.canParse(sourceId)) { | ||
| return undefined | ||
| } | ||
|
|
||
| const sourceParts = getSourceParts(sourceId) | ||
|
|
||
| if (sourceId.endsWith('/')) { | ||
| const url = new URL(sourceId) | ||
| const bucket = url.hostname.split('.')[0] | ||
| const prefix = url.pathname.slice(1) | ||
|
|
||
| if (!bucket) { | ||
| return undefined | ||
| } | ||
|
|
||
| return { | ||
| kind: 'directory', | ||
| sourceId, | ||
| sourceParts, | ||
| prefix, | ||
| listFiles: () => s3list(bucket, prefix).then(items => | ||
| items | ||
| .filter(item => item.key !== undefined) | ||
| .map(item => { | ||
| if (!item.key) { | ||
| throw new Error('Key is undefined') | ||
| } | ||
| const isDirectory = item.key.endsWith('/') | ||
| const itemSourceId = `https://${bucket}.s3.amazonaws.com/${item.key}` | ||
| // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a reason we can't use nullish |
||
| let name = item.key.split('/').pop() || item.key | ||
| if (name && isDirectory) { | ||
| name = name.replace(prefix, '') | ||
| } | ||
| return { | ||
| name, | ||
| lastModified: item.lastModified, | ||
| sourceId: itemSourceId, | ||
| kind: isDirectory ? 'directory' : 'file', | ||
| } as FileMetadata | ||
| }) | ||
| ), | ||
| } as DirSource | ||
| } | ||
|
|
||
| return { | ||
| kind: 'file', | ||
| sourceId, | ||
| sourceParts: [{ text: sourceId, sourceId }], | ||
| sourceParts, | ||
| fileName: getFileName(sourceId), | ||
| resolveUrl: sourceId, | ||
| requestInit: options?.requestInit, | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's fine for now, but we should probably make this 0 and hide in the ui.