public JsObject GetDescriptorObject(BaristaContext context) { var descriptor = context.CreateObject(); if (Configurable) { descriptor.SetProperty("configurable", context.True); } if (Enumerable) { descriptor.SetProperty("enumerable", context.True); } if (Writable) { descriptor.SetProperty("enumerable", context.True); } if (Get != null) { descriptor.SetProperty("get", Get); } if (Set != null) { descriptor.SetProperty("set", Set); } if (Value != null) { descriptor.SetProperty("value", Value); } return(descriptor); }
public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule) { var buffer = SerializedScriptService.GetSerializedScript(ResourceName, context, mapWindowToGlobal: true); var fnScript = context.ParseSerializedScript(buffer, () => EmbeddedResourceHelper.LoadResource(ResourceName), "[uuid]"); return(fnScript.Call <JsObject>()); }
public void BaristaContextThrowsIfConversionStrategyNotSpecified() { using (var rt = BaristaRuntimeFactory.CreateRuntime()) { var valueFactoryBuilder = m_provider.GetRequiredService <IBaristaValueFactoryBuilder>(); var moduleRecordFactory = m_provider.GetRequiredService <IBaristaModuleRecordFactory>(); var taskQueue = m_provider.GetRequiredService <IPromiseTaskQueue>(); var contextHandle = rt.Engine.JsCreateContext(rt.Handle); try { Assert.Throws <ArgumentNullException>(() => { var ctx = new BaristaContext(rt.Engine, valueFactoryBuilder, null, moduleRecordFactory, taskQueue, contextHandle); }); } finally { //Without disposing of the contextHandle, the runtime *will* crash the process. contextHandle.Dispose(); } } Assert.True(true); }
public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule) { var tsBuffer = TypeScriptTranspiler.GetSerializedTypeScriptCompiler(context); var fnTypeScript = context.ParseSerializedScript(tsBuffer, () => EmbeddedResourceHelper.LoadResource(TypeScriptTranspiler.ResourceName), "[typescript]"); return(fnTypeScript.Call <JsObject>()); }
public static JsValue EvaluateTypeScriptModule(this BaristaContext context, string script, IBaristaModuleLoader moduleLoader = null, bool isTsx = false) { var transpileTask = TypeScriptTranspiler.Default.Transpile(script, isTsx ? "main.tsx" : "main.ts"); var transpilerOutput = transpileTask.GetAwaiter().GetResult(); return(context.EvaluateModule(transpilerOutput.OutputText, moduleLoader)); }
public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule) { var brewRequest = new BrewRequest(context, m_request); context.Converter.TryFromObject(context, brewRequest, out JsValue requestObj); context.Converter.TryFromObject(context, typeof(BrewResponse), out JsValue responseObj); //context.Converter.TryFromObject(context, m_log, out JsValue logObj); var contextObj = context.CreateObject(); context.Object.DefineProperty(contextObj, "request", new JsPropertyDescriptor() { Configurable = false, Writable = false, Value = requestObj }); context.Object.DefineProperty(contextObj, "response", new JsPropertyDescriptor() { Configurable = false, Writable = false, Value = responseObj }); return(contextObj); }
public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule) { var buffer = SerializedScriptService.GetSerializedScript(ResourceName, context); var fnScript = context.ParseSerializedScript(buffer, () => EmbeddedResourceHelper.LoadResource(ResourceName), "[react-dom-server]"); var jsReact = fnScript.Call <JsObject>(); return(jsReact); }
public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule) { var fnFetch = context.CreateFunction(new Func <JsObject, JsValue, JsObject, object>((thisObj, input, init) => { Request request; if (input is JsObject inputObj && inputObj.Type == JsValueType.Object && inputObj.TryGetBean(out JsExternalObject exObj) && exObj.Target is Request inputRequest) { request = new Request(inputRequest); }
/// <summary> /// returns a byte array consisting of a runtime-independent typescript compiler. /// </summary> /// <param name="context"></param> /// <returns></returns> public static byte[] GetSerializedTypeScriptCompiler(BaristaContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } return(SerializedScriptService.GetSerializedScript(ResourceName, context)); }
public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule) { var foo = new CarlyRae() { Name = "Kilroy" }; context.Converter.TryFromObject(context, foo, out JsValue resultObj); return(resultObj); }
public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule) { var transpiledScript = TypeScriptTranspiler.Default.Transpile(new TranspileOptions() { Script = Script, FileName = m_filename }).GetAwaiter().GetResult(); return(context.CreateString(transpiledScript.OutputText)); }
public BrewRequest(BaristaContext context, HttpRequest request) { m_context = context ?? throw new ArgumentNullException(nameof(context)); m_httpRequest = request ?? throw new ArgumentNullException(nameof(request)); Method = request.Method; m_headers = new Headers(context); foreach (var header in request.Headers) { m_headers.Append(header.Key, header.Value); } }
public void JsModulesWillExecuteEvenWithoutATaskQueue() { using (var rt = BaristaRuntimeFactory.CreateRuntime()) { var converter = m_provider.GetRequiredService <IBaristaConversionStrategy>(); var valueFactoryBuilder = m_provider.GetRequiredService <IBaristaValueFactoryBuilder>(); var moduleRecordFactory = m_provider.GetRequiredService <IBaristaModuleRecordFactory>(); IPromiseTaskQueue taskQueue = null; var contextHandle = rt.Engine.JsCreateContext(rt.Handle); using (var ctx = new BaristaContext(rt.Engine, valueFactoryBuilder, converter, moduleRecordFactory, taskQueue, contextHandle)) { var result = ctx.EvaluateModule("export default 'foo';"); Assert.Equal("foo", result.ToString()); } Assert.True(contextHandle.IsClosed); } }
public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule) { var fnResult = context.CreateFunction(new Func <JsObject, JsValue, JsValue>((thisObj, toReverse) => { if (toReverse == null || String.IsNullOrWhiteSpace(toReverse.ToString())) { return(context.Undefined); } var str = toReverse.ToString(); var charArray = str.ToCharArray(); Array.Reverse(charArray); var reversed = new string(charArray); return(context.CreateString(reversed)); })); return(fnResult); }
public static void PopulateHttpResponse(HttpResponse response, BaristaContext brewContext, BrewResponse brewResponse) { response.StatusCode = brewResponse.StatusCode; response.HttpContext.Features.Get <IHttpResponseFeature>().ReasonPhrase = brewResponse.StatusDescription; foreach (var header in brewResponse.Headers.AllHeaders) { var values = new StringValues(header.Value.ToArray()); response.Headers.Add(header.Key, values); } if (!string.IsNullOrWhiteSpace(brewResponse.ContentType)) { response.ContentType = brewResponse.ContentType; } if (!string.IsNullOrWhiteSpace(brewResponse.ContentDisposition)) { response.Headers["Content-Disposition"] = brewResponse.ContentDisposition; } ResponseValueConverter.PopulateResponseForValue(response, brewContext, brewResponse.Body, false); }
public static HttpResponseMessage CreateResponseMessage(BaristaContext ctx, BrewResponse response) { var result = ResponseValueConverter.CreateResponseMessageForValue(ctx, response.Body); result.StatusCode = (HttpStatusCode)response.StatusCode; result.ReasonPhrase = response.StatusDescription; foreach (var header in response.Headers.AllHeaders) { result.Headers.Add(header.Key, header.Value); } if (!string.IsNullOrWhiteSpace(response.ContentType)) { result.Content.Headers.ContentType = new MediaTypeHeaderValue(response.ContentType); } if (!string.IsNullOrWhiteSpace(response.ContentDisposition)) { result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue(response.ContentDisposition); } return(result); }
public static void PopulateResponseForValue(HttpResponse response, BaristaContext context, JsValue value, bool setHeaders = true) { Action setHeaderAction; byte[] bodyBuffer; switch (value) { case JsError errorValue: setHeaderAction = () => { response.StatusCode = (int)HttpStatusCode.InternalServerError; response.HttpContext.Features.Get <IHttpResponseFeature>().ReasonPhrase = value.ToString(); }; //TODO: Set response body. bodyBuffer = Encoding.UTF8.GetBytes(String.Empty); break; case JsArrayBuffer arrayBufferValue: setHeaderAction = () => { response.StatusCode = (int)HttpStatusCode.OK; response.ContentType = "application/octet-stream"; }; bodyBuffer = arrayBufferValue.GetArrayBufferStorage(); break; case JsBoolean booleanValue: case JsNumber numberValue: case JsString stringValue: setHeaderAction = () => { response.StatusCode = (int)HttpStatusCode.OK; response.ContentType = new MediaTypeHeaderValue("text/plain").ToString(); }; bodyBuffer = Encoding.UTF8.GetBytes(value.ToString()); break; case JsObject objValue: //if the object contains a bean - attempt to serialize that. if (objValue.Type == JsValueType.Object && objValue.TryGetBean(out JsExternalObject exObj)) { switch (exObj.Target) { case Blob blobObj: setHeaderAction = () => { response.StatusCode = (int)HttpStatusCode.OK; response.ContentType = blobObj.Type; if (!String.IsNullOrWhiteSpace(blobObj.Disposition)) { response.Headers.Add("Content-Disposition", blobObj.Disposition); } }; bodyBuffer = blobObj.Data; break; default: setHeaderAction = () => { response.StatusCode = (int)HttpStatusCode.OK; response.ContentType = "application/json; charset=utf-8"; }; //Use Json.net to serialize the object to a string. var jsonObj = JsonConvert.SerializeObject(exObj); bodyBuffer = Encoding.UTF8.GetBytes(jsonObj); break; } } else { setHeaderAction = () => { response.StatusCode = (int)HttpStatusCode.OK; response.ContentType = "application/json; charset=utf-8"; }; //Use the built-in JSON object to serialize the jsValue to a string. var json = context.JSON.Stringify(objValue, null, null); bodyBuffer = Encoding.UTF8.GetBytes(json); } break; default: setHeaderAction = () => { response.StatusCode = (int)HttpStatusCode.OK; response.ContentType = "text/plain; charset=utf-8"; }; var responseTextBody = Encoding.UTF8.GetBytes(value.ToString()); bodyBuffer = responseTextBody; break; } if (setHeaders) { setHeaderAction(); } response.Body.Write(bodyBuffer, 0, bodyBuffer.Length); }
public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule) { return(context.JSON.Parse(context.CreateString(JsonText))); }
public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule) { return(context.CreateString("The maze isn't meant for you.")); }
public HttpResponseMessage Serve(BaristaContext ctx, JsValue result) { return(ServeMiddleware.Invoke(ctx, result)); }
public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule) { return(context.CreateString("You'll not see me.")); }
public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule) { return(context.CreateString(referencingModule.Name)); }
public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule) { throw new Exception("Derp!"); }
public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule) { return(context.CreateNumber(42)); }
public static HttpResponseMessage CreateResponseMessageForValue(BaristaContext context, JsValue value) { HttpResponseMessage response; switch (value) { case JsError errorValue: response = new HttpResponseMessage(HttpStatusCode.InternalServerError) { ReasonPhrase = value.ToString() }; //TODO: Set response body break; case JsArrayBuffer arrayBufferValue: response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(arrayBufferValue.GetArrayBufferStorage()) }; response.Content.Headers.Add("Content-Type", "application/octet-stream"); break; case JsBoolean booleanValue: case JsNumber numberValue: case JsString stringValue: response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(Encoding.UTF8.GetBytes(value.ToString())) }; response.Content.Headers.Add("Content-Type", "text/plain"); break; case JsObject objValue: //if the object contains a bean - attempt to serialize that. if (objValue.Type == JsValueType.Object && objValue.TryGetBean(out JsExternalObject exObj)) { switch (exObj.Target) { case Blob blobObj: response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(blobObj.Data) }; response.Content.Headers.Add("Content-Type", blobObj.Type); if (!String.IsNullOrWhiteSpace(blobObj.Disposition)) { response.Content.Headers.Add("Content-Disposition", blobObj.Disposition); } break; default: //Use Json.net to serialize the object to a string. var jsonObj = JsonConvert.SerializeObject(exObj); response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(jsonObj, Encoding.UTF8, "application/json") }; break; } } else { var json = context.JSON.Stringify(objValue, null, null); response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(Encoding.UTF8.GetBytes(json.ToString())) }; response.Content.Headers.Add("Content-Type", "application/json"); } break; default: response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(Encoding.UTF8.GetBytes(value.ToString())) }; response.Content.Headers.Add("Content-Type", "text/plain"); break; } return(response); }
public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule) { return(context.CreateString("Hello, World!")); }
public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule) { return(context.CreateString(m_resourceManager.GetString(ResourceName, ResourceCulture))); }
public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule) { context.Converter.TryFromObject(context, typeof(Blob), out JsValue value); return(value); }
public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule) { var toXml = context.CreateFunction(new Func <JsObject, JsValue, JsValue>((thisObj, json) => { if (json == context.Undefined || json == context.Null || String.IsNullOrWhiteSpace(json.ToString())) { var error = context.CreateError($"A Json string must be specified as the first argument."); context.Engine.JsSetException(error.Handle); return(context.Undefined); } var jsonData = json.ToString(); var xmlDoc = JsonConvert.DeserializeXmlNode(jsonData); using (var stringWriter = new StringWriter()) using (var xmlTextWriter = XmlWriter.Create(stringWriter)) { xmlDoc.WriteTo(xmlTextWriter); xmlTextWriter.Flush(); return(context.CreateString(stringWriter.GetStringBuilder().ToString())); } })); var toJson = context.CreateFunction(new Func <JsObject, JsValue, JsValue, JsValue>((thisObj, xml, options) => { if (xml == context.Undefined || xml == context.Null || String.IsNullOrWhiteSpace(xml.ToString())) { var error = context.CreateError($"An xml string must be specified as the first argument."); context.Engine.JsSetException(error.Handle); return(context.Undefined); } var xmlData = xml.ToString(); var xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlData); bool toObject = false; bool omitRootObject = false; Newtonsoft.Json.Formatting formatting = Newtonsoft.Json.Formatting.None; if (options is JsObject jsOptions && jsOptions.Type == JsValueType.Object) { if (jsOptions.HasProperty("object") && jsOptions["object"].ToBoolean() == true) { toObject = true; } if (jsOptions.HasProperty("omitRootObject") && jsOptions["omitRootObject"].ToBoolean() == true) { omitRootObject = true; } if (jsOptions.HasProperty("formatting") && Enum.TryParse(jsOptions["formatting"].ToString(), out Newtonsoft.Json.Formatting requestedFormatting)) { formatting = requestedFormatting; } } var json = JsonConvert.SerializeXmlNode(xmlDoc, formatting, omitRootObject); if (toObject) { return(context.JSON.Parse(context.CreateString(json))); } return(context.CreateString(json)); })); var resultObj = context.CreateObject(); context.Object.DefineProperty(resultObj, "toXml", new JsPropertyDescriptor { Enumerable = true, Value = toXml }); context.Object.DefineProperty(resultObj, "toJson", new JsPropertyDescriptor { Enumerable = true, Value = toJson }); return(resultObj); }
public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule) { return(context.CreateString("Goodnight, moon.")); }