{"version":3,"file":"index.mjs","names":[],"sources":["../src/builder.ts","../src/runner.ts"],"sourcesContent":["import { DIDError, DIDMessage } from '@hiero-did-sdk/core';\nimport { Steps, CallbackStep, CatchStep } from './interfaces/steps';\n\n/**\n * A builder for constructing a lifecycle pipeline.\n * A lifecycle pipeline is a series of steps that are executed in order.\n */\nexport class LifecycleBuilder<Message extends DIDMessage, Context extends object = object> {\n  protected readonly pipeline: Steps<Message, Context>[] = [];\n  public catchStep?: CatchStep;\n\n  /**\n   * The number of steps in the pipeline.\n   * @returns The number of steps in the pipeline.\n   */\n  get length(): number {\n    return this.pipeline.length;\n  }\n\n  /**\n   * Gets the step at the specified index.\n   * @param stepIndex The index of the step to get.\n   * @returns The step at the specified index.\n   * @throws If the step does not exist.\n   */\n  getByIndex(stepIndex: number): Steps<Message, Context> {\n    if (stepIndex >= this.pipeline.length) {\n      throw new DIDError('internalError', 'Step index out of bounds');\n    }\n    return this.pipeline[stepIndex];\n  }\n\n  /**\n   * Gets the step with the specified label.\n   * @param stepLabel The label of the step to get.\n   * @returns The step with the specified label.\n   * @throws If the step does not exist.\n   */\n  getByLabel(stepLabel: string): Steps<Message, Context> {\n    const stepOrUndefined = this.pipeline.find((s) => s.label === stepLabel);\n\n    if (!stepOrUndefined) {\n      throw new DIDError('internalError', `Step with label '${stepLabel}' does not exist`);\n    }\n\n    return stepOrUndefined;\n  }\n\n  /**\n   * Gets the step with the specified label.\n   * @param stepLabel The label of the step to get.\n   * @returns The step with the specified label.\n   * @throws If the step does not exist.\n   */\n  getIndexByLabel(stepLabel: string): number {\n    const stepIndexOrUndefined = this.pipeline.findIndex((s) => s.label === stepLabel);\n\n    if (stepIndexOrUndefined < 0) {\n      throw new DIDError('internalError', `Step with label '${stepLabel}' does not exist`);\n    }\n\n    return stepIndexOrUndefined;\n  }\n\n  /**\n   * Adds a callback step to the pipeline.\n   * A callback step is a step that calls a callback function with a message and a publisher.\n   * The callback function can be used to perform any operation on the message, such as signing or verifying.\n   * @param label The label of the step.\n   * @param callback The callback function to be executed.\n   * @returns The builder instance.\n   */\n  callback(label: string, callback: CallbackStep<Message, Context>['callback']): this {\n    this.validateLabel(label);\n\n    this.pipeline.push({ type: 'callback', label, callback });\n    return this;\n  }\n\n  /**\n   * Adds a signature step to the pipeline.\n   * A signature step is a step that adds a provided signature to a message.\n   * @param label The label of the step.\n   * @returns The builder instance.\n   */\n  signature(label: string): this {\n    this.validateLabel(label);\n\n    this.pipeline.push({ type: 'signature', label });\n    return this;\n  }\n\n  /**\n   * Adds a sign step to the pipeline.\n   * A sign step is a step that signs a message using a provided Signer to the lifecycle pipeline.\n   * @param label The label of the step.\n   * @returns The builder instance.\n   */\n  signWithSigner(label: string): this {\n    this.validateLabel(label);\n\n    this.pipeline.push({ type: 'sign', label });\n    return this;\n  }\n\n  /**\n   * Adds a pause step to the pipeline.\n   * A pause step is a step that pauses the lifecycle pipeline. The pipeline can be resumed from the paused step.\n   * @param label The label of the step.\n   * @returns The builder instance.\n   */\n  pause(label: string): this {\n    this.validateLabel(label);\n\n    this.pipeline.push({ type: 'pause', label });\n    return this;\n  }\n\n  /**\n   * Adds a catch step to the pipeline.\n   * A catch step is a special step that catches errors in the whole lifecycle pipeline.\n   * Only one catch step can be added to the pipeline.\n   * @param label The label of the step.\n   * @param callback The callback function to be executed.\n   * @returns The builder instance.\n   */\n  catch(label: string, callback: CatchStep['callback']): this {\n    this.validateLabel(label);\n\n    this.catchStep = { type: 'catch', label, callback };\n    return this;\n  }\n\n  private validateLabel(label: string): void {\n    if (this.pipeline.some((s) => s.label === label)) {\n      throw new DIDError('internalError', `Step with label '${label}' already exists`);\n    }\n  }\n}\n","import { DIDError, DIDMessage, Publisher, Signer, Verifier } from '@hiero-did-sdk/core';\nimport { LifecycleBuilder } from './builder';\nimport { RunnerState } from './interfaces/runner-state';\nimport { HookFunction } from './interfaces/hooks';\n\n/**\n * Options for the lifecycle runner.\n */\nexport interface LifecycleRunnerOptions<Context extends object = object> {\n  /**\n   * Additional arguments to pass to the runner.\n   */\n  args?: {\n    /**\n     * Signature to use for the message.\n     */\n    signature?: Uint8Array;\n\n    /**\n     * Verifier to use for verifying the signature.\n     */\n    verifier?: Verifier;\n  };\n\n  /**\n   * The signer to use for signing the message.\n   */\n  signer?: Signer;\n\n  /**\n   * The step label to resume from.\n   */\n  label?: string;\n\n  /**\n   * The publisher to use for publishing the message.\n   */\n  publisher: Publisher;\n\n  /**\n   * The context to use for the runner.\n   */\n  context?: Context;\n}\n\n/**\n * A runner for executing a lifecycle pipeline.\n * A lifecycle pipeline is a series of steps that are executed in order.\n */\nexport class LifecycleRunner<Message extends DIDMessage, Context extends object = object> {\n  private readonly hooks: Record<string, HookFunction<Message>[]> = {};\n\n  /**\n   * Creates a new instance of the LifecycleRunner class.\n   * @param builder - The lifecycle builder to use.\n   * @returns A new instance of the LifecycleRunner class.\n   */\n  constructor(private readonly builder: LifecycleBuilder<Message, Context>) {}\n\n  /**\n   * Resumes the lifecycle pipeline from the specified state.\n   * @param state - The state to resume from.\n   * @param options - The options to use.\n   * @returns The state of the lifecycle pipeline after resuming.\n   * @throws If an error occurs during the process.\n   */\n  async resume(state: RunnerState<Message>, options: LifecycleRunnerOptions<Context>): Promise<RunnerState<Message>> {\n    return this.process(state.message, { ...options, label: state.label });\n  }\n\n  /**\n   * Starts the lifecycle pipeline from the beginning and processes the message.\n   * @param message - The message to process.\n   * @param options - The options to use.\n   * @returns The state of the lifecycle pipeline after processing.\n   * @throws If an error occurs during the process. If a catch step is not provided, the error is rethrown.\n   */\n  async process(message: Message, options: LifecycleRunnerOptions<Context>): Promise<RunnerState<Message>> {\n    try {\n      const initialStep = options.label ? (this.builder.getIndexByLabel(options.label) ?? -1) + 1 : 0;\n\n      for (let stepIndex = initialStep; stepIndex < this.builder.length; stepIndex++) {\n        const step = this.builder.getByIndex(stepIndex);\n        const prevStep = stepIndex > 0 ? this.builder.getByIndex(stepIndex - 1) : undefined;\n\n        if (prevStep?.type === 'pause') {\n          await this.callHooks(prevStep.label, message);\n        }\n\n        if (step.type === 'callback') {\n          await step.callback(message, options.publisher, options.context);\n          await this.callHooks(step.label, message);\n\n          continue;\n        }\n\n        if (step.type === 'sign') {\n          if (!options.signer || !options.args?.verifier) {\n            throw new DIDError('invalidArgument', 'Signer and verifier are required for the sign step');\n          }\n\n          await message.signWith(options.signer, options.args.verifier);\n          await this.callHooks(step.label, message);\n\n          continue;\n        }\n\n        if (step.type === 'signature') {\n          if (!options.args?.signature || !options.args?.verifier) {\n            throw new DIDError('invalidArgument', 'Signature and verifier are required for the signature step');\n          }\n\n          await message.setSignature(options.args.signature, options.args.verifier);\n          await this.callHooks(step.label, message);\n\n          continue;\n        }\n\n        if (step.type === 'pause') {\n          // Pause the process\n          return {\n            message,\n            status: 'pause',\n            index: stepIndex,\n            label: step.label,\n          };\n        }\n      }\n\n      return {\n        message,\n        status: 'success',\n        label: '',\n        index: -1,\n      };\n    } catch (error: unknown) {\n      if (!this.builder.catchStep) {\n        throw error;\n      }\n\n      await this.builder.catchStep.callback(error);\n      return {\n        message,\n        status: 'error',\n        label: '',\n        index: -1,\n      };\n    }\n  }\n\n  /**\n   * Registers a hook to be called when the specified label is completed.\n   * Hooks are called in the parallel been the completion of the label and the next step.\n   * @param label Name of the label to hook into.\n   * @param callback The hook function to call.\n   */\n  onComplete(label: string, callback: HookFunction<Message>): void {\n    this.hooks[label] = this.hooks[label] ?? [];\n    this.hooks[label].push(callback);\n  }\n\n  /**\n   * Calls the hooks for the specified label.\n   * @param label The label to call the hooks for.\n   */\n  private async callHooks(label: string, message: Message) {\n    const hooks = this.hooks[label] ?? [];\n\n    // eslint-disable-next-line @typescript-eslint/await-thenable\n    await Promise.all(hooks.map((hook) => hook(message)));\n  }\n}\n"],"mappings":";;;;;;;AAOA,IAAa,mBAAb,MAA2F;;OACtE,WAAsC,EAAE;;;;;;CAO3D,IAAI,SAAiB;AACnB,SAAO,KAAK,SAAS;;;;;;;;CASvB,WAAW,WAA4C;AACrD,MAAI,aAAa,K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