Skip to content

Commit b201f68

Browse files
author
phillipperalez
authored
Cork streams before getting a response (#670)
This will prevent sending data through the streams before knowing if the response was actually going to give data:wq
1 parent 82c7fd1 commit b201f68

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/hub/action_request.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ class ActionRequest {
121121
if (url) {
122122
winston.info(`[stream] beginning stream via download url`, this.logInfo);
123123
let hasResolved = false;
124+
// Here we can cork the stream and uncork if we receive a 200 OK response
125+
// If we do not receive a 200 we do not want to allow data to pipe and ignore a non-200 response
126+
stream.cork();
124127
httpRequest
125128
.get(url, { timeout })
126129
.on("error", (err) => {
@@ -137,7 +140,11 @@ class ActionRequest {
137140
}
138141
})
139142
.on("response", (response) => {
140-
if (response.statusCode !== 200) {
143+
if (response.statusCode === 200) {
144+
// Stop buffering in memory and allow action to send data
145+
stream.uncork();
146+
}
147+
else {
141148
winston.warn(`[stream] There was an error received from Looker.` +
142149
`ErrorCode: ${response.statusCode} ErrorMessage: ${response.statusMessage}`, this.logInfo);
143150
if (!hasResolved) {

src/hub/action_request.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ export class ActionRequest {
191191
if (url) {
192192
winston.info(`[stream] beginning stream via download url`, this.logInfo)
193193
let hasResolved = false
194+
// Here we can cork the stream and uncork if we receive a 200 OK response
195+
// If we do not receive a 200 we do not want to allow data to pipe and ignore a non-200 response
196+
stream.cork()
194197
httpRequest
195198
.get(url, {timeout})
196199
.on("error", (err) => {
@@ -206,7 +209,10 @@ export class ActionRequest {
206209
}
207210
})
208211
.on("response", (response) => {
209-
if (response.statusCode !== 200) {
212+
if (response.statusCode === 200) {
213+
// Stop buffering in memory and allow action to send data
214+
stream.uncork()
215+
} else {
210216
winston.warn(`[stream] There was an error received from Looker.` +
211217
`ErrorCode: ${response.statusCode} ErrorMessage: ${response.statusMessage}`, this.logInfo)
212218
if (!hasResolved) {

0 commit comments

Comments
 (0)