|
1 | | -import { spawn } from 'child_process' |
| 1 | +import { request, RequestOptions } from 'https' |
2 | 2 | import { CancellationToken } from 'vscode' |
3 | 3 | import { log } from '../log' |
4 | 4 | import { debugEnabledSetting } from '../settings/debugEnabledSetting' |
| 5 | +import { endpointHostnameSetting } from '../settings/endpointSetting' |
5 | 6 |
|
6 | 7 | export function graphqlQuery<A, B>(query: string, variables: A, token: CancellationToken): Promise<B | undefined> { |
7 | 8 | return new Promise<B | undefined>((resolve, reject) => { |
8 | | - const stdoutBuffer: string[] = [] |
9 | | - const onExit = (exit: number) => { |
10 | | - if (exit === 0) { |
11 | | - const json = stdoutBuffer.join('') |
12 | | - try { |
13 | | - const parsed: B = JSON.parse(json) |
14 | | - resolve(parsed) // wrap in promise because this method will be async in the future |
15 | | - } catch (error) { |
16 | | - reject(error) |
| 9 | + const data = JSON.stringify({ |
| 10 | + query, |
| 11 | + variables, |
| 12 | + }) |
| 13 | + const accessToken = process.env.SRC_ACCESS_TOKEN || '' |
| 14 | + const options: RequestOptions = { |
| 15 | + hostname: endpointHostnameSetting(), |
| 16 | + port: 443, |
| 17 | + path: '/.api/graphql', |
| 18 | + method: 'POST', |
| 19 | + headers: { |
| 20 | + Authorization: `token ${accessToken}`, |
| 21 | + 'Content-Length': data.length, |
| 22 | + }, |
| 23 | + } |
| 24 | + const req = request(options, res => { |
| 25 | + const body: Uint8Array[] = [] |
| 26 | + res.on('data', json => { |
| 27 | + body.push(json) |
| 28 | + }) |
| 29 | + res.on('error', reject) |
| 30 | + const onClose = () => { |
| 31 | + if (res.statusCode === 200) { |
| 32 | + try { |
| 33 | + const json = Buffer.concat(body).toString() |
| 34 | + const parsed: B = JSON.parse(json) |
| 35 | + resolve(parsed) |
| 36 | + } catch (error) { |
| 37 | + log.error(`graphql(${data})`, error) |
| 38 | + reject(error) |
| 39 | + } |
| 40 | + } else { |
| 41 | + log.error(`graphql(${data}), statusCode=${res.statusCode}`, body) |
| 42 | + reject(body.join('')) |
17 | 43 | } |
18 | | - } else { |
19 | | - reject({ exit }) |
20 | 44 | } |
21 | | - } |
22 | | - const onData = (chunk: string) => { |
23 | | - stdoutBuffer.push(chunk) |
24 | | - } |
25 | | - const command: string[] = [ |
26 | | - 'api', |
27 | | - '-query', |
28 | | - query.trim().replace(/\n/g, ' ').replace(/ +/g, ' '), |
29 | | - '-vars', |
30 | | - JSON.stringify(variables), |
31 | | - ] |
| 45 | + res.on('close', onClose) |
| 46 | + res.on('end', onClose) |
| 47 | + }) |
| 48 | + req.on('error', reject) |
| 49 | + req.write(data) |
| 50 | + req.end() |
32 | 51 | if (debugEnabledSetting()) { |
| 52 | + const command: string[] = [ |
| 53 | + 'api', |
| 54 | + '-query', |
| 55 | + query.trim().replace(/\n/g, ' ').replace(/ +/g, ' '), |
| 56 | + '-vars', |
| 57 | + JSON.stringify(variables), |
| 58 | + ] |
33 | 59 | log.appendLine('src ' + command.map(part => `'${part}'`).join(' ')) |
34 | 60 | } |
35 | | - const proc = spawn('src', command) |
36 | | - proc.stdout.on('data', onData) |
37 | | - proc.on('close', onExit) |
38 | | - proc.on('disconnect', onExit) |
39 | | - proc.on('error', onExit) |
40 | | - proc.on('exit', onExit) |
41 | 61 | token.onCancellationRequested(() => { |
42 | | - if (!proc.killed) { |
43 | | - proc.kill() |
44 | | - reject() |
45 | | - } |
| 62 | + req.destroy() |
46 | 63 | }) |
47 | 64 | }) |
48 | 65 | } |
0 commit comments