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,IAAI,aAAc,OAAO,iBAAiB,EAAK,MAAM,YAI/D,MAAM,QAAe,QAAQ,EAAO,GAC9B,QAAiB,EAAQ,OAAO,IAEhC,MAAE,SAAgB,EAAM,QAAQ,WAAW,SAAS,MAAM,CAC9D,KAAM,EACN,OAAQ,EACR,MAAO,aAAa,MAAM,KAW5B,OAJI,GACF,EAAM,MAAM,GAGP,CACT,UAUK,MAAM,QAAU,MAAO,EAAO,KACnC,MAAM,EAAW,EAAM,YAAY,KAAK,GAAM,IAAI,EAAO,KACnD,QACE,QAAQ,IAAI,GAGpB,OAAO,QAAQ,MAAM,CAAE,YAAU,SAW5B,MAAM,IAAM,MAAO,EAAO,KAC/B,MAAM,QAAe,EAAM,QAAQ,WAAW,SAAS,IAAI,EAAW,QACtE,GAAI,EAAO,GACT,OAAO,EAAO,GAKhB,MAAM,EAA4B,EAQlC,aAAa,OAAO,OAAO,EAAY,EAAM,SAOxC,MAAM,cAAiB,IAAY,CACxC,OAAQ,oBAAoB,GAC5B,QAAS,qBAAqB,GAC9B,SAAU,sBAAsB,GAChC,SAAU,sBAAsB,GAChC,SAAU,sBAAsB,GAChC,aAAc,uBAAuB,GACrC,MAAO,mBAAmB,GAC1B,MAAO,mBAAmB,GAC1B,MAAO,mBAAmB,GAC1B,aAAc,0BAA0B,GACxC,OAAQ,oBAAoB,GAC5B,KAAM,kBAAkB,GACxB,KAAM,kBAAkB,GACxB,eAAkB,iBAAiB,GAEnC,SAAU,sBAAsB,GAAS,SACzC,MAAO,mBAAmB,YASrB,MAAM,QAAU,EAAG,KAAI,UAAS,QAAQ,IAAI,YACjD,OAAO,QAAQ,CACb,KACA,UACA,iBAIF,qCACA,mCACA","file":"/npm/@web3-storage/upload-api@19.2.0/src/lib.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@web3-storage/upload-api@19.2.0/src/lib.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import * as Server from '@ucanto/server/server'\n\nimport * as Client from '@ucanto/client'\nimport { Message } from '@ucanto/core'\nimport * as Types from './types.js'\nimport * as Legacy from '@ucanto/transport/legacy'\nimport * as CAR from '@ucanto/transport/car'\nimport { create as createRevocationChecker } from './utils/revocation.js'\nimport { createService as createStoreService } from './store.js'\nimport { createService as createUploadService } from './upload.js'\nimport { createService as createConsoleService } from './console.js'\nimport { createService as createAccessService } from './access.js'\nimport { createService as createConsumerService } from './consumer.js'\nimport { createService as createCustomerService } from './customer.js'\nimport { createService as createSpaceService } from './space.js'\nimport { createService as createProviderService } from './provider.js'\nimport { createService as createSubscriptionService } from './subscription.js'\nimport { createService as createAdminService } from './admin.js'\nimport { createService as createRateLimitService } from './rate-limit.js'\nimport { createService as createUcanService } from './ucan.js'\nimport { createService as createW3sService } from './service.js'\nimport { createService as createPlanService } from './plan.js'\nimport { createService as createUsageService } from './usage.js'\nimport { createService as createFilecoinService } from '@web3-storage/filecoin-api/storefront/service'\nimport * as AgentMessage from './utils/agent-message.js'\n\nexport * from './types.js'\nexport { AgentMessage }\n\n/**\n * @param {Omit<Types.UcantoServerContext, 'validateAuthorization'>} options\n * @returns {Agent<Types.Service>}\n */\nexport const createServer = ({ codec = Legacy.inbound, ...options }) => {\n  const context = {\n    ...options,\n    ...createRevocationChecker(options),\n  }\n\n  const server = Server.create({\n    ...context,\n    codec,\n    service: createService(context),\n    catch: (error) => context.errorReporter.catch(error),\n  })\n\n  /**\n   * @type {Types.ServerView<Types.Service>['request']}\n   */\n  const request = (request) => handle(agent, request)\n\n  const agent = /** @type {Agent<Types.Service>} */ ({\n    ...server,\n    id: server.id,\n    context: { ...server.context, ...context },\n    request,\n  })\n\n  return agent\n}\n\n/**\n * @typedef {object} WithContext\n * @property {Types.AgentContext} context\n */\n/**\n * @template {Record<string, any>} S\n * @typedef {Types.Server<S> & Types.ErrorReporter & WithContext & Types.Transport.Channel<S>} Agent\n */\n\n/**\n * @template {Record<string, any>} S\n * @template {Types.Tuple<Types.ServiceInvocation<Types.Capability, S>>} I\n * @param {Agent<S>} agent\n * @param {Types.HTTPRequest<Types.AgentMessage<{ In: Types.InferInvocations<I>, Out: Types.Tuple<Types.Receipt> }>>} request\n * @returns {Promise<Types.HTTPResponse<Types.AgentMessage<{ Out: Types.InferReceipts<I, S>, In: Types.Tuple<Types.Invocation> }>>>}\n */\nexport const handle = async (agent, request) => {\n  const selection = agent.codec.accept(request)\n  if (selection.error) {\n    const { status, headers = {}, message } = selection.error\n    return {\n      status,\n      headers,\n      body: new TextEncoder().encode(message),\n    }\n  } else {\n    const { encoder, decoder } = selection.ok\n    const input = await decoder.decode(request)\n\n    // Save invocation inside agent store so we can find it later. If we fail\n    // to save it we return 500 as we do not want to run the invocation that\n    // we are unable to service.\n    const save = await agent.context.agentStore.messages.write({\n      data: input,\n      source: request,\n      index: AgentMessage.index(input),\n    })\n\n    if (save.error) {\n      return {\n        status: 500,\n        headers: {},\n        body: new TextEncoder().encode(`Server error: ${save.error.message}`),\n      }\n    }\n\n    const output = await execute(agent, input)\n    const response = await encoder.encode(output)\n\n    const { error } = await agent.context.agentStore.messages.write({\n      data: output,\n      source: response,\n      index: AgentMessage.index(output),\n    })\n\n    // Failure to write a receipt is not something we can recover from. Throwing\n    // or returning HTTP 500 is also a not a great option because invocation may\n    // have change state and we would not want to rerun it. Which is why we\n    // report an error but return a message back.\n    if (error) {\n      agent.catch(error)\n    }\n\n    return response\n  }\n}\n\n/**\n * @template {Record<string, any>} S\n * @template {Types.Tuple} I\n * @param {Agent<S>} agent\n * @param {Types.AgentMessage<{ In: Types.InferInvocations<I>, Out: Types.Tuple<Types.Receipt> }>} input\n * @returns {Promise<Types.AgentMessage<{ Out: Types.InferReceipts<I, S>, In: Types.Tuple<Types.Invocation> }>>}\n */\nexport const execute = async (agent, input) => {\n  const promises = input.invocations.map(($) => run(agent, $))\n  const receipts = /** @type {Types.InferReceipts<I, S>} */ (\n    await Promise.all(promises)\n  )\n\n  return Message.build({ receipts })\n}\n\n/**\n * Runs given invocation unless agent already has a receipt, if agent does have\n * a receipt it will return receipt without running invocation.\n *\n * @template {Record<string, any>} S\n * @param {Agent<S>} agent\n * @param {Types.Invocation} invocation\n */\nexport const run = async (agent, invocation) => {\n  const cached = await agent.context.agentStore.receipts.get(invocation.link())\n  if (cached.ok) {\n    return cached.ok\n  }\n\n  // ucanto does not actually requires use of ServerView\n  /** @type {Types.ServerView<S>} */\n  const server = /** @type {any} */ (agent)\n\n  // ⚠️ There might be a race condition if we received two concurrent requests\n  // with a same invocation. Second one will not find a receipt if first one\n  // is still in flight. Looking up whether we have invocation is also not\n  // going to help because race might happen after that is first one may write\n  // record invocation status after we lookup proper solution is to use FIFO\n  // queue which we hope to introduce with a scheduler.\n  return await Server.invoke(invocation, server)\n}\n\n/**\n * @param {Types.ServiceContext} context\n * @returns {Types.Service}\n */\nexport const createService = (context) => ({\n  access: createAccessService(context),\n  console: createConsoleService(context),\n  consumer: createConsumerService(context),\n  customer: createCustomerService(context),\n  provider: createProviderService(context),\n  'rate-limit': createRateLimitService(context),\n  admin: createAdminService(context),\n  space: createSpaceService(context),\n  store: createStoreService(context),\n  subscription: createSubscriptionService(context),\n  upload: createUploadService(context),\n  ucan: createUcanService(context),\n  plan: createPlanService(context),\n  ['web3.storage']: createW3sService(context),\n  // storefront of filecoin pipeline\n  filecoin: createFilecoinService(context).filecoin,\n  usage: createUsageService(context),\n})\n\n/**\n * @param {object} options\n * @param {Types.Principal} options.id\n * @param {Types.Transport.Channel<Types.Service>} options.channel\n * @param {Types.OutboundCodec} [options.codec]\n */\nexport const connect = ({ id, channel, codec = CAR.outbound }) =>\n  Client.connect({\n    id,\n    channel,\n    codec,\n  })\n\nexport {\n  createService as createUploadService,\n  createServer as createUploadServer,\n  connect as createUploadClient,\n}\n"]}