-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathServer.ts
More file actions
94 lines (91 loc) · 3.17 KB
/
Server.ts
File metadata and controls
94 lines (91 loc) · 3.17 KB
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
import { Socket } from 'net';
import { _assignPackageExport } from '../globals.js';
import type { Value } from '../resources/analytics/write.ts';
import type { Resources } from '../resources/Resources.ts';
import { OperationDefinition } from './serverHelpers/serverUtilities.ts';
import { Duplex } from 'stream';
import { Request } from './serverHelpers/Request.ts';
export type HttpListener = (request: Request, nextLayer: (request: Request) => Response) => void;
// based on `upgrade` event in Node.js http server: https://nodejs.org/docs/latest/api/http.html#event-upgrade-1
export type UpgradeListener = (
request: Request,
socket: Duplex,
head: Buffer,
nextLayer: (request: Request, socket: Duplex, head: Buffer) => Promise<void>
) => Promise<void>;
/**
* This is the central interface by which we define entry points for different server protocol plugins to listen for
* incoming connections and requests.
*/
export interface Server {
socket?(listener: (socket: Socket) => void, options: ServerOptions): void;
http?(listener: HttpListener, options?: HttpOptions): void;
request?(listener: HttpListener, options?: HttpOptions): void;
ws?(
listener: (ws: WebSocket, request: Request, requestCompletion: Promise<any>) => any,
options?: WebSocketOptions
): void;
upgrade?(listener: UpgradeListener, options?: UpgradeOptions): void;
contentTypes: Map<string, ContentTypeHandler>;
getUser(username: string, password: string | null, request: Request): any;
authenticateUser(username: string, password: string, request: Request): any;
operation(operation: any, context: any, authorize?: boolean): Promise<any>;
registerOperation(operationDefinition: OperationDefinition): void;
recordAnalytics(value: Value, metric: string, path?: string, method?: string, type?: string): void;
nodes: Node[];
shards: Map<number, string[]>;
hostname: string;
resources: Resources;
replication: {
replicateOperation(operation: {
replicated: boolean;
[key: string]: any;
}): Promise<{ message: string; replicated?: unknown[] }>;
monitorNodeCAs(listener: () => void): void;
sendOperationToNode(node: string, operation: any, options: any): Promise<any>;
};
}
interface Node {
name: string;
shard: number;
url: string;
}
export interface ServerOptions {
port?: number;
securePort?: number;
mtls?: boolean;
usageType?: string;
}
interface WebSocketOptions extends ServerOptions {
subProtocol: string;
}
export interface UpgradeOptions {
port?: number;
securePort?: number;
runFirst?: boolean;
}
export interface HttpOptions extends ServerOptions {
runFirst?: boolean;
}
export interface ContentTypeHandler {
serialize(data: any): Buffer | string;
serializeStream(data: any): Buffer | string;
deserialize(data: any): Buffer | string;
q: number;
}
export const server: Server = {
replication: {
replicateOperation(operation) {
return operation.replicated
? Promise.reject(new Error('Replication not implemented.'))
: Promise.resolve({ message: '' });
},
monitorNodeCAs(_listener: () => void) {
throw new Error('Replication not implemented.');
},
sendOperationToNode() {
return Promise.reject(new Error('Replication not implemented.'));
},
},
};
_assignPackageExport('server', server);