Comparing version 1.2.3 to 1.3.0
{ | ||
"name": "maltypart", | ||
"description": "Maltypart is a simple multipart request builder.", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"authors": [ | ||
@@ -6,0 +6,0 @@ "Jason Miller <jasonmiller@synacor.com>" |
@@ -50,9 +50,14 @@ /** Maltypart is a simple multipart builder. | ||
* @memberOf module:maltypart | ||
* @param {Object|Array} fields | ||
* @param {String} [boundary=random] | ||
* @param {Object|Array} fields A key-value Object map of fields to append | ||
* @param {String} [boundary=random] Override the default MIME part boundary | ||
* @param {String} [callback] If set, invoked after appending `fields` | ||
*/ | ||
function RequestBody(fields, boundary) { | ||
function RequestBody(fields, boundary, callback) { | ||
this.fields = {}; | ||
if (typeof boundary==='function') { | ||
callback = boundary; | ||
boundary = null; | ||
} | ||
if (fields) { | ||
this.append(fields); | ||
this.append(fields, callback); | ||
} | ||
@@ -106,8 +111,18 @@ this.setBoundary(boundary); | ||
append : function(fields, replace) { | ||
var name, i; | ||
var name, i, pending, callback; | ||
if (typeof fields==='string' && arguments.length>=2) { | ||
this.setField.apply(this, arguments); | ||
return this.setField.apply(this, arguments); | ||
} | ||
else if (isArray(fields)) { | ||
if (typeof replace==='function') { | ||
pending = 1; | ||
callback = replace; | ||
replace = function() { | ||
if (!--pending) callback(); | ||
}; | ||
} | ||
if (isArray(fields)) { | ||
for (i=0; i<fields.length; i++) { | ||
pending++; | ||
this.setField(fields[i].name, fields[i].value, replace); | ||
@@ -119,2 +134,3 @@ } | ||
if (fields.hasOwnProperty(name)) { | ||
pending++; | ||
this.setField(name, fields[name], replace); | ||
@@ -124,2 +140,5 @@ } | ||
} | ||
if (pending) replace(); | ||
return this; | ||
@@ -142,2 +161,6 @@ }, | ||
} | ||
if (typeof replace==='function') { | ||
callback = replace; | ||
replace = true; | ||
} | ||
if (value && value.nodeType && value.nodeName && value.getAttribute) { | ||
@@ -161,2 +184,5 @@ value = value.value; | ||
} | ||
if (typeof callback==='function') { | ||
callback(); | ||
} | ||
return this; | ||
@@ -163,0 +189,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
/*! maltypart 1.2.3 */ | ||
!function(a,b){"function"==typeof define&&define.amd?define([],b):"object"==typeof module&&module.exports?module.exports=b():a.maltypart=b()}(this,function(){function a(a,b){this.fields={},a&&this.append(a),this.setBoundary(b)}function b(a,b,c,d){this.data=a,this.contentType=b,this.filename=c,this.headers=d}function c(a){return"[object Array]"===Object.prototype.toString.call(a)}function d(){return Math.random().toString(36).substring(2)+(++f).toString(36)}function e(a){for(var b="",c=new Uint8Array(a),d=0;d<c.byteLength;d++)b+=String.fromCharCode(c[d]);return b}var f=0;return a.prototype={constructor:a,availableTypes:["multipart","form-encoded"],setType:function(a){return this.typeOverride=-1!==this.availableTypes.indexOf(a)?a:null,this},getType:function(){var a=this.typeOverride;if(-1!==this.availableTypes.indexOf(a))return a;for(var b in this.fields)if(this.fields.hasOwnProperty(b)&&this.fields[b].contentType)return"multipart";return"form-encoded"},append:function(a,b){var d,e;if("string"==typeof a&&arguments.length>=2)this.setField.apply(this,arguments);else if(c(a))for(e=0;e<a.length;e++)this.setField(a[e].name,a[e].value,b);else for(d in a)a.hasOwnProperty(d)&&this.setField(d,a[d],b);return this},setField:function(a,d,e){var f=this.fields;return a+="",window.File&&d instanceof window.File||window.Blob&&d instanceof window.Blob?this.setFileField.apply(this,arguments):(d&&d.nodeType&&d.nodeName&&d.getAttribute?d=d.value:c(d)||d&&(d instanceof b||d.contentType&&d.data)||(d+=""),a.match(/\[\]$/g)&&(e=!1),e===!1&&f.hasOwnProperty(a)?(c(f[a])||(f[a]=[f[a]]),f[a].push(d)):f[a]=d,this)},setFileField:function(a,c,d,f,g){var h=this,i=new FileReader;return"function"==typeof d&&(g=d,d=!0),i.onloadend=function(){h.setField(a,new b(e(i.result),c.type,c.name,f),d!==!1),"function"==typeof g&&g(),c=g=h=i=null},i.readAsArrayBuffer(c),this},setBoundary:function(a){return this.boundary=(a||d())+"",this},toString:function(a){var b,d,e,f,g,h,i,j,k,l=this.getType(),m="";this.boundary||this.setBoundary(),a&&this.append(a);for(d in this.fields)if(this.fields.hasOwnProperty(d))for(e=this.fields[d],b=c(e)?e:[e],i=0;i<b.length;i++){if(h=b[i],"multipart"===l){if(j=h.headers,f=null,h.contentType&&(f=h.contentType,h=h.data),g="--"+this.boundary+'\r\nContent-Disposition: form-data; name="'+d+'"',b[i].filename&&(g+='; filename="'+b[i].filename+'"'),g+="\r\n",f&&(g+="Content-type: "+f+"\r\n"),j)for(k in j)j.hasOwnProperty(k)&&(g+=k+": "+j[k]+"\r\n");g+="\r\n"+h+"\r\n"}else"form-encoded"===l&&(g=encodeURIComponent(d)+"="+encodeURIComponent(h)+"&");m+=g}return"multipart"===l?m+="--"+this.boundary+"--\r\n":"form-encoded"===l&&(m=m.substring(0,m.length-1)),m},getData:function(){var a,b,c=this.toString();if("multipart"===this.getType()){a=c.length,b=new Uint8Array(a);for(var d=0;a>d;d++)b[d]=255&c.charCodeAt(d);return b}return c},getContentType:function(a){var b=this.getType();return"multipart"===b?"multipart/form-data; "+(a?"charset=utf-8; ":"")+"boundary="+this.boundary:"application/x-www-form-encoded"}},{RequestBody:a,RequestField:b}}); | ||
/*! maltypart 1.3.0 */ | ||
!function(a,b){"function"==typeof define&&define.amd?define([],b):"object"==typeof module&&module.exports?module.exports=b():a.maltypart=b()}(this,function(){function a(a,b,c){this.fields={},"function"==typeof b&&(c=b,b=null),a&&this.append(a,c),this.setBoundary(b)}function b(a,b,c,d){this.data=a,this.contentType=b,this.filename=c,this.headers=d}function c(a){return"[object Array]"===Object.prototype.toString.call(a)}function d(){return Math.random().toString(36).substring(2)+(++f).toString(36)}function e(a){for(var b="",c=new Uint8Array(a),d=0;d<c.byteLength;d++)b+=String.fromCharCode(c[d]);return b}var f=0;return a.prototype={constructor:a,availableTypes:["multipart","form-encoded"],setType:function(a){return this.typeOverride=-1!==this.availableTypes.indexOf(a)?a:null,this},getType:function(){var a=this.typeOverride;if(-1!==this.availableTypes.indexOf(a))return a;for(var b in this.fields)if(this.fields.hasOwnProperty(b)&&this.fields[b].contentType)return"multipart";return"form-encoded"},append:function(a,b){var d,e,f,g;if("string"==typeof a&&arguments.length>=2)return this.setField.apply(this,arguments);if("function"==typeof b&&(f=1,g=b,b=function(){--f||g()}),c(a))for(e=0;e<a.length;e++)f++,this.setField(a[e].name,a[e].value,b);else for(d in a)a.hasOwnProperty(d)&&(f++,this.setField(d,a[d],b));return f&&b(),this},setField:function(a,d,e,f,g){var h=this.fields;return a+="",window.File&&d instanceof window.File||window.Blob&&d instanceof window.Blob?this.setFileField.apply(this,arguments):("function"==typeof e&&(g=e,e=!0),d&&d.nodeType&&d.nodeName&&d.getAttribute?d=d.value:c(d)||d&&(d instanceof b||d.contentType&&d.data)||(d+=""),a.match(/\[\]$/g)&&(e=!1),e===!1&&h.hasOwnProperty(a)?(c(h[a])||(h[a]=[h[a]]),h[a].push(d)):h[a]=d,"function"==typeof g&&g(),this)},setFileField:function(a,c,d,f,g){var h=this,i=new FileReader;return"function"==typeof d&&(g=d,d=!0),i.onloadend=function(){h.setField(a,new b(e(i.result),c.type,c.name,f),d!==!1),"function"==typeof g&&g(),c=g=h=i=null},i.readAsArrayBuffer(c),this},setBoundary:function(a){return this.boundary=(a||d())+"",this},toString:function(a){var b,d,e,f,g,h,i,j,k,l=this.getType(),m="";this.boundary||this.setBoundary(),a&&this.append(a);for(d in this.fields)if(this.fields.hasOwnProperty(d))for(e=this.fields[d],b=c(e)?e:[e],i=0;i<b.length;i++){if(h=b[i],"multipart"===l){if(j=h.headers,f=null,h.contentType&&(f=h.contentType,h=h.data),g="--"+this.boundary+'\r\nContent-Disposition: form-data; name="'+d+'"',b[i].filename&&(g+='; filename="'+b[i].filename+'"'),g+="\r\n",f&&(g+="Content-type: "+f+"\r\n"),j)for(k in j)j.hasOwnProperty(k)&&(g+=k+": "+j[k]+"\r\n");g+="\r\n"+h+"\r\n"}else"form-encoded"===l&&(g=encodeURIComponent(d)+"="+encodeURIComponent(h)+"&");m+=g}return"multipart"===l?m+="--"+this.boundary+"--\r\n":"form-encoded"===l&&(m=m.substring(0,m.length-1)),m},getData:function(){var a,b,c=this.toString();if("multipart"===this.getType()){a=c.length,b=new Uint8Array(a);for(var d=0;a>d;d++)b[d]=255&c.charCodeAt(d);return b}return c},getContentType:function(a){var b=this.getType();return"multipart"===b?"multipart/form-data; "+(a?"charset=utf-8; ":"")+"boundary="+this.boundary:"application/x-www-form-encoded"}},{RequestBody:a,RequestField:b}}); |
{ | ||
"name": "maltypart", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"description": "Maltypart is a simple multipart request builder.", | ||
@@ -5,0 +5,0 @@ "main": "dist/maltypart.js", |
@@ -50,9 +50,14 @@ /** Maltypart is a simple multipart builder. | ||
* @memberOf module:maltypart | ||
* @param {Object|Array} fields | ||
* @param {String} [boundary=random] | ||
* @param {Object|Array} fields A key-value Object map of fields to append | ||
* @param {String} [boundary=random] Override the default MIME part boundary | ||
* @param {String} [callback] If set, invoked after appending `fields` | ||
*/ | ||
function RequestBody(fields, boundary) { | ||
function RequestBody(fields, boundary, callback) { | ||
this.fields = {}; | ||
if (typeof boundary==='function') { | ||
callback = boundary; | ||
boundary = null; | ||
} | ||
if (fields) { | ||
this.append(fields); | ||
this.append(fields, callback); | ||
} | ||
@@ -106,8 +111,18 @@ this.setBoundary(boundary); | ||
append : function(fields, replace) { | ||
var name, i; | ||
var name, i, pending, callback; | ||
if (typeof fields==='string' && arguments.length>=2) { | ||
this.setField.apply(this, arguments); | ||
return this.setField.apply(this, arguments); | ||
} | ||
else if (isArray(fields)) { | ||
if (typeof replace==='function') { | ||
pending = 1; | ||
callback = replace; | ||
replace = function() { | ||
if (!--pending) callback(); | ||
}; | ||
} | ||
if (isArray(fields)) { | ||
for (i=0; i<fields.length; i++) { | ||
pending++; | ||
this.setField(fields[i].name, fields[i].value, replace); | ||
@@ -119,2 +134,3 @@ } | ||
if (fields.hasOwnProperty(name)) { | ||
pending++; | ||
this.setField(name, fields[name], replace); | ||
@@ -124,2 +140,5 @@ } | ||
} | ||
if (pending) replace(); | ||
return this; | ||
@@ -142,2 +161,6 @@ }, | ||
} | ||
if (typeof replace==='function') { | ||
callback = replace; | ||
replace = true; | ||
} | ||
if (value && value.nodeType && value.nodeName && value.getAttribute) { | ||
@@ -161,2 +184,5 @@ value = value.value; | ||
} | ||
if (typeof callback==='function') { | ||
callback(); | ||
} | ||
return this; | ||
@@ -163,0 +189,0 @@ }, |
@@ -28,3 +28,3 @@ describe('maltypart', function() { | ||
expect(spy).to.have.been.calledOnce; | ||
expect(spy).to.have.been.calledWithExactly(fields); | ||
expect(spy).to.have.been.calledWith(fields); | ||
spy.restore(); | ||
@@ -89,3 +89,33 @@ }); | ||
}); | ||
describe('#append', function() { | ||
it('should invoke callbacks for single fields', function() { | ||
var body = new maltypart.RequestBody(), | ||
callback = sinon.spy(); | ||
body.append('foo', 'bar', callback); | ||
expect(callback).to.have.been.calledOnce; | ||
body.append('test', new maltypart.RequestField('<h1>hi</h1>', 'text/html'), callback); | ||
expect(callback).to.have.been.calledTwice; | ||
}); | ||
it('should invoke callbacks for multiple fields', function() { | ||
var body = new maltypart.RequestBody(), | ||
callback = sinon.spy(); | ||
body.append({ | ||
foo: 'bar', | ||
test: new maltypart.RequestField('<h1>hi</h1>', 'text/html') | ||
}, callback); | ||
expect(callback).to.have.been.calledOnce; | ||
body.append([ | ||
{ name: 'foo2', value: 'bar' }, | ||
{ name: 'test2', value: new maltypart.RequestField('<h1>hi</h1>', 'text/html') } | ||
], callback); | ||
expect(callback).to.have.been.calledTwice; | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
701840
16079