Ejemplo n.º 1
0
 public void Register(ManticoreEngine engine)
 {
     engine.ManticoreJsObject.FastAddProperty("_log",
                                              engine.AsJsFunction((thisObject, args) =>
     {
         JsValue extra = JsValue.Null;
         if (args.Length > 2)
         {
             extra = args[2];
         }
         Log(args[0].AsString(), args[1].AsString(), extra);
         return(JsValue.Undefined);
     }), true, false, false);
     engine.ManticoreJsObject.FastAddProperty("_setTimeout",
                                              engine.AsJsFunction((thisObject, args) =>
     {
         SetTimeout(engine, args[0].As <FunctionInstance>(), (int)args[1].AsNumber());
         return(JsValue.Undefined);
     }), true, false, false);
     engine.ManticoreJsObject.FastAddProperty("_fetch",
                                              engine.AsJsFunction((thisObject, args) =>
     {
         Task.Factory.StartNew(() =>
         {
             Fetch(engine, args[0], args[1]);
         });
         return(JsValue.Undefined);
     }), true, false, false);
 }
Ejemplo n.º 2
0
        private async void sendRequest(ManticoreEngine engine, int timeout, HttpClient client, HttpRequestMessage request, JsValue callback)
        {
            var responseInfo = engine.CreateJsObject();
            HttpResponseMessage     response = null;
            CancellationTokenSource cts      = new CancellationTokenSource(timeout);

            try
            {
                response = await client.SendRequestAsync(request, HttpCompletionOption.ResponseContentRead).AsTask(cts.Token);
            }
            catch (Exception x)
            {
                var ei = new JsErrorBuilder(engine, x).Build();
                responseInfo.FastAddProperty("error", ei, false, true, false);
            }

            if (response != null && response.Headers.Count > 0)
            {
                var headerCollection = engine.CreateJsObject();
                foreach (var kv in response.Headers)
                {
                    try
                    {
                        headerCollection.FastAddProperty(kv.Key, new JsValue(kv.Value), false, true, false);
                    }
                    catch (ArgumentException)
                    {
                        // Swallow duplicate headers for now.
                    }
                }
                responseInfo.FastAddProperty("headers", headerCollection, false, true, false);
            }

            byte[] binaryResult = null;
            if (response != null)
            {
                responseInfo.FastAddProperty("status", new JsValue((int)response.StatusCode), false, true, false);
                // TODO find a way to sneak this wait into the gap between returning and asking for the results
                // json/body/text signatures probably need to change to take a callback.
                binaryResult = (await response.Content.ReadAsBufferAsync()).ToArray();
            }
            engine.Js(() =>
            {
                responseInfo.FastAddProperty("json", engine.AsJsFunction((thisObject, args) => {
                    return(engine.jsEngine.Json.Parse(JsValue.Null,
                                                      new JsValue[] { Encoding.UTF8.GetString(binaryResult, 0, binaryResult.Length) }));
                }), false, false, false);
                responseInfo.FastAddProperty("body", engine.AsJsFunction((thisObject, args) => {
                    return(engine.jsEngine.Json.Parse(JsValue.Null, new JsValue[] { Convert.ToBase64String(binaryResult) }));
                }), false, false, false);
                responseInfo.FastAddProperty("text", engine.AsJsFunction((thisObject, args) => {
                    return(new JsValue(Encoding.UTF8.GetString(binaryResult, 0, binaryResult.Length)));
                }), false, false, false);
                callback.As <FunctionInstance>().Call(engine.ManticoreJsObject, new JsValue[] { JsValue.Null, responseInfo });
            });
        }
 internal ManticoreJsError(ManticoreEngine manticoreEngine, Exception ex, int?code = null)
     : base(manticoreEngine.jsEngine, ex.Message)
 {
     FastAddProperty("message", ex.Message, true, false, true);
     FastAddProperty("code", code.HasValue ? new JsValue(code.Value) : JsValue.Null, true, false, true);
     FastAddProperty("stack", ex.StackTrace, true, false, true);
     FastAddProperty("toString", manticoreEngine.AsJsFunction(((thisObj, args) => ex.ToString())), true, false, false);
 }
Ejemplo n.º 4
0
        private void sendRequest(ManticoreEngine engine, HttpWebRequest request, JsValue callback)
        {
            request.BeginGetResponse((asyncResult) =>
            {
                HttpWebResponse response = null;
                var errorInstance        = JsValue.Null;
                try
                {
                    response = (HttpWebResponse)request.EndGetResponse(asyncResult);
                }
                catch (WebException e)
                {
                    if (e.Status == WebExceptionStatus.ProtocolError) //Response was received from server but indicated protocol level error
                    {
                        response = (HttpWebResponse)e.Response;
                    }
                    else
                    {
                        errorInstance = new JsErrorBuilder(engine, e).SetErrorCode((int)ErrorCodes.NetworkOffline).Build();
                    }
                }

                if (response == null)
                {
                    engine.Js(() =>
                    {
                        callback.As <FunctionInstance>().Call(engine.ManticoreJsObject, new[] { errorInstance, JsValue.Null });
                    });
                    return;
                }

                var responseInfo = engine.CreateJsObject();
                if (response.Headers.Count > 0)
                {
                    var headerCollection = engine.CreateJsObject();
                    foreach (var kv in response.Headers.AllKeys)
                    {
                        headerCollection.FastAddProperty(kv, new JsValue(response.Headers[kv]), false, true, false);
                    }
                    responseInfo.FastAddProperty("headers", headerCollection, false, true, false);
                }

                responseInfo.FastAddProperty("status", new JsValue((int)response.StatusCode), false, true, false);
                // TODO find a way to sneak this wait into the gap between returning and asking for the results
                // json/body/text signatures probably need to change to take a callback.
                var memStream = new MemoryStream();
                response.GetResponseStream().CopyTo(memStream);
                var binaryResult = memStream.ToArray();
                responseInfo.FastAddProperty("json", engine.AsJsFunction((thisObject, args) => {
                    return(engine.jsEngine.Json.Parse(JsValue.Null, new JsValue[] { Encoding.UTF8.GetString(binaryResult) }));
                }), false, false, false);
                responseInfo.FastAddProperty("body", engine.AsJsFunction((thisObject, args) => {
                    return(engine.jsEngine.Json.Parse(JsValue.Null, new JsValue[] { Convert.ToBase64String(binaryResult) }));
                }), false, false, false);
                responseInfo.FastAddProperty("text", engine.AsJsFunction((thisObject, args) => {
                    return(new JsValue(Encoding.UTF8.GetString(binaryResult)));
                }), false, false, false);
                engine.Js(() =>
                {
                    callback.As <FunctionInstance>().Call(engine.ManticoreJsObject, new[] { errorInstance, responseInfo });
                });
            }, null);
        }