Esempio n. 1
0
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
		}
	}
}
Esempio n. 2
0
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
	})
}
Esempio n. 3
0
func init() {
	Header = mc.Header([]byte("/multicodec"))
}
Esempio n. 4
0
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
Esempio n. 5
0
func init() {
	Header = mc.Header([]byte("/msgio"))
}
Esempio n. 6
0
func init() {
	HeaderPath = "/json"
	HeaderMsgioPath = "/json/msgio"
	Header = mc.Header([]byte(HeaderPath))
	HeaderMsgio = mc.Header([]byte(HeaderMsgioPath))
}
Esempio n. 7
0
func init() {
	Header = mc.Header([]byte("/protobuf"))
	HeaderMsgio = mc.Header([]byte("/protobuf/msgio"))
}
Esempio n. 8
0
func init() {
	HeaderPath = "/cbor"
	Header = mc.Header([]byte(HeaderPath))
}
Esempio n. 9
0
func codecWithPath(path string) mc.Multicodec {
	hdr := mc.Header([]byte(path))
	codec := mux.CodecWithHeader(hdr, mux.StandardMux().Codecs)
	return codec
}
Esempio n. 10
0
func header(w io.Writer, path string) error {
	_, err := w.Write(mc.Header([]byte(path)))
	return err
}