@venizia/ignis-helpers 0.4.6 → 0.4.7-1
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/LICENSE.md +1 -3
- package/README.md +4 -5
- package/dist/esm/common/redact.d.ts +25 -17
- package/dist/esm/common/redact.d.ts.map +1 -1
- package/dist/esm/common/redact.js +50 -46
- package/dist/esm/common/redact.js.map +1 -1
- package/dist/esm/common/resolvers.d.ts +7 -5
- package/dist/esm/common/resolvers.d.ts.map +1 -1
- package/dist/esm/common/resolvers.js +11 -9
- package/dist/esm/common/resolvers.js.map +1 -1
- package/dist/esm/core.d.ts +6 -3
- package/dist/esm/core.d.ts.map +1 -1
- package/dist/esm/core.js +6 -3
- package/dist/esm/core.js.map +1 -1
- package/dist/esm/modules/base.js +4 -4
- package/dist/esm/modules/base.js.map +1 -1
- package/dist/esm/modules/cron/helper.js +4 -4
- package/dist/esm/modules/cron/helper.js.map +1 -1
- package/dist/esm/modules/crypto/algorithms/aes-legacy.algorithm.js +3 -3
- package/dist/esm/modules/crypto/algorithms/aes-legacy.algorithm.js.map +1 -1
- package/dist/esm/modules/crypto/algorithms/aes.algorithm.js +5 -5
- package/dist/esm/modules/crypto/algorithms/aes.algorithm.js.map +1 -1
- package/dist/esm/modules/crypto/algorithms/base.algorithm.js +3 -3
- package/dist/esm/modules/crypto/algorithms/base.algorithm.js.map +1 -1
- package/dist/esm/modules/env/app-env.d.ts.map +1 -1
- package/dist/esm/modules/env/app-env.js +2 -2
- package/dist/esm/modules/env/app-env.js.map +1 -1
- package/dist/esm/modules/logger/common/constants.d.ts +23 -21
- package/dist/esm/modules/logger/common/constants.d.ts.map +1 -1
- package/dist/esm/modules/logger/common/constants.js +45 -42
- package/dist/esm/modules/logger/common/constants.js.map +1 -1
- package/dist/esm/modules/logger/factory.d.ts +5 -3
- package/dist/esm/modules/logger/factory.d.ts.map +1 -1
- package/dist/esm/modules/logger/factory.js +5 -3
- package/dist/esm/modules/logger/factory.js.map +1 -1
- package/dist/esm/modules/logger/formatting/deep-splat.d.ts +10 -6
- package/dist/esm/modules/logger/formatting/deep-splat.d.ts.map +1 -1
- package/dist/esm/modules/logger/formatting/deep-splat.js +73 -62
- package/dist/esm/modules/logger/formatting/deep-splat.js.map +1 -1
- package/dist/esm/modules/logger/formatting/error-prettier.d.ts.map +1 -1
- package/dist/esm/modules/logger/formatting/error-prettier.js +8 -6
- package/dist/esm/modules/logger/formatting/error-prettier.js.map +1 -1
- package/dist/esm/modules/logger/hf/logger.d.ts.map +1 -1
- package/dist/esm/modules/logger/hf/logger.js +2 -2
- package/dist/esm/modules/logger/hf/logger.js.map +1 -1
- package/dist/esm/modules/logger/pino/backing.d.ts +0 -9
- package/dist/esm/modules/logger/pino/backing.d.ts.map +1 -1
- package/dist/esm/modules/logger/pino/backing.js +3 -8
- package/dist/esm/modules/logger/pino/backing.js.map +1 -1
- package/dist/esm/modules/logger/pino/destination.d.ts +6 -9
- package/dist/esm/modules/logger/pino/destination.d.ts.map +1 -1
- package/dist/esm/modules/logger/pino/destination.js +12 -13
- package/dist/esm/modules/logger/pino/destination.js.map +1 -1
- package/dist/esm/modules/logger/pino/logger.js +2 -2
- package/dist/esm/modules/logger/pino/logger.js.map +1 -1
- package/dist/esm/modules/logger/resolver.js +2 -2
- package/dist/esm/modules/logger/resolver.js.map +1 -1
- package/dist/esm/modules/logger/slot.d.ts +39 -18
- package/dist/esm/modules/logger/slot.d.ts.map +1 -1
- package/dist/esm/modules/logger/slot.js +117 -110
- package/dist/esm/modules/logger/slot.js.map +1 -1
- package/dist/esm/modules/logger/winston/common/constants.d.ts.map +1 -1
- package/dist/esm/modules/logger/winston/common/constants.js +7 -6
- package/dist/esm/modules/logger/winston/common/constants.js.map +1 -1
- package/dist/esm/modules/logger/winston/define.d.ts +0 -7
- package/dist/esm/modules/logger/winston/define.d.ts.map +1 -1
- package/dist/esm/modules/logger/winston/define.js +0 -5
- package/dist/esm/modules/logger/winston/define.js.map +1 -1
- package/dist/esm/modules/logger/winston/formats.d.ts +2 -0
- package/dist/esm/modules/logger/winston/formats.d.ts.map +1 -1
- package/dist/esm/modules/logger/winston/formats.js +17 -4
- package/dist/esm/modules/logger/winston/formats.js.map +1 -1
- package/dist/esm/modules/logger/winston/index.d.ts +3 -2
- package/dist/esm/modules/logger/winston/index.d.ts.map +1 -1
- package/dist/esm/modules/logger/winston/index.js +3 -2
- package/dist/esm/modules/logger/winston/index.js.map +1 -1
- package/dist/esm/modules/logger/winston/logger-factory.js +4 -4
- package/dist/esm/modules/logger/winston/logger-factory.js.map +1 -1
- package/dist/esm/modules/network/http-request/base-network-request.helper.js +2 -2
- package/dist/esm/modules/network/http-request/base-network-request.helper.js.map +1 -1
- package/dist/esm/modules/network/http-request/common/index.d.ts +0 -1
- package/dist/esm/modules/network/http-request/common/index.d.ts.map +1 -1
- package/dist/esm/modules/network/http-request/common/index.js +0 -1
- package/dist/esm/modules/network/http-request/common/index.js.map +1 -1
- package/dist/esm/modules/network/http-request/fetcher/axios-fetcher.d.ts.map +1 -1
- package/dist/esm/modules/network/http-request/fetcher/axios-fetcher.js +4 -2
- package/dist/esm/modules/network/http-request/fetcher/axios-fetcher.js.map +1 -1
- package/dist/esm/modules/network/http-request/fetcher/node-fetcher.d.ts.map +1 -1
- package/dist/esm/modules/network/http-request/fetcher/node-fetcher.js +6 -4
- package/dist/esm/modules/network/http-request/fetcher/node-fetcher.js.map +1 -1
- package/dist/esm/modules/network/tcp-socket/base-tcp-client.helper.js +4 -4
- package/dist/esm/modules/network/tcp-socket/base-tcp-client.helper.js.map +1 -1
- package/dist/esm/modules/network/tcp-socket/base-tcp-server.helper.js +4 -4
- package/dist/esm/modules/network/tcp-socket/base-tcp-server.helper.js.map +1 -1
- package/dist/esm/modules/network/udp-socket/network-udp-client.helper.js +2 -2
- package/dist/esm/modules/network/udp-socket/network-udp-client.helper.js.map +1 -1
- package/dist/esm/modules/network/utilities/query-string.d.ts +22 -20
- package/dist/esm/modules/network/utilities/query-string.d.ts.map +1 -1
- package/dist/esm/modules/network/utilities/query-string.js +48 -46
- package/dist/esm/modules/network/utilities/query-string.js.map +1 -1
- package/dist/esm/modules/network/utilities/url-safety/common/errors.d.ts +0 -10
- package/dist/esm/modules/network/utilities/url-safety/common/errors.d.ts.map +1 -1
- package/dist/esm/modules/network/utilities/url-safety/common/errors.js +0 -11
- package/dist/esm/modules/network/utilities/url-safety/common/errors.js.map +1 -1
- package/dist/esm/modules/network/utilities/url-safety/ingest.d.ts.map +1 -1
- package/dist/esm/modules/network/utilities/url-safety/ingest.js +2 -1
- package/dist/esm/modules/network/utilities/url-safety/ingest.js.map +1 -1
- package/dist/esm/modules/network/utilities/url-safety/policy.d.ts +10 -0
- package/dist/esm/modules/network/utilities/url-safety/policy.d.ts.map +1 -1
- package/dist/esm/modules/network/utilities/url-safety/policy.js +11 -0
- package/dist/esm/modules/network/utilities/url-safety/policy.js.map +1 -1
- package/dist/esm/modules/pool/abstract.d.ts.map +1 -1
- package/dist/esm/modules/pool/abstract.js +4 -2
- package/dist/esm/modules/pool/abstract.js.map +1 -1
- package/dist/esm/modules/queue/bullmq/helper.d.ts.map +1 -1
- package/dist/esm/modules/queue/bullmq/helper.js +6 -6
- package/dist/esm/modules/queue/bullmq/helper.js.map +1 -1
- package/dist/esm/modules/queue/common/hook.d.ts +14 -12
- package/dist/esm/modules/queue/common/hook.d.ts.map +1 -1
- package/dist/esm/modules/queue/common/hook.js +21 -19
- package/dist/esm/modules/queue/common/hook.js.map +1 -1
- package/dist/esm/modules/queue/common/index.d.ts +0 -1
- package/dist/esm/modules/queue/common/index.d.ts.map +1 -1
- package/dist/esm/modules/queue/common/index.js +0 -1
- package/dist/esm/modules/queue/common/index.js.map +1 -1
- package/dist/esm/modules/queue/internal/sequential/helper.js +8 -8
- package/dist/esm/modules/queue/internal/sequential/helper.js.map +1 -1
- package/dist/esm/modules/queue/kafka/base.js +3 -3
- package/dist/esm/modules/queue/kafka/base.js.map +1 -1
- package/dist/esm/modules/queue/kafka/bundler/index.d.ts +0 -2
- package/dist/esm/modules/queue/kafka/bundler/index.d.ts.map +1 -1
- package/dist/esm/modules/queue/kafka/bundler/index.js +0 -2
- package/dist/esm/modules/queue/kafka/bundler/index.js.map +1 -1
- package/dist/esm/modules/queue/kafka/bundler/plugins.d.ts +8 -2
- package/dist/esm/modules/queue/kafka/bundler/plugins.d.ts.map +1 -1
- package/dist/esm/modules/queue/kafka/bundler/plugins.js +64 -7
- package/dist/esm/modules/queue/kafka/bundler/plugins.js.map +1 -1
- package/dist/esm/modules/queue/kafka/consumer.d.ts.map +1 -1
- package/dist/esm/modules/queue/kafka/consumer.js +26 -24
- package/dist/esm/modules/queue/kafka/consumer.js.map +1 -1
- package/dist/esm/modules/queue/mqtt/helper.js +10 -10
- package/dist/esm/modules/queue/mqtt/helper.js.map +1 -1
- package/dist/esm/modules/redis/base/abstract.helper.js +8 -8
- package/dist/esm/modules/redis/base/abstract.helper.js.map +1 -1
- package/dist/esm/modules/redis/cluster/cluster.helper.js +2 -2
- package/dist/esm/modules/redis/cluster/cluster.helper.js.map +1 -1
- package/dist/esm/modules/redis/factory.d.ts +13 -9
- package/dist/esm/modules/redis/factory.d.ts.map +1 -1
- package/dist/esm/modules/redis/factory.js +15 -13
- package/dist/esm/modules/redis/factory.js.map +1 -1
- package/dist/esm/modules/redis/sentinel/sentinel.helper.js +2 -2
- package/dist/esm/modules/redis/sentinel/sentinel.helper.js.map +1 -1
- package/dist/esm/modules/redis/single/single.helper.js +2 -2
- package/dist/esm/modules/redis/single/single.helper.js.map +1 -1
- package/dist/esm/modules/redis/utility.d.ts +13 -11
- package/dist/esm/modules/redis/utility.d.ts.map +1 -1
- package/dist/esm/modules/redis/utility.js +46 -44
- package/dist/esm/modules/redis/utility.js.map +1 -1
- package/dist/esm/modules/retry/helper.d.ts.map +1 -1
- package/dist/esm/modules/retry/helper.js +6 -4
- package/dist/esm/modules/retry/helper.js.map +1 -1
- package/dist/esm/modules/secrets/factory.d.ts +5 -3
- package/dist/esm/modules/secrets/factory.d.ts.map +1 -1
- package/dist/esm/modules/secrets/factory.js +36 -34
- package/dist/esm/modules/secrets/factory.js.map +1 -1
- package/dist/esm/modules/secrets/hashicorp/hashicorp.helper.d.ts.map +1 -1
- package/dist/esm/modules/secrets/hashicorp/hashicorp.helper.js +4 -2
- package/dist/esm/modules/secrets/hashicorp/hashicorp.helper.js.map +1 -1
- package/dist/esm/modules/socket/socket-io/server/helper.d.ts.map +1 -1
- package/dist/esm/modules/socket/socket-io/server/helper.js +11 -11
- package/dist/esm/modules/socket/socket-io/server/helper.js.map +1 -1
- package/dist/esm/modules/socket/websocket/emitter/helper.d.ts.map +1 -1
- package/dist/esm/modules/socket/websocket/emitter/helper.js +3 -3
- package/dist/esm/modules/socket/websocket/emitter/helper.js.map +1 -1
- package/dist/esm/modules/socket/websocket/server/delivery.d.ts.map +1 -1
- package/dist/esm/modules/socket/websocket/server/delivery.js +5 -6
- package/dist/esm/modules/socket/websocket/server/delivery.js.map +1 -1
- package/dist/esm/modules/socket/websocket/server/helper.d.ts.map +1 -1
- package/dist/esm/modules/socket/websocket/server/helper.js +7 -7
- package/dist/esm/modules/socket/websocket/server/helper.js.map +1 -1
- package/dist/esm/modules/storage/base.d.ts.map +1 -1
- package/dist/esm/modules/storage/base.js +9 -8
- package/dist/esm/modules/storage/base.js.map +1 -1
- package/dist/esm/modules/storage/bun-s3/helper.d.ts.map +1 -1
- package/dist/esm/modules/storage/bun-s3/helper.js +25 -18
- package/dist/esm/modules/storage/bun-s3/helper.js.map +1 -1
- package/dist/esm/modules/storage/bun-s3/utility.d.ts +95 -77
- package/dist/esm/modules/storage/bun-s3/utility.d.ts.map +1 -1
- package/dist/esm/modules/storage/bun-s3/utility.js +261 -244
- package/dist/esm/modules/storage/bun-s3/utility.js.map +1 -1
- package/dist/esm/modules/storage/common/errors.d.ts +0 -4
- package/dist/esm/modules/storage/common/errors.d.ts.map +1 -1
- package/dist/esm/modules/storage/common/errors.js +0 -10
- package/dist/esm/modules/storage/common/errors.js.map +1 -1
- package/dist/esm/modules/storage/common/utility.d.ts +11 -5
- package/dist/esm/modules/storage/common/utility.d.ts.map +1 -1
- package/dist/esm/modules/storage/common/utility.js +30 -17
- package/dist/esm/modules/storage/common/utility.js.map +1 -1
- package/dist/esm/modules/storage/disk/helper.d.ts.map +1 -1
- package/dist/esm/modules/storage/disk/helper.js +18 -9
- package/dist/esm/modules/storage/disk/helper.js.map +1 -1
- package/dist/esm/modules/tree/builder.d.ts.map +1 -1
- package/dist/esm/modules/tree/builder.js +2 -1
- package/dist/esm/modules/tree/builder.js.map +1 -1
- package/dist/esm/modules/uid/opaque.d.ts.map +1 -1
- package/dist/esm/modules/uid/opaque.js +4 -2
- package/dist/esm/modules/uid/opaque.js.map +1 -1
- package/dist/esm/modules/uid/uuid/helper.d.ts +1 -1
- package/dist/esm/modules/uid/uuid/helper.d.ts.map +1 -1
- package/dist/esm/modules/uid/uuid/helper.js +7 -7
- package/dist/esm/modules/uid/uuid/helper.js.map +1 -1
- package/dist/esm/modules/uid/uuid/v4.d.ts +11 -8
- package/dist/esm/modules/uid/uuid/v4.d.ts.map +1 -1
- package/dist/esm/modules/uid/uuid/v4.js +51 -46
- package/dist/esm/modules/uid/uuid/v4.js.map +1 -1
- package/dist/esm/modules/uid/uuid/v5.d.ts +17 -14
- package/dist/esm/modules/uid/uuid/v5.d.ts.map +1 -1
- package/dist/esm/modules/uid/uuid/v5.js +83 -78
- package/dist/esm/modules/uid/uuid/v5.js.map +1 -1
- package/dist/esm/modules/uid/uuid/v7.d.ts +15 -10
- package/dist/esm/modules/uid/uuid/v7.d.ts.map +1 -1
- package/dist/esm/modules/uid/uuid/v7.js +88 -79
- package/dist/esm/modules/uid/uuid/v7.js.map +1 -1
- package/dist/esm/modules/worker-thread/bus/base.js +2 -2
- package/dist/esm/modules/worker-thread/bus/base.js.map +1 -1
- package/dist/esm/modules/worker-thread/worker/base.d.ts.map +1 -1
- package/dist/esm/modules/worker-thread/worker/base.js +4 -3
- package/dist/esm/modules/worker-thread/worker/base.js.map +1 -1
- package/dist/esm/utilities/error.utility.d.ts +7 -0
- package/dist/esm/utilities/error.utility.d.ts.map +1 -0
- package/dist/esm/utilities/error.utility.js +9 -0
- package/dist/esm/utilities/error.utility.js.map +1 -0
- package/dist/esm/utilities/form-body.utility.d.ts +12 -10
- package/dist/esm/utilities/form-body.utility.d.ts.map +1 -1
- package/dist/esm/utilities/form-body.utility.js +19 -16
- package/dist/esm/utilities/form-body.utility.js.map +1 -1
- package/dist/esm/utilities/hr-time.utility.d.ts +7 -5
- package/dist/esm/utilities/hr-time.utility.d.ts.map +1 -1
- package/dist/esm/utilities/hr-time.utility.js +11 -9
- package/dist/esm/utilities/hr-time.utility.js.map +1 -1
- package/dist/esm/utilities/index.d.ts +2 -2
- package/dist/esm/utilities/index.d.ts.map +1 -1
- package/dist/esm/utilities/index.js +2 -2
- package/dist/esm/utilities/index.js.map +1 -1
- package/dist/esm/utilities/module.utility.d.ts.map +1 -1
- package/dist/esm/utilities/module.utility.js +4 -2
- package/dist/esm/utilities/module.utility.js.map +1 -1
- package/dist/esm/utilities/parse.utility.d.ts +89 -43
- package/dist/esm/utilities/parse.utility.d.ts.map +1 -1
- package/dist/esm/utilities/parse.utility.js +173 -141
- package/dist/esm/utilities/parse.utility.js.map +1 -1
- package/dist/esm/utilities/performance.utility.d.ts +15 -13
- package/dist/esm/utilities/performance.utility.d.ts.map +1 -1
- package/dist/esm/utilities/performance.utility.js +34 -29
- package/dist/esm/utilities/performance.utility.js.map +1 -1
- package/dist/esm/utilities/promise.utility.d.ts +20 -18
- package/dist/esm/utilities/promise.utility.d.ts.map +1 -1
- package/dist/esm/utilities/promise.utility.js +41 -51
- package/dist/esm/utilities/promise.utility.js.map +1 -1
- package/dist/esm/utilities/request.utility.d.ts +35 -29
- package/dist/esm/utilities/request.utility.d.ts.map +1 -1
- package/dist/esm/utilities/request.utility.js +117 -115
- package/dist/esm/utilities/request.utility.js.map +1 -1
- package/package.json +4 -15
- package/dist/esm/modules/logger/winston/formatters/deep-splat.d.ts +0 -4
- package/dist/esm/modules/logger/winston/formatters/deep-splat.d.ts.map +0 -1
- package/dist/esm/modules/logger/winston/formatters/deep-splat.js +0 -14
- package/dist/esm/modules/logger/winston/formatters/deep-splat.js.map +0 -1
- package/dist/esm/modules/logger/winston/formatters/index.d.ts +0 -2
- package/dist/esm/modules/logger/winston/formatters/index.d.ts.map +0 -1
- package/dist/esm/modules/logger/winston/formatters/index.js +0 -2
- package/dist/esm/modules/logger/winston/formatters/index.js.map +0 -1
- package/dist/esm/modules/network/http-request/common/types.d.ts +0 -2
- package/dist/esm/modules/network/http-request/common/types.d.ts.map +0 -1
- package/dist/esm/modules/network/http-request/common/types.js +0 -2
- package/dist/esm/modules/network/http-request/common/types.js.map +0 -1
- package/dist/esm/modules/queue/common/types.d.ts +0 -2
- package/dist/esm/modules/queue/common/types.d.ts.map +0 -1
- package/dist/esm/modules/queue/common/types.js +0 -2
- package/dist/esm/modules/queue/common/types.js.map +0 -1
- package/dist/esm/modules/queue/kafka/bundler/platformatic-require.plugin.d.ts +0 -4
- package/dist/esm/modules/queue/kafka/bundler/platformatic-require.plugin.d.ts.map +0 -1
- package/dist/esm/modules/queue/kafka/bundler/platformatic-require.plugin.js +0 -28
- package/dist/esm/modules/queue/kafka/bundler/platformatic-require.plugin.js.map +0 -1
- package/dist/esm/modules/queue/kafka/bundler/platformatic-wasm.plugin.d.ts +0 -4
- package/dist/esm/modules/queue/kafka/bundler/platformatic-wasm.plugin.d.ts.map +0 -1
- package/dist/esm/modules/queue/kafka/bundler/platformatic-wasm.plugin.js +0 -26
- package/dist/esm/modules/queue/kafka/bundler/platformatic-wasm.plugin.js.map +0 -1
- package/dist/esm/modules/storage/minio/helper.d.ts +0 -55
- package/dist/esm/modules/storage/minio/helper.d.ts.map +0 -1
- package/dist/esm/modules/storage/minio/helper.js +0 -176
- package/dist/esm/modules/storage/minio/helper.js.map +0 -1
- package/dist/esm/modules/storage/minio/index.d.ts +0 -2
- package/dist/esm/modules/storage/minio/index.d.ts.map +0 -1
- package/dist/esm/modules/storage/minio/index.js +0 -2
- package/dist/esm/modules/storage/minio/index.js.map +0 -1
- package/dist/esm/utilities/sleep.utility.d.ts +0 -2
- package/dist/esm/utilities/sleep.utility.d.ts.map +0 -1
- package/dist/esm/utilities/sleep.utility.js +0 -4
- package/dist/esm/utilities/sleep.utility.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ingest.js","sourceRoot":"","sources":["../../../../../../src/modules/network/utilities/url-safety/ingest.ts"],"names":[],"mappings":"AAAA,gGAAgG;AAChG,yCAAyC;AAEzC,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAElD,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAErC,6EAA6E;AAC7E,MAAM,OAAO,SAAS;IACpB;;;;;;;OAOG;IACH,MAAM,CAAC,KAAK,CAAC,gBAAgB,CAAC,IAG7B;QACC,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;QAElC,IAAI,MAAM,EAAE,mBAAmB,IAAI,iBAAiB,CAAC,qBAAqB,EAAE,CAAC;YAC3E,OAAO;QACT,CAAC;QAED,+FAA+F;QAC/F,gGAAgG;QAChG,MAAM,OAAO,GAAG,SAAS,CAAC,eAAe,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC;QACxD,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,OAAO,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC;QAEvD,IAAI,CAAC,SAAS,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;IAC/B,CAAC;CACF","sourcesContent":["// The half `policy.ts` cannot do: resolving a hostname needs `node:dns`, so nothing here may be\n// re-exported from the `./core` subpath.\n\nimport { DurationMultipliers } from '@/common/constants/duration';\nimport { getError } from '@/modules/error';\nimport { lookup } from 'node:dns/promises';\nimport { UrlSafetyDefaults } from './common/constants';\nimport { UrlSafetyErrors } from './common/errors';\nimport type { IUrlSafetyPolicy } from './common/types';\nimport { UrlPolicy } from './policy';\n\n/** Fetching an untrusted url without letting it reach inside the network. */\nexport class UrlIngest {\n /**\n * Rejects when ANY resolved address is non-public. Checking only the first walks straight into a\n * host that publishes one public A record beside an internal one.\n *\n * A rebinding window remains: the address can change between this check and the connect. Closing\n * it means connecting to the resolved address and setting `Host` by hand, which no fetch API\n * exposes.\n */\n static async assertPublicHost(opts: {\n hostname: string;\n policy?: IUrlSafetyPolicy;\n }): Promise<void> {\n const { hostname, policy } = opts;\n\n if (policy?.allowPrivateAddress ?? UrlSafetyDefaults.ALLOW_PRIVATE_ADDRESS) {\n return;\n }\n\n // `URL.hostname` keeps the brackets on an IPv6 literal; `lookup` does not accept them, and the\n // ENOTFOUND that follows reads as a transient DNS failure rather than the refusal it should be.\n const address = UrlPolicy.readHostAddress({ hostname });\n const addresses = await lookup(address, { all: true });\n\n if (!addresses.length) {\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[assertPublicHost] Host resolves to nothing | host: ${hostname}`,\n });\n }\n\n const offender = addresses.find((entry) =>\n UrlPolicy.isNonPublicAddress({ address: entry.address }),\n );\n\n if (offender) {\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[assertPublicHost] Host resolves to a non-public address | host: ${hostname} | address: ${offender.address}`,\n });\n }\n }\n\n /** Both halves, in the order they must run: shape first, then the network. */\n static async assertReachableUrl(opts: { url: string; policy?: IUrlSafetyPolicy }): Promise<URL> {\n const parsed = UrlPolicy.assertSafeUrl(opts);\n await UrlIngest.assertPublicHost({ hostname: parsed.hostname, policy: opts.policy });\n return parsed;\n }\n\n private static readTimeoutMilliseconds(opts: { policy?: IUrlSafetyPolicy }): number {\n const { timeout } = opts.policy ?? {};\n const milliseconds = timeout ? DurationMultipliers.toMilliseconds(timeout) : null;\n return milliseconds ?? UrlSafetyDefaults.TIMEOUT_MILLISECONDS;\n }\n\n /**\n * A GET that an untrusted url cannot steer inside the network. Redirects are followed by hand so\n * every hop is re-checked - `redirect: 'follow'` would land on an internal host with no second\n * look.\n *\n * The body is returned unread: the caller decides whether to stream it onward or cap it with\n * {@link UrlIngest.readCappedBody}. Nothing is buffered here.\n */\n static async fetchGuarded(opts: {\n url: string;\n policy?: IUrlSafetyPolicy;\n headers?: Record<string, string>;\n }): Promise<Response> {\n const { url, policy, headers } = opts;\n const maxRedirects = policy?.maxRedirects ?? UrlSafetyDefaults.MAX_REDIRECTS;\n const timeoutMilliseconds = UrlIngest.readTimeoutMilliseconds({ policy });\n\n let target = await UrlIngest.assertReachableUrl({ url, policy });\n\n for (let hop = 0; hop <= maxRedirects; hop += 1) {\n const response = await fetch(target, {\n redirect: 'manual',\n signal: AbortSignal.timeout(timeoutMilliseconds),\n ...(headers ? { headers } : {}),\n });\n\n if (response.status < 300 || response.status > 399) {\n if (!response.ok) {\n // Cancelled, or the socket stays open on every 4xx and 5xx.\n await response.body?.cancel();\n throw getError({\n message: `[fetchGuarded] Request failed | status: ${response.status} | url: ${url}`,\n });\n }\n\n return response;\n }\n\n const location = response.headers.get('location');\n await response.body?.cancel();\n\n if (!location) {\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[fetchGuarded] Redirect carried no location | url: ${url}`,\n });\n }\n\n target = await UrlIngest.assertReachableUrl({\n url: new URL(location, target).toString(),\n policy,\n });\n }\n\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[fetchGuarded] Too many redirects | max: ${maxRedirects} | url: ${url}`,\n });\n }\n\n /**\n * Enforces `maxBytes` on a stream nothing reads into this process - the shape a caller wants when\n * the body is piped onward rather than held. Without it a `maxBytes` policy is inert on that path.\n */\n static capStream(opts: {\n source: ReadableStream<Uint8Array>;\n policy?: IUrlSafetyPolicy;\n }): ReadableStream<Uint8Array> {\n const { source } = opts;\n const maxBytes = opts.policy?.maxBytes ?? UrlSafetyDefaults.MAX_BYTES;\n let received = 0;\n\n return source.pipeThrough(\n new TransformStream<Uint8Array, Uint8Array>({\n transform(chunk, controller) {\n received += chunk.byteLength;\n\n if (received > maxBytes) {\n controller.error(\n getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[capStream] Body exceeds the cap | received: ${received} | max: ${maxBytes}`,\n }),\n );\n return;\n }\n\n controller.enqueue(chunk);\n },\n }),\n );\n }\n\n /**\n * Reads a body that must not exceed `maxBytes`, cancelling the moment it does. `content-length` is\n * checked first because it is free, and counted anyway because it may be absent or lie.\n */\n static async readCappedBody(opts: {\n response: Response;\n policy?: IUrlSafetyPolicy;\n }): Promise<Buffer> {\n const { response, policy } = opts;\n const maxBytes = policy?.maxBytes ?? UrlSafetyDefaults.MAX_BYTES;\n\n const declaredLength = Number(response.headers.get('content-length') ?? 0);\n if (declaredLength > maxBytes) {\n await response.body?.cancel();\n // The same fact as the streaming branch below, so the same verdict: a caller must not retry an\n // oversized body when the server sends `content-length` and drop it when the server omits one.\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[readCappedBody] Body exceeds the cap | declared: ${declaredLength} | max: ${maxBytes}`,\n });\n }\n\n const reader = response.body?.getReader();\n if (!reader) {\n throw getError({ message: '[readCappedBody] Response carried no body' });\n }\n\n const chunks: Uint8Array[] = [];\n let received = 0;\n\n for (;;) {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n\n received += value.length;\n if (received > maxBytes) {\n await reader.cancel();\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[readCappedBody] Body exceeds the cap | received: ${received} | max: ${maxBytes}`,\n });\n }\n\n chunks.push(value);\n }\n\n return Buffer.concat(chunks);\n }\n}\n"]}
|
|
1
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F,+FAA+F;YAC/F,MAAM,QAAQ,CAAC;gBACb,KAAK,EAAE,eAAe,CAAC,WAAW;gBAClC,OAAO,EAAE,qDAAqD,cAAc,WAAW,QAAQ,EAAE;aAClG,CAAC,CAAC;QACL,CAAC;QAED,MAAM,MAAM,GAAG,QAAQ,CAAC,IAAI,EAAE,SAAS,EAAE,CAAC;QAC1C,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,QAAQ,CAAC,EAAE,OAAO,EAAE,2CAA2C,EAAE,CAAC,CAAC;QAC3E,CAAC;QAED,MAAM,MAAM,GAAiB,EAAE,CAAC;QAChC,IAAI,QAAQ,GAAG,CAAC,CAAC;QAEjB,SAAS,CAAC;YACR,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;YAC5C,IAAI,IAAI,EAAE,CAAC;gBACT,MAAM;YACR,CAAC;YAED,QAAQ,IAAI,KAAK,CAAC,MAAM,CAAC;YACzB,IAAI,QAAQ,GAAG,QAAQ,EAAE,CAAC;gBACxB,MAAM,MAAM,CAAC,MAAM,EAAE,CAAC;gBACtB,MAAM,QAAQ,CAAC;oBACb,KAAK,EAAE,eAAe,CAAC,WAAW;oBAClC,OAAO,EAAE,qDAAqD,QAAQ,WAAW,QAAQ,EAAE;iBAC5F,CAAC,CAAC;YACL,CAAC;YAED,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACrB,CAAC;QAED,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC/B,CAAC;CACF","sourcesContent":["// The half `policy.ts` cannot do: resolving a hostname needs `node:dns`, so nothing here may be\n// re-exported from the `./core` subpath.\n\nimport { DurationMultipliers } from '@/common/constants/duration';\nimport { getError } from '@/modules/error';\nimport { lookup } from 'node:dns/promises';\nimport { UrlSafetyDefaults } from './common/constants';\nimport { UrlSafetyErrors } from './common/errors';\nimport type { IUrlSafetyPolicy } from './common/types';\nimport { UrlPolicy } from './policy';\n\n/** Fetching an untrusted url without letting it reach inside the network. */\nexport class UrlIngest {\n /**\n * Rejects when ANY resolved address is non-public. Checking only the first walks straight into a\n * host that publishes one public A record beside an internal one.\n *\n * A rebinding window remains: the address can change between this check and the connect. Closing\n * it means connecting to the resolved address and setting `Host` by hand, which no fetch API\n * exposes.\n */\n static async assertPublicHost(opts: {\n hostname: string;\n policy?: IUrlSafetyPolicy;\n }): Promise<void> {\n const { hostname, policy } = opts;\n\n if (policy?.allowPrivateAddress ?? UrlSafetyDefaults.ALLOW_PRIVATE_ADDRESS) {\n return;\n }\n\n // `URL.hostname` keeps the brackets on an IPv6 literal; `lookup` does not accept them, and the\n // ENOTFOUND that follows reads as a transient DNS failure rather than the refusal it should be.\n const address = UrlPolicy.readHostAddress({ hostname });\n const addresses = await lookup(address, { all: true });\n\n if (!addresses.length) {\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[assertPublicHost] Host resolves to nothing | host: ${hostname}`,\n });\n }\n\n const offender = addresses.find((entry) =>\n UrlPolicy.isNonPublicAddress({ address: entry.address }),\n );\n\n if (offender) {\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[assertPublicHost] Host resolves to a non-public address | host: ${hostname} | address: ${offender.address}`,\n });\n }\n }\n\n /** Both halves, in the order they must run: shape first, then the network. */\n static async assertReachableUrl(opts: { url: string; policy?: IUrlSafetyPolicy }): Promise<URL> {\n const parsed = UrlPolicy.assertSafeUrl(opts);\n await UrlIngest.assertPublicHost({ hostname: parsed.hostname, policy: opts.policy });\n return parsed;\n }\n\n private static readTimeoutMilliseconds(opts: { policy?: IUrlSafetyPolicy }): number {\n const { timeout } = opts.policy ?? {};\n const milliseconds = timeout ? DurationMultipliers.toMilliseconds(timeout) : null;\n return milliseconds ?? UrlSafetyDefaults.TIMEOUT_MILLISECONDS;\n }\n\n /**\n * A GET that an untrusted url cannot steer inside the network. Redirects are followed by hand so\n * every hop is re-checked - `redirect: 'follow'` would land on an internal host with no second\n * look.\n *\n * The body is returned unread: the caller decides whether to stream it onward or cap it with\n * {@link UrlIngest.readCappedBody}. Nothing is buffered here.\n */\n static async fetchGuarded(opts: {\n url: string;\n policy?: IUrlSafetyPolicy;\n headers?: Record<string, string>;\n }): Promise<Response> {\n const { url, policy, headers } = opts;\n const maxRedirects = policy?.maxRedirects ?? UrlSafetyDefaults.MAX_REDIRECTS;\n const timeoutMilliseconds = UrlIngest.readTimeoutMilliseconds({ policy });\n\n let target = await UrlIngest.assertReachableUrl({ url, policy });\n\n for (let hop = 0; hop <= maxRedirects; hop += 1) {\n const response = await fetch(target, {\n redirect: 'manual',\n signal: AbortSignal.timeout(timeoutMilliseconds),\n ...(headers ? { headers } : {}),\n });\n\n if (response.status < 300 || response.status > 399) {\n if (!response.ok) {\n // Cancelled, or the socket stays open on every 4xx and 5xx.\n await response.body?.cancel();\n throw getError({\n message: `[fetchGuarded] Request failed | status: ${response.status} | url: ${url}`,\n });\n }\n\n return response;\n }\n\n const location = response.headers.get('location');\n await response.body?.cancel();\n\n if (!location) {\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[fetchGuarded] Redirect carried no location | url: ${url}`,\n });\n }\n\n const result = await UrlIngest.assertReachableUrl({\n url: new URL(location, target).toString(),\n policy,\n });\n target = result;\n }\n\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[fetchGuarded] Too many redirects | max: ${maxRedirects} | url: ${url}`,\n });\n }\n\n /**\n * Enforces `maxBytes` on a stream nothing reads into this process - the shape a caller wants when\n * the body is piped onward rather than held. Without it a `maxBytes` policy is inert on that path.\n */\n static capStream(opts: {\n source: ReadableStream<Uint8Array>;\n policy?: IUrlSafetyPolicy;\n }): ReadableStream<Uint8Array> {\n const { source } = opts;\n const maxBytes = opts.policy?.maxBytes ?? UrlSafetyDefaults.MAX_BYTES;\n let received = 0;\n\n return source.pipeThrough(\n new TransformStream<Uint8Array, Uint8Array>({\n transform(chunk, controller) {\n received += chunk.byteLength;\n\n if (received > maxBytes) {\n controller.error(\n getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[capStream] Body exceeds the cap | received: ${received} | max: ${maxBytes}`,\n }),\n );\n return;\n }\n\n controller.enqueue(chunk);\n },\n }),\n );\n }\n\n /**\n * Reads a body that must not exceed `maxBytes`, cancelling the moment it does. `content-length` is\n * checked first because it is free, and counted anyway because it may be absent or lie.\n */\n static async readCappedBody(opts: {\n response: Response;\n policy?: IUrlSafetyPolicy;\n }): Promise<Buffer> {\n const { response, policy } = opts;\n const maxBytes = policy?.maxBytes ?? UrlSafetyDefaults.MAX_BYTES;\n\n const declaredLength = Number(response.headers.get('content-length') ?? 0);\n if (declaredLength > maxBytes) {\n await response.body?.cancel();\n // The same fact as the streaming branch below, so the same verdict: a caller must not retry an\n // oversized body when the server sends `content-length` and drop it when the server omits one.\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[readCappedBody] Body exceeds the cap | declared: ${declaredLength} | max: ${maxBytes}`,\n });\n }\n\n const reader = response.body?.getReader();\n if (!reader) {\n throw getError({ message: '[readCappedBody] Response carried no body' });\n }\n\n const chunks: Uint8Array[] = [];\n let received = 0;\n\n for (;;) {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n\n received += value.length;\n if (received > maxBytes) {\n await reader.cancel();\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[readCappedBody] Body exceeds the cap | received: ${received} | max: ${maxBytes}`,\n });\n }\n\n chunks.push(value);\n }\n\n return Buffer.concat(chunks);\n }\n}\n"]}
|
|
@@ -23,6 +23,16 @@ export declare class UrlPolicy {
|
|
|
23
23
|
url: string;
|
|
24
24
|
policy?: IUrlSafetyPolicy;
|
|
25
25
|
}): URL;
|
|
26
|
+
/**
|
|
27
|
+
* True when the url will never be allowed, whatever happens next: a bad scheme, a non-public
|
|
28
|
+
* address, a host outside the allow-list, a malformed url, or too many hops. Retrying any of those
|
|
29
|
+
* repeats the same rejection - drop the work instead.
|
|
30
|
+
*
|
|
31
|
+
* A timeout, a DNS failure or a 5xx answers `false`: those are worth another attempt.
|
|
32
|
+
*/
|
|
33
|
+
static isRefusedError(opts: {
|
|
34
|
+
error: unknown;
|
|
35
|
+
}): boolean;
|
|
26
36
|
}
|
|
27
37
|
export { UrlSafetyDefaults, UrlSchemes };
|
|
28
38
|
export type { IUrlSafetyPolicy };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"policy.d.ts","sourceRoot":"","sources":["../../../../../../src/modules/network/utilities/url-safety/policy.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAEnE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,+EAA+E;AAC/E,qBAAa,SAAS;IACpB,4GAA4G;IAC5G,OAAO,CAAC,MAAM,CAAC,eAAe;IA0B9B;;;OAGG;IACH,MAAM,CAAC,kBAAkB,CAAC,IAAI,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO;IAoC7D,6FAA6F;IAC7F,MAAM,CAAC,eAAe,CAAC,IAAI,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM;IAK1D;;;OAGG;IACH,MAAM,CAAC,aAAa,CAAC,IAAI,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,gBAAgB,CAAA;KAAE,GAAG,GAAG;
|
|
1
|
+
{"version":3,"file":"policy.d.ts","sourceRoot":"","sources":["../../../../../../src/modules/network/utilities/url-safety/policy.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAEnE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,+EAA+E;AAC/E,qBAAa,SAAS;IACpB,4GAA4G;IAC5G,OAAO,CAAC,MAAM,CAAC,eAAe;IA0B9B;;;OAGG;IACH,MAAM,CAAC,kBAAkB,CAAC,IAAI,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO;IAoC7D,6FAA6F;IAC7F,MAAM,CAAC,eAAe,CAAC,IAAI,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM;IAK1D;;;OAGG;IACH,MAAM,CAAC,aAAa,CAAC,IAAI,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,gBAAgB,CAAA;KAAE,GAAG,GAAG;IA2C3E;;;;;;OAMG;IACH,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE;QAAE,KAAK,EAAE,OAAO,CAAA;KAAE,GAAG,OAAO;CAIzD;AAED,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,CAAC;AACzC,YAAY,EAAE,gBAAgB,EAAE,CAAC"}
|
|
@@ -104,6 +104,17 @@ export class UrlPolicy {
|
|
|
104
104
|
}
|
|
105
105
|
return parsed;
|
|
106
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* True when the url will never be allowed, whatever happens next: a bad scheme, a non-public
|
|
109
|
+
* address, a host outside the allow-list, a malformed url, or too many hops. Retrying any of those
|
|
110
|
+
* repeats the same rejection - drop the work instead.
|
|
111
|
+
*
|
|
112
|
+
* A timeout, a DNS failure or a 5xx answers `false`: those are worth another attempt.
|
|
113
|
+
*/
|
|
114
|
+
static isRefusedError(opts) {
|
|
115
|
+
const source = opts.error;
|
|
116
|
+
return source?.normalized?.code === UrlSafetyErrors.URL_REFUSED.message.code;
|
|
117
|
+
}
|
|
107
118
|
}
|
|
108
119
|
export { UrlSafetyDefaults, UrlSchemes };
|
|
109
120
|
//# sourceMappingURL=policy.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"policy.js","sourceRoot":"","sources":["../../../../../../src/modules/network/utilities/url-safety/policy.ts"],"names":[],"mappings":"AAAA,8FAA8F;A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AW;gBAClC,OAAO,EAAE,wCAAwC,GAAG,EAAE;aACvD,CAAC,CAAC;QACL,CAAC;QAED,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC9C,MAAM,QAAQ,CAAC;gBACb,KAAK,EAAE,eAAe,CAAC,WAAW;gBAClC,OAAO,EAAE,+CAA+C,MAAM,CAAC,QAAQ,eAAe,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;aAClH,CAAC,CAAC;QACL,CAAC;QAED,IAAI,MAAM,EAAE,YAAY,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC3E,MAAM,QAAQ,CAAC;gBACb,KAAK,EAAE,eAAe,CAAC,WAAW;gBAClC,OAAO,EAAE,yDAAyD,MAAM,CAAC,QAAQ,EAAE;aACpF,CAAC,CAAC;QACL,CAAC;QAED,MAAM,OAAO,GAAG,SAAS,CAAC,eAAe,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC;QACzE,MAAM,gBAAgB,GAAG,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QAE3E,IAAI,CAAC,mBAAmB,IAAI,gBAAgB,IAAI,SAAS,CAAC,kBAAkB,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC;YAC1F,MAAM,QAAQ,CAAC;gBACb,KAAK,EAAE,eAAe,CAAC,WAAW;gBAClC,OAAO,EAAE,iEAAiE,OAAO,EAAE;aACpF,CAAC,CAAC;QACL,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AAED,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,CAAC","sourcesContent":["// The browser-pure half of the guard: a url and a literal address are decided with string and\n// number work alone. Resolving a hostname needs `node:dns` and lives in `ingest.ts`, which this\n// file must never import - `policy.ts` is re-exported from the `./core` subpath and is gated by\n// `make purity`.\n\nimport { getError } from '@/modules/error';\nimport { UrlSafetyDefaults, UrlSchemes } from './common/constants';\nimport { UrlSafetyErrors } from './common/errors';\nimport type { IUrlSafetyPolicy } from './common/types';\n\n/** Everything about a url that can be decided without touching the network. */\nexport class UrlPolicy {\n /** A dotted-quad that is not routable on the public internet. An unparseable input counts as non-public. */\n private static isNonPublicIPv4(opts: { address: string }): boolean {\n const parts = opts.address.split('.').map(Number);\n\n if (\n parts.length !== 4 ||\n parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)\n ) {\n return true;\n }\n\n const [first, second] = parts;\n\n return (\n first === 0 || // this host\n first === 10 || // private\n first === 127 || // loopback\n (first === 100 && second >= 64 && second <= 127) || // carrier-grade NAT\n (first === 169 && second === 254) || // link-local, and the cloud metadata address\n (first === 172 && second >= 16 && second <= 31) || // private\n (first === 192 && second === 0) || // IETF protocol assignments\n (first === 192 && second === 168) || // private\n (first === 198 && (second === 18 || second === 19)) || // benchmarking\n first >= 224 // multicast and reserved\n );\n }\n\n /**\n * An address that must never be reached with an untrusted url. Anything unrecognised answers\n * `true`: a guard that fails open on a string it cannot parse is not a guard.\n */\n static isNonPublicAddress(opts: { address: string }): boolean {\n const normalized = opts.address.toLowerCase().split('%')[0];\n\n if (!normalized.includes(':')) {\n return UrlPolicy.isNonPublicIPv4({ address: normalized });\n }\n\n // An IPv4 address wearing an IPv6 coat walks past every naive v6 check. Both spellings must be\n // unwrapped: `new URL` rewrites `::ffff:169.254.169.254` into the hex form `::ffff:a9fe:a9fe`,\n // so a dotted-quad-only check is green on a value the parser never actually produces.\n const dottedMapped = /^::ffff:(\\d+\\.\\d+\\.\\d+\\.\\d+)$/.exec(normalized)?.[1];\n if (dottedMapped) {\n return UrlPolicy.isNonPublicIPv4({ address: dottedMapped });\n }\n\n const hexMapped = /^::ffff:([\\da-f]{1,4}):([\\da-f]{1,4})$/.exec(normalized);\n if (hexMapped) {\n const high = Number.parseInt(hexMapped[1], 16);\n const low = Number.parseInt(hexMapped[2], 16);\n const dotted = [high >> 8, high & 0xff, low >> 8, low & 0xff].join('.');\n\n return UrlPolicy.isNonPublicIPv4({ address: dotted });\n }\n\n return (\n normalized === '::' ||\n normalized === '::1' ||\n normalized.startsWith('fc') || // unique local\n normalized.startsWith('fd') ||\n normalized.startsWith('fe8') || // link-local\n normalized.startsWith('fe9') ||\n normalized.startsWith('fea') ||\n normalized.startsWith('feb')\n );\n }\n\n /** A bracketed IPv6 literal arrives as `[::1]`; the brackets are not part of the address. */\n static readHostAddress(opts: { hostname: string }): string {\n const { hostname } = opts;\n return hostname.startsWith('[') && hostname.endsWith(']') ? hostname.slice(1, -1) : hostname;\n }\n\n /**\n * A hostname that RESOLVES to a private address passes here on purpose - `UrlIngest` catches that,\n * and this half runs where `node:dns` does not exist.\n */\n static assertSafeUrl(opts: { url: string; policy?: IUrlSafetyPolicy }): URL {\n const { url, policy } = opts;\n const allowedSchemes = policy?.allowedSchemes ?? UrlSafetyDefaults.ALLOWED_SCHEMES;\n const allowPrivateAddress =\n policy?.allowPrivateAddress ?? UrlSafetyDefaults.ALLOW_PRIVATE_ADDRESS;\n\n let parsed: URL;\n try {\n parsed = new URL(url);\n } catch {\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[assertSafeUrl] Malformed url | url: ${url}`,\n });\n }\n\n if (!allowedSchemes.includes(parsed.protocol)) {\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[assertSafeUrl] Disallowed scheme | scheme: ${parsed.protocol} | allowed: ${allowedSchemes.join(', ')}`,\n });\n }\n\n if (policy?.allowedHosts && !policy.allowedHosts.includes(parsed.hostname)) {\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[assertSafeUrl] Host is not in the allow-list | host: ${parsed.hostname}`,\n });\n }\n\n const address = UrlPolicy.readHostAddress({ hostname: parsed.hostname });\n const isLiteralAddress = /^[\\d.]+$/.test(address) || address.includes(':');\n\n if (!allowPrivateAddress && isLiteralAddress && UrlPolicy.isNonPublicAddress({ address })) {\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[assertSafeUrl] Url points at a non-public address | address: ${address}`,\n });\n }\n\n return parsed;\n }\n}\n\nexport { UrlSafetyDefaults, UrlSchemes };\nexport type { IUrlSafetyPolicy };\n"]}
|
|
1
|
+
{"version":3,"file":"policy.js","sourceRoot":"","sources":["../../../../../../src/modules/network/utilities/url-safety/policy.ts"],"names":[],"mappings":"AAAA,8FAA8F;AAC9F,gGAAgG;AAChG,gGAAgG;AAChG,iBAAiB;AAEjB,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AACnE,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAGlD,+EAA+E;AAC/E,MAAM,OAAO,SAAS;IACpB,4GAA4G;IACpG,MAAM,CAAC,eAAe,CAAC,IAAyB;QACtD,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAElD,IACE,KAAK,CAAC,MAAM,KAAK,CAAC;YAClB,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,IAAI,IAAI,GAAG,GAAG,CAAC,EACvE,CAAC;YACD,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,KAAK,CAAC;QAE9B,OAAO,CACL,KAAK,KAAK,CAAC,IAAI,YAAY;YAC3B,KAAK,KAAK,EAAE,IAAI,UAAU;YAC1B,KAAK,KAAK,GAAG,IAAI,WAAW;YAC5B,CAAC,KAAK,KAAK,GAAG,IAAI,MAAM,IAAI,EAA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E,OAAO,EAAE,CAAC,EAAE,CAAC;YAC1F,MAAM,QAAQ,CAAC;gBACb,KAAK,EAAE,eAAe,CAAC,WAAW;gBAClC,OAAO,EAAE,iEAAiE,OAAO,EAAE;aACpF,CAAC,CAAC;QACL,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IAED;;;;;;OAMG;IACH,MAAM,CAAC,cAAc,CAAC,IAAwB;QAC5C,MAAM,MAAM,GAAG,IAAI,CAAC,KAA2C,CAAC;QAChE,OAAO,MAAM,EAAE,UAAU,EAAE,IAAI,KAAK,eAAe,CAAC,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC;IAC/E,CAAC;CACF;AAED,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,CAAC","sourcesContent":["// The browser-pure half of the guard: a url and a literal address are decided with string and\n// number work alone. Resolving a hostname needs `node:dns` and lives in `ingest.ts`, which this\n// file must never import - `policy.ts` is re-exported from the `./core` subpath and is gated by\n// `make purity`.\n\nimport { getError } from '@/modules/error';\nimport { UrlSafetyDefaults, UrlSchemes } from './common/constants';\nimport { UrlSafetyErrors } from './common/errors';\nimport type { IUrlSafetyPolicy } from './common/types';\n\n/** Everything about a url that can be decided without touching the network. */\nexport class UrlPolicy {\n /** A dotted-quad that is not routable on the public internet. An unparseable input counts as non-public. */\n private static isNonPublicIPv4(opts: { address: string }): boolean {\n const parts = opts.address.split('.').map(Number);\n\n if (\n parts.length !== 4 ||\n parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)\n ) {\n return true;\n }\n\n const [first, second] = parts;\n\n return (\n first === 0 || // this host\n first === 10 || // private\n first === 127 || // loopback\n (first === 100 && second >= 64 && second <= 127) || // carrier-grade NAT\n (first === 169 && second === 254) || // link-local, and the cloud metadata address\n (first === 172 && second >= 16 && second <= 31) || // private\n (first === 192 && second === 0) || // IETF protocol assignments\n (first === 192 && second === 168) || // private\n (first === 198 && (second === 18 || second === 19)) || // benchmarking\n first >= 224 // multicast and reserved\n );\n }\n\n /**\n * An address that must never be reached with an untrusted url. Anything unrecognised answers\n * `true`: a guard that fails open on a string it cannot parse is not a guard.\n */\n static isNonPublicAddress(opts: { address: string }): boolean {\n const normalized = opts.address.toLowerCase().split('%')[0];\n\n if (!normalized.includes(':')) {\n return UrlPolicy.isNonPublicIPv4({ address: normalized });\n }\n\n // An IPv4 address wearing an IPv6 coat walks past every naive v6 check. Both spellings must be\n // unwrapped: `new URL` rewrites `::ffff:169.254.169.254` into the hex form `::ffff:a9fe:a9fe`,\n // so a dotted-quad-only check is green on a value the parser never actually produces.\n const dottedMapped = /^::ffff:(\\d+\\.\\d+\\.\\d+\\.\\d+)$/.exec(normalized)?.[1];\n if (dottedMapped) {\n return UrlPolicy.isNonPublicIPv4({ address: dottedMapped });\n }\n\n const hexMapped = /^::ffff:([\\da-f]{1,4}):([\\da-f]{1,4})$/.exec(normalized);\n if (hexMapped) {\n const high = Number.parseInt(hexMapped[1], 16);\n const low = Number.parseInt(hexMapped[2], 16);\n const dotted = [high >> 8, high & 0xff, low >> 8, low & 0xff].join('.');\n\n return UrlPolicy.isNonPublicIPv4({ address: dotted });\n }\n\n return (\n normalized === '::' ||\n normalized === '::1' ||\n normalized.startsWith('fc') || // unique local\n normalized.startsWith('fd') ||\n normalized.startsWith('fe8') || // link-local\n normalized.startsWith('fe9') ||\n normalized.startsWith('fea') ||\n normalized.startsWith('feb')\n );\n }\n\n /** A bracketed IPv6 literal arrives as `[::1]`; the brackets are not part of the address. */\n static readHostAddress(opts: { hostname: string }): string {\n const { hostname } = opts;\n return hostname.startsWith('[') && hostname.endsWith(']') ? hostname.slice(1, -1) : hostname;\n }\n\n /**\n * A hostname that RESOLVES to a private address passes here on purpose - `UrlIngest` catches that,\n * and this half runs where `node:dns` does not exist.\n */\n static assertSafeUrl(opts: { url: string; policy?: IUrlSafetyPolicy }): URL {\n const { url, policy } = opts;\n const allowedSchemes = policy?.allowedSchemes ?? UrlSafetyDefaults.ALLOWED_SCHEMES;\n const allowPrivateAddress =\n policy?.allowPrivateAddress ?? UrlSafetyDefaults.ALLOW_PRIVATE_ADDRESS;\n\n let parsed: URL;\n try {\n parsed = new URL(url);\n } catch {\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[assertSafeUrl] Malformed url | url: ${url}`,\n });\n }\n\n if (!allowedSchemes.includes(parsed.protocol)) {\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[assertSafeUrl] Disallowed scheme | scheme: ${parsed.protocol} | allowed: ${allowedSchemes.join(', ')}`,\n });\n }\n\n if (policy?.allowedHosts && !policy.allowedHosts.includes(parsed.hostname)) {\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[assertSafeUrl] Host is not in the allow-list | host: ${parsed.hostname}`,\n });\n }\n\n const address = UrlPolicy.readHostAddress({ hostname: parsed.hostname });\n const isLiteralAddress = /^[\\d.]+$/.test(address) || address.includes(':');\n\n if (!allowPrivateAddress && isLiteralAddress && UrlPolicy.isNonPublicAddress({ address })) {\n throw getError({\n error: UrlSafetyErrors.URL_REFUSED,\n message: `[assertSafeUrl] Url points at a non-public address | address: ${address}`,\n });\n }\n\n return parsed;\n }\n\n /**\n * True when the url will never be allowed, whatever happens next: a bad scheme, a non-public\n * address, a host outside the allow-list, a malformed url, or too many hops. Retrying any of those\n * repeats the same rejection - drop the work instead.\n *\n * A timeout, a DNS failure or a 5xx answers `false`: those are worth another attempt.\n */\n static isRefusedError(opts: { error: unknown }): boolean {\n const source = opts.error as { normalized?: { code?: string } };\n return source?.normalized?.code === UrlSafetyErrors.URL_REFUSED.message.code;\n }\n}\n\nexport { UrlSafetyDefaults, UrlSchemes };\nexport type { IUrlSafetyPolicy };\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"abstract.d.ts","sourceRoot":"","sources":["../../../../src/modules/pool/abstract.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACrE,OAAO,EAA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;YACvF,aAAa;IAa3B,mJAAmJ;IACnJ,OAAO,CAAC,YAAY;IAwBpB,sGAAsG;IACtG,OAAO,CAAC,cAAc;IAQtB,OAAO,CAAC,gBAAgB;CAOzB"}
|
|
@@ -201,7 +201,8 @@ export class AbstractPoolHelper extends BaseHelper {
|
|
|
201
201
|
const logger = this.logger.for(this.takeIdleResource.name);
|
|
202
202
|
let isValid;
|
|
203
203
|
try {
|
|
204
|
-
|
|
204
|
+
const result = await this.validateResource({ resource });
|
|
205
|
+
isValid = result;
|
|
205
206
|
}
|
|
206
207
|
catch (error) {
|
|
207
208
|
// A throwing validate must NOT leak the popped candidate (it would corrupt `total` and can permanently starve the pool); treat it like an invalid resource - discard and retry.
|
|
@@ -227,7 +228,8 @@ export class AbstractPoolHelper extends BaseHelper {
|
|
|
227
228
|
async createAndHand() {
|
|
228
229
|
let resource;
|
|
229
230
|
try {
|
|
230
|
-
|
|
231
|
+
const result = await this.createResource();
|
|
232
|
+
resource = result;
|
|
231
233
|
}
|
|
232
234
|
catch (error) {
|
|
233
235
|
this.takeNextWaiter()?.reject(error);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"abstract.js","sourceRoot":"","sources":["../../../../src/modules/pool/abstract.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAG5D,4SAA4S;AAC5S,MAAM,OAAgB,kBAAsB,SAAQ,UAAU;IAa5D,YAAY,IAAyB;QACnC,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,IAAI,gBAAgB,EAAE,CAAC,CAAC;QATlC,SAAI,GAAQ,EAAE,CAAC;QACf,aAAQ,GAAG,IAAI,GAAG,EAAK,CAAC;QACxB,gBAAW,GAAG,IAAI,aAAa,EAAkB,CAAC;QAE3D,UAA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ules/base';\nimport { getError } from '@/modules/error';\nimport { HfQueueHelper } from '@/modules/queue/internal/hf';\nimport { type IPool, type IPoolControlOptions, type IPoolStats, type IPoolWaiter } from './common';\n\n/** Single-borrower pool skeleton: subclasses override {@link create} (required) and optionally validateResource/resetResource/onDestroyResource. All resource-waiter pairing flows through ONE re-entrancy-guarded `dispatch()` loop; the waiter queue is {@link HfQueueHelper} - O(1), no shift/splice. */\nexport abstract class AbstractPoolHelper<T> extends BaseHelper implements IPool<T> {\n protected readonly size: number;\n protected readonly acquireTimeoutMs?: number;\n protected readonly maxWaitingClients?: number;\n\n private readonly idle: T[] = [];\n private readonly borrowed = new Set<T>();\n private readonly waiterQueue = new HfQueueHelper<IPoolWaiter<T>>();\n\n private total = 0; // live or being-created resources\n private isDispatching = false;\n private isDestroyed = false;\n\n constructor(opts: IPoolControlOptions) {\n super({ scope: opts.scope ?? 'BasePoolHelper' });\n this.size = opts.size;\n this.acquireTimeoutMs = opts.acquireTimeoutMs;\n this.maxWaitingClients = opts.maxWaitingClients;\n }\n\n // ----------------------------------------------------------------------------------------------------------\n\n /** Create one resource. Required. */\n protected abstract create(): ValueOrPromise<T>;\n\n /** Validate an idle resource just before hand-out; false → destroyed + replaced. Default: always valid. */\n protected validateResource(_opts: { resource: T }): ValueOrPromise<boolean> {\n return true;\n }\n\n /** Reset an idle resource before hand-out (e.g. clear state). Throw → resource discarded. Default: no-op. */\n protected resetResource(_opts: { resource: T }): ValueOrPromise<void> {\n return undefined;\n }\n\n /** Tear down a resource being removed from the pool. Default: no-op. */\n protected onDestroyResource(_opts: { resource: T }): ValueOrPromise<void> {\n return undefined;\n }\n\n // ----------------------------------------------------------------------------------------------------------\n\n getStats(): IPoolStats {\n return {\n size: this.size,\n available: this.idle.length,\n borrowed: this.borrowed.size,\n pending: this.waiterQueue.size,\n };\n }\n\n async acquire(): Promise<T> {\n if (this.isDestroyed) {\n throw getError({ message: '[acquire] Pool has been destroyed.' });\n }\n\n if (this.maxWaitingClients !== undefined && this.maxWaitingClients >= 0) {\n // Only reject when this acquire would actually have to WAIT: no idle resource and the pool at capacity. With a free/creatable resource the caller is served, so maxWaitingClients:0 means \"serve if available, never queue\" rather than \"reject everything\".\n const isWaitRequired = !this.idle.length && this.total >= this.size;\n\n if (isWaitRequired && this.waiterQueue.size >= this.maxWaitingClients) {\n throw getError({\n message: `[acquire] Too many waiting clients (>= ${this.maxWaitingClients}).`,\n });\n }\n }\n\n const promise = new Promise<T>((resolve, reject) => {\n const waiter: IPoolWaiter<T> = { resolve, reject, timer: null };\n const node = this.waiterQueue.enqueue({ value: waiter });\n\n if (this.acquireTimeoutMs !== undefined && this.acquireTimeoutMs >= 0) {\n waiter.timer = setTimeout(() => {\n this.waiterQueue.cancel({ node }); // O(1) — dispatch skips it when reached\n\n reject(\n getError({\n message: `[acquire] Acquire timed out after ${this.acquireTimeoutMs}ms.`,\n }),\n );\n }, this.acquireTimeoutMs);\n }\n });\n\n this.safeDispatch({ action: 'acquire' });\n return promise;\n }\n\n release(opts: { resource: T }): void {\n const { resource } = opts;\n const logger = this.logger.for(this.release.name);\n\n if (!this.borrowed.has(resource)) {\n logger.warn('Called with a foreign/already-released resource; ignoring.');\n return;\n }\n this.borrowed.delete(resource);\n\n if (this.isDestroyed) {\n this.destroyResource({ resource }).catch((error) => {\n logger.warn('destroyResource error on release: %s', error);\n });\n return;\n }\n\n this.idle.push(resource);\n this.safeDispatch({ action: 'release' });\n }\n\n async warmup(): Promise<void> {\n if (this.isDestroyed) {\n throw getError({ message: '[warmup] Pool has been destroyed.' });\n }\n\n // Create sequentially, re-checking the live `total` before each create so the `size` cap holds under concurrent warmup()/acquire(); createResource() bumps `total` synchronously, before its await, so a re-check observes in-flight creations from other callers - no overshoot.\n while (this.total < this.size) {\n const resource = await this.createResource();\n this.idle.push(resource);\n }\n\n this.safeDispatch({ action: 'warmup' });\n }\n\n async use<R>(opts: { execution: (resource: T) => ValueOrPromise<R> }): Promise<R> {\n const resource = await this.acquire();\n\n try {\n const result = await opts.execution(resource);\n this.release({ resource });\n return result;\n } catch (error) {\n this.logger\n .for(this.use.name)\n .error('execution threw → release skipped, force DISCARD | Error: %s', error);\n await this.discard({ resource });\n throw error;\n }\n }\n\n async discard(opts: { resource: T }): Promise<void> {\n const { resource } = opts;\n\n if (!this.borrowed.delete(resource)) {\n this.logger.for(this.discard.name).warn('Called with a non-borrowed resource; ignoring.');\n return;\n }\n\n await this.destroyResource({ resource });\n this.safeDispatch({ action: 'discard' });\n }\n\n async destroy(): Promise<void> {\n this.isDestroyed = true;\n\n const error = getError({ message: '[destroy] Pool destroyed while awaiting a resource.' });\n const draineds = this.waiterQueue.drain();\n for (const waiter of draineds) {\n this.clearWaiterTimer({ waiter });\n waiter.reject(error);\n }\n\n const toDestroy = this.idle.splice(0);\n await Promise.all(toDestroy.map((resource) => this.destroyResource({ resource })));\n }\n\n // ----------------------------------------------------------------------------------------------------------\n\n private async createResource(): Promise<T> {\n this.total += 1; // bump BEFORE await so concurrent dispatch/warmup see the in-flight create (no overshoot)\n\n try {\n const resource = await this.create();\n return resource;\n } catch (error) {\n this.total -= 1;\n this.logger\n .for(this.createResource.name)\n .error('Error creating pool resource | Error: %s', error);\n throw error;\n }\n }\n\n private async destroyResource(opts: { resource: T }): Promise<void> {\n this.total -= 1;\n\n try {\n await this.onDestroyResource(opts);\n } catch (error) {\n this.logger.for(this.destroyResource.name).warn('destroy hook threw: %s', error);\n }\n }\n\n // ----------------------------------------------------------------------------------------------------------\n\n /** Fire-and-forget dispatch with logged rejection; shared by all dispatch call sites. */\n private safeDispatch(opts: { action: string }): void {\n this.dispatch().catch((error) => {\n this.logger\n .for(this.dispatch.name)\n .warn('dispatch error | action: %s | Error: %s', opts.action, error);\n });\n }\n\n /** Pair servable resources with waiting acquirers until the queue drains or the pool is exhausted. Re-entrancy-guarded so only one loop runs at a time. */\n private async dispatch() {\n if (this.isDispatching) {\n return;\n }\n\n this.isDispatching = true;\n\n try {\n while (this.waiterQueue.size > 0) {\n // Prefer a warm idle resource (LIFO) over creating a new one.\n if (this.idle.length > 0) {\n const resource = await this.takeIdleResource();\n if (resource === null) {\n continue; // bad idle resource was discarded → try the next one\n }\n\n if (!this.handToWaiter({ resource })) {\n return;\n }\n continue;\n }\n\n if (this.total < this.size) {\n await this.createAndHand();\n continue;\n }\n\n return;\n }\n } catch (error) {\n this.logger\n .for(this.dispatch.name)\n .error('[dispatch] Error while dispatching pool element | Error: %s', error);\n } finally {\n this.isDispatching = false;\n }\n }\n\n /** Pop an idle resource and validate+reset it; on ANY failure destroy it and return null (caller retries). */\n private async takeIdleResource(): Promise<T | null> {\n const resource = this.idle.pop() as T;\n const logger = this.logger.for(this.takeIdleResource.name);\n\n let isValid: boolean;\n try {\n isValid = await this.validateResource({ resource });\n } catch (error) {\n // A throwing validate must NOT leak the popped candidate (it would corrupt `total` and can permanently starve the pool); treat it like an invalid resource - discard and retry.\n logger.warn('validate hook threw; discarding resource: %s', error);\n await this.destroyResource({ resource });\n return null;\n }\n\n if (!isValid) {\n await this.destroyResource({ resource });\n return null;\n }\n\n try {\n await this.resetResource({ resource });\n } catch (error) {\n logger.warn('reset hook threw; discarding resource: %s', error);\n await this.destroyResource({ resource });\n return null;\n }\n\n return resource;\n }\n\n /** Create a fresh resource and hand it to the next waiter; on create failure, reject that waiter. */\n private async createAndHand(): Promise<void> {\n let resource: T;\n try {\n resource = await this.createResource();\n } catch (error) {\n this.takeNextWaiter()?.reject(error);\n return;\n }\n\n this.handToWaiter({ resource });\n }\n\n /** Give a ready resource to the next live waiter; returns false and parks it in idle if the waiter raced away (e.g. timed out during an await). */\n private handToWaiter(opts: { resource: T }): boolean {\n const { resource } = opts;\n\n // `destroy()` tears down what it can SEE - the waiter queue and the idle list. A resource whose create() was still in flight is in neither, so parking it in idle now would leave it alive with nothing left to reclaim it.\n if (this.isDestroyed) {\n this.destroyResource({ resource }).catch((error) => {\n this.logger\n .for(this.handToWaiter.name)\n .warn('destroyResource error on a resource created after destroy(): %s', error);\n });\n return false;\n }\n\n const waiter = this.takeNextWaiter();\n if (!waiter) {\n this.idle.push(resource);\n return false;\n }\n\n this.borrowed.add(resource);\n waiter.resolve(resource);\n return true;\n }\n\n /** Dequeue the next live waiter and disarm its acquire-timeout so it cannot fire after settlement. */\n private takeNextWaiter(): TNullable<IPoolWaiter<T>> {\n const waiter = this.waiterQueue.dequeue();\n if (waiter) {\n this.clearWaiterTimer({ waiter });\n }\n return waiter;\n }\n\n private clearWaiterTimer(opts: { waiter: IPoolWaiter<T> }): void {\n const { waiter } = opts;\n if (waiter.timer) {\n clearTimeout(waiter.timer);\n waiter.timer = null;\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"abstract.js","sourceRoot":"","sources":["../../../../src/modules/pool/abstract.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAG5D,4SAA4S;AAC5S,MAAM,OAAgB,kBAAsB,SAAQ,UAAU;IAa5D,YAAY,IAAyB;QACnC,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,IAAI,gBAAgB,EAAE,CAAC,CAAC;QATlC,SAAI,GAAQ,EAAE,CAAC;QACf,aAAQ,GAAG,IAAI,GAAG,EAAK,CAAC;QACxB,gBAAW,GAAG,IAAI,aAAa,EAAkB,CAAC;QAE3D,UAAK,GAAG,CAAC,CAAC,CAAC,kCAAkC;QAC7C,kBAAa,GAAG,KAAK,CAAC;QACtB,gBAAW,GAAG,KAAK,CAAC;QAI1B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACtB,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC,gBAAgB,CAAC;QAC9C,IAAI,CAAC,iBAAiB,GAAG,IAAI,CAAC,iBAAiB,CAAC;IAClD,CAAC;IAOD,2GAA2G;IAC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CAAC,YAAY,CAAC,IAAI,CAAC;qBAC3B,IAAI,CAAC,iEAAiE,EAAE,KAAK,CAAC,CAAC;YACpF,CAAC,CAAC,CAAC;YACH,OAAO,KAAK,CAAC;QACf,CAAC;QAED,MAAM,MAAM,GAAG,IAAI,CAAC,cAAc,EAAE,CAAC;QACrC,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YACzB,OAAO,KAAK,CAAC;QACf,CAAC;QAED,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC5B,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QACzB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,sGAAsG;IAC9F,cAAc;QACpB,MAAM,MAAM,GAAG,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,CAAC;QAC1C,IAAI,MAAM,EAAE,CAAC;YACX,IAAI,CAAC,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;QACpC,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;IAEO,gBAAgB,CAAC,IAAgC;QACvD,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;QACxB,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;YACjB,YAAY,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAC3B,MAAM,CAAC,KAAK,GAAG,IAAI,CAAC;QACtB,CAAC;IACH,CAAC;CACF","sourcesContent":["import { type TNullable, type ValueOrPromise } from '@/common/types';\nimport { BaseHelper } from '@/modules/base';\nimport { getError } from '@/modules/error';\nimport { HfQueueHelper } from '@/modules/queue/internal/hf';\nimport { type IPool, type IPoolControlOptions, type IPoolStats, type IPoolWaiter } from './common';\n\n/** Single-borrower pool skeleton: subclasses override {@link create} (required) and optionally validateResource/resetResource/onDestroyResource. All resource-waiter pairing flows through ONE re-entrancy-guarded `dispatch()` loop; the waiter queue is {@link HfQueueHelper} - O(1), no shift/splice. */\nexport abstract class AbstractPoolHelper<T> extends BaseHelper implements IPool<T> {\n protected readonly size: number;\n protected readonly acquireTimeoutMs?: number;\n protected readonly maxWaitingClients?: number;\n\n private readonly idle: T[] = [];\n private readonly borrowed = new Set<T>();\n private readonly waiterQueue = new HfQueueHelper<IPoolWaiter<T>>();\n\n private total = 0; // live or being-created resources\n private isDispatching = false;\n private isDestroyed = false;\n\n constructor(opts: IPoolControlOptions) {\n super({ scope: opts.scope ?? 'BasePoolHelper' });\n this.size = opts.size;\n this.acquireTimeoutMs = opts.acquireTimeoutMs;\n this.maxWaitingClients = opts.maxWaitingClients;\n }\n\n // ----------------------------------------------------------------------------------------------------------\n\n /** Create one resource. Required. */\n protected abstract create(): ValueOrPromise<T>;\n\n /** Validate an idle resource just before hand-out; false → destroyed + replaced. Default: always valid. */\n protected validateResource(_opts: { resource: T }): ValueOrPromise<boolean> {\n return true;\n }\n\n /** Reset an idle resource before hand-out (e.g. clear state). Throw → resource discarded. Default: no-op. */\n protected resetResource(_opts: { resource: T }): ValueOrPromise<void> {\n return undefined;\n }\n\n /** Tear down a resource being removed from the pool. Default: no-op. */\n protected onDestroyResource(_opts: { resource: T }): ValueOrPromise<void> {\n return undefined;\n }\n\n // ----------------------------------------------------------------------------------------------------------\n\n getStats(): IPoolStats {\n return {\n size: this.size,\n available: this.idle.length,\n borrowed: this.borrowed.size,\n pending: this.waiterQueue.size,\n };\n }\n\n async acquire(): Promise<T> {\n if (this.isDestroyed) {\n throw getError({ message: '[acquire] Pool has been destroyed.' });\n }\n\n if (this.maxWaitingClients !== undefined && this.maxWaitingClients >= 0) {\n // Only reject when this acquire would actually have to WAIT: no idle resource and the pool at capacity. With a free/creatable resource the caller is served, so maxWaitingClients:0 means \"serve if available, never queue\" rather than \"reject everything\".\n const isWaitRequired = !this.idle.length && this.total >= this.size;\n\n if (isWaitRequired && this.waiterQueue.size >= this.maxWaitingClients) {\n throw getError({\n message: `[acquire] Too many waiting clients (>= ${this.maxWaitingClients}).`,\n });\n }\n }\n\n const promise = new Promise<T>((resolve, reject) => {\n const waiter: IPoolWaiter<T> = { resolve, reject, timer: null };\n const node = this.waiterQueue.enqueue({ value: waiter });\n\n if (this.acquireTimeoutMs !== undefined && this.acquireTimeoutMs >= 0) {\n waiter.timer = setTimeout(() => {\n this.waiterQueue.cancel({ node }); // O(1) — dispatch skips it when reached\n\n reject(\n getError({\n message: `[acquire] Acquire timed out after ${this.acquireTimeoutMs}ms.`,\n }),\n );\n }, this.acquireTimeoutMs);\n }\n });\n\n this.safeDispatch({ action: 'acquire' });\n return promise;\n }\n\n release(opts: { resource: T }): void {\n const { resource } = opts;\n const logger = this.logger.for(this.release.name);\n\n if (!this.borrowed.has(resource)) {\n logger.warn('Called with a foreign/already-released resource; ignoring.');\n return;\n }\n this.borrowed.delete(resource);\n\n if (this.isDestroyed) {\n this.destroyResource({ resource }).catch((error) => {\n logger.warn('destroyResource error on release: %s', error);\n });\n return;\n }\n\n this.idle.push(resource);\n this.safeDispatch({ action: 'release' });\n }\n\n async warmup(): Promise<void> {\n if (this.isDestroyed) {\n throw getError({ message: '[warmup] Pool has been destroyed.' });\n }\n\n // Create sequentially, re-checking the live `total` before each create so the `size` cap holds under concurrent warmup()/acquire(); createResource() bumps `total` synchronously, before its await, so a re-check observes in-flight creations from other callers - no overshoot.\n while (this.total < this.size) {\n const resource = await this.createResource();\n this.idle.push(resource);\n }\n\n this.safeDispatch({ action: 'warmup' });\n }\n\n async use<R>(opts: { execution: (resource: T) => ValueOrPromise<R> }): Promise<R> {\n const resource = await this.acquire();\n\n try {\n const result = await opts.execution(resource);\n this.release({ resource });\n return result;\n } catch (error) {\n this.logger\n .for(this.use.name)\n .error('execution threw → release skipped, force DISCARD | Error: %s', error);\n await this.discard({ resource });\n throw error;\n }\n }\n\n async discard(opts: { resource: T }): Promise<void> {\n const { resource } = opts;\n\n if (!this.borrowed.delete(resource)) {\n this.logger.for(this.discard.name).warn('Called with a non-borrowed resource; ignoring.');\n return;\n }\n\n await this.destroyResource({ resource });\n this.safeDispatch({ action: 'discard' });\n }\n\n async destroy(): Promise<void> {\n this.isDestroyed = true;\n\n const error = getError({ message: '[destroy] Pool destroyed while awaiting a resource.' });\n const draineds = this.waiterQueue.drain();\n for (const waiter of draineds) {\n this.clearWaiterTimer({ waiter });\n waiter.reject(error);\n }\n\n const toDestroy = this.idle.splice(0);\n await Promise.all(toDestroy.map((resource) => this.destroyResource({ resource })));\n }\n\n // ----------------------------------------------------------------------------------------------------------\n\n private async createResource(): Promise<T> {\n this.total += 1; // bump BEFORE await so concurrent dispatch/warmup see the in-flight create (no overshoot)\n\n try {\n const resource = await this.create();\n return resource;\n } catch (error) {\n this.total -= 1;\n this.logger\n .for(this.createResource.name)\n .error('Error creating pool resource | Error: %s', error);\n throw error;\n }\n }\n\n private async destroyResource(opts: { resource: T }): Promise<void> {\n this.total -= 1;\n\n try {\n await this.onDestroyResource(opts);\n } catch (error) {\n this.logger.for(this.destroyResource.name).warn('destroy hook threw: %s', error);\n }\n }\n\n // ----------------------------------------------------------------------------------------------------------\n\n /** Fire-and-forget dispatch with logged rejection; shared by all dispatch call sites. */\n private safeDispatch(opts: { action: string }): void {\n this.dispatch().catch((error) => {\n this.logger\n .for(this.dispatch.name)\n .warn('dispatch error | action: %s | Error: %s', opts.action, error);\n });\n }\n\n /** Pair servable resources with waiting acquirers until the queue drains or the pool is exhausted. Re-entrancy-guarded so only one loop runs at a time. */\n private async dispatch() {\n if (this.isDispatching) {\n return;\n }\n\n this.isDispatching = true;\n\n try {\n while (this.waiterQueue.size > 0) {\n // Prefer a warm idle resource (LIFO) over creating a new one.\n if (this.idle.length > 0) {\n const resource = await this.takeIdleResource();\n if (resource === null) {\n continue; // bad idle resource was discarded → try the next one\n }\n\n if (!this.handToWaiter({ resource })) {\n return;\n }\n continue;\n }\n\n if (this.total < this.size) {\n await this.createAndHand();\n continue;\n }\n\n return;\n }\n } catch (error) {\n this.logger\n .for(this.dispatch.name)\n .error('[dispatch] Error while dispatching pool element | Error: %s', error);\n } finally {\n this.isDispatching = false;\n }\n }\n\n /** Pop an idle resource and validate+reset it; on ANY failure destroy it and return null (caller retries). */\n private async takeIdleResource(): Promise<T | null> {\n const resource = this.idle.pop() as T;\n const logger = this.logger.for(this.takeIdleResource.name);\n\n let isValid: boolean;\n try {\n const result = await this.validateResource({ resource });\n isValid = result;\n } catch (error) {\n // A throwing validate must NOT leak the popped candidate (it would corrupt `total` and can permanently starve the pool); treat it like an invalid resource - discard and retry.\n logger.warn('validate hook threw; discarding resource: %s', error);\n await this.destroyResource({ resource });\n return null;\n }\n\n if (!isValid) {\n await this.destroyResource({ resource });\n return null;\n }\n\n try {\n await this.resetResource({ resource });\n } catch (error) {\n logger.warn('reset hook threw; discarding resource: %s', error);\n await this.destroyResource({ resource });\n return null;\n }\n\n return resource;\n }\n\n /** Create a fresh resource and hand it to the next waiter; on create failure, reject that waiter. */\n private async createAndHand(): Promise<void> {\n let resource: T;\n try {\n const result = await this.createResource();\n resource = result;\n } catch (error) {\n this.takeNextWaiter()?.reject(error);\n return;\n }\n\n this.handToWaiter({ resource });\n }\n\n /** Give a ready resource to the next live waiter; returns false and parks it in idle if the waiter raced away (e.g. timed out during an await). */\n private handToWaiter(opts: { resource: T }): boolean {\n const { resource } = opts;\n\n // `destroy()` tears down what it can SEE - the waiter queue and the idle list. A resource whose create() was still in flight is in neither, so parking it in idle now would leave it alive with nothing left to reclaim it.\n if (this.isDestroyed) {\n this.destroyResource({ resource }).catch((error) => {\n this.logger\n .for(this.handToWaiter.name)\n .warn('destroyResource error on a resource created after destroy(): %s', error);\n });\n return false;\n }\n\n const waiter = this.takeNextWaiter();\n if (!waiter) {\n this.idle.push(resource);\n return false;\n }\n\n this.borrowed.add(resource);\n waiter.resolve(resource);\n return true;\n }\n\n /** Dequeue the next live waiter and disarm its acquire-timeout so it cannot fire after settlement. */\n private takeNextWaiter(): TNullable<IPoolWaiter<T>> {\n const waiter = this.waiterQueue.dequeue();\n if (waiter) {\n this.clearWaiterTimer({ waiter });\n }\n return waiter;\n }\n\n private clearWaiterTimer(opts: { waiter: IPoolWaiter<T> }): void {\n const { waiter } = opts;\n if (waiter.timer) {\n clearTimeout(waiter.timer);\n waiter.timer = null;\n }\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../../../../../src/modules/queue/bullmq/helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAE5C,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAGpD,OAAO,KAAK,EAAE,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AACzF,OAAO,
|
|
1
|
+
{"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../../../../../src/modules/queue/bullmq/helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAE5C,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAGpD,OAAO,KAAK,EAAE,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AACzF,OAAO,EAAe,KAAK,cAAc,EAAE,MAAM,WAAW,CAAC;AAE7D,MAAM,WAAW,cAAc,CAAC,aAAa,GAAG,GAAG,EAAE,YAAY,GAAG,GAAG;IACrE,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,cAAc,CAAC;IACrB,eAAe,EAAE,YAAY,CAAC;IAE9B,6JAA6J;IAC7J,MAAM,CAAC,EAAE,cAAc,QAAQ,CAAC,CAAC;IAEjC,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,YAAY,CAAC,EAAE,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WAAW,CAAC,IAAI,EAAE;QAC1B,SAAS,EAAE,MAAM,CAAC;QAClB,SAAS,EAAE,SAAS,CAAC,aAAa,EAAE,YAAY,CAAC,CAAC;KACnD,GAAG,MAAM,CAAC,aAAa,EAAE,YAAY,CAAC;IASvC,eAAe;IA8Df,SAAS;IAuBH,KAAK;CAgCZ"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { BaseHelper } from '../../base.js';
|
|
2
2
|
import { getError } from '../../error/index.js';
|
|
3
|
-
import {
|
|
3
|
+
import { ErrorUtility } from '../../../utilities/error.utility.js';
|
|
4
4
|
import { ModuleUtility } from '../../../utilities/module.utility.js';
|
|
5
|
-
import {
|
|
5
|
+
import { HookUtility } from '../common/index.js';
|
|
6
6
|
export class BullMQHelper extends BaseHelper {
|
|
7
7
|
constructor(options) {
|
|
8
8
|
super({ scope: BullMQHelper.name, identifier: options.identifier });
|
|
@@ -118,14 +118,14 @@ export class BullMQHelper extends BaseHelper {
|
|
|
118
118
|
},
|
|
119
119
|
});
|
|
120
120
|
this.worker.on('completed', (job, result) => {
|
|
121
|
-
|
|
121
|
+
HookUtility.invoke({
|
|
122
122
|
logger: this.logger,
|
|
123
123
|
scope: 'worker-completed',
|
|
124
124
|
execution: () => this.onWorkerDataCompleted?.(job, result),
|
|
125
125
|
});
|
|
126
126
|
});
|
|
127
127
|
this.worker.on('failed', (job, reason) => {
|
|
128
|
-
|
|
128
|
+
HookUtility.invoke({
|
|
129
129
|
logger: this.logger,
|
|
130
130
|
scope: 'worker-failed',
|
|
131
131
|
execution: () => this.onWorkerDataFail?.(job, reason),
|
|
@@ -166,7 +166,7 @@ export class BullMQHelper extends BaseHelper {
|
|
|
166
166
|
this.logger
|
|
167
167
|
.for(this.close.name)
|
|
168
168
|
.error('Error closing BullMQ worker: %s | ID: %s', error, this.identifier);
|
|
169
|
-
failures.push(toError(error).message);
|
|
169
|
+
failures.push(ErrorUtility.toError({ error }).message);
|
|
170
170
|
}
|
|
171
171
|
try {
|
|
172
172
|
await this.queue?.close();
|
|
@@ -175,7 +175,7 @@ export class BullMQHelper extends BaseHelper {
|
|
|
175
175
|
this.logger
|
|
176
176
|
.for(this.close.name)
|
|
177
177
|
.error('Error closing BullMQ queue: %s | ID: %s', error, this.identifier);
|
|
178
|
-
failures.push(toError(error).message);
|
|
178
|
+
failures.push(ErrorUtility.toError({ error }).message);
|
|
179
179
|
}
|
|
180
180
|
if (failures.length) {
|
|
181
181
|
throw getError({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helper.js","sourceRoot":"","sources":["../../../../../src/modules/queue/bullmq/helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE3C,OAAO,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D,OAAO,EAAE,UAAU,EAAuB,MAAM,WAAW,CAAC;AA2B5D,MAAM,OAAO,YAAsD,SAAQ,UAAU;IA0BnF,YAAY,OAAoD;QAC9D,KAAK,CAAC,EAAE,KAAK,EAAE,YAAY,CAAC,IAAI,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC;QAnB5D,mBAAc,GAAG,CAAC,CAAC;QACnB,iBAAY,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;QAmBtC,MAAM,EACJ,SAAS,EACT,eAAe,EACf,IAAI,EACJ,cAAc,GAAG,CAAC,EAClB,YAAY,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,EAC7B,YAAY,EACZ,aAAa,EACb,YAAY,EACZ,qBAAqB,EACrB,gBAAgB,EAChB,MAAM,GACP,GAAG,OAAO,CAAC;QAEZ,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC;QAE3B,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC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O,CAAC,CAAC;QACxC,CAAC;QAED,IAAI,QAAQ,CAAC,MAAM,EAAE,CAAC;YACpB,MAAM,QAAQ,CAAC;gBACb,OAAO,EAAE,WAAW,IAAI,CAAC,UAAU,6CAA6C,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE;aACvG,CAAC,CAAC;QACL,CAAC;QAED,IAAI,CAAC,MAAM;aACR,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;aACpB,IAAI,CAAC,4CAA4C,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;IACzE,CAAC;CACF","sourcesContent":["import { BaseHelper } from '@/modules/base';\nimport { getError } from '@/modules/error';\nimport { type IRedisHelper } from '@/modules/redis';\nimport { toError } from '@/utilities/promise.utility';\nimport { ModuleUtility } from '@/utilities/module.utility';\nimport type { Job, Processor, Queue, QueueOptions, Worker, WorkerOptions } from 'bullmq';\nimport { invokeHook, type TBullQueueRole } from '../common';\n\nexport interface IBullMQOptions<TQueueElement = any, TQueueResult = any> {\n queueName: string;\n identifier: string;\n role: TBullQueueRole;\n redisConnection: IRedisHelper;\n\n /** `bullmq` itself, for a `bun build --compile` binary that has no `node_modules` to load it from. Omitted, it is loaded when a queue or worker is built. */\n module?: typeof import('bullmq');\n\n numberOfWorker?: number;\n lockDuration?: number;\n\n /** Merged over the framework defaults, under the fields the helper owns (`connection`) - see `queueOptionsFor`. */\n queueOptions?: Omit<QueueOptions, 'connection'>;\n /** Merged under the fields the helper owns (`connection`, `concurrency`, `lockDuration`) - see `workerOptionsFor`. */\n workerOptions?: Omit<WorkerOptions, 'connection' | 'concurrency' | 'lockDuration'>;\n\n onWorkerData?: (job: Job<TQueueElement, TQueueResult>) => Promise<any>;\n onWorkerDataCompleted?: (job: Job<TQueueElement, TQueueResult>, result: any) => Promise<void>;\n onWorkerDataFail?: (\n job: Job<TQueueElement, TQueueResult> | undefined,\n error: Error,\n ) => Promise<void>;\n}\n\nexport class BullMQHelper<TQueueElement = any, TQueueResult = any> extends BaseHelper {\n protected queueName: string;\n protected role: TBullQueueRole;\n protected redisConnection: IRedisHelper;\n\n queue: Queue<TQueueElement, TQueueResult>;\n worker: Worker<TQueueElement, TQueueResult>;\n\n protected numberOfWorker = 1;\n protected lockDuration = 90 * 60 * 1000;\n\n protected queueOptions?: Omit<QueueOptions, 'connection'>;\n protected workerOptions?: Omit<WorkerOptions, 'connection' | 'concurrency' | 'lockDuration'>;\n\n protected onWorkerData?: (job: Job<TQueueElement, TQueueResult>) => Promise<any>;\n protected onWorkerDataCompleted?: (\n job: Job<TQueueElement, TQueueResult>,\n result: any,\n ) => Promise<void>;\n protected onWorkerDataFail?: (\n job: Job<TQueueElement, TQueueResult> | undefined,\n error: Error,\n ) => Promise<void>;\n\n private readonly bullmqModule?: typeof import('bullmq');\n\n constructor(options: IBullMQOptions<TQueueElement, TQueueResult>) {\n super({ scope: BullMQHelper.name, identifier: options.identifier });\n const {\n queueName,\n redisConnection,\n role,\n numberOfWorker = 1,\n lockDuration = 90 * 60 * 1000,\n queueOptions,\n workerOptions,\n onWorkerData,\n onWorkerDataCompleted,\n onWorkerDataFail,\n module,\n } = options;\n\n this.bullmqModule = module;\n\n this.queueName = queueName;\n this.role = role;\n this.redisConnection = redisConnection;\n\n this.numberOfWorker = numberOfWorker;\n this.lockDuration = lockDuration;\n\n this.queueOptions = queueOptions;\n this.workerOptions = workerOptions;\n\n this.onWorkerData = onWorkerData;\n this.onWorkerDataCompleted = onWorkerDataCompleted;\n this.onWorkerDataFail = onWorkerDataFail;\n\n this.configure();\n }\n\n static newInstance<T = any, R = any>(opts: IBullMQOptions<T, R>) {\n return new BullMQHelper<T, R>(opts);\n }\n\n /** Structural, never `instanceof`: ioredis is an optional peer, and a second copy fails `instanceof` anyway. */\n protected isClusterClient(): boolean {\n const client = Object(this.redisConnection.getClient());\n\n if (Reflect.get(client, 'isCluster') === true) {\n return true;\n }\n\n return typeof Reflect.get(client, 'nodes') === 'function';\n }\n\n protected resolveQueueName(): string {\n if (this.isClusterClient() && !this.queueName.startsWith('{')) {\n return `{${this.queueName}}`;\n }\n\n return this.queueName;\n }\n\n configureQueue() {\n if (!this.queueName) {\n this.logger\n .for(this.configureQueue.name)\n .error('Invalid queue name | ID: %s', this.identifier);\n return;\n }\n\n const queueName = this.resolveQueueName();\n this.queue = this.buildQueue({ queueName });\n\n // An 'error' event without a listener is re-thrown by EventEmitter - a Redis hiccup would take the whole process down.\n this.queue.on('error', (error) => {\n this.logger\n .for('queue-error')\n .error(\n 'queue: %s | Queue error! Error: %s | ID: %s',\n this.queueName,\n error,\n this.identifier,\n );\n });\n }\n\n /** Full `Queue` options: framework defaults, then `this.queueOptions`, then the connection the helper owns - in that precedence order. */\n protected queueOptionsFor(opts: { queueName: string }): QueueOptions {\n this.logger\n .for(this.queueOptionsFor.name)\n .debug('Building queue options | Queue: %s | ID: %s', opts.queueName, this.identifier);\n\n return {\n defaultJobOptions: {\n removeOnComplete: true,\n removeOnFail: true,\n },\n ...this.queueOptions,\n connection: this.redisConnection.duplicateClient(),\n };\n }\n\n /** Full `Worker` options: `this.workerOptions`, then the connection/concurrency/lock fields the helper owns - in that precedence order. */\n protected workerOptionsFor(opts: {\n queueName: string;\n processor: Processor<TQueueElement, TQueueResult>;\n }): WorkerOptions {\n this.logger\n .for(this.workerOptionsFor.name)\n .debug('Building worker options | Queue: %s | ID: %s', opts.queueName, this.identifier);\n\n return {\n ...this.workerOptions,\n connection: this.redisConnection.duplicateClient(),\n concurrency: this.numberOfWorker,\n lockDuration: this.lockDuration,\n };\n }\n\n /** `bullmq` is an optional peer: loaded when a queue or worker is built, not when this module is. */\n private loadBullMQ(): typeof import('bullmq') {\n return (\n this.bullmqModule ?? ModuleUtility.loadSync<typeof import('bullmq')>({ module: 'bullmq' })\n );\n }\n\n /** Client factory seam - overridden in tests to run the helper without Redis. */\n protected buildQueue(opts: { queueName: string }): Queue<TQueueElement, TQueueResult> {\n const bullmq = this.loadBullMQ();\n return new bullmq.Queue<TQueueElement, TQueueResult>(\n opts.queueName,\n this.queueOptionsFor(opts),\n );\n }\n\n /** Client factory seam - overridden in tests to run the helper without Redis. */\n protected buildWorker(opts: {\n queueName: string;\n processor: Processor<TQueueElement, TQueueResult>;\n }): Worker<TQueueElement, TQueueResult> {\n const bullmq = this.loadBullMQ();\n return new bullmq.Worker<TQueueElement, TQueueResult>(\n opts.queueName,\n opts.processor,\n this.workerOptionsFor(opts),\n );\n }\n\n configureWorker() {\n if (!this.queueName) {\n this.logger\n .for(this.configureWorker.name)\n .error('Invalid worker name | ID: %s', this.identifier);\n return;\n }\n\n const queueName = this.resolveQueueName();\n this.worker = this.buildWorker({\n queueName,\n processor: async (job) => {\n if (this.onWorkerData) {\n const result = await this.onWorkerData(job);\n return result;\n }\n\n const { id, name, data } = job;\n this.logger\n .for('onWorkerData')\n .info(\n 'queue: %s | id: %s | name: %s | data: %j | ID: %s',\n this.queueName,\n id,\n name,\n data,\n this.identifier,\n );\n\n return undefined as TQueueResult;\n },\n });\n\n this.worker.on('completed', (job, result) => {\n invokeHook({\n logger: this.logger,\n scope: 'worker-completed',\n execution: () => this.onWorkerDataCompleted?.(job, result),\n });\n });\n\n this.worker.on('failed', (job, reason) => {\n invokeHook({\n logger: this.logger,\n scope: 'worker-failed',\n execution: () => this.onWorkerDataFail?.(job, reason),\n });\n });\n\n // An 'error' event without a listener is re-thrown by EventEmitter - a Redis hiccup would take the whole process down.\n this.worker.on('error', (error) => {\n this.logger\n .for('worker-error')\n .error(\n 'queue: %s | Worker error! Error: %s | ID: %s',\n this.queueName,\n error,\n this.identifier,\n );\n });\n }\n\n configure() {\n if (!this.role) {\n this.logger\n .for(this.configure.name)\n .error(\n 'Invalid client role to configure | Valid roles: [queue|worker] | ID: %s',\n this.identifier,\n );\n return;\n }\n\n switch (this.role) {\n case 'queue': {\n this.configureQueue();\n break;\n }\n case 'worker': {\n this.configureWorker();\n break;\n }\n }\n }\n\n async close() {\n const failures: string[] = [];\n\n // Both connections must be released even when the first close fails - otherwise a failing worker close silently leaks the queue's Redis connection forever.\n try {\n await this.worker?.close();\n } catch (error) {\n this.logger\n .for(this.close.name)\n .error('Error closing BullMQ worker: %s | ID: %s', error, this.identifier);\n failures.push(toError(error).message);\n }\n\n try {\n await this.queue?.close();\n } catch (error) {\n this.logger\n .for(this.close.name)\n .error('Error closing BullMQ queue: %s | ID: %s', error, this.identifier);\n failures.push(toError(error).message);\n }\n\n if (failures.length) {\n throw getError({\n message: `[close][${this.identifier}] Failed to close BullMQ helper | Errors: ${failures.join(' | ')}`,\n });\n }\n\n this.logger\n .for(this.close.name)\n .info('BullMQ helper closed successfully | ID: %s', this.identifier);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"helper.js","sourceRoot":"","sources":["../../../../../src/modules/queue/bullmq/helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE3C,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D,OAAO,EAAE,WAAW,EAAuB,MAAM,WAAW,CAAC;AA2B7D,MAAM,OAAO,YAAsD,SAAQ,UAAU;IA0BnF,YAAY,OAAoD;QAC9D,KAAK,CAAC,EAAE,KAAK,EAAE,YAAY,CAAC,IAAI,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC;QAnB5D,mBAAc,GAAG,CAAC,CAAC;QACnB,iBAAY,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;QAmBtC,MAAM,EACJ,SAAS,EACT,eAAe,EACf,IAAI,EACJ,cAAc,GAAG,CAAC,EAClB,YAAY,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,EAC7B,YAAY,EACZ,aAAa,EACb,YAAY,EACZ,qBAAqB,EACrB,gBAAgB,EAChB,MAAM,GACP,GAAG,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AAI,CAAC;iBACpB,KAAK,CAAC,yCAAyC,EAAE,KAAK,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;YAC5E,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC;QACzD,CAAC;QAED,IAAI,QAAQ,CAAC,MAAM,EAAE,CAAC;YACpB,MAAM,QAAQ,CAAC;gBACb,OAAO,EAAE,WAAW,IAAI,CAAC,UAAU,6CAA6C,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE;aACvG,CAAC,CAAC;QACL,CAAC;QAED,IAAI,CAAC,MAAM;aACR,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;aACpB,IAAI,CAAC,4CAA4C,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;IACzE,CAAC;CACF","sourcesContent":["import { BaseHelper } from '@/modules/base';\nimport { getError } from '@/modules/error';\nimport { type IRedisHelper } from '@/modules/redis';\nimport { ErrorUtility } from '@/utilities/error.utility';\nimport { ModuleUtility } from '@/utilities/module.utility';\nimport type { Job, Processor, Queue, QueueOptions, Worker, WorkerOptions } from 'bullmq';\nimport { HookUtility, type TBullQueueRole } from '../common';\n\nexport interface IBullMQOptions<TQueueElement = any, TQueueResult = any> {\n queueName: string;\n identifier: string;\n role: TBullQueueRole;\n redisConnection: IRedisHelper;\n\n /** `bullmq` itself, for a `bun build --compile` binary that has no `node_modules` to load it from. Omitted, it is loaded when a queue or worker is built. */\n module?: typeof import('bullmq');\n\n numberOfWorker?: number;\n lockDuration?: number;\n\n /** Merged over the framework defaults, under the fields the helper owns (`connection`) - see `queueOptionsFor`. */\n queueOptions?: Omit<QueueOptions, 'connection'>;\n /** Merged under the fields the helper owns (`connection`, `concurrency`, `lockDuration`) - see `workerOptionsFor`. */\n workerOptions?: Omit<WorkerOptions, 'connection' | 'concurrency' | 'lockDuration'>;\n\n onWorkerData?: (job: Job<TQueueElement, TQueueResult>) => Promise<any>;\n onWorkerDataCompleted?: (job: Job<TQueueElement, TQueueResult>, result: any) => Promise<void>;\n onWorkerDataFail?: (\n job: Job<TQueueElement, TQueueResult> | undefined,\n error: Error,\n ) => Promise<void>;\n}\n\nexport class BullMQHelper<TQueueElement = any, TQueueResult = any> extends BaseHelper {\n protected queueName: string;\n protected role: TBullQueueRole;\n protected redisConnection: IRedisHelper;\n\n queue: Queue<TQueueElement, TQueueResult>;\n worker: Worker<TQueueElement, TQueueResult>;\n\n protected numberOfWorker = 1;\n protected lockDuration = 90 * 60 * 1000;\n\n protected queueOptions?: Omit<QueueOptions, 'connection'>;\n protected workerOptions?: Omit<WorkerOptions, 'connection' | 'concurrency' | 'lockDuration'>;\n\n protected onWorkerData?: (job: Job<TQueueElement, TQueueResult>) => Promise<any>;\n protected onWorkerDataCompleted?: (\n job: Job<TQueueElement, TQueueResult>,\n result: any,\n ) => Promise<void>;\n protected onWorkerDataFail?: (\n job: Job<TQueueElement, TQueueResult> | undefined,\n error: Error,\n ) => Promise<void>;\n\n private readonly bullmqModule?: typeof import('bullmq');\n\n constructor(options: IBullMQOptions<TQueueElement, TQueueResult>) {\n super({ scope: BullMQHelper.name, identifier: options.identifier });\n const {\n queueName,\n redisConnection,\n role,\n numberOfWorker = 1,\n lockDuration = 90 * 60 * 1000,\n queueOptions,\n workerOptions,\n onWorkerData,\n onWorkerDataCompleted,\n onWorkerDataFail,\n module,\n } = options;\n\n this.bullmqModule = module;\n\n this.queueName = queueName;\n this.role = role;\n this.redisConnection = redisConnection;\n\n this.numberOfWorker = numberOfWorker;\n this.lockDuration = lockDuration;\n\n this.queueOptions = queueOptions;\n this.workerOptions = workerOptions;\n\n this.onWorkerData = onWorkerData;\n this.onWorkerDataCompleted = onWorkerDataCompleted;\n this.onWorkerDataFail = onWorkerDataFail;\n\n this.configure();\n }\n\n static newInstance<T = any, R = any>(opts: IBullMQOptions<T, R>) {\n return new BullMQHelper<T, R>(opts);\n }\n\n /** Structural, never `instanceof`: ioredis is an optional peer, and a second copy fails `instanceof` anyway. */\n protected isClusterClient(): boolean {\n const client = Object(this.redisConnection.getClient());\n\n if (Reflect.get(client, 'isCluster') === true) {\n return true;\n }\n\n return typeof Reflect.get(client, 'nodes') === 'function';\n }\n\n protected resolveQueueName(): string {\n if (this.isClusterClient() && !this.queueName.startsWith('{')) {\n return `{${this.queueName}}`;\n }\n\n return this.queueName;\n }\n\n configureQueue() {\n if (!this.queueName) {\n this.logger\n .for(this.configureQueue.name)\n .error('Invalid queue name | ID: %s', this.identifier);\n return;\n }\n\n const queueName = this.resolveQueueName();\n this.queue = this.buildQueue({ queueName });\n\n // An 'error' event without a listener is re-thrown by EventEmitter - a Redis hiccup would take the whole process down.\n this.queue.on('error', (error) => {\n this.logger\n .for('queue-error')\n .error(\n 'queue: %s | Queue error! Error: %s | ID: %s',\n this.queueName,\n error,\n this.identifier,\n );\n });\n }\n\n /** Full `Queue` options: framework defaults, then `this.queueOptions`, then the connection the helper owns - in that precedence order. */\n protected queueOptionsFor(opts: { queueName: string }): QueueOptions {\n this.logger\n .for(this.queueOptionsFor.name)\n .debug('Building queue options | Queue: %s | ID: %s', opts.queueName, this.identifier);\n\n return {\n defaultJobOptions: {\n removeOnComplete: true,\n removeOnFail: true,\n },\n ...this.queueOptions,\n connection: this.redisConnection.duplicateClient(),\n };\n }\n\n /** Full `Worker` options: `this.workerOptions`, then the connection/concurrency/lock fields the helper owns - in that precedence order. */\n protected workerOptionsFor(opts: {\n queueName: string;\n processor: Processor<TQueueElement, TQueueResult>;\n }): WorkerOptions {\n this.logger\n .for(this.workerOptionsFor.name)\n .debug('Building worker options | Queue: %s | ID: %s', opts.queueName, this.identifier);\n\n return {\n ...this.workerOptions,\n connection: this.redisConnection.duplicateClient(),\n concurrency: this.numberOfWorker,\n lockDuration: this.lockDuration,\n };\n }\n\n /** `bullmq` is an optional peer: loaded when a queue or worker is built, not when this module is. */\n private loadBullMQ(): typeof import('bullmq') {\n return (\n this.bullmqModule ?? ModuleUtility.loadSync<typeof import('bullmq')>({ module: 'bullmq' })\n );\n }\n\n /** Client factory seam - overridden in tests to run the helper without Redis. */\n protected buildQueue(opts: { queueName: string }): Queue<TQueueElement, TQueueResult> {\n const bullmq = this.loadBullMQ();\n return new bullmq.Queue<TQueueElement, TQueueResult>(\n opts.queueName,\n this.queueOptionsFor(opts),\n );\n }\n\n /** Client factory seam - overridden in tests to run the helper without Redis. */\n protected buildWorker(opts: {\n queueName: string;\n processor: Processor<TQueueElement, TQueueResult>;\n }): Worker<TQueueElement, TQueueResult> {\n const bullmq = this.loadBullMQ();\n return new bullmq.Worker<TQueueElement, TQueueResult>(\n opts.queueName,\n opts.processor,\n this.workerOptionsFor(opts),\n );\n }\n\n configureWorker() {\n if (!this.queueName) {\n this.logger\n .for(this.configureWorker.name)\n .error('Invalid worker name | ID: %s', this.identifier);\n return;\n }\n\n const queueName = this.resolveQueueName();\n this.worker = this.buildWorker({\n queueName,\n processor: async (job) => {\n if (this.onWorkerData) {\n const result = await this.onWorkerData(job);\n return result;\n }\n\n const { id, name, data } = job;\n this.logger\n .for('onWorkerData')\n .info(\n 'queue: %s | id: %s | name: %s | data: %j | ID: %s',\n this.queueName,\n id,\n name,\n data,\n this.identifier,\n );\n\n return undefined as TQueueResult;\n },\n });\n\n this.worker.on('completed', (job, result) => {\n HookUtility.invoke({\n logger: this.logger,\n scope: 'worker-completed',\n execution: () => this.onWorkerDataCompleted?.(job, result),\n });\n });\n\n this.worker.on('failed', (job, reason) => {\n HookUtility.invoke({\n logger: this.logger,\n scope: 'worker-failed',\n execution: () => this.onWorkerDataFail?.(job, reason),\n });\n });\n\n // An 'error' event without a listener is re-thrown by EventEmitter - a Redis hiccup would take the whole process down.\n this.worker.on('error', (error) => {\n this.logger\n .for('worker-error')\n .error(\n 'queue: %s | Worker error! Error: %s | ID: %s',\n this.queueName,\n error,\n this.identifier,\n );\n });\n }\n\n configure() {\n if (!this.role) {\n this.logger\n .for(this.configure.name)\n .error(\n 'Invalid client role to configure | Valid roles: [queue|worker] | ID: %s',\n this.identifier,\n );\n return;\n }\n\n switch (this.role) {\n case 'queue': {\n this.configureQueue();\n break;\n }\n case 'worker': {\n this.configureWorker();\n break;\n }\n }\n }\n\n async close() {\n const failures: string[] = [];\n\n // Both connections must be released even when the first close fails - otherwise a failing worker close silently leaks the queue's Redis connection forever.\n try {\n await this.worker?.close();\n } catch (error) {\n this.logger\n .for(this.close.name)\n .error('Error closing BullMQ worker: %s | ID: %s', error, this.identifier);\n failures.push(ErrorUtility.toError({ error }).message);\n }\n\n try {\n await this.queue?.close();\n } catch (error) {\n this.logger\n .for(this.close.name)\n .error('Error closing BullMQ queue: %s | ID: %s', error, this.identifier);\n failures.push(ErrorUtility.toError({ error }).message);\n }\n\n if (failures.length) {\n throw getError({\n message: `[close][${this.identifier}] Failed to close BullMQ helper | Errors: ${failures.join(' | ')}`,\n });\n }\n\n this.logger\n .for(this.close.name)\n .info('BullMQ helper closed successfully | ID: %s', this.identifier);\n }\n}\n"]}
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import { type ValueOrPromise } from '../../../common/types/index.js';
|
|
2
2
|
import type { ILogger } from '../../logger/index.js';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
3
|
+
export declare class HookUtility {
|
|
4
|
+
/** Fire-and-forget a hook from inside an event listener. Takes a thunk because `PromiseUtility.voidExecution` takes a VALUE - a synchronous throw would escape the guard and, inside an emitter callback, take the process down. Rejections still route to the logger. */
|
|
5
|
+
static invoke(opts: {
|
|
6
|
+
logger: ILogger;
|
|
7
|
+
scope: string;
|
|
8
|
+
execution: () => ValueOrPromise<unknown>;
|
|
9
|
+
}): void;
|
|
10
|
+
/** Await a user-supplied hook, absorbing sync throws and rejections without breaking the caller's state machine. */
|
|
11
|
+
static execute(opts: {
|
|
12
|
+
logger: ILogger;
|
|
13
|
+
scope: string;
|
|
14
|
+
execution: () => ValueOrPromise<unknown>;
|
|
15
|
+
}): Promise<void>;
|
|
16
|
+
}
|
|
15
17
|
//# sourceMappingURL=hook.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hook.d.ts","sourceRoot":"","sources":["../../../../../src/modules/queue/common/hook.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAGhD,
|
|
1
|
+
{"version":3,"file":"hook.d.ts","sourceRoot":"","sources":["../../../../../src/modules/queue/common/hook.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAGhD,qBAAa,WAAW;IACtB,0QAA0Q;IAC1Q,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE;QAClB,MAAM,EAAE,OAAO,CAAC;QAChB,KAAK,EAAE,MAAM,CAAC;QACd,SAAS,EAAE,MAAM,cAAc,CAAC,OAAO,CAAC,CAAC;KAC1C,GAAG,IAAI;IAUR,oHAAoH;WACvG,OAAO,CAAC,IAAI,EAAE;QACzB,MAAM,EAAE,OAAO,CAAC;QAChB,KAAK,EAAE,MAAM,CAAC;QACd,SAAS,EAAE,MAAM,cAAc,CAAC,OAAO,CAAC,CAAC;KAC1C,GAAG,OAAO,CAAC,IAAI,CAAC;CASlB"}
|
|
@@ -1,22 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { PromiseUtility } from '../../../utilities/promise.utility.js';
|
|
2
|
+
export class HookUtility {
|
|
3
|
+
/** Fire-and-forget a hook from inside an event listener. Takes a thunk because `PromiseUtility.voidExecution` takes a VALUE - a synchronous throw would escape the guard and, inside an emitter callback, take the process down. Rejections still route to the logger. */
|
|
4
|
+
static invoke(opts) {
|
|
5
|
+
const { logger, scope, execution } = opts;
|
|
6
|
+
try {
|
|
7
|
+
PromiseUtility.voidExecution({ logger, scope, execution: execution() });
|
|
8
|
+
}
|
|
9
|
+
catch (error) {
|
|
10
|
+
logger.for(scope).error('Hook execution FAILED | Error: %s', error);
|
|
11
|
+
}
|
|
7
12
|
}
|
|
8
|
-
|
|
9
|
-
|
|
13
|
+
/** Await a user-supplied hook, absorbing sync throws and rejections without breaking the caller's state machine. */
|
|
14
|
+
static async execute(opts) {
|
|
15
|
+
const { logger, scope, execution } = opts;
|
|
16
|
+
try {
|
|
17
|
+
await execution();
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
logger.for(scope).error('Hook execution FAILED | Error: %s', error);
|
|
21
|
+
}
|
|
10
22
|
}
|
|
11
|
-
}
|
|
12
|
-
/** Await a user-supplied hook, absorbing sync throws and rejections without breaking the caller's state machine. */
|
|
13
|
-
export const awaitHook = async (opts) => {
|
|
14
|
-
const { logger, scope, execution } = opts;
|
|
15
|
-
try {
|
|
16
|
-
await execution();
|
|
17
|
-
}
|
|
18
|
-
catch (error) {
|
|
19
|
-
logger.for(scope).error('Hook execution FAILED | Error: %s', error);
|
|
20
|
-
}
|
|
21
|
-
};
|
|
23
|
+
}
|
|
22
24
|
//# sourceMappingURL=hook.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hook.js","sourceRoot":"","sources":["../../../../../src/modules/queue/common/hook.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"hook.js","sourceRoot":"","sources":["../../../../../src/modules/queue/common/hook.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAE7D,MAAM,OAAO,WAAW;IACtB,0QAA0Q;IAC1Q,MAAM,CAAC,MAAM,CAAC,IAIb;QACC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,IAAI,CAAC;QAE1C,IAAI,CAAC;YACH,cAAc,CAAC,aAAa,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,EAAE,CAAC,CAAC;QAC1E,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,mCAAmC,EAAE,KAAK,CAAC,CAAC;QACtE,CAAC;IACH,CAAC;IAED,oHAAoH;IACpH,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,IAIpB;QACC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,IAAI,CAAC;QAE1C,IAAI,CAAC;YACH,MAAM,SAAS,EAAE,CAAC;QACpB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,mCAAmC,EAAE,KAAK,CAAC,CAAC;QACtE,CAAC;IACH,CAAC;CACF","sourcesContent":["import { type ValueOrPromise } from '@/common/types';\nimport type { ILogger } from '@/modules/logger';\nimport { PromiseUtility } from '@/utilities/promise.utility';\n\nexport class HookUtility {\n /** Fire-and-forget a hook from inside an event listener. Takes a thunk because `PromiseUtility.voidExecution` takes a VALUE - a synchronous throw would escape the guard and, inside an emitter callback, take the process down. Rejections still route to the logger. */\n static invoke(opts: {\n logger: ILogger;\n scope: string;\n execution: () => ValueOrPromise<unknown>;\n }): void {\n const { logger, scope, execution } = opts;\n\n try {\n PromiseUtility.voidExecution({ logger, scope, execution: execution() });\n } catch (error) {\n logger.for(scope).error('Hook execution FAILED | Error: %s', error);\n }\n }\n\n /** Await a user-supplied hook, absorbing sync throws and rejections without breaking the caller's state machine. */\n static async execute(opts: {\n logger: ILogger;\n scope: string;\n execution: () => ValueOrPromise<unknown>;\n }): Promise<void> {\n const { logger, scope, execution } = opts;\n\n try {\n await execution();\n } catch (error) {\n logger.for(scope).error('Hook execution FAILED | Error: %s', error);\n }\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/modules/queue/common/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,QAAQ,CAAC
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/modules/queue/common/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,QAAQ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/modules/queue/common/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,QAAQ,CAAC
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/modules/queue/common/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,QAAQ,CAAC","sourcesContent":["export * from './constants';\nexport * from './hook';\n"]}
|