Permalink
Browse files

# removing nibbler dependency + more cc

  • Loading branch information...
1 parent a0dd7b7 commit 24793429bff5e49ae372137265a12b7d3c4356a8 Juan Pablo Garcia Dalolla committed Aug 27, 2011
View
@@ -117,10 +117,8 @@ To install via npm
# Remaining Stuff
* Documentation
-* SharedAccessSignature
## Blobs
-* Shared Access Signature
* Blocks
## Queues
@@ -2,7 +2,7 @@ var utils = require('../waz-storage/utils');
var serviceInstance;
-var Blob = module.exports = exports = function Blob(options) {
+var Blob = module.exports = exports = function Blob(options) {
this.name = options.name;
this.url = options.url;
this.path = this.url.replace(/https?:\/\/[^\/]+\//i, '').match(/([^&]+)/i)[1];
@@ -1,4 +1,4 @@
-exports.load = function(base) {
+exports.load = function(base) {
exports.container = require('./container').load(base);
return this;
}
@@ -18,15 +18,14 @@ function CoreService(options) {
CoreService.prototype.init = function(){
this.useSasAuthOnly = this.options.useSasAuthOnly || false;
- this.sharedaccesssignature = this.options.sharedAccessSignature;
this.accountName = this.options.accountName;
this.accountKey = this.options.accountKey;
this.typeOfService = this.options.typeOfService || "blob";
this.useSsl = this.options.useSsl || false;
this.useDevEnv = this.options.useDevEnv;
this.baseUrl = this.options.baseUrl || "core.windows.net";
- if (!options.useDevEnv)
+ if (!this.options.useDevEnv)
this.baseUrl = this.typeOfService + "." + this.baseUrl
}
@@ -44,7 +43,7 @@ CoreService.prototype.generateRequestUri = function(path, options) {
.map(function(k){ return (k + '=' + escape(options[k])).toString(); } )
.join("&");
}
-
+
return protocol + ":\/\/" + this.accountName + '.' + this.baseUrl + path.replace("//", "/") + params;
};
@@ -86,21 +85,22 @@ CoreService.prototype.canonicalizeMessage20090919 = function(url){
}
CoreService.prototype.generateSignature = function(options){
- this.init();
- if (options.headers['x-ms-version'] == "2009-09-19")
+ this.init();
+
+ if (options.headers['x-ms-version'] != undefined && options.headers['x-ms-version'] == "2009-09-19")
return this.generateSignature20090919(options);
var signature = options.method.toUpperCase() + "\x0A" +
- (options.headers["Content-MD5"] ? options.headers["Content-MD5"] : "") + "\x0A" +
- (options.headers["Content-Type"] ? options.headers["Content-Type"] : "") + "\x0A" +
- (options.headers["Date"] ? options.headers["Date"] : "") + "\x0A";
+ (options.headers["Content-MD5"] != undefined ? options.headers["Content-MD5"] : "") + "\x0A" +
+ (options.headers["Content-Type"] != undefined ? options.headers["Content-Type"] : "") + "\x0A" +
+ (options.headers["Date"] != undefined ? options.headers["Date"] : "") + "\x0A";
if (this.typeOfService != 'table')
signature += this.canonicalizeHeaders(options.headers) + "\x0A";
signature += this.canonicalizeMessage(options.url);
-
- return crypto.createHmac('RSA-SHA256', this.accountKey.base64decode()).update(signature).digest('base64');
+
+ return crypto.createHmac('RSA-SHA256', new Buffer(this.accountKey, 'base64').toString('binary')).update(new Buffer(signature, 'utf8')).digest('base64');
}
CoreService.prototype.generateSignature20090919 = function(options){
@@ -120,12 +120,11 @@ CoreService.prototype.generateSignature20090919 = function(options){
this.canonicalizeHeaders(options.headers) + "\x0A" +
this.canonicalizeMessage20090919(options.url);
- return crypto.createHmac('RSA-SHA256', this.accountKey.base64decode()).update(signature).digest('base64');
+ return crypto.createHmac('RSA-SHA256', new Buffer(this.accountKey, 'base64').toString('binary')).update(new Buffer(signature, 'utf8')).digest('base64');
}
-CoreService.prototype.execute = function(verb, path, query, headers, payload, callback){
- this.init();
- var error, data;
+CoreService.prototype.generateRequestOptions = function(verb, path, query, headers, payload){
+ this.init();
var parsedUrl = url.parse(this.generateRequestUri(path, query));
var headers = headers ? headers : {};
@@ -137,18 +136,25 @@ CoreService.prototype.execute = function(verb, path, query, headers, payload, ca
headers = headers.merge({'Content-Length' : 0 });
var params = { method: verb, headers : headers, url : parsedUrl.href };
-
+
headers = headers.merge({ 'Authorization' : 'SharedKey ' + this.accountName + ':' + this.generateSignature(params) });
- var options = {
+ return {
host: parsedUrl.host,
port: parsedUrl.protocol == 'https:' ? 443: 80,
path: (parsedUrl.pathname + "?" + (parsedUrl.query || "")).replace(/\?$/, ''),
method: verb,
- headers: headers
+ headers: headers,
+ protocol: parsedUrl.protocol.replace(':','')
};
+}
+
+CoreService.prototype.execute = function(verb, path, query, headers, payload, callback){
+ this.init();
+ var error, data;
+ var requestOptions = this.generateRequestOptions(verb, path, query, headers, payload);
- var request = require(parsedUrl.protocol.replace(':','')).request(options, function(response) {
+ var request = require(requestOptions.protocol).request(requestOptions, function(response) {
response.setEncoding('utf8');
var body = [];
@@ -1,5 +1,3 @@
-var nibbler = require('../../support/nibbler');
-
Object.prototype.merge = function(obj) {
if (!obj) obj = {};
var keys = Object.keys(obj);
@@ -26,12 +24,4 @@ Object.prototype.flatten = function flatten() {
}
return result;
-};
-
-String.prototype.base64encode = function() {
- return nibbler.b64encode(this);
-};
-
-String.prototype.base64decode = function() {
- return nibbler.b64decode(this);
};
View
@@ -1,249 +0,0 @@
-/*
-Adapted for Node.js by Matt Robenolt
-
-Reference: http://www.tumuski.com/2010/04/nibbler/
-*/
-
-/**
- * Node.js example:
- *
- * var nibbler = require('nibbler');
- *
- * nibbler.b32encode('Hello, World!'); // returns JBSWY3DPFQQFO33SNRSCC====='
- * nibbler.b32decode('JBSWY3DPFQQFO33SNRSCC====='); // returns 'Hello, World!'
- * nibbler.b64encode('Hello, World!'); // returns 'SGVsbG8sIFdvcmxkIQ=='
- * nibbler.b64decode('SGVsbG8sIFdvcmxkIQ=='); // returns 'Hello, World!'
- */
-
-/*
-Copyright (c) 2010 Thomas Peri
-http://www.tumuski.com/
-
-Permission is hereby granted, free of charge, to any person obtaining a
-copy of this software and associated documentation files (the
-"Software"), to deal in the Software without restriction, including
-without limitation the rights to use, copy, modify, merge, publish,
-distribute, sublicense, and/or sell copies of the Software, and to
-permit persons to whom the Software is furnished to do so, subject to
-the following conditions:
-
-The above copyright notice and this permission notice shall be included
-in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-/*jslint white: true, browser: true, onevar: true, undef: true, nomen: true,
- eqeqeq: true, plusplus: true, regexp: true, newcap: true, immed: true */
-// (good parts minus bitwise and strict, plus white.)
-
-/**
- * Nibbler - Multi-Base Encoder
- *
- * version 2010-04-07
- *
- * Options:
- * dataBits: The number of bits in each character of unencoded data.
- * codeBits: The number of bits in each character of encoded data.
- * keyString: The characters that correspond to each value when encoded.
- * pad (optional): The character to pad the end of encoded output.
- * arrayData (optional): If truthy, unencoded data is an array instead of a string.
- *
- * Example:
- *
- * var base64_8bit = new Nibbler({
- * dataBits: 8,
- * codeBits: 6,
- * keyString: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
- * pad: '='
- * });
- * base64_8bit.encode("Hello, World!"); // returns "SGVsbG8sIFdvcmxkIQ=="
- * base64_8bit.decode("SGVsbG8sIFdvcmxkIQ=="); // returns "Hello, World!"
- *
- * var base64_7bit = new Nibbler({
- * dataBits: 7,
- * codeBits: 6,
- * keyString: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
- * pad: '='
- * });
- * base64_7bit.encode("Hello, World!"); // returns "kZdmzesQV9/LZkQg=="
- * base64_7bit.decode("kZdmzesQV9/LZkQg=="); // returns "Hello, World!"
- *
- */
-var Nibbler = function (options) {
- var construct,
-
- // options
- pad, dataBits, codeBits, keyString, arrayData,
-
- // private instance variables
- mask, group, max,
-
- // private methods
- gcd, translate,
-
- // public methods
- encode, decode;
-
- // pseudo-constructor
- construct = function () {
- var i, mag, prev;
-
- // options
- pad = options.pad || '';
- dataBits = options.dataBits;
- codeBits = options.codeBits;
- keyString = options.keyString;
- arrayData = options.arrayData;
-
- // bitmasks
- mag = Math.max(dataBits, codeBits);
- prev = 0;
- mask = [];
- for (i = 0; i < mag; i += 1) {
- mask.push(prev);
- prev += prev + 1;
- }
- max = prev;
-
- // ouput code characters in multiples of this number
- group = dataBits / gcd(dataBits, codeBits);
- };
-
- // greatest common divisor
- gcd = function (a, b) {
- var t;
- while (b !== 0) {
- t = b;
- b = a % b;
- a = t;
- }
- return a;
- };
-
- // the re-coder
- translate = function (input, bitsIn, bitsOut, decoding) {
- var i, len, chr, byteIn,
- buffer, size, output,
- write;
-
- // append a byte to the output
- write = function (n) {
- if (!decoding) {
- output.push(keyString.charAt(n));
- } else if (arrayData) {
- output.push(n);
- } else {
- output.push(String.fromCharCode(n));
- }
- };
-
- buffer = 0;
- size = 0;
- output = [];
-
- len = input.length;
- for (i = 0; i < len; i += 1) {
- // the new size the buffer will be after adding these bits
- size += bitsIn;
-
- // read a character
- if (decoding) {
- // decode it
- chr = input.charAt(i);
- byteIn = keyString.indexOf(chr);
- if (chr === pad) {
- break;
- } else if (byteIn < 0) {
- throw 'the character "' + chr + '" is not a member of ' + keyString;
- }
- } else {
- if (arrayData) {
- byteIn = input[i];
- } else {
- byteIn = input.charCodeAt(i);
- }
- if ((byteIn | max) !== max) {
- throw byteIn + " is outside the range 0-" + max;
- }
- }
-
- // shift the buffer to the left and add the new bits
- buffer = (buffer << bitsIn) | byteIn;
-
- // as long as there's enough in the buffer for another output...
- while (size >= bitsOut) {
- // the new size the buffer will be after an output
- size -= bitsOut;
-
- // output the part that lies to the left of that number of bits
- // by shifting the them to the right
- write(buffer >> size);
-
- // remove the bits we wrote from the buffer
- // by applying a mask with the new size
- buffer &= mask[size];
- }
- }
-
- // If we're encoding and there's input left over, pad the output.
- // Otherwise, leave the extra bits off, 'cause they themselves are padding
- if (!decoding && size > 0) {
-
- // flush the buffer
- write(buffer << (bitsOut - size));
-
- // add padding keyString for the remainder of the group
- len = output.length % group;
- for (i = 0; i < len; i += 1) {
- output.push(pad);
- }
- }
-
- // string!
- return (arrayData && decoding) ? output : output.join('');
- };
-
- /**
- * Encode. Input and output are strings.
- */
- encode = function (input) {
- return translate(input, dataBits, codeBits, false);
- };
-
- /**
- * Decode. Input and output are strings.
- */
- decode = function (input) {
- return translate(input, codeBits, dataBits, true);
- };
-
- this.encode = encode;
- this.decode = decode;
- construct();
-};
-
-var Base32 = new Nibbler({
- dataBits: 8,
- codeBits: 5,
- keyString: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
- pad: '='
-});
-var Base64 = new Nibbler({
- dataBits: 8,
- codeBits: 6,
- keyString: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
- pad: '='
-});
-
-exports.Nibbler = Nibbler;
-exports.b32encode = Base32.encode;
-exports.b32decode = Base32.decode;
-exports.b64encode = Base64.encode;
-exports.b64decode = Base64.decode;
Oops, something went wrong. Retry.

0 comments on commit 2479342

Please sign in to comment.