Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ describe('given a default instance of NodeRequests', () => {
let resolve: (value: TestRequestData | PromiseLike<TestRequestData>) => void;
let promise: Promise<TestRequestData>;
let server: http.Server;
let resetResolve: () => void;
let resetPromise: Promise<void>;

beforeEach(() => {
resetPromise = new Promise((res) => {
resetResolve = res;
});

promise = new Promise<TestRequestData>((res) => {
resolve = res;
});
Expand All @@ -43,6 +49,14 @@ describe('given a default instance of NodeRequests', () => {
} else if ((req.url?.indexOf('404') || -1) >= 0) {
res.statusCode = 404;
res.end();
} else if ((req.url?.indexOf('reset') || -1) >= 0) {
res.statusCode = 200;
res.flushHeaders();
res.write('potato');
setTimeout(() => {
res.destroy();
resetResolve();
}, 0);
} else {
res.end(TEXT_RESPONSE);
}
Expand Down Expand Up @@ -115,4 +129,19 @@ describe('given a default instance of NodeRequests', () => {
expect(serverResult.body).toEqual('BODY TEXT');
expect(serverResult.headers['sample-header']).toEqual('Some header value');
});

it('rejection is handled for response even if not awaited', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running this test against the original code will fail, against this code it works.

const res = await requests.fetch(`http://localhost:${PORT}/reset`);
expect(res.status).toEqual(200);
await resetPromise;
});

it('rejection is propagated with json promise', async () => {
const res = await requests.fetch(`http://localhost:${PORT}/reset`);
expect(res.status).toEqual(200);

await expect(async () => {
await res.json();
}).rejects.toThrow();
});
});
20 changes: 17 additions & 3 deletions packages/sdk/server-node/src/platform/NodeResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export default class NodeResponse implements platform.Response {

status: number;

listened: boolean = false;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically we don't attempt to read the body from all HTTP requests we make. Event posting doesn't return a body, so we discard the inner response.

Unfortunately that inner response may still be rejected.

So we track if the promise has been listened to. If it has been, then we reject like normal. If not we cache the rejection error and throw it when someone listens to the promise.

rejection?: Error;

constructor(res: http.IncomingMessage) {
this.headers = new HeaderWrapper(res.headers);
// Status code is optionally typed, but will always be present for this
Expand All @@ -28,7 +31,10 @@ export default class NodeResponse implements platform.Response {
});

res.on('error', (err) => {
reject(err);
this.rejection = err;
if (this.listened) {
reject(err);
}
});

res.on('end', () => {
Expand All @@ -37,12 +43,20 @@ export default class NodeResponse implements platform.Response {
});
}

text(): Promise<string> {
private async wrappedWait(): Promise<string> {
this.listened = true;
if (this.rejection) {
throw this.rejection;
}
return this.promise;
}

text(): Promise<string> {
return this.wrappedWait();
}

async json(): Promise<any> {
const stringValue = await this.promise;
const stringValue = await this.wrappedWait();
return JSON.parse(stringValue);
}
}