diff --git a/lib/hub/action_request.js b/lib/hub/action_request.js index 7cdc015a..1a1df78e 100644 --- a/lib/hub/action_request.js +++ b/lib/hub/action_request.js @@ -10,8 +10,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ActionRequest = exports.ActionDownloadSettings = exports.ActionVisualizationFormatting = exports.ActionFormatting = exports.ActionFormat = exports.ActionType = void 0; +const node_fetch_1 = require("node-fetch"); const oboe = require("oboe"); -const httpRequest = require("request"); const semver = require("semver"); const stream_1 = require("stream"); const winston = require("winston"); @@ -122,55 +122,41 @@ class ActionRequest { return __awaiter(this, void 0, void 0, function* () { const stream = new stream_1.PassThrough(); const returnPromise = callback(stream); - const timeout = process.env.ACTION_HUB_STREAM_REQUEST_TIMEOUT ? - parseInt(process.env.ACTION_HUB_STREAM_REQUEST_TIMEOUT, 10) - : - 13 * 60 * 1000; const url = this.scheduledPlan && this.scheduledPlan.downloadUrl; const streamPromise = new Promise((resolve, reject) => { if (url) { winston.info(`[stream] beginning stream via download url`, this.logInfo); - let hasResolved = false; - httpRequest - .get(url, { timeout }) - .on("error", (err) => { - if (hasResolved && err.code === "ECONNRESET") { - winston.info(`[stream] ignoring ECONNRESET that occured after streaming finished`, this.logInfo); - } - else { - winston.error(`[stream] request stream error`, Object.assign(Object.assign({}, this.logInfo), { error: err.message, stack: err.stack })); - reject(err); - } - }) - .on("finish", () => { - winston.info(`[stream] streaming via download url finished`, this.logInfo); - }) - .on("socket", (socket) => { - winston.info(`[stream] setting keepalive on socket`, this.logInfo); - socket.setKeepAlive(true); - }) - .on("abort", () => { - winston.info(`[stream] streaming via download url aborted`, this.logInfo); - }) - .on("response", () => { - winston.info(`[stream] got response from download url`, this.logInfo); - }) - .on("close", () => { - winston.info(`[stream] request stream closed`, this.logInfo); - }) - .pipe(stream) - .on("error", (err) => { - winston.error(`[stream] PassThrough stream error`, Object.assign({}, this.logInfo)); - reject(err); - }) - .on("finish", () => { - winston.info(`[stream] PassThrough stream finished`, this.logInfo); - resolve(); - hasResolved = true; - }) - .on("close", () => { - winston.info(`[stream] PassThrough stream closed`, this.logInfo); - }); + try { + (0, node_fetch_1.default)(url).then((response) => { + if (response.ok) { + response.body.pipe(stream) + .on("error", (err) => { + winston.error(`[stream] PassThrough stream error`, this.logInfo); + reject(err); + }) + .on("finish", () => { + winston.info(`[stream] PassThrough stream finished`, this.logInfo); + }) + .on("close", () => { + winston.info(`[stream] PassThrough stream closed`, this.logInfo); + resolve(); + }); + } + else { + const responseText = `[stream] There was a problem in the callback HTTPS Status Code: ${response.status}`; + winston.warn(responseText, this.logInfo); + reject(responseText); + } + }).catch((err) => { + const responseText = `[stream] There was a problem in the fetch request: ${err.toString()}`; + winston.warn(responseText, this.logInfo); + reject(responseText); + }); + } + catch (e) { + winston.warn(`Error connecting to callback url`, this.logInfo); + reject(e); + } } else { if (this.attachment && this.attachment.dataBuffer) { diff --git a/package.json b/package.json index fddb10f5..5f439f67 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "@types/express": "^4.17.17", "@types/jsforce": "^1.9.33", "@types/node": "^18.11.11", + "@types/node-fetch": "^2.6.11", "@types/nodemailer": "^4.6.8", "@types/normalize-url": "3.3.0", "@types/oboe": "^2.0.29", diff --git a/src/hub/action_request.ts b/src/hub/action_request.ts index faba2b06..8f9fb450 100644 --- a/src/hub/action_request.ts +++ b/src/hub/action_request.ts @@ -1,6 +1,6 @@ import * as express from "express" +import fetch from "node-fetch" import * as oboe from "oboe" -import * as httpRequest from "request" import * as semver from "semver" import { PassThrough, Readable } from "stream" import * as winston from "winston" @@ -180,62 +180,41 @@ export class ActionRequest { const stream = new PassThrough() const returnPromise = callback(stream) - const timeout = process.env.ACTION_HUB_STREAM_REQUEST_TIMEOUT ? - parseInt(process.env.ACTION_HUB_STREAM_REQUEST_TIMEOUT, 10) - : - 13 * 60 * 1000 const url = this.scheduledPlan && this.scheduledPlan.downloadUrl const streamPromise = new Promise((resolve, reject) => { if (url) { winston.info(`[stream] beginning stream via download url`, this.logInfo) - let hasResolved = false - httpRequest - .get(url, {timeout}) - .on("error", (err) => { - if (hasResolved && (err as any).code === "ECONNRESET") { - winston.info(`[stream] ignoring ECONNRESET that occured after streaming finished`, this.logInfo) + try { + fetch(url).then((response: any) => { + if (response.ok) { + response.body.pipe(stream) + .on("error", (err: any) => { + winston.error(`[stream] PassThrough stream error`, this.logInfo) + reject(err) + }) + .on("finish", () => { + winston.info(`[stream] PassThrough stream finished`, this.logInfo) + }) + .on("close", () => { + winston.info(`[stream] PassThrough stream closed`, this.logInfo) + resolve() + }) } else { - winston.error(`[stream] request stream error`, { - ...this.logInfo, - error: err.message, - stack: err.stack, - }) - reject(err) + const responseText = `[stream] There was a problem in the callback HTTPS Status Code: ${response.status}` + winston.warn(responseText, this.logInfo) + reject(responseText) } + }).catch((err: any) => { + const responseText = `[stream] There was a problem in the fetch request: ${err.toString()}` + winston.warn(responseText, this.logInfo) + reject(responseText) }) - .on("finish", () => { - winston.info(`[stream] streaming via download url finished`, this.logInfo) - }) - .on("socket", (socket) => { - winston.info(`[stream] setting keepalive on socket`, this.logInfo) - socket.setKeepAlive(true) - }) - .on("abort", () => { - winston.info(`[stream] streaming via download url aborted`, this.logInfo) - }) - .on("response", () => { - winston.info(`[stream] got response from download url`, this.logInfo) - }) - .on("close", () => { - winston.info(`[stream] request stream closed`, this.logInfo) - }) - .pipe(stream) - .on("error", (err) => { - winston.error(`[stream] PassThrough stream error`, { - ...this.logInfo, - }) - reject(err) - }) - .on("finish", () => { - winston.info(`[stream] PassThrough stream finished`, this.logInfo) - resolve() - hasResolved = true - }) - .on("close", () => { - winston.info(`[stream] PassThrough stream closed`, this.logInfo) - }) + } catch (e) { + winston.warn(`Error connecting to callback url`, this.logInfo) + reject(e) + } } else { if (this.attachment && this.attachment.dataBuffer) { winston.info(`Using "fake" streaming because request contained attachment data.`, this.logInfo) diff --git a/yarn.lock b/yarn.lock index ac1325bb..6933761e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -627,6 +627,14 @@ resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-8.2.3.tgz#bbeb55fbc73f28ea6de601fbfa4613f58d785323" integrity sha512-ekGvFhFgrc2zYQoX4JeZPmVzZxw6Dtllga7iGHzfbYIYkAMUx/sAFP2GdFpLff+vdHXu5fl7WX9AT+TtqYcsyw== +"@types/node-fetch@^2.6.11": + version "2.6.11" + resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.11.tgz#9b39b78665dae0e82a08f02f4967d62c66f95d24" + integrity sha512-24xFj9R5+rfQJLRyM56qh+wnVSYhyXC2tkoBndtY0U+vubqNsYXGjufB2nn8Q6gt0LrARwL6UBtMCSVCwl4B1g== + dependencies: + "@types/node" "*" + form-data "^4.0.0" + "@types/node@*": version "17.0.10" resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.10.tgz#616f16e9d3a2a3d618136b1be244315d95bd7cab"