1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = Object.setPrototypeOf ||
|
4 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
6 | return function (d, b) {
|
7 | extendStatics(d, b);
|
8 | function __() { this.constructor = d; }
|
9 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
10 | };
|
11 | })();
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | var xml_http_request_event_target_1 = require("./xml-http-request-event-target");
|
14 | var XMLHttpRequestUpload = (function (_super) {
|
15 | __extends(XMLHttpRequestUpload, _super);
|
16 | function XMLHttpRequestUpload() {
|
17 | var _this = _super.call(this) || this;
|
18 | _this._contentType = null;
|
19 | _this._body = null;
|
20 | _this._reset();
|
21 | return _this;
|
22 | }
|
23 | XMLHttpRequestUpload.prototype._reset = function () {
|
24 | this._contentType = null;
|
25 | this._body = null;
|
26 | };
|
27 | XMLHttpRequestUpload.prototype._setData = function (data) {
|
28 | if (data == null) {
|
29 | return;
|
30 | }
|
31 | if (typeof data === 'string') {
|
32 | if (data.length !== 0) {
|
33 | this._contentType = 'text/plain;charset=UTF-8';
|
34 | }
|
35 | this._body = new Buffer(data, 'utf-8');
|
36 | }
|
37 | else if (Buffer.isBuffer(data)) {
|
38 | this._body = data;
|
39 | }
|
40 | else if (data instanceof ArrayBuffer) {
|
41 | var body = new Buffer(data.byteLength);
|
42 | var view = new Uint8Array(data);
|
43 | for (var i = 0; i < data.byteLength; i++) {
|
44 | body[i] = view[i];
|
45 | }
|
46 | this._body = body;
|
47 | }
|
48 | else if (data.buffer && data.buffer instanceof ArrayBuffer) {
|
49 | var body = new Buffer(data.byteLength);
|
50 | var offset = data.byteOffset;
|
51 | var view = new Uint8Array(data.buffer);
|
52 | for (var i = 0; i < data.byteLength; i++) {
|
53 | body[i] = view[i + offset];
|
54 | }
|
55 | this._body = body;
|
56 | }
|
57 | else {
|
58 | throw new Error("Unsupported send() data " + data);
|
59 | }
|
60 | };
|
61 | XMLHttpRequestUpload.prototype._finalizeHeaders = function (headers, loweredHeaders) {
|
62 | if (this._contentType && !loweredHeaders['content-type']) {
|
63 | headers['Content-Type'] = this._contentType;
|
64 | }
|
65 | if (this._body) {
|
66 | headers['Content-Length'] = this._body.length.toString();
|
67 | }
|
68 | };
|
69 | XMLHttpRequestUpload.prototype._startUpload = function (request) {
|
70 | if (this._body) {
|
71 | request.write(this._body);
|
72 | }
|
73 | request.end();
|
74 | };
|
75 | return XMLHttpRequestUpload;
|
76 | }(xml_http_request_event_target_1.XMLHttpRequestEventTarget));
|
77 | exports.XMLHttpRequestUpload = XMLHttpRequestUpload;
|
78 |
|
\ | No newline at end of file |