From ba682915666730c5a470189c99c89a4e2d537723 Mon Sep 17 00:00:00 2001 From: JohnySigma Date: Tue, 5 May 2026 07:40:16 +0300 Subject: [PATCH] Serverless Engine Patch --- core/pkg/serverless/engine.go | 48 +++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/core/pkg/serverless/engine.go b/core/pkg/serverless/engine.go index 7086934..1a35f12 100644 --- a/core/pkg/serverless/engine.go +++ b/core/pkg/serverless/engine.go @@ -421,6 +421,8 @@ func (e *Engine) registerHostModule(ctx context.Context) error { NewFunctionBuilder().WithFunc(e.hPushSend).Export("push_send"). NewFunctionBuilder().WithFunc(e.hWSPubSubBridge).Export("ws_pubsub_bridge"). NewFunctionBuilder().WithFunc(e.hWSPubSubUnbridge).Export("ws_pubsub_unbridge"). + NewFunctionBuilder().WithFunc(e.hWSSend).Export("ws_send"). + NewFunctionBuilder().WithFunc(e.hWSBroadcast).Export("ws_broadcast"). NewFunctionBuilder().WithFunc(e.hLogInfo).Export("log_info"). NewFunctionBuilder().WithFunc(e.hLogError).Export("log_error"). Instantiate(ctx) @@ -741,6 +743,52 @@ func (e *Engine) hWSPubSubUnbridge(ctx context.Context, mod api.Module, return 1 } +// hWSSend is the WASM-callable wrapper for WSSend. +// Inputs: clientID + raw frame bytes. clientID may be empty — in that case +// the host falls back to the current invocation's WS client (if any). +// Returns 1 on success, 0 on error. +func (e *Engine) hWSSend(ctx context.Context, mod api.Module, + cidPtr, cidLen, dataPtr, dataLen uint32) uint32 { + cid, ok := e.executor.ReadFromGuest(mod, cidPtr, cidLen) + if !ok { + return 0 + } + data, ok := e.executor.ReadFromGuest(mod, dataPtr, dataLen) + if !ok { + return 0 + } + if err := e.hostServices.WSSend(ctx, string(cid), data); err != nil { + e.logger.Warn("ws_send failed", + zap.String("client_id", string(cid)), + zap.Error(err)) + return 0 + } + return 1 +} + +// hWSBroadcast is the WASM-callable wrapper for WSBroadcast. +// Inputs: topic + raw frame bytes. Sends `data` to every WS client currently +// subscribed to `topic` in the function's namespace. Returns 1 on success, +// 0 on error. +func (e *Engine) hWSBroadcast(ctx context.Context, mod api.Module, + topicPtr, topicLen, dataPtr, dataLen uint32) uint32 { + topic, ok := e.executor.ReadFromGuest(mod, topicPtr, topicLen) + if !ok { + return 0 + } + data, ok := e.executor.ReadFromGuest(mod, dataPtr, dataLen) + if !ok { + return 0 + } + if err := e.hostServices.WSBroadcast(ctx, string(topic), data); err != nil { + e.logger.Warn("ws_broadcast failed", + zap.String("topic", string(topic)), + zap.Error(err)) + return 0 + } + return 1 +} + // hPushSend is the WASM-callable wrapper for PushSend. // Inputs: // userIDPtr/userIDLen — UTF-8 user ID to push to (within the function's