Example #1
0
        private async Task GetReferencesAsync(ScriptExecutionContext context, DomainId appId, ClaimsPrincipal user, JsValue references, Action <JsValue> callback)
        {
            Guard.NotNull(callback, nameof(callback));

            var ids = new List <DomainId>();

            if (references.IsString())
            {
                ids.Add(DomainId.Create(references.ToString()));
            }
            else if (references.IsArray())
            {
                foreach (var value in references.AsArray())
                {
                    if (value.IsString())
                    {
                        ids.Add(DomainId.Create(value.ToString()));
                    }
                }
            }

            if (ids.Count == 0)
            {
                var emptyContents = Array.Empty <IEnrichedContentEntity>();

                callback(JsValue.FromObject(context.Engine, emptyContents));
                return;
            }

            context.MarkAsync();

            try
            {
                var app = await GetAppAsync(appId);

                var requestContext =
                    new Context(user, app).Clone(b => b
                                                 .WithoutContentEnrichment()
                                                 .WithUnpublished()
                                                 .WithoutTotal());

                var contentQuery = serviceProvider.GetRequiredService <IContentQueryService>();

                var contents = await contentQuery.QueryAsync(requestContext, Q.Empty.WithIds(ids), context.CancellationToken);

                // Reset the time contraints and other constraints so that our awaiting does not count as script time.
                context.Engine.ResetConstraints();

                callback(JsValue.FromObject(context.Engine, contents.ToArray()));
            }
            catch (Exception ex)
            {
                context.Fail(ex);
            }
        }
Example #2
0
        private async Task RequestAsync(ScriptExecutionContext context, HttpMethod method, string url, JsValue?body, Action <JsValue> callback, JsValue?headers)
        {
            if (callback == null)
            {
                context.Fail(new JavaScriptException("Callback cannot be null."));
                return;
            }

            if (!Uri.TryCreate(url, UriKind.Absolute, out var uri))
            {
                context.Fail(new JavaScriptException("URL is not valid."));
                return;
            }

            context.MarkAsync();

            try
            {
                using (var httpClient = httpClientFactory.CreateClient())
                {
                    using (var request = CreateRequest(context, method, uri, body, headers))
                    {
                        using (var response = await httpClient.SendAsync(request, context.CancellationToken))
                        {
                            response.EnsureSuccessStatusCode();

                            var responseObject = await ParseResponse(context, response);

                            // Reset the time contraints and other constraints so that our awaiting does not count as script time.
                            context.Engine.ResetConstraints();

                            callback(responseObject);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                context.Fail(ex);
            }
        }
        private async Task GetTextAsync(ScriptExecutionContext context, JsValue input, Action <JsValue> callback, JsValue encoding)
        {
            Guard.NotNull(callback, nameof(callback));

            if (input is not ObjectWrapper objectWrapper)
            {
                callback(JsValue.FromObject(context.Engine, "ErrorNoAsset"));
                return;
            }

            async Task ResolveAssetText(DomainId appId, DomainId id, long fileSize, long fileVersion)
            {
                if (fileSize > 256_000)
                {
                    callback(JsValue.FromObject(context.Engine, "ErrorTooBig"));
                    return;
                }

                context.MarkAsync();

                try
                {
                    var assetFileStore = serviceProvider.GetRequiredService <IAssetFileStore>();

                    var encoded = await assetFileStore.GetTextAsync(appId, id, fileVersion, encoding?.ToString());

                    // Reset the time contraints and other constraints so that our awaiting does not count as script time.
                    context.Engine.ResetConstraints();

                    callback(JsValue.FromObject(context.Engine, encoded));
                }
                catch (Exception ex)
                {
                    context.Fail(ex);
                }
            }

            switch (objectWrapper.Target)
            {
            case IAssetEntity asset:
                await ResolveAssetText(asset.AppId.Id, asset.Id, asset.FileSize, asset.FileVersion);

                return;

            case EnrichedAssetEvent @event:
                await ResolveAssetText(@event.AppId.Id, @event.Id, @event.FileSize, @event.FileVersion);

                return;
            }

            callback(JsValue.FromObject(context.Engine, "ErrorNoAsset"));
        }