<commit_before>package main

import (
	"encoding/json"
	"errors"
	"io"
	"os"

	"github.com/proglottis/oyster"
	"github.com/sourcegraph/rwvfs"
)

type Message struct {
	Type string          `json:"type"`
	Data json.RawMessage `json:"data"`
}

type SearchData struct {
	Query string `json:"query"`
}

type GetData struct {
	Key        string `json:"key"`
	Passphrase string `json:"passphrase"`
}

type DeleteData struct {
	Key string `json:"key"`
}

func readRequests(r io.Reader, requests chan<- *Message) {
	dec := NewDecoder(r)
	for {
		var req Message
		if err := dec.Decode(&req); err != nil {
			switch err {
			case io.EOF:
				close(requests)
				return
			default:
				panic(err)
			}
		}
		select {
		case requests <- &req:
		}
	}
}

type RequestHandler struct {
	requests <-chan *Message
	enc      *Encoder
	repo     *oyster.FormRepo
}

func (h *RequestHandler) Handle(req *Message) {
	switch req.Type {
	case "LIST":
		forms, err := h.repo.List()
		if err != nil {
			h.errorResponse(err)
			return
		}
		h.formsResponse(forms)
	case "SEARCH":
		var data SearchData
		if err := json.Unmarshal(req.Data, &data); err != nil {
			h.errorResponse(err)
			return
		}
		forms, err := h.repo.Search(data.Query)
		if err != nil {
			h.errorResponse(err)
			return
		}
		h.formsResponse(forms)
	case "GET":
		var data GetData
		if err := json.Unmarshal(req.Data, &data); err != nil {
			h.errorResponse(err)
			return
		}
		form, err := h.repo.Get(data.Key, []byte(data.Passphrase))
		if err != nil {
			h.errorResponse(err)
			return
		}
		h.formResponse(form)
	case "PUT":
		var data oyster.Form
		if err := json.Unmarshal(req.Data, &data); err != nil {
			h.errorResponse(err)
			return
		}
		if err := h.repo.Put(&data); err != nil {
			h.errorResponse(err)
			return
		}
	case "REMOVE":
		var data DeleteData
		if err := json.Unmarshal(req.Data, &data); err != nil {
			h.errorResponse(err)
			return
		}
		if err := h.repo.Remove(data.Key); err != nil {
			h.errorResponse(err)
			return
		}
	default:
		h.errorResponse(errors.New("Unknown request type"))
	}
}

func (h *RequestHandler) formsResponse(forms []oyster.Form) {
	var err error
	response := &Message{Type: "FORMS"}
	response.Data, err = json.Marshal(forms)
	if err != nil {
		h.errorResponse(err)
	}
	if err := h.enc.Encode(response); err != nil {
		h.errorResponse(err)
	}
}

func (h *RequestHandler) formResponse(form *oyster.Form) {
	var err error
	response := &Message{Type: "FORM"}
	response.Data, err = json.Marshal(form)
	if err != nil {
		h.errorResponse(err)
	}
	if err := h.enc.Encode(response); err != nil {
		h.errorResponse(err)
	}
}

func (h *RequestHandler) errorResponse(err error) {
	var e error
	response := &Message{Type: "ERROR"}<commit_msg>Always respond to chrome requests<commit_after>package main

import (
	"encoding/json"
	"errors"
	"io"
	"os"

	"github.com/proglottis/oyster"
	"github.com/sourcegraph/rwvfs"
)

type Message struct {
	Type string          `json:"type"`
	Data json.RawMessage `json:"data"`
}

type SearchData struct {
	Query string `json:"query"`
}

type GetData struct {
	Key        string `json:"key"`
	Passphrase string `json:"passphrase"`
}

type DeleteData struct {
	Key string `json:"key"`
}

func readRequests(r io.Reader, requests chan<- *Message) {
	dec := NewDecoder(r)
	for {
		var req Message
		if err := dec.Decode(&req); err != nil {
			switch err {
			case io.EOF:
				close(requests)
				return
			default:
				panic(err)
			}
		}
		select {
		case requests <- &req:
		}
	}
}

type RequestHandler struct {
	requests <-chan *Message
	enc      *Encoder
	repo     *oyster.FormRepo
}

func (h *RequestHandler) Handle(req *Message) {
	switch req.Type {
	case "LIST":
		forms, err := h.repo.List()
		if err != nil {
			h.errorResponse(err)
			return
		}
		h.formsResponse(forms)
	case "SEARCH":
		var data SearchData
		if err := json.Unmarshal(req.Data, &data); err != nil {
			h.errorResponse(err)
			return
		}
		forms, err := h.repo.Search(data.Query)
		if err != nil {
			h.errorResponse(err)
			return
		}
		h.formsResponse(forms)
	case "GET":
		var data GetData
		if err := json.Unmarshal(req.Data, &data); err != nil {
			h.errorResponse(err)
			return
		}
		form, err := h.repo.Get(data.Key, []byte(data.Passphrase))
		if err != nil {
			h.errorResponse(err)
			return
		}
		h.formResponse(form)
	case "PUT":
		var data oyster.Form
		if err := json.Unmarshal(req.Data, &data); err != nil {
			h.errorResponse(err)
			return
		}
		if err := h.repo.Put(&data); err != nil {
			h.errorResponse(err)
			return
		}
		h.okResponse()
	case "REMOVE":
		var data DeleteData
		if err := json.Unmarshal(req.Data, &data); err != nil {
			h.errorResponse(err)
			return
		}
		if err := h.repo.Remove(data.Key); err != nil {
			h.errorResponse(err)
			return
		}
		h.okResponse()
	default:
		h.errorResponse(errors.New("Unknown request type"))
	}
}

func (h *RequestHandler) formsResponse(forms []oyster.Form) {
	var err error
	response := &Message{Type: "FORMS"}
	response.Data, err = json.Marshal(forms)
	if err != nil {
		h.errorResponse(err)
	}
	if err := h.enc.Encode(response); err != nil {
		h.errorResponse(err)
	}
}

func (h *RequestHandler) formResponse(form *oyster.Form) {
	var err error
	response := &Message{Type: "FORM"}
	response.Data, err = json.Marshal(form)
	if err != nil {
		h.errorResponse(err)
	}
	if err := h.enc.Encode(response); err != nil {
		h.errorResponse(err)
	}
}

func (h *RequestHandler) okResponse() {
	response := &Message{Type: "OK"}
	if err := h.enc.Encode(response); err != nil {
		h.errorResponse(err)
	}
}

func (h *RequestHandler) errorResponse(err error) {
	var e error
	response := &Message{Type: "ERROR"}
{'fraction_non_alphanumeric': 0.1488610277238213, 'fraction_numerical': 0.0, 'mean_word_length': 3.317073170731707, 'pattern_counts': {'":': 10, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1327383', 'n_tokens_mistral': 2272, 'n_tokens_neox': 2068, 'n_words': 506}