-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathformdata.js
147 lines (133 loc) · 4.03 KB
/
formdata.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Generated by CoffeeScript 1.7.1
(function(self) {
var BOUNDARY, BlobPart, CRLF, FormData, StringPart, support;
if (self.FormData) {
return;
}
support = {
arrayBuffer: !!self.ArrayBuffer,
blob: !!self.FileReader && !!self.Blob && (function() {
try {
new Blob();
return true;
} catch (_error) {
return false;
}
})()
};
CRLF = "\r\n";
BOUNDARY = "--------FormData" + Math.random();
StringPart = (function() {
function StringPart(name, value) {
this.name = name;
this.value = value;
}
StringPart.prototype.convertToString = function() {
var lines;
lines = [];
return new Promise((function(_this) {
return function(resolve) {
lines.push("--" + BOUNDARY + CRLF);
lines.push("Content-Disposition: form-data; name=\"" + _this.name + "\";" + CRLF + CRLF);
lines.push("" + _this.value + CRLF);
return resolve(lines.join(''));
};
})(this));
};
return StringPart;
})();
BlobPart = (function() {
function BlobPart(name, filename, souce) {
this.name = name;
this.filename = filename;
this.souce = souce;
}
BlobPart.prototype._readArrayBufferAsString = function(buff) {
var view;
view = new Uint8Array(buff);
return view.reduce(function(acc, b) {
acc.push(String.fromCharCode(b));
return acc;
}, new Array(view.length)).join('');
};
BlobPart.prototype._readBlobAsArrayBuffer = function() {
return new Promise((function(_this) {
return function(resolve) {
var reader;
reader = new FileReader();
reader.readAsArrayBuffer(_this.souce);
return reader.onload = function() {
return resolve(_this._readArrayBufferAsString(reader.result));
};
};
})(this));
};
BlobPart.prototype._readBlobAsBinary = function() {
return new Promise((function(_this) {
return function(resolve) {
return resolve(_this.souce.getAsBinary());
};
})(this));
};
BlobPart.prototype.convertToString = function() {
var lines;
lines = [];
lines.push("--" + BOUNDARY + CRLF);
lines.push("Content-Disposition: form-data; name=\"" + this.name + "\"; filename=\"" + this.filename + "\"" + CRLF);
lines.push("Content-Type: " + this.souce.type + CRLF + CRLF);
if (support.blob && support.arrayBuffer) {
return this._readBlobAsArrayBuffer().then(function(strings) {
lines.push(strings + CRLF);
return lines.join('');
});
} else {
return this._readBlobAsBinary().then(function(strings) {
lines.push(strings + CRLF);
return lines.join('');
});
}
};
return BlobPart;
})();
FormData = (function() {
function FormData() {
this.polyfill = true;
this._parts = [];
this.boundary = BOUNDARY;
}
FormData.prototype._stringToTypedArray = function(string) {
var bytes;
bytes = Array.prototype.map.call(string, function(s) {
return s.charCodeAt(0);
});
return new Uint8Array(bytes);
};
FormData.prototype.append = function(key, value) {
var part;
part = null;
if (typeof value === "string") {
part = new StringPart(key, value);
} else if (value instanceof Blob) {
part = new BlobPart(key, value.name || "blob", value);
} else {
part = new StringPart(key, value);
}
if (part) {
this._parts.push(part);
}
return this;
};
FormData.prototype.toString = function() {
var parts;
parts = this._parts;
return Promise.all(this._parts.map(function(part) {
return part.convertToString();
})).then(function(lines) {
lines.push("--" + BOUNDARY + "--");
return lines.join('');
}).then(this._stringToTypedArray);
};
return FormData;
})();
return self.FormData = FormData;
})(typeof self !== 'undefined' ? self : this);