-
Notifications
You must be signed in to change notification settings - Fork 26
feat: new core piece url retriever #617
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
41892d0
chore: testing new piece retriver
hugomrdias 98a5d2b
feat: add piece url retriever and clean up piece related functions
hugomrdias 66a9c6d
feat: use core piece url retriver and remove retriever classes
hugomrdias 4e2ada3
refactor: update piece file path to index for better organization
hugomrdias c05e1ba
chore: remove testing stuff
hugomrdias 423bb66
fix: add providersResolver to context.download
hugomrdias 2e2a637
fix: update providersResolver to return a structured piece URL object
hugomrdias 9d659b2
fix: rename pingProviders to findPieceOnProviders for clarity
hugomrdias 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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { isSynapseError, SynapseError, type SynapseErrorOptions } from './base.ts' | ||
|
|
||
| export class InvalidPieceCIDError extends SynapseError { | ||
| override name: 'InvalidPieceCIDError' = 'InvalidPieceCIDError' | ||
|
|
||
| constructor(input: unknown, options?: SynapseErrorOptions) { | ||
| let msg = 'Invalid piece CID' | ||
| if (typeof input === 'object' && input != null && 'toString' in input && typeof input.toString === 'function') { | ||
| msg = `Invalid piece CID: ${input.toString()}` | ||
| } else if (typeof input === 'string') { | ||
| msg = `Invalid piece CID: ${input}` | ||
| } | ||
| super(msg, options) | ||
| } | ||
|
|
||
| static override is(value: unknown): value is InvalidPieceCIDError { | ||
| return isSynapseError(value) && value.name === 'InvalidPieceCIDError' | ||
| } | ||
| } |
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
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
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 |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import { type AbortError, HttpError, type NetworkError, request, type TimeoutError } from 'iso-web/http' | ||
| import { DownloadPieceError } from '../errors/pdp.ts' | ||
| import { InvalidPieceCIDError } from '../errors/piece.ts' | ||
| import { asPieceCID, createPieceCIDStream, type PieceCID } from './piece.ts' | ||
|
|
||
| export namespace download { | ||
| export type OptionsType = { | ||
| url: string | ||
| } | ||
| export type ReturnType = Uint8Array | ||
| export type ErrorType = DownloadPieceError | TimeoutError | NetworkError | AbortError | ||
| } | ||
|
|
||
| /** | ||
| * Download a piece from a URL. | ||
| * | ||
| * @param options - {@link download.OptionsType} | ||
| * @returns Data {@link download.ReturnType} | ||
| * @throws Errors {@link download.ErrorType} | ||
| */ | ||
| export async function download(options: download.OptionsType): Promise<download.ReturnType> { | ||
| const response = await request.get(options.url) | ||
| if (response.error) { | ||
| if (HttpError.is(response.error)) { | ||
| throw new DownloadPieceError(await response.error.response.text()) | ||
| } | ||
| throw response.error | ||
| } | ||
| return new Uint8Array(await response.result.arrayBuffer()) | ||
| } | ||
|
|
||
| export namespace downloadAndValidate { | ||
| export type OptionsType = { | ||
| url: string | ||
| expectedPieceCid: string | PieceCID | ||
| } | ||
| export type ReturnType = Uint8Array | ||
| export type ErrorType = DownloadPieceError | TimeoutError | NetworkError | AbortError | InvalidPieceCIDError | ||
| } | ||
|
|
||
| /** | ||
| * Download data from a URL, validate its PieceCID, and return as Uint8Array | ||
| * | ||
| * This function: | ||
| * 1. Downloads data from the URL | ||
| * 2. Calculates PieceCID during streaming | ||
| * 3. Collects all chunks into a Uint8Array | ||
| * 4. Validates the calculated PieceCID matches the expected value | ||
| * | ||
| * @param options - {@link downloadAndValidate.OptionsType} | ||
| * @returns Data {@link downloadAndValidate.ReturnType} | ||
| * @throws Errors {@link downloadAndValidate.ErrorType} | ||
| * @example | ||
| * ```ts | ||
| * import * as Piece from '@filoz/synapse-core/piece' | ||
| * const data = await Piece.downloadAndValidate({ | ||
| * url: 'https://example.com/piece', | ||
| * expectedPieceCid: 'bafkzcib...', | ||
| * }) | ||
| * console.log(data) | ||
| * ``` | ||
| */ | ||
| export async function downloadAndValidate(options: downloadAndValidate.OptionsType): Promise<Uint8Array> { | ||
| const { url, expectedPieceCid } = options | ||
|
|
||
| // Parse and validate the expected PieceCID | ||
| const parsedPieceCid = asPieceCID(expectedPieceCid) | ||
| if (parsedPieceCid == null) { | ||
| throw new InvalidPieceCIDError(expectedPieceCid) | ||
| } | ||
|
|
||
| const rsp = await request.get(url) | ||
| if (rsp.error) { | ||
| if (HttpError.is(rsp.error)) { | ||
| throw new DownloadPieceError(await rsp.error.response.text()) | ||
| } | ||
| throw rsp.error | ||
| } | ||
|
|
||
| if (rsp.result.body == null) { | ||
| throw new DownloadPieceError('Response body is null') | ||
| } | ||
|
|
||
| // Create PieceCID calculation stream | ||
| const { stream: pieceCidStream, getPieceCID } = createPieceCIDStream() | ||
|
|
||
| // Create a stream that collects all chunks into an array | ||
| const chunks: Uint8Array[] = [] | ||
| const collectStream = new TransformStream<Uint8Array, Uint8Array>({ | ||
| transform(chunk: Uint8Array, controller: TransformStreamDefaultController<Uint8Array>) { | ||
| chunks.push(chunk) | ||
| controller.enqueue(chunk) | ||
| }, | ||
| }) | ||
|
|
||
| // Pipe the response through both streams | ||
| const pipelineStream = rsp.result.body.pipeThrough(pieceCidStream).pipeThrough(collectStream) | ||
|
|
||
| // Consume the stream to completion | ||
| const reader = pipelineStream.getReader() | ||
| try { | ||
| while (true) { | ||
| const { done } = await reader.read() | ||
| if (done) break | ||
| } | ||
| } finally { | ||
| reader.releaseLock() | ||
| } | ||
|
|
||
| if (chunks.length === 0) { | ||
| throw new DownloadPieceError('Response body is empty') | ||
| } | ||
|
|
||
| // Get the calculated PieceCID | ||
| const calculatedPieceCid = getPieceCID() | ||
|
|
||
| if (calculatedPieceCid == null) { | ||
| throw new DownloadPieceError('Failed to calculate PieceCID from stream') | ||
| } | ||
|
|
||
| // Verify the PieceCID | ||
| if (calculatedPieceCid.toString() !== parsedPieceCid.toString()) { | ||
| throw new DownloadPieceError( | ||
| `PieceCID verification failed. Expected: ${String(parsedPieceCid)}, Got: ${String(calculatedPieceCid)}` | ||
| ) | ||
| } | ||
|
|
||
| // Combine all chunks into a single Uint8Array | ||
| const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0) | ||
| const result = new Uint8Array(totalLength) | ||
| let offset = 0 | ||
| for (const chunk of chunks) { | ||
| result.set(chunk, offset) | ||
| offset += chunk.length | ||
| } | ||
|
|
||
| return result | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export * from '../utils/piece-url.ts' | ||
| export * from './download.ts' | ||
| export * from './piece.ts' | ||
| export * from './resolve-piece-url.ts' |
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.
Uh oh!
There was an error while loading. Please reload this page.