From 1e086ee3306d419dd1cd348f759d909fc7a2d3c2 Mon Sep 17 00:00:00 2001 From: Luke Elmers Date: Tue, 8 Dec 2020 20:56:54 -0700 Subject: [PATCH] Fix bug where AbortController was not polyfilled on the server. --- .../expressions/common/execution/execution.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/plugins/expressions/common/execution/execution.ts b/src/plugins/expressions/common/execution/execution.ts index 9eae7fd717eda..c5c7d82e223b0 100644 --- a/src/plugins/expressions/common/execution/execution.ts +++ b/src/plugins/expressions/common/execution/execution.ts @@ -40,6 +40,23 @@ import { getByAlias } from '../util/get_by_alias'; import { ExecutionContract } from './execution_contract'; import { ExpressionExecutionParams } from '../service'; +/** + * AbortController is not available in Node until v15, so we + * need to temporarily mock it for plugins using expressions + * on the server. + * + * TODO: Remove this once Kibana is upgraded to Node 15. + */ +const getNewAbortController = (): AbortController => { + try { + return new AbortController(); + } catch (error) { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const polyfill = require('abortcontroller-polyfill/dist/cjs-ponyfill'); + return new polyfill.AbortController(); + } +}; + const createAbortErrorValue = () => createError({ message: 'The expression was aborted.', @@ -87,7 +104,7 @@ export class Execution< /** * AbortController to cancel this Execution. */ - private readonly abortController = new AbortController(); + private readonly abortController = getNewAbortController(); /** * Promise that rejects if/when abort controller sends "abort" signal.