module.exports = function (psqlOptions, wrapperOptions){ psqlOptions = Extend({}, internals.psqlOptions, psqlOptions); wrapperOptions = Extend({}, internals.wrapperOptions, wrapperOptions); let optionsStr = internals.createPsqlOptionsStr(psqlOptions); let shellCommand = `${ wrapperOptions.psqlPath || 'psql' } ${ optionsStr }`; // TODO: add timestamps if (wrapperOptions.displayShellCommand) { console.log(Chalk.blue.bold('executing: ' + shellCommand)); } let output = ''; try { output = execSync(shellCommand, { encoding: 'utf8', maxBuffer: 1000 * 1024, stdio: 'pipe', timeout: 300 * 1000 }); internals.checkForErrors(output); return output; } catch (err) { console.log(Chalk.white.bgRed.bold(err.message.trim())); let exitStatus = err.status; let exitStatusDesc = ''; if (exitStatus >= 1 && exitStatus <= 3) { if (exitStatus === 1) { exitStatusDesc = ': fatal error'; } else if (exitStatus === 2) { exitStatusDesc = ': bad connection to the server'; } else if (exitStatus === 3) { exitStatusDesc = ': an error occurred in a script'; } console.log(Chalk.white.bgRed.bold(`Exit code ${ exitStatus }${ exitStatusDesc }`) + ' (more details here: https://www.postgresql.org/docs/current/static/app-psql.html)') console.log(Chalk.white.bgRed.bold('\nstdout:') + internals.getOutputFormatted(err.output[1])) console.log(Chalk.white.bgRed.bold('\nstderr:') + internals.getOutputFormatted(err.output[2])) } else { console.log(Chalk.white.bgRed.bold('\nOutput from execSync:') + internals.getOutputFormatted(err.output)) } throw err; } };
function render (opts) { state = extend(state, opts); let angles = state.angle; let step = state.step; let frequency = state.frequency; let lineWidth = state.lineWidth; let modWidth = state.modWidth; let modWidthF = state.modWidthF; let modAmp = state.modAmp; let modAmpF = state.modAmpF; let intensity = state.intensity; context.lineJoin = 'round'; context.lineCap = 'round'; let lastX, lastY; context.clearRect(0, 0, canvas.width, canvas.height); context.strokeStyle = state.color; for (let angle = angles[0]; angle <= angles[1]; angle+=step) { context.beginPath(); context.moveTo(lastX, lastY); context.lineWidth = (lineWidth + modWidth*Math.sin(angle * modWidthF + Math.PI*1.666)); let r = -Math.PI * (angle + tri(angle * frequency)*angle*intensity ) + modAmp*Math.sin(angle*modAmpF); let x = cx + -r * Math.cos(angle); let y = cy + r * Math.sin(angle); context.lineTo(x, y); if (lastY != null) { context.stroke(); context.closePath(); } lastX = x; lastY = y; } }
extend(FakeXMLHttpRequest.prototype, sinonEvent.EventTarget, { async: true, open: function open(method, url, async, username, password) { this.method = method; this.url = url; this.async = typeof async === "boolean" ? async : true; this.username = username; this.password = password; clearResponse(this); this.requestHeaders = {}; this.sendFlag = false; if (FakeXMLHttpRequest.useFilters === true) { var xhrArgs = arguments; var defake = FakeXMLHttpRequest.filters.some(function (filter) { return filter.apply(this, xhrArgs); }); if (defake) { FakeXMLHttpRequest.defake(this, arguments); return; } } this.readyStateChange(FakeXMLHttpRequest.OPENED); }, readyStateChange: function readyStateChange(state) { this.readyState = state; var readyStateChangeEvent = new sinonEvent.Event("readystatechange", false, false, this); var event, progress; if (typeof this.onreadystatechange === "function") { try { this.onreadystatechange(readyStateChangeEvent); } catch (e) { this.logError("Fake XHR onreadystatechange handler", e); } } if (this.readyState === FakeXMLHttpRequest.DONE) { if (this.timedOut || this.aborted || this.status === 0) { progress = {loaded: 0, total: 0}; event = (this.timedOut && "timeout") || (this.aborted && "abort") || "error"; } else { progress = {loaded: 100, total: 100}; event = "load"; } if (supportsProgress) { this.upload.dispatchEvent(new sinonEvent.ProgressEvent("progress", progress, this)); this.upload.dispatchEvent(new sinonEvent.ProgressEvent(event, progress, this)); this.upload.dispatchEvent(new sinonEvent.ProgressEvent("loadend", progress, this)); } this.dispatchEvent(new sinonEvent.ProgressEvent("progress", progress, this)); this.dispatchEvent(new sinonEvent.ProgressEvent(event, progress, this)); this.dispatchEvent(new sinonEvent.ProgressEvent("loadend", progress, this)); } this.dispatchEvent(readyStateChangeEvent); }, // Ref https://xhr.spec.whatwg.org/#the-setrequestheader()-method setRequestHeader: function setRequestHeader(header, value) { if (typeof value !== "string") { throw new TypeError("By RFC7230, section 3.2.4, header values should be strings. Got " + typeof value); } verifyState(this); var checkUnsafeHeaders = true; if (typeof this.unsafeHeadersEnabled === "function") { checkUnsafeHeaders = this.unsafeHeadersEnabled(); } if (checkUnsafeHeaders && (getHeader(unsafeHeaders, header) !== null || /^(Sec-|Proxy-)/i.test(header))) { throw new Error("Refused to set unsafe header \"" + header + "\""); } value = normalizeHeaderValue(value); var existingHeader = getHeader(this.requestHeaders, header); if (existingHeader) { this.requestHeaders[existingHeader] += ", " + value; } else { this.requestHeaders[header] = value; } }, setStatus: function setStatus(status) { var sanitizedStatus = typeof status === "number" ? status : 200; verifyRequestOpened(this); this.status = sanitizedStatus; this.statusText = FakeXMLHttpRequest.statusCodes[sanitizedStatus]; }, // Helps testing setResponseHeaders: function setResponseHeaders(headers) { verifyRequestOpened(this); var responseHeaders = this.responseHeaders = {}; Object.keys(headers).forEach(function (header) { responseHeaders[header] = headers[header]; }); if (this.async) { this.readyStateChange(FakeXMLHttpRequest.HEADERS_RECEIVED); } else { this.readyState = FakeXMLHttpRequest.HEADERS_RECEIVED; } }, // Currently treats ALL data as a DOMString (i.e. no Document) send: function send(data) { verifyState(this); if (!/^(head)$/i.test(this.method)) { var contentType = getHeader(this.requestHeaders, "Content-Type"); if (this.requestHeaders[contentType]) { var value = this.requestHeaders[contentType].split(";"); this.requestHeaders[contentType] = value[0] + ";charset=utf-8"; } else if (supportsFormData && !(data instanceof FormData)) { this.requestHeaders["Content-Type"] = "text/plain;charset=utf-8"; } this.requestBody = data; } this.errorFlag = false; this.sendFlag = this.async; clearResponse(this); this.readyStateChange(FakeXMLHttpRequest.OPENED); if (typeof this.onSend === "function") { this.onSend(this); } // Only listen if setInterval and Date are a stubbed. if (sinonXhr.supportsTimeout && typeof setInterval.clock === "object" && typeof Date.clock === "object") { var initiatedTime = Date.now(); var self = this; // Listen to any possible tick by fake timers and check to see if timeout has // been exceeded. It's important to note that timeout can be changed while a request // is in flight, so we must check anytime the end user forces a clock tick to make // sure timeout hasn't changed. // https://xhr.spec.whatwg.org/#dfnReturnLink-2 var clearIntervalId = setInterval(function () { // Check if the readyState has been reset or is done. If this is the case, there // should be no timeout. This will also prevent aborted requests and // fakeServerWithClock from triggering unnecessary responses. if (self.readyState === FakeXMLHttpRequest.UNSENT || self.readyState === FakeXMLHttpRequest.DONE) { clearInterval(clearIntervalId); } else if (typeof self.timeout === "number" && self.timeout > 0) { if (Date.now() >= (initiatedTime + self.timeout)) { self.triggerTimeout(); clearInterval(clearIntervalId); } } }, 1); } this.dispatchEvent(new sinonEvent.Event("loadstart", false, false, this)); }, abort: function abort() { this.aborted = true; requestErrorSteps(this); this.readyState = FakeXMLHttpRequest.UNSENT; }, error: function () { clearResponse(this); this.errorFlag = true; this.requestHeaders = {}; this.responseHeaders = {}; this.readyStateChange(FakeXMLHttpRequest.DONE); }, triggerTimeout: function triggerTimeout() { if (sinonXhr.supportsTimeout) { this.timedOut = true; requestErrorSteps(this); } }, getResponseHeader: function getResponseHeader(header) { if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) { return null; } if (/^Set-Cookie2?$/i.test(header)) { return null; } header = getHeader(this.responseHeaders, header); return this.responseHeaders[header] || null; }, getAllResponseHeaders: function getAllResponseHeaders() { if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) { return ""; } var responseHeaders = this.responseHeaders; var headers = Object.keys(responseHeaders) .filter(excludeSetCookie2Header) .reduce(function (prev, header) { var value = responseHeaders[header]; return prev + (header + ": " + value + "\r\n"); }, ""); return headers; }, setResponseBody: function setResponseBody(body) { verifyRequestSent(this); verifyHeadersReceived(this); verifyResponseBodyType(body, this.responseType); var contentType = this.overriddenMimeType || this.getResponseHeader("Content-Type"); var isTextResponse = this.responseType === "" || this.responseType === "text"; clearResponse(this); if (this.async) { var chunkSize = this.chunkSize || 10; var index = 0; do { this.readyStateChange(FakeXMLHttpRequest.LOADING); if (isTextResponse) { this.responseText = this.response += body.substring(index, index + chunkSize); } index += chunkSize; } while (index < body.length); } this.response = convertResponseBody(this.responseType, contentType, body); if (isTextResponse) { this.responseText = this.response; } if (this.responseType === "document") { this.responseXML = this.response; } else if (this.responseType === "" && isXmlContentType(contentType)) { this.responseXML = FakeXMLHttpRequest.parseXML(this.responseText); } this.readyStateChange(FakeXMLHttpRequest.DONE); }, respond: function respond(status, headers, body) { this.setStatus(status); this.setResponseHeaders(headers || {}); this.setResponseBody(body || ""); }, uploadProgress: function uploadProgress(progressEventRaw) { if (supportsProgress) { this.upload.dispatchEvent(new sinonEvent.ProgressEvent("progress", progressEventRaw, this.upload)); } }, downloadProgress: function downloadProgress(progressEventRaw) { if (supportsProgress) { this.dispatchEvent(new sinonEvent.ProgressEvent("progress", progressEventRaw, this)); } }, uploadError: function uploadError(error) { if (supportsCustomEvent) { this.upload.dispatchEvent(new sinonEvent.CustomEvent("error", {detail: error})); } }, overrideMimeType: function overrideMimeType(type) { if (this.readyState >= FakeXMLHttpRequest.LOADING) { throw new Error("INVALID_STATE_ERR"); } this.overriddenMimeType = type; } });
module.exports.configure = function (psqlOptions, wrapperOptions){ internals.psqlOptions = Extend({}, internals.psqlOptions, psqlOptions); internals.wrapperOptions = Extend({}, internals.wrapperOptions, wrapperOptions); };