-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IPFS Gateway HTTP transport metadata as a well-known multicodec
Add SDK to aid parsing IPNI metadata that represents IPFS Gateway HTTP transport protocol. Relates to: - multiformats/multicodec#321 Fixes #21
- Loading branch information
Showing
4 changed files
with
58 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package metadata | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/multiformats/go-multicodec" | ||
"github.com/multiformats/go-varint" | ||
) | ||
|
||
var ( | ||
ipfsGatewayHttpBytes = append(varint.ToUvarint(uint64(multicodec.TransportIpfsGatewayHttp)), ipfsGatewayHttpPayloadLenBytes...) | ||
ipfsGatewayHttpPayloadLenBytes = varint.ToUvarint(0) | ||
|
||
_ Protocol = (*IpfsGatewayHttp)(nil) | ||
) | ||
|
||
// IpfsGatewayHttp represents the indexing metadata that uses multicodec.TransportIpfsGatewayHttp. | ||
type IpfsGatewayHttp struct { | ||
} | ||
|
||
func (b IpfsGatewayHttp) ID() multicodec.Code { | ||
return multicodec.TransportIpfsGatewayHttp | ||
} | ||
|
||
func (b IpfsGatewayHttp) MarshalBinary() ([]byte, error) { | ||
return ipfsGatewayHttpBytes, nil | ||
} | ||
|
||
func (b IpfsGatewayHttp) UnmarshalBinary(data []byte) error { | ||
if !bytes.Equal(data, ipfsGatewayHttpBytes) { | ||
return fmt.Errorf("transport ID does not match %s", multicodec.TransportIpfsGatewayHttp) | ||
} | ||
return nil | ||
} | ||
|
||
func (b IpfsGatewayHttp) ReadFrom(r io.Reader) (n int64, err error) { | ||
wantLen := len(ipfsGatewayHttpBytes) | ||
buf := make([]byte, wantLen) | ||
read, err := r.Read(buf) | ||
bRead := int64(read) | ||
if err != nil { | ||
return bRead, err | ||
} | ||
if wantLen != read { | ||
return bRead, fmt.Errorf("expected %d readable bytes but read %d", wantLen, read) | ||
} | ||
|
||
if !bytes.Equal(ipfsGatewayHttpBytes, buf) { | ||
return bRead, fmt.Errorf("transport ID does not match %s", multicodec.TransportIpfsGatewayHttp) | ||
} | ||
return bRead, nil | ||
} |
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