Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/socket/socket.js
Show All 31 Lines | import { | ||||
type ErrorServerSocketMessage, | type ErrorServerSocketMessage, | ||||
type AuthErrorServerSocketMessage, | type AuthErrorServerSocketMessage, | ||||
type PingClientSocketMessage, | type PingClientSocketMessage, | ||||
type AckUpdatesClientSocketMessage, | type AckUpdatesClientSocketMessage, | ||||
type APIRequestClientSocketMessage, | type APIRequestClientSocketMessage, | ||||
clientSocketMessageTypes, | clientSocketMessageTypes, | ||||
stateSyncPayloadTypes, | stateSyncPayloadTypes, | ||||
serverSocketMessageTypes, | serverSocketMessageTypes, | ||||
serverServerSocketMessageValidator, | |||||
} from 'lib/types/socket-types.js'; | } from 'lib/types/socket-types.js'; | ||||
import { ServerError } from 'lib/utils/errors.js'; | import { ServerError } from 'lib/utils/errors.js'; | ||||
import { values } from 'lib/utils/objects.js'; | import { values } from 'lib/utils/objects.js'; | ||||
import { promiseAll } from 'lib/utils/promises.js'; | import { promiseAll } from 'lib/utils/promises.js'; | ||||
import SequentialPromiseResolver from 'lib/utils/sequential-promise-resolver.js'; | import SequentialPromiseResolver from 'lib/utils/sequential-promise-resolver.js'; | ||||
import sleep from 'lib/utils/sleep.js'; | import sleep from 'lib/utils/sleep.js'; | ||||
import { tShape, tCookie } from 'lib/utils/validation-utils.js'; | import { tShape, tCookie } from 'lib/utils/validation-utils.js'; | ||||
Show All 33 Lines | |||||
} from '../session/cookies.js'; | } from '../session/cookies.js'; | ||||
import { Viewer } from '../session/viewer.js'; | import { Viewer } from '../session/viewer.js'; | ||||
import { commitSessionUpdate } from '../updaters/session-updaters.js'; | import { commitSessionUpdate } from '../updaters/session-updaters.js'; | ||||
import { assertSecureRequest } from '../utils/security-utils.js'; | import { assertSecureRequest } from '../utils/security-utils.js'; | ||||
import { | import { | ||||
checkInputValidator, | checkInputValidator, | ||||
checkClientSupported, | checkClientSupported, | ||||
policiesValidator, | policiesValidator, | ||||
validateOutput, | |||||
} from '../utils/validation-utils.js'; | } from '../utils/validation-utils.js'; | ||||
const clientSocketMessageInputValidator = t.union([ | const clientSocketMessageInputValidator = t.union([ | ||||
tShape({ | tShape({ | ||||
type: t.irreducible( | type: t.irreducible( | ||||
'clientSocketMessageTypes.INITIAL', | 'clientSocketMessageTypes.INITIAL', | ||||
x => x === clientSocketMessageTypes.INITIAL, | x => x === clientSocketMessageTypes.INITIAL, | ||||
), | ), | ||||
▲ Show 20 Lines • Show All 273 Lines • ▼ Show 20 Lines | class Socket { | ||||
}; | }; | ||||
sendMessage = (message: ServerServerSocketMessage) => { | sendMessage = (message: ServerServerSocketMessage) => { | ||||
invariant( | invariant( | ||||
this.ws.readyState > 0, | this.ws.readyState > 0, | ||||
"shouldn't send message until connection established", | "shouldn't send message until connection established", | ||||
); | ); | ||||
if (this.ws.readyState === 1) { | if (this.ws.readyState === 1) { | ||||
this.ws.send(JSON.stringify(message)); | const { viewer } = this; | ||||
const validatedMessage = validateOutput( | |||||
viewer, | |||||
serverServerSocketMessageValidator, | |||||
message, | |||||
); | |||||
this.ws.send(JSON.stringify(validatedMessage)); | |||||
} | } | ||||
}; | }; | ||||
async handleClientSocketMessage( | async handleClientSocketMessage( | ||||
message: ClientSocketMessage, | message: ClientSocketMessage, | ||||
): Promise<ServerServerSocketMessage[]> { | ): Promise<ServerServerSocketMessage[]> { | ||||
const resultPromise = (async () => { | const resultPromise = (async () => { | ||||
if (message.type === clientSocketMessageTypes.INITIAL) { | if (message.type === clientSocketMessageTypes.INITIAL) { | ||||
▲ Show 20 Lines • Show All 449 Lines • Show Last 20 Lines |