@segment/actions-core
Advanced tools
Comparing version 3.141.0 to 3.142.0
@@ -256,4 +256,5 @@ import { EventEmitterSlug } from '@segment/action-emitters'; | ||
private getDestinationSettings; | ||
handleAuthError(error: ResponseError & HTTPError, settings: JSONObject, options?: OnEventOptions): Promise<JSONObject>; | ||
handleError(error: ResponseError | HTTPError, settings: JSONObject, options?: OnEventOptions): Promise<JSONObject>; | ||
handleAuthError(settings: JSONObject, options?: OnEventOptions): Promise<JSONObject>; | ||
refreshTokenAndGetNewToken(settings: JSONObject, options?: OnEventOptions): Promise<RefreshAccessTokenResult>; | ||
} |
@@ -81,3 +81,3 @@ "use strict"; | ||
const onFailedAttempt = async (error) => { | ||
settings = await this.handleAuthError(error, settings); | ||
settings = await this.handleError(error, settings); | ||
}; | ||
@@ -106,3 +106,3 @@ return await retry_1.retry(run, { retries: 2, onFailedAttempt }); | ||
const onFailedAttempt = async (error) => { | ||
settings = await this.handleAuthError(error, settings); | ||
settings = await this.handleError(error, settings); | ||
}; | ||
@@ -368,3 +368,3 @@ return await retry_1.retry(run, { retries: 2, onFailedAttempt }); | ||
const onFailedAttempt = async (error) => { | ||
settings = await this.handleAuthError(error, settings, options); | ||
settings = await this.handleError(error, settings, options); | ||
}; | ||
@@ -383,6 +383,19 @@ return await retry_1.retry(run, { retries: 2, onFailedAttempt }); | ||
}; | ||
const MAX_ATTEMPTS = 2; | ||
const onFailedAttempt = async (error) => { | ||
settings = await this.handleAuthError(error, settings, options); | ||
settings = await this.handleError(error, settings, options); | ||
}; | ||
return await retry_1.retry(run, { retries: 2, onFailedAttempt }); | ||
const shouldRetry = async (response, attemptCount) => { | ||
const results = response; | ||
for (const result of results) { | ||
const has401Errors = result?.multistatus?.some((event) => event.status === 401); | ||
const isOAuthDestination = this.authentication?.scheme === 'oauth2' || this.authentication?.scheme === 'oauth-managed'; | ||
if (attemptCount <= MAX_ATTEMPTS && has401Errors && isOAuthDestination) { | ||
await this.handleAuthError(settings, options); | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
return await retry_1.retry(run, { retries: MAX_ATTEMPTS, onFailedAttempt, shouldRetry }); | ||
} | ||
@@ -407,4 +420,4 @@ getSubscriptions(settings) { | ||
} | ||
async handleAuthError(error, settings, options) { | ||
const statusCode = error?.status ?? error?.response?.status ?? 500; | ||
async handleError(error, settings, options) { | ||
const statusCode = error.status ?? error?.response?.status ?? 500; | ||
const needsReauthentication = statusCode === 401 && | ||
@@ -415,2 +428,5 @@ (this.authentication?.scheme === 'oauth2' || this.authentication?.scheme === 'oauth-managed'); | ||
} | ||
return this.handleAuthError(settings, options); | ||
} | ||
async handleAuthError(settings, options) { | ||
const newTokens = await this.refreshTokenAndGetNewToken(settings, options); | ||
@@ -417,0 +433,0 @@ await options?.onTokenRefresh?.(newTokens); |
export declare function sha256SmartHash(value: string): string; | ||
export declare function sha1Hash(value: string): string; | ||
export declare class SmartHashing { | ||
@@ -3,0 +4,0 @@ encryptionMethod: 'sha256'; |
@@ -22,3 +22,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SmartHashing = exports.sha256SmartHash = void 0; | ||
exports.SmartHashing = exports.sha1Hash = exports.sha256SmartHash = void 0; | ||
const crypto = __importStar(require("crypto")); | ||
@@ -33,2 +33,6 @@ const sha256HashedRegex = /^[a-f0-9]{64}$/i; | ||
exports.sha256SmartHash = sha256SmartHash; | ||
function sha1Hash(value) { | ||
return crypto.createHash('sha1').update(value).digest('hex'); | ||
} | ||
exports.sha1Hash = sha1Hash; | ||
class SmartHashing { | ||
@@ -35,0 +39,0 @@ constructor(encryptionMethod) { |
@@ -15,3 +15,3 @@ export { Destination, fieldsToJsonSchema } from './destination-kit'; | ||
export { removeUndefined } from './remove-undefined'; | ||
export { sha256SmartHash, SmartHashing } from './hashing-utils'; | ||
export { sha256SmartHash, SmartHashing, sha1Hash } from './hashing-utils'; | ||
export { time, duration } from './time'; | ||
@@ -18,0 +18,0 @@ export { realTypeOf, isObject, isArray, isString } from './real-type-of'; |
@@ -6,4 +6,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ActionDestinationSuccessResponse = exports.Headers = exports.Response = exports.Request = exports.fetch = exports.DEFAULT_REQUEST_TIMEOUT = exports.HTTPError = exports.isString = exports.isArray = exports.isObject = exports.realTypeOf = exports.duration = exports.time = exports.SmartHashing = exports.sha256SmartHash = exports.removeUndefined = exports.omit = exports.get = exports.retry = exports.getErrorCodeFromHttpStatus = exports.CustomErrorCodes = exports.HttpErrorCodes = exports.ErrorCodes = exports.APIError = exports.SelfTimeoutError = exports.PayloadValidationError = exports.RetryableError = exports.InvalidAuthenticationError = exports.IntegrationError = exports.defaultValues = exports.createRequestClient = exports.createInstance = exports.createTestIntegration = exports.createTestEvent = exports.isExcludeWhenNullDirective = exports.isJSONDirective = exports.isTemplateDirective = exports.isReplaceDirective = exports.isPathDirective = exports.isLiteralDirective = exports.isIfDirective = exports.isDirective = exports.isCaseDirective = exports.isArrayPathDirective = exports.getFieldValueKeys = exports.getFieldValue = exports.transform = exports.getAuthData = exports.fieldsToJsonSchema = exports.Destination = void 0; | ||
exports.MultiStatusResponse = exports.ActionDestinationErrorResponse = void 0; | ||
exports.Headers = exports.Response = exports.Request = exports.fetch = exports.DEFAULT_REQUEST_TIMEOUT = exports.HTTPError = exports.isString = exports.isArray = exports.isObject = exports.realTypeOf = exports.duration = exports.time = exports.sha1Hash = exports.SmartHashing = exports.sha256SmartHash = exports.removeUndefined = exports.omit = exports.get = exports.retry = exports.getErrorCodeFromHttpStatus = exports.CustomErrorCodes = exports.HttpErrorCodes = exports.ErrorCodes = exports.APIError = exports.SelfTimeoutError = exports.PayloadValidationError = exports.RetryableError = exports.InvalidAuthenticationError = exports.IntegrationError = exports.defaultValues = exports.createRequestClient = exports.createInstance = exports.createTestIntegration = exports.createTestEvent = exports.isExcludeWhenNullDirective = exports.isJSONDirective = exports.isTemplateDirective = exports.isReplaceDirective = exports.isPathDirective = exports.isLiteralDirective = exports.isIfDirective = exports.isDirective = exports.isCaseDirective = exports.isArrayPathDirective = exports.getFieldValueKeys = exports.getFieldValue = exports.transform = exports.getAuthData = exports.fieldsToJsonSchema = exports.Destination = void 0; | ||
exports.MultiStatusResponse = exports.ActionDestinationErrorResponse = exports.ActionDestinationSuccessResponse = void 0; | ||
var destination_kit_1 = require("./destination-kit"); | ||
@@ -61,2 +61,3 @@ Object.defineProperty(exports, "Destination", { enumerable: true, get: function () { return destination_kit_1.Destination; } }); | ||
Object.defineProperty(exports, "SmartHashing", { enumerable: true, get: function () { return hashing_utils_1.SmartHashing; } }); | ||
Object.defineProperty(exports, "sha1Hash", { enumerable: true, get: function () { return hashing_utils_1.sha1Hash; } }); | ||
var time_1 = require("./time"); | ||
@@ -63,0 +64,0 @@ Object.defineProperty(exports, "time", { enumerable: true, get: function () { return time_1.time; } }); |
interface RetryOptions { | ||
retries?: number; | ||
onFailedAttempt?: (error: any, attemptCount: number) => PromiseLike<void> | void; | ||
shouldRetry?: (response: any, attemptCount: number) => PromiseLike<boolean> | boolean; | ||
} | ||
export declare function retry<T>(input: (attemptCount: number) => PromiseLike<T> | T, options?: RetryOptions): Promise<T>; | ||
export {}; |
@@ -9,3 +9,10 @@ "use strict"; | ||
try { | ||
return await input(attemptCount); | ||
const response = await input(attemptCount); | ||
if (options?.shouldRetry) { | ||
const success = await options.shouldRetry(response, attemptCount); | ||
if (!success && attemptCount < retries) { | ||
continue; | ||
} | ||
} | ||
return response; | ||
} | ||
@@ -12,0 +19,0 @@ catch (error) { |
@@ -256,4 +256,5 @@ import { EventEmitterSlug } from '@segment/action-emitters'; | ||
private getDestinationSettings; | ||
handleAuthError(error: ResponseError & HTTPError, settings: JSONObject, options?: OnEventOptions): Promise<JSONObject>; | ||
handleError(error: ResponseError | HTTPError, settings: JSONObject, options?: OnEventOptions): Promise<JSONObject>; | ||
handleAuthError(settings: JSONObject, options?: OnEventOptions): Promise<JSONObject>; | ||
refreshTokenAndGetNewToken(settings: JSONObject, options?: OnEventOptions): Promise<RefreshAccessTokenResult>; | ||
} |
@@ -75,3 +75,3 @@ import { validate, parseFql } from '@segment/destination-subscriptions'; | ||
const onFailedAttempt = async (error) => { | ||
settings = await this.handleAuthError(error, settings); | ||
settings = await this.handleError(error, settings); | ||
}; | ||
@@ -100,3 +100,3 @@ return await retry(run, { retries: 2, onFailedAttempt }); | ||
const onFailedAttempt = async (error) => { | ||
settings = await this.handleAuthError(error, settings); | ||
settings = await this.handleError(error, settings); | ||
}; | ||
@@ -362,3 +362,3 @@ return await retry(run, { retries: 2, onFailedAttempt }); | ||
const onFailedAttempt = async (error) => { | ||
settings = await this.handleAuthError(error, settings, options); | ||
settings = await this.handleError(error, settings, options); | ||
}; | ||
@@ -377,6 +377,19 @@ return await retry(run, { retries: 2, onFailedAttempt }); | ||
}; | ||
const MAX_ATTEMPTS = 2; | ||
const onFailedAttempt = async (error) => { | ||
settings = await this.handleAuthError(error, settings, options); | ||
settings = await this.handleError(error, settings, options); | ||
}; | ||
return await retry(run, { retries: 2, onFailedAttempt }); | ||
const shouldRetry = async (response, attemptCount) => { | ||
const results = response; | ||
for (const result of results) { | ||
const has401Errors = result?.multistatus?.some((event) => event.status === 401); | ||
const isOAuthDestination = this.authentication?.scheme === 'oauth2' || this.authentication?.scheme === 'oauth-managed'; | ||
if (attemptCount <= MAX_ATTEMPTS && has401Errors && isOAuthDestination) { | ||
await this.handleAuthError(settings, options); | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
return await retry(run, { retries: MAX_ATTEMPTS, onFailedAttempt, shouldRetry }); | ||
} | ||
@@ -401,4 +414,4 @@ getSubscriptions(settings) { | ||
} | ||
async handleAuthError(error, settings, options) { | ||
const statusCode = error?.status ?? error?.response?.status ?? 500; | ||
async handleError(error, settings, options) { | ||
const statusCode = error.status ?? error?.response?.status ?? 500; | ||
const needsReauthentication = statusCode === 401 && | ||
@@ -409,2 +422,5 @@ (this.authentication?.scheme === 'oauth2' || this.authentication?.scheme === 'oauth-managed'); | ||
} | ||
return this.handleAuthError(settings, options); | ||
} | ||
async handleAuthError(settings, options) { | ||
const newTokens = await this.refreshTokenAndGetNewToken(settings, options); | ||
@@ -411,0 +427,0 @@ await options?.onTokenRefresh?.(newTokens); |
export declare function sha256SmartHash(value: string): string; | ||
export declare function sha1Hash(value: string): string; | ||
export declare class SmartHashing { | ||
@@ -3,0 +4,0 @@ encryptionMethod: 'sha256'; |
@@ -9,2 +9,5 @@ import * as crypto from 'crypto'; | ||
} | ||
export function sha1Hash(value) { | ||
return crypto.createHash('sha1').update(value).digest('hex'); | ||
} | ||
export class SmartHashing { | ||
@@ -11,0 +14,0 @@ constructor(encryptionMethod) { |
@@ -15,3 +15,3 @@ export { Destination, fieldsToJsonSchema } from './destination-kit'; | ||
export { removeUndefined } from './remove-undefined'; | ||
export { sha256SmartHash, SmartHashing } from './hashing-utils'; | ||
export { sha256SmartHash, SmartHashing, sha1Hash } from './hashing-utils'; | ||
export { time, duration } from './time'; | ||
@@ -18,0 +18,0 @@ export { realTypeOf, isObject, isArray, isString } from './real-type-of'; |
@@ -15,3 +15,3 @@ export { Destination, fieldsToJsonSchema } from './destination-kit'; | ||
export { removeUndefined } from './remove-undefined'; | ||
export { sha256SmartHash, SmartHashing } from './hashing-utils'; | ||
export { sha256SmartHash, SmartHashing, sha1Hash } from './hashing-utils'; | ||
export { time, duration } from './time'; | ||
@@ -18,0 +18,0 @@ export { realTypeOf, isObject, isArray, isString } from './real-type-of'; |
interface RetryOptions { | ||
retries?: number; | ||
onFailedAttempt?: (error: any, attemptCount: number) => PromiseLike<void> | void; | ||
shouldRetry?: (response: any, attemptCount: number) => PromiseLike<boolean> | boolean; | ||
} | ||
export declare function retry<T>(input: (attemptCount: number) => PromiseLike<T> | T, options?: RetryOptions): Promise<T>; | ||
export {}; |
@@ -6,3 +6,10 @@ const DEFAULT_RETRY_ATTEMPTS = 2; | ||
try { | ||
return await input(attemptCount); | ||
const response = await input(attemptCount); | ||
if (options?.shouldRetry) { | ||
const success = await options.shouldRetry(response, attemptCount); | ||
if (!success && attemptCount < retries) { | ||
continue; | ||
} | ||
} | ||
return response; | ||
} | ||
@@ -9,0 +16,0 @@ catch (error) { |
{ | ||
"name": "@segment/actions-core", | ||
"description": "Core runtime for Destinations Actions.", | ||
"version": "3.141.0", | ||
"version": "3.142.0", | ||
"repository": { | ||
@@ -112,3 +112,3 @@ "type": "git", | ||
}, | ||
"gitHead": "b3bf23153b9752f1021e8f75ea1d1c96acc50a9e" | ||
"gitHead": "baf2c0a064e6198cae1327bfb6fe04d0bd3105a3" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
591307
7969