|
1 |
| -import { createHttpError, HttpError, isNull, Status } from "./deps.ts"; |
| 1 | +import { |
| 2 | + createHttpError, |
| 3 | + has, |
| 4 | + HttpError, |
| 5 | + isNull, |
| 6 | + isPlainObject, |
| 7 | + isString, |
| 8 | + Status, |
| 9 | +} from "./deps.ts"; |
| 10 | +import { MessageType } from "./constants.ts"; |
| 11 | +import { |
| 12 | + CompleteMessage, |
| 13 | + GraphQLParameters, |
| 14 | + Message, |
| 15 | + NextMessage, |
| 16 | + SubscribeMessage, |
| 17 | +} from "./types.ts"; |
2 | 18 |
|
3 | 19 | export function validateWebSocketRequest(req: Request): [result: true] | [
|
4 | 20 | result: false,
|
@@ -79,3 +95,165 @@ export function validateWebSocketRequest(req: Request): [result: true] | [
|
79 | 95 |
|
80 | 96 | return [true];
|
81 | 97 | }
|
| 98 | + |
| 99 | +export function validateMessage( |
| 100 | + value: unknown, |
| 101 | +): [data: Message] | [data: undefined, error: Error] { |
| 102 | + if (!isPlainObject(value)) { |
| 103 | + return [ |
| 104 | + , |
| 105 | + Error( |
| 106 | + `Invalid data type. Must be plain object.`, |
| 107 | + ), |
| 108 | + ]; |
| 109 | + } |
| 110 | + |
| 111 | + if (!has(value, "type")) { |
| 112 | + return [, Error(`Missing field. Must include "type" field.`)]; |
| 113 | + } |
| 114 | + if (!isString(value.type)) { |
| 115 | + return [, Error(`Invalid field. "type" field of value must be string.`)]; |
| 116 | + } |
| 117 | + |
| 118 | + switch (value.type) { |
| 119 | + case MessageType.ConnectionInit: |
| 120 | + case MessageType.ConnectionAck: |
| 121 | + case MessageType.Ping: |
| 122 | + case MessageType.Pong: { |
| 123 | + if (has(value, "payload") && !isPlainObject(value.payload)) { |
| 124 | + return [, Error(`Invalid field. "payload" must be plain object.`)]; |
| 125 | + } |
| 126 | + |
| 127 | + return [value as Message]; |
| 128 | + } |
| 129 | + |
| 130 | + case MessageType.Subscribe: { |
| 131 | + if (!has(value, "id")) { |
| 132 | + return [, Error(`Missing field. "id"`)]; |
| 133 | + } |
| 134 | + if (!isString(value.id)) { |
| 135 | + return [ |
| 136 | + , |
| 137 | + Error( |
| 138 | + `Invalid field. "id" must be string.`, |
| 139 | + ), |
| 140 | + ]; |
| 141 | + } |
| 142 | + if (!has(value, "payload")) { |
| 143 | + return [, Error(`Missing field. "payload"`)]; |
| 144 | + } |
| 145 | + |
| 146 | + const graphqlParametersResult = validateGraphQLParameters(value.payload); |
| 147 | + |
| 148 | + if (!graphqlParametersResult[0]) { |
| 149 | + return graphqlParametersResult; |
| 150 | + } |
| 151 | + |
| 152 | + return [ |
| 153 | + { ...value, payload: graphqlParametersResult[0] } as SubscribeMessage, |
| 154 | + ]; |
| 155 | + } |
| 156 | + |
| 157 | + case MessageType.Next: { |
| 158 | + if (!has(value, "id")) { |
| 159 | + return [, Error(`Missing property. "id"`)]; |
| 160 | + } |
| 161 | + if (!has(value, "payload")) { |
| 162 | + return [, Error(`Missing property. "payload"`)]; |
| 163 | + } |
| 164 | + return [value as NextMessage]; |
| 165 | + } |
| 166 | + |
| 167 | + case MessageType.Complete: { |
| 168 | + if (!has(value, "id")) { |
| 169 | + return [, Error(`Missing property. "id"`)]; |
| 170 | + } |
| 171 | + |
| 172 | + return [value as CompleteMessage]; |
| 173 | + } |
| 174 | + |
| 175 | + default: { |
| 176 | + return [ |
| 177 | + , |
| 178 | + Error( |
| 179 | + `Invalid field. "type" field of "${value.type}" is not supported.`, |
| 180 | + ), |
| 181 | + ]; |
| 182 | + } |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +export function validateGraphQLParameters( |
| 187 | + value: unknown, |
| 188 | +): [data: GraphQLParameters] | [data: undefined, error: Error] { |
| 189 | + if (!isPlainObject(value)) { |
| 190 | + return [ |
| 191 | + , |
| 192 | + Error( |
| 193 | + `Invalid field. "payload" must be plain object.`, |
| 194 | + ), |
| 195 | + ]; |
| 196 | + } |
| 197 | + |
| 198 | + if (!has(value, "query")) { |
| 199 | + return [ |
| 200 | + , |
| 201 | + Error( |
| 202 | + `Missing field. "query"`, |
| 203 | + ), |
| 204 | + ]; |
| 205 | + } |
| 206 | + |
| 207 | + if (!isString(value.query)) { |
| 208 | + return [ |
| 209 | + , |
| 210 | + Error( |
| 211 | + `Invalid field. "query" must be string.`, |
| 212 | + ), |
| 213 | + ]; |
| 214 | + } |
| 215 | + |
| 216 | + if ( |
| 217 | + has(value, "variables") && |
| 218 | + (!isNull(value.variables) && !isPlainObject(value.variables)) |
| 219 | + ) { |
| 220 | + return [ |
| 221 | + , |
| 222 | + Error( |
| 223 | + `Invalid field. "variables" must be plain object or null`, |
| 224 | + ), |
| 225 | + ]; |
| 226 | + } |
| 227 | + if ( |
| 228 | + has(value, "operationName") && |
| 229 | + (!isNull(value.operationName) && !isString(value.operationName)) |
| 230 | + ) { |
| 231 | + return [ |
| 232 | + , |
| 233 | + Error( |
| 234 | + `Invalid field. "operationName" must be string or null.`, |
| 235 | + ), |
| 236 | + ]; |
| 237 | + } |
| 238 | + if ( |
| 239 | + has(value, "extensions") && |
| 240 | + (!isNull(value.extensions) && !isPlainObject(value.extensions)) |
| 241 | + ) { |
| 242 | + return [ |
| 243 | + , |
| 244 | + Error( |
| 245 | + `Invalid field. "extensions" must be plain object or null`, |
| 246 | + ), |
| 247 | + ]; |
| 248 | + } |
| 249 | + |
| 250 | + const { query, ...rest } = value; |
| 251 | + |
| 252 | + return [{ |
| 253 | + operationName: null, |
| 254 | + variableValues: null, |
| 255 | + extensions: null, |
| 256 | + query, |
| 257 | + ...rest, |
| 258 | + }]; |
| 259 | +} |
0 commit comments