Skip to content

Commit

Permalink
Type RequestContext.buffer correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew authored and paul-marechal committed May 25, 2022
1 parent 0cdc910 commit b8076ef
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
16 changes: 10 additions & 6 deletions dev-packages/request-service/src/common-request-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ export interface RequestContext {
headers: Headers;
statusCode?: number;
};
buffer: Uint8Array;
/**
* Contains the data returned by the request.
*
* If the request was transferred from the backend to the frontend, the buffer has been compressed into a string. In every case the buffer is an {@link Uint8Array}.
*/
buffer: Uint8Array | string;
}

export namespace RequestContext {
Expand All @@ -57,8 +62,10 @@ export namespace RequestContext {
if (hasNoContent(context)) {
return '';
}
// Ensures that the buffer is an Uint8Array
context = decompress(context);
if (textDecoder) {
return textDecoder.decode(context.buffer);
return textDecoder.decode(context.buffer as Uint8Array);
} else {
return context.buffer.toString();
}
Expand All @@ -81,10 +88,7 @@ export namespace RequestContext {
*/
export function compress(context: RequestContext): RequestContext {
if (context.buffer instanceof Uint8Array && Buffer !== undefined) {
const base64Data = Buffer.from(context.buffer).toString('base64');
Object.assign(context, {
buffer: base64Data
});
context.buffer = Buffer.from(context.buffer).toString('base64');
}
return context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import 'reflect-metadata';
import { decorate, injectable } from 'inversify';
import { NodeRequestService } from '@theia/request-service/lib/node-request-service';

Expand Down

0 comments on commit b8076ef

Please sign in to comment.