Skip to content

Commit d7420d7

Browse files
committed
Fix graphql query file import fails Huachao#699
1 parent f377477 commit d7420d7

3 files changed

Lines changed: 47 additions & 29 deletions

File tree

‎src/utils/httpClient.ts‎

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as fs from 'fs-extra';
22
import * as iconv from 'iconv-lite';
33
import * as path from 'path';
4-
import { Readable, Stream } from 'stream';
54
import { Cookie, CookieJar, Store } from 'tough-cookie';
65
import * as url from 'url';
76
import { Uri, window } from 'vscode';
@@ -13,6 +12,7 @@ import { awsSignature } from './auth/awsSignature';
1312
import { digest } from './auth/digest';
1413
import { MimeUtility } from './mimeUtility';
1514
import { getHeader, hasHeader, removeHeader } from './misc';
15+
import { convertBufferToStream, convertStreamToBuffer } from './streamUtility';
1616
import { UserDataManager } from './userDataManager';
1717
import { getCurrentHttpFileName, getWorkspaceRootPath } from './workspaceUtility';
1818

@@ -98,7 +98,7 @@ export class HttpClient {
9898
HttpClient.normalizeHeaderNames(
9999
(response as any).request.gotOptions.headers as RequestHeaders,
100100
Object.keys(httpRequest.headers)),
101-
Buffer.isBuffer(requestBody) ? this.convertBufferToStream(requestBody) : requestBody,
101+
Buffer.isBuffer(requestBody) ? convertBufferToStream(requestBody) : requestBody,
102102
httpRequest.rawBody,
103103
httpRequest.name
104104
));
@@ -109,7 +109,7 @@ export class HttpClient {
109109
let requestBody: string | Buffer | undefined;
110110
if (originalRequestBody) {
111111
if (typeof originalRequestBody !== 'string') {
112-
requestBody = await this.convertStreamToBuffer(originalRequestBody);
112+
requestBody = await convertStreamToBuffer(originalRequestBody);
113113
} else {
114114
requestBody = originalRequestBody;
115115
}
@@ -237,25 +237,6 @@ export class HttpClient {
237237
return options;
238238
}
239239

240-
private async convertStreamToBuffer(stream: Stream): Promise<Buffer> {
241-
return new Promise<Buffer>((resolve, reject) => {
242-
const buffers: Buffer[] = [];
243-
stream.on('data', buffer => buffers.push(typeof buffer === 'string' ? Buffer.from(buffer) : buffer));
244-
stream.on('end', () => resolve(Buffer.concat(buffers)));
245-
stream.on('error', error => reject(error));
246-
(<any>stream).resume();
247-
});
248-
}
249-
250-
private convertBufferToStream(buffer: Buffer): Stream {
251-
return new Readable({
252-
read() {
253-
this.push(buffer);
254-
this.push(null);
255-
}
256-
});
257-
}
258-
259240
private decodeEscapedUnicodeCharacters(body: string): string {
260241
return body.replace(/\\u([0-9a-fA-F]{4})/gi, (_, g) => {
261242
const char = String.fromCharCode(parseInt(g, 16));

‎src/utils/httpRequestParser.ts‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { RequestParser } from '../models/requestParser';
88
import { MimeUtility } from './mimeUtility';
99
import { getContentType, getHeader, removeHeader } from './misc';
1010
import { parseRequestHeaders, resolveRequestBodyPath } from './requestParserUtil';
11+
import { convertStreamToString } from './streamUtility';
1112
import { VariableProcessor } from "./variableProcessor";
1213

1314
const CombinedStream = require('combined-stream');
@@ -97,13 +98,7 @@ export class HttpRequestParser implements RequestParser {
9798
const contentTypeHeader = getContentType(headers);
9899
let body = await this.parseBody(bodyLines, contentTypeHeader);
99100
if (isGraphQlRequest) {
100-
const variables = await this.parseBody(variableLines, contentTypeHeader);
101-
102-
const graphQlPayload = {
103-
query: body,
104-
variables: variables ? JSON.parse(variables.toString()) : {}
105-
};
106-
body = JSON.stringify(graphQlPayload);
101+
body = await this.createGraphQlBody(variableLines, contentTypeHeader, body);
107102
} else if (this.settings.formParamEncodingStrategy !== FormParamEncodingStrategy.Never && typeof body === 'string' && MimeUtility.isFormUrlEncoded(contentTypeHeader)) {
108103
if (this.settings.formParamEncodingStrategy === FormParamEncodingStrategy.Always) {
109104
const stringPairs = body.split('&');
@@ -130,6 +125,23 @@ export class HttpRequestParser implements RequestParser {
130125
return new HttpRequest(requestLine.method, requestLine.url, headers, body, bodyLines.join(EOL), name);
131126
}
132127

128+
private async createGraphQlBody(variableLines: string[], contentTypeHeader: string | undefined, body: string | Stream | undefined) {
129+
let variables = await this.parseBody(variableLines, contentTypeHeader);
130+
if (variables && typeof variables !== 'string') {
131+
variables = await convertStreamToString(variables);
132+
}
133+
134+
if (body && typeof body !== 'string') {
135+
body = await convertStreamToString(body);
136+
}
137+
138+
const graphQlPayload = {
139+
query: body,
140+
variables: variables ? JSON.parse(variables) : {}
141+
};
142+
return JSON.stringify(graphQlPayload);
143+
}
144+
133145
private parseRequestLine(line: string): { method: string, url: string } {
134146
// Request-Line = Method SP Request-URI SP HTTP-Version CRLF
135147
let method: string;

‎src/utils/streamUtility.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Readable, Stream } from 'stream';
2+
3+
export async function convertStreamToBuffer(stream: Stream): Promise<Buffer> {
4+
return new Promise<Buffer>((resolve, reject) => {
5+
const buffers: Buffer[] = [];
6+
stream.on('data', buffer => buffers.push(typeof buffer === 'string' ? Buffer.from(buffer) : buffer));
7+
stream.on('end', () => resolve(Buffer.concat(buffers)));
8+
stream.on('error', error => reject(error));
9+
(<any>stream).resume();
10+
});
11+
}
12+
13+
export async function convertStreamToString(stream: Stream, encoding: string = "UTF-8"): Promise<string> {
14+
const buffer = await convertStreamToBuffer(stream);
15+
return buffer.toString(encoding);
16+
}
17+
18+
export function convertBufferToStream(buffer: Buffer): Stream {
19+
return new Readable({
20+
read() {
21+
this.push(buffer);
22+
this.push(null);
23+
}
24+
});
25+
}

0 commit comments

Comments
 (0)