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BAAiB,EAAE,KAAK,CAAC,CAAC;QAC3D,CAAC;QACD,WAAW,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;IACjC,CAAC;AACH,CAAC;AAhFD,sCAgFC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport {\n LroError,\n InnerError,\n Operation,\n OperationStatus,\n RestorableOperationState,\n StateProxy,\n} from \"./models.js\";\nimport { logger } from \"../logger.js\";\nimport { terminalStates } from \"./constants.js\";\n\n/**\n * Deserializes the state\n */\nexport function deserializeState(\n serializedState: string,\n): RestorableOperationState {\n try {\n return JSON.parse(serializedState).state;\n } catch (e) {\n throw new Error(`Unable to deserialize input state: ${serializedState}`);\n }\n}\n\nfunction setStateError(inputs: {\n state: TState;\n stateProxy: StateProxy;\n isOperationError: (error: Error) => boolean;\n}): (error: Error) => never {\n const { state, stateProxy, isOperationError } = inputs;\n return (error: Error) => {\n if (isOperationError(error)) {\n stateProxy.setError(state, error);\n stateProxy.setFailed(state);\n }\n throw error;\n };\n}\n\nfunction appendReadableErrorMessage(currentMessage: string, innerMessage: string): string {\n let message = currentMessage;\n if (message.slice(-1) !== \".\") {\n message = message + \".\";\n }\n return message + \" \" + innerMessage;\n}\n\nfunction simplifyError(err: LroError): {\n code: string;\n message: string;\n} {\n let message = err.message;\n let code = err.code;\n let curErr = err as InnerError;\n while (curErr.innererror) {\n curErr = curErr.innererror;\n code = curErr.code;\n message = appendReadableErrorMessage(message, curErr.message);\n }\n return {\n code,\n message,\n };\n}\n\nfunction processOperationStatus(result: {\n status: OperationStatus;\n response: TResponse;\n state: RestorableOperationState;\n stateProxy: StateProxy;\n processResult?: (result: TResponse, state: TState) => TResult;\n getError?: (response: TResponse) => LroError | undefined;\n isDone?: (lastResponse: TResponse, state: TState) => boolean;\n setErrorAsResult: boolean;\n}): void {\n const { state, stateProxy, status, isDone, processResult, getError, response, setErrorAsResult } =\n result;\n switch (status) {\n case \"succeeded\": {\n stateProxy.setSucceeded(state);\n break;\n }\n case \"failed\": {\n const err = getError?.(response);\n let postfix = \"\";\n if (err) {\n const { code, message } = simplifyError(err);\n postfix = `. ${code}. ${message}`;\n }\n const errStr = `The long-running operation has failed${postfix}`;\n stateProxy.setError(state, new Error(errStr));\n stateProxy.setFailed(state);\n logger.warning(errStr);\n break;\n }\n case \"canceled\": {\n stateProxy.setCanceled(state);\n break;\n }\n }\n if (\n isDone?.(response, state) ||\n (isDone === undefined &&\n [\"succeeded\", \"canceled\"].concat(setErrorAsResult ? [] : [\"failed\"]).includes(status))\n ) {\n stateProxy.setResult(\n state,\n buildResult({\n response,\n state,\n processResult,\n }),\n );\n }\n}\n\nfunction buildResult(inputs: {\n response: TResponse;\n state: TState;\n processResult?: (result: TResponse, state: TState) => TResult;\n}): TResult {\n const { processResult, response, state } = inputs;\n return processResult ? processResult(response, state) : (response as unknown as TResult);\n}\n\n/**\n * Initiates the long-running operation.\n */\nexport async function initOperation(inputs: {\n init: Operation[\"init\"];\n stateProxy: StateProxy;\n getOperationStatus: (inputs: {\n response: TResponse;\n state: RestorableOperationState;\n operationLocation?: string;\n }) => OperationStatus;\n processResult?: (result: TResponse, state: TState) => TResult;\n withOperationLocation?: (operationLocation: string, isUpdated: boolean) => void;\n setErrorAsResult: boolean;\n}): Promise> {\n const {\n init,\n stateProxy,\n processResult,\n getOperationStatus,\n withOperationLocation,\n setErrorAsResult,\n } = inputs;\n const { operationLocation, resourceLocation, metadata, response } = await init();\n if (operationLocation) withOperationLocation?.(operationLocation, false);\n const config = {\n metadata,\n operationLocation,\n resourceLocation,\n };\n logger.verbose(`LRO: Operation description:`, config);\n const state = stateProxy.initState(config);\n const status = getOperationStatus({ response, state, operationLocation });\n processOperationStatus({ state, status, stateProxy, response, setErrorAsResult, processResult });\n return state;\n}\n\nasync function pollOperationHelper(inputs: {\n poll: Operation[\"poll\"];\n stateProxy: StateProxy;\n state: RestorableOperationState;\n operationLocation: string;\n getOperationStatus: (\n response: TResponse,\n state: RestorableOperationState,\n ) => OperationStatus;\n getResourceLocation: (\n response: TResponse,\n state: RestorableOperationState,\n ) => string | undefined;\n isOperationError: (error: Error) => boolean;\n options?: TOptions;\n}): Promise<{\n status: OperationStatus;\n response: TResponse;\n}> {\n const {\n poll,\n state,\n stateProxy,\n operationLocation,\n getOperationStatus,\n getResourceLocation,\n isOperationError,\n options,\n } = inputs;\n const response = await poll(operationLocation, options).catch(\n setStateError({\n state,\n stateProxy,\n isOperationError,\n }),\n );\n const status = getOperationStatus(response, state);\n logger.verbose(\n `LRO: Status:\\n\\tPolling from: ${\n state.config.operationLocation\n }\\n\\tOperation status: ${status}\\n\\tPolling status: ${\n terminalStates.includes(status) ? \"Stopped\" : \"Running\"\n }`,\n );\n if (status === \"succeeded\") {\n const resourceLocation = getResourceLocation(response, state);\n if (resourceLocation !== undefined) {\n return {\n response: await poll(resourceLocation).catch(\n setStateError({ state, stateProxy, isOperationError }),\n ),\n status,\n };\n }\n }\n return { response, status };\n}\n\n/** Polls the long-running operation. */\nexport async function pollOperation(inputs: {\n poll: Operation[\"poll\"];\n stateProxy: StateProxy;\n state: RestorableOperationState;\n getOperationStatus: (\n response: TResponse,\n state: RestorableOperationState,\n ) => OperationStatus;\n getResourceLocation: (\n response: TResponse,\n state: RestorableOperationState,\n ) => string | undefined;\n isOperationError: (error: Error) => boolean;\n getPollingInterval?: (response: TResponse) => number | undefined;\n setDelay: (intervalInMs: number) => void;\n getOperationLocation?: (\n response: TResponse,\n state: RestorableOperationState,\n ) => string | undefined;\n withOperationLocation?: (operationLocation: string, isUpdated: boolean) => void;\n processResult?: (result: TResponse, state: TState) => TResult;\n getError?: (response: TResponse) => LroError | undefined;\n updateState?: (state: TState, lastResponse: TResponse) => void;\n isDone?: (lastResponse: TResponse, state: TState) => boolean;\n setErrorAsResult: boolean;\n options?: TOptions;\n}): Promise {\n const {\n poll,\n state,\n stateProxy,\n options,\n getOperationStatus,\n getResourceLocation,\n getOperationLocation,\n isOperationError,\n withOperationLocation,\n getPollingInterval,\n processResult,\n getError,\n updateState,\n setDelay,\n isDone,\n setErrorAsResult,\n } = inputs;\n const { operationLocation } = state.config;\n if (operationLocation !== undefined) {\n const { response, status } = await pollOperationHelper({\n poll,\n getOperationStatus,\n state,\n stateProxy,\n operationLocation,\n getResourceLocation,\n isOperationError,\n options,\n });\n processOperationStatus({\n status,\n response,\n state,\n stateProxy,\n isDone,\n processResult,\n getError,\n setErrorAsResult,\n });\n\n if (!terminalStates.includes(status)) {\n const intervalInMs = getPollingInterval?.(response);\n if (intervalInMs) setDelay(intervalInMs);\n const location = getOperationLocation?.(response, state);\n if (location !== undefined) {\n const isUpdated = operationLocation !== location;\n state.config.operationLocation = location;\n withOperationLocation?.(location, isUpdated);\n } else withOperationLocation?.(operationLocation, false);\n }\n updateState?.(state, response);\n }\n}\n"]}