Created
June 13, 2018 20:15
-
-
Save kylemart/30c2b464b6447d9511515900f051b4dd to your computer and use it in GitHub Desktop.
This file has been truncated, but you can view the full file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Azure Storage JavaScript Client Library 2.9.100-preview | |
// Copyright (c) Microsoft and contributors. All rights reserved. | |
/* eslint-disable */ | |
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}(g.AzureStorage || (g.AzureStorage = {})).Blob = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[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. | |
// | |
module.exports.generateDevelopmentStorageCredentials = function (proxyUri) { | |
var devStore = 'UseDevelopmentStorage=true;'; | |
if(proxyUri){ | |
devStore += 'DevelopmentStorageProxyUri=' + proxyUri; | |
} | |
return devStore; | |
}; | |
var BlobService = require('../lib/services/blob/blobservice.browser'); | |
module.exports.BlobService = BlobService; | |
module.exports.BlobUtilities = require('../lib/services/blob/blobutilities'); | |
module.exports.createBlobService = function (storageAccountOrConnectionString, storageAccessKey, host) { | |
return new BlobService(storageAccountOrConnectionString, storageAccessKey, host, null); | |
}; | |
module.exports.createBlobServiceWithSas = function (host, sasToken) { | |
return new BlobService(null, null, host, sasToken); | |
}; | |
module.exports.createBlobServiceWithTokenCredential = function (host, tokenCredential) { | |
return new BlobService(null, null, host, null, null, tokenCredential); | |
}; | |
module.exports.createBlobServiceAnonymous = function (host) { | |
return new BlobService(null, null, host, null); | |
}; | |
var azureCommon = require('../lib/common/common.browser'); | |
var StorageServiceClient = azureCommon.StorageServiceClient; | |
var SharedKey = azureCommon.SharedKey; | |
module.exports.generateAccountSharedAccessSignature = function(storageAccountOrConnectionString, storageAccessKey, sharedAccessAccountPolicy) | |
{ | |
var storageSettings = StorageServiceClient.getStorageSettings(storageAccountOrConnectionString, storageAccessKey); | |
var sharedKey = new SharedKey(storageSettings._name, storageSettings._key); | |
return sharedKey.generateAccountSignedQueryString(sharedAccessAccountPolicy); | |
}; | |
module.exports.Constants = azureCommon.Constants; | |
module.exports.StorageUtilities = azureCommon.StorageUtilities; | |
module.exports.AccessCondition = azureCommon.AccessCondition; | |
module.exports.SR = azureCommon.SR; | |
module.exports.StorageServiceClient = StorageServiceClient; | |
module.exports.Logger = azureCommon.Logger; | |
module.exports.WebResource = azureCommon.WebResource; | |
module.exports.Validate = azureCommon.validate; | |
module.exports.date = azureCommon.date; | |
module.exports.TokenCredential = azureCommon.TokenCredential; | |
// Other filters | |
module.exports.LinearRetryPolicyFilter = azureCommon.LinearRetryPolicyFilter; | |
module.exports.ExponentialRetryPolicyFilter = azureCommon.ExponentialRetryPolicyFilter; | |
module.exports.RetryPolicyFilter = azureCommon.RetryPolicyFilter; | |
},{"../lib/common/common.browser":2,"../lib/services/blob/blobservice.browser":40,"../lib/services/blob/blobutilities":42}],2:[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":3,"./streams/browserfilereadstream":24}],3:[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'); | |
exports.TokenCredential = require('./models/tokencredential'); | |
// 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":4,"./filters/exponentialretrypolicyfilter":6,"./filters/linearretrypolicyfilter":7,"./filters/retrypolicyfilter":8,"./http/webresource":9,"./models/aclresult":11,"./models/servicepropertiesresult":12,"./models/servicestatsparser":13,"./models/tokencredential":14,"./services/storageserviceclient":17,"./signing/sharedaccesssignature":20,"./signing/sharedkey":21,"./streams/batchoperation":23,"./streams/bufferstream":25,"./streams/chunkallocator":26,"./streams/chunkstream":27,"./streams/chunkstreamwithstream":28,"./streams/speedsummary":30,"./util/accesscondition":31,"./util/constants":32,"./util/date":33,"./util/iso8061date":34,"./util/patch-xmlbuilder":35,"./util/sr":36,"./util/storageutilities":37,"./util/util":38,"./util/validate":39,"xml2js":312,"xmlbuilder":315}],4:[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. | |
// | |
/** | |
* Creates a new Logger object | |
* @class | |
* The Logger class is used to write log information. | |
* | |
* @constructor | |
* | |
* @param {string} [level] The log level. Refer to Logger.LogLevels. | |
* @param {object} [loggerFunction] The function to write log information. | |
*/ | |
function Logger(level, loggerFunction) { | |
/** | |
* The log level. Refer to the Logger.LogLevels for available log levels. | |
* @name Logger#level | |
* @type {string} | |
* @see Logger.LogLevels | |
*/ | |
this.level = level; | |
this.loggerFunction = loggerFunction; | |
if (!this.loggerFunction) { | |
this.loggerFunction = this.defaultLoggerFunction; | |
} | |
} | |
/** | |
* The available log levels. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
Logger.LogLevels = { | |
/** | |
* System is unusable. | |
*/ | |
EMERGENCY: 'emergency', | |
/** | |
* Action must be taken immediately. | |
*/ | |
ALERT : 'alert', | |
/** | |
* Critical condition. | |
*/ | |
CRITICAL : 'critical', | |
/** | |
* Error condition. | |
*/ | |
ERROR : 'error', | |
/** | |
* Warning condition. | |
*/ | |
WARNING : 'warning', | |
/** | |
* Normal but significant condition. | |
*/ | |
NOTICE : 'notice', | |
/** | |
* Purely informational message. | |
*/ | |
INFO : 'info', | |
/** | |
* Application debug messages. | |
*/ | |
DEBUG : 'debug' | |
}; | |
Logger.logPriority = [ | |
Logger.LogLevels.EMERGENCY, | |
Logger.LogLevels.ALERT, | |
Logger.LogLevels.CRITICAL, | |
Logger.LogLevels.ERROR, | |
Logger.LogLevels.WARNING, | |
Logger.LogLevels.NOTICE, | |
Logger.LogLevels.INFO, | |
Logger.LogLevels.DEBUG | |
]; | |
Logger.prototype.log = function (level, msg) { | |
this.loggerFunction(level, msg); | |
}; | |
Logger.prototype.emergency = function(msg) { | |
this.log(Logger.LogLevels.EMERGENCY, msg); | |
}; | |
Logger.prototype.critical = function(msg) { | |
this.log(Logger.LogLevels.CRITICAL, msg); | |
}; | |
Logger.prototype.alert = function(msg) { | |
this.log(Logger.LogLevels.ALERT, msg); | |
}; | |
Logger.prototype.error = function(msg) { | |
this.log(Logger.LogLevels.ERROR, msg); | |
}; | |
Logger.prototype.warn = function(msg) { | |
this.log(Logger.LogLevels.WARNING, msg); | |
}; | |
Logger.prototype.notice = function(msg) { | |
this.log(Logger.LogLevels.NOTICE, msg); | |
}; | |
Logger.prototype.info = function(msg) { | |
this.log(Logger.LogLevels.INFO, msg); | |
}; | |
Logger.prototype.debug = function(msg) { | |
this.log(Logger.LogLevels.DEBUG, msg); | |
}; | |
Logger.prototype.defaultLoggerFunction = function(logLevel , msg) { | |
var currentLevelIndex = Logger.logPriority.indexOf(this.level); | |
var logLevelIndex = Logger.logPriority.indexOf(logLevel); | |
var time = new Date(); | |
var timeStamp = time.toISOString(); | |
if (logLevelIndex <= currentLevelIndex) { | |
console.log('[' + timeStamp + ']' + this.level + ' : ' + msg); | |
} | |
}; | |
module.exports = Logger; | |
},{}],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. | |
// | |
// Module dependencies. | |
var util = require('util'); | |
var _ = require('underscore'); | |
function captureStackTrace(targetObject, constructorOpt) { | |
if (Error.captureStackTrace) { | |
Error.captureStackTrace(targetObject, constructorOpt); | |
} | |
} | |
function ArgumentError(argumentName, message) { | |
captureStackTrace(this, this.constructor); | |
this.name = this.constructor.name; | |
this.argumentName = argumentName; | |
this.message = message || util.format('Invalid or missing argument supplied: %s', argumentName); | |
} | |
util.inherits(ArgumentError, Error); | |
function ArgumentNullError(argumentName, message) { | |
captureStackTrace(this, this.constructor); | |
this.name = this.constructor.name; | |
this.argumentName = argumentName; | |
this.message = message || util.format('Missing argument: %s', argumentName); | |
} | |
util.inherits(ArgumentNullError, Error); | |
function StorageError(message, properties) { | |
captureStackTrace(this, this.constructor); | |
this.name = this.constructor.name; | |
this.message = message; | |
if(properties){ | |
_.extend(this, properties); | |
} | |
} | |
util.inherits(StorageError, Error); | |
function TimeoutError(message) { | |
captureStackTrace(this, this.constructor); | |
this.name = this.constructor.name; | |
this.message = message; | |
} | |
util.inherits(TimeoutError, Error); | |
module.exports.ArgumentError = ArgumentError; | |
module.exports.ArgumentNullError = ArgumentNullError; | |
module.exports.StorageError = StorageError; | |
module.exports.TimeoutError = TimeoutError; | |
module.exports.captureStackTrace = captureStackTrace; | |
},{"underscore":228,"util":234}],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. | |
// | |
'use strict'; | |
var RetryPolicyFilter = require('./retrypolicyfilter'); | |
/** | |
* Creates a new 'ExponentialRetryPolicyFilter' instance. | |
* @class | |
* The ExponentialRetryPolicyFilter allows you to retry operations, | |
* using an exponential back-off interval between retries. | |
* To apply a filter to service operations, use `withFilter` | |
* and specify the filter to be used when creating a service. | |
* @constructor | |
* @param {number} [retryCount=3] The client retry count. | |
* @param {number} [retryInterval=30000] The client retry interval, in milliseconds. | |
* @param {number} [minRetryInterval=3000] The minimum retry interval, in milliseconds. | |
* @param {number} [maxRetryInterval=90000] The maximum retry interval, in milliseconds. | |
* | |
* @example | |
* var azure = require('azure-storage'); | |
* var retryOperations = new azure.ExponentialRetryPolicyFilter(); | |
* var blobService = azure.createBlobService().withFilter(retryOperations) | |
*/ | |
function ExponentialRetryPolicyFilter(retryCount, retryInterval, minRetryInterval, maxRetryInterval) { | |
this.retryCount = retryCount ? retryCount : ExponentialRetryPolicyFilter.DEFAULT_CLIENT_RETRY_COUNT; | |
this.retryInterval = retryInterval ? retryInterval : ExponentialRetryPolicyFilter.DEFAULT_CLIENT_RETRY_INTERVAL; | |
this.minRetryInterval = minRetryInterval ? minRetryInterval : ExponentialRetryPolicyFilter.DEFAULT_CLIENT_MIN_RETRY_INTERVAL; | |
this.maxRetryInterval = maxRetryInterval ? maxRetryInterval : ExponentialRetryPolicyFilter.DEFAULT_CLIENT_MAX_RETRY_INTERVAL; | |
} | |
/** | |
* Represents the default client retry interval, in milliseconds. | |
*/ | |
ExponentialRetryPolicyFilter.DEFAULT_CLIENT_RETRY_INTERVAL = 1000 * 30; | |
/** | |
* Represents the default client retry count. | |
*/ | |
ExponentialRetryPolicyFilter.DEFAULT_CLIENT_RETRY_COUNT = 3; | |
/** | |
* Represents the default maximum retry interval, in milliseconds. | |
*/ | |
ExponentialRetryPolicyFilter.DEFAULT_CLIENT_MAX_RETRY_INTERVAL = 1000 * 90; | |
/** | |
* Represents the default minimum retry interval, in milliseconds. | |
*/ | |
ExponentialRetryPolicyFilter.DEFAULT_CLIENT_MIN_RETRY_INTERVAL = 1000 * 3; | |
/** | |
* Determines if the operation should be retried and how long to wait until the next retry. | |
* | |
* @param {number} statusCode The HTTP status code. | |
* @param {object} requestOptions The request options. | |
* @return {retryInfo} Information about whether the operation qualifies for a retry and the retryInterval. | |
*/ | |
ExponentialRetryPolicyFilter.prototype.shouldRetry = function (statusCode, requestOptions) { | |
var retryData = (requestOptions && requestOptions.retryContext) ? requestOptions.retryContext : {}; | |
// Adjust retry interval | |
var incrementDelta = Math.pow(2, retryData.retryCount) - 1; | |
var boundedRandDelta = this.retryInterval * 0.8 + Math.floor(Math.random() * (this.retryInterval * 1.2 - this.retryInterval * 0.8)); | |
incrementDelta *= boundedRandDelta; | |
retryData.retryInterval = Math.min(this.minRetryInterval + incrementDelta, this.maxRetryInterval); | |
return RetryPolicyFilter._shouldRetryOnError(statusCode, requestOptions); | |
}; | |
/** | |
* Handles an operation with an exponential retry policy. | |
* | |
* @param {Object} requestOptions The original request options. | |
* @param {function} next The next filter to be handled. | |
*/ | |
ExponentialRetryPolicyFilter.prototype.handle = function (requestOptions, next) { | |
RetryPolicyFilter._handle(this, requestOptions, next); | |
}; | |
module.exports = ExponentialRetryPolicyFilter; | |
},{"./retrypolicyfilter":8}],7:[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 RetryPolicyFilter = require('./retrypolicyfilter'); | |
/** | |
* Creates a new LinearRetryPolicyFilter instance. | |
* @class | |
* The LinearRetryPolicyFilter allows you to retry operations, | |
* using an linear back-off interval between retries. | |
* To apply a filter to service operations, use `withFilter` | |
* and specify the filter to be used when creating a service. | |
* @constructor | |
* @param {number} [retryCount=3] The client retry count. | |
* @param {number} [retryInterval=30000] The client retry interval, in milliseconds. | |
* | |
* @example | |
* var azure = require('azure-storage'); | |
* var retryOperations = new azure.LinearRetryPolicyFilter(); | |
* var blobService = azure.createBlobService().withFilter(retryOperations) | |
*/ | |
function LinearRetryPolicyFilter(retryCount, retryInterval) { | |
this.retryCount = retryCount ? retryCount : LinearRetryPolicyFilter.DEFAULT_CLIENT_RETRY_COUNT; | |
this.retryInterval = retryInterval ? retryInterval : LinearRetryPolicyFilter.DEFAULT_CLIENT_RETRY_INTERVAL; | |
} | |
/** | |
* Represents the default client retry interval, in milliseconds. | |
*/ | |
LinearRetryPolicyFilter.DEFAULT_CLIENT_RETRY_INTERVAL = 1000 * 30; | |
/** | |
* Represents the default client retry count. | |
*/ | |
LinearRetryPolicyFilter.DEFAULT_CLIENT_RETRY_COUNT = 3; | |
/** | |
* Determines if the operation should be retried and how long to wait until the next retry. | |
* | |
* @param {number} statusCode The HTTP status code. | |
* @param {object} requestOptions The request options. | |
* @return {retryInfo} Information about whether the operation qualifies for a retry and the retryInterval. | |
*/ | |
LinearRetryPolicyFilter.prototype.shouldRetry = function (statusCode, requestOptions) { | |
var retryData = (requestOptions && requestOptions.retryContext) ? requestOptions.retryContext : {}; | |
retryData.retryInterval = this.retryInterval; | |
return RetryPolicyFilter._shouldRetryOnError(statusCode, requestOptions); | |
}; | |
/** | |
* Handles an operation with a linear retry policy. | |
* | |
* @param {Object} requestOptions The original request options. | |
* @param {function} next The next filter to be handled. | |
*/ | |
LinearRetryPolicyFilter.prototype.handle = function (requestOptions, next) { | |
RetryPolicyFilter._handle(this, requestOptions, next); | |
}; | |
module.exports = LinearRetryPolicyFilter; | |
},{"./retrypolicyfilter":8}],8:[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 azureutil = require('../util/util'); | |
var Constants = require('../util/constants'); | |
var StorageUtilities = require('../util/storageutilities'); | |
var extend = require('util')._extend; | |
/** | |
* Creates a new RetryPolicyFilter instance. | |
* @class | |
* The RetryPolicyFilter allows you to retry operations, | |
* using a custom retry policy. Users are responsible to | |
* define the shouldRetry method. | |
* To apply a filter to service operations, use `withFilter` | |
* and specify the filter to be used when creating a service. | |
* @constructor | |
* @param {number} [retryCount=30000] The client retry count. | |
* @param {number} [retryInterval=3] The client retry interval, in milliseconds. | |
* | |
* @example | |
* var azure = require('azure-storage'); | |
* var retryPolicy = new azure.RetryPolicyFilter(); | |
* retryPolicy.retryCount = 3; | |
* retryPolicy.retryInterval = 3000; | |
* retryPolicy.shouldRetry = function(statusCode, retryContext) { | |
* | |
* }; | |
* var blobService = azure.createBlobService().withFilter(retryPolicy); | |
*/ | |
function RetryPolicyFilter(retryCount, retryInterval) { | |
this.retryCount = retryCount ? retryCount : RetryPolicyFilter.DEFAULT_CLIENT_RETRY_COUNT; | |
this.retryInterval = retryInterval ? retryInterval : RetryPolicyFilter.DEFAULT_CLIENT_RETRY_INTERVAL; | |
} | |
/** | |
* Represents the default client retry interval, in milliseconds. | |
*/ | |
RetryPolicyFilter.DEFAULT_CLIENT_RETRY_INTERVAL = 1000 * 30; | |
/** | |
* Represents the default client retry count. | |
*/ | |
RetryPolicyFilter.DEFAULT_CLIENT_RETRY_COUNT = 3; | |
/** | |
* Handles an operation with a retry policy. | |
* | |
* @param {Object} requestOptions The original request options. | |
* @param {function} next The next filter to be handled. | |
*/ | |
RetryPolicyFilter.prototype.handle = function (requestOptions, next) { | |
RetryPolicyFilter._handle(this, requestOptions, next); | |
}; | |
/** | |
* Handles an operation with a retry policy. | |
* | |
* @param {Object} requestOptions The original request options. | |
* @param {function} next The next filter to be handled. | |
*/ | |
RetryPolicyFilter._handle = function (self, requestOptions, next) { | |
var retryRequestOptions = extend({}, requestOptions); | |
retryRequestOptions.retryInterval = 0; | |
// Initialize retryContext because that will be passed to the shouldRetry method which users will implement | |
retryRequestOptions.retryContext = { | |
retryCount: 0, | |
error: null, | |
retryInterval: retryRequestOptions.retryInterval, | |
locationMode: retryRequestOptions.locationMode, | |
currentLocation: retryRequestOptions.currentLocation | |
}; | |
var lastPrimaryAttempt; | |
var lastSecondaryAttempt; | |
var operation = function () { | |
// retry policies dont really do anything to the request options | |
// so move on to next | |
if (next) { | |
next(retryRequestOptions, function (returnObject, finalCallback, nextPostCallback) { | |
// Previous operation ended so update the retry data | |
if (returnObject.error) { | |
if (retryRequestOptions.retryContext.error) { | |
returnObject.error.innerError = retryRequestOptions.retryContext.error; | |
} | |
retryRequestOptions.retryContext.error = returnObject.error; | |
} | |
// If a request sent to the secondary location fails with 404 (Not Found), it is possible | |
// that the resource replication is not finished yet. So, in case of 404 only in the secondary | |
// location, the failure should still be retryable. | |
var secondaryNotFound = (retryRequestOptions.currentLocation === Constants.StorageLocation.SECONDARY) && ((returnObject.response && returnObject.response.statusCode === 404) || (returnObject.error && returnObject.error.code === 'ENOTFOUND')); | |
var notExceedMaxRetryCount = retryRequestOptions.retryContext.retryCount ? retryRequestOptions.retryContext.retryCount <= self.retryCount : true; | |
var retryInfo = self.shouldRetry(secondaryNotFound ? 500 : (azureutil.objectIsNull(returnObject.response) ? 306 : returnObject.response.statusCode), retryRequestOptions); | |
retryRequestOptions.retryContext.retryCount++; | |
if (retryInfo.ignore) { | |
returnObject.error = null; | |
} | |
// If the custom retry logic(shouldRetry) does not return a targetLocation, calculate based on the previous location and locationMode. | |
if(azureutil.objectIsNull(retryInfo.targetLocation)) { | |
retryInfo.targetLocation = azureutil.getNextLocation(retryRequestOptions.currentLocation, retryRequestOptions.locationMode); | |
} | |
// If the custom retry logic(shouldRetry) does not return a retryInterval, try to set it to the value on the instance if it is available. Otherwise, the default(30000) will be used. | |
if(azureutil.objectIsNull(retryInfo.retryInterval)) { | |
retryInfo.retryInterval = self.retryInterval; | |
} | |
// Only in the case of success from server but client side failure like MD5 or length mismatch, returnObject.retryable has a value(we explicitly set it to false). | |
// In this case, we should not retry the request. | |
// If the output stream already get sent to server and get error back, | |
// we should NOT retry within the SDK as the stream data is not valid anymore if we retry directly. | |
if ( | |
!returnObject.outputStreamSent && returnObject.error && azureutil.objectIsNull(returnObject.retryable) && notExceedMaxRetryCount && | |
( | |
(!azureutil.objectIsNull(returnObject.response) && retryInfo.retryable) || | |
( | |
returnObject.error.code === 'ECONNREFUSED' || | |
returnObject.error.code === 'ETIMEDOUT' || | |
returnObject.error.code === 'ESOCKETTIMEDOUT' || | |
returnObject.error.code === 'ECONNRESET' || | |
returnObject.error.code === 'EAI_AGAIN' || | |
returnObject.error.message === 'XHR error' // stream-http XHR network error message in browsers | |
) | |
) | |
) { | |
if (retryRequestOptions.currentLocation === Constants.StorageLocation.PRIMARY) { | |
lastPrimaryAttempt = returnObject.operationEndTime; | |
} else { | |
lastSecondaryAttempt = returnObject.operationEndTime; | |
} | |
// Moreover, in case of 404 when trying the secondary location, instead of retrying on the | |
// secondary, further requests should be sent only to the primary location, as it most | |
// probably has a higher chance of succeeding there. | |
if (secondaryNotFound && (retryRequestOptions.locationMode !== StorageUtilities.LocationMode.SECONDARY_ONLY)) | |
{ | |
retryInfo.locationMode = StorageUtilities.LocationMode.PRIMARY_ONLY; | |
retryInfo.targetLocation = Constants.StorageLocation.PRIMARY; | |
} | |
// Now is the time to calculate the exact retry interval. ShouldRetry call above already | |
// returned back how long two requests to the same location should be apart from each other. | |
// However, for the reasons explained above, the time spent between the last attempt to | |
// the target location and current time must be subtracted from the total retry interval | |
// that ShouldRetry returned. | |
var lastAttemptTime = retryInfo.targetLocation === Constants.StorageLocation.PRIMARY ? lastPrimaryAttempt : lastSecondaryAttempt; | |
if (!azureutil.objectIsNull(lastAttemptTime)) { | |
var sinceLastAttempt = new Date().getTime() - lastAttemptTime.getTime(); | |
if (sinceLastAttempt < 0) { | |
sinceLastAttempt = 0; | |
} | |
retryRequestOptions.retryInterval = retryInfo.retryInterval - sinceLastAttempt; | |
} | |
else { | |
retryRequestOptions.retryInterval = 0; | |
} | |
if(!azureutil.objectIsNull(retryInfo.locationMode)) { | |
retryRequestOptions.locationMode = retryInfo.locationMode; | |
} | |
retryRequestOptions.currentLocation = retryInfo.targetLocation; | |
operation(); | |
} else { | |
if (nextPostCallback) { | |
nextPostCallback(returnObject); | |
} else if (finalCallback) { | |
finalCallback(returnObject); | |
} | |
} | |
}); | |
} | |
}; | |
operation(); | |
}; | |
RetryPolicyFilter._shouldRetryOnError = function (statusCode, requestOptions) { | |
var retryInfo = (requestOptions && requestOptions.retryContext) ? requestOptions.retryContext : {}; | |
// Non-timeout Cases | |
if (statusCode >= 300 && statusCode != 408) { | |
// Always no retry on "not implemented" and "version not supported" | |
if (statusCode == 501 || statusCode == 505) { | |
retryInfo.retryable = false; | |
return retryInfo; | |
} | |
// When absorbConditionalErrorsOnRetry is set (for append blob) | |
if (requestOptions && requestOptions.absorbConditionalErrorsOnRetry) { | |
if (statusCode == 412) { | |
// When appending block with precondition failure and their was a server error before, we ignore the error. | |
if (retryInfo.lastServerError) { | |
retryInfo.ignore = true; | |
retryInfo.retryable = true; | |
} else { | |
retryInfo.retryable = false; | |
} | |
} else if (retryInfo.retryable && statusCode >= 500 && statusCode < 600) { | |
// Retry on the server error | |
retryInfo.retryable = true; | |
retryInfo.lastServerError = true; | |
} | |
} else if (statusCode < 500) { | |
// No retry on the client error | |
retryInfo.retryable = false; | |
} | |
} | |
return retryInfo; | |
}; | |
module.exports = RetryPolicyFilter; | |
},{"../util/constants":32,"../util/storageutilities":37,"../util/util":38,"util":234}],9:[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. | |
// | |
// Module dependencies. | |
var azureutil = require('../util/util'); | |
var SR = require('../util/sr'); | |
var Constants = require('../util/constants'); | |
var errors = require('../errors/errors'); | |
var ArgumentError = errors.ArgumentError; | |
var HeaderConstants = Constants.HeaderConstants; | |
var HttpConstants = Constants.HttpConstants; | |
var HttpConstants = Constants.HttpConstants; | |
var HttpVerbs = HttpConstants.HttpVerbs; | |
function encodeSpecialCharacters(path) { | |
return path.replace(/'/g, '%27'); | |
} | |
/** | |
* Creates a new WebResource object. | |
* | |
* This class provides an abstraction over a REST call by being library / implementation agnostic and wrapping the necessary | |
* properties to initiate a request. | |
* | |
* @constructor | |
*/ | |
function WebResource() { | |
this.rawResponse = false; | |
this.queryString = {}; | |
} | |
/** | |
* Creates a new put request web resource. | |
* | |
* @param {string} path The path for the put operation. | |
* @return {WebResource} A new webresource with a put operation for the given path. | |
*/ | |
WebResource.put = function (path) { | |
var webResource = new WebResource(); | |
webResource.path = path ? encodeSpecialCharacters(path) : null; | |
webResource.method = HttpConstants.HttpVerbs.PUT; | |
return webResource; | |
}; | |
/** | |
* Creates a new get request web resource. | |
* | |
* @param {string} path The path for the get operation. | |
* @return {WebResource} A new webresource with a get operation for the given path. | |
*/ | |
WebResource.get = function (path) { | |
var webResource = new WebResource(); | |
webResource.path = path ? encodeSpecialCharacters(path) : null; | |
webResource.method = HttpConstants.HttpVerbs.GET; | |
return webResource; | |
}; | |
/** | |
* Creates a new head request web resource. | |
* | |
* @param {string} path The path for the head operation. | |
* @return {WebResource} A new webresource with a head operation for the given path. | |
*/ | |
WebResource.head = function (path) { | |
var webResource = new WebResource(); | |
webResource.path = path ? encodeSpecialCharacters(path) : null; | |
webResource.method = HttpConstants.HttpVerbs.HEAD; | |
return webResource; | |
}; | |
/** | |
* Creates a new delete request web resource. | |
* | |
* @param {string} path The path for the delete operation. | |
* @return {WebResource} A new webresource with a delete operation for the given path. | |
*/ | |
WebResource.del = function (path) { | |
var webResource = new WebResource(); | |
webResource.path = path ? encodeSpecialCharacters(path) : null; | |
webResource.method = HttpConstants.HttpVerbs.DELETE; | |
return webResource; | |
}; | |
/** | |
* Creates a new post request web resource. | |
* | |
* @param {string} path The path for the post operation. | |
* @return {WebResource} A new webresource with a post operation for the given path. | |
*/ | |
WebResource.post = function (path) { | |
var webResource = new WebResource(); | |
webResource.path = path ? encodeSpecialCharacters(path) : null; | |
webResource.method = HttpConstants.HttpVerbs.POST; | |
return webResource; | |
}; | |
/** | |
* Creates a new merge request web resource. | |
* | |
* @param {string} path The path for the merge operation. | |
* @return {WebResource} A new webresource with a merge operation for the given path. | |
*/ | |
WebResource.merge = function (path) { | |
var webResource = new WebResource(); | |
webResource.path = path ? encodeSpecialCharacters(path) : null; | |
webResource.method = HttpConstants.HttpVerbs.MERGE; | |
return webResource; | |
}; | |
/** | |
* Specifies a custom property in the web resource. | |
* | |
* @param {string} name The property name. | |
* @param {string} value The property value. | |
* @return {WebResource} The webresource. | |
*/ | |
WebResource.prototype.withProperty = function (name, value) { | |
if (!this.properties) { | |
this.properties = {}; | |
} | |
this.properties[name] = value; | |
return this; | |
}; | |
/** | |
* Specifies if the response should be parsed or not. | |
* | |
* @param {bool} rawResponse true if the response should not be parsed; false otherwise. | |
* @return {WebResource} The webresource. | |
*/ | |
WebResource.prototype.withRawResponse = function (rawResponse) { | |
this.rawResponse = rawResponse; | |
if (azureutil.objectIsNull(this.rawResponse)) { | |
this.rawResponse = true; | |
} | |
return this; | |
}; | |
WebResource.prototype.withHeadersOnly = function (headersOnly) { | |
if (headersOnly !== undefined) { | |
this.headersOnly = headersOnly; | |
} else { | |
this.headersOnly = true; | |
} | |
return this; | |
}; | |
/** | |
* Adds an optional query string parameter. | |
* | |
* @param {Object} name The name of the query string parameter. | |
* @param {Object} value The value of the query string parameter. | |
* @param {Object} defaultValue The default value for the query string parameter to be used if no value is passed. | |
* @return {Object} The web resource. | |
*/ | |
WebResource.prototype.withQueryOption = function (name, value, defaultValue) { | |
if (!azureutil.objectIsNull(value)) { | |
this.queryString[name] = value; | |
} else if (defaultValue) { | |
this.queryString[name] = defaultValue; | |
} | |
return this; | |
}; | |
/** | |
* Adds optional query string parameters. | |
* | |
* Additional arguments will be the needles to search in the haystack. | |
* | |
* @param {Object} object The haystack of query string parameters. | |
* @return {Object} The web resource. | |
*/ | |
WebResource.prototype.withQueryOptions = function (object) { | |
if (object) { | |
for (var i = 1; i < arguments.length; i++) { | |
if (object[arguments[i]]) { | |
this.withQueryOption(arguments[i], object[arguments[i]]); | |
} | |
} | |
} | |
return this; | |
}; | |
/** | |
* Adds an optional header parameter. | |
* | |
* @param {Object} name The name of the header parameter. | |
* @param {Object} value The value of the header parameter. | |
* @return {Object} The web resource. | |
*/ | |
WebResource.prototype.withHeader = function (name, value) { | |
if (!this.headers) { | |
this.headers = {}; | |
} | |
if (!azureutil.IsNullOrEmptyOrUndefinedOrWhiteSpace(value)) { | |
value = value instanceof Date ? value.toUTCString() : value; | |
this.headers[name] = value; | |
} | |
return this; | |
}; | |
/** | |
* Adds an optional body. | |
* | |
* @param {Object} body The request body. | |
* @return {Object} The web resource. | |
*/ | |
WebResource.prototype.withBody = function (body) { | |
this.body = body; | |
return this; | |
}; | |
/** | |
* Adds optional query string parameters. | |
* | |
* Additional arguments will be the needles to search in the haystack. | |
* | |
* @param {Object} object The haystack of headers. | |
* @return {Object} The web resource. | |
*/ | |
WebResource.prototype.withHeaders = function (object) { | |
if (object) { | |
for (var i = 1; i < arguments.length; i++) { | |
if (object[arguments[i]]) { | |
this.withHeader(arguments[i], object[arguments[i]]); | |
} | |
} | |
} | |
return this; | |
}; | |
WebResource.prototype.addOptionalMetadataHeaders = function (metadata) { | |
var self = this; | |
if (metadata) { | |
Object.keys(metadata).forEach(function (metadataKey) { | |
if (azureutil.IsNullOrEmptyOrUndefinedOrWhiteSpace(metadataKey)) { | |
throw new ArgumentError('metadata', SR.METADATA_KEY_INVALID); | |
} | |
var value = metadata[metadataKey]; | |
if (azureutil.IsNullOrEmptyOrUndefinedOrWhiteSpace(value)) { | |
throw new ArgumentError('metadata', SR.METADATA_VALUE_INVALID); | |
} | |
var metadataHeaderName = HeaderConstants.PREFIX_FOR_STORAGE_METADATA + metadataKey; | |
var existingMetadataHeaderName = ''; | |
var headers = self.headers ? self.headers : {}; | |
if (Object.keys(headers).some(function (headerName) { | |
existingMetadataHeaderName = headerName; | |
return headerName.toString().toLowerCase() === metadataHeaderName.toLowerCase(); | |
})) { | |
self.withHeader(existingMetadataHeaderName, self.headers[existingMetadataHeaderName] + ',' + value); | |
} else { | |
self.withHeader(metadataHeaderName, value); | |
} | |
}); | |
} | |
return this; | |
}; | |
/** | |
* Determines if a status code corresponds to a valid response according to the WebResource's expected status codes. | |
* | |
* @param {int} statusCode The response status code. | |
* @return true if the response is valid; false otherwise. | |
*/ | |
WebResource.validResponse = function (statusCode) { | |
if (statusCode >= 200 && statusCode < 300) { | |
return true; | |
} | |
return false; | |
}; | |
function isMethodWithBody(verb) { | |
return verb === HttpVerbs.PUT || | |
verb === HttpVerbs.POST || | |
verb === HttpVerbs.MERGE; | |
} | |
/** | |
* Hook up the given input stream to a destination output stream if the WebResource method | |
* requires a request body and a body is not already set. | |
* | |
* @param {Stream} inputStream the stream to pipe from | |
* @param {Stream} outputStream the stream to pipe to | |
* | |
* @return destStream | |
*/ | |
WebResource.prototype.pipeInput = function(inputStream, destStream) { | |
if (isMethodWithBody(this.method) && !this.hasOwnProperty('body')) { | |
inputStream.pipe(destStream); | |
} | |
return destStream; | |
}; | |
module.exports = WebResource; | |
},{"../errors/errors":5,"../util/constants":32,"../util/sr":36,"../util/util":38}],10:[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 MD5 = require('md5.js'); | |
var Md5Wrapper = function () { | |
}; | |
Md5Wrapper.prototype.createMd5Hash = function() { | |
return new MD5(); | |
}; | |
module.exports = Md5Wrapper; | |
},{"md5.js":159}],11:[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. | |
// | |
// Module dependencies. | |
var _ = require('underscore'); | |
var xmlbuilder = require('xmlbuilder'); | |
var azureutil = require('../util/util'); | |
var ISO8061Date = require('../util/iso8061date'); | |
var Constants = require('../util/constants'); | |
var AclConstants = Constants.AclConstants; | |
exports = module.exports; | |
/** | |
* Builds an XML representation for container acl permissions. | |
* | |
* @param {Object.<string, AccessPolicy>} entity The signed identifiers. | |
* @return {string} The XML container acl permissions. | |
*/ | |
exports.serialize = function (signedIdentifiersJs) { | |
var doc = xmlbuilder.create(); | |
doc = doc.begin(AclConstants.SIGNED_IDENTIFIERS_ELEMENT, { version: '1.0', encoding: 'utf-8' }); | |
var keys = Object.keys(signedIdentifiersJs); | |
if (keys.length > 0) { | |
keys.forEach(function (key) { | |
var accessPolicy = signedIdentifiersJs[key]; | |
doc = doc | |
.ele(AclConstants.SIGNED_IDENTIFIER_ELEMENT) | |
.ele(AclConstants.ID) | |
.txt(key) | |
.up() | |
.ele(AclConstants.ACCESS_POLICY); | |
if (accessPolicy.Start) { | |
var startIsoString = accessPolicy.Start; | |
if (!_.isDate(startIsoString)) { | |
startIsoString = new Date(startIsoString); | |
} | |
// Convert to expected ISO 8061 date format | |
startIsoString = ISO8061Date.format(startIsoString); | |
doc = doc | |
.ele(AclConstants.START) | |
.txt(startIsoString) | |
.up(); | |
} | |
if (accessPolicy.Expiry) { | |
var expiryIsoString = accessPolicy.Expiry; | |
if (!_.isDate(expiryIsoString)) { | |
expiryIsoString = new Date(expiryIsoString); | |
} | |
// Convert to expected ISO 8061 date format | |
expiryIsoString = ISO8061Date.format(expiryIsoString); | |
doc = doc | |
.ele(AclConstants.EXPIRY) | |
.txt(expiryIsoString) | |
.up(); | |
} | |
if (accessPolicy.Permissions) { | |
doc = doc | |
.ele(AclConstants.PERMISSION) | |
.txt(accessPolicy.Permissions) | |
.up(); | |
} | |
doc = doc.up().up(); | |
}); | |
} | |
return doc.doc().toString(); | |
}; | |
exports.parse = function (signedIdentifiersXml) { | |
var signedIdentifiers = {}; | |
signedIdentifiersXml = azureutil.tryGetValueChain(signedIdentifiersXml, [ 'SignedIdentifiers', 'SignedIdentifier' ]); | |
if (signedIdentifiersXml) { | |
if (!_.isArray(signedIdentifiersXml)) { | |
signedIdentifiersXml = [ signedIdentifiersXml ]; | |
} | |
signedIdentifiersXml.forEach(function (signedIdentifier) { | |
var accessPolicy = {}; | |
if (signedIdentifier.AccessPolicy) { | |
if (signedIdentifier.AccessPolicy.Start) { | |
accessPolicy.Start = ISO8061Date.parse(signedIdentifier.AccessPolicy.Start); | |
} | |
if (signedIdentifier.AccessPolicy.Expiry) { | |
accessPolicy.Expiry = ISO8061Date.parse(signedIdentifier.AccessPolicy.Expiry); | |
} | |
if (signedIdentifier.AccessPolicy.Permission) { | |
accessPolicy.Permissions = signedIdentifier.AccessPolicy.Permission; | |
} | |
} | |
signedIdentifiers[signedIdentifier.Id] = accessPolicy; | |
}); | |
} | |
return signedIdentifiers; | |
}; | |
},{"../util/constants":32,"../util/iso8061date":34,"../util/util":38,"underscore":228,"xmlbuilder":315}],12:[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. | |
// | |
// Module dependencies. | |
var _ = require('underscore'); | |
var xmlbuilder = require('xmlbuilder'); | |
var Constants = require('../util/constants'); | |
var ServicePropertiesConstants = Constants.ServicePropertiesConstants; | |
exports = module.exports; | |
function serializeRetentionPolicy(doc, policy) { | |
if (policy !== null) { | |
if (typeof policy === 'undefined') { | |
policy = {}; | |
} | |
doc = doc.ele(ServicePropertiesConstants.RETENTION_POLICY_ELEMENT); | |
if (typeof policy.Enabled !== 'undefined') { | |
doc = doc.ele(ServicePropertiesConstants.ENABLED_ELEMENT) | |
.txt(policy.Enabled) | |
.up(); | |
} else { | |
doc = doc.ele(ServicePropertiesConstants.ENABLED_ELEMENT) | |
.txt(false) | |
.up(); | |
} | |
if (typeof policy.Days !== 'undefined') { | |
doc = doc.ele(ServicePropertiesConstants.DAYS_ELEMENT) | |
.txt(policy.Days) | |
.up(); | |
} else if (policy.Enabled === true) { | |
doc = doc.ele(ServicePropertiesConstants.DAYS_ELEMENT) | |
.txt(1) | |
.up(); | |
} | |
doc = doc.up(); | |
} | |
} | |
function serializeDeleteRetentionPolicy(doc, policy) { | |
if (policy !== null) { | |
if (typeof policy === 'undefined') { | |
policy = {}; | |
} | |
if (typeof policy.Enabled !== 'undefined') { | |
doc = doc.ele(ServicePropertiesConstants.ENABLED_ELEMENT) | |
.txt(policy.Enabled) | |
.up(); | |
} else { | |
doc = doc.ele(ServicePropertiesConstants.ENABLED_ELEMENT) | |
.txt(false) | |
.up(); | |
} | |
if (typeof policy.Days !== 'undefined') { | |
doc = doc.ele(ServicePropertiesConstants.DAYS_ELEMENT) | |
.txt(policy.Days) | |
.up(); | |
} else if (policy.Enabled === true) { | |
doc = doc.ele(ServicePropertiesConstants.DAYS_ELEMENT) | |
.txt(1) | |
.up(); | |
} | |
doc = doc.up(); | |
} | |
} | |
function serializeLogging(doc, logging) { | |
if (typeof logging.Version !== 'undefined') { | |
doc = doc.ele(ServicePropertiesConstants.VERSION_ELEMENT) | |
.txt(logging.Version) | |
.up(); | |
} else { | |
doc = doc.ele(ServicePropertiesConstants.VERSION_ELEMENT) | |
.txt(ServicePropertiesConstants.DEFAULT_ANALYTICS_VERSION) | |
.up(); | |
} | |
if (typeof logging.Delete !== 'undefined') { | |
doc = doc.ele(ServicePropertiesConstants.DELETE_ELEMENT) | |
.txt(logging.Delete) | |
.up(); | |
} else { | |
doc = doc.ele(ServicePropertiesConstants.DELETE_ELEMENT) | |
.txt(false) | |
.up(); | |
} | |
if (typeof logging.Read !== 'undefined') { | |
doc = doc.ele(ServicePropertiesConstants.READ_ELEMENT) | |
.txt(logging.Read) | |
.up(); | |
} else { | |
doc = doc.ele(ServicePropertiesConstants.READ_ELEMENT) | |
.txt(false) | |
.up(); | |
} | |
if (typeof logging.Write !== 'undefined') { | |
doc = doc.ele(ServicePropertiesConstants.WRITE_ELEMENT) | |
.txt(logging.Write) | |
.up(); | |
} else { | |
doc = doc.ele(ServicePropertiesConstants.WRITE_ELEMENT) | |
.txt(false) | |
.up(); | |
} | |
serializeRetentionPolicy(doc, logging.RetentionPolicy); | |
doc = doc.up(); | |
} | |
function serializeMetrics(doc, metrics) { | |
if (typeof metrics.Version !== 'undefined') { | |
doc = doc.ele(ServicePropertiesConstants.VERSION_ELEMENT) | |
.txt(metrics.Version) | |
.up(); | |
} else { | |
doc = doc.ele(ServicePropertiesConstants.VERSION_ELEMENT) | |
.txt(ServicePropertiesConstants.DEFAULT_ANALYTICS_VERSION) | |
.up(); | |
} | |
if (typeof metrics.Enabled !== 'undefined') { | |
doc = doc.ele(ServicePropertiesConstants.ENABLED_ELEMENT) | |
.txt(metrics.Enabled) | |
.up(); | |
} else { | |
doc = doc.ele(ServicePropertiesConstants.ENABLED_ELEMENT) | |
.txt(false) | |
.up(); | |
} | |
if (metrics.Enabled) { | |
if (typeof metrics.IncludeAPIs !== 'undefined') { | |
doc = doc.ele(ServicePropertiesConstants.INCLUDE_APIS_ELEMENT) | |
.txt(metrics.IncludeAPIs) | |
.up(); | |
} else if (metrics.Enabled === true) { | |
doc = doc.ele(ServicePropertiesConstants.INCLUDE_APIS_ELEMENT) | |
.txt(false) | |
.up(); | |
} | |
} | |
serializeRetentionPolicy(doc, metrics.RetentionPolicy); | |
} | |
function serializeCorsRules(doc, rules) { | |
if (typeof rules !== 'undefined' && rules !== null && _.isArray(rules)) { | |
rules.forEach(function (rule) { | |
doc = doc.ele(ServicePropertiesConstants.CORS_RULE_ELEMENT); | |
if (typeof rule.AllowedMethods !== 'undefined' && _.isArray(rule.AllowedMethods)) { | |
doc = doc.ele(ServicePropertiesConstants.ALLOWED_METHODS_ELEMENT) | |
.txt(rule.AllowedMethods.join(',')) | |
.up(); | |
} | |
if (typeof rule.AllowedOrigins !== 'undefined' && _.isArray(rule.AllowedOrigins)) { | |
doc = doc.ele(ServicePropertiesConstants.ALLOWED_ORIGINS_ELEMENT) | |
.txt(rule.AllowedOrigins.join(',')) | |
.up(); | |
} | |
if (typeof rule.AllowedHeaders !== 'undefined' && _.isArray(rule.AllowedHeaders)) { | |
doc = doc.ele(ServicePropertiesConstants.ALLOWED_HEADERS_ELEMENT) | |
.txt(rule.AllowedHeaders.join(',')) | |
.up(); | |
} else { | |
doc = doc.ele(ServicePropertiesConstants.ALLOWED_HEADERS_ELEMENT) | |
.txt('') | |
.up(); | |
} | |
if (typeof rule.ExposedHeaders !== 'undefined' && _.isArray(rule.ExposedHeaders)) { | |
doc = doc.ele(ServicePropertiesConstants.EXPOSED_HEADERS_ELEMENT) | |
.txt(rule.ExposedHeaders.join(',')) | |
.up(); | |
} else { | |
doc = doc.ele(ServicePropertiesConstants.EXPOSED_HEADERS_ELEMENT) | |
.txt('') | |
.up(); | |
} | |
if (typeof rule.MaxAgeInSeconds !== 'undefined') { | |
doc = doc.ele(ServicePropertiesConstants.MAX_AGE_IN_SECONDS_ELEMENT) | |
.txt(rule.MaxAgeInSeconds) | |
.up(); | |
} else { | |
doc = doc.ele(ServicePropertiesConstants.MAX_AGE_IN_SECONDS_ELEMENT) | |
.txt('0') | |
.up(); | |
} | |
doc = doc.up(); | |
}); | |
} | |
} | |
exports.serialize = function (servicePropertiesJs) { | |
var doc = xmlbuilder.create(); | |
doc = doc.begin(ServicePropertiesConstants.STORAGE_SERVICE_PROPERTIES_ELEMENT, { version: '1.0', encoding: 'utf-8' }); | |
if (servicePropertiesJs.Logging) { | |
doc = doc.ele(ServicePropertiesConstants.LOGGING_ELEMENT); | |
serializeLogging(doc, servicePropertiesJs.Logging); | |
doc = doc.up(); | |
} | |
if (servicePropertiesJs.HourMetrics) { | |
doc = doc.ele(ServicePropertiesConstants.HOUR_METRICS_ELEMENT); | |
serializeMetrics(doc, servicePropertiesJs.HourMetrics); | |
doc = doc.up(); | |
} | |
if (servicePropertiesJs.MinuteMetrics) { | |
doc = doc.ele(ServicePropertiesConstants.MINUTE_METRICS_ELEMENT); | |
serializeMetrics(doc, servicePropertiesJs.MinuteMetrics); | |
doc = doc.up(); | |
} | |
if (servicePropertiesJs.Cors) { | |
doc = doc.ele(ServicePropertiesConstants.CORS_ELEMENT); | |
serializeCorsRules(doc, servicePropertiesJs.Cors.CorsRule); | |
doc = doc.up(); | |
} | |
if (servicePropertiesJs.DefaultServiceVersion) { | |
doc = doc.ele(ServicePropertiesConstants.DEFAULT_SERVICE_VERSION_ELEMENT) | |
.txt(servicePropertiesJs.DefaultServiceVersion) | |
.up(); | |
} | |
if (servicePropertiesJs.DeleteRetentionPolicy) { | |
doc = doc.ele(ServicePropertiesConstants.DEFAULT_DELETE_RETENTION_POLICY_ELEMENT); | |
serializeDeleteRetentionPolicy(doc, servicePropertiesJs.DeleteRetentionPolicy); | |
doc = doc.up(); | |
} | |
return doc.doc().toString(); | |
}; | |
function parseRetentionPolicy(policyXml) { | |
var policy = {}; | |
if (typeof policyXml.Enabled !== 'undefined') { | |
policy.Enabled = policyXml.Enabled === 'true'; | |
} | |
if (typeof policyXml.Days !== 'undefined') { | |
policy.Days = parseInt(policyXml.Days, 10); | |
} | |
return policy; | |
} | |
function parseLogging(loggingXml) { | |
var logging = {}; | |
if (typeof loggingXml.Version !== 'undefined') { | |
logging.Version = loggingXml.Version; | |
} | |
if (typeof loggingXml.Delete !== 'undefined') { | |
logging.Delete = loggingXml.Delete === 'true'; | |
} | |
if (typeof loggingXml.Read !== 'undefined') { | |
logging.Read = loggingXml.Read === 'true'; | |
} | |
if (typeof loggingXml.Write !== 'undefined') { | |
logging.Write = loggingXml.Write === 'true'; | |
} | |
if (typeof loggingXml.RetentionPolicy !== 'undefined') { | |
logging.RetentionPolicy = parseRetentionPolicy(loggingXml.RetentionPolicy); | |
} | |
return logging; | |
} | |
function parseMetrics(metricsXml) { | |
var metrics = {}; | |
if (typeof metricsXml.Version !== 'undefined') { | |
metrics.Version = metricsXml.Version; | |
} | |
if (typeof metricsXml.Enabled !== 'undefined') { | |
metrics.Enabled = metricsXml.Enabled === 'true'; | |
} | |
if (typeof metricsXml.IncludeAPIs !== 'undefined') { | |
metrics.IncludeAPIs = metricsXml.IncludeAPIs === 'true'; | |
} | |
if (typeof metricsXml.RetentionPolicy !== 'undefined') { | |
metrics.RetentionPolicy = parseRetentionPolicy(metricsXml.RetentionPolicy); | |
} | |
return metrics; | |
} | |
function parseCors(corsXml) { | |
var cors = {}; | |
if (typeof corsXml.CorsRule !== 'undefined') { | |
var rulesXml = corsXml.CorsRule; | |
if (!_.isArray(rulesXml)) { | |
rulesXml = [rulesXml]; | |
} | |
cors.CorsRule = []; | |
rulesXml.forEach(function (ruleXml) { | |
var rule = {}; | |
if (typeof ruleXml.AllowedMethods !== 'undefined') { | |
if (ruleXml.AllowedMethods !== '') { | |
rule.AllowedMethods = ruleXml.AllowedMethods.split(','); | |
} | |
else { | |
rule.AllowedMethods = []; | |
} | |
} | |
if (typeof ruleXml.AllowedOrigins !== 'undefined') { | |
if (ruleXml.AllowedOrigins !== '') { | |
rule.AllowedOrigins = ruleXml.AllowedOrigins.split(','); | |
} | |
else { | |
rule.AllowedOrigins = []; | |
} | |
} | |
if (typeof ruleXml.AllowedHeaders !== 'undefined') { | |
if (ruleXml.AllowedHeaders !== '') { | |
rule.AllowedHeaders = ruleXml.AllowedHeaders.split(','); | |
} | |
else { | |
rule.AllowedHeaders = []; | |
} | |
} | |
if (typeof ruleXml.ExposedHeaders !== 'undefined') { | |
if (ruleXml.ExposedHeaders !== '') { | |
rule.ExposedHeaders = ruleXml.ExposedHeaders.split(','); | |
} | |
else { | |
rule.ExposedHeaders = []; | |
} | |
} | |
if (typeof ruleXml.MaxAgeInSeconds !== 'undefined') { | |
rule.MaxAgeInSeconds = parseInt(ruleXml.MaxAgeInSeconds, 10); | |
} | |
cors.CorsRule.push(rule); | |
}); | |
} | |
return cors; | |
} | |
function parseDeleteRetentionPolicy(deleteRetentionPolicyXml) { | |
var deleteRetentionPolicy = {}; | |
if (typeof deleteRetentionPolicyXml.Enabled !== 'undefined') { | |
deleteRetentionPolicy.Enabled = deleteRetentionPolicyXml.Enabled === 'true'; | |
} | |
if (typeof deleteRetentionPolicyXml.Days !== 'undefined') { | |
deleteRetentionPolicy.Days = deleteRetentionPolicyXml.Days; | |
} | |
return deleteRetentionPolicy; | |
} | |
exports.parse = function (servicePropertiesXml) { | |
var serviceProperties = {}; | |
if (typeof servicePropertiesXml.Logging !== 'undefined') { | |
serviceProperties.Logging = parseLogging(servicePropertiesXml.Logging); | |
} | |
if (typeof servicePropertiesXml.HourMetrics !== 'undefined') { | |
serviceProperties.HourMetrics = parseMetrics(servicePropertiesXml.HourMetrics); | |
} | |
if (typeof servicePropertiesXml.MinuteMetrics !== 'undefined') { | |
serviceProperties.MinuteMetrics = parseMetrics(servicePropertiesXml.MinuteMetrics); | |
} | |
if (typeof servicePropertiesXml.Cors !== 'undefined') { | |
serviceProperties.Cors = parseCors(servicePropertiesXml.Cors); | |
} | |
if (typeof servicePropertiesXml.DefaultServiceVersion !== 'undefined') { | |
serviceProperties.DefaultServiceVersion = servicePropertiesXml.DefaultServiceVersion; | |
} | |
if (typeof servicePropertiesXml.DeleteRetentionPolicy !== 'undefined') { | |
serviceProperties.DeleteRetentionPolicy = parseDeleteRetentionPolicy(servicePropertiesXml.DeleteRetentionPolicy); | |
} | |
return serviceProperties; | |
}; | |
},{"../util/constants":32,"underscore":228,"xmlbuilder":315}],13:[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. | |
// | |
exports = module.exports; | |
exports.parse = function (serviceStatsXml) { | |
var serviceStats = {}; | |
if (typeof serviceStatsXml.GeoReplication !== 'undefined') { | |
serviceStats.GeoReplication = {}; | |
if (typeof serviceStatsXml.GeoReplication.Status !== 'undefined') { | |
serviceStats.GeoReplication.Status = serviceStatsXml.GeoReplication.Status; | |
} | |
if (typeof serviceStatsXml.GeoReplication.LastSyncTime !== 'undefined' && serviceStatsXml.GeoReplication.LastSyncTime !== '') { | |
serviceStats.GeoReplication.LastSyncTime = new Date(serviceStatsXml.GeoReplication.LastSyncTime); | |
} | |
} | |
return serviceStats; | |
}; | |
},{}],14:[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. | |
// | |
/** | |
* Creates a new TokenCredential object. | |
* @class | |
* The TokenCredential class is used to store the access token string. | |
* | |
* @constructor | |
* @param {string} token The access token, such as an OAuth access token in string type. | |
* | |
* @example | |
* var azure = require('azure-storage'); | |
* var tokenCredential = new azure.TokenCredential('myOAuthAccessToken'); | |
* var blobService = azure.createBlobServiceWithTokenCredential('https://account.blob.core.windows.net', tokenCredential); | |
* tokenCredential.set('updatedOAuthAccessToken'); | |
*/ | |
function TokenCredential (token) { | |
this.token = token; | |
} | |
/** | |
* Get current access token. | |
* | |
* @return {string} The current access token in string type. | |
*/ | |
TokenCredential.prototype.get = function () { | |
return this.token; | |
}; | |
/** | |
* Renew the access token. | |
* | |
* @param {string} token The new access token in string. | |
*/ | |
TokenCredential.prototype.set = function (token) { | |
this.token = token; | |
}; | |
module.exports = TokenCredential; | |
},{}],15:[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 azureCommon = require('../common.browser'); | |
var Duplex = require('stream').Duplex; | |
var extend = require('extend'); | |
var http = require('http'); | |
var https = require('https'); | |
var url = require('url'); | |
var util = require('util'); | |
/** | |
* Creates a new Request object. | |
* | |
* @constructor | |
* @param {object} options The options for a Request object. | |
* @param {function} callback Callback | |
*/ | |
function Request(options, callback) { | |
Duplex.call(this); | |
this._init(options, callback); | |
this._send(); | |
} | |
util.inherits(Request, Duplex); | |
Request.prototype._init = function (options, callback) { | |
this.callback = callback; | |
var nonReservedProperties = azureCommon.util.filterOutNonReservedProperties(this, options); | |
extend(this, nonReservedProperties); | |
this.agent = this.agent || false; | |
this.timeout = this.timeout || Math.pow(2, 32) * 1000; | |
this._initUri(); | |
this._initHeaders(); | |
}; | |
Request.prototype._initUri = function () { | |
if (!this.uri) { | |
return this.emit('error', new Error('options.uri is a required argument')); | |
} | |
if (typeof this.uri === 'string') { | |
this.uri = url.parse(this.uri); | |
} | |
if (!this.uri.href) { | |
this.uri.href = url.format(this.uri); | |
} | |
}; | |
Request.prototype._initHeaders = function () { | |
this.headers = this.headers || {}; | |
this.headers['content-length'] = this.headers['content-length'] || 0; | |
}; | |
Request.prototype._send = function () { | |
this._sent = true; | |
var protocol = this.uri.protocol || ''; | |
var iface = (protocol === 'https:' ? https : http); | |
var options = { | |
scheme: protocol.replace(/:$/, ''), | |
method: this.method, | |
host: this.uri.hostname, | |
port: Number(this.uri.port) || (protocol === 'https:' ? 443 : 80), | |
path: this.uri.path, | |
agent: this.agent, | |
headers: this.headers, | |
withCredentials: this.withCredentials, | |
localAddress: this.localAddress, | |
mode: this.mode | |
}; | |
if (protocol === 'https:') { | |
options.pfx = this.pfx; | |
options.key = this.key; | |
options.cert = this.cert; | |
options.ca = this.ca; | |
options.ciphers = this.ciphers; | |
options.rejectUnauthorized = this.rejectUnauthorized; | |
options.secureProtocol = this.secureProtocol; | |
} | |
var httpRequest = iface.request(options); | |
if (this.timeout && httpRequest.setTimeout) { | |
httpRequest.setTimeout(this.timeout); | |
} | |
this.httpRequest = httpRequest; | |
this.emit('request', httpRequest); | |
this._sendBody(); | |
this._listenHttpResponse(); | |
this._listenHttpError(); | |
return httpRequest; | |
}; | |
Request.prototype._sendBody = function () { | |
if (this.body) { | |
this.httpRequest.write(this.body); | |
this.httpRequest.end(); | |
return; | |
} | |
if (this.headers['content-length'] == '0') { | |
this.httpRequest.end(); | |
return; | |
} | |
}; | |
Request.prototype._listenHttpError = function() { | |
var self = this; | |
self.httpRequest.on('error', function(error) { | |
self.emit('error', error); | |
}); | |
self.on('error', function(error) { | |
if (self.callback) { | |
self.callback(error); | |
} | |
}); | |
}; | |
Request.prototype._listenHttpResponse = function () { | |
var self = this; | |
self.httpRequest.on('response', function (response) { | |
var buffers = []; | |
var bufferLength = 0; | |
self.response = response; | |
response.on('data', function (chunk) { | |
self.push(chunk); | |
buffers.push(chunk); | |
bufferLength += chunk.length; | |
}); | |
response.on('error', function (error) { | |
self.emit('error', error); | |
}); | |
response.on('end', function () { | |
self.push(null); | |
response.body = ''; | |
if (bufferLength > 0) { | |
response.body = Buffer.concat(buffers, bufferLength); | |
} | |
if (self.encoding !== null) { | |
response.body = response.body.toString(self.encoding); | |
} | |
if (self.callback) { | |
self.callback(null, response); | |
} | |
}); | |
self.emit('response', response); | |
}); | |
}; | |
/** | |
* Set a Request header. | |
* | |
* @param {string} key The user provided header key. | |
* @param {string} value The valid header value. | |
*/ | |
Request.prototype.setHeader = function (key, value) { | |
if (this._sent) { | |
throw new Error('Request already sent'); | |
} | |
this.headers[key] = value; | |
return this; | |
}; | |
/** | |
* Set a Request URI. | |
* | |
* @param {string} uri The user provided uri. | |
*/ | |
Request.prototype.setLocation = function (uri) { | |
this.uri = uri; | |
return this; | |
}; | |
Request.prototype.end = function (chunk) { | |
if (chunk) { | |
this.httpRequest.write(chunk); | |
} | |
this.httpRequest.end(); | |
}; | |
Request.prototype._write = function (chunk, encoding, callback) { | |
this.httpRequest.write(chunk); | |
callback(); | |
}; | |
Request.prototype._read = function () { | |
}; | |
/** | |
* Create a Request object | |
* @ignore | |
* | |
* @param {object} options Options | |
* @param {function} callback Callback | |
* @return {Request} The created request object. | |
*/ | |
function req(optionsOrCallback, callback) { | |
var reqDefaults = req.defaults(); | |
return reqDefaults(optionsOrCallback, callback); | |
} | |
/** | |
* Create a Request creator with default options | |
* @ignore | |
* | |
* @param {object} defaultOptions Default options | |
* @return {function} | |
*/ | |
req.defaults = function (defaultOptions) { | |
return function (optionsOrCallback, callback) { | |
var options; | |
azureCommon.util.normalizeArgs(optionsOrCallback, callback, function (o, c) { options = o; callback = c; }); | |
var nonReservedProperties = azureCommon.util.filterOutNonReservedProperties(options, defaultOptions); | |
extend(options, nonReservedProperties); | |
var request = new Request(options, callback); | |
return request; | |
}; | |
}; | |
module.exports = req; | |
}).call(this,require("buffer").Buffer) | |
},{"../common.browser":2,"buffer":99,"extend":137,"http":210,"https":152,"stream":209,"url":229,"util":234}],16:[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'); | |
var errors = require('../errors/errors'); | |
var SR = require('../util/sr'); | |
var Constants = require('../util/constants'); | |
exports = module.exports; | |
/** | |
* The default protocol. | |
*/ | |
exports.DEFAULT_PROTOCOL = Constants.HTTPS; | |
var NoMatchError = function (msg, constr) { | |
errors.captureStackTrace(this, constr || this); | |
this.message = msg || 'Error'; | |
}; | |
util.inherits(NoMatchError, Error); | |
NoMatchError.prototype.name = 'NoMatchError'; | |
exports.NoMatchError = NoMatchError; | |
/** | |
* Throws an exception if the connection string format does not match any of the | |
* available formats. | |
* | |
* @param {string} connectionString The invalid formatted connection string. | |
* @return none | |
*/ | |
exports.noMatchConnectionString = function (connectionString) { | |
throw new NoMatchError('The provided connection string "' + connectionString + '" does not have complete configuration settings.'); | |
}; | |
/** | |
* Throws an exception if the settings dont match any of the | |
* available formats. | |
* | |
* @param {object} settings The invalid settings. | |
* @return none | |
*/ | |
exports.noMatchSettings = function (settings) { | |
throw new NoMatchError('The provided settings ' + JSON.stringify(settings) + ' are not complete.'); | |
}; | |
/** | |
* Parses the connection string and then validate that the parsed keys belong to | |
* the validSettingKeys | |
* | |
* @param {string} connectionString The user provided connection string. | |
* @param {array} validKeys The valid keys. | |
* @return {array} The tokenized connection string keys. | |
*/ | |
exports.parseAndValidateKeys = function (connectionString, validKeys) { | |
var parsedConnectionString = { }; | |
// parse key/value pairs from connection string | |
var pairs = connectionString.split(';'); | |
for (var m = 0; m < pairs.length; m++) { | |
if (pairs[m].length === 0) { | |
continue; | |
} | |
var equalDex = pairs[m].indexOf('='); | |
if (equalDex < 0) { | |
throw new SyntaxError(SR.INVALID_CONNECTION_STRING); | |
} else if (equalDex === 0) { | |
// empty key name. | |
throw new SyntaxError(SR.INVALID_CONNECTION_STRING_EMPTY_KEY); | |
} | |
var key = pairs[m].substring(0, equalDex); | |
// assure that all given keys are valid. | |
if (!azureUtil.inArrayInsensitive(key, validKeys)) { | |
throw new SyntaxError(util.format(SR.INVALID_CONNECTION_STRING_BAD_KEY, key)); | |
} | |
var value = pairs[m].substring(equalDex + 1); | |
if(typeof parsedConnectionString[key] === 'undefined'){ | |
parsedConnectionString[key] = value; | |
} else { | |
// duplicate key name | |
throw new SyntaxError(util.format(SR.INVALID_CONNECTION_STRING_DUPLICATE_KEY, key)); | |
} | |
} | |
return parsedConnectionString; | |
}; | |
/** | |
* Creates an anonymous function that acts as predicate to perform a validation. | |
* | |
* @param array {requirements} The array of conditions to satisfy. | |
* @param boolean {isRequired} Either these conditions are all required or all | |
* optional. | |
* @param boolean {atLeastOne} Indicates that at least one requirement must | |
* succeed. | |
* @return {function} | |
*/ | |
exports.getValidator = function (requirements, isRequired, atLeastOne) { | |
return function (userSettings) { | |
var oneFound = false; | |
var result = { }; | |
for (var key in userSettings) { | |
if (userSettings.hasOwnProperty(key)) { | |
result[key.toLowerCase()] = userSettings[key]; | |
} | |
} | |
for (var requirement in requirements) { | |
if (requirements.hasOwnProperty(requirement)) { | |
var settingName = requirements[requirement].SettingName.toLowerCase(); | |
// Check if the setting name exists in the provided user settings. | |
if (result[settingName]) { | |
// Check if the provided user setting value is valid. | |
var validationFunc = requirements[requirement].SettingConstraint; | |
var isValid = validationFunc(result[settingName]); | |
if (isValid) { | |
// Remove the setting as indicator for successful validation. | |
delete result[settingName]; | |
oneFound = true; | |
} | |
} else if (isRequired) { | |
// If required then fail because the setting does not exist | |
return null; | |
} | |
} | |
} | |
if (atLeastOne) { | |
// At least one requirement must succeed, otherwise fail. | |
return oneFound ? result : null; | |
} else { | |
return result; | |
} | |
}; | |
}; | |
/** | |
* Creates a setting value condition that validates it is one of the | |
* passed valid values. | |
* | |
* @param {string} name The setting key name. | |
* @return {array} | |
*/ | |
exports.setting = function (name) { | |
var validValues = Array.prototype.slice.call(arguments, 1, arguments.length); | |
var predicate = function (settingValue) { | |
var validValuesString = JSON.stringify(validValues); | |
if (validValues.length === 0) { | |
// No restrictions, succeed. | |
return true; | |
} | |
// Check to find if the settingValue is valid or not. | |
for (var index = 0; index < validValues.length; index++) { | |
if (settingValue.toString() == validValues[index].toString()) { | |
// SettingValue is found in valid values set, succeed. | |
return true; | |
} | |
} | |
// settingValue is missing in valid values set, fail. | |
throw new RangeError('The provided config value ' + settingValue + ' does not belong to the valid values subset:\n' + validValuesString); | |
}; | |
return exports.settingWithFunc(name, predicate); | |
}; | |
/** | |
* Creates an "at lease one" predicate for the provided list of requirements. | |
* | |
* @return callable | |
*/ | |
exports.atLeastOne = function () { | |
var allSettings = arguments; | |
return exports.getValidator(allSettings, false, true); | |
}; | |
/** | |
* Creates an optional predicate for the provided list of requirements. | |
* | |
* @return {function} | |
*/ | |
exports.optional = function () { | |
var optionalSettings = arguments; | |
return exports.getValidator(optionalSettings, false, false); | |
}; | |
/** | |
* Creates an required predicate for the provided list of requirements. | |
* | |
* @return {function} | |
*/ | |
exports.allRequired = function () { | |
var requiredSettings = arguments; | |
return exports.getValidator(requiredSettings, true, false); | |
}; | |
/** | |
* Creates a setting value condition using the passed predicate. | |
* | |
* @param {string} name The setting key name. | |
* @param {function} predicate The setting value predicate. | |
* @return {array} | |
*/ | |
exports.settingWithFunc = function (name, predicate) { | |
var requirement = {}; | |
requirement.SettingName = name; | |
requirement.SettingConstraint = predicate; | |
return requirement; | |
}; | |
/** | |
* Tests to see if a given list of settings matches a set of filters exactly. | |
* | |
* @param array $settings The settings to check. | |
* @return boolean If any filter returns null, false. If there are any settings | |
* left over after all filters are processed, false. Otherwise true. | |
*/ | |
exports.matchedSpecification = function (settings) { | |
var constraints = Array.prototype.slice.call(arguments, 1, arguments.length); | |
for (var constraint in constraints) { | |
if (constraints.hasOwnProperty(constraint)) { | |
var remainingSettings = constraints[constraint](settings); | |
if (!remainingSettings) { | |
return false; | |
} else { | |
settings = remainingSettings; | |
} | |
} | |
} | |
return azureUtil.objectKeysLength(settings) === 0; | |
}; | |
},{"../errors/errors":5,"../util/constants":32,"../util/sr":36,"../util/util":38,"util":234}],17:[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 TokenSigner = require('../signing/tokensigner'); | |
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} sas The Shared Access Signature string. | |
* @param {TokenCredential} [token] The {@link TokenCredential} object, which can be created with an OAuth access token string. | |
*/ | |
function StorageServiceClient(storageAccount, storageAccessKey, host, usePathStyleUri, sas, token) { | |
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 (sas) { | |
// sas | |
this.sasToken = sas; | |
this.storageCredentials = new SharedAccessSignature(sas); | |
} else if (token) { | |
// access token | |
this.token = token; | |
this.storageCredentials = new TokenSigner(token); | |
} 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 | |
} | |
requestOptions.forever = true; | |
} | |
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} [sas] The Shared Access Signature string. | |
* @param {TokenCredential} [token] The {@link TokenCredential} object. | |
* | |
* @return {StorageServiceSettings} | |
*/ | |
StorageServiceClient.getStorageSettings = function (storageAccountOrConnectionString, storageAccessKey, host, sas, endpointSuffix, token) { | |
var storageServiceSettings; | |
if (storageAccountOrConnectionString && !storageAccessKey && !sas) { | |
// If storageAccountOrConnectionString was passed and no accessKey was passed, assume connection string | |
storageServiceSettings = StorageServiceSettings.createFromConnectionString(storageAccountOrConnectionString); | |
} else if ((storageAccountOrConnectionString && storageAccessKey) || sas || token || host) { | |
// Account and key or credentials or anonymous | |
storageServiceSettings = StorageServiceSettings.createExplicitly(storageAccountOrConnectionString, storageAccessKey, host, sas, endpointSuffix, token); | |
} 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":50,"../diagnostics/logger":4,"../errors/errors":5,"../http/webresource":9,"../md5-wrapper":10,"../models/servicepropertiesresult":12,"../request-wrapper":15,"../signing/sharedaccesssignature":20,"../signing/sharedkey":21,"../signing/tokensigner":22,"../streams/bufferstream.js":25,"../util/constants":32,"../util/sr":36,"../util/storageutilities":37,"../util/util":38,"../util/validate":39,"./servicesettings":16,"./storageservicesettings":18,"_process":175,"buffer":99,"events":135,"extend":137,"json-edm-parser":157,"os":163,"querystring":185,"underscore":228,"url":229,"util":234,"uuid":235,"xml2js":312}],18:[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. | |
// | |
'use strict'; | |
var _ = require('underscore'); | |
var url = require('url'); | |
var util = require('../util/util'); | |
var ServiceSettings = require('./servicesettings'); | |
var Constants = require('../util/constants'); | |
var StorageServiceClientConstants = Constants.StorageServiceClientConstants; | |
var ConnectionStringKeys = Constants.ConnectionStringKeys; | |
var Validate = require('../util/validate'); | |
var SR = require('../util/sr'); | |
var TokenCredential = require('../models/tokencredential'); | |
var useDevelopmentStorageSetting = ServiceSettings.setting(ConnectionStringKeys.USE_DEVELOPMENT_STORAGE_NAME, true); | |
var developmentStorageProxyUriSetting = ServiceSettings.settingWithFunc(ConnectionStringKeys.DEVELOPMENT_STORAGE_PROXY_URI_NAME, Validate.isValidUri); | |
var defaultEndpointsProtocolSetting = ServiceSettings.setting(ConnectionStringKeys.DEFAULT_ENDPOINTS_PROTOCOL_NAME, 'http', 'https'); | |
var accountNameSetting = ServiceSettings.setting(ConnectionStringKeys.ACCOUNT_NAME_NAME); | |
var accountKeySetting = ServiceSettings.settingWithFunc(ConnectionStringKeys.ACCOUNT_KEY_NAME, Validate.isBase64Encoded); | |
var sasSetting = ServiceSettings.settingWithFunc(ConnectionStringKeys.SHARED_ACCESS_SIGNATURE_NAME, _.isString); | |
var tokenSetting = ServiceSettings.settingWithFunc('token', function (object) {return object instanceof TokenCredential;}); | |
var blobEndpointSetting = ServiceSettings.settingWithFunc( | |
ConnectionStringKeys.BLOB_ENDPOINT_NAME, | |
Validate.isValidHost | |
); | |
var queueEndpointSetting = ServiceSettings.settingWithFunc( | |
ConnectionStringKeys.QUEUE_ENDPOINT_NAME, | |
Validate.isValidHost | |
); | |
var tableEndpointSetting = ServiceSettings.settingWithFunc( | |
ConnectionStringKeys.TABLE_ENDPOINT_NAME, | |
Validate.isValidHost | |
); | |
var fileEndpointSetting = ServiceSettings.settingWithFunc( | |
ConnectionStringKeys.FILE_ENDPOINT_NAME, | |
Validate.isValidHost | |
); | |
var endpointSuffixSetting = ServiceSettings.settingWithFunc( | |
ConnectionStringKeys.ENDPOINT_SUFFIX_NAME, | |
Validate.isValidHost | |
); | |
var validKeys = [ | |
ConnectionStringKeys.USE_DEVELOPMENT_STORAGE_NAME, | |
ConnectionStringKeys.DEVELOPMENT_STORAGE_PROXY_URI_NAME, | |
ConnectionStringKeys.DEFAULT_ENDPOINTS_PROTOCOL_NAME, | |
ConnectionStringKeys.ACCOUNT_NAME_NAME, | |
ConnectionStringKeys.ACCOUNT_KEY_NAME, | |
ConnectionStringKeys.SHARED_ACCESS_SIGNATURE_NAME, | |
ConnectionStringKeys.BLOB_ENDPOINT_NAME, | |
ConnectionStringKeys.QUEUE_ENDPOINT_NAME, | |
ConnectionStringKeys.TABLE_ENDPOINT_NAME, | |
ConnectionStringKeys.FILE_ENDPOINT_NAME, | |
ConnectionStringKeys.ENDPOINT_SUFFIX_NAME | |
]; | |
/** | |
* Creates new storage service settings instance. | |
* | |
* @param {string} name The storage service name. | |
* @param {string} key The storage service key. | |
* @param {string} sasToken The storage service shared access signature token. | |
* @param {string} blobEndpoint The storage service blob endpoint. | |
* @param {string} queueEndpoint The storage service queue endpoint. | |
* @param {string} tableEndpoint The storage service table endpoint. | |
* @param {string} fileEndpoint The storage service file endpoint. | |
* @param {bool} usePathStyleUri Boolean value indicating wether to use path style uri or not. | |
* @param {TokenCredential} [token] The {@link TokenCredential} object. | |
*/ | |
function StorageServiceSettings(name, key, sasToken, blobEndpoint, queueEndpoint, tableEndpoint, fileEndpoint, usePathStyleUri, token) { | |
this._name = name; | |
this._key = key; | |
if (sasToken && sasToken[0] === '?') { | |
this._sasToken = sasToken.slice(1); | |
} else { | |
this._sasToken = sasToken; | |
} | |
this._blobEndpoint = blobEndpoint; | |
this._queueEndpoint = queueEndpoint; | |
this._tableEndpoint = tableEndpoint; | |
this._fileEndpoint = fileEndpoint; | |
if (usePathStyleUri) { | |
this._usePathStyleUri = usePathStyleUri; | |
} else { | |
this._usePathStyleUri = false; | |
} | |
this._token = token; | |
} | |
/** | |
* Creates a StorageServiceSettings object from the given connection string. | |
* | |
* @param {string} connectionString The storage settings connection string. | |
* @return {StorageServiceSettings} | |
*/ | |
StorageServiceSettings.createFromConnectionString = function (connectionString) { | |
var tokenizedSettings = ServiceSettings.parseAndValidateKeys(connectionString, validKeys); | |
try { | |
return StorageServiceSettings.createFromSettings(tokenizedSettings); | |
} catch (e) { | |
if (e instanceof ServiceSettings.NoMatchError) { | |
// Replace no match settings exception by no match connection string one. | |
ServiceSettings.noMatchConnectionString(connectionString); | |
} else { | |
throw e; | |
} | |
} | |
}; | |
StorageServiceSettings.createExplicitly = function (storageAccount, storageAccessKey, host, sasToken, endpointSuffix, token) { | |
var settings = {}; | |
function addIfNotNullOrEmpty(key, value){ | |
if(typeof value === 'string' && !util.stringIsEmpty(value)){ | |
settings[key] = value; | |
} else if (typeof value == 'object' && !util.objectIsNull(value)) { | |
settings[key] = value; | |
} | |
} | |
// Endpoints | |
if (host) { | |
addIfNotNullOrEmpty('blobendpoint', host); | |
addIfNotNullOrEmpty('tableendpoint', host); | |
addIfNotNullOrEmpty('queueendpoint', host); | |
addIfNotNullOrEmpty('fileendpoint', host); | |
} else { | |
addIfNotNullOrEmpty('defaultendpointsprotocol', ServiceSettings.DEFAULT_PROTOCOL.split(':', 1)[0]); | |
} | |
addIfNotNullOrEmpty('accountname', storageAccount); | |
addIfNotNullOrEmpty('accountkey', storageAccessKey); | |
addIfNotNullOrEmpty('sharedaccesssignature', sasToken); | |
addIfNotNullOrEmpty('endpointsuffix', endpointSuffix); | |
addIfNotNullOrEmpty('token', token); | |
return StorageServiceSettings.createFromSettings(settings); | |
}; | |
StorageServiceSettings.createFromEnvironment = function () { | |
var emulated = process.env[StorageServiceClientConstants.EnvironmentVariables.EMULATED]; | |
if (emulated) { | |
return StorageServiceSettings.getDevelopmentStorageAccountSettings(); | |
} | |
var connectionString = process.env[StorageServiceClientConstants.EnvironmentVariables.AZURE_STORAGE_CONNECTION_STRING]; | |
if (connectionString) { | |
return StorageServiceSettings.createFromConnectionString(connectionString); | |
} | |
var storageAccount = process.env[StorageServiceClientConstants.EnvironmentVariables.AZURE_STORAGE_ACCOUNT]; | |
var storageAccessKey = process.env[StorageServiceClientConstants.EnvironmentVariables.AZURE_STORAGE_ACCESS_KEY]; | |
if(storageAccount && storageAccessKey){ | |
return StorageServiceSettings.createExplicitly(storageAccount, storageAccessKey, null, null, null); | |
} | |
throw new Error(SR.NO_CREDENTIALS_PROVIDED); | |
}; | |
/** | |
* Creates a StorageServiceSettings object from a set of settings. | |
* | |
* @param {object} settings The settings object. | |
* @return {StorageServiceSettings} | |
*/ | |
StorageServiceSettings.createFromSettings = function (settings) { | |
// Devstore case | |
var matchedSpecs = ServiceSettings.matchedSpecification( | |
settings, | |
ServiceSettings.allRequired(useDevelopmentStorageSetting), | |
ServiceSettings.optional(developmentStorageProxyUriSetting) | |
); | |
if (matchedSpecs) { | |
var proxyUri = util.tryGetValueInsensitive( | |
ConnectionStringKeys.DEVELOPMENT_STORAGE_PROXY_URI_NAME, | |
settings | |
); | |
return this.getDevelopmentStorageAccountSettings(proxyUri); | |
} | |
// Account/Key automatic case | |
matchedSpecs = ServiceSettings.matchedSpecification( | |
settings, | |
ServiceSettings.allRequired( | |
defaultEndpointsProtocolSetting, | |
accountNameSetting, | |
accountKeySetting | |
), | |
ServiceSettings.optional( | |
blobEndpointSetting, | |
queueEndpointSetting, | |
tableEndpointSetting, | |
fileEndpointSetting, | |
endpointSuffixSetting | |
) | |
); | |
if (matchedSpecs) { | |
return this._createStorageServiceSettings(settings); | |
} | |
// Account/Key explicit case | |
matchedSpecs = ServiceSettings.matchedSpecification( | |
settings, | |
ServiceSettings.allRequired( | |
accountNameSetting, | |
accountKeySetting | |
), | |
ServiceSettings.atLeastOne( | |
blobEndpointSetting, | |
queueEndpointSetting, | |
tableEndpointSetting, | |
fileEndpointSetting, | |
endpointSuffixSetting | |
) | |
); | |
if (matchedSpecs) { | |
return this._createStorageServiceSettings(settings); | |
} | |
// SAS case | |
matchedSpecs = ServiceSettings.matchedSpecification( | |
settings, | |
ServiceSettings.allRequired( | |
sasSetting | |
), | |
ServiceSettings.atLeastOne( | |
blobEndpointSetting, | |
queueEndpointSetting, | |
tableEndpointSetting, | |
fileEndpointSetting, | |
endpointSuffixSetting | |
) | |
); | |
if(matchedSpecs) { | |
return this._createStorageServiceSettings(settings); | |
} | |
// anonymous explicit case | |
// Only blob anonymous access is valid. | |
matchedSpecs = ServiceSettings.matchedSpecification( | |
settings, | |
ServiceSettings.allRequired( | |
blobEndpointSetting | |
), | |
ServiceSettings.optional( | |
fileEndpointSetting, | |
queueEndpointSetting, | |
tableEndpointSetting, | |
endpointSuffixSetting | |
) | |
); | |
if(matchedSpecs) { | |
return this._createStorageServiceSettings(settings); | |
} | |
// Token case | |
matchedSpecs = ServiceSettings.matchedSpecification( | |
settings, | |
ServiceSettings.allRequired( | |
tokenSetting | |
), | |
ServiceSettings.atLeastOne( | |
blobEndpointSetting, | |
queueEndpointSetting, | |
tableEndpointSetting, | |
fileEndpointSetting | |
) | |
); | |
if(matchedSpecs) { | |
return this._createStorageServiceSettings(settings); | |
} | |
ServiceSettings.noMatchSettings(settings); | |
}; | |
/** | |
* Returns a StorageServiceSettings with development storage credentials using | |
* the specified proxy Uri. | |
* | |
* @param {string} proxyUri The proxy endpoint to use. | |
* @return {StorageServiceSettings} | |
*/ | |
StorageServiceSettings.getDevelopmentStorageAccountSettings = function (proxyUri) { | |
if (!proxyUri) { | |
proxyUri = StorageServiceClientConstants.DEV_STORE_URI; | |
} | |
var parsedUri = url.parse(proxyUri); | |
var scheme = parsedUri.protocol; | |
var host = parsedUri.host; | |
var prefix = scheme + '//' + host; | |
var blobEndpoint = { | |
primaryHost: prefix + ':10000' + '/' + StorageServiceClientConstants.DEVSTORE_STORAGE_ACCOUNT, | |
secondaryHost: prefix + ':10000' + '/' + StorageServiceClientConstants.DEVSTORE_STORAGE_ACCOUNT + '-secondary' | |
}; | |
var queueEndpoint = { | |
primaryHost: prefix + ':10001' + '/' + StorageServiceClientConstants.DEVSTORE_STORAGE_ACCOUNT, | |
secondaryHost: prefix + ':10001' + '/' + StorageServiceClientConstants.DEVSTORE_STORAGE_ACCOUNT + '-secondary' | |
}; | |
var tableEndpoint = { | |
primaryHost: prefix + ':10002' + '/' + StorageServiceClientConstants.DEVSTORE_STORAGE_ACCOUNT, | |
secondaryHost: prefix + ':10002' + '/' + StorageServiceClientConstants.DEVSTORE_STORAGE_ACCOUNT + '-secondary' | |
}; | |
return new StorageServiceSettings( | |
StorageServiceClientConstants.DEVSTORE_STORAGE_ACCOUNT, | |
StorageServiceClientConstants.DEVSTORE_STORAGE_ACCESS_KEY, | |
null, | |
blobEndpoint, | |
queueEndpoint, | |
tableEndpoint, | |
null, | |
true | |
); | |
}; | |
/** | |
* Creates StorageServiceSettings object given endpoints uri. | |
* | |
* @ignore | |
* @param {array} settings The service settings. | |
* @param {string} blobEndpointUri The blob endpoint uri. | |
* @param {string} queueEndpointUri The queue endpoint uri. | |
* @param {string} tableEndpointUri The table endpoint uri. | |
* @param {string} fileEndpointUri The file endpoint uri. | |
* @return {StorageServiceSettings} | |
*/ | |
StorageServiceSettings._createStorageServiceSettings = function (settings) { | |
var standardizeHost = function (host, accountName, scheme, dns){ | |
var storageHost; | |
if (host) { | |
storageHost = {}; | |
storageHost.primaryHost = _.isString(host) ? host : host.primaryHost; | |
storageHost.secondaryHost = _.isString(host) ? undefined : host.secondaryHost; | |
} | |
if (scheme && accountName && dns) { | |
storageHost = storageHost ? storageHost : {}; | |
storageHost.primaryHost = storageHost.primaryHost ? storageHost.primaryHost : url.format({ protocol: scheme, hostname: accountName + '.' + dns}); | |
storageHost.secondaryHost = storageHost.secondaryHost ? storageHost.secondaryHost : url.format({ protocol: scheme, hostname: accountName + '-secondary.' + dns}); | |
} | |
return storageHost; | |
}; | |
var scheme = util.tryGetValueInsensitive( | |
ConnectionStringKeys.DEFAULT_ENDPOINTS_PROTOCOL_NAME, | |
settings | |
); | |
var accountName = util.tryGetValueInsensitive( | |
ConnectionStringKeys.ACCOUNT_NAME_NAME, | |
settings | |
); | |
var accountKey = util.tryGetValueInsensitive( | |
ConnectionStringKeys.ACCOUNT_KEY_NAME, | |
settings | |
); | |
var sasToken = util.tryGetValueInsensitive( | |
ConnectionStringKeys.SHARED_ACCESS_SIGNATURE_NAME, | |
settings | |
); | |
var endpointSuffix = util.tryGetValueInsensitive( | |
ConnectionStringKeys.ENDPOINT_SUFFIX_NAME, | |
settings | |
); | |
var token = util.tryGetValueInsensitive( | |
'token', | |
settings | |
); | |
var blobEndpoint = standardizeHost( | |
util.tryGetValueInsensitive(ConnectionStringKeys.BLOB_ENDPOINT_NAME, settings), | |
accountName, | |
scheme, | |
endpointSuffix ? 'blob.' + endpointSuffix : StorageServiceClientConstants.CLOUD_BLOB_HOST); | |
var queueEndpoint = standardizeHost( | |
util.tryGetValueInsensitive(ConnectionStringKeys.QUEUE_ENDPOINT_NAME, settings), | |
accountName, | |
scheme, | |
endpointSuffix ? 'queue.' + endpointSuffix : StorageServiceClientConstants.CLOUD_QUEUE_HOST); | |
var tableEndpoint = standardizeHost( | |
util.tryGetValueInsensitive(ConnectionStringKeys.TABLE_ENDPOINT_NAME, settings), | |
accountName, | |
scheme, | |
endpointSuffix ? 'table.' + endpointSuffix : StorageServiceClientConstants.CLOUD_TABLE_HOST); | |
var fileEndpoint = standardizeHost( | |
util.tryGetValueInsensitive(ConnectionStringKeys.FILE_ENDPOINT_NAME, settings), | |
accountName, | |
scheme, | |
endpointSuffix ? 'file.' + endpointSuffix : StorageServiceClientConstants.CLOUD_FILE_HOST); | |
return new StorageServiceSettings( | |
accountName, | |
accountKey, | |
sasToken, | |
blobEndpoint, | |
queueEndpoint, | |
tableEndpoint, | |
fileEndpoint, | |
token | |
); | |
}; | |
StorageServiceSettings.validKeys = validKeys; | |
exports = module.exports = StorageServiceSettings; | |
}).call(this,require('_process')) | |
},{"../models/tokencredential":14,"../util/constants":32,"../util/sr":36,"../util/util":38,"../util/validate":39,"./servicesettings":16,"_process":175,"underscore":228,"url":229}],19:[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. | |
// | |
// Module dependencies. | |
var crypto = require('crypto'); | |
/** | |
* Creates a new HmacSHA256Sign object. | |
* | |
* @constructor | |
*/ | |
function HmacSha256Sign(accessKey) { | |
this._accessKey = accessKey; | |
this._decodedAccessKey = new Buffer(this._accessKey, 'base64'); | |
} | |
/** | |
* Computes a signature for the specified string using the HMAC-SHA256 algorithm. | |
* | |
* @param {string} stringToSign The UTF-8-encoded string to sign. | |
* @return A String that contains the HMAC-SHA256-encoded signature. | |
*/ | |
HmacSha256Sign.prototype.sign = function (stringToSign) { | |
// Encoding the Signature | |
// Signature=Base64(HMAC-SHA256(UTF8(StringToSign))) | |
return crypto.createHmac('sha256', this._decodedAccessKey).update(stringToSign, 'utf-8').digest('base64'); | |
}; | |
module.exports = HmacSha256Sign; | |
}).call(this,require("buffer").Buffer) | |
},{"buffer":99,"crypto":108}],20:[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 HmacSha256Sign = require('./hmacsha256sign'); | |
var Constants = require('./../util/constants'); | |
var SR = require('./../util/sr'); | |
/** | |
* Creates a new SharedAccessSignature object. | |
* | |
* @constructor | |
* @param {string} sasToken The sasToken. | |
*/ | |
function SharedAccessSignature(sasToken) { | |
this.sasToken = sasToken; | |
this.signer = new HmacSha256Sign(sasToken); | |
} | |
/** | |
* Signs a request with the signature header. | |
* | |
* @this {SharedAccessSignature} | |
* @param {WebResource} The webresource to be signed. | |
* @param {function(error)} callback The callback function. | |
*/ | |
SharedAccessSignature.prototype.signRequest = function (webResource, callback) { | |
if (webResource.uri.indexOf('?') === -1) { | |
webResource.uri += '?'; | |
} else { | |
webResource.uri += '&'; | |
} | |
webResource.uri += this.sasToken; | |
// Add the api-version | |
if (this.sasToken.indexOf('api-version') == -1) { | |
webResource.uri += '&' + Constants.QueryStringConstants.API_VERSION + '=' + Constants.HeaderConstants.TARGET_STORAGE_VERSION; | |
} else { | |
throw new SyntaxError(SR.INVALID_SAS_TOKEN); | |
} | |
callback(null); | |
}; | |
module.exports = SharedAccessSignature; | |
},{"./../util/constants":32,"./../util/sr":36,"./hmacsha256sign":19}],21:[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. | |
// | |
// Module dependencies. | |
var _ = require('underscore'); | |
var qs = require('querystring'); | |
var azureutil = require('../util/util'); | |
var HmacSha256Sign = require('./hmacsha256sign'); | |
var SR = require('../util/sr'); | |
var errors = require('../errors/errors'); | |
var ArgumentError = errors.ArgumentError; | |
var Constants = require('../util/constants'); | |
var HeaderConstants = Constants.HeaderConstants; | |
var QueryStringConstants = Constants.QueryStringConstants; | |
var HeaderConstants = Constants.HeaderConstants; | |
var CompatibleVersionConstants = Constants.CompatibleVersionConstants; | |
/** | |
* Creates a new SharedKey object. | |
* | |
* @constructor | |
* @param {string} storageAccount The storage account. | |
* @param {string} storageAccessKey The storage account's access key. | |
* @param {bool} usePathStyleUri Boolean value indicating if the path, or the hostname, should include the storage account. | |
*/ | |
function SharedKey(storageAccount, storageAccessKey, usePathStyleUri) { | |
this.storageAccount = storageAccount; | |
this.storageAccessKey = storageAccessKey; | |
this.usePathStyleUri = usePathStyleUri; | |
this.signer = new HmacSha256Sign(storageAccessKey); | |
} | |
/** | |
* Generates the shared access signature for a account. | |
* For more detailed information, refer to https://msdn.microsoft.com/en-us/library/azure/mt584140.aspx | |
* | |
* @param {object} sharedAccessPolicy The shared access policy. | |
* @param {SharedAccessServices} sharedAccessPolicy.AccessPolicy.Services The services (blob, file, queue, table) for a shared access signature associated with this shared access policy. | |
* Refer to `Constants.AccountSasConstants.Services`. | |
* @param {SharedAccessResourceTypes} sharedAccessPolicy.AccessPolicy.ResourceTypes The resource type for a shared access signature associated with this shared access policy. | |
* Refer to `Constants.AccountSasConstants.Resources`. | |
* @param {SharedAccessPermissions} sharedAccessPolicy.AccessPolicy.Permissions The permissions for a shared access signature. | |
* Refer to `Constants.AccountSasConstants.Permissions`. | |
* @param {date} sharedAccessPolicy.AccessPolicy.Start The time at which the Shared Access Signature becomes valid. | |
* @param {date} sharedAccessPolicy.AccessPolicy.Expiry The time at which the Shared Access Signature becomes expired. | |
* @param {string} sharedAccessPolicy.AccessPolicy.IPAddressOrRange An IP address or a range of IP addresses from which to accept requests. When specifying a range, note that the range is inclusive. | |
* @param {string} sharedAccessPolicy.AccessPolicy.Protocols The protocols permitted for a request made with the account SAS. | |
* Possible values are both HTTPS and HTTP (https,http) or HTTPS only (https). The default value is https,http. | |
* Refer to `Constants.AccountSasConstants.Protocols`. | |
* @return {string} The shared access signature. | |
*/ | |
SharedKey.prototype.generateAccountSignedQueryString = function (sharedAccessPolicy) { | |
var addIfNotNull = function (queryString, name, value) { | |
if (!azureutil.objectIsNull(name) && !azureutil.objectIsNull(value)) { | |
queryString[name] = value; | |
} | |
}; | |
var formatAccessPolicyDates = function (accessPolicy) { | |
if (!azureutil.objectIsNull(accessPolicy.Start)) { | |
if (!_.isDate(accessPolicy.Start)) { | |
accessPolicy.Start = new Date(accessPolicy.Start); | |
} | |
accessPolicy.Start = azureutil.truncatedISO8061Date(accessPolicy.Start); | |
} | |
if (!azureutil.objectIsNull(accessPolicy.Expiry)) { | |
if (!_.isDate(accessPolicy.Expiry)) { | |
accessPolicy.Expiry = new Date(accessPolicy.Expiry); | |
} | |
accessPolicy.Expiry = azureutil.truncatedISO8061Date(accessPolicy.Expiry); | |
} | |
}; | |
var queryString = {}; | |
addIfNotNull(queryString, QueryStringConstants.SIGNED_VERSION, HeaderConstants.TARGET_STORAGE_VERSION); | |
// add shared access policy params | |
if (sharedAccessPolicy.AccessPolicy) { | |
formatAccessPolicyDates(sharedAccessPolicy.AccessPolicy); | |
addIfNotNull(queryString, QueryStringConstants.SIGNED_SERVICES, sharedAccessPolicy.AccessPolicy.Services); | |
addIfNotNull(queryString, QueryStringConstants.SIGNED_RESOURCE_TYPES, sharedAccessPolicy.AccessPolicy.ResourceTypes); | |
addIfNotNull(queryString, QueryStringConstants.SIGNED_PERMISSIONS, sharedAccessPolicy.AccessPolicy.Permissions); | |
addIfNotNull(queryString, QueryStringConstants.SIGNED_START, sharedAccessPolicy.AccessPolicy.Start); | |
addIfNotNull(queryString, QueryStringConstants.SIGNED_EXPIRY, sharedAccessPolicy.AccessPolicy.Expiry); | |
addIfNotNull(queryString, QueryStringConstants.SIGNED_IP, sharedAccessPolicy.AccessPolicy.IPAddressOrRange); | |
addIfNotNull(queryString, QueryStringConstants.SIGNED_PROTOCOL, sharedAccessPolicy.AccessPolicy.Protocols); | |
} | |
// add signature | |
addIfNotNull(queryString, QueryStringConstants.SIGNATURE, this._generateAccountSharedAccessSignature(sharedAccessPolicy)); | |
return qs.stringify(queryString); | |
}; | |
/** | |
* Generates the signature part of the shared access signature for a account. | |
* For more detailed information, refer to https://msdn.microsoft.com/en-us/library/azure/mt584140.aspx | |
* | |
* @param {object} sharedAccessPolicy The shared access policy. | |
* @param {SharedAccessServices} sharedAccessPolicy.AccessPolicy.Services The services (blob, file, queue, table) for a shared access signature associated with this shared access policy. | |
* Refer to `Constants.AccountSasConstants.Services`. | |
* @param {SharedAccessResourceTypes} sharedAccessPolicy.AccessPolicy.ResourceTypes The resource type for a shared access signature associated with this shared access policy. | |
* Refer to `Constants.AccountSasConstants.ResourceTypes`. | |
* @param {SharedAccessPermissions} sharedAccessPolicy.AccessPolicy.Permissions The permissions for a shared access signature. | |
* Refer to `Constants.AccountSasConstants.Permissions`. | |
* @param {date} sharedAccessPolicy.AccessPolicy.Start The time at which the Shared Access Signature becomes valid. | |
* @param {date} sharedAccessPolicy.AccessPolicy.Expiry The time at which the Shared Access Signature becomes expired. | |
* @param {string} sharedAccessPolicy.AccessPolicy.IPAddressOrRange An IP address or a range of IP addresses from which to accept requests. When specifying a range, note that the range is inclusive. | |
* @param {string} sharedAccessPolicy.AccessPolicy.Protocols The protocols permitted for a request made with the account SAS. | |
* Possible values are both HTTPS and HTTP (https,http) or HTTPS only (https). The default value is https,http. | |
* Refer to `Constants.AccountSasConstants.Protocols`. | |
* @return {string} The signature part of the shared access signature. | |
*/ | |
SharedKey.prototype._generateAccountSharedAccessSignature = function(sharedAccessPolicy){ | |
var getvalueToAppend = function (value, noNewLine) { | |
var returnValue = ''; | |
if (!azureutil.objectIsNull(value)) { | |
returnValue = value; | |
} | |
if (noNewLine !== true) { | |
returnValue += '\n'; | |
} | |
return returnValue; | |
}; | |
var stringToSign = getvalueToAppend(this.storageAccount) + | |
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.Permissions : '') + | |
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.Services : '') + | |
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.ResourceTypes : '') + | |
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.Start : '') + | |
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.Expiry : '') + | |
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.IPAddressOrRange : '') + | |
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.Protocols : '') + | |
getvalueToAppend(HeaderConstants.TARGET_STORAGE_VERSION); | |
return this.signer.sign(stringToSign); | |
}; | |
/** | |
* Signs a request with the Authentication header. | |
* | |
* @param {WebResource} webResource The webresource to be signed. | |
* @param {function(error)} callback The callback function. | |
*/ | |
SharedKey.prototype.signRequest = function (webResource, callback) { | |
var getvalueToAppend = function (value, headerName) { | |
// Do not sign content-length 0 in 2014-08-16 and later | |
if (headerName === HeaderConstants.CONTENT_LENGTH && (azureutil.objectIsNull(value[headerName]) || value[headerName].toString() === '0')) { | |
return '\n'; | |
} else if (azureutil.objectIsNull(value) || azureutil.objectIsNull(value[headerName])) { | |
return '\n'; | |
} else { | |
return value[headerName] + '\n'; | |
} | |
}; | |
var stringToSign = | |
webResource.method + '\n' + | |
getvalueToAppend(webResource.headers, HeaderConstants.CONTENT_ENCODING) + | |
getvalueToAppend(webResource.headers, HeaderConstants.CONTENT_LANGUAGE) + | |
getvalueToAppend(webResource.headers, HeaderConstants.CONTENT_LENGTH) + | |
getvalueToAppend(webResource.headers, HeaderConstants.CONTENT_MD5) + | |
getvalueToAppend(webResource.headers, HeaderConstants.CONTENT_TYPE) + | |
getvalueToAppend(webResource.headers, HeaderConstants.DATE) + | |
getvalueToAppend(webResource.headers, HeaderConstants.IF_MODIFIED_SINCE) + | |
getvalueToAppend(webResource.headers, HeaderConstants.IF_MATCH) + | |
getvalueToAppend(webResource.headers, HeaderConstants.IF_NONE_MATCH) + | |
getvalueToAppend(webResource.headers, HeaderConstants.IF_UNMODIFIED_SINCE) + | |
getvalueToAppend(webResource.headers, HeaderConstants.RANGE) + | |
this._getCanonicalizedHeaders(webResource) + | |
this._getCanonicalizedResource(webResource); | |
var signature = this.signer.sign(stringToSign); | |
webResource.withHeader(HeaderConstants.AUTHORIZATION, 'SharedKey ' + this.storageAccount + ':' + signature); | |
callback(null); | |
}; | |
/* | |
* Retrieves the webresource's canonicalized resource string. | |
* @param {WebResource} webResource The webresource to get the canonicalized resource string from. | |
* @return {string} The canonicalized resource string. | |
*/ | |
SharedKey.prototype._getCanonicalizedResource = function (webResource) { | |
var path = '/'; | |
if (webResource.path) { | |
path = webResource.path; | |
} | |
var canonicalizedResource = '/' + this.storageAccount + path; | |
// Get the raw query string values for signing | |
var queryStringValues = webResource.queryString; | |
// Build the canonicalized resource by sorting the values by name. | |
if (queryStringValues) { | |
var paramNames = []; | |
Object.keys(queryStringValues).forEach(function (n) { | |
paramNames.push(n); | |
}); | |
paramNames = paramNames.sort(); | |
Object.keys(paramNames).forEach(function (name) { | |
canonicalizedResource += '\n' + paramNames[name] + ':' + queryStringValues[paramNames[name]]; | |
}); | |
} | |
return canonicalizedResource; | |
}; | |
/* | |
* Constructs the Canonicalized Headers string. | |
* | |
* To construct the CanonicalizedHeaders portion of the signature string, | |
* follow these steps: 1. Retrieve all headers for the resource that begin | |
* with x-ms-, including the x-ms-date header. 2. Convert each HTTP header | |
* name to lowercase. 3. Sort the headers lexicographically by header name, | |
* in ascending order. Each header may appear only once in the | |
* string. 4. Unfold the string by replacing any breaking white space with a | |
* single space. 5. Trim any white space around the colon in the header. 6. | |
* Finally, append a new line character to each canonicalized header in the | |
* resulting list. Construct the CanonicalizedHeaders string by | |
* concatenating all headers in this list into a single string. | |
* | |
* @param {object} The webresource object. | |
* @return {string} The canonicalized headers. | |
*/ | |
SharedKey.prototype._getCanonicalizedHeaders = function (webResource) { | |
// Build canonicalized headers | |
var canonicalizedHeaders = ''; | |
if (webResource.headers) { | |
var canonicalizedHeadersArray = []; | |
for (var header in webResource.headers) { | |
if (header.indexOf(HeaderConstants.PREFIX_FOR_STORAGE) === 0) { | |
var headerItem = { canonicalized: header.toLowerCase(), original: header }; | |
canonicalizedHeadersArray.push(headerItem); | |
} | |
} | |
canonicalizedHeadersArray.sort(function(a, b) { return a.canonicalized.localeCompare(b.canonicalized); }); | |
_.each(canonicalizedHeadersArray, function (currentHeaderItem) { | |
var value = webResource.headers[currentHeaderItem.original]; | |
if (!azureutil.IsNullOrEmptyOrUndefinedOrWhiteSpace(value)) { | |
canonicalizedHeaders += currentHeaderItem.canonicalized + ':' + value + '\n'; | |
} else { | |
canonicalizedHeaders += currentHeaderItem.canonicalized + ':\n'; | |
} | |
}); | |
} | |
return canonicalizedHeaders; | |
}; | |
/** | |
* Generates the query string for a shared access signature signing. | |
* | |
* @this {SharedAccessSignature} | |
* @param {string} serviceType The service type. | |
* @param {string} path The path to the resource. | |
* @param {object} sharedAccessPolicy The shared access policy. | |
* @param {string} [sharedAccessPolicy.Id] The signed identifier. | |
* @param {SharedAccessPermissions} [sharedAccessPolicy.AccessPolicy.Permissions] The permission type. | |
* @param {date} [sharedAccessPolicy.AccessPolicy.Start] The time at which the Shared Access Signature becomes valid. | |
* @param {date} [sharedAccessPolicy.AccessPolicy.Expiry] The time at which the Shared Access Signature becomes expired. | |
* @param {string} [sharedAccessPolicy.AccessPolicy.IPAddressOrRange] An IP address or a range of IP addresses from which to accept requests. When specifying a range, note that the range is inclusive. | |
* @param {string} [sharedAccessPolicy.AccessPolicy.Protocols] The protocols permitted for a request made with the account SAS. | |
* Possible values are both HTTPS and HTTP (https,http) or HTTPS only (https). The default value is https,http. | |
* @param {string} sasVersion A string indicating the desired SAS Version to use, in storage service version format. Value must be 2012-02-12 or later. | |
* @parma {ResourceTypes} [args.resourceType] The resource type, if the resource is a blob or container. Null if the resource is a queue or table. | |
* @parma {ResourceTypes} [args.tableName] The table name, if the resource is a table. Null if the resource is a blob orqueue. | |
* @parma {ResourceTypes} [args.queryString] The query string, if additional parameters are desired. | |
* @param {object} [args.headers] The optional header values to set for a blob returned wth this SAS. | |
* @param {string} [args.headers.CacheControl] The value of the Cache-Control response header to be returned when this SAS is used. | |
* @param {string} [args.headers.ContentType] The value of the Content-Type response header to be returned when this SAS is used. | |
* @param {string} [args.headers.ContentEncoding] The value of the Content-Encoding response header to be returned when this SAS is used. | |
* @param {string} [args.headers.ContentLanguage] The value of the Content-Language response header to be returned when this SAS is used. | |
* @param {string} [args.headers.ContentDisposition] The value of the Content-Disposition response header to be returned when this SAS is used. | |
* @return {object} The shared access signature query string. | |
*/ | |
SharedKey.prototype.generateSignedQueryString = function (serviceType, path, sharedAccessPolicy, sasVersion, args) { | |
var addIfNotNull = function (queryString, name, value) { | |
if (!azureutil.objectIsNull(name) && !azureutil.objectIsNull(value)) { | |
queryString[name] = value; | |
} | |
}; | |
var validateVersion = function (sasVersion) { | |
// validate and add version | |
if (azureutil.objectIsNull(sasVersion)) { | |
return HeaderConstants.TARGET_STORAGE_VERSION; | |
} else { | |
var values = _.values(CompatibleVersionConstants); | |
if (values.some(function(version) { | |
return version.toLowerCase() === sasVersion.toLowerCase(); | |
})) { | |
return sasVersion; | |
} else { | |
throw new ArgumentError('sasVersion', azureutil.stringFormat(SR.INVALID_SAS_VERSION, sasVersion, values)); | |
} | |
} | |
}; | |
var formatAccessPolicyDates = function (accessPolicy) { | |
if (!azureutil.objectIsNull(accessPolicy.Start)) { | |
if (!_.isDate(accessPolicy.Start)) { | |
accessPolicy.Start = new Date(accessPolicy.Start); | |
} | |
accessPolicy.Start = azureutil.truncatedISO8061Date(accessPolicy.Start); | |
} | |
if (!azureutil.objectIsNull(accessPolicy.Expiry)) { | |
if (!_.isDate(accessPolicy.Expiry)) { | |
accessPolicy.Expiry = new Date(accessPolicy.Expiry); | |
} | |
accessPolicy.Expiry = azureutil.truncatedISO8061Date(accessPolicy.Expiry); | |
} | |
}; | |
// set up optional args | |
var queryString; | |
var resourceType; | |
var headers; | |
var tableName; | |
if(args) { | |
queryString = args.queryString; | |
resourceType = args.resourceType; | |
tableName = args.tableName; | |
headers = args.headers; | |
} | |
if(!queryString) { | |
queryString = {}; | |
} | |
// add shared access policy params | |
if (sharedAccessPolicy.AccessPolicy) { | |
formatAccessPolicyDates(sharedAccessPolicy.AccessPolicy); | |
addIfNotNull(queryString, QueryStringConstants.SIGNED_START, sharedAccessPolicy.AccessPolicy.Start); | |
addIfNotNull(queryString, QueryStringConstants.SIGNED_EXPIRY, sharedAccessPolicy.AccessPolicy.Expiry); | |
addIfNotNull(queryString, QueryStringConstants.SIGNED_PERMISSIONS, sharedAccessPolicy.AccessPolicy.Permissions); | |
addIfNotNull(queryString, QueryStringConstants.SIGNED_IP, sharedAccessPolicy.AccessPolicy.IPAddressOrRange); | |
addIfNotNull(queryString, QueryStringConstants.SIGNED_PROTOCOL, sharedAccessPolicy.AccessPolicy.Protocols); | |
// tables only | |
addIfNotNull(queryString, QueryStringConstants.STARTPK, sharedAccessPolicy.AccessPolicy.StartPk); | |
addIfNotNull(queryString, QueryStringConstants.ENDPK, sharedAccessPolicy.AccessPolicy.EndPk); | |
addIfNotNull(queryString, QueryStringConstants.STARTRK, sharedAccessPolicy.AccessPolicy.StartRk); | |
addIfNotNull(queryString, QueryStringConstants.ENDRK, sharedAccessPolicy.AccessPolicy.EndRk); | |
} | |
// validate and add version | |
var validatedSASVersionString = validateVersion(sasVersion); | |
addIfNotNull(queryString, QueryStringConstants.SIGNED_VERSION, validatedSASVersionString); | |
// add signed identifier | |
addIfNotNull(queryString, QueryStringConstants.SIGNED_IDENTIFIER, sharedAccessPolicy.Id); | |
// blobs only | |
addIfNotNull(queryString, QueryStringConstants.SIGNED_RESOURCE, resourceType); | |
if (headers) { | |
addIfNotNull(queryString, QueryStringConstants.CACHE_CONTROL, headers.cacheControl); | |
addIfNotNull(queryString, QueryStringConstants.CONTENT_TYPE, headers.contentType); | |
addIfNotNull(queryString, QueryStringConstants.CONTENT_ENCODING, headers.contentEncoding); | |
addIfNotNull(queryString, QueryStringConstants.CONTENT_LANGUAGE, headers.contentLanguage); | |
addIfNotNull(queryString, QueryStringConstants.CONTENT_DISPOSITION, headers.contentDisposition); | |
} | |
// tables only | |
addIfNotNull(queryString, QueryStringConstants.TABLENAME, tableName); | |
// add signature | |
addIfNotNull(queryString, QueryStringConstants.SIGNATURE, this._generateSignature(serviceType, path, sharedAccessPolicy, validatedSASVersionString, {resourceType: resourceType, headers: headers, tableName: tableName})); | |
return qs.stringify(queryString); | |
}; | |
/** | |
* Generates the shared access signature for a resource. | |
* | |
* @this {SharedAccessSignature} | |
* @param {string} serviceType The service type. | |
* @param {string} path The path to the resource. | |
* @param {object} sharedAccessPolicy The shared access policy. | |
* @param {string} [sharedAccessPolicy.Id] The signed identifier. | |
* @param {SharedAccessPermissions} [sharedAccessPolicy.AccessPolicy.Permissions] The permission type. | |
* @param {date} [sharedAccessPolicy.AccessPolicy.Start] The time at which the Shared Access Signature becomes valid. | |
* @param {date} [sharedAccessPolicy.AccessPolicy.Expiry] The time at which the Shared Access Signature becomes expired. | |
* @param {string} [sharedAccessPolicy.AccessPolicy.IPAddressOrRange] An IP address or a range of IP addresses from which to accept requests. When specifying a range, note that the range is inclusive. | |
* @param {string} [sharedAccessPolicy.AccessPolicy.Protocols] The protocols permitted for a request made with the account SAS. | |
* Possible values are both HTTPS and HTTP (https,http) or HTTPS only (https). The default value is https,http. | |
* @param {string} sasVersion A string indicating the desired SAS Version to use, in storage service version format. Value must be 2012-02-12 or later. | |
* @parma {ResourceTypes} [args.resourceType] The resource type, if the resource is a blob or container. Null if the resource is a queue or table. | |
* @parma {ResourceTypes} [args.tableName] The table name, if the resource is a table. Null if the resource is a blob or queue. | |
* @param {object} [args.headers] The optional header values to set for a blob returned wth this SAS. | |
* @param {string} [args.headers.CacheControl] The value of the Cache-Control response header to be returned when this SAS is used. | |
* @param {string} [args.headers.ContentType] The value of the Content-Type response header to be returned when this SAS is used. | |
* @param {string} [args.headers.ContentEncoding] The value of the Content-Encoding response header to be returned when this SAS is used. | |
* @param {string} [args.headers.ContentLanguage] The value of the Content-Language response header to be returned when this SAS is used. | |
* @param {string} [args.headers.ContentDisposition] The value of the Content-Disposition response header to be returned when this SAS is used. | |
* @return {string} The shared access signature. | |
*/ | |
SharedKey.prototype._generateSignature = function (serviceType, path, sharedAccessPolicy, sasVersion, args) { | |
var getvalueToAppend = function (value, noNewLine) { | |
var returnValue = ''; | |
if (!azureutil.objectIsNull(value)) { | |
returnValue = value; | |
} | |
if (noNewLine !== true) { | |
returnValue += '\n'; | |
} | |
return returnValue; | |
}; | |
// set up optional args | |
var resourceType; | |
var tableName; | |
var headers; | |
if(args) { | |
resourceType = args.resourceType; | |
tableName = args.tableName; | |
headers = args.headers; | |
} | |
// Add leading slash to path | |
if (path.substr(0, 1) !== '/') { | |
path = '/' + path; | |
} | |
var canonicalizedResource; | |
if (sasVersion === CompatibleVersionConstants.FEBRUARY_2012 || sasVersion === CompatibleVersionConstants.AUGUST_2013) { | |
// Do not prepend service name for older versions | |
canonicalizedResource = '/' + this.storageAccount + path; | |
} else { | |
canonicalizedResource = '/' + serviceType + '/' + this.storageAccount + path; | |
} | |
var stringToSign = getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.Permissions : '') + | |
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.Start : '') + | |
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.Expiry : '') + | |
getvalueToAppend(canonicalizedResource) + | |
getvalueToAppend(sharedAccessPolicy.Id) + | |
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.IPAddressOrRange : '') + | |
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.Protocols : '') + | |
sasVersion; | |
if(sasVersion == CompatibleVersionConstants.FEBRUARY_2012) { | |
if(headers) { | |
throw new ArgumentError('args.headers', SR.INVALID_HEADERS); | |
} | |
} else if (resourceType) { | |
stringToSign += '\n' + | |
getvalueToAppend(headers ? headers.cacheControl : '') + | |
getvalueToAppend(headers ? headers.contentDisposition : '') + | |
getvalueToAppend(headers ? headers.contentEncoding : '') + | |
getvalueToAppend(headers ? headers.contentLanguage : '') + | |
getvalueToAppend(headers ? headers.contentType : '', true); | |
} | |
if(tableName) { | |
stringToSign += '\n' + | |
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.StartPk : '') + | |
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.StartRk : '') + | |
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.EndPk : '') + | |
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.EndRk : '', true); | |
} | |
return this.signer.sign(stringToSign); | |
}; | |
module.exports = SharedKey; | |
},{"../errors/errors":5,"../util/constants":32,"../util/sr":36,"../util/util":38,"./hmacsha256sign":19,"querystring":185,"underscore":228}],22:[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 Constants = require('../util/constants'); | |
var HeaderConstants = Constants.HeaderConstants; | |
/** | |
* Creates a new TokenSigner object. | |
* | |
* @constructor | |
* @param {TokenCredential} tokenCredential The token credential, such as containing an OAuth access token. | |
*/ | |
function TokenSigner (tokenCredential) { | |
this.tokenCredential = tokenCredential; | |
} | |
/** | |
* Signs a request with the Authentication header. | |
* | |
* @param {WebResource} webResource The webresource to be signed. | |
* @param {function(error)} callback The callback function. | |
*/ | |
TokenSigner.prototype.signRequest = function (webResource, callback) { | |
webResource.withHeader(HeaderConstants.AUTHORIZATION, 'Bearer ' + this.tokenCredential.get()); | |
callback(null); | |
}; | |
module.exports = TokenSigner; | |
},{"../util/constants":32}],23:[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":4,"../errors/errors":5,"../util/constants":32,"../util/util":38,"_process":175,"events":135,"http":210,"https":152,"os":163,"util":234}],24:[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":32,"buffer":99,"stream":209,"util":234}],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 stream = require('stream'); | |
var util = require('util'); | |
function BufferStream(buffer, options) { | |
stream.Readable.call(this, options); | |
this._buffer = buffer; | |
this._offset = 0; | |
this._chunkSize = 4 * 1024 * 1024; | |
this._bufferSize = buffer.length; | |
} | |
util.inherits(BufferStream, stream.Readable); | |
BufferStream.prototype._read = function () { | |
while (this.push(this._readNextChunk())) { | |
continue; | |
} | |
}; | |
BufferStream.prototype._readNextChunk = function () { | |
var data = null; | |
if (this._offset < this._bufferSize) { | |
var end = this._offset + this._chunkSize; | |
end = end > this._bufferSize ? this._bufferSize : end; | |
data = this._buffer.slice(this._offset, end); | |
this._offset = end; | |
} | |
return data; | |
}; | |
module.exports = BufferStream; | |
},{"stream":209,"util":234}],26:[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":99}],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. | |
// | |
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":10,"../util/constants":32,"../util/util":38,"buffer":99,"stream":209,"util":234}],28:[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":38,"./chunkstream":27,"events":135,"util":234}],29:[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 azureCommon = require('./../common.core'); | |
var azureutil = azureCommon.util; | |
var Constants = require('./../util/constants'); | |
var EventEmitter = require('events').EventEmitter; | |
/** | |
* Range stream | |
*/ | |
function RangeStream(serviceClient, container, blob, options) { | |
this.serviceClient = serviceClient; | |
this._emitter = new EventEmitter(); | |
this._paused = false; | |
this._emittedAll = false; | |
this._emittedRangeIndex = 0; | |
this._rangelist = []; | |
this._resourcePath = []; | |
this._isEmitting = false; | |
this._rangeStreamEnded = false; | |
this._lengthHeader = Constants.HeaderConstants.CONTENT_LENGTH; | |
this._minRangeSize = Constants.BlobConstants.MIN_WRITE_PAGE_SIZE_IN_BYTES; | |
this._maxRangeSize = Constants.BlobConstants.DEFAULT_WRITE_PAGE_SIZE_IN_BYTES; | |
if (options.rangeStart) { | |
this._startOffset = options.rangeStart; | |
} else { | |
this._startOffset = 0; | |
} | |
this._dataOffset = this._startOffset; | |
if (options.rangeEnd) { | |
this._endOffset = options.rangeEnd; | |
} else { | |
this._endOffset = Number.MAX_VALUE; | |
} | |
if (container) { | |
this._resourcePath.push(container); | |
} | |
if (blob) { | |
this._resourcePath.push(blob); | |
} | |
} | |
/** | |
* Get range list | |
*/ | |
RangeStream.prototype.list = function (options, callback) { | |
var start = this._startOffset; | |
var end; | |
var singleRangeSize = Constants.BlobConstants.MAX_SINGLE_GET_PAGE_RANGE_SIZE; | |
if (this._listFunc === undefined) { | |
// the default function puts the whole blob into a single list item | |
this._listFunc = this._defaultListFunc; | |
end = this._endOffset; | |
} else { | |
end = Math.min(this._startOffset + singleRangeSize - 1, this._endOffset); | |
} | |
options.rangeStart = start; | |
if (end != Number.MAX_VALUE) { | |
options.rangeEnd = end; | |
} | |
var self = this; | |
var onList = function (error, ranges, response) { | |
if (error) { | |
callback(error); | |
} else { | |
if (self._rangeStreamEnded) { | |
return; | |
} | |
var totalSize = parseInt(response.headers[self._lengthHeader], 10); | |
var endOffset = Math.min(totalSize - 1, self._endOffset); | |
var rangeEnd = Math.min(end, endOffset); | |
if (!ranges.length) { | |
// convert single object to range | |
// start >= 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":3,"./../util/constants":32,"_process":175,"events":135}],30:[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 EventEmitter = require('events'); | |
var util = require('util'); | |
var azureutil = require('../util/util'); | |
/** | |
* Blob upload/download speed summary. | |
* Trigger 'progress' event every progress updates. | |
*/ | |
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; | |
} | |
util.inherits(SpeedSummary, EventEmitter); | |
/** | |
* 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); | |
var that = this; | |
process.nextTick(function () { | |
that.emit('progress'); | |
}); | |
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; | |
}).call(this,require('_process')) | |
},{"../util/util":38,"_process":175,"events":135,"util":234}],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. | |
// | |
// 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; | |
}; | |
},{}],32:[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. | |
// | |
// Expose 'Constants'. | |
exports = module.exports; | |
var storageDnsSuffix = process.env.AZURE_STORAGE_DNS_SUFFIX || 'core.windows.net'; | |
/** | |
* Defines constants. | |
*/ | |
var Constants = { | |
/* | |
* Specifies the value to use for UserAgent header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
USER_AGENT_PRODUCT_NAME: 'Azure-Storage', | |
/* | |
* Specifies the value to use for UserAgent header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
USER_AGENT_PRODUCT_VERSION: '2.9.0-preview', | |
/** | |
* The number of default concurrent requests for parallel operation. | |
* | |
* @const | |
* @type {int} | |
*/ | |
DEFAULT_PARALLEL_OPERATION_THREAD_COUNT: 5, | |
/** | |
* The value of default socket reuse for batch operation. | |
* | |
* @const | |
* @type {boolean} | |
*/ | |
DEFAULT_ENABLE_REUSE_SOCKET: true, | |
/** | |
* Constant representing a kilobyte (Non-SI version). | |
* | |
* @const | |
* @type {int} | |
*/ | |
KB: 1024, | |
/** | |
* Constant representing a megabyte (Non-SI version). | |
* | |
* @const | |
* @type {int} | |
*/ | |
MB: 1024 * 1024, | |
/** | |
* Constant representing a gigabyte (Non-SI version). | |
* | |
* @const | |
* @type {int} | |
*/ | |
GB: 1024 * 1024 * 1024, | |
/** | |
* Specifies HTTP. | |
* | |
* @const | |
* @type {string} | |
*/ | |
HTTP: 'http:', | |
/** | |
* Specifies HTTPS. | |
* | |
* @const | |
* @type {string} | |
*/ | |
HTTPS: 'https:', | |
/** | |
* Default HTTP port. | |
* | |
* @const | |
* @type {int} | |
*/ | |
DEFAULT_HTTP_PORT: 80, | |
/** | |
* Default HTTPS port. | |
* | |
* @const | |
* @type {int} | |
*/ | |
DEFAULT_HTTPS_PORT: 443, | |
/** | |
* Default client request timeout in milliseconds. | |
* Integer containing the number of milliseconds to wait for a server to send response headers (and start the response body) before aborting the request. | |
* 2 minutes by default. | |
* | |
* @const | |
* @type {int} | |
*/ | |
DEFAULT_CLIENT_REQUEST_TIMEOUT_IN_MS: 120000, | |
/** | |
* Marker for atom metadata. | |
* | |
* @const | |
* @type {string} | |
*/ | |
XML_METADATA_MARKER: '$', | |
/** | |
* Marker for atom value. | |
* | |
* @const | |
* @type {string} | |
*/ | |
XML_VALUE_MARKER: '_', | |
/** | |
* Defines the service types indicators. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
ServiceType: { | |
Blob: 'blob', | |
Queue: 'queue', | |
Table: 'table', | |
File: 'file' | |
}, | |
/** | |
* Specifies the location used to indicate which location the operation can be performed against. | |
* | |
* @const | |
* @enum {int} | |
*/ | |
RequestLocationMode: { | |
PRIMARY_ONLY: 0, | |
SECONDARY_ONLY: 1, | |
PRIMARY_OR_SECONDARY: 2 | |
}, | |
/** | |
* Represents a storage service location. | |
* | |
* @const | |
* @enum {int} | |
*/ | |
StorageLocation: { | |
PRIMARY: 0, | |
SECONDARY: 1 | |
}, | |
/** | |
* Defines constants for use with account SAS. | |
*/ | |
AccountSasConstants:{ | |
/** | |
* Permission types. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
Permissions: { | |
READ: 'r', | |
ADD: 'a', | |
CREATE: 'c', | |
UPDATE: 'u', | |
PROCESS: 'p', | |
WRITE: 'w', | |
DELETE: 'd', | |
LIST: 'l' | |
}, | |
/** | |
* Services types. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
Services: { | |
BLOB: 'b', | |
FILE: 'f', | |
QUEUE: 'q', | |
TABLE: 't' | |
}, | |
/** | |
* Resources types. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
Resources: { | |
SERVICE: 's', | |
CONTAINER: 'c', | |
OBJECT: 'o' | |
}, | |
/** | |
* Protocols types. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
Protocols: { | |
HTTPSONLY: 'https', | |
HTTPSORHTTP: 'https,http' | |
} | |
}, | |
/** | |
* Defines constants for use with shared access policies. | |
*/ | |
AclConstants: { | |
/** | |
* XML element for an access policy. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ACCESS_POLICY: 'AccessPolicy', | |
/** | |
* XML element for the end time of an access policy. | |
* | |
* @const | |
* @type {string} | |
*/ | |
EXPIRY: 'Expiry', | |
/** | |
* XML attribute for IDs. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ID: 'Id', | |
/** | |
* XML element for the permission of an access policy. | |
* | |
* @const | |
* @type {string} | |
*/ | |
PERMISSION: 'Permission', | |
/** | |
* XML element for a signed identifier. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SIGNED_IDENTIFIER_ELEMENT: 'SignedIdentifier', | |
/** | |
* XML element for signed identifiers. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SIGNED_IDENTIFIERS_ELEMENT: 'SignedIdentifiers', | |
/** | |
* XML element for the start time of an access policy. | |
* | |
* @const | |
* @type {string} | |
*/ | |
START: 'Start' | |
}, | |
/** | |
* Defines constants for use with service properties. | |
*/ | |
ServicePropertiesConstants: { | |
/** | |
* XML element for storage service properties. | |
* | |
* @const | |
* @type {string} | |
*/ | |
STORAGE_SERVICE_PROPERTIES_ELEMENT: 'StorageServiceProperties', | |
/** | |
* Default analytics version to send for logging, hour metrics and minute metrics. | |
* | |
* @const | |
* @type {string} | |
*/ | |
DEFAULT_ANALYTICS_VERSION: '1.0', | |
/** | |
* XML element for logging. | |
* | |
* @const | |
* @type {string} | |
*/ | |
LOGGING_ELEMENT: 'Logging', | |
/** | |
* XML element for version. | |
* | |
* @const | |
* @type {string} | |
*/ | |
VERSION_ELEMENT: 'Version', | |
/** | |
* XML element for delete. | |
* | |
* @const | |
* @type {string} | |
*/ | |
DELETE_ELEMENT: 'Delete', | |
/** | |
* XML element for read. | |
* | |
* @const | |
* @type {string} | |
*/ | |
READ_ELEMENT: 'Read', | |
/** | |
* XML element for write. | |
* | |
* @const | |
* @type {string} | |
*/ | |
WRITE_ELEMENT: 'Write', | |
/** | |
* XML element for retention policy. | |
* | |
* @const | |
* @type {string} | |
*/ | |
RETENTION_POLICY_ELEMENT: 'RetentionPolicy', | |
/** | |
* XML element for enabled. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ENABLED_ELEMENT: 'Enabled', | |
/** | |
* XML element for days. | |
* | |
* @const | |
* @type {string} | |
*/ | |
DAYS_ELEMENT: 'Days', | |
/** | |
* XML element for HourMetrics. | |
* | |
* @const | |
* @type {string} | |
*/ | |
HOUR_METRICS_ELEMENT: 'HourMetrics', | |
/** | |
* XML element for MinuteMetrics. | |
* | |
* @const | |
* @type {string} | |
*/ | |
MINUTE_METRICS_ELEMENT: 'MinuteMetrics', | |
/** | |
* XML element for Cors. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CORS_ELEMENT: 'Cors', | |
/** | |
* XML element for CorsRule. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CORS_RULE_ELEMENT: 'CorsRule', | |
/** | |
* XML element for AllowedOrigins. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ALLOWED_ORIGINS_ELEMENT: 'AllowedOrigins', | |
/** | |
* XML element for AllowedMethods. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ALLOWED_METHODS_ELEMENT: 'AllowedMethods', | |
/** | |
* XML element for MaxAgeInSeconds. | |
* | |
* @const | |
* @type {string} | |
*/ | |
MAX_AGE_IN_SECONDS_ELEMENT: 'MaxAgeInSeconds', | |
/** | |
* XML element for ExposedHeaders. | |
* | |
* @const | |
* @type {string} | |
*/ | |
EXPOSED_HEADERS_ELEMENT: 'ExposedHeaders', | |
/** | |
* XML element for AllowedHeaders. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ALLOWED_HEADERS_ELEMENT: 'AllowedHeaders', | |
/** | |
* XML element for IncludeAPIs. | |
* | |
* @const | |
* @type {string} | |
*/ | |
INCLUDE_APIS_ELEMENT: 'IncludeAPIs', | |
/** | |
* XML element for DefaultServiceVersion. | |
* | |
* @const | |
* @type {string} | |
*/ | |
DEFAULT_SERVICE_VERSION_ELEMENT: 'DefaultServiceVersion', | |
/** | |
* XML element for DeleteRetentionPolicy. | |
* | |
* @const | |
* @type {string} | |
*/ | |
DEFAULT_DELETE_RETENTION_POLICY_ELEMENT: 'DeleteRetentionPolicy' | |
}, | |
/** | |
* Defines constants for use with blob operations. | |
*/ | |
BlobConstants: { | |
/** | |
* XML element for the latest. | |
* | |
* @const | |
* @type {string} | |
*/ | |
LATEST_ELEMENT: 'Latest', | |
/** | |
* XML element for uncommitted blocks. | |
* | |
* @const | |
* @type {string} | |
*/ | |
UNCOMMITTED_ELEMENT: 'Uncommitted', | |
/** | |
* XML element for a block list. | |
* | |
* @const | |
* @type {string} | |
*/ | |
BLOCK_LIST_ELEMENT: 'BlockList', | |
/** | |
* XML element for committed blocks. | |
* | |
* @const | |
* @type {string} | |
*/ | |
COMMITTED_ELEMENT: 'Committed', | |
/** | |
* The default write page size, in bytes, used by blob streams. | |
* | |
* @const | |
* @type {int} | |
*/ | |
DEFAULT_WRITE_PAGE_SIZE_IN_BYTES: 4 * 1024 * 1024, | |
/** | |
* The minimum write page size, in bytes, used by blob streams. | |
* | |
* @const | |
* @type {int} | |
*/ | |
MIN_WRITE_PAGE_SIZE_IN_BYTES: 2 * 1024 * 1024, | |
/** | |
* The default maximum size, in bytes, of a blob before it must be separated into blocks. | |
* | |
* @const | |
* @type {int} | |
*/ | |
DEFAULT_SINGLE_BLOB_PUT_THRESHOLD_IN_BYTES: 32 * 1024 * 1024, | |
/** | |
* The default write block size, in bytes, used by blob streams. | |
* | |
* @const | |
* @type {int} | |
*/ | |
DEFAULT_WRITE_BLOCK_SIZE_IN_BYTES: 4 * 1024 * 1024, | |
/** | |
* The default critical memory limitation in 32bit Node.js environment, in bytes. | |
* | |
* @const | |
* @type {int} | |
*/ | |
DEFAULT_CRITICAL_MEMORY_LIMITATION_32_IN_BYTES: 800 * 1024 * 1024, | |
/** | |
* The default critical memory limitation in browser environment, in bytes. | |
* | |
* @const | |
* @type {int} | |
*/ | |
DEFAULT_CRITICAL_MEMORY_LIMITATION_BROWSER_IN_BYTES: 1 * 1024 * 1024 * 1024, | |
/** | |
* The default minimum memory usage in browser environment, in bytes. | |
* | |
* @const | |
* @type {int} | |
*/ | |
DEFAULT_MINIMUM_MEMORY_USAGE_BROWSER_IN_BYTES: 4 * 1024 * 1024, | |
/** | |
* The maximum size of a single block of block blob. | |
* | |
* @const | |
* @type {int} | |
*/ | |
MAX_BLOCK_BLOB_BLOCK_SIZE: 100 * 1024 * 1024, | |
/** | |
* The maximum size of a single block of append blob. | |
* | |
* @const | |
* @type {int} | |
*/ | |
MAX_APPEND_BLOB_BLOCK_SIZE: 4 * 1024 * 1024, | |
/** | |
* The maximum size, in bytes, of a blob before it must be separated into blocks. | |
* | |
* @const | |
* @type {int} | |
*/ | |
MAX_SINGLE_UPLOAD_BLOB_SIZE_IN_BYTES: 64 * 1024 * 1024, | |
/** | |
* The maximum range get size when requesting for a contentMD5. | |
* | |
* @const | |
* @type {int} | |
*/ | |
MAX_RANGE_GET_SIZE_WITH_MD5 : 4 * 1024 * 1024, | |
/** | |
* The maximum page range size for a page update operation. | |
* | |
* @const | |
* @type {int} | |
*/ | |
MAX_UPDATE_PAGE_SIZE : 4 * 1024 * 1024, | |
/** | |
* The maximum buffer size for writing a stream buffer. | |
* | |
* @const | |
* @type {int} | |
*/ | |
MAX_QUEUED_WRITE_DISK_BUFFER_SIZE : 64 * 1024 * 1024, | |
/** | |
* Max size for single get page range. The max value should be 150MB. | |
* http://blogs.msdn.com/b/windowsazurestorage/archive/2012/03/26/getting-the-page-ranges-of-a-large-page-blob-in-segments.aspx | |
* | |
* @const | |
* @type {int} | |
*/ | |
MAX_SINGLE_GET_PAGE_RANGE_SIZE : 37 * 4 * 1024 * 1024, | |
/** | |
* The size of a page, in bytes, in a page blob. | |
* | |
* @const | |
* @type {int} | |
*/ | |
PAGE_SIZE: 512, | |
/** | |
* Resource types. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
ResourceTypes: { | |
CONTAINER: 'c', | |
BLOB: 'b' | |
}, | |
/** | |
* List blob types. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
ListBlobTypes: { | |
Blob: 'b', | |
Directory: 'd' | |
}, | |
/** | |
* Put page write options | |
* | |
* @const | |
* @enum {string} | |
*/ | |
PageWriteOptions: { | |
UPDATE: 'update', | |
CLEAR: 'clear' | |
}, | |
/** | |
* Blob types | |
* | |
* @const | |
* @enum {string} | |
*/ | |
BlobTypes: { | |
BLOCK: 'BlockBlob', | |
PAGE: 'PageBlob', | |
APPEND: 'AppendBlob' | |
}, | |
/** | |
* Blob lease constants | |
* | |
* @const | |
* @enum {string} | |
*/ | |
LeaseOperation: { | |
ACQUIRE: 'acquire', | |
RENEW: 'renew', | |
CHANGE: 'change', | |
RELEASE: 'release', | |
BREAK: 'break' | |
} | |
}, | |
/** | |
* Defines constants for use with file operations. | |
*/ | |
FileConstants: { | |
/** | |
* The default write size, in bytes, used by file streams. | |
* | |
* @const | |
* @type {int} | |
*/ | |
DEFAULT_WRITE_SIZE_IN_BYTES: 4 * 1024 * 1024, | |
/** | |
* The maximum range size when requesting for a contentMD5. | |
* | |
* @const | |
* @type {int} | |
*/ | |
MAX_RANGE_GET_SIZE_WITH_MD5 : 4 * 1024 * 1024, | |
/** | |
* The maximum range size for a file update operation. | |
* | |
* @const | |
* @type {int} | |
*/ | |
MAX_UPDATE_FILE_SIZE : 4 * 1024 * 1024, | |
/** | |
* The default minimum size, in bytes, of a file when it must be separated into ranges. | |
* | |
* @const | |
* @type {int} | |
*/ | |
DEFAULT_SINGLE_FILE_GET_THRESHOLD_IN_BYTES: 32 * 1024 * 1024, | |
/** | |
* The minimum write file size, in bytes, used by file streams. | |
* | |
* @const | |
* @type {int} | |
*/ | |
MIN_WRITE_FILE_SIZE_IN_BYTES: 2 * 1024 * 1024, | |
/** | |
* Put range write options | |
* | |
* @const | |
* @enum {string} | |
*/ | |
RangeWriteOptions: { | |
UPDATE: 'update', | |
CLEAR: 'clear' | |
}, | |
/** | |
* Resource types. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
ResourceTypes: { | |
SHARE: 's', | |
FILE: 'f' | |
} | |
}, | |
/** | |
* Defines constants for use with queue storage. | |
*/ | |
QueueConstants: { | |
/** | |
* XML element for QueueMessage. | |
* | |
* @const | |
* @type {string} | |
*/ | |
QUEUE_MESSAGE_ELEMENT: 'QueueMessage', | |
/** | |
* XML element for MessageText. | |
* | |
* @const | |
* @type {string} | |
*/ | |
MESSAGE_TEXT_ELEMENT: 'MessageText' | |
}, | |
/** | |
* Defines constants for use with table storage. | |
*/ | |
TableConstants: { | |
/** | |
* The changeset response delimiter. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CHANGESET_DELIMITER: '--changesetresponse_', | |
/** | |
* The batch response delimiter. | |
* | |
* @const | |
* @type {string} | |
*/ | |
BATCH_DELIMITER: '--batchresponse_', | |
/** | |
* The next continuation row key token. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CONTINUATION_NEXT_ROW_KEY: 'x-ms-continuation-nextrowkey', | |
/** | |
* The next continuation partition key token. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CONTINUATION_NEXT_PARTITION_KEY: 'x-ms-continuation-nextpartitionkey', | |
/** | |
* The next continuation table name token. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CONTINUATION_NEXT_TABLE_NAME: 'x-ms-continuation-nexttablename', | |
/** | |
* The next row key query string argument. | |
* | |
* @const | |
* @type {string} | |
*/ | |
NEXT_ROW_KEY: 'NextRowKey', | |
/** | |
* The next partition key query string argument. | |
* | |
* @const | |
* @type {string} | |
*/ | |
NEXT_PARTITION_KEY: 'NextPartitionKey', | |
/** | |
* The next table name query string argument. | |
* | |
* @const | |
* @type {string} | |
*/ | |
NEXT_TABLE_NAME: 'NextTableName', | |
/** | |
* Prefix of the odata properties returned in a JSON query. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ODATA_PREFIX: 'odata.', | |
/** | |
* Constant representing the string following a type annotation in a JSON table query. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ODATA_TYPE_SUFFIX: '@odata.type', | |
/** | |
* Constant representing the property where the odata metadata elements are stored. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ODATA_METADATA_MARKER: '.metadata', | |
/** | |
* Constant representing the value for an entity property. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ODATA_VALUE_MARKER: '_', | |
/** | |
* Constant representing the type for an entity property. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ODATA_TYPE_MARKER: '$', | |
/** | |
* The value to set the maximum data service version header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
DEFAULT_DATA_SERVICE_VERSION: '3.0;NetFx', | |
/** | |
* The name of the property that stores the table name. | |
* | |
* @const | |
* @type {string} | |
*/ | |
TABLE_NAME: 'TableName', | |
/** | |
* The name of the special table used to store tables. | |
* | |
* @const | |
* @type {string} | |
*/ | |
TABLE_SERVICE_TABLE_NAME: 'Tables', | |
/** | |
* Operations. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
Operations: { | |
RETRIEVE: 'RETRIEVE', | |
INSERT: 'INSERT', | |
REPLACE: 'REPLACE', | |
MERGE: 'MERGE', | |
DELETE: 'DELETE', | |
INSERT_OR_REPLACE: 'INSERT_OR_REPLACE', | |
INSERT_OR_MERGE: 'INSERT_OR_MERGE' | |
} | |
}, | |
/** | |
* Defines constants for use with HTTP headers. | |
*/ | |
HeaderConstants: { | |
/** | |
* The accept ranges header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ACCEPT_RANGES: 'accept_ranges', | |
/** | |
* The content transfer encoding header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CONTENT_TRANSFER_ENCODING: 'content-transfer-encoding', | |
/** | |
* The transfer encoding header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
TRANSFER_ENCODING: 'transfer-encoding', | |
/** | |
* The server header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SERVER: 'server', | |
/** | |
* The location header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
LOCATION: 'location', | |
/** | |
* The Last-Modified header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
LAST_MODIFIED: 'Last-Modified', | |
/** | |
* The creation time header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CREATION_TIME: 'x-ms-creation-time', | |
/** | |
* The data service version. | |
* | |
* @const | |
* @type {string} | |
*/ | |
DATA_SERVICE_VERSION: 'dataserviceversion', | |
/** | |
* The maximum data service version. | |
* | |
* @const | |
* @type {string} | |
*/ | |
MAX_DATA_SERVICE_VERSION: 'maxdataserviceversion', | |
/** | |
* The master Windows Azure Storage header prefix. | |
* | |
* @const | |
* @type {string} | |
*/ | |
PREFIX_FOR_STORAGE: 'x-ms-', | |
/** | |
* The client request Id header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CLIENT_REQUEST_ID: 'x-ms-client-request-id', | |
/** | |
* The header that specifies the approximate message count of a queue. | |
* | |
* @const | |
* @type {string} | |
*/ | |
APPROXIMATE_MESSAGES_COUNT: 'x-ms-approximate-messages-count', | |
/** | |
* The Authorization header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
AUTHORIZATION: 'authorization', | |
/** | |
* The header that is used to avoid browser cache. | |
* | |
* @const | |
* @type {string} | |
*/ | |
FORCE_NO_CACHE_IN_BROWSER: '_', | |
/** | |
* The header that specifies public access to blobs. | |
* | |
* @const | |
* @type {string} | |
*/ | |
BLOB_PUBLIC_ACCESS: 'x-ms-blob-public-access', | |
/** | |
* The header that specifies container immutability policy. | |
* | |
* @const | |
* @type {boolean} | |
*/ | |
HAS_IMMUTABILITY_POLICY: 'x-ms-has-immutability-policy', | |
/** | |
* The header that specifies container has legal hold. | |
* | |
* @const | |
* @type {boolean} | |
*/ | |
HAS_LEGAL_HOLD: 'x-ms-has-legal-hold', | |
/** | |
* The header for the blob type. | |
* | |
* @const | |
* @type {string} | |
*/ | |
BLOB_TYPE: 'x-ms-blob-type', | |
/** | |
* The header for the type. | |
* | |
* @const | |
* @type {string} | |
*/ | |
TYPE: 'x-ms-type', | |
/** | |
* Specifies the block blob type. | |
* | |
* @const | |
* @type {string} | |
*/ | |
BLOCK_BLOB: 'blockblob', | |
/** | |
* The CacheControl header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CACHE_CONTROL: 'cache-control', | |
/** | |
* The header that specifies blob caching control. | |
* | |
* @const | |
* @type {string} | |
*/ | |
BLOB_CACHE_CONTROL: 'x-ms-blob-cache-control', | |
/** | |
* The header that specifies caching control. | |
* | |
* @const | |
* @type {string} | |
*/ | |
FILE_CACHE_CONTROL: 'x-ms-cache-control', | |
/** | |
* The copy status. | |
* | |
* @const | |
* @type {string} | |
*/ | |
COPY_STATUS: 'x-ms-copy-status', | |
/** | |
* The copy completion time | |
* | |
* @const | |
* @type {string} | |
*/ | |
COPY_COMPLETION_TIME: 'x-ms-copy-completion-time', | |
/** | |
* The copy status message | |
* | |
* @const | |
* @type {string} | |
*/ | |
COPY_STATUS_DESCRIPTION: 'x-ms-copy-status-description', | |
/** | |
* The copy identifier. | |
* | |
* @const | |
* @type {string} | |
*/ | |
COPY_ID: 'x-ms-copy-id', | |
/** | |
* Progress of any copy operation | |
* | |
* @const | |
* @type {string} | |
*/ | |
COPY_PROGRESS: 'x-ms-copy-progress', | |
/** | |
* The copy action. | |
* | |
* @const | |
* @type {string} | |
*/ | |
COPY_ACTION: 'x-ms-copy-action', | |
/** | |
* Flag if the blob is incremental copy blob. | |
* | |
* @const | |
* @type {string} | |
*/ | |
INCREMENTAL_COPY: 'x-ms-incremental-copy', | |
/** | |
* Snapshot time of the last successful incremental copy snapshot for this blob. | |
* | |
* @const | |
* @type {string} | |
*/ | |
COPY_DESTINATION_SNAPSHOT: 'x-ms-copy-destination-snapshot', | |
/** | |
* The ContentID header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CONTENT_ID: 'content-id', | |
/** | |
* The ContentEncoding header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CONTENT_ENCODING: 'content-encoding', | |
/** | |
* The header that specifies blob content encoding. | |
* | |
* @const | |
* @type {string} | |
*/ | |
BLOB_CONTENT_ENCODING: 'x-ms-blob-content-encoding', | |
/** | |
* The header that specifies content encoding. | |
* | |
* @const | |
* @type {string} | |
*/ | |
FILE_CONTENT_ENCODING: 'x-ms-content-encoding', | |
/** | |
* The ContentLangauge header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CONTENT_LANGUAGE: 'content-language', | |
/** | |
* The header that specifies blob content language. | |
* | |
* @const | |
* @type {string} | |
*/ | |
BLOB_CONTENT_LANGUAGE: 'x-ms-blob-content-language', | |
/** | |
* The header that specifies content language. | |
* | |
* @const | |
* @type {string} | |
*/ | |
FILE_CONTENT_LANGUAGE: 'x-ms-content-language', | |
/** | |
* The ContentLength header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CONTENT_LENGTH: 'content-length', | |
/** | |
* The header that specifies blob content length. | |
* | |
* @const | |
* @type {string} | |
*/ | |
BLOB_CONTENT_LENGTH: 'x-ms-blob-content-length', | |
/** | |
* The header that specifies content length. | |
* | |
* @const | |
* @type {string} | |
*/ | |
FILE_CONTENT_LENGTH: 'x-ms-content-length', | |
/** | |
* The ContentDisposition header. | |
* @const | |
* @type {string} | |
*/ | |
CONTENT_DISPOSITION: 'content-disposition', | |
/** | |
* The header that specifies blob content disposition. | |
* | |
* @const | |
* @type {string} | |
*/ | |
BLOB_CONTENT_DISPOSITION: 'x-ms-blob-content-disposition', | |
/** | |
* The header that specifies content disposition. | |
* | |
* @const | |
* @type {string} | |
*/ | |
FILE_CONTENT_DISPOSITION: 'x-ms-content-disposition', | |
/** | |
* The ContentMD5 header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CONTENT_MD5: 'content-md5', | |
/** | |
* The header that specifies blob content MD5. | |
* | |
* @const | |
* @type {string} | |
*/ | |
BLOB_CONTENT_MD5: 'x-ms-blob-content-md5', | |
/** | |
* The header that specifies content MD5. | |
* | |
* @const | |
* @type {string} | |
*/ | |
FILE_CONTENT_MD5: 'x-ms-content-md5', | |
/** | |
* The ContentRange header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CONTENT_RANGE: 'cache-range', | |
/** | |
* The ContentType header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CONTENT_TYPE: 'content-type', | |
/** | |
* The header that specifies blob content type. | |
* | |
* @const | |
* @type {string} | |
*/ | |
BLOB_CONTENT_TYPE: 'x-ms-blob-content-type', | |
/** | |
* The header that specifies content type. | |
* | |
* @const | |
* @type {string} | |
*/ | |
FILE_CONTENT_TYPE: 'x-ms-content-type', | |
/** | |
* The header for copy source. | |
* | |
* @const | |
* @type {string} | |
*/ | |
COPY_SOURCE: 'x-ms-copy-source', | |
/** | |
* The header that specifies the date. | |
* | |
* @const | |
* @type {string} | |
*/ | |
DATE: 'date', | |
/** | |
* The header that specifies the date. | |
* | |
* @const | |
* @type {string} | |
*/ | |
MS_DATE: 'x-ms-date', | |
/** | |
* The header to delete snapshots. | |
* | |
* @const | |
* @type {string} | |
*/ | |
DELETE_SNAPSHOT: 'x-ms-delete-snapshots', | |
/** | |
* The ETag header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ETAG: 'etag', | |
/** | |
* The IfMatch header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
IF_MATCH: 'if-match', | |
/** | |
* The IfModifiedSince header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
IF_MODIFIED_SINCE: 'if-modified-since', | |
/** | |
* The IfNoneMatch header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
IF_NONE_MATCH: 'if-none-match', | |
/** | |
* The IfUnmodifiedSince header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
IF_UNMODIFIED_SINCE: 'if-unmodified-since', | |
/** | |
* Specifies snapshots are to be included. | |
* | |
* @const | |
* @type {string} | |
*/ | |
INCLUDE_SNAPSHOTS_VALUE: 'include', | |
/** | |
* Specifies that the content-type is JSON. | |
* | |
* @const | |
* @type {string} | |
*/ | |
JSON_CONTENT_TYPE_VALUE: 'application/json;', | |
/** | |
* The header that specifies lease ID. | |
* | |
* @const | |
* @type {string} | |
*/ | |
LEASE_ID: 'x-ms-lease-id', | |
/** | |
* The header that specifies the lease break period. | |
* | |
* @const | |
* @type {string} | |
*/ | |
LEASE_BREAK_PERIOD: 'x-ms-lease-break-period', | |
/** | |
* The header that specifies the proposed lease identifier. | |
* | |
* @const | |
* @type {string} | |
*/ | |
PROPOSED_LEASE_ID: 'x-ms-proposed-lease-id', | |
/** | |
* The header that specifies the lease duration. | |
* | |
* @const | |
* @type {string} | |
*/ | |
LEASE_DURATION: 'x-ms-lease-duration', | |
/** | |
* The header that specifies the source lease ID. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SOURCE_LEASE_ID: 'x-ms-source-lease-id', | |
/** | |
* The header that specifies lease time. | |
* | |
* @const | |
* @type {string} | |
*/ | |
LEASE_TIME: 'x-ms-lease-time', | |
/** | |
* The header that specifies lease status. | |
* | |
* @const | |
* @type {string} | |
*/ | |
LEASE_STATUS: 'x-ms-lease-status', | |
/** | |
* The header that specifies lease state. | |
* | |
* @const | |
* @type {string} | |
*/ | |
LEASE_STATE: 'x-ms-lease-state', | |
/** | |
* Specifies the page blob type. | |
* | |
* @const | |
* @type {string} | |
*/ | |
PAGE_BLOB: 'PageBlob', | |
/** | |
* The header that specifies page write mode. | |
* | |
* @const | |
* @type {string} | |
*/ | |
PAGE_WRITE: 'x-ms-page-write', | |
/** | |
* The header that specifies file range write mode. | |
* | |
* @const | |
* @type {string} | |
*/ | |
FILE_WRITE: 'x-ms-write', | |
/** | |
* The header that specifies whether the response should include the inserted entity. | |
* | |
* @const | |
* @type {string} | |
*/ | |
PREFER: 'Prefer', | |
/** | |
* The header value which specifies that the response should include the inserted entity. | |
* | |
* @const | |
* @type {string} | |
*/ | |
PREFER_CONTENT: 'return-content', | |
/** | |
* The header value which specifies that the response should not include the inserted entity. | |
* | |
* @const | |
* @type {string} | |
*/ | |
PREFER_NO_CONTENT: 'return-no-content', | |
/** | |
* The header prefix for metadata. | |
* | |
* @const | |
* @type {string} | |
*/ | |
PREFIX_FOR_STORAGE_METADATA: 'x-ms-meta-', | |
/** | |
* The header prefix for properties. | |
* | |
* @const | |
* @type {string} | |
*/ | |
PREFIX_FOR_STORAGE_PROPERTIES: 'x-ms-prop-', | |
/** | |
* The Range header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
RANGE: 'Range', | |
/** | |
* The header that specifies if the request will populate the ContentMD5 header for range gets. | |
* | |
* @const | |
* @type {string} | |
*/ | |
RANGE_GET_CONTENT_MD5: 'x-ms-range-get-content-md5', | |
/** | |
* The format string for specifying ranges. | |
* | |
* @const | |
* @type {string} | |
*/ | |
RANGE_HEADER_FORMAT: 'bytes:%d-%d', | |
/** | |
* The header that indicates the request ID. | |
* | |
* @const | |
* @type {string} | |
*/ | |
REQUEST_ID: 'x-ms-request-id', | |
/** | |
* The header for specifying the sequence number. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SEQUENCE_NUMBER: 'x-ms-blob-sequence-number', | |
/** | |
* The header for specifying the If-Sequence-Number-EQ condition. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SEQUENCE_NUMBER_EQUAL: 'x-ms-if-sequence-number-eq', | |
/** | |
* The header for specifying the If-Sequence-Number-LT condition. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SEQUENCE_NUMBER_LESS_THAN: 'x-ms-if-sequence-number-lt', | |
/** | |
* The header for specifying the If-Sequence-Number-LE condition. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SEQUENCE_NUMBER_LESS_THAN_OR_EQUAL: 'x-ms-if-sequence-number-le', | |
/** | |
* The header that specifies sequence number action. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SEQUENCE_NUMBER_ACTION: 'x-ms-sequence-number-action', | |
/** | |
* The header for the blob content length. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SIZE: 'x-ms-blob-content-length', | |
/** | |
* The header for snapshots. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SNAPSHOT: 'x-ms-snapshot', | |
/** | |
* Specifies only snapshots are to be included. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SNAPSHOTS_ONLY_VALUE: 'only', | |
/** | |
* The header for the If-Match condition. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SOURCE_IF_MATCH: 'x-ms-source-if-match', | |
/** | |
* The header for the If-Modified-Since condition. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SOURCE_IF_MODIFIED_SINCE: 'x-ms-source-if-modified-since', | |
/** | |
* The header for the If-None-Match condition. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SOURCE_IF_NONE_MATCH: 'x-ms-source-if-none-match', | |
/** | |
* The header for the If-Unmodified-Since condition. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SOURCE_IF_UNMODIFIED_SINCE: 'x-ms-source-if-unmodified-since', | |
/** | |
* The header for data ranges. | |
* | |
* @const | |
* @type {string} | |
*/ | |
STORAGE_RANGE: 'x-ms-range', | |
/** | |
* The header for storage version. | |
* | |
* @const | |
* @type {string} | |
*/ | |
STORAGE_VERSION: 'x-ms-version', | |
/** | |
* The current storage version header value. | |
* | |
* @const | |
* @type {string} | |
*/ | |
TARGET_STORAGE_VERSION: '2017-11-09', | |
/** | |
* The UserAgent header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
USER_AGENT: 'user-agent', | |
/** | |
* The pop receipt header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
POP_RECEIPT: 'x-ms-popreceipt', | |
/** | |
* The time next visibile header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
TIME_NEXT_VISIBLE: 'x-ms-time-next-visible', | |
/** | |
* The approximate message counter header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
APPROXIMATE_MESSAGE_COUNT: 'x-ms-approximate-message-count', | |
/** | |
* The lease action header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
LEASE_ACTION: 'x-ms-lease-action', | |
/** | |
* The accept header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ACCEPT: 'accept', | |
/** | |
* The accept charset header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ACCEPT_CHARSET: 'Accept-Charset', | |
/** | |
* The host header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
HOST: 'host', | |
/** | |
* The correlation identifier header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CORRELATION_ID: 'x-ms-correlation-id', | |
/** | |
* The group identifier header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
GROUP_ID: 'x-ms-group-id', | |
/** | |
* The share quota header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SHARE_QUOTA: 'x-ms-share-quota', | |
/** | |
* The max blob size header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
BLOB_CONDITION_MAX_SIZE: 'x-ms-blob-condition-maxsize', | |
/** | |
* The append blob position header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
BLOB_CONDITION_APPEND_POSITION: 'x-ms-blob-condition-appendpos', | |
/** | |
* The append blob append offset header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
BLOB_APPEND_OFFSET: 'x-ms-blob-append-offset', | |
/** | |
* The append blob committed block header. | |
* | |
* @const | |
* @type {string} | |
*/ | |
BLOB_COMMITTED_BLOCK_COUNT: 'x-ms-blob-committed-block-count', | |
/** | |
* If the contents of the request have been successfully encrypted using the specified algorithm. | |
* | |
* @const | |
* @type {string} | |
*/ | |
REQUEST_SERVER_ENCRYPTED: 'x-ms-request-server-encrypted', | |
/** | |
* If the data and application metadata are completely encrypted using the specified algorithm. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SERVER_ENCRYPTED: 'x-ms-server-encrypted', | |
/** | |
* Header indicates the resulting tier of the blob. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ACCESS_TIER: 'x-ms-access-tier', | |
/** | |
* This is the datetime of when the last time tier was changed on the blob. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ACCESS_TIER_CHANGE_TIME: 'x-ms-access-tier-change-time', | |
/** | |
* If the access tier is not explicitly set on the blob, | |
* the tier is inferred based on its content length | |
* and this header will be returned with true value. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ACCESS_TIER_INFERRED: 'x-ms-access-tier-inferred', | |
/** | |
* For BlobStorage accounts, the header is returned if archive tier is set | |
* and rehydrate operation is pending for the request version is 2017-04-17 or later. | |
* The valid values are rehydrate-pending-to-hot or rehydrate-pending-to-cool. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ARCHIVE_STATUS: 'x-ms-archive-status' | |
}, | |
QueryStringConstants: { | |
/** | |
* Query component for SAS API version. | |
* @const | |
* @type {string} | |
*/ | |
API_VERSION: 'api-version', | |
/** | |
* The Comp value. | |
* | |
* @const | |
* @type {string} | |
*/ | |
COMP: 'comp', | |
/** | |
* The Res Type. | |
* | |
* @const | |
* @type {string} | |
*/ | |
RESTYPE: 'restype', | |
/** | |
* The copy Id. | |
* @const | |
* @type {string} | |
*/ | |
COPY_ID: 'copyid', | |
/** | |
* The snapshot value. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SNAPSHOT: 'snapshot', | |
/** | |
* The share snapshot value. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SHARE_SNAPSHOT: 'sharesnapshot', | |
/** | |
* The previous snapshot value. | |
* | |
* @const | |
* @type {string} | |
*/ | |
PREV_SNAPSHOT: 'prevsnapshot', | |
/** | |
* The timeout value. | |
* | |
* @const | |
* @type {string} | |
*/ | |
TIMEOUT: 'timeout', | |
/** | |
* The signed start time query string argument for shared access signature. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SIGNED_START: 'st', | |
/** | |
* The signed expiry time query string argument for shared access signature. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SIGNED_EXPIRY: 'se', | |
/** | |
* The signed resource query string argument for shared access signature. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SIGNED_RESOURCE: 'sr', | |
/** | |
* The signed permissions query string argument for shared access signature. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SIGNED_PERMISSIONS: 'sp', | |
/** | |
* The signed services query string argument for shared access signature. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SIGNED_SERVICES: 'ss', | |
/** | |
* The signed resource types query string argument for shared access signature. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SIGNED_RESOURCE_TYPES: 'srt', | |
/** | |
* The signed IP query string argument for shared access signature. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SIGNED_IP: 'sip', | |
/** | |
* The signed protocol query string argument for shared access signature. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SIGNED_PROTOCOL: 'spr', | |
/** | |
* The signed identifier query string argument for shared access signature. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SIGNED_IDENTIFIER: 'si', | |
/** | |
* The signature query string argument for shared access signature. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SIGNATURE: 'sig', | |
/** | |
* The signed version argument for shared access signature. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SIGNED_VERSION: 'sv', | |
/** | |
* The cache control argument for shared access signature. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CACHE_CONTROL: 'rscc', | |
/** | |
* The content type argument for shared access signature. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CONTENT_TYPE: 'rsct', | |
/** | |
* The content encoding argument for shared access signature. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CONTENT_ENCODING: 'rsce', | |
/** | |
* The content language argument for shared access signature. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CONTENT_LANGUAGE: 'rscl', | |
/** | |
* The content disposition argument for shared access signature. | |
* | |
* @const | |
* @type {string} | |
*/ | |
CONTENT_DISPOSITION: 'rscd', | |
/** | |
* The block identifier query string argument for blob service. | |
* | |
* @const | |
* @type {string} | |
*/ | |
BLOCK_ID: 'blockid', | |
/** | |
* The block list type query string argument for blob service. | |
* | |
* @const | |
* @type {string} | |
*/ | |
BLOCK_LIST_TYPE: 'blocklisttype', | |
/** | |
* The prefix query string argument for listing operations. | |
* | |
* @const | |
* @type {string} | |
*/ | |
PREFIX: 'prefix', | |
/** | |
* The marker query string argument for listing operations. | |
* | |
* @const | |
* @type {string} | |
*/ | |
MARKER: 'marker', | |
/** | |
* The maxresults query string argument for listing operations. | |
* | |
* @const | |
* @type {string} | |
*/ | |
MAX_RESULTS: 'maxresults', | |
/** | |
* The delimiter query string argument for listing operations. | |
* | |
* @const | |
* @type {string} | |
*/ | |
DELIMITER: 'delimiter', | |
/** | |
* The include query string argument for listing operations. | |
* | |
* @const | |
* @type {string} | |
*/ | |
INCLUDE: 'include', | |
/** | |
* The peekonly query string argument for queue service. | |
* | |
* @const | |
* @type {string} | |
*/ | |
PEEK_ONLY: 'peekonly', | |
/** | |
* The numofmessages query string argument for queue service. | |
* | |
* @const | |
* @type {string} | |
*/ | |
NUM_OF_MESSAGES: 'numofmessages', | |
/** | |
* The popreceipt query string argument for queue service. | |
* | |
* @const | |
* @type {string} | |
*/ | |
POP_RECEIPT: 'popreceipt', | |
/** | |
* The visibilitytimeout query string argument for queue service. | |
* | |
* @const | |
* @type {string} | |
*/ | |
VISIBILITY_TIMEOUT: 'visibilitytimeout', | |
/** | |
* The messagettl query string argument for queue service. | |
* | |
* @const | |
* @type {string} | |
*/ | |
MESSAGE_TTL: 'messagettl', | |
/** | |
* The select query string argument. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SELECT: '$select', | |
/** | |
* The filter query string argument. | |
* | |
* @const | |
* @type {string} | |
*/ | |
FILTER: '$filter', | |
/** | |
* The top query string argument. | |
* | |
* @const | |
* @type {string} | |
*/ | |
TOP: '$top', | |
/** | |
* The skip query string argument. | |
* | |
* @const | |
* @type {string} | |
*/ | |
SKIP: '$skip', | |
/** | |
* The next partition key query string argument for table service. | |
* | |
* @const | |
* @type {string} | |
*/ | |
NEXT_PARTITION_KEY: 'NextPartitionKey', | |
/** | |
* The next row key query string argument for table service. | |
* | |
* @const | |
* @type {string} | |
*/ | |
NEXT_ROW_KEY: 'NextRowKey', | |
/** | |
* The lock identifier for service bus messages. | |
* | |
* @const | |
* @type {string} | |
*/ | |
LOCK_ID: 'lockid', | |
/** | |
* The table name for table SAS URI's. | |
* | |
* @const | |
* @type {string} | |
*/ | |
TABLENAME: 'tn', | |
/** | |
* The starting Partition Key for tableSAS URI's. | |
* | |
* @const | |
* @type {string} | |
*/ | |
STARTPK: 'spk', | |
/** | |
* The starting Partition Key for tableSAS URI's. | |
* | |
* @const | |
* @type {string} | |
*/ | |
STARTRK: 'srk', | |
/** | |
* The ending Partition Key for tableSAS URI's. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ENDPK: 'epk', | |
/** | |
* The ending Partition Key for tableSAS URI's. | |
* | |
* @const | |
* @type {string} | |
*/ | |
ENDRK: 'erk' | |
}, | |
StorageServiceClientConstants: { | |
/** | |
* The default protocol. | |
* | |
* @const | |
* @type {string} | |
*/ | |
DEFAULT_PROTOCOL: 'https:', | |
/* | |
* Used environment variables. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
EnvironmentVariables: { | |
AZURE_STORAGE_ACCOUNT: 'AZURE_STORAGE_ACCOUNT', | |
AZURE_STORAGE_ACCESS_KEY: 'AZURE_STORAGE_ACCESS_KEY', | |
AZURE_STORAGE_DNS_SUFFIX: 'AZURE_STORAGE_DNS_SUFFIX', | |
AZURE_STORAGE_CONNECTION_STRING: 'AZURE_STORAGE_CONNECTION_STRING', | |
HTTP_PROXY: 'HTTP_PROXY', | |
HTTPS_PROXY: 'HTTPS_PROXY', | |
EMULATED: 'EMULATED' | |
}, | |
/** | |
* Default credentials. | |
*/ | |
DEVSTORE_STORAGE_ACCOUNT: 'devstoreaccount1', | |
DEVSTORE_STORAGE_ACCESS_KEY: 'Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==', | |
/** | |
* The development store URI. | |
* | |
* @const | |
* @type {string} | |
*/ | |
DEV_STORE_URI: 'http://127.0.0.1', | |
/** | |
* Development ServiceClient URLs. | |
*/ | |
DEVSTORE_DEFAULT_PROTOCOL: 'http://', | |
DEVSTORE_BLOB_HOST: '127.0.0.1:10000', | |
DEVSTORE_QUEUE_HOST: '127.0.0.1:10001', | |
DEVSTORE_TABLE_HOST: '127.0.0.1:10002', | |
/** | |
* Production ServiceClient URLs. | |
*/ | |
CLOUD_BLOB_HOST: 'blob.' + storageDnsSuffix, | |
CLOUD_QUEUE_HOST: 'queue.' + storageDnsSuffix, | |
CLOUD_TABLE_HOST: 'table.' + storageDnsSuffix, | |
CLOUD_FILE_HOST: 'file.' + storageDnsSuffix | |
}, | |
HttpConstants: { | |
/** | |
* Http Verbs | |
* | |
* @const | |
* @enum {string} | |
*/ | |
HttpVerbs: { | |
PUT: 'PUT', | |
GET: 'GET', | |
DELETE: 'DELETE', | |
POST: 'POST', | |
MERGE: 'MERGE', | |
HEAD: 'HEAD' | |
}, | |
/** | |
* Response codes. | |
* | |
* @const | |
* @enum {int} | |
*/ | |
HttpResponseCodes: { | |
Ok: 200, | |
Created: 201, | |
Accepted: 202, | |
NoContent: 204, | |
PartialContent: 206, | |
BadRequest: 400, | |
Unauthorized: 401, | |
Forbidden: 403, | |
NotFound: 404, | |
Conflict: 409, | |
LengthRequired: 411, | |
PreconditionFailed: 412 | |
} | |
}, | |
CompatibleVersionConstants: { | |
/** | |
* Constant for the 2013-08-15 version. | |
* | |
* @const | |
* @type {string} | |
*/ | |
AUGUST_2013: '2013-08-15', | |
/** | |
* Constant for the 2012-02-12 version. | |
* | |
* @const | |
* @type {string} | |
*/ | |
FEBRUARY_2012: '2012-02-12' | |
}, | |
BlobErrorCodeStrings: { | |
INVALID_BLOCK_ID: 'InvalidBlockId', | |
BLOB_NOT_FOUND: 'BlobNotFound', | |
BLOB_ALREADY_EXISTS: 'BlobAlreadyExists', | |
CONTAINER_ALREADY_EXISTS: 'ContainerAlreadyExists', | |
CONTAINER_NOT_FOUND: 'ContainerNotFound', | |
INVALID_BLOB_OR_BLOCK: 'InvalidBlobOrBlock', | |
INVALID_BLOCK_LIST: 'InvalidBlockList' | |
}, | |
FileErrorCodeStrings: { | |
SHARE_ALREADY_EXISTS: 'ShareAlreadyExists', | |
SHARE_NOT_FOUND: 'ShareNotFound', | |
FILE_NOT_FOUND: 'FileNotFound' | |
}, | |
QueueErrorCodeStrings: { | |
QUEUE_NOT_FOUND: 'QueueNotFound', | |
QUEUE_DISABLED: 'QueueDisabled', | |
QUEUE_ALREADY_EXISTS: 'QueueAlreadyExists', | |
QUEUE_NOT_EMPTY: 'QueueNotEmpty', | |
QUEUE_BEING_DELETED: 'QueueBeingDeleted', | |
POP_RECEIPT_MISMATCH: 'PopReceiptMismatch', | |
INVALID_PARAMETER: 'InvalidParameter', | |
MESSAGE_NOT_FOUND: 'MessageNotFound', | |
MESSAGE_TOO_LARGE: 'MessageTooLarge', | |
INVALID_MARKER: 'InvalidMarker' | |
}, | |
/** | |
* Constants for storage error strings | |
* | |
* More details are at: http://msdn.microsoft.com/en-us/library/azure/dd179357.aspx | |
*/ | |
StorageErrorCodeStrings: { | |
// Not Modified (304): The condition specified in the conditional header(s) was not met for a read operation. | |
// Precondition Failed (412): The condition specified in the conditional header(s) was not met for a write operation. | |
CONDITION_NOT_MET: 'ConditionNotMet', | |
// Bad Request (400): A required HTTP header was not specified. | |
MISSING_REQUIRED_HEADER: 'MissingRequiredHeader', | |
// Bad Request (400): A required XML node was not specified in the request body. | |
MISSING_REQUIRED_XML_NODE: 'MissingRequiredXmlNode', | |
// Bad Request (400): One of the HTTP headers specified in the request is not supported. | |
UNSUPPORTED_HEADER: 'UnsupportedHeader', | |
// Bad Request (400): One of the XML nodes specified in the request body is not supported. | |
UNSUPPORTED_XML_NODE: 'UnsupportedXmlNode', | |
// Bad Request (400): The value provided for one of the HTTP headers was not in the correct format. | |
INVALID_HEADER_VALUE: 'InvalidHeaderValue', | |
// Bad Request (400): The value provided for one of the XML nodes in the request body was not in the correct format. | |
INVALID_XML_NODE_VALUE: 'InvalidXmlNodeValue', | |
// Bad Request (400): A required query parameter was not specified for this request. | |
MISSING_REQUIRED_QUERY_PARAMETER: 'MissingRequiredQueryParameter', | |
// Bad Request (400): One of the query parameters specified in the request URI is not supported. | |
UNSUPPORTED_QUERY_PARAMETER: 'UnsupportedQueryParameter', | |
// Bad Request (400): An invalid value was specified for one of the query parameters in the request URI. | |
INVALID_QUERY_PARAMETER_VALUE: 'InvalidQueryParameterValue', | |
// Bad Request (400): A query parameter specified in the request URI is outside the permissible range. | |
OUT_OF_RANGE_QUERY_PARAMETER_VALUE: 'OutOfRangeQueryParameterValue', | |
// Bad Request (400): The url in the request could not be parsed. | |
REQUEST_URL_FAILED_TO_PARSE: 'RequestUrlFailedToParse', | |
// Bad Request (400): The requested URI does not represent any resource on the server. | |
INVALID_URI: 'InvalidUri', | |
// Bad Request (400): The HTTP verb specified was not recognized by the server. | |
INVALID_HTTP_VERB: 'InvalidHttpVerb', | |
// Bad Request (400): The key for one of the metadata key-value pairs is empty. | |
EMPTY_METADATA_KEY: 'EmptyMetadataKey', | |
// Bad Request (400): The specified XML is not syntactically valid. | |
INVALID_XML_DOCUMENT: 'InvalidXmlDocument', | |
// Bad Request (400): The MD5 value specified in the request did not match the MD5 value calculated by the server. | |
MD5_MISMATCH: 'Md5Mismatch', | |
// Bad Request (400): The MD5 value specified in the request is invalid. The MD5 value must be 128 bits and Base64-encoded. | |
INVALID_MD5: 'InvalidMd5', | |
// Bad Request (400): One of the request inputs is out of range. | |
OUT_OF_RANGE_INPUT: 'OutOfRangeInput', | |
// Bad Request (400): The authentication information was not provided in the correct format. Verify the value of Authorization header. | |
INVALID_AUTHENTICATION_INFO: 'InvalidAuthenticationInfo', | |
// Bad Request (400): One of the request inputs is not valid. | |
INVALID_INPUT: 'InvalidInput', | |
// Bad Request (400): The specified metadata is invalid. It includes characters that are not permitted. | |
INVALID_METADATA: 'InvalidMetadata', | |
// Bad Request (400): The specifed resource name contains invalid characters. | |
INVALID_RESOURCE_NAME: 'InvalidResourceName', | |
// Bad Request (400): The size of the specified metadata exceeds the maximum size permitted. | |
METADATA_TOO_LARGE: 'MetadataTooLarge', | |
// Bad Request (400): Condition headers are not supported. | |
CONDITION_HEADER_NOT_SUPPORTED: 'ConditionHeadersNotSupported', | |
// Bad Request (400): Multiple condition headers are not supported. | |
MULTIPLE_CONDITION_HEADER_NOT_SUPPORTED: 'MultipleConditionHeadersNotSupported', | |
// Forbidden (403): Server failed to authenticate the request. Make sure the value of the Authorization header is formed correctly including the signature. | |
AUTHENTICATION_FAILED: 'AuthenticationFailed', | |
// Forbidden (403): Read-access geo-redundant replication is not enabled for the account. | |
// Forbidden (403): Write operations to the secondary location are not allowed. | |
// Forbidden (403): The account being accessed does not have sufficient permissions to execute this operation. | |
INSUFFICIENT_ACCOUNT_PERMISSIONS: 'InsufficientAccountPermissions', | |
// Not Found (404): The specified resource does not exist. | |
RESOURCE_NOT_FOUND: 'ResourceNotFound', | |
// Forbidden (403): The specified account is disabled. | |
ACCOUNT_IS_DISABLED: 'AccountIsDisabled', | |
// Method Not Allowed (405): The resource doesn't support the specified HTTP verb. | |
UNSUPPORTED_HTTP_VERB: 'UnsupportedHttpVerb', | |
// Conflict (409): The specified account already exists. | |
ACCOUNT_ALREADY_EXISTS: 'AccountAlreadyExists', | |
// Conflict (409): The specified account is in the process of being created. | |
ACCOUNT_BEING_CREATED: 'AccountBeingCreated', | |
// Conflict (409): The specified resource already exists. | |
RESOURCE_ALREADY_EXISTS: 'ResourceAlreadyExists', | |
// Conflict (409): The specified resource type does not match the type of the existing resource. | |
RESOURCE_TYPE_MISMATCH: 'ResourceTypeMismatch', | |
// Length Required (411): The Content-Length header was not specified. | |
MISSING_CONTENT_LENGTH_HEADER: 'MissingContentLengthHeader', | |
// Request Entity Too Large (413): The size of the request body exceeds the maximum size permitted. | |
REQUEST_BODY_TOO_LARGE: 'RequestBodyTooLarge', | |
// Requested Range Not Satisfiable (416): The range specified is invalid for the current size of the resource. | |
INVALID_RANGE: 'InvalidRange', | |
// Internal Server Error (500): The server encountered an internal error. Please retry the request. | |
INTERNAL_ERROR: 'InternalError', | |
// Internal Server Error (500): The operation could not be completed within the permitted time. | |
OPERATION_TIMED_OUT: 'OperationTimedOut', | |
// Service Unavailable (503): The server is currently unable to receive requests. Please retry your request. | |
SERVER_BUSY: 'ServerBusy', | |
// Legacy error code strings | |
UPDATE_CONDITION_NOT_SATISFIED: 'UpdateConditionNotSatisfied', | |
CONTAINER_NOT_FOUND: 'ContainerNotFound', | |
CONTAINER_ALREADY_EXISTS: 'ContainerAlreadyExists', | |
CONTAINER_DISABLED: 'ContainerDisabled', | |
CONTAINER_BEING_DELETED: 'ContainerBeingDeleted' | |
}, | |
TableErrorCodeStrings: { | |
XMETHOD_NOT_USING_POST: 'XMethodNotUsingPost', | |
XMETHOD_INCORRECT_VALUE: 'XMethodIncorrectValue', | |
XMETHOD_INCORRECT_COUNT: 'XMethodIncorrectCount', | |
TABLE_HAS_NO_PROPERTIES: 'TableHasNoProperties', | |
DUPLICATE_PROPERTIES_SPECIFIED: 'DuplicatePropertiesSpecified', | |
TABLE_HAS_NO_SUCH_PROPERTY: 'TableHasNoSuchProperty', | |
DUPLICATE_KEY_PROPERTY_SPECIFIED: 'DuplicateKeyPropertySpecified', | |
TABLE_ALREADY_EXISTS: 'TableAlreadyExists', | |
TABLE_NOT_FOUND: 'TableNotFound', | |
ENTITY_NOT_FOUND: 'EntityNotFound', | |
ENTITY_ALREADY_EXISTS: 'EntityAlreadyExists', | |
PARTITION_KEY_NOT_SPECIFIED: 'PartitionKeyNotSpecified', | |
OPERATOR_INVALID: 'OperatorInvalid', | |
UPDATE_CONDITION_NOT_SATISFIED: 'UpdateConditionNotSatisfied', | |
PROPERTIES_NEED_VALUE: 'PropertiesNeedValue', | |
PARTITION_KEY_PROPERTY_CANNOT_BE_UPDATED: 'PartitionKeyPropertyCannotBeUpdated', | |
TOO_MANY_PROPERTIES: 'TooManyProperties', | |
ENTITY_TOO_LARGE: 'EntityTooLarge', | |
PROPERTY_VALUE_TOO_LARGE: 'PropertyValueTooLarge', | |
INVALID_VALUE_TYPE: 'InvalidValueType', | |
TABLE_BEING_DELETED: 'TableBeingDeleted', | |
TABLE_SERVER_OUT_OF_MEMORY: 'TableServerOutOfMemory', | |
PRIMARY_KEY_PROPERTY_IS_INVALID_TYPE: 'PrimaryKeyPropertyIsInvalidType', | |
PROPERTY_NAME_TOO_LONG: 'PropertyNameTooLong', | |
PROPERTY_NAME_INVALID: 'PropertyNameInvalid', | |
BATCH_OPERATION_NOT_SUPPORTED: 'BatchOperationNotSupported', | |
JSON_FORMAT_NOT_SUPPORTED: 'JsonFormatNotSupported', | |
METHOD_NOT_ALLOWED: 'MethodNotAllowed', | |
NOT_IMPLEMENTED: 'NotImplemented' | |
}, | |
ConnectionStringKeys: { | |
USE_DEVELOPMENT_STORAGE_NAME: 'UseDevelopmentStorage', | |
DEVELOPMENT_STORAGE_PROXY_URI_NAME: 'DevelopmentStorageProxyUri', | |
DEFAULT_ENDPOINTS_PROTOCOL_NAME: 'DefaultEndpointsProtocol', | |
ACCOUNT_NAME_NAME: 'AccountName', | |
ACCOUNT_KEY_NAME: 'AccountKey', | |
BLOB_ENDPOINT_NAME: 'BlobEndpoint', | |
FILE_ENDPOINT_NAME: 'FileEndpoint', | |
QUEUE_ENDPOINT_NAME: 'QueueEndpoint', | |
TABLE_ENDPOINT_NAME: 'TableEndpoint', | |
SHARED_ACCESS_SIGNATURE_NAME: 'SharedAccessSignature', | |
ENDPOINT_SUFFIX_NAME: 'EndpointSuffix', | |
BLOB_BASE_DNS_NAME: 'blob.core.windows.net', | |
FILE_BASE_DNS_NAME: 'file.core.windows.net', | |
QUEUE_BASE_DNS_NAME: 'queue.core.windows.net', | |
TABLE_BASE_DNS_NAME: 'table.core.windows.net' | |
} | |
}; | |
module.exports = Constants; | |
}).call(this,require('_process')) | |
},{"_process":175}],33:[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; | |
}; | |
},{}],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. | |
// | |
var rightPad = function (n, number) { | |
var currentN = '' + n; | |
while (currentN.length < number) { | |
currentN = currentN + '0'; | |
} | |
return currentN; | |
}; | |
/** | |
* Formats a date into an iso 8061 string. | |
* | |
* @param {date} date The date to format. | |
* @param {bool} skipMilliseconds Boolean value indicating if the miliseconds part of the date should not be included. | |
* @param {integer} millisecondsPading Number of digits to left pad the miliseconds. | |
* @return {string} The date formated in the ISO 8061 date format. | |
*/ | |
exports.format = function (date) { | |
var dateString = date.toISOString(); | |
return dateString.substring(0, dateString.length - 1) + '0000Z'; | |
}; | |
/** | |
* Parses an ISO 8061 date string into a date object. | |
* | |
* @param {string} stringDateTime The string with the date to parse in the ISO 8061 format. | |
* @return {date} The parsed date. | |
*/ | |
exports.parse = function (stringDateTime) { | |
var parts = stringDateTime.split('T'); | |
var ymd = parts[0].split('-'); | |
var time = parts[1].split('.'); | |
var hms = time[0].split(':'); | |
var ms = 0; | |
if (time[1]) { | |
ms = time[1].split('Z'); | |
} | |
var date = new Date(Date.UTC( | |
parseInt(ymd[0], 10), | |
parseInt(ymd[1], 10) - 1, | |
parseInt(ymd[2], 10), | |
parseInt(hms[0], 10), | |
parseInt(hms[1], 10), | |
parseInt(hms[2], 10), | |
Math.round(parseInt(rightPad(ms[0], 7), 10) / 10000) | |
)); | |
return date; | |
}; | |
},{}],35:[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":313}],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. | |
// | |
exports = module.exports; | |
var SR = { | |
ANONYMOUS_ACCESS_BLOBSERVICE_ONLY: 'Anonymous access is only valid for the BlobService.', | |
ARGUMENT_NULL_OR_EMPTY: 'The argument must not be null or an empty string. Argument name: %s.', | |
ARGUMENT_NULL_OR_UNDEFINED: 'The argument must not be null or undefined. Argument name: %s.', | |
ARGUMENT_OUT_OF_RANGE_ERROR: 'The argument is out of range. Argument name: %s, Value passed: %s.', | |
BATCH_ONE_PARTITION_KEY: 'All entities in the batch must have the same PartitionKey value.', | |
BATCH_ONE_RETRIEVE: 'If a retrieve operation is part of a batch, it must be the only operation in the batch.', | |
BATCH_TOO_LARGE: 'Batches must not contain more than 100 operations.', | |
BLOB_INVALID_SEQUENCE_NUMBER: 'The sequence number may not be specified for an increment operation.', | |
BLOB_TYPE_MISMATCH: 'Blob type of the blob reference doesn\'t match blob type of the blob.', | |
CANNOT_CREATE_SAS_WITHOUT_ACCOUNT_KEY: 'Cannot create Shared Access Signature unless the Account Name and Key are used to create the ServiceClient.', | |
CONTENT_LENGTH_MISMATCH: 'An incorrect number of bytes was read from the connection. The connection may have been closed.', | |
CONTENT_TYPE_MISSING: 'Content-Type response header is missing or invalid.', | |
EMPTY_BATCH: 'Batch must not be empty.', | |
EXCEEDED_SIZE_LIMITATION: 'Upload exceeds the size limitation. Max size is %s but the current size is %s', | |
HASH_MISMATCH: 'Hash mismatch (integrity check failed), Expected value is %s, retrieved %s.', | |
INCORRECT_ENTITY_KEYS: 'PartitionKey and RowKey must be specified as strings in the entity object.', | |
INVALID_BLOB_LENGTH: 'createBlockBlobFromText requires the size of text to be less than 64MB. Please use createBlockBlobFromLocalFile or createBlockBlobFromStream to upload large blobs.', | |
INVALID_CONNECTION_STRING: 'Connection strings must be of the form "key1=value1;key2=value2".', | |
INVALID_CONNECTION_STRING_BAD_KEY: 'Connection string contains unrecognized key: "%s"', | |
INVALID_CONNECTION_STRING_DUPLICATE_KEY: 'Connection string contains duplicate key: "%s"', | |
INVALID_CONNECTION_STRING_EMPTY_KEY: 'Connection strings must not contain empty keys.', | |
INVALID_DELETE_SNAPSHOT_OPTION: 'The deleteSnapshots option cannot be included when deleting a specific snapshot using the snapshotId option.', | |
INVALID_EDM_TYPE: 'The value \'%s\' does not match the type \'%s\'.', | |
INVALID_FILE_LENGTH: 'createFileFromText requires the size of text to be less than 4MB. Please use createFileFromLocalFile or createFileFromStream to upload large files.', | |
INVALID_FILE_RANGE_FOR_UPDATE: 'Range size should be less than 4MB for a file range update operation.', | |
INVALID_HEADERS: 'Headers are not supported in the 2012-02-12 version.', | |
INVALID_MESSAGE_ID: 'Message ID cannot be null or undefined for deleteMessage and updateMessage operations.', | |
INVALID_PAGE_BLOB_LENGTH: 'Page blob length must be multiple of 512.', | |
INVALID_PAGE_END_OFFSET: 'Page end offset must be multiple of 512.', | |
INVALID_PAGE_RANGE_FOR_UPDATE: 'Page range size should be less than 4MB for a page update operation.', | |
INVALID_PAGE_START_OFFSET: 'Page start offset must be multiple of 512.', | |
INVALID_POP_RECEIPT: 'Pop Receipt cannot be null or undefined for deleteMessage and updateMessage operations.', | |
INVALID_PROPERTY_RESOLVER: 'The specified property resolver returned an invalid type. %s:{_:%s,$:%s }', | |
INVALID_RANGE_FOR_MD5: 'The requested range should be less than 4MB when contentMD5 is expected from the server', | |
INVALID_SAS_VERSION: 'SAS Version ? is invalid. Valid versions include: ?.', | |
INVALID_SAS_TOKEN: 'The SAS token should not contain api-version.', | |
INVALID_SIGNED_IDENTIFIERS: 'Signed identifiers need to be a hash object with key as the id and the value as the access policy.', | |
INVALID_STREAM_LENGTH: 'The length of the provided stream is invalid.', | |
INVALID_STRING_ERROR: 'Invalid string error.', | |
INVALID_TABLE_OPERATION: 'Operation not found: %s', | |
INVALID_TEXT_LENGTH: 'The length of the provided text is invalid.', | |
MAXIMUM_EXECUTION_TIMEOUT_EXCEPTION: 'The client could not finish the operation within specified maximum execution timeout.', | |
MD5_NOT_POSSIBLE: 'MD5 cannot be calculated for an existing blob because it would require reading the existing data. Please disable storeBlobContentMD5.', | |
MD5_NOT_PRESENT_ERROR: 'MD5 does not exist. If you do not want to force validation, please disable useTransactionalMD5.', | |
METADATA_KEY_INVALID: 'The key for one of the metadata key-value pairs is null, empty, or whitespace.', | |
METADATA_VALUE_INVALID: 'The value for one of the metadata key-value pairs is null, empty, or whitespace.', | |
NO_CREDENTIALS_PROVIDED: 'Credentials must be provided when creating a service client.', | |
PRIMARY_ONLY_COMMAND: 'This operation can only be executed against the primary storage location.', | |
QUERY_OPERATOR_REQUIRES_WHERE: '%s operator needs to be used after where.', | |
SECONDARY_ONLY_COMMAND: 'This operation can only be executed against the secondary storage location.', | |
STORAGE_HOST_LOCATION_REQUIRED: 'The host for the storage service must be specified.', | |
STORAGE_HOST_MISSING_LOCATION: 'The host for the target storage location is not specified. Please consider changing the request\'s location mode.', | |
TYPE_NOT_SUPPORTED: 'Type not supported when sending data to the service: ', | |
MAX_BLOB_SIZE_CONDITION_NOT_MEET: 'Append block data should not exceed the maximum blob size condition value.', | |
}; | |
module.exports = SR; | |
},{}],37:[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 'StorageUtilities'. | |
/** | |
* Defines constants, enums, and utility functions for use with storage. | |
* @namespace | |
*/ | |
var StorageUtilities = { | |
/** | |
* Specifies the location mode used to decide which location the request should be sent to. | |
* | |
* @const | |
* @enum {number} | |
*/ | |
LocationMode: { | |
/** The primary location only */ | |
PRIMARY_ONLY: 0, | |
/** The primary location first, then the secondary */ | |
PRIMARY_THEN_SECONDARY: 1, | |
/** The secondary location only */ | |
SECONDARY_ONLY: 2, | |
/** The secondary location first, then the primary */ | |
SECONDARY_THEN_PRIMARY: 3 | |
} | |
}; | |
module.exports = StorageUtilities; | |
},{}],38:[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. | |
// | |
var _ = require('underscore'); | |
var util = require('util'); | |
var url = require('url'); | |
var stream = require('stream'); | |
var Constants = require('./constants'); | |
var Md5Wrapper = require('../md5-wrapper'); | |
var StorageUtilities = require('./storageutilities'); | |
var SR = require('./sr'); | |
/** | |
* Trim the default port in the url. | |
* | |
* @param {string} uri The URI to be encoded. | |
* @return {string} The URI without defualt port. | |
*/ | |
exports.trimPortFromUri = function (uri) { | |
var uri = url.parse(uri); | |
if ((uri.protocol === Constants.HTTPS && uri.port == Constants.DEFAULT_HTTPS_PORT) || (uri.protocol === Constants.HTTP && uri.port == Constants.DEFAULT_HTTP_PORT)) { | |
uri.host = uri.hostname; | |
} | |
return url.format(uri); | |
}; | |
/** | |
* Returns the number of keys (properties) in an object. | |
* | |
* @param {object} value The object which keys are to be counted. | |
* @return {number} The number of keys in the object. | |
*/ | |
exports.objectKeysLength = function (value) { | |
if (!value) { | |
return 0; | |
} | |
return _.keys(value).length; | |
}; | |
/** | |
* Checks if in a browser environment. | |
* | |
* @return {bool} True if in a browser environment, false otherwise. | |
*/ | |
exports.isBrowser = function () { | |
return typeof window !== 'undefined'; | |
}; | |
/** | |
* Checks if in IE. | |
* | |
* @return {bool} True if in IE, false otherwise. | |
*/ | |
exports.isIE = function () { | |
if (!exports.isBrowser()) { | |
return false; | |
} | |
var ua = window.navigator.userAgent; | |
var msie = ua.indexOf('MSIE '); | |
var trident = ua.indexOf('Trident/'); | |
return msie > 0 || trident > 0; | |
}; | |
/** | |
* Checks if in a 32bit Node.js environment. | |
* | |
* @return {bool} True if in a 32bit Node.js environment, false otherwise. | |
*/ | |
exports.is32 = function () { | |
return !exports.isBrowser() && process.arch === 'ia32'; | |
}; | |
/** | |
* Checks if a value is null or undefined. | |
* | |
* @param {object} value The value to check for null or undefined. | |
* @return {bool} True if the value is null or undefined, false otherwise. | |
*/ | |
exports.objectIsNull = function (value) { | |
return _.isNull(value) || _.isUndefined(value); | |
}; | |
/** | |
* Checks if an object is empty. | |
* | |
* @param {object} object The object to check if it is null. | |
* @return {bool} True if the object is empty, false otherwise. | |
*/ | |
exports.objectIsEmpty = function (object) { | |
return _.isEmpty(object); | |
}; | |
/** | |
* Determines if an object contains an integer number. | |
* | |
* @param {object} value The object to assert. | |
* @return {bool} True if the object contains an integer number; false otherwise. | |
*/ | |
exports.objectIsInt = function (value) { | |
return typeof value === 'number' && parseFloat(value) == parseInt(value, 10) && !isNaN(value); | |
}; | |
/** | |
* Determines if an object is a NaN. | |
* | |
* @param {object} value The object to assert. | |
* @return {bool} True if the object is a NaN; false otherwise. | |
*/ | |
exports.objectIsNaN = function (value) { | |
return typeof(value) === 'number' && isNaN(value); | |
}; | |
/** | |
* Checks if an object is a string. | |
* | |
* @param {object} object The object to check if it is a string. | |
* @return {bool} True if the object is a string, false otherwise. | |
*/ | |
exports.objectIsString = function (object) { | |
return _.isString(object); | |
}; | |
/** | |
* Check if an object is a function | |
* @param {object} object The object to check whether it is function | |
* @return {bool} True if the specified object is function, otherwise false | |
*/ | |
exports.objectIsFunction = function (object) { | |
return _.isFunction(object); | |
}; | |
/** | |
* Front zero padding of string to sepcified length | |
*/ | |
exports.zeroPaddingString = function(str, len) { | |
var paddingStr = '0000000000' + str; | |
if(paddingStr.length < len) { | |
return exports.zeroPaddingString(paddingStr, len); | |
} else { | |
return paddingStr.substr(-1 * len); | |
} | |
}; | |
/** | |
* Checks if a value is an empty string, null or undefined. | |
* | |
* @param {object} value The value to check for an empty string, null or undefined. | |
* @return {bool} True if the value is an empty string, null or undefined, false otherwise. | |
*/ | |
exports.stringIsEmpty = function (value) { | |
return _.isNull(value) || _.isUndefined(value) || value === ''; | |
}; | |
/** | |
* Checks if a value is null, empty, undefined or consists only of white-space characters. | |
* | |
* @param {object} value The value to check for null, empty, undefined and white-space only characters. | |
* @return {bool} True if the value is an empty string, null, undefined, or consists only of white-space characters, false otherwise. | |
*/ | |
exports.IsNullOrEmptyOrUndefinedOrWhiteSpace = function (value) { | |
if(_.isNull(value) || _.isUndefined(value) || value === '') { | |
return true; | |
} | |
if(_.isString(value) && value.trim().length === 0) { | |
return true; | |
} | |
return false; | |
}; | |
/** | |
* Formats a text replacing '?' by the arguments. | |
* | |
* @param {string} text The string where the ? should be replaced. | |
* @param {array} arguments Value(s) to insert in question mark (?) parameters. | |
* @return {string} | |
*/ | |
exports.stringFormat = function (text) { | |
if (arguments.length > 1) { | |
for (var i = 1; text.indexOf('?') !== -1; i++) { | |
text = text.replace('?', arguments[i]); | |
} | |
} | |
return text; | |
}; | |
/** | |
* Determines if a string starts with another. | |
* | |
* @param {string} text The string to assert. | |
* @param {string} prefix The string prefix. | |
* @return {Bool} True if the string starts with the prefix; false otherwise. | |
*/ | |
exports.stringStartsWith = function (text, prefix) { | |
if (_.isNull(prefix)) { | |
return true; | |
} | |
return text.substr(0, prefix.length) === prefix; | |
}; | |
/** | |
* Determines if a string ends with another. | |
* | |
* @param {string} text The string to assert. | |
* @param {string} suffix The string suffix. | |
* @return {Bool} True if the string ends with the suffix; false otherwise. | |
*/ | |
exports.stringEndsWith = function (text, suffix) { | |
if (_.isNull(suffix)) { | |
return true; | |
} | |
return text.substr(text.length - suffix.length) === suffix; | |
}; | |
/** | |
* Removes the BOM from a string. | |
* | |
* @param {string} str The string from where the BOM is to be removed | |
* @return {string} The string without the BOM. | |
*/ | |
exports.removeBOM = function (str) { | |
if (str.charCodeAt(0) === 0xfeff || str.charCodeAt(0) === 0xffef) { | |
str = str.substring(1); | |
} | |
return str; | |
}; | |
/** | |
* Merges multiple objects. | |
* | |
* @param {object} object The objects to be merged | |
* @return {object} The merged object. | |
*/ | |
exports.merge = function () { | |
return _.extend.apply(this, arguments); | |
}; | |
/** | |
* Checks if a value exists in an array. The comparison is done in a case | |
* insensitive manner. | |
* | |
* @param {string} needle The searched value. | |
* @param {array} haystack The array. | |
* | |
* @static | |
* | |
* @return {boolean} | |
*/ | |
exports.inArrayInsensitive = function (needle, haystack) { | |
return _.contains(_.map(haystack, function (h) { return h.toLowerCase(); }), needle.toLowerCase()); | |
}; | |
/** | |
* Returns the specified value of the key passed from object and in case that | |
* this key doesn't exist, the default value is returned. The key matching is | |
* done in a case insensitive manner. | |
* | |
* @param {string} key The array key. | |
* @param {object} haystack The object to be used. | |
* @param {mix} default The value to return if $key is not found in $array. | |
* | |
* @static | |
* | |
* @return mix | |
*/ | |
exports.tryGetValueInsensitive = function (key, haystack, defaultValue) { | |
if (haystack) { | |
for (var i in haystack) { | |
if (haystack.hasOwnProperty(i) && i.toString().toLowerCase() === key.toString().toLowerCase()) { | |
return haystack[i]; | |
} | |
} | |
} | |
return defaultValue; | |
}; | |
/** | |
* Returns the value in a chained object. | |
* | |
* @param {object} object The object with the values. | |
* @param {array} keys The keys. | |
* @param {mix} default The value to return if $key is not found in $array. | |
* | |
* @static | |
* | |
* @return mix | |
*/ | |
exports.tryGetValueChain = function (object, keys, defaultValue) { | |
if (keys.length === 0) { | |
return object; | |
} | |
var currentKey = keys.shift(); | |
if (object && object[currentKey] !== undefined) { | |
return exports.tryGetValueChain(object[currentKey], keys, defaultValue); | |
} | |
return defaultValue; | |
}; | |
/** | |
* Set the value of an inner property of an object. | |
* | |
* @param {object} object The target object. | |
* @param {array} keys The property chain keys. | |
* @param {mix} object The value to be set. | |
* | |
* @static | |
* @example | |
* // Set targetObject.propA.propB to 'testValue' | |
* var targetObject = {}; | |
* util.setObjectInnerPropertyValue(targetObject, ['propA', 'propB'], 'testValue'); | |
*/ | |
exports.setObjectInnerPropertyValue = function(object, propertyChainKeys, value){ | |
if(!object || propertyChainKeys.length < 1) { | |
return; | |
} | |
var currentKey = propertyChainKeys.shift(); | |
if(propertyChainKeys.length === 0) { | |
object[currentKey] = value; | |
return; | |
} | |
if (!object[currentKey]) { | |
object[currentKey] = {}; | |
} | |
exports.setObjectInnerPropertyValue(object[currentKey], propertyChainKeys, value); | |
}; | |
/** | |
* Rounds a date off to seconds. | |
* | |
* @param {Date} a date | |
* @return {string} the date in ISO8061 format, with no milliseconds component | |
*/ | |
exports.truncatedISO8061Date = function (date) { | |
var dateString = date.toISOString(); | |
return dateString.substring(0, dateString.length - 5) + 'Z'; | |
}; | |
exports.normalizeArgs = function (optionsOrCallback, callback, result) { | |
var options = {}; | |
if(_.isFunction(optionsOrCallback) && !callback) { | |
callback = optionsOrCallback; | |
} else if (optionsOrCallback) { | |
options = optionsOrCallback; | |
} | |
result(options, callback); | |
}; | |
exports.getNodeVersion = function () { | |
var parsedVersion = process.version.split('.'); | |
return { | |
major: parseInt(parsedVersion[0].substr(1), 10), | |
minor: parseInt(parsedVersion[1], 10), | |
patch: parseInt(parsedVersion[2], 10) | |
}; | |
}; | |
/** | |
* Calculate md5sum for the stream | |
* @ignore | |
*/ | |
exports.calculateMD5 = function(readStream, bufferLength, options, callback) { | |
var internalBuff = new Buffer(bufferLength); | |
var index = 0; | |
var internalHash = new Md5Wrapper().createMd5Hash(); | |
readStream.on('data', function(data) { | |
if (index + data.length > bufferLength) { | |
var copyLength = bufferLength - index; | |
if (copyLength > 0) { | |
data = data.slice(0, copyLength); | |
data.copy(internalBuff, index); | |
internalHash.update(data); | |
index += copyLength; | |
} | |
readStream.emit('end'); | |
} else { | |
data.copy(internalBuff, index); | |
internalHash.update(data); | |
index += data.length; | |
} | |
}).on('end', function() { | |
if (!readStream.endEmitted) { | |
internalBuff = internalBuff.slice(0, index); | |
var contentMD5 = internalHash.digest('base64'); | |
// Set the flag to be compatible with Nodejs 0.10 which will keep emitting data from | |
// the file stream when the read stream has emitted the end event from its listner. | |
readStream.endEmitted = true; | |
callback(internalBuff, contentMD5); | |
} | |
}); | |
}; | |
/** | |
* Whether the content of buffer is all zero | |
*/ | |
exports.isBufferAllZero = function (buffer) { | |
for(var i = 0, len = buffer.length; i < len; i++) { | |
if (buffer[i] !== 0) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
/** | |
* Write zero to stream | |
*/ | |
var zeroBuffer = null; | |
exports.writeZerosToStream = function (stream, length, md5Hash, progressCallback, callback) { | |
var defaultBufferSize = Constants.BlobConstants.DEFAULT_WRITE_BLOCK_SIZE_IN_BYTES; | |
var bufferSize = Math.min(defaultBufferSize, length); | |
var remaining = length - bufferSize; | |
var buffer = null; | |
if (bufferSize == defaultBufferSize) { | |
if (!zeroBuffer) { | |
zeroBuffer = new Buffer(defaultBufferSize); | |
zeroBuffer.fill(0); | |
} | |
buffer = zeroBuffer; | |
} else { | |
buffer = new Buffer(bufferSize); | |
buffer.fill(0); | |
} | |
if (md5Hash) { | |
md5Hash.update(buffer); | |
} | |
//We can only write the entire buffer to stream instead of part of buffer. | |
return stream.write(buffer, function () { | |
if (exports.objectIsFunction(progressCallback)) { | |
progressCallback(null, buffer.length); | |
} | |
buffer = null; | |
if (remaining > 0) { | |
exports.writeZerosToStream(stream, remaining, md5Hash, progressCallback, callback); | |
} else if (exports.objectIsFunction(callback)) { | |
callback(null, null); | |
} | |
}); | |
}; | |
/** | |
* Calculate md5sum for the content | |
*/ | |
exports.getContentMd5 = function (content, encoding) { | |
if (!encoding) encoding = 'base64'; | |
var internalHash = new Md5Wrapper().createMd5Hash(); | |
internalHash.update(content, 'utf8'); | |
return internalHash.digest(encoding); | |
}; | |
exports.getNextLocation = function(lastLocation, locationMode) { | |
switch(locationMode) { | |
case StorageUtilities.LocationMode.PRIMARY_ONLY: | |
return Constants.StorageLocation.PRIMARY; | |
case StorageUtilities.LocationMode.SECONDARY_ONLY: | |
return Constants.StorageLocation.SECONDARY; | |
case StorageUtilities.LocationMode.PRIMARY_THEN_SECONDARY: | |
case StorageUtilities.LocationMode.SECONDARY_THEN_PRIMARY: | |
return (lastLocation === Constants.StorageLocation.PRIMARY) ? Constants.StorageLocation.SECONDARY : Constants.StorageLocation.PRIMARY; | |
default: | |
throw new RangeError(util.format(SR.ARGUMENT_OUT_OF_RANGE_ERROR, 'locationMode', locationMode)); | |
} | |
}; | |
exports.getNextListingLocationMode = function (token) { | |
if(_.isNull(token) || _.isUndefined(token)) { | |
return Constants.RequestLocationMode.PRIMARY_OR_SECONDARY; | |
} | |
else { | |
switch (token.targetLocation) { | |
case Constants.StorageLocation.PRIMARY: | |
return Constants.RequestLocationMode.PRIMARY_ONLY; | |
case Constants.StorageLocation.SECONDARY: | |
return Constants.RequestLocationMode.SECONDARY_ONLY; | |
default: | |
throw new RangeError(util.format(SR.ARGUMENT_OUT_OF_RANGE_ERROR, 'targetLocation', token.targetLocation)); | |
} | |
} | |
}; | |
exports.isStreamPaused = function (object) { | |
if (object instanceof stream) { | |
return object._paused === true || (object._readableState && object._readableState.flowing === false); | |
} | |
return false; | |
}; | |
/** | |
* Parse copy progress string in the format of bytesCopied/totalBytes | |
*/ | |
exports.parseCopyProgress = function (progress) { | |
if (typeof progress != 'string' || progress.indexOf('/') === -1) { | |
return {}; | |
} | |
var progressInfo = progress.split('/'); | |
return { bytesCopied: progressInfo[0], totalBytes: progressInfo[1] }; | |
}; | |
/** | |
* The list of the properties should be normalized with explicit mapping | |
*/ | |
var normalizePropertyNameExceptionList = { | |
'x-ms-blob-sequence-number': 'sequenceNumber', | |
'content-Type': 'contentSettings.contentType', | |
'content-Encoding': 'contentSettings.contentEncoding', | |
'content-Language': 'contentSettings.contentLanguage', | |
'cache-Control': 'contentSettings.cacheControl', | |
'content-Disposition': 'contentSettings.contentDisposition', | |
'content-MD5': 'contentSettings.contentMD5', | |
'leaseId': 'lease.id', | |
'leaseStatus': 'lease.status', | |
'leaseDuration': 'lease.duration', | |
'leaseState': 'lease.state', | |
'copyId': 'copy.id', | |
'copyStatus': 'copy.status', | |
'copySource': 'copy.source', | |
'copyProgress': 'copy.progress', | |
'copyCompletionTime': 'copy.completionTime', | |
'copyStatusDescription': 'copy.statusDescription', | |
'copyDestinationSnapshot': 'copy.destinationSnapshot', | |
'publicAccess': 'publicAccessLevel', | |
'incrementalCopy': 'isIncrementalCopy' | |
}; | |
/** | |
* Normalize the property name from XML to keep consistent with | |
* the name defined in the property headers | |
*/ | |
exports.normalizePropertyNameFromXML = function (propertyName) { | |
if (this.IsNullOrEmptyOrUndefinedOrWhiteSpace(propertyName)) { | |
return ''; | |
} | |
propertyName = propertyName.trim(); | |
propertyName = propertyName[0].toLowerCase() + propertyName.substring(1); | |
// So far the cases are: | |
// for the 'last-modified' property in listing resources | |
// for the 'content-*' properties in listing resources | |
// for the 'cache-control' property in listing blobs | |
// for the 'x-ms-blob-sequence-number' in listing blobs | |
if (propertyName in normalizePropertyNameExceptionList) { | |
return normalizePropertyNameExceptionList[propertyName]; | |
} else if (propertyName.toLowerCase().indexOf('-') != -1) { | |
return propertyName.replace('-', ''); | |
} else { | |
return propertyName; | |
} | |
}; | |
/** | |
* Set the property value from XML | |
*/ | |
exports.setPropertyValueFromXML = function (result, xmlNode, toNormalize) { | |
for (var subPropertyName in xmlNode) { | |
if (xmlNode.hasOwnProperty(subPropertyName)) { | |
if (toNormalize) { | |
var propertyChain = this.normalizePropertyNameFromXML(subPropertyName).split('.'); | |
exports.setObjectInnerPropertyValue(result, propertyChain, xmlNode[subPropertyName]); | |
} else { | |
result[subPropertyName.toLowerCase()] = xmlNode[subPropertyName]; | |
} | |
if (subPropertyName.toLowerCase() === 'copyprogress') { | |
var info = this.parseCopyProgress(xmlNode[subPropertyName]); | |
exports.setObjectInnerPropertyValue(result, ['copy', 'bytesCopied'], parseInt(info.bytesCopied)); | |
exports.setObjectInnerPropertyValue(result, ['copy', 'totalBytes'], parseInt(info.totalBytes)); | |
} | |
} | |
} | |
}; | |
/** | |
* Filter out non-reserved properties from options | |
*/ | |
exports.filterOutNonReservedProperties = function (reserved, options) { | |
var nonReservedProperties = {}; | |
if (options) { | |
for (var prop in options) { | |
if (options.hasOwnProperty(prop)) { | |
var isReserved = reserved.hasOwnProperty(prop); | |
var isFunction = typeof options[prop] === 'function'; | |
if (!isReserved && !isFunction) { | |
nonReservedProperties[prop] = options[prop]; | |
} | |
} | |
} | |
} | |
return nonReservedProperties; | |
}; | |
}).call(this,require('_process'),require("buffer").Buffer) | |
},{"../md5-wrapper":10,"./constants":32,"./sr":36,"./storageutilities":37,"_process":175,"buffer":99,"stream":209,"underscore":228,"url":229,"util":234}],39:[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 _ = require('underscore'); | |
var util = require('util'); | |
var constants = require('./../util/constants'); | |
var blobConstants = constants.BlobConstants; | |
var BlobUtilities = require('./../../services/blob/blobutilities'); | |
var FileUtilities = require('./../../services/file/fileutilities'); | |
var azureutil = require('./util'); | |
var SR = require('./sr'); | |
var check = require('validator'); | |
var errors = require('../errors/errors'); | |
var ArgumentError = errors.ArgumentError; | |
var ArgumentNullError = errors.ArgumentNullError; | |
exports = module.exports; | |
function initCallback(callbackParam, resultsCb) { | |
var fail; | |
if (callbackParam) { | |
fail = function (err) { | |
callbackParam(err); | |
return false; | |
}; | |
} else { | |
fail = function (err) { | |
throw err; | |
}; | |
callbackParam = function () {}; | |
} | |
resultsCb(fail, callbackParam); | |
} | |
/** | |
* Checks if the given value is a valid enumeration or not. | |
* | |
* @param {object} value The value to validate. | |
* @param {object} list The enumeration values. | |
* @return {boolean} | |
*/ | |
exports.isValidEnumValue = function (value, list, callback) { | |
var fail; | |
initCallback(callback, function (f, cb) { | |
fail = f; | |
callback = cb; | |
}); | |
if (!list.some(function (current) { | |
return current.toLowerCase() === value.toLowerCase(); | |
})) { | |
return fail(new RangeError(util.format('Invalid value: %s. Options are: %s.', value, list))); | |
} | |
callback(); | |
return true; | |
}; | |
/** | |
* Creates a anonymous function that check if the given uri is valid or not. | |
* | |
* @param {string} uri The uri to validate. | |
* @return {boolean} | |
*/ | |
exports.isValidUri = function (uri) { | |
if (!check.isURL(uri)){ | |
throw new URIError('The provided URI "' + uri + '" is invalid.'); | |
} | |
return true; | |
}; | |
/** | |
* Checks if the given host is valid or not. | |
* | |
* @param {string|object} host The host to validate. | |
* @return {boolean} | |
*/ | |
exports.isValidHost= function (host) { | |
if (azureutil.objectIsNull(host)) { | |
throw new ArgumentNullError('host', SR.STORAGE_HOST_LOCATION_REQUIRED); | |
} else { | |
var storageHost = {}; | |
storageHost.primaryHost = _.isString(host) ? host : host.primaryHost; | |
if (storageHost.primaryHost && !check.isURL(storageHost.primaryHost)){ | |
throw new URIError('The provided URI "' + storageHost.primaryHost + '" is invalid.'); | |
} | |
storageHost.secondaryHost = _.isString(host) ? undefined : host.secondaryHost; | |
if (storageHost.secondaryHost && !check.isURL(storageHost.secondaryHost)){ | |
throw new URIError('The provided URI "' + storageHost.secondaryHost + '" is invalid.'); | |
} | |
if (!storageHost.primaryHost && !storageHost.secondaryHost) { | |
throw new ArgumentNullError('host', SR.STORAGE_HOST_LOCATION_REQUIRED); | |
} | |
} | |
return true; | |
}; | |
/** | |
* Checks if the given value is a valid UUID or not. | |
* | |
* @param {string|object} uuid The uuid to validate. | |
* @return {boolean} | |
*/ | |
exports.isValidUuid = function(uuid, callback) { | |
var validUuidRegex = /^[a-zA-Z0-9]{8}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{12}$/; | |
var fail; | |
initCallback(callback, function (f, cb) { | |
fail = f; | |
callback = cb; | |
}); | |
if (!validUuidRegex.test(uuid)) { | |
return fail(new SyntaxError('The value is not a valid UUID format.')); | |
} | |
callback(); | |
return true; | |
}; | |
/** | |
* Creates a anonymous function that check if a given key is base 64 encoded. | |
* | |
* @param {string} key The key to validate. | |
* @return {function} | |
*/ | |
exports.isBase64Encoded = function (key) { | |
var isValidBase64String = key.match(/^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/); | |
if (isValidBase64String) { | |
return true; | |
} else { | |
throw new SyntaxError('The provided account key ' + key + ' is not a valid base64 string.'); | |
} | |
}; | |
/** | |
* Validates a function. | |
* | |
* @param {object} function The function to validate. | |
* @return {function} | |
*/ | |
exports.isValidFunction = function (functionObject, functionName) { | |
if (!functionObject) { | |
throw new ArgumentNullError('functionObject', functionName + ' must be specified.'); | |
} | |
if(!_.isFunction(functionObject)){ | |
throw new TypeError(functionName + ' specified should be a function.'); | |
} | |
return true; | |
}; | |
var getNameError = function(name, typeName) { | |
// checks if name is null, undefined or empty | |
if (azureutil.stringIsEmpty(name)) { | |
return new ArgumentNullError('name', util.format('%s name must be a non empty string.', typeName)); | |
} | |
// check if name is between 3 and 63 characters | |
if (name.length < 3 || name.length > 63) { | |
return new ArgumentError('name', util.format('%s name must be between 3 and 63 characters long.', typeName)); | |
} | |
// check if name follows naming rules | |
if (name.match(/^([a-z0-9]+(-[a-z0-9]+)*)$/) === null) { | |
return new SyntaxError(util.format('%s name format is incorrect.', typeName)); | |
} | |
return null; | |
}; | |
/** | |
* Validates a container name. | |
* | |
* @param {string} containerName The container name. | |
*/ | |
exports.containerNameIsValid = function (containerName, callback) { | |
var fail; | |
initCallback(callback, function (f, cb) { | |
fail = f; | |
callback = cb; | |
}); | |
var nameError = getNameError(containerName, 'Container'); | |
if (!nameError || containerName.match(/^(\$root|\$logs|\$web)$/)) { | |
callback(); | |
return true; | |
} else { | |
return fail(nameError); | |
} | |
}; | |
/** | |
* Validates a blob name. | |
* | |
* @param {string} containerName The container name. | |
* @param {string} blobname The blob name. | |
*/ | |
exports.blobNameIsValid = function (containerName, blobName, callback) { | |
var fail; | |
initCallback(callback, function (f, cb) { | |
fail = f; | |
callback = cb; | |
}); | |
if (!blobName) { | |
return fail(new ArgumentNullError('blobName', 'Blob name is not specified.')); | |
} | |
if (containerName === '$root' && blobName.indexOf('/') !== -1) { | |
return fail(new SyntaxError('Blob name format is incorrect.')); | |
} | |
callback(); | |
return true; | |
}; | |
/** | |
* Validates a blob tier name. | |
* | |
* @param {string} blobTier The blob tier name. | |
*/ | |
exports.blobTierNameIsValid = function (blobTier, callback) { | |
var fail; | |
initCallback(callback, function (f, cb) { | |
fail = f; | |
callback = cb; | |
}); | |
if (!blobTier) { | |
return fail(new ArgumentNullError('blobTier', 'Blob tier is not specified.')); | |
} | |
if (!_.chain(_.union( | |
_.values(BlobUtilities.BlobTier.PremiumPageBlobTier), | |
_.values(BlobUtilities.BlobTier.StandardBlobTier) | |
)) | |
.map(function (val) { return val.toString().toUpperCase(); }) | |
.contains(blobTier.toString().toUpperCase()) | |
.value()) { | |
return fail(new SyntaxError('Blob tier is incorrect. Refer to BlobUtilities.BlobTier for possible values.')); | |
} | |
callback(); | |
return true; | |
}; | |
/** | |
* Validates a share name. | |
* | |
* @param {string} shareName The share name. | |
*/ | |
exports.shareNameIsValid = function (shareName, callback) { | |
var fail; | |
initCallback(callback, function (f, cb) { | |
fail = f; | |
callback = cb; | |
}); | |
var nameError = getNameError(shareName, 'Share'); | |
if (!nameError) { | |
callback(); | |
return true; | |
} else { | |
return fail(nameError); | |
} | |
}; | |
/** | |
* Validates a queue name. | |
* | |
* @param {string} queueName The queue name. | |
*/ | |
exports.queueNameIsValid = function (queueName, callback) { | |
var fail; | |
initCallback(callback, function (f, cb) { | |
fail = f; | |
callback = cb; | |
}); | |
var nameError = getNameError(queueName, 'Queue'); | |
if (!nameError) { | |
callback(); | |
return true; | |
} else { | |
return fail(nameError); | |
} | |
}; | |
/** | |
* Validates a table name. | |
* | |
* @param {string} table The table name. | |
*/ | |
exports.tableNameIsValid = function (table, callback) { | |
var fail; | |
initCallback(callback, function (f, cb) { | |
fail = f; | |
callback = cb; | |
}); | |
if (azureutil.stringIsEmpty(table)) { | |
return fail(new ArgumentNullError('table', 'Table name must be a non empty string.')); | |
} | |
if (table.length < 3 || table.length > 63) { | |
return fail(new ArgumentError('table', 'Table name must be between 3 and 63 characters long.')); | |
} | |
if(table.toLowerCase() === 'tables') { | |
return fail(new RangeError('Table name cannot be \'Tables\'.')); | |
} | |
if (table.match(/^([A-Za-z][A-Za-z0-9]{2,62})$/) !== null || table === '$MetricsCapacityBlob' || table.match(/^(\$Metrics(HourPrimary|MinutePrimary|HourSecondary|MinuteSecondary)?(Transactions)(Blob|Queue|Table|File))$/) !== null) | |
{ | |
callback(); | |
return true; | |
} else { | |
return fail(new SyntaxError('Table name format is incorrect.')); | |
} | |
}; | |
/** | |
* Validates an HTML File object. | |
* | |
* @param {File} browserFile The HTML File object. | |
*/ | |
exports.browserFileIsValid = function (browserFile, callback) { | |
var fail; | |
initCallback(callback, function (f, cb) { | |
fail = f; | |
callback = cb; | |
}); | |
// IE doesn't support File.constructor.name | |
if (!azureutil.isBrowser() || | |
!browserFile || | |
!browserFile.constructor || | |
(!azureutil.isIE() && !browserFile.constructor.name) || | |
(!azureutil.isIE() && browserFile.constructor.name !== 'File' && browserFile.constructor.name !== 'Blob') || | |
!azureutil.objectIsInt(browserFile.size)) { | |
return fail(new ArgumentError('type', 'Invalid HTML File object.')); | |
} else { | |
callback(); | |
return true; | |
} | |
}; | |
/** | |
* Validates page ranges. | |
* | |
* @param {int} rangeStart The range starting position. | |
* @param {int} rangeEnd The range ending position. | |
* @param {int} writeBlockSizeInBytes The block size. | |
*/ | |
exports.pageRangesAreValid = function (rangeStart, rangeEnd, writeBlockSizeInBytes, callback) { | |
var fail; | |
initCallback(callback, function (f, cb) { | |
fail = f; | |
callback = cb; | |
}); | |
if (rangeStart % 512 !== 0) { | |
return fail(new RangeError('Start byte offset must be a multiple of 512.')); | |
} | |
var size = null; | |
if (!azureutil.objectIsNull(rangeEnd)) { | |
if ((rangeEnd + 1) % 512 !== 0) { | |
return fail(new RangeError('End byte offset must be a multiple of 512 minus 1.')); | |
} | |
size = (rangeEnd - rangeStart) + 1; | |
if (size > writeBlockSizeInBytes) { | |
return fail(new RangeError('Page blob size cannot be larger than ' + writeBlockSizeInBytes + ' bytes.')); | |
} | |
} | |
callback(); | |
return true; | |
}; | |
/** | |
* Validates a blob type. | |
* | |
* @param {string} type The type name. | |
*/ | |
exports.blobTypeIsValid = function (type, callback) { | |
var getEnumValues = function (obj) { | |
var values = []; | |
for (var prop in obj) { | |
if (obj.hasOwnProperty(prop)) { | |
values.push(obj[prop]); | |
} | |
} | |
return values; | |
}; | |
return this.isValidEnumValue(type, getEnumValues(blobConstants.BlobTypes), callback); | |
}; | |
/** | |
* Validates share ACL type. | |
* | |
* @param {string} type The type name. | |
*/ | |
exports.shareACLIsValid = function (type, callback) { | |
var fail; | |
initCallback(callback, function (f, cb) { | |
fail = f; | |
callback = cb; | |
}); | |
if (type != FileUtilities.SharePublicAccessType.OFF) { | |
fail(new ArgumentError('type', 'The access type is not supported.')); | |
} | |
callback(); | |
return true; | |
}; | |
/** | |
* Validates share quota value. | |
* | |
* @param {int} type The quota value. | |
*/ | |
exports.shareQuotaIsValid = function (quota, callback) { | |
var fail; | |
initCallback(callback, function (f, cb) { | |
fail = f; | |
callback = cb; | |
}); | |
if (quota && quota <= 0) { | |
fail(new RangeError('The share quota value, in GB, must be greater than 0.')); | |
} | |
callback(); | |
return true; | |
}; | |
// common functions for validating arguments | |
function throwMissingArgument(name, func) { | |
throw new ArgumentNullError(name, 'Required argument ' + name + ' for function ' + func + ' is not defined'); | |
} | |
function ArgumentValidator(functionName) { | |
this.func = functionName; | |
} | |
_.extend(ArgumentValidator.prototype, { | |
string: function (val, name) { | |
this.exists(val, name); | |
if (typeof val !== 'string') { | |
throw new TypeError('Parameter ' + name + ' for function ' + this.func + ' should be a non-empty string'); | |
} | |
}, | |
stringAllowEmpty: function (val, name) { | |
if (typeof val !== 'string') { | |
throw new TypeError('Parameter ' + name + ' for function ' + this.func + ' should be a string'); | |
} | |
}, | |
object: function (val, name) { | |
this.exists(val, name); | |
if (typeof val !== 'object') { | |
throw new TypeError('Parameter ' + name + ' for function ' + this.func + ' should be an object'); | |
} | |
}, | |
exists: function (val, name) { | |
if (!val) { | |
throwMissingArgument(name, this.func); | |
} | |
}, | |
function: function (val, name) { | |
this.exists(val, name); | |
if (typeof val !== 'function') { | |
throw new TypeError('Parameter ' + name + ' for function ' + this.func + ' should be a function'); | |
} | |
}, | |
value: function (val, name) { | |
if (!val && val !== 0) { | |
throwMissingArgument(name, this.func); | |
} | |
}, | |
nonEmptyArray: function (val, name) { | |
if (!val || val.length === 0) { | |
throw new TypeError('Required array argument ' + name + ' for function ' + this.func + ' is either not defined or empty'); | |
} | |
}, | |
callback: function (val) { | |
this.exists(val, 'callback'); | |
this.function(val, 'callback'); | |
}, | |
test: function (predicate, message) { | |
if (!predicate()) { | |
throw new Error(message + ' in function ' + this.func); | |
} | |
}, | |
tableNameIsValid: exports.tableNameIsValid, | |
browserFileIsValid: exports.browserFileIsValid, | |
containerNameIsValid: exports.containerNameIsValid, | |
shareNameIsValid: exports.shareNameIsValid, | |
blobNameIsValid: exports.blobNameIsValid, | |
blobTierNameIsValid: exports.blobTierNameIsValid, | |
pageRangesAreValid: exports.pageRangesAreValid, | |
queueNameIsValid: exports.queueNameIsValid, | |
blobTypeIsValid: exports.blobTypeIsValid, | |
shareACLIsValid: exports.shareACLIsValid, | |
shareQuotaIsValid: exports.shareQuotaIsValid, | |
isValidEnumValue: exports.isValidEnumValue | |
}); | |
function validateArgs(functionName, validationRules) { | |
var validator = new ArgumentValidator(functionName); | |
validationRules(validator); | |
} | |
exports.ArgumentValidator = ArgumentValidator; | |
exports.validateArgs = validateArgs; | |
},{"../errors/errors":5,"./../../services/blob/blobutilities":42,"./../../services/file/fileutilities":49,"./../util/constants":32,"./sr":36,"./util":38,"underscore":228,"util":234,"validator":240}],40:[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 BlobService = require('./blobservice.core'); | |
var azureCommon = require('./../../common/common.browser'); | |
var extend = require('extend'); | |
var mime = require('browserify-mime'); | |
var Constants = azureCommon.Constants; | |
var azureutil = azureCommon.util; | |
var BlobConstants = Constants.BlobConstants; | |
var BrowserFileReadStream = azureCommon.BrowserFileReadStream; | |
var SpeedSummary = azureCommon.SpeedSummary; | |
var validate = azureCommon.validate; | |
/** | |
* Creates a new block blob. If the blob already exists on the service, it will be overwritten. | |
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object. | |
* (Only available in the JavaScript Client Library for Browsers) | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {File} browserFile The File object to be uploaded created by HTML File API. | |
* @param {object} [options] The request options. | |
* @param {int} [options.blockSize] The size of each block. Maximum is 100MB. | |
* @param {string} [options.blockIdPrefix] The prefix to be used to generate the block id. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.transactionalContentMD5] The MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. The default value is true for block blobs. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the blob information. | |
* `response` will contain information related to this operation. | |
* @return {SpeedSummary} | |
*/ | |
BlobService.prototype.createBlockBlobFromBrowserFile = function (container, blob, browserFile, optionsOrCallback, callback) { | |
return this._createBlobFromBrowserFile(container, blob, BlobConstants.BlobTypes.BLOCK, browserFile, optionsOrCallback, callback); | |
}; | |
/** | |
* Uploads a page blob from an HTML file. If the blob already exists on the service, it will be overwritten. | |
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object. | |
* (Only available in the JavaScript Client Library for Browsers) | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {File} browserFile The File object to be uploaded created by HTML File API. | |
* @param {object} [options] The request options. | |
* @param {SpeedSummary} [options.speedSummary] The upload tracker objects. | |
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.transactionalContentMD5] An MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. | |
* The default value is false for page blobs. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the blob information. | |
* `response` will contain information related to this operation. | |
* @return {SpeedSummary} | |
*/ | |
BlobService.prototype.createPageBlobFromBrowserFile = function (container, blob, browserFile, optionsOrCallback, callback) { | |
return this._createBlobFromBrowserFile(container, blob, BlobConstants.BlobTypes.PAGE, browserFile, optionsOrCallback, callback); | |
}; | |
/** | |
* Creates a new append blob from an HTML File object. If the blob already exists on the service, it will be overwritten. | |
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object. | |
* This API should be used strictly in a single writer scenario because the API internally uses the append-offset conditional header to avoid duplicate blocks. | |
* If you are guaranteed to have a single writer scenario, please look at options.absorbConditionalErrorsOnRetry and see if setting this flag to true is acceptable for you. | |
* If you want to append data to an already existing blob, please look at appendFromBrowserFile. | |
* (Only available in the JavaScript Client Library for Browsers) | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {File} browserFile The File object to be uploaded created by HTML File API. | |
* @param {object} [options] The request options. | |
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. The default value is true for block blobs. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 ahash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the blob information. | |
* `response` will contain information related to this operation. | |
* @return {SpeedSummary} | |
*/ | |
BlobService.prototype.createAppendBlobFromBrowserFile = function (container, blob, browserFile, optionsOrCallback, callback) { | |
return this._createBlobFromBrowserFile(container, blob, BlobConstants.BlobTypes.APPEND, browserFile, optionsOrCallback, callback); | |
}; | |
/** | |
* Appends to an append blob from an HTML File object. Assumes the blob already exists on the service. | |
* This API should be used strictly in a single writer scenario because the API internally uses the append-offset conditional header to avoid duplicate blocks. | |
* If you are guaranteed to have a single writer scenario, please look at options.absorbConditionalErrorsOnRetry and see if setting this flag to true is acceptable for you. | |
* (Only available in the JavaScript Client Library for Browsers) | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {File} browserFile The File object to be uploaded created by HTML File API. | |
* @param {object} [options] The request options. | |
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the blob information. | |
* `response` will contain information related to this operation. | |
* @return {SpeedSummary} | |
*/ | |
BlobService.prototype.appendFromBrowserFile = function (container, blob, browserFile, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('appendFromBrowserFile', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.browserFileIsValid(browserFile); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
options.speedSummary = options.speedSummary || new SpeedSummary(blob); | |
var stream = new BrowserFileReadStream(browserFile); | |
var streamCallback = function (appendError, blob, response) { | |
if (azureutil.objectIsFunction(stream.destroy)) { | |
stream.destroy(); | |
} | |
callback(appendError, blob, response); | |
}; | |
this._uploadBlobFromStream(false, container, blob, BlobConstants.BlobTypes.APPEND, stream, browserFile.size, options, streamCallback); | |
return options.speedSummary; | |
}; | |
// Private methods | |
/** | |
* Creates a new blob (Block/Page/Append). If the blob already exists on the service, it will be overwritten. | |
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object. | |
* (Only available in the JavaScript Client Library for Browsers) | |
* | |
* @ignore | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {BlobType} blobType The blob type. | |
* @param {File} browserFile The File object to be uploaded created by HTML File API. | |
* @param {object} [options] The request options. | |
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. (For append blob only) | |
* @param {int} [options.blockSize] The size of each block. Maximum is 100MB. | |
* @param {string} [options.blockIdPrefix] The prefix to be used to generate the block id. (For block blob only) | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.transactionalContentMD5] An MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. The default value is true for block blobs. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The MD5 hash of the blob content. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 The callback function. | |
* | |
* @return {SpeedSummary} | |
* | |
*/ | |
BlobService.prototype._createBlobFromBrowserFile = function (container, blob, blobType, browserFile, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('_createBlobFromBrowserFile', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.blobTypeIsValid(blobType); | |
v.browserFileIsValid(browserFile); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
options.speedSummary = options.speedSummary || new SpeedSummary(blob); | |
var self = this; | |
var creationCallback = function (createError, createBlob, createResponse) { | |
if (createError) { | |
callback(createError, createBlob, createResponse); | |
} else { | |
// Automatically detect the mime type | |
if(azureutil.tryGetValueChain(options, ['contentSettings','contentType'], undefined) === undefined) { | |
azureutil.setObjectInnerPropertyValue(options, ['contentSettings','contentType'], mime.lookup(browserFile.name)); | |
} | |
var stream = new BrowserFileReadStream(browserFile); | |
var streamCallback = function (createError, createBlob, createResponse) { | |
if (azureutil.objectIsFunction(stream.destroy)) { | |
stream.destroy(); | |
} | |
callback(createError, createBlob, createResponse); | |
}; | |
self._uploadBlobFromStream(true, container, blob, blobType, stream, browserFile.size, options, streamCallback); | |
} | |
}; | |
this._createBlob(container, blob, blobType, browserFile.size, options, creationCallback); | |
return options.speedSummary; | |
}; | |
module.exports = BlobService; | |
},{"./../../common/common.browser":2,"./blobservice.core":41,"browserify-mime":89,"extend":137}],41:[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. | |
// | |
// Module dependencies. | |
var qs = require('querystring'); | |
var url = require('url'); | |
var util = require('util'); | |
var _ = require('underscore'); | |
var extend = require('extend'); | |
var azureCommon = require('./../../common/common.core'); | |
var BlockRangeStream = require('./internal/blockrangestream'); | |
var Md5Wrapper = require('./../../common/md5-wrapper'); | |
var PageRangeStream = require('./internal/pagerangestream'); | |
var RangeStream = require('./../../common/streams/rangestream'); | |
var azureutil = azureCommon.util; | |
var SR = azureCommon.SR; | |
var validate = azureCommon.validate; | |
var StorageServiceClient = azureCommon.StorageServiceClient; | |
var WebResource = azureCommon.WebResource; | |
// Constants | |
var Constants = azureCommon.Constants; | |
var BlobConstants = Constants.BlobConstants; | |
var HeaderConstants = Constants.HeaderConstants; | |
var QueryStringConstants = Constants.QueryStringConstants; | |
var RequestLocationMode = Constants.RequestLocationMode; | |
// Streams | |
var BatchOperation = azureCommon.BatchOperation; | |
var SpeedSummary = azureCommon.SpeedSummary; | |
var ChunkAllocator = azureCommon.ChunkAllocator; | |
var ChunkStream = azureCommon.ChunkStream; | |
var ChunkStreamWithStream = azureCommon.ChunkStreamWithStream; | |
// Models requires | |
var AclResult = azureCommon.AclResult; | |
var ServiceStatsParser = azureCommon.ServiceStatsParser; | |
var BlockListResult = require('./models/blocklistresult'); | |
var BlobResult = require('./models/blobresult'); | |
var ContainerResult = require('./models/containerresult'); | |
var LeaseResult = require('./models/leaseresult'); | |
var BlobUtilities = require('./blobutilities'); | |
// Errors requires | |
var errors = require('../../common/errors/errors'); | |
var ArgumentError = errors.ArgumentError; | |
var ArgumentNullError = errors.ArgumentNullError; | |
var StorageError = errors.StorageError; | |
/** | |
* Creates a new BlobService object. | |
* If no connection string or storageaccount and storageaccesskey are provided, | |
* the AZURE_STORAGE_CONNECTION_STRING or AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_ACCESS_KEY environment variables will be used. | |
* @class | |
* The BlobService class is used to perform operations on the Microsoft Azure Blob Service. | |
* The Blob Service provides storage for binary large objects, and provides | |
* functions for working with data stored in blobs as either streams or pages of data. | |
* | |
* For more information on the Blob Service, as well as task focused information on using it in a Node.js application, see | |
* [How to Use the Blob Service from Node.js](http://azure.microsoft.com/en-us/documentation/articles/storage-nodejs-how-to-use-blob-storage/). | |
* The following defaults can be set on the blob service. | |
* singleBlobPutThresholdInBytes The default maximum size, in bytes, of a blob before it must be separated into blocks. | |
* defaultEnableReuseSocket The default boolean value to enable socket reuse when uploading local files or streams. | |
* If the Node.js version is lower than 0.10.x, socket reuse will always be turned off. | |
* defaultTimeoutIntervalInMs The default timeout interval, in milliseconds, to use for request made via the Blob service. | |
* defaultClientRequestTimeoutInMs The default timeout of client requests, in milliseconds, to use for the request made via the Blob service. | |
* defaultMaximumExecutionTimeInMs The default maximum execution time across all potential retries, for requests made via the Blob service. | |
* defaultLocationMode The default location mode for requests made via the Blob service. | |
* parallelOperationThreadCount The number of parallel operations that may be performed when uploading a blob that is greater than | |
* the value specified by the singleBlobPutThresholdInBytes property in size. | |
* useNagleAlgorithm Determines whether the Nagle algorithm is used for requests made via the Blob service; true to use the | |
* Nagle algorithm; otherwise, false. The default value is false. | |
* @constructor | |
* @extends {StorageServiceClient} | |
* | |
* @param {string} [storageAccountOrConnectionString] The storage account or the connection string. | |
* @param {string} [storageAccessKey] The storage access key. | |
* @param {string|object} [host] The host address. To define primary only, pass a string. | |
* Otherwise 'host.primaryHost' defines the primary host and 'host.secondaryHost' defines the secondary host. | |
* @param {string} [sas] The Shared Access Signature string. | |
* @param {string} [endpointSuffix] The endpoint suffix. | |
* @param {TokenCredential} [token] The {@link TokenCredential} object. | |
*/ | |
function BlobService(storageAccountOrConnectionString, storageAccessKey, host, sas, endpointSuffix, token) { | |
var storageServiceSettings = StorageServiceClient.getStorageSettings(storageAccountOrConnectionString, storageAccessKey, host, sas, endpointSuffix, token); | |
BlobService['super_'].call(this, | |
storageServiceSettings._name, | |
storageServiceSettings._key, | |
storageServiceSettings._blobEndpoint, | |
storageServiceSettings._usePathStyleUri, | |
storageServiceSettings._sasToken, | |
token); | |
this.defaultEnableReuseSocket = Constants.DEFAULT_ENABLE_REUSE_SOCKET; | |
this.singleBlobPutThresholdInBytes = BlobConstants.DEFAULT_SINGLE_BLOB_PUT_THRESHOLD_IN_BYTES; | |
this.parallelOperationThreadCount = Constants.DEFAULT_PARALLEL_OPERATION_THREAD_COUNT; | |
} | |
util.inherits(BlobService, StorageServiceClient); | |
// Non-class methods | |
/** | |
* Create resource name | |
* @ignore | |
* | |
* @param {string} containerName Container name | |
* @param {string} blobName Blob name | |
* @return {string} The encoded resource name. | |
*/ | |
function createResourceName(containerName, blobName, forSAS) { | |
// Resource name | |
if (blobName && !forSAS) { | |
blobName = encodeURIComponent(blobName); | |
blobName = blobName.replace(/%2F/g, '/'); | |
blobName = blobName.replace(/%5C/g, '/'); | |
blobName = blobName.replace(/\+/g, '%20'); | |
} | |
// return URI encoded resource name | |
if (blobName) { | |
return containerName + '/' + blobName; | |
} | |
else { | |
return containerName; | |
} | |
} | |
// Blob service methods | |
/** | |
* Gets the service stats for a storage account’s Blob service. | |
* | |
* @this {BlobService} | |
* @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 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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link ServiceStats}` will contain the stats and | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.getServiceStats = function (optionsOrCallback, callback) { | |
var options; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { options = o; callback = c; }); | |
validate.validateArgs('getServiceStats', function (v) { | |
v.callback(callback); | |
}); | |
var webResource = WebResource.get() | |
.withQueryOption(QueryStringConstants.COMP, 'stats') | |
.withQueryOption(QueryStringConstants.RESTYPE, 'service'); | |
options.requestLocationMode = RequestLocationMode.PRIMARY_OR_SECONDARY; | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.serviceStatsResult = null; | |
if (!responseObject.error) { | |
responseObject.serviceStatsResult = ServiceStatsParser.parse(responseObject.response.body.StorageServiceStats); | |
} | |
// function to be called after all filters | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.serviceStatsResult, returnObject.response); | |
}; | |
// call the first filter | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Gets the properties of a storage account’s Blob service, including Azure Storage Analytics. | |
* | |
* @this {BlobService} | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link ServiceProperties}` will contain the properties | |
* and `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.getServiceProperties = function (optionsOrCallback, callback) { | |
return this.getAccountServiceProperties(optionsOrCallback, callback); | |
}; | |
/** | |
* Sets the properties of a storage account's Blob 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. | |
* When you set blob service properties (such as enabling soft delete), it may take up to 30 seconds to take effect. | |
* | |
* @this {BlobService} | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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. | |
*/ | |
BlobService.prototype.setServiceProperties = function (serviceProperties, optionsOrCallback, callback) { | |
return this.setAccountServiceProperties(serviceProperties, optionsOrCallback, callback); | |
}; | |
/** | |
* Sets the tier of a blockblob under a blob storage account, or the tier of a pageblob under a premium storage account. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {string} blobTier Please see BlobUtilities.BlobTier.StandardBlobTier or BlobUtilities.BlobTier.PremiumPageBlobTier for possible values. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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. | |
*/ | |
BlobService.prototype.setBlobTier = function (container, blob, blobTier, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('setBlobTier', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.string(blobTier, 'blobTier'); | |
v.containerNameIsValid(container); | |
v.blobNameIsValid(container, blob); | |
v.blobTierNameIsValid(blobTier); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.put(resourceName) | |
.withQueryOption(QueryStringConstants.COMP, 'tier') | |
.withHeader(HeaderConstants.ACCESS_TIER, blobTier); | |
var processResponseCallback = function (responseObject, next) { | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Lists a segment containing a collection of container items under the specified account. | |
* | |
* @this {BlobService} | |
* @param {object} currentToken A continuation token returned by a previous listing operation. Please use 'null' or 'undefined' if this is the first operation. | |
* @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.maxResults] Specifies the maximum number of containers to return per call to Azure storage. | |
* @param {string} [options.include] Include this parameter to specify that the container's metadata be returned as part of the response body. (allowed values: '', 'metadata') | |
* **Note** that all metadata names returned from the server will be converted to lower case by NodeJS itself as metadata is set via HTTP headers and HTTP header names are case insensitive. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 `entries` and `continuationToken`. | |
* `entries` gives a list of `[containers]{@link ContainerResult}` and the `continuationToken` is used for the next listing operation. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.listContainersSegmented = function (currentToken, optionsOrCallback, callback) { | |
this.listContainersSegmentedWithPrefix(null /* prefix */, currentToken, optionsOrCallback, callback); | |
}; | |
/** | |
* Lists a segment containing a collection of container items whose names begin with the specified prefix under the specified account. | |
* | |
* @this {BlobService} | |
* @param {string} prefix The prefix of the container name. | |
* @param {object} currentToken A continuation token returned by a previous listing operation. Please use 'null' or 'undefined' if this is the first operation. | |
* @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.maxResults] Specifies the maximum number of containers to return per call to Azure storage. | |
* @param {string} [options.include] Include this parameter to specify that the container's metadata be returned as part of the response body. (allowed values: '', 'metadata') | |
* **Note** that all metadata names returned from the server will be converted to lower case by NodeJS itself as metadata is set via HTTP headers and HTTP header names are case insensitive. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 `entries` and `continuationToken`. | |
* `entries` gives a list of `[containers]{@link ContainerResult}` and the `continuationToken` is used for the next listing operation. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.listContainersSegmentedWithPrefix = function (prefix, currentToken, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('listContainers', function (v) { | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var webResource = WebResource.get() | |
.withQueryOption(QueryStringConstants.COMP, 'list') | |
.withQueryOption(QueryStringConstants.MAX_RESULTS, options.maxResults) | |
.withQueryOption(QueryStringConstants.INCLUDE, options.include); | |
if (!azureutil.objectIsNull(currentToken)) { | |
webResource.withQueryOption(QueryStringConstants.MARKER, currentToken.nextMarker); | |
} | |
webResource.withQueryOption(QueryStringConstants.PREFIX, prefix); | |
options.requestLocationMode = azureutil.getNextListingLocationMode(currentToken); | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.listContainersResult = null; | |
if (!responseObject.error) { | |
responseObject.listContainersResult = { | |
entries: null, | |
continuationToken: null | |
}; | |
responseObject.listContainersResult.entries = []; | |
var containers = []; | |
if (responseObject.response.body.EnumerationResults.Containers && responseObject.response.body.EnumerationResults.Containers.Container) { | |
containers = responseObject.response.body.EnumerationResults.Containers.Container; | |
if (!_.isArray(containers)) { | |
containers = [containers]; | |
} | |
} | |
containers.forEach(function (currentContainer) { | |
var containerResult = ContainerResult.parse(currentContainer); | |
responseObject.listContainersResult.entries.push(containerResult); | |
}); | |
if (responseObject.response.body.EnumerationResults.NextMarker) { | |
responseObject.listContainersResult.continuationToken = { | |
nextMarker: null, | |
targetLocation: null | |
}; | |
responseObject.listContainersResult.continuationToken.nextMarker = responseObject.response.body.EnumerationResults.NextMarker; | |
responseObject.listContainersResult.continuationToken.targetLocation = responseObject.targetLocation; | |
} | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.listContainersResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
// Container methods | |
/** | |
* Checks whether or not a container exists on the service. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link ContainerResult}` will contain | |
* the container information including `exists` boolean member. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.doesContainerExist = function (container, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('doesContainerExist', function (v) { | |
v.string(container, 'container'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
this._doesContainerExist(container, false, options, callback); | |
}; | |
/** | |
* Creates a new container under the specified account. | |
* If a container with the same name already exists, the operation fails. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @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 {object} [options.metadata] The metadata key/value pairs. | |
* @param {string} [options.publicAccessLevel] Specifies whether data in the container may be accessed publicly and the level of access. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link ContainerResult}` will contain | |
* the container information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.createContainer = function (container, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('createContainer', function (v) { | |
v.string(container, 'container'); | |
v.test(function () { return container !== '$logs'; }, | |
'Container name format is incorrect'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var webResource = WebResource.put(container) | |
.withQueryOption(QueryStringConstants.RESTYPE, 'container'); | |
webResource.addOptionalMetadataHeaders(options.metadata); | |
webResource.withHeader(HeaderConstants.BLOB_PUBLIC_ACCESS, options.publicAccessLevel); | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.containerResult = null; | |
if (!responseObject.error) { | |
responseObject.containerResult = new ContainerResult(container); | |
responseObject.containerResult.getPropertiesFromHeaders(responseObject.response.headers); | |
if (options.metadata) { | |
responseObject.containerResult.metadata = options.metadata; | |
} | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.containerResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Creates a new container under the specified account if the container does not exists. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @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 {object} [options.metadata] The metadata key/value pairs. | |
* @param {string} [options.publicAccessLevel] Specifies whether data in the container may be accessed publicly and the level of access. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link ContainerResult}` will contain | |
* the container information including `created` boolean member. | |
* `response` will contain information related to this operation. | |
* | |
* @example | |
* var azure = require('azure-storage'); | |
* var blobService = azure.createBlobService(); | |
* blobService.createContainerIfNotExists('taskcontainer', {publicAccessLevel : 'blob'}, function(error) { | |
* if(!error) { | |
* // Container created or exists, and is public | |
* } | |
* }); | |
*/ | |
BlobService.prototype.createContainerIfNotExists = function (container, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('createContainerIfNotExists', function (v) { | |
v.string(container, 'container'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var self = this; | |
self._doesContainerExist(container, true, options, function (error, result, response) { | |
var exists = result.exists; | |
result.created = false; | |
delete result.exists; | |
if (error) { | |
callback(error, result, response); | |
} else if (exists) { | |
response.isSuccessful = true; | |
callback(error, result, response); | |
} else { | |
self.createContainer(container, options, function (createError, containerResult, createResponse) { | |
if (!createError) { | |
containerResult.created = true; | |
} | |
else if (createError && createError.statusCode === Constants.HttpConstants.HttpResponseCodes.Conflict && createError.code === Constants.BlobErrorCodeStrings.CONTAINER_ALREADY_EXISTS) { | |
// If it was created before, there was no actual error. | |
createError = null; | |
createResponse.isSuccessful = true; | |
} | |
callback(createError, containerResult, createResponse); | |
}); | |
} | |
}); | |
}; | |
/** | |
* Retrieves a container and its properties from a specified account. | |
* **Note** that all metadata names returned from the server will be converted to lower case by NodeJS itself as metadata is set via HTTP headers and HTTP header names are case insensitive. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @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 {string} [options.leaseId] The container lease identifier. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link ContainerResult}` will contain | |
* information for the container. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.getContainerProperties = function (container, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('getContainerProperties', function (v) { | |
v.string(container, 'container'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var webResource = WebResource.head(container) | |
.withQueryOption(QueryStringConstants.RESTYPE, 'container') | |
.withHeader(HeaderConstants.LEASE_ID, options.leaseId); | |
options.requestLocationMode = Constants.RequestLocationMode.PRIMARY_OR_SECONDARY; | |
var self = this; | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.containerResult = null; | |
if (!responseObject.error) { | |
responseObject.containerResult = new ContainerResult(container); | |
responseObject.containerResult.metadata = self.parseMetadataHeaders(responseObject.response.headers); | |
responseObject.containerResult.getPropertiesFromHeaders(responseObject.response.headers); | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.containerResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Returns all user-defined metadata for the container. | |
* **Note** that all metadata names returned from the server will be converted to lower case by NodeJS itself as metadata is set via HTTP headers and HTTP header names are case insensitive. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {object} [options] The request options. | |
* @param {string} [options.leaseId] The container lease identifier. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link ContainerResult}` will contain | |
* information for the container. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.getContainerMetadata = function (container, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('getContainerMetadata', function (v) { | |
v.string(container, 'container'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var webResource = WebResource.head(container) | |
.withQueryOption(QueryStringConstants.RESTYPE, 'container') | |
.withQueryOption(QueryStringConstants.COMP, 'metadata') | |
.withHeader(HeaderConstants.LEASE_ID, options.leaseId); | |
options.requestLocationMode = Constants.RequestLocationMode.PRIMARY_OR_SECONDARY; | |
var self = this; | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.containerResult = null; | |
if (!responseObject.error) { | |
responseObject.containerResult = new ContainerResult(container); | |
responseObject.containerResult.metadata = self.parseMetadataHeaders(responseObject.response.headers); | |
responseObject.containerResult.getPropertiesFromHeaders(responseObject.response.headers); | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.containerResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Sets the container's metadata. | |
* | |
* Calling the Set Container Metadata operation overwrites all existing metadata that is associated with the container. | |
* It's not possible to modify an individual name/value pair. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {object} metadata The metadata key/value pairs. | |
* @param {object} [options] The request options. | |
* @param {string} [options.leaseId] The container lease identifier. | |
* @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 {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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. | |
*/ | |
BlobService.prototype.setContainerMetadata = function (container, metadata, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('setContainerMetadata', function (v) { | |
v.string(container, 'container'); | |
v.object(metadata, 'metadata'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var webResource = WebResource.put(container) | |
.withQueryOption(QueryStringConstants.RESTYPE, 'container') | |
.withQueryOption(QueryStringConstants.COMP, 'metadata') | |
.withHeader(HeaderConstants.LEASE_ID, options.leaseId); | |
webResource.addOptionalMetadataHeaders(metadata); | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.containerResult = null; | |
if (!responseObject.error) { | |
responseObject.containerResult = new ContainerResult(container); | |
responseObject.containerResult.getPropertiesFromHeaders(responseObject.response.headers); | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.containerResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Gets the container's ACL. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {object} [options] The request options. | |
* @param {string} [options.leaseId] The container lease identifier. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link ContainerAclResult}` will contain | |
* information for the container. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.getContainerAcl = function (container, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('getContainerAcl', function (v) { | |
v.string(container, 'container'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var webResource = WebResource.get(container) | |
.withQueryOption(QueryStringConstants.RESTYPE, 'container') | |
.withQueryOption(QueryStringConstants.COMP, 'acl') | |
.withHeader(HeaderConstants.LEASE_ID, options.leaseId); | |
options.requestLocationMode = Constants.RequestLocationMode.PRIMARY_OR_SECONDARY; | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.containerResult = null; | |
if (!responseObject.error) { | |
responseObject.containerResult = new ContainerResult(container); | |
responseObject.containerResult.getPropertiesFromHeaders(responseObject.response.headers); | |
responseObject.containerResult.signedIdentifiers = AclResult.parse(responseObject.response.body); | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.containerResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Updates the container's ACL. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {Object.<string, AccessPolicy>} signedIdentifiers The container ACL settings. See `[AccessPolicy]{@link AccessPolicy}` for detailed information. | |
* @param {object} [options] The request options. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @param {string} [options.publicAccessLevel] Specifies whether data in the container may be accessed publicly and the level of access. | |
* @param {string} [options.leaseId] The container lease identifier. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link ContainerAclResult}` will contain | |
* information for the container. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.setContainerAcl = function (container, signedIdentifiers, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('setContainerAcl', function (v) { | |
v.string(container, 'container'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var policies = null; | |
if (signedIdentifiers) { | |
if (_.isArray(signedIdentifiers)) { | |
throw new TypeError(SR.INVALID_SIGNED_IDENTIFIERS); | |
} | |
policies = AclResult.serialize(signedIdentifiers); | |
} | |
var webResource = WebResource.put(container) | |
.withQueryOption(QueryStringConstants.RESTYPE, 'container') | |
.withQueryOption(QueryStringConstants.COMP, 'acl') | |
.withHeader(HeaderConstants.CONTENT_LENGTH, !azureutil.objectIsNull(policies) ? Buffer.byteLength(policies) : 0) | |
.withHeader(HeaderConstants.BLOB_PUBLIC_ACCESS, options.publicAccessLevel) | |
.withHeader(HeaderConstants.LEASE_ID, options.leaseId) | |
.withBody(policies); | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.containerResult = null; | |
if (!responseObject.error) { | |
responseObject.containerResult = new ContainerResult(container, options.publicAccessLevel); | |
responseObject.containerResult.getPropertiesFromHeaders(responseObject.response.headers); | |
if (signedIdentifiers) { | |
responseObject.containerResult.signedIdentifiers = signedIdentifiers; | |
} | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.containerResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, webResource.body, options, processResponseCallback); | |
}; | |
/** | |
* Marks the specified container for deletion. | |
* The container and any blobs contained within it are later deleted during garbage collection. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {object} [options] The request options. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @param {string} [options.leaseId] The container lease identifier. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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. | |
*/ | |
BlobService.prototype.deleteContainer = function (container, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('deleteContainer', function (v) { | |
v.string(container, 'container'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var webResource = WebResource.del(container) | |
.withQueryOption(QueryStringConstants.RESTYPE, 'container') | |
.withHeader(HeaderConstants.LEASE_ID, options.leaseId); | |
var processResponseCallback = function (responseObject, next) { | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Marks the specified container for deletion if it exists. | |
* The container and any blobs contained within it are later deleted during garbage collection. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {object} [options] The request options. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @param {string} [options.leaseId] The container lease identifier. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 | |
* be true if the container exists and was deleted, or false if the container | |
* did not exist. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.deleteContainerIfExists = function (container, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('deleteContainerIfExists', function (v) { | |
v.string(container, 'container'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var self = this; | |
self._doesContainerExist(container, true, options, function (error, result, response) { | |
if (error) { | |
callback(error, result.exists, response); | |
} else if (!result.exists) { | |
response.isSuccessful = true; | |
callback(error, false, response); | |
} else { | |
self.deleteContainer(container, options, function (deleteError, deleteResponse) { | |
var deleted; | |
if (!deleteError) { | |
deleted = true; | |
} else if (deleteError && deleteError.statuscode === Constants.HttpConstants.HttpResponseCodes.NotFound && deleteError.code === Constants.BlobErrorCodeStrings.CONTAINER_NOT_FOUND) { | |
// If it was deleted already, there was no actual error. | |
deleted = false; | |
deleteError = null; | |
deleteResponse.isSuccessful = true; | |
} | |
callback(deleteError, deleted, deleteResponse); | |
}); | |
} | |
}); | |
}; | |
/** | |
* Lists a segment containing a collection of blob directory items in the container. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {object} currentToken A continuation token returned by a previous listing operation. Please use 'null' or 'undefined' if this is the first operation. | |
* @param {object} [options] The request options. | |
* @param {int} [options.maxResults] Specifies the maximum number of directories to return per call to Azure ServiceClient. This does NOT affect list size returned by this function. (maximum: 5000) | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 `entries` and `continuationToken`. | |
* `entries` gives a list of `[directories]{@link DirectoryResult}` and the `continuationToken` is used for the next listing operation. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.listBlobDirectoriesSegmented = function (container, currentToken, optionsOrCallback, callback) { | |
this.listBlobDirectoriesSegmentedWithPrefix(container, null /* prefix */, currentToken, optionsOrCallback, callback); | |
}; | |
/** | |
* Lists a segment containing a collection of blob directory items in the container. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} prefix The prefix of the blob directory. | |
* @param {object} currentToken A continuation token returned by a previous listing operation. Please use 'null' or 'undefined' if this is the first operation. | |
* @param {object} [options] The request options. | |
* @param {int} [options.maxResults] Specifies the maximum number of directories to return per call to Azure ServiceClient. This does NOT affect list size returned by this function. (maximum: 5000) | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 `entries` and `continuationToken`. | |
* `entries` gives a list of `[directories]{@link BlobResult}` and the `continuationToken` is used for the next listing operation. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.listBlobDirectoriesSegmentedWithPrefix = function (container, prefix, currentToken, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
userOptions.delimiter = '/'; | |
this._listBlobsOrDircotriesSegmentedWithPrefix(container, prefix, currentToken, BlobConstants.ListBlobTypes.Directory, userOptions, callback); | |
}; | |
/** | |
* Lists a segment containing a collection of blob items in the container. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {object} currentToken A continuation token returned by a previous listing operation. Please use 'null' or 'undefined' if this is the first operation. | |
* @param {object} [options] The request options. | |
* @param {string} [options.delimiter] Delimiter, i.e. '/', for specifying folder hierarchy. | |
* @param {int} [options.maxResults] Specifies the maximum number of blobs to return per call to Azure ServiceClient. This does NOT affect list size returned by this function. (maximum: 5000) | |
* @param {string} [options.include] Specifies that the response should include one or more of the following subsets: '', 'metadata', 'snapshots', 'uncommittedblobs', 'copy', 'deleted'). | |
* Please find these values in BlobUtilities.BlobListingDetails. Multiple values can be added separated with a comma (,). | |
* **Note** that all metadata names returned from the server will be converted to lower case by NodeJS itself as metadata is set via HTTP headers and HTTP header names are case insensitive. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 `entries` and `continuationToken`. | |
* `entries` gives a list of `[blobs]{@link BlobResult}` and the `continuationToken` is used for the next listing operation. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.listBlobsSegmented = function (container, currentToken, optionsOrCallback, callback) { | |
this.listBlobsSegmentedWithPrefix(container, null /* prefix */, currentToken, optionsOrCallback, callback); | |
}; | |
/** | |
* Lists a segment containing a collection of blob items whose names begin with the specified prefix in the container. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} prefix The prefix of the blob name. | |
* @param {object} currentToken A continuation token returned by a previous listing operation. Please use 'null' or 'undefined' if this is the first operation. | |
* @param {object} [options] The request options. | |
* @param {string} [options.delimiter] Delimiter, i.e. '/', for specifying folder hierarchy. | |
* @param {int} [options.maxResults] Specifies the maximum number of blobs to return per call to Azure ServiceClient. This does NOT affect list size returned by this function. (maximum: 5000) | |
* @param {string} [options.include] Specifies that the response should include one or more of the following subsets: '', 'metadata', 'snapshots', 'uncommittedblobs', 'copy', 'deleted'). | |
* Please find these values in BlobUtilities.BlobListingDetails. Multiple values can be added separated with a comma (,). | |
* **Note** that all metadata names returned from the server will be converted to lower case by NodeJS itself as metadata is set via HTTP headers and HTTP header names are case insensitive. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 entries of `[blobs]{@link BlobResult}` and the continuation token for the next listing operation. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.listBlobsSegmentedWithPrefix = function (container, prefix, currentToken, optionsOrCallback, callback) { | |
this._listBlobsOrDircotriesSegmentedWithPrefix(container, prefix, currentToken, BlobConstants.ListBlobTypes.Blob, optionsOrCallback, callback); | |
}; | |
// Lease methods | |
/** | |
* Acquires a new lease. If container and blob are specified, acquires a blob lease. Otherwise, if only container is specified and blob is null, acquires a container lease. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} [options] The request options. | |
* @param {string} [options.leaseDuration] The lease duration in seconds. A non-infinite lease can be between 15 and 60 seconds. Default is never to expire. | |
* @param {string} [options.proposedLeaseId] The proposed lease identifier. Must be a GUID. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link LeaseResult}` will contain | |
* the lease information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.acquireLease = function (container, blob, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('acquireLease', function (v) { | |
v.string(container, 'container'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
if (!options.leaseDuration) { | |
options.leaseDuration = -1; | |
} | |
this._leaseImpl(container, blob, null /* leaseId */, BlobConstants.LeaseOperation.ACQUIRE, options, callback); | |
}; | |
/** | |
* Renews an existing lease. If container and blob are specified, renews the blob lease. Otherwise, if only container is specified and blob is null, renews the container lease. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {string} leaseId The lease identifier. Must be a GUID. | |
* @param {object} [options] The request options. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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]{@link LeaseResult}` will contain | |
* the lease information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.renewLease = function (container, blob, leaseId, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('renewLease', function (v) { | |
v.string(container, 'container'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
this._leaseImpl(container, blob, leaseId, BlobConstants.LeaseOperation.RENEW, options, callback); | |
}; | |
/** | |
* Changes the lease ID of an active lease. If container and blob are specified, changes the blob lease. Otherwise, if only container is specified and blob is null, changes the | |
* container lease. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {string} leaseId The current lease identifier. | |
* @param {string} proposedLeaseId The proposed lease identifier. Must be a GUID. | |
* @param {object} [options] The request options. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link LeaseResult}` will contain the lease information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.changeLease = function (container, blob, leaseId, proposedLeaseId, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('changeLease', function (v) { | |
v.string(container, 'container'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
options.proposedLeaseId = proposedLeaseId; | |
this._leaseImpl(container, blob, leaseId, BlobConstants.LeaseOperation.CHANGE, options, callback); | |
}; | |
/** | |
* Releases the lease. If container and blob are specified, releases the blob lease. Otherwise, if only container is specified and blob is null, releases the container lease. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {string} leaseId The lease identifier. | |
* @param {object} [options] The request options. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link LeaseResult}` will contain | |
* the lease information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.releaseLease = function (container, blob, leaseId, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('releaseLease', function (v) { | |
v.string(container, 'container'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
this._leaseImpl(container, blob, leaseId, BlobConstants.LeaseOperation.RELEASE, options, callback); | |
}; | |
/** | |
* Breaks the lease but ensures that another client cannot acquire a new lease until the current lease period has expired. If container and blob are specified, breaks the blob lease. | |
* Otherwise, if only container is specified and blob is null, breaks the container lease. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} [options] The request options. | |
* @param {int} [options.leaseBreakPeriod] The lease break period, between 0 and 60 seconds. If unspecified, a fixed-duration lease breaks after | |
* the remaining lease period elapses, and an infinite lease breaks immediately. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link LeaseResult}` will contain | |
* the lease information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.breakLease = function (container, blob, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('breakLease', function (v) { | |
v.string(container, 'container'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
this._leaseImpl(container, blob, null /*leaseId*/, BlobConstants.LeaseOperation.BREAK, options, callback); | |
}; | |
// Blob methods | |
/** | |
* Returns all user-defined metadata, standard HTTP properties, and system properties for the blob. | |
* It does not return or modify the content of the blob. | |
* **Note** that all metadata names returned from the server will be converted to lower case by NodeJS itself as metadata is set via HTTP headers and HTTP header names are case insensitive. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} [options] The request options. | |
* @param {string} [options.snapshotId] The snapshot identifier. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* information about the blob. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.getBlobProperties = function (container, blob, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('getBlobProperties', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.head(resourceName); | |
if (options.snapshotId) { | |
webResource.withQueryOption(QueryStringConstants.SNAPSHOT, options.snapshotId); | |
} | |
BlobResult.setHeadersFromBlob(webResource, options); | |
options.requestLocationMode = Constants.RequestLocationMode.PRIMARY_OR_SECONDARY; | |
var self = this; | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.blobResult = null; | |
if (!responseObject.error) { | |
responseObject.blobResult = new BlobResult(container, blob); | |
responseObject.blobResult.metadata = self.parseMetadataHeaders(responseObject.response.headers); | |
responseObject.blobResult.getPropertiesFromHeaders(responseObject.response.headers); | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.blobResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Returns all user-defined metadata for the specified blob or snapshot. | |
* It does not modify or return the content of the blob. | |
* **Note** that all metadata names returned from the server will be converted to lower case by NodeJS itself as metadata is set via HTTP headers and HTTP header names are case insensitive. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} [options] The request options. | |
* @param {string} [options.snapshotId] The snapshot identifier. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* information about the blob. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.getBlobMetadata = function (container, blob, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('getBlobMetadata', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.head(resourceName); | |
webResource.withQueryOption(QueryStringConstants.COMP, 'metadata'); | |
webResource.withQueryOption(QueryStringConstants.SNAPSHOT, options.snapshotId); | |
BlobResult.setHeadersFromBlob(webResource, options); | |
options.requestLocationMode = Constants.RequestLocationMode.PRIMARY_OR_SECONDARY; | |
var self = this; | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.blobResult = null; | |
if (!responseObject.error) { | |
responseObject.blobResult = new BlobResult(container, blob); | |
responseObject.blobResult.metadata = self.parseMetadataHeaders(responseObject.response.headers); | |
responseObject.blobResult.getPropertiesFromHeaders(responseObject.response.headers); | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.blobResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Sets user-defined properties for the specified blob or snapshot. | |
* It does not modify or return the content of the blob. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} [properties] The blob properties to set. | |
* @param {string} [properties.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [properties.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [properties.contentLanguage] The natural languages used by this resource. | |
* @param {string} [properties.cacheControl] The blob's cache control. | |
* @param {string} [properties.contentDisposition] The blob's content disposition. | |
* @param {string} [properties.contentMD5] The blob's MD5 hash. | |
* @param {object} [options] The request options. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @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 {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* information about the blob. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.setBlobProperties = function (container, blob, properties, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('setBlobProperties', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, { contentSettings: properties }, userOptions); | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.put(resourceName) | |
.withQueryOption(QueryStringConstants.COMP, 'properties'); | |
BlobResult.setPropertiesFromBlob(webResource, options); | |
this._setBlobPropertiesHelper({ | |
webResource: webResource, | |
options: options, | |
container: container, | |
blob: blob, | |
callback: callback | |
}); | |
}; | |
/** | |
* Sets user-defined metadata for the specified blob or snapshot as one or more name-value pairs | |
* It does not modify or return the content of the blob. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} metadata The metadata key/value pairs. | |
* @param {object} [options] The request options. | |
* @param {string} [options.snapshotId] The snapshot identifier. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* information on the blob. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.setBlobMetadata = function (container, blob, metadata, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('setBlobMetadata', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.object(metadata, 'metadata'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.put(resourceName) | |
.withQueryOption(QueryStringConstants.COMP, 'metadata'); | |
webResource.withQueryOption(QueryStringConstants.SNAPSHOT, options.snapshotId); | |
options.metadata = metadata; | |
BlobResult.setHeadersFromBlob(webResource, options); | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.blobResult = null; | |
if (!responseObject.error) { | |
responseObject.blobResult = new BlobResult(container, blob); | |
responseObject.blobResult.getPropertiesFromHeaders(responseObject.response.headers); | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.blobResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Provides a stream to read from a blob. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} [options] The request options. | |
* @param {string} [options.snapshotId] The snapshot identifier. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.rangeStart] Return only the bytes of the blob in the specified range. | |
* @param {string} [options.rangeEnd] Return only the bytes of the blob in the specified range. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @param {boolean} [options.useTransactionalMD5] When set to true, Calculate and send/validate content MD5 for transactions. | |
* @param {boolean} [options.disableContentMD5Validation] When set to true, MD5 validation will be disabled when downloading blobs. | |
* @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 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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain the blob information. | |
* `response` will contain information related to this operation. | |
* @return {Readable} A Node.js Readable stream. | |
* @example | |
* var azure = require('azure-storage'); | |
* var blobService = azure.createBlobService(); | |
* var writable = fs.createWriteStream(destinationFileNameTarget); | |
* blobService.createReadStream(containerName, blobName).pipe(writable); | |
*/ | |
BlobService.prototype.createReadStream = function (container, blob, optionsOrCallback, callback) { | |
var options; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { options = o; callback = c; }); | |
validate.validateArgs('createReadStream', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
}); | |
var readStream = new ChunkStream(); | |
this.getBlobToStream(container, blob, readStream, options, function (error, responseBlob, response) { | |
if (error) { | |
readStream.emit('error', error); | |
} | |
if (callback) { | |
callback(error, responseBlob, response); | |
} | |
}); | |
return readStream; | |
}; | |
/** | |
* Downloads a blob into a stream. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {Writable} writeStream The Node.js Writable stream. | |
* @param {object} [options] The request options. | |
* @param {boolean} [options.skipSizeCheck] Skip the size check to perform direct download. | |
* Set the option to true for small blobs. | |
* Parallel download and speed summary won't work with this option on. | |
* @param {SpeedSummary} [options.speedSummary] The download tracker objects. | |
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading. | |
* @param {string} [options.snapshotId] The snapshot identifier. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.rangeStart] Return only the bytes of the blob in the specified range. | |
* @param {string} [options.rangeEnd] Return only the bytes of the blob in the specified range. | |
* @param {boolean} [options.useTransactionalMD5] When set to true, Calculate and send/validate content MD5 for transactions. | |
* @param {boolean} [options.disableContentMD5Validation] When set to true, MD5 validation will be disabled when downloading blobs. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain the blob information. | |
* `response` will contain information related to this operation. | |
* @return {SpeedSummary} | |
* | |
* @example | |
* var azure = require('azure-storage'); | |
* var blobService = azure.createBlobService(); | |
* blobService.getBlobToStream('taskcontainer', 'task1', fs.createWriteStream('task1-download.txt'), function(error, serverBlob) { | |
* if(!error) { | |
* // Blob available in serverBlob.blob variable | |
* } | |
* }); | |
*/ | |
BlobService.prototype.getBlobToStream = function (container, blob, writeStream, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
userOptions.speedSummary = userOptions.speedSummary || new SpeedSummary(blob); | |
validate.validateArgs('getBlobToStream', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.object(writeStream, 'writeStream'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var propertiesRequestOptions = { | |
timeoutIntervalInMs: options.timeoutIntervalInMs, | |
clientRequestTimeoutInMs: options.clientRequestTimeoutInMs, | |
snapshotId: options.snapshotId, | |
accessConditions: options.accessConditions | |
}; | |
if (options.skipSizeCheck) { | |
this._getBlobToStream(container, blob, writeStream, options, callback); | |
} else { | |
var self = this; | |
this.getBlobProperties(container, blob, propertiesRequestOptions, function (error, properties) { | |
if (error) { | |
callback(error); | |
} else { | |
var size; | |
if (options.rangeStart) { | |
var endOffset = properties.contentLength - 1; | |
var end = options.rangeEnd ? Math.min(options.rangeEnd, endOffset) : endOffset; | |
size = end - options.rangeStart + 1; | |
} else { | |
size = properties.contentLength; | |
} | |
options.speedSummary.totalSize = size; | |
if (size > self.singleBlobPutThresholdInBytes) { | |
azureutil.setObjectInnerPropertyValue(options, ['contentSettings', 'contentMD5'], azureutil.tryGetValueChain(properties, ['contentSettings', 'contentMD5'], null)); | |
self._getBlobToRangeStream(container, blob, properties.blobType, writeStream, options, callback); | |
} else { | |
self._getBlobToStream(container, blob, writeStream, options, callback); | |
} | |
} | |
}); | |
} | |
return options.speedSummary; | |
}; | |
/** | |
* Downloads a blob into a text string. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} [options] The request options. | |
* @param {string} [options.snapshotId] The snapshot identifier. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.rangeStart] Return only the bytes of the blob in the specified range. | |
* @param {string} [options.rangeEnd] Return only the bytes of the blob in the specified range. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {boolean} [options.disableContentMD5Validation] When set to true, MD5 validation will be disabled when downloading blobs. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 {BlobService~blobToText} callback `error` will contain information | |
* if an error occurs; otherwise `text` will contain the blob contents, | |
* and `[blockBlob]{@link BlobResult}` will contain | |
* the blob information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.getBlobToText = function (container, blob, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('getBlobToText', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.get(resourceName) | |
.withRawResponse(); | |
webResource.withQueryOption(QueryStringConstants.SNAPSHOT, options.snapshotId); | |
BlobResult.setHeadersFromBlob(webResource, options); | |
this._setRangeContentMD5Header(webResource, options); | |
options.requestLocationMode = Constants.RequestLocationMode.PRIMARY_OR_SECONDARY; | |
var self = this; | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.text = null; | |
responseObject.blobResult = null; | |
if (!responseObject.error) { | |
responseObject.blobResult = new BlobResult(container, blob); | |
responseObject.blobResult.metadata = self.parseMetadataHeaders(responseObject.response.headers); | |
responseObject.blobResult.getPropertiesFromHeaders(responseObject.response.headers); | |
responseObject.text = responseObject.response.body; | |
self._validateLengthAndMD5(options, responseObject); | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.text, returnObject.blobResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Marks the specified blob or snapshot for deletion. The blob is later deleted during garbage collection. | |
* If a blob has snapshots, you must delete them when deleting the blob. Using the deleteSnapshots option, you can choose either to delete both the blob and its snapshots, | |
* or to delete only the snapshots but not the blob itself. If the blob has snapshots, you must include the deleteSnapshots option or the blob service will return an error | |
* and nothing will be deleted. | |
* If you are deleting a specific snapshot using the snapshotId option, the deleteSnapshots option must NOT be included. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} [options] The request options. | |
* @param {string} [options.deleteSnapshots] The snapshot delete option. See azure.BlobUtilities.SnapshotDeleteOptions.*. | |
* @param {string} [options.snapshotId] The snapshot identifier. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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; `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.deleteBlob = function (container, blob, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('deleteBlob', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.del(resourceName) | |
.withHeader(HeaderConstants.LEASE_ID, options.leaseId); | |
if (!azureutil.objectIsNull(options.snapshotId) && !azureutil.objectIsNull(options.deleteSnapshots)) { | |
throw new ArgumentError('options', SR.INVALID_DELETE_SNAPSHOT_OPTION); | |
} | |
webResource.withQueryOption(QueryStringConstants.SNAPSHOT, options.snapshotId); | |
webResource.withHeader(HeaderConstants.DELETE_SNAPSHOT, options.deleteSnapshots); | |
BlobResult.setHeadersFromBlob(webResource, options); | |
var processResponseCallback = function (responseObject, next) { | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* The undelete Blob operation restores the contents and metadata of soft deleted blob or snapshot. | |
* Attempting to undelete a blob or snapshot that is not soft deleted will succeed without any changes. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} [options] The request options. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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; `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.undeleteBlob = function (container, blob, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('deleteBlob', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.put(resourceName) | |
.withQueryOption(QueryStringConstants.COMP, 'undelete'); | |
BlobResult.setHeadersFromBlob(webResource, options); | |
var processResponseCallback = function (responseObject, next) { | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Checks whether or not a blob exists on the service. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} [options] The request options. | |
* @param {string} [options.snapshotId] The snapshot identifier. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 {Function(error, result, response)} callback `error` will contain information | |
* if an error occurs; otherwise `[result]{@link BlobResult}` will contain | |
* the blob information including the `exists` boolean member. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.doesBlobExist = function (container, blob, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('doesBlobExist', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
this._doesBlobExist(container, blob, false, options, callback); | |
}; | |
/** | |
* Marks the specified blob or snapshot for deletion if it exists. The blob is later deleted during garbage collection. | |
* If a blob has snapshots, you must delete them when deleting the blob. Using the deleteSnapshots option, you can choose either to delete both the blob and its snapshots, | |
* or to delete only the snapshots but not the blob itself. If the blob has snapshots, you must include the deleteSnapshots option or the blob service will return an error | |
* and nothing will be deleted. | |
* If you are deleting a specific snapshot using the snapshotId option, the deleteSnapshots option must NOT be included. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} [options] The request options. | |
* @param {string} [options.deleteSnapshots] The snapshot delete option. See azure.BlobUtilities.SnapshotDeleteOptions.*. | |
* @param {string} [options.snapshotId] The snapshot identifier. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 | |
* be true if the blob was deleted, or false if the blob | |
* does not exist. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.deleteBlobIfExists = function (container, blob, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('deleteBlobIfExists', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var self = this; | |
self._doesBlobExist(container, blob, true, options, function (error, existsResult, response) { | |
if (error) { | |
callback(error, existsResult.exists, response); | |
} else if (!existsResult.exists) { | |
response.isSuccessful = true; | |
callback(error, false, response); | |
} else { | |
self.deleteBlob(container, blob, options, function (deleteError, deleteResponse) { | |
var deleted; | |
if (!deleteError) { | |
deleted = true; | |
} else if (deleteError && deleteError.statusCode === Constants.HttpConstants.HttpResponseCodes.NotFound && deleteError.code === Constants.BlobErrorCodeStrings.BLOB_NOT_FOUND) { | |
// If it was deleted already, there was no actual error. | |
deleted = false; | |
deleteError = null; | |
deleteResponse.isSuccessful = true; | |
} | |
callback(deleteError, deleted, deleteResponse); | |
}); | |
} | |
}); | |
}; | |
/** | |
* Creates a read-only snapshot of a blob. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} [options] The request options. | |
* @param {string} [options.snapshotId] The snapshot identifier. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 ID of the snapshot. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.createBlobSnapshot = function (container, blob, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('createBlobSnapshot', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.put(resourceName) | |
.withQueryOption(QueryStringConstants.COMP, 'snapshot'); | |
BlobResult.setHeadersFromBlob(webResource, options); | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.snapshotId = null; | |
if (!responseObject.error) { | |
responseObject.snapshotId = responseObject.response.headers[HeaderConstants.SNAPSHOT]; | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.snapshotId, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Starts to copy a blob to a destination within the storage account. The Copy Blob operation copies the entire committed blob. | |
* | |
* @this {BlobService} | |
* @param {string} sourceUri The source blob URI. | |
* @param {string} targetContainer The target container name. | |
* @param {string} targetBlob The target blob name. | |
* @param {object} [options] The request options. | |
* @param {string} [options.blobTier] For page blobs on premium accounts only. Set the tier of target blob. Refer to BlobUtilities.BlobTier.PremiumPageBlobTier. | |
* @param {boolean} [options.isIncrementalCopy] If it's incremental copy or not. Refer to https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/incremental-copy-blob | |
* @param {string} [options.snapshotId] The source blob snapshot identifier. | |
* @param {object} [options.metadata] The target blob metadata key/value pairs. | |
* @param {string} [options.leaseId] The target blob lease identifier. | |
* @param {string} [options.sourceLeaseId] The source blob lease identifier. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @param {AccessConditions} [options.sourceAccessConditions] The source access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the blob information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.startCopyBlob = function (sourceUri, targetContainer, targetBlob, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('startCopyBlob', function (v) { | |
v.string(sourceUri, 'sourceUri'); | |
v.string(targetContainer, 'targetContainer'); | |
v.string(targetBlob, 'targetBlob'); | |
v.containerNameIsValid(targetContainer); | |
v.callback(callback); | |
}); | |
var targetResourceName = createResourceName(targetContainer, targetBlob); | |
var options = extend(true, {}, userOptions); | |
if (options.snapshotId) { | |
var uri = url.parse(sourceUri, true); | |
if (uri.query['snapshot']) { | |
throw new ArgumentError('options.snapshotId', 'Duplicate snapshot supplied in both the source uri and option.'); | |
} | |
uri.search = undefined; | |
uri.query['snapshot'] = options.snapshotId; | |
sourceUri = url.format(uri); | |
} | |
var webResource = WebResource.put(targetResourceName) | |
.withHeader(HeaderConstants.COPY_SOURCE, sourceUri); | |
if (options.isIncrementalCopy) { | |
webResource.withQueryOption(QueryStringConstants.COMP, 'incrementalcopy'); | |
} | |
webResource.withHeader(HeaderConstants.ACCESS_TIER, options.blobTier); | |
webResource.withHeader(HeaderConstants.LEASE_ID, options.leaseId); | |
webResource.withHeader(HeaderConstants.SOURCE_LEASE_ID, options.sourceLeaseId); | |
webResource.addOptionalMetadataHeaders(options.metadata); | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.blobResult = null; | |
if (!responseObject.error) { | |
responseObject.blobResult = new BlobResult(targetContainer, targetBlob); | |
responseObject.blobResult.getPropertiesFromHeaders(responseObject.response.headers); | |
if (options.metadata) { | |
responseObject.blobResult.metadata = options.metadata; | |
} | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.blobResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Abort a blob copy operation. | |
* | |
* @this {BlobService} | |
* @param {string} container The destination container name. | |
* @param {string} blob The destination blob name. | |
* @param {string} copyId The copy operation identifier. | |
* @param {object} [options] The request options. | |
* @param {string} [options.leaseId] The target blob lease identifier. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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; | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.abortCopyBlob = function (container, blob, copyId, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('abortCopyBlob', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var resourceName = createResourceName(container, blob); | |
var options = extend(true, {}, userOptions); | |
var webResource = WebResource.put(resourceName) | |
.withQueryOption(QueryStringConstants.COPY_ID, copyId) | |
.withQueryOption(QueryStringConstants.COMP, 'copy') | |
.withHeader(HeaderConstants.COPY_ACTION, 'abort'); | |
webResource.withHeader(HeaderConstants.LEASE_ID, options.leaseId); | |
var processResponseCallback = function (responseObject, next) { | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Retrieves a shared access signature token. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} [blob] The blob name. | |
* @param {object} sharedAccessPolicy The shared access policy. | |
* @param {string} [sharedAccessPolicy.Id] The signed identifier. | |
* @param {object} [sharedAccessPolicy.AccessPolicy.Permissions] The permission type. | |
* @param {date|string} [sharedAccessPolicy.AccessPolicy.Start] The time at which the Shared Access Signature becomes valid (The UTC value will be used). | |
* @param {date|string} [sharedAccessPolicy.AccessPolicy.Expiry] The time at which the Shared Access Signature becomes expired (The UTC value will be used). | |
* @param {string} [sharedAccessPolicy.AccessPolicy.IPAddressOrRange] An IP address or a range of IP addresses from which to accept requests. When specifying a range, note that the range is inclusive. | |
* @param {string} [sharedAccessPolicy.AccessPolicy.Protocols] The protocols permitted for a request made with the account SAS. | |
* Possible values are both HTTPS and HTTP (https,http) or HTTPS only (https). The default value is https,http. | |
* @param {object} [headers] The optional header values to set for a blob returned wth this SAS. | |
* @param {string} [headers.cacheControl] The optional value of the Cache-Control response header to be returned when this SAS is used. | |
* @param {string} [headers.contentType] The optional value of the Content-Type response header to be returned when this SAS is used. | |
* @param {string} [headers.contentEncoding] The optional value of the Content-Encoding response header to be returned when this SAS is used. | |
* @param {string} [headers.contentLanguage] The optional value of the Content-Language response header to be returned when this SAS is used. | |
* @param {string} [headers.contentDisposition] The optional value of the Content-Disposition response header to be returned when this SAS is used. | |
* @return {string} The shared access signature query string. Note this string does not contain the leading "?". | |
*/ | |
BlobService.prototype.generateSharedAccessSignature = function (container, blob, sharedAccessPolicy, headers) { | |
// check if the BlobService is able to generate a shared access signature | |
if (!this.storageCredentials) { | |
throw new ArgumentNullError('storageCredentials'); | |
} | |
if (!this.storageCredentials.generateSignedQueryString) { | |
throw new ArgumentError('storageCredentials', SR.CANNOT_CREATE_SAS_WITHOUT_ACCOUNT_KEY); | |
} | |
// Validate container name. Blob name is optional. | |
validate.validateArgs('generateSharedAccessSignature', function (v) { | |
v.string(container, 'container'); | |
v.containerNameIsValid(container); | |
v.object(sharedAccessPolicy, 'sharedAccessPolicy'); | |
}); | |
var resourceType = BlobConstants.ResourceTypes.CONTAINER; | |
if (blob) { | |
validate.validateArgs('generateSharedAccessSignature', function (v) { | |
v.string(blob, 'blob'); | |
}); | |
resourceType = BlobConstants.ResourceTypes.BLOB; | |
} | |
if (sharedAccessPolicy.AccessPolicy) { | |
if (!azureutil.objectIsNull(sharedAccessPolicy.AccessPolicy.Start)) { | |
if (!_.isDate(sharedAccessPolicy.AccessPolicy.Start)) { | |
sharedAccessPolicy.AccessPolicy.Start = new Date(sharedAccessPolicy.AccessPolicy.Start); | |
} | |
sharedAccessPolicy.AccessPolicy.Start = azureutil.truncatedISO8061Date(sharedAccessPolicy.AccessPolicy.Start); | |
} | |
if (!azureutil.objectIsNull(sharedAccessPolicy.AccessPolicy.Expiry)) { | |
if (!_.isDate(sharedAccessPolicy.AccessPolicy.Expiry)) { | |
sharedAccessPolicy.AccessPolicy.Expiry = new Date(sharedAccessPolicy.AccessPolicy.Expiry); | |
} | |
sharedAccessPolicy.AccessPolicy.Expiry = azureutil.truncatedISO8061Date(sharedAccessPolicy.AccessPolicy.Expiry); | |
} | |
} | |
var resourceName = createResourceName(container, blob, true); | |
return this.storageCredentials.generateSignedQueryString(Constants.ServiceType.Blob, resourceName, sharedAccessPolicy, null, { headers: headers, resourceType: resourceType }); | |
}; | |
/** | |
* Retrieves a blob or container URL. | |
* | |
* @param {string} container The container name. | |
* @param {string} [blob] The blob name. | |
* @param {string} [sasToken] The Shared Access Signature token. | |
* @param {boolean} [primary] A boolean representing whether to use the primary or the secondary endpoint. | |
* @param {string} [snapshotId] The snapshot identifier. | |
* @return {string} The formatted URL string. | |
* @example | |
* var azure = require('azure-storage'); | |
* var blobService = azure.createBlobService(); | |
* var sharedAccessPolicy = { | |
* AccessPolicy: { | |
* Permissions: azure.BlobUtilities.SharedAccessPermissions.READ, | |
* Start: startDate, | |
* Expiry: expiryDate | |
* }, | |
* }; | |
* | |
* var sasToken = blobService.generateSharedAccessSignature(containerName, blobName, sharedAccessPolicy); | |
* var sasUrl = blobService.getUrl(containerName, blobName, sasToken); | |
*/ | |
BlobService.prototype.getUrl = function (container, blob, sasToken, primary, snapshotId) { | |
validate.validateArgs('getUrl', function (v) { | |
v.string(container, 'container'); | |
v.containerNameIsValid(container); | |
}); | |
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); | |
if (snapshotId) { | |
query[QueryStringConstants.SNAPSHOT] = snapshotId; | |
} | |
var fullPath = url.format({ pathname: this._getPath(createResourceName(container, blob)), query: query }); | |
return url.resolve(host, fullPath); | |
}; | |
// Page blob methods | |
/** | |
* Creates a page blob of the specified length. If the blob already exists on the service, it will be overwritten. | |
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {int} length The length of the page blob in bytes. | |
* @param {object} [options] The request options. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {string} [options.leaseId] The target blob lease identifier. | |
* @param {string} [options.blobTier] For page blobs on premium accounts only. Set the tier of the target blob. Refer to BlobUtilities.BlobTier.PremiumPageBlobTier. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The MD5 hash of the blob content. | |
* @param {string} [options.sequenceNumber] The blob's sequence number. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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. | |
*/ | |
BlobService.prototype.createPageBlob = function (container, blob, length, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('createPageBlob', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.value(length, 'length'); | |
v.callback(callback); | |
}); | |
if (length && length % BlobConstants.PAGE_SIZE !== 0) { | |
throw new RangeError(SR.INVALID_PAGE_BLOB_LENGTH); | |
} | |
var options = extend(true, {}, userOptions); | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.put(resourceName) | |
.withHeader(HeaderConstants.BLOB_TYPE, BlobConstants.BlobTypes.PAGE) | |
.withHeader(HeaderConstants.BLOB_CONTENT_LENGTH, length) | |
.withHeader(HeaderConstants.CONTENT_LENGTH, 0) | |
.withHeader(HeaderConstants.ACCESS_TIER, options.blobTier) | |
.withHeader(HeaderConstants.LEASE_ID, options.leaseId); | |
BlobResult.setHeadersFromBlob(webResource, options); | |
var processResponseCallback = function (responseObject, next) { | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Uploads a page blob from a stream. If the blob already exists on the service, it will be overwritten. | |
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param (Stream) stream Stream to the data to store. | |
* @param {int} streamLength The length of the stream to upload. | |
* @param {object} [options] The request options. | |
* @param {SpeedSummary} [options.speedSummary] The download tracker objects; | |
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.transactionalContentMD5] An MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. | |
* The default value is false for page blobs. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {string} [options.blobTier] For page blobs on premium accounts only. Set the tier of the target blob. Refer to BlobUtilities.BlobTier.PremiumPageBlobTier. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the blob information. | |
* `response` will contain information related to this operation. | |
* @return {SpeedSummary} | |
*/ | |
BlobService.prototype.createPageBlobFromStream = function (container, blob, stream, streamLength, optionsOrCallback, callback) { | |
return this._createBlobFromStream(container, blob, BlobConstants.BlobTypes.PAGE, stream, streamLength, optionsOrCallback, callback); | |
}; | |
/** | |
* Provides a stream to write to a page blob. Assumes that the blob exists. | |
* If it does not, please create the blob using createPageBlob before calling this method or use createWriteStreamNewPageBlob. | |
* Please note the `Stream` returned by this API should be used with piping. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} [options] The request options. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.transactionalContentMD5] The MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. | |
* The default value is false for page blobs and true for block blobs. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the blob information. | |
* `response` will contain information related to this operation. | |
* @return {Writable} A Node.js Writable stream. | |
* @example | |
* var azure = require('azure-storage'); | |
* var blobService = azure.createBlobService(); | |
* blobService.createPageBlob(containerName, blobName, 1024, function (err) { | |
* // Pipe file to a blob | |
* var stream = fs.createReadStream(fileNameTarget).pipe(blobService.createWriteStreamToExistingPageBlob(containerName, blobName)); | |
* }); | |
*/ | |
BlobService.prototype.createWriteStreamToExistingPageBlob = function (container, blob, optionsOrCallback, callback) { | |
return this._createWriteStreamToBlob(container, blob, BlobConstants.BlobTypes.PAGE, 0, false, optionsOrCallback, callback); | |
}; | |
/** | |
* Provides a stream to write to a page blob. Creates the blob before writing data. If the blob already exists on the service, it will be overwritten. | |
* Please note the `Stream` returned by this API should be used with piping. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {string} length The blob length. | |
* @param {object} [options] The request options. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.transactionalContentMD5] The MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. | |
* The default value is false for page blobs and true for block blobs. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {string} [options.blobTier] For page blobs on premium accounts only. Set the tier of the target blob. Refer to BlobUtilities.BlobTier.PremiumPageBlobTier. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the blob information. | |
* `response` will contain information related to this operation. | |
* @return {Writable} A Node.js Writable stream. | |
* @example | |
* var azure = require('azure-storage'); | |
* var blobService = azure.createBlobService(); | |
* blobService.createPageBlob(containerName, blobName, 1024, function (err) { | |
* // Pipe file to a blob | |
* var stream = fs.createReadStream(fileNameTarget).pipe(blobService.createWriteStreamToNewPageBlob(containerName, blobName)); | |
* }); | |
*/ | |
BlobService.prototype.createWriteStreamToNewPageBlob = function (container, blob, length, optionsOrCallback, callback) { | |
return this._createWriteStreamToBlob(container, blob, BlobConstants.BlobTypes.PAGE, length, true, optionsOrCallback, callback); | |
}; | |
/** | |
* Updates a page blob from a stream. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {Readable} readStream The Node.js Readable stream. | |
* @param {int} rangeStart The range start. | |
* @param {int} rangeEnd The range end. | |
* @param {object} [options] The request options. | |
* @param {string} [options.leaseId] The target blob lease identifier. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {string} [options.transactionalContentMD5] An optional hash value used to ensure transactional integrity for the page. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the page information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.createPagesFromStream = function (container, blob, readStream, rangeStart, rangeEnd, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('createPagesFromStream', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
if ((rangeEnd - rangeStart) + 1 > BlobConstants.MAX_UPDATE_PAGE_SIZE) { | |
throw new RangeError(SR.INVALID_PAGE_RANGE_FOR_UPDATE); | |
} | |
var self = this; | |
if (azureutil.objectIsNull(options.transactionalContentMD5) && options.useTransactionalMD5) { | |
azureutil.calculateMD5(readStream, BlobConstants.MAX_UPDATE_PAGE_SIZE, options, function (internalBuff, contentMD5) { | |
options.transactionalContentMD5 = contentMD5; | |
self._createPages(container, blob, internalBuff, null /* stream */, rangeStart, rangeEnd, options, callback); | |
}); | |
} else { | |
self._createPages(container, blob, null /* text */, readStream, rangeStart, rangeEnd, options, callback); | |
} | |
}; | |
/** | |
* Lists page ranges. Lists all of the page ranges by default, or only the page ranges over a specific range of bytes if rangeStart and rangeEnd are specified. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} [options] The request options. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @param {int} [options.rangeStart] The range start. | |
* @param {int} [options.rangeEnd] The range end. | |
* @param {string} [options.snapshotId] The snapshot identifier. | |
* @param {string} [options.leaseId] The target blob lease identifier. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 page ranges information, see `[Range]{@link Range}` for detailed information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.listPageRanges = function (container, blob, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('listPageRanges', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.get(resourceName) | |
.withQueryOption(QueryStringConstants.COMP, 'pagelist') | |
.withQueryOption(QueryStringConstants.SNAPSHOT, options.snapshotId); | |
if (options.rangeStart && options.rangeStart % BlobConstants.PAGE_SIZE !== 0) { | |
throw new RangeError(SR.INVALID_PAGE_START_OFFSET); | |
} | |
if (options.rangeEnd && (options.rangeEnd + 1) % BlobConstants.PAGE_SIZE !== 0) { | |
throw new RangeError(SR.INVALID_PAGE_END_OFFSET); | |
} | |
BlobResult.setHeadersFromBlob(webResource, options); | |
options.requestLocationMode = RequestLocationMode.PRIMARY_OR_SECONDARY; | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.pageRanges = null; | |
if (!responseObject.error) { | |
responseObject.pageRanges = []; | |
var pageRanges = []; | |
if (responseObject.response.body.PageList.PageRange) { | |
pageRanges = responseObject.response.body.PageList.PageRange; | |
if (!_.isArray(pageRanges)) { | |
pageRanges = [pageRanges]; | |
} | |
} | |
pageRanges.forEach(function (pageRange) { | |
var range = { | |
start: parseInt(pageRange.Start, 10), | |
end: parseInt(pageRange.End, 10) | |
}; | |
responseObject.pageRanges.push(range); | |
}); | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.pageRanges, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Gets page ranges that have been updated or cleared since the snapshot specified by `previousSnapshotTime` was taken. Gets all of the page ranges by default, or only the page ranges over a specific range of bytes if rangeStart and rangeEnd are specified. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {string} previousSnapshotTime The previous snapshot time for comparison. Must be prior to `options.snapshotId` if it's provided. | |
* @param {object} [options] The request options. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @param {int} [options.rangeStart] The range start. | |
* @param {int} [options.rangeEnd] The range end. | |
* @param {string} [options.snapshotId] The snapshot identifier. | |
* @param {string} [options.leaseId] The target blob lease identifier. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 page ranges diff information, see `[RangeDiff]{@link RangeDiff}` for detailed information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.getPageRangesDiff = function (container, blob, previousSnapshotTime, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('getPageRangesDiff', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.get(resourceName) | |
.withQueryOption(QueryStringConstants.COMP, 'pagelist') | |
.withQueryOption(QueryStringConstants.SNAPSHOT, options.snapshotId) | |
.withQueryOption(QueryStringConstants.PREV_SNAPSHOT, previousSnapshotTime); | |
if (options.rangeStart && options.rangeStart % BlobConstants.PAGE_SIZE !== 0) { | |
throw new RangeError(SR.INVALID_PAGE_START_OFFSET); | |
} | |
if (options.rangeEnd && (options.rangeEnd + 1) % BlobConstants.PAGE_SIZE !== 0) { | |
throw new RangeError(SR.INVALID_PAGE_END_OFFSET); | |
} | |
if (options.rangeEnd && (options.rangeEnd + 1) % BlobConstants.PAGE_SIZE !== 0) { | |
throw new RangeError(SR.INVALID_PAGE_END_OFFSET); | |
} | |
BlobResult.setHeadersFromBlob(webResource, options); | |
options.requestLocationMode = RequestLocationMode.PRIMARY_OR_SECONDARY; | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.pageRangesDiff = null; | |
if (!responseObject.error) { | |
responseObject.pageRangesDiff = []; | |
if (responseObject.response.body.PageList.PageRange) { | |
var updatedPageRanges = responseObject.response.body.PageList.PageRange; | |
if (!_.isArray(updatedPageRanges)) { | |
updatedPageRanges = [updatedPageRanges]; | |
} | |
updatedPageRanges.forEach(function (pageRange) { | |
var range = { | |
start: parseInt(pageRange.Start, 10), | |
end: parseInt(pageRange.End, 10), | |
isCleared: false | |
}; | |
responseObject.pageRangesDiff.push(range); | |
}); | |
} | |
if (responseObject.response.body.PageList.ClearRange) { | |
var clearedPageRanges = responseObject.response.body.PageList.ClearRange; | |
if (!_.isArray(clearedPageRanges)) { | |
clearedPageRanges = [clearedPageRanges]; | |
} | |
clearedPageRanges.forEach(function (pageRange) { | |
var range = { | |
start: parseInt(pageRange.Start, 10), | |
end: parseInt(pageRange.End, 10), | |
isCleared: true | |
}; | |
responseObject.pageRangesDiff.push(range); | |
}); | |
} | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.pageRangesDiff, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Clears a range of pages. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {int} rangeStart The range start. | |
* @param {int} rangeEnd The range end. | |
* @param {object} [options] The request options. | |
* @param {string} [options.leaseId] The target blob lease identifier. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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. | |
*/ | |
BlobService.prototype.clearPageRange = function (container, blob, rangeStart, rangeEnd, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('clearPageRange', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var request = this._updatePageBlobPagesImpl(container, blob, rangeStart, rangeEnd, BlobConstants.PageWriteOptions.CLEAR, options); | |
var self = this; | |
var processResponseCallback = function (responseObject, next) { | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
self.performRequest(request, null, options, processResponseCallback); | |
}; | |
/** | |
* Resizes a page blob. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {String} size The size of the page blob, in bytes. | |
* @param {object} [options] The request options. | |
* @param {string} [options.leaseId] The blob lease identifier. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the page information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.resizePageBlob = function (container, blob, size, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('resizePageBlob', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.put(resourceName) | |
.withQueryOption(QueryStringConstants.COMP, 'properties') | |
.withHeader(HeaderConstants.LEASE_ID, options.leaseId); | |
if (size && size % BlobConstants.PAGE_SIZE !== 0) { | |
throw new RangeError(SR.INVALID_PAGE_BLOB_LENGTH); | |
} | |
webResource.withHeader(HeaderConstants.BLOB_CONTENT_LENGTH, size); | |
this._setBlobPropertiesHelper({ | |
webResource: webResource, | |
options: options, | |
container: container, | |
blob: blob, | |
callback: callback | |
}); | |
}; | |
/** | |
* Sets the page blob's sequence number. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {SequenceNumberAction} sequenceNumberAction A value indicating the operation to perform on the sequence number. | |
* The allowed values are defined in azure.BlobUtilities.SequenceNumberAction. | |
* @param {string} sequenceNumber The sequence number. The value of the sequence number must be between 0 and 2^63 - 1. | |
* Set this parameter to null if this operation is an increment action. | |
* @param {object} [options] The request options. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the page information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.setPageBlobSequenceNumber = function (container, blob, sequenceNumberAction, sequenceNumber, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('setPageBlobSequenceNumber', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
if (sequenceNumberAction === BlobUtilities.SequenceNumberAction.INCREMENT) { | |
if (!azureutil.objectIsNull(sequenceNumber)) { | |
throw new ArgumentError('sequenceNumber', SR.BLOB_INVALID_SEQUENCE_NUMBER); | |
} | |
} else { | |
if (azureutil.objectIsNull(sequenceNumber)) { | |
throw new ArgumentNullError('sequenceNumber', util.format(SR.ARGUMENT_NULL_OR_EMPTY, 'sequenceNumber')); | |
} | |
} | |
var options = extend(true, {}, userOptions); | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.put(resourceName) | |
.withQueryOption(QueryStringConstants.COMP, 'properties') | |
.withHeader(HeaderConstants.SEQUENCE_NUMBER_ACTION, sequenceNumberAction); | |
if (sequenceNumberAction !== BlobUtilities.SequenceNumberAction.INCREMENT) { | |
webResource.withHeader(HeaderConstants.SEQUENCE_NUMBER, sequenceNumber); | |
} | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.blobResult = null; | |
if (!responseObject.error) { | |
responseObject.blobResult = new BlobResult(container, blob); | |
responseObject.blobResult.getPropertiesFromHeaders(responseObject.response.headers); | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.blobResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
// Block blob methods | |
/** | |
* Uploads a block blob from a stream. If the blob already exists on the service, it will be overwritten. | |
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param (Stream) stream Stream to the data to store. | |
* @param {int} streamLength The length of the stream to upload. | |
* @param {object} [options] The request options. | |
* @param {SpeedSummary} [options.speedSummary] The download tracker objects. | |
* @param {int} [options.blockSize] The size of each block. Maximum is 100MB. | |
* @param {string} [options.blockIdPrefix] The prefix to be used to generate the block id. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.transactionalContentMD5] The MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. The default value is true for block blobs. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the blob information. | |
* `response` will contain information related to this operation. | |
* @return {SpeedSummary} | |
*/ | |
BlobService.prototype.createBlockBlobFromStream = function (container, blob, stream, streamLength, optionsOrCallback, callback) { | |
return this._createBlobFromStream(container, blob, BlobConstants.BlobTypes.BLOCK, stream, streamLength, optionsOrCallback, callback); | |
}; | |
/** | |
* Uploads a block blob from a text string. If the blob already exists on the service, it will be overwritten. | |
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {string|object} text The blob text, as a string or in a Buffer. | |
* @param {object} [options] The request options. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.transactionalContentMD5] The MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. The default value is true for block blobs. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the blob information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.createBlockBlobFromText = function (container, blob, text, optionsOrCallback, callback) { | |
return this._createBlobFromText(container, blob, BlobConstants.BlobTypes.BLOCK, text, optionsOrCallback, callback); | |
}; | |
/** | |
* Provides a stream to write to a block blob. If the blob already exists on the service, it will be overwritten. | |
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object. | |
* Please note the `Stream` returned by this API should be used with piping. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} [options] The request options. | |
* @param {int} [options.blockSize] The size of each block. Maximum is 100MB. | |
* @param {string} [options.blockIdPrefix] The prefix to be used to generate the block id. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.transactionalContentMD5] The MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. | |
* The default value is false for page blobs and true for block blobs. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the blob information. | |
* `response` will contain information related to this operation. | |
* @return {Writable} A Node.js Writable stream. | |
* @example | |
* var azure = require('azure-storage'); | |
* var blobService = azure.createBlobService(); | |
* var stream = fs.createReadStream(fileNameTarget).pipe(blobService.createWriteStreamToBlockBlob(containerName, blobName, { blockIdPrefix: 'block' })); | |
*/ | |
BlobService.prototype.createWriteStreamToBlockBlob = function (container, blob, optionsOrCallback, callback) { | |
return this._createWriteStreamToBlob(container, blob, BlobConstants.BlobTypes.BLOCK, 0, false, optionsOrCallback, callback); | |
}; | |
/** | |
* Creates a new block to be committed as part of a blob. | |
* | |
* @this {BlobService} | |
* @param {string} blockId The block identifier. | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {Readable} readStream The Node.js Readable stream. | |
* @param {int} streamLength The stream length. | |
* @param {object} [options] The request options. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {string} [options.leaseId] The target blob lease identifier. | |
* @param {string} [options.transactionalContentMD5] An MD5 hash of the block content. This hash is used to verify the integrity of the block during transport. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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. | |
*/ | |
BlobService.prototype.createBlockFromStream = function (blockId, container, blob, readStream, streamLength, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('createBlockFromStream', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.exists(readStream, 'readStream'); | |
v.value(streamLength, 'streamLength'); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
if (streamLength > BlobConstants.MAX_BLOCK_BLOB_BLOCK_SIZE) { | |
throw new RangeError(SR.INVALID_STREAM_LENGTH); | |
} else { | |
this._createBlock(blockId, container, blob, null, readStream, streamLength, options, callback); | |
} | |
}; | |
/** | |
* Creates a new block to be committed as part of a blob. | |
* | |
* @this {BlobService} | |
* @param {string} blockId The block identifier. | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {string|buffer} content The block content. | |
* @param {object} [options] The request options. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {string} [options.leaseId] The target blob lease identifier. | |
* @param {string} [options.transactionalContentMD5] An MD5 hash of the block content. This hash is used to verify the integrity of the block during transport. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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. | |
*/ | |
BlobService.prototype.createBlockFromText = function (blockId, container, blob, content, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('createBlockFromText', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var contentLength = (Buffer.isBuffer(content) ? content.length : Buffer.byteLength(content)); | |
if (contentLength > BlobConstants.MAX_BLOCK_BLOB_BLOCK_SIZE) { | |
throw new RangeError(SR.INVALID_TEXT_LENGTH); | |
} else { | |
this._createBlock(blockId, container, blob, content, null, contentLength, options, callback); | |
} | |
}; | |
/** | |
* Creates a new block to be committed as part of a block blob. | |
* @ignore | |
* | |
* @this {BlobService} | |
* @param {string} blockId The block identifier. | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {string|buffer} content The block content. | |
* @param (Stream) stream The stream to the data to store. | |
* @param {int} length The length of the stream or text to upload. | |
* @param {object} [options] The request options. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {string} [options.leaseId] The target blob lease identifier. | |
* @param {string} [options.transactionalContentMD5] An MD5 hash of the block content. This hash is used to verify the integrity of the block during transport. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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. | |
*/ | |
BlobService.prototype._createBlock = function (blockId, container, blob, content, stream, length, options, callback) { | |
var resourceName = createResourceName(container, blob); | |
var self = this; | |
var startCreateBlock = function () { | |
var webResource = WebResource.put(resourceName) | |
.withQueryOption(QueryStringConstants.COMP, 'block') | |
.withQueryOption(QueryStringConstants.BLOCK_ID, new Buffer(blockId).toString('base64')) | |
.withHeader(HeaderConstants.CONTENT_LENGTH, length); | |
BlobResult.setHeadersFromBlob(webResource, options); | |
var processResponseCallback = function (responseObject, next) { | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
if (!azureutil.objectIsNull(content)) { | |
self.performRequest(webResource, content, options, processResponseCallback); | |
} else { | |
self.performRequestOutputStream(webResource, stream, options, processResponseCallback); | |
} | |
}; | |
if (azureutil.objectIsNull(options.transactionalContentMD5) && options.useTransactionalMD5) { | |
if (!azureutil.objectIsNull(content)) { | |
options.transactionalContentMD5 = azureutil.getContentMd5(content); | |
startCreateBlock(); | |
} else { | |
azureutil.calculateMD5(stream, length, options, function (internalBuff, contentMD5) { | |
options.transactionalContentMD5 = contentMD5; | |
content = internalBuff; | |
length = internalBuff.length; | |
startCreateBlock(); | |
}); | |
} | |
} else { | |
startCreateBlock(); | |
} | |
}; | |
/** | |
* Writes a blob by specifying the list of block IDs that make up the blob. | |
* In order to be written as part of a blob, a block must have been successfully written to the server in a prior | |
* createBlock operation. | |
* Note: If no valid list is specified in the blockList parameter, blob would be updated with empty content, | |
* i.e. existing blocks in the blob will be removed, this behavior is kept for backward compatibility consideration. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} blockList The wrapper for block ID list contains block IDs that make up the blob. | |
* Three kinds of list are provided, please choose one to use according to requirement. | |
* For more background knowledge, please refer to https://docs.microsoft.com/en-us/rest/api/storageservices/put-block-list | |
* @param {string[]} [blockList.LatestBlocks] The list contains block IDs that make up the blob sequentially. | |
* All the block IDs in this list will be specified within Latest element. | |
* Choose this list to contain block IDs indicates that the Blob service should first search | |
* the uncommitted block list, and then the committed block list for the named block. | |
* @param {string[]} [blockList.CommittedBlocks] The list contains block IDs that make up the blob sequentially. | |
* All the block IDs in this list will be specified within Committed element. | |
* Choose this list to contain block IDs indicates that the Blob service should only search | |
* the committed block list for the named block. | |
* @param {string[]} [blockList.UncommittedBlocks] The list contains block IDs that make up the blob sequentially. | |
* All the block IDs in this list will be specified within Uncommitted element. | |
* Choose this list to contain block IDs indicates that the Blob service should only search | |
* the uncommitted block list for the named block. | |
* @param {object} [options] The request options. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {string} [options.leaseId] The target blob lease identifier. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the blob information. | |
* `response` will contain information related to this operation. | |
* @example | |
* var azure = require('azure-storage'); | |
* var blobService = azure.createBlobService(); | |
* blobService.createBlockFromText("sampleBlockName", containerName, blobName, "sampleBlockContent", function(error) { | |
* assert.equal(error, null); | |
* // In this example, LatestBlocks is used, we hope the Blob service first search | |
* // the uncommitted block list, and then the committed block list for the named block "sampleBlockName", | |
* // and thus make sure the block is with latest content. | |
* blobService.commitBlocks(containerName, blobName, { LatestBlocks: ["sampleBlockName"] }, function(error) { | |
* assert.equal(error, null); | |
* }); | |
* }); | |
* | |
*/ | |
BlobService.prototype.commitBlocks = function (container, blob, blockList, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('commitBlocks', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.object(blockList, 'blockList'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var blockListXml = BlockListResult.serialize(blockList); | |
var resourceName = createResourceName(container, blob); | |
var options = extend(true, {}, userOptions); | |
var webResource = WebResource.put(resourceName) | |
.withQueryOption(QueryStringConstants.COMP, 'blocklist') | |
.withHeader(HeaderConstants.CONTENT_LENGTH, Buffer.byteLength(blockListXml)) | |
.withBody(blockListXml); | |
BlobResult.setPropertiesFromBlob(webResource, options); | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.blobResult = new BlobResult(container, blob); | |
responseObject.blobResult.list = null; | |
if (!responseObject.error) { | |
responseObject.blobResult.getPropertiesFromHeaders(responseObject.response.headers); | |
responseObject.blobResult.list = blockList; | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.blobResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, webResource.body, options, processResponseCallback); | |
}; | |
/** | |
* Retrieves the list of blocks that have been uploaded as part of a block blob. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {BlockListFilter} blocklisttype The type of block list to retrieve. | |
* @param {object} [options] The request options. | |
* @param {string} [options.snapshotId] The source blob snapshot identifier. | |
* @param {string} [options.leaseId] The target blob lease identifier. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 blocklist information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.listBlocks = function (container, blob, blocklisttype, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('listBlocks', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var resourceName = createResourceName(container, blob); | |
var options = extend(true, {}, userOptions); | |
var webResource = WebResource.get(resourceName) | |
.withQueryOption(QueryStringConstants.COMP, 'blocklist') | |
.withQueryOption(QueryStringConstants.BLOCK_LIST_TYPE, blocklisttype) | |
.withQueryOption(QueryStringConstants.SNAPSHOT, options.snapshotId); | |
options.requestLocationMode = RequestLocationMode.PRIMARY_OR_SECONDARY; | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.blockListResult = null; | |
if (!responseObject.error) { | |
responseObject.blockListResult = BlockListResult.parse(responseObject.response.body.BlockList); | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.blockListResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Generate a random block id prefix | |
*/ | |
BlobService.prototype.generateBlockIdPrefix = function () { | |
var prefix = Math.floor(Math.random() * 0x100000000).toString(16); | |
return azureutil.zeroPaddingString(prefix, 8); | |
}; | |
/** | |
* Get a block id according to prefix and block number | |
*/ | |
BlobService.prototype.getBlockId = function (prefix, number) { | |
return prefix + '-' + azureutil.zeroPaddingString(number, 6); | |
}; | |
// Append blob methods | |
/** | |
* Creates an empty append blob. If the blob already exists on the service, it will be overwritten. | |
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} [options] The request options. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {string} [options.leaseId] The target blob lease identifier. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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. | |
*/ | |
BlobService.prototype.createOrReplaceAppendBlob = function (container, blob, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('createOrReplaceAppendBlob', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.put(resourceName) | |
.withHeader(HeaderConstants.BLOB_TYPE, BlobConstants.BlobTypes.APPEND) | |
.withHeader(HeaderConstants.LEASE_ID, options.leaseId) | |
.withHeader(HeaderConstants.CONTENT_LENGTH, 0); | |
BlobResult.setHeadersFromBlob(webResource, options); | |
var processResponseCallback = function (responseObject, next) { | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Uploads an append blob from a stream. If the blob already exists on the service, it will be overwritten. | |
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object. | |
* This API should be used strictly in a single writer scenario because the API internally uses the append-offset conditional header to avoid duplicate blocks. | |
* If you are guaranteed to have a single writer scenario, please look at options.absorbConditionalErrorsOnRetry and see if setting this flag to true is acceptable for you. | |
* If you want to append data to an already existing blob, please look at appendFromStream. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param (Stream) stream Stream to the data to store. | |
* @param {int} streamLength The length of the stream to upload. | |
* @param {object} [options] The request options. | |
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. | |
* @param {SpeedSummary} [options.speedSummary] The download tracker objects. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. The default value is true for block blobs. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the blob information. | |
* `response` will contain information related to this operation. | |
* @return {SpeedSummary} | |
*/ | |
BlobService.prototype.createAppendBlobFromStream = function (container, blob, stream, streamLength, optionsOrCallback, callback) { | |
return this._createBlobFromStream(container, blob, BlobConstants.BlobTypes.APPEND, stream, streamLength, optionsOrCallback, callback); | |
}; | |
/** | |
* Uploads an append blob from a text string. If the blob already exists on the service, it will be overwritten. | |
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object. | |
* This API should be used strictly in a single writer scenario because the API internally uses the append-offset conditional header to avoid duplicate blocks. | |
* If you are guaranteed to have a single writer scenario, please look at options.absorbConditionalErrorsOnRetry and see if setting this flag to true is acceptable for you. | |
* If you want to append data to an already existing blob, please look at appendFromText. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {string|object} text The blob text, as a string or in a Buffer. | |
* @param {object} [options] The request options. | |
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. The default value is true for block blobs. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the blob information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.createAppendBlobFromText = function (container, blob, text, optionsOrCallback, callback) { | |
return this._createBlobFromText(container, blob, BlobConstants.BlobTypes.APPEND, text, optionsOrCallback, callback); | |
}; | |
/** | |
* Provides a stream to write to a new append blob. If the blob already exists on the service, it will be overwritten. | |
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object. | |
* This API should be used strictly in a single writer scenario because the API internally uses the append-offset conditional header to avoid duplicate blocks. | |
* If you are guaranteed to have a single writer scenario, please look at options.absorbConditionalErrorsOnRetry and see if setting this flag to true is acceptable for you. | |
* Please note the `Stream` returned by this API should be used with piping. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} [options] The request options. | |
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. | |
* The default value is false for page blobs and true for block blobs. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 The callback function. | |
* @return {Writable} A Node.js Writable stream. | |
* @example | |
* var azure = require('azure-storage'); | |
* var blobService = azure.createBlobService(); | |
* var stream = fs.createReadStream(fileNameTarget).pipe(blobService.createWriteStreamToAppendBlob(containerName, blobName)); | |
*/ | |
BlobService.prototype.createWriteStreamToNewAppendBlob = function (container, blob, optionsOrCallback, callback) { | |
return this._createWriteStreamToBlob(container, blob, BlobConstants.BlobTypes.APPEND, 0, true, optionsOrCallback, callback); | |
}; | |
/** | |
* Provides a stream to write to an existing append blob. Assumes that the blob exists. | |
* If it does not, please create the blob using createAppendBlob before calling this method or use createWriteStreamToNewAppendBlob. | |
* This API should be used strictly in a single writer scenario because the API internally uses the append-offset conditional header to avoid duplicate blocks. | |
* If you are guaranteed to have a single writer scenario, please look at options.absorbConditionalErrorsOnRetry and see if setting this flag to true is acceptable for you. | |
* Please note the `Stream` returned by this API should be used with piping. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {object} [options] The request options. | |
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. | |
* The default value is false for page blobs and true for block blobs. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 The callback function. | |
* @return {Writable} A Node.js Writable stream. | |
* @example | |
* var azure = require('azure-storage'); | |
* var blobService = azure.createBlobService(); | |
* var stream = fs.createReadStream(fileNameTarget).pipe(blobService.createWriteStreamToAppendBlob(containerName, blobName)); | |
*/ | |
BlobService.prototype.createWriteStreamToExistingAppendBlob = function (container, blob, optionsOrCallback, callback) { | |
return this._createWriteStreamToBlob(container, blob, BlobConstants.BlobTypes.APPEND, 0, false, optionsOrCallback, callback); | |
}; | |
/** | |
* Appends to an append blob from a stream. Assumes the blob already exists on the service. | |
* This API should be used strictly in a single writer scenario because the API internally uses the append-offset conditional header to avoid duplicate blocks. | |
* If you are guaranteed to have a single writer scenario, please look at options.absorbConditionalErrorsOnRetry and see if setting this flag to true is acceptable for you. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param (Stream) stream Stream to the data to store. | |
* @param {int} streamLength The length of the stream to upload. | |
* @param {object} [options] The request options. | |
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. | |
* @param {SpeedSummary} [options.speedSummary] The download tracker objects. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the blob information. | |
* `response` will contain information related to this operation. | |
* @return {SpeedSummary} | |
*/ | |
BlobService.prototype.appendFromStream = function (container, blob, stream, streamLength, optionsOrCallback, callback) { | |
var options; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { options = o; callback = c; }); | |
validate.validateArgs('appendFromStream', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.exists(stream, 'stream'); | |
v.value(streamLength, 'streamLength'); | |
v.callback(callback); | |
}); | |
return this._uploadBlobFromStream(false, container, blob, BlobConstants.BlobTypes.APPEND, stream, streamLength, options, callback); | |
}; | |
/** | |
* Appends to an append blob from a text string. Assumes the blob already exists on the service. | |
* This API should be used strictly in a single writer scenario because the API internally uses the append-offset conditional header to avoid duplicate blocks. | |
* If you are guaranteed to have a single writer scenario, please look at options.absorbConditionalErrorsOnRetry and see if setting this flag to true is acceptable for you. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {string|object} text The blob text, as a string or in a Buffer. | |
* @param {object} [options] The request options. | |
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. The default value is true for block blobs. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the blob information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.appendFromText = function (container, blob, text, optionsOrCallback, callback) { | |
return this._uploadBlobFromText(false, container, blob, BlobConstants.BlobTypes.APPEND, text, optionsOrCallback, callback); | |
}; | |
/** | |
* Creates a new block from a read stream to be appended to an append blob. | |
* This API should be used strictly in a single writer scenario because the API internally uses the append-offset conditional header to avoid duplicate blocks. | |
* If you are guaranteed to have a single writer scenario, please look at options.absorbConditionalErrorsOnRetry and see if setting this flag to true is acceptable for you. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {Readable} readStream The Node.js Readable stream. | |
* @param {int} streamLength The stream length. | |
* @param {object} [options] The request options. | |
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. | |
* @param {int} [options.maxBlobSize] The max length in bytes allowed for the append blob to grow to. | |
* @param {int} [options.appendPosition] The number indicating the byte offset to check for. The append will succeed only if the end position of the blob is equal to this number. | |
* @param {string} [options.leaseId] The target blob lease identifier. | |
* @param {string} [options.transactionalContentMD5] An MD5 hash of the block content. This hash is used to verify the integrity of the block during transport. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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]{@link BlobResult}` will contain | |
* the blob information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype.appendBlockFromStream = function (container, blob, readStream, streamLength, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('appendBlockFromStream', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.exists(readStream, 'readStream'); | |
v.value(streamLength, 'streamLength'); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
if (streamLength > BlobConstants.MAX_APPEND_BLOB_BLOCK_SIZE) { | |
throw new RangeError(SR.INVALID_STREAM_LENGTH); | |
} else { | |
this._appendBlock(container, blob, null, readStream, streamLength, options, callback); | |
} | |
}; | |
/** | |
* Creates a new block from a text to be appended to an append blob. | |
* This API should be used strictly in a single writer scenario because the API internally uses the append-offset conditional header to avoid duplicate blocks. | |
* If you are guaranteed to have a single writer scenario, please look at options.absorbConditionalErrorsOnRetry and see if setting this flag to true is acceptable for you. | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {string|object} content The block text, as a string or in a Buffer. | |
* @param {object} [options] The request options. | |
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. | |
* @param {int} [options.maxBlobSize] The max length in bytes allowed for the append blob to grow to. | |
* @param {int} [options.appendPosition] The number indicating the byte offset to check for. The append will succeed only if the end position of the blob is equal to this number. | |
* @param {string} [options.leaseId] The target blob lease identifier. | |
* @param {string} [options.transactionalContentMD5] An MD5 hash of the block content. This hash is used to verify the integrity of the block during transport. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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. | |
*/ | |
BlobService.prototype.appendBlockFromText = function (container, blob, content, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('appendBlockFromText', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var contentLength = (Buffer.isBuffer(content) ? content.length : Buffer.byteLength(content)); | |
if (contentLength > BlobConstants.MAX_APPEND_BLOB_BLOCK_SIZE) { | |
throw new RangeError(SR.INVALID_TEXT_LENGTH); | |
} else { | |
this._appendBlock(container, blob, content, null, contentLength, options, callback); | |
} | |
}; | |
// Private methods | |
/** | |
* Creates a new blob from a stream. If the blob already exists on the service, it will be overwritten. | |
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object. | |
* | |
* @ignore | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {BlobType} blobType The blob type. | |
* @param (Stream) stream Stream to the data to store. | |
* @param {int} streamLength The length of the stream to upload. | |
* @param {object} [options] The request options. | |
* @param {SpeedSummary} [options.speedSummary] The upload tracker objects. | |
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. (For append blob only) | |
* @param {string} [options.blockIdPrefix] The prefix to be used to generate the block id. (For block blob only) | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.transactionalContentMD5] The MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. The default value is true for block blobs. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {string} [options.blobTier] For page blobs on premium accounts only. Set the tier of the target blob. Refer to BlobUtilities.BlobTier.PremiumPageBlobTier. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 The callback function. | |
* @return {SpeedSummary} | |
*/ | |
BlobService.prototype._createBlobFromStream = function (container, blob, blobType, stream, streamLength, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('_createBlobFromStream', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.blobTypeIsValid(blobType); | |
v.exists(stream, 'stream'); | |
v.value(streamLength, 'streamLength'); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var self = this; | |
var creationCallback = function (createError, createBlob, createResponse) { | |
if (createError) { | |
callback(createError, createBlob, createResponse); | |
} else { | |
self._uploadBlobFromStream(true, container, blob, blobType, stream, streamLength, options, callback); | |
} | |
}; | |
this._createBlob(container, blob, blobType, streamLength, options, creationCallback); | |
return options.speedSummary; | |
}; | |
/** | |
* Uploads a block blob or an append blob from a text string. If the blob already exists on the service, it will be overwritten. | |
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object. | |
* | |
* @ignore | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {BlobType} blobType The blob type. | |
* @param {string|buffer} content The blob text, as a string or in a Buffer. | |
* @param {object} [options] The request options. | |
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. (For append blob only) | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.transactionalContentMD5] The MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. The default value is true for block blobs. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 | |
* information about the blob. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype._createBlobFromText = function (container, blob, blobType, content, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('_createBlobFromText', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.blobTypeIsValid(blobType); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var self = this; | |
var creationCallback = function (createError, createBlob, createResponse) { | |
if (createError) { | |
callback(createError, createBlob, createResponse); | |
} else { | |
self._uploadBlobFromText(true, container, blob, blobType, content, options, callback); | |
} | |
}; | |
var contentLength = azureutil.objectIsNull(content) ? 0 : ((Buffer.isBuffer(content) ? content.length : Buffer.byteLength(content))); | |
this._createBlob(container, blob, blobType, contentLength, options, creationCallback); | |
return options.speedSummary; | |
}; | |
/** | |
* Provides a stream to write to a block blob or an append blob. | |
* | |
* @ignore | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {BlobType} blobType The blob type. | |
* @param {int} length The blob length. | |
* @param {bool} createNewBlob Specifies whether create a new blob. | |
* @param {object} [options] The request options. | |
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. (For append blob only) | |
* @param {string} [options.blockSize] The size of each block. Maximum is 100MB. (For block blob only) | |
* @param {string} [options.blockIdPrefix] The prefix to be used to generate the block id. (For block blob only) | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.transactionalContentMD5] The MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. | |
* The default value is false for page blobs and true for block blobs. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {string} [options.blobTier] For page blobs on premium accounts only. Set the tier of the target blob. Refer to BlobUtilities.BlobTier.PremiumPageBlobTier. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 The callback function. | |
* @return {Writable} A Node.js Writable stream. | |
*/ | |
BlobService.prototype._createWriteStreamToBlob = function (container, blob, blobType, length, createNewBlob, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('_createWriteStreamToBlob', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.blobTypeIsValid(blobType); | |
}); | |
var options = extend(true, {}, userOptions); | |
var sizeLimitation; | |
if (blobType === BlobConstants.BlobTypes.BLOCK) { | |
// default to true, unless explicitly set to false | |
options.storeBlobContentMD5 = options.storeBlobContentMD5 === false ? false : true; | |
sizeLimitation = options.blockSize || BlobConstants.DEFAULT_WRITE_BLOCK_SIZE_IN_BYTES; | |
} else if (blobType == BlobConstants.BlobTypes.PAGE) { | |
sizeLimitation = BlobConstants.DEFAULT_WRITE_PAGE_SIZE_IN_BYTES; | |
} else if (blobType == BlobConstants.BlobTypes.APPEND) { | |
sizeLimitation = BlobConstants.DEFAULT_WRITE_BLOCK_SIZE_IN_BYTES; | |
} | |
var stream = new ChunkStream({ calcContentMd5: options.storeBlobContentMD5 }); | |
stream._highWaterMark = sizeLimitation; | |
stream.pause(); //Immediately pause the stream in order to wait for the destination to getting ready | |
var self = this; | |
var createCallback = function (createError, createBlob, createResponse) { | |
if (createError) { | |
if (callback) { | |
callback(createError, createBlob, createResponse); | |
} | |
} else { | |
self._uploadBlobFromStream(createNewBlob, container, blob, blobType, stream, null, options, function (error, blob, response) { | |
if (error) { | |
stream.emit('error', error); | |
} | |
if (callback) { | |
callback(error, blob, response); | |
} | |
}); | |
} | |
}; | |
if (createNewBlob === true) { | |
this._createBlob(container, blob, blobType, length, options, createCallback); | |
} else { | |
createCallback(); | |
} | |
return stream; | |
}; | |
/** | |
* Upload blob content from a stream. Assumes the blob already exists. | |
* | |
* @ignore | |
* | |
* @this {BlobService} | |
* @param {bool} isNewBlob Specifies whether the blob is newly created. | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {BlobType} blobType The blob type. | |
* @param (Stream) stream Stream to the data to store. | |
* @param {int} streamLength The length of the stream to upload. | |
* @param {object} [options] The request options. | |
* @param {SpeedSummary} [options.speedSummary] The upload tracker objects. | |
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. (For append blob only) | |
* @param {string} [options.blockIdPrefix] The prefix to be used to generate the block id. (For block blob only) | |
* @param {int} [options.blockSize] The size of each block. Maximum is 100MB. (For block blob only) | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.transactionalContentMD5] The MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. The default value is true for block blobs. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 The callback function. | |
* @return {SpeedSummary} | |
*/ | |
BlobService.prototype._uploadBlobFromStream = function (isNewBlob, container, blob, blobType, stream, streamLength, optionsOrCallback, callback) { | |
var options; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { options = o; callback = c; }); | |
options.speedSummary = options.speedSummary || new SpeedSummary(blob); | |
if (blobType === BlobConstants.BlobTypes.BLOCK) { | |
// default to true, unless explicitly set to false | |
options.storeBlobContentMD5 = options.storeBlobContentMD5 === false ? false : true; | |
} | |
stream.pause(); | |
var self = this; | |
var startUpload = function () { | |
var putBlockBlobFromStream = function () { | |
if (streamLength > 0 && azureutil.objectIsNull(azureutil.tryGetValueChain(options, ['contentSettings', 'contentMD5'], null)) && options.storeBlobContentMD5) { | |
azureutil.calculateMD5(stream, Math.min(self.singleBlobPutThresholdInBytes, streamLength), options, function (internalBuff, contentMD5) { | |
azureutil.setObjectInnerPropertyValue(options, ['contentSettings', 'contentMD5'], contentMD5); | |
self._putBlockBlob(container, blob, internalBuff, null, internalBuff.length, options, callback); | |
}); | |
stream.resume(); | |
} else { | |
// Stream will resume when it has a pipe destination or a 'data' listener | |
self._putBlockBlob(container, blob, null, stream, streamLength, options, callback); | |
} | |
}; | |
if (streamLength === null || streamLength >= self.singleBlobPutThresholdInBytes || blobType !== BlobConstants.BlobTypes.BLOCK) { | |
var chunkStream = new ChunkStreamWithStream(stream, { calcContentMd5: options.storeBlobContentMD5 }); | |
self._uploadContentFromChunkStream(container, blob, blobType, chunkStream, streamLength, options, callback); | |
} else { | |
putBlockBlobFromStream(); | |
} | |
}; | |
if (!isNewBlob) { | |
if (options.storeBlobContentMD5 && blobType !== BlobConstants.BlobTypes.BLOCK) { | |
throw new Error(SR.MD5_NOT_POSSIBLE); | |
} | |
if (blobType === BlobConstants.BlobTypes.APPEND || options.accessConditions) { | |
// Do a getBlobProperties right at the beginning for existing blobs and use the user passed in access conditions. | |
// So any pre-condition failure on the first block (in a strictly single writer scenario) is caught. | |
// This call also helps us get the append position to append to if the user hasn’t specified an access condition. | |
this.getBlobProperties(container, blob, options, function (error, properties, response) { | |
if (error && !(options.accessConditions && options.accessConditions.EtagNonMatch === '*' && response.statusCode === 400)) { | |
callback(error); | |
} else { | |
if (blobType === BlobConstants.BlobTypes.APPEND) { | |
options.appendPosition = properties.contentLength; | |
} | |
startUpload(); | |
} | |
}); | |
} else { | |
startUpload(); | |
} | |
} else { | |
startUpload(); | |
} | |
return options.speedSummary; | |
}; | |
/** | |
* Upload blob content from a text. Assumes the blob already exists. | |
* | |
* @ignore | |
* | |
* @this {BlobService} | |
* @param {bool} isNewBlob Specifies whether the blob is newly created. | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {BlobType} blobType The blob type. | |
* @param (string) content The blob text, as a string or in a Buffer. | |
* @param {object} [options] The request options. | |
* @param {SpeedSummary} [options.speedSummary] The upload tracker objects. | |
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. (For append blob only) | |
* @param {string} [options.blockIdPrefix] The prefix to be used to generate the block id. (For block blob only) | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.transactionalContentMD5] The MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. The default value is true for block blobs. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 The callback function. | |
* @return {SpeedSummary} | |
*/ | |
BlobService.prototype._uploadBlobFromText = function (isNewBlob, container, blob, blobType, content, optionsOrCallback, callback) { | |
var options; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { options = o; callback = c; }); | |
options.speedSummary = options.speedSummary || new SpeedSummary(blob); | |
options[HeaderConstants.CONTENT_TYPE] = (options.contentSettings && options.contentSettings.contentType) || 'text/plain;charset="utf-8"'; | |
var self = this; | |
var startUpload = function () { | |
var operationFunc; | |
var length = azureutil.objectIsNull(content) ? 0 : (Buffer.isBuffer(content) ? content.length : Buffer.byteLength(content)); | |
if (blobType === BlobConstants.BlobTypes.BLOCK) { | |
// default to true, unless explicitly set to false | |
options.storeBlobContentMD5 = options.storeBlobContentMD5 === false ? false : true; | |
operationFunc = self._putBlockBlob; | |
if (length > BlobConstants.MAX_SINGLE_UPLOAD_BLOB_SIZE_IN_BYTES) { | |
throw new RangeError(SR.INVALID_BLOB_LENGTH); | |
} | |
} else if (blobType === BlobConstants.BlobTypes.APPEND) { | |
operationFunc = self._appendBlock; | |
if (length > BlobConstants.MAX_APPEND_BLOB_BLOCK_SIZE) { | |
throw new RangeError(SR.INVALID_TEXT_LENGTH); | |
} | |
} | |
var finalCallback = function (error, blobResult, response) { | |
if (blobType !== BlobConstants.BlobTypes.BLOCK) { | |
self.setBlobProperties(container, blob, options.contentSettings, options, function (error, blob, response) { | |
blob = extend(false, blob, blobResult); | |
callback(error, blob, response); | |
}); | |
} else { | |
callback(error, blobResult, response); | |
} | |
}; | |
operationFunc.call(self, container, blob, content, null, length, options, finalCallback); | |
}; | |
if (!isNewBlob) { | |
if (options.storeBlobContentMD5 && blobType !== BlobConstants.BlobTypes.BLOCK) { | |
throw new Error(SR.MD5_NOT_POSSIBLE); | |
} | |
if (blobType === BlobConstants.BlobTypes.APPEND || options.accessConditions) { | |
// Do a getBlobProperties right at the beginning for existing blobs and use the user passed in access conditions. | |
// So any pre-condition failure on the first block (in a strictly single writer scenario) is caught. | |
// This call also helps us get the append position to append to if the user hasn’t specified an access condition. | |
this.getBlobProperties(container, blob, options, function (error, properties) { | |
if (error) { | |
callback(error); | |
} else { | |
if (blobType === BlobConstants.BlobTypes.APPEND) { | |
options.appendPosition = properties.contentLength; | |
} | |
startUpload(); | |
} | |
}); | |
} | |
} else { | |
if (!azureutil.objectIsNull(content) && azureutil.objectIsNull(azureutil.tryGetValueChain(options, ['contentSettings', 'contentMD5'], null)) && options.storeBlobContentMD5) { | |
azureutil.setObjectInnerPropertyValue(options, ['contentSettings', 'contentMD5'], azureutil.getContentMd5(content)); | |
} | |
startUpload(); | |
} | |
}; | |
/** | |
* Uploads a block blob from a stream. | |
* @ignore | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {string} text The blob text. | |
* @param (Stream) stream Stream to the data to store. | |
* @param {int} length The length of the stream or text to upload. | |
* @param {object} [options] The request options. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.transactionalContentMD5] The MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. The default value is true for block blobs. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 | |
* information about the blob. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype._putBlockBlob = function (container, blob, text, stream, length, options, callback) { | |
if (!options.speedSummary) { | |
options.speedSummary = new SpeedSummary(blob); | |
} | |
var speedSummary = options.speedSummary; | |
speedSummary.totalSize = length; | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.put(resourceName) | |
.withHeader(HeaderConstants.CONTENT_TYPE, 'application/octet-stream') | |
.withHeader(HeaderConstants.BLOB_TYPE, BlobConstants.BlobTypes.BLOCK) | |
.withHeader(HeaderConstants.CONTENT_LENGTH, length); | |
if (!azureutil.objectIsNull(text) && azureutil.objectIsNull(options.transactionalContentMD5) && options.useTransactionalMD5) { | |
options.transactionalContentMD5 = azureutil.getContentMd5(text); | |
} | |
BlobResult.setHeadersFromBlob(webResource, options); | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.blobResult = null; | |
if (!responseObject.error) { | |
responseObject.blobResult = new BlobResult(container, blob); | |
responseObject.blobResult.getPropertiesFromHeaders(responseObject.response.headers); | |
if (options.metadata) { | |
responseObject.blobResult.metadata = options.metadata; | |
} | |
} | |
var finalCallback = function (returnObject) { | |
if (!returnObject || !returnObject.error) { | |
speedSummary.increment(length); | |
} | |
callback(returnObject.error, returnObject.blobResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
if (!azureutil.objectIsNull(text)) { | |
this.performRequest(webResource, text, options, processResponseCallback); | |
} else { | |
this.performRequestOutputStream(webResource, stream, options, processResponseCallback); | |
} | |
return options.speedSummary; | |
}; | |
/** | |
* Appends a new block to an append blob. | |
* | |
* @ignore | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {string|buffer} content The block content. | |
* @param (Stream) stream The stream to the data to store. | |
* @param {int} length The length of the stream or content to upload. | |
* @param {object} [options] The request options. | |
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. | |
* @param {int} [options.maxBlobSize] The max length in bytes allowed for the append blob to grow to. | |
* @param {int} [options.appendPosition] The number indicating the byte offset to check for. The append will succeed only if the end position of the blob is equal to this number. | |
* @param {string} [options.leaseId] The target blob lease identifier. | |
* @param {string} [options.transactionalContentMD5] The blob’s MD5 hash. This hash is used to verify the integrity of the blob during transport. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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. | |
*/ | |
BlobService.prototype._appendBlock = function (container, blob, content, stream, length, options, callback) { | |
var speedSummary = options.speedSummary || new SpeedSummary(blob); | |
speedSummary.totalSize = length; | |
var self = this; | |
var startAppendBlock = function () { | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.put(resourceName) | |
.withQueryOption(QueryStringConstants.COMP, 'appendblock') | |
.withHeader(HeaderConstants.CONTENT_LENGTH, length) | |
.withHeader(HeaderConstants.BLOB_CONDITION_MAX_SIZE, options.maxBlobSize) | |
.withHeader(HeaderConstants.BLOB_CONDITION_APPEND_POSITION, options.appendPosition); | |
BlobResult.setHeadersFromBlob(webResource, options); | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.blobResult = null; | |
if (!responseObject.error) { | |
responseObject.blobResult = new BlobResult(container, blob); | |
responseObject.blobResult.getPropertiesFromHeaders(responseObject.response.headers); | |
} | |
var finalCallback = function (returnObject) { | |
if (!returnObject || !returnObject.error) { | |
speedSummary.increment(length); | |
} | |
callback(returnObject.error, returnObject.blobResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
if (!azureutil.objectIsNull(content)) { | |
self.performRequest(webResource, content, options, processResponseCallback); | |
} else { | |
self.performRequestOutputStream(webResource, stream, options, processResponseCallback); | |
} | |
}; | |
if (azureutil.objectIsNull(options.transactionalContentMD5) && options.useTransactionalMD5) { | |
if (!azureutil.objectIsNull(content)) { | |
options.transactionalContentMD5 = azureutil.getContentMd5(content); | |
startAppendBlock(); | |
} else { | |
azureutil.calculateMD5(stream, length, options, function (internalBuff, contentMD5) { | |
options.transactionalContentMD5 = contentMD5; | |
content = internalBuff; | |
length = internalBuff.length; | |
startAppendBlock(); | |
}); | |
} | |
} else { | |
startAppendBlock(); | |
} | |
return options.speedSummary; | |
}; | |
/** | |
* Creates and dispatches lease requests. | |
* @ignore | |
* | |
* @this {BlobService} | |
* @param {object} webResource The web resource. | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {string} leaseId The lease identifier. Required to renew, change or release the lease. | |
* @param {string} leaseAction The lease action (BlobConstants.LeaseOperation.*). Required. | |
* @param {object} userOptions The request options. | |
* @param {int} [userOptions.leaseBreakPeriod] The lease break period. | |
* @param {string} [userOptions.leaseDuration] The lease duration. Default is never to expire. | |
* @param {string} [userOptions.proposedLeaseId] The proposed lease identifier. This is required for the CHANGE lease action. | |
* @param {LocationMode} [userOptions.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 {AccessConditions} [options.accessConditions] The access conditions. | |
* @param {int} [userOptions.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} [userOptions.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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 {Function(error, lease, response)} callback `error` will contain information | |
* if an error occurs; otherwise `lease` will contain | |
* the lease information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype._leaseImpl = function (container, blob, leaseId, leaseAction, options, callback) { | |
var webResource; | |
if (!azureutil.objectIsNull(blob)) { | |
validate.validateArgs('_leaseImpl', function (v) { | |
v.string(blob, 'blob'); | |
}); | |
var resourceName = createResourceName(container, blob); | |
webResource = WebResource.put(resourceName); | |
} else { | |
webResource = WebResource.put(container) | |
.withQueryOption(QueryStringConstants.RESTYPE, 'container'); | |
} | |
webResource.withQueryOption(QueryStringConstants.COMP, 'lease') | |
.withHeader(HeaderConstants.LEASE_ID, leaseId) | |
.withHeader(HeaderConstants.LEASE_ACTION, leaseAction.toLowerCase()) | |
.withHeader(HeaderConstants.LEASE_BREAK_PERIOD, options.leaseBreakPeriod) | |
.withHeader(HeaderConstants.PROPOSED_LEASE_ID, options.proposedLeaseId) | |
.withHeader(HeaderConstants.LEASE_DURATION, options.leaseDuration); | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.leaseResult = null; | |
if (!responseObject.error) { | |
responseObject.leaseResult = new LeaseResult(container, blob); | |
responseObject.leaseResult.getPropertiesFromHeaders(responseObject.response.headers); | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.leaseResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Updates a page blob from text. | |
* @ignore | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {string} text The text string. | |
* @param {Readable} readStream The Node.js Readable stream. | |
* @param {int} rangeStart The range start. | |
* @param {int} rangeEnd The range end. | |
* @param {object} [options] The request options. | |
* @param {string} [options.leaseId] The target blob lease identifier. | |
* @param {string} [options.transactionalContentMD5] An MD5 hash of the page content. This hash is used to verify the integrity of the page during transport. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 {Function(error, pageBlob, response)} callback `error` will contain information | |
* if an error occurs; otherwise `pageBlob` will contain | |
* the blob information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype._createPages = function (container, blob, text, readStream, rangeStart, rangeEnd, options, callback) { | |
var request = this._updatePageBlobPagesImpl(container, blob, rangeStart, rangeEnd, BlobConstants.PageWriteOptions.UPDATE, options); | |
// At this point, we have already validated that the range is less than 4MB. Therefore, we just need to calculate the contentMD5 if required. | |
// Even when this is called from the createPagesFromStream method, it is pre-buffered and called with text. | |
if (!azureutil.objectIsNull(text) && azureutil.objectIsNull(options.transactionalContentMD5) && options.useTransactionalMD5) { | |
request.withHeader(HeaderConstants.CONTENT_MD5, azureutil.getContentMd5(text)); | |
} | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.blobResult = null; | |
if (!responseObject.error) { | |
responseObject.blobResult = new BlobResult(container, blob); | |
responseObject.blobResult.getPropertiesFromHeaders(responseObject.response.headers); | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.blobResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
if (!azureutil.objectIsNull(text)) { | |
this.performRequest(request, text, options, processResponseCallback); | |
} else { | |
this.performRequestOutputStream(request, readStream, options, processResponseCallback); | |
} | |
}; | |
/** | |
* @ignore | |
*/ | |
BlobService.prototype._updatePageBlobPagesImpl = function (container, blob, rangeStart, rangeEnd, writeMethod, options) { | |
if (rangeStart && rangeStart % BlobConstants.PAGE_SIZE !== 0) { | |
throw new RangeError(SR.INVALID_PAGE_START_OFFSET); | |
} | |
if (rangeEnd && (rangeEnd + 1) % BlobConstants.PAGE_SIZE !== 0) { | |
throw new RangeError(SR.INVALID_PAGE_END_OFFSET); | |
} | |
// this is necessary if this is called from _uploadContentFromChunkStream->_createPages | |
if (!options) { | |
options = {}; | |
} | |
options.rangeStart = rangeStart; | |
options.rangeEnd = rangeEnd; | |
options.contentLength = writeMethod === BlobConstants.PageWriteOptions.UPDATE ? (rangeEnd - rangeStart) + 1 : 0; | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.put(resourceName) | |
.withQueryOption(QueryStringConstants.COMP, 'page') | |
.withHeader(HeaderConstants.CONTENT_TYPE, 'application/octet-stream') | |
.withHeader(HeaderConstants.PAGE_WRITE, writeMethod); | |
BlobResult.setHeadersFromBlob(webResource, options); | |
return webResource; | |
}; | |
/** | |
* Uploads blob content from a stream. | |
* For block blob, it creates a new block to be committed. | |
* For page blob, it writes a range of pages. | |
* For append blob, it appends a new block. | |
* | |
* @ignore | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {string} blobType The blob type. | |
* @param (Stream) stream Stream to the data to store. | |
* @param {int} streamLength The length of the stream to upload. | |
* @param {object|function} [options] The request options. | |
* @param {SpeedSummary} [options.speedSummary] The download tracker objects; | |
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading. | |
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. (For append blob only) | |
* @param {int} [options.maxBlobSize] The max length in bytes allowed for the append blob to grow to. | |
* @param {int} [options.appendPosition] The number indicating the byte offset to check for. The append will succeed only if the end position of the blob is equal to this number. | |
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions. | |
* @param {string} [options.blockIdPrefix] The prefix to be used to generate the block id. (For block blob only) | |
* @param {int} [options.blockSize] The size of each block. Maximum is 100MB. (For block blob only) | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {object} [options.metadata] The metadata key/value pairs. | |
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. | |
* @param {object} [options.contentSettings] The content settings of the blob. | |
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream. | |
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob. | |
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource. | |
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it. | |
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition. | |
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 {function(error, null)} callback The callback function. | |
* @return {SpeedSummary} | |
*/ | |
BlobService.prototype._uploadContentFromChunkStream = function (container, blob, blobType, chunkStream, streamLength, options, callback) { | |
this.logger.debug(util.format('_uploadContentFromChunkStream for blob %s', blob)); | |
var apiName; | |
var isBlockBlobUpload; | |
var isPageBlobUpload; | |
var isAppendBlobUpload; | |
var sizeLimitation; | |
var originalContentMD5 = azureutil.tryGetValueChain(options, ['contentSettings', 'contentMD5'], null); | |
var parallelOperationThreadCount = options.parallelOperationThreadCount || this.parallelOperationThreadCount; | |
if (blobType == BlobConstants.BlobTypes.BLOCK) { | |
apiName = 'createBlockFromText'; | |
isBlockBlobUpload = true; | |
// BlockBlob can only have 50000 blocks in maximum | |
var minBlockSize = Math.ceil(streamLength / 50000); | |
if (options.blockSize) { | |
if (options.blockSize < minBlockSize) { | |
// options.blockSize is less than the minBlockSize, error callback | |
var error = new ArgumentError('options.blockSize', util.format('The minimum blockSize is %s and the provided blockSize %s is too small.', minBlockSize, options.blockSize)); | |
callback(error); | |
return; | |
} else { | |
sizeLimitation = options.blockSize; | |
} | |
} else { | |
// 4MB minimum for auto-calculated block size | |
sizeLimitation = Math.max(minBlockSize, BlobConstants.DEFAULT_WRITE_BLOCK_SIZE_IN_BYTES); | |
} | |
} else if (blobType == BlobConstants.BlobTypes.PAGE) { | |
apiName = '_createPages'; | |
isPageBlobUpload = true; | |
sizeLimitation = BlobConstants.DEFAULT_WRITE_PAGE_SIZE_IN_BYTES; | |
} else if (blobType == BlobConstants.BlobTypes.APPEND) { | |
apiName = 'appendBlockFromText'; | |
isAppendBlobUpload = true; | |
parallelOperationThreadCount = 1; | |
sizeLimitation = BlobConstants.DEFAULT_WRITE_BLOCK_SIZE_IN_BYTES; | |
} else { | |
var error = new ArgumentError('blobType', util.format('Unknown blob type %s', blobType)); | |
callback(error); | |
return; | |
} | |
chunkStream._highWaterMark = sizeLimitation; | |
this._setOperationExpiryTime(options); | |
// initialize the speed summary | |
var speedSummary = options.speedSummary || new SpeedSummary(blob); | |
speedSummary.totalSize = streamLength; | |
// initialize chunk allocator | |
var allocator = new ChunkAllocator(sizeLimitation, parallelOperationThreadCount, { logger: this.logger }); | |
chunkStream.setMemoryAllocator(allocator); | |
chunkStream.setOutputLength(streamLength); | |
// if this is a FileReadStream, set the allocator on that stream | |
if (chunkStream._stream && chunkStream._stream.setMemoryAllocator) { | |
var fileReadStreamAllocator = new ChunkAllocator(chunkStream._stream._highWaterMark, parallelOperationThreadCount, { logger: this.logger }); | |
chunkStream._stream.setMemoryAllocator(fileReadStreamAllocator); | |
} | |
// initialize batch operations | |
var batchOperations = new BatchOperation(apiName, { | |
callInOrder: isAppendBlobUpload, | |
callbackInOrder: isAppendBlobUpload, | |
logger: this.logger, | |
enableReuseSocket: this.defaultEnableReuseSocket, | |
operationMemoryUsage: sizeLimitation | |
}); | |
batchOperations.setConcurrency(parallelOperationThreadCount); | |
// initialize options | |
var rangeOptions = { | |
leaseId: options.leaseId, | |
timeoutIntervalInMs: options.timeoutIntervalInMs, | |
clientRequestTimeoutInMs: options.clientRequestTimeoutInMs, | |
operationExpiryTime: options.operationExpiryTime, | |
maxBlobSize: options.maxBlobSize, | |
appendPosition: options.appendPosition || 0, | |
initialAppendPosition: options.appendPosition || 0, | |
absorbConditionalErrorsOnRetry: options.absorbConditionalErrorsOnRetry | |
}; | |
// initialize block blob variables | |
var blockIdPrefix = options.blockIdPrefix || this.generateBlockIdPrefix(); | |
var blockCount = 0; | |
var blockIds = []; | |
var blobResult = {}; | |
var self = this; | |
chunkStream.on('data', function (data, range) { | |
var operation = null; | |
var full = false; | |
var autoIncrement = speedSummary.getAutoIncrementFunction(data.length); | |
if (data.length > sizeLimitation) { | |
throw new RangeError(util.format(SR.EXCEEDED_SIZE_LIMITATION, sizeLimitation, data.length)); | |
} | |
if (options.useTransactionalMD5) { | |
//calculate content md5 for the current uploading block data | |
var contentMD5 = azureutil.getContentMd5(data); | |
rangeOptions.transactionalContentMD5 = contentMD5; | |
} | |
var checkLengthLimit = function () { | |
if (!streamLength) return true; | |
if (range.start >= streamLength) { | |
self.logger.debug(util.format('Stop uploading data from %s bytes to %s bytes to blob %s because of limit %s', range.start, range.end, blob, streamLength)); | |
chunkStream.stop(); | |
return false; | |
} else if (range.end >= streamLength) { | |
self.logger.debug(util.format('Clip uploading data from %s bytes to %s bytes to blob %s because of limit %s', range.start, range.end, blob, streamLength)); | |
range.end = streamLength - 1; | |
data = data.slice(0, streamLength - range.start); | |
if (options.useTransactionalMD5) { | |
rangeOptions.transactionalContentMD5 = azureutil.getContentMd5(data); | |
} | |
} | |
return true; | |
}; | |
var uploadBlockBlobChunk = function () { | |
if (!checkLengthLimit()) return; | |
var blockId = self.getBlockId(blockIdPrefix, blockCount); | |
blockIds.push(blockId); | |
operation = new BatchOperation.RestOperation(self, apiName, blockId, container, blob, data, rangeOptions, function (error) { | |
if (!error) { | |
autoIncrement(); | |
} else { | |
self.logger.debug(util.format('Stop uploading data as error happens. Error: %s', util.inspect(error))); | |
chunkStream.stop(); | |
} | |
allocator.releaseBuffer(data); | |
data = null; | |
}); | |
blockCount++; | |
}; | |
var uploadPageBlobChunk = function () { | |
if (!checkLengthLimit()) return; | |
if (azureutil.isBufferAllZero(data)) { | |
self.logger.debug(util.format('Skip upload data from %s bytes to %s bytes to blob %s', range.start, range.end, blob)); | |
speedSummary.increment(data.length); | |
} else { | |
self.logger.debug(util.format('Upload data from %s bytes to %s bytes to blob %s', range.start, range.end, blob)); | |
operation = new BatchOperation.RestOperation(self, apiName, container, blob, data, null, range.start, range.end, rangeOptions, function (error) { | |
if (!error) { | |
autoIncrement(); | |
} else { | |
self.logger.debug(util.format('Stop uploading data as error happens. Error: %s', util.inspect(error))); | |
chunkStream.stop(); | |
} | |
allocator.releaseBuffer(data); | |
data = null; | |
}); | |
} | |
}; | |
var uploadAppendBlobChunk = function () { | |
if (!checkLengthLimit()) return; | |
rangeOptions.appendPosition = Number(rangeOptions.initialAppendPosition) + Number(range.start); | |
// We cannot differentiate between max size condition failing only in the retry versus failing in the first attempt and retry. | |
// So we will eliminate the latter and handle the former in the append operation callback. | |
if (options.maxBlobSize && rangeOptions.appendPosition + data.length > options.maxBlobSize) { | |
throw new Error(SR.MAX_BLOB_SIZE_CONDITION_NOT_MEET); | |
} | |
operation = new BatchOperation.RestOperation(self, apiName, container, blob, data, rangeOptions, function (error, currentBlob) { | |
if (!error) { | |
autoIncrement(); | |
} else { | |
self.logger.debug(util.format('Stop uploading data as error happens. Error: %s', util.inspect(error))); | |
chunkStream.stop(); | |
} | |
blobResult = currentBlob; | |
allocator.releaseBuffer(data); | |
data = null; | |
}); | |
}; | |
if (isBlockBlobUpload) { | |
uploadBlockBlobChunk(); | |
} else if (isAppendBlobUpload) { | |
uploadAppendBlobChunk(); | |
} else if (isPageBlobUpload) { | |
uploadPageBlobChunk(); | |
} | |
if (operation) { | |
full = batchOperations.addOperation(operation); | |
operation = null; | |
if (full) { | |
self.logger.debug('File stream paused'); | |
chunkStream.pause(); | |
} | |
} | |
}); | |
chunkStream.on('end', function () { | |
self.logger.debug(util.format('File read stream ended for blob %s', blob)); | |
batchOperations.enableComplete(); | |
}); | |
batchOperations.on('drain', function () { | |
self.logger.debug('file stream resume'); | |
chunkStream.resume(); | |
}); | |
batchOperations.on('end', function (error) { | |
self.logger.debug('batch operations commited'); | |
speedSummary = null; | |
if (error) { | |
callback(error); | |
return; | |
} | |
if (originalContentMD5) { | |
options.contentSettings.contentMD5 = originalContentMD5; | |
} else if (options.storeBlobContentMD5) { | |
var contentMD5 = chunkStream.getContentMd5('base64'); | |
azureutil.setObjectInnerPropertyValue(options, ['contentSettings', 'contentMD5'], contentMD5); | |
} | |
if (isBlockBlobUpload) { | |
//commit block list | |
var blockList = { 'UncommittedBlocks': blockIds }; | |
self.commitBlocks(container, blob, blockList, options, function (error, blockList, response) { | |
self.logger.debug(util.format('Blob %s committed', blob)); | |
if (error) { | |
chunkStream.finish(); | |
callback(error); | |
} else { | |
blobResult['commmittedBlocks'] = blockIds; | |
chunkStream.finish(); | |
callback(error, blobResult, response); | |
} | |
}); | |
} else { | |
// upload page blob or append blob completely | |
var blobProperties = options.contentSettings; | |
self.setBlobProperties(container, blob, blobProperties, function (error, blob, response) { | |
chunkStream.finish(); | |
blob = extend(false, blob, blobResult); | |
callback(error, blob, response); | |
}); | |
} | |
}); | |
return speedSummary; | |
}; | |
/** | |
* Checks whether or not a container exists on the service. | |
* @ignore | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} primaryOnly If true, the request will be executed against the primary storage location. | |
* @param {object} [options] The request options. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 {Function(error, result, response)} callback `error` will contain information | |
* if an error occurs; otherwise `result` will contain | |
* the container information including `exists` boolean member. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype._doesContainerExist = function (container, primaryOnly, options, callback) { | |
var webResource = WebResource.head(container) | |
.withQueryOption(QueryStringConstants.RESTYPE, 'container') | |
.withHeader(HeaderConstants.LEASE_ID, options.leaseId); | |
if (primaryOnly === false) { | |
options.requestLocationMode = RequestLocationMode.PRIMARY_OR_SECONDARY; | |
} | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.containerResult = new ContainerResult(container); | |
if (!responseObject.error) { | |
responseObject.containerResult.exists = true; | |
responseObject.containerResult.getPropertiesFromHeaders(responseObject.response.headers); | |
} else if (responseObject.error && responseObject.error.statusCode === Constants.HttpConstants.HttpResponseCodes.NotFound) { | |
responseObject.error = null; | |
responseObject.containerResult.exists = false; | |
responseObject.response.isSuccessful = true; | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.containerResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Checks whether or not a blob exists on the service. | |
* @ignore | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {string} primaryOnly If true, the request will be executed against the primary storage location. | |
* @param {object} [options] The request options. | |
* @param {string} [options.snapshotId] The snapshot identifier. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 {Function(error, result, response)} callback `error` will contain information | |
* if an error occurs; otherwise `result` will contain | |
* the blob information including `exists` boolean member. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype._doesBlobExist = function (container, blob, primaryOnly, options, callback) { | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.head(resourceName) | |
.withQueryOption(QueryStringConstants.SNAPSHOT, options.snapshotId) | |
.withHeader(HeaderConstants.LEASE_ID, options.leaseId); | |
if (primaryOnly === false) { | |
options.requestLocationMode = RequestLocationMode.PRIMARY_OR_SECONDARY; | |
} | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.blobResult = new BlobResult(container, blob); | |
if (!responseObject.error) { | |
responseObject.blobResult.exists = true; | |
responseObject.blobResult.getPropertiesFromHeaders(responseObject.response.headers); | |
} else if (responseObject.error && responseObject.error.statusCode === Constants.HttpConstants.HttpResponseCodes.NotFound) { | |
responseObject.error = null; | |
responseObject.blobResult.exists = false; | |
responseObject.response.isSuccessful = true; | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.blobResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* @ignore | |
*/ | |
BlobService.prototype._setBlobPropertiesHelper = function (settings) { | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.blobResult = null; | |
if (!responseObject.error) { | |
responseObject.blobResult = new BlobResult(settings.container, settings.blob); | |
responseObject.blobResult.getPropertiesFromHeaders(responseObject.response.headers); | |
} | |
var finalCallback = function (returnObject) { | |
settings.callback(returnObject.error, returnObject.blobResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(settings.webResource, null, settings.options, processResponseCallback); | |
}; | |
/** | |
* @ignore | |
*/ | |
BlobService.prototype._validateLengthAndMD5 = function (options, responseObject) { | |
var storedMD5 = responseObject.response.headers[Constants.HeaderConstants.CONTENT_MD5]; | |
var contentLength; | |
if (!azureutil.objectIsNull(responseObject.response.headers[Constants.HeaderConstants.CONTENT_LENGTH])) { | |
contentLength = parseInt(responseObject.response.headers[Constants.HeaderConstants.CONTENT_LENGTH], 10); | |
} | |
// If the user has not specified this option, the default value should be false. | |
if (azureutil.objectIsNull(options.disableContentMD5Validation)) { | |
options.disableContentMD5Validation = false; | |
} | |
// None of the below cases should be retried. So set the error in every case so the retry policy filter handle knows that it shouldn't be retried. | |
if (options.disableContentMD5Validation === false && options.useTransactionalMD5 === true && azureutil.objectIsNull(storedMD5)) { | |
responseObject.error = new StorageError(SR.MD5_NOT_PRESENT_ERROR); | |
responseObject.retryable = false; | |
} | |
// Validate length and if required, MD5. | |
// If getBlobToText called this method, then the responseObject.length and responseObject.contentMD5 are not set. Calculate them first using responseObject.response.body and then validate. | |
if (azureutil.objectIsNull(responseObject.length)) { | |
if (typeof responseObject.response.body == 'string') { | |
responseObject.length = Buffer.byteLength(responseObject.response.body); | |
} else if (Buffer.isBuffer(responseObject.response.body)) { | |
responseObject.length = responseObject.response.body.length; | |
} | |
} | |
if (!azureutil.objectIsNull(contentLength) && responseObject.length !== contentLength) { | |
responseObject.error = new Error(SR.CONTENT_LENGTH_MISMATCH); | |
responseObject.retryable = false; | |
} | |
if (options.disableContentMD5Validation === false && azureutil.objectIsNull(responseObject.contentMD5)) { | |
responseObject.contentMD5 = azureutil.getContentMd5(responseObject.response.body); | |
} | |
if (options.disableContentMD5Validation === false && !azureutil.objectIsNull(storedMD5) && storedMD5 !== responseObject.contentMD5) { | |
responseObject.error = new Error(util.format(SR.HASH_MISMATCH, storedMD5, responseObject.contentMD5)); | |
responseObject.retryable = false; | |
} | |
}; | |
/** | |
* @ignore | |
*/ | |
BlobService.prototype._setRangeContentMD5Header = function (webResource, options) { | |
if (!azureutil.objectIsNull(options.rangeStart) && options.useTransactionalMD5) { | |
if (azureutil.objectIsNull(options.rangeEnd)) { | |
throw new ArgumentNullError('options.rangeEndHeader', util.format(SR.ARGUMENT_NULL_OR_EMPTY, options.rangeEndHeader)); | |
} | |
var size = parseInt(options.rangeEnd, 10) - parseInt(options.rangeStart, 10) + 1; | |
if (size > BlobConstants.MAX_RANGE_GET_SIZE_WITH_MD5) { | |
throw new ArgumentError('options', SR.INVALID_RANGE_FOR_MD5); | |
} else { | |
webResource.withHeader(HeaderConstants.RANGE_GET_CONTENT_MD5, 'true'); | |
} | |
} | |
}; | |
/** | |
* Downloads a blockblob, pageblob or appendblob into a range stream. | |
* @ignore | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {string} blobType The type of blob to download: block blob, page blob or append blob. | |
* @param {Writable} writeStream The Node.js Writable stream. | |
* @param {object} [options] The request options. | |
* @param {SpeedSummary} [options.speedSummary] The download tracker objects. | |
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading. | |
* @param {string} [options.snapshotId] The snapshot identifier. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.rangeStart] Return only the bytes of the blob in the specified range. | |
* @param {string} [options.rangeEnd] Return only the bytes of the blob in the specified range. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @param {boolean} [options.useTransactionalMD5] When set to true, Calculate and send/validate content MD5 for transactions. | |
* @param {boolean} [options.disableContentMD5Validation] When set to true, MD5 validation will be disabled when downloading blobs. | |
* @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 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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 blob information. | |
* `response` will contain information related to this operation. | |
* @return {SpeedSummary} | |
*/ | |
BlobService.prototype._getBlobToRangeStream = function (container, blob, blobType, writeStream, optionsOrCallback, callback) { | |
var options; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { options = o; callback = c; }); | |
validate.validateArgs('_getBlobToRangeStream', function (v) { | |
v.string(container, 'container'); | |
v.string(blob, 'blob'); | |
v.containerNameIsValid(container); | |
v.blobNameIsValid(container, blob); | |
v.blobTypeIsValid(blobType); | |
v.callback(callback); | |
}); | |
var rangeStream = null; | |
var isPageBlobDownload = true; | |
if (blobType == BlobConstants.BlobTypes.PAGE) { | |
rangeStream = new PageRangeStream(this, container, blob, options); | |
} else if (blobType == BlobConstants.BlobTypes.APPEND) { | |
rangeStream = new RangeStream(this, container, blob, options); | |
isPageBlobDownload = false; | |
} else if (blobType == BlobConstants.BlobTypes.BLOCK) { | |
rangeStream = new BlockRangeStream(this, container, blob, options); | |
isPageBlobDownload = false; | |
} | |
if (!options.speedSummary) { | |
options.speedSummary = new SpeedSummary(blob); | |
} | |
var speedSummary = options.speedSummary; | |
var parallelOperationThreadCount = options.parallelOperationThreadCount || this.parallelOperationThreadCount; | |
var batchOperations = new BatchOperation('getBlobInRanges', { callbackInOrder: true, logger: this.logger, enableReuseSocket: this.defaultEnableReuseSocket }); | |
batchOperations.setConcurrency(parallelOperationThreadCount); | |
var self = this; | |
var checkMD5sum = !options.disableContentMD5Validation; | |
var md5Hash = null; | |
if (checkMD5sum) { | |
md5Hash = new Md5Wrapper().createMd5Hash(); | |
} | |
var savedBlobResult = null; | |
var savedBlobResponse = null; | |
rangeStream.on('range', function (range) { | |
if (!speedSummary.totalSize) { | |
speedSummary.totalSize = rangeStream.rangeSize; | |
} | |
var requestOptions = { | |
rangeStart: range.start, | |
rangeEnd: range.end, | |
responseEncoding: null //Use Buffer to store the response data | |
}; | |
var rangeSize = range.size; | |
requestOptions.timeoutIntervalInMs = options.timeoutIntervalInMs; | |
requestOptions.clientRequestTimeoutInMs = options.clientRequestTimeoutInMs; | |
requestOptions.useTransactionalMD5 = options.useTransactionalMD5; | |
requestOptions.snapshotId = options.snapshotId; | |
if (range.dataSize === 0) { | |
if (isPageBlobDownload) { | |
var autoIncrement = speedSummary.getAutoIncrementFunction(rangeSize); | |
//No operation to do and only wait for write zero to file in callback | |
var writeZeroOperation = new BatchOperation.CommonOperation(BatchOperation.noOperation, function (error) { | |
if (error) return; | |
var bufferAvailable = azureutil.writeZerosToStream(writeStream, rangeSize, md5Hash, autoIncrement); | |
//There is no need to pause the rangestream since we can perform http request and write disk at the same time | |
self.logger.debug(util.format('Write %s bytes Zero from %s to %s', rangeSize, range.start, range.end)); | |
if (!bufferAvailable) { | |
self.logger.debug('Write stream is full and pause batch operation'); | |
batchOperations.pause(); | |
} | |
}); | |
batchOperations.addOperation(writeZeroOperation); | |
} else { | |
self.logger.debug(util.format('Can not read %s bytes to %s bytes of blob %s', range.start, range.end, blob)); | |
} | |
return; | |
} | |
if (range.start > range.end) { | |
return; | |
} | |
var operation = new BatchOperation.RestOperation(self, 'getBlobToText', container, blob, requestOptions, function (error, content, blobResult, response) { | |
if (!error) { | |
if (rangeSize !== content.length) { | |
self.logger.warn(util.format('Request %s bytes, but server returns %s bytes', rangeSize, content.length)); | |
} | |
//Save one of the succeeded callback parameters and use them at the final callback | |
if (!savedBlobResult) { | |
savedBlobResult = blobResult; | |
} | |
if (!savedBlobResponse) { | |
savedBlobResponse = response; | |
} | |
var autoIncrement = speedSummary.getAutoIncrementFunction(content.length); | |
var bufferAvailable = writeStream.write(content, autoIncrement); | |
if (!bufferAvailable) { | |
self.logger.debug('Write stream is full and pause batch operation'); | |
batchOperations.pause(); | |
} | |
if (md5Hash) { | |
md5Hash.update(content); | |
} | |
content = null; | |
} else { | |
self.logger.debug(util.format('Stop downloading data as error happens. Error: %s', util.inspect(error))); | |
rangeStream.stop(); | |
} | |
}); | |
var full = batchOperations.addOperation(operation); | |
if (full) { | |
self.logger.debug('Pause range stream'); | |
rangeStream.pause(); | |
} | |
}); | |
rangeStream.on('end', function () { | |
self.logger.debug('Range stream has ended.'); | |
batchOperations.enableComplete(); | |
}); | |
batchOperations.on('drain', function () { | |
self.logger.debug('Resume range stream'); | |
rangeStream.resume(); | |
}); | |
writeStream.on('drain', function () { | |
self.logger.debug('Resume batch operations'); | |
batchOperations.resume(); | |
}); | |
batchOperations.on('end', function (error) { | |
self.logger.debug('Download completed!'); | |
if (error) { | |
callback(error); | |
return; | |
} else { | |
writeStream.end(function () { | |
self.logger.debug('Write stream has ended'); | |
if (!savedBlobResult) { | |
savedBlobResult = {}; | |
} | |
azureutil.setObjectInnerPropertyValue(savedBlobResult, ['contentSettings', 'contentMD5'], azureutil.tryGetValueChain(options, ['contentSettings', 'contentMD5'], null)); | |
savedBlobResult.clientSideContentMD5 = null; | |
if (md5Hash) { | |
savedBlobResult.clientSideContentMD5 = md5Hash.digest('base64'); | |
} | |
callback(error, savedBlobResult, savedBlobResponse); | |
}); | |
} | |
}); | |
var listOptions = { | |
timeoutIntervalInMs: options.timeoutIntervalInMs, | |
clientRequestTimeoutInMs: options.clientRequestTimeoutInMs, | |
snapshotId: options.snapshotId, | |
leaseId: options.leaseId, | |
blockListFilter: BlobUtilities.BlockListFilter.COMMITTED | |
}; | |
rangeStream.list(listOptions, function (error) { | |
callback(error); | |
}); | |
return speedSummary; | |
}; | |
/** | |
* Downloads a blockblob or pageblob into a stream. | |
* @ignore | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {Writable} writeStream The Node.js Writable stream. | |
* @param {object} [options] The request options. | |
* @param {string} [options.snapshotId] The snapshot identifier. | |
* @param {string} [options.leaseId] The lease identifier. | |
* @param {string} [options.rangeStart] Return only the bytes of the blob in the specified range. | |
* @param {string} [options.rangeEnd] Return only the bytes of the blob in the specified range. | |
* @param {AccessConditions} [options.accessConditions] The access conditions. | |
* @param {boolean} [options.useTransactionalMD5] When set to true, Calculate and send/validate content MD5 for transactions. | |
* @param {boolean} [options.disableContentMD5Validation] When set to true, MD5 validation will be disabled when downloading blobs. | |
* @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 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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 blob information. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype._getBlobToStream = function (container, blob, writeStream, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
var resourceName = createResourceName(container, blob); | |
var webResource = WebResource.get(resourceName).withRawResponse(); | |
var options = extend(true, {}, userOptions); | |
webResource.withQueryOption(QueryStringConstants.SNAPSHOT, options.snapshotId); | |
BlobResult.setHeadersFromBlob(webResource, options); | |
this._setRangeContentMD5Header(webResource, options); | |
var self = this; | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.blobResult = null; | |
if (!responseObject.error) { | |
responseObject.blobResult = new BlobResult(container, blob); | |
responseObject.blobResult.metadata = self.parseMetadataHeaders(responseObject.response.headers); | |
responseObject.blobResult.getPropertiesFromHeaders(responseObject.response.headers); | |
self._validateLengthAndMD5(options, responseObject); | |
if (options.speedSummary) { | |
options.speedSummary.increment(responseObject.length); | |
} | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.blobResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequestInputStream(webResource, null, writeStream, options, processResponseCallback); | |
}; | |
/** | |
* Lists a segment containing a collection of blob items whose names begin with the specified prefix in the container. | |
* @ignore | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} prefix The prefix of the blob name. | |
* @param {object} currentToken A continuation token returned by a previous listing operation. Please use 'null' or 'undefined' if this is the first operation. | |
* @param {ListBlobTypes} listBlobType Specifies the item type of the results. | |
* @param {object} [options] The request options. | |
* @param {int} [options.maxResults] Specifies the maximum number of blobs to return per call to Azure ServiceClient. This does NOT affect list size returned by this function. (maximum: 5000) | |
* @param {string} [options.include] Specifies that the response should include one or more of the following subsets: '', 'metadata', 'snapshots', 'uncommittedblobs', 'copy', 'deleted'). | |
* Please find these values in BlobUtilities.BlobListingDetails. Multiple values can be added separated with a comma (,). | |
* @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 {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. | |
* @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 entries of blobs and the continuation token for the next listing operation. | |
* `response` will contain information related to this operation. | |
*/ | |
BlobService.prototype._listBlobsOrDircotriesSegmentedWithPrefix = function (container, prefix, currentToken, listBlobType, optionsOrCallback, callback) { | |
var userOptions; | |
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; }); | |
validate.validateArgs('listBlobsSegmented', function (v) { | |
v.string(container, 'container'); | |
v.containerNameIsValid(container); | |
v.callback(callback); | |
}); | |
var options = extend(true, {}, userOptions); | |
var webResource = WebResource.get(container) | |
.withQueryOption(QueryStringConstants.RESTYPE, 'container') | |
.withQueryOption(QueryStringConstants.COMP, 'list') | |
.withQueryOption(QueryStringConstants.MAX_RESULTS, options.maxResults) | |
.withQueryOptions(options, | |
QueryStringConstants.DELIMITER, | |
QueryStringConstants.INCLUDE); | |
if (!azureutil.objectIsNull(currentToken)) { | |
webResource.withQueryOption(QueryStringConstants.MARKER, currentToken.nextMarker); | |
} | |
webResource.withQueryOption(QueryStringConstants.PREFIX, prefix); | |
options.requestLocationMode = azureutil.getNextListingLocationMode(currentToken); | |
var processResponseCallback = function (responseObject, next) { | |
responseObject.listBlobsResult = null; | |
if (!responseObject.error) { | |
responseObject.listBlobsResult = { | |
entries: null, | |
continuationToken: null | |
}; | |
responseObject.listBlobsResult.entries = []; | |
var results = []; | |
if (listBlobType == BlobConstants.ListBlobTypes.Directory && responseObject.response.body.EnumerationResults.Blobs.BlobPrefix) { | |
results = responseObject.response.body.EnumerationResults.Blobs.BlobPrefix; | |
if (!_.isArray(results)) { | |
results = [results]; | |
} | |
} else if (listBlobType == BlobConstants.ListBlobTypes.Blob && responseObject.response.body.EnumerationResults.Blobs.Blob) { | |
results = responseObject.response.body.EnumerationResults.Blobs.Blob; | |
if (!_.isArray(results)) { | |
results = [results]; | |
} | |
} | |
results.forEach(function (currentBlob) { | |
var blobResult = BlobResult.parse(currentBlob); | |
responseObject.listBlobsResult.entries.push(blobResult); | |
}); | |
if (responseObject.response.body.EnumerationResults.NextMarker) { | |
responseObject.listBlobsResult.continuationToken = { | |
nextMarker: null, | |
targetLocation: null | |
}; | |
responseObject.listBlobsResult.continuationToken.nextMarker = responseObject.response.body.EnumerationResults.NextMarker; | |
responseObject.listBlobsResult.continuationToken.targetLocation = responseObject.targetLocation; | |
} | |
} | |
var finalCallback = function (returnObject) { | |
callback(returnObject.error, returnObject.listBlobsResult, returnObject.response); | |
}; | |
next(responseObject, finalCallback); | |
}; | |
this.performRequest(webResource, null, options, processResponseCallback); | |
}; | |
/** | |
* Create a new blob. | |
* @ignore | |
* | |
* @this {BlobService} | |
* @param {string} container The container name. | |
* @param {string} blob The blob name. | |
* @param {BlobType} blobType The blob type. | |
* @param {int} size The blob size. | |
* @param {object} [options] The request options. | |
* @param {string} [options.blobTier] For page blobs on premium accounts only. Set the tier of the target blob. Refer to BlobUtilities.BlobTier.PremiumPageBlobTier. | |
* @param {errorOrResult} callback The callback which operates on the specific blob. | |
*/ | |
BlobService.prototype._createBlob = function (container, blob, blobType, size, options, creationCallback) { | |
if (blobType == BlobConstants.BlobTypes.APPEND) { | |
this.createOrReplaceAppendBlob(container, blob, options, function (createError, createResponse) { | |
creationCallback(createError, null, createResponse); | |
}); | |
} else if (blobType == BlobConstants.BlobTypes.PAGE) { | |
this.createPageBlob(container, blob, size, options, function (createError) { | |
creationCallback(createError); | |
}); | |
} else if (blobType == BlobConstants.BlobTypes.BLOCK) { | |
creationCallback(); | |
} | |
}; | |
/** | |
* The callback for {BlobService~getBlobToText}. | |
* @typedef {function} BlobService~blobToText | |
* @param {object} error If an error occurs, the error information. | |
* @param {string} text The text returned from the blob. | |
* @param {object} blockBlob Information about the blob. | |
* @param {object} response Information related to this operation. | |
*/ | |
BlobService.SpeedSummary = SpeedSummary; | |
module.exports = BlobService; | |
}).call(this,require("buffer").Buffer) | |
},{"../../common/errors/errors":5,"./../../common/common.core":3,"./../../common/md5-wrapper":10,"./../../common/streams/rangestream":29,"./blobutilities":42,"./internal/blockrangestream":43,"./internal/pagerangestream":44,"./models/blobresult":45,"./models/blocklistresult":46,"./models/containerresult":47,"./models/leaseresult":48,"buffer":99,"extend":137,"querystring":185,"underscore":228,"url":229,"util":234}],42:[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 'BlobUtilities'. | |
exports = module.exports; | |
/** | |
* Defines constants, enums, and utility functions for use with the Blob service. | |
* @namespace BlobUtilities | |
*/ | |
var BlobUtilities = { | |
/** | |
* Permission types | |
* | |
* @const | |
* @enum {string} | |
*/ | |
SharedAccessPermissions: { | |
READ: 'r', | |
ADD: 'a', | |
CREATE: 'c', | |
WRITE: 'w', | |
DELETE: 'd', | |
LIST: 'l' | |
}, | |
/** | |
* Blob listing details. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
BlobListingDetails: { | |
SNAPSHOTS: 'snapshots', | |
METADATA: 'metadata', | |
UNCOMMITTED_BLOBS: 'uncommittedblobs', | |
COPY: 'copy', | |
DELETED: 'deleted' | |
}, | |
/** | |
* Deletion options for blob snapshots | |
* | |
* @const | |
* @enum {string} | |
*/ | |
SnapshotDeleteOptions: { | |
SNAPSHOTS_ONLY: 'only', | |
BLOB_AND_SNAPSHOTS: 'include' | |
}, | |
/** | |
* Type of block list to retrieve | |
* | |
* @const | |
* @enum {string} | |
*/ | |
BlockListFilter: { | |
ALL: 'all', | |
COMMITTED: 'committed', | |
UNCOMMITTED: 'uncommitted' | |
}, | |
/** | |
* Blobs and container public access types. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
BlobContainerPublicAccessType: { | |
OFF: null, | |
CONTAINER: 'container', | |
BLOB: 'blob' | |
}, | |
/** | |
* Describes actions that can be performed on a page blob sequence number. | |
* @const | |
* @enum {string} | |
*/ | |
SequenceNumberAction: { | |
MAX: 'max', | |
UPDATE: 'update', | |
INCREMENT: 'increment' | |
}, | |
/** | |
* Candidate values for blob tiers. | |
* | |
* @property {object} PremiumPageBlobTier Candidate values for premium pageblob tiers. | |
* @property {string} PremiumPageBlobTier.P4 | |
* @property {string} PremiumPageBlobTier.P6 | |
* @property {string} PremiumPageBlobTier.P10 | |
* @property {string} PremiumPageBlobTier.P20 | |
* @property {string} PremiumPageBlobTier.P30 | |
* @property {string} PremiumPageBlobTier.P40 | |
* @property {string} PremiumPageBlobTier.P50 | |
* @property {string} PremiumPageBlobTier.P60 | |
* @property {object} StandardBlobTier Candidate values for standard blobs tiers. | |
* @property {string} StandardBlobTier.HOT | |
* @property {string} StandardBlobTier.COOL | |
* @property {string} StandardBlobTier.ARCHIVE | |
*/ | |
BlobTier: { | |
PremiumPageBlobTier: { | |
P4: 'P4', | |
P6: 'P6', | |
P10: 'P10', | |
P20: 'P20', | |
P30: 'P30', | |
P40: 'P40', | |
P50: 'P50', | |
P60: 'P60' | |
}, | |
StandardBlobTier: { | |
HOT: 'Hot', | |
COOL: 'Cool', | |
ARCHIVE: 'Archive' | |
} | |
} | |
}; | |
module.exports = BlobUtilities; | |
},{}],43:[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 Constants = require('./../../../common/util/constants'); | |
var EventEmitter = require('events').EventEmitter; | |
var BlobUtilities = require('./../blobutilities'); | |
/** | |
* BlockBlob block range stream | |
*/ | |
function BlockRangeStream(blobServiceClient, container, blob, options) { | |
this.blobServiceClient = blobServiceClient; | |
this.container = container; | |
this.blob = blob; | |
this._emitter = new EventEmitter(); | |
this._paused = false; | |
this._emittedAll = false; | |
this._emittedRangeType = null; | |
this._emittedRangeIndex = null; | |
this._offset = 0; | |
this._rangelist = []; | |
this._isEmitting = false; | |
if (options.rangeStart) { | |
this._startOffset = options.rangeStart; | |
} else { | |
this._startOffset = 0; | |
} | |
if (options.rangeEnd) { | |
this._endOffset = options.rangeEnd; | |
} else { | |
this._endOffset = Number.MAX_VALUE; | |
} | |
} | |
/** | |
* Add event listener | |
*/ | |
BlockRangeStream.prototype.on = function (event, listener) { | |
this._emitter.on(event, listener); | |
}; | |
/** | |
* Get block list | |
*/ | |
BlockRangeStream.prototype.list = function (options, callback) { | |
if (!options) { | |
options = {}; | |
} | |
if (!options.blockListFilter) { | |
options.blockListFilter = BlobUtilities.BlockListFilter.ALL; | |
} | |
var self = this; | |
this.blobServiceClient.listBlocks(this.container, this.blob, options.blockListFilter, options, function (error, blocklist, response) { | |
if (error) { | |
callback(error); | |
} else { | |
var totalSize = parseInt(response.headers[Constants.HeaderConstants.BLOB_CONTENT_LENGTH], 10); | |
if (!blocklist.CommittedBlocks) { | |
//Convert single block blob to block blob range | |
var name = 'NODESDK_BLOCKBLOB_RANGESTREAM'; | |
blocklist.CommittedBlocks = [{ Name : name, Size : totalSize }]; | |
} | |
self._rangelist = blocklist; | |
self._emitBlockList(); | |
self = blocklist = null; | |
} | |
}); | |
}; | |
/** | |
* Emit block ranges | |
*/ | |
BlockRangeStream.prototype._emitBlockList = function () { | |
if (this._paused || this._emittedAll || this._isEmitting) return; | |
var self = this; | |
this._getTypeList(function () { | |
self._rangelist = null; | |
self._emittedAll = true; | |
self._emitter.emit('end'); | |
}); | |
}; | |
/** | |
* Get the block type list | |
*/ | |
BlockRangeStream.prototype._getTypeList = function (callback) { | |
this._isEmitting = true; | |
try { | |
var typeStart = false; | |
if (this._rangelist) { | |
for (var blockType in this._rangelist) { | |
if (this._rangelist.hasOwnProperty(blockType)) { | |
if (this._emittedRangeType === null || typeStart || this._emittedRangeType == blockType) { | |
this._emittedRangeType = blockType; | |
typeStart = true; | |
} else if (this._emittedRangeType !== blockType) { | |
continue; | |
} | |
if (this._paused) { | |
return; | |
} | |
this._emitBlockRange (blockType, callback); | |
} | |
} | |
} | |
} finally { | |
this._isEmitting = false; | |
} | |
}; | |
/** | |
* Get the block list | |
*/ | |
BlockRangeStream.prototype._emitBlockRange = function (blockType, callback) { | |
var blockList = this._rangelist[blockType]; | |
var indexStart = false; | |
for (var blockIndex = 0; blockIndex < blockList.length; blockIndex++) { | |
if (this._emittedRangeIndex === null || indexStart || this._emittedRangeIndex === blockIndex) { | |
this._emittedRangeIndex = blockIndex; | |
indexStart = true; | |
} else if (this._emittedRangeIndex !== blockIndex) { | |
continue; | |
} | |
if (this._paused) { | |
return; | |
} | |
var range = blockList[blockIndex]; | |
// follow the same naming convention of page ranges and json | |
range.name = range.Name; | |
range.type = blockType; | |
range.start = this._offset; | |
this._offset += parseInt(range.Size, 10); | |
range.end = this._offset - 1; | |
delete range.Name; | |
delete range.Size; | |
if (range.start > this._endOffset) { | |
break; | |
} else if (range.end < this._startOffset) { | |
continue; | |
} else { | |
range.start = Math.max(range.start, this._startOffset); | |
range.end = Math.min(range.end, this._endOffset); | |
range.size = range.end - range.start + 1; | |
range.dataSize = range.size; | |
this._emitter.emit('range', range); | |
} | |
} | |
// remove the used range and avoid memory leak | |
this._rangelist[blockType] = null; | |
callback(); | |
}; | |
/** | |
* Pause the stream | |
*/ | |
BlockRangeStream.prototype.pause = function () { | |
this._paused = true; | |
}; | |
/** | |
* Resume the stream | |
*/ | |
BlockRangeStream.prototype.resume = function () { | |
this._paused = false; | |
if (!this._isEmitting) { | |
this._emitBlockList(); | |
} | |
}; | |
/** | |
* Stop the stream | |
*/ | |
BlockRangeStream.prototype.stop = function () { | |
this.pause(); | |
this._emittedAll = true; | |
this._emitter.emit('end'); | |
}; | |
module.exports = BlockRangeStream; | |
},{"./../../../common/util/constants":32,"./../blobutilities":42,"events":135}],44:[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 RangeStream = require('./../../../common/streams/rangestream'); | |
var Constants = require('./../../../common/util/constants'); | |
/** | |
* PageBlob page range stream | |
*/ | |
function PageRangeStream(blobServiceClient, container, blob, options) { | |
PageRangeStream['super_'].call(this, blobServiceClient, container, blob, options); | |
if (options.minRangeSize) { | |
this._minRangeSize = options.minRangeSize; | |
} else { | |
this._minRangeSize = Constants.BlobConstants.MIN_WRITE_PAGE_SIZE_IN_BYTES; | |
} | |
if (options.maxRangeSize) { | |
this._maxRangeSize = options.maxRangeSize; | |
} else { | |
this._maxRangeSize = Constants.BlobConstants.DEFAULT_WRITE_PAGE_SIZE_IN_BYTES; | |
} | |
this._lengthHeader = Constants.HeaderConstants.BLOB_CONTENT_LENGTH; | |
this._listFunc = blobServiceClient.listPageRanges; | |
} | |
util.inherits(PageRangeStream, RangeStream); | |
module.exports = PageRangeStream; | |
},{"./../../../common/streams/rangestream":29,"./../../../common/util/constants":32,"util":234}],45:[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. | |
// | |
// Module dependencies. | |
var _ = require('underscore'); | |
var azureCommon = require('./../../../common/common.core'); | |
var azureutil = azureCommon.util; | |
var Constants = azureCommon.Constants; | |
var HeaderConstants = Constants.HeaderConstants; | |
/** | |
* Creates a new BlobResult object. | |
* @class | |
* The BlobResult class is used to store the blob information. | |
* | |
* @property {string} container The container name. | |
* @property {string} name The blob name. | |
* @property {object} metadata The metadata key/value pair. | |
* @property {string} etag The etag. | |
* @property {string} lastModified The date/time that the blob was last modified. | |
* @property {string} contentLength The size of the blob in bytes. | |
* @property {string} blobType The blob type. | |
* @property {boolean} isIncrementalCopy If the blob is incremental copy blob. | |
* @property {string} requestId The request id. | |
* @property {string} sequenceNumber The current sequence number for a page blob. | |
* @property {string} contentRange The content range. | |
* @property {string} committedBlockCount The committed block count. | |
* @property {string} serverEncrypted If the blob data and application metadata are completely encrypted using the specified algorithm. true/false. | |
* @property {object} contentSettings The content settings. | |
* @property {string} contentSettings.contentType The content type. | |
* @property {string} contentSettings.contentEncoding The content encoding. | |
* @property {string} contentSettings.contentLanguage The content language. | |
* @property {string} contentSettings.cacheControl The cache control. | |
* @property {string} contentSettings.contentDisposition The content disposition. | |
* @property {string} contentSettings.contentMD5 The content MD5 hash. | |
* @property {object} lease The lease information. | |
* @property {string} lease.id The lease id. | |
* @property {string} lease.status The lease status. | |
* @property {string} lease.state The lease state. | |
* @property {string} lease.duration The lease duration. | |
* @property {object} copy The copy information. | |
* @property {string} copy.id The copy id. | |
* @property {string} copy.status The copy status. | |
* @property {string} copy.completionTime The copy completion time. | |
* @property {string} copy.statusDescription The copy status description. | |
* @property {string} copy.destinationSnapshot The snapshot time of the last successful incremental copy snapshot for this blob. | |
* @property {string} copy.progress The copy progress. | |
* @property {string} copy.source The copy source. | |
* | |
* @constructor | |
* @param {string} [container] The container name. | |
* @param {string} [name] The blob name. | |
*/ | |
function BlobResult(container, name) { | |
if (container) { | |
this.container = container; | |
} | |
if (name) { | |
this.name = name; | |
} | |
} | |
BlobResult.parse = function (blobXml) { | |
var blobResult = new BlobResult(); | |
for (var propertyName in blobXml) { | |
if (blobXml.hasOwnProperty(propertyName)) { | |
if (propertyName === 'Properties') { | |
// Lift out the properties onto the main object to keep consistent across all APIs like: getBlobProperties | |
azureutil.setPropertyValueFromXML(blobResult, blobXml[propertyName], true); | |
} else if (propertyName === 'Metadata') { | |
var resultPropertyName = azureutil.normalizePropertyNameFromXML(propertyName); | |
blobResult[resultPropertyName] = {}; | |
azureutil.setPropertyValueFromXML(blobResult[resultPropertyName], blobXml[propertyName], false); | |
} else { | |
blobResult[propertyName.toLowerCase()] = blobXml[propertyName]; | |
} | |
} | |
} | |
if (blobResult.isIncrementalCopy !== undefined) { | |
blobResult.isIncrementalCopy = (blobResult.isIncrementalCopy === 'true'); | |
} | |
// convert accessTierInferred to boolean type | |
if (blobResult.accessTierInferred !== undefined) { | |
blobResult.accessTierInferred = (blobResult.accessTierInferred === 'true'); | |
} | |
if (blobResult.deleted !== undefined) { | |
blobResult.deleted = (blobResult.deleted == 'true'); | |
} | |
if (blobResult.remainingRetentionDays !== undefined) { | |
blobResult.remainingRetentionDays = parseInt(blobResult.remainingRetentionDays); | |
} | |
return blobResult; | |
}; | |
var headersForProperties = { | |
'lastModified': 'LAST_MODIFIED', | |
'creationTime': 'CREATION_TIME', | |
'etag': 'ETAG', | |
'sequenceNumber': 'SEQUENCE_NUMBER', | |
'blobType': 'BLOB_TYPE', | |
'contentLength': 'CONTENT_LENGTH', | |
'blobContentLength': 'BLOB_CONTENT_LENGTH', | |
'contentRange': 'CONTENT_RANGE', | |
'committedBlockCount': 'BLOB_COMMITTED_BLOCK_COUNT', | |
'serverEncrypted': 'SERVER_ENCRYPTED', | |
'requestId': 'REQUEST_ID', | |
'range': 'RANGE', | |
'blobRange': 'STORAGE_RANGE', | |
'getContentMd5': 'RANGE_GET_CONTENT_MD5', | |
'acceptRanges': 'ACCEPT_RANGES', | |
'appendOffset': 'BLOB_APPEND_OFFSET', | |
'accessTier': 'ACCESS_TIER', | |
'accessTierChangeTime': 'ACCESS_TIER_CHANGE_TIME', | |
'accessTierInferred': 'ACCESS_TIER_INFERRED', | |
'archiveStatus': 'ARCHIVE_STATUS', | |
'isIncrementalCopy': 'INCREMENTAL_COPY', | |
// ContentSettings | |
'contentSettings.contentType': 'CONTENT_TYPE', | |
'contentSettings.contentEncoding': 'CONTENT_ENCODING', | |
'contentSettings.contentLanguage': 'CONTENT_LANGUAGE', | |
'contentSettings.cacheControl': 'CACHE_CONTROL', | |
'contentSettings.contentDisposition': 'CONTENT_DISPOSITION', | |
'contentSettings.contentMD5': 'CONTENT_MD5', | |
// Lease | |
'lease.id': 'LEASE_ID', | |
'lease.status': 'LEASE_STATUS', | |
'lease.duration': 'LEASE_DURATION', | |
'lease.state': 'LEASE_STATE', | |
// Copy | |
'copy.id': 'COPY_ID', | |
'copy.status': 'COPY_STATUS', | |
'copy.source': 'COPY_SOURCE', | |
'copy.progress': 'COPY_PROGRESS', | |
'copy.completionTime': 'COPY_COMPLETION_TIME', | |
'copy.statusDescription': 'COPY_STATUS_DESCRIPTION', | |
'copy.destinationSnapshot': 'COPY_DESTINATION_SNAPSHOT' | |
}; | |
BlobResult.prototype.getPropertiesFromHeaders = function (headers) { | |
var self = this; | |
var setBlobPropertyFromHeaders = function (blobProperty, headerProperty) { | |
if (!azureutil.tryGetValueChain(self, blobProperty.split('.'), null) && headers[headerProperty.toLowerCase()]) { | |
azureutil.setObjectInnerPropertyValue(self, blobProperty.split('.'), headers[headerProperty.toLowerCase()]); | |
if (blobProperty === 'copy.progress') { | |
var info = azureutil.parseCopyProgress(self.copy.progress); | |
self.copy.bytesCopied = parseInt(info.bytesCopied); | |
self.copy.totalBytes = parseInt(info.totalBytes); | |
} | |
} | |
}; | |
// For range get, 'x-ms-blob-content-md5' indicate the overall MD5 of the blob. Try to set the contentMD5 using this header if it presents | |
setBlobPropertyFromHeaders('contentSettings.contentMD5', HeaderConstants.BLOB_CONTENT_MD5); | |
_.chain(headersForProperties).pairs().each(function (pair) { | |
var property = pair[0]; | |
var header = HeaderConstants[pair[1]]; | |
setBlobPropertyFromHeaders(property, header); | |
}); | |
// convert isIncrementalCopy to boolean type | |
if (self.isIncrementalCopy !== undefined) { | |
self.isIncrementalCopy = (self.isIncrementalCopy === 'true'); | |
} | |
// convert accessTierInferred to boolean type | |
if (self.accessTierInferred !== undefined) { | |
self.accessTierInferred = (self.accessTierInferred == 'true'); | |
} | |
}; | |
/** | |
* This method sets the HTTP headers and is used by all methods except setBlobProperties and commitBlocks. Those 2 methods will set the x-ms-* headers using setPropertiesFromBlob. | |
* @ignore | |
*/ | |
BlobResult.setHeadersFromBlob = function (webResource, blob) { | |
var setHeaderPropertyFromBlob = function (headerProperty, blobProperty) { | |
var blobPropertyValue = azureutil.tryGetValueChain(blob, blobProperty.split('.'), null); | |
if (blobPropertyValue) { | |
webResource.withHeader(headerProperty, blobPropertyValue); | |
} | |
}; | |
if (blob) { | |
// Content-Type | |
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_TYPE, 'contentSettings.contentType'); | |
// Content-Encoding | |
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_ENCODING, 'contentSettings.contentEncoding'); | |
// Content-Language | |
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_LANGUAGE, 'contentSettings.contentLanguage'); | |
// Content-Disposition | |
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_DISPOSITION, 'contentSettings.contentDisposition'); | |
// Cache-Control | |
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CACHE_CONTROL, 'contentSettings.cacheControl'); | |
// Blob's Content-MD5 | |
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_MD5, 'contentSettings.contentMD5'); | |
// Content-Length | |
setHeaderPropertyFromBlob(HeaderConstants.CONTENT_LENGTH, 'contentLength'); | |
// transactional Content-MD5 | |
setHeaderPropertyFromBlob(HeaderConstants.CONTENT_MD5, 'transactionalContentMD5'); | |
// Range | |
if (!azureutil.objectIsNull(blob.rangeStart)) { | |
var range = 'bytes=' + blob.rangeStart + '-'; | |
if (!azureutil.objectIsNull(blob.rangeEnd)) { | |
range += blob.rangeEnd; | |
} | |
webResource.withHeader(HeaderConstants.RANGE, range); | |
} | |
// Blob Type | |
setHeaderPropertyFromBlob(HeaderConstants.BLOB_TYPE, 'blobType'); | |
// Lease id | |
setHeaderPropertyFromBlob(HeaderConstants.LEASE_ID, 'leaseId'); | |
// Sequence number | |
setHeaderPropertyFromBlob(HeaderConstants.SEQUENCE_NUMBER, 'sequenceNumber'); | |
setHeaderPropertyFromBlob(HeaderConstants.SEQUENCE_NUMBER_ACTION, 'sequenceNumberAction'); | |
if (blob.metadata) { | |
webResource.addOptionalMetadataHeaders(blob.metadata); | |
} | |
} | |
}; | |
/** | |
* This method sets the x-ms-* headers and is used by setBlobProperties and commitBlocks. All other methods will set the regular HTTP headers using setHeadersFromBlob. | |
* @ignore | |
*/ | |
BlobResult.setPropertiesFromBlob = function (webResource, blob) { | |
var setHeaderPropertyFromBlob = function (headerProperty, blobProperty) { | |
var propertyValue = azureutil.tryGetValueChain(blob, blobProperty.split('.'), null); | |
if (propertyValue) { | |
webResource.withHeader(headerProperty, propertyValue); | |
} | |
}; | |
if (blob) { | |
// Content-Type | |
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_TYPE, 'contentSettings.contentType'); | |
// Content-Encoding | |
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_ENCODING, 'contentSettings.contentEncoding'); | |
// Content-Language | |
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_LANGUAGE, 'contentSettings.contentLanguage'); | |
// Content-Disposition | |
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_DISPOSITION, 'contentSettings.contentDisposition'); | |
// Cache-Control | |
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CACHE_CONTROL, 'contentSettings.cacheControl'); | |
// Content-MD5 | |
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_MD5, 'contentSettings.contentMD5'); | |
// Lease id | |
setHeaderPropertyFromBlob(HeaderConstants.LEASE_ID, 'leaseId'); | |
if (blob.metadata) { | |
webResource.addOptionalMetadataHeaders(blob.metadata); | |
} | |
} | |
}; | |
module.exports = BlobResult; | |
},{"./../../../common/common.core":3,"underscore":228}],46:[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. | |
// | |
// Module dependencies. | |
var _ = require('underscore'); | |
var azureCommon = require('./../../../common/common.core'); | |
var xmlbuilder = azureCommon.xmlbuilder; | |
var Constants = azureCommon.Constants; | |
/** | |
* Builds an XML representation for a block list. | |
* | |
* @param {array} The block list. | |
* @return {string} The XML block list. | |
*/ | |
exports.serialize = function (blockListJs) { | |
var blockListDoc = xmlbuilder.create(); | |
blockListDoc = blockListDoc.begin(Constants.BlobConstants.BLOCK_LIST_ELEMENT, { version: '1.0', encoding: 'utf-8' }); | |
if (_.isArray(blockListJs.LatestBlocks)) { | |
blockListJs.LatestBlocks.forEach(function (block) { | |
blockListDoc = blockListDoc.ele(Constants.BlobConstants.LATEST_ELEMENT) | |
.txt(new Buffer(block).toString('base64')) | |
.up(); | |
}); | |
} | |
if (_.isArray(blockListJs.CommittedBlocks)) { | |
blockListJs.CommittedBlocks.forEach(function (block) { | |
blockListDoc = blockListDoc.ele(Constants.BlobConstants.COMMITTED_ELEMENT) | |
.txt(new Buffer(block).toString('base64')) | |
.up(); | |
}); | |
} | |
if (_.isArray(blockListJs.UncommittedBlocks)) { | |
blockListJs.UncommittedBlocks.forEach(function (block) { | |
blockListDoc = blockListDoc.ele(Constants.BlobConstants.UNCOMMITTED_ELEMENT) | |
.txt(new Buffer(block).toString('base64')) | |
.up(); | |
}); | |
} | |
return blockListDoc.doc().toString(); | |
}; | |
exports.parse = function (blockListXml) { | |
var blockListResult = {}; | |
if (blockListXml.CommittedBlocks && blockListXml.CommittedBlocks.Block) { | |
blockListResult.CommittedBlocks = blockListXml.CommittedBlocks.Block; | |
if (!_.isArray(blockListResult.CommittedBlocks)) { | |
blockListResult.CommittedBlocks = [blockListResult.CommittedBlocks]; | |
} | |
blockListResult.CommittedBlocks.forEach(function(block) { | |
block.Name = new Buffer(block.Name, 'base64').toString(); | |
}); | |
} | |
if (blockListXml.UncommittedBlocks && blockListXml.UncommittedBlocks.Block) { | |
blockListResult.UncommittedBlocks = blockListXml.UncommittedBlocks.Block; | |
if (!_.isArray(blockListResult.UncommittedBlocks)) { | |
blockListResult.UncommittedBlocks = [blockListResult.UncommittedBlocks]; | |
} | |
blockListResult.UncommittedBlocks.forEach(function(block) { | |
block.Name = new Buffer(block.Name, 'base64').toString(); | |
}); | |
} | |
return blockListResult; | |
}; | |
}).call(this,require("buffer").Buffer) | |
},{"./../../../common/common.core":3,"buffer":99,"underscore":228}],47:[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. | |
// | |
// Module dependencies. | |
var azureCommon = require('./../../../common/common.core'); | |
var azureutil = azureCommon.util; | |
var Constants = azureCommon.Constants; | |
var HeaderConstants = Constants.HeaderConstants; | |
var BlobUtilities = require('../blobutilities'); | |
/** | |
* Creates a new ContainerResult object. | |
* @class | |
* The ContainerResult class is used to store the container information. | |
* | |
* @property {string} name The container name. | |
* @property {string} publicAccessLevel The public access level. | |
* @property {object} metadata The metadata key/value pair. | |
* @property {string} etag The etag. | |
* @property {string} lastModified The date/time that the container was last modified. | |
* @property {string} requestId The request id. | |
* @property {object} lease The lease information. | |
* @property {string} lease.status The lease status. | |
* @property {string} lease.state The lease state. | |
* @property {string} lease.duration The lease duration. | |
* | |
* @constructor | |
* @param {string} [container] The container name. | |
* @param {string} [publicAccessLevel] The public access level. | |
*/ | |
function ContainerResult(name, publicAccessLevel) { | |
if (name) { | |
this.name = name; | |
} | |
if (publicAccessLevel) { | |
this.publicAccessLevel = publicAccessLevel; | |
} | |
} | |
ContainerResult.parse = function (containerXml) { | |
var containerResult = new ContainerResult(); | |
for (var propertyName in containerXml) { | |
if (containerXml.hasOwnProperty(propertyName)) { | |
if (propertyName === 'Properties') { | |
// Lift out the properties onto the main object to keep consistent across all APIs like: getContainerProperties | |
azureutil.setPropertyValueFromXML(containerResult, containerXml[propertyName], true); | |
} else if (propertyName === 'Metadata') { | |
var resultPropertyName = azureutil.normalizePropertyNameFromXML(propertyName); | |
containerResult[resultPropertyName] = {}; | |
azureutil.setPropertyValueFromXML(containerResult[resultPropertyName], containerXml[propertyName], false); | |
} else { | |
containerResult[propertyName.toLowerCase()] = containerXml[propertyName]; | |
} | |
} | |
} | |
if (!containerResult.publicAccessLevel) { | |
containerResult.publicAccessLevel = BlobUtilities.BlobContainerPublicAccessType.OFF; | |
} | |
// convert hasImmutabilityPolicy to boolean type | |
if (containerResult.hasImmutabilityPolicy !== undefined) { | |
containerResult.hasImmutabilityPolicy = (containerResult.hasImmutabilityPolicy === 'true'); | |
} | |
// convert hasLegalHold to boolean type | |
if (containerResult.hasLegalHold !== undefined) { | |
containerResult.hasLegalHold = (containerResult.hasLegalHold === 'true'); | |
} | |
return containerResult; | |
}; | |
ContainerResult.prototype.getPropertiesFromHeaders = function (headers) { | |
var self = this; | |
var setContainerPropertyFromHeaders = function (containerProperty, headerProperty) { | |
if (!azureutil.tryGetValueChain(self, containerProperty.split('.'), null) && headers[headerProperty.toLowerCase()]) { | |
azureutil.setObjectInnerPropertyValue(self, containerProperty.split('.'), headers[headerProperty.toLowerCase()]); | |
} | |
}; | |
setContainerPropertyFromHeaders('etag', HeaderConstants.ETAG); | |
setContainerPropertyFromHeaders('lastModified', HeaderConstants.LAST_MODIFIED); | |
setContainerPropertyFromHeaders('lease.status', HeaderConstants.LEASE_STATUS); | |
setContainerPropertyFromHeaders('lease.state', HeaderConstants.LEASE_STATE); | |
setContainerPropertyFromHeaders('lease.duration', HeaderConstants.LEASE_DURATION); | |
setContainerPropertyFromHeaders('requestId', HeaderConstants.REQUEST_ID); | |
setContainerPropertyFromHeaders('hasImmutabilityPolicy', HeaderConstants.HAS_IMMUTABILITY_POLICY); | |
setContainerPropertyFromHeaders('hasLegalHold', HeaderConstants.HAS_LEGAL_HOLD); | |
// convert hasImmutabilityPolicy to boolean type | |
if (self.hasImmutabilityPolicy !== undefined) { | |
self.hasImmutabilityPolicy = (self.hasImmutabilityPolicy === 'true'); | |
} | |
// convert hasLegalHold to boolean type | |
if (self.hasLegalHold !== undefined) { | |
self.hasLegalHold = (self.hasLegalHold === 'true'); | |
} | |
if (!self.publicAccessLevel) { | |
self.publicAccessLevel = BlobUtilities.BlobContainerPublicAccessType.OFF; | |
if (headers[HeaderConstants.BLOB_PUBLIC_ACCESS]) { | |
self.publicAccessLevel = headers[HeaderConstants.BLOB_PUBLIC_ACCESS]; | |
} | |
} | |
if (self.publicAccessLevel === 'true') { | |
// The container was marked for full public read access using a version prior to 2009-09-19. | |
self.publicAccessLevel = BlobUtilities.BlobContainerPublicAccessType.CONTAINER; | |
} | |
}; | |
/** | |
* The container ACL settings. | |
* @typedef {object} ContainerAclResult | |
* @extends {ContainerResult} | |
* @property {Object.<string, AccessPolicy>} signedIdentifiers The container ACL settings. See `[AccessPolicy]{@link AccessPolicy}` for detailed information. | |
*/ | |
module.exports = ContainerResult; | |
},{"../blobutilities":42,"./../../../common/common.core":3}],48:[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. | |
// | |
// Module dependencies. | |
var Constants = require('./../../../common/common.core').Constants; | |
var HeaderConstants = Constants.HeaderConstants; | |
/** | |
* Creates a new LeaseResult object. | |
* @class | |
* The LeaseResult class is used to store the lease information. | |
* | |
* @property {string} container The container name. | |
* @property {string} blob The blob name. | |
* @property {string} id The lease id. | |
* @property {string} time Approximate time remaining in the lease period, in seconds. | |
* @property {string} etag The etag. | |
* @property {string} lastModified The date/time that the lease was last modified. | |
* | |
* @constructor | |
* @param {string} [container] The container name. | |
* @param {string} [blob] The blob name. | |
* @param {string} [id] The lease id. | |
* @param {string} [time] Approximate time remaining in the lease period, in seconds. | |
*/ | |
function LeaseResult(container, blob, id, time) { | |
if (container) { | |
this.container = container; | |
} | |
if (blob) { | |
this.blob = blob; | |
} | |
if (id) { | |
this.id = id; | |
} | |
if (time) { | |
this.time = time; | |
} | |
} | |
LeaseResult.prototype.getPropertiesFromHeaders = function (headers) { | |
var self = this; | |
if (!self['id'] && headers[HeaderConstants.LEASE_ID]) { | |
self['id'] = headers[HeaderConstants.LEASE_ID]; | |
} | |
if (!self['time'] && headers[HeaderConstants.LEASE_TIME]) { | |
self['time'] = parseInt(headers[HeaderConstants.LEASE_TIME], 10); | |
} | |
self['etag'] = headers[HeaderConstants.ETAG]; | |
self['lastModified'] = headers[HeaderConstants.LAST_MODIFIED.toLowerCase()]; | |
}; | |
module.exports = LeaseResult; | |
},{"./../../../common/common.core":3}],49:[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 'FileUtilities'. | |
exports = module.exports; | |
/** | |
* Defines constants, enums, and utility functions for use with the File service. | |
* @namespace FileUtilities | |
*/ | |
var FileUtilities = { | |
/** | |
* Permission types | |
* | |
* @const | |
* @enum {string} | |
*/ | |
SharedAccessPermissions: { | |
READ: 'r', | |
CREATE: 'c', | |
WRITE: 'w', | |
DELETE: 'd', | |
LIST: 'l' | |
}, | |
/** | |
* Listing details. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
ListingDetails: { | |
METADATA: 'metadata' | |
}, | |
/** | |
* File and share public access types. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
SharePublicAccessType: { | |
OFF: null, | |
SHARE: 'share', | |
FILE: 'file' | |
}, | |
/** | |
* Deletion options for share snapshots | |
* | |
* @const | |
* @enum {string} | |
*/ | |
ShareSnapshotDeleteOptions: { | |
SHARE_AND_SNAPSHOTS: 'include' | |
}, | |
}; | |
module.exports = FileUtilities; | |
},{}],50:[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 'HeaderConstants'. | |
exports = module.exports; | |
/** | |
* Defines constants, enums, and utility functions for use with the Table service. | |
* @namespace TableUtilities | |
*/ | |
var TableUtilities = { | |
/** | |
* Permission types. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
SharedAccessPermissions: { | |
QUERY: 'r', | |
ADD: 'a', | |
UPDATE: 'u', | |
DELETE: 'd' | |
}, | |
/** | |
* Payload Format. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
PayloadFormat: { | |
FULL_METADATA: 'application/json;odata=fullmetadata', | |
MINIMAL_METADATA: 'application/json;odata=minimalmetadata', | |
NO_METADATA: 'application/json;odata=nometadata' | |
}, | |
/** | |
* Defines the set of Boolean operators for constructing queries. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
TableOperators: { | |
AND: 'and', | |
NOT: 'not', | |
OR: 'or' | |
}, | |
/** | |
* Filter property comparison operators. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
QueryComparisons: { | |
EQUAL: 'eq', | |
NOT_EQUAL: 'ne', | |
GREATER_THAN: 'gt', | |
GREATER_THAN_OR_EQUAL: 'ge', | |
LESS_THAN: 'lt', | |
LESS_THAN_OR_EQUAL: 'le' | |
}, | |
/** | |
* Edm types. | |
* | |
* @const | |
* @enum {string} | |
*/ | |
EdmType: { | |
STRING: 'Edm.String', | |
BINARY: 'Edm.Binary', | |
INT64: 'Edm.Int64', | |
INT32: 'Edm.Int32', | |
DOUBLE: 'Edm.Double', | |
DATETIME: 'Edm.DateTime', | |
GUID: 'Edm.Guid', | |
BOOLEAN: 'Edm.Boolean' | |
}, | |
/** | |
* A helper to create table entities. | |
* | |
* @example | |
* var entGen = TableUtilities.entityGenerator; | |
* var entity = { PartitionKey: entGen.String('part2'), | |
* RowKey: entGen.String('row1'), | |
* boolValue: entGen.Boolean(true), | |
* intValue: entGen.Int32(42), | |
* dateValue: entGen.DateTime(new Date(Date.UTC(2011, 10, 25))), | |
* }; | |
*/ | |
entityGenerator: (function() | |
{ | |
var EntityProperty = function (value, type) { | |
var entityProperty = { _:value}; | |
if (type) { | |
entityProperty['$'] = type; | |
} | |
return entityProperty; | |
}; | |
return { | |
EntityProperty : EntityProperty, | |
Int32 : function(value) { | |
return new EntityProperty(value, 'Edm.Int32'); | |
}, | |
Int64 : function(value) { | |
return new EntityProperty(value, 'Edm.Int64'); | |
}, | |
Binary : function(value) { | |
return new EntityProperty(value, 'Edm.Binary'); | |
}, | |
Boolean : function(value) { | |
return new EntityProperty(value, 'Edm.Boolean'); | |
}, | |
String : function(value) { | |
return new EntityProperty(value, 'Edm.String'); | |
}, | |
Guid : function(value) { | |
return new EntityProperty(value, 'Edm.Guid'); | |
}, | |
Double : function(value) { | |
return new EntityProperty(value, 'Edm.Double'); | |
}, | |
DateTime : function(value) { | |
return new EntityProperty(value, 'Edm.DateTime'); | |
} | |
}; | |
})() | |
}; | |
module.exports = TableUtilities; | |
},{}],51:[function(require,module,exports){ | |
var asn1 = exports; | |
asn1.bignum = require('bn.js'); | |
asn1.define = require('./asn1/api').define; | |
asn1.base = require('./asn1/base'); | |
asn1.constants = require('./asn1/constants'); | |
asn1.decoders = require('./asn1/decoders'); | |
asn1.encoders = require('./asn1/encoders'); | |
},{"./asn1/api":52,"./asn1/base":54,"./asn1/constants":58,"./asn1/decoders":60,"./asn1/encoders":63,"bn.js":66}],52:[function(require,module,exports){ | |
var asn1 = require('../asn1'); | |
var inherits = require('inherits'); | |
var api = exports; | |
api.define = function define(name, body) { | |
return new Entity(name, body); | |
}; | |
function Entity(name, body) { | |
this.name = name; | |
this.body = body; | |
this.decoders = {}; | |
this.encoders = {}; | |
}; | |
Entity.prototype._createNamed = function createNamed(base) { | |
var named; | |
try { | |
named = require('vm').runInThisContext( | |
'(function ' + this.name + '(entity) {\n' + | |
' this._initNamed(entity);\n' + | |
'})' | |
); | |
} catch (e) { | |
named = function (entity) { | |
this._initNamed(entity); | |
}; | |
} | |
inherits(named, base); | |
named.prototype._initNamed = function initnamed(entity) { | |
base.call(this, entity); | |
}; | |
return new named(this); | |
}; | |
Entity.prototype._getDecoder = function _getDecoder(enc) { | |
enc = enc || 'der'; | |
// Lazily create decoder | |
if (!this.decoders.hasOwnProperty(enc)) | |
this.decoders[enc] = this._createNamed(asn1.decoders[enc]); | |
return this.decoders[enc]; | |
}; | |
Entity.prototype.decode = function decode(data, enc, options) { | |
return this._getDecoder(enc).decode(data, options); | |
}; | |
Entity.prototype._getEncoder = function _getEncoder(enc) { | |
enc = enc || 'der'; | |
// Lazily create encoder | |
if (!this.encoders.hasOwnProperty(enc)) | |
this.encoders[enc] = this._createNamed(asn1.encoders[enc]); | |
return this.encoders[enc]; | |
}; | |
Entity.prototype.encode = function encode(data, enc, /* internal */ reporter) { | |
return this._getEncoder(enc).encode(data, reporter); | |
}; | |
},{"../asn1":51,"inherits":154,"vm":311}],53:[function(require,module,exports){ | |
var inherits = require('inherits'); | |
var Reporter = require('../base').Reporter; | |
var Buffer = require('buffer').Buffer; | |
function DecoderBuffer(base, options) { | |
Reporter.call(this, options); | |
if (!Buffer.isBuffer(base)) { | |
this.error('Input not Buffer'); | |
return; | |
} | |
this.base = base; | |
this.offset = 0; | |
this.length = base.length; | |
} | |
inherits(DecoderBuffer, Reporter); | |
exports.DecoderBuffer = DecoderBuffer; | |
DecoderBuffer.prototype.save = function save() { | |
return { offset: this.offset, reporter: Reporter.prototype.save.call(this) }; | |
}; | |
DecoderBuffer.prototype.restore = function restore(save) { | |
// Return skipped data | |
var res = new DecoderBuffer(this.base); | |
res.offset = save.offset; | |
res.length = this.offset; | |
this.offset = save.offset; | |
Reporter.prototype.restore.call(this, save.reporter); | |
return res; | |
}; | |
DecoderBuffer.prototype.isEmpty = function isEmpty() { | |
return this.offset === this.length; | |
}; | |
DecoderBuffer.prototype.readUInt8 = function readUInt8(fail) { | |
if (this.offset + 1 <= this.length) | |
return this.base.readUInt8(this.offset++, true); | |
else | |
return this.error(fail || 'DecoderBuffer overrun'); | |
} | |
DecoderBuffer.prototype.skip = function skip(bytes, fail) { | |
if (!(this.offset + bytes <= this.length)) | |
return this.error(fail || 'DecoderBuffer overrun'); | |
var res = new DecoderBuffer(this.base); | |
// Share reporter state | |
res._reporterState = this._reporterState; | |
res.offset = this.offset; | |
res.length = this.offset + bytes; | |
this.offset += bytes; | |
return res; | |
} | |
DecoderBuffer.prototype.raw = function raw(save) { | |
return this.base.slice(save ? save.offset : this.offset, this.length); | |
} | |
function EncoderBuffer(value, reporter) { | |
if (Array.isArray(value)) { | |
this.length = 0; | |
this.value = value.map(function(item) { | |
if (!(item instanceof EncoderBuffer)) | |
item = new EncoderBuffer(item, reporter); | |
this.length += item.length; | |
return item; | |
}, this); | |
} else if (typeof value === 'number') { | |
if (!(0 <= value && value <= 0xff)) | |
return reporter.error('non-byte EncoderBuffer value'); | |
this.value = value; | |
this.length = 1; | |
} else if (typeof value === 'string') { | |
this.value = value; | |
this.length = Buffer.byteLength(value); | |
} else if (Buffer.isBuffer(value)) { | |
this.value = value; | |
this.length = value.length; | |
} else { | |
return reporter.error('Unsupported type: ' + typeof value); | |
} | |
} | |
exports.EncoderBuffer = EncoderBuffer; | |
EncoderBuffer.prototype.join = function join(out, offset) { | |
if (!out) | |
out = new Buffer(this.length); | |
if (!offset) | |
offset = 0; | |
if (this.length === 0) | |
return out; | |
if (Array.isArray(this.value)) { | |
this.value.forEach(function(item) { | |
item.join(out, offset); | |
offset += item.length; | |
}); | |
} else { | |
if (typeof this.value === 'number') | |
out[offset] = this.value; | |
else if (typeof this.value === 'string') | |
out.write(this.value, offset); | |
else if (Buffer.isBuffer(this.value)) | |
this.value.copy(out, offset); | |
offset += this.length; | |
} | |
return out; | |
}; | |
},{"../base":54,"buffer":99,"inherits":154}],54:[function(require,module,exports){ | |
var base = exports; | |
base.Reporter = require('./reporter').Reporter; | |
base.DecoderBuffer = require('./buffer').DecoderBuffer; | |
base.EncoderBuffer = require('./buffer').EncoderBuffer; | |
base.Node = require('./node'); | |
},{"./buffer":53,"./node":55,"./reporter":56}],55:[function(require,module,exports){ | |
var Reporter = require('../base').Reporter; | |
var EncoderBuffer = require('../base').EncoderBuffer; | |
var DecoderBuffer = require('../base').DecoderBuffer; | |
var assert = require('minimalistic-assert'); | |
// Supported tags | |
var tags = [ | |
'seq', 'seqof', 'set', 'setof', 'objid', 'bool', | |
'gentime', 'utctime', 'null_', 'enum', 'int', 'objDesc', | |
'bitstr', 'bmpstr', 'charstr', 'genstr', 'graphstr', 'ia5str', 'iso646str', | |
'numstr', 'octstr', 'printstr', 't61str', 'unistr', 'utf8str', 'videostr' | |
]; | |
// Public methods list | |
var methods = [ | |
'key', 'obj', 'use', 'optional', 'explicit', 'implicit', 'def', 'choice', | |
'any', 'contains' | |
].concat(tags); | |
// Overrided methods list | |
var overrided = [ | |
'_peekTag', '_decodeTag', '_use', | |
'_decodeStr', '_decodeObjid', '_decodeTime', | |
'_decodeNull', '_decodeInt', '_decodeBool', '_decodeList', | |
'_encodeComposite', '_encodeStr', '_encodeObjid', '_encodeTime', | |
'_encodeNull', '_encodeInt', '_encodeBool' | |
]; | |
function Node(enc, parent) { | |
var state = {}; | |
this._baseState = state; | |
state.enc = enc; | |
state.parent = parent || null; | |
state.children = null; | |
// State | |
state.tag = null; | |
state.args = null; | |
state.reverseArgs = null; | |
state.choice = null; | |
state.optional = false; | |
state.any = false; | |
state.obj = false; | |
state.use = null; | |
state.useDecoder = null; | |
state.key = null; | |
state['default'] = null; | |
state.explicit = null; | |
state.implicit = null; | |
state.contains = null; | |
// Should create new instance on each method | |
if (!state.parent) { | |
state.children = []; | |
this._wrap(); | |
} | |
} | |
module.exports = Node; | |
var stateProps = [ | |
'enc', 'parent', 'children', 'tag', 'args', 'reverseArgs', 'choice', | |
'optional', 'any', 'obj', 'use', 'alteredUse', 'key', 'default', 'explicit', | |
'implicit', 'contains' | |
]; | |
Node.prototype.clone = function clone() { | |
var state = this._baseState; | |
var cstate = {}; | |
stateProps.forEach(function(prop) { | |
cstate[prop] = state[prop]; | |
}); | |
var res = new this.constructor(cstate.parent); | |
res._baseState = cstate; | |
return res; | |
}; | |
Node.prototype._wrap = function wrap() { | |
var state = this._baseState; | |
methods.forEach(function(method) { | |
this[method] = function _wrappedMethod() { | |
var clone = new this.constructor(this); | |
state.children.push(clone); | |
return clone[method].apply(clone, arguments); | |
}; | |
}, this); | |
}; | |
Node.prototype._init = function init(body) { | |
var state = this._baseState; | |
assert(state.parent === null); | |
body.call(this); | |
// Filter children | |
state.children = state.children.filter(function(child) { | |
return child._baseState.parent === this; | |
}, this); | |
assert.equal(state.children.length, 1, 'Root node can have only one child'); | |
}; | |
Node.prototype._useArgs = function useArgs(args) { | |
var state = this._baseState; | |
// Filter children and args | |
var children = args.filter(function(arg) { | |
return arg instanceof this.constructor; | |
}, this); | |
args = args.filter(function(arg) { | |
return !(arg instanceof this.constructor); | |
}, this); | |
if (children.length !== 0) { | |
assert(state.children === null); | |
state.children = children; | |
// Replace parent to maintain backward link | |
children.forEach(function(child) { | |
child._baseState.parent = this; | |
}, this); | |
} | |
if (args.length !== 0) { | |
assert(state.args === null); | |
state.args = args; | |
state.reverseArgs = args.map(function(arg) { | |
if (typeof arg !== 'object' || arg.constructor !== Object) | |
return arg; | |
var res = {}; | |
Object.keys(arg).forEach(function(key) { | |
if (key == (key | 0)) | |
key |= 0; | |
var value = arg[key]; | |
res[value] = key; | |
}); | |
return res; | |
}); | |
} | |
}; | |
// | |
// Overrided methods | |
// | |
overrided.forEach(function(method) { | |
Node.prototype[method] = function _overrided() { | |
var state = this._baseState; | |
throw new Error(method + ' not implemented for encoding: ' + state.enc); | |
}; | |
}); | |
// | |
// Public methods | |
// | |
tags.forEach(function(tag) { | |
Node.prototype[tag] = function _tagMethod() { | |
var state = this._baseState; | |
var args = Array.prototype.slice.call(arguments); | |
assert(state.tag === null); | |
state.tag = tag; | |
this._useArgs(args); | |
return this; | |
}; | |
}); | |
Node.prototype.use = function use(item) { | |
assert(item); | |
var state = this._baseState; | |
assert(state.use === null); | |
state.use = item; | |
return this; | |
}; | |
Node.prototype.optional = function optional() { | |
var state = this._baseState; | |
state.optional = true; | |
return this; | |
}; | |
Node.prototype.def = function def(val) { | |
var state = this._baseState; | |
assert(state['default'] === null); | |
state['default'] = val; | |
state.optional = true; | |
return this; | |
}; | |
Node.prototype.explicit = function explicit(num) { | |
var state = this._baseState; | |
assert(state.explicit === null && state.implicit === null); | |
state.explicit = num; | |
return this; | |
}; | |
Node.prototype.implicit = function implicit(num) { | |
var state = this._baseState; | |
assert(state.explicit === null && state.implicit === null); | |
state.implicit = num; | |
return this; | |
}; | |
Node.prototype.obj = function obj() { | |
var state = this._baseState; | |
var args = Array.prototype.slice.call(arguments); | |
state.obj = true; | |
if (args.length !== 0) | |
this._useArgs(args); | |
return this; | |
}; | |
Node.prototype.key = function key(newKey) { | |
var state = this._baseState; | |
assert(state.key === null); | |
state.key = newKey; | |
return this; | |
}; | |
Node.prototype.any = function any() { | |
var state = this._baseState; | |
state.any = true; | |
return this; | |
}; | |
Node.prototype.choice = function choice(obj) { | |
var state = this._baseState; | |
assert(state.choice === null); | |
state.choice = obj; | |
this._useArgs(Object.keys(obj).map(function(key) { | |
return obj[key]; | |
})); | |
return this; | |
}; | |
Node.prototype.contains = function contains(item) { | |
var state = this._baseState; | |
assert(state.use === null); | |
state.contains = item; | |
return this; | |
}; | |
// | |
// Decoding | |
// | |
Node.prototype._decode = function decode(input, options) { | |
var state = this._baseState; | |
// Decode root node | |
if (state.parent === null) | |
return input.wrapResult(state.children[0]._decode(input, options)); | |
var result = state['default']; | |
var present = true; | |
var prevKey = null; | |
if (state.key !== null) | |
prevKey = input.enterKey(state.key); | |
// Check if tag is there | |
if (state.optional) { | |
var tag = null; | |
if (state.explicit !== null) | |
tag = state.explicit; | |
else if (state.implicit !== null) | |
tag = state.implicit; | |
else if (state.tag !== null) | |
tag = state.tag; | |
if (tag === null && !state.any) { | |
// Trial and Error | |
var save = input.save(); | |
try { | |
if (state.choice === null) | |
this._decodeGeneric(state.tag, input, options); | |
else | |
this._decodeChoice(input, options); | |
present = true; | |
} catch (e) { | |
present = false; | |
} | |
input.restore(save); | |
} else { | |
present = this._peekTag(input, tag, state.any); | |
if (input.isError(present)) | |
return present; | |
} | |
} | |
// Push object on stack | |
var prevObj; | |
if (state.obj && present) | |
prevObj = input.enterObject(); | |
if (present) { | |
// Unwrap explicit values | |
if (state.explicit !== null) { | |
var explicit = this._decodeTag(input, state.explicit); | |
if (input.isError(explicit)) | |
return explicit; | |
input = explicit; | |
} | |
var start = input.offset; | |
// Unwrap implicit and normal values | |
if (state.use === null && state.choice === null) { | |
if (state.any) | |
var save = input.save(); | |
var body = this._decodeTag( | |
input, | |
state.implicit !== null ? state.implicit : state.tag, | |
state.any | |
); | |
if (input.isError(body)) | |
return body; | |
if (state.any) | |
result = input.raw(save); | |
else | |
input = body; | |
} | |
if (options && options.track && state.tag !== null) | |
options.track(input.path(), start, input.length, 'tagged'); | |
if (options && options.track && state.tag !== null) | |
options.track(input.path(), input.offset, input.length, 'content'); | |
// Select proper method for tag | |
if (state.any) | |
result = result; | |
else if (state.choice === null) | |
result = this._decodeGeneric(state.tag, input, options); | |
else | |
result = this._decodeChoice(input, options); | |
if (input.isError(result)) | |
return result; | |
// Decode children | |
if (!state.any && state.choice === null && state.children !== null) { | |
state.children.forEach(function decodeChildren(child) { | |
// NOTE: We are ignoring errors here, to let parser continue with other | |
// parts of encoded data | |
child._decode(input, options); | |
}); | |
} | |
// Decode contained/encoded by schema, only in bit or octet strings | |
if (state.contains && (state.tag === 'octstr' || state.tag === 'bitstr')) { | |
var data = new DecoderBuffer(result); | |
result = this._getUse(state.contains, input._reporterState.obj) | |
._decode(data, options); | |
} | |
} | |
// Pop object | |
if (state.obj && present) | |
result = input.leaveObject(prevObj); | |
// Set key | |
if (state.key !== null && (result !== null || present === true)) | |
input.leaveKey(prevKey, state.key, result); | |
else if (prevKey !== null) | |
input.exitKey(prevKey); | |
return result; | |
}; | |
Node.prototype._decodeGeneric = function decodeGeneric(tag, input, options) { | |
var state = this._baseState; | |
if (tag === 'seq' || tag === 'set') | |
return null; | |
if (tag === 'seqof' || tag === 'setof') | |
return this._decodeList(input, tag, state.args[0], options); | |
else if (/str$/.test(tag)) | |
return this._decodeStr(input, tag, options); | |
else if (tag === 'objid' && state.args) | |
return this._decodeObjid(input, state.args[0], state.args[1], options); | |
else if (tag === 'objid') | |
return this._decodeObjid(input, null, null, options); | |
else if (tag === 'gentime' || tag === 'utctime') | |
return this._decodeTime(input, tag, options); | |
else if (tag === 'null_') | |
return this._decodeNull(input, options); | |
else if (tag === 'bool') | |
return this._decodeBool(input, options); | |
else if (tag === 'objDesc') | |
return this._decodeStr(input, tag, options); | |
else if (tag === 'int' || tag === 'enum') | |
return this._decodeInt(input, state.args && state.args[0], options); | |
if (state.use !== null) { | |
return this._getUse(state.use, input._reporterState.obj) | |
._decode(input, options); | |
} else { | |
return input.error('unknown tag: ' + tag); | |
} | |
}; | |
Node.prototype._getUse = function _getUse(entity, obj) { | |
var state = this._baseState; | |
// Create altered use decoder if implicit is set | |
state.useDecoder = this._use(entity, obj); | |
assert(state.useDecoder._baseState.parent === null); | |
state.useDecoder = state.useDecoder._baseState.children[0]; | |
if (state.implicit !== state.useDecoder._baseState.implicit) { | |
state.useDecoder = state.useDecoder.clone(); | |
state.useDecoder._baseState.implicit = state.implicit; | |
} | |
return state.useDecoder; | |
}; | |
Node.prototype._decodeChoice = function decodeChoice(input, options) { | |
var state = this._baseState; | |
var result = null; | |
var match = false; | |
Object.keys(state.choice).some(function(key) { | |
var save = input.save(); | |
var node = state.choice[key]; | |
try { | |
var value = node._decode(input, options); | |
if (input.isError(value)) | |
return false; | |
result = { type: key, value: value }; | |
match = true; | |
} catch (e) { | |
input.restore(save); | |
return false; | |
} | |
return true; | |
}, this); | |
if (!match) | |
return input.error('Choice not matched'); | |
return result; | |
}; | |
// | |
// Encoding | |
// | |
Node.prototype._createEncoderBuffer = function createEncoderBuffer(data) { | |
return new EncoderBuffer(data, this.reporter); | |
}; | |
Node.prototype._encode = function encode(data, reporter, parent) { | |
var state = this._baseState; | |
if (state['default'] !== null && state['default'] === data) | |
return; | |
var result = this._encodeValue(data, reporter, parent); | |
if (result === undefined) | |
return; | |
if (this._skipDefault(result, reporter, parent)) | |
return; | |
return result; | |
}; | |
Node.prototype._encodeValue = function encode(data, reporter, parent) { | |
var state = this._baseState; | |
// Decode root node | |
if (state.parent === null) | |
return state.children[0]._encode(data, reporter || new Reporter()); | |
var result = null; | |
// Set reporter to share it with a child class | |
this.reporter = reporter; | |
// Check if data is there | |
if (state.optional && data === undefined) { | |
if (state['default'] !== null) | |
data = state['default'] | |
else | |
return; | |
} | |
// Encode children first | |
var content = null; | |
var primitive = false; | |
if (state.any) { | |
// Anything that was given is translated to buffer | |
result = this._createEncoderBuffer(data); | |
} else if (state.choice) { | |
result = this._encodeChoice(data, reporter); | |
} else if (state.contains) { | |
content = this._getUse(state.contains, parent)._encode(data, reporter); | |
primitive = true; | |
} else if (state.children) { | |
content = state.children.map(function(child) { | |
if (child._baseState.tag === 'null_') | |
return child._encode(null, reporter, data); | |
if (child._baseState.key === null) | |
return reporter.error('Child should have a key'); | |
var prevKey = reporter.enterKey(child._baseState.key); | |
if (typeof data !== 'object') | |
return reporter.error('Child expected, but input is not object'); | |
var res = child._encode(data[child._baseState.key], reporter, data); | |
reporter.leaveKey(prevKey); | |
return res; | |
}, this).filter(function(child) { | |
return child; | |
}); | |
content = this._createEncoderBuffer(content); | |
} else { | |
if (state.tag === 'seqof' || state.tag === 'setof') { | |
// TODO(indutny): this should be thrown on DSL level | |
if (!(state.args && state.args.length === 1)) | |
return reporter.error('Too many args for : ' + state.tag); | |
if (!Array.isArray(data)) | |
return reporter.error('seqof/setof, but data is not Array'); | |
var child = this.clone(); | |
child._baseState.implicit = null; | |
content = this._createEncoderBuffer(data.map(function(item) { | |
var state = this._baseState; | |
return this._getUse(state.args[0], data)._encode(item, reporter); | |
}, child)); | |
} else if (state.use !== null) { | |
result = this._getUse(state.use, parent)._encode(data, reporter); | |
} else { | |
content = this._encodePrimitive(state.tag, data); | |
primitive = true; | |
} | |
} | |
// Encode data itself | |
var result; | |
if (!state.any && state.choice === null) { | |
var tag = state.implicit !== null ? state.implicit : state.tag; | |
var cls = state.implicit === null ? 'universal' : 'context'; | |
if (tag === null) { | |
if (state.use === null) | |
reporter.error('Tag could be omitted only for .use()'); | |
} else { | |
if (state.use === null) | |
result = this._encodeComposite(tag, primitive, cls, content); | |
} | |
} | |
// Wrap in explicit | |
if (state.explicit !== null) | |
result = this._encodeComposite(state.explicit, false, 'context', result); | |
return result; | |
}; | |
Node.prototype._encodeChoice = function encodeChoice(data, reporter) { | |
var state = this._baseState; | |
var node = state.choice[data.type]; | |
if (!node) { | |
assert( | |
false, | |
data.type + ' not found in ' + | |
JSON.stringify(Object.keys(state.choice))); | |
} | |
return node._encode(data.value, reporter); | |
}; | |
Node.prototype._encodePrimitive = function encodePrimitive(tag, data) { | |
var state = this._baseState; | |
if (/str$/.test(tag)) | |
return this._encodeStr(data, tag); | |
else if (tag === 'objid' && state.args) | |
return this._encodeObjid(data, state.reverseArgs[0], state.args[1]); | |
else if (tag === 'objid') | |
return this._encodeObjid(data, null, null); | |
else if (tag === 'gentime' || tag === 'utctime') | |
return this._encodeTime(data, tag); | |
else if (tag === 'null_') | |
return this._encodeNull(); | |
else if (tag === 'int' || tag === 'enum') | |
return this._encodeInt(data, state.args && state.reverseArgs[0]); | |
else if (tag === 'bool') | |
return this._encodeBool(data); | |
else if (tag === 'objDesc') | |
return this._encodeStr(data, tag); | |
else | |
throw new Error('Unsupported tag: ' + tag); | |
}; | |
Node.prototype._isNumstr = function isNumstr(str) { | |
return /^[0-9 ]*$/.test(str); | |
}; | |
Node.prototype._isPrintstr = function isPrintstr(str) { | |
return /^[A-Za-z0-9 '\(\)\+,\-\.\/:=\?]*$/.test(str); | |
}; | |
},{"../base":54,"minimalistic-assert":161}],56:[function(require,module,exports){ | |
var inherits = require('inherits'); | |
function Reporter(options) { | |
this._reporterState = { | |
obj: null, | |
path: [], | |
options: options || {}, | |
errors: [] | |
}; | |
} | |
exports.Reporter = Reporter; | |
Reporter.prototype.isError = function isError(obj) { | |
return obj instanceof ReporterError; | |
}; | |
Reporter.prototype.save = function save() { | |
var state = this._reporterState; | |
return { obj: state.obj, pathLen: state.path.length }; | |
}; | |
Reporter.prototype.restore = function restore(data) { | |
var state = this._reporterState; | |
state.obj = data.obj; | |
state.path = state.path.slice(0, data.pathLen); | |
}; | |
Reporter.prototype.enterKey = function enterKey(key) { | |
return this._reporterState.path.push(key); | |
}; | |
Reporter.prototype.exitKey = function exitKey(index) { | |
var state = this._reporterState; | |
state.path = state.path.slice(0, index - 1); | |
}; | |
Reporter.prototype.leaveKey = function leaveKey(index, key, value) { | |
var state = this._reporterState; | |
this.exitKey(index); | |
if (state.obj !== null) | |
state.obj[key] = value; | |
}; | |
Reporter.prototype.path = function path() { | |
return this._reporterState.path.join('/'); | |
}; | |
Reporter.prototype.enterObject = function enterObject() { | |
var state = this._reporterState; | |
var prev = state.obj; | |
state.obj = {}; | |
return prev; | |
}; | |
Reporter.prototype.leaveObject = function leaveObject(prev) { | |
var state = this._reporterState; | |
var now = state.obj; | |
state.obj = prev; | |
return now; | |
}; | |
Reporter.prototype.error = function error(msg) { | |
var err; | |
var state = this._reporterState; | |
var inherited = msg instanceof ReporterError; | |
if (inherited) { | |
err = msg; | |
} else { | |
err = new ReporterError(state.path.map(function(elem) { | |
return '[' + JSON.stringify(elem) + ']'; | |
}).join(''), msg.message || msg, msg.stack); | |
} | |
if (!state.options.partial) | |
throw err; | |
if (!inherited) | |
state.errors.push(err); | |
return err; | |
}; | |
Reporter.prototype.wrapResult = function wrapResult(result) { | |
var state = this._reporterState; | |
if (!state.options.partial) | |
return result; | |
return { | |
result: this.isError(result) ? null : result, | |
errors: state.errors | |
}; | |
}; | |
function ReporterError(path, msg) { | |
this.path = path; | |
this.rethrow(msg); | |
}; | |
inherits(ReporterError, Error); | |
ReporterError.prototype.rethrow = function rethrow(msg) { | |
this.message = msg + ' at: ' + (this.path || '(shallow)'); | |
if (Error.captureStackTrace) | |
Error.captureStackTrace(this, ReporterError); | |
if (!this.stack) { | |
try { | |
// IE only adds stack when thrown | |
throw new Error(this.message); | |
} catch (e) { | |
this.stack = e.stack; | |
} | |
} | |
return this; | |
}; | |
},{"inherits":154}],57:[function(require,module,exports){ | |
var constants = require('../constants'); | |
exports.tagClass = { | |
0: 'universal', | |
1: 'application', | |
2: 'context', | |
3: 'private' | |
}; | |
exports.tagClassByName = constants._reverse(exports.tagClass); | |
exports.tag = { | |
0x00: 'end', | |
0x01: 'bool', | |
0x02: 'int', | |
0x03: 'bitstr', | |
0x04: 'octstr', | |
0x05: 'null_', | |
0x06: 'objid', | |
0x07: 'objDesc', | |
0x08: 'external', | |
0x09: 'real', | |
0x0a: 'enum', | |
0x0b: 'embed', | |
0x0c: 'utf8str', | |
0x0d: 'relativeOid', | |
0x10: 'seq', | |
0x11: 'set', | |
0x12: 'numstr', | |
0x13: 'printstr', | |
0x14: 't61str', | |
0x15: 'videostr', | |
0x16: 'ia5str', | |
0x17: 'utctime', | |
0x18: 'gentime', | |
0x19: 'graphstr', | |
0x1a: 'iso646str', | |
0x1b: 'genstr', | |
0x1c: 'unistr', | |
0x1d: 'charstr', | |
0x1e: 'bmpstr' | |
}; | |
exports.tagByName = constants._reverse(exports.tag); | |
},{"../constants":58}],58:[function(require,module,exports){ | |
var constants = exports; | |
// Helper | |
constants._reverse = function reverse(map) { | |
var res = {}; | |
Object.keys(map).forEach(function(key) { | |
// Convert key to integer if it is stringified | |
if ((key | 0) == key) | |
key = key | 0; | |
var value = map[key]; | |
res[value] = key; | |
}); | |
return res; | |
}; | |
constants.der = require('./der'); | |
},{"./der":57}],59:[function(require,module,exports){ | |
var inherits = require('inherits'); | |
var asn1 = require('../../asn1'); | |
var base = asn1.base; | |
var bignum = asn1.bignum; | |
// Import DER constants | |
var der = asn1.constants.der; | |
function DERDecoder(entity) { | |
this.enc = 'der'; | |
this.name = entity.name; | |
this.entity = entity; | |
// Construct base tree | |
this.tree = new DERNode(); | |
this.tree._init(entity.body); | |
}; | |
module.exports = DERDecoder; | |
DERDecoder.prototype.decode = function decode(data, options) { | |
if (!(data instanceof base.DecoderBuffer)) | |
data = new base.DecoderBuffer(data, options); | |
return this.tree._decode(data, options); | |
}; | |
// Tree methods | |
function DERNode(parent) { | |
base.Node.call(this, 'der', parent); | |
} | |
inherits(DERNode, base.Node); | |
DERNode.prototype._peekTag = function peekTag(buffer, tag, any) { | |
if (buffer.isEmpty()) | |
return false; | |
var state = buffer.save(); | |
var decodedTag = derDecodeTag(buffer, 'Failed to peek tag: "' + tag + '"'); | |
if (buffer.isError(decodedTag)) | |
return decodedTag; | |
buffer.restore(state); | |
return decodedTag.tag === tag || decodedTag.tagStr === tag || | |
(decodedTag.tagStr + 'of') === tag || any; | |
}; | |
DERNode.prototype._decodeTag = function decodeTag(buffer, tag, any) { | |
var decodedTag = derDecodeTag(buffer, | |
'Failed to decode tag of "' + tag + '"'); | |
if (buffer.isError(decodedTag)) | |
return decodedTag; | |
var len = derDecodeLen(buffer, | |
decodedTag.primitive, | |
'Failed to get length of "' + tag + '"'); | |
// Failure | |
if (buffer.isError(len)) | |
return len; | |
if (!any && | |
decodedTag.tag !== tag && | |
decodedTag.tagStr !== tag && | |
decodedTag.tagStr + 'of' !== tag) { | |
return buffer.error('Failed to match tag: "' + tag + '"'); | |
} | |
if (decodedTag.primitive || len !== null) | |
return buffer.skip(len, 'Failed to match body of: "' + tag + '"'); | |
// Indefinite length... find END tag | |
var state = buffer.save(); | |
var res = this._skipUntilEnd( | |
buffer, | |
'Failed to skip indefinite length body: "' + this.tag + '"'); | |
if (buffer.isError(res)) | |
return res; | |
len = buffer.offset - state.offset; | |
buffer.restore(state); | |
return buffer.skip(len, 'Failed to match body of: "' + tag + '"'); | |
}; | |
DERNode.prototype._skipUntilEnd = function skipUntilEnd(buffer, fail) { | |
while (true) { | |
var tag = derDecodeTag(buffer, fail); | |
if (buffer.isError(tag)) | |
return tag; | |
var len = derDecodeLen(buffer, tag.primitive, fail); | |
if (buffer.isError(len)) | |
return len; | |
var res; | |
if (tag.primitive || len !== null) | |
res = buffer.skip(len) | |
else | |
res = this._skipUntilEnd(buffer, fail); | |
// Failure | |
if (buffer.isError(res)) | |
return res; | |
if (tag.tagStr === 'end') | |
break; | |
} | |
}; | |
DERNode.prototype._decodeList = function decodeList(buffer, tag, decoder, | |
options) { | |
var result = []; | |
while (!buffer.isEmpty()) { | |
var possibleEnd = this._peekTag(buffer, 'end'); | |
if (buffer.isError(possibleEnd)) | |
return possibleEnd; | |
var res = decoder.decode(buffer, 'der', options); | |
if (buffer.isError(res) && possibleEnd) | |
break; | |
result.push(res); | |
} | |
return result; | |
}; | |
DERNode.prototype._decodeStr = function decodeStr(buffer, tag) { | |
if (tag === 'bitstr') { | |
var unused = buffer.readUInt8(); | |
if (buffer.isError(unused)) | |
return unused; | |
return { unused: unused, data: buffer.raw() }; | |
} else if (tag === 'bmpstr') { | |
var raw = buffer.raw(); | |
if (raw.length % 2 === 1) | |
return buffer.error('Decoding of string type: bmpstr length mismatch'); | |
var str = ''; | |
for (var i = 0; i < raw.length / 2; i++) { | |
str += String.fromCharCode(raw.readUInt16BE(i * 2)); | |
} | |
return str; | |
} else if (tag === 'numstr') { | |
var numstr = buffer.raw().toString('ascii'); | |
if (!this._isNumstr(numstr)) { | |
return buffer.error('Decoding of string type: ' + | |
'numstr unsupported characters'); | |
} | |
return numstr; | |
} else if (tag === 'octstr') { | |
return buffer.raw(); | |
} else if (tag === 'objDesc') { | |
return buffer.raw(); | |
} else if (tag === 'printstr') { | |
var printstr = buffer.raw().toString('ascii'); | |
if (!this._isPrintstr(printstr)) { | |
return buffer.error('Decoding of string type: ' + | |
'printstr unsupported characters'); | |
} | |
return printstr; | |
} else if (/str$/.test(tag)) { | |
return buffer.raw().toString(); | |
} else { | |
return buffer.error('Decoding of string type: ' + tag + ' unsupported'); | |
} | |
}; | |
DERNode.prototype._decodeObjid = function decodeObjid(buffer, values, relative) { | |
var result; | |
var identifiers = []; | |
var ident = 0; | |
while (!buffer.isEmpty()) { | |
var subident = buffer.readUInt8(); | |
ident <<= 7; | |
ident |= subident & 0x7f; | |
if ((subident & 0x80) === 0) { | |
identifiers.push(ident); | |
ident = 0; | |
} | |
} | |
if (subident & 0x80) | |
identifiers.push(ident); | |
var first = (identifiers[0] / 40) | 0; | |
var second = identifiers[0] % 40; | |
if (relative) | |
result = identifiers; | |
else | |
result = [first, second].concat(identifiers.slice(1)); | |
if (values) { | |
var tmp = values[result.join(' ')]; | |
if (tmp === undefined) | |
tmp = values[result.join('.')]; | |
if (tmp !== undefined) | |
result = tmp; | |
} | |
return result; | |
}; | |
DERNode.prototype._decodeTime = function decodeTime(buffer, tag) { | |
var str = buffer.raw().toString(); | |
if (tag === 'gentime') { | |
var year = str.slice(0, 4) | 0; | |
var mon = str.slice(4, 6) | 0; | |
var day = str.slice(6, 8) | 0; | |
var hour = str.slice(8, 10) | 0; | |
var min = str.slice(10, 12) | 0; | |
var sec = str.slice(12, 14) | 0; | |
} else if (tag === 'utctime') { | |
var year = str.slice(0, 2) | 0; | |
var mon = str.slice(2, 4) | 0; | |
var day = str.slice(4, 6) | 0; | |
var hour = str.slice(6, 8) | 0; | |
var min = str.slice(8, 10) | 0; | |
var sec = str.slice(10, 12) | 0; | |
if (year < 70) | |
year = 2000 + year; | |
else | |
year = 1900 + year; | |
} else { | |
return buffer.error('Decoding ' + tag + ' time is not supported yet'); | |
} | |
return Date.UTC(year, mon - 1, day, hour, min, sec, 0); | |
}; | |
DERNode.prototype._decodeNull = function decodeNull(buffer) { | |
return null; | |
}; | |
DERNode.prototype._decodeBool = function decodeBool(buffer) { | |
var res = buffer.readUInt8(); | |
if (buffer.isError(res)) | |
return res; | |
else | |
return res !== 0; | |
}; | |
DERNode.prototype._decodeInt = function decodeInt(buffer, values) { | |
// Bigint, return as it is (assume big endian) | |
var raw = buffer.raw(); | |
var res = new bignum(raw); | |
if (values) | |
res = values[res.toString(10)] || res; | |
return res; | |
}; | |
DERNode.prototype._use = function use(entity, obj) { | |
if (typeof entity === 'function') | |
entity = entity(obj); | |
return entity._getDecoder('der').tree; | |
}; | |
// Utility methods | |
function derDecodeTag(buf, fail) { | |
var tag = buf.readUInt8(fail); | |
if (buf.isError(tag)) | |
return tag; | |
var cls = der.tagClass[tag >> 6]; | |
var primitive = (tag & 0x20) === 0; | |
// Multi-octet tag - load | |
if ((tag & 0x1f) === 0x1f) { | |
var oct = tag; | |
tag = 0; | |
while ((oct & 0x80) === 0x80) { | |
oct = buf.readUInt8(fail); | |
if (buf.isError(oct)) | |
return oct; | |
tag <<= 7; | |
tag |= oct & 0x7f; | |
} | |
} else { | |
tag &= 0x1f; | |
} | |
var tagStr = der.tag[tag]; | |
return { | |
cls: cls, | |
primitive: primitive, | |
tag: tag, | |
tagStr: tagStr | |
}; | |
} | |
function derDecodeLen(buf, primitive, fail) { | |
var len = buf.readUInt8(fail); | |
if (buf.isError(len)) | |
return len; | |
// Indefinite form | |
if (!primitive && len === 0x80) | |
return null; | |
// Definite form | |
if ((len & 0x80) === 0) { | |
// Short form | |
return len; | |
} | |
// Long form | |
var num = len & 0x7f; | |
if (num > 4) | |
return buf.error('length octect is too long'); | |
len = 0; | |
for (var i = 0; i < num; i++) { | |
len <<= 8; | |
var j = buf.readUInt8(fail); | |
if (buf.isError(j)) | |
return j; | |
len |= j; | |
} | |
return len; | |
} | |
},{"../../asn1":51,"inherits":154}],60:[function(require,module,exports){ | |
var decoders = exports; | |
decoders.der = require('./der'); | |
decoders.pem = require('./pem'); | |
},{"./der":59,"./pem":61}],61:[function(require,module,exports){ | |
var inherits = require('inherits'); | |
var Buffer = require('buffer').Buffer; | |
var DERDecoder = require('./der'); | |
function PEMDecoder(entity) { | |
DERDecoder.call(this, entity); | |
this.enc = 'pem'; | |
}; | |
inherits(PEMDecoder, DERDecoder); | |
module.exports = PEMDecoder; | |
PEMDecoder.prototype.decode = function decode(data, options) { | |
var lines = data.toString().split(/[\r\n]+/g); | |
var label = options.label.toUpperCase(); | |
var re = /^-----(BEGIN|END) ([^-]+)-----$/; | |
var start = -1; | |
var end = -1; | |
for (var i = 0; i < lines.length; i++) { | |
var match = lines[i].match(re); | |
if (match === null) | |
continue; | |
if (match[2] !== label) | |
continue; | |
if (start === -1) { | |
if (match[1] !== 'BEGIN') | |
break; | |
start = i; | |
} else { | |
if (match[1] !== 'END') | |
break; | |
end = i; | |
break; | |
} | |
} | |
if (start === -1 || end === -1) | |
throw new Error('PEM section not found for: ' + label); | |
var base64 = lines.slice(start + 1, end).join(''); | |
// Remove excessive symbols | |
base64.replace(/[^a-z0-9\+\/=]+/gi, ''); | |
var input = new Buffer(base64, 'base64'); | |
return DERDecoder.prototype.decode.call(this, input, options); | |
}; | |
},{"./der":59,"buffer":99,"inherits":154}],62:[function(require,module,exports){ | |
var inherits = require('inherits'); | |
var Buffer = require('buffer').Buffer; | |
var asn1 = require('../../asn1'); | |
var base = asn1.base; | |
// Import DER constants | |
var der = asn1.constants.der; | |
function DEREncoder(entity) { | |
this.enc = 'der'; | |
this.name = entity.name; | |
this.entity = entity; | |
// Construct base tree | |
this.tree = new DERNode(); | |
this.tree._init(entity.body); | |
}; | |
module.exports = DEREncoder; | |
DEREncoder.prototype.encode = function encode(data, reporter) { | |
return this.tree._encode(data, reporter).join(); | |
}; | |
// Tree methods | |
function DERNode(parent) { | |
base.Node.call(this, 'der', parent); | |
} | |
inherits(DERNode, base.Node); | |
DERNode.prototype._encodeComposite = function encodeComposite(tag, | |
primitive, | |
cls, | |
content) { | |
var encodedTag = encodeTag(tag, primitive, cls, this.reporter); | |
// Short form | |
if (content.length < 0x80) { | |
var header = new Buffer(2); | |
header[0] = encodedTag; | |
header[1] = content.length; | |
return this._createEncoderBuffer([ header, content ]); | |
} | |
// Long form | |
// Count octets required to store length | |
var lenOctets = 1; | |
for (var i = content.length; i >= 0x100; i >>= 8) | |
lenOctets++; | |
var header = new Buffer(1 + 1 + lenOctets); | |
header[0] = encodedTag; | |
header[1] = 0x80 | lenOctets; | |
for (var i = 1 + lenOctets, j = content.length; j > 0; i--, j >>= 8) | |
header[i] = j & 0xff; | |
return this._createEncoderBuffer([ header, content ]); | |
}; | |
DERNode.prototype._encodeStr = function encodeStr(str, tag) { | |
if (tag === 'bitstr') { | |
return this._createEncoderBuffer([ str.unused | 0, str.data ]); | |
} else if (tag === 'bmpstr') { | |
var buf = new Buffer(str.length * 2); | |
for (var i = 0; i < str.length; i++) { | |
buf.writeUInt16BE(str.charCodeAt(i), i * 2); | |
} | |
return this._createEncoderBuffer(buf); | |
} else if (tag === 'numstr') { | |
if (!this._isNumstr(str)) { | |
return this.reporter.error('Encoding of string type: numstr supports ' + | |
'only digits and space'); | |
} | |
return this._createEncoderBuffer(str); | |
} else if (tag === 'printstr') { | |
if (!this._isPrintstr(str)) { | |
return this.reporter.error('Encoding of string type: printstr supports ' + | |
'only latin upper and lower case letters, ' + | |
'digits, space, apostrophe, left and rigth ' + | |
'parenthesis, plus sign, comma, hyphen, ' + | |
'dot, slash, colon, equal sign, ' + | |
'question mark'); | |
} | |
return this._createEncoderBuffer(str); | |
} else if (/str$/.test(tag)) { | |
return this._createEncoderBuffer(str); | |
} else if (tag === 'objDesc') { | |
return this._createEncoderBuffer(str); | |
} else { | |
return this.reporter.error('Encoding of string type: ' + tag + | |
' unsupported'); | |
} | |
}; | |
DERNode.prototype._encodeObjid = function encodeObjid(id, values, relative) { | |
if (typeof id === 'string') { | |
if (!values) | |
return this.reporter.error('string objid given, but no values map found'); | |
if (!values.hasOwnProperty(id)) | |
return this.reporter.error('objid not found in values map'); | |
id = values[id].split(/[\s\.]+/g); | |
for (var i = 0; i < id.length; i++) | |
id[i] |= 0; | |
} else if (Array.isArray(id)) { | |
id = id.slice(); | |
for (var i = 0; i < id.length; i++) | |
id[i] |= 0; | |
} | |
if (!Array.isArray(id)) { | |
return this.reporter.error('objid() should be either array or string, ' + | |
'got: ' + JSON.stringify(id)); | |
} | |
if (!relative) { | |
if (id[1] >= 40) | |
return this.reporter.error('Second objid identifier OOB'); | |
id.splice(0, 2, id[0] * 40 + id[1]); | |
} | |
// Count number of octets | |
var size = 0; | |
for (var i = 0; i < id.length; i++) { | |
var ident = id[i]; | |
for (size++; ident >= 0x80; ident >>= 7) | |
size++; | |
} | |
var objid = new Buffer(size); | |
var offset = objid.length - 1; | |
for (var i = id.length - 1; i >= 0; i--) { | |
var ident = id[i]; | |
objid[offset--] = ident & 0x7f; | |
while ((ident >>= 7) > 0) | |
objid[offset--] = 0x80 | (ident & 0x7f); | |
} | |
return this._createEncoderBuffer(objid); | |
}; | |
function two(num) { | |
if (num < 10) | |
return '0' + num; | |
else | |
return num; | |
} | |
DERNode.prototype._encodeTime = function encodeTime(time, tag) { | |
var str; | |
var date = new Date(time); | |
if (tag === 'gentime') { | |
str = [ | |
two(date.getFullYear()), | |
two(date.getUTCMonth() + 1), | |
two(date.getUTCDate()), | |
two(date.getUTCHours()), | |
two(date.getUTCMinutes()), | |
two(date.getUTCSeconds()), | |
'Z' | |
].join(''); | |
} else if (tag === 'utctime') { | |
str = [ | |
two(date.getFullYear() % 100), | |
two(date.getUTCMonth() + 1), | |
two(date.getUTCDate()), | |
two(date.getUTCHours()), | |
two(date.getUTCMinutes()), | |
two(date.getUTCSeconds()), | |
'Z' | |
].join(''); | |
} else { | |
this.reporter.error('Encoding ' + tag + ' time is not supported yet'); | |
} | |
return this._encodeStr(str, 'octstr'); | |
}; | |
DERNode.prototype._encodeNull = function encodeNull() { | |
return this._createEncoderBuffer(''); | |
}; | |
DERNode.prototype._encodeInt = function encodeInt(num, values) { | |
if (typeof num === 'string') { | |
if (!values) | |
return this.reporter.error('String int or enum given, but no values map'); | |
if (!values.hasOwnProperty(num)) { | |
return this.reporter.error('Values map doesn\'t contain: ' + | |
JSON.stringify(num)); | |
} | |
num = values[num]; | |
} | |
// Bignum, assume big endian | |
if (typeof num !== 'number' && !Buffer.isBuffer(num)) { | |
var numArray = num.toArray(); | |
if (!num.sign && numArray[0] & 0x80) { | |
numArray.unshift(0); | |
} | |
num = new Buffer(numArray); | |
} | |
if (Buffer.isBuffer(num)) { | |
var size = num.length; | |
if (num.length === 0) | |
size++; | |
var out = new Buffer(size); | |
num.copy(out); | |
if (num.length === 0) | |
out[0] = 0 | |
return this._createEncoderBuffer(out); | |
} | |
if (num < 0x80) | |
return this._createEncoderBuffer(num); | |
if (num < 0x100) | |
return this._createEncoderBuffer([0, num]); | |
var size = 1; | |
for (var i = num; i >= 0x100; i >>= 8) | |
size++; | |
var out = new Array(size); | |
for (var i = out.length - 1; i >= 0; i--) { | |
out[i] = num & 0xff; | |
num >>= 8; | |
} | |
if(out[0] & 0x80) { | |
out.unshift(0); | |
} | |
return this._createEncoderBuffer(new Buffer(out)); | |
}; | |
DERNode.prototype._encodeBool = function encodeBool(value) { | |
return this._createEncoderBuffer(value ? 0xff : 0); | |
}; | |
DERNode.prototype._use = function use(entity, obj) { | |
if (typeof entity === 'function') | |
entity = entity(obj); | |
return entity._getEncoder('der').tree; | |
}; | |
DERNode.prototype._skipDefault = function skipDefault(dataBuffer, reporter, parent) { | |
var state = this._baseState; | |
var i; | |
if (state['default'] === null) | |
return false; | |
var data = dataBuffer.join(); | |
if (state.defaultBuffer === undefined) | |
state.defaultBuffer = this._encodeValue(state['default'], reporter, parent).join(); | |
if (data.length !== state.defaultBuffer.length) | |
return false; | |
for (i=0; i < data.length; i++) | |
if (data[i] !== state.defaultBuffer[i]) | |
return false; | |
return true; | |
}; | |
// Utility methods | |
function encodeTag(tag, primitive, cls, reporter) { | |
var res; | |
if (tag === 'seqof') | |
tag = 'seq'; | |
else if (tag === 'setof') | |
tag = 'set'; | |
if (der.tagByName.hasOwnProperty(tag)) | |
res = der.tagByName[tag]; | |
else if (typeof tag === 'number' && (tag | 0) === tag) | |
res = tag; | |
else | |
return reporter.error('Unknown tag: ' + tag); | |
if (res >= 0x1f) | |
return reporter.error('Multi-octet tag encoding unsupported'); | |
if (!primitive) | |
res |= 0x20; | |
res |= (der.tagClassByName[cls || 'universal'] << 6); | |
return res; | |
} | |
},{"../../asn1":51,"buffer":99,"inherits":154}],63:[function(require,module,exports){ | |
var encoders = exports; | |
encoders.der = require('./der'); | |
encoders.pem = require('./pem'); | |
},{"./der":62,"./pem":64}],64:[function(require,module,exports){ | |
var inherits = require('inherits'); | |
var DEREncoder = require('./der'); | |
function PEMEncoder(entity) { | |
DEREncoder.call(this, entity); | |
this.enc = 'pem'; | |
}; | |
inherits(PEMEncoder, DEREncoder); | |
module.exports = PEMEncoder; | |
PEMEncoder.prototype.encode = function encode(data, options) { | |
var buf = DEREncoder.prototype.encode.call(this, data); | |
var p = buf.toString('base64'); | |
var out = [ '-----BEGIN ' + options.label + '-----' ]; | |
for (var i = 0; i < p.length; i += 64) | |
out.push(p.slice(i, i + 64)); | |
out.push('-----END ' + options.label + '-----'); | |
return out.join('\n'); | |
}; | |
},{"./der":62,"inherits":154}],65:[function(require,module,exports){ | |
'use strict' | |
exports.byteLength = byteLength | |
exports.toByteArray = toByteArray | |
exports.fromByteArray = fromByteArray | |
var lookup = [] | |
var revLookup = [] | |
var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array | |
var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' | |
for (var i = 0, len = code.length; i < len; ++i) { | |
lookup[i] = code[i] | |
revLookup[code.charCodeAt(i)] = i | |
} | |
// Support decoding URL-safe base64 strings, as Node.js does. | |
// See: https://en.wikipedia.org/wiki/Base64#URL_applications | |
revLookup['-'.charCodeAt(0)] = 62 | |
revLookup['_'.charCodeAt(0)] = 63 | |
function getLens (b64) { | |
var len = b64.length | |
if (len % 4 > 0) { | |
throw new Error('Invalid string. Length must be a multiple of 4') | |
} | |
// Trim off extra bytes after placeholder bytes are found | |
// See: https://github.com/beatgammit/base64-js/issues/42 | |
var validLen = b64.indexOf('=') | |
if (validLen === -1) validLen = len | |
var placeHoldersLen = validLen === len | |
? 0 | |
: 4 - (validLen % 4) | |
return [validLen, placeHoldersLen] | |
} | |
// base64 is 4/3 + up to two characters of the original data | |
function byteLength (b64) { | |
var lens = getLens(b64) | |
var validLen = lens[0] | |
var placeHoldersLen = lens[1] | |
return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen | |
} | |
function _byteLength (b64, validLen, placeHoldersLen) { | |
return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen | |
} | |
function toByteArray (b64) { | |
var tmp | |
var lens = getLens(b64) | |
var validLen = lens[0] | |
var placeHoldersLen = lens[1] | |
var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen)) | |
var curByte = 0 | |
// if there are placeholders, only get up to the last complete 4 chars | |
var len = placeHoldersLen > 0 | |
? validLen - 4 | |
: validLen | |
for (var i = 0; i < len; i += 4) { | |
tmp = | |
(revLookup[b64.charCodeAt(i)] << 18) | | |
(revLookup[b64.charCodeAt(i + 1)] << 12) | | |
(revLookup[b64.charCodeAt(i + 2)] << 6) | | |
revLookup[b64.charCodeAt(i + 3)] | |
arr[curByte++] = (tmp >> 16) & 0xFF | |
arr[curByte++] = (tmp >> 8) & 0xFF | |
arr[curByte++] = tmp & 0xFF | |
} | |
if (placeHoldersLen === 2) { | |
tmp = | |
(revLookup[b64.charCodeAt(i)] << 2) | | |
(revLookup[b64.charCodeAt(i + 1)] >> 4) | |
arr[curByte++] = tmp & 0xFF | |
} | |
if (placeHoldersLen === 1) { | |
tmp = | |
(revLookup[b64.charCodeAt(i)] << 10) | | |
(revLookup[b64.charCodeAt(i + 1)] << 4) | | |
(revLookup[b64.charCodeAt(i + 2)] >> 2) | |
arr[curByte++] = (tmp >> 8) & 0xFF | |
arr[curByte++] = tmp & 0xFF | |
} | |
return arr | |
} | |
function tripletToBase64 (num) { | |
return lookup[num >> 18 & 0x3F] + | |
lookup[num >> 12 & 0x3F] + | |
lookup[num >> 6 & 0x3F] + | |
lookup[num & 0x3F] | |
} | |
function encodeChunk (uint8, start, end) { | |
var tmp | |
var output = [] | |
for (var i = start; i < end; i += 3) { | |
tmp = | |
((uint8[i] << 16) & 0xFF0000) + | |
((uint8[i + 1] << 8) & 0xFF00) + | |
(uint8[i + 2] & 0xFF) | |
output.push(tripletToBase64(tmp)) | |
} | |
return output.join('') | |
} | |
function fromByteArray (uint8) { | |
var tmp | |
var len = uint8.length | |
var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes | |
var parts = [] | |
var maxChunkLength = 16383 // must be multiple of 3 | |
// go through the array every three bytes, we'll deal with trailing stuff later | |
for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { | |
parts.push(encodeChunk( | |
uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength) | |
)) | |
} | |
// pad the end with zeros, but make sure to not forget the extra bytes | |
if (extraBytes === 1) { | |
tmp = uint8[len - 1] | |
parts.push( | |
lookup[tmp >> 2] + | |
lookup[(tmp << 4) & 0x3F] + | |
'==' | |
) | |
} else if (extraBytes === 2) { | |
tmp = (uint8[len - 2] << 8) + uint8[len - 1] | |
parts.push( | |
lookup[tmp >> 10] + | |
lookup[(tmp >> 4) & 0x3F] + | |
lookup[(tmp << 2) & 0x3F] + | |
'=' | |
) | |
} | |
return parts.join('') | |
} | |
},{}],66:[function(require,module,exports){ | |
(function (module, exports) { | |
'use strict'; | |
// Utils | |
function assert (val, msg) { | |
if (!val) throw new Error(msg || 'Assertion failed'); | |
} | |
// Could use `inherits` module, but don't want to move from single file | |
// architecture yet. | |
function inherits (ctor, superCtor) { | |
ctor.super_ = superCtor; | |
var TempCtor = function () {}; | |
TempCtor.prototype = superCtor.prototype; | |
ctor.prototype = new TempCtor(); | |
ctor.prototype.constructor = ctor; | |
} | |
// BN | |
function BN (number, base, endian) { | |
if (BN.isBN(number)) { | |
return number; | |
} | |
this.negative = 0; | |
this.words = null; | |
this.length = 0; | |
// Reduction context | |
this.red = null; | |
if (number !== null) { | |
if (base === 'le' || base === 'be') { | |
endian = base; | |
base = 10; | |
} | |
this._init(number || 0, base || 10, endian || 'be'); | |
} | |
} | |
if (typeof module === 'object') { | |
module.exports = BN; | |
} else { | |
exports.BN = BN; | |
} | |
BN.BN = BN; | |
BN.wordSize = 26; | |
var Buffer; | |
try { | |
Buffer = require('buffer').Buffer; | |
} catch (e) { | |
} | |
BN.isBN = function isBN (num) { | |
if (num instanceof BN) { | |
return true; | |
} | |
return num !== null && typeof num === 'object' && | |
num.constructor.wordSize === BN.wordSize && Array.isArray(num.words); | |
}; | |
BN.max = function max (left, right) { | |
if (left.cmp(right) > 0) return left; | |
return right; | |
}; | |
BN.min = function min (left, right) { | |
if (left.cmp(right) < 0) return left; | |
return right; | |
}; | |
BN.prototype._init = function init (number, base, endian) { | |
if (typeof number === 'number') { | |
return this._initNumber(number, base, endian); | |
} | |
if (typeof number === 'object') { | |
return this._initArray(number, base, endian); | |
} | |
if (base === 'hex') { | |
base = 16; | |
} | |
assert(base === (base | 0) && base >= 2 && base <= 36); | |
number = number.toString().replace(/\s+/g, ''); | |
var start = 0; | |
if (number[0] === '-') { | |
start++; | |
} | |
if (base === 16) { | |
this._parseHex(number, start); | |
} else { | |
this._parseBase(number, base, start); | |
} | |
if (number[0] === '-') { | |
this.negative = 1; | |
} | |
this.strip(); | |
if (endian !== 'le') return; | |
this._initArray(this.toArray(), base, endian); | |
}; | |
BN.prototype._initNumber = function _initNumber (number, base, endian) { | |
if (number < 0) { | |
this.negative = 1; | |
number = -number; | |
} | |
if (number < 0x4000000) { | |
this.words = [ number & 0x3ffffff ]; | |
this.length = 1; | |
} else if (number < 0x10000000000000) { | |
this.words = [ | |
number & 0x3ffffff, | |
(number / 0x4000000) & 0x3ffffff | |
]; | |
this.length = 2; | |
} else { | |
assert(number < 0x20000000000000); // 2 ^ 53 (unsafe) | |
this.words = [ | |
number & 0x3ffffff, | |
(number / 0x4000000) & 0x3ffffff, | |
1 | |
]; | |
this.length = 3; | |
} | |
if (endian !== 'le') return; | |
// Reverse the bytes | |
this._initArray(this.toArray(), base, endian); | |
}; | |
BN.prototype._initArray = function _initArray (number, base, endian) { | |
// Perhaps a Uint8Array | |
assert(typeof number.length === 'number'); | |
if (number.length <= 0) { | |
this.words = [ 0 ]; | |
this.length = 1; | |
return this; | |
} | |
this.length = Math.ceil(number.length / 3); | |
this.words = new Array(this.length); | |
for (var i = 0; i < this.length; i++) { | |
this.words[i] = 0; | |
} | |
var j, w; | |
var off = 0; | |
if (endian === 'be') { | |
for (i = number.length - 1, j = 0; i >= 0; i -= 3) { | |
w = number[i] | (number[i - 1] << 8) | (number[i - 2] << 16); | |
this.words[j] |= (w << off) & 0x3ffffff; | |
this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff; | |
off += 24; | |
if (off >= 26) { | |
off -= 26; | |
j++; | |
} | |
} | |
} else if (endian === 'le') { | |
for (i = 0, j = 0; i < number.length; i += 3) { | |
w = number[i] | (number[i + 1] << 8) | (number[i + 2] << 16); | |
this.words[j] |= (w << off) & 0x3ffffff; | |
this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff; | |
off += 24; | |
if (off >= 26) { | |
off -= 26; | |
j++; | |
} | |
} | |
} | |
return this.strip(); | |
}; | |
function parseHex (str, start, end) { | |
var r = 0; | |
var len = Math.min(str.length, end); | |
for (var i = start; i < len; i++) { | |
var c = str.charCodeAt(i) - 48; | |
r <<= 4; | |
// 'a' - 'f' | |
if (c >= 49 && c <= 54) { | |
r |= c - 49 + 0xa; | |
// 'A' - 'F' | |
} else if (c >= 17 && c <= 22) { | |
r |= c - 17 + 0xa; | |
// '0' - '9' | |
} else { | |
r |= c & 0xf; | |
} | |
} | |
return r; | |
} | |
BN.prototype._parseHex = function _parseHex (number, start) { | |
// Create possibly bigger array to ensure that it fits the number | |
this.length = Math.ceil((number.length - start) / 6); | |
this.words = new Array(this.length); | |
for (var i = 0; i < this.length; i++) { | |
this.words[i] = 0; | |
} | |
var j, w; | |
// Scan 24-bit chunks and add them to the number | |
var off = 0; | |
for (i = number.length - 6, j = 0; i >= start; i -= 6) { | |
w = parseHex(number, i, i + 6); | |
this.words[j] |= (w << off) & 0x3ffffff; | |
// NOTE: `0x3fffff` is intentional here, 26bits max shift + 24bit hex limb | |
this.words[j + 1] |= w >>> (26 - off) & 0x3fffff; | |
off += 24; | |
if (off >= 26) { | |
off -= 26; | |
j++; | |
} | |
} | |
if (i + 6 !== start) { | |
w = parseHex(number, start, i + 6); | |
this.words[j] |= (w << off) & 0x3ffffff; | |
this.words[j + 1] |= w >>> (26 - off) & 0x3fffff; | |
} | |
this.strip(); | |
}; | |
function parseBase (str, start, end, mul) { | |
var r = 0; | |
var len = Math.min(str.length, end); | |
for (var i = start; i < len; i++) { | |
var c = str.charCodeAt(i) - 48; | |
r *= mul; | |
// 'a' | |
if (c >= 49) { | |
r += c - 49 + 0xa; | |
// 'A' | |
} else if (c >= 17) { | |
r += c - 17 + 0xa; | |
// '0' - '9' | |
} else { | |
r += c; | |
} | |
} | |
return r; | |
} | |
BN.prototype._parseBase = function _parseBase (number, base, start) { | |
// Initialize as zero | |
this.words = [ 0 ]; | |
this.length = 1; | |
// Find length of limb in base | |
for (var limbLen = 0, limbPow = 1; limbPow <= 0x3ffffff; limbPow *= base) { | |
limbLen++; | |
} | |
limbLen--; | |
limbPow = (limbPow / base) | 0; | |
var total = number.length - start; | |
var mod = total % limbLen; | |
var end = Math.min(total, total - mod) + start; | |
var word = 0; | |
for (var i = start; i < end; i += limbLen) { | |
word = parseBase(number, i, i + limbLen, base); | |
this.imuln(limbPow); | |
if (this.words[0] + word < 0x4000000) { | |
this.words[0] += word; | |
} else { | |
this._iaddn(word); | |
} | |
} | |
if (mod !== 0) { | |
var pow = 1; | |
word = parseBase(number, i, number.length, base); | |
for (i = 0; i < mod; i++) { | |
pow *= base; | |
} | |
this.imuln(pow); | |
if (this.words[0] + word < 0x4000000) { | |
this.words[0] += word; | |
} else { | |
this._iaddn(word); | |
} | |
} | |
}; | |
BN.prototype.copy = function copy (dest) { | |
dest.words = new Array(this.length); | |
for (var i = 0; i < this.length; i++) { | |
dest.words[i] = this.words[i]; | |
} | |
dest.length = this.length; | |
dest.negative = this.negative; | |
dest.red = this.red; | |
}; | |
BN.prototype.clone = function clone () { | |
var r = new BN(null); | |
this.copy(r); | |
return r; | |
}; | |
BN.prototype._expand = function _expand (size) { | |
while (this.length < size) { | |
this.words[this.length++] = 0; | |
} | |
return this; | |
}; | |
// Remove leading `0` from `this` | |
BN.prototype.strip = function strip () { | |
while (this.length > 1 && this.words[this.length - 1] === 0) { | |
this.length--; | |
} | |
return this._normSign(); | |
}; | |
BN.prototype._normSign = function _normSign () { | |
// -0 = 0 | |
if (this.length === 1 && this.words[0] === 0) { | |
this.negative = 0; | |
} | |
return this; | |
}; | |
BN.prototype.inspect = function inspect () { | |
return (this.red ? '<BN-R: ' : '<BN: ') + this.toString(16) + '>'; | |
}; | |
/* | |
var zeros = []; | |
var groupSizes = []; | |
var groupBases = []; | |
var s = ''; | |
var i = -1; | |
while (++i < BN.wordSize) { | |
zeros[i] = s; | |
s += '0'; | |
} | |
groupSizes[0] = 0; | |
groupSizes[1] = 0; | |
groupBases[0] = 0; | |
groupBases[1] = 0; | |
var base = 2 - 1; | |
while (++base < 36 + 1) { | |
var groupSize = 0; | |
var groupBase = 1; | |
while (groupBase < (1 << BN.wordSize) / base) { | |
groupBase *= base; | |
groupSize += 1; | |
} | |
groupSizes[base] = groupSize; | |
groupBases[base] = groupBase; | |
} | |
*/ | |
var zeros = [ | |
'', | |
'0', | |
'00', | |
'000', | |
'0000', | |
'00000', | |
'000000', | |
'0000000', | |
'00000000', | |
'000000000', | |
'0000000000', | |
'00000000000', | |
'000000000000', | |
'0000000000000', | |
'00000000000000', | |
'000000000000000', | |
'0000000000000000', | |
'00000000000000000', | |
'000000000000000000', | |
'0000000000000000000', | |
'00000000000000000000', | |
'000000000000000000000', | |
'0000000000000000000000', | |
'00000000000000000000000', | |
'000000000000000000000000', | |
'0000000000000000000000000' | |
]; | |
var groupSizes = [ | |
0, 0, | |
25, 16, 12, 11, 10, 9, 8, | |
8, 7, 7, 7, 7, 6, 6, | |
6, 6, 6, 6, 6, 5, 5, | |
5, 5, 5, 5, 5, 5, 5, | |
5, 5, 5, 5, 5, 5, 5 | |
]; | |
var groupBases = [ | |
0, 0, | |
33554432, 43046721, 16777216, 48828125, 60466176, 40353607, 16777216, | |
43046721, 10000000, 19487171, 35831808, 62748517, 7529536, 11390625, | |
16777216, 24137569, 34012224, 47045881, 64000000, 4084101, 5153632, | |
6436343, 7962624, 9765625, 11881376, 14348907, 17210368, 20511149, | |
24300000, 28629151, 33554432, 39135393, 45435424, 52521875, 60466176 | |
]; | |
BN.prototype.toString = function toString (base, padding) { | |
base = base || 10; | |
padding = padding | 0 || 1; | |
var out; | |
if (base === 16 || base === 'hex') { | |
out = ''; | |
var off = 0; | |
var carry = 0; | |
for (var i = 0; i < this.length; i++) { | |
var w = this.words[i]; | |
var word = (((w << off) | carry) & 0xffffff).toString(16); | |
carry = (w >>> (24 - off)) & 0xffffff; | |
if (carry !== 0 || i !== this.length - 1) { | |
out = zeros[6 - word.length] + word + out; | |
} else { | |
out = word + out; | |
} | |
off += 2; | |
if (off >= 26) { | |
off -= 26; | |
i--; | |
} | |
} | |
if (carry !== 0) { | |
out = carry.toString(16) + out; | |
} | |
while (out.length % padding !== 0) { | |
out = '0' + out; | |
} | |
if (this.negative !== 0) { | |
out = '-' + out; | |
} | |
return out; | |
} | |
if (base === (base | 0) && base >= 2 && base <= 36) { | |
// var groupSize = Math.floor(BN.wordSize * Math.LN2 / Math.log(base)); | |
var groupSize = groupSizes[base]; | |
// var groupBase = Math.pow(base, groupSize); | |
var groupBase = groupBases[base]; | |
out = ''; | |
var c = this.clone(); | |
c.negative = 0; | |
while (!c.isZero()) { | |
var r = c.modn(groupBase).toString(base); | |
c = c.idivn(groupBase); | |
if (!c.isZero()) { | |
out = zeros[groupSize - r.length] + r + out; | |
} else { | |
out = r + out; | |
} | |
} | |
if (this.isZero()) { | |
out = '0' + out; | |
} | |
while (out.length % padding !== 0) { | |
out = '0' + out; | |
} | |
if (this.negative !== 0) { | |
out = '-' + out; | |
} | |
return out; | |
} | |
assert(false, 'Base should be between 2 and 36'); | |
}; | |
BN.prototype.toNumber = function toNumber () { | |
var ret = this.words[0]; | |
if (this.length === 2) { | |
ret += this.words[1] * 0x4000000; | |
} else if (this.length === 3 && this.words[2] === 0x01) { | |
// NOTE: at this stage it is known that the top bit is set | |
ret += 0x10000000000000 + (this.words[1] * 0x4000000); | |
} else if (this.length > 2) { | |
assert(false, 'Number can only safely store up to 53 bits'); | |
} | |
return (this.negative !== 0) ? -ret : ret; | |
}; | |
BN.prototype.toJSON = function toJSON () { | |
return this.toString(16); | |
}; | |
BN.prototype.toBuffer = function toBuffer (endian, length) { | |
assert(typeof Buffer !== 'undefined'); | |
return this.toArrayLike(Buffer, endian, length); | |
}; | |
BN.prototype.toArray = function toArray (endian, length) { | |
return this.toArrayLike(Array, endian, length); | |
}; | |
BN.prototype.toArrayLike = function toArrayLike (ArrayType, endian, length) { | |
var byteLength = this.byteLength(); | |
var reqLength = length || Math.max(1, byteLength); | |
assert(byteLength <= reqLength, 'byte array longer than desired length'); | |
assert(reqLength > 0, 'Requested array length <= 0'); | |
this.strip(); | |
var littleEndian = endian === 'le'; | |
var res = new ArrayType(reqLength); | |
var b, i; | |
var q = this.clone(); | |
if (!littleEndian) { | |
// Assume big-endian | |
for (i = 0; i < reqLength - byteLength; i++) { | |
res[i] = 0; | |
} | |
for (i = 0; !q.isZero(); i++) { | |
b = q.andln(0xff); | |
q.iushrn(8); | |
res[reqLength - i - 1] = b; | |
} | |
} else { | |
for (i = 0; !q.isZero(); i++) { | |
b = q.andln(0xff); | |
q.iushrn(8); | |
res[i] = b; | |
} | |
for (; i < reqLength; i++) { | |
res[i] = 0; | |
} | |
} | |
return res; | |
}; | |
if (Math.clz32) { | |
BN.prototype._countBits = function _countBits (w) { | |
return 32 - Math.clz32(w); | |
}; | |
} else { | |
BN.prototype._countBits = function _countBits (w) { | |
var t = w; | |
var r = 0; | |
if (t >= 0x1000) { | |
r += 13; | |
t >>>= 13; | |
} | |
if (t >= 0x40) { | |
r += 7; | |
t >>>= 7; | |
} | |
if (t >= 0x8) { | |
r += 4; | |
t >>>= 4; | |
} | |
if (t >= 0x02) { | |
r += 2; | |
t >>>= 2; | |
} | |
return r + t; | |
}; | |
} | |
BN.prototype._zeroBits = function _zeroBits (w) { | |
// Short-cut | |
if (w === 0) return 26; | |
var t = w; | |
var r = 0; | |
if ((t & 0x1fff) === 0) { | |
r += 13; | |
t >>>= 13; | |
} | |
if ((t & 0x7f) === 0) { | |
r += 7; | |
t >>>= 7; | |
} | |
if ((t & 0xf) === 0) { | |
r += 4; | |
t >>>= 4; | |
} | |
if ((t & 0x3) === 0) { | |
r += 2; | |
t >>>= 2; | |
} | |
if ((t & 0x1) === 0) { | |
r++; | |
} | |
return r; | |
}; | |
// Return number of used bits in a BN | |
BN.prototype.bitLength = function bitLength () { | |
var w = this.words[this.length - 1]; | |
var hi = this._countBits(w); | |
return (this.length - 1) * 26 + hi; | |
}; | |
function toBitArray (num) { | |
var w = new Array(num.bitLength()); | |
for (var bit = 0; bit < w.length; bit++) { | |
var off = (bit / 26) | 0; | |
var wbit = bit % 26; | |
w[bit] = (num.words[off] & (1 << wbit)) >>> wbit; | |
} | |
return w; | |
} | |
// Number of trailing zero bits | |
BN.prototype.zeroBits = function zeroBits () { | |
if (this.isZero()) return 0; | |
var r = 0; | |
for (var i = 0; i < this.length; i++) { | |
var b = this._zeroBits(this.words[i]); | |
r += b; | |
if (b !== 26) break; | |
} | |
return r; | |
}; | |
BN.prototype.byteLength = function byteLength () { | |
return Math.ceil(this.bitLength() / 8); | |
}; | |
BN.prototype.toTwos = function toTwos (width) { | |
if (this.negative !== 0) { | |
return this.abs().inotn(width).iaddn(1); | |
} | |
return this.clone(); | |
}; | |
BN.prototype.fromTwos = function fromTwos (width) { | |
if (this.testn(width - 1)) { | |
return this.notn(width).iaddn(1).ineg(); | |
} | |
return this.clone(); | |
}; | |
BN.prototype.isNeg = function isNeg () { | |
return this.negative !== 0; | |
}; | |
// Return negative clone of `this` | |
BN.prototype.neg = function neg () { | |
return this.clone().ineg(); | |
}; | |
BN.prototype.ineg = function ineg () { | |
if (!this.isZero()) { | |
this.negative ^= 1; | |
} | |
return this; | |
}; | |
// Or `num` with `this` in-place | |
BN.prototype.iuor = function iuor (num) { | |
while (this.length < num.length) { | |
this.words[this.length++] = 0; | |
} | |
for (var i = 0; i < num.length; i++) { | |
this.words[i] = this.words[i] | num.words[i]; | |
} | |
return this.strip(); | |
}; | |
BN.prototype.ior = function ior (num) { | |
assert((this.negative | num.negative) === 0); | |
return this.iuor(num); | |
}; | |
// Or `num` with `this` | |
BN.prototype.or = function or (num) { | |
if (this.length > num.length) return this.clone().ior(num); | |
return num.clone().ior(this); | |
}; | |
BN.prototype.uor = function uor (num) { | |
if (this.length > num.length) return this.clone().iuor(num); | |
return num.clone().iuor(this); | |
}; | |
// And `num` with `this` in-place | |
BN.prototype.iuand = function iuand (num) { | |
// b = min-length(num, this) | |
var b; | |
if (this.length > num.length) { | |
b = num; | |
} else { | |
b = this; | |
} | |
for (var i = 0; i < b.length; i++) { | |
this.words[i] = this.words[i] & num.words[i]; | |
} | |
this.length = b.length; | |
return this.strip(); | |
}; | |
BN.prototype.iand = function iand (num) { | |
assert((this.negative | num.negative) === 0); | |
return this.iuand(num); | |
}; | |
// And `num` with `this` | |
BN.prototype.and = function and (num) { | |
if (this.length > num.length) return this.clone().iand(num); | |
return num.clone().iand(this); | |
}; | |
BN.prototype.uand = function uand (num) { | |
if (this.length > num.length) return this.clone().iuand(num); | |
return num.clone().iuand(this); | |
}; | |
// Xor `num` with `this` in-place | |
BN.prototype.iuxor = function iuxor (num) { | |
// a.length > b.length | |
var a; | |
var b; | |
if (this.length > num.length) { | |
a = this; | |
b = num; | |
} else { | |
a = num; | |
b = this; | |
} | |
for (var i = 0; i < b.length; i++) { | |
this.words[i] = a.words[i] ^ b.words[i]; | |
} | |
if (this !== a) { | |
for (; i < a.length; i++) { | |
this.words[i] = a.words[i]; | |
} | |
} | |
this.length = a.length; | |
return this.strip(); | |
}; | |
BN.prototype.ixor = function ixor (num) { | |
assert((this.negative | num.negative) === 0); | |
return this.iuxor(num); | |
}; | |
// Xor `num` with `this` | |
BN.prototype.xor = function xor (num) { | |
if (this.length > num.length) return this.clone().ixor(num); | |
return num.clone().ixor(this); | |
}; | |
BN.prototype.uxor = function uxor (num) { | |
if (this.length > num.length) return this.clone().iuxor(num); | |
return num.clone().iuxor(this); | |
}; | |
// Not ``this`` with ``width`` bitwidth | |
BN.prototype.inotn = function inotn (width) { | |
assert(typeof width === 'number' && width >= 0); | |
var bytesNeeded = Math.ceil(width / 26) | 0; | |
var bitsLeft = width % 26; | |
// Extend the buffer with leading zeroes | |
this._expand(bytesNeeded); | |
if (bitsLeft > 0) { | |
bytesNeeded--; | |
} | |
// Handle complete words | |
for (var i = 0; i < bytesNeeded; i++) { | |
this.words[i] = ~this.words[i] & 0x3ffffff; | |
} | |
// Handle the residue | |
if (bitsLeft > 0) { | |
this.words[i] = ~this.words[i] & (0x3ffffff >> (26 - bitsLeft)); | |
} | |
// And remove leading zeroes | |
return this.strip(); | |
}; | |
BN.prototype.notn = function notn (width) { | |
return this.clone().inotn(width); | |
}; | |
// Set `bit` of `this` | |
BN.prototype.setn = function setn (bit, val) { | |
assert(typeof bit === 'number' && bit >= 0); | |
var off = (bit / 26) | 0; | |
var wbit = bit % 26; | |
this._expand(off + 1); | |
if (val) { | |
this.words[off] = this.words[off] | (1 << wbit); | |
} else { | |
this.words[off] = this.words[off] & ~(1 << wbit); | |
} | |
return this.strip(); | |
}; | |
// Add `num` to `this` in-place | |
BN.prototype.iadd = function iadd (num) { | |
var r; | |
// negative + positive | |
if (this.negative !== 0 && num.negative === 0) { | |
this.negative = 0; | |
r = this.isub(num); | |
this.negative ^= 1; | |
return this._normSign(); | |
// positive + negative | |
} else if (this.negative === 0 && num.negative !== 0) { | |
num.negative = 0; | |
r = this.isub(num); | |
num.negative = 1; | |
return r._normSign(); | |
} | |
// a.length > b.length | |
var a, b; | |
if (this.length > num.length) { | |
a = this; | |
b = num; | |
} else { | |
a = num; | |
b = this; | |
} | |
var carry = 0; | |
for (var i = 0; i < b.length; i++) { | |
r = (a.words[i] | 0) + (b.words[i] | 0) + carry; | |
this.words[i] = r & 0x3ffffff; | |
carry = r >>> 26; | |
} | |
for (; carry !== 0 && i < a.length; i++) { | |
r = (a.words[i] | 0) + carry; | |
this.words[i] = r & 0x3ffffff; | |
carry = r >>> 26; | |
} | |
this.length = a.length; | |
if (carry !== 0) { | |
this.words[this.length] = carry; | |
this.length++; | |
// Copy the rest of the words | |
} else if (a !== this) { | |
for (; i < a.length; i++) { | |
this.words[i] = a.words[i]; | |
} | |
} | |
return this; | |
}; | |
// Add `num` to `this` | |
BN.prototype.add = function add (num) { | |
var res; | |
if (num.negative !== 0 && this.negative === 0) { | |
num.negative = 0; | |
res = this.sub(num); | |
num.negative ^= 1; | |
return res; | |
} else if (num.negative === 0 && this.negative !== 0) { | |
this.negative = 0; | |
res = num.sub(this); | |
this.negative = 1; | |
return res; | |
} | |
if (this.length > num.length) return this.clone().iadd(num); | |
return num.clone().iadd(this); | |
}; | |
// Subtract `num` from `this` in-place | |
BN.prototype.isub = function isub (num) { | |
// this - (-num) = this + num | |
if (num.negative !== 0) { | |
num.negative = 0; | |
var r = this.iadd(num); | |
num.negative = 1; | |
return r._normSign(); | |
// -this - num = -(this + num) | |
} else if (this.negative !== 0) { | |
this.negative = 0; | |
this.iadd(num); | |
this.negative = 1; | |
return this._normSign(); | |
} | |
// At this point both numbers are positive | |
var cmp = this.cmp(num); | |
// Optimization - zeroify | |
if (cmp === 0) { | |
this.negative = 0; | |
this.length = 1; | |
this.words[0] = 0; | |
return this; | |
} | |
// a > b | |
var a, b; | |
if (cmp > 0) { | |
a = this; | |
b = num; | |
} else { | |
a = num; | |
b = this; | |
} | |
var carry = 0; | |
for (var i = 0; i < b.length; i++) { | |
r = (a.words[i] | 0) - (b.words[i] | 0) + carry; | |
carry = r >> 26; | |
this.words[i] = r & 0x3ffffff; | |
} | |
for (; carry !== 0 && i < a.length; i++) { | |
r = (a.words[i] | 0) + carry; | |
carry = r >> 26; | |
this.words[i] = r & 0x3ffffff; | |
} | |
// Copy rest of the words | |
if (carry === 0 && i < a.length && a !== this) { | |
for (; i < a.length; i++) { | |
this.words[i] = a.words[i]; | |
} | |
} | |
this.length = Math.max(this.length, i); | |
if (a !== this) { | |
this.negative = 1; | |
} | |
return this.strip(); | |
}; | |
// Subtract `num` from `this` | |
BN.prototype.sub = function sub (num) { | |
return this.clone().isub(num); | |
}; | |
function smallMulTo (self, num, out) { | |
out.negative = num.negative ^ self.negative; | |
var len = (self.length + num.length) | 0; | |
out.length = len; | |
len = (len - 1) | 0; | |
// Peel one iteration (compiler can't do it, because of code complexity) | |
var a = self.words[0] | 0; | |
var b = num.words[0] | 0; | |
var r = a * b; | |
var lo = r & 0x3ffffff; | |
var carry = (r / 0x4000000) | 0; | |
out.words[0] = lo; | |
for (var k = 1; k < len; k++) { | |
// Sum all words with the same `i + j = k` and accumulate `ncarry`, | |
// note that ncarry could be >= 0x3ffffff | |
var ncarry = carry >>> 26; | |
var rword = carry & 0x3ffffff; | |
var maxJ = Math.min(k, num.length - 1); | |
for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) { | |
var i = (k - j) | 0; | |
a = self.words[i] | 0; | |
b = num.words[j] | 0; | |
r = a * b + rword; | |
ncarry += (r / 0x4000000) | 0; | |
rword = r & 0x3ffffff; | |
} | |
out.words[k] = rword | 0; | |
carry = ncarry | 0; | |
} | |
if (carry !== 0) { | |
out.words[k] = carry | 0; | |
} else { | |
out.length--; | |
} | |
return out.strip(); | |
} | |
// TODO(indutny): it may be reasonable to omit it for users who don't need | |
// to work with 256-bit numbers, otherwise it gives 20% improvement for 256-bit | |
// multiplication (like elliptic secp256k1). | |
var comb10MulTo = function comb10MulTo (self, num, out) { | |
var a = self.words; | |
var b = num.words; | |
var o = out.words; | |
var c = 0; | |
var lo; | |
var mid; | |
var hi; | |
var a0 = a[0] | 0; | |
var al0 = a0 & 0x1fff; | |
var ah0 = a0 >>> 13; | |
var a1 = a[1] | 0; | |
var al1 = a1 & 0x1fff; | |
var ah1 = a1 >>> 13; | |
var a2 = a[2] | 0; | |
var al2 = a2 & 0x1fff; | |
var ah2 = a2 >>> 13; | |
var a3 = a[3] | 0; | |
var al3 = a3 & 0x1fff; | |
var ah3 = a3 >>> 13; | |
var a4 = a[4] | 0; | |
var al4 = a4 & 0x1fff; | |
var ah4 = a4 >>> 13; | |
var a5 = a[5] | 0; | |
var al5 = a5 & 0x1fff; | |
var ah5 = a5 >>> 13; | |
var a6 = a[6] | 0; | |
var al6 = a6 & 0x1fff; | |
var ah6 = a6 >>> 13; | |
var a7 = a[7] | 0; | |
var al7 = a7 & 0x1fff; | |
var ah7 = a7 >>> 13; | |
var a8 = a[8] | 0; | |
var al8 = a8 & 0x1fff; | |
var ah8 = a8 >>> 13; | |
var a9 = a[9] | 0; | |
var al9 = a9 & 0x1fff; | |
var ah9 = a9 >>> 13; | |
var b0 = b[0] | 0; | |
var bl0 = b0 & 0x1fff; | |
var bh0 = b0 >>> 13; | |
var b1 = b[1] | 0; | |
var bl1 = b1 & 0x1fff; | |
var bh1 = b1 >>> 13; | |
var b2 = b[2] | 0; | |
var bl2 = b2 & 0x1fff; | |
var bh2 = b2 >>> 13; | |
var b3 = b[3] | 0; | |
var bl3 = b3 & 0x1fff; | |
var bh3 = b3 >>> 13; | |
var b4 = b[4] | 0; | |
var bl4 = b4 & 0x1fff; | |
var bh4 = b4 >>> 13; | |
var b5 = b[5] | 0; | |
var bl5 = b5 & 0x1fff; | |
var bh5 = b5 >>> 13; | |
var b6 = b[6] | 0; | |
var bl6 = b6 & 0x1fff; | |
var bh6 = b6 >>> 13; | |
var b7 = b[7] | 0; | |
var bl7 = b7 & 0x1fff; | |
var bh7 = b7 >>> 13; | |
var b8 = b[8] | 0; | |
var bl8 = b8 & 0x1fff; | |
var bh8 = b8 >>> 13; | |
var b9 = b[9] | 0; | |
var bl9 = b9 & 0x1fff; | |
var bh9 = b9 >>> 13; | |
out.negative = self.negative ^ num.negative; | |
out.length = 19; | |
/* k = 0 */ | |
lo = Math.imul(al0, bl0); | |
mid = Math.imul(al0, bh0); | |
mid = (mid + Math.imul(ah0, bl0)) | 0; | |
hi = Math.imul(ah0, bh0); | |
var w0 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
c = (((hi + (mid >>> 13)) | 0) + (w0 >>> 26)) | 0; | |
w0 &= 0x3ffffff; | |
/* k = 1 */ | |
lo = Math.imul(al1, bl0); | |
mid = Math.imul(al1, bh0); | |
mid = (mid + Math.imul(ah1, bl0)) | 0; | |
hi = Math.imul(ah1, bh0); | |
lo = (lo + Math.imul(al0, bl1)) | 0; | |
mid = (mid + Math.imul(al0, bh1)) | 0; | |
mid = (mid + Math.imul(ah0, bl1)) | 0; | |
hi = (hi + Math.imul(ah0, bh1)) | 0; | |
var w1 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
c = (((hi + (mid >>> 13)) | 0) + (w1 >>> 26)) | 0; | |
w1 &= 0x3ffffff; | |
/* k = 2 */ | |
lo = Math.imul(al2, bl0); | |
mid = Math.imul(al2, bh0); | |
mid = (mid + Math.imul(ah2, bl0)) | 0; | |
hi = Math.imul(ah2, bh0); | |
lo = (lo + Math.imul(al1, bl1)) | 0; | |
mid = (mid + Math.imul(al1, bh1)) | 0; | |
mid = (mid + Math.imul(ah1, bl1)) | 0; | |
hi = (hi + Math.imul(ah1, bh1)) | 0; | |
lo = (lo + Math.imul(al0, bl2)) | 0; | |
mid = (mid + Math.imul(al0, bh2)) | 0; | |
mid = (mid + Math.imul(ah0, bl2)) | 0; | |
hi = (hi + Math.imul(ah0, bh2)) | 0; | |
var w2 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
c = (((hi + (mid >>> 13)) | 0) + (w2 >>> 26)) | 0; | |
w2 &= 0x3ffffff; | |
/* k = 3 */ | |
lo = Math.imul(al3, bl0); | |
mid = Math.imul(al3, bh0); | |
mid = (mid + Math.imul(ah3, bl0)) | 0; | |
hi = Math.imul(ah3, bh0); | |
lo = (lo + Math.imul(al2, bl1)) | 0; | |
mid = (mid + Math.imul(al2, bh1)) | 0; | |
mid = (mid + Math.imul(ah2, bl1)) | 0; | |
hi = (hi + Math.imul(ah2, bh1)) | 0; | |
lo = (lo + Math.imul(al1, bl2)) | 0; | |
mid = (mid + Math.imul(al1, bh2)) | 0; | |
mid = (mid + Math.imul(ah1, bl2)) | 0; | |
hi = (hi + Math.imul(ah1, bh2)) | 0; | |
lo = (lo + Math.imul(al0, bl3)) | 0; | |
mid = (mid + Math.imul(al0, bh3)) | 0; | |
mid = (mid + Math.imul(ah0, bl3)) | 0; | |
hi = (hi + Math.imul(ah0, bh3)) | 0; | |
var w3 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
c = (((hi + (mid >>> 13)) | 0) + (w3 >>> 26)) | 0; | |
w3 &= 0x3ffffff; | |
/* k = 4 */ | |
lo = Math.imul(al4, bl0); | |
mid = Math.imul(al4, bh0); | |
mid = (mid + Math.imul(ah4, bl0)) | 0; | |
hi = Math.imul(ah4, bh0); | |
lo = (lo + Math.imul(al3, bl1)) | 0; | |
mid = (mid + Math.imul(al3, bh1)) | 0; | |
mid = (mid + Math.imul(ah3, bl1)) | 0; | |
hi = (hi + Math.imul(ah3, bh1)) | 0; | |
lo = (lo + Math.imul(al2, bl2)) | 0; | |
mid = (mid + Math.imul(al2, bh2)) | 0; | |
mid = (mid + Math.imul(ah2, bl2)) | 0; | |
hi = (hi + Math.imul(ah2, bh2)) | 0; | |
lo = (lo + Math.imul(al1, bl3)) | 0; | |
mid = (mid + Math.imul(al1, bh3)) | 0; | |
mid = (mid + Math.imul(ah1, bl3)) | 0; | |
hi = (hi + Math.imul(ah1, bh3)) | 0; | |
lo = (lo + Math.imul(al0, bl4)) | 0; | |
mid = (mid + Math.imul(al0, bh4)) | 0; | |
mid = (mid + Math.imul(ah0, bl4)) | 0; | |
hi = (hi + Math.imul(ah0, bh4)) | 0; | |
var w4 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
c = (((hi + (mid >>> 13)) | 0) + (w4 >>> 26)) | 0; | |
w4 &= 0x3ffffff; | |
/* k = 5 */ | |
lo = Math.imul(al5, bl0); | |
mid = Math.imul(al5, bh0); | |
mid = (mid + Math.imul(ah5, bl0)) | 0; | |
hi = Math.imul(ah5, bh0); | |
lo = (lo + Math.imul(al4, bl1)) | 0; | |
mid = (mid + Math.imul(al4, bh1)) | 0; | |
mid = (mid + Math.imul(ah4, bl1)) | 0; | |
hi = (hi + Math.imul(ah4, bh1)) | 0; | |
lo = (lo + Math.imul(al3, bl2)) | 0; | |
mid = (mid + Math.imul(al3, bh2)) | 0; | |
mid = (mid + Math.imul(ah3, bl2)) | 0; | |
hi = (hi + Math.imul(ah3, bh2)) | 0; | |
lo = (lo + Math.imul(al2, bl3)) | 0; | |
mid = (mid + Math.imul(al2, bh3)) | 0; | |
mid = (mid + Math.imul(ah2, bl3)) | 0; | |
hi = (hi + Math.imul(ah2, bh3)) | 0; | |
lo = (lo + Math.imul(al1, bl4)) | 0; | |
mid = (mid + Math.imul(al1, bh4)) | 0; | |
mid = (mid + Math.imul(ah1, bl4)) | 0; | |
hi = (hi + Math.imul(ah1, bh4)) | 0; | |
lo = (lo + Math.imul(al0, bl5)) | 0; | |
mid = (mid + Math.imul(al0, bh5)) | 0; | |
mid = (mid + Math.imul(ah0, bl5)) | 0; | |
hi = (hi + Math.imul(ah0, bh5)) | 0; | |
var w5 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
c = (((hi + (mid >>> 13)) | 0) + (w5 >>> 26)) | 0; | |
w5 &= 0x3ffffff; | |
/* k = 6 */ | |
lo = Math.imul(al6, bl0); | |
mid = Math.imul(al6, bh0); | |
mid = (mid + Math.imul(ah6, bl0)) | 0; | |
hi = Math.imul(ah6, bh0); | |
lo = (lo + Math.imul(al5, bl1)) | 0; | |
mid = (mid + Math.imul(al5, bh1)) | 0; | |
mid = (mid + Math.imul(ah5, bl1)) | 0; | |
hi = (hi + Math.imul(ah5, bh1)) | 0; | |
lo = (lo + Math.imul(al4, bl2)) | 0; | |
mid = (mid + Math.imul(al4, bh2)) | 0; | |
mid = (mid + Math.imul(ah4, bl2)) | 0; | |
hi = (hi + Math.imul(ah4, bh2)) | 0; | |
lo = (lo + Math.imul(al3, bl3)) | 0; | |
mid = (mid + Math.imul(al3, bh3)) | 0; | |
mid = (mid + Math.imul(ah3, bl3)) | 0; | |
hi = (hi + Math.imul(ah3, bh3)) | 0; | |
lo = (lo + Math.imul(al2, bl4)) | 0; | |
mid = (mid + Math.imul(al2, bh4)) | 0; | |
mid = (mid + Math.imul(ah2, bl4)) | 0; | |
hi = (hi + Math.imul(ah2, bh4)) | 0; | |
lo = (lo + Math.imul(al1, bl5)) | 0; | |
mid = (mid + Math.imul(al1, bh5)) | 0; | |
mid = (mid + Math.imul(ah1, bl5)) | 0; | |
hi = (hi + Math.imul(ah1, bh5)) | 0; | |
lo = (lo + Math.imul(al0, bl6)) | 0; | |
mid = (mid + Math.imul(al0, bh6)) | 0; | |
mid = (mid + Math.imul(ah0, bl6)) | 0; | |
hi = (hi + Math.imul(ah0, bh6)) | 0; | |
var w6 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
c = (((hi + (mid >>> 13)) | 0) + (w6 >>> 26)) | 0; | |
w6 &= 0x3ffffff; | |
/* k = 7 */ | |
lo = Math.imul(al7, bl0); | |
mid = Math.imul(al7, bh0); | |
mid = (mid + Math.imul(ah7, bl0)) | 0; | |
hi = Math.imul(ah7, bh0); | |
lo = (lo + Math.imul(al6, bl1)) | 0; | |
mid = (mid + Math.imul(al6, bh1)) | 0; | |
mid = (mid + Math.imul(ah6, bl1)) | 0; | |
hi = (hi + Math.imul(ah6, bh1)) | 0; | |
lo = (lo + Math.imul(al5, bl2)) | 0; | |
mid = (mid + Math.imul(al5, bh2)) | 0; | |
mid = (mid + Math.imul(ah5, bl2)) | 0; | |
hi = (hi + Math.imul(ah5, bh2)) | 0; | |
lo = (lo + Math.imul(al4, bl3)) | 0; | |
mid = (mid + Math.imul(al4, bh3)) | 0; | |
mid = (mid + Math.imul(ah4, bl3)) | 0; | |
hi = (hi + Math.imul(ah4, bh3)) | 0; | |
lo = (lo + Math.imul(al3, bl4)) | 0; | |
mid = (mid + Math.imul(al3, bh4)) | 0; | |
mid = (mid + Math.imul(ah3, bl4)) | 0; | |
hi = (hi + Math.imul(ah3, bh4)) | 0; | |
lo = (lo + Math.imul(al2, bl5)) | 0; | |
mid = (mid + Math.imul(al2, bh5)) | 0; | |
mid = (mid + Math.imul(ah2, bl5)) | 0; | |
hi = (hi + Math.imul(ah2, bh5)) | 0; | |
lo = (lo + Math.imul(al1, bl6)) | 0; | |
mid = (mid + Math.imul(al1, bh6)) | 0; | |
mid = (mid + Math.imul(ah1, bl6)) | 0; | |
hi = (hi + Math.imul(ah1, bh6)) | 0; | |
lo = (lo + Math.imul(al0, bl7)) | 0; | |
mid = (mid + Math.imul(al0, bh7)) | 0; | |
mid = (mid + Math.imul(ah0, bl7)) | 0; | |
hi = (hi + Math.imul(ah0, bh7)) | 0; | |
var w7 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
c = (((hi + (mid >>> 13)) | 0) + (w7 >>> 26)) | 0; | |
w7 &= 0x3ffffff; | |
/* k = 8 */ | |
lo = Math.imul(al8, bl0); | |
mid = Math.imul(al8, bh0); | |
mid = (mid + Math.imul(ah8, bl0)) | 0; | |
hi = Math.imul(ah8, bh0); | |
lo = (lo + Math.imul(al7, bl1)) | 0; | |
mid = (mid + Math.imul(al7, bh1)) | 0; | |
mid = (mid + Math.imul(ah7, bl1)) | 0; | |
hi = (hi + Math.imul(ah7, bh1)) | 0; | |
lo = (lo + Math.imul(al6, bl2)) | 0; | |
mid = (mid + Math.imul(al6, bh2)) | 0; | |
mid = (mid + Math.imul(ah6, bl2)) | 0; | |
hi = (hi + Math.imul(ah6, bh2)) | 0; | |
lo = (lo + Math.imul(al5, bl3)) | 0; | |
mid = (mid + Math.imul(al5, bh3)) | 0; | |
mid = (mid + Math.imul(ah5, bl3)) | 0; | |
hi = (hi + Math.imul(ah5, bh3)) | 0; | |
lo = (lo + Math.imul(al4, bl4)) | 0; | |
mid = (mid + Math.imul(al4, bh4)) | 0; | |
mid = (mid + Math.imul(ah4, bl4)) | 0; | |
hi = (hi + Math.imul(ah4, bh4)) | 0; | |
lo = (lo + Math.imul(al3, bl5)) | 0; | |
mid = (mid + Math.imul(al3, bh5)) | 0; | |
mid = (mid + Math.imul(ah3, bl5)) | 0; | |
hi = (hi + Math.imul(ah3, bh5)) | 0; | |
lo = (lo + Math.imul(al2, bl6)) | 0; | |
mid = (mid + Math.imul(al2, bh6)) | 0; | |
mid = (mid + Math.imul(ah2, bl6)) | 0; | |
hi = (hi + Math.imul(ah2, bh6)) | 0; | |
lo = (lo + Math.imul(al1, bl7)) | 0; | |
mid = (mid + Math.imul(al1, bh7)) | 0; | |
mid = (mid + Math.imul(ah1, bl7)) | 0; | |
hi = (hi + Math.imul(ah1, bh7)) | 0; | |
lo = (lo + Math.imul(al0, bl8)) | 0; | |
mid = (mid + Math.imul(al0, bh8)) | 0; | |
mid = (mid + Math.imul(ah0, bl8)) | 0; | |
hi = (hi + Math.imul(ah0, bh8)) | 0; | |
var w8 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
c = (((hi + (mid >>> 13)) | 0) + (w8 >>> 26)) | 0; | |
w8 &= 0x3ffffff; | |
/* k = 9 */ | |
lo = Math.imul(al9, bl0); | |
mid = Math.imul(al9, bh0); | |
mid = (mid + Math.imul(ah9, bl0)) | 0; | |
hi = Math.imul(ah9, bh0); | |
lo = (lo + Math.imul(al8, bl1)) | 0; | |
mid = (mid + Math.imul(al8, bh1)) | 0; | |
mid = (mid + Math.imul(ah8, bl1)) | 0; | |
hi = (hi + Math.imul(ah8, bh1)) | 0; | |
lo = (lo + Math.imul(al7, bl2)) | 0; | |
mid = (mid + Math.imul(al7, bh2)) | 0; | |
mid = (mid + Math.imul(ah7, bl2)) | 0; | |
hi = (hi + Math.imul(ah7, bh2)) | 0; | |
lo = (lo + Math.imul(al6, bl3)) | 0; | |
mid = (mid + Math.imul(al6, bh3)) | 0; | |
mid = (mid + Math.imul(ah6, bl3)) | 0; | |
hi = (hi + Math.imul(ah6, bh3)) | 0; | |
lo = (lo + Math.imul(al5, bl4)) | 0; | |
mid = (mid + Math.imul(al5, bh4)) | 0; | |
mid = (mid + Math.imul(ah5, bl4)) | 0; | |
hi = (hi + Math.imul(ah5, bh4)) | 0; | |
lo = (lo + Math.imul(al4, bl5)) | 0; | |
mid = (mid + Math.imul(al4, bh5)) | 0; | |
mid = (mid + Math.imul(ah4, bl5)) | 0; | |
hi = (hi + Math.imul(ah4, bh5)) | 0; | |
lo = (lo + Math.imul(al3, bl6)) | 0; | |
mid = (mid + Math.imul(al3, bh6)) | 0; | |
mid = (mid + Math.imul(ah3, bl6)) | 0; | |
hi = (hi + Math.imul(ah3, bh6)) | 0; | |
lo = (lo + Math.imul(al2, bl7)) | 0; | |
mid = (mid + Math.imul(al2, bh7)) | 0; | |
mid = (mid + Math.imul(ah2, bl7)) | 0; | |
hi = (hi + Math.imul(ah2, bh7)) | 0; | |
lo = (lo + Math.imul(al1, bl8)) | 0; | |
mid = (mid + Math.imul(al1, bh8)) | 0; | |
mid = (mid + Math.imul(ah1, bl8)) | 0; | |
hi = (hi + Math.imul(ah1, bh8)) | 0; | |
lo = (lo + Math.imul(al0, bl9)) | 0; | |
mid = (mid + Math.imul(al0, bh9)) | 0; | |
mid = (mid + Math.imul(ah0, bl9)) | 0; | |
hi = (hi + Math.imul(ah0, bh9)) | 0; | |
var w9 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
c = (((hi + (mid >>> 13)) | 0) + (w9 >>> 26)) | 0; | |
w9 &= 0x3ffffff; | |
/* k = 10 */ | |
lo = Math.imul(al9, bl1); | |
mid = Math.imul(al9, bh1); | |
mid = (mid + Math.imul(ah9, bl1)) | 0; | |
hi = Math.imul(ah9, bh1); | |
lo = (lo + Math.imul(al8, bl2)) | 0; | |
mid = (mid + Math.imul(al8, bh2)) | 0; | |
mid = (mid + Math.imul(ah8, bl2)) | 0; | |
hi = (hi + Math.imul(ah8, bh2)) | 0; | |
lo = (lo + Math.imul(al7, bl3)) | 0; | |
mid = (mid + Math.imul(al7, bh3)) | 0; | |
mid = (mid + Math.imul(ah7, bl3)) | 0; | |
hi = (hi + Math.imul(ah7, bh3)) | 0; | |
lo = (lo + Math.imul(al6, bl4)) | 0; | |
mid = (mid + Math.imul(al6, bh4)) | 0; | |
mid = (mid + Math.imul(ah6, bl4)) | 0; | |
hi = (hi + Math.imul(ah6, bh4)) | 0; | |
lo = (lo + Math.imul(al5, bl5)) | 0; | |
mid = (mid + Math.imul(al5, bh5)) | 0; | |
mid = (mid + Math.imul(ah5, bl5)) | 0; | |
hi = (hi + Math.imul(ah5, bh5)) | 0; | |
lo = (lo + Math.imul(al4, bl6)) | 0; | |
mid = (mid + Math.imul(al4, bh6)) | 0; | |
mid = (mid + Math.imul(ah4, bl6)) | 0; | |
hi = (hi + Math.imul(ah4, bh6)) | 0; | |
lo = (lo + Math.imul(al3, bl7)) | 0; | |
mid = (mid + Math.imul(al3, bh7)) | 0; | |
mid = (mid + Math.imul(ah3, bl7)) | 0; | |
hi = (hi + Math.imul(ah3, bh7)) | 0; | |
lo = (lo + Math.imul(al2, bl8)) | 0; | |
mid = (mid + Math.imul(al2, bh8)) | 0; | |
mid = (mid + Math.imul(ah2, bl8)) | 0; | |
hi = (hi + Math.imul(ah2, bh8)) | 0; | |
lo = (lo + Math.imul(al1, bl9)) | 0; | |
mid = (mid + Math.imul(al1, bh9)) | 0; | |
mid = (mid + Math.imul(ah1, bl9)) | 0; | |
hi = (hi + Math.imul(ah1, bh9)) | 0; | |
var w10 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
c = (((hi + (mid >>> 13)) | 0) + (w10 >>> 26)) | 0; | |
w10 &= 0x3ffffff; | |
/* k = 11 */ | |
lo = Math.imul(al9, bl2); | |
mid = Math.imul(al9, bh2); | |
mid = (mid + Math.imul(ah9, bl2)) | 0; | |
hi = Math.imul(ah9, bh2); | |
lo = (lo + Math.imul(al8, bl3)) | 0; | |
mid = (mid + Math.imul(al8, bh3)) | 0; | |
mid = (mid + Math.imul(ah8, bl3)) | 0; | |
hi = (hi + Math.imul(ah8, bh3)) | 0; | |
lo = (lo + Math.imul(al7, bl4)) | 0; | |
mid = (mid + Math.imul(al7, bh4)) | 0; | |
mid = (mid + Math.imul(ah7, bl4)) | 0; | |
hi = (hi + Math.imul(ah7, bh4)) | 0; | |
lo = (lo + Math.imul(al6, bl5)) | 0; | |
mid = (mid + Math.imul(al6, bh5)) | 0; | |
mid = (mid + Math.imul(ah6, bl5)) | 0; | |
hi = (hi + Math.imul(ah6, bh5)) | 0; | |
lo = (lo + Math.imul(al5, bl6)) | 0; | |
mid = (mid + Math.imul(al5, bh6)) | 0; | |
mid = (mid + Math.imul(ah5, bl6)) | 0; | |
hi = (hi + Math.imul(ah5, bh6)) | 0; | |
lo = (lo + Math.imul(al4, bl7)) | 0; | |
mid = (mid + Math.imul(al4, bh7)) | 0; | |
mid = (mid + Math.imul(ah4, bl7)) | 0; | |
hi = (hi + Math.imul(ah4, bh7)) | 0; | |
lo = (lo + Math.imul(al3, bl8)) | 0; | |
mid = (mid + Math.imul(al3, bh8)) | 0; | |
mid = (mid + Math.imul(ah3, bl8)) | 0; | |
hi = (hi + Math.imul(ah3, bh8)) | 0; | |
lo = (lo + Math.imul(al2, bl9)) | 0; | |
mid = (mid + Math.imul(al2, bh9)) | 0; | |
mid = (mid + Math.imul(ah2, bl9)) | 0; | |
hi = (hi + Math.imul(ah2, bh9)) | 0; | |
var w11 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
c = (((hi + (mid >>> 13)) | 0) + (w11 >>> 26)) | 0; | |
w11 &= 0x3ffffff; | |
/* k = 12 */ | |
lo = Math.imul(al9, bl3); | |
mid = Math.imul(al9, bh3); | |
mid = (mid + Math.imul(ah9, bl3)) | 0; | |
hi = Math.imul(ah9, bh3); | |
lo = (lo + Math.imul(al8, bl4)) | 0; | |
mid = (mid + Math.imul(al8, bh4)) | 0; | |
mid = (mid + Math.imul(ah8, bl4)) | 0; | |
hi = (hi + Math.imul(ah8, bh4)) | 0; | |
lo = (lo + Math.imul(al7, bl5)) | 0; | |
mid = (mid + Math.imul(al7, bh5)) | 0; | |
mid = (mid + Math.imul(ah7, bl5)) | 0; | |
hi = (hi + Math.imul(ah7, bh5)) | 0; | |
lo = (lo + Math.imul(al6, bl6)) | 0; | |
mid = (mid + Math.imul(al6, bh6)) | 0; | |
mid = (mid + Math.imul(ah6, bl6)) | 0; | |
hi = (hi + Math.imul(ah6, bh6)) | 0; | |
lo = (lo + Math.imul(al5, bl7)) | 0; | |
mid = (mid + Math.imul(al5, bh7)) | 0; | |
mid = (mid + Math.imul(ah5, bl7)) | 0; | |
hi = (hi + Math.imul(ah5, bh7)) | 0; | |
lo = (lo + Math.imul(al4, bl8)) | 0; | |
mid = (mid + Math.imul(al4, bh8)) | 0; | |
mid = (mid + Math.imul(ah4, bl8)) | 0; | |
hi = (hi + Math.imul(ah4, bh8)) | 0; | |
lo = (lo + Math.imul(al3, bl9)) | 0; | |
mid = (mid + Math.imul(al3, bh9)) | 0; | |
mid = (mid + Math.imul(ah3, bl9)) | 0; | |
hi = (hi + Math.imul(ah3, bh9)) | 0; | |
var w12 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
c = (((hi + (mid >>> 13)) | 0) + (w12 >>> 26)) | 0; | |
w12 &= 0x3ffffff; | |
/* k = 13 */ | |
lo = Math.imul(al9, bl4); | |
mid = Math.imul(al9, bh4); | |
mid = (mid + Math.imul(ah9, bl4)) | 0; | |
hi = Math.imul(ah9, bh4); | |
lo = (lo + Math.imul(al8, bl5)) | 0; | |
mid = (mid + Math.imul(al8, bh5)) | 0; | |
mid = (mid + Math.imul(ah8, bl5)) | 0; | |
hi = (hi + Math.imul(ah8, bh5)) | 0; | |
lo = (lo + Math.imul(al7, bl6)) | 0; | |
mid = (mid + Math.imul(al7, bh6)) | 0; | |
mid = (mid + Math.imul(ah7, bl6)) | 0; | |
hi = (hi + Math.imul(ah7, bh6)) | 0; | |
lo = (lo + Math.imul(al6, bl7)) | 0; | |
mid = (mid + Math.imul(al6, bh7)) | 0; | |
mid = (mid + Math.imul(ah6, bl7)) | 0; | |
hi = (hi + Math.imul(ah6, bh7)) | 0; | |
lo = (lo + Math.imul(al5, bl8)) | 0; | |
mid = (mid + Math.imul(al5, bh8)) | 0; | |
mid = (mid + Math.imul(ah5, bl8)) | 0; | |
hi = (hi + Math.imul(ah5, bh8)) | 0; | |
lo = (lo + Math.imul(al4, bl9)) | 0; | |
mid = (mid + Math.imul(al4, bh9)) | 0; | |
mid = (mid + Math.imul(ah4, bl9)) | 0; | |
hi = (hi + Math.imul(ah4, bh9)) | 0; | |
var w13 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
c = (((hi + (mid >>> 13)) | 0) + (w13 >>> 26)) | 0; | |
w13 &= 0x3ffffff; | |
/* k = 14 */ | |
lo = Math.imul(al9, bl5); | |
mid = Math.imul(al9, bh5); | |
mid = (mid + Math.imul(ah9, bl5)) | 0; | |
hi = Math.imul(ah9, bh5); | |
lo = (lo + Math.imul(al8, bl6)) | 0; | |
mid = (mid + Math.imul(al8, bh6)) | 0; | |
mid = (mid + Math.imul(ah8, bl6)) | 0; | |
hi = (hi + Math.imul(ah8, bh6)) | 0; | |
lo = (lo + Math.imul(al7, bl7)) | 0; | |
mid = (mid + Math.imul(al7, bh7)) | 0; | |
mid = (mid + Math.imul(ah7, bl7)) | 0; | |
hi = (hi + Math.imul(ah7, bh7)) | 0; | |
lo = (lo + Math.imul(al6, bl8)) | 0; | |
mid = (mid + Math.imul(al6, bh8)) | 0; | |
mid = (mid + Math.imul(ah6, bl8)) | 0; | |
hi = (hi + Math.imul(ah6, bh8)) | 0; | |
lo = (lo + Math.imul(al5, bl9)) | 0; | |
mid = (mid + Math.imul(al5, bh9)) | 0; | |
mid = (mid + Math.imul(ah5, bl9)) | 0; | |
hi = (hi + Math.imul(ah5, bh9)) | 0; | |
var w14 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
c = (((hi + (mid >>> 13)) | 0) + (w14 >>> 26)) | 0; | |
w14 &= 0x3ffffff; | |
/* k = 15 */ | |
lo = Math.imul(al9, bl6); | |
mid = Math.imul(al9, bh6); | |
mid = (mid + Math.imul(ah9, bl6)) | 0; | |
hi = Math.imul(ah9, bh6); | |
lo = (lo + Math.imul(al8, bl7)) | 0; | |
mid = (mid + Math.imul(al8, bh7)) | 0; | |
mid = (mid + Math.imul(ah8, bl7)) | 0; | |
hi = (hi + Math.imul(ah8, bh7)) | 0; | |
lo = (lo + Math.imul(al7, bl8)) | 0; | |
mid = (mid + Math.imul(al7, bh8)) | 0; | |
mid = (mid + Math.imul(ah7, bl8)) | 0; | |
hi = (hi + Math.imul(ah7, bh8)) | 0; | |
lo = (lo + Math.imul(al6, bl9)) | 0; | |
mid = (mid + Math.imul(al6, bh9)) | 0; | |
mid = (mid + Math.imul(ah6, bl9)) | 0; | |
hi = (hi + Math.imul(ah6, bh9)) | 0; | |
var w15 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
c = (((hi + (mid >>> 13)) | 0) + (w15 >>> 26)) | 0; | |
w15 &= 0x3ffffff; | |
/* k = 16 */ | |
lo = Math.imul(al9, bl7); | |
mid = Math.imul(al9, bh7); | |
mid = (mid + Math.imul(ah9, bl7)) | 0; | |
hi = Math.imul(ah9, bh7); | |
lo = (lo + Math.imul(al8, bl8)) | 0; | |
mid = (mid + Math.imul(al8, bh8)) | 0; | |
mid = (mid + Math.imul(ah8, bl8)) | 0; | |
hi = (hi + Math.imul(ah8, bh8)) | 0; | |
lo = (lo + Math.imul(al7, bl9)) | 0; | |
mid = (mid + Math.imul(al7, bh9)) | 0; | |
mid = (mid + Math.imul(ah7, bl9)) | 0; | |
hi = (hi + Math.imul(ah7, bh9)) | 0; | |
var w16 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
c = (((hi + (mid >>> 13)) | 0) + (w16 >>> 26)) | 0; | |
w16 &= 0x3ffffff; | |
/* k = 17 */ | |
lo = Math.imul(al9, bl8); | |
mid = Math.imul(al9, bh8); | |
mid = (mid + Math.imul(ah9, bl8)) | 0; | |
hi = Math.imul(ah9, bh8); | |
lo = (lo + Math.imul(al8, bl9)) | 0; | |
mid = (mid + Math.imul(al8, bh9)) | 0; | |
mid = (mid + Math.imul(ah8, bl9)) | 0; | |
hi = (hi + Math.imul(ah8, bh9)) | 0; | |
var w17 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
c = (((hi + (mid >>> 13)) | 0) + (w17 >>> 26)) | 0; | |
w17 &= 0x3ffffff; | |
/* k = 18 */ | |
lo = Math.imul(al9, bl9); | |
mid = Math.imul(al9, bh9); | |
mid = (mid + Math.imul(ah9, bl9)) | 0; | |
hi = Math.imul(ah9, bh9); | |
var w18 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
c = (((hi + (mid >>> 13)) | 0) + (w18 >>> 26)) | 0; | |
w18 &= 0x3ffffff; | |
o[0] = w0; | |
o[1] = w1; | |
o[2] = w2; | |
o[3] = w3; | |
o[4] = w4; | |
o[5] = w5; | |
o[6] = w6; | |
o[7] = w7; | |
o[8] = w8; | |
o[9] = w9; | |
o[10] = w10; | |
o[11] = w11; | |
o[12] = w12; | |
o[13] = w13; | |
o[14] = w14; | |
o[15] = w15; | |
o[16] = w16; | |
o[17] = w17; | |
o[18] = w18; | |
if (c !== 0) { | |
o[19] = c; | |
out.length++; | |
} | |
return out; | |
}; | |
// Polyfill comb | |
if (!Math.imul) { | |
comb10MulTo = smallMulTo; | |
} | |
function bigMulTo (self, num, out) { | |
out.negative = num.negative ^ self.negative; | |
out.length = self.length + num.length; | |
var carry = 0; | |
var hncarry = 0; | |
for (var k = 0; k < out.length - 1; k++) { | |
// Sum all words with the same `i + j = k` and accumulate `ncarry`, | |
// note that ncarry could be >= 0x3ffffff | |
var ncarry = hncarry; | |
hncarry = 0; | |
var rword = carry & 0x3ffffff; | |
var maxJ = Math.min(k, num.length - 1); | |
for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) { | |
var i = k - j; | |
var a = self.words[i] | 0; | |
var b = num.words[j] | 0; | |
var r = a * b; | |
var lo = r & 0x3ffffff; | |
ncarry = (ncarry + ((r / 0x4000000) | 0)) | 0; | |
lo = (lo + rword) | 0; | |
rword = lo & 0x3ffffff; | |
ncarry = (ncarry + (lo >>> 26)) | 0; | |
hncarry += ncarry >>> 26; | |
ncarry &= 0x3ffffff; | |
} | |
out.words[k] = rword; | |
carry = ncarry; | |
ncarry = hncarry; | |
} | |
if (carry !== 0) { | |
out.words[k] = carry; | |
} else { | |
out.length--; | |
} | |
return out.strip(); | |
} | |
function jumboMulTo (self, num, out) { | |
var fftm = new FFTM(); | |
return fftm.mulp(self, num, out); | |
} | |
BN.prototype.mulTo = function mulTo (num, out) { | |
var res; | |
var len = this.length + num.length; | |
if (this.length === 10 && num.length === 10) { | |
res = comb10MulTo(this, num, out); | |
} else if (len < 63) { | |
res = smallMulTo(this, num, out); | |
} else if (len < 1024) { | |
res = bigMulTo(this, num, out); | |
} else { | |
res = jumboMulTo(this, num, out); | |
} | |
return res; | |
}; | |
// Cooley-Tukey algorithm for FFT | |
// slightly revisited to rely on looping instead of recursion | |
function FFTM (x, y) { | |
this.x = x; | |
this.y = y; | |
} | |
FFTM.prototype.makeRBT = function makeRBT (N) { | |
var t = new Array(N); | |
var l = BN.prototype._countBits(N) - 1; | |
for (var i = 0; i < N; i++) { | |
t[i] = this.revBin(i, l, N); | |
} | |
return t; | |
}; | |
// Returns binary-reversed representation of `x` | |
FFTM.prototype.revBin = function revBin (x, l, N) { | |
if (x === 0 || x === N - 1) return x; | |
var rb = 0; | |
for (var i = 0; i < l; i++) { | |
rb |= (x & 1) << (l - i - 1); | |
x >>= 1; | |
} | |
return rb; | |
}; | |
// Performs "tweedling" phase, therefore 'emulating' | |
// behaviour of the recursive algorithm | |
FFTM.prototype.permute = function permute (rbt, rws, iws, rtws, itws, N) { | |
for (var i = 0; i < N; i++) { | |
rtws[i] = rws[rbt[i]]; | |
itws[i] = iws[rbt[i]]; | |
} | |
}; | |
FFTM.prototype.transform = function transform (rws, iws, rtws, itws, N, rbt) { | |
this.permute(rbt, rws, iws, rtws, itws, N); | |
for (var s = 1; s < N; s <<= 1) { | |
var l = s << 1; | |
var rtwdf = Math.cos(2 * Math.PI / l); | |
var itwdf = Math.sin(2 * Math.PI / l); | |
for (var p = 0; p < N; p += l) { | |
var rtwdf_ = rtwdf; | |
var itwdf_ = itwdf; | |
for (var j = 0; j < s; j++) { | |
var re = rtws[p + j]; | |
var ie = itws[p + j]; | |
var ro = rtws[p + j + s]; | |
var io = itws[p + j + s]; | |
var rx = rtwdf_ * ro - itwdf_ * io; | |
io = rtwdf_ * io + itwdf_ * ro; | |
ro = rx; | |
rtws[p + j] = re + ro; | |
itws[p + j] = ie + io; | |
rtws[p + j + s] = re - ro; | |
itws[p + j + s] = ie - io; | |
/* jshint maxdepth : false */ | |
if (j !== l) { | |
rx = rtwdf * rtwdf_ - itwdf * itwdf_; | |
itwdf_ = rtwdf * itwdf_ + itwdf * rtwdf_; | |
rtwdf_ = rx; | |
} | |
} | |
} | |
} | |
}; | |
FFTM.prototype.guessLen13b = function guessLen13b (n, m) { | |
var N = Math.max(m, n) | 1; | |
var odd = N & 1; | |
var i = 0; | |
for (N = N / 2 | 0; N; N = N >>> 1) { | |
i++; | |
} | |
return 1 << i + 1 + odd; | |
}; | |
FFTM.prototype.conjugate = function conjugate (rws, iws, N) { | |
if (N <= 1) return; | |
for (var i = 0; i < N / 2; i++) { | |
var t = rws[i]; | |
rws[i] = rws[N - i - 1]; | |
rws[N - i - 1] = t; | |
t = iws[i]; | |
iws[i] = -iws[N - i - 1]; | |
iws[N - i - 1] = -t; | |
} | |
}; | |
FFTM.prototype.normalize13b = function normalize13b (ws, N) { | |
var carry = 0; | |
for (var i = 0; i < N / 2; i++) { | |
var w = Math.round(ws[2 * i + 1] / N) * 0x2000 + | |
Math.round(ws[2 * i] / N) + | |
carry; | |
ws[i] = w & 0x3ffffff; | |
if (w < 0x4000000) { | |
carry = 0; | |
} else { | |
carry = w / 0x4000000 | 0; | |
} | |
} | |
return ws; | |
}; | |
FFTM.prototype.convert13b = function convert13b (ws, len, rws, N) { | |
var carry = 0; | |
for (var i = 0; i < len; i++) { | |
carry = carry + (ws[i] | 0); | |
rws[2 * i] = carry & 0x1fff; carry = carry >>> 13; | |
rws[2 * i + 1] = carry & 0x1fff; carry = carry >>> 13; | |
} | |
// Pad with zeroes | |
for (i = 2 * len; i < N; ++i) { | |
rws[i] = 0; | |
} | |
assert(carry === 0); | |
assert((carry & ~0x1fff) === 0); | |
}; | |
FFTM.prototype.stub = function stub (N) { | |
var ph = new Array(N); | |
for (var i = 0; i < N; i++) { | |
ph[i] = 0; | |
} | |
return ph; | |
}; | |
FFTM.prototype.mulp = function mulp (x, y, out) { | |
var N = 2 * this.guessLen13b(x.length, y.length); | |
var rbt = this.makeRBT(N); | |
var _ = this.stub(N); | |
var rws = new Array(N); | |
var rwst = new Array(N); | |
var iwst = new Array(N); | |
var nrws = new Array(N); | |
var nrwst = new Array(N); | |
var niwst = new Array(N); | |
var rmws = out.words; | |
rmws.length = N; | |
this.convert13b(x.words, x.length, rws, N); | |
this.convert13b(y.words, y.length, nrws, N); | |
this.transform(rws, _, rwst, iwst, N, rbt); | |
this.transform(nrws, _, nrwst, niwst, N, rbt); | |
for (var i = 0; i < N; i++) { | |
var rx = rwst[i] * nrwst[i] - iwst[i] * niwst[i]; | |
iwst[i] = rwst[i] * niwst[i] + iwst[i] * nrwst[i]; | |
rwst[i] = rx; | |
} | |
this.conjugate(rwst, iwst, N); | |
this.transform(rwst, iwst, rmws, _, N, rbt); | |
this.conjugate(rmws, _, N); | |
this.normalize13b(rmws, N); | |
out.negative = x.negative ^ y.negative; | |
out.length = x.length + y.length; | |
return out.strip(); | |
}; | |
// Multiply `this` by `num` | |
BN.prototype.mul = function mul (num) { | |
var out = new BN(null); | |
out.words = new Array(this.length + num.length); | |
return this.mulTo(num, out); | |
}; | |
// Multiply employing FFT | |
BN.prototype.mulf = function mulf (num) { | |
var out = new BN(null); | |
out.words = new Array(this.length + num.length); | |
return jumboMulTo(this, num, out); | |
}; | |
// In-place Multiplication | |
BN.prototype.imul = function imul (num) { | |
return this.clone().mulTo(num, this); | |
}; | |
BN.prototype.imuln = function imuln (num) { | |
assert(typeof num === 'number'); | |
assert(num < 0x4000000); | |
// Carry | |
var carry = 0; | |
for (var i = 0; i < this.length; i++) { | |
var w = (this.words[i] | 0) * num; | |
var lo = (w & 0x3ffffff) + (carry & 0x3ffffff); | |
carry >>= 26; | |
carry += (w / 0x4000000) | 0; | |
// NOTE: lo is 27bit maximum | |
carry += lo >>> 26; | |
this.words[i] = lo & 0x3ffffff; | |
} | |
if (carry !== 0) { | |
this.words[i] = carry; | |
this.length++; | |
} | |
return this; | |
}; | |
BN.prototype.muln = function muln (num) { | |
return this.clone().imuln(num); | |
}; | |
// `this` * `this` | |
BN.prototype.sqr = function sqr () { | |
return this.mul(this); | |
}; | |
// `this` * `this` in-place | |
BN.prototype.isqr = function isqr () { | |
return this.imul(this.clone()); | |
}; | |
// Math.pow(`this`, `num`) | |
BN.prototype.pow = function pow (num) { | |
var w = toBitArray(num); | |
if (w.length === 0) return new BN(1); | |
// Skip leading zeroes | |
var res = this; | |
for (var i = 0; i < w.length; i++, res = res.sqr()) { | |
if (w[i] !== 0) break; | |
} | |
if (++i < w.length) { | |
for (var q = res.sqr(); i < w.length; i++, q = q.sqr()) { | |
if (w[i] === 0) continue; | |
res = res.mul(q); | |
} | |
} | |
return res; | |
}; | |
// Shift-left in-place | |
BN.prototype.iushln = function iushln (bits) { | |
assert(typeof bits === 'number' && bits >= 0); | |
var r = bits % 26; | |
var s = (bits - r) / 26; | |
var carryMask = (0x3ffffff >>> (26 - r)) << (26 - r); | |
var i; | |
if (r !== 0) { | |
var carry = 0; | |
for (i = 0; i < this.length; i++) { | |
var newCarry = this.words[i] & carryMask; | |
var c = ((this.words[i] | 0) - newCarry) << r; | |
this.words[i] = c | carry; | |
carry = newCarry >>> (26 - r); | |
} | |
if (carry) { | |
this.words[i] = carry; | |
this.length++; | |
} | |
} | |
if (s !== 0) { | |
for (i = this.length - 1; i >= 0; i--) { | |
this.words[i + s] = this.words[i]; | |
} | |
for (i = 0; i < s; i++) { | |
this.words[i] = 0; | |
} | |
this.length += s; | |
} | |
return this.strip(); | |
}; | |
BN.prototype.ishln = function ishln (bits) { | |
// TODO(indutny): implement me | |
assert(this.negative === 0); | |
return this.iushln(bits); | |
}; | |
// Shift-right in-place | |
// NOTE: `hint` is a lowest bit before trailing zeroes | |
// NOTE: if `extended` is present - it will be filled with destroyed bits | |
BN.prototype.iushrn = function iushrn (bits, hint, extended) { | |
assert(typeof bits === 'number' && bits >= 0); | |
var h; | |
if (hint) { | |
h = (hint - (hint % 26)) / 26; | |
} else { | |
h = 0; | |
} | |
var r = bits % 26; | |
var s = Math.min((bits - r) / 26, this.length); | |
var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r); | |
var maskedWords = extended; | |
h -= s; | |
h = Math.max(0, h); | |
// Extended mode, copy masked part | |
if (maskedWords) { | |
for (var i = 0; i < s; i++) { | |
maskedWords.words[i] = this.words[i]; | |
} | |
maskedWords.length = s; | |
} | |
if (s === 0) { | |
// No-op, we should not move anything at all | |
} else if (this.length > s) { | |
this.length -= s; | |
for (i = 0; i < this.length; i++) { | |
this.words[i] = this.words[i + s]; | |
} | |
} else { | |
this.words[0] = 0; | |
this.length = 1; | |
} | |
var carry = 0; | |
for (i = this.length - 1; i >= 0 && (carry !== 0 || i >= h); i--) { | |
var word = this.words[i] | 0; | |
this.words[i] = (carry << (26 - r)) | (word >>> r); | |
carry = word & mask; | |
} | |
// Push carried bits as a mask | |
if (maskedWords && carry !== 0) { | |
maskedWords.words[maskedWords.length++] = carry; | |
} | |
if (this.length === 0) { | |
this.words[0] = 0; | |
this.length = 1; | |
} | |
return this.strip(); | |
}; | |
BN.prototype.ishrn = function ishrn (bits, hint, extended) { | |
// TODO(indutny): implement me | |
assert(this.negative === 0); | |
return this.iushrn(bits, hint, extended); | |
}; | |
// Shift-left | |
BN.prototype.shln = function shln (bits) { | |
return this.clone().ishln(bits); | |
}; | |
BN.prototype.ushln = function ushln (bits) { | |
return this.clone().iushln(bits); | |
}; | |
// Shift-right | |
BN.prototype.shrn = function shrn (bits) { | |
return this.clone().ishrn(bits); | |
}; | |
BN.prototype.ushrn = function ushrn (bits) { | |
return this.clone().iushrn(bits); | |
}; | |
// Test if n bit is set | |
BN.prototype.testn = function testn (bit) { | |
assert(typeof bit === 'number' && bit >= 0); | |
var r = bit % 26; | |
var s = (bit - r) / 26; | |
var q = 1 << r; | |
// Fast case: bit is much higher than all existing words | |
if (this.length <= s) return false; | |
// Check bit and return | |
var w = this.words[s]; | |
return !!(w & q); | |
}; | |
// Return only lowers bits of number (in-place) | |
BN.prototype.imaskn = function imaskn (bits) { | |
assert(typeof bits === 'number' && bits >= 0); | |
var r = bits % 26; | |
var s = (bits - r) / 26; | |
assert(this.negative === 0, 'imaskn works only with positive numbers'); | |
if (this.length <= s) { | |
return this; | |
} | |
if (r !== 0) { | |
s++; | |
} | |
this.length = Math.min(s, this.length); | |
if (r !== 0) { | |
var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r); | |
this.words[this.length - 1] &= mask; | |
} | |
return this.strip(); | |
}; | |
// Return only lowers bits of number | |
BN.prototype.maskn = function maskn (bits) { | |
return this.clone().imaskn(bits); | |
}; | |
// Add plain number `num` to `this` | |
BN.prototype.iaddn = function iaddn (num) { | |
assert(typeof num === 'number'); | |
assert(num < 0x4000000); | |
if (num < 0) return this.isubn(-num); | |
// Possible sign change | |
if (this.negative !== 0) { | |
if (this.length === 1 && (this.words[0] | 0) < num) { | |
this.words[0] = num - (this.words[0] | 0); | |
this.negative = 0; | |
return this; | |
} | |
this.negative = 0; | |
this.isubn(num); | |
this.negative = 1; | |
return this; | |
} | |
// Add without checks | |
return this._iaddn(num); | |
}; | |
BN.prototype._iaddn = function _iaddn (num) { | |
this.words[0] += num; | |
// Carry | |
for (var i = 0; i < this.length && this.words[i] >= 0x4000000; i++) { | |
this.words[i] -= 0x4000000; | |
if (i === this.length - 1) { | |
this.words[i + 1] = 1; | |
} else { | |
this.words[i + 1]++; | |
} | |
} | |
this.length = Math.max(this.length, i + 1); | |
return this; | |
}; | |
// Subtract plain number `num` from `this` | |
BN.prototype.isubn = function isubn (num) { | |
asser |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment