Skip to content

Commit

Permalink
Cleanup host services (#195)
Browse files Browse the repository at this point in the history
* Cleanup host services params handling
* Make the use of headers and non-enveloped message payloads consistent
across host services
* Make header names more self-documenting
* Move agent message header constants into agent api package
* Use Uint8Array in v8
* Use Bytes() when converting v8 bigint value
* Support objects, arrays, an strings when marshaling []byte from v8
value
* Use toUInt8ArrayValue() when passing payload into triggered v8 functions
* Only deal in native Uint8Array in v8 functions
* Make native v8 utility function name consistent
* Remove unused code
* Cleanup basic echofunction.js test
  • Loading branch information
kthomas committed Apr 26, 2024
1 parent 316166b commit 47abb0a
Show file tree
Hide file tree
Showing 13 changed files with 245 additions and 488 deletions.
323 changes: 169 additions & 154 deletions agent/providers/lib/v8.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/v8/echofunction/src/echofunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
console.log(subject);
return {
triggered_on: subject,
payload: payload
payload: String.fromCharCode(...payload)
}
};
2 changes: 1 addition & 1 deletion examples/v8/echofunction/src/kv.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
this.hostServices.kv.set('hello2', payload);
return {
keys: this.hostServices.kv.keys(),
hello2: this.hostServices.kv.get('hello2')
hello2: String.fromCharCode(...this.hostServices.kv.get('hello2'))
}
};
22 changes: 16 additions & 6 deletions examples/v8/echofunction/src/messaging.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
(subject, payload) => {
this.hostServices.messaging.publish('hello.world', payload);

var req;
var reqResp;
var reqEx;

var reqMany;
var reqManyResp;
var reqManyEx;

try {
req = this.hostServices.messaging.request('hello.world.request', payload);
reqResp = this.hostServices.messaging.request('hello.world.request', payload);
reqResp = String.fromCharCode(...reqResp)
} catch (e) {
reqEx = e;
}

try {
reqMany = this.hostServices.messaging.requestMany('hello.world.request.many', payload);
reqManyResp = []

let responses = this.hostServices.messaging.requestMany('hello.world.request.many', payload)

// responses is an array of Uint8Array... flatten it so we can use each value
responses = Array.prototype.slice.call(responses)

for (let i = 0; i < responses.length; i++) {
reqManyResp.push(String.fromCharCode(...responses[i]))
}
} catch (e) {
reqManyEx = e;
}

return {
'hello.world.request': req,
'hello.world.request': reqResp,
'hello.world.request.ex': reqEx,
'hello.world.request.many': reqMany,
'hello.world.request.many': reqManyResp,
'hello.world.request.many.ex': reqManyEx
}
};
2 changes: 1 addition & 1 deletion examples/v8/echofunction/src/objectstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
this.hostServices.objectStore.put('hello2', payload);
return {
list: this.hostServices.objectStore.list(),
hello2: this.hostServices.objectStore.get('hello2')
hello2: String.fromCharCode(...this.hostServices.objectStore.get('hello2'))
}
};
13 changes: 11 additions & 2 deletions internal/agent-api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ type EventCallback func(string, cloudevents.Event)
type LogCallback func(string, LogEntry)

const (
nexTriggerSubject = "x-nex-trigger-subject"
NexTriggerSubject = "x-nex-trigger-subject"
NexRuntimeNs = "x-nex-runtime-ns"

HttpURLHeader = "x-http-url"

KeyValueKeyHeader = "x-keyvalue-key"

MessagingSubjectHeader = "x-subject"

ObjectStoreObjectNameHeader = "x-object-name"
)

type AgentClient struct {
Expand Down Expand Up @@ -182,7 +191,7 @@ func (a *AgentClient) UptimeMillis() time.Duration {
func (a *AgentClient) RunTrigger(ctx context.Context, tracer trace.Tracer, subject string, data []byte) (*nats.Msg, error) {
intmsg := nats.NewMsg(fmt.Sprintf("agentint.%s.trigger", a.agentID))
// TODO: inject tracer context into message header
intmsg.Header.Add(nexTriggerSubject, subject)
intmsg.Header.Add(NexTriggerSubject, subject)
intmsg.Data = data

cctx, childSpan := tracer.Start(
Expand Down
7 changes: 3 additions & 4 deletions internal/agent-api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,11 @@ type HostServicesHTTPResponse struct {
Error *string `json:"error,omitempty"`
}

type HostServicesKeyValueRequest struct {
Key *string `json:"key"`
Value *json.RawMessage `json:"value,omitempty"`

type HostServicesKeyValueResponse struct {
Revision int64 `json:"revision,omitempty"`
Success *bool `json:"success,omitempty"`

Errors []string `json:"errors,omitempty"`
}

type HostServicesObjectStoreResponse struct {
Expand Down
Loading

0 comments on commit 47abb0a

Please sign in to comment.