forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
freedom.d.ts
580 lines (502 loc) · 20.6 KB
/
freedom.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
// Type definitions for freedom v0.6.29
// Project: https://github.com/freedomjs/freedom
// Definitions by: Jonathan Pevarnek <https://github.com/jpevarnek/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace freedom {
// Common on/emit for message passing interfaces.
interface EventDispatchFn<T> { (eventType: string, value?: T): void; }
interface EventHandlerFn<T> {
(eventType: string, handler: (eventData:T) => void): void;
}
interface Error {
errcode: string;
message: string;
}
// TODO: replace OnAndEmit with EventHandler and EventEmitter;
interface OnAndEmit<T,T2> {
on: EventHandlerFn<T>;
emit: EventDispatchFn<T2>;
}
interface EventHandler {
// Adds |f| as an event handler for all subsiquent events of type |t|.
on(t: string, f: Function): void;
// Adds |f| as an event handler for only the next event of type |t|.
once(t: string, f: Function): void;
// The |off| function removes the event event handling function |f| from
// both |on| and the |once| event handling.
off(t: string, f: Function): void;
}
interface PortModule<T, T2> extends OnAndEmit<T, T2> {
controlChannel: string;
}
interface ModuleSelfConstructor {
// Identifies a named API's provider class.
provideSynchronous: (classFn?: Function) => void;
provideAsynchronous :(classFn?: Function) => void;
providePromises: (classFn?: Function) => void;
}
interface ParentModuleThing extends ModuleSelfConstructor, OnAndEmit<any, any> {
}
interface Logger {
debug(...args: any[]): void;
info(...args: any[]): void;
log(...args: any[]): void;
warn(...args: any[]): void;
error(...args: any[]): void;
}
// See |Core_unprivileged| in |core.unprivileged.js|
interface Core {
// Create a new channel which which to communicate between modules.
createChannel(): Promise<ChannelSpecifier>;
// Given an ChannelEndpointIdentifier for a channel, create a proxy event
// interface for it.
bindChannel(channelIdentifier: string): Promise<Channel>;
// Returns the list of identifiers describing the dependency path.
getId(): Promise<string[]>;
getLogger(tag: string): Promise<Logger>;
}
// Channels are ways that freedom modules can send each other messages.
interface Channel extends OnAndEmit<any,any> {
close(): void;
}
// Specification for a channel.
interface ChannelSpecifier {
channel: Channel; // How to communicate over this channel.
// A freedom channel endpoint identifier. Can be passed over a freedom
// message-passing boundary. It is used to create a channel to the freedom
// module that called createChannel and created this ChannelSpecifier.
identifier: string;
}
// This is the first argument given to a core provider's constructor. It is an
// object that describes the parent module the core provider instance has been
// created for.
interface CoreProviderParentApp {
manifestId: string;
config: {
views: {[viewName: string]: Object};
};
global: {
removeEventListener: (s: string, f: Function, b: boolean) => void;
};
}
// A Freedom module sub is both a function and an object with members. The
// type |T| is the type of the module's stub interface.
interface FreedomModuleFactoryManager<T> {
// This is the factory constructor for a new instance of a stub/channel to a
// module.
(...args: any[]): T;
// This is the call to close a particular stub's channel and resources. It
// is assumed that the argument is a result of the factory constructor. If
// no argument is supplied, all stubs are closed.
close: (freedomModuleStubInstance?: T) => Promise<void>;
api: string;
}
interface FreedomInCoreEnvOptions {
debug?: string; // debug level
logger?: string; // string to json for logging provider.
}
interface FreedomInCoreEnv extends OnAndEmit<any,any> {
// Represents the call to freedom when you create a root module. Returns a
// promise to a factory constructor for the freedom module. The
// |manifestPath| should be a path to a json string that specifies the
// freedom module.
(manifestPath: string, options?: FreedomInCoreEnvOptions):
Promise<FreedomModuleFactoryManager<any>>;
}
interface FreedomInModuleEnv {
// Represents the call to freedom(), which returns the parent module's
// freedom stub interface in an on/emit style. This is a getter.
(): ParentModuleThing;
// Creates an interface to the freedom core provider which can be used to
// create loggers and channels.
// Note: unlike other providers, core is a getter.
'core': FreedomModuleFactoryManager<Core>;
'core.console': FreedomModuleFactoryManager<Console.Console>;
'core.rtcdatachannel': FreedomModuleFactoryManager<RTCDataChannel.RTCDataChannel>;
'core.rtcpeerconnection': FreedomModuleFactoryManager<RTCPeerConnection.RTCPeerConnection>;
'core.storage': FreedomModuleFactoryManager<Storage.Storage>;
'core.tcpsocket': FreedomModuleFactoryManager<TcpSocket.Socket>;
'core.udpsocket': FreedomModuleFactoryManager<UdpSocket.Socket>;
'pgp': FreedomModuleFactoryManager<PgpProvider.PgpProvider>;
'portControl': FreedomModuleFactoryManager<PortControl.PortControl>;
// We use this specification so that you can reference freedom sub-modules by
// an array-lookup of its name. One day, maybe we'll have a nicer way to do
// this.
[moduleName: string]: FreedomModuleFactoryManager<any>;
}
// This generic interface represents any freedom method. Its purpose is to extend
// the basic definition to include the reckless call method, which does not
// produce a reply message.
interface Method0<R> {
(): Promise<R>;
reckless: () => void;
}
interface Method1<T, R> {
(a: T): Promise<R>;
reckless: (a: T) => void;
}
interface Method2<T, U, R> {
(a: T, b: U) : Promise<R>;
reckless: (a: T, b: U) => void;
}
interface Method3<T, U, V, R> {
(a: T, b: U, c: V): Promise<R>;
reckless: (a: T, b: U, c: V) => void;
}
}
declare namespace freedom.Console {
interface Console {
log(source: string, message: string): Promise<void>;
debug(source: string, message: string): Promise<void>;
info(source: string, message: string): Promise<void>;
warn(source: string, message: string): Promise<void>;
error(source: string, message: string): Promise<void>;
}
}
declare namespace freedom.RTCDataChannel {
interface Message {
// Exactly one of the below must be specified.
text?: string;
buffer?: ArrayBuffer;
binary?: Blob; // Not yet supported in Chrome.
}
// Constructed by |freedom['rtcdatachannel'](id)| where |id| is a string
// representing the channel id created by an |rtcpeerconnection| object.
interface RTCDataChannel {
getLabel(): Promise<string>;
getOrdered(): Promise<boolean>;
getMaxPacketLifeTime(): Promise<number>;
getMaxRetransmits(): Promise<number>;
getProtocol(): Promise<string>;
getNegotiated(): Promise<boolean>;
getId(): Promise<number>;
getReadyState(): Promise<string>;
getBufferedAmount(): Promise<number>;
on(t: 'onopen', f: () => void): void;
on(t: 'onerror', f: () => void): void;
on(t: 'onclose', f: () => void): void;
on(t: 'onmessage', f: (m: Message) => void): void;
on(t: string, f: Function): void;
close(): Promise<void>;
getBinaryType(): Promise<string>;
setBinaryType(type: string): Promise<void>;
send: freedom.Method1<string, void>;
sendBuffer: freedom.Method1<ArrayBuffer, void>;
}
}
declare namespace freedom.RTCPeerConnection {
interface RTCIceServer {
urls: string[];
username?: string;
credential?: string;
}
interface RTCConfiguration {
iceServers: RTCIceServer[];
iceTransports?: string;
peerIdentity?: string;
}
interface RTCOfferOptions {
offerToReceiveVideo?: number;
offerToReceiveAudio?: number;
voiceActivityDetection?: boolean;
iceRestart?: boolean;
}
interface RTCSessionDescription {
type: string;
sdp: string;
}
interface RTCIceCandidate {
candidate: string;
sdpMid?: string;
sdpMLineIndex?: number;
}
interface OnIceCandidateEvent {
candidate: RTCIceCandidate
}
interface RTCDataChannelInit {
ordered?: boolean;
maxPacketLifeTime?: number;
maxRetransmits?: number;
protocol?: string;
negotiated?: boolean;
id?: number;
}
// Note: the freedom factory constructor
// |freedom['rtcpeerconnection'](config)| to create an RTCPeerConnection has
// |RTCConfiguration| as the type of its config its argument.
interface RTCPeerConnection {
createOffer(options?: RTCOfferOptions): Promise<RTCSessionDescription>;
createAnswer(): Promise<RTCSessionDescription>;
setLocalDescription(desc: RTCSessionDescription): Promise<void>;
getLocalDescription(): Promise<RTCSessionDescription>;
setRemoteDescription(desc: RTCSessionDescription): Promise<void>;
getRemoteDescription(): Promise<RTCSessionDescription>;
getSignalingState(): Promise<string>;
updateIce(configuration: RTCConfiguration): Promise<void>;
addIceCandidate(candidate: RTCIceCandidate): Promise<void>;
getIceGatheringState(): Promise<string>;
getIceConnectionState(): Promise<string>;
getConfiguration(): Promise<RTCConfiguration>;
getLocalStreams(): Promise<string[]>;
getRemoteStreams(): Promise<string[]>;
getStreamById(id: string): Promise<string>;
addStream(ref: string): Promise<void>;
removeStream(ref: string): Promise<void>;
close(): Promise<void>;
createDataChannel(label: string, init: RTCDataChannelInit): Promise<string>;
getStats(selector?: string): Promise<any>;
on(t: 'ondatachannel', f: (d: {channel: string}) => void): void;
on(t: 'onnegotiationneeded', f: () => void): void;
on(t: 'onicecandidate', f: (d: OnIceCandidateEvent) => void): void;
on(t: 'onsignalingstatechange', f: () => void): void;
on(t: 'onaddstream', f: (d: {stream: number}) => void): void;
on(t: 'onremovestream', f: (d: {stream: number}) => void): void;
on(t: 'oniceconnectionstatechange', f: () => void): void;
on(t: string, f: Function): void;
}
}
declare namespace freedom.Storage {
interface Storage {
// Fetch array of all keys.
keys(): Promise<string[]>;
// Fetch a value for a key.
get(key: string): Promise<string>;
// Sets a value to a key. Fulfills promise with the previous value, if it
// exists.
set(key: string, value: string): Promise<string>;
// Remove a single key. Fulfills promise with previous value, if exists.
remove(key: string): Promise<string>;
// Remove all data from storage.
clear(): Promise<void>;
} // class Storage
}
declare namespace freedom.TcpSocket {
interface DisconnectInfo {
errcode: string;
message: string;
}
interface ReadInfo {
data: ArrayBuffer;
}
interface WriteInfo {
bytesWritten: number;
}
interface SocketInfo {
connected: boolean;
localAddress?: string;
localPort?: number;
peerAddress?: string;
peerPort?: number;
}
interface ConnectInfo {
socket: number;
host: string;
port: number;
}
// The TcpSocket class (freedom['core.TcpSocket'])
interface Socket {
listen(address: string, port: number): Promise<void>;
connect(hostname: string, port: number): Promise<void>;
secure(): Promise<void>;
write: freedom.Method1<ArrayBuffer, WriteInfo>;
pause: freedom.Method0<void>;
resume: freedom.Method0<void>;
getInfo(): Promise<SocketInfo>;
close(): Promise<void>;
// TcpSockets have 3 types of events:
on(type: 'onConnection', f: (i: ConnectInfo) => void): void;
on(type: 'onData', f: (i:ReadInfo) => void): void;
off(type: 'onData', f: (i: ReadInfo) => void): void;
on(type: 'onDisconnect', f: (i: DisconnectInfo) => void): void;
on(eventType: string, f: (i: Object) => void): void;
off(eventType: string, f: (i: Object) => void): void;
}
}
declare namespace freedom.UdpSocket {
// Type for the chrome.socket.getInfo callback:
// https://developer.chrome.com/apps/sockets_udp#type-SocketInfo
// This is also the type returned by getInfo().
interface SocketInfo {
// Note that there are other fields but these are the ones we care about.
localAddress: string;
localPort: number;
}
// Type for the chrome.socket.recvFrom callback:
// http://developer.chrome.com/apps/socket#method-recvFrom
// This is also the type returned to onData callbacks.
interface RecvFromInfo {
resultCode: number;
address: string;
port: number;
data: ArrayBuffer;
}
interface Implementation {
bind(address: string, port: number, continuation: () => void) : void;
sendTo(data: ArrayBuffer, address: string, port: number,
continuation: (bytesWritten: number) => void): void;
destroy(continuation: () => void): void;
getInfo(continuation: (socketInfo: SocketInfo) => void): void;
}
interface Socket {
bind: (address: string, port: number) => Promise<void>;
sendTo: freedom.Method3<ArrayBuffer, string, number, number>;
destroy: () => Promise<void>;
on: (name: string, listener: Function) => void;
getInfo: () => Promise<SocketInfo>;
}
}
declare namespace freedom.PgpProvider {
interface PublicKey {
key: string;
fingerprint: string;
words: string[];
}
interface KeyFingerprint {
fingerprint: string;
words: string[];
}
interface VerifyDecryptResult {
data: ArrayBuffer;
signedBy: string[];
}
interface PgpProvider {
// Standard freedom crypto API
setup(passphrase: string, userid: string): Promise<void>;
clear(): Promise<void>;
exportKey(): Promise<PublicKey>;
getFingerprint(publicKey: string): Promise<KeyFingerprint>;
ecdhBob(curve: string, pubKey: string): Promise<ArrayBuffer>;
signEncrypt(data: ArrayBuffer, encryptKey?: string,
sign?: boolean): Promise<ArrayBuffer>;
verifyDecrypt(data: ArrayBuffer,
verifyKey?: string): Promise<VerifyDecryptResult>;
armor(data: ArrayBuffer, type?: string): Promise<string>;
dearmor(data: string): Promise<ArrayBuffer>;
}
}
declare namespace freedom.PortControl {
interface Mapping {
internalIp: string;
internalPort: number;
externalIp?: string;
externalPort: number;
lifetime: number;
protocol: string;
timeoutId?: number;
nonce?: number[];
errInfo?: string;
}
// A collection of Mappings
interface ActiveMappings {
[extPort: string]: Mapping;
}
// An object returned by probeProtocolSupport()
interface ProtocolSupport {
natPmp: boolean;
pcp: boolean;
upnp: boolean;
}
// Main interface for the module
interface PortControl {
addMapping(intPort: number, extPort: number, lifetime: number): Promise<Mapping>;
deleteMapping(extPort: number): Promise<boolean>;
probeProtocolSupport(): Promise<ProtocolSupport>;
probePmpSupport(): Promise<boolean>;
addMappingPmp(intPort: number, extPort: number, lifetime: number): Promise<Mapping>;
deleteMappingPmp(extPort: number): Promise<boolean>;
probePcpSupport(): Promise<boolean>;
addMappingPcp(intPort: number, extPort: number, lifetime: number): Promise<Mapping>;
deleteMappingPcp(extPort: number): Promise<boolean>;
probeUpnpSupport(): Promise<boolean>;
addMappingUpnp(intPort: number, extPort: number, lifetime: number,
controlUrl?: string): Promise<Mapping>;
deleteMappingUpnp(extPort: number): Promise<boolean>;
getActiveMappings(): Promise<ActiveMappings>;
getPrivateIps(): Promise<string[]>;
close(): Promise<void>;
}
}
declare namespace freedom.Social {
// Status of a client connected to a social network.
interface ClientState {
userId: string;
clientId: string;
status: string; // Either ONLINE, OFFLINE, or ONLINE_WITH_OTHER_APP
timestamp: number;
}
// The profile of a user on a social network.
interface UserProfile {
userId: string;
name: string;
status?: number;
url?: string;
// Image URI (e.g. data:image/png;base64,adkwe329...)
imageData?: string;
timestamp?: number;
}
interface Users {
[userId: string]: UserProfile;
}
interface Clients {
[clientId: string]: ClientState;
}
// Event for an incoming messages
interface IncomingMessage {
// UserID/ClientID/status of user from whom the message comes from.
from: ClientState;
// Message contents.
message: string;
}
// A request to login to a specific network as a specific agent
interface LoginRequest {
// Name of the application connecting to the network. Other logins with
// the same agent field will be listed as having status |ONLINE|, where
// those with different agents will be listed as
// |ONLINE_WITH_OTHER_CLIENT|
agent: string;
// Version of application
version: string;
// URL of application
url: string;
// When |interactive === true| social will always prompt user for login.
// Promise fails if the user did not login or provided invalid
// credentials. When |interactive === false|, promise fails unless the
// social provider has cached tokens/credentials.
interactive: boolean;
// When true, social provider will remember the token/credentials.
rememberLogin: boolean;
}
interface Social {
// Generic Freedom Event stuff. |on| binds an event handler to event type
// |eventType|. Every time |eventType| event is raised, the function |f|
// will be called.
//
// Message type |onMessage| happens when the user receives a message from
// another contact.
on(eventType: string, f: Function) : void;
on(eventType: 'onMessage', f: (message: IncomingMessage) => void): void;
// Message type |onRosterProfile| events are received when another user's
// profile is received or when a client changes status.
on(eventType: 'onUserProfile', f: (profile: UserProfile) => void): void;
// Message type |onMyStatus| is received when the user's client's status
// changes, e.g. when disconnected and online status becomes offline.
on(eventType: 'onClientState', f: (status: ClientState) => void): void;
// Do a singleton event binding: |f| will only be called once, on the next
// event of type |eventType|. Same events as above.
once(eventType: string, f: Function): void;
login(loginRequest: LoginRequest): Promise<ClientState>;
getUsers(): Promise<Users>;
getClients(): Promise<Clients>;
// Send a message to user on your network
// If the message is sent to a userId, it is sent to all clients
// If the message is sent to a clientId, it is sent to just that one client
// If the destination id is not specified or invalid, promise rejects.
sendMessage(destinationId: string, message: string): Promise<void>;
// Logs the user out of the social network. After the logout promise, the
// user status is OFFLINE.
logout(): Promise<void>;
// Forget any tokens/credentials used for logging in with the last used
// userId.
clearCachedCredentials(): Promise<void>;
}
} // declare module Social