Fix EsLint Brackets (error markup 'const').
This commit is contained in:
+352
@@ -0,0 +1,352 @@
|
||||
import { root } from '../../util/root';
|
||||
import { tryCatch } from '../../util/tryCatch';
|
||||
import { errorObject } from '../../util/errorObject';
|
||||
import { Observable } from '../../Observable';
|
||||
import { Subscriber } from '../../Subscriber';
|
||||
import { map } from '../../operators/map';
|
||||
function getCORSRequest() {
|
||||
if (root.XMLHttpRequest) {
|
||||
return new root.XMLHttpRequest();
|
||||
}
|
||||
else if (!!root.XDomainRequest) {
|
||||
return new root.XDomainRequest();
|
||||
}
|
||||
else {
|
||||
throw new Error('CORS is not supported by your browser');
|
||||
}
|
||||
}
|
||||
function getXMLHttpRequest() {
|
||||
if (root.XMLHttpRequest) {
|
||||
return new root.XMLHttpRequest();
|
||||
}
|
||||
else {
|
||||
let progId;
|
||||
try {
|
||||
const progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
|
||||
for (let i = 0; i < 3; i++) {
|
||||
try {
|
||||
progId = progIds[i];
|
||||
if (new root.ActiveXObject(progId)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
}
|
||||
}
|
||||
return new root.ActiveXObject(progId);
|
||||
}
|
||||
catch (e) {
|
||||
throw new Error('XMLHttpRequest is not supported by your browser');
|
||||
}
|
||||
}
|
||||
}
|
||||
export function ajaxGet(url, headers = null) {
|
||||
return new AjaxObservable({ method: 'GET', url, headers });
|
||||
}
|
||||
export function ajaxPost(url, body, headers) {
|
||||
return new AjaxObservable({ method: 'POST', url, body, headers });
|
||||
}
|
||||
export function ajaxDelete(url, headers) {
|
||||
return new AjaxObservable({ method: 'DELETE', url, headers });
|
||||
}
|
||||
export function ajaxPut(url, body, headers) {
|
||||
return new AjaxObservable({ method: 'PUT', url, body, headers });
|
||||
}
|
||||
export function ajaxPatch(url, body, headers) {
|
||||
return new AjaxObservable({ method: 'PATCH', url, body, headers });
|
||||
}
|
||||
const mapResponse = map((x, index) => x.response);
|
||||
export function ajaxGetJSON(url, headers) {
|
||||
return mapResponse(new AjaxObservable({
|
||||
method: 'GET',
|
||||
url,
|
||||
responseType: 'json',
|
||||
headers
|
||||
}));
|
||||
}
|
||||
export class AjaxObservable extends Observable {
|
||||
constructor(urlOrRequest) {
|
||||
super();
|
||||
const request = {
|
||||
async: true,
|
||||
createXHR: function () {
|
||||
return this.crossDomain ? getCORSRequest() : getXMLHttpRequest();
|
||||
},
|
||||
crossDomain: true,
|
||||
withCredentials: false,
|
||||
headers: {},
|
||||
method: 'GET',
|
||||
responseType: 'json',
|
||||
timeout: 0
|
||||
};
|
||||
if (typeof urlOrRequest === 'string') {
|
||||
request.url = urlOrRequest;
|
||||
}
|
||||
else {
|
||||
for (const prop in urlOrRequest) {
|
||||
if (urlOrRequest.hasOwnProperty(prop)) {
|
||||
request[prop] = urlOrRequest[prop];
|
||||
}
|
||||
}
|
||||
}
|
||||
this.request = request;
|
||||
}
|
||||
_subscribe(subscriber) {
|
||||
return new AjaxSubscriber(subscriber, this.request);
|
||||
}
|
||||
}
|
||||
AjaxObservable.create = (() => {
|
||||
const create = (urlOrRequest) => {
|
||||
return new AjaxObservable(urlOrRequest);
|
||||
};
|
||||
create.get = ajaxGet;
|
||||
create.post = ajaxPost;
|
||||
create.delete = ajaxDelete;
|
||||
create.put = ajaxPut;
|
||||
create.patch = ajaxPatch;
|
||||
create.getJSON = ajaxGetJSON;
|
||||
return create;
|
||||
})();
|
||||
export class AjaxSubscriber extends Subscriber {
|
||||
constructor(destination, request) {
|
||||
super(destination);
|
||||
this.request = request;
|
||||
this.done = false;
|
||||
const headers = request.headers = request.headers || {};
|
||||
if (!request.crossDomain && !headers['X-Requested-With']) {
|
||||
headers['X-Requested-With'] = 'XMLHttpRequest';
|
||||
}
|
||||
if (!('Content-Type' in headers) && !(root.FormData && request.body instanceof root.FormData) && typeof request.body !== 'undefined') {
|
||||
headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
|
||||
}
|
||||
request.body = this.serializeBody(request.body, request.headers['Content-Type']);
|
||||
this.send();
|
||||
}
|
||||
next(e) {
|
||||
this.done = true;
|
||||
const { xhr, request, destination } = this;
|
||||
const response = new AjaxResponse(e, xhr, request);
|
||||
if (response.response === errorObject) {
|
||||
destination.error(errorObject.e);
|
||||
}
|
||||
else {
|
||||
destination.next(response);
|
||||
}
|
||||
}
|
||||
send() {
|
||||
const { request, request: { user, method, url, async, password, headers, body } } = this;
|
||||
const createXHR = request.createXHR;
|
||||
const xhr = tryCatch(createXHR).call(request);
|
||||
if (xhr === errorObject) {
|
||||
this.error(errorObject.e);
|
||||
}
|
||||
else {
|
||||
this.xhr = xhr;
|
||||
this.setupEvents(xhr, request);
|
||||
let result;
|
||||
if (user) {
|
||||
result = tryCatch(xhr.open).call(xhr, method, url, async, user, password);
|
||||
}
|
||||
else {
|
||||
result = tryCatch(xhr.open).call(xhr, method, url, async);
|
||||
}
|
||||
if (result === errorObject) {
|
||||
this.error(errorObject.e);
|
||||
return null;
|
||||
}
|
||||
if (async) {
|
||||
xhr.timeout = request.timeout;
|
||||
xhr.responseType = request.responseType;
|
||||
}
|
||||
if ('withCredentials' in xhr) {
|
||||
xhr.withCredentials = !!request.withCredentials;
|
||||
}
|
||||
this.setHeaders(xhr, headers);
|
||||
result = body ? tryCatch(xhr.send).call(xhr, body) : tryCatch(xhr.send).call(xhr);
|
||||
if (result === errorObject) {
|
||||
this.error(errorObject.e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return xhr;
|
||||
}
|
||||
serializeBody(body, contentType) {
|
||||
if (!body || typeof body === 'string') {
|
||||
return body;
|
||||
}
|
||||
else if (root.FormData && body instanceof root.FormData) {
|
||||
return body;
|
||||
}
|
||||
if (contentType) {
|
||||
const splitIndex = contentType.indexOf(';');
|
||||
if (splitIndex !== -1) {
|
||||
contentType = contentType.substring(0, splitIndex);
|
||||
}
|
||||
}
|
||||
switch (contentType) {
|
||||
case 'application/x-www-form-urlencoded':
|
||||
return Object.keys(body).map(key => `${encodeURIComponent(key)}=${encodeURIComponent(body[key])}`).join('&');
|
||||
case 'application/json':
|
||||
return JSON.stringify(body);
|
||||
default:
|
||||
return body;
|
||||
}
|
||||
}
|
||||
setHeaders(xhr, headers) {
|
||||
for (let key in headers) {
|
||||
if (headers.hasOwnProperty(key)) {
|
||||
xhr.setRequestHeader(key, headers[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
setupEvents(xhr, request) {
|
||||
const progressSubscriber = request.progressSubscriber;
|
||||
function xhrTimeout(e) {
|
||||
const { subscriber, progressSubscriber, request } = xhrTimeout;
|
||||
if (progressSubscriber) {
|
||||
progressSubscriber.error(e);
|
||||
}
|
||||
const ajaxTimeoutError = new AjaxTimeoutError(this, request);
|
||||
if (ajaxTimeoutError.response === errorObject) {
|
||||
subscriber.error(errorObject.e);
|
||||
}
|
||||
else {
|
||||
subscriber.error(ajaxTimeoutError);
|
||||
}
|
||||
}
|
||||
xhr.ontimeout = xhrTimeout;
|
||||
xhrTimeout.request = request;
|
||||
xhrTimeout.subscriber = this;
|
||||
xhrTimeout.progressSubscriber = progressSubscriber;
|
||||
if (xhr.upload && 'withCredentials' in xhr) {
|
||||
if (progressSubscriber) {
|
||||
let xhrProgress;
|
||||
xhrProgress = function (e) {
|
||||
const { progressSubscriber } = xhrProgress;
|
||||
progressSubscriber.next(e);
|
||||
};
|
||||
if (root.XDomainRequest) {
|
||||
xhr.onprogress = xhrProgress;
|
||||
}
|
||||
else {
|
||||
xhr.upload.onprogress = xhrProgress;
|
||||
}
|
||||
xhrProgress.progressSubscriber = progressSubscriber;
|
||||
}
|
||||
let xhrError;
|
||||
xhrError = function (e) {
|
||||
const { progressSubscriber, subscriber, request } = xhrError;
|
||||
if (progressSubscriber) {
|
||||
progressSubscriber.error(e);
|
||||
}
|
||||
const ajaxError = new AjaxError('ajax error', this, request);
|
||||
if (ajaxError.response === errorObject) {
|
||||
subscriber.error(errorObject.e);
|
||||
}
|
||||
else {
|
||||
subscriber.error(ajaxError);
|
||||
}
|
||||
};
|
||||
xhr.onerror = xhrError;
|
||||
xhrError.request = request;
|
||||
xhrError.subscriber = this;
|
||||
xhrError.progressSubscriber = progressSubscriber;
|
||||
}
|
||||
function xhrReadyStateChange(e) {
|
||||
return;
|
||||
}
|
||||
xhr.onreadystatechange = xhrReadyStateChange;
|
||||
xhrReadyStateChange.subscriber = this;
|
||||
xhrReadyStateChange.progressSubscriber = progressSubscriber;
|
||||
xhrReadyStateChange.request = request;
|
||||
function xhrLoad(e) {
|
||||
const { subscriber, progressSubscriber, request } = xhrLoad;
|
||||
if (this.readyState === 4) {
|
||||
let status = this.status === 1223 ? 204 : this.status;
|
||||
let response = (this.responseType === 'text' ? (this.response || this.responseText) : this.response);
|
||||
if (status === 0) {
|
||||
status = response ? 200 : 0;
|
||||
}
|
||||
if (status < 400) {
|
||||
if (progressSubscriber) {
|
||||
progressSubscriber.complete();
|
||||
}
|
||||
subscriber.next(e);
|
||||
subscriber.complete();
|
||||
}
|
||||
else {
|
||||
if (progressSubscriber) {
|
||||
progressSubscriber.error(e);
|
||||
}
|
||||
const ajaxError = new AjaxError('ajax error ' + status, this, request);
|
||||
if (ajaxError.response === errorObject) {
|
||||
subscriber.error(errorObject.e);
|
||||
}
|
||||
else {
|
||||
subscriber.error(ajaxError);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
xhr.onload = xhrLoad;
|
||||
xhrLoad.subscriber = this;
|
||||
xhrLoad.progressSubscriber = progressSubscriber;
|
||||
xhrLoad.request = request;
|
||||
}
|
||||
unsubscribe() {
|
||||
const { done, xhr } = this;
|
||||
if (!done && xhr && xhr.readyState !== 4 && typeof xhr.abort === 'function') {
|
||||
xhr.abort();
|
||||
}
|
||||
super.unsubscribe();
|
||||
}
|
||||
}
|
||||
export class AjaxResponse {
|
||||
constructor(originalEvent, xhr, request) {
|
||||
this.originalEvent = originalEvent;
|
||||
this.xhr = xhr;
|
||||
this.request = request;
|
||||
this.status = xhr.status;
|
||||
this.responseType = xhr.responseType || request.responseType;
|
||||
this.response = parseXhrResponse(this.responseType, xhr);
|
||||
}
|
||||
}
|
||||
function AjaxErrorImpl(message, xhr, request) {
|
||||
Error.call(this);
|
||||
this.message = message;
|
||||
this.name = 'AjaxError';
|
||||
this.xhr = xhr;
|
||||
this.request = request;
|
||||
this.status = xhr.status;
|
||||
this.responseType = xhr.responseType || request.responseType;
|
||||
this.response = parseXhrResponse(this.responseType, xhr);
|
||||
return this;
|
||||
}
|
||||
AjaxErrorImpl.prototype = Object.create(Error.prototype);
|
||||
export const AjaxError = AjaxErrorImpl;
|
||||
function parseJson(xhr) {
|
||||
if ('response' in xhr) {
|
||||
return xhr.responseType ? xhr.response : JSON.parse(xhr.response || xhr.responseText || 'null');
|
||||
}
|
||||
else {
|
||||
return JSON.parse(xhr.responseText || 'null');
|
||||
}
|
||||
}
|
||||
function parseXhrResponse(responseType, xhr) {
|
||||
switch (responseType) {
|
||||
case 'json':
|
||||
return tryCatch(parseJson)(xhr);
|
||||
case 'xml':
|
||||
return xhr.responseXML;
|
||||
case 'text':
|
||||
default:
|
||||
return ('response' in xhr) ? xhr.response : xhr.responseText;
|
||||
}
|
||||
}
|
||||
function AjaxTimeoutErrorImpl(xhr, request) {
|
||||
AjaxError.call(this, 'ajax timeout', xhr, request);
|
||||
this.name = 'AjaxTimeoutError';
|
||||
return this;
|
||||
}
|
||||
export const AjaxTimeoutError = AjaxTimeoutErrorImpl;
|
||||
//# sourceMappingURL=AjaxObservable.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+210
@@ -0,0 +1,210 @@
|
||||
import { Subject, AnonymousSubject } from '../../Subject';
|
||||
import { Subscriber } from '../../Subscriber';
|
||||
import { Observable } from '../../Observable';
|
||||
import { Subscription } from '../../Subscription';
|
||||
import { ReplaySubject } from '../../ReplaySubject';
|
||||
import { tryCatch } from '../../util/tryCatch';
|
||||
import { errorObject } from '../../util/errorObject';
|
||||
const DEFAULT_WEBSOCKET_CONFIG = {
|
||||
url: '',
|
||||
deserializer: (e) => JSON.parse(e.data),
|
||||
serializer: (value) => JSON.stringify(value),
|
||||
};
|
||||
const WEBSOCKETSUBJECT_INVALID_ERROR_OBJECT = 'WebSocketSubject.error must be called with an object with an error code, and an optional reason: { code: number, reason: string }';
|
||||
export class WebSocketSubject extends AnonymousSubject {
|
||||
constructor(urlConfigOrSource, destination) {
|
||||
super();
|
||||
if (urlConfigOrSource instanceof Observable) {
|
||||
this.destination = destination;
|
||||
this.source = urlConfigOrSource;
|
||||
}
|
||||
else {
|
||||
const config = this._config = Object.assign({}, DEFAULT_WEBSOCKET_CONFIG);
|
||||
this._output = new Subject();
|
||||
if (typeof urlConfigOrSource === 'string') {
|
||||
config.url = urlConfigOrSource;
|
||||
}
|
||||
else {
|
||||
for (let key in urlConfigOrSource) {
|
||||
if (urlConfigOrSource.hasOwnProperty(key)) {
|
||||
config[key] = urlConfigOrSource[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!config.WebSocketCtor && WebSocket) {
|
||||
config.WebSocketCtor = WebSocket;
|
||||
}
|
||||
else if (!config.WebSocketCtor) {
|
||||
throw new Error('no WebSocket constructor can be found');
|
||||
}
|
||||
this.destination = new ReplaySubject();
|
||||
}
|
||||
}
|
||||
lift(operator) {
|
||||
const sock = new WebSocketSubject(this._config, this.destination);
|
||||
sock.operator = operator;
|
||||
sock.source = this;
|
||||
return sock;
|
||||
}
|
||||
_resetState() {
|
||||
this._socket = null;
|
||||
if (!this.source) {
|
||||
this.destination = new ReplaySubject();
|
||||
}
|
||||
this._output = new Subject();
|
||||
}
|
||||
multiplex(subMsg, unsubMsg, messageFilter) {
|
||||
const self = this;
|
||||
return new Observable((observer) => {
|
||||
const result = tryCatch(subMsg)();
|
||||
if (result === errorObject) {
|
||||
observer.error(errorObject.e);
|
||||
}
|
||||
else {
|
||||
self.next(result);
|
||||
}
|
||||
let subscription = self.subscribe(x => {
|
||||
const result = tryCatch(messageFilter)(x);
|
||||
if (result === errorObject) {
|
||||
observer.error(errorObject.e);
|
||||
}
|
||||
else if (result) {
|
||||
observer.next(x);
|
||||
}
|
||||
}, err => observer.error(err), () => observer.complete());
|
||||
return () => {
|
||||
const result = tryCatch(unsubMsg)();
|
||||
if (result === errorObject) {
|
||||
observer.error(errorObject.e);
|
||||
}
|
||||
else {
|
||||
self.next(result);
|
||||
}
|
||||
subscription.unsubscribe();
|
||||
};
|
||||
});
|
||||
}
|
||||
_connectSocket() {
|
||||
const { WebSocketCtor, protocol, url, binaryType } = this._config;
|
||||
const observer = this._output;
|
||||
let socket = null;
|
||||
try {
|
||||
socket = protocol ?
|
||||
new WebSocketCtor(url, protocol) :
|
||||
new WebSocketCtor(url);
|
||||
this._socket = socket;
|
||||
if (binaryType) {
|
||||
this._socket.binaryType = binaryType;
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
observer.error(e);
|
||||
return;
|
||||
}
|
||||
const subscription = new Subscription(() => {
|
||||
this._socket = null;
|
||||
if (socket && socket.readyState === 1) {
|
||||
socket.close();
|
||||
}
|
||||
});
|
||||
socket.onopen = (e) => {
|
||||
const { openObserver } = this._config;
|
||||
if (openObserver) {
|
||||
openObserver.next(e);
|
||||
}
|
||||
const queue = this.destination;
|
||||
this.destination = Subscriber.create((x) => {
|
||||
if (socket.readyState === 1) {
|
||||
const { serializer } = this._config;
|
||||
const msg = tryCatch(serializer)(x);
|
||||
if (msg === errorObject) {
|
||||
this.destination.error(errorObject.e);
|
||||
return;
|
||||
}
|
||||
socket.send(msg);
|
||||
}
|
||||
}, (e) => {
|
||||
const { closingObserver } = this._config;
|
||||
if (closingObserver) {
|
||||
closingObserver.next(undefined);
|
||||
}
|
||||
if (e && e.code) {
|
||||
socket.close(e.code, e.reason);
|
||||
}
|
||||
else {
|
||||
observer.error(new TypeError(WEBSOCKETSUBJECT_INVALID_ERROR_OBJECT));
|
||||
}
|
||||
this._resetState();
|
||||
}, () => {
|
||||
const { closingObserver } = this._config;
|
||||
if (closingObserver) {
|
||||
closingObserver.next(undefined);
|
||||
}
|
||||
socket.close();
|
||||
this._resetState();
|
||||
});
|
||||
if (queue && queue instanceof ReplaySubject) {
|
||||
subscription.add(queue.subscribe(this.destination));
|
||||
}
|
||||
};
|
||||
socket.onerror = (e) => {
|
||||
this._resetState();
|
||||
observer.error(e);
|
||||
};
|
||||
socket.onclose = (e) => {
|
||||
this._resetState();
|
||||
const { closeObserver } = this._config;
|
||||
if (closeObserver) {
|
||||
closeObserver.next(e);
|
||||
}
|
||||
if (e.wasClean) {
|
||||
observer.complete();
|
||||
}
|
||||
else {
|
||||
observer.error(e);
|
||||
}
|
||||
};
|
||||
socket.onmessage = (e) => {
|
||||
const { deserializer } = this._config;
|
||||
const result = tryCatch(deserializer)(e);
|
||||
if (result === errorObject) {
|
||||
observer.error(errorObject.e);
|
||||
}
|
||||
else {
|
||||
observer.next(result);
|
||||
}
|
||||
};
|
||||
}
|
||||
_subscribe(subscriber) {
|
||||
const { source } = this;
|
||||
if (source) {
|
||||
return source.subscribe(subscriber);
|
||||
}
|
||||
if (!this._socket) {
|
||||
this._connectSocket();
|
||||
}
|
||||
this._output.subscribe(subscriber);
|
||||
subscriber.add(() => {
|
||||
const { _socket } = this;
|
||||
if (this._output.observers.length === 0) {
|
||||
if (_socket && _socket.readyState === 1) {
|
||||
_socket.close();
|
||||
}
|
||||
this._resetState();
|
||||
}
|
||||
});
|
||||
return subscriber;
|
||||
}
|
||||
unsubscribe() {
|
||||
const { source, _socket } = this;
|
||||
if (_socket && _socket.readyState === 1) {
|
||||
_socket.close();
|
||||
this._resetState();
|
||||
}
|
||||
super.unsubscribe();
|
||||
if (!source) {
|
||||
this.destination = new ReplaySubject();
|
||||
}
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=WebSocketSubject.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+3
@@ -0,0 +1,3 @@
|
||||
import { AjaxObservable } from './AjaxObservable';
|
||||
export const ajax = AjaxObservable.create;
|
||||
//# sourceMappingURL=ajax.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"ajax.js","sources":["../../../../src/internal/observable/dom/ajax.ts"],"names":[],"mappings":"AAAA,OAAO,EAAG,cAAc,EAAuB,MAAM,kBAAkB,CAAC;AAkBxE,MAAM,CAAC,MAAM,IAAI,GAAuB,cAAc,CAAC,MAAM,CAAC"}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import { WebSocketSubject } from './WebSocketSubject';
|
||||
export function webSocket(urlConfigOrSource) {
|
||||
return new WebSocketSubject(urlConfigOrSource);
|
||||
}
|
||||
//# sourceMappingURL=webSocket.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"webSocket.js","sources":["../../../../src/internal/observable/dom/webSocket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAA0B,MAAM,oBAAoB,CAAC;AA4J9E,MAAM,UAAU,SAAS,CAAI,iBAAqD;IAChF,OAAO,IAAI,gBAAgB,CAAI,iBAAiB,CAAC,CAAC;AACpD,CAAC"}
|
||||
Reference in New Issue
Block a user