UNPKG

8.26 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright 2017 Google LLC
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17import { RepoInfo } from '../core/RepoInfo';
18import { PacketReceiver } from './polling/PacketReceiver';
19import { Transport } from './Transport';
20export declare const FIREBASE_LONGPOLL_START_PARAM = "start";
21export declare const FIREBASE_LONGPOLL_CLOSE_COMMAND = "close";
22export declare const FIREBASE_LONGPOLL_COMMAND_CB_NAME = "pLPCommand";
23export declare const FIREBASE_LONGPOLL_DATA_CB_NAME = "pRTLPCB";
24export declare const FIREBASE_LONGPOLL_ID_PARAM = "id";
25export declare const FIREBASE_LONGPOLL_PW_PARAM = "pw";
26export declare const FIREBASE_LONGPOLL_SERIAL_PARAM = "ser";
27export declare const FIREBASE_LONGPOLL_CALLBACK_ID_PARAM = "cb";
28export declare const FIREBASE_LONGPOLL_SEGMENT_NUM_PARAM = "seg";
29export declare const FIREBASE_LONGPOLL_SEGMENTS_IN_PACKET = "ts";
30export declare const FIREBASE_LONGPOLL_DATA_PARAM = "d";
31export declare const FIREBASE_LONGPOLL_DISCONN_FRAME_PARAM = "disconn";
32export declare const FIREBASE_LONGPOLL_DISCONN_FRAME_REQUEST_PARAM = "dframe";
33/**
34 * This class manages a single long-polling connection.
35 */
36export declare class BrowserPollConnection implements Transport {
37 connId: string;
38 repoInfo: RepoInfo;
39 private applicationId?;
40 private appCheckToken?;
41 private authToken?;
42 transportSessionId?: string;
43 lastSessionId?: string;
44 bytesSent: number;
45 bytesReceived: number;
46 urlFn: (params: object) => string;
47 scriptTagHolder: FirebaseIFrameScriptHolder;
48 myDisconnFrame: HTMLIFrameElement;
49 curSegmentNum: number;
50 myPacketOrderer: PacketReceiver;
51 id: string;
52 password: string;
53 private log_;
54 private stats_;
55 private everConnected_;
56 private isClosed_;
57 private connectTimeoutTimer_;
58 private onDisconnect_;
59 /**
60 * @param connId An identifier for this connection, used for logging
61 * @param repoInfo The info for the endpoint to send data to.
62 * @param applicationId The Firebase App ID for this project.
63 * @param appCheckToken The AppCheck token for this client.
64 * @param authToken The AuthToken to use for this connection.
65 * @param transportSessionId Optional transportSessionid if we are
66 * reconnecting for an existing transport session
67 * @param lastSessionId Optional lastSessionId if the PersistentConnection has
68 * already created a connection previously
69 */
70 constructor(connId: string, repoInfo: RepoInfo, applicationId?: string, appCheckToken?: string, authToken?: string, transportSessionId?: string, lastSessionId?: string);
71 /**
72 * @param onMessage - Callback when messages arrive
73 * @param onDisconnect - Callback with connection lost.
74 */
75 open(onMessage: (msg: {}) => void, onDisconnect: (a?: boolean) => void): void;
76 /**
77 * Call this when a handshake has completed successfully and we want to consider the connection established
78 */
79 start(): void;
80 static forceAllow_: boolean;
81 /**
82 * Forces long polling to be considered as a potential transport
83 */
84 static forceAllow(): void;
85 static forceDisallow_: boolean;
86 /**
87 * Forces longpolling to not be considered as a potential transport
88 */
89 static forceDisallow(): void;
90 static isAvailable(): boolean;
91 /**
92 * No-op for polling
93 */
94 markConnectionHealthy(): void;
95 /**
96 * Stops polling and cleans up the iframe
97 */
98 private shutdown_;
99 /**
100 * Triggered when this transport is closed
101 */
102 private onClosed_;
103 /**
104 * External-facing close handler. RealTime has requested we shut down. Kill our connection and tell the server
105 * that we've left.
106 */
107 close(): void;
108 /**
109 * Send the JSON object down to the server. It will need to be stringified, base64 encoded, and then
110 * broken into chunks (since URLs have a small maximum length).
111 * @param data - The JSON data to transmit.
112 */
113 send(data: {}): void;
114 /**
115 * This is how we notify the server that we're leaving.
116 * We aren't able to send requests with DHTML on a window close event, but we can
117 * trigger XHR requests in some browsers (everything but Opera basically).
118 */
119 addDisconnectPingFrame(id: string, pw: string): void;
120 /**
121 * Used to track the bytes received by this client
122 */
123 private incrementIncomingBytes_;
124}
125export interface IFrameElement extends HTMLIFrameElement {
126 doc: Document;
127}
128/*********************************************************************************************
129 * A wrapper around an iframe that is used as a long-polling script holder.
130 *********************************************************************************************/
131export declare class FirebaseIFrameScriptHolder {
132 onDisconnect: () => void;
133 urlFn: (a: object) => string;
134 outstandingRequests: Set<number>;
135 pendingSegs: Array<{
136 seg: number;
137 ts: number;
138 d: unknown;
139 }>;
140 currentSerial: number;
141 sendNewPolls: boolean;
142 uniqueCallbackIdentifier: number;
143 myIFrame: IFrameElement;
144 alive: boolean;
145 myID: string;
146 myPW: string;
147 commandCB: (command: string, ...args: unknown[]) => void;
148 onMessageCB: (...args: unknown[]) => void;
149 /**
150 * @param commandCB - The callback to be called when control commands are recevied from the server.
151 * @param onMessageCB - The callback to be triggered when responses arrive from the server.
152 * @param onDisconnect - The callback to be triggered when this tag holder is closed
153 * @param urlFn - A function that provides the URL of the endpoint to send data to.
154 */
155 constructor(commandCB: (command: string, ...args: unknown[]) => void, onMessageCB: (...args: unknown[]) => void, onDisconnect: () => void, urlFn: (a: object) => string);
156 /**
157 * Each browser has its own funny way to handle iframes. Here we mush them all together into one object that I can
158 * actually use.
159 */
160 private static createIFrame_;
161 /**
162 * Cancel all outstanding queries and remove the frame.
163 */
164 close(): void;
165 /**
166 * Actually start the long-polling session by adding the first script tag(s) to the iframe.
167 * @param id - The ID of this connection
168 * @param pw - The password for this connection
169 */
170 startLongPoll(id: string, pw: string): void;
171 /**
172 * This is called any time someone might want a script tag to be added. It adds a script tag when there aren't
173 * too many outstanding requests and we are still alive.
174 *
175 * If there are outstanding packet segments to send, it sends one. If there aren't, it sends a long-poll anyways if
176 * needed.
177 */
178 private newRequest_;
179 /**
180 * Queue a packet for transmission to the server.
181 * @param segnum - A sequential id for this packet segment used for reassembly
182 * @param totalsegs - The total number of segments in this packet
183 * @param data - The data for this segment.
184 */
185 enqueueSegment(segnum: number, totalsegs: number, data: unknown): void;
186 /**
187 * Add a script tag for a regular long-poll request.
188 * @param url - The URL of the script tag.
189 * @param serial - The serial number of the request.
190 */
191 private addLongPollTag_;
192 /**
193 * Add an arbitrary script tag to the iframe.
194 * @param url - The URL for the script tag source.
195 * @param loadCB - A callback to be triggered once the script has loaded.
196 */
197 addTag(url: string, loadCB: () => void): void;
198}