UNPKG

siesta-lite

Version:

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

148 lines (104 loc) 4.51 kB
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>The source code</title> <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" /> <script type="text/javascript" src="../resources/prettify/prettify.js"></script> <style type="text/css"> .highlight { display: block; background-color: #ddd; } </style> <script type="text/javascript"> function highlight() { document.getElementById(location.hash.replace(/#/, "")).className = "highlight"; } </script> </head> <body onload="prettyPrint(); highlight();"> <pre class="prettyprint lang-js">/* Siesta 5.6.1 Copyright(c) 2009-2022 Bryntum AB https://bryntum.com/contact https://bryntum.com/products/siesta/license */ Class(&#39;Siesta.Project.Browser.Automation.ScriptExecution&#39;, { has : { id : { required : true }, chunks : Joose.I.Array, maxMessageSize : { required : true }, result : null, exception : null, resultPos : 0, resultChunkIndex : 0, isDestroyed : false }, methods : { execute: function () { if (this.isDestroyed) throw new Error(&quot;Result already retrieved&quot;) var geval = typeof executeScript != &#39;undefined&#39; ? executeScript : eval __EXECUTE_SCRIPT_RESULT__ = null try { geval(&#39;__EXECUTE_SCRIPT_RESULT__ = (function () {&#39; + this.chunks.join(&#39;&#39;) + &#39;})()&#39;) } catch (e) { // always assume exception message size is negligible this.exception = e + &#39;&#39; + (e.stack ? &#39;\n&#39; + e.stack : &#39;&#39;) return } this.result = JSON.stringify(__EXECUTE_SCRIPT_RESULT__) || &quot;null&quot; __EXECUTE_SCRIPT_RESULT__ = undefined }, addChunk : function (text, index) { if (this.isDestroyed) throw new Error(&quot;Result already retrieved&quot;) if (index != this.chunks.length) throw new Error(&quot;Non-sequential chunk index&quot;) this.chunks.push(text) }, getPartialResult : function (scriptId, index) { if (this.isDestroyed) throw new Error(&quot;Result already retrieved&quot;) if (scriptId != this.id) throw new Error(&quot;Wrong id&quot;) if (this.exception) { if (index) throw new Error(&quot;Started from non-zero index&quot;) var res = JSON.stringify({ exception : this.exception, chunk : null, index : 0, isLastChunk : true }) this.destroy() return res } var text = this.result var length = text.length var resultPos = this.resultPos if (this.resultChunkIndex != index) throw new Error(&quot;Non-sequential result fetching&quot;) if (resultPos &gt;= length) throw new Error(&quot;Over fetching of the result&quot;) var chunk var chunkSize = this.maxMessageSize * 0.8 while (!chunk) { // while stringifying, the size of the chunk can increase, we don&#39;t know how much upfront chunk = JSON.stringify(text.substr(resultPos, chunkSize)) if (chunk.length &gt; this.maxMessageSize) { chunk = null chunkSize = Math.floor(chunkSize * 0.8) if (!chunkSize) throw new Error(&quot;Chunk size zero&quot;) } } var res = { exception : null, chunk : text.substr(resultPos, chunkSize), index : this.resultChunkIndex++ } this.resultPos += chunkSize res.isLastChunk = this.resultPos &gt;= length if (res.isLastChunk) this.destroy() return JSON.stringify(res) }, destroy : function () { this.isDestroyed = true this.chunks = this.result = this.exception = null } } // eof methods }) </pre> </body> </html>