<commit_before>package relayR

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
)

type callInstruction struct {
	R string        // The Relay to use
	F string        // The function to call
	A []interface{} // The arguments to use
	C string        // ConnectionId
}

type negotiation struct {
	T string
}

type negotiationResponse struct {
	ConnectionID string
}

// Handle specifies the route to server the RelayR client script from.
func Handle(route string, e *Exchange) {
	setupMainRoute(route, e)
	setupNegotiateRoute(route, e)
	setupWebSocketRoute(route, e)
}

func setupMainRoute(route string, e *Exchange) {
	http.HandleFunc(route, func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "GET" {
			jsonResponse(w)
			writeRelayScript(e, w, route)
		} else {
			// Decode the post request
			decoder := json.NewDecoder(r.Body)
			var call callInstruction

			decoder.Decode(&call)

			relay := e.getRelayByName(call.R)
			err := e.callMethod(relay.t, call.F, call.C, call.A)

			// if there was a problem .. report it back
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
		}
	})
}

func setupNegotiateRoute(route string, e *Exchange) {
	http.HandleFunc(route+"/negotiate", func(w http.ResponseWriter, r *http.Request) {
		jsonResponse(w)
		decoder := json.NewDecoder(r.Body)

		var neg negotiation

		decoder.Decode(&neg)

		encoder := json.NewEncoder(w)
		encoder.Encode(negotiationResponse{ConnectionID: e.addClient(neg.T)})
	})
}

func setupWebSocketRoute(route string, e *Exchange) {
	http.Handle(route+"/ws", socketHandler{e: e, c: e.circuits["WebSocket"].(*webSocketCircuit)})
}

// Write the client side script to the response
func writeRelayScript(e *Exchange, w http.ResponseWriter, route string) {
	buff := bytes.Buffer{}

	buff.WriteString(fmt.Sprintf(connectionClassScript, route))

	buff.WriteString(relayClassBegin)

	for _, relay := range e.Relays {
		buff.WriteString(fmt.Sprintf(relayBegin, relay.Name))

		for _, method := range relay.methods {
			buff.WriteString(fmt.Sprintf(relayMethod, lowerFirst(method), relay.Name, method))

		}
		buff.WriteString(relayEnd)
	}

	buff.WriteString(relayClassEnd)
	io.Copy(w, &buff)
}<commit_msg>Added ClientScriptFunc for consumer processing<commit_after>package relayR

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
)

type callInstruction struct {
	R string        // The Relay to use
	F string        // The function to call
	A []interface{} // The arguments to use
	C string        // ConnectionId
}

type negotiation struct {
	T string
}

type negotiationResponse struct {
	ConnectionID string
}

var ClientScriptFunc func([]byte) []byte

// Handle specifies the route to server the RelayR client script from.
func Handle(route string, e *Exchange) {
	setupMainRoute(route, e)
	setupNegotiateRoute(route, e)
	setupWebSocketRoute(route, e)
}

func setupMainRoute(route string, e *Exchange) {
	http.HandleFunc(route, func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "GET" {
			jsonResponse(w)
			writeRelayScript(e, w, route)
		} else {
			// Decode the post request
			decoder := json.NewDecoder(r.Body)
			var call callInstruction

			decoder.Decode(&call)

			relay := e.getRelayByName(call.R)
			err := e.callMethod(relay.t, call.F, call.C, call.A)

			// if there was a problem .. report it back
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
		}
	})
}

func setupNegotiateRoute(route string, e *Exchange) {
	http.HandleFunc(route+"/negotiate", func(w http.ResponseWriter, r *http.Request) {
		jsonResponse(w)
		decoder := json.NewDecoder(r.Body)

		var neg negotiation

		decoder.Decode(&neg)

		encoder := json.NewEncoder(w)
		encoder.Encode(negotiationResponse{ConnectionID: e.addClient(neg.T)})
	})
}

func setupWebSocketRoute(route string, e *Exchange) {
	http.Handle(route+"/ws", socketHandler{e: e, c: e.circuits["WebSocket"].(*webSocketCircuit)})
}

// Write the client side script to the response
func writeRelayScript(e *Exchange, w http.ResponseWriter, route string) {
	resultBuff := bytes.Buffer{}
	buff := bytes.Buffer{}

	buff.WriteString(fmt.Sprintf(connectionClassScript, route))

	buff.WriteString(relayClassBegin)

	for _, relay := range e.Relays {
		buff.WriteString(fmt.Sprintf(relayBegin, relay.Name))

		for _, method := range relay.methods {
			buff.WriteString(fmt.Sprintf(relayMethod, lowerFirst(method), relay.Name, method))

		}
		buff.WriteString(relayEnd)
	}

	buff.WriteString(relayClassEnd)

	if ClientScriptFunc != nil {
		resultBuff.Write(ClientScriptFunc(buff.Bytes()))
	} else {
		resultBuff.Write(buff.Bytes())
	}

	io.Copy(w, &resultBuff)
}
{'fraction_non_alphanumeric': 0.12489361702127659, 'fraction_numerical': 0.0, 'mean_word_length': 4.0931744312026, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3648949', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1548, 'n_words': 382}