-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
203 lines (182 loc) · 6.17 KB
/
index.js
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
#!/usr/bin/env node
const { Command } = require("commander");
const got = require("got");
const { buildClientSchema, validateSchema, getIntrospectionQuery } = require('graphql');
const WebSocketClient = require('websocket').client;
const program = new Command();
program
.requiredOption("--endpoint <endpoint>", "endpoint to diagnose")
.option(
"--origin <origin>",
"origin (for testing CORS headers)",
"https://studio.apollographql.com"
);
program.parse(process.argv);
const options = program.opts();
const diagnoseWebSocket = (endpoint, origin) => {
return new Promise((resolve, reject) => {
const client = new WebSocketClient();
client.on('connectFailed', (error) => {
if (!error.code) {
error.code = "ENOTFOUND";
}
reject(error);
});
client.on('connect', (connection) => {
connection.on('error', (error) => {
reject(error);
});
connection.on('close', () => {
resolve();
});
});
client.connect(endpoint, undefined, origin);
})};
(async () => {
let hasIdentifiedProblem = false;
let hasIdentifiedCorsProblem = false;
const identifyProblem = (...problemDescription) => {
hasIdentifiedProblem = true;
console.log("⚠️ ", ...problemDescription);
};
const identifyCorsProblem = (...problemDescription) => {
identifyProblem(...problemDescription);
hasIdentifiedCorsProblem = true;
}
const isWebSocket = !!options.endpoint.match(/^wss?:/i);
try {
console.log(`Diagnosing ${options.endpoint}`);
if (isWebSocket) {
await diagnoseWebSocket(options.endpoint, options.origin);
} else {
const optionsResponse = await got(options.endpoint, {
method: "OPTIONS",
headers: {
"access-control-request-method": "POST",
origin: options.origin,
},
throwHttpErrors: false,
});
if (optionsResponse.statusCode === 401) {
identifyProblem(
`OPTIONS response returned 401. Are authorization headers or cookies required?`
);
} else if (optionsResponse.statusCode === 404) {
identifyProblem(
`OPTIONS response returned 404. Is the url correct? Are authorization headers or cookies required?`
);
}
if (
!(
optionsResponse.headers["access-control-allow-methods"] &&
optionsResponse.headers["access-control-allow-methods"].includes("POST")
)
) {
identifyCorsProblem(
`OPTIONS response is missing header 'access-control-allow-methods: POST'`
);
}
const pingResponse = await got.post(options.endpoint, {
json: {
query: `query Ping { __typename }`,
},
headers: {
origin: options.origin,
},
throwHttpErrors: false,
});
if (pingResponse.statusCode === 401) {
identifyProblem(
`POST response returned 401. Are authorization headers or cookies required?`
);
} else if (pingResponse.statusCode === 404) {
identifyProblem(
`POST response returned 404. Is the url correct? Are authorization headers or cookies required?`
);
}
if (
!pingResponse.headers["access-control-allow-origin"] ||
(pingResponse.headers["access-control-allow-origin"] !== "*" &&
pingResponse.headers["access-control-allow-origin"] !== options.origin)
) {
identifyCorsProblem(
[
`POST response missing 'access-control-allow-origin' header.`,
`If using cookie-based authentication, the following headers are required from your endpoint: `,
` access-control-allow-origin: ${options.origin}`,
` access-control-allow-credentials: true`,
`Otherwise, a wildcard value would work:`,
` access-control-allow-origin: *`,
].join("\n")
);
}
}
} catch (e) {
switch (e.code) {
case "ENOTFOUND":
identifyProblem(
`Could not resolve (ENOTFOUND)\nIs the address correct?\nIs the server running?`
);
break;
case "ECONNREFUSED":
identifyProblem(
`Connection refused (ECONNREFUSED)\nIs the address correct?\nIs the server running?`
);
break;
case "EPROTO":
identifyProblem(
`Protocol wrong type for socket (EPROTO)\nIs the address correct?\nIs the server running?`
);
break;
default:
// unexpected error
identifyProblem(
`Failed to diagnose what went wrong. Here's the error: `,
e,
e.message
);
}
}
if (hasIdentifiedCorsProblem) {
console.log(
`(📫 Interested in previewing a local tunnel to bypass CORS requirements? Please let us know at https://docs.google.com/forms/d/e/1FAIpQLScUCi3PdMerraiy6GpD-QiC_9KEKVHr4oDL5Vef5fIvzqqQWg/viewform )`
);
}
if (!hasIdentifiedProblem && !isWebSocket) {
// Only check for introspection problems if there are no other problems found
const introspectionQueryResponse = await got.post(options.endpoint, {
json: {
query: getIntrospectionQuery(),
},
headers: {
origin: options.origin,
},
throwHttpErrors: false,
});
try {
const responseData = JSON.parse(introspectionQueryResponse.body)
if (!('data' in responseData) || !('__schema' in responseData.data)) {
identifyProblem(`Introspection query received a response of ${introspectionQueryResponse.body}. Does introspection need to be turned on?`)
} else {
const schemaFromIntrospection = buildClientSchema(responseData.data);
const validationErrors = validateSchema(schemaFromIntrospection);
if (validationErrors.length) {
identifyProblem(
`Invalid schema from introspection: ${validationErrors}`
);
}
}
} catch (e) {
identifyProblem(
`Introspection query could not parse "${introspectionQueryResponse.body}" As valid json. Here is the error: `,
e,
e.message
);
}
}
if (!hasIdentifiedProblem) {
console.log(
`Failed to diagnose the problems with the endpoint.`
);
}
})();