func p2h(w io.Writer, r io.Reader) error { br := bufio.NewReader(r) for { p, err := br.ReadBytes('\n') if err != nil { if err == io.EOF { return nil } return err } p = p[:len(p)-1] // remove \n hdr := mc.Header(p) _, err = w.Write(hdr) if err != nil { return err } } }
func filter(w io.Writer, r io.Reader, path string) error { hdr := mc.Header([]byte(path)) // as we decode, write everything to a buffer. buf := bytes.NewBuffer(nil) r = io.TeeReader(r, buf) return decode(r, func(codec *mux.Multicodec, value interface{}) error { defer buf.Reset() // c.Last is the last multicodec encoded or decoded. // skip headers which do not match. if !bytes.Equal(hdr, codec.Last.Header()) { return nil } _, err := io.Copy(w, buf) if err != nil && err != io.EOF { return err } return nil }) }
func init() { Header = mc.Header([]byte("/multicodec")) }
package b64 import ( "encoding/base64" "io" mc "github.com/multiformats/go-multicodec" base "github.com/multiformats/go-multicodec/base" ) var ( HeaderPath = "/base64/" Header = mc.Header([]byte(HeaderPath)) multic = mc.NewMulticodecFromCodec(Codec(), Header) ) type codec struct{} type decoder struct { r io.Reader } func (d decoder) Decode(v interface{}) error { out, ok := v.([]byte) if !ok { return base.ErrExpectedByteSlice } _, err := d.r.Read(out) return err
func init() { Header = mc.Header([]byte("/msgio")) }
func init() { HeaderPath = "/json" HeaderMsgioPath = "/json/msgio" Header = mc.Header([]byte(HeaderPath)) HeaderMsgio = mc.Header([]byte(HeaderMsgioPath)) }
func init() { Header = mc.Header([]byte("/protobuf")) HeaderMsgio = mc.Header([]byte("/protobuf/msgio")) }
func init() { HeaderPath = "/cbor" Header = mc.Header([]byte(HeaderPath)) }
func codecWithPath(path string) mc.Multicodec { hdr := mc.Header([]byte(path)) codec := mux.CodecWithHeader(hdr, mux.StandardMux().Codecs) return codec }
func header(w io.Writer, path string) error { _, err := w.Write(mc.Header([]byte(path))) return err }