-
Notifications
You must be signed in to change notification settings - Fork 106
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
feat: download tars from @helia/verified-fetch #442
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
66c7617
feat: handle Accept header for raw types in @helia/verified-fetch
achingbrain 67ddf99
chore: add docs
achingbrain 97724a7
Merge remote-tracking branch 'origin/main' into feat/handle-accept-he…
achingbrain 19b04a6
feat: convert between types
achingbrain 0ad29ea
chore: update dev deps
achingbrain 9a1849c
feat: support downloading car files from @helia/verified-fetch
achingbrain 8356cf4
chore: fix deps
achingbrain 70f2efa
feat: download tars from @helia/verified-fetch
achingbrain eae25ce
chore: apply suggestions from code review
achingbrain 236bae0
Merge remote-tracking branch 'origin/main' into feat/download-tars-fr…
achingbrain 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 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 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 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
18 changes: 18 additions & 0 deletions
18
packages/verified-fetch/src/utils/get-content-disposition-filename.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Takes a filename URL param and returns a string for use in a | ||
* `Content-Disposition` header | ||
*/ | ||
export function getContentDispositionFilename (filename: string): string { | ||
const asciiOnly = replaceNonAsciiCharacters(filename) | ||
|
||
if (asciiOnly === filename) { | ||
return `filename="${filename}"` | ||
} | ||
|
||
return `filename="${asciiOnly}"; filename*=UTF-8''${encodeURIComponent(filename)}` | ||
} | ||
|
||
function replaceNonAsciiCharacters (filename: string): string { | ||
// eslint-disable-next-line no-control-regex | ||
return filename.replace(/[^\x00-\x7F]/g, '_') | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { CodeError } from '@libp2p/interface' | ||
import { exporter, recursive, type UnixFSEntry } from 'ipfs-unixfs-exporter' | ||
import map from 'it-map' | ||
import { pipe } from 'it-pipe' | ||
import { pack, type TarEntryHeader, type TarImportCandidate } from 'it-tar' | ||
import type { AbortOptions } from '@libp2p/interface' | ||
import type { Blockstore } from 'interface-blockstore' | ||
|
||
const EXPORTABLE = ['file', 'raw', 'directory'] | ||
|
||
function toHeader (file: UnixFSEntry): Partial<TarEntryHeader> & { name: string } { | ||
let mode: number | undefined | ||
let mtime: Date | undefined | ||
|
||
if (file.type === 'file' || file.type === 'directory') { | ||
mode = file.unixfs.mode | ||
mtime = file.unixfs.mtime != null ? new Date(Number(file.unixfs.mtime.secs * 1000n)) : undefined | ||
} | ||
|
||
return { | ||
name: file.path, | ||
mode, | ||
mtime, | ||
size: Number(file.size), | ||
type: file.type === 'directory' ? 'directory' : 'file' | ||
} | ||
} | ||
|
||
function toTarImportCandidate (entry: UnixFSEntry): TarImportCandidate { | ||
if (!EXPORTABLE.includes(entry.type)) { | ||
throw new CodeError('Not a UnixFS node', 'ERR_NOT_UNIXFS') | ||
} | ||
|
||
const candidate: TarImportCandidate = { | ||
header: toHeader(entry) | ||
} | ||
|
||
if (entry.type === 'file' || entry.type === 'raw') { | ||
candidate.body = entry.content() | ||
} | ||
|
||
return candidate | ||
} | ||
|
||
export async function * tarStream (ipfsPath: string, blockstore: Blockstore, options?: AbortOptions): AsyncGenerator<Uint8Array> { | ||
const file = await exporter(ipfsPath, blockstore, options) | ||
|
||
if (file.type === 'file' || file.type === 'raw') { | ||
yield * pipe( | ||
[toTarImportCandidate(file)], | ||
pack() | ||
) | ||
|
||
return | ||
} | ||
|
||
if (file.type === 'directory') { | ||
yield * pipe( | ||
recursive(ipfsPath, blockstore, options), | ||
(source) => map(source, (entry) => toTarImportCandidate(entry)), | ||
pack() | ||
) | ||
|
||
return | ||
} | ||
|
||
throw new CodeError('Not a UnixFS node', 'ERR_NOT_UNIXFS') | ||
} | ||
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
do we want to squash the bigint from
Exportable.size
to a Number here?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.
We have to convert it because
it-tar
expects the field to be a number.We may lose some precision but
Number.MAX_SAFE_INTEGER
is 9PB so famous last words but I think files of that size may be uncommon.