Skip to content

Commit 3193651

Browse files
committed
WIP: working on the broken npm test command
1 parent 379a263 commit 3193651

File tree

14 files changed

+175
-112
lines changed

14 files changed

+175
-112
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ npm-debug.log
66
/.nyc_output
77
/tests/config
88
/temp
9-
/.vscode
9+
/.vscode
10+
/.ts-node

gulp/tasks/test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ function runBrowserUnitTests(dev) {
6767
// we don't want this file as it references files that only exist once compiled
6868
`./src/firebase-*.ts`,
6969

70+
// We don't want to load the node env
71+
`./src/utils/nodePatches.ts`,
72+
7073
// Don't include node test files
7174
'./tests/**/node/**/*.test.ts',
7275

@@ -114,7 +117,7 @@ function runAllKarmaTests(done) {
114117
// list of files to exclude from the included globs above
115118
exclude: [
116119
// we don't want this file as it references files that only exist once compiled
117-
`./src/firebase.ts`,
120+
`./src/firebase-*.ts`,
118121

119122
// Don't include node test files
120123
'./tests/**/node/**/*.test.ts',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"babel-preset-env": "^1.2.1",
4040
"chai": "^3.5.0",
4141
"child-process-promise": "^2.2.1",
42+
"cross-env": "^5.0.1",
4243
"cz-customizable": "^5.0.0",
4344
"filesize": "^3.5.6",
4445
"git-rev-sync": "^1.9.0",

src/database/core/util/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export const buildLogMessage_ = function(var_args) {
114114
* Use this for all debug messages in Firebase.
115115
* @type {?function(string)}
116116
*/
117-
export var logger = null;
117+
export var logger = console.log.bind(console);
118118

119119

120120
/**

src/database/core/view/filter/RangedFilter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IndexedFilter } from "./IndexedFilter";
22
import { PRIORITY_INDEX } from "../../../core/snap/indexes/PriorityIndex";
3-
import { NamedNode } from "../../../core/snap/Node";
3+
import { Node, NamedNode } from "../../../core/snap/Node";
44
import { ChildrenNode } from "../../../core/snap/ChildrenNode";
55
/**
66
* Filters nodes by range and uses an IndexFilter to track any changes after filtering the node

src/database/realtime/BrowserPollConnection.ts

Lines changed: 15 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ import { Transport } from './Transport';
1818
import { RepoInfo } from '../core/RepoInfo';
1919

2020
// URL query parameters associated with longpolling
21-
const FIREBASE_LONGPOLL_START_PARAM = 'start';
22-
const FIREBASE_LONGPOLL_CLOSE_COMMAND = 'close';
23-
const FIREBASE_LONGPOLL_COMMAND_CB_NAME = 'pLPCommand';
24-
const FIREBASE_LONGPOLL_DATA_CB_NAME = 'pRTLPCB';
25-
const FIREBASE_LONGPOLL_ID_PARAM = 'id';
26-
const FIREBASE_LONGPOLL_PW_PARAM = 'pw';
27-
const FIREBASE_LONGPOLL_SERIAL_PARAM = 'ser';
28-
const FIREBASE_LONGPOLL_CALLBACK_ID_PARAM = 'cb';
29-
const FIREBASE_LONGPOLL_SEGMENT_NUM_PARAM = 'seg';
30-
const FIREBASE_LONGPOLL_SEGMENTS_IN_PACKET = 'ts';
31-
const FIREBASE_LONGPOLL_DATA_PARAM = 'd';
32-
const FIREBASE_LONGPOLL_DISCONN_FRAME_PARAM = 'disconn';
33-
const FIREBASE_LONGPOLL_DISCONN_FRAME_REQUEST_PARAM = 'dframe';
21+
export const FIREBASE_LONGPOLL_START_PARAM = 'start';
22+
export const FIREBASE_LONGPOLL_CLOSE_COMMAND = 'close';
23+
export const FIREBASE_LONGPOLL_COMMAND_CB_NAME = 'pLPCommand';
24+
export const FIREBASE_LONGPOLL_DATA_CB_NAME = 'pRTLPCB';
25+
export const FIREBASE_LONGPOLL_ID_PARAM = 'id';
26+
export const FIREBASE_LONGPOLL_PW_PARAM = 'pw';
27+
export const FIREBASE_LONGPOLL_SERIAL_PARAM = 'ser';
28+
export const FIREBASE_LONGPOLL_CALLBACK_ID_PARAM = 'cb';
29+
export const FIREBASE_LONGPOLL_SEGMENT_NUM_PARAM = 'seg';
30+
export const FIREBASE_LONGPOLL_SEGMENTS_IN_PACKET = 'ts';
31+
export const FIREBASE_LONGPOLL_DATA_PARAM = 'd';
32+
export const FIREBASE_LONGPOLL_DISCONN_FRAME_PARAM = 'disconn';
33+
export const FIREBASE_LONGPOLL_DISCONN_FRAME_REQUEST_PARAM = 'dframe';
3434

3535
//Data size constants.
3636
//TODO: Perf: the maximum length actually differs from browser to browser.
@@ -340,7 +340,7 @@ export class BrowserPollConnection implements Transport {
340340
};
341341
}
342342

343-
interface IFrameElement extends HTMLIFrameElement {
343+
export interface IFrameElement extends HTMLIFrameElement {
344344
doc: Document;
345345
}
346346

@@ -352,7 +352,7 @@ interface IFrameElement extends HTMLIFrameElement {
352352
* @param onDisconnect - The callback to be triggered when this tag holder is closed
353353
* @param urlFn - A function that provides the URL of the endpoint to send data to.
354354
*********************************************************************************************/
355-
class FirebaseIFrameScriptHolder {
355+
export class FirebaseIFrameScriptHolder {
356356
//We maintain a count of all of the outstanding requests, because if we have too many active at once it can cause
357357
//problems in some browsers.
358358
/**
@@ -650,53 +650,3 @@ class FirebaseIFrameScriptHolder {
650650
}
651651
}
652652
}
653-
654-
if (isNodeSdk()) {
655-
/**
656-
* @type {?function({url: string, forever: boolean}, function(Error, number, string))}
657-
*/
658-
(FirebaseIFrameScriptHolder as any).request = null;
659-
660-
/**
661-
* @param {{url: string, forever: boolean}} req
662-
* @param {function(string)=} onComplete
663-
*/
664-
(FirebaseIFrameScriptHolder as any).nodeRestRequest = function(req, onComplete) {
665-
if (!(FirebaseIFrameScriptHolder as any).request)
666-
(FirebaseIFrameScriptHolder as any).request =
667-
/** @type {function({url: string, forever: boolean}, function(Error, number, string))} */ (require('request'));
668-
669-
(FirebaseIFrameScriptHolder as any).request(req, function(error, response, body) {
670-
if (error)
671-
throw 'Rest request for ' + req.url + ' failed.';
672-
673-
if (onComplete)
674-
onComplete(body);
675-
});
676-
};
677-
678-
/**
679-
* @param {!string} url
680-
* @param {function()} loadCB
681-
*/
682-
(<any>FirebaseIFrameScriptHolder.prototype).doNodeLongPoll = function(url, loadCB) {
683-
var self = this;
684-
(FirebaseIFrameScriptHolder as any).nodeRestRequest({ url: url, forever: true }, function(body) {
685-
self.evalBody(body);
686-
loadCB();
687-
});
688-
};
689-
690-
/**
691-
* Evaluates the string contents of a jsonp response.
692-
* @param {!string} body
693-
*/
694-
(<any>FirebaseIFrameScriptHolder.prototype).evalBody = function(body) {
695-
var jsonpCB;
696-
//jsonpCB is externed in firebase-extern.js
697-
eval('jsonpCB = function(' + FIREBASE_LONGPOLL_COMMAND_CB_NAME + ', ' + FIREBASE_LONGPOLL_DATA_CB_NAME + ') {' +
698-
body +
699-
'}');
700-
jsonpCB(this.commandCB, this.onMessageCB);
701-
};
702-
}

src/database/realtime/Connection.ts

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class Connection {
107107
*/
108108
private start_() {
109109
const conn = this.transportManager_.initialTransport();
110-
this.conn_ = new conn(this.nextTransportId_(), this.repoInfo_, /*transportSessionId=*/undefined, this.lastSessionId);
110+
this.conn_ = new conn(this.nextTransportId_(), this.repoInfo_, undefined, this.lastSessionId);
111111

112112
// For certain transports (WebSockets), we need to send and receive several messages back and forth before we
113113
// can consider the transport healthy.
@@ -120,37 +120,36 @@ export class Connection {
120120
this.secondaryConn_ = null;
121121
this.isHealthy_ = false;
122122

123-
const self = this;
124123
/*
125124
* Firefox doesn't like when code from one iframe tries to create another iframe by way of the parent frame.
126125
* This can occur in the case of a redirect, i.e. we guessed wrong on what server to connect to and received a reset.
127126
* Somehow, setTimeout seems to make this ok. That doesn't make sense from a security perspective, since you should
128127
* still have the context of your originating frame.
129128
*/
130-
setTimeout(function () {
129+
setTimeout(() => {
131130
// self.conn_ gets set to null in some of the tests. Check to make sure it still exists before using it
132-
self.conn_ && self.conn_.open(onMessageReceived, onConnectionLost);
131+
this.conn_ && this.conn_.open(onMessageReceived, onConnectionLost);
133132
}, Math.floor(0));
134133

135134

136135
const healthyTimeout_ms = conn['healthyTimeout'] || 0;
137136
if (healthyTimeout_ms > 0) {
138-
this.healthyTimeout_ = setTimeoutNonBlocking(function () {
139-
self.healthyTimeout_ = null;
140-
if (!self.isHealthy_) {
141-
if (self.conn_ && self.conn_.bytesReceived > BYTES_RECEIVED_HEALTHY_OVERRIDE) {
142-
self.log_('Connection exceeded healthy timeout but has received ' + self.conn_.bytesReceived +
137+
this.healthyTimeout_ = setTimeoutNonBlocking(() => {
138+
this.healthyTimeout_ = null;
139+
if (!this.isHealthy_) {
140+
if (this.conn_ && this.conn_.bytesReceived > BYTES_RECEIVED_HEALTHY_OVERRIDE) {
141+
this.log_('Connection exceeded healthy timeout but has received ' + this.conn_.bytesReceived +
143142
' bytes. Marking connection healthy.');
144-
self.isHealthy_ = true;
145-
self.conn_.markConnectionHealthy();
146-
} else if (self.conn_ && self.conn_.bytesSent > BYTES_SENT_HEALTHY_OVERRIDE) {
147-
self.log_('Connection exceeded healthy timeout but has sent ' + self.conn_.bytesSent +
143+
this.isHealthy_ = true;
144+
this.conn_.markConnectionHealthy();
145+
} else if (this.conn_ && this.conn_.bytesSent > BYTES_SENT_HEALTHY_OVERRIDE) {
146+
this.log_('Connection exceeded healthy timeout but has sent ' + this.conn_.bytesSent +
148147
' bytes. Leaving connection alive.');
149148
// NOTE: We don't want to mark it healthy, since we have no guarantee that the bytes have made it to
150149
// the server.
151150
} else {
152-
self.log_('Closing unhealthy connection after timeout.');
153-
self.close();
151+
this.log_('Closing unhealthy connection after timeout.');
152+
this.close();
154153
}
155154
}
156155
}, Math.floor(healthyTimeout_ms));
@@ -166,29 +165,27 @@ export class Connection {
166165
};
167166

168167
private disconnReceiver_(conn) {
169-
const self = this;
170-
return function (everConnected) {
171-
if (conn === self.conn_) {
172-
self.onConnectionLost_(everConnected);
173-
} else if (conn === self.secondaryConn_) {
174-
self.log_('Secondary connection lost.');
175-
self.onSecondaryConnectionLost_();
168+
return everConnected => {
169+
if (conn === this.conn_) {
170+
this.onConnectionLost_(everConnected);
171+
} else if (conn === this.secondaryConn_) {
172+
this.log_('Secondary connection lost.');
173+
this.onSecondaryConnectionLost_();
176174
} else {
177-
self.log_('closing an old connection');
175+
this.log_('closing an old connection');
178176
}
179177
}
180178
};
181179

182180
private connReceiver_(conn) {
183-
const self = this;
184-
return function (message) {
185-
if (self.state_ != REALTIME_STATE_DISCONNECTED) {
186-
if (conn === self.rx_) {
187-
self.onPrimaryMessageReceived_(message);
188-
} else if (conn === self.secondaryConn_) {
189-
self.onSecondaryMessageReceived_(message);
181+
return message => {
182+
if (this.state_ != REALTIME_STATE_DISCONNECTED) {
183+
if (conn === this.rx_) {
184+
this.onPrimaryMessageReceived_(message);
185+
} else if (conn === this.secondaryConn_) {
186+
this.onSecondaryMessageReceived_(message);
190187
} else {
191-
self.log_('message on old connection');
188+
this.log_('message on old connection');
192189
}
193190
}
194191
};

src/database/realtime/WebSocketConnection.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ const WEBSOCKET_MAX_FRAME_SIZE = 16384;
1616
const WEBSOCKET_KEEPALIVE_INTERVAL = 45000;
1717

1818
let WebSocketImpl = null;
19-
if (isNodeSdk()) {
20-
WebSocketImpl = require('faye-websocket')['Client'];
21-
} else if (typeof MozWebSocket !== 'undefined') {
19+
if (typeof MozWebSocket !== 'undefined') {
2220
WebSocketImpl = MozWebSocket;
2321
} else if (typeof WebSocket !== 'undefined') {
2422
WebSocketImpl = WebSocket;
2523
}
2624

25+
export function setWebSocketImpl(impl) {
26+
WebSocketImpl = impl;
27+
}
28+
2729
/**
2830
* Create a new websocket connection with the given callbacks.
2931
* @constructor
@@ -122,11 +124,9 @@ export class WebSocketConnection implements Transport {
122124
}
123125

124126
this.mySock = new WebSocketImpl(this.connURL, [], options);
125-
}
126-
else {
127+
} else {
127128
this.mySock = new WebSocketImpl(this.connURL);
128129
}
129-
this.mySock = new WebSocketImpl(this.connURL);
130130
} catch (e) {
131131
this.log_('Error instantiating WebSocket.');
132132
const error = e.message || e.data;

src/firebase-node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import firebase from "./app";
1818
import './auth';
1919
import './database';
20-
import './database/nodePatches';
20+
import './utils/nodePatches';
2121

2222

2323
var Storage = require('dom-storage');

src/utils/nodePatches.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import { CONSTANTS } from "./constants";
2+
import { setWebSocketImpl } from "../../src/database/realtime/WebSocketConnection";
3+
import {
4+
FirebaseIFrameScriptHolder,
5+
FIREBASE_LONGPOLL_COMMAND_CB_NAME,
6+
FIREBASE_LONGPOLL_DATA_CB_NAME
7+
} from "../../src/database/realtime/BrowserPollConnection";
28

39
// Overriding the constant (we should be the only ones doing this)
410
CONSTANTS.NODE_CLIENT = true;
511

12+
setWebSocketImpl(require('faye-websocket').Client);
13+
614
/**
715
* @suppress {es5Strict}
816
*/
@@ -120,3 +128,51 @@ CONSTANTS.NODE_CLIENT = true;
120128
var Duplex = require('_stream_duplex');
121129
Duplex['prototype']['write'] = Writable['prototype']['write'];
122130
})();
131+
132+
/**
133+
* @type {?function({url: string, forever: boolean}, function(Error, number, string))}
134+
*/
135+
(FirebaseIFrameScriptHolder as any).request = null;
136+
137+
/**
138+
* @param {{url: string, forever: boolean}} req
139+
* @param {function(string)=} onComplete
140+
*/
141+
(FirebaseIFrameScriptHolder as any).nodeRestRequest = function(req, onComplete) {
142+
if (!(FirebaseIFrameScriptHolder as any).request)
143+
(FirebaseIFrameScriptHolder as any).request =
144+
/** @type {function({url: string, forever: boolean}, function(Error, number, string))} */ (require('request'));
145+
146+
(FirebaseIFrameScriptHolder as any).request(req, function(error, response, body) {
147+
if (error)
148+
throw 'Rest request for ' + req.url + ' failed.';
149+
150+
if (onComplete)
151+
onComplete(body);
152+
});
153+
};
154+
155+
/**
156+
* @param {!string} url
157+
* @param {function()} loadCB
158+
*/
159+
(<any>FirebaseIFrameScriptHolder.prototype).doNodeLongPoll = function(url, loadCB) {
160+
var self = this;
161+
(FirebaseIFrameScriptHolder as any).nodeRestRequest({ url: url, forever: true }, function(body) {
162+
self.evalBody(body);
163+
loadCB();
164+
});
165+
};
166+
167+
/**
168+
* Evaluates the string contents of a jsonp response.
169+
* @param {!string} body
170+
*/
171+
(<any>FirebaseIFrameScriptHolder.prototype).evalBody = function(body) {
172+
var jsonpCB;
173+
//jsonpCB is externed in firebase-extern.js
174+
eval('jsonpCB = function(' + FIREBASE_LONGPOLL_COMMAND_CB_NAME + ', ' + FIREBASE_LONGPOLL_DATA_CB_NAME + ') {' +
175+
body +
176+
'}');
177+
jsonpCB(this.commandCB, this.onMessageCB);
178+
};

0 commit comments

Comments
 (0)