Skip to content

Commit

Permalink
fix js request encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
AR1011 committed Jan 9, 2024
1 parent ac97a9b commit 1945306
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
28 changes: 23 additions & 5 deletions examples/js/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
// This should come from the official SDK.
// But there is no official SDK yet, so we keep it here.
function generateEndHex(length, status) {
var buffer = new ArrayBuffer(8);
var view = new DataView(buffer);

view.setUint32(0, status, true);
view.setUint32(4, length, true);

var hexString = "";
for (var i = 0; i < buffer.byteLength; i++) {
var hex = view.getUint8(i).toString(16);
hexString += hex.padStart(2, "0");
}

return hexString;
}

function respond(res, status) {
console.log(res)
console.log(status)
endHex = generateEndHex(res.length, status);
//using putstr because console.log adds "\n" to the end
putstr(res + endHex);
}

respond("<h1>From my Raptor application</h1></br>some other stuff here</br>", 200)
respond(
"<h1>From my Raptor application</h1></br>some other stuff here</br>",
200
);
34 changes: 18 additions & 16 deletions internal/actrs/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ type shutdown struct{}

// Runtime is an actor that can execute compiled WASM blobs in a distributed cluster.
type Runtime struct {
store storage.Store
cache storage.ModCacher
started time.Time
deploymentID uuid.UUID
managerPID *actor.PID
runtime *runtime.Runtime
repeat actor.SendRepeater
stdout *bytes.Buffer
store storage.Store
cache storage.ModCacher
started time.Time
deployment *types.Deployment
managerPID *actor.PID
runtime *runtime.Runtime
repeat actor.SendRepeater
stdout *bytes.Buffer
}

func NewRuntime(store storage.Store, cache storage.ModCacher) actor.Producer {
Expand All @@ -60,7 +60,7 @@ func (r *Runtime) Receive(c *actor.Context) {
case actor.Stopped:
// TODO: send metrics about the runtime to the metric actor.
_ = time.Since(r.started)
c.Send(r.managerPID, removeRuntime{key: r.deploymentID.String()})
c.Send(r.managerPID, removeRuntime{key: r.deployment.ID.String()})
r.runtime.Close()
// Releasing this mod will invalidate the cache for some reason.
// r.mod.Close(context.TODO())
Expand All @@ -78,17 +78,18 @@ func (r *Runtime) Receive(c *actor.Context) {
}

func (r *Runtime) initialize(msg *proto.HTTPRequest) error {
r.deploymentID = uuid.MustParse(msg.DeploymentID)
id := uuid.MustParse(msg.DeploymentID)
// TODO: this could be coming from a Redis cache instead of Postres.
// Maybe only the blob. Not sure...
deploy, err := r.store.GetDeployment(r.deploymentID)
deploy, err := r.store.GetDeployment(id)
if err != nil {
return fmt.Errorf("runtime: could not find deployment (%s)", r.deploymentID)
return fmt.Errorf("runtime: could not find deployment (%s)", id)
}
r.deployment = deploy

modCache, ok := r.cache.Get(r.deploymentID)
modCache, ok := r.cache.Get(r.deployment.ID)
if !ok {
slog.Warn("no cache hit", "endpoint", r.deploymentID)
slog.Warn("no cache hit", "endpoint", r.deployment.ID)
modCache = wazero.NewCompilationCache()
}

Expand Down Expand Up @@ -120,7 +121,8 @@ func (r *Runtime) handleHTTPRequest(ctx *actor.Context, msg *proto.HTTPRequest)

var args []string = nil
if msg.Runtime == "js" {
args = []string{"", "-e", string(r.runtime.Blob())}

args = []string{"", "-e", string(r.deployment.Blob)}
}

req := bytes.NewReader(b)
Expand Down Expand Up @@ -149,7 +151,7 @@ func (r *Runtime) handleHTTPRequest(ctx *actor.Context, msg *proto.HTTPRequest)
metric := types.RequestMetric{
ID: uuid.New(),
Duration: time.Since(start),
DeploymentID: r.deploymentID,
DeploymentID: r.deployment.ID,
// EndpointID: deploy.EndpointID,
RequestURL: msg.URL,
StatusCode: status,
Expand Down

0 comments on commit 1945306

Please sign in to comment.