Comparing version 1.3.0 to 1.4.0
@@ -109,4 +109,4 @@ /** Maltypart is a simple multipart builder. | ||
*/ | ||
append : function(fields, replace) { | ||
var name, i, pending, callback; | ||
append : function(fields, replace, callback) { | ||
var name, val, i, pending, cb; | ||
if (typeof fields==='string' && arguments.length>=2) { | ||
@@ -117,5 +117,10 @@ return this.setField.apply(this, arguments); | ||
if (typeof replace==='function') { | ||
i = callback; | ||
callback = replace; | ||
replace = i; | ||
} | ||
if (typeof callback==='function') { | ||
pending = 1; | ||
callback = replace; | ||
replace = function() { | ||
cb = function() { | ||
if (!--pending) callback(); | ||
@@ -127,4 +132,5 @@ }; | ||
for (i=0; i<fields.length; i++) { | ||
val = fields[i]; | ||
pending++; | ||
this.setField(fields[i].name, fields[i].value, replace); | ||
this.setField(val.name, val.value, replace!==false, null, cb); | ||
} | ||
@@ -135,4 +141,13 @@ } | ||
if (fields.hasOwnProperty(name)) { | ||
pending++; | ||
this.setField(name, fields[name], replace); | ||
val = fields[name]; | ||
if (isArray(val)) { | ||
for (i=0; i<val.length; i++) { | ||
pending++; | ||
this.setField(name, val[i], replace===true, null, cb); | ||
} | ||
} | ||
else { | ||
pending++; | ||
this.setField(name, val, replace!==false, null, cb); | ||
} | ||
} | ||
@@ -142,3 +157,3 @@ } | ||
if (pending) replace(); | ||
if (pending) cb(); | ||
@@ -145,0 +160,0 @@ return this; |
@@ -1,2 +0,2 @@ | ||
/*! 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}}); | ||
/*! maltypart 1.4.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,d){var e,f,g,h,i;if("string"==typeof a&&arguments.length>=2)return this.setField.apply(this,arguments);if("function"==typeof b&&(g=d,d=b,b=g),"function"==typeof d&&(h=1,i=function(){--h||d()}),c(a))for(g=0;g<a.length;g++)f=a[g],h++,this.setField(f.name,f.value,b!==!1,null,i);else for(e in a)if(a.hasOwnProperty(e))if(f=a[e],c(f))for(g=0;g<f.length;g++)h++,this.setField(e,f[g],b===!0,null,i);else h++,this.setField(e,f,b!==!1,null,i);return h&&i(),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}}); |
@@ -46,2 +46,3 @@ module.exports = function(grunt) { | ||
options : { | ||
multistr : true, // allow unsafe line breaks (for test fixtures and whatnot) | ||
'-W030' : true | ||
@@ -48,0 +49,0 @@ }, |
{ | ||
"name": "maltypart", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Maltypart is a simple multipart request builder.", | ||
@@ -5,0 +5,0 @@ "main": "dist/maltypart.js", |
@@ -109,4 +109,4 @@ /** Maltypart is a simple multipart builder. | ||
*/ | ||
append : function(fields, replace) { | ||
var name, i, pending, callback; | ||
append : function(fields, replace, callback) { | ||
var name, val, i, pending, cb; | ||
if (typeof fields==='string' && arguments.length>=2) { | ||
@@ -117,5 +117,10 @@ return this.setField.apply(this, arguments); | ||
if (typeof replace==='function') { | ||
i = callback; | ||
callback = replace; | ||
replace = i; | ||
} | ||
if (typeof callback==='function') { | ||
pending = 1; | ||
callback = replace; | ||
replace = function() { | ||
cb = function() { | ||
if (!--pending) callback(); | ||
@@ -127,4 +132,5 @@ }; | ||
for (i=0; i<fields.length; i++) { | ||
val = fields[i]; | ||
pending++; | ||
this.setField(fields[i].name, fields[i].value, replace); | ||
this.setField(val.name, val.value, replace!==false, null, cb); | ||
} | ||
@@ -135,4 +141,13 @@ } | ||
if (fields.hasOwnProperty(name)) { | ||
pending++; | ||
this.setField(name, fields[name], replace); | ||
val = fields[name]; | ||
if (isArray(val)) { | ||
for (i=0; i<val.length; i++) { | ||
pending++; | ||
this.setField(name, val[i], replace===true, null, cb); | ||
} | ||
} | ||
else { | ||
pending++; | ||
this.setField(name, val, replace!==false, null, cb); | ||
} | ||
} | ||
@@ -142,3 +157,3 @@ } | ||
if (pending) replace(); | ||
if (pending) cb(); | ||
@@ -145,0 +160,0 @@ return this; |
@@ -117,4 +117,77 @@ describe('maltypart', function() { | ||
}); | ||
it('should append multiple fields with the same name', function() { | ||
var body = new maltypart.RequestBody(), | ||
callback = sinon.spy(); | ||
body.append({ | ||
foo: 'bar', | ||
test: new maltypart.RequestField('<h1>hi</h1>', 'text/html'), | ||
multiple: [ | ||
new maltypart.RequestField('<a>a</a>', 'text/html'), | ||
new maltypart.RequestField('b', 'text/plain'), | ||
new maltypart.RequestField('{"c":"c"}', 'text/json') | ||
] | ||
}, callback); | ||
expect(callback).to.have.been.calledOnce; | ||
expect(body.fields) | ||
.to.have.property('test') | ||
.that.is.an('object'); | ||
expect(body.fields) | ||
.to.have.property('multiple') | ||
.that.is.an('array') | ||
.with.length(3); | ||
}); | ||
it('should serialize complex example', function() { | ||
var body = new maltypart.RequestBody(), | ||
callback = sinon.spy(); | ||
body.append({ | ||
foo: 'bar', | ||
test: new maltypart.RequestField('<h1>hi</h1>', 'text/html'), | ||
multiple: [ | ||
new maltypart.RequestField('<a>a</a>', 'text/html'), | ||
new maltypart.RequestField('b', 'text/plain'), | ||
new maltypart.RequestField('{"c":"c"}', 'text/json') | ||
] | ||
}); | ||
var boundary = body.boundary; | ||
var reference = '\ | ||
--'+boundary+'\r\n\ | ||
Content-Disposition: form-data; name="foo"\r\n\ | ||
\r\n\ | ||
bar\r\n\ | ||
--'+boundary+'\r\n\ | ||
Content-Disposition: form-data; name="test"\r\n\ | ||
Content-type: text/html\r\n\ | ||
\r\n\ | ||
<h1>hi</h1>\r\n\ | ||
--'+boundary+'\r\n\ | ||
Content-Disposition: form-data; name="multiple"\r\n\ | ||
Content-type: text/html\r\n\ | ||
\r\n\ | ||
<a>a</a>\r\n\ | ||
--'+boundary+'\r\n\ | ||
Content-Disposition: form-data; name="multiple"\r\n\ | ||
Content-type: text/plain\r\n\ | ||
\r\n\ | ||
b\r\n\ | ||
--'+boundary+'\r\n\ | ||
Content-Disposition: form-data; name="multiple"\r\n\ | ||
Content-type: text/json\r\n\ | ||
\r\n\ | ||
{"c":"c"}\r\n\ | ||
--'+boundary+'--\r\n\ | ||
'; | ||
expect(body.toString()).to.equal(reference); | ||
}); | ||
}); | ||
}); | ||
}); |
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
704899
16171