UNPKG

siesta-lite

Version:

Stress-free JavaScript unit testing and functional testing tool, works in NodeJS and browsers

60 lines (40 loc) 1.73 kB
import {Channel, Envelop, Message} from "../../generic/channel/Types.js"; import {ChildEndPoint} from "../channel/ipc/ChildEndPoint.js"; import {NodeJsIpcClienState, NodeJsIpcRpc, NodeJsPageRpcFunctionCall} from "./NodeJsPageRpc.js"; import {requireInContext} from "../util/helper/RequireInContext.js"; let vm = require('vm') //--------------------------------------------------------------------------------- export class NodeJsPageChildEndPoint extends ChildEndPoint { projectContext : object constructor (config : Partial<NodeJsPageChildEndPoint>) { super(config) Object.assign(this, config) } doConnect (channel : Channel) : Promise<any> { // relative to CanRequireInContext file this.projectContext = requireInContext('../../../../siesta-nodejs-all.js') let res = super.doConnect(channel) this.sendMessage(NodeJsIpcClienState.Ready) return res } doProcessRawChannelMessage (message : Message, envelop : Envelop) { } doDispatchEnvelop (envelop : Envelop) { const payload = envelop.payload if (payload.type === NodeJsIpcRpc.FunctionCall) { const rpcData = payload as NodeJsPageRpcFunctionCall const code = ';(' + rpcData.func.toString() + ')();' let result let exception try { result = vm.runInContext(code, this.projectContext) } catch (e) { exception = e } if (exception) this.replyWith(envelop, exception, true) else this.replyWith(envelop, result) } } }