-
Notifications
You must be signed in to change notification settings - Fork 0
fix: avoid TypeError when Apollo withDisabledDeprecations is not a function #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import EventEmitter from 'events'; | ||
| import { createRequire } from 'module'; | ||
| import { cliux } from '@contentstack/cli-utilities'; | ||
| import { ApolloClient, ObservableQuery } from '@apollo/client/core'; | ||
| import { Ora } from 'ora'; | ||
|
|
@@ -8,6 +9,8 @@ import { deploymentQuery, deploymentLogsQuery, serverlessLogsQuery } from '../gr | |
| import { setTimeout as sleep } from 'timers/promises'; | ||
| import { isNotDevelopment } from './apollo-client'; | ||
|
|
||
| const requireApolloDeprecation = createRequire(__filename); | ||
|
|
||
| export default class LogPolling { | ||
| private config: ConfigType; | ||
| private $event!: EventEmitter; | ||
|
|
@@ -33,19 +36,25 @@ export default class LogPolling { | |
| * only during its execution, and restored immediately after. | ||
| */ | ||
| private withDeprecationsDisabled<T>(fn: () => T): T { | ||
|
|
||
| if (!isNotDevelopment) { | ||
| return fn(); | ||
| } | ||
| let withDisabledDeprecations: any; | ||
|
|
||
| let withDisabledDeprecations: unknown; | ||
| try { | ||
| withDisabledDeprecations = require('@apollo/client/utilities/deprecation').withDisabledDeprecations; | ||
| withDisabledDeprecations = requireApolloDeprecation( | ||
| '@apollo/client/utilities/deprecation', | ||
| ).withDisabledDeprecations; | ||
| } catch { | ||
| return fn(); | ||
| } | ||
|
|
||
| const handler = withDisabledDeprecations(); | ||
| if (typeof withDisabledDeprecations !== 'function') { | ||
| return fn(); | ||
| } | ||
|
|
||
| const handler = (withDisabledDeprecations as () => unknown)(); | ||
|
|
||
| try { | ||
| return fn(); | ||
| } finally { | ||
|
|
@@ -61,14 +70,18 @@ export default class LogPolling { | |
| dispose.call(handler); | ||
| return; | ||
| } | ||
| } catch {} | ||
| } catch { | ||
| void 0; | ||
| } | ||
|
Comment on lines
+73
to
+75
|
||
| try { | ||
| const asyncDispose = (handler as any)[(Symbol as any).asyncDispose]; | ||
| if (typeof asyncDispose === 'function') { | ||
| asyncDispose.call(handler); | ||
| return; | ||
| } | ||
| } catch {} | ||
| } catch { | ||
| void 0; | ||
| } | ||
|
Comment on lines
+82
to
+84
|
||
| try { | ||
| const symbols = Object.getOwnPropertySymbols(handler); | ||
| for (const sym of symbols) { | ||
|
|
@@ -78,7 +91,9 @@ export default class LogPolling { | |
| break; | ||
| } | ||
| } | ||
| } catch {} | ||
| } catch { | ||
| void 0; | ||
| } | ||
|
Comment on lines
+94
to
+96
|
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -101,17 +116,17 @@ export default class LogPolling { | |
| > { | ||
| return this.withDeprecationsDisabled(() => { | ||
| const statusWatchQuery = this.apolloManageClient.watchQuery({ | ||
| fetchPolicy: 'network-only', | ||
| query: deploymentQuery, | ||
| variables: { | ||
| query: { | ||
| uid: this.config.deployment, | ||
| environment: this.config.environment, | ||
| fetchPolicy: 'network-only', | ||
| query: deploymentQuery, | ||
| variables: { | ||
| query: { | ||
| uid: this.config.deployment, | ||
| environment: this.config.environment, | ||
| }, | ||
| }, | ||
| }, | ||
| pollInterval: this.config.pollingInterval, | ||
| errorPolicy: 'all', | ||
| }); | ||
| pollInterval: this.config.pollingInterval, | ||
| errorPolicy: 'all', | ||
| }); | ||
| return statusWatchQuery; | ||
| }); | ||
| } | ||
|
|
@@ -146,14 +161,14 @@ export default class LogPolling { | |
| }); | ||
| const logsWatchQuery = this.withDeprecationsDisabled(() => { | ||
| return this.apolloLogsClient.watchQuery({ | ||
| fetchPolicy: 'network-only', | ||
| query: deploymentLogsQuery, | ||
| variables: { | ||
| deploymentUid: this.config.deployment, | ||
| }, | ||
| pollInterval: this.config.pollingInterval, | ||
| errorPolicy: 'all', | ||
| }); | ||
| fetchPolicy: 'network-only', | ||
| query: deploymentLogsQuery, | ||
| variables: { | ||
| deploymentUid: this.config.deployment, | ||
| }, | ||
| pollInterval: this.config.pollingInterval, | ||
| errorPolicy: 'all', | ||
| }); | ||
| }); | ||
| this.subscribeDeploymentLogs(logsWatchQuery); | ||
| } | ||
|
|
@@ -243,19 +258,19 @@ export default class LogPolling { | |
|
|
||
| const serverLogsWatchQuery = this.withDeprecationsDisabled(() => { | ||
| return this.apolloLogsClient.watchQuery({ | ||
| fetchPolicy: 'network-only', | ||
| query: serverlessLogsQuery, | ||
| variables: { | ||
| query: { | ||
| environmentUid: this.config.environment, | ||
| startTime: this.startTime, | ||
| endTime: this.endTime, | ||
| deploymentUid: this.config.deployment, | ||
| fetchPolicy: 'network-only', | ||
| query: serverlessLogsQuery, | ||
| variables: { | ||
| query: { | ||
| environmentUid: this.config.environment, | ||
| startTime: this.startTime, | ||
| endTime: this.endTime, | ||
| deploymentUid: this.config.deployment, | ||
| }, | ||
| }, | ||
| }, | ||
| pollInterval: this.config.pollingInterval, | ||
| errorPolicy: 'all', | ||
| }); | ||
| pollInterval: this.config.pollingInterval, | ||
| errorPolicy: 'all', | ||
| }); | ||
| }); | ||
| this.subscribeServerLogs(serverLogsWatchQuery); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new guard that falls back when
withDisabledDeprecationsis missing or not a function is the core of this fix, but it isn’t covered by tests. Please add a regression test that simulates the module exporting a non-function (or missing)withDisabledDeprecationsand assertswithDeprecationsDisabledstill runsfn()without throwing.