@vtex/api 6.45.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1685 -0
- package/README.md +65 -0
- package/gen/manifest.schema +189 -0
- package/lib/HttpClient/GraphQLClient.d.ts +28 -0
- package/lib/HttpClient/GraphQLClient.js +30 -0
- package/lib/HttpClient/HttpClient.d.ts +29 -0
- package/lib/HttpClient/HttpClient.js +130 -0
- package/lib/HttpClient/agents.d.ts +3 -0
- package/lib/HttpClient/agents.js +24 -0
- package/lib/HttpClient/index.d.ts +6 -0
- package/lib/HttpClient/index.js +14 -0
- package/lib/HttpClient/middlewares/cache.d.ts +35 -0
- package/lib/HttpClient/middlewares/cache.js +173 -0
- package/lib/HttpClient/middlewares/cancellationToken.d.ts +3 -0
- package/lib/HttpClient/middlewares/cancellationToken.js +72 -0
- package/lib/HttpClient/middlewares/inflight.d.ts +5 -0
- package/lib/HttpClient/middlewares/inflight.js +50 -0
- package/lib/HttpClient/middlewares/memoization.d.ts +7 -0
- package/lib/HttpClient/middlewares/memoization.js +40 -0
- package/lib/HttpClient/middlewares/metrics.d.ts +9 -0
- package/lib/HttpClient/middlewares/metrics.js +110 -0
- package/lib/HttpClient/middlewares/notFound.d.ts +7 -0
- package/lib/HttpClient/middlewares/notFound.js +20 -0
- package/lib/HttpClient/middlewares/recorder.d.ts +3 -0
- package/lib/HttpClient/middlewares/recorder.js +21 -0
- package/lib/HttpClient/middlewares/request/HttpAgentSingleton.d.ts +14 -0
- package/lib/HttpClient/middlewares/request/HttpAgentSingleton.js +37 -0
- package/lib/HttpClient/middlewares/request/index.d.ts +18 -0
- package/lib/HttpClient/middlewares/request/index.js +83 -0
- package/lib/HttpClient/middlewares/request/setupAxios/index.d.ts +1 -0
- package/lib/HttpClient/middlewares/request/setupAxios/index.js +20 -0
- package/lib/HttpClient/middlewares/request/setupAxios/interceptors/exponentialBackoff.d.ts +2 -0
- package/lib/HttpClient/middlewares/request/setupAxios/interceptors/exponentialBackoff.js +48 -0
- package/lib/HttpClient/middlewares/request/setupAxios/interceptors/index.d.ts +2 -0
- package/lib/HttpClient/middlewares/request/setupAxios/interceptors/index.js +7 -0
- package/lib/HttpClient/middlewares/request/setupAxios/interceptors/tracing/index.d.ts +8 -0
- package/lib/HttpClient/middlewares/request/setupAxios/interceptors/tracing/index.js +51 -0
- package/lib/HttpClient/middlewares/request/setupAxios/interceptors/tracing/spanSetup.d.ts +4 -0
- package/lib/HttpClient/middlewares/request/setupAxios/interceptors/tracing/spanSetup.js +29 -0
- package/lib/HttpClient/middlewares/tracing.d.ts +15 -0
- package/lib/HttpClient/middlewares/tracing.js +54 -0
- package/lib/HttpClient/typings.d.ts +123 -0
- package/lib/HttpClient/typings.js +11 -0
- package/lib/caches/CacheLayer.d.ts +7 -0
- package/lib/caches/CacheLayer.js +2 -0
- package/lib/caches/DiskCache.d.ts +17 -0
- package/lib/caches/DiskCache.js +70 -0
- package/lib/caches/LRUCache.d.ts +16 -0
- package/lib/caches/LRUCache.js +48 -0
- package/lib/caches/LRUDiskCache.d.ts +21 -0
- package/lib/caches/LRUDiskCache.js +132 -0
- package/lib/caches/MultilayeredCache.d.ts +14 -0
- package/lib/caches/MultilayeredCache.js +68 -0
- package/lib/caches/index.d.ts +6 -0
- package/lib/caches/index.js +9 -0
- package/lib/caches/typings.d.ts +48 -0
- package/lib/caches/typings.js +2 -0
- package/lib/clients/IOClient.d.ts +13 -0
- package/lib/clients/IOClient.js +19 -0
- package/lib/clients/IOClients.d.ts +37 -0
- package/lib/clients/IOClients.js +89 -0
- package/lib/clients/IOGraphQLClient.d.ts +13 -0
- package/lib/clients/IOGraphQLClient.js +16 -0
- package/lib/clients/apps/AppClient.d.ts +9 -0
- package/lib/clients/apps/AppClient.js +34 -0
- package/lib/clients/apps/AppGraphQLClient.d.ts +9 -0
- package/lib/clients/apps/AppGraphQLClient.js +34 -0
- package/lib/clients/apps/Billing.d.ts +12 -0
- package/lib/clients/apps/Billing.js +21 -0
- package/lib/clients/apps/Builder.d.ts +45 -0
- package/lib/clients/apps/Builder.js +114 -0
- package/lib/clients/apps/MessagesGraphQL.d.ts +61 -0
- package/lib/clients/apps/MessagesGraphQL.js +121 -0
- package/lib/clients/apps/Settings.d.ts +14 -0
- package/lib/clients/apps/Settings.js +38 -0
- package/lib/clients/apps/catalogGraphQL/brand.d.ts +13 -0
- package/lib/clients/apps/catalogGraphQL/brand.js +18 -0
- package/lib/clients/apps/catalogGraphQL/category.d.ts +11 -0
- package/lib/clients/apps/catalogGraphQL/category.js +16 -0
- package/lib/clients/apps/catalogGraphQL/index.d.ts +22 -0
- package/lib/clients/apps/catalogGraphQL/index.js +88 -0
- package/lib/clients/apps/catalogGraphQL/product.d.ts +25 -0
- package/lib/clients/apps/catalogGraphQL/product.js +30 -0
- package/lib/clients/apps/catalogGraphQL/sku.d.ts +26 -0
- package/lib/clients/apps/catalogGraphQL/sku.js +30 -0
- package/lib/clients/apps/index.d.ts +7 -0
- package/lib/clients/apps/index.js +12 -0
- package/lib/clients/external/ExternalClient.d.ts +9 -0
- package/lib/clients/external/ExternalClient.js +21 -0
- package/lib/clients/external/ID.d.ts +23 -0
- package/lib/clients/external/ID.js +60 -0
- package/lib/clients/external/Masterdata.d.ts +93 -0
- package/lib/clients/external/Masterdata.js +223 -0
- package/lib/clients/external/PaymentProvider.d.ts +31 -0
- package/lib/clients/external/PaymentProvider.js +42 -0
- package/lib/clients/external/index.d.ts +4 -0
- package/lib/clients/external/index.js +9 -0
- package/lib/clients/index.d.ts +9 -0
- package/lib/clients/index.js +17 -0
- package/lib/clients/infra/Apps.d.ts +88 -0
- package/lib/clients/infra/Apps.js +508 -0
- package/lib/clients/infra/Assets.d.ts +34 -0
- package/lib/clients/infra/Assets.js +165 -0
- package/lib/clients/infra/BillingMetrics.d.ts +13 -0
- package/lib/clients/infra/BillingMetrics.js +15 -0
- package/lib/clients/infra/Events.d.ts +12 -0
- package/lib/clients/infra/Events.js +33 -0
- package/lib/clients/infra/Housekeeper.d.ts +10 -0
- package/lib/clients/infra/Housekeeper.js +30 -0
- package/lib/clients/infra/InfraClient.d.ts +9 -0
- package/lib/clients/infra/InfraClient.js +34 -0
- package/lib/clients/infra/Metadata.d.ts +26 -0
- package/lib/clients/infra/Metadata.js +83 -0
- package/lib/clients/infra/Registry.d.ts +59 -0
- package/lib/clients/infra/Registry.js +207 -0
- package/lib/clients/infra/Router.d.ts +34 -0
- package/lib/clients/infra/Router.js +81 -0
- package/lib/clients/infra/Sphinx.d.ts +13 -0
- package/lib/clients/infra/Sphinx.js +29 -0
- package/lib/clients/infra/VBase.d.ts +60 -0
- package/lib/clients/infra/VBase.js +171 -0
- package/lib/clients/infra/Workspaces.d.ts +18 -0
- package/lib/clients/infra/Workspaces.js +65 -0
- package/lib/clients/infra/index.d.ts +12 -0
- package/lib/clients/infra/index.js +17 -0
- package/lib/clients/janus/JanusClient.d.ts +21 -0
- package/lib/clients/janus/JanusClient.js +30 -0
- package/lib/clients/janus/LicenseManager.d.ts +7 -0
- package/lib/clients/janus/LicenseManager.js +58 -0
- package/lib/clients/janus/Segment.d.ts +36 -0
- package/lib/clients/janus/Segment.js +79 -0
- package/lib/clients/janus/Session.d.ts +15 -0
- package/lib/clients/janus/Session.js +75 -0
- package/lib/clients/janus/Tenant.d.ts +29 -0
- package/lib/clients/janus/Tenant.js +28 -0
- package/lib/clients/janus/index.d.ts +5 -0
- package/lib/clients/janus/index.js +10 -0
- package/lib/constants.d.ts +59 -0
- package/lib/constants.js +65 -0
- package/lib/errors/AuthenticationError.d.ts +18 -0
- package/lib/errors/AuthenticationError.js +24 -0
- package/lib/errors/ForbiddenError.d.ts +18 -0
- package/lib/errors/ForbiddenError.js +24 -0
- package/lib/errors/NotFoundError.d.ts +18 -0
- package/lib/errors/NotFoundError.js +24 -0
- package/lib/errors/RequestCancelledError.d.ts +6 -0
- package/lib/errors/RequestCancelledError.js +11 -0
- package/lib/errors/ResolverError.d.ts +29 -0
- package/lib/errors/ResolverError.js +35 -0
- package/lib/errors/ResolverWarning.d.ts +21 -0
- package/lib/errors/ResolverWarning.js +28 -0
- package/lib/errors/TooManyRequestsError.d.ts +4 -0
- package/lib/errors/TooManyRequestsError.js +9 -0
- package/lib/errors/UserInputError.d.ts +18 -0
- package/lib/errors/UserInputError.js +24 -0
- package/lib/errors/customGraphQLError.d.ts +4 -0
- package/lib/errors/customGraphQLError.js +9 -0
- package/lib/errors/index.d.ts +8 -0
- package/lib/errors/index.js +13 -0
- package/lib/index.d.ts +17 -0
- package/lib/index.js +21 -0
- package/lib/metrics/MetricsAccumulator.d.ts +35 -0
- package/lib/metrics/MetricsAccumulator.js +125 -0
- package/lib/responses.d.ts +79 -0
- package/lib/responses.js +2 -0
- package/lib/service/index.d.ts +2 -0
- package/lib/service/index.js +30 -0
- package/lib/service/loaders.d.ts +8 -0
- package/lib/service/loaders.js +54 -0
- package/lib/service/logger/console.d.ts +10 -0
- package/lib/service/logger/console.js +36 -0
- package/lib/service/logger/index.d.ts +2 -0
- package/lib/service/logger/index.js +7 -0
- package/lib/service/logger/logger.d.ts +35 -0
- package/lib/service/logger/logger.js +73 -0
- package/lib/service/logger/metricsLogger.d.ts +40 -0
- package/lib/service/logger/metricsLogger.js +149 -0
- package/lib/service/master.d.ts +2 -0
- package/lib/service/master.js +91 -0
- package/lib/service/tracing/TracerSingleton.d.ts +7 -0
- package/lib/service/tracing/TracerSingleton.js +54 -0
- package/lib/service/tracing/metrics/MetricNames.d.ts +60 -0
- package/lib/service/tracing/metrics/MetricNames.js +44 -0
- package/lib/service/tracing/metrics/instruments.d.ts +9 -0
- package/lib/service/tracing/metrics/instruments.js +11 -0
- package/lib/service/tracing/metrics/measurers/EventLoopLagMeasurer.d.ts +9 -0
- package/lib/service/tracing/metrics/measurers/EventLoopLagMeasurer.js +25 -0
- package/lib/service/tracing/tracingMiddlewares.d.ts +5 -0
- package/lib/service/tracing/tracingMiddlewares.js +119 -0
- package/lib/service/worker/index.d.ts +11 -0
- package/lib/service/worker/index.js +193 -0
- package/lib/service/worker/listeners.d.ts +3 -0
- package/lib/service/worker/listeners.js +43 -0
- package/lib/service/worker/runtime/Service.d.ts +17 -0
- package/lib/service/worker/runtime/Service.js +23 -0
- package/lib/service/worker/runtime/builtIn/handlers.d.ts +4 -0
- package/lib/service/worker/runtime/builtIn/handlers.js +27 -0
- package/lib/service/worker/runtime/builtIn/middlewares.d.ts +4 -0
- package/lib/service/worker/runtime/builtIn/middlewares.js +40 -0
- package/lib/service/worker/runtime/events/index.d.ts +4 -0
- package/lib/service/worker/runtime/events/index.js +46 -0
- package/lib/service/worker/runtime/events/middlewares/body.d.ts +3 -0
- package/lib/service/worker/runtime/events/middlewares/body.js +22 -0
- package/lib/service/worker/runtime/events/middlewares/context.d.ts +3 -0
- package/lib/service/worker/runtime/events/middlewares/context.js +22 -0
- package/lib/service/worker/runtime/events/router.d.ts +2 -0
- package/lib/service/worker/runtime/events/router.js +27 -0
- package/lib/service/worker/runtime/graphql/index.d.ts +6 -0
- package/lib/service/worker/runtime/graphql/index.js +27 -0
- package/lib/service/worker/runtime/graphql/middlewares/context.d.ts +2 -0
- package/lib/service/worker/runtime/graphql/middlewares/context.js +16 -0
- package/lib/service/worker/runtime/graphql/middlewares/error.d.ts +2 -0
- package/lib/service/worker/runtime/graphql/middlewares/error.js +93 -0
- package/lib/service/worker/runtime/graphql/middlewares/query.d.ts +2 -0
- package/lib/service/worker/runtime/graphql/middlewares/query.js +52 -0
- package/lib/service/worker/runtime/graphql/middlewares/response.d.ts +2 -0
- package/lib/service/worker/runtime/graphql/middlewares/response.js +38 -0
- package/lib/service/worker/runtime/graphql/middlewares/run.d.ts +2 -0
- package/lib/service/worker/runtime/graphql/middlewares/run.js +19 -0
- package/lib/service/worker/runtime/graphql/middlewares/updateSchema.d.ts +4 -0
- package/lib/service/worker/runtime/graphql/middlewares/updateSchema.js +28 -0
- package/lib/service/worker/runtime/graphql/middlewares/upload.d.ts +4 -0
- package/lib/service/worker/runtime/graphql/middlewares/upload.js +12 -0
- package/lib/service/worker/runtime/graphql/schema/index.d.ts +7 -0
- package/lib/service/worker/runtime/graphql/schema/index.js +31 -0
- package/lib/service/worker/runtime/graphql/schema/messagesLoaderV2.d.ts +6 -0
- package/lib/service/worker/runtime/graphql/schema/messagesLoaderV2.js +79 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/Auth.d.ts +6 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/Auth.js +73 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/CacheControl.d.ts +7 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/CacheControl.js +37 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/Deprecated.d.ts +11 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/Deprecated.js +61 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/Metric.d.ts +7 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/Metric.js +40 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/Sanitize.d.ts +9 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/Sanitize.js +35 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/Settings.d.ts +6 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/Settings.js +34 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/SmartCacheDirective.d.ts +6 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/SmartCacheDirective.js +29 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/TranslatableV2.d.ts +7 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/TranslatableV2.js +39 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/TranslateTo.d.ts +7 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/TranslateTo.js +33 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/index.d.ts +22 -0
- package/lib/service/worker/runtime/graphql/schema/schemaDirectives/index.js +36 -0
- package/lib/service/worker/runtime/graphql/schema/typeDefs/index.d.ts +7 -0
- package/lib/service/worker/runtime/graphql/schema/typeDefs/index.js +12 -0
- package/lib/service/worker/runtime/graphql/schema/typeDefs/ioUpload.d.ts +3 -0
- package/lib/service/worker/runtime/graphql/schema/typeDefs/ioUpload.js +16 -0
- package/lib/service/worker/runtime/graphql/schema/typeDefs/sanitizedString.d.ts +10 -0
- package/lib/service/worker/runtime/graphql/schema/typeDefs/sanitizedString.js +39 -0
- package/lib/service/worker/runtime/graphql/typings.d.ts +31 -0
- package/lib/service/worker/runtime/graphql/typings.js +2 -0
- package/lib/service/worker/runtime/graphql/utils/cacheControl.d.ts +1 -0
- package/lib/service/worker/runtime/graphql/utils/cacheControl.js +32 -0
- package/lib/service/worker/runtime/graphql/utils/error.d.ts +1 -0
- package/lib/service/worker/runtime/graphql/utils/error.js +76 -0
- package/lib/service/worker/runtime/graphql/utils/pathname.d.ts +1 -0
- package/lib/service/worker/runtime/graphql/utils/pathname.js +7 -0
- package/lib/service/worker/runtime/graphql/utils/translations.d.ts +14 -0
- package/lib/service/worker/runtime/graphql/utils/translations.js +38 -0
- package/lib/service/worker/runtime/http/index.d.ts +5 -0
- package/lib/service/worker/runtime/http/index.js +58 -0
- package/lib/service/worker/runtime/http/middlewares/authTokens.d.ts +3 -0
- package/lib/service/worker/runtime/http/middlewares/authTokens.js +12 -0
- package/lib/service/worker/runtime/http/middlewares/cancellationToken.d.ts +3 -0
- package/lib/service/worker/runtime/http/middlewares/cancellationToken.js +18 -0
- package/lib/service/worker/runtime/http/middlewares/clients.d.ts +4 -0
- package/lib/service/worker/runtime/http/middlewares/clients.js +12 -0
- package/lib/service/worker/runtime/http/middlewares/context.d.ts +4 -0
- package/lib/service/worker/runtime/http/middlewares/context.js +36 -0
- package/lib/service/worker/runtime/http/middlewares/error.d.ts +2 -0
- package/lib/service/worker/runtime/http/middlewares/error.js +84 -0
- package/lib/service/worker/runtime/http/middlewares/rateLimit.d.ts +6 -0
- package/lib/service/worker/runtime/http/middlewares/rateLimit.js +42 -0
- package/lib/service/worker/runtime/http/middlewares/requestStats.d.ts +17 -0
- package/lib/service/worker/runtime/http/middlewares/requestStats.js +40 -0
- package/lib/service/worker/runtime/http/middlewares/setCookie.d.ts +3 -0
- package/lib/service/worker/runtime/http/middlewares/setCookie.js +52 -0
- package/lib/service/worker/runtime/http/middlewares/settings.d.ts +9 -0
- package/lib/service/worker/runtime/http/middlewares/settings.js +43 -0
- package/lib/service/worker/runtime/http/middlewares/timings.d.ts +3 -0
- package/lib/service/worker/runtime/http/middlewares/timings.js +45 -0
- package/lib/service/worker/runtime/http/middlewares/vary.d.ts +3 -0
- package/lib/service/worker/runtime/http/middlewares/vary.js +52 -0
- package/lib/service/worker/runtime/http/router.d.ts +2 -0
- package/lib/service/worker/runtime/http/router.js +23 -0
- package/lib/service/worker/runtime/http/routes.d.ts +19 -0
- package/lib/service/worker/runtime/http/routes.js +12 -0
- package/lib/service/worker/runtime/method.d.ts +9 -0
- package/lib/service/worker/runtime/method.js +23 -0
- package/lib/service/worker/runtime/statusTrack.d.ts +14 -0
- package/lib/service/worker/runtime/statusTrack.js +37 -0
- package/lib/service/worker/runtime/typings.d.ts +172 -0
- package/lib/service/worker/runtime/typings.js +2 -0
- package/lib/service/worker/runtime/utils/compose.d.ts +5 -0
- package/lib/service/worker/runtime/utils/compose.js +11 -0
- package/lib/service/worker/runtime/utils/context.d.ts +2 -0
- package/lib/service/worker/runtime/utils/context.js +40 -0
- package/lib/service/worker/runtime/utils/diff.d.ts +12 -0
- package/lib/service/worker/runtime/utils/diff.js +21 -0
- package/lib/service/worker/runtime/utils/recorder.d.ts +8 -0
- package/lib/service/worker/runtime/utils/recorder.js +37 -0
- package/lib/service/worker/runtime/utils/toArray.d.ts +1 -0
- package/lib/service/worker/runtime/utils/toArray.js +3 -0
- package/lib/service/worker/runtime/utils/tokenBucket.d.ts +2 -0
- package/lib/service/worker/runtime/utils/tokenBucket.js +16 -0
- package/lib/tracing/LogEvents.d.ts +31 -0
- package/lib/tracing/LogEvents.js +3 -0
- package/lib/tracing/LogFields.d.ts +61 -0
- package/lib/tracing/LogFields.js +3 -0
- package/lib/tracing/Tags.d.ts +79 -0
- package/lib/tracing/Tags.js +7 -0
- package/lib/tracing/UserLandTracer.d.ts +27 -0
- package/lib/tracing/UserLandTracer.js +55 -0
- package/lib/tracing/errorReporting/ErrorReport.d.ts +31 -0
- package/lib/tracing/errorReporting/ErrorReport.js +61 -0
- package/lib/tracing/index.d.ts +9 -0
- package/lib/tracing/index.js +19 -0
- package/lib/tracing/spanReference/SpanReferenceTypes.d.ts +4 -0
- package/lib/tracing/spanReference/SpanReferenceTypes.js +7 -0
- package/lib/tracing/spanReference/createSpanReference.d.ts +3 -0
- package/lib/tracing/spanReference/createSpanReference.js +8 -0
- package/lib/tracing/utils.d.ts +13 -0
- package/lib/tracing/utils.js +40 -0
- package/lib/utils/MineWinsConflictsResolver.d.ts +28 -0
- package/lib/utils/MineWinsConflictsResolver.js +130 -0
- package/lib/utils/app.d.ts +23 -0
- package/lib/utils/app.js +48 -0
- package/lib/utils/appsStaleIfError.d.ts +11 -0
- package/lib/utils/appsStaleIfError.js +63 -0
- package/lib/utils/billingOptions.d.ts +36 -0
- package/lib/utils/billingOptions.js +9 -0
- package/lib/utils/binding.d.ts +13 -0
- package/lib/utils/binding.js +38 -0
- package/lib/utils/cancel.d.ts +3 -0
- package/lib/utils/cancel.js +12 -0
- package/lib/utils/domain.d.ts +1 -0
- package/lib/utils/domain.js +5 -0
- package/lib/utils/error.d.ts +8 -0
- package/lib/utils/error.js +94 -0
- package/lib/utils/index.d.ts +9 -0
- package/lib/utils/index.js +14 -0
- package/lib/utils/message.d.ts +25 -0
- package/lib/utils/message.js +23 -0
- package/lib/utils/renameBy.d.ts +1 -0
- package/lib/utils/renameBy.js +4 -0
- package/lib/utils/retry.d.ts +4 -0
- package/lib/utils/retry.js +32 -0
- package/lib/utils/status.d.ts +1 -0
- package/lib/utils/status.js +11 -0
- package/lib/utils/tenant.d.ts +5 -0
- package/lib/utils/tenant.js +6 -0
- package/lib/utils/throwOnGraphQLErrors.d.ts +2 -0
- package/lib/utils/throwOnGraphQLErrors.js +15 -0
- package/lib/utils/time.d.ts +31 -0
- package/lib/utils/time.js +118 -0
- package/package.json +127 -0
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
|
+
import { Span } from 'opentracing';
|
|
3
|
+
export declare const injectRequestInfoOnSpan: (span: Span, http: AxiosInstance, config: AxiosRequestConfig) => void;
|
|
4
|
+
export declare const injectResponseInfoOnSpan: (span: Span, response: AxiosResponse<any> | undefined) => void;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const buildFullPath_1 = __importDefault(require("axios/lib/core/buildFullPath"));
|
|
7
|
+
const constants_1 = require("../../../../../../constants");
|
|
8
|
+
const Tags_1 = require("../../../../../../tracing/Tags");
|
|
9
|
+
const utils_1 = require("../../../../../../tracing/utils");
|
|
10
|
+
exports.injectRequestInfoOnSpan = (span, http, config) => {
|
|
11
|
+
span.addTags({
|
|
12
|
+
[Tags_1.OpentracingTags.SPAN_KIND]: Tags_1.OpentracingTags.SPAN_KIND_RPC_CLIENT,
|
|
13
|
+
[Tags_1.OpentracingTags.HTTP_METHOD]: config.method,
|
|
14
|
+
[Tags_1.OpentracingTags.HTTP_URL]: buildFullPath_1.default(config.baseURL, http.getUri(config)),
|
|
15
|
+
});
|
|
16
|
+
span.log({ 'request-headers': utils_1.cloneAndSanitizeHeaders(config.headers) });
|
|
17
|
+
};
|
|
18
|
+
// Response may be undefined in case of client timeout, invalid URL, ...
|
|
19
|
+
exports.injectResponseInfoOnSpan = (span, response) => {
|
|
20
|
+
if (!response) {
|
|
21
|
+
span.setTag("http.no_response" /* HTTP_NO_RESPONSE */, 'true');
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
span.log({ 'response-headers': utils_1.cloneAndSanitizeHeaders(response.headers) });
|
|
25
|
+
span.setTag(Tags_1.OpentracingTags.HTTP_STATUS_CODE, response.status);
|
|
26
|
+
if (response.headers[constants_1.ROUTER_CACHE_HEADER]) {
|
|
27
|
+
span.setTag("http.cache.router" /* HTTP_ROUTER_CACHE_RESULT */, response.headers[constants_1.ROUTER_CACHE_HEADER]);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { MiddlewaresTracingContext, RequestConfig } from '..';
|
|
2
|
+
import { IOContext } from '../../service/worker/runtime/typings';
|
|
3
|
+
import { MiddlewareContext } from '../typings';
|
|
4
|
+
interface HttpClientTracingMiddlewareConfig {
|
|
5
|
+
clientName: string;
|
|
6
|
+
tracer: IOContext['tracer'];
|
|
7
|
+
logger: IOContext['logger'];
|
|
8
|
+
hasMemoryCacheMiddleware: boolean;
|
|
9
|
+
hasDiskCacheMiddleware: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface TraceableRequestConfig extends RequestConfig {
|
|
12
|
+
tracing: MiddlewaresTracingContext;
|
|
13
|
+
}
|
|
14
|
+
export declare const createHttpClientTracingMiddleware: ({ tracer, logger, clientName, hasMemoryCacheMiddleware, hasDiskCacheMiddleware, }: HttpClientTracingMiddlewareConfig) => (ctx: MiddlewareContext, next: () => Promise<void>) => Promise<void>;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tracing_1 = require("../../tracing");
|
|
4
|
+
const Tags_1 = require("../../tracing/Tags");
|
|
5
|
+
const cache_1 = require("./cache");
|
|
6
|
+
exports.createHttpClientTracingMiddleware = ({ tracer, logger, clientName, hasMemoryCacheMiddleware, hasDiskCacheMiddleware, }) => {
|
|
7
|
+
return async function tracingMiddleware(ctx, next) {
|
|
8
|
+
const { rootSpan, requestSpanNameSuffix, referenceType = tracing_1.SpanReferenceTypes.CHILD_OF } = ctx.config.tracing || {};
|
|
9
|
+
const spanName = requestSpanNameSuffix ? `request:${requestSpanNameSuffix}` : 'request';
|
|
10
|
+
const span = rootSpan
|
|
11
|
+
? tracer.startSpan(spanName, {
|
|
12
|
+
references: [tracing_1.createSpanReference(rootSpan, referenceType)],
|
|
13
|
+
})
|
|
14
|
+
: tracer.startSpan(spanName);
|
|
15
|
+
ctx.tracing = {
|
|
16
|
+
...ctx.config.tracing,
|
|
17
|
+
isSampled: tracing_1.getTraceInfo(span).isSampled,
|
|
18
|
+
logger,
|
|
19
|
+
rootSpan: span,
|
|
20
|
+
tracer,
|
|
21
|
+
};
|
|
22
|
+
ctx.config.tracing = ctx.tracing;
|
|
23
|
+
const hasMemoCache = !(!cache_1.isLocallyCacheable(ctx.config, cache_1.CacheType.Any) || !ctx.config.memoizable);
|
|
24
|
+
const hasMemoryCache = hasMemoryCacheMiddleware && !!cache_1.isLocallyCacheable(ctx.config, cache_1.CacheType.Memory);
|
|
25
|
+
const hasDiskCache = hasDiskCacheMiddleware && !!cache_1.isLocallyCacheable(ctx.config, cache_1.CacheType.Disk);
|
|
26
|
+
span.addTags({
|
|
27
|
+
["http.cache.memoization.enabled" /* HTTP_MEMOIZATION_CACHE_ENABLED */]: hasMemoCache,
|
|
28
|
+
["http.cache.memory.enabled" /* HTTP_MEMORY_CACHE_ENABLED */]: hasMemoryCache,
|
|
29
|
+
["http.cache.disk.enabled" /* HTTP_DISK_CACHE_ENABLED */]: hasDiskCache,
|
|
30
|
+
["http.client.name" /* HTTP_CLIENT_NAME */]: clientName,
|
|
31
|
+
});
|
|
32
|
+
let response;
|
|
33
|
+
try {
|
|
34
|
+
await next();
|
|
35
|
+
response = ctx.response;
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
response = err.response;
|
|
39
|
+
if (ctx.tracing.isSampled) {
|
|
40
|
+
tracing_1.ErrorReport.create({ originalError: err }).injectOnSpan(span, logger);
|
|
41
|
+
}
|
|
42
|
+
throw err;
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
if (response) {
|
|
46
|
+
span.setTag(Tags_1.OpentracingTags.HTTP_STATUS_CODE, response.status);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
span.setTag("http.no_response" /* HTTP_NO_RESPONSE */, true);
|
|
50
|
+
}
|
|
51
|
+
span.finish();
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
};
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
3
|
+
import { Middleware } from 'koa-compose';
|
|
4
|
+
import { Span } from 'opentracing';
|
|
5
|
+
import { CacheLayer } from '../caches/CacheLayer';
|
|
6
|
+
import { MetricsAccumulator } from '../metrics/MetricsAccumulator';
|
|
7
|
+
import { IOContext } from '../service/worker/runtime/typings';
|
|
8
|
+
import { SpanReferenceTypes } from '../tracing';
|
|
9
|
+
import { IUserLandTracer } from '../tracing/UserLandTracer';
|
|
10
|
+
import { Cached, CacheType } from './middlewares/cache';
|
|
11
|
+
export declare type InflightKeyGenerator = (x: RequestConfig) => string;
|
|
12
|
+
interface RequestTracingUserConfig {
|
|
13
|
+
rootSpan?: Span;
|
|
14
|
+
referenceType?: SpanReferenceTypes;
|
|
15
|
+
requestSpanNameSuffix?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface RequestTracingConfig {
|
|
18
|
+
tracing?: RequestTracingUserConfig;
|
|
19
|
+
}
|
|
20
|
+
export interface RequestConfig extends AxiosRequestConfig, RequestTracingConfig {
|
|
21
|
+
retries?: number;
|
|
22
|
+
exponentialTimeoutCoefficient?: number;
|
|
23
|
+
initialBackoffDelay?: number;
|
|
24
|
+
exponentialBackoffCoefficient?: number;
|
|
25
|
+
retryCount?: number;
|
|
26
|
+
/**
|
|
27
|
+
* Identifies the type of request for metrification purposes. Should vary with client method.
|
|
28
|
+
*/
|
|
29
|
+
metric?: string;
|
|
30
|
+
/**
|
|
31
|
+
* In verbose mode, a counter will be incremented for each request made with the same `metric` label.
|
|
32
|
+
*/
|
|
33
|
+
count?: number;
|
|
34
|
+
/**
|
|
35
|
+
* In verbose mode, a label will be created with a counter for each request made with the same `metric` label.
|
|
36
|
+
*/
|
|
37
|
+
label?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Outputs verbose logs to console for this request.
|
|
40
|
+
*/
|
|
41
|
+
verbose?: boolean;
|
|
42
|
+
production?: boolean;
|
|
43
|
+
cacheable?: CacheType;
|
|
44
|
+
memoizeable?: boolean;
|
|
45
|
+
inflightKey?: InflightKeyGenerator;
|
|
46
|
+
forceMaxAge?: number;
|
|
47
|
+
responseEncoding?: BufferEncoding;
|
|
48
|
+
nullIfNotFound?: boolean;
|
|
49
|
+
ignoreRecorder?: boolean;
|
|
50
|
+
}
|
|
51
|
+
export interface CacheHit {
|
|
52
|
+
disk?: 0 | 1;
|
|
53
|
+
memory?: 0 | 1;
|
|
54
|
+
revalidated?: 0 | 1;
|
|
55
|
+
router?: 0 | 1;
|
|
56
|
+
}
|
|
57
|
+
export interface MiddlewaresTracingContext extends Omit<RequestTracingUserConfig, 'rootSpan'> {
|
|
58
|
+
tracer: IUserLandTracer;
|
|
59
|
+
logger: IOContext['logger'];
|
|
60
|
+
rootSpan: Span;
|
|
61
|
+
isSampled: boolean;
|
|
62
|
+
}
|
|
63
|
+
export interface MiddlewareContext {
|
|
64
|
+
config: RequestConfig;
|
|
65
|
+
tracing?: MiddlewaresTracingContext;
|
|
66
|
+
response?: AxiosResponse;
|
|
67
|
+
cacheHit?: CacheHit;
|
|
68
|
+
inflightHit?: boolean;
|
|
69
|
+
memoizedHit?: boolean;
|
|
70
|
+
}
|
|
71
|
+
export declare type CacheStorage = CacheLayer<string, Cached>;
|
|
72
|
+
export interface InstanceOptions {
|
|
73
|
+
authType?: AuthType;
|
|
74
|
+
timeout?: number;
|
|
75
|
+
memoryCache?: CacheLayer<string, Cached>;
|
|
76
|
+
diskCache?: CacheLayer<string, Cached>;
|
|
77
|
+
baseURL?: string;
|
|
78
|
+
retries?: number;
|
|
79
|
+
exponentialTimeoutCoefficient?: number;
|
|
80
|
+
initialBackoffDelay?: number;
|
|
81
|
+
exponentialBackoffCoefficient?: number;
|
|
82
|
+
metrics?: MetricsAccumulator;
|
|
83
|
+
/**
|
|
84
|
+
* Maximum number of concurrent requests
|
|
85
|
+
*
|
|
86
|
+
* @type {number}
|
|
87
|
+
* @memberof InstanceOptions
|
|
88
|
+
*/
|
|
89
|
+
concurrency?: number;
|
|
90
|
+
/**
|
|
91
|
+
* Default headers to be sent with every request
|
|
92
|
+
*
|
|
93
|
+
* @type {Record<string, string>}
|
|
94
|
+
* @memberof InstanceOptions
|
|
95
|
+
*/
|
|
96
|
+
headers?: Record<string, string>;
|
|
97
|
+
/**
|
|
98
|
+
* Default query string parameters to be sent with every request
|
|
99
|
+
*
|
|
100
|
+
* @type {Record<string, string>}
|
|
101
|
+
* @memberof InstanceOptions
|
|
102
|
+
*/
|
|
103
|
+
params?: Record<string, string>;
|
|
104
|
+
middlewares?: Array<Middleware<MiddlewareContext>>;
|
|
105
|
+
verbose?: boolean;
|
|
106
|
+
name?: string;
|
|
107
|
+
serverTimings?: Record<string, string>;
|
|
108
|
+
httpsAgent?: AxiosRequestConfig['httpsAgent'];
|
|
109
|
+
}
|
|
110
|
+
export interface IOResponse<T> {
|
|
111
|
+
data: T;
|
|
112
|
+
headers: Record<string, string>;
|
|
113
|
+
status: number;
|
|
114
|
+
}
|
|
115
|
+
export declare enum AuthType {
|
|
116
|
+
basic = "Basic",
|
|
117
|
+
bearer = "Bearer",
|
|
118
|
+
/**
|
|
119
|
+
* Supported for legacy reasons - this is not spec compliant!
|
|
120
|
+
*/
|
|
121
|
+
token = "token"
|
|
122
|
+
}
|
|
123
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var AuthType;
|
|
4
|
+
(function (AuthType) {
|
|
5
|
+
AuthType["basic"] = "Basic";
|
|
6
|
+
AuthType["bearer"] = "Bearer";
|
|
7
|
+
/**
|
|
8
|
+
* Supported for legacy reasons - this is not spec compliant!
|
|
9
|
+
*/
|
|
10
|
+
AuthType["token"] = "token";
|
|
11
|
+
})(AuthType = exports.AuthType || (exports.AuthType = {}));
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { FetchResult } from './typings';
|
|
2
|
+
export interface CacheLayer<K, V> {
|
|
3
|
+
get(key: K, fetcher?: () => Promise<FetchResult<V>>): Promise<V | void> | V | void;
|
|
4
|
+
has(key: K): Promise<boolean> | boolean;
|
|
5
|
+
set(key: K, value: V, maxAge?: number | void): Promise<boolean> | boolean;
|
|
6
|
+
getStats?(name?: string): any;
|
|
7
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { CacheLayer } from './CacheLayer';
|
|
2
|
+
import { DiskStats } from './typings';
|
|
3
|
+
import { outputJSON, readJSON } from 'fs-extra';
|
|
4
|
+
export declare class DiskCache<V> implements CacheLayer<string, V> {
|
|
5
|
+
private cachePath;
|
|
6
|
+
private readFile;
|
|
7
|
+
private writeFile;
|
|
8
|
+
private hits;
|
|
9
|
+
private total;
|
|
10
|
+
private lock;
|
|
11
|
+
constructor(cachePath: string, readFile?: typeof readJSON, writeFile?: typeof outputJSON);
|
|
12
|
+
has: (key: string) => boolean;
|
|
13
|
+
getStats: (name?: string) => DiskStats;
|
|
14
|
+
get: (key: string) => Promise<void | V>;
|
|
15
|
+
set: (key: string, value: V) => Promise<boolean>;
|
|
16
|
+
private getPathKey;
|
|
17
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const fs_extra_1 = require("fs-extra");
|
|
7
|
+
const path_1 = require("path");
|
|
8
|
+
const rwlock_1 = __importDefault(require("rwlock"));
|
|
9
|
+
class DiskCache {
|
|
10
|
+
constructor(cachePath, readFile = fs_extra_1.readJSON, writeFile = fs_extra_1.outputJSON) {
|
|
11
|
+
this.cachePath = cachePath;
|
|
12
|
+
this.readFile = readFile;
|
|
13
|
+
this.writeFile = writeFile;
|
|
14
|
+
this.hits = 0;
|
|
15
|
+
this.total = 0;
|
|
16
|
+
this.has = (key) => {
|
|
17
|
+
const pathKey = this.getPathKey(key);
|
|
18
|
+
return fs_extra_1.pathExistsSync(pathKey);
|
|
19
|
+
};
|
|
20
|
+
this.getStats = (name = 'disk-cache') => {
|
|
21
|
+
const stats = {
|
|
22
|
+
hits: this.hits,
|
|
23
|
+
name,
|
|
24
|
+
total: this.total,
|
|
25
|
+
};
|
|
26
|
+
this.hits = 0;
|
|
27
|
+
this.total = 0;
|
|
28
|
+
return stats;
|
|
29
|
+
};
|
|
30
|
+
this.get = async (key) => {
|
|
31
|
+
const pathKey = this.getPathKey(key);
|
|
32
|
+
this.total += 1;
|
|
33
|
+
const data = await new Promise(resolve => {
|
|
34
|
+
this.lock.readLock(key, async (release) => {
|
|
35
|
+
try {
|
|
36
|
+
const fileData = await this.readFile(pathKey);
|
|
37
|
+
release();
|
|
38
|
+
this.hits += 1;
|
|
39
|
+
resolve(fileData);
|
|
40
|
+
}
|
|
41
|
+
catch (e) {
|
|
42
|
+
release();
|
|
43
|
+
resolve(undefined);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
return data;
|
|
48
|
+
};
|
|
49
|
+
this.set = async (key, value) => {
|
|
50
|
+
const pathKey = this.getPathKey(key);
|
|
51
|
+
const failure = await new Promise(resolve => {
|
|
52
|
+
this.lock.writeLock(key, async (release) => {
|
|
53
|
+
try {
|
|
54
|
+
const writePromise = await this.writeFile(pathKey, value);
|
|
55
|
+
release();
|
|
56
|
+
resolve(writePromise);
|
|
57
|
+
}
|
|
58
|
+
catch (e) {
|
|
59
|
+
release();
|
|
60
|
+
resolve(true);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
return !failure;
|
|
65
|
+
};
|
|
66
|
+
this.getPathKey = (key) => path_1.join(this.cachePath, key);
|
|
67
|
+
this.lock = new rwlock_1.default();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.DiskCache = DiskCache;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import LRU from 'lru-cache';
|
|
2
|
+
import { CacheLayer } from './CacheLayer';
|
|
3
|
+
import { FetchResult, LRUStats } from './typings';
|
|
4
|
+
export declare class LRUCache<K, V> implements CacheLayer<K, V> {
|
|
5
|
+
private multilayer;
|
|
6
|
+
private storage;
|
|
7
|
+
private hits;
|
|
8
|
+
private total;
|
|
9
|
+
private disposed;
|
|
10
|
+
constructor(options: LRU.Options<K, V>);
|
|
11
|
+
get: (key: K) => void | V;
|
|
12
|
+
getOrSet: (key: K, fetcher?: (() => Promise<FetchResult<V>>) | undefined) => Promise<void | V>;
|
|
13
|
+
set: (key: K, value: V, maxAge?: number | undefined) => boolean;
|
|
14
|
+
has: (key: K) => boolean;
|
|
15
|
+
getStats: (name?: string) => LRUStats;
|
|
16
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const lru_cache_1 = __importDefault(require("lru-cache"));
|
|
7
|
+
const MultilayeredCache_1 = require("./MultilayeredCache");
|
|
8
|
+
class LRUCache {
|
|
9
|
+
constructor(options) {
|
|
10
|
+
this.get = (key) => {
|
|
11
|
+
const value = this.storage.get(key);
|
|
12
|
+
if (this.storage.has(key)) {
|
|
13
|
+
this.hits += 1;
|
|
14
|
+
}
|
|
15
|
+
this.total += 1;
|
|
16
|
+
return value;
|
|
17
|
+
};
|
|
18
|
+
this.getOrSet = async (key, fetcher) => this.multilayer.get(key, fetcher);
|
|
19
|
+
this.set = (key, value, maxAge) => this.storage.set(key, value, maxAge);
|
|
20
|
+
this.has = (key) => this.storage.has(key);
|
|
21
|
+
this.getStats = (name = 'lru-cache') => {
|
|
22
|
+
const stats = {
|
|
23
|
+
disposedItems: this.disposed,
|
|
24
|
+
hitRate: this.total > 0 ? this.hits / this.total : undefined,
|
|
25
|
+
hits: this.hits,
|
|
26
|
+
itemCount: this.storage.itemCount,
|
|
27
|
+
length: this.storage.length,
|
|
28
|
+
max: this.storage.max,
|
|
29
|
+
name,
|
|
30
|
+
total: this.total,
|
|
31
|
+
};
|
|
32
|
+
this.hits = 0;
|
|
33
|
+
this.total = 0;
|
|
34
|
+
this.disposed = 0;
|
|
35
|
+
return stats;
|
|
36
|
+
};
|
|
37
|
+
this.hits = 0;
|
|
38
|
+
this.total = 0;
|
|
39
|
+
this.disposed = 0;
|
|
40
|
+
this.storage = new lru_cache_1.default({
|
|
41
|
+
...options,
|
|
42
|
+
dispose: () => this.disposed += 1,
|
|
43
|
+
noDisposeOnSet: true,
|
|
44
|
+
});
|
|
45
|
+
this.multilayer = new MultilayeredCache_1.MultilayeredCache([this]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.LRUCache = LRUCache;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { CacheLayer } from './CacheLayer';
|
|
2
|
+
import { LRUDiskCacheOptions, LRUStats } from './typings';
|
|
3
|
+
import { outputJSON, readJSON } from 'fs-extra';
|
|
4
|
+
export declare class LRUDiskCache<V> implements CacheLayer<string, V> {
|
|
5
|
+
private cachePath;
|
|
6
|
+
private readFile;
|
|
7
|
+
private writeFile;
|
|
8
|
+
private lock;
|
|
9
|
+
private disposed;
|
|
10
|
+
private hits;
|
|
11
|
+
private total;
|
|
12
|
+
private lruStorage;
|
|
13
|
+
private keyToBeDeleted;
|
|
14
|
+
constructor(cachePath: string, options: LRUDiskCacheOptions, readFile?: typeof readJSON, writeFile?: typeof outputJSON);
|
|
15
|
+
has: (key: string) => boolean;
|
|
16
|
+
getStats: (name?: string) => LRUStats;
|
|
17
|
+
get: (key: string) => Promise<void | V>;
|
|
18
|
+
set: (key: string, value: V, maxAge?: number | undefined) => Promise<boolean>;
|
|
19
|
+
private getPathKey;
|
|
20
|
+
private deleteFile;
|
|
21
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const fs_extra_1 = require("fs-extra");
|
|
7
|
+
const lru_cache_1 = __importDefault(require("lru-cache"));
|
|
8
|
+
const path_1 = require("path");
|
|
9
|
+
const rwlock_1 = __importDefault(require("rwlock"));
|
|
10
|
+
class LRUDiskCache {
|
|
11
|
+
constructor(cachePath, options, readFile = fs_extra_1.readJSON, writeFile = fs_extra_1.outputJSON) {
|
|
12
|
+
this.cachePath = cachePath;
|
|
13
|
+
this.readFile = readFile;
|
|
14
|
+
this.writeFile = writeFile;
|
|
15
|
+
this.hits = 0;
|
|
16
|
+
this.total = 0;
|
|
17
|
+
this.has = (key) => this.lruStorage.has(key);
|
|
18
|
+
this.getStats = (name = 'disk-lru-cache') => {
|
|
19
|
+
const stats = {
|
|
20
|
+
disposedItems: this.disposed,
|
|
21
|
+
hitRate: this.total > 0 ? this.hits / this.total : undefined,
|
|
22
|
+
hits: this.hits,
|
|
23
|
+
itemCount: this.lruStorage.itemCount,
|
|
24
|
+
length: this.lruStorage.length,
|
|
25
|
+
max: this.lruStorage.max,
|
|
26
|
+
name,
|
|
27
|
+
total: this.total,
|
|
28
|
+
};
|
|
29
|
+
this.hits = 0;
|
|
30
|
+
this.total = 0;
|
|
31
|
+
this.disposed = 0;
|
|
32
|
+
return stats;
|
|
33
|
+
};
|
|
34
|
+
this.get = async (key) => {
|
|
35
|
+
const timeOfDeath = this.lruStorage.get(key);
|
|
36
|
+
this.total += 1;
|
|
37
|
+
if (timeOfDeath === undefined) {
|
|
38
|
+
// if it is an outdated file when stale=false
|
|
39
|
+
if (this.keyToBeDeleted) {
|
|
40
|
+
await this.deleteFile(key);
|
|
41
|
+
}
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
const pathKey = this.getPathKey(key);
|
|
45
|
+
const data = await new Promise(resolve => {
|
|
46
|
+
this.lock.readLock(key, async (release) => {
|
|
47
|
+
try {
|
|
48
|
+
const fileData = await this.readFile(pathKey);
|
|
49
|
+
release();
|
|
50
|
+
this.hits += 1;
|
|
51
|
+
resolve(fileData);
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
release();
|
|
55
|
+
resolve(undefined);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
// if it is an outdated file when stale=true
|
|
60
|
+
if (timeOfDeath < Date.now()) {
|
|
61
|
+
this.lruStorage.del(key);
|
|
62
|
+
await this.deleteFile(key);
|
|
63
|
+
}
|
|
64
|
+
return data;
|
|
65
|
+
};
|
|
66
|
+
this.set = async (key, value, maxAge) => {
|
|
67
|
+
let timeOfDeath = NaN;
|
|
68
|
+
if (maxAge) {
|
|
69
|
+
timeOfDeath = maxAge + Date.now();
|
|
70
|
+
this.lruStorage.set(key, timeOfDeath, maxAge);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
this.lruStorage.set(key, NaN);
|
|
74
|
+
}
|
|
75
|
+
if (this.keyToBeDeleted && this.keyToBeDeleted !== key) {
|
|
76
|
+
await this.deleteFile(this.keyToBeDeleted);
|
|
77
|
+
}
|
|
78
|
+
const pathKey = this.getPathKey(key);
|
|
79
|
+
const failure = await new Promise(resolve => {
|
|
80
|
+
this.lock.writeLock(key, async (release) => {
|
|
81
|
+
try {
|
|
82
|
+
const writePromise = await this.writeFile(pathKey, value);
|
|
83
|
+
release();
|
|
84
|
+
resolve(writePromise);
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
release();
|
|
88
|
+
resolve(true);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
return !failure;
|
|
93
|
+
};
|
|
94
|
+
this.getPathKey = (key) => {
|
|
95
|
+
return path_1.join(this.cachePath, key);
|
|
96
|
+
};
|
|
97
|
+
this.deleteFile = async (key) => {
|
|
98
|
+
const pathKey = this.getPathKey(key);
|
|
99
|
+
this.keyToBeDeleted = '';
|
|
100
|
+
const failure = new Promise(resolve => {
|
|
101
|
+
this.lock.writeLock(key, async (release) => {
|
|
102
|
+
try {
|
|
103
|
+
const removePromise = await fs_extra_1.remove(pathKey);
|
|
104
|
+
release();
|
|
105
|
+
resolve(removePromise);
|
|
106
|
+
}
|
|
107
|
+
catch (e) {
|
|
108
|
+
release();
|
|
109
|
+
resolve(true);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
return !failure;
|
|
114
|
+
};
|
|
115
|
+
this.hits = 0;
|
|
116
|
+
this.total = 0;
|
|
117
|
+
this.disposed = 0;
|
|
118
|
+
this.keyToBeDeleted = '';
|
|
119
|
+
this.lock = new rwlock_1.default();
|
|
120
|
+
const dispose = (key) => {
|
|
121
|
+
this.keyToBeDeleted = key;
|
|
122
|
+
this.disposed += 1;
|
|
123
|
+
};
|
|
124
|
+
const lruOptions = {
|
|
125
|
+
...options,
|
|
126
|
+
dispose,
|
|
127
|
+
noDisposeOnSet: true,
|
|
128
|
+
};
|
|
129
|
+
this.lruStorage = new lru_cache_1.default(lruOptions);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
exports.LRUDiskCache = LRUDiskCache;
|