@luckylillia-dev/luckylillia 0.0.0-stage → 9.0.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/chunks/preload-helper-D5R8FHQt.js +342 -0
- package/chunks/preload-helper-D5R8FHQt.js.map +1 -0
- package/default_config.json +83 -0
- package/llbot-core.js +35013 -0
- package/llbot-core.js.map +1 -0
- package/llbot.js +138 -0
- package/llbot.js.map +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/commands.d.ts +86 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/commands.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/config.d.ts +14 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/config.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/ffmpeg.d.ts +41 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/ffmpeg.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/http.d.ts +88 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/http.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/index.d.ts +17 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/index.js +6 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/internal.d.ts +430 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/internal.js +7 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/manifest.d.ts +68 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/manifest.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/server.d.ts +23 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/server.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/storage.d.ts +21 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/storage.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/testing.d.ts +71 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/testing.js +1123 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/universal.d.ts +299 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/universal.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/webui.d.ts +762 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/webui.js +9 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/package.json +63 -0
- package/node_modules/@satorijs/element/jsx-dev-runtime.d.ts +3 -0
- package/node_modules/@satorijs/element/jsx-runtime.d.ts +3 -0
- package/node_modules/@satorijs/element/lib/index.cjs +447 -0
- package/node_modules/@satorijs/element/lib/index.d.ts +116 -0
- package/node_modules/@satorijs/element/lib/index.mjs +455 -0
- package/node_modules/@satorijs/element/package.json +60 -0
- package/node_modules/@satorijs/element/src/index.ts +559 -0
- package/node_modules/@satorijs/protocol/lib/index.cjs +187 -0
- package/node_modules/@satorijs/protocol/lib/index.d.ts +328 -0
- package/node_modules/@satorijs/protocol/lib/index.mjs +148 -0
- package/node_modules/@satorijs/protocol/package.json +44 -0
- package/node_modules/@satorijs/protocol/src/index.ts +513 -0
- package/node_modules/@standard-schema/spec/LICENSE +21 -0
- package/node_modules/@standard-schema/spec/README.md +198 -0
- package/node_modules/@standard-schema/spec/dist/index.cjs +18 -0
- package/node_modules/@standard-schema/spec/dist/index.d.cts +119 -0
- package/node_modules/@standard-schema/spec/dist/index.d.ts +119 -0
- package/node_modules/@standard-schema/spec/dist/index.js +0 -0
- package/node_modules/@standard-schema/spec/package.json +52 -0
- package/node_modules/cordis/README.md +10 -0
- package/node_modules/cordis/bin.js +16 -0
- package/node_modules/cordis/lib/context.d.ts +32 -0
- package/node_modules/cordis/lib/events.d.ts +62 -0
- package/node_modules/cordis/lib/fiber.d.ts +85 -0
- package/node_modules/cordis/lib/index.d.ts +7 -0
- package/node_modules/cordis/lib/index.js +1577 -0
- package/node_modules/cordis/lib/logger.d.ts +74 -0
- package/node_modules/cordis/lib/reflect.d.ts +54 -0
- package/node_modules/cordis/lib/registry.d.ts +78 -0
- package/node_modules/cordis/lib/service.d.ts +19 -0
- package/node_modules/cordis/lib/utils.d.ts +50 -0
- package/node_modules/cordis/package.json +49 -0
- package/node_modules/cosmokit/LICENSE +21 -0
- package/node_modules/cosmokit/README.md +24 -0
- package/node_modules/cosmokit/lib/index.cjs +419 -0
- package/node_modules/cosmokit/lib/index.cjs.map +6 -0
- package/node_modules/cosmokit/lib/index.d.ts +142 -0
- package/node_modules/cosmokit/lib/index.mjs +357 -0
- package/node_modules/cosmokit/lib/index.mjs.map +6 -0
- package/node_modules/cosmokit/package.json +57 -0
- package/node_modules/cosmokit/src/array.ts +35 -0
- package/node_modules/cosmokit/src/index.ts +5 -0
- package/node_modules/cosmokit/src/misc.ts +57 -0
- package/node_modules/cosmokit/src/string.ts +101 -0
- package/node_modules/cosmokit/src/time.ts +91 -0
- package/node_modules/cosmokit/src/types.ts +131 -0
- package/node_modules/schemastery/README.md +342 -0
- package/node_modules/schemastery/lib/index.cjs +648 -0
- package/node_modules/schemastery/lib/index.cjs.map +6 -0
- package/node_modules/schemastery/lib/index.d.ts +138 -0
- package/node_modules/schemastery/lib/index.mjs +656 -0
- package/node_modules/schemastery/lib/index.mjs.map +6 -0
- package/node_modules/schemastery/package.json +32 -0
- package/node_modules/schemastery/src/index.ts +841 -0
- package/node_modules/undici/LICENSE +21 -0
- package/node_modules/undici/README.md +741 -0
- package/node_modules/undici/docs/docs/api/Agent.md +84 -0
- package/node_modules/undici/docs/docs/api/BalancedPool.md +99 -0
- package/node_modules/undici/docs/docs/api/CacheStorage.md +30 -0
- package/node_modules/undici/docs/docs/api/CacheStore.md +164 -0
- package/node_modules/undici/docs/docs/api/Client.md +290 -0
- package/node_modules/undici/docs/docs/api/ClientStats.md +27 -0
- package/node_modules/undici/docs/docs/api/Connector.md +115 -0
- package/node_modules/undici/docs/docs/api/ContentType.md +57 -0
- package/node_modules/undici/docs/docs/api/Cookies.md +128 -0
- package/node_modules/undici/docs/docs/api/Debug.md +62 -0
- package/node_modules/undici/docs/docs/api/DiagnosticsChannel.md +317 -0
- package/node_modules/undici/docs/docs/api/Dispatcher.md +1393 -0
- package/node_modules/undici/docs/docs/api/EnvHttpProxyAgent.md +159 -0
- package/node_modules/undici/docs/docs/api/Errors.md +49 -0
- package/node_modules/undici/docs/docs/api/EventSource.md +45 -0
- package/node_modules/undici/docs/docs/api/Fetch.md +60 -0
- package/node_modules/undici/docs/docs/api/GlobalInstallation.md +139 -0
- package/node_modules/undici/docs/docs/api/H2CClient.md +263 -0
- package/node_modules/undici/docs/docs/api/MockAgent.md +603 -0
- package/node_modules/undici/docs/docs/api/MockCallHistory.md +197 -0
- package/node_modules/undici/docs/docs/api/MockCallHistoryLog.md +43 -0
- package/node_modules/undici/docs/docs/api/MockClient.md +81 -0
- package/node_modules/undici/docs/docs/api/MockErrors.md +12 -0
- package/node_modules/undici/docs/docs/api/MockPool.md +555 -0
- package/node_modules/undici/docs/docs/api/Pool.md +84 -0
- package/node_modules/undici/docs/docs/api/PoolStats.md +35 -0
- package/node_modules/undici/docs/docs/api/ProxyAgent.md +229 -0
- package/node_modules/undici/docs/docs/api/RedirectHandler.md +93 -0
- package/node_modules/undici/docs/docs/api/RetryAgent.md +50 -0
- package/node_modules/undici/docs/docs/api/RetryHandler.md +118 -0
- package/node_modules/undici/docs/docs/api/RoundRobinPool.md +145 -0
- package/node_modules/undici/docs/docs/api/SnapshotAgent.md +616 -0
- package/node_modules/undici/docs/docs/api/Socks5ProxyAgent.md +275 -0
- package/node_modules/undici/docs/docs/api/Util.md +25 -0
- package/node_modules/undici/docs/docs/api/WebSocket.md +141 -0
- package/node_modules/undici/docs/docs/api/api-lifecycle.md +91 -0
- package/node_modules/undici/docs/docs/best-practices/client-certificate.md +64 -0
- package/node_modules/undici/docs/docs/best-practices/crawling.md +58 -0
- package/node_modules/undici/docs/docs/best-practices/mocking-request.md +190 -0
- package/node_modules/undici/docs/docs/best-practices/proxy.md +127 -0
- package/node_modules/undici/docs/docs/best-practices/undici-vs-builtin-fetch.md +224 -0
- package/node_modules/undici/docs/docs/best-practices/writing-tests.md +63 -0
- package/node_modules/undici/index-fetch.js +65 -0
- package/node_modules/undici/index.d.ts +3 -0
- package/node_modules/undici/index.js +234 -0
- package/node_modules/undici/lib/api/abort-signal.js +59 -0
- package/node_modules/undici/lib/api/api-connect.js +110 -0
- package/node_modules/undici/lib/api/api-pipeline.js +252 -0
- package/node_modules/undici/lib/api/api-request.js +214 -0
- package/node_modules/undici/lib/api/api-stream.js +209 -0
- package/node_modules/undici/lib/api/api-upgrade.js +111 -0
- package/node_modules/undici/lib/api/index.js +7 -0
- package/node_modules/undici/lib/api/readable.js +580 -0
- package/node_modules/undici/lib/cache/memory-cache-store.js +279 -0
- package/node_modules/undici/lib/cache/sqlite-cache-store.js +467 -0
- package/node_modules/undici/lib/core/connect.js +159 -0
- package/node_modules/undici/lib/core/constants.js +143 -0
- package/node_modules/undici/lib/core/diagnostics.js +227 -0
- package/node_modules/undici/lib/core/errors.js +477 -0
- package/node_modules/undici/lib/core/request.js +485 -0
- package/node_modules/undici/lib/core/socks5-client.js +422 -0
- package/node_modules/undici/lib/core/socks5-utils.js +212 -0
- package/node_modules/undici/lib/core/symbols.js +76 -0
- package/node_modules/undici/lib/core/tree.js +160 -0
- package/node_modules/undici/lib/core/util.js +992 -0
- package/node_modules/undici/lib/dispatcher/agent.js +162 -0
- package/node_modules/undici/lib/dispatcher/balanced-pool.js +221 -0
- package/node_modules/undici/lib/dispatcher/client-h1.js +1744 -0
- package/node_modules/undici/lib/dispatcher/client-h2.js +1056 -0
- package/node_modules/undici/lib/dispatcher/client.js +669 -0
- package/node_modules/undici/lib/dispatcher/dispatcher-base.js +196 -0
- package/node_modules/undici/lib/dispatcher/dispatcher.js +48 -0
- package/node_modules/undici/lib/dispatcher/env-http-proxy-agent.js +146 -0
- package/node_modules/undici/lib/dispatcher/fixed-queue.js +135 -0
- package/node_modules/undici/lib/dispatcher/h2c-client.js +51 -0
- package/node_modules/undici/lib/dispatcher/pool-base.js +214 -0
- package/node_modules/undici/lib/dispatcher/pool.js +118 -0
- package/node_modules/undici/lib/dispatcher/proxy-agent.js +319 -0
- package/node_modules/undici/lib/dispatcher/retry-agent.js +35 -0
- package/node_modules/undici/lib/dispatcher/round-robin-pool.js +137 -0
- package/node_modules/undici/lib/dispatcher/socks5-proxy-agent.js +260 -0
- package/node_modules/undici/lib/encoding/index.js +33 -0
- package/node_modules/undici/lib/global.js +59 -0
- package/node_modules/undici/lib/handler/cache-handler.js +824 -0
- package/node_modules/undici/lib/handler/cache-revalidation-handler.js +124 -0
- package/node_modules/undici/lib/handler/decorator-handler.js +67 -0
- package/node_modules/undici/lib/handler/deduplication-handler.js +504 -0
- package/node_modules/undici/lib/handler/redirect-handler.js +245 -0
- package/node_modules/undici/lib/handler/retry-handler.js +504 -0
- package/node_modules/undici/lib/handler/unwrap-handler.js +106 -0
- package/node_modules/undici/lib/handler/wrap-handler.js +105 -0
- package/node_modules/undici/lib/interceptor/cache.js +621 -0
- package/node_modules/undici/lib/interceptor/decompress.js +561 -0
- package/node_modules/undici/lib/interceptor/deduplicate.js +117 -0
- package/node_modules/undici/lib/interceptor/dns.js +571 -0
- package/node_modules/undici/lib/interceptor/dump.js +99 -0
- package/node_modules/undici/lib/interceptor/redirect.js +21 -0
- package/node_modules/undici/lib/interceptor/response-error.js +95 -0
- package/node_modules/undici/lib/interceptor/retry.js +19 -0
- package/node_modules/undici/lib/llhttp/constants.d.ts +195 -0
- package/node_modules/undici/lib/llhttp/constants.js +531 -0
- package/node_modules/undici/lib/llhttp/llhttp-wasm.js +15 -0
- package/node_modules/undici/lib/llhttp/llhttp_simd-wasm.js +15 -0
- package/node_modules/undici/lib/llhttp/utils.d.ts +2 -0
- package/node_modules/undici/lib/llhttp/utils.js +12 -0
- package/node_modules/undici/lib/mock/mock-agent.js +232 -0
- package/node_modules/undici/lib/mock/mock-call-history.js +248 -0
- package/node_modules/undici/lib/mock/mock-client.js +68 -0
- package/node_modules/undici/lib/mock/mock-errors.js +29 -0
- package/node_modules/undici/lib/mock/mock-interceptor.js +209 -0
- package/node_modules/undici/lib/mock/mock-pool.js +68 -0
- package/node_modules/undici/lib/mock/mock-symbols.js +32 -0
- package/node_modules/undici/lib/mock/mock-utils.js +486 -0
- package/node_modules/undici/lib/mock/pending-interceptors-formatter.js +43 -0
- package/node_modules/undici/lib/mock/snapshot-agent.js +353 -0
- package/node_modules/undici/lib/mock/snapshot-recorder.js +588 -0
- package/node_modules/undici/lib/mock/snapshot-utils.js +158 -0
- package/node_modules/undici/lib/util/cache.js +676 -0
- package/node_modules/undici/lib/util/date.js +670 -0
- package/node_modules/undici/lib/util/promise.js +28 -0
- package/node_modules/undici/lib/util/runtime-features.js +124 -0
- package/node_modules/undici/lib/util/stats.js +32 -0
- package/node_modules/undici/lib/util/timers.js +425 -0
- package/node_modules/undici/lib/web/cache/cache.js +864 -0
- package/node_modules/undici/lib/web/cache/cachestorage.js +152 -0
- package/node_modules/undici/lib/web/cache/util.js +45 -0
- package/node_modules/undici/lib/web/cookies/constants.js +12 -0
- package/node_modules/undici/lib/web/cookies/index.js +201 -0
- package/node_modules/undici/lib/web/cookies/parse.js +315 -0
- package/node_modules/undici/lib/web/cookies/util.js +371 -0
- package/node_modules/undici/lib/web/eventsource/eventsource-stream.js +575 -0
- package/node_modules/undici/lib/web/eventsource/eventsource.js +504 -0
- package/node_modules/undici/lib/web/eventsource/util.js +29 -0
- package/node_modules/undici/lib/web/fetch/LICENSE +21 -0
- package/node_modules/undici/lib/web/fetch/body.js +509 -0
- package/node_modules/undici/lib/web/fetch/constants.js +131 -0
- package/node_modules/undici/lib/web/fetch/data-url.js +596 -0
- package/node_modules/undici/lib/web/fetch/formdata-parser.js +586 -0
- package/node_modules/undici/lib/web/fetch/formdata.js +259 -0
- package/node_modules/undici/lib/web/fetch/global.js +40 -0
- package/node_modules/undici/lib/web/fetch/headers.js +719 -0
- package/node_modules/undici/lib/web/fetch/index.js +2413 -0
- package/node_modules/undici/lib/web/fetch/request.js +1115 -0
- package/node_modules/undici/lib/web/fetch/response.js +641 -0
- package/node_modules/undici/lib/web/fetch/util.js +1522 -0
- package/node_modules/undici/lib/web/infra/index.js +243 -0
- package/node_modules/undici/lib/web/subresource-integrity/Readme.md +9 -0
- package/node_modules/undici/lib/web/subresource-integrity/subresource-integrity.js +307 -0
- package/node_modules/undici/lib/web/webidl/index.js +1006 -0
- package/node_modules/undici/lib/web/websocket/connection.js +339 -0
- package/node_modules/undici/lib/web/websocket/constants.js +126 -0
- package/node_modules/undici/lib/web/websocket/events.js +331 -0
- package/node_modules/undici/lib/web/websocket/frame.js +133 -0
- package/node_modules/undici/lib/web/websocket/permessage-deflate.js +105 -0
- package/node_modules/undici/lib/web/websocket/receiver.js +520 -0
- package/node_modules/undici/lib/web/websocket/sender.js +109 -0
- package/node_modules/undici/lib/web/websocket/stream/websocketerror.js +104 -0
- package/node_modules/undici/lib/web/websocket/stream/websocketstream.js +496 -0
- package/node_modules/undici/lib/web/websocket/util.js +347 -0
- package/node_modules/undici/lib/web/websocket/websocket.js +758 -0
- package/node_modules/undici/package.json +152 -0
- package/node_modules/undici/scripts/strip-comments.js +10 -0
- package/node_modules/undici/types/README.md +6 -0
- package/node_modules/undici/types/agent.d.ts +32 -0
- package/node_modules/undici/types/api.d.ts +43 -0
- package/node_modules/undici/types/balanced-pool.d.ts +30 -0
- package/node_modules/undici/types/cache-interceptor.d.ts +179 -0
- package/node_modules/undici/types/cache.d.ts +36 -0
- package/node_modules/undici/types/client-stats.d.ts +15 -0
- package/node_modules/undici/types/client.d.ts +153 -0
- package/node_modules/undici/types/connector.d.ts +36 -0
- package/node_modules/undici/types/content-type.d.ts +21 -0
- package/node_modules/undici/types/cookies.d.ts +30 -0
- package/node_modules/undici/types/diagnostics-channel.d.ts +74 -0
- package/node_modules/undici/types/dispatcher.d.ts +275 -0
- package/node_modules/undici/types/env-http-proxy-agent.d.ts +22 -0
- package/node_modules/undici/types/errors.d.ts +177 -0
- package/node_modules/undici/types/eventsource.d.ts +66 -0
- package/node_modules/undici/types/fetch.d.ts +231 -0
- package/node_modules/undici/types/formdata.d.ts +114 -0
- package/node_modules/undici/types/global-dispatcher.d.ts +9 -0
- package/node_modules/undici/types/global-origin.d.ts +7 -0
- package/node_modules/undici/types/h2c-client.d.ts +73 -0
- package/node_modules/undici/types/handlers.d.ts +14 -0
- package/node_modules/undici/types/header.d.ts +160 -0
- package/node_modules/undici/types/index.d.ts +91 -0
- package/node_modules/undici/types/interceptors.d.ts +82 -0
- package/node_modules/undici/types/mock-agent.d.ts +68 -0
- package/node_modules/undici/types/mock-call-history.d.ts +111 -0
- package/node_modules/undici/types/mock-client.d.ts +27 -0
- package/node_modules/undici/types/mock-errors.d.ts +12 -0
- package/node_modules/undici/types/mock-interceptor.d.ts +94 -0
- package/node_modules/undici/types/mock-pool.d.ts +27 -0
- package/node_modules/undici/types/patch.d.ts +29 -0
- package/node_modules/undici/types/pool-stats.d.ts +19 -0
- package/node_modules/undici/types/pool.d.ts +41 -0
- package/node_modules/undici/types/proxy-agent.d.ts +29 -0
- package/node_modules/undici/types/readable.d.ts +68 -0
- package/node_modules/undici/types/retry-agent.d.ts +8 -0
- package/node_modules/undici/types/retry-handler.d.ts +125 -0
- package/node_modules/undici/types/round-robin-pool.d.ts +41 -0
- package/node_modules/undici/types/snapshot-agent.d.ts +109 -0
- package/node_modules/undici/types/socks5-proxy-agent.d.ts +25 -0
- package/node_modules/undici/types/util.d.ts +18 -0
- package/node_modules/undici/types/utility.d.ts +7 -0
- package/node_modules/undici/types/webidl.d.ts +347 -0
- package/node_modules/undici/types/websocket.d.ts +188 -0
- package/node_modules/ws/LICENSE +20 -0
- package/node_modules/ws/README.md +548 -0
- package/node_modules/ws/browser.js +8 -0
- package/node_modules/ws/index.js +22 -0
- package/node_modules/ws/lib/buffer-util.js +131 -0
- package/node_modules/ws/lib/constants.js +19 -0
- package/node_modules/ws/lib/event-target.js +292 -0
- package/node_modules/ws/lib/extension.js +203 -0
- package/node_modules/ws/lib/limiter.js +55 -0
- package/node_modules/ws/lib/permessage-deflate.js +530 -0
- package/node_modules/ws/lib/receiver.js +743 -0
- package/node_modules/ws/lib/sender.js +607 -0
- package/node_modules/ws/lib/stream.js +161 -0
- package/node_modules/ws/lib/subprotocol.js +62 -0
- package/node_modules/ws/lib/validation.js +152 -0
- package/node_modules/ws/lib/websocket-server.js +562 -0
- package/node_modules/ws/lib/websocket.js +1407 -0
- package/node_modules/ws/package.json +74 -0
- package/node_modules/ws/wrapper.mjs +21 -0
- package/package.json +17 -4
- package/webui/assets/group-bot-profile-CYK81678.jpg +0 -0
- package/webui/assets/group-bot-settings-BBQBbfgh.jpg +0 -0
- package/webui/assets/index-CuH0UYYo.js +28 -0
- package/webui/assets/index-DjbPiYmB.css +2 -0
- package/webui/favicon.ico +0 -0
- package/webui/index.html +26 -0
- package/webui/logo-128.webp +0 -0
- package/webui/logo-192.webp +0 -0
- package/webui/logo-256.webp +0 -0
- package/webui/logo-64.webp +0 -0
- package/webui/logo.jpg +0 -0
- package/webui/plugin-shared/plugin-sdk-webui.js +3 -0
- package/webui/plugin-shared/react-dom.js +3 -0
- package/webui/plugin-shared/react-jsx-runtime.js +3 -0
- package/webui/plugin-shared/react.js +3 -0
- package//344/275/277/347/224/250/350/257/264/346/230/216.txt +6 -0
- package//346/233/264/346/226/260/346/227/245/345/277/227.txt +1091 -0
- package/README.md +0 -3
|
@@ -0,0 +1,656 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
5
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
// src/index.ts
|
|
9
|
+
import { Binary, clone, deepEqual, filterKeys, isNullable, isPlainObject, pick, valueMap } from "cosmokit";
|
|
10
|
+
var require_index = __commonJS({
|
|
11
|
+
"src/index.ts"(exports, module) {
|
|
12
|
+
var kSchema = Symbol.for("schemastery");
|
|
13
|
+
var kValidationError = Symbol.for("ValidationError");
|
|
14
|
+
globalThis.__schemastery_index__ ??= 0;
|
|
15
|
+
globalThis.__schemastery_refs__ = void 0;
|
|
16
|
+
var ValidationError = class extends TypeError {
|
|
17
|
+
constructor(message, options) {
|
|
18
|
+
let prefix = "$";
|
|
19
|
+
for (const segment of options.path || []) {
|
|
20
|
+
if (typeof segment === "string") {
|
|
21
|
+
prefix += "." + segment;
|
|
22
|
+
} else if (typeof segment === "number") {
|
|
23
|
+
prefix += "[" + segment + "]";
|
|
24
|
+
} else if (typeof segment === "symbol") {
|
|
25
|
+
prefix += `[Symbol(${segment.toString()})]`;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (prefix.startsWith(".")) prefix = prefix.slice(1);
|
|
29
|
+
super((prefix === "$" ? "" : `${prefix} `) + message);
|
|
30
|
+
this.options = options;
|
|
31
|
+
}
|
|
32
|
+
static {
|
|
33
|
+
__name(this, "ValidationError");
|
|
34
|
+
}
|
|
35
|
+
name = "ValidationError";
|
|
36
|
+
static is(error) {
|
|
37
|
+
return !!error?.[kValidationError];
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
Object.defineProperty(ValidationError.prototype, kValidationError, {
|
|
41
|
+
value: true
|
|
42
|
+
});
|
|
43
|
+
var Schema = /* @__PURE__ */ __name(function(options) {
|
|
44
|
+
const schema = /* @__PURE__ */ __name(function(data, options2 = {}) {
|
|
45
|
+
return Schema.resolve(data, schema, options2)[0];
|
|
46
|
+
}, "schema");
|
|
47
|
+
if (options.refs) {
|
|
48
|
+
const refs = valueMap(options.refs, (options2) => new Schema(options2));
|
|
49
|
+
const getRef = /* @__PURE__ */ __name((uid) => refs[uid], "getRef");
|
|
50
|
+
for (const key in refs) {
|
|
51
|
+
const options2 = refs[key];
|
|
52
|
+
options2.sKey = getRef(options2.sKey);
|
|
53
|
+
options2.inner = getRef(options2.inner);
|
|
54
|
+
options2.list = options2.list && options2.list.map(getRef);
|
|
55
|
+
options2.dict = options2.dict && valueMap(options2.dict, getRef);
|
|
56
|
+
}
|
|
57
|
+
return refs[options.uid];
|
|
58
|
+
}
|
|
59
|
+
Object.assign(schema, options);
|
|
60
|
+
if (typeof schema.callback === "string") {
|
|
61
|
+
try {
|
|
62
|
+
schema.callback = new Function("return " + schema.callback)();
|
|
63
|
+
} catch {
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
Object.defineProperty(schema, "uid", { value: globalThis.__schemastery_index__++ });
|
|
67
|
+
Object.setPrototypeOf(schema, Schema.prototype);
|
|
68
|
+
schema.meta ||= {};
|
|
69
|
+
schema.toString = schema.toString.bind(schema);
|
|
70
|
+
return schema;
|
|
71
|
+
}, "Schema");
|
|
72
|
+
Schema.prototype = Object.create(Function.prototype);
|
|
73
|
+
Schema.prototype[kSchema] = true;
|
|
74
|
+
Object.defineProperty(Schema.prototype, "~standard", {
|
|
75
|
+
get() {
|
|
76
|
+
return {
|
|
77
|
+
version: 1,
|
|
78
|
+
vendor: "schemastery",
|
|
79
|
+
validate: /* @__PURE__ */ __name((value) => {
|
|
80
|
+
try {
|
|
81
|
+
return { value: Schema.resolve(value, this, {})[0] };
|
|
82
|
+
} catch (error) {
|
|
83
|
+
if (ValidationError.is(error)) {
|
|
84
|
+
return { issues: [{ message: error.message, path: error.options.path }] };
|
|
85
|
+
}
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
}, "validate")
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
Schema.ValidationError = ValidationError;
|
|
93
|
+
Schema.prototype.toJSON = /* @__PURE__ */ __name(function toJSON() {
|
|
94
|
+
if (globalThis.__schemastery_refs__) {
|
|
95
|
+
globalThis.__schemastery_refs__[this.uid] ??= JSON.parse(JSON.stringify({ ...this }));
|
|
96
|
+
return this.uid;
|
|
97
|
+
}
|
|
98
|
+
globalThis.__schemastery_refs__ = { [this.uid]: { ...this } };
|
|
99
|
+
globalThis.__schemastery_refs__[this.uid] = JSON.parse(JSON.stringify({ ...this }));
|
|
100
|
+
const result = { uid: this.uid, refs: globalThis.__schemastery_refs__ };
|
|
101
|
+
globalThis.__schemastery_refs__ = void 0;
|
|
102
|
+
return result;
|
|
103
|
+
}, "toJSON");
|
|
104
|
+
Schema.prototype.set = /* @__PURE__ */ __name(function set(key, value) {
|
|
105
|
+
this.dict[key] = value;
|
|
106
|
+
return this;
|
|
107
|
+
}, "set");
|
|
108
|
+
Schema.prototype.push = /* @__PURE__ */ __name(function push(value) {
|
|
109
|
+
this.list.push(value);
|
|
110
|
+
return this;
|
|
111
|
+
}, "push");
|
|
112
|
+
function mergeDesc(original, messages) {
|
|
113
|
+
const result = typeof original === "string" ? { "": original } : { ...original };
|
|
114
|
+
for (const locale in messages) {
|
|
115
|
+
const value = messages[locale];
|
|
116
|
+
if (value?.$description || value?.$desc) {
|
|
117
|
+
result[locale] = value.$description || value.$desc;
|
|
118
|
+
} else if (typeof value === "string") {
|
|
119
|
+
result[locale] = value;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return result;
|
|
123
|
+
}
|
|
124
|
+
__name(mergeDesc, "mergeDesc");
|
|
125
|
+
function getInner(value) {
|
|
126
|
+
return value?.$value ?? value?.$inner;
|
|
127
|
+
}
|
|
128
|
+
__name(getInner, "getInner");
|
|
129
|
+
function extractKeys(data) {
|
|
130
|
+
return filterKeys(data ?? {}, (key) => !key.startsWith("$"));
|
|
131
|
+
}
|
|
132
|
+
__name(extractKeys, "extractKeys");
|
|
133
|
+
Schema.prototype.i18n = /* @__PURE__ */ __name(function i18n(messages) {
|
|
134
|
+
const schema = Schema(this);
|
|
135
|
+
const desc = mergeDesc(schema.meta.description, messages);
|
|
136
|
+
if (Object.keys(desc).length) schema.meta.description = desc;
|
|
137
|
+
if (schema.dict) {
|
|
138
|
+
schema.dict = valueMap(schema.dict, (inner, key) => {
|
|
139
|
+
return inner.i18n(valueMap(messages, (data) => getInner(data)?.[key] ?? data?.[key]));
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
if (schema.list) {
|
|
143
|
+
schema.list = schema.list.map((inner, index) => {
|
|
144
|
+
return inner.i18n(valueMap(messages, (data = {}) => {
|
|
145
|
+
if (Array.isArray(getInner(data))) return getInner(data)[index];
|
|
146
|
+
if (Array.isArray(data)) return data[index];
|
|
147
|
+
return extractKeys(data);
|
|
148
|
+
}));
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
if (schema.inner) {
|
|
152
|
+
schema.inner = schema.inner.i18n(valueMap(messages, (data) => {
|
|
153
|
+
if (getInner(data)) return getInner(data);
|
|
154
|
+
return extractKeys(data);
|
|
155
|
+
}));
|
|
156
|
+
}
|
|
157
|
+
if (schema.sKey) {
|
|
158
|
+
schema.sKey = schema.sKey.i18n(valueMap(messages, (data) => data?.$key));
|
|
159
|
+
}
|
|
160
|
+
return schema;
|
|
161
|
+
}, "i18n");
|
|
162
|
+
Schema.prototype.extra = /* @__PURE__ */ __name(function extra(key, value) {
|
|
163
|
+
const schema = Schema(this);
|
|
164
|
+
schema.meta = { ...schema.meta, [key]: value };
|
|
165
|
+
return schema;
|
|
166
|
+
}, "extra");
|
|
167
|
+
for (const key of ["required", "disabled", "collapse", "hidden", "loose"]) {
|
|
168
|
+
Object.assign(Schema.prototype, {
|
|
169
|
+
[key](value = true) {
|
|
170
|
+
const schema = Schema(this);
|
|
171
|
+
schema.meta = { ...schema.meta, [key]: value };
|
|
172
|
+
return schema;
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
Schema.prototype.deprecated = /* @__PURE__ */ __name(function deprecated() {
|
|
177
|
+
const schema = Schema(this);
|
|
178
|
+
schema.meta.badges ||= [];
|
|
179
|
+
schema.meta.badges.push({ text: "deprecated", type: "danger" });
|
|
180
|
+
return schema;
|
|
181
|
+
}, "deprecated");
|
|
182
|
+
Schema.prototype.experimental = /* @__PURE__ */ __name(function experimental() {
|
|
183
|
+
const schema = Schema(this);
|
|
184
|
+
schema.meta.badges ||= [];
|
|
185
|
+
schema.meta.badges.push({ text: "experimental", type: "warning" });
|
|
186
|
+
return schema;
|
|
187
|
+
}, "experimental");
|
|
188
|
+
Schema.prototype.pattern = /* @__PURE__ */ __name(function pattern(regexp) {
|
|
189
|
+
const schema = Schema(this);
|
|
190
|
+
const pattern2 = pick(regexp, ["source", "flags"]);
|
|
191
|
+
schema.meta = { ...schema.meta, pattern: pattern2 };
|
|
192
|
+
return schema;
|
|
193
|
+
}, "pattern");
|
|
194
|
+
Schema.prototype.simplify = /* @__PURE__ */ __name(function simplify(value) {
|
|
195
|
+
if (deepEqual(value, this.meta.default, this.type === "dict")) return null;
|
|
196
|
+
if (isNullable(value)) return value;
|
|
197
|
+
if (this.type === "object" || this.type === "dict") {
|
|
198
|
+
const result = {};
|
|
199
|
+
for (const key in value) {
|
|
200
|
+
const schema = this.type === "object" ? this.dict[key] : this.inner;
|
|
201
|
+
const item = schema?.simplify(value[key]);
|
|
202
|
+
if (this.type === "dict" || !isNullable(item)) result[key] = item;
|
|
203
|
+
}
|
|
204
|
+
if (deepEqual(result, this.meta.default, this.type === "dict")) return null;
|
|
205
|
+
return result;
|
|
206
|
+
} else if (this.type === "array" || this.type === "tuple") {
|
|
207
|
+
const result = [];
|
|
208
|
+
value.forEach((value2, index) => {
|
|
209
|
+
const schema = this.type === "array" ? this.inner : this.list[index];
|
|
210
|
+
const item = schema ? schema.simplify(value2) : value2;
|
|
211
|
+
result.push(item);
|
|
212
|
+
});
|
|
213
|
+
return result;
|
|
214
|
+
} else if (this.type === "intersect") {
|
|
215
|
+
const result = {};
|
|
216
|
+
for (const item of this.list) {
|
|
217
|
+
Object.assign(result, item.simplify(value));
|
|
218
|
+
}
|
|
219
|
+
return result;
|
|
220
|
+
} else if (this.type === "union") {
|
|
221
|
+
for (const schema of this.list) {
|
|
222
|
+
try {
|
|
223
|
+
Schema.resolve(value, schema, {});
|
|
224
|
+
return schema.simplify(value);
|
|
225
|
+
} catch {
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return value;
|
|
230
|
+
}, "simplify");
|
|
231
|
+
Schema.prototype.toString = /* @__PURE__ */ __name(function toString(inline) {
|
|
232
|
+
return formatters[this.type]?.(this, inline) ?? `Schema<${this.type}>`;
|
|
233
|
+
}, "toString");
|
|
234
|
+
Schema.prototype.role = /* @__PURE__ */ __name(function role(role, extra) {
|
|
235
|
+
const schema = Schema(this);
|
|
236
|
+
schema.meta = { ...schema.meta, role, extra };
|
|
237
|
+
return schema;
|
|
238
|
+
}, "role");
|
|
239
|
+
for (const key of ["default", "link", "comment", "description", "max", "min", "step"]) {
|
|
240
|
+
Object.assign(Schema.prototype, {
|
|
241
|
+
[key](value) {
|
|
242
|
+
const schema = Schema(this);
|
|
243
|
+
schema.meta = { ...schema.meta, [key]: value };
|
|
244
|
+
return schema;
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
var resolvers = {};
|
|
249
|
+
Schema.extend = /* @__PURE__ */ __name(function extend(type, resolve) {
|
|
250
|
+
resolvers[type] = resolve;
|
|
251
|
+
}, "extend");
|
|
252
|
+
Schema.resolve = /* @__PURE__ */ __name(function resolve(data, schema, options = {}, strict = false) {
|
|
253
|
+
if (!schema) return [data];
|
|
254
|
+
if (options.ignore?.(data, schema)) return [data];
|
|
255
|
+
if (isNullable(data) && schema.type !== "lazy") {
|
|
256
|
+
if (schema.meta.required) throw new ValidationError(`missing required value`, options);
|
|
257
|
+
let current = schema;
|
|
258
|
+
let fallback = schema.meta.default;
|
|
259
|
+
while (current?.type === "intersect" && isNullable(fallback)) {
|
|
260
|
+
current = current.list[0];
|
|
261
|
+
fallback = current?.meta.default;
|
|
262
|
+
}
|
|
263
|
+
if (isNullable(fallback)) return [data];
|
|
264
|
+
data = clone(fallback);
|
|
265
|
+
}
|
|
266
|
+
const callback = resolvers[schema.type];
|
|
267
|
+
if (!callback) throw new ValidationError(`unsupported type "${schema.type}"`, options);
|
|
268
|
+
try {
|
|
269
|
+
return callback(data, schema, options, strict);
|
|
270
|
+
} catch (error) {
|
|
271
|
+
if (!schema.meta.loose) throw error;
|
|
272
|
+
return [schema.meta.default];
|
|
273
|
+
}
|
|
274
|
+
}, "resolve");
|
|
275
|
+
Schema.from = /* @__PURE__ */ __name(function from(source) {
|
|
276
|
+
if (isNullable(source)) {
|
|
277
|
+
return Schema.any();
|
|
278
|
+
} else if (["string", "number", "boolean"].includes(typeof source)) {
|
|
279
|
+
return Schema.const(source).required();
|
|
280
|
+
} else if (source[kSchema]) {
|
|
281
|
+
return source;
|
|
282
|
+
} else if (typeof source === "function") {
|
|
283
|
+
switch (source) {
|
|
284
|
+
case String:
|
|
285
|
+
return Schema.string().required();
|
|
286
|
+
case Number:
|
|
287
|
+
return Schema.number().required();
|
|
288
|
+
case Boolean:
|
|
289
|
+
return Schema.boolean().required();
|
|
290
|
+
case Function:
|
|
291
|
+
return Schema.function().required();
|
|
292
|
+
default:
|
|
293
|
+
return Schema.is(source).required();
|
|
294
|
+
}
|
|
295
|
+
} else {
|
|
296
|
+
throw new TypeError(`cannot infer schema from ${source}`);
|
|
297
|
+
}
|
|
298
|
+
}, "from");
|
|
299
|
+
Schema.lazy = /* @__PURE__ */ __name(function lazy(builder) {
|
|
300
|
+
const toJSON = /* @__PURE__ */ __name(() => {
|
|
301
|
+
if (!schema.inner[kSchema]) {
|
|
302
|
+
schema.inner = schema.builder();
|
|
303
|
+
schema.inner.meta = { ...schema.meta, ...schema.inner.meta };
|
|
304
|
+
}
|
|
305
|
+
return schema.inner.toJSON();
|
|
306
|
+
}, "toJSON");
|
|
307
|
+
const schema = new Schema({ type: "lazy", builder, inner: { toJSON } });
|
|
308
|
+
return schema;
|
|
309
|
+
}, "lazy");
|
|
310
|
+
Schema.natural = /* @__PURE__ */ __name(function natural() {
|
|
311
|
+
return Schema.number().step(1).min(0);
|
|
312
|
+
}, "natural");
|
|
313
|
+
Schema.percent = /* @__PURE__ */ __name(function percent() {
|
|
314
|
+
return Schema.number().step(0.01).min(0).max(1).role("slider");
|
|
315
|
+
}, "percent");
|
|
316
|
+
Schema.date = /* @__PURE__ */ __name(function date() {
|
|
317
|
+
return Schema.union([
|
|
318
|
+
Schema.is(Date),
|
|
319
|
+
Schema.transform(Schema.string().role("datetime"), (value, options) => {
|
|
320
|
+
const date2 = new Date(value);
|
|
321
|
+
if (isNaN(+date2)) throw new ValidationError(`invalid date "${value}"`, options);
|
|
322
|
+
return date2;
|
|
323
|
+
}, true)
|
|
324
|
+
]);
|
|
325
|
+
}, "date");
|
|
326
|
+
Schema.regExp = /* @__PURE__ */ __name(function regExp(flag = "") {
|
|
327
|
+
return Schema.union([
|
|
328
|
+
Schema.is(RegExp),
|
|
329
|
+
Schema.transform(Schema.string().role("regexp", { flag }), (value, options) => {
|
|
330
|
+
try {
|
|
331
|
+
return new RegExp(value, flag);
|
|
332
|
+
} catch (e) {
|
|
333
|
+
throw new ValidationError(e.message, options);
|
|
334
|
+
}
|
|
335
|
+
}, true)
|
|
336
|
+
]);
|
|
337
|
+
}, "regExp");
|
|
338
|
+
Schema.arrayBuffer = /* @__PURE__ */ __name(function arrayBuffer(encoding) {
|
|
339
|
+
return Schema.union([
|
|
340
|
+
Schema.is(ArrayBuffer),
|
|
341
|
+
Schema.is(SharedArrayBuffer),
|
|
342
|
+
Schema.transform(Schema.any(), (value, options) => {
|
|
343
|
+
if (Binary.isSource(value)) return Binary.fromSource(value);
|
|
344
|
+
throw new ValidationError(`expected ArrayBufferSource but got ${value}`, options);
|
|
345
|
+
}, true),
|
|
346
|
+
...encoding ? [Schema.transform(Schema.string(), (value, options) => {
|
|
347
|
+
try {
|
|
348
|
+
return encoding === "base64" ? Binary.fromBase64(value) : Binary.fromHex(value);
|
|
349
|
+
} catch (e) {
|
|
350
|
+
throw new ValidationError(e.message, options);
|
|
351
|
+
}
|
|
352
|
+
}, true)] : []
|
|
353
|
+
]);
|
|
354
|
+
}, "arrayBuffer");
|
|
355
|
+
Schema.extend("lazy", (data, schema, options, strict) => {
|
|
356
|
+
if (!schema.inner[kSchema]) {
|
|
357
|
+
schema.inner = schema.builder();
|
|
358
|
+
schema.inner.meta = { ...schema.meta, ...schema.inner.meta };
|
|
359
|
+
}
|
|
360
|
+
return Schema.resolve(data, schema.inner, options, strict);
|
|
361
|
+
});
|
|
362
|
+
Schema.extend("any", (data) => {
|
|
363
|
+
return [data];
|
|
364
|
+
});
|
|
365
|
+
Schema.extend("never", (data, _, options) => {
|
|
366
|
+
throw new ValidationError(`expected nullable but got ${data}`, options);
|
|
367
|
+
});
|
|
368
|
+
Schema.extend("const", (data, { value }, options) => {
|
|
369
|
+
if (deepEqual(data, value)) return [value];
|
|
370
|
+
throw new ValidationError(`expected ${value} but got ${data}`, options);
|
|
371
|
+
});
|
|
372
|
+
function checkWithinRange(data, meta, description, options, skipMin = false) {
|
|
373
|
+
const { max = Infinity, min = -Infinity } = meta;
|
|
374
|
+
if (data > max) throw new ValidationError(`expected ${description} <= ${max} but got ${data}`, options);
|
|
375
|
+
if (data < min && !skipMin) throw new ValidationError(`expected ${description} >= ${min} but got ${data}`, options);
|
|
376
|
+
}
|
|
377
|
+
__name(checkWithinRange, "checkWithinRange");
|
|
378
|
+
Schema.extend("string", (data, { meta }, options) => {
|
|
379
|
+
if (typeof data !== "string") throw new ValidationError(`expected string but got ${data}`, options);
|
|
380
|
+
if (meta.pattern) {
|
|
381
|
+
const regexp = new RegExp(meta.pattern.source, meta.pattern.flags);
|
|
382
|
+
if (!regexp.test(data)) throw new ValidationError(`expect string to match regexp ${regexp}`, options);
|
|
383
|
+
}
|
|
384
|
+
checkWithinRange(data.length, meta, "string length", options);
|
|
385
|
+
return [data];
|
|
386
|
+
});
|
|
387
|
+
function decimalShift(data, digits) {
|
|
388
|
+
const str = data.toString();
|
|
389
|
+
if (str.includes("e")) return data * Math.pow(10, digits);
|
|
390
|
+
const index = str.indexOf(".");
|
|
391
|
+
if (index === -1) return data * Math.pow(10, digits);
|
|
392
|
+
const frac = str.slice(index + 1);
|
|
393
|
+
const integer = str.slice(0, index);
|
|
394
|
+
if (frac.length <= digits) return +(integer + frac.padEnd(digits, "0"));
|
|
395
|
+
return +(integer + frac.slice(0, digits) + "." + frac.slice(digits));
|
|
396
|
+
}
|
|
397
|
+
__name(decimalShift, "decimalShift");
|
|
398
|
+
function isMultipleOf(data, min, step) {
|
|
399
|
+
step = Math.abs(step);
|
|
400
|
+
if (!/^\d+\.\d+$/.test(step.toString())) {
|
|
401
|
+
return (data - min) % step === 0;
|
|
402
|
+
}
|
|
403
|
+
const index = step.toString().indexOf(".");
|
|
404
|
+
const digits = step.toString().slice(index + 1).length;
|
|
405
|
+
return Math.abs(decimalShift(data, digits) - decimalShift(min, digits)) % decimalShift(step, digits) === 0;
|
|
406
|
+
}
|
|
407
|
+
__name(isMultipleOf, "isMultipleOf");
|
|
408
|
+
Schema.extend("number", (data, { meta }, options) => {
|
|
409
|
+
if (typeof data !== "number") throw new ValidationError(`expected number but got ${data}`, options);
|
|
410
|
+
checkWithinRange(data, meta, "number", options);
|
|
411
|
+
const { step } = meta;
|
|
412
|
+
if (step && !isMultipleOf(data, meta.min ?? 0, step)) {
|
|
413
|
+
throw new ValidationError(`expected number multiple of ${step} but got ${data}`, options);
|
|
414
|
+
}
|
|
415
|
+
return [data];
|
|
416
|
+
});
|
|
417
|
+
Schema.extend("boolean", (data, _, options) => {
|
|
418
|
+
if (typeof data === "boolean") return [data];
|
|
419
|
+
throw new ValidationError(`expected boolean but got ${data}`, options);
|
|
420
|
+
});
|
|
421
|
+
Schema.extend("bitset", (data, { bits, meta }, options) => {
|
|
422
|
+
let value = 0, keys = [];
|
|
423
|
+
if (typeof data === "number") {
|
|
424
|
+
value = data;
|
|
425
|
+
for (const key in bits) {
|
|
426
|
+
if (data & bits[key]) {
|
|
427
|
+
keys.push(key);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
} else if (Array.isArray(data)) {
|
|
431
|
+
keys = data;
|
|
432
|
+
for (const key of keys) {
|
|
433
|
+
if (typeof key !== "string") throw new ValidationError(`expected string but got ${key}`, options);
|
|
434
|
+
if (key in bits) value |= bits[key];
|
|
435
|
+
}
|
|
436
|
+
} else {
|
|
437
|
+
throw new ValidationError(`expected number or array but got ${data}`, options);
|
|
438
|
+
}
|
|
439
|
+
if (value === meta.default) return [value];
|
|
440
|
+
return [value, keys];
|
|
441
|
+
});
|
|
442
|
+
Schema.extend("function", (data, _, options) => {
|
|
443
|
+
if (typeof data === "function") return [data];
|
|
444
|
+
throw new ValidationError(`expected function but got ${data}`, options);
|
|
445
|
+
});
|
|
446
|
+
Schema.extend("is", (data, { constructor }, options) => {
|
|
447
|
+
if (typeof constructor === "function") {
|
|
448
|
+
if (data instanceof constructor) return [data];
|
|
449
|
+
throw new ValidationError(`expected ${constructor.name} but got ${data}`, options);
|
|
450
|
+
} else {
|
|
451
|
+
if (isNullable(data)) {
|
|
452
|
+
throw new ValidationError(`expected ${constructor} but got ${data}`, options);
|
|
453
|
+
}
|
|
454
|
+
let prototype = Object.getPrototypeOf(data);
|
|
455
|
+
while (prototype) {
|
|
456
|
+
if (prototype.constructor?.name === constructor) return [data];
|
|
457
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
458
|
+
}
|
|
459
|
+
throw new ValidationError(`expected ${constructor} but got ${data}`, options);
|
|
460
|
+
}
|
|
461
|
+
});
|
|
462
|
+
function property(data, key, schema, options) {
|
|
463
|
+
try {
|
|
464
|
+
const [value, adapted] = Schema.resolve(data[key], schema, {
|
|
465
|
+
...options,
|
|
466
|
+
path: [...options.path || [], key]
|
|
467
|
+
});
|
|
468
|
+
if (adapted !== void 0) data[key] = adapted;
|
|
469
|
+
return value;
|
|
470
|
+
} catch (e) {
|
|
471
|
+
if (!options?.autofix) throw e;
|
|
472
|
+
delete data[key];
|
|
473
|
+
return schema.meta.default;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
__name(property, "property");
|
|
477
|
+
Schema.extend("array", (data, { inner, meta }, options) => {
|
|
478
|
+
if (!Array.isArray(data)) throw new ValidationError(`expected array but got ${data}`, options);
|
|
479
|
+
checkWithinRange(data.length, meta, "array length", options, !isNullable(inner.meta.default));
|
|
480
|
+
return [data.map((_, index) => property(data, index, inner, options))];
|
|
481
|
+
});
|
|
482
|
+
Schema.extend("dict", (data, { inner, sKey }, options, strict) => {
|
|
483
|
+
if (!isPlainObject(data)) throw new ValidationError(`expected object but got ${data}`, options);
|
|
484
|
+
const result = {};
|
|
485
|
+
for (const key in data) {
|
|
486
|
+
let rKey;
|
|
487
|
+
try {
|
|
488
|
+
rKey = Schema.resolve(key, sKey, options)[0];
|
|
489
|
+
} catch (error) {
|
|
490
|
+
if (strict) continue;
|
|
491
|
+
throw error;
|
|
492
|
+
}
|
|
493
|
+
result[rKey] = property(data, key, inner, options);
|
|
494
|
+
data[rKey] = data[key];
|
|
495
|
+
if (key !== rKey) delete data[key];
|
|
496
|
+
}
|
|
497
|
+
return [result];
|
|
498
|
+
});
|
|
499
|
+
Schema.extend("tuple", (data, { list }, options, strict) => {
|
|
500
|
+
if (!Array.isArray(data)) throw new ValidationError(`expected array but got ${data}`, options);
|
|
501
|
+
const result = list.map((inner, index) => property(data, index, inner, options));
|
|
502
|
+
if (strict) return [result];
|
|
503
|
+
result.push(...data.slice(list.length));
|
|
504
|
+
return [result];
|
|
505
|
+
});
|
|
506
|
+
function merge(result, data) {
|
|
507
|
+
for (const key in data) {
|
|
508
|
+
if (key in result) continue;
|
|
509
|
+
result[key] = data[key];
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
__name(merge, "merge");
|
|
513
|
+
Schema.extend("object", (data, { dict }, options, strict) => {
|
|
514
|
+
if (!isPlainObject(data)) throw new ValidationError(`expected object but got ${data}`, options);
|
|
515
|
+
const result = {};
|
|
516
|
+
for (const key in dict) {
|
|
517
|
+
const value = property(data, key, dict[key], options);
|
|
518
|
+
if (!isNullable(value) || key in data) {
|
|
519
|
+
result[key] = value;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
if (!strict) merge(result, data);
|
|
523
|
+
return [result];
|
|
524
|
+
});
|
|
525
|
+
Schema.extend("union", (data, { list, toString }, options, strict) => {
|
|
526
|
+
const messages = [];
|
|
527
|
+
for (const inner of list) {
|
|
528
|
+
try {
|
|
529
|
+
return Schema.resolve(data, inner, options, strict);
|
|
530
|
+
} catch (error) {
|
|
531
|
+
messages.push(error);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
throw new ValidationError(`expected ${toString()} but got ${JSON.stringify(data)}`, options);
|
|
535
|
+
});
|
|
536
|
+
Schema.extend("intersect", (data, { list, toString }, options, strict) => {
|
|
537
|
+
if (!list.length) return [data];
|
|
538
|
+
let result;
|
|
539
|
+
for (const inner of list) {
|
|
540
|
+
const value = Schema.resolve(data, inner, options, true)[0];
|
|
541
|
+
if (isNullable(value)) continue;
|
|
542
|
+
if (isNullable(result)) {
|
|
543
|
+
result = value;
|
|
544
|
+
} else if (typeof result !== typeof value) {
|
|
545
|
+
throw new ValidationError(`expected ${toString()} but got ${JSON.stringify(data)}`, options);
|
|
546
|
+
} else if (typeof value === "object") {
|
|
547
|
+
merge(result ??= {}, value);
|
|
548
|
+
} else if (result !== value) {
|
|
549
|
+
throw new ValidationError(`expected ${toString()} but got ${JSON.stringify(data)}`, options);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
if (!strict && isPlainObject(data)) merge(result, data);
|
|
553
|
+
return [result];
|
|
554
|
+
});
|
|
555
|
+
Schema.extend("transform", (data, { inner, callback, preserve }, options) => {
|
|
556
|
+
const [result, adapted = data] = Schema.resolve(data, inner, options, true);
|
|
557
|
+
if (preserve) {
|
|
558
|
+
return [callback(result)];
|
|
559
|
+
} else {
|
|
560
|
+
return [callback(result), callback(adapted)];
|
|
561
|
+
}
|
|
562
|
+
});
|
|
563
|
+
var formatters = {};
|
|
564
|
+
function defineMethod(name, keys, format) {
|
|
565
|
+
formatters[name] = format;
|
|
566
|
+
Object.assign(Schema, {
|
|
567
|
+
[name](...args) {
|
|
568
|
+
const schema = new Schema({ type: name });
|
|
569
|
+
keys.forEach((key, index) => {
|
|
570
|
+
switch (key) {
|
|
571
|
+
case "sKey":
|
|
572
|
+
schema.sKey = args[index] ?? Schema.string();
|
|
573
|
+
break;
|
|
574
|
+
case "inner":
|
|
575
|
+
schema.inner = Schema.from(args[index]);
|
|
576
|
+
break;
|
|
577
|
+
case "list":
|
|
578
|
+
schema.list = args[index].map(Schema.from);
|
|
579
|
+
break;
|
|
580
|
+
case "dict":
|
|
581
|
+
schema.dict = valueMap(args[index], Schema.from);
|
|
582
|
+
break;
|
|
583
|
+
case "bits": {
|
|
584
|
+
schema.bits = {};
|
|
585
|
+
for (const key2 in args[index]) {
|
|
586
|
+
if (typeof args[index][key2] !== "number") continue;
|
|
587
|
+
schema.bits[key2] = args[index][key2];
|
|
588
|
+
}
|
|
589
|
+
break;
|
|
590
|
+
}
|
|
591
|
+
case "callback": {
|
|
592
|
+
const callback = schema.callback = args[index];
|
|
593
|
+
callback["toJSON"] ||= () => callback.toString();
|
|
594
|
+
break;
|
|
595
|
+
}
|
|
596
|
+
case "constructor": {
|
|
597
|
+
const constructor = schema.constructor = args[index];
|
|
598
|
+
if (typeof constructor === "function") {
|
|
599
|
+
;
|
|
600
|
+
constructor["toJSON"] ||= () => constructor["name"];
|
|
601
|
+
}
|
|
602
|
+
break;
|
|
603
|
+
}
|
|
604
|
+
default:
|
|
605
|
+
schema[key] = args[index];
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
if (name === "object" || name === "dict") {
|
|
609
|
+
schema.meta.default = {};
|
|
610
|
+
} else if (name === "array" || name === "tuple") {
|
|
611
|
+
schema.meta.default = [];
|
|
612
|
+
} else if (name === "bitset") {
|
|
613
|
+
schema.meta.default = 0;
|
|
614
|
+
}
|
|
615
|
+
return schema;
|
|
616
|
+
}
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
__name(defineMethod, "defineMethod");
|
|
620
|
+
defineMethod("is", ["constructor"], ({ constructor }) => {
|
|
621
|
+
if (typeof constructor === "function") {
|
|
622
|
+
return constructor.name;
|
|
623
|
+
} else {
|
|
624
|
+
return constructor;
|
|
625
|
+
}
|
|
626
|
+
});
|
|
627
|
+
defineMethod("any", [], () => "any");
|
|
628
|
+
defineMethod("never", [], () => "never");
|
|
629
|
+
defineMethod("const", ["value"], ({ value }) => typeof value === "string" ? JSON.stringify(value) : value);
|
|
630
|
+
defineMethod("string", [], () => "string");
|
|
631
|
+
defineMethod("number", [], () => "number");
|
|
632
|
+
defineMethod("boolean", [], () => "boolean");
|
|
633
|
+
defineMethod("bitset", ["bits"], () => "bitset");
|
|
634
|
+
defineMethod("function", [], () => "function");
|
|
635
|
+
defineMethod("array", ["inner"], ({ inner }) => `${inner.toString(true)}[]`);
|
|
636
|
+
defineMethod("dict", ["inner", "sKey"], ({ inner, sKey }) => `{ [key: ${sKey.toString()}]: ${inner.toString()} }`);
|
|
637
|
+
defineMethod("tuple", ["list"], ({ list }) => `[${list.map((inner) => inner.toString()).join(", ")}]`);
|
|
638
|
+
defineMethod("object", ["dict"], ({ dict }) => {
|
|
639
|
+
if (Object.keys(dict).length === 0) return "{}";
|
|
640
|
+
return `{ ${Object.entries(dict).map(([key, inner]) => {
|
|
641
|
+
return `${key}${inner.meta.required ? "" : "?"}: ${inner.toString()}`;
|
|
642
|
+
}).join(", ")} }`;
|
|
643
|
+
});
|
|
644
|
+
defineMethod("union", ["list"], ({ list }, inline) => {
|
|
645
|
+
const result = list.map(({ toString: format }) => format()).join(" | ");
|
|
646
|
+
return inline ? `(${result})` : result;
|
|
647
|
+
});
|
|
648
|
+
defineMethod("intersect", ["list"], ({ list }) => {
|
|
649
|
+
return `${list.map((inner) => inner.toString(true)).join(" & ")}`;
|
|
650
|
+
});
|
|
651
|
+
defineMethod("transform", ["inner", "callback", "preserve"], ({ inner }, isInner) => inner.toString(isInner));
|
|
652
|
+
module.exports = Schema;
|
|
653
|
+
}
|
|
654
|
+
});
|
|
655
|
+
export default require_index();
|
|
656
|
+
//# sourceMappingURL=index.mjs.map
|