This commit is contained in:
tatianamac
2019-11-26 14:50:43 -08:00
parent 8a55660ed0
commit 6d5445ecc5
13894 changed files with 2233957 additions and 0 deletions

View File

@@ -0,0 +1,399 @@
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
}));
}
;
/**
* We need this JSDoc comment for affecting ESDoc.
* @extends {Ignored}
* @hide true
*/
export class AjaxObservable extends Observable {
constructor(urlOrRequest) {
super();
const request = {
async: true,
createXHR: function () {
return this.crossDomain ? getCORSRequest.call(this) : getXMLHttpRequest();
},
crossDomain: false,
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;
}
/** @deprecated internal use only */ _subscribe(subscriber) {
return new AjaxSubscriber(subscriber, this.request);
}
}
/**
* Creates an observable for an Ajax request with either a request object with
* url, headers, etc or a string for a URL.
*
* @example
* source = Rx.Observable.ajax('/products');
* source = Rx.Observable.ajax({ url: 'products', method: 'GET' });
*
* @param {string|Object} request Can be one of the following:
* A string of the URL to make the Ajax call.
* An object with the following properties
* - url: URL of the request
* - body: The body of the request
* - method: Method of the request, such as GET, POST, PUT, PATCH, DELETE
* - async: Whether the request is async
* - headers: Optional headers
* - crossDomain: true if a cross domain request, else false
* - createXHR: a function to override if you need to use an alternate
* XMLHttpRequest implementation.
* - resultSelector: a function to use to alter the output value type of
* the Observable. Gets {@link AjaxResponse} as an argument.
* @return {Observable} An observable sequence containing the XMLHttpRequest.
* @static true
* @name ajax
* @owner Observable
*/
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;
})();
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
export class AjaxSubscriber extends Subscriber {
constructor(destination, request) {
super(destination);
this.request = request;
this.done = false;
const headers = request.headers = request.headers || {};
// force CORS if requested
if (!request.crossDomain && !headers['X-Requested-With']) {
headers['X-Requested-With'] = 'XMLHttpRequest';
}
// ensure content type is set
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';
}
// properly serialize body
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);
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;
// set up the events before open XHR
// https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest
// You need to add the event listeners before calling open() on the request.
// Otherwise the progress events will not fire.
this.setupEvents(xhr, request);
// open XHR
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;
}
// timeout, responseType and withCredentials can be set once the XHR is open
if (async) {
xhr.timeout = request.timeout;
xhr.responseType = request.responseType;
}
if ('withCredentials' in xhr) {
xhr.withCredentials = !!request.withCredentials;
}
// set headers
this.setHeaders(xhr, headers);
// finally send the request
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);
}
subscriber.error(new AjaxTimeoutError(this, request)); //TODO: Make betterer.
}
;
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);
}
subscriber.error(new AjaxError('ajax error', this, request));
};
xhr.onerror = xhrError;
xhrError.request = request;
xhrError.subscriber = this;
xhrError.progressSubscriber = progressSubscriber;
}
function xhrReadyStateChange(e) {
const { subscriber, progressSubscriber, request } = xhrReadyStateChange;
if (this.readyState === 4) {
// normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
let status = this.status === 1223 ? 204 : this.status;
let response = (this.responseType === 'text' ? (this.response || this.responseText) : this.response);
// fix status code when it is 0 (0 status is undocumented).
// Occurs when accessing file resources or on Android 4.1 stock browser
// while retrieving files from application cache.
if (status === 0) {
status = response ? 200 : 0;
}
if (200 <= status && status < 300) {
if (progressSubscriber) {
progressSubscriber.complete();
}
subscriber.next(e);
subscriber.complete();
}
else {
if (progressSubscriber) {
progressSubscriber.error(e);
}
subscriber.error(new AjaxError('ajax error ' + status, this, request));
}
}
}
;
xhr.onreadystatechange = xhrReadyStateChange;
xhrReadyStateChange.subscriber = this;
xhrReadyStateChange.progressSubscriber = progressSubscriber;
xhrReadyStateChange.request = request;
}
unsubscribe() {
const { done, xhr } = this;
if (!done && xhr && xhr.readyState !== 4 && typeof xhr.abort === 'function') {
xhr.abort();
}
super.unsubscribe();
}
}
/**
* A normalized AJAX response.
*
* @see {@link ajax}
*
* @class AjaxResponse
*/
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);
}
}
/**
* A normalized AJAX error.
*
* @see {@link ajax}
*
* @class AjaxError
*/
export class AjaxError extends Error {
constructor(message, xhr, request) {
super(message);
this.message = message;
this.xhr = xhr;
this.request = request;
this.status = xhr.status;
this.responseType = xhr.responseType || request.responseType;
this.response = parseXhrResponse(this.responseType, xhr);
}
}
function parseXhrResponse(responseType, xhr) {
switch (responseType) {
case 'json':
if ('response' in xhr) {
//IE does not support json as responseType, parse it internally
return xhr.responseType ? xhr.response : JSON.parse(xhr.response || xhr.responseText || 'null');
}
else {
// HACK(benlesh): TypeScript shennanigans
// tslint:disable-next-line:no-any latest TS seems to think xhr is "never" here.
return JSON.parse(xhr.responseText || 'null');
}
case 'xml':
return xhr.responseXML;
case 'text':
default:
// HACK(benlesh): TypeScript shennanigans
// tslint:disable-next-line:no-any latest TS seems to think xhr is "never" here.
return ('response' in xhr) ? xhr.response : xhr.responseText;
}
}
/**
* @see {@link ajax}
*
* @class AjaxTimeoutError
*/
export class AjaxTimeoutError extends AjaxError {
constructor(xhr, request) {
super('ajax timeout', xhr, request);
}
}
//# sourceMappingURL=AjaxObservable.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,239 @@
import { Subject, AnonymousSubject } from '../../Subject';
import { Subscriber } from '../../Subscriber';
import { Observable } from '../../Observable';
import { Subscription } from '../../Subscription';
import { root } from '../../util/root';
import { ReplaySubject } from '../../ReplaySubject';
import { tryCatch } from '../../util/tryCatch';
import { errorObject } from '../../util/errorObject';
import { assign } from '../../util/assign';
/**
* We need this JSDoc comment for affecting ESDoc.
* @extends {Ignored}
* @hide true
*/
export class WebSocketSubject extends AnonymousSubject {
constructor(urlConfigOrSource, destination) {
if (urlConfigOrSource instanceof Observable) {
super(destination, urlConfigOrSource);
}
else {
super();
this.WebSocketCtor = root.WebSocket;
this._output = new Subject();
if (typeof urlConfigOrSource === 'string') {
this.url = urlConfigOrSource;
}
else {
// WARNING: config object could override important members here.
assign(this, urlConfigOrSource);
}
if (!this.WebSocketCtor) {
throw new Error('no WebSocket constructor can be found');
}
this.destination = new ReplaySubject();
}
}
resultSelector(e) {
return JSON.parse(e.data);
}
/**
* Wrapper around the w3c-compatible WebSocket object provided by the browser.
*
* @example <caption>Wraps browser WebSocket</caption>
*
* let socket$ = Observable.webSocket('ws://localhost:8081');
*
* socket$.subscribe(
* (msg) => console.log('message received: ' + msg),
* (err) => console.log(err),
* () => console.log('complete')
* );
*
* socket$.next(JSON.stringify({ op: 'hello' }));
*
* @example <caption>Wraps WebSocket from nodejs-websocket (using node.js)</caption>
*
* import { w3cwebsocket } from 'websocket';
*
* let socket$ = Observable.webSocket({
* url: 'ws://localhost:8081',
* WebSocketCtor: w3cwebsocket
* });
*
* socket$.subscribe(
* (msg) => console.log('message received: ' + msg),
* (err) => console.log(err),
* () => console.log('complete')
* );
*
* socket$.next(JSON.stringify({ op: 'hello' }));
*
* @param {string | WebSocketSubjectConfig} urlConfigOrSource the source of the websocket as an url or a structure defining the websocket object
* @return {WebSocketSubject}
* @static true
* @name webSocket
* @owner Observable
*/
static create(urlConfigOrSource) {
return new WebSocketSubject(urlConfigOrSource);
}
lift(operator) {
const sock = new WebSocketSubject(this, this.destination);
sock.operator = operator;
return sock;
}
_resetState() {
this.socket = null;
if (!this.source) {
this.destination = new ReplaySubject();
}
this._output = new Subject();
}
// TODO: factor this out to be a proper Operator/Subscriber implementation and eliminate closures
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 } = this;
const observer = this._output;
let socket = null;
try {
socket = this.protocol ?
new WebSocketCtor(this.url, this.protocol) :
new WebSocketCtor(this.url);
this.socket = socket;
if (this.binaryType) {
this.socket.binaryType = this.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.openObserver;
if (openObserver) {
openObserver.next(e);
}
const queue = this.destination;
this.destination = Subscriber.create((x) => socket.readyState === 1 && socket.send(x), (e) => {
const closingObserver = this.closingObserver;
if (closingObserver) {
closingObserver.next(undefined);
}
if (e && e.code) {
socket.close(e.code, e.reason);
}
else {
observer.error(new TypeError('WebSocketSubject.error must be called with an object with an error code, ' +
'and an optional reason: { code: number, reason: string }'));
}
this._resetState();
}, () => {
const closingObserver = this.closingObserver;
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.closeObserver;
if (closeObserver) {
closeObserver.next(e);
}
if (e.wasClean) {
observer.complete();
}
else {
observer.error(e);
}
};
socket.onmessage = (e) => {
const result = tryCatch(this.resultSelector)(e);
if (result === errorObject) {
observer.error(errorObject.e);
}
else {
observer.next(result);
}
};
}
/** @deprecated internal use only */ _subscribe(subscriber) {
const { source } = this;
if (source) {
return source.subscribe(subscriber);
}
if (!this.socket) {
this._connectSocket();
}
let subscription = new Subscription();
subscription.add(this._output.subscribe(subscriber));
subscription.add(() => {
const { socket } = this;
if (this._output.observers.length === 0) {
if (socket && socket.readyState === 1) {
socket.close();
}
this._resetState();
}
});
return subscription;
}
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

File diff suppressed because one or more lines are too long

3
node_modules/rxjs/_esm2015/observable/dom/ajax.js generated vendored Normal file
View File

@@ -0,0 +1,3 @@
import { AjaxObservable } from './AjaxObservable';
export const ajax = AjaxObservable.create;
//# sourceMappingURL=ajax.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"ajax.js","sourceRoot":"","sources":["../../../src/observable/dom/ajax.ts"],"names":[],"mappings":"OAAO,EAAG,cAAc,EAAuB,MAAM,kBAAkB;AAEvE,OAAO,MAAM,IAAI,GAAuB,cAAc,CAAC,MAAM,CAAC"}

View File

@@ -0,0 +1,3 @@
import { WebSocketSubject } from './WebSocketSubject';
export const webSocket = WebSocketSubject.create;
//# sourceMappingURL=webSocket.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"webSocket.js","sourceRoot":"","sources":["../../../src/observable/dom/webSocket.ts"],"names":[],"mappings":"OAAO,EAAG,gBAAgB,EAAG,MAAM,oBAAoB;AAEvD,OAAO,MAAM,SAAS,GAAG,gBAAgB,CAAC,MAAM,CAAC"}