commit 8070095b9239327084f39bc72427f2c6a90044c5 Author: Luis Fernando Molina Date: Wed Feb 14 13:00:19 2018 +0100 Initial commit diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..eba1110b5 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto \ No newline at end of file diff --git a/.vs/AAPlatform/v15/.suo b/.vs/AAPlatform/v15/.suo new file mode 100644 index 000000000..234526e37 Binary files /dev/null and b/.vs/AAPlatform/v15/.suo differ diff --git a/.vs/AAWebPlatform/v15/.suo b/.vs/AAWebPlatform/v15/.suo new file mode 100644 index 000000000..599f546a5 Binary files /dev/null and b/.vs/AAWebPlatform/v15/.suo differ diff --git a/.vs/ProjectSettings.json b/.vs/ProjectSettings.json new file mode 100644 index 000000000..f8b488856 --- /dev/null +++ b/.vs/ProjectSettings.json @@ -0,0 +1,3 @@ +{ + "CurrentProjectSetting": null +} \ No newline at end of file diff --git a/.vs/VSWorkspaceState.json b/.vs/VSWorkspaceState.json new file mode 100644 index 000000000..6b6114114 --- /dev/null +++ b/.vs/VSWorkspaceState.json @@ -0,0 +1,6 @@ +{ + "ExpandedNodes": [ + "" + ], + "PreviewInSolutionExplorer": false +} \ No newline at end of file diff --git a/.vs/browser/v15/.suo b/.vs/browser/v15/.suo new file mode 100644 index 000000000..344fbebac Binary files /dev/null and b/.vs/browser/v15/.suo differ diff --git a/.vs/slnx.sqlite b/.vs/slnx.sqlite new file mode 100644 index 000000000..cc77797ae Binary files /dev/null and b/.vs/slnx.sqlite differ diff --git a/AAWebPlatform.njsproj b/AAWebPlatform.njsproj new file mode 100644 index 000000000..b869122bf --- /dev/null +++ b/AAWebPlatform.njsproj @@ -0,0 +1,165 @@ + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + AAWebPlatform + browser + + + + Debug + 2.0 + 3ef11619-de26-4b8b-819d-a413df220b76 + + + server.js + + + . + . + v4.0 + {3AF33F2E-1136-4D97-BBB7-1795711AC8B8};{349c5851-65df-11da-9384-00065b846f21};{9092AA53-FB77-4645-B42D-1CCCA6BD08BD} + 1337 + true + + + true + + + true + + + + + + + + + + + Code + + + + + + + + + + + + + + + + + + + + + + + + + + + Code + + + Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + False + True + 0 + / + http://localhost:48022/ + False + True + http://localhost:1337 + False + + + + + + + CurrentPage + True + False + False + False + + + + + + + + + False + False + + + + + \ No newline at end of file diff --git a/AAWebPlatform.sln b/AAWebPlatform.sln new file mode 100644 index 000000000..705d8abc8 --- /dev/null +++ b/AAWebPlatform.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26730.10 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9092AA53-FB77-4645-B42D-1CCCA6BD08BD}") = "AAWebPlatform", "AAWebPlatform.njsproj", "{3EF11619-DE26-4B8B-819D-A413DF220B76}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3EF11619-DE26-4B8B-819D-A413DF220B76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3EF11619-DE26-4B8B-819D-A413DF220B76}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3EF11619-DE26-4B8B-819D-A413DF220B76}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3EF11619-DE26-4B8B-819D-A413DF220B76}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {92DF2F3B-B37A-407F-B313-3897AEFCCAFC} + EndGlobalSection +EndGlobal diff --git a/AccumulatedVolumeBar.js b/AccumulatedVolumeBar.js new file mode 100644 index 000000000..191a44b46 --- /dev/null +++ b/AccumulatedVolumeBar.js @@ -0,0 +1,12 @@ +function newAccumulatedVolumeBar() { + + var accumulatedVolumeBar = { + begin: undefined, + end: undefined, + accumulatedBuys: 0, + accumulatedSells: 0 + }; + + return accumulatedVolumeBar; + +} \ No newline at end of file diff --git a/AggregatedOrdersArray.js b/AggregatedOrdersArray.js new file mode 100644 index 000000000..ff6b77e8e --- /dev/null +++ b/AggregatedOrdersArray.js @@ -0,0 +1,120 @@ + +function newAggregatedOrdersArray() { + + /* This object generates an array of orders that are aggregated by Amount A. */ + + var aggregatedOrdersArray = { + eventHandler: undefined, + addOrder: addOrder, + removeOrder: removeOrder, + setSortOrder: setSortOrder, + initialize: initialize + }; + + var array = []; + + var sortOrder = 'ascending'; + + aggregatedOrdersArray.eventHandler = newEventHandler(); + + return aggregatedOrdersArray; + + function initialize() { + this.eventHandler.initialize(); + } + + function addOrder(order) { + + var wasAdded = false; + + for (var i = 0; i < array.length; i++) { + + var currentItem = array[i]; + + if (currentItem.rate === order.rate) { + + currentItem.amountSum = currentItem.amountSum + order.amountA; + wasAdded = true; + + } + + } + + if (wasAdded === false) { + + var newItem = { + rate: order.rate, + amountSum: order.amountA + }; + + array.push(newItem); + + } + + applySortOrder(); + + aggregatedOrdersArray.eventHandler.raiseEvent('Array Changed', array); + + } + + function removeOrder(order) { + + for (var i = 0; i < array.length; i++) { + + var currentItem = array[i]; + + if (currentItem.rate === order.rate) { // We find the item in the array where the rate is the same as the orders rate. + + currentItem.amountSum = currentItem.amountSum - order.amountA; + + if (currentItem.amountSum === 0) { // If we reach zero, then there is no reason to keep this element in the array. + + array.splice(i, 1); // This means remove this item from the array. + + } + + } + + } + + applySortOrder(); + + aggregatedOrdersArray.eventHandler.raiseEvent('Array Changed', array); + + } + + function setSortOrder(newSortOrder) { + + sortOrder = newSortOrder; + + applySortOrder(); + + aggregatedOrdersArray.eventHandler.raiseEvent('Array Changed', array); + + } + + function applySortOrder() { + + array.sort(compare); + + } + + function compare(itemA, itemB) { + + if (sortOrder === 'ascending') { + + return itemA.rate - itemB.rate; + + } + + if (sortOrder === 'descending') { + + return itemB.rate - itemA.rate; + + } + + return undefined; + + } + +} diff --git a/Animation.js b/Animation.js new file mode 100644 index 000000000..073bde721 --- /dev/null +++ b/Animation.js @@ -0,0 +1,90 @@ +/* + +Essentially, what the animation object does, is to call several functions in order to draw the content of each frame. +To do that, it has an array of functions that need to be called, and it just goes one by one through them. His parent object +is responsible for providing more functions to call, or removing functions from its list when they are not longer needed. + +*/ + +function newAnimation() { + + var animationLoopHandle; // This is the handle to the animation loop. With this handle we can cancel the loop, for instance. + var callBackFunctions = new Map(); + var clearCanvasFunction; + + var animation = { + start: start, + stop: stop, + addCallBackFunction: addCallBackFunction, + removeCallBackFunction: removeCallBackFunction, + initialize: initialize + }; + + return animation; + + function initialize(callBackFunction) { + + /* We store the function to be called each time we need to draw a new animation frame. */ + + clearCanvasFunction = callBackFunction; + } + + + function start() { + + animationLoop(); // Inside this function the animation process is started, and at the same time it creates a loop. + + } + + function stop() { + + window.cancelAnimationFrame(animationLoopHandle); + + } + + function addCallBackFunction(key, callBackFunction) { + + callBackFunctions.set(key, callBackFunction); + } + + function removeCallBackFunction(key, callBackFunction) { + + callBackFunctions.delete(key); + } + + function animationLoop() { + + /* First thing is to clear the actual canvas */ + + clearCanvasFunction(); + + /* We loop through the callback functions collections and execute them all. */ + + callBackFunctions.forEach(function (callBackFunction) { + + callBackFunction(); + + }); + + /* We also draw this array of circles, that is used for debugging purposes. */ + + drawSmallBall(); + + /* We request the next frame to be drawn, and stablishing a loop */ + + animationLoopHandle = window.requestAnimationFrame(animationLoop); + + /* We animate the zoom of the viewPort */ + + viewPort.animate(); + viewPort.draw(); + + } +} + + + + + + + diff --git a/Azure/azure-storage.common.js b/Azure/azure-storage.common.js new file mode 100644 index 000000000..125658c00 --- /dev/null +++ b/Azure/azure-storage.common.js @@ -0,0 +1,42918 @@ +// Azure Storage JavaScript Client Library 0.2.6-preview.12 +// Copyright (c) Microsoft and contributors. All rights reserved. +require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o= end means there is no valid regions + ranges.push({ start : start, end : rangeEnd, dataSize: 0 }); + } else if (ranges[ranges.length - 1].end !== rangeEnd) { + // don't forget the zero chunk at the end of range + ranges.push({ start : ranges[ranges.length - 1].end + 1, end : rangeEnd, dataSize: 0 }); + } + + if (end >= endOffset) { + self._rangeStreamEnded = true; + } + self.resizeAndSaveRanges(ranges); + self._startOffset += singleRangeSize; + self._emitRange(); + + // This is only valid when listing pages because when listing with the default function, the "endOffset" will always equal to or greater than the "end". + if (end < endOffset && !self._rangeStreamEnded) { + process.nextTick(function () { + ranges = null; + self.list(options, callback); + self = null; + }); + } + } + }; + + var callArguments = Array.prototype.slice.call(this._resourcePath); + callArguments.push(options); + callArguments.push(onList); + this._listFunc.apply(this.serviceClient, callArguments); +}; + +/** +* Resize regions: +* 1. Merge small pieces into a range no less than this._minRangeSize +* 2. Split large pieces into ranges no more than this._maxRangeSize +*/ +RangeStream.prototype.resizeAndSaveRanges = function (ranges) { + var rangeList = this._rangelist; + var holdingRange = { type : 'range', size : 0, dataSize : 0, start : this._startOffset, end : -1 }; + var readingRange = null; + var rangeSize = 0; + + for (var index = 0; index < ranges.length; index++) { + readingRange = ranges[index]; + rangeSize = readingRange.end - holdingRange.start + 1; + + if (rangeSize < this._minRangeSize) { + // merge fragment ranges + this.mergeRanges(holdingRange, readingRange); + } else { + if (holdingRange.end != -1) { + // save the holding range list and hold the reading range + this.splitAndSaveRanges(holdingRange, rangeList); + holdingRange = readingRange; + } + + if (this._dataOffset != readingRange.start) { + // padding zero for empty range and hold the reading range + this.putZeroRange(this._dataOffset, readingRange.start - 1, rangeList); + holdingRange = readingRange; + } else if (holdingRange.end == -1) { + // if holdingRange is never set, it means readingRange exceeds MIN_WRITE_FILE_SIZE_IN_BYTES + this.splitAndSaveRanges(readingRange, rangeList); + // reading range has been saved, offset the holding start position for calculating the range size in next loop + holdingRange.start = readingRange.end + 1; + } + } + + // If it is the last range, put the holding range into list anyway + if (index == ranges.length - 1 && holdingRange.end > holdingRange.start) { + this.splitAndSaveRanges(holdingRange, rangeList); + } + + this._dataOffset = readingRange.end + 1; + } +}; + +/** +* Put a zero range into range list +*/ +RangeStream.prototype.putZeroRange = function (startOffset, endOffset, rangeList) { + var zeroDataRange = { type : 'range', size : -1, dataSize : 0, start : startOffset, end : endOffset }; + this.splitAndSaveRanges(zeroDataRange, rangeList); +}; + +/** +* Merge small ranges +*/ +RangeStream.prototype.mergeRanges = function (holdingRange, readingRange) { + holdingRange.size = readingRange.end - holdingRange.start + 1; + holdingRange.dataSize += readingRange.dataSize; + holdingRange.end = readingRange.end; + return holdingRange; +}; + +/** +* Split range into small pieces with maximum _maxRangeSize and minimum _minRangeSize size. +* For example, [0, 10G - 1] => [0, 4MB - 1], [4MB, 8MB - 1] ... [10GB - 4MB, 10GB - 1] +*/ +RangeStream.prototype.splitAndSaveRanges = function (range, rangeList) { + var rangeSize = range.end - range.start + 1; + var offset = range.start; + var limitedSize = 0; + + while (rangeSize > 0) { + var newRange = { type : 'range', size : 0, dataSize : 0, start : -1, end : -1 }; + limitedSize = Math.min(rangeSize, this._maxRangeSize); + newRange.start = offset; + newRange.size = limitedSize; + if (range.dataSize === 0) { + newRange.dataSize = 0; + } else { + newRange.dataSize = limitedSize; + } + offset += limitedSize; + newRange.end = offset - 1; + rangeList.push(newRange); + rangeSize -= limitedSize; + } +}; + +/** +* Emit a range +*/ +RangeStream.prototype._emitRange = function () { + if (this._paused || this._emittedAll || this._isEmitting) return; + this._isEmitting = true; + try { + for (; this._emittedRangeIndex < this._rangelist.length; this._emittedRangeIndex++) { + if (this._paused) { + return; + } + var range = this._rangelist[this._emittedRangeIndex]; + this._emitter.emit('range', range); + this._rangelist[this._emittedRangeIndex] = null; + } + + if (this._rangeStreamEnded) { + this._rangelist = null; + this._emittedAll = true; + this._emitter.emit('end'); + } + } finally { + this._isEmitting = false; + } +}; + +/** +* The Default list function which puts the whole blob into one range. +*/ +RangeStream.prototype._defaultListFunc = function (container, blob, optionsOrCallback, callback) { + var options; + azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { options = o; callback = c; }); + + this.getBlobProperties(container, blob, options, function (error, result, response) { + if (error) { + callback(error); + } else { + var range = [{}]; + range[0].start = options.rangeStart ? Math.max(options.rangeStart, 0) : 0; + range[0].end = options.rangeEnd ? Math.min(options.rangeEnd, result.contentLength - 1) : result.contentLength - 1; + range[0].size = range[0].end - range[0].start + 1; + range[0].dataSize = range[0].size; + callback(error, range, response); + } + }); +}; + +/** +* Add event listener +*/ +RangeStream.prototype.on = function (event, listener) { + this._emitter.on(event, listener); +}; + +/** +* Pause the stream +*/ +RangeStream.prototype.pause = function () { + this._paused = true; +}; + +/** +* Resume the stream +*/ +RangeStream.prototype.resume = function () { + this._paused = false; + if (!this._isEmitting) { + this._emitRange(); + } +}; + +/** +* Stop the stream +*/ +RangeStream.prototype.stop = function () { + this.pause(); + this._emittedAll = true; + this._emitter.emit('end'); +}; + +module.exports = RangeStream; + +}).call(this,require('_process')) +},{"./../common.core":6,"./../util/constants":33,"_process":197,"events":155}],5:[function(require,module,exports){ +// +// Copyright (c) Microsoft and contributors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// +// See the License for the specific language governing permissions and +// limitations under the License. +// + +var azureCommon = require('./common.core'); + +azureCommon.BrowserFileReadStream = require('./streams/browserfilereadstream'); + +module.exports = azureCommon; +},{"./common.core":6,"./streams/browserfilereadstream":25}],25:[function(require,module,exports){ +// +// Copyright (c) Microsoft and contributors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// +// See the License for the specific language governing permissions and +// limitations under the License. +// + +var buffer = require('buffer').Buffer; +var stream = require('stream'); +var util = require('util'); + +var Constants = require('../util/constants'); +var bufferSize = Constants.BlobConstants.DEFAULT_WRITE_BLOCK_SIZE_IN_BYTES; + +function BrowserFileReadStream(file, options) { + stream.Readable.call(this, options); + + if (!options) { + options = {}; + } + + this._fileReader = new FileReader(file); // HTML FileReader + this._file = file; + this._size = file.size; + this._highWaterMark = options.highWaterMark || bufferSize; + this._offset = 0; + var self = this; + + this._fileReader.onloadend = function (event) { + var data = event.target.result; + var buf = buffer.from(data); + self.push(buf); + }; + + this._fileReader.onerror = function (error) { + self.emit('error', error); + }; +} +util.inherits(BrowserFileReadStream, stream.Readable); + +BrowserFileReadStream.prototype._read = function() { + if (this._offset >= this._size) { + this.push(null); + } else { + var end = this._offset + this._highWaterMark; + var slice = this._file.slice(this._offset, end); + this._fileReader.readAsArrayBuffer(slice); + this._offset = end; + } +}; + +module.exports = BrowserFileReadStream; +},{"../util/constants":33,"buffer":"buffer","stream":"stream","util":"util"}],6:[function(require,module,exports){ +// +// Copyright (c) Microsoft and contributors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// +// See the License for the specific language governing permissions and +// limitations under the License. +// + +var exports = module.exports; + +var azureutil = require('./util/util'); + +require('./util/patch-xmlbuilder'); + +var nodeVersion = azureutil.getNodeVersion(); +if (nodeVersion.major === 0 && nodeVersion.minor > 8 && !(nodeVersion.minor > 10 || (nodeVersion.minor === 10 && nodeVersion.patch >= 3))) { + throw new Error('The Microsoft Azure node SDK does not work with node versions > 0.9.0 and < 0.10.3. Please upgrade to node >= 0.10.3'); +} + +exports.xmlbuilder = require('xmlbuilder'); +exports.xml2js = require('xml2js'); + +exports.Logger = require('./diagnostics/logger'); +exports.WebResource = require('./http/webresource'); + +// Services +exports.StorageServiceClient = require('./services/storageserviceclient'); + +// Models +exports.ServicePropertiesResult = require('./models/servicepropertiesresult'); +exports.ServiceStatsParser = require('./models/servicestatsparser'); +exports.AclResult = require('./models/aclresult'); + +// Filters +exports.LinearRetryPolicyFilter = require('./filters/linearretrypolicyfilter'); +exports.ExponentialRetryPolicyFilter = require('./filters/exponentialretrypolicyfilter'); +exports.RetryPolicyFilter = require('./filters/retrypolicyfilter'); + +// Signing +exports.SharedAccessSignature = require('./signing/sharedaccesssignature'); +exports.SharedKey = require('./signing/sharedkey'); + +// Streams +exports.BatchOperation = require('./streams/batchoperation'); +exports.ChunkAllocator = require('./streams/chunkallocator'); +exports.ChunkStream = require('./streams/chunkstream'); +exports.ChunkStreamWithStream = require('./streams/chunkstreamwithstream'); +exports.SpeedSummary = require('./streams/speedsummary'); +exports.BufferStream = require('./streams/bufferstream'); + +// Utilities +exports.Constants = require('./util/constants'); +exports.SR = require('./util/sr'); +exports.date = require('./util/date'); +exports.ISO8061Date = require('./util/iso8061date'); +exports.util = require('./util/util'); +exports.validate = require('./util/validate'); +exports.StorageUtilities = require('./util/storageutilities'); +exports.AccessCondition = require('./util/accesscondition'); +},{"./diagnostics/logger":7,"./filters/exponentialretrypolicyfilter":9,"./filters/linearretrypolicyfilter":10,"./filters/retrypolicyfilter":11,"./http/webresource":12,"./models/aclresult":14,"./models/servicepropertiesresult":15,"./models/servicestatsparser":16,"./services/storageserviceclient":19,"./signing/sharedaccesssignature":22,"./signing/sharedkey":23,"./streams/batchoperation":24,"./streams/bufferstream":26,"./streams/chunkallocator":27,"./streams/chunkstream":28,"./streams/chunkstreamwithstream":29,"./streams/speedsummary":31,"./util/accesscondition":32,"./util/constants":33,"./util/date":34,"./util/iso8061date":35,"./util/patch-xmlbuilder":36,"./util/sr":37,"./util/storageutilities":38,"./util/util":39,"./util/validate":40,"xml2js":260,"xmlbuilder":263}],36:[function(require,module,exports){ +// +// Copyright (c) Microsoft and contributors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// +// See the License for the specific language governing permissions and +// limitations under the License. +// + +'use strict'; + +var XMLBuilder = require('xmlbuilder/lib/XMLBuilder'); + +// Patch xmlbuilder to allow Unicode surrogate pair code +// points in XML bodies + +XMLBuilder.prototype.assertLegalChar = function(str) { + var chars, chr; + chars = /[\u0000-\u0008\u000B-\u000C\u000E-\u001F\uFFFE-\uFFFF]/; + chr = str.match(chars); + if (chr) { + throw new Error('Invalid character (' + chr + ') in string: ' + str); + } +}; +},{"xmlbuilder/lib/XMLBuilder":261}],34:[function(require,module,exports){ +// +// Copyright (c) Microsoft and contributors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// +// See the License for the specific language governing permissions and +// limitations under the License. +// + +/** +* Date/time related helper functions +* @module date +* +*/ + +/** +* Generates a Date object which is in the given days from now. +* +* @param {int} days The days timespan. +* @return {Date} +*/ +exports.daysFromNow = function (days) { + var date = new Date(); + date.setDate(date.getDate() + days); + return date; +}; + +/** +* Generates a Date object which is in the given hours from now. +* +* @param {int} hours The hours timespan. +* @return {Date} +*/ +exports.hoursFromNow = function (hours) { + var date = new Date(); + date.setHours(date.getHours() + hours); + return date; +}; + +/** +* Generates a Date object which is in the given minutes from now. +* +* @param {int} minutes The minutes timespan. +* @return {Date} +*/ +exports.minutesFromNow = function (minutes) { + var date = new Date(); + date.setMinutes(date.getMinutes() + minutes); + return date; +}; + +/** +* Generates a Date object which is in the given seconds from now. +* +* @param {int} seconds The seconds timespan. +* @return {Date} +*/ +exports.secondsFromNow = function (seconds) { + var date = new Date(); + date.setSeconds(date.getSeconds() + seconds); + return date; +}; +},{}],32:[function(require,module,exports){ +// +// Copyright (c) Microsoft and contributors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// +// See the License for the specific language governing permissions and +// limitations under the License. +// + +// Expose 'AccessCondition'. + +/** +* Defines constants, enums, and utility functions for use with storage access condition. +* @namespace +*/ + +'use strict'; + +exports = module.exports; + +/** +* Constructs an empty access condition. +* +* @return {object} An empty AccessCondition object +*/ +exports.generateEmptyCondition = function () { + return {}; +}; + +/** +* Constructs an access condition such that an operation will be performed only if the resource does not exist on the service +* +* Setting this access condition modifies the request to include the HTTP If-None-Match conditional header + +* @return {AccessConditions} An AccessCondition object that represents a condition that checks for nonexistence +*/ +exports.generateIfNotExistsCondition = function () { + var accessCondition = {}; + accessCondition.EtagNonMatch = '*'; + return accessCondition; +}; + +/** +* Constructs an access condition such that an operation will be performed only if the resource exists on the service +* +* Setting this access condition modifies the request to include the HTTP If-Match conditional header + +* @return {AccessConditions} An AccessCondition object that represents a condition that checks for existence +*/ +exports.generateIfExistsCondition = function () { + var accessCondition = {}; + accessCondition.EtagMatch = '*'; + return accessCondition; +}; + +/** +* Constructs an access condition such that an operation will be performed only if the resource's ETag value +* does not match the specified ETag value +* +* Setting this access condition modifies the request to include the HTTP If-None-Match conditional header +* +* @param {string} etag The ETag value to check against the resource's ETag +* @return {AccessConditions} An AccessCondition object that represents the If-None-Match condition +*/ +exports.generateIfNoneMatchCondition = function (etag) { + var accessCondition = {}; + accessCondition.EtagNonMatch = etag; + return accessCondition; +}; + +/** +* Constructs an access condition such that an operation will be performed only if the resource's ETag value +* matches the specified ETag value +* +* Setting this access condition modifies the request to include the HTTP If-Match conditional header +* +* @param {string} etag The ETag value to check against the resource's ETag +* @return {AccessConditions} An AccessCondition object that represents the If-Match condition +*/ +exports.generateIfMatchCondition = function (etag) { + var accessCondition = {}; + accessCondition.EtagMatch = etag; + return accessCondition; +}; + +/** +* Constructs an access condition such that an operation will be performed only if the resource has been +* modified since the specified time +* +* Setting this access condition modifies the request to include the HTTP If-Modified-Since conditional header +* +* @param {Date|string} time A date object specifying the time since which the resource must have been modified +* @return {AccessConditions} An AccessCondition object that represents the If-Modified-Since condition +*/ +exports.generateIfModifiedSinceCondition = function (time) { + var accessCondition = {}; + accessCondition.DateModifedSince = time; + return accessCondition; +}; + +/** +* Constructs an access condition such that an operation will be performed only if the resource has not been +* modified since the specified time +* +* Setting this access condition modifies the request to include the HTTP If-Unmodified-Since conditional header +* +* @param {Date|string} time A date object specifying the time since which the resource must have not been modified +* @return {AccessConditions} An AccessCondition object that represents the If-Unmodified-Since condition +*/ +exports.generateIfNotModifiedSinceCondition = function (time) { + var accessCondition = {}; + accessCondition.DateUnModifiedSince = time; + return accessCondition; +}; + +/** +* Constructs an access condition such that an operation will be performed only if the resource's sequence number +* is equal to the specified value +* +* Setting this access condition modifies the request to include the HTTP x-ms-if-sequence-number-eq conditional header +* +* @param {Number|string} sequenceNumber A date object specifying the time since which the resource must have not been modified +* @return {AccessConditions} An AccessCondition object that represents the If-Unmodified-Since condition +*/ +exports.generateSequenceNumberEqualCondition = function (sequenceNumber) { + var accessCondition = {}; + accessCondition.SequenceNumberEqual = sequenceNumber; + return accessCondition; +}; + +/** +* Constructs an access condition such that an operation will be performed only if the resource's sequence number +* is less than the specified value +* +* Setting this access condition modifies the request to include the HTTP x-ms-if-sequence-number-lt conditional header +* +* @param {Number|string} sequenceNumber A date object specifying the time since which the resource must have not been modified +* @return {AccessConditions} An AccessCondition object that represents the If-Unmodified-Since condition +*/ +exports.generateSequenceNumberLessThanCondition = function (sequenceNumber) { + var accessCondition = {}; + accessCondition.SequenceNumberLessThan = sequenceNumber; + return accessCondition; +}; + +/** +* Constructs an access condition such that an operation will be performed only if the resource's sequence number +* is less than or equal to the specified value +* +* Setting this access condition modifies the request to include the HTTP x-ms-if-sequence-number-le conditional header +* +* @param {Number|string} sequenceNumber A date object specifying the time since which the resource must have not been modified +* @return {AccessConditions} An AccessCondition object that represents the If-Unmodified-Since condition +*/ +exports.generateSequenceNumberLessThanOrEqualCondition = function (sequenceNumber) { + var accessCondition = {}; + accessCondition.SequenceNumberLessThanOrEqual = sequenceNumber; + return accessCondition; +}; +},{}],31:[function(require,module,exports){ +// +// Copyright (c) Microsoft and contributors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// +// See the License for the specific language governing permissions and +// limitations under the License. +// + +var util = require('util'); +var azureutil = require('../util/util'); + +/** +* Blob upload/download speed summary +*/ +function SpeedSummary (name) { + this.name = name; + this._startTime = Date.now(); + this._timeWindowInSeconds = 10; + this._timeWindow = this._timeWindowInSeconds * 1000; + this._totalWindowSize = 0; + this._speedTracks = new Array(this._timeWindowInSeconds); + this._speedTrackPtr = 0; + this.totalSize = undefined; + this.completeSize = 0; +} + +/** +* Convert the size to human readable size +*/ +function toHumanReadableSize(size, len) { + if(!size) return '0B'; + if (!len || len <= 0) { + len = 2; + } + var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + var i = Math.floor( Math.log(size) / Math.log(1024)); + return (size/Math.pow(1024, i)).toFixed(len) + units[i]; +} + +/** +* Get running seconds +*/ +SpeedSummary.prototype.getElapsedSeconds = function(humanReadable) { + var now = Date.now(); + var seconds = parseInt((now - this._startTime) / 1000, 10); + if (humanReadable !== false) { + var s = parseInt(seconds % 60, 10); + seconds /= 60; + var m = Math.floor(seconds % 60); + seconds /= 60; + var h = Math.floor(seconds); + seconds = util.format('%s:%s:%s', azureutil.zeroPaddingString(h, 2), azureutil.zeroPaddingString(m, 2), azureutil.zeroPaddingString(s, 2)); + } + return seconds; +}; + +/** +* Get complete percentage +* @param {int} len The number of digits after the decimal point. +*/ +SpeedSummary.prototype.getCompletePercent = function(len) { + if (this.totalSize) { + if(!len || len <= 0) { + len = 1; + } + return (this.completeSize * 100 / this.totalSize).toFixed(len); + } else { + if(this.totalSize === 0) { + return 100; + } else { + return 0; + } + } +}; + +/** +* Get average upload/download speed +*/ +SpeedSummary.prototype.getAverageSpeed = function(humanReadable) { + var elapsedTime = this.getElapsedSeconds(false); + return this._getInternalSpeed(this.completeSize, elapsedTime, humanReadable); +}; + +/** +* Get instant speed +*/ +SpeedSummary.prototype.getSpeed = function(humanReadable) { + this._refreshSpeedTracks(); + var elapsedTime = this.getElapsedSeconds(false); + elapsedTime = Math.min(elapsedTime, this._timeWindowInSeconds); + return this._getInternalSpeed(this._totalWindowSize, elapsedTime, humanReadable); +}; + +/** +* Get internal speed +*/ +SpeedSummary.prototype._getInternalSpeed = function(totalSize, elapsedTime, humanReadable) { + if (elapsedTime <= 0) { + elapsedTime = 1; + } + var speed = totalSize / elapsedTime; + if(humanReadable !== false) { + speed = toHumanReadableSize(speed) + '/s'; + } + return speed; +}; + +/** +* Refresh speed tracks +*/ +SpeedSummary.prototype._refreshSpeedTracks = function() { + var now = Date.now(); + var totalSize = 0; + for(var i = 0; i < this._speedTracks.length; i++) { + if(!this._speedTracks[i]) continue; + if(now - this._speedTracks[i].timeStamp <= this._timeWindow) { + totalSize += this._speedTracks[i].size; + } else { + this._speedTracks[i] = null; + } + } + this._totalWindowSize = totalSize; +}; + +/** +* Increment the complete data size +*/ +SpeedSummary.prototype.increment = function(len) { + this.completeSize += len; + this._recordSpeed(len); + return this.completeSize; +}; + +/** +* record complete size into speed tracks +*/ +SpeedSummary.prototype._recordSpeed = function(completeSize) { + var now = Date.now(); + var track = this._speedTracks[this._speedTrackPtr]; + if(track) { + var timeDiff = now - track.timeStamp; + if(timeDiff > this._timeWindow) { + track.timeStamp = now; + track.size = completeSize; + } else if(timeDiff <= 1000) { //1 seconds + track.size += completeSize; + } else { + this._speedTrackPtr = (this._speedTrackPtr + 1) % this._timeWindowInSeconds; + this._recordSpeed(completeSize); + } + } else { + track = {timeStamp : now, size: completeSize}; + this._speedTracks[this._speedTrackPtr] = track; + } +}; + +/** +* Get auto increment function +*/ +SpeedSummary.prototype.getAutoIncrementFunction = function(size) { + var self = this; + return function(error, retValue) { + if(!error) { + var doneSize = 0; + if((!retValue && retValue !== 0) || isNaN(retValue)) { + doneSize = size; + } else { + doneSize = retValue; + } + self.increment(doneSize); + } + }; +}; + +/** +* Get total size +*/ +SpeedSummary.prototype.getTotalSize = function(humanReadable) { + if (humanReadable !== false) { + return toHumanReadableSize(this.totalSize); + } else { + return this.totalSize; + } +}; + +/** +* Get completed data size +*/ +SpeedSummary.prototype.getCompleteSize = function(humanReadable) { + if (humanReadable !== false) { + return toHumanReadableSize(this.completeSize); + } else { + return this.completeSize; + } +}; + +module.exports = SpeedSummary; + +},{"../util/util":39,"util":"util"}],29:[function(require,module,exports){ +// +// Copyright (c) Microsoft and contributors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// +// See the License for the specific language governing permissions and +// limitations under the License. +// + +var ChunkStream = require('./chunkstream'); +var EventEmitter = require('events').EventEmitter; +var util = require('util'); +var azureutil = require('./../util/util'); + +/** +* Chunk stream +* 1. Calculate md5 +* 2. Track reading offset +* 3. Work with customize memory allocator +* 4. Buffer data from stream. +* @param {object} options stream.Readable options +*/ +function ChunkStreamWithStream(stream, options) { + ChunkStream.call(this, options); + + stream.pause(); // Pause stream and wait for data listener. It's useful for node v0.8 + this._stream = stream; + this._stream.on('end', this.end.bind(this)); // Should catch the end event for node v0.8 +} + +util.inherits(ChunkStreamWithStream, ChunkStream); + +/** +* Add event listener +*/ +ChunkStreamWithStream.prototype.on = function(event, listener) { + if(event === 'end' && this._streamEnded) { + listener(); //Directly call the end event when stream already ended + } else { + EventEmitter.prototype.on.call(this, event, listener); + } + + if (event === 'data') { + if (!this._isStreamOpened) { + this._isStreamOpened = true; + this._stream.on('data', this._buildChunk.bind(this)); + } + if (this._paused === undefined) { + this._stream.resume(); + } + } + + return this; +}; + +/** +* Stop stream from external +*/ +ChunkStreamWithStream.prototype.stop = function (chunk, encoding, cb) { + if (azureutil.objectIsFunction(this._stream.destroy)) { + this._stream.destroy(); + } else { + this.pause(); + } + ChunkStream.prototype.end.call(this, chunk, encoding, cb); +}; + +/** +* Pause chunk stream +*/ +ChunkStreamWithStream.prototype.pause = function () { + ChunkStream.prototype.pause.call(this); + + this._stream.pause(); +}; + +/** +* Resume read stream +*/ +ChunkStreamWithStream.prototype.resume = function() { + ChunkStream.prototype.resume.call(this); + + this._stream.resume(); +}; + +ChunkStreamWithStream.prototype.finish = function () { + ChunkStream.prototype.finish.call(this); + this._stream.emit.call(this._stream, 'finish'); +}; + +ChunkStreamWithStream.prototype.destroy = function () { + ChunkStream.prototype.destroy.call(this); + this._stream.emit.call(this._stream, 'close'); +}; + +module.exports = ChunkStreamWithStream; + +},{"./../util/util":39,"./chunkstream":28,"events":155,"util":"util"}],28:[function(require,module,exports){ +(function (Buffer){ +// +// Copyright (c) Microsoft and contributors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// +// See the License for the specific language governing permissions and +// limitations under the License. +// + +var stream = require('stream'); +var util = require('util'); + +var azureutil = require('../util/util'); +var Md5Wrapper = require('../md5-wrapper'); +var Constants = require('../util/constants'); +var bufferSize = Constants.BlobConstants.DEFAULT_WRITE_BLOCK_SIZE_IN_BYTES; + +/** +* Chunk stream +* 1. Calculate md5 +* 2. Track reading offset +* 3. Work with customize memory allocator +* 4. Buffer data from stream. +* @param {object} options stream.Readable options +*/ +function ChunkStream(options) { + stream.Stream.call(this); + this.writable = this.readable = true; + + if (!options) { + options = {}; + } + + this._highWaterMark = options.highWaterMark || bufferSize; + + this._paused = undefined; //True/false is the external status from users. + + this._isStreamOpened = false; + this._offset = 0; + this._allocator = options.allocator; + this._streamEnded = false; + this._md5hash = null; + this._buffer = null; + this._internalBufferSize = 0; + this._outputLengthLimit = 0; + this._md5sum = undefined; + + if (options.calcContentMd5) { + this._md5hash = new Md5Wrapper().createMd5Hash(); + } +} + +util.inherits(ChunkStream, stream.Stream); + +/** +* Set the memory allocator. +*/ +ChunkStream.prototype.setMemoryAllocator = function(allocator) { + this._allocator = allocator; +}; + +/** +* Set the output length. +*/ +ChunkStream.prototype.setOutputLength = function(length) { + if (length) { + this._outputLengthLimit = length; + } +}; + +/** +* Internal stream ended +*/ +ChunkStream.prototype.end = function (chunk, encoding, cb) { + if (typeof chunk === 'function') { + cb = chunk; + chunk = null; + encoding = null; + } else if (typeof encoding === 'function') { + cb = encoding; + encoding = null; + } + + if (chunk) { + this.write(chunk, encoding); + } + + this._streamEnded = true; + this._flushInternalBuffer(); + + if (cb) { + this.once('end', cb); + } + + this.emit('end'); +}; + +ChunkStream.prototype.finish = function () { + this.emit('finish'); + + this.destroy(); +}; + +ChunkStream.prototype.error = function () { + this.emit('error'); + + this.destroy(); +}; + +ChunkStream.prototype.destroy = function () { + this.writable = this.readable = false; + + if (this._allocator && azureutil.objectIsFunction(this._allocator.destroy)) { + this._allocator.destroy(); + } + + this.emit('close'); +}; + +ChunkStream.prototype.stop = function () { + this.destroy(); + this._streamEnded = true; + this.emit('end'); +}; + +/** +* Add event listener +*/ +ChunkStream.prototype.write = function (chunk, encoding) { + if (!this._isStreamOpened) { + this._isStreamOpened = true; + } + + this._buildChunk(chunk, encoding); + + return !this._paused; +}; + +/** +* Buffer the data into a chunk and emit it +*/ +ChunkStream.prototype._buildChunk = function (data) { + if (typeof data == 'string') { + data = new Buffer(data); + } + var dataSize = data.length; + var dataOffset = 0; + do { + var buffer = null; + var targetSize = this._internalBufferSize + dataSize; + + if (targetSize < this._highWaterMark) { + // add the data to the internal buffer and return as it is not yet full + this._copyToInternalBuffer(data, dataOffset, data.length); + return; + } else if (targetSize == this._highWaterMark){ + var canReleaseInnerStreamBuffer = this._stream && this._stream._allocator && this._stream._allocator.releaseBuffer; + if(this._internalBufferSize === 0 && data.length === this._highWaterMark && !canReleaseInnerStreamBuffer) { + // set the buffer to the data passed in to avoid creating a new buffer + buffer = data; + } else { + // add the data to the internal buffer and pop that buffer + this._copyToInternalBuffer(data, dataOffset, data.length); + buffer = this._popInternalBuffer(); + } + dataSize = 0; + } else { + // add data to the internal buffer until its full, then return it + // set the dataSize parameter so that additional data is not lost + var copySize = this._highWaterMark - this._internalBufferSize; + this._copyToInternalBuffer(data, dataOffset, dataOffset + copySize); + dataSize -= copySize; + dataOffset += copySize; + buffer = this._popInternalBuffer(); + } + this._emitBufferData(buffer); + } while(dataSize > 0); +}; + +/** +* Emit the buffer +*/ +ChunkStream.prototype._emitBufferData = function(buffer) { + var newOffset = this._offset + buffer.length; + var range = { + start : this._offset, + end : newOffset - 1, + size : buffer.length + }; + + this._offset = newOffset; + + if (this._outputLengthLimit > 0) { + // When the start postion is larger than the limit, no data will be consumed though there is an event to be emitted. + // So the buffer should not be calculated. + if (range.start <= this._outputLengthLimit) { + if (this._offset > this._outputLengthLimit) { + // Don't use negative end parameter which means the index starting from the end of the buffer + // to be compatible with node 0.8. + buffer = buffer.slice(0, buffer.length - (this._offset - this._outputLengthLimit)); + } + if (this._md5hash) { + this._md5hash.update(buffer); + } + } + } else if (this._md5hash) { + this._md5hash.update(buffer); + } + + this.emit('data', buffer, range); +}; + +/** +* Copy data into internal buffer +*/ +ChunkStream.prototype._copyToInternalBuffer = function(data, start, end) { + if(start === undefined) start = 0; + if(end === undefined) end = data.length; + if (!this._buffer) { + this._buffer = this._allocateNewBuffer(); + this._internalBufferSize = 0; + } + var copied = data.copy(this._buffer, this._internalBufferSize, start, end); + this._internalBufferSize += copied; + + if (this._stream && this._stream._allocator && this._stream._allocator.releaseBuffer) { + this._stream._allocator.releaseBuffer(data); + } + + if(copied != (end - start)) { + throw new Error('Can not copy entire data to buffer'); + } +}; + +/** +* Flush internal buffer +*/ +ChunkStream.prototype._flushInternalBuffer = function() { + var buffer = this._popInternalBuffer(); + if (buffer) { + this._emitBufferData(buffer); + } +}; + +/** +* Pop internal buffer +*/ +ChunkStream.prototype._popInternalBuffer = function () { + var buf = null; + if (!this._buffer || this._internalBufferSize === 0) { + buf = null; + } else if(this._internalBufferSize == this._highWaterMark) { + buf = this._buffer; + } else { + buf = this._buffer.slice(0, this._internalBufferSize); + } + + this._buffer = null; + this._internalBufferSize = 0; + + return buf; +}; + +/** +* Allocate a buffer +*/ +ChunkStream.prototype._allocateNewBuffer = function() { + var size = this._highWaterMark; + if(this._allocator && azureutil.objectIsFunction(this._allocator.getBuffer)) { + return this._allocator.getBuffer(size); + } else { + var buffer = new Buffer(size); + return buffer; + } +}; + +/** +* Get file content md5 when read completely. +*/ +ChunkStream.prototype.getContentMd5 = function(encoding) { + if (!encoding) encoding = 'base64'; + if(!this._md5hash) { + throw new Error('Can\'t get content md5, please set the calcContentMd5 option for FileReadStream.'); + } else { + if (this._streamEnded) { + if (!this._md5sum) { + this._md5sum = this._md5hash.digest(encoding); + } + return this._md5sum; + } else { + throw new Error('Stream has not ended.'); + } + } +}; + +/** +* Pause chunk stream +*/ +ChunkStream.prototype.pause = function() { + this._paused = true; +}; + +/** +* Resume read stream +*/ +ChunkStream.prototype.resume = function() { + if (this._paused) { + this._paused = false; + + this.emit('drain'); + } +}; + +module.exports = ChunkStream; +}).call(this,require("buffer").Buffer) +},{"../md5-wrapper":13,"../util/constants":33,"../util/util":39,"buffer":"buffer","stream":"stream","util":"util"}],27:[function(require,module,exports){ +(function (Buffer){ +// +// Copyright (c) Microsoft and contributors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// +// See the License for the specific language governing permissions and +// limitations under the License. +// +/** +* Chunked memory pool allocator. +* It could dramatically reduce the memory usage. +* However, it can't dramatically reduce the CPU time since GC in v8 is very efficient. +*/ +function ChunkAllocator(chunkSize, maxCount) { + // Track the unused buffers and number of used buffers + this._pool = []; + this._inuse = 0; + + // Buffer size + this._chunkSize = chunkSize; + + // If total memory is larger than this._chunkSize * this._maxCount, the buffer pool is not used. + this._maxCount = maxCount || 10; + + // Immediately add a buffer to the pool. + this._extendMemoryPool(); +} + +/** +* Synchronously require a buffer +* Caller should be aware of that the content of buffer is random since the Buffer.fill is Time-consumed opreation. +*/ +ChunkAllocator.prototype.getBuffer = function(size) { + var buffer = this._getBufferFromPool(size); + if (buffer === null) { + // Either the total memory is larger than this._chunkSize * this._maxCount + // Or, the size does not match the chunk size of the pool + buffer = new Buffer(size); + } + + this._inuse++; + return buffer; +}; + +/** +* Get buffer from the current memory pool. +*/ +ChunkAllocator.prototype._getBufferFromPool = function(size) { + // Return null if the given size does not match the chunk size of the buffer pool. + if(size !== this._chunkSize) { + return null; + } + + // Extend the memory pool if it is empty. + if(this._pool.length === 0) { + this._extendMemoryPool(); + } + + // If the pool is not empty, return a buffer. + if(this._pool.length !== 0) { + return this._pool.pop(); + } else { + return null; + } +}; + +/** +* Extend the memory pool if the maximum size has not been reached. +*/ +ChunkAllocator.prototype._extendMemoryPool = function() { + var total = this._pool.length + this._inuse; + + // If the total is larger than the max, do not allocate more memory. + if(total >= this._maxCount) return; + + // Calculate the new number of buffers, equal to the total*2 bounded by 1 and the maxCount + var nextSize = Math.min(total * 2, this._maxCount) || 1; + + // Add more buffers. + var increment = nextSize - total; + for(var i = 0; i < increment; i++) { + var buffer = new Buffer(this._chunkSize); + this._pool.push(buffer); + } +}; + +/** +* Release the buffer. +*/ +ChunkAllocator.prototype.releaseBuffer = function(buffer) { + if(buffer.length !== this._chunkSize) { + // Directly delete the buffer if bufferSize is invalid and wait for GC. + buffer = null; + return; + } + + // Add the buffer to the pool if it is not full, otherwise delete it + if (this._pool.length < this._maxCount) { + this._pool.push(buffer); + } else { + buffer = null; + } + + // Decrement _inuse + this._inuse--; + + // _inuse could be below zero if a buffer is released which was not returned by getBuffer + if(this._inuse < 0) { + this._inuse = 0; + } +}; + +/** +* Destroy ChunkAllocator. +*/ +ChunkAllocator.prototype.destroy = function() { + this._pool = []; + this._inuse = 0; +}; + +module.exports = ChunkAllocator; + +}).call(this,require("buffer").Buffer) +},{"buffer":"buffer"}],24:[function(require,module,exports){ +(function (process){ +// +// Copyright (c) Microsoft and contributors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// +// See the License for the specific language governing permissions and +// limitations under the License. +// + +var util = require('util'); +var http = require('http'); +var https = require('https'); +var EventEmitter = require('events').EventEmitter; +var os = require('os'); + +var azureutil = require('../util/util'); +var Logger = require('../diagnostics/logger'); +var Constants = require('../util/constants'); +var errors = require('../errors/errors'); +var ArgumentError = errors.ArgumentError; + +var DEFAULT_OPERATION_MEMORY_USAGE = Constants.BlobConstants.DEFAULT_WRITE_BLOCK_SIZE_IN_BYTES; +var DEFAULT_CRITICAL_MEMORY_LIMITATION_32_IN_BYTES = Constants.BlobConstants.DEFAULT_CRITICAL_MEMORY_LIMITATION_32_IN_BYTES; +var DEFAULT_CRITICAL_MEMORY_LIMITATION_BROWSER_IN_BYTES = Constants.BlobConstants.DEFAULT_CRITICAL_MEMORY_LIMITATION_BROWSER_IN_BYTES; +var DEFAULT_MINIMUM_MEMORY_USAGE_BROWSER_IN_BYTES = Constants.BlobConstants.DEFAULT_MINIMUM_MEMORY_USAGE_BROWSER_IN_BYTES; +var DEFAULT_GLOBAL_CONCURRENCY = 5; //Default http connection limitation for nodejs + +var SystemTotalMemory = os.totalmem(); +var CriticalFreeMemory = 0.1 * SystemTotalMemory; +var nodeVersion = azureutil.getNodeVersion(); + +/** +* Concurrently execute batch operations and call operation callback randomly or in sequence. +* Random mode is for uploading. +* 1. Fire user callback when the operation is done. +* Sequence mode is for downloading. +* 1. Fire user callback when the operation is done and all previous operations and callback has finished. +* 2. BatchOperation guarantees the user callback is fired one by one. +* 3. The next user callback can't be fired until the current one is completed. +*/ +function BatchOperation(name, options) { + if (!options) { + options = {}; + } + + this.name = name; + this.logger = options.logger || new Logger(Logger.LogLevels.INFO); + this.operationMemoryUsage = options.operationMemoryUsage || DEFAULT_OPERATION_MEMORY_USAGE; + this.callbackInOrder = options.callbackInOrder === true; + this.callInOrder = options.callInOrder === true; + this._currentOperationId = this.callbackInOrder ? 1 : -1; + this.concurrency = DEFAULT_GLOBAL_CONCURRENCY; + this.enableReuseSocket = (nodeVersion.major > 0 || nodeVersion.minor >= 10) && options.enableReuseSocket; + + this._emitter = new EventEmitter(); + this._enableComplete = false; + this._ended = false; + this._error = null; + this._paused = false; + + //Total operations count(queued and active and connected) + this._totalOperation = 0; + + //Action operations count(The operations which are connecting to remote or executing callback or queued for executing) + this._activeOperation = 0; + + //Queued operations count(The operations which are connecting to remote or queued for executing) + this._queuedOperation = 0; + + //finished operation should be removed from this array + this._operations = []; +} + +/** +* Operation state +*/ +var OperationState = { + INITED : 'inited', + QUEUED : 'queued', + RUNNING : 'running', + COMPLETE : 'complete', + CALLBACK : 'callback', + ERROR : 'error' +}; + +BatchOperation.OperationState = OperationState; + +/** +* Set batch operation concurrency +*/ +BatchOperation.prototype.setConcurrency = function(concurrency) { + if (concurrency) { + this.concurrency = concurrency; + http.Agent.maxSockets = this.concurrency; + https.Agent.maxSockets = this.concurrency; + } +}; + +/** +* Is the workload heavy and It can be used to determine whether we could queue operations +*/ +BatchOperation.prototype.IsWorkloadHeavy = function() { + //Only support one batch operation for now. + //In order to work with the multiple batch operation, we can use global operation track objects + //BatchOperation acquire a bunch of operation ids from global and allocated ids to RestOperation + //RestOperation start to run in order of id + var sharedRequest = 1; + if(this.enableReuseSocket && !this.callInOrder) { + sharedRequest = 2; + } + return this._activeOperation >= sharedRequest * this.concurrency || this._isLowMemory(); +}; + +/** +* Get the approximate memory usage for batch operation. +*/ +BatchOperation.prototype._getApproximateMemoryUsage = function() { + var currentUsage = azureutil.isBrowser() ? DEFAULT_MINIMUM_MEMORY_USAGE_BROWSER_IN_BYTES : process.memoryUsage().rss; // Currently, we cannot get memory usage in browsers + var futureUsage = this._queuedOperation * this.operationMemoryUsage; + return currentUsage + futureUsage; +}; + +/** +* Return whether in a low memory situation. +*/ +BatchOperation.prototype._isLowMemory = function() { + var approximateMemoryUsage = this._getApproximateMemoryUsage(); + return os.freemem() < CriticalFreeMemory || + (this._activeOperation >= this.concurrency && approximateMemoryUsage > 0.5 * SystemTotalMemory) || + (azureutil.is32() && approximateMemoryUsage > DEFAULT_CRITICAL_MEMORY_LIMITATION_32_IN_BYTES) || + (azureutil.isBrowser() && approximateMemoryUsage > DEFAULT_CRITICAL_MEMORY_LIMITATION_BROWSER_IN_BYTES); +}; + +/** +* Add a operation into batch operation +*/ +BatchOperation.prototype.addOperation = function(operation) { + this._operations.push(operation); + operation.status = OperationState.QUEUED; + operation.operationId = ++this._totalOperation; + this._queuedOperation++; + this.logger.debug(util.format('Add operation %d into batch operation %s. Active: %s; Queued: %s', operation.operationId, this.name, this._activeOperation, this._queuedOperation)); + //Immediately start the idle operation if workload isn't heavy + this._runOperation(operation); + return this.IsWorkloadHeavy(); +}; + +/** +* Enable batch operation complete when there is no operation to run. +*/ +BatchOperation.prototype.enableComplete = function() { + this._enableComplete = true; + this.logger.debug(util.format('Enable batch operation %s complete', this.name)); + this._tryEmitEndEvent(); +}; + +/** +* Stop firing user call back +*/ +BatchOperation.prototype.pause = function () { + this._paused = true; +}; + +/** +* Start firing user call back +*/ +BatchOperation.prototype.resume = function () { + if (this._paused) { + this._paused = false; + this._fireOperationUserCallback(); + } +}; + +/** +* Add event listener +*/ +BatchOperation.prototype.on = function(event, listener) { + // only emit end if the batch has completed all operations + if(this._ended && event === 'end') { + listener(); + } else { + this._emitter.on(event, listener); + } +}; + +/** +* Run operation +*/ +BatchOperation.prototype._runOperation = function (operation) { + this.logger.debug(util.format('Operation %d start to run', operation.operationId)); + var cb = this.getBatchOperationCallback(operation); + + if(this._error) { + cb(this._error);//Directly call the callback with previous error. + } else { + operation.run(cb); + } + + this._activeOperation++; +}; + +/** +* Return an general operation call back. +* This callback is used to update the internal status and fire user's callback when operation is complete. +*/ +BatchOperation.prototype.getBatchOperationCallback = function (operation) { + var self = this; + return function (error) { + self._queuedOperation--; + if (error) { + operation.status = OperationState.ERROR; + self.logger.debug(util.format('Operation %d failed. Error %s', operation.operationId, error)); + self._error = error; + } else { + operation.status = OperationState.CALLBACK; + self.logger.debug(util.format('Operation %d succeed', operation.operationId)); + } + + operation._callbackArguments = arguments; + if (self._paused) { + operation.status = OperationState.CALLBACK; + self.logger.debug(util.format('Batch operation paused and Operation %d wait for firing callback', operation.operationId)); + } else if (self.callbackInOrder) { + operation.status = OperationState.CALLBACK; + if (self._currentOperationId === operation.operationId) { + self._fireOperationUserCallback(operation); + } else if (self._currentOperationId > operation.operationId) { + throw new Error('Debug error: current callback operation id cannot be larger than operation id'); + } else { + self.logger.debug(util.format('Operation %d is waiting for firing callback %s', operation.operationId, self._currentOperationId)); + } + } else { + self._fireOperationUserCallback(operation); + } + + self._tryEmitDrainEvent(); + operation = null; + self = null; + }; +}; + +/** +* Fire user's call back +*/ +BatchOperation.prototype._fireOperationUserCallback = function (operation) { + var index = this._getCallbackOperationIndex(); + if (!operation && index != -1) { + operation = this._operations[index]; + } + + if (operation && !this._paused) { + // fire the callback, if exists + if (operation._userCallback) { + this.logger.debug(util.format('Fire user call back for operation %d', operation.operationId)); + // make sure UserCallback is a sync operation in sequence mode. + // both async and sync operations are available for random mode. + operation._fireUserCallback(); + } + + // remove the operation from the array and decrement the counter + this._operations.splice(index, 1); + this._activeOperation--; + operation.status = OperationState.COMPLETE; + index = operation = null; + + if (this.callbackInOrder) { + this._currentOperationId++; + } + + this._fireOperationUserCallback(); + } else if (this._paused) { + this._tryEmitDrainEvent(); + } else { + // check if batch has ended and if so emit end event + this._tryEmitEndEvent(); + } +}; + +/** +* Try to emit the BatchOperation end event +* End event means all the operation and callback already finished. +*/ +BatchOperation.prototype._tryEmitEndEvent = function () { + if(this._enableComplete && this._activeOperation === 0 && this._operations.length === 0) { + this._ended = true; + this.logger.debug(util.format('Batch operation %s emits the end event', this.name)); + this._emitter.emit('end', this._error, null); + return true; + } + + // Workaround to recover from the 'hang' edge case. _tryEmitEndEvent function is not supposed to be called if the bacth is not really completed. + this._tryEmitDrainEvent(); + return false; +}; + +/** +* Try to emit the drain event +*/ +BatchOperation.prototype._tryEmitDrainEvent = function () { + if (!this._emitter) return false; + if(!this.IsWorkloadHeavy() || this._activeOperation < this.concurrency) { + this._emitter.emit('drain'); + return true; + } + return false; +}; + +/** +* Get the current active operation index. +* Only the active operation could call user's callback in sequence model. +* The other finished but not active operations should wait for wake up. +*/ +BatchOperation.prototype._getCallbackOperationIndex = function () { + var operation = null; + for (var i = 0; i < this._operations.length; i++) { + operation = this._operations[i]; + if (this.callbackInOrder) { + //Sequence mode + if (operation.operationId == this._currentOperationId) { + if (operation.status === OperationState.CALLBACK) { + return i; + } else { + return -1; + } + } + } else { + //Random mode + if (operation.status === OperationState.CALLBACK) { + return i; + } + } + } + return -1; +}; + +/** +* Do nothing and directly call the callback. +* In random mode, the user callback will be called immediately +* In sequence mode, the user callback will be called after the previous callback has been called +*/ +BatchOperation.noOperation = function (cb) { + cb(); +}; + +/** +* Rest operation in sdk +*/ +function RestOperation(serviceClient, operation) { + this.status = OperationState.Inited; + this.operationId = -1; + this._callbackArguments = null; + + // setup callback and args + this._userCallback = arguments[arguments.length - 1]; + var sliceEnd = arguments.length; + if(azureutil.objectIsFunction(this._userCallback)) { + sliceEnd--; + } else { + this._userCallback = null; + } + var operationArguments = Array.prototype.slice.call(arguments).slice(2, sliceEnd); + + this.run = function(cb) { + var func = serviceClient[operation]; + if(!func) { + throw new ArgumentError('operation', util.format('Unknown operation %s in serviceclient', operation)); + } else { + if(!cb) cb = this._userCallback; + operationArguments.push(cb); + this.status = OperationState.RUNNING; + func.apply(serviceClient, operationArguments); + operationArguments = operation = null; + } + }; + + this._fireUserCallback = function () { + if(this._userCallback) { + this._userCallback.apply(null, this._callbackArguments); + } + }; +} + +BatchOperation.RestOperation = RestOperation; + +/** +* Common operation wrapper +*/ +function CommonOperation(operationFunc, callback) { + this.status = OperationState.Inited; + this.operationId = -1; + this._callbackArguments = null; + var sliceStart = 2; + if (azureutil.objectIsFunction(callback)) { + this._userCallback = callback; + } else { + this._userCallback = null; + sliceStart = 1; + } + var operationArguments = Array.prototype.slice.call(arguments).slice(sliceStart); + this.run = function (cb) { + if (!cb) cb = this._userCallback; + operationArguments.push(cb); + this.status = OperationState.RUNNING; + operationFunc.apply(null, operationArguments); + operationArguments = operationFunc = null; + }; + + this._fireUserCallback = function () { + if (this._userCallback) { + this._userCallback.apply(null, this._callbackArguments); + } + this._userCallback = this._callbackArguments = null; + }; +} + +BatchOperation.CommonOperation = CommonOperation; + +module.exports = BatchOperation; + +}).call(this,require('_process')) +},{"../diagnostics/logger":7,"../errors/errors":8,"../util/constants":33,"../util/util":39,"_process":197,"events":155,"http":231,"https":172,"os":184,"util":"util"}],19:[function(require,module,exports){ +(function (process,Buffer){ +// +// Copyright (c) Microsoft and contributors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// +// See the License for the specific language governing permissions and +// limitations under the License. +// + +// Module dependencies. +var request = require('../request-wrapper'); +var url = require('url'); +var qs = require('querystring'); +var util = require('util'); +var xml2js = require('xml2js'); +var events = require('events'); +var _ = require('underscore'); +var guid = require('uuid'); +var os = require('os'); +var extend = require('extend'); +var Parser = require('json-edm-parser'); + +var Md5Wrapper = require('../md5-wrapper'); +var azureutil = require('../util/util'); +var validate = require('../util/validate'); +var SR = require('../util/sr'); +var WebResource = require('../http/webresource'); +var BufferStream = require('../streams/bufferstream.js'); + +var ServiceSettings = require('./servicesettings'); +var StorageServiceSettings = require('./storageservicesettings'); +var Constants = require('../util/constants'); +var StorageUtilities = require('../util/storageutilities'); +var ServicePropertiesResult = require('../models/servicepropertiesresult'); +var TableUtilities = require('../../services/table/tableutilities'); + +var SharedKey = require('../signing/sharedkey'); +var SharedAccessSignature = require('../signing/sharedaccesssignature'); + +var HeaderConstants = Constants.HeaderConstants; +var QueryStringConstants = Constants.QueryStringConstants; +var HttpResponseCodes = Constants.HttpConstants.HttpResponseCodes; +var StorageServiceClientConstants = Constants.StorageServiceClientConstants; +var defaultRequestLocationMode = Constants.RequestLocationMode.PRIMARY_ONLY; +var RequestLocationMode = Constants.RequestLocationMode; + +var Logger = require('../diagnostics/logger'); +var errors = require('../errors/errors'); +var ArgumentError = errors.ArgumentError; +var ArgumentNullError = errors.ArgumentNullError; +var TimeoutError = errors.TimeoutError; +var StorageError = errors.StorageError; + +/** +* Creates a new StorageServiceClient object. +* +* @class +* The StorageServiceClient class is the base class of all the service classes. +* @constructor +* @param {string} storageAccount The storage account. +* @param {string} storageAccessKey The storage access key. +* @param {object} host The host for the service. +* @param {bool} usePathStyleUri Boolean value indicating wether to use path style uris. +* @param {string} sasToken The Shared Access Signature token. +*/ +function StorageServiceClient(storageAccount, storageAccessKey, host, usePathStyleUri, sasToken) { + StorageServiceClient['super_'].call(this); + + if(storageAccount && storageAccessKey) { + // account and key + this.storageAccount = storageAccount; + this.storageAccessKey = storageAccessKey; + this.storageCredentials = new SharedKey(this.storageAccount, this.storageAccessKey, usePathStyleUri); + } else if (sasToken) { + // sas + this.sasToken = sasToken; + this.storageCredentials = new SharedAccessSignature(sasToken); + } else { + // anonymous + this.anonymous = true; + this.storageCredentials = { + signRequest: function(webResource, callback){ + // no op, anonymous access + callback(null); + } + }; + } + + if(host){ + this.setHost(host); + } + + this.apiVersion = HeaderConstants.TARGET_STORAGE_VERSION; + this.usePathStyleUri = usePathStyleUri; + + this._initDefaultFilter(); + + /** + * The logger of the service. To change the log level of the services, set the `[logger.level]{@link Logger#level}`. + * @name StorageServiceClient#logger + * @type Logger + * */ + this.logger = new Logger(Logger.LogLevels.INFO); + + this._setDefaultProxy(); + + this.xml2jsSettings = StorageServiceClient._getDefaultXml2jsSettings(); + this.defaultLocationMode = StorageUtilities.LocationMode.PRIMARY_ONLY; +} + +util.inherits(StorageServiceClient, events.EventEmitter); + +/** +* Gets the default xml2js settings. +* @ignore +* @return {object} The default settings +*/ +StorageServiceClient._getDefaultXml2jsSettings = function() { + var xml2jsSettings = _.clone(xml2js.defaults['0.2']); + + // these determine what happens if the xml contains attributes + xml2jsSettings.attrkey = Constants.TableConstants.XML_METADATA_MARKER; + xml2jsSettings.charkey = Constants.TableConstants.XML_VALUE_MARKER; + + // from xml2js guide: always put child nodes in an array if true; otherwise an array is created only if there is more than one. + xml2jsSettings.explicitArray = false; + + return xml2jsSettings; +}; + +/** +* Sets a host for the service. +* @ignore +* @param {string} host The host for the service. +*/ +StorageServiceClient.prototype.setHost = function (host) { + var parseHost = function(hostUri){ + var parsedHost; + if(!azureutil.objectIsNull(hostUri)) { + if(hostUri.indexOf('http') === -1 && hostUri.indexOf('//') !== 0){ + hostUri = '//' + hostUri; + } + parsedHost = url.parse(hostUri, false, true); + + if(!parsedHost.protocol){ + parsedHost.protocol = ServiceSettings.DEFAULT_PROTOCOL; + } + + if (!parsedHost.port) { + if (parsedHost.protocol === Constants.HTTPS) { + parsedHost.port = Constants.DEFAULT_HTTPS_PORT; + } else { + parsedHost.port = Constants.DEFAULT_HTTP_PORT; + } + } + + parsedHost = url.format({ + protocol: parsedHost.protocol, + port: parsedHost.port, + hostname: parsedHost.hostname, + pathname: parsedHost.pathname + }); + } + + return parsedHost; + }; + + validate.isValidHost(host); + + this.host = { + primaryHost: parseHost(host.primaryHost), + secondaryHost: parseHost(host.secondaryHost) + }; +}; + +/** +* Performs a REST service request through HTTP expecting an input stream. +* @ignore +* +* @param {WebResource} webResource The webresource on which to perform the request. +* @param {string} outputData The outgoing request data as a raw string. +* @param {object} [options] The request options. +* @param {int} [options.timeoutIntervalInMs] The timeout interval, in milliseconds, to use for the request. +* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request. +* @param {function} callback The response callback function. +*/ +StorageServiceClient.prototype.performRequest = function (webResource, outputData, options, callback) { + this._performRequest(webResource, { outputData: outputData }, options, callback); +}; + +/** +* Performs a REST service request through HTTP expecting an input stream. +* @ignore +* +* @param {WebResource} webResource The webresource on which to perform the request. +* @param {Stream} outputStream The outgoing request data as a stream. +* @param {object} [options] The request options. +* @param {int} [options.timeoutIntervalInMs] The timeout interval, in milliseconds, to use for the request. +* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request. +* @param {function} callback The response callback function. +*/ +StorageServiceClient.prototype.performRequestOutputStream = function (webResource, outputStream, options, callback) { + this._performRequest(webResource, { outputStream: outputStream }, options, callback); +}; + +/** +* Performs a REST service request through HTTP expecting an input stream. +* @ignore +* +* @param {WebResource} webResource The webresource on which to perform the request. +* @param {string} outputData The outgoing request data as a raw string. +* @param {Stream} inputStream The ingoing response data as a stream. +* @param {object} [options] The request options. +* @param {int} [options.timeoutIntervalInMs] The timeout interval, in milliseconds, to use for the request. +* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request. +* @param {function} callback The response callback function. +*/ +StorageServiceClient.prototype.performRequestInputStream = function (webResource, outputData, inputStream, options, callback) { + this._performRequest(webResource, { outputData: outputData, inputStream: inputStream }, options, callback); +}; + +/** +* Performs a REST service request through HTTP. +* @ignore +* +* @param {WebResource} webResource The webresource on which to perform the request. +* @param {object} body The request body. +* @param {string} [body.outputData] The outgoing request data as a raw string. +* @param {Stream} [body.outputStream] The outgoing request data as a stream. +* @param {Stream} [body.inputStream] The ingoing response data as a stream. +* @param {object} [options] The request options. +* @param {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. +* @param {int} [options.timeoutIntervalInMs] The timeout interval, in milliseconds, to use for the request. +* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request. +* @param {function} callback The response callback function. +*/ +StorageServiceClient.prototype._performRequest = function (webResource, body, options, callback) { + var self = this; + + // Sets a requestId on the webResource + if(!options.clientRequestId) { + options.clientRequestId = guid.v1(); + } + + webResource.withHeader(HeaderConstants.CLIENT_REQUEST_ID, options.clientRequestId); + + // Sets the user-agent string + var userAgentComment = util.format('(NODE-VERSION %s; %s %s)', process.version, os.type(), os.release()); + webResource.withHeader(HeaderConstants.USER_AGENT, Constants.USER_AGENT_PRODUCT_NAME + '/' + Constants.USER_AGENT_PRODUCT_VERSION + ' ' + userAgentComment); + + // Initialize the location that the request is going to be sent to. + if(azureutil.objectIsNull(options.locationMode)) { + options.locationMode = this.defaultLocationMode; + } + + // Initialize the location that the request can be sent to. + if(azureutil.objectIsNull(options.requestLocationMode)) { + options.requestLocationMode = defaultRequestLocationMode; + } + + // Initialize whether nagling is used or not. + if(azureutil.objectIsNull(options.useNagleAlgorithm)) { + options.useNagleAlgorithm = this.useNagleAlgorithm; + } + + this._initializeLocation(options); + + // Initialize the operationExpiryTime + this._setOperationExpiryTime(options); + + // If the output stream already got sent to server and got error back, + // we should NOT retry within the SDK as the stream data is not valid anymore if we retry directly. + // And it's very hard for SDK to re-wind the stream. + // + // If users want to retry on this kind of error, they can implement their own logic to parse the response and + // determine if they need to re-prepare a stream and call our SDK API to retry. + // + // Currently for blobs/files with size greater than 32MB (DEFAULT_SINGLE_BLOB_PUT_THRESHOLD_IN_BYTES), + // we'll send the steam by chunk buffers which doesn't have this issue. + var outputStreamSent = false; + + var operation = function (options, next) { + self._validateLocation(options); + var currentLocation = options.currentLocation; + self._buildRequestOptions(webResource, body, options, function (err, finalRequestOptions) { + if (err) { + callback({ error: err, response: null }, function (finalRequestOptions, finalCallback) { + finalCallback(finalRequestOptions); + }); + } else { + self.logger.log(Logger.LogLevels.DEBUG, 'FINAL REQUEST OPTIONS:\n' + util.inspect(finalRequestOptions)); + + if(self._maximumExecutionTimeExceeded(Date.now(), options.operationExpiryTime)) { + callback({ error: new TimeoutError(SR.MAXIMUM_EXECUTION_TIMEOUT_EXCEPTION), response: null }, function (finalRequestOptions, finalCallback) { + finalCallback(finalRequestOptions); + }); + } else { + var processResponseCallback = function (error, response) { + var responseObject; + + if (error) { + responseObject = { error: error, response: null }; + } else { + responseObject = self._processResponse(webResource, response, options); + responseObject.contentMD5 = response.contentMD5; + responseObject.length = response.length; + } + + responseObject.operationEndTime = new Date(); + // Required for listing operations to make sure successive operations go to the same location. + responseObject.targetLocation = currentLocation; + responseObject.outputStreamSent = outputStreamSent; + + callback(responseObject, next); + }; + + var endResponse; + var buildRequest = function (headersOnly, inputStream) { + // Build request (if body was set before, request will process immediately, if not it'll wait for the piping to happen + var requestStream; + + var requestWithDefaults; + + if(self.proxy) { + if(requestWithDefaults === undefined) { + requestWithDefaults = request.defaults({'proxy':self.proxy}); + } + } else { + requestWithDefaults = request; + } + + if (headersOnly) { + requestStream = requestWithDefaults(finalRequestOptions); + + requestStream.on('error', processResponseCallback); + requestStream.on('response', function (response) { + var isValid = WebResource.validResponse(response.statusCode); + if (!isValid) { + // When getting invalid response, try to get the error message for future steps to extract the detailed error information + var contentLength = parseInt(response.headers['content-length']); + var errorMessageBuffer; + var index = 0; + if (contentLength !== undefined) { + errorMessageBuffer = new Buffer(contentLength); + } + + requestStream.on('data', function (data) { + if (contentLength !== undefined) { + data.copy(errorMessageBuffer, index); + index += data.length; + } else { + if (!errorMessageBuffer) { + errorMessageBuffer = data; + } else { + errorMessageBuffer = Buffer.concat([errorMessageBuffer, data]); + } + } + }); + requestStream.on('end', function () { + if (errorMessageBuffer) { + // Strip the UTF8 BOM following the same ways as 'request' module + if (errorMessageBuffer.length > 3 && + errorMessageBuffer[0] === 239 && + errorMessageBuffer[1] === 187 && + errorMessageBuffer[2] === 191) { + response.body = errorMessageBuffer.toString('utf8', 3); + } else { + response.body = errorMessageBuffer.toString('utf8'); + } + } + processResponseCallback(null, response); + }); + } else { + // Only pipe to the destination stream when we get a valid response from service + // Error message should NOT be piped to the destination stream + if (inputStream) { + requestStream.pipe(inputStream); + } + + var responseLength = 0; + var internalHash = new Md5Wrapper().createMd5Hash(); + response.on('data', function(data) { + responseLength += data.length; + internalHash.update(data); + }); + + response.on('end', function () { + // Calculate and set MD5 here + if(azureutil.objectIsNull(options.disableContentMD5Validation) || options.disableContentMD5Validation === false) { + response.contentMD5 = internalHash.digest('base64'); + } + + response.length = responseLength; + endResponse = response; + }); + } + }); + } else { + requestStream = requestWithDefaults(finalRequestOptions, processResponseCallback); + } + + //If useNagleAlgorithm is not set or the value is set and is false, setNoDelay is set to true. + if (azureutil.objectIsNull(options.useNagleAlgorithm) || options.useNagleAlgorithm === false) { + requestStream.on('request', function(httpRequest) { + httpRequest.setNoDelay(true); + }); + } + + // Workaround to avoid request from potentially setting unwanted (rejected) headers by the service + var oldEnd = requestStream.end; + requestStream.end = function () { + if (finalRequestOptions.headers['content-length']) { + requestStream.headers['content-length'] = finalRequestOptions.headers['content-length']; + } else if (requestStream.headers['content-length']) { + delete requestStream.headers['content-length']; + } + + oldEnd.call(requestStream); + }; + + // Bubble events up -- This is when the request is going to be made. + requestStream.on('response', function (response) { + self.emit('receivedResponseEvent', response); + }); + + return requestStream; + }; + + if (body && body.outputData) { + if (!azureutil.isBrowser() && Buffer.isBuffer(body.outputData)) { + // Request module will take 200MB additional memory when we pass a 100MB buffer as body + // Transfer buffer to stream will highly reduce the memory used by request module + finalRequestOptions.body = new BufferStream(body.outputData); + } else { + finalRequestOptions.body = body.outputData; + } + } + + // Pipe any input / output streams + if (body && body.inputStream) { + body.inputStream.on('close', function () { + if (endResponse) { + processResponseCallback(null, endResponse); + endResponse = null; + } + }); + body.inputStream.on('end', function () { + if (endResponse) { + processResponseCallback(null, endResponse); + endResponse = null; + } + }); + body.inputStream.on('finish', function () { + if (endResponse) { + processResponseCallback(null, endResponse); + endResponse = null; + } + }); + buildRequest(true, body.inputStream); + } else if (body && body.outputStream) { + var sendUnchunked = function () { + var size = finalRequestOptions.headers['content-length'] ? + finalRequestOptions.headers['content-length'] : + Constants.BlobConstants.MAX_SINGLE_UPLOAD_BLOB_SIZE_IN_BYTES; + + var concatBuf = new Buffer(size); + var index = 0; + + body.outputStream.on('data', function (d) { + outputStreamSent = true; + if(self._maximumExecutionTimeExceeded(Date.now(), options.operationExpiryTime)) { + processResponseCallback(new TimeoutError(SR.MAXIMUM_EXECUTION_TIMEOUT_EXCEPTION)); + } else { + d.copy(concatBuf, index); + index += d.length; + } + }).on('end', function () { + var requestStream = buildRequest(); + requestStream.write(concatBuf); + requestStream.end(); + }); + if (azureutil.isStreamPaused(body.outputStream)) { + body.outputStream.resume(); + } + }; + + var sendStream = function () { + // NOTE: workaround for an unexpected EPIPE exception when piping streams larger than 29 MB + if (!azureutil.objectIsNull(finalRequestOptions.headers['content-length']) && finalRequestOptions.headers['content-length'] < 29 * 1024 * 1024) { + body.outputStream.pipe(buildRequest()); + outputStreamSent = true; + + if (azureutil.isStreamPaused(body.outputStream)) { + body.outputStream.resume(); + } + } else { + sendUnchunked(); + } + }; + + if (!body.outputStream.readable) { + // if the content length is zero, build the request and don't send a body + if (finalRequestOptions.headers['content-length'] === 0) { + buildRequest(); + } else { + // otherwise, wait until we know the readable stream is actually valid before piping + body.outputStream.on('open', function () { + sendStream(); + }); + } + } else { + sendStream(); + } + + // This catches any errors that happen while creating the readable stream (usually invalid names) + body.outputStream.on('error', function (error) { + processResponseCallback(error); + }); + } else { + buildRequest(); + } + } + } + }); + }; + + // The filter will do what it needs to the requestOptions and will provide a + // function to be handled after the reply + self.filter(options, function (postFiltersRequestOptions, nextPostCallback) { + if(self._maximumExecutionTimeExceeded(Date.now() + postFiltersRequestOptions.retryInterval, postFiltersRequestOptions.operationExpiryTime)) { + callback({ error: new TimeoutError(SR.MAXIMUM_EXECUTION_TIMEOUT_EXCEPTION), response: null}, function (postFiltersRequestOptions, finalCallback) { + finalCallback(postFiltersRequestOptions); + }); + } else { + // If there is a filter, flow is: + // filter -> operation -> process response + if(postFiltersRequestOptions.retryContext) { + var func = function() { + operation(postFiltersRequestOptions, nextPostCallback); + }; + + // Sleep for retryInterval before making the request + setTimeout(func, postFiltersRequestOptions.retryInterval); + } else { + // No retry policy filter specified + operation(postFiltersRequestOptions, nextPostCallback); + } + } + }); +}; + + +/** +* Builds the request options to be passed to the http.request method. +* @ignore +* @param {WebResource} webResource The webresource where to build the options from. +* @param {object} options The request options. +* @param {function(error, requestOptions)} callback The callback function. +*/ +StorageServiceClient.prototype._buildRequestOptions = function (webResource, body, options, callback) { + webResource.withHeader(HeaderConstants.STORAGE_VERSION, this.apiVersion); + webResource.withHeader(HeaderConstants.MS_DATE, new Date().toUTCString()); + if (!webResource.headers[HeaderConstants.ACCEPT]) { + webResource.withHeader(HeaderConstants.ACCEPT, 'application/atom+xml,application/xml'); + } + webResource.withHeader(HeaderConstants.ACCEPT_CHARSET, 'UTF-8'); + + // Browsers cache GET/HEAD requests by adding conditional headers such as 'IF_MODIFIED_SINCE' after Azure Storage 'Authorization header' calculation, + // which may result in a 403 authorization error. So add timestamp to GET/HEAD request URLs thus avoid the browser cache. + if (azureutil.isBrowser() && ( + webResource.method === Constants.HttpConstants.HttpVerbs.GET || + webResource.method === Constants.HttpConstants.HttpVerbs.HEAD)) { + webResource.withQueryOption(HeaderConstants.FORCE_NO_CACHE_IN_BROWSER, new Date().getTime()); + } + + if(azureutil.objectIsNull(options.timeoutIntervalInMs)) { + options.timeoutIntervalInMs = this.defaultTimeoutIntervalInMs; + } + + if(azureutil.objectIsNull(options.clientRequestTimeoutInMs)) { + options.clientRequestTimeoutInMs = this.defaultClientRequestTimeoutInMs; + } + + if(!azureutil.objectIsNull(options.timeoutIntervalInMs) && options.timeoutIntervalInMs > 0) { + webResource.withQueryOption(QueryStringConstants.TIMEOUT, Math.ceil(options.timeoutIntervalInMs / 1000)); + } + + if(options.accessConditions) { + webResource.withHeader(HeaderConstants.IF_MATCH, options.accessConditions.EtagMatch); + webResource.withHeader(HeaderConstants.IF_MODIFIED_SINCE, options.accessConditions.DateModifedSince); + webResource.withHeader(HeaderConstants.IF_NONE_MATCH, options.accessConditions.EtagNonMatch); + webResource.withHeader(HeaderConstants.IF_UNMODIFIED_SINCE, options.accessConditions.DateUnModifiedSince); + webResource.withHeader(HeaderConstants.SEQUENCE_NUMBER_EQUAL, options.accessConditions.SequenceNumberEqual); + webResource.withHeader(HeaderConstants.SEQUENCE_NUMBER_LESS_THAN, options.accessConditions.SequenceNumberLessThan); + webResource.withHeader(HeaderConstants.SEQUENCE_NUMBER_LESS_THAN_OR_EQUAL, options.accessConditions.SequenceNumberLessThanOrEqual); + webResource.withHeader(HeaderConstants.BLOB_CONDITION_MAX_SIZE, options.accessConditions.MaxBlobSize); + webResource.withHeader(HeaderConstants.BLOB_CONDITION_APPEND_POSITION, options.accessConditions.MaxAppendPosition); + } + + if(options.sourceAccessConditions) { + webResource.withHeader(HeaderConstants.SOURCE_IF_MATCH, options.sourceAccessConditions.EtagMatch); + webResource.withHeader(HeaderConstants.SOURCE_IF_MODIFIED_SINCE, options.sourceAccessConditions.DateModifedSince); + webResource.withHeader(HeaderConstants.SOURCE_IF_NONE_MATCH, options.sourceAccessConditions.EtagNonMatch); + webResource.withHeader(HeaderConstants.SOURCE_IF_UNMODIFIED_SINCE, options.sourceAccessConditions.DateUnModifiedSince); + } + + if (!webResource.headers || webResource.headers[HeaderConstants.CONTENT_TYPE] === undefined) { + // work around to add an empty content type header to prevent the request module from magically adding a content type. + webResource.headers[HeaderConstants.CONTENT_TYPE] = ''; + } else if (webResource.headers && webResource.headers[HeaderConstants.CONTENT_TYPE] === null) { + delete webResource.headers[HeaderConstants.CONTENT_TYPE]; + } + + if (!webResource.headers || webResource.headers[HeaderConstants.CONTENT_LENGTH] === undefined) { + if (body && body.outputData) { + webResource.withHeader(HeaderConstants.CONTENT_LENGTH, Buffer.byteLength(body.outputData, 'UTF8')); + } else if (webResource.headers[HeaderConstants.CONTENT_LENGTH] === undefined) { + webResource.withHeader(HeaderConstants.CONTENT_LENGTH, 0); + } + } else if (webResource.headers && webResource.headers[HeaderConstants.CONTENT_LENGTH] === null) { + delete webResource.headers[HeaderConstants.CONTENT_LENGTH]; + } + + // Sets the request url in the web resource. + this._setRequestUrl(webResource, options); + + this.emit('sendingRequestEvent', webResource); + + // Now that the web request is finalized, sign it + this.storageCredentials.signRequest(webResource, function (error) { + var requestOptions = null; + + if (!error) { + var targetUrl = webResource.uri; + + requestOptions = { + uri: url.format(targetUrl), + method: webResource.method, + headers: webResource.headers, + mode: 'disable-fetch' + }; + + if (options) { + //set encoding of response data. If set to null, the body is returned as a Buffer + requestOptions.encoding = options.responseEncoding; + } + + if (options && options.clientRequestTimeoutInMs) { + requestOptions.timeout = options.clientRequestTimeoutInMs; + } else { + requestOptions.timeout = Constants.DEFAULT_CLIENT_REQUEST_TIMEOUT_IN_MS; // 2 minutes + } + } + + callback(error, requestOptions); + }); +}; + +/** +* Process the response. +* @ignore +* +* @param {WebResource} webResource The web resource that made the request. +* @param {Response} response The response object. +* @param {Options} options The response parsing options. +* @param {String} options.payloadFormat The payload format. +* @return The normalized responseObject. +*/ +StorageServiceClient.prototype._processResponse = function (webResource, response, options) { + var self = this; + + function convertRawHeadersToHeaders(rawHeaders) { + var headers = {}; + if(!rawHeaders) { + return undefined; + } + + for(var i = 0; i < rawHeaders.length; i++) { + var headerName; + if (rawHeaders[i].indexOf(HeaderConstants.PREFIX_FOR_STORAGE_METADATA) === 0) { + headerName = rawHeaders[i]; + } else { + headerName = rawHeaders[i].toLowerCase(); + } + headers[headerName] = rawHeaders[++i]; + } + + return headers; + } + + var validResponse = WebResource.validResponse(response.statusCode); + var rsp = StorageServiceClient._buildResponse(validResponse, response.body, convertRawHeadersToHeaders(response.rawHeaders) || response.headers, response.statusCode, response.md5); + var responseObject; + + if (validResponse && webResource.rawResponse) { + responseObject = { error: null, response: rsp }; + } else { + // attempt to parse the response body, errors will be returned in rsp.error without modifying the body + rsp = StorageServiceClient._parseResponse(rsp, self.xml2jsSettings, options); + + if (validResponse && !rsp.error) { + responseObject = { error: null, response: rsp }; + } else { + rsp.isSuccessful = false; + + if (response.statusCode < 400 || response.statusCode >= 500) { + this.logger.log(Logger.LogLevels.DEBUG, + 'ERROR code = ' + response.statusCode + ' :\n' + util.inspect(rsp.body)); + } + + // responseObject.error should contain normalized parser errors if they occured in _parseResponse + // responseObject.response.body should contain the raw response body in that case + var errorBody = rsp.body; + if(rsp.error) { + errorBody = rsp.error; + delete rsp.error; + } + + if (!errorBody) { + var code = Object.keys(HttpResponseCodes).filter(function (name) { + if (HttpResponseCodes[name] === rsp.statusCode) { + return name; + } + }); + + errorBody = { error: { code: code[0] } }; + } + + var normalizedError = StorageServiceClient._normalizeError(errorBody, response); + responseObject = { error: normalizedError, response: rsp }; + } + } + + this.logger.log(Logger.LogLevels.DEBUG, 'RESPONSE:\n' + util.inspect(responseObject)); + + return responseObject; +}; + +/** +* Associate a filtering operation with this StorageServiceClient. Filtering operations +* can include logging, automatically retrying, etc. Filter operations are objects +* that implement a method with the signature: +* +* "function handle (requestOptions, next)". +* +* After doing its preprocessing on the request options, the method needs to call +* "next" passing a callback with the following signature: +* signature: +* +* "function (returnObject, finalCallback, next)" +* +* In this callback, and after processing the returnObject (the response from the +* request to the server), the callback needs to either invoke next if it exists to +* continue processing other filters or simply invoke finalCallback otherwise to end +* up the service invocation. +* +* @param {Object} filter The new filter object. +* @return {StorageServiceClient} A new service client with the filter applied. +*/ +StorageServiceClient.prototype.withFilter = function (newFilter) { + // Create a new object with the same members as the current service + var derived = _.clone(this); + + // If the current service has a filter, merge it with the new filter + // (allowing us to effectively pipeline a series of filters) + var parentFilter = this.filter; + var mergedFilter = newFilter; + if (parentFilter !== undefined) { + // The parentFilterNext is either the operation or the nextPipe function generated on a previous merge + // Ordering is [f3 pre] -> [f2 pre] -> [f1 pre] -> operation -> [f1 post] -> [f2 post] -> [f3 post] + mergedFilter = function (originalRequestOptions, parentFilterNext) { + newFilter.handle(originalRequestOptions, function (postRequestOptions, newFilterCallback) { + // handle parent filter pre and get Parent filter post + var next = function (postPostRequestOptions, parentFilterCallback) { + // The parentFilterNext is the filter next to the merged filter. + // For 2 filters, that'd be the actual operation. + parentFilterNext(postPostRequestOptions, function (responseObject, responseCallback, finalCallback) { + parentFilterCallback(responseObject, finalCallback, function (postResponseObject) { + newFilterCallback(postResponseObject, responseCallback, finalCallback); + }); + }); + }; + + parentFilter(postRequestOptions, next); + }); + }; + } + + // Store the filter so it can be applied in performRequest + derived.filter = mergedFilter; + return derived; + }; + +/* +* Builds a response object with normalized key names. +* @ignore +* +* @param {Bool} isSuccessful Boolean value indicating if the request was successful +* @param {Object} body The response body. +* @param {Object} headers The response headers. +* @param {int} statusCode The response status code. +* @param {string} md5 The response's content md5 hash. +* @return {Object} A response object. +*/ +StorageServiceClient._buildResponse = function (isSuccessful, body, headers, statusCode, md5) { + var response = { + isSuccessful: isSuccessful, + statusCode: statusCode, + body: body, + headers: headers, + md5: md5 + }; + + if (!azureutil.objectIsNull(headers)) { + if (headers[HeaderConstants.REQUEST_SERVER_ENCRYPTED] !== undefined) { + response.requestServerEncrypted = (headers[HeaderConstants.REQUEST_SERVER_ENCRYPTED] === 'true'); + } + } + + return response; +}; + +/** +* Parses a server response body from XML or JSON into a JS object. +* This is done using the xml2js library. +* @ignore +* +* @param {object} response The response object with a property "body" with a XML or JSON string content. +* @param {object} xml2jsSettings The XML to json settings. +* @param {Options} options The response parsing options. +* @param {String} options.payloadFormat The payload format. +* @return {object} The same response object with the body part as a JS object instead of a XML or JSON string. +*/ +StorageServiceClient._parseResponse = function (response, xml2jsSettings, options) { + function parseXml(body) { + var parsed; + var parser = new xml2js.Parser(xml2jsSettings); + parser.parseString(azureutil.removeBOM(body.toString()), function (err, parsedBody) { + if (err) { + var xmlError = new SyntaxError('EXMLFORMAT'); + xmlError.innerError = err; + throw xmlError; + } else { parsed = parsedBody; } + }); + + return parsed; + } + + if (response.body && Buffer.byteLength(response.body.toString()) > 0) { + var contentType = ''; + if (response.headers && response.headers['content-type']) { + contentType = response.headers['content-type'].toLowerCase(); + } + + try { + if (contentType.indexOf('application/json') !== -1) { + if (options && options.payloadFormat && options.payloadFormat !== TableUtilities.PayloadFormat.NO_METADATA) { + var parser = new Parser(); + parser.onValue = function (value) { + response.body = value; + }; + parser.write(response.body); + } else { + response.body = JSON.parse(response.body); + } + } else if (contentType.indexOf('application/xml') !== -1 || contentType.indexOf('application/atom+xml') !== -1) { + response.body = parseXml(response.body); + } else if (contentType.indexOf('text/html') !== -1) { + response.body = response.body; + } else { + response.body = parseXml(response.body); + // throw new SyntaxError(SR.CONTENT_TYPE_MISSING, null); + } + } catch (e) { + response.error = e; + } + } + + return response; +}; + +/** +* Gets the storage settings. +* +* @param {string} [storageAccountOrConnectionString] The storage account or the connection string. +* @param {string} [storageAccessKey] The storage access key. +* @param {string} [host] The host address. +* @param {object} [sasToken] The sas token. +* +* @return {StorageServiceSettings} +*/ +StorageServiceClient.getStorageSettings = function (storageAccountOrConnectionString, storageAccessKey, host, sasToken, endpointSuffix) { + var storageServiceSettings; + if (storageAccountOrConnectionString && !storageAccessKey && !sasToken) { + // If storageAccountOrConnectionString was passed and no accessKey was passed, assume connection string + storageServiceSettings = StorageServiceSettings.createFromConnectionString(storageAccountOrConnectionString); + } else if ((storageAccountOrConnectionString && storageAccessKey) || sasToken || host) { + // Account and key or credentials or anonymous + storageServiceSettings = StorageServiceSettings.createExplicitly(storageAccountOrConnectionString, storageAccessKey, host, sasToken, endpointSuffix); + } else { + // Use environment variables + storageServiceSettings = StorageServiceSettings.createFromEnvironment(); + } + + return storageServiceSettings; +}; + +/** +* Sets the webResource's requestUrl based on the service client settings. +* @ignore +* +* @param {WebResource} webResource The web resource where to set the request url. +*/ +StorageServiceClient.prototype._setRequestUrl = function (webResource, options) { + // Normalize the path + // Backup the original path of the webResource to make sure it works fine even this function get executed multiple times - like RetryFilter + webResource.originalPath = webResource.originalPath || webResource.path; + webResource.path = this._getPath(webResource.originalPath); + + if(!this.host){ + throw new ArgumentNullError('this.host', SR.STORAGE_HOST_LOCATION_REQUIRED); + } + + var host = this.host.primaryHost; + + if(!azureutil.objectIsNull(options) && options.currentLocation === Constants.StorageLocation.SECONDARY) { + host = this.host.secondaryHost; + } + + if(host && host.lastIndexOf('/') !== (host.length - 1)){ + host = host + '/'; + } + + var fullPath = url.format({pathname: webResource.path, query: webResource.queryString}); + webResource.uri = url.resolve(host, fullPath); + webResource.path = url.parse(webResource.uri).pathname; +}; + +/** +* Retrieves the normalized path to be used in a request. +* It also removes any leading "/" of the path in case +* it's there before. +* @ignore +* @param {string} path The path to be normalized. +* @return {string} The normalized path. +*/ +StorageServiceClient.prototype._getPath = function (path) { + if (path === null || path === undefined) { + path = ''; + } else if (path.indexOf('/') === 0) { + path = path.substring(1); + } + + return path; +}; + +/** + * Get the url of a given path + */ +StorageServiceClient.prototype._getUrl = function (path, sasToken, primary) { + var host; + if (!azureutil.objectIsNull(primary) && primary === false) { + host = this.host.secondaryHost; + } else { + host = this.host.primaryHost; + } + + host = azureutil.trimPortFromUri(host); + if(host && host.lastIndexOf('/') !== (host.length - 1)){ + host = host + '/'; + } + + var query = qs.parse(sasToken); + var fullPath = url.format({ pathname: this._getPath(path), query: query }); + return url.resolve(host, fullPath); +}; + +/** +* Initializes the default filter. +* This filter is responsible for chaining the pre filters request into the operation and, after processing the response, +* pass it to the post processing filters. This method should only be invoked by the StorageServiceClient constructor. +* @ignore +* +*/ +StorageServiceClient.prototype._initDefaultFilter = function () { + this.filter = function (requestOptions, nextPreCallback) { + if (nextPreCallback) { + // Handle the next pre callback and pass the function to be handled as post call back. + nextPreCallback(requestOptions, function (returnObject, finalCallback, nextPostCallback) { + if (nextPostCallback) { + nextPostCallback(returnObject); + } else if (finalCallback) { + finalCallback(returnObject); + } + }); + } + }; +}; + +/** +* Retrieves the metadata headers from the response headers. +* @ignore +* +* @param {object} headers The metadata headers. +* @return {object} An object with the metadata headers (without the "x-ms-" prefix). +*/ +StorageServiceClient.prototype.parseMetadataHeaders = function (headers) { + var metadata = {}; + + if (!headers) { + return metadata; + } + + for (var header in headers) { + if (header.indexOf(HeaderConstants.PREFIX_FOR_STORAGE_METADATA) === 0) { + var key = header.substr(HeaderConstants.PREFIX_FOR_STORAGE_METADATA.length, header.length - HeaderConstants.PREFIX_FOR_STORAGE_METADATA.length); + metadata[key] = headers[header]; + } + } + + return metadata; +}; + +/** +* Gets the properties of a storage account’s service, including Azure Storage Analytics. +* @ignore +* +* @this {StorageServiceClient} +* @param {object} [options] The request options. +* @param {LocationMode} [options.locationMode] Specifies the location mode used to decide which location the request should be sent to. +* Please see StorageUtilities.LocationMode for the possible values. +* @param {int} [options.timeoutIntervalInMs] The server timeout interval, in milliseconds, to use for the request. +* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request. +* @param {int} [options.maximumExecutionTimeInMs] The maximum execution time, in milliseconds, across all potential retries, to use when making this request. +* The maximum execution time interval begins at the time that the client begins building the request. The maximum +* execution time is checked intermittently while performing requests, and before executing retries. +* @param {bool} [options.useNagleAlgorithm] Determines whether the Nagle algorithm is used; true to use the Nagle algorithm; otherwise, false. +* The default value is false. +* @param {errorOrResult} callback `error` will contain information if an error occurs; otherwise, `result` will contain the properties +* and `response` will contain information related to this operation. +*/ +StorageServiceClient.prototype.getAccountServiceProperties = function (optionsOrCallback, callback) { + var userOptions; + azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); + + validate.validateArgs('getServiceProperties', function (v) { + v.callback(callback); + }); + + var options = extend(true, {}, userOptions); + + var webResource = WebResource.get() + .withQueryOption(QueryStringConstants.COMP, 'properties') + .withQueryOption(QueryStringConstants.RESTYPE, 'service'); + + options.requestLocationMode = RequestLocationMode.PRIMARY_OR_SECONDARY; + + var processResponseCallback = function (responseObject, next) { + responseObject.servicePropertiesResult = null; + if (!responseObject.error) { + responseObject.servicePropertiesResult = ServicePropertiesResult.parse(responseObject.response.body.StorageServiceProperties); + } + + // function to be called after all filters + var finalCallback = function (returnObject) { + callback(returnObject.error, returnObject.servicePropertiesResult, returnObject.response); + }; + + // call the first filter + next(responseObject, finalCallback); + }; + + this.performRequest(webResource, null, options, processResponseCallback); +}; + +/** +* Sets the properties of a storage account’s service, including Azure Storage Analytics. +* You can also use this operation to set the default request version for all incoming requests that do not have a version specified. +* +* @this {StorageServiceClient} +* @param {object} serviceProperties The service properties. +* @param {object} [options] The request options. +* @param {LocationMode} [options.locationMode] Specifies the location mode used to decide which location the request should be sent to. +* Please see StorageUtilities.LocationMode for the possible values. +* @param {int} [options.timeoutIntervalInMs] The server timeout interval, in milliseconds, to use for the request. +* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request. +* @param {int} [options.maximumExecutionTimeInMs] The maximum execution time, in milliseconds, across all potential retries, to use when making this request. +* The maximum execution time interval begins at the time that the client begins building the request. The maximum +* execution time is checked intermittently while performing requests, and before executing retries. +* @param {bool} [options.useNagleAlgorithm] Determines whether the Nagle algorithm is used; true to use the Nagle algorithm; otherwise, false. +* The default value is false. +* @param {errorOrResponse} callback `error` will contain information +* if an error occurs; otherwise, `response` +* will contain information related to this operation. +*/ +StorageServiceClient.prototype.setAccountServiceProperties = function (serviceProperties, optionsOrCallback, callback) { + var userOptions; + azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); + + validate.validateArgs('setServiceProperties', function (v) { + v.object(serviceProperties, 'serviceProperties'); + v.callback(callback); + }); + + var options = extend(true, {}, userOptions); + var servicePropertiesXml = ServicePropertiesResult.serialize(serviceProperties); + + var webResource = WebResource.put() + .withQueryOption(QueryStringConstants.COMP, 'properties') + .withQueryOption(QueryStringConstants.RESTYPE, 'service') + .withHeader(HeaderConstants.CONTENT_TYPE, 'application/xml;charset="utf-8"') + .withHeader(HeaderConstants.CONTENT_LENGTH, Buffer.byteLength(servicePropertiesXml)) + .withBody(servicePropertiesXml); + + var processResponseCallback = function (responseObject, next) { + var finalCallback = function (returnObject) { + callback(returnObject.error, returnObject.response); + }; + + next(responseObject, finalCallback); + }; + + this.performRequest(webResource, webResource.body, options, processResponseCallback); +}; + +// Other functions + +/** +* Processes the error body into a normalized error object with all the properties lowercased. +* +* Error information may be returned by a service call with additional debugging information: +* http://msdn.microsoft.com/en-us/library/windowsazure/dd179382.aspx +* +* Table services returns these properties lowercased, example, "code" instead of "Code". So that the user +* can always expect the same format, this method lower cases everything. +* +* @ignore +* +* @param {Object} error The error object as returned by the service and parsed to JSON by the xml2json. +* @return {Object} The normalized error object with all properties lower cased. +*/ +StorageServiceClient._normalizeError = function (error, response) { + if (azureutil.objectIsString(error)) { + return new StorageError(error, null); + } else if (error) { + var normalizedError = {}; + + // blob/queue errors should have error.Error, table errors should have error['odata.error'] + var errorProperties = error.Error || error.error || error['odata.error'] || error['m:error'] || error; + normalizedError.code = errorProperties.message; // The message exists when there is error.Error. + + for (var property in errorProperties) { + if (errorProperties.hasOwnProperty(property)) { + var key = property.toLowerCase(); + if(key.indexOf('m:') === 0) { + key = key.substring(2); + } + + normalizedError[key] = errorProperties[property]; + + // if this is a table error, message is an object - flatten it to normalize with blob/queue errors + // ex: "message":{"lang":"en-US","value":"The specified resource does not exist."} becomes message: "The specified resource does not exist." + if (key === 'message' && _.isObject(errorProperties[property])) { + if (errorProperties[property]['value']) { + normalizedError[key] = errorProperties[property]['value']; + } + } + } + } + + // add status code and server request id if available + if (response) { + if (response.statusCode) { + normalizedError.statusCode = response.statusCode; + } + + if (response.headers && response.headers['x-ms-request-id']) { + normalizedError.requestId = response.headers['x-ms-request-id']; + } + } + + var errorObject = new StorageError(normalizedError.code, normalizedError); + return errorObject; + } + + return null; +}; + +/** +* Sets proxy object specified by caller. +* +* @param {object} proxy proxy to use for tunneling +* { +* host: hostname +* port: port number +* proxyAuth: 'user:password' for basic auth +* headers: {...} headers for proxy server +* key: key for proxy server +* ca: ca for proxy server +* cert: cert for proxy server +* } +* if null or undefined, clears proxy +*/ +StorageServiceClient.prototype.setProxy = function (proxy) { + if (proxy) { + this.proxy = proxy; + } else { + this.proxy = null; + } +}; + +/** +* Sets the service host default proxy from the environment. +* Can be overridden by calling _setProxyUrl or _setProxy +* +*/ +StorageServiceClient.prototype._setDefaultProxy = function () { + var proxyUrl = StorageServiceClient._loadEnvironmentProxyValue(); + if (proxyUrl) { + var parsedUrl = url.parse(proxyUrl); + if (!parsedUrl.port) { + parsedUrl.port = 80; + } + this.setProxy(parsedUrl); + } else { + this.setProxy(null); + } +}; + +/* +* Loads the fields "useProxy" and respective protocol, port and url +* from the environment values HTTPS_PROXY and HTTP_PROXY +* in case those are set. +* @ignore +* +* @return {string} or null +*/ +StorageServiceClient._loadEnvironmentProxyValue = function () { + var proxyUrl = null; + if (process.env[StorageServiceClientConstants.EnvironmentVariables.HTTPS_PROXY]) { + proxyUrl = process.env[StorageServiceClientConstants.EnvironmentVariables.HTTPS_PROXY]; + } else if (process.env[StorageServiceClientConstants.EnvironmentVariables.HTTPS_PROXY.toLowerCase()]) { + proxyUrl = process.env[StorageServiceClientConstants.EnvironmentVariables.HTTPS_PROXY.toLowerCase()]; + } else if (process.env[StorageServiceClientConstants.EnvironmentVariables.HTTP_PROXY]) { + proxyUrl = process.env[StorageServiceClientConstants.EnvironmentVariables.HTTP_PROXY]; + } else if (process.env[StorageServiceClientConstants.EnvironmentVariables.HTTP_PROXY.toLowerCase()]) { + proxyUrl = process.env[StorageServiceClientConstants.EnvironmentVariables.HTTP_PROXY.toLowerCase()]; + } + + return proxyUrl; +}; + +/** +* Initializes the location to which the operation is being sent to. +*/ +StorageServiceClient.prototype._initializeLocation = function (options) { + if(!azureutil.objectIsNull(options.locationMode)) { + switch(options.locationMode) { + case StorageUtilities.LocationMode.PRIMARY_ONLY: + case StorageUtilities.LocationMode.PRIMARY_THEN_SECONDARY: + options.currentLocation = Constants.StorageLocation.PRIMARY; + break; + case StorageUtilities.LocationMode.SECONDARY_ONLY: + case StorageUtilities.LocationMode.SECONDARY_THEN_PRIMARY: + options.currentLocation = Constants.StorageLocation.SECONDARY; + break; + default: + throw new RangeError(util.format(SR.ARGUMENT_OUT_OF_RANGE_ERROR, 'locationMode', options.locationMode)); + } + } else { + options.locationMode = StorageUtilities.LocationMode.PRIMARY_ONLY; + options.currentLocation = Constants.StorageLocation.PRIMARY; + } +}; + +/** +* Validates the location to which the operation is being sent to. +*/ +StorageServiceClient.prototype._validateLocation = function (options) { + if(this._invalidLocationMode(options.locationMode)) { + throw new ArgumentNullError('host', SR.STORAGE_HOST_MISSING_LOCATION); + } + + switch(options.requestLocationMode) { + case Constants.RequestLocationMode.PRIMARY_ONLY: + if(options.locationMode === StorageUtilities.LocationMode.SECONDARY_ONLY) { + throw new ArgumentError('host.primaryHost', SR.PRIMARY_ONLY_COMMAND); + } + + options.currentLocation = Constants.StorageLocation.PRIMARY; + options.locationMode = StorageUtilities.LocationMode.PRIMARY_ONLY; + break; + + case Constants.RequestLocationMode.SECONDARY_ONLY: + if(options.locationMode === StorageUtilities.LocationMode.PRIMARY_ONLY) { + throw new ArgumentError('host.secondaryHost', SR.SECONDARY_ONLY_COMMAND); + } + + options.currentLocation = Constants.StorageLocation.SECONDARY; + options.locationMode = StorageUtilities.LocationMode.SECONDARY_ONLY; + break; + + default: + // no op + } +}; + +/** +* Checks whether we have the relevant host information based on the locationMode. +*/ +StorageServiceClient.prototype._invalidLocationMode = function (locationMode) { + switch(locationMode) { + case StorageUtilities.LocationMode.PRIMARY_ONLY: + return azureutil.objectIsNull(this.host.primaryHost); + case StorageUtilities.LocationMode.SECONDARY_ONLY: + return azureutil.objectIsNull(this.host.secondaryHost); + default: + return (azureutil.objectIsNull(this.host.primaryHost) || azureutil.objectIsNull(this.host.secondaryHost)); + } +}; + +/** +* Checks to see if the maximum execution timeout provided has been exceeded. +*/ +StorageServiceClient.prototype._maximumExecutionTimeExceeded = function (currentTime, expiryTime) { + if(!azureutil.objectIsNull(expiryTime) && currentTime > expiryTime) { + return true; + } else { + return false; + } +}; + +/** +* Sets the operation expiry time. +*/ +StorageServiceClient.prototype._setOperationExpiryTime = function (options) { + if(azureutil.objectIsNull(options.operationExpiryTime)) { + if(!azureutil.objectIsNull(options.maximumExecutionTimeInMs)) { + options.operationExpiryTime = Date.now() + options.maximumExecutionTimeInMs; + } else if(this.defaultMaximumExecutionTimeInMs) { + options.operationExpiryTime = Date.now() + this.defaultMaximumExecutionTimeInMs; + } + } +}; + +module.exports = StorageServiceClient; + +}).call(this,require('_process'),require("buffer").Buffer) +},{"../../services/table/tableutilities":72,"../diagnostics/logger":7,"../errors/errors":8,"../http/webresource":12,"../md5-wrapper":13,"../models/servicepropertiesresult":15,"../request-wrapper":17,"../signing/sharedaccesssignature":22,"../signing/sharedkey":23,"../streams/bufferstream.js":26,"../util/constants":33,"../util/sr":37,"../util/storageutilities":38,"../util/util":39,"../util/validate":40,"./servicesettings":18,"./storageservicesettings":20,"_process":197,"buffer":"buffer","events":155,"extend":157,"json-edm-parser":178,"os":184,"querystring":207,"underscore":247,"url":248,"util":"util","uuid":253,"xml2js":260}],260:[function(require,module,exports){ +(function (process){ +// Generated by CoffeeScript 1.6.1 +(function() { + var events, isEmpty, sax, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + _this = this; + + sax = require('sax'); + + events = require('events'); + + isEmpty = function(thing) { + return typeof thing === "object" && (thing != null) && Object.keys(thing).length === 0; + }; + + exports.defaults = { + "0.1": { + explicitCharkey: false, + trim: true, + normalize: true, + normalizeTags: false, + attrkey: "@", + charkey: "#", + explicitArray: false, + ignoreAttrs: false, + mergeAttrs: false, + explicitRoot: false, + validator: null, + xmlns: false, + explicitChildren: false, + childkey: '@@', + charsAsChildren: false, + async: false, + strict: true + }, + "0.2": { + explicitCharkey: false, + trim: false, + normalize: false, + normalizeTags: false, + attrkey: "$", + charkey: "_", + explicitArray: true, + ignoreAttrs: false, + mergeAttrs: false, + explicitRoot: true, + validator: null, + xmlns: false, + explicitChildren: false, + childkey: '$$', + charsAsChildren: false, + async: false, + strict: true + } + }; + + exports.ValidationError = (function(_super) { + + __extends(ValidationError, _super); + + function ValidationError(message) { + this.message = message; + } + + return ValidationError; + + })(Error); + + exports.Parser = (function(_super) { + + __extends(Parser, _super); + + function Parser(opts) { + var key, value, _ref, + _this = this; + this.parseString = function(str, cb) { + return Parser.prototype.parseString.apply(_this, arguments); + }; + this.reset = function() { + return Parser.prototype.reset.apply(_this, arguments); + }; + this.options = {}; + _ref = exports.defaults["0.2"]; + for (key in _ref) { + if (!__hasProp.call(_ref, key)) continue; + value = _ref[key]; + this.options[key] = value; + } + for (key in opts) { + if (!__hasProp.call(opts, key)) continue; + value = opts[key]; + this.options[key] = value; + } + if (this.options.xmlns) { + this.options.xmlnskey = this.options.attrkey + "ns"; + } + this.reset(); + } + + Parser.prototype.reset = function() { + var attrkey, charkey, err, stack, + _this = this; + this.removeAllListeners(); + this.saxParser = sax.parser(this.options.strict, { + trim: false, + normalize: false, + xmlns: this.options.xmlns + }); + err = false; + this.saxParser.onerror = function(error) { + if (!err) { + err = true; + return _this.emit("error", error); + } + }; + this.EXPLICIT_CHARKEY = this.options.explicitCharkey; + this.resultObject = null; + stack = []; + attrkey = this.options.attrkey; + charkey = this.options.charkey; + this.saxParser.onopentag = function(node) { + var key, obj, _ref; + obj = {}; + obj[charkey] = ""; + if (!_this.options.ignoreAttrs) { + _ref = node.attributes; + for (key in _ref) { + if (!__hasProp.call(_ref, key)) continue; + if (!(attrkey in obj) && !_this.options.mergeAttrs) { + obj[attrkey] = {}; + } + if (_this.options.mergeAttrs) { + obj[key] = node.attributes[key]; + } else { + obj[attrkey][key] = node.attributes[key]; + } + } + } + obj["#name"] = _this.options.normalizeTags ? node.name.toLowerCase() : node.name; + if (_this.options.xmlns) { + obj[_this.options.xmlnskey] = { + uri: node.uri, + local: node.local + }; + } + return stack.push(obj); + }; + this.saxParser.onclosetag = function() { + var node, nodeName, obj, old, s, xpath; + obj = stack.pop(); + nodeName = obj["#name"]; + delete obj["#name"]; + s = stack[stack.length - 1]; + if (obj[charkey].match(/^\s*$/)) { + delete obj[charkey]; + } else { + if (_this.options.trim) { + obj[charkey] = obj[charkey].trim(); + } + if (_this.options.normalize) { + obj[charkey] = obj[charkey].replace(/\s{2,}/g, " ").trim(); + } + if (Object.keys(obj).length === 1 && charkey in obj && !_this.EXPLICIT_CHARKEY) { + obj = obj[charkey]; + } + } + if ((isEmpty(obj)) && stack.length > 0) { + obj = _this.options.emptyTag !== void 0 ? _this.options.emptyTag : ''; + } + if (_this.options.validator != null) { + xpath = "/" + ((function() { + var _i, _len, _results; + _results = []; + for (_i = 0, _len = stack.length; _i < _len; _i++) { + node = stack[_i]; + _results.push(node["#name"]); + } + return _results; + })()).concat(nodeName).join("/"); + try { + obj = _this.options.validator(xpath, s && s[nodeName], obj); + } catch (err) { + _this.emit("error", err); + } + } + if (_this.options.explicitChildren && !_this.options.mergeAttrs && typeof obj === 'object') { + node = {}; + if (_this.options.attrkey in obj) { + node[_this.options.attrkey] = obj[_this.options.attrkey]; + delete obj[_this.options.attrkey]; + } + if (!_this.options.charsAsChildren && _this.options.charkey in obj) { + node[_this.options.charkey] = obj[_this.options.charkey]; + delete obj[_this.options.charkey]; + } + if (Object.getOwnPropertyNames(obj).length > 0) { + node[_this.options.childkey] = obj; + } + obj = node; + } + if (stack.length > 0) { + if (!_this.options.explicitArray) { + if (!(nodeName in s)) { + return s[nodeName] = obj; + } else if (s[nodeName] instanceof Array) { + return s[nodeName].push(obj); + } else { + old = s[nodeName]; + s[nodeName] = [old]; + return s[nodeName].push(obj); + } + } else { + if (!(s[nodeName] instanceof Array)) { + s[nodeName] = []; + } + return s[nodeName].push(obj); + } + } else { + if (_this.options.explicitRoot) { + old = obj; + obj = {}; + obj[nodeName] = old; + } + _this.resultObject = obj; + return _this.emit("end", _this.resultObject); + } + }; + return this.saxParser.ontext = this.saxParser.oncdata = function(text) { + var s; + s = stack[stack.length - 1]; + if (s) { + return s[charkey] += text; + } + }; + }; + + Parser.prototype.parseString = function(str, cb) { + if ((cb != null) && typeof cb === "function") { + this.on("end", function(result) { + this.reset(); + if (this.options.async) { + return process.nextTick(function() { + return cb(null, result); + }); + } else { + return cb(null, result); + } + }); + this.on("error", function(err) { + this.reset(); + if (this.options.async) { + return process.nextTick(function() { + return cb(err); + }); + } else { + return cb(err); + } + }); + } + if (str.toString().trim() === '') { + this.emit("end", null); + return true; + } + return this.saxParser.write(str.toString()); + }; + + return Parser; + + })(events.EventEmitter); + + exports.parseString = function(str, a, b) { + var cb, options, parser; + if (b != null) { + if (typeof b === 'function') { + cb = b; + } + if (typeof a === 'object') { + options = a; + } + } else { + if (typeof a === 'function') { + cb = a; + } + options = {}; + } + parser = new exports.Parser(options); + return parser.parseString(str, cb); + }; + +}).call(this); + +}).call(this,require('_process')) +},{"_process":197,"events":155,"sax":222}],222:[function(require,module,exports){ +// wrapper for non-node envs +;(function (sax) { + +sax.parser = function (strict, opt) { return new SAXParser(strict, opt) } +sax.SAXParser = SAXParser +sax.SAXStream = SAXStream +sax.createStream = createStream + +// When we pass the MAX_BUFFER_LENGTH position, start checking for buffer overruns. +// When we check, schedule the next check for MAX_BUFFER_LENGTH - (max(buffer lengths)), +// since that's the earliest that a buffer overrun could occur. This way, checks are +// as rare as required, but as often as necessary to ensure never crossing this bound. +// Furthermore, buffers are only tested at most once per write(), so passing a very +// large string into write() might have undesirable effects, but this is manageable by +// the caller, so it is assumed to be safe. Thus, a call to write() may, in the extreme +// edge case, result in creating at most one complete copy of the string passed in. +// Set to Infinity to have unlimited buffers. +sax.MAX_BUFFER_LENGTH = 64 * 1024 + +var buffers = [ + "comment", "sgmlDecl", "textNode", "tagName", "doctype", + "procInstName", "procInstBody", "entity", "attribName", + "attribValue", "cdata", "script" +] + +sax.EVENTS = // for discoverability. + [ "text" + , "processinginstruction" + , "sgmldeclaration" + , "doctype" + , "comment" + , "attribute" + , "opentag" + , "closetag" + , "opencdata" + , "cdata" + , "closecdata" + , "error" + , "end" + , "ready" + , "script" + , "opennamespace" + , "closenamespace" + ] + +function SAXParser (strict, opt) { + if (!(this instanceof SAXParser)) return new SAXParser(strict, opt) + + var parser = this + clearBuffers(parser) + parser.q = parser.c = "" + parser.bufferCheckPosition = sax.MAX_BUFFER_LENGTH + parser.opt = opt || {} + parser.opt.lowercase = parser.opt.lowercase || parser.opt.lowercasetags + parser.looseCase = parser.opt.lowercase ? "toLowerCase" : "toUpperCase" + parser.tags = [] + parser.closed = parser.closedRoot = parser.sawRoot = false + parser.tag = parser.error = null + parser.strict = !!strict + parser.noscript = !!(strict || parser.opt.noscript) + parser.state = S.BEGIN + parser.ENTITIES = Object.create(sax.ENTITIES) + parser.attribList = [] + + // namespaces form a prototype chain. + // it always points at the current tag, + // which protos to its parent tag. + if (parser.opt.xmlns) parser.ns = Object.create(rootNS) + + // mostly just for error reporting + parser.trackPosition = parser.opt.position !== false + if (parser.trackPosition) { + parser.position = parser.line = parser.column = 0 + } + emit(parser, "onready") +} + +if (!Object.create) Object.create = function (o) { + function f () { this.__proto__ = o } + f.prototype = o + return new f +} + +if (!Object.getPrototypeOf) Object.getPrototypeOf = function (o) { + return o.__proto__ +} + +if (!Object.keys) Object.keys = function (o) { + var a = [] + for (var i in o) if (o.hasOwnProperty(i)) a.push(i) + return a +} + +function checkBufferLength (parser) { + var maxAllowed = Math.max(sax.MAX_BUFFER_LENGTH, 10) + , maxActual = 0 + for (var i = 0, l = buffers.length; i < l; i ++) { + var len = parser[buffers[i]].length + if (len > maxAllowed) { + // Text/cdata nodes can get big, and since they're buffered, + // we can get here under normal conditions. + // Avoid issues by emitting the text node now, + // so at least it won't get any bigger. + switch (buffers[i]) { + case "textNode": + closeText(parser) + break + + case "cdata": + emitNode(parser, "oncdata", parser.cdata) + parser.cdata = "" + break + + case "script": + emitNode(parser, "onscript", parser.script) + parser.script = "" + break + + default: + error(parser, "Max buffer length exceeded: "+buffers[i]) + } + } + maxActual = Math.max(maxActual, len) + } + // schedule the next check for the earliest possible buffer overrun. + parser.bufferCheckPosition = (sax.MAX_BUFFER_LENGTH - maxActual) + + parser.position +} + +function clearBuffers (parser) { + for (var i = 0, l = buffers.length; i < l; i ++) { + parser[buffers[i]] = "" + } +} + +SAXParser.prototype = + { end: function () { end(this) } + , write: write + , resume: function () { this.error = null; return this } + , close: function () { return this.write(null) } + } + +try { + var Stream = require("stream").Stream +} catch (ex) { + var Stream = function () {} +} + + +var streamWraps = sax.EVENTS.filter(function (ev) { + return ev !== "error" && ev !== "end" +}) + +function createStream (strict, opt) { + return new SAXStream(strict, opt) +} + +function SAXStream (strict, opt) { + if (!(this instanceof SAXStream)) return new SAXStream(strict, opt) + + Stream.apply(this) + + this._parser = new SAXParser(strict, opt) + this.writable = true + this.readable = true + + + var me = this + + this._parser.onend = function () { + me.emit("end") + } + + this._parser.onerror = function (er) { + me.emit("error", er) + + // if didn't throw, then means error was handled. + // go ahead and clear error, so we can write again. + me._parser.error = null + } + + streamWraps.forEach(function (ev) { + Object.defineProperty(me, "on" + ev, { + get: function () { return me._parser["on" + ev] }, + set: function (h) { + if (!h) { + me.removeAllListeners(ev) + return me._parser["on"+ev] = h + } + me.on(ev, h) + }, + enumerable: true, + configurable: false + }) + }) +} + +SAXStream.prototype = Object.create(Stream.prototype, + { constructor: { value: SAXStream } }) + +SAXStream.prototype.write = function (data) { + this._parser.write(data.toString()) + this.emit("data", data) + return true +} + +SAXStream.prototype.end = function (chunk) { + if (chunk && chunk.length) this._parser.write(chunk.toString()) + this._parser.end() + return true +} + +SAXStream.prototype.on = function (ev, handler) { + var me = this + if (!me._parser["on"+ev] && streamWraps.indexOf(ev) !== -1) { + me._parser["on"+ev] = function () { + var args = arguments.length === 1 ? [arguments[0]] + : Array.apply(null, arguments) + args.splice(0, 0, ev) + me.emit.apply(me, args) + } + } + + return Stream.prototype.on.call(me, ev, handler) +} + + + +// character classes and tokens +var whitespace = "\r\n\t " + // this really needs to be replaced with character classes. + // XML allows all manner of ridiculous numbers and digits. + , number = "0124356789" + , letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + // (Letter | "_" | ":") + , quote = "'\"" + , entity = number+letter+"#" + , attribEnd = whitespace + ">" + , CDATA = "[CDATA[" + , DOCTYPE = "DOCTYPE" + , XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace" + , XMLNS_NAMESPACE = "http://www.w3.org/2000/xmlns/" + , rootNS = { xml: XML_NAMESPACE, xmlns: XMLNS_NAMESPACE } + +// turn all the string character sets into character class objects. +whitespace = charClass(whitespace) +number = charClass(number) +letter = charClass(letter) + +// http://www.w3.org/TR/REC-xml/#NT-NameStartChar +// This implementation works on strings, a single character at a time +// as such, it cannot ever support astral-plane characters (10000-EFFFF) +// without a significant breaking change to either this parser, or the +// JavaScript language. Implementation of an emoji-capable xml parser +// is left as an exercise for the reader. +var nameStart = /[:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/ + +var nameBody = /[:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u00B7\u0300-\u036F\u203F-\u2040\.\d-]/ + +quote = charClass(quote) +entity = charClass(entity) +attribEnd = charClass(attribEnd) + +function charClass (str) { + return str.split("").reduce(function (s, c) { + s[c] = true + return s + }, {}) +} + +function isRegExp (c) { + return Object.prototype.toString.call(c) === '[object RegExp]' +} + +function is (charclass, c) { + return isRegExp(charclass) ? !!c.match(charclass) : charclass[c] +} + +function not (charclass, c) { + return !is(charclass, c) +} + +var S = 0 +sax.STATE = +{ BEGIN : S++ +, TEXT : S++ // general stuff +, TEXT_ENTITY : S++ // & and such. +, OPEN_WAKA : S++ // < +, SGML_DECL : S++ // +, SCRIPT : S++ // + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +> + + \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 000000000..0b84c487d --- /dev/null +++ b/package.json @@ -0,0 +1,9 @@ +{ + "name": "browser", + "version": "0.0.0", + "description": "browser", + "main": "server.js", + "author": { + "name": "Luis" + } +} diff --git a/server.js b/server.js new file mode 100644 index 000000000..bfc709d14 --- /dev/null +++ b/server.js @@ -0,0 +1,333 @@ + +const MODULE_NAME = "Node Server"; +const START_BROWSER_APP = true; + +const FILE_PATH = '../../AAlgos/Data'; + +///const DATABASE_MODULE = require('../../Picha - API/Picha/Database'); +//const database = DATABASE_MODULE.newDatabase(); + +try { + // database.connectToServer(startHtttpServer); +} +catch (err) { + //console.log(err); +} + + + +//'use strict'; +var http = require('http'); +var port = process.env.PORT || 1337; + +startHtttpServer(); + +function onRequestReceivedFromBrowser(request, response) { + + var htmlResponse; + var requestParameters = request.url.split("/"); + + + switch (requestParameters[1]) { + + case "index-files": + { + respondWithFile(FILE_PATH + '/index-files/' + requestParameters[2], response); + } + break; + + case "daily-files": + { + respondWithFile(FILE_PATH + '/daily-files/' + requestParameters[2], response); + } + break; + + case "other-files": + { + respondWithFile(FILE_PATH + '/other-files/' + requestParameters[2], response); + } + break; + + case "all-times": + { + respondWithFile(FILE_PATH + '/all-times/' + requestParameters[2], response); + } + break; + + case "order-books": + { + respondWithFile(FILE_PATH + '/order-books/' + requestParameters[2] + "/" + requestParameters[3] + "/" + requestParameters[4] + "/" + requestParameters[5] + "/" + requestParameters[6] + "/" + requestParameters[7], response); + } + break; + + case "aggregatted-order-books": + { + respondWithFile(FILE_PATH + '/aggregatted-order-books/' + requestParameters[2] + "/" + requestParameters[3] + "/" + requestParameters[4] + "/" + requestParameters[5] + "/" + requestParameters[6] + "/" + requestParameters[7], response); + } + break; + + case "CandleTechnicalAnalisys": + { + respondWithFile('./' + requestParameters[1] + '/' + requestParameters[2], response); + } + break; + + case "VolumeTechnicalAnalisys": + { + respondWithFile('./' + requestParameters[1] + '/' + requestParameters[2], response); + } + break; + + case "Panels": + { + respondWithFile('./' + requestParameters[1] + '/' + requestParameters[2], response); + } + break; + + case "ChartLayers": + { + respondWithFile('./' + requestParameters[1] + '/' + requestParameters[2], response); + } + break; + + case "Indicators": + { + respondWithFile('./' + requestParameters[1] + '/' + requestParameters[2], response); + } + break; + + case "Azure": + { + respondWithFile('./' + requestParameters[1] + '/' + requestParameters[2], response); + } + break; + + case "lastTrades": + { + const marketId = requestParameters[2]; + const exchangeId = requestParameters[3]; + + const sqlQuery = "SELECT TOP 100000 " + + " InternalExchangeTradeId AS Id, " + + " Type, " + + " Rate, " + + " AmountAssetA AS AmountA, " + + " AmountAssetB AS AmountB, " + + " Timestamp " + + " FROM Trades " + + " WHERE " + + " IdMarket = " + marketId + " " + + " AND " + + " IdExchange = " + exchangeId + " " + + " ORDER BY IdExchange ASC, IdMarket ASC, InternalExchangeTradeId DESC" + ; + + ////console.log("About to execute: " + sqlQuery); + + + /* Then we execute the query */ + + database.executeSQL(sqlQuery, function (err, resultSet) { + + //console.log("Query executed: " + sqlQuery); + + var myJsonString = JSON.stringify(resultSet); + + ////console.log(myJsonString); + + response.writeHead(200, { 'Content-Type': 'text/plain' }); + response.write(myJsonString); + response.end("\n"); + } + ); + + } + break; + + + case "trades": + { + const marketId = requestParameters[2]; + const exchangeId = requestParameters[3]; + const idBegin = unescape(requestParameters[4]); + const idEnd = unescape(requestParameters[5]); + + const sqlQuery = "SELECT " + + " InternalExchangeTradeId AS Id, " + + " Type, " + + " Rate, " + + " AmountAssetA AS AmountA, " + + " AmountAssetB AS AmountB, " + + " Timestamp " + + " FROM Trades " + + " WHERE " + + " IdExchange = " + exchangeId + " " + + " AND " + + " IdMarket = " + marketId + " " + + " AND " + + " InternalExchangeTradeId >= " + idBegin + " " + + " AND " + + " InternalExchangeTradeId <= " + idEnd + " " + + " ORDER BY IdExchange, IdMarket, InternalExchangeTradeId " + ; + + let records = idEnd - idBegin; + console.log("Records Requested: " + records + " idBegin = " + idBegin + " idEnd = " + idEnd); + + + /* Then we execute the query */ + + database.executeSQL(sqlQuery, function (err, resultSet) { + + //console.log("Query executed: " + sqlQuery); + + var myJsonString = JSON.stringify(resultSet); + + ////console.log(myJsonString); + + response.writeHead(200, { 'Content-Type': 'text/plain' }); + response.write(myJsonString); + response.end("\n"); + } + ); + + } + break; + + + case "orderbook": + + { + const marketId = requestParameters[2]; + const datetimeBegin = unescape(requestParameters[3]); + const datetimeEnd = unescape(requestParameters[4]); + + var sqlQuery = "SELECT " + + " Id, " + + " Rate, " + + " Amount * Rate AS AmountA, " + + " Amount AS AmountB, " + + " Type AS Type, " + + " DatetimeIn AS DatetimeIn, " + + " ISNULL(DatetimeOut, '3000-01-01') AS DatetimeOut " + + " FROM OrderBooks " + + " WHERE " + + " ( " + + " IdMarket = " + marketId + " " + + " ) " + + " AND " + + " ( " + + " DatetimeIn <= '" + datetimeEnd + "' " + + " ) " + + " AND " + + " ( " + + " ISNULL(DatetimeOut, '3000-01-01') > '" + datetimeBegin + "' " + + " ) " + ; + + + //console.log(sqlQuery); + + + /* Then we execute the query */ + + database.executeSQL(sqlQuery, function (err, resultSet) { + + var myJsonString = JSON.stringify(resultSet); + + //console.log(myJsonString); + + response.writeHead(200, { 'Content-Type': 'text/plain' }); + response.write(myJsonString); + response.end("\n"); + } + ); + } + break; + + + + + + default: + ////console.log("Unknown request received from the browser: " + request.url); + + if (requestParameters[1] === "") { + requestParameters[1] = "index.html"; + } + + respondWithFile("" + requestParameters[1], response); + + } + + function sendResponseToBrowser(htmlResponse) { + response.writeHead(200, { 'Content-Type': 'text/html' }); + response.write(htmlResponse); + + response.end("\n"); + } +} + + +function respondWithFile(fileName, response) { + + let fs = require('fs'); + try { + + fs.readFile(fileName, onFileRead); + + function onFileRead(err, file) { + + try { + let htmlResponse = file.toString(); + + response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1. + response.setHeader("Pragma", "no-cache"); // HTTP 1.0. + response.setHeader("Expires", "0"); // Proxies. + response.setHeader("Access-Control-Allow-Origin", "*"); // Allows to access data from other domains. + + response.writeHead(200, { 'Content-Type': 'text/html' }); + response.write(htmlResponse); + response.end("\n"); + console.log("File Sent: " + fileName); + // + } + catch (err) { + returnEmptyArray(); + console.log("File Not Found: " + fileName); + } + + } + } + catch (err) { + returnEmptyArray(); + } + + function returnEmptyArray() { + + response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1. + response.setHeader("Pragma", "no-cache"); // HTTP 1.0. + response.setHeader("Expires", "0"); // Proxies. + + response.writeHead(200, { 'Content-Type': 'text/html' }); + response.write("[]"); + response.end("\n"); + + } +} + +function startHtttpServer() { + + if (START_BROWSER_APP === true) { + + try { + + gWebServer = http.createServer(onRequestReceivedFromBrowser).listen(port); + } + catch (err) { + console.log(err); + } + } +} + + diff --git a/temp.js b/temp.js new file mode 100644 index 000000000..55b9e3634 --- /dev/null +++ b/temp.js @@ -0,0 +1,791 @@ + +function newOrderBookChart() { + + /* + + This particular chart, plots information valid at a certain point in time. That menas that first it needs to get that information, and it also + has to discard information that is not valid anymore. There is one issue: some graphics elements are displayed and animated, like balls, that + means that every time this chart request new info, it can not destroy all the current orders it is based on, since, if they are still valid + for the next period of time, recreating them would men producing a glinch on the visualization. So that means that every time that the time period + changes, it needs not only to incorporate new orders, but also it needs to eliminate the ones that are out of scope. + + */ + + var datetime; + + var orderBookChart = { + setDatetime: setDatetime, + isInitialized: false, + container: undefined, + plotArea: undefined, + draw: draw, + getContainer: getContainer, // returns the inner most container that holds the point received by parameter. + initialize: initialize + }; + + + + /* + + Here we will define the two arrays that will contain mostly the points to be plotted on the chart. + These arrays contains an array that have the following structure: + + 0. Rate + 1. Amount A This is the SUM of Amounts A that belongs to this point in the chart. + 2. The SUM of Amount A This is the cumulative SUM of SUMs of Amounts A, begining from the smaller rate and going up. + 3. A Label Ball object + + */ + + var orderBookAsksChartPoints = []; + var orderBookBidsChartPoints = []; + + var ballsOrdersQueue = []; // This is the queue of balls waiting for the order to be added to the array to be displayed. + var orderBallsReleaseTimer; + + var container = newContainer(); + container.initialize(); + orderBookChart.container = container; + + container.displacement.containerName = "Order Book Chart"; + container.zoom.containerName = "Order Book Chart"; + container.frame.containerName = "Order Book Chart"; + + var orderBookSnapShot = newOrderBookSnapShot(); + + return orderBookChart; + + + function initialize() { + + orderBookSnapShot.initialize(); + + orderBookSnapShot.eventHandler.listenToEvent('Order Added', onOrderAdded, undefined); + orderBookSnapShot.eventHandler.listenToEvent('Order Removed', onOrderRemoved, undefined); + + orderBookSnapShot.setDatetime(datetime); + + orderBallsReleaseTimer = setInterval(onReleaseOneMoreBall, 100); + // clearTimeout(orderBallsReleaseTimer); Use this to cancel the timer. + + + + } + + + + function getContainer(point) { + + var container; + + /* First we check if this point is inside this space. */ + + if (this.container.frame.isThisPointHere(point) === true) { + + return this.container; + + } else { + + /* This point does not belong to this space. */ + + return undefined; + } + + } + + /* + + FUNCTIONS RELATED TO "HANDLING EVENTS" + + */ + + function onOrderAdded() { + + } + + + function onOrderRemoved() { + + } + + + + function onOrderBookChanged() { + + /* + + Fresh data about the order book has just arrived. We need to take it an put it in the right way so that + this chart can show what it is supposed to show. + + */ + + /* Since the range of the data might change, we must calculate the plotArea everytime new data arrives. */ + + var plotArea = newPlotArea(); + orderBookChart.plotArea = plotArea; + + orderBookChart.plotArea.initialize( + orderBookChart.orderBook.server.orderBookStatistics.resultSet["row 1"].MIN_Rate, + orderBookChart.orderBook.server.orderBookStatistics.resultSet["row 2"].MAX_Rate, + orderBookChart.container.frame.width + ); + + /* Balls are being created, before they enter into the floating space. */ + + transformRecivedOrdersIntoOrderBalls(); + + orderBookChart.isInitialized = true; + } + + + function setDatetime(newDatetime) { + + datetime = newDatetime; + orderBookChart.orderBookSnapShot.setDatetime(datetime); + + } + + + function onReleaseOneMoreBall() { + + /* + + Orders that are queued to be displayed, are released, one by one every time this function is executed. + With released, we mean that are pushed to an array of balls that are currently rendered at the Floating Space. + + */ + + if (ballsOrdersQueue.length > 0) { + + /* We take the first order in the queue and push it to the balls array. */ + + var ball = ballsOrdersQueue.shift(); + + balls.push(ball); + + orderBookChart.addOrder(ball.linkedObject); // We add the order linked to this ball to the order book. + + updateBallsTargets(); // This will recalculate the target for the new ball added. + } + + } + + + /* + + FUNCTIONS RELATED TO "PLOTTING THE CHART" + + */ + + function drawOrderBookChart() { + + /* + + This function is responsible for plotting the chart onto the browser canvas. It assumes that there exists 2 arrays, containing + the points already calculated to be plotted. Those calculations happens when a new order is added to the chart, in this function + we just plot whatever is already calculated. + + */ + + /* The first part of this function will plot BIDs, the second one will plot the ASKs. */ + + if (orderBookBidsChartPoints.length > 0) { + + /* We calculate the first points of the chart. These are not points representing orders, but the lower part of the hill plotted. */ + + var maxBasePoint = { + x: (orderBookBidsChartPoints[orderBookBidsChartPoints.length - 1][0] - orderBookChart.plotArea.min.x) * orderBookChart.plotArea.plotArea.x, // We take the first Order Rate on the array for this. + y: orderBookChart.container.frame.height * 5 / 7 + }; + + var minBasePoint = { + x: (orderBookBidsChartPoints[0][0] - orderBookChart.plotArea.min.x) * orderBookChart.plotArea.x, // We take the first Order Rate on the array for this. + y: orderBookChart.container.frame.height * 5 / 7 + }; + + /* We make the point relative to the current frame */ + + maxBasePoint = orderBookChart.container.frame.frameThisPoint(maxBasePoint); + minBasePoint = orderBookChart.container.frame.frameThisPoint(minBasePoint); + + /* We add the possible displacement */ + + maxBasePoint = orderBookChart.container.displacement.displaceThisPoint(maxBasePoint); + minBasePoint = orderBookChart.container.displacement.displaceThisPoint(minBasePoint); + + /* We apply the zoom factor. */ + + minBasePoint = orderBookChart.container.zoom.zoomThisPoint(minBasePoint); + maxBasePoint = orderBookChart.container.zoom.zoomThisPoint(maxBasePoint); + + /* Lets start the drawing. */ + + browserCanvasContext.beginPath(); + browserCanvasContext.moveTo(minBasePoint.x, minBasePoint.y); + + var sumY = 0; + + for (var i = 0; i < orderBookBidsChartPoints.length; i++) { + + sumY = sumY + orderBookBidsChartPoints[i][1]; + + var point = { + x: (orderBookBidsChartPoints[i][0] - orderBookChart.plotArea.min.x) * orderBookChart.plotArea.plotArea.x, + y: orderBookChart.container.frame.height * 5 / 7 - sumY + }; + + var rawPosition = { + x: point.x, + y: point.y + }; + + /* Frame the point */ + + point = orderBookChart.container.frame.frameThisPoint(point); + + /* First we apply the displacement. */ + + point = orderBookChart.container.displacement.displaceThisPoint(point); + + /* We apply the zoom factor. */ + + point = orderBookChart.container.zoom.zoomThisPoint(point); + + browserCanvasContext.lineTo(point.x, point.y); + + + + } + + browserCanvasContext.lineTo(maxBasePoint.x, maxBasePoint.y); + browserCanvasContext.lineTo(minBasePoint.x, minBasePoint.y); + browserCanvasContext.closePath(); + browserCanvasContext.fillStyle = 'rgba(130, 180, 30, 0.9)'; + browserCanvasContext.fill(); + browserCanvasContext.strokeStyle = 'rgba(30, 230, 30, 0.9)'; + browserCanvasContext.lineWidth = 1; + browserCanvasContext.stroke(); + + browserCanvasContext.closePath(); + + + } + + /* Ok, Now it is the turn of the ASKs, following is the code that plots them on the chart. */ + + + if (orderBookAsksChartPoints.length > 0) { + + /* We calculate the first point of the chart */ + + var maxBasePoint = { + x: (orderBookAsksChartPoints[orderBookAsksChartPoints.length - 1][0] - orderBookChart.plotArea.min.x) * orderBookChart.plotArea.plotArea.x, // We take the first Order Rate on the array for this. + y: orderBookChart.container.frame.height * 5 / 7 + }; + + var minBasePoint = { + x: (orderBookAsksChartPoints[0][0] - orderBookChart.plotArea.min.x) * orderBookChart.plotArea.plotArea.x, // We take the first Order Rate on the array for this. + y: orderBookChart.container.frame.height * 5 / 7 + }; + + /* We make the point relative to the current frame */ + + maxBasePoint = orderBookChart.container.frame.frameThisPoint(maxBasePoint); + minBasePoint = orderBookChart.container.frame.frameThisPoint(minBasePoint); + + /* We add the possible displacement */ + + maxBasePoint = orderBookChart.container.displacement.displaceThisPoint(maxBasePoint); + minBasePoint = orderBookChart.container.displacement.displaceThisPoint(minBasePoint); + + /* We apply the zoom factor. */ + + minBasePoint = orderBookChart.container.zoom.zoomThisPoint(minBasePoint); + maxBasePoint = orderBookChart.container.zoom.zoomThisPoint(maxBasePoint); + + /* Lets start the drawing. */ + + browserCanvasContext.beginPath(); + browserCanvasContext.moveTo(minBasePoint.x, minBasePoint.y); + + var sumY = 0; + + for (var i = 0; i < orderBookAsksChartPoints.length; i++) { + + sumY = sumY + orderBookAsksChartPoints[i][1]; + + var point = { + x: (orderBookAsksChartPoints[i][0] - orderBookChart.plotArea.min.x) * orderBookChart.plotArea.plotArea.x, + y: orderBookChart.container.frame.height * 5 / 7 - sumY + }; + + /* Frame the point */ + + point = orderBookChart.container.frame.frameThisPoint(point); + + /* We add the possible displacement */ + + point = orderBookChart.container.displacement.displaceThisPoint(point); + + /* We apply the zoom factor. */ + + point = orderBookChart.container.zoom.zoomThisPoint(point); + + browserCanvasContext.lineTo(point.x, point.y); + + } + + browserCanvasContext.lineTo(maxBasePoint.x, maxBasePoint.y); + browserCanvasContext.lineTo(minBasePoint.x, minBasePoint.y); + browserCanvasContext.closePath(); + browserCanvasContext.fillStyle = 'rgba(180, 130, 30, 0.75)'; + browserCanvasContext.fill(); + browserCanvasContext.strokeStyle = 'rgba(230, 30, 30, 0.75)'; + browserCanvasContext.lineWidth = 1; + browserCanvasContext.stroke(); + + browserCanvasContext.closePath(); + } + + } + + + function drawXAxis() { + + var step = (orderBookChart.plotArea.max.x - orderBookChart.plotArea.min.x) / 20; + + var decimals; + + /* Lets identify how many decimals are needed to be displayed. To do that we will chekck when is the first time that two consecutives points in the plotArea are different. */ + + for (var j = 0; j < 10; j++) { + + var diff = (orderBookChart.plotArea.min.x + step * 2).toFixed(j) - (orderBookChart.plotArea.min.x + step * 1).toFixed(j); + + if (diff !== 0 && decimals === undefined) { + decimals = j; + } + } + + for (var i = 1; i < 20; i++) { + + var point = { + x: orderBookChart.plotArea.min.x + step * i, + y: orderBookChart.container.frame.height * 6 / 7 + }; + + + browserCanvasContext.strokeStyle = 'rgba(10, 103, 201, 1)'; + + var fontSize = 10;// + orderBookChart.container.zoom.level(); + + browserCanvasContext.font = fontSize + 'px verdana'; + + /* Se the label */ + + var label = point.x.toFixed(decimals); + + /* Now we transform x on the actual coordinate on the canvas. */ + + point.x = (point.x - orderBookChart.plotArea.min.x) * orderBookChart.plotArea.scale.x; // Now x represent a coordinate on the canvas. + + point = orderBookChart.container.frame.frameThisPoint(point); + point = orderBookChart.container.displacement.displaceThisPoint(point); + point = orderBookChart.container.zoom.zoomThisPoint(point); + + browserCanvasContext.fillText(label, point.x, point.y); + + } + } + + + function calculateOrdersSum(pointsArray) { + + /* In this chart we represent the cummulative sum of all orders. That means that every time a new order arrives, we need to recalculate this. */ + + var sum = 0; + + for (var i = 0; i < pointsArray.length; i++) { + + sum = sum + pointsArray[i][1]; + pointsArray[i][2] = sum; + + } + } + + + /* + + FUNCTIONS RELATED TO "NEW ORDERS" + + */ + + function removeOrder(order) { + /* + + Here we will remove the order from the chart to be ploted. The chart has 2 parts: bids and asks, so we need to know which part to remove it from. + + */ + + if (order.type === 'Bid') { + + removeOrderFromPointArray(order, orderBookBidsChartPoints); + calculateOrdersSum(orderBookBidsChartPoints); // We sum all the amounts into the 3rd column of the array. + + } else { + + removeOrderFromPointArray(order, orderBookAsksChartPoints); + calculateOrdersSum(orderBookAsksChartPoints); // We sum all the amounts into the 3rd column of the array. + + } + } + + + function removeOrderFromPointArray(order, pointsArray) { + + /* To remove an order, we simply scan the array received and when we find the same order rate, we substract the amount, and thats it. */ + + for (var i = 0; i < pointsArray.length; i++) { + + if (pointsArray[i][0] === order.rate) { // We find the item in the pointArray where the rate is the same as the orders rate. + + pointsArray[i][1] = pointsArray[i][1] - order.amountA; + + if (pointsArray[i][1] === 0) { // If we reach zero, then there is no reason to keep this element in the array. + + pointsArray.splice(i, 1); // This means remove this item from the array. + + } + + } + + } + + } + + + function addOrder(order) { + + /* + + This is the first function executed when a new order arrives. Here we still don't know if it is an ASK or BID. From here on we + do all the things needed to incorporate this new order to the chart. + + */ + + + var pointIndex; + + if (order.type === 'Bid') { + + pointIndex = addNewBidOrder(order); // We add the new order to the array, if necesary. + calculateOrdersSum(orderBookBidsChartPoints); // We sum all the amounts into the 3rd column of the array. + + //if (pointIndex >= 0) { // < 0 means that the amount of the order was summed into an existing item of the array. + + // var ball; + + // ball = addNewLabelBall(pointIndex, order.type, order.rate, 'rgba(30, 130, 30, 0.65)'); // We add a new ball. + // orderBookBidsChartPoints[pointIndex][3] = ball; + // updateLabelBallsLData(orderBookBidsChartPoints); // We update the labels of the balls we the new values, resulting of the sum calculation. + //} + + } else { + + pointIndex = addNewAskOrder(order); + calculateOrdersSum(orderBookAsksChartPoints); // We sum all the amounts into the 3rd column of the array. + + //if (pointIndex >= 0) { // < 0 means that the amount of the order was summed into an existing item of the array. + + // var ball; + + // ball = addNewLabelBall(pointIndex, order.type, order.rate, 'rgba(130, 30, 30, 0.65)'); // We add a new ball. + // orderBookAsksChartPoints[pointIndex][3] = ball; + // updateLabelBallsLData(orderBookAsksChartPoints); + //} + } + + + } + + + function addNewBidOrder(order) { + + /* + + Everytime a new order arrives, we need to add it to the array that contains all the chart points for this type of order. + To add it, we need to follow certain rules, that are defined by this type of chart. This is what this function does for BID orders. + + */ + + if (orderBookBidsChartPoints.length === 0) { + + orderBookBidsChartPoints.push([order.rate, order.amountA, 0, undefined]); // There it goes the first element of the order book. + return 0; + + } else { + + for (var i = 0; i < orderBookBidsChartPoints.length; i++) { + + try { + if (order.rate < orderBookBidsChartPoints[i][0] && order.rate > orderBookBidsChartPoints[i + 1][0]) { + + orderBookBidsChartPoints.splice(i + 1, 0, [order.rate, order.amountA, 0, undefined]); + return i + 1; + } + } catch (err) { + // It is fine if that does not work. It might be because the index i+1 does not exist. + } + + if (order.rate === orderBookBidsChartPoints[i][0]) { + + /* Orders with the exact same rate that previous orders on the order book, are added to the same item of the arrray. */ + + orderBookBidsChartPoints[i][1] = orderBookBidsChartPoints[i][1] + order.rate; + + return -1; // This means that no new item was added. + + } else { + + if (order.rate > orderBookBidsChartPoints[i][0] && i === 0) { + + orderBookBidsChartPoints.splice(i, 0, [order.rate, order.amountA, 0, undefined]); + return i; + + } else { + + if (order.rate < orderBookBidsChartPoints[i][0] && i === orderBookBidsChartPoints.length - 1) { + + orderBookBidsChartPoints.push([order.rate, order.amountA, 0, undefined]); + return orderBookBidsChartPoints.length - 1; + } + } + } + } + } + } + + + function addNewAskOrder(order) { + + /* + + Everytime a new order arrives, we need to add it to the array that contains all the chart points for this type of order. + To add it, we need to follow certain rules, that are defined by this type of chart. This is what this function does for ASK orders. + + */ + + if (orderBookAsksChartPoints.length === 0) { + + orderBookAsksChartPoints.push([order.rate, order.amountA, 0, undefined]); // There it goes the first element of the order book. + return 0; + + } else { + + for (var i = 0; i < orderBookAsksChartPoints.length; i++) { + + try { + if (order.rate > orderBookAsksChartPoints[i][0] && order.rate < orderBookAsksChartPoints[i + 1][0]) { + + orderBookAsksChartPoints.splice(i + 1, 0, [order.rate, order.amountA, 0, undefined]); + return i + 1; + } + } catch (err) { + // It is fine if that does not work. It might be because the index i+1 does not exist. + } + + + if (order.rate === orderBookAsksChartPoints[i][0]) { + + /* Orders with the exact same rate that previous orders on the order book, are added to the same item of the arrray. */ + + orderBookAsksChartPoints[i][1] = orderBookAsksChartPoints[i][1] + order.rate; + return -1; + + } else { + + if (order.rate < orderBookAsksChartPoints[i][0]) { + + orderBookAsksChartPoints.splice(i, 0, [order.rate, order.amountA, 0, undefined]); + return i; + + } else { + + if (order.rate > orderBookAsksChartPoints[i][0] && i === orderBookAsksChartPoints.length - 1) { + + orderBookAsksChartPoints.push([order.rate, order.amountA, 0, undefined]); + return orderBookAsksChartPoints.length - 1; + } + } + } + } + } + } + + + /* + + FUNCTIONS RELATED TO "THE ANIMATION" + + */ + + function draw() { + + /* This function is meant to be executed to draw the whole chart, for instance, as part of some animation loop. */ + + this.container.frame.draw(); + + if (orderBookChart.isInitialized === true) { + + drawOrderBookChart(); + + drawXAxis(); + } + + } + + /* + + FUNCTIONS RELATED TO "ORDER BALLS" + + */ + + function transformRecivedOrdersIntoOrderBalls() { + + /* + + Every new order we receive, we automatically transform it into a new ball. That doesnt mean that the ball is going to be + displayed right away, but anyway we have it ready for that. + + */ + + var totalRows = Object.keys(orderBookChart.orderBook.server.orderBookData.resultSet).length; + + for (var i = 1; i <= totalRows; i++) { + + var row = orderBookChart.orderBook.server.orderBookData.resultSet["row " + i]; + + /* We move the information from the result set to an order object, and use that from now on. */ + + var order = newOrder(undefined, row.Type, row.Rate, row.AmountA, row.AmountB, undefined, undefined); + + /* Position of the ball without displacement or zoon. */ + + var rawPosition = { + x: (order.rate - orderBookChart.plotArea.min.x) * orderBookChart.plotArea.scale.x, + y: orderBookChart.container.frame.height * 5 / 7 + }; + + + var ball = newBall(); + + ball.container = orderBookChart.container; + + + ball.radomizeCurrentSpeed(); + + ball.friction = .995; + + ball.rawPosition = rawPosition; + + ball.initializeMass(order.amountA * 5); + ball.initializeRadius(order.amountA * 3); + ball.initializeTargetPosition(); + + ball.radomizeCurrentPosition(ball.targetPosition); + + ball.linkedObject = order; + ball.linkedObjectType = "order"; + + if (order.type === 'Bid') { + ball.fillStyle = 'rgba(102, 204, 0, 0.75)'; + } else { + ball.fillStyle = 'rgba(222, 105, 42, 0.75)'; + } + + if (order.type === 'Bid') { + ball.labelStrokeStyle = 'rgba(255, 255, 255, 1)'; + } else { + ball.labelStrokeStyle = 'rgba(255, 255, 255, 1)'; + } + + ball.labelFirstText = order.amountA; + ball.labelSecondText = order.rate; + + + + ballsOrdersQueue.push(ball); + } + } + + /* + + FUNCTIONS RELATED TO "LABEL BALLS" + + */ + + function addNewLabelBall(id, type, rate, fillStyle) { + + /* This function is used to create new balls, representing labels of each point on the chart. */ + + var rawPosition = { + x: (rate - orderBookChart.plotArea.min.x) * orderBookChart.plotArea.scale.x, + y: orderBookChart.container.frame.height * 5 / 7 + }; + + var ball = newBall(); + + ball.container = orderBookChart.container; + + ball.radomizeCurrentPosition(rawPosition); + ball.radomizeCurrentSpeed(); + + ball.friction = .995; + + ball.rawPosition = rawPosition; + + ball.initializeMass(15); + ball.initializeRadius(3); + ball.initializeTargetPosition(); + + + ball.fillStyle = fillStyle; + ball.labelStrokeStyle = 'rgba(255, 255, 255, 1)'; + + ball.labelFirstText = 0; //.toFixed(4); + ball.labelSecondText = rate.toFixed(8); + + + balls.push(ball); + + return ball; + } + + + function updateLabelBallsLData(pointsArray) { + + /* When a new order arrives to the chart, all labels of balls representing points in the chart must be updated. */ + + var ball; + var sum; + + for (var i = 0; i < pointsArray.length; i++) { + + /* We update the balls label. */ + + ball = pointsArray[i][3]; + sum = pointsArray[i][2]; + + ball.labelFirstText = sum.toFixed(2); + + /* We update the balls raw position. */ + + var rawPositionY = orderBookChart.container.frame.height * 5 / 7 - sum; + + ball.rawPosition.y = rawPositionY; + } + } + + +} +