effect 4.0.2 → 4.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +24 -0
- package/CLAUDE.md +24 -0
- package/ai-docs/package.json +2 -2
- package/ai-docs/src/52_graphql/10_basics.ts +111 -0
- package/ai-docs/src/52_graphql/20_testing.ts +45 -0
- package/ai-docs/src/52_graphql/30_batching.ts +116 -0
- package/ai-docs/src/52_graphql/40_aliased-batching.ts +122 -0
- package/ai-docs/src/52_graphql/50_errors-and-retries.ts +74 -0
- package/ai-docs/src/52_graphql/index.md +6 -0
- package/dist/Context.d.ts.map +1 -1
- package/dist/Context.js +12 -3
- package/dist/Context.js.map +1 -1
- package/dist/Crypto.d.ts +33 -5
- package/dist/Crypto.d.ts.map +1 -1
- package/dist/Crypto.js +8 -2
- package/dist/Crypto.js.map +1 -1
- package/dist/DateTime.d.ts +9 -6
- package/dist/DateTime.d.ts.map +1 -1
- package/dist/DateTime.js +9 -6
- package/dist/DateTime.js.map +1 -1
- package/dist/Duration.d.ts +6 -6
- package/dist/Duration.d.ts.map +1 -1
- package/dist/Duration.js +27 -8
- package/dist/Duration.js.map +1 -1
- package/dist/FileSystem.d.ts +44 -2
- package/dist/FileSystem.d.ts.map +1 -1
- package/dist/FileSystem.js.map +1 -1
- package/dist/RequestResolver.d.ts.map +1 -1
- package/dist/RequestResolver.js +4 -10
- package/dist/RequestResolver.js.map +1 -1
- package/dist/Scheduler.d.ts +9 -0
- package/dist/Scheduler.d.ts.map +1 -1
- package/dist/Scheduler.js +9 -0
- package/dist/Scheduler.js.map +1 -1
- package/dist/Schema.d.ts +481 -1
- package/dist/Schema.d.ts.map +1 -1
- package/dist/Schema.js +362 -4
- package/dist/Schema.js.map +1 -1
- package/dist/SchemaGetter.d.ts +4 -0
- package/dist/SchemaGetter.d.ts.map +1 -1
- package/dist/SchemaGetter.js +52 -33
- package/dist/SchemaGetter.js.map +1 -1
- package/dist/Sink.d.ts.map +1 -1
- package/dist/Sink.js +38 -17
- package/dist/Sink.js.map +1 -1
- package/dist/Stdio.d.ts +7 -1
- package/dist/Stdio.d.ts.map +1 -1
- package/dist/Stdio.js +1 -0
- package/dist/Stdio.js.map +1 -1
- package/dist/Stream.d.ts +22 -11
- package/dist/Stream.d.ts.map +1 -1
- package/dist/Stream.js +21 -6
- package/dist/Stream.js.map +1 -1
- package/dist/Utils.js +8 -3
- package/dist/Utils.js.map +1 -1
- package/dist/ai/AnthropicStructuredOutput.js +0 -2
- package/dist/ai/AnthropicStructuredOutput.js.map +1 -1
- package/dist/ai/Chat.d.ts +2 -2
- package/dist/ai/Chat.d.ts.map +1 -1
- package/dist/ai/LanguageModel.d.ts +38 -7
- package/dist/ai/LanguageModel.d.ts.map +1 -1
- package/dist/ai/LanguageModel.js +25 -7
- package/dist/ai/LanguageModel.js.map +1 -1
- package/dist/ai/McpServer.d.ts +61 -0
- package/dist/ai/McpServer.d.ts.map +1 -1
- package/dist/ai/McpServer.js +196 -44
- package/dist/ai/McpServer.js.map +1 -1
- package/dist/ai/Prompt.d.ts.map +1 -1
- package/dist/ai/Prompt.js +28 -5
- package/dist/ai/Prompt.js.map +1 -1
- package/dist/ai/Response.d.ts +4 -3
- package/dist/ai/Response.d.ts.map +1 -1
- package/dist/ai/Response.js.map +1 -1
- package/dist/ai/internal/mcpProtocol/v2026_07_28.js +5 -0
- package/dist/ai/internal/mcpProtocol/v2026_07_28.js.map +1 -1
- package/dist/ai/internal/mcpProtocol.js.map +1 -1
- package/dist/ai/internal/mcpRuntime.js +1 -0
- package/dist/ai/internal/mcpRuntime.js.map +1 -1
- package/dist/cli/CliOutput.d.ts +27 -0
- package/dist/cli/CliOutput.d.ts.map +1 -1
- package/dist/cli/CliOutput.js +33 -10
- package/dist/cli/CliOutput.js.map +1 -1
- package/dist/cli/Command.d.ts.map +1 -1
- package/dist/cli/Command.js +6 -3
- package/dist/cli/Command.js.map +1 -1
- package/dist/cli/Completions.d.ts +3 -3
- package/dist/cli/Completions.d.ts.map +1 -1
- package/dist/cli/Completions.js +8 -5
- package/dist/cli/Completions.js.map +1 -1
- package/dist/cli/GlobalFlag.d.ts +5 -4
- package/dist/cli/GlobalFlag.d.ts.map +1 -1
- package/dist/cli/GlobalFlag.js +3 -3
- package/dist/cli/GlobalFlag.js.map +1 -1
- package/dist/cli/Prompt.d.ts +17 -1
- package/dist/cli/Prompt.d.ts.map +1 -1
- package/dist/cli/Prompt.js +119 -114
- package/dist/cli/Prompt.js.map +1 -1
- package/dist/cli/internal/ansi.d.ts +0 -1
- package/dist/cli/internal/ansi.d.ts.map +1 -1
- package/dist/cli/internal/ansi.js +20 -3
- package/dist/cli/internal/ansi.js.map +1 -1
- package/dist/cli/internal/completions/powershell.d.ts +2 -0
- package/dist/cli/internal/completions/powershell.d.ts.map +1 -0
- package/dist/cli/internal/completions/powershell.js +273 -0
- package/dist/cli/internal/completions/powershell.js.map +1 -0
- package/dist/cli/internal/completions/zsh.js +3 -2
- package/dist/cli/internal/completions/zsh.js.map +1 -1
- package/dist/cli/internal/wizard.d.ts +3 -3
- package/dist/cli/internal/wizard.d.ts.map +1 -1
- package/dist/cli/internal/wizard.js +21 -18
- package/dist/cli/internal/wizard.js.map +1 -1
- package/dist/cluster/ClusterSchema.d.ts +28 -0
- package/dist/cluster/ClusterSchema.d.ts.map +1 -1
- package/dist/cluster/ClusterSchema.js +30 -0
- package/dist/cluster/ClusterSchema.js.map +1 -1
- package/dist/cluster/ClusterWorkflowEngine.d.ts.map +1 -1
- package/dist/cluster/ClusterWorkflowEngine.js +56 -26
- package/dist/cluster/ClusterWorkflowEngine.js.map +1 -1
- package/dist/cluster/MessageStorage.d.ts +18 -3
- package/dist/cluster/MessageStorage.d.ts.map +1 -1
- package/dist/cluster/MessageStorage.js +31 -11
- package/dist/cluster/MessageStorage.js.map +1 -1
- package/dist/cluster/Runners.d.ts.map +1 -1
- package/dist/cluster/Runners.js +8 -2
- package/dist/cluster/Runners.js.map +1 -1
- package/dist/cluster/Sharding.d.ts.map +1 -1
- package/dist/cluster/Sharding.js +187 -66
- package/dist/cluster/Sharding.js.map +1 -1
- package/dist/cluster/SqlMessageStorage.d.ts +59 -19
- package/dist/cluster/SqlMessageStorage.d.ts.map +1 -1
- package/dist/cluster/SqlMessageStorage.js +84 -55
- package/dist/cluster/SqlMessageStorage.js.map +1 -1
- package/dist/cluster/SqlRunnerStorage.d.ts +68 -19
- package/dist/cluster/SqlRunnerStorage.d.ts.map +1 -1
- package/dist/cluster/SqlRunnerStorage.js +191 -113
- package/dist/cluster/SqlRunnerStorage.js.map +1 -1
- package/dist/cluster/internal/entityManager.js +104 -37
- package/dist/cluster/internal/entityManager.js.map +1 -1
- package/dist/encoding/Sse.d.ts +6 -0
- package/dist/encoding/Sse.d.ts.map +1 -1
- package/dist/encoding/Sse.js +62 -70
- package/dist/encoding/Sse.js.map +1 -1
- package/dist/graphql/GraphQL.d.ts +349 -0
- package/dist/graphql/GraphQL.d.ts.map +1 -0
- package/dist/graphql/GraphQL.js +207 -0
- package/dist/graphql/GraphQL.js.map +1 -0
- package/dist/graphql/GraphQLClient.d.ts +562 -0
- package/dist/graphql/GraphQLClient.d.ts.map +1 -0
- package/dist/graphql/GraphQLClient.js +498 -0
- package/dist/graphql/GraphQLClient.js.map +1 -0
- package/dist/graphql/GraphQLClientError.d.ts +445 -0
- package/dist/graphql/GraphQLClientError.d.ts.map +1 -0
- package/dist/graphql/GraphQLClientError.js +479 -0
- package/dist/graphql/GraphQLClientError.js.map +1 -0
- package/dist/graphql/GraphQLGroup.d.ts +118 -0
- package/dist/graphql/GraphQLGroup.d.ts.map +1 -0
- package/dist/graphql/GraphQLGroup.js +109 -0
- package/dist/graphql/GraphQLGroup.js.map +1 -0
- package/dist/graphql/GraphQLMiddleware.d.ts +249 -0
- package/dist/graphql/GraphQLMiddleware.d.ts.map +1 -0
- package/dist/graphql/GraphQLMiddleware.js +130 -0
- package/dist/graphql/GraphQLMiddleware.js.map +1 -0
- package/dist/graphql/GraphQLProtocol.d.ts +731 -0
- package/dist/graphql/GraphQLProtocol.d.ts.map +1 -0
- package/dist/graphql/GraphQLProtocol.js +719 -0
- package/dist/graphql/GraphQLProtocol.js.map +1 -0
- package/dist/graphql/GraphQLResolver.d.ts +453 -0
- package/dist/graphql/GraphQLResolver.d.ts.map +1 -0
- package/dist/graphql/GraphQLResolver.js +552 -0
- package/dist/graphql/GraphQLResolver.js.map +1 -0
- package/dist/graphql/index.d.ts +40 -0
- package/dist/graphql/index.d.ts.map +1 -0
- package/dist/graphql/index.js +41 -0
- package/dist/graphql/index.js.map +1 -0
- package/dist/http/FindMyWay/internal/router.js +3 -6
- package/dist/http/FindMyWay/internal/router.js.map +1 -1
- package/dist/http/HttpClient.d.ts +21 -0
- package/dist/http/HttpClient.d.ts.map +1 -1
- package/dist/http/HttpClient.js +63 -12
- package/dist/http/HttpClient.js.map +1 -1
- package/dist/http/HttpEffect.d.ts.map +1 -1
- package/dist/http/HttpEffect.js +14 -7
- package/dist/http/HttpEffect.js.map +1 -1
- package/dist/http/HttpMiddleware.d.ts.map +1 -1
- package/dist/http/HttpMiddleware.js +31 -32
- package/dist/http/HttpMiddleware.js.map +1 -1
- package/dist/http/HttpRouter.d.ts +35 -0
- package/dist/http/HttpRouter.d.ts.map +1 -1
- package/dist/http/HttpRouter.js +35 -0
- package/dist/http/HttpRouter.js.map +1 -1
- package/dist/http/HttpServerResponse.d.ts +8 -6
- package/dist/http/HttpServerResponse.d.ts.map +1 -1
- package/dist/http/HttpServerResponse.js +27 -16
- package/dist/http/HttpServerResponse.js.map +1 -1
- package/dist/http/HttpTraceContext.d.ts.map +1 -1
- package/dist/http/HttpTraceContext.js +4 -2
- package/dist/http/HttpTraceContext.js.map +1 -1
- package/dist/http/Multipart.d.ts.map +1 -1
- package/dist/http/Multipart.js +15 -1
- package/dist/http/Multipart.js.map +1 -1
- package/dist/http-api/HttpApiScalar.d.ts +5 -0
- package/dist/http-api/HttpApiScalar.d.ts.map +1 -1
- package/dist/http-api/HttpApiScalar.js.map +1 -1
- package/dist/internal/dateTime.js +40 -35
- package/dist/internal/dateTime.js.map +1 -1
- package/dist/internal/effect.js +19 -9
- package/dist/internal/effect.js.map +1 -1
- package/dist/internal/graphql.d.ts +2 -0
- package/dist/internal/graphql.d.ts.map +1 -0
- package/dist/internal/graphql.js +195 -0
- package/dist/internal/graphql.js.map +1 -0
- package/dist/internal/graphqlHeaders.d.ts +4 -0
- package/dist/internal/graphqlHeaders.d.ts.map +1 -0
- package/dist/internal/graphqlHeaders.js +11 -0
- package/dist/internal/graphqlHeaders.js.map +1 -0
- package/dist/internal/request.js +20 -1
- package/dist/internal/request.js.map +1 -1
- package/dist/internal/schema/toCodeDocument.js +4 -1
- package/dist/internal/schema/toCodeDocument.js.map +1 -1
- package/dist/internal/schema/toJsonSchemaDocument.js +29 -5
- package/dist/internal/schema/toJsonSchemaDocument.js.map +1 -1
- package/dist/internal/version.js +1 -1
- package/dist/net/AddressResolver.d.ts +145 -0
- package/dist/net/AddressResolver.d.ts.map +1 -0
- package/dist/net/AddressResolver.js +114 -0
- package/dist/net/AddressResolver.js.map +1 -0
- package/dist/net/Dns.d.ts +596 -0
- package/dist/net/Dns.d.ts.map +1 -0
- package/dist/net/Dns.js +549 -0
- package/dist/net/Dns.js.map +1 -0
- package/dist/net/Host.d.ts +369 -0
- package/dist/net/Host.d.ts.map +1 -0
- package/dist/net/Host.js +397 -0
- package/dist/net/Host.js.map +1 -0
- package/dist/net/IpInterface.d.ts +76 -3
- package/dist/net/IpInterface.d.ts.map +1 -1
- package/dist/net/IpInterface.js +53 -4
- package/dist/net/IpInterface.js.map +1 -1
- package/dist/net/IpNetwork.d.ts +82 -8
- package/dist/net/IpNetwork.d.ts.map +1 -1
- package/dist/net/IpNetwork.js +60 -4
- package/dist/net/IpNetwork.js.map +1 -1
- package/dist/net/NetAddress.d.ts +396 -35
- package/dist/net/NetAddress.d.ts.map +1 -1
- package/dist/net/NetAddress.js +358 -34
- package/dist/net/NetAddress.js.map +1 -1
- package/dist/net/index.d.ts +15 -0
- package/dist/net/index.d.ts.map +1 -1
- package/dist/net/index.js +15 -0
- package/dist/net/index.js.map +1 -1
- package/dist/observability/OtlpMetrics.d.ts.map +1 -1
- package/dist/observability/OtlpMetrics.js +15 -21
- package/dist/observability/OtlpMetrics.js.map +1 -1
- package/dist/observability/PrometheusMetrics.js +1 -0
- package/dist/observability/PrometheusMetrics.js.map +1 -1
- package/dist/persistence/PersistedQueue.d.ts +32 -3
- package/dist/persistence/PersistedQueue.d.ts.map +1 -1
- package/dist/persistence/PersistedQueue.js +75 -16
- package/dist/persistence/PersistedQueue.js.map +1 -1
- package/dist/rpc/RpcClient.d.ts +22 -6
- package/dist/rpc/RpcClient.d.ts.map +1 -1
- package/dist/rpc/RpcClient.js +12 -5
- package/dist/rpc/RpcClient.js.map +1 -1
- package/dist/rpc/RpcServer.d.ts +6 -0
- package/dist/rpc/RpcServer.d.ts.map +1 -1
- package/dist/rpc/RpcServer.js +78 -29
- package/dist/rpc/RpcServer.js.map +1 -1
- package/dist/socket/DatagramSocket.d.ts +456 -0
- package/dist/socket/DatagramSocket.d.ts.map +1 -0
- package/dist/socket/DatagramSocket.js +535 -0
- package/dist/socket/DatagramSocket.js.map +1 -0
- package/dist/socket/Socket.d.ts +20 -1
- package/dist/socket/Socket.d.ts.map +1 -1
- package/dist/socket/Socket.js +9 -1
- package/dist/socket/Socket.js.map +1 -1
- package/dist/socket/index.d.ts +5 -0
- package/dist/socket/index.d.ts.map +1 -1
- package/dist/socket/index.js +5 -0
- package/dist/socket/index.js.map +1 -1
- package/dist/sql/Migrator.d.ts.map +1 -1
- package/dist/sql/Migrator.js +7 -2
- package/dist/sql/Migrator.js.map +1 -1
- package/dist/sql/SqlClient.d.ts +7 -1
- package/dist/sql/SqlClient.d.ts.map +1 -1
- package/dist/sql/SqlClient.js +31 -6
- package/dist/sql/SqlClient.js.map +1 -1
- package/dist/testing/TestCrypto.d.ts +73 -0
- package/dist/testing/TestCrypto.d.ts.map +1 -0
- package/dist/testing/TestCrypto.js +87 -0
- package/dist/testing/TestCrypto.js.map +1 -0
- package/dist/testing/index.d.ts +5 -0
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +5 -0
- package/dist/testing/index.js.map +1 -1
- package/dist/workflow/DurableDeferred.d.ts +54 -0
- package/dist/workflow/DurableDeferred.d.ts.map +1 -1
- package/dist/workflow/DurableDeferred.js +108 -50
- package/dist/workflow/DurableDeferred.js.map +1 -1
- package/dist/workflow/WorkflowEngine.d.ts +24 -2
- package/dist/workflow/WorkflowEngine.d.ts.map +1 -1
- package/dist/workflow/WorkflowEngine.js +13 -3
- package/dist/workflow/WorkflowEngine.js.map +1 -1
- package/package.json +6 -3
- package/src/Context.ts +12 -3
- package/src/Crypto.ts +43 -9
- package/src/DateTime.ts +9 -6
- package/src/Duration.ts +36 -16
- package/src/FileSystem.ts +48 -2
- package/src/RequestResolver.ts +4 -10
- package/src/Scheduler.ts +9 -0
- package/src/Schema.ts +677 -5
- package/src/SchemaGetter.ts +47 -35
- package/src/Sink.ts +45 -18
- package/src/Stdio.ts +9 -2
- package/src/Stream.ts +44 -15
- package/src/Utils.ts +8 -5
- package/src/ai/AnthropicStructuredOutput.ts +2 -2
- package/src/ai/Chat.ts +2 -2
- package/src/ai/LanguageModel.ts +96 -24
- package/src/ai/McpServer.ts +288 -94
- package/src/ai/Prompt.ts +24 -10
- package/src/ai/Response.ts +6 -3
- package/src/ai/internal/mcpProtocol/v2026_07_28.ts +5 -0
- package/src/ai/internal/mcpProtocol.ts +3 -0
- package/src/ai/internal/mcpRuntime.ts +2 -0
- package/src/cli/CliOutput.ts +33 -16
- package/src/cli/Command.ts +4 -3
- package/src/cli/Completions.ts +9 -6
- package/src/cli/GlobalFlag.ts +19 -11
- package/src/cli/Prompt.ts +159 -149
- package/src/cli/internal/ansi.ts +26 -3
- package/src/cli/internal/completions/powershell.ts +322 -0
- package/src/cli/internal/completions/zsh.ts +4 -2
- package/src/cli/internal/wizard.ts +34 -26
- package/src/cluster/ClusterSchema.ts +32 -0
- package/src/cluster/ClusterWorkflowEngine.ts +64 -36
- package/src/cluster/MessageStorage.ts +45 -14
- package/src/cluster/Runners.ts +8 -2
- package/src/cluster/Sharding.ts +219 -58
- package/src/cluster/SqlMessageStorage.ts +118 -84
- package/src/cluster/SqlRunnerStorage.ts +224 -128
- package/src/cluster/internal/entityManager.ts +151 -55
- package/src/encoding/Sse.ts +69 -79
- package/src/graphql/GraphQL.ts +494 -0
- package/src/graphql/GraphQLClient.ts +912 -0
- package/src/graphql/GraphQLClientError.ts +542 -0
- package/src/graphql/GraphQLGroup.ts +167 -0
- package/src/graphql/GraphQLMiddleware.ts +253 -0
- package/src/graphql/GraphQLProtocol.ts +1286 -0
- package/src/graphql/GraphQLResolver.ts +1010 -0
- package/src/graphql/index.ts +48 -0
- package/src/http/FindMyWay/internal/router.ts +4 -7
- package/src/http/HttpClient.ts +110 -12
- package/src/http/HttpEffect.ts +11 -5
- package/src/http/HttpMiddleware.ts +33 -34
- package/src/http/HttpRouter.ts +35 -0
- package/src/http/HttpServerResponse.ts +28 -16
- package/src/http/HttpTraceContext.ts +8 -2
- package/src/http/Multipart.ts +17 -1
- package/src/http-api/HttpApiScalar.ts +5 -0
- package/src/internal/dateTime.ts +13 -10
- package/src/internal/effect.ts +22 -15
- package/src/internal/graphql.ts +263 -0
- package/src/internal/graphqlHeaders.ts +11 -0
- package/src/internal/request.ts +31 -3
- package/src/internal/schema/toCodeDocument.ts +7 -1
- package/src/internal/schema/toJsonSchemaDocument.ts +34 -2
- package/src/internal/version.ts +1 -1
- package/src/net/AddressResolver.ts +222 -0
- package/src/net/Dns.ts +1035 -0
- package/src/net/Host.ts +544 -0
- package/src/net/IpInterface.ts +117 -7
- package/src/net/IpNetwork.ts +116 -17
- package/src/net/NetAddress.ts +620 -76
- package/src/net/index.ts +18 -0
- package/src/observability/OtlpMetrics.ts +12 -23
- package/src/observability/PrometheusMetrics.ts +1 -0
- package/src/persistence/PersistedQueue.ts +146 -53
- package/src/rpc/RpcClient.ts +41 -19
- package/src/rpc/RpcServer.ts +115 -40
- package/src/socket/DatagramSocket.ts +919 -0
- package/src/socket/Socket.ts +28 -2
- package/src/socket/index.ts +6 -0
- package/src/sql/Migrator.ts +21 -5
- package/src/sql/SqlClient.ts +39 -15
- package/src/testing/TestCrypto.ts +94 -0
- package/src/testing/index.ts +6 -0
- package/src/workflow/DurableDeferred.ts +160 -47
- package/src/workflow/WorkflowEngine.ts +50 -5
package/dist/net/NetAddress.js
CHANGED
|
@@ -2,9 +2,9 @@ import * as Data from "../Data.js";
|
|
|
2
2
|
import * as Equal from "../Equal.js";
|
|
3
3
|
import { dual } from "../Function.js";
|
|
4
4
|
import * as Hash from "../Hash.js";
|
|
5
|
-
import
|
|
5
|
+
import * as Inspectable from "../Inspectable.js";
|
|
6
6
|
import * as Option from "../Option.js";
|
|
7
|
-
import { hasProperty } from "../Predicate.js";
|
|
7
|
+
import { hasProperty, isTupleOf } from "../Predicate.js";
|
|
8
8
|
import * as Result from "../Result.js";
|
|
9
9
|
const TypeId = "~effect/net/NetAddress";
|
|
10
10
|
const MulticastTypeId = "~effect/net/NetAddress/MulticastAddress";
|
|
@@ -71,6 +71,32 @@ export const isIpAddress = u => isIpv4Address(u) || isIpv6Address(u);
|
|
|
71
71
|
* @since 4.0.0
|
|
72
72
|
*/
|
|
73
73
|
export const width = address => isIpv4Address(address) ? 32 : 128;
|
|
74
|
+
/**
|
|
75
|
+
* Returns the family of an IP address or internet address.
|
|
76
|
+
*
|
|
77
|
+
* **Example** (Reading address families)
|
|
78
|
+
*
|
|
79
|
+
* ```ts import.meta.vitest
|
|
80
|
+
* import { NetAddress } from "effect/net"
|
|
81
|
+
*
|
|
82
|
+
* NetAddress.familyOf(NetAddress.ipv4Loopback) // => "IPv4"
|
|
83
|
+
* NetAddress.familyOf(NetAddress.inetAddressFromStringUnsafe("[::1]:80")) // => "IPv6"
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* @stability experimental
|
|
87
|
+
* @category getters
|
|
88
|
+
* @since 4.0.0
|
|
89
|
+
*/
|
|
90
|
+
export const familyOf = self => isIpv4Address(self) || isInetAddressV4(self) ? "IPv4" : "IPv6";
|
|
91
|
+
/**
|
|
92
|
+
* Returns `true` when an IP address or internet address belongs to a family,
|
|
93
|
+
* narrowing it to that family.
|
|
94
|
+
*
|
|
95
|
+
* @stability experimental
|
|
96
|
+
* @category predicates
|
|
97
|
+
* @since 4.0.0
|
|
98
|
+
*/
|
|
99
|
+
export const isFamily = /*#__PURE__*/dual(2, (self, family) => familyOf(self) === family);
|
|
74
100
|
/**
|
|
75
101
|
* Returns `true` when a value is a MAC address.
|
|
76
102
|
*
|
|
@@ -120,6 +146,7 @@ export const isUnixPathAddress = u => isAddress(u) && u._tag === "UnixPathAddres
|
|
|
120
146
|
*/
|
|
121
147
|
export const isSocketAddress = u => isInetAddress(u) || isUnixPathAddress(u);
|
|
122
148
|
const Ipv4Proto = {
|
|
149
|
+
...Inspectable.BaseProto,
|
|
123
150
|
_tag: "Ipv4Address",
|
|
124
151
|
[TypeId]: TypeId,
|
|
125
152
|
[Equal.symbol](that) {
|
|
@@ -133,12 +160,10 @@ const Ipv4Proto = {
|
|
|
133
160
|
},
|
|
134
161
|
toJSON() {
|
|
135
162
|
return this.toString();
|
|
136
|
-
},
|
|
137
|
-
[NodeInspectSymbol]() {
|
|
138
|
-
return this.toJSON();
|
|
139
163
|
}
|
|
140
164
|
};
|
|
141
165
|
const Ipv6Proto = {
|
|
166
|
+
...Inspectable.BaseProto,
|
|
142
167
|
_tag: "Ipv6Address",
|
|
143
168
|
[TypeId]: TypeId,
|
|
144
169
|
[Equal.symbol](that) {
|
|
@@ -159,12 +184,10 @@ const Ipv6Proto = {
|
|
|
159
184
|
},
|
|
160
185
|
toJSON() {
|
|
161
186
|
return this.toString();
|
|
162
|
-
},
|
|
163
|
-
[NodeInspectSymbol]() {
|
|
164
|
-
return this.toJSON();
|
|
165
187
|
}
|
|
166
188
|
};
|
|
167
189
|
const MacProto = {
|
|
190
|
+
...Inspectable.BaseProto,
|
|
168
191
|
_tag: "MacAddress",
|
|
169
192
|
[TypeId]: TypeId,
|
|
170
193
|
[Equal.symbol](that) {
|
|
@@ -178,9 +201,6 @@ const MacProto = {
|
|
|
178
201
|
},
|
|
179
202
|
toJSON() {
|
|
180
203
|
return this.toString();
|
|
181
|
-
},
|
|
182
|
-
[NodeInspectSymbol]() {
|
|
183
|
-
return this.toJSON();
|
|
184
204
|
}
|
|
185
205
|
};
|
|
186
206
|
const bytesEqual = (self, that) => {
|
|
@@ -247,6 +267,14 @@ export const ipv4Loopback = /*#__PURE__*/makeIpv4(0x7f000001);
|
|
|
247
267
|
* @since 4.0.0
|
|
248
268
|
*/
|
|
249
269
|
export const ipv6Loopback = /*#__PURE__*/makeIpv6(0, 0, 0, 1);
|
|
270
|
+
/**
|
|
271
|
+
* The loopback addresses `127.0.0.1` and `::1`.
|
|
272
|
+
*
|
|
273
|
+
* @stability unstable
|
|
274
|
+
* @category constants
|
|
275
|
+
* @since 4.0.0
|
|
276
|
+
*/
|
|
277
|
+
export const loopbackAddresses = [ipv4Loopback, ipv6Loopback];
|
|
250
278
|
/**
|
|
251
279
|
* The unspecified IPv4 address `0.0.0.0`.
|
|
252
280
|
*
|
|
@@ -275,6 +303,14 @@ const addressError = (input, message) => Result.fail(new NetAddressError({
|
|
|
275
303
|
input,
|
|
276
304
|
message
|
|
277
305
|
}));
|
|
306
|
+
// Checks by index because `every` skips holes in sparse arrays.
|
|
307
|
+
const allIntegersInRange = (values, max) => {
|
|
308
|
+
for (let i = 0; i < values.length; i++) {
|
|
309
|
+
const n = values[i];
|
|
310
|
+
if (!Number.isInteger(n) || n < 0 || n > max) return false;
|
|
311
|
+
}
|
|
312
|
+
return true;
|
|
313
|
+
};
|
|
278
314
|
/**
|
|
279
315
|
* Creates an IPv4 address from four checked octets.
|
|
280
316
|
*
|
|
@@ -283,7 +319,7 @@ const addressError = (input, message) => Result.fail(new NetAddressError({
|
|
|
283
319
|
* @since 4.0.0
|
|
284
320
|
*/
|
|
285
321
|
export const ipv4FromOctets = octets => {
|
|
286
|
-
if (!octets
|
|
322
|
+
if (!allIntegersInRange(octets, 255)) {
|
|
287
323
|
return addressError(octets, "octets must be integers from 0 through 255");
|
|
288
324
|
}
|
|
289
325
|
return Result.succeed(makeIpv4(packOctets(octets[0], octets[1], octets[2], octets[3])));
|
|
@@ -296,7 +332,7 @@ export const ipv4FromOctets = octets => {
|
|
|
296
332
|
* @since 4.0.0
|
|
297
333
|
*/
|
|
298
334
|
export const ipv6FromSegments = segments => {
|
|
299
|
-
if (!segments
|
|
335
|
+
if (!allIntegersInRange(segments, 0xffff)) {
|
|
300
336
|
return addressError(segments, "segments must be integers from 0 through 65535");
|
|
301
337
|
}
|
|
302
338
|
return Result.succeed(makeIpv6(segments[0] * 0x10000 + segments[1], segments[2] * 0x10000 + segments[3], segments[4] * 0x10000 + segments[5], segments[6] * 0x10000 + segments[7]));
|
|
@@ -309,7 +345,7 @@ export const ipv6FromSegments = segments => {
|
|
|
309
345
|
* @since 4.0.0
|
|
310
346
|
*/
|
|
311
347
|
export const macAddressFromOctets = octets => {
|
|
312
|
-
if (!octets
|
|
348
|
+
if (!allIntegersInRange(octets, 255)) {
|
|
313
349
|
return addressError(octets, "octets must be integers from 0 through 255");
|
|
314
350
|
}
|
|
315
351
|
return Result.succeed(makeMac(new Uint8Array(octets)));
|
|
@@ -335,6 +371,29 @@ export const macAddressFromString = input => {
|
|
|
335
371
|
* @since 4.0.0
|
|
336
372
|
*/
|
|
337
373
|
export const macAddressFromStringUnsafe = input => Result.getOrThrow(macAddressFromString(input));
|
|
374
|
+
/**
|
|
375
|
+
* Converts a `MacAddressInput` to a MAC address, parsing strings like
|
|
376
|
+
* `macAddressFromString`, checking octets like `macAddressFromOctets`, and
|
|
377
|
+
* returning MAC addresses unchanged.
|
|
378
|
+
*
|
|
379
|
+
* @stability unstable
|
|
380
|
+
* @category constructors
|
|
381
|
+
* @since 4.0.0
|
|
382
|
+
*/
|
|
383
|
+
export const macAddressFromInput = input => {
|
|
384
|
+
if (isMacAddress(input)) return Result.succeed(input);
|
|
385
|
+
if (typeof input === "string") return macAddressFromString(input);
|
|
386
|
+
if (Array.isArray(input) && isTupleOf(input, 6)) return macAddressFromOctets(input);
|
|
387
|
+
return addressError(input, "expected a MAC address, string, or 6 octets");
|
|
388
|
+
};
|
|
389
|
+
/**
|
|
390
|
+
* Converts a trusted `MacAddressInput` to a MAC address, throwing on failure.
|
|
391
|
+
*
|
|
392
|
+
* @stability unstable
|
|
393
|
+
* @category unsafe
|
|
394
|
+
* @since 4.0.0
|
|
395
|
+
*/
|
|
396
|
+
export const macAddressFromInputUnsafe = input => Result.getOrThrow(macAddressFromInput(input));
|
|
338
397
|
/**
|
|
339
398
|
* Parses a strict dotted-decimal IPv4 address.
|
|
340
399
|
*
|
|
@@ -426,6 +485,127 @@ export const ipFromString = input => {
|
|
|
426
485
|
* @since 4.0.0
|
|
427
486
|
*/
|
|
428
487
|
export const ipFromStringUnsafe = input => Result.getOrThrow(ipFromString(input));
|
|
488
|
+
/**
|
|
489
|
+
* Converts an `IpAddressInput` to an IP address, returning IP addresses
|
|
490
|
+
* unchanged.
|
|
491
|
+
*
|
|
492
|
+
* **Details**
|
|
493
|
+
*
|
|
494
|
+
* Strings are parsed like `ipFromString`. Four octets or bytes make an IPv4
|
|
495
|
+
* address, and eight segments or sixteen bytes make an IPv6 address.
|
|
496
|
+
*
|
|
497
|
+
* **Example** (Converting IP address inputs)
|
|
498
|
+
*
|
|
499
|
+
* ```ts import.meta.vitest
|
|
500
|
+
* import { Result } from "effect"
|
|
501
|
+
* import { NetAddress } from "effect/net"
|
|
502
|
+
*
|
|
503
|
+
* const address = Result.getOrThrow(NetAddress.ipFromInput("192.0.2.1"))
|
|
504
|
+
* NetAddress.formatIp(address) // => "192.0.2.1"
|
|
505
|
+
* Result.getOrThrow(NetAddress.ipFromInput(address)) === address // => true
|
|
506
|
+
* NetAddress.formatIp(Result.getOrThrow(NetAddress.ipFromInput([192, 0, 2, 1]))) // => "192.0.2.1"
|
|
507
|
+
* Result.isFailure(NetAddress.ipFromInput("example.com")) // => true
|
|
508
|
+
* ```
|
|
509
|
+
*
|
|
510
|
+
* @stability unstable
|
|
511
|
+
* @category constructors
|
|
512
|
+
* @since 4.0.0
|
|
513
|
+
*/
|
|
514
|
+
export const ipFromInput = input => {
|
|
515
|
+
if (isIpAddress(input)) return Result.succeed(input);
|
|
516
|
+
if (typeof input === "string") return ipFromString(input);
|
|
517
|
+
if (input instanceof Uint8Array && input.length === 4) return Result.succeed(ipv4FromBytesUnsafe(input));
|
|
518
|
+
if (input instanceof Uint8Array && input.length === 16) return Result.succeed(ipv6FromBytesUnsafe(input));
|
|
519
|
+
if (Array.isArray(input) && isTupleOf(input, 4)) return ipv4FromOctets(input);
|
|
520
|
+
if (Array.isArray(input) && isTupleOf(input, 8)) return ipv6FromSegments(input);
|
|
521
|
+
return addressError(input, "expected an IP address, string, 4 octets, 8 segments, or 4 or 16 bytes");
|
|
522
|
+
};
|
|
523
|
+
/**
|
|
524
|
+
* Converts a trusted `IpAddressInput` to an IP address, throwing on failure.
|
|
525
|
+
*
|
|
526
|
+
* @stability unstable
|
|
527
|
+
* @category unsafe
|
|
528
|
+
* @since 4.0.0
|
|
529
|
+
*/
|
|
530
|
+
export const ipFromInputUnsafe = input => Result.getOrThrow(ipFromInput(input));
|
|
531
|
+
/**
|
|
532
|
+
* Converts an `Ipv4AddressInput` to an IPv4 address, parsing strings like
|
|
533
|
+
* `ipv4FromString`, checking octets like `ipv4FromOctets`, and returning IPv4
|
|
534
|
+
* addresses unchanged.
|
|
535
|
+
*
|
|
536
|
+
* @stability unstable
|
|
537
|
+
* @category constructors
|
|
538
|
+
* @since 4.0.0
|
|
539
|
+
*/
|
|
540
|
+
export const ipv4FromInput = input => {
|
|
541
|
+
if (isIpv4Address(input)) return Result.succeed(input);
|
|
542
|
+
if (typeof input === "string") return ipv4FromString(input);
|
|
543
|
+
if (input instanceof Uint8Array && input.length === 4) return Result.succeed(ipv4FromBytesUnsafe(input));
|
|
544
|
+
if (Array.isArray(input) && isTupleOf(input, 4)) return ipv4FromOctets(input);
|
|
545
|
+
return addressError(input, "expected an IPv4 address, string, 4 octets, or 4 bytes");
|
|
546
|
+
};
|
|
547
|
+
/**
|
|
548
|
+
* Converts an `Ipv6AddressInput` to an IPv6 address, parsing strings like
|
|
549
|
+
* `ipv6FromString`, checking segments like `ipv6FromSegments`, and returning
|
|
550
|
+
* IPv6 addresses unchanged.
|
|
551
|
+
*
|
|
552
|
+
* @stability unstable
|
|
553
|
+
* @category constructors
|
|
554
|
+
* @since 4.0.0
|
|
555
|
+
*/
|
|
556
|
+
export const ipv6FromInput = input => {
|
|
557
|
+
if (isIpv6Address(input)) return Result.succeed(input);
|
|
558
|
+
if (typeof input === "string") return ipv6FromString(input);
|
|
559
|
+
if (input instanceof Uint8Array && input.length === 16) return Result.succeed(ipv6FromBytesUnsafe(input));
|
|
560
|
+
if (Array.isArray(input) && isTupleOf(input, 8)) return ipv6FromSegments(input);
|
|
561
|
+
return addressError(input, "expected an IPv6 address, string, 8 segments, or 16 bytes");
|
|
562
|
+
};
|
|
563
|
+
/**
|
|
564
|
+
* Returns `true` when a value is a canonical scoped IPv6 literal.
|
|
565
|
+
*
|
|
566
|
+
* @stability experimental
|
|
567
|
+
* @category guards
|
|
568
|
+
* @since 4.0.0
|
|
569
|
+
*/
|
|
570
|
+
export const isScopedIpv6Literal = u => {
|
|
571
|
+
if (typeof u !== "string" || !u.includes("%")) return false;
|
|
572
|
+
const result = scopedIpv6LiteralFromString(u);
|
|
573
|
+
return Result.isSuccess(result) && result.success === u;
|
|
574
|
+
};
|
|
575
|
+
/**
|
|
576
|
+
* Parses an IPv6 literal with a numeric or named zone, such as `fe80::1%eth0`.
|
|
577
|
+
*
|
|
578
|
+
* @stability experimental
|
|
579
|
+
* @category decoding
|
|
580
|
+
* @since 4.0.0
|
|
581
|
+
*/
|
|
582
|
+
export const scopedIpv6LiteralFromString = input => {
|
|
583
|
+
const separator = input.indexOf("%");
|
|
584
|
+
if (separator === -1) return addressError(input, "expected an IPv6 zone");
|
|
585
|
+
let zone = input.slice(separator + 1);
|
|
586
|
+
if (zone.length === 0 || /[%[\]/\s\p{Cc}]/u.test(zone)) {
|
|
587
|
+
return addressError(input, "invalid IPv6 scope identifier");
|
|
588
|
+
}
|
|
589
|
+
if (/^\d+$/.test(zone)) {
|
|
590
|
+
const scopeId = Number(zone);
|
|
591
|
+
if (scopeId > 0xffffffff) return addressError(input, "scope identifier must be an unsigned 32-bit integer");
|
|
592
|
+
zone = String(scopeId);
|
|
593
|
+
}
|
|
594
|
+
return Result.map(ipv6FromString(input.slice(0, separator)), address => `${formatIp(address)}%${zone}`);
|
|
595
|
+
};
|
|
596
|
+
/**
|
|
597
|
+
* Converts a `ScopedIpv6LiteralInput` to a scoped IPv6 literal, parsing it
|
|
598
|
+
* like `scopedIpv6LiteralFromString`.
|
|
599
|
+
*
|
|
600
|
+
* **Details**
|
|
601
|
+
*
|
|
602
|
+
* Scoped IPv6 literals are strings, so every input is parsed.
|
|
603
|
+
*
|
|
604
|
+
* @stability experimental
|
|
605
|
+
* @category constructors
|
|
606
|
+
* @since 4.0.0
|
|
607
|
+
*/
|
|
608
|
+
export const scopedIpv6LiteralFromInput = input => scopedIpv6LiteralFromString(input);
|
|
429
609
|
/**
|
|
430
610
|
* Returns the four numeric octets of an IPv4 address in a fresh tuple.
|
|
431
611
|
*
|
|
@@ -738,6 +918,7 @@ export function toCanonical(self) {
|
|
|
738
918
|
return isIpv6Address(self) ? Option.getOrElse(fromIpv4Mapped(self), () => self) : self;
|
|
739
919
|
}
|
|
740
920
|
const InetV4Proto = {
|
|
921
|
+
...Inspectable.BaseProto,
|
|
741
922
|
_tag: "InetAddressV4",
|
|
742
923
|
[TypeId]: TypeId,
|
|
743
924
|
[Equal.symbol](that) {
|
|
@@ -751,12 +932,10 @@ const InetV4Proto = {
|
|
|
751
932
|
},
|
|
752
933
|
toJSON() {
|
|
753
934
|
return this.toString();
|
|
754
|
-
},
|
|
755
|
-
[NodeInspectSymbol]() {
|
|
756
|
-
return this.toJSON();
|
|
757
935
|
}
|
|
758
936
|
};
|
|
759
937
|
const InetV6Proto = {
|
|
938
|
+
...Inspectable.BaseProto,
|
|
760
939
|
_tag: "InetAddressV6",
|
|
761
940
|
[TypeId]: TypeId,
|
|
762
941
|
[Equal.symbol](that) {
|
|
@@ -770,9 +949,6 @@ const InetV6Proto = {
|
|
|
770
949
|
},
|
|
771
950
|
toJSON() {
|
|
772
951
|
return this.toString();
|
|
773
|
-
},
|
|
774
|
-
[NodeInspectSymbol]() {
|
|
775
|
-
return this.toJSON();
|
|
776
952
|
}
|
|
777
953
|
};
|
|
778
954
|
const checkPort = port => Number.isInteger(port) && port >= 0 && port <= 0xffff;
|
|
@@ -857,6 +1033,103 @@ export const inetAddressFromIpString = (address, port) => {
|
|
|
857
1033
|
* @since 4.0.0
|
|
858
1034
|
*/
|
|
859
1035
|
export const inetAddressFromIpStringUnsafe = (address, port) => Result.getOrThrow(inetAddressFromIpString(address, port));
|
|
1036
|
+
/**
|
|
1037
|
+
* Creates an internet address from a trusted native numeric host and port.
|
|
1038
|
+
* Named IPv6 zones are resolved using the supplied interface-to-scope map.
|
|
1039
|
+
* Use only for runtime-supplied socket addresses: this skips input validation,
|
|
1040
|
+
* so malformed strings or ports may silently produce incorrect values. An
|
|
1041
|
+
* unknown named IPv6 zone throws rather than losing its scope.
|
|
1042
|
+
*
|
|
1043
|
+
* @stability unstable
|
|
1044
|
+
* @category unsafe
|
|
1045
|
+
* @since 4.0.0
|
|
1046
|
+
*/
|
|
1047
|
+
export const inetAddressFromNativeUnsafe = (host, port, scopeIds) => {
|
|
1048
|
+
const zoneStart = host.indexOf("%");
|
|
1049
|
+
const end = zoneStart === -1 ? host.length : zoneStart;
|
|
1050
|
+
if (host.indexOf(":") === -1) {
|
|
1051
|
+
const self = Object.create(InetV4Proto);
|
|
1052
|
+
self.address = makeIpv4(parseNativeIpv4(host, 0, end));
|
|
1053
|
+
self.port = port;
|
|
1054
|
+
return Object.freeze(self);
|
|
1055
|
+
}
|
|
1056
|
+
let scopeId = 0;
|
|
1057
|
+
if (zoneStart !== -1) {
|
|
1058
|
+
const zone = host.slice(zoneStart + 1);
|
|
1059
|
+
const resolved = numericZone.test(zone) ? Number(zone) : scopeIds?.get(zone);
|
|
1060
|
+
if (resolved === undefined) throw new Error(`unknown IPv6 interface: ${zone}`);
|
|
1061
|
+
scopeId = resolved;
|
|
1062
|
+
}
|
|
1063
|
+
let w0 = 0;
|
|
1064
|
+
let w1 = 0;
|
|
1065
|
+
let w2 = 0;
|
|
1066
|
+
let w3 = 0;
|
|
1067
|
+
let segment = 0;
|
|
1068
|
+
let i = 0;
|
|
1069
|
+
while (i < end) {
|
|
1070
|
+
if (host.charCodeAt(i) === 58) {
|
|
1071
|
+
// The rest of a compressed address is anchored at the last segment.
|
|
1072
|
+
i += 2;
|
|
1073
|
+
if (i === end) break;
|
|
1074
|
+
let remaining = 1;
|
|
1075
|
+
for (let j = i; j < end; j++) {
|
|
1076
|
+
if (host.charCodeAt(j) === 58) remaining++;
|
|
1077
|
+
}
|
|
1078
|
+
const dot = host.indexOf(".", i);
|
|
1079
|
+
if (dot !== -1 && dot < end) remaining++;
|
|
1080
|
+
segment = 8 - remaining;
|
|
1081
|
+
}
|
|
1082
|
+
const start = i;
|
|
1083
|
+
let value = 0;
|
|
1084
|
+
while (i < end) {
|
|
1085
|
+
const ch = host.charCodeAt(i);
|
|
1086
|
+
if (ch === 58 || ch === 46) break;
|
|
1087
|
+
value = value << 4 | (ch <= 57 ? ch - 48 : (ch & 0x5f) - 55);
|
|
1088
|
+
i++;
|
|
1089
|
+
}
|
|
1090
|
+
if (i < end && host.charCodeAt(i) === 46) {
|
|
1091
|
+
w3 = parseNativeIpv4(host, start, end);
|
|
1092
|
+
break;
|
|
1093
|
+
}
|
|
1094
|
+
// even segments fill the high half of their 32-bit word
|
|
1095
|
+
const shifted = value << (1 - (segment & 1)) * 16;
|
|
1096
|
+
switch (segment >>> 1) {
|
|
1097
|
+
case 0:
|
|
1098
|
+
w0 |= shifted;
|
|
1099
|
+
break;
|
|
1100
|
+
case 1:
|
|
1101
|
+
w1 |= shifted;
|
|
1102
|
+
break;
|
|
1103
|
+
case 2:
|
|
1104
|
+
w2 |= shifted;
|
|
1105
|
+
break;
|
|
1106
|
+
default:
|
|
1107
|
+
w3 |= shifted;
|
|
1108
|
+
}
|
|
1109
|
+
segment++;
|
|
1110
|
+
if (i < end && host.charCodeAt(i + 1) !== 58) i++;
|
|
1111
|
+
}
|
|
1112
|
+
const self = Object.create(InetV6Proto);
|
|
1113
|
+
self.address = makeIpv6(w0, w1, w2, w3);
|
|
1114
|
+
self.port = port;
|
|
1115
|
+
self.scopeId = scopeId;
|
|
1116
|
+
return Object.freeze(self);
|
|
1117
|
+
};
|
|
1118
|
+
const numericZone = /^\d+$/;
|
|
1119
|
+
const parseNativeIpv4 = (host, start, end) => {
|
|
1120
|
+
let value = 0;
|
|
1121
|
+
let octet = 0;
|
|
1122
|
+
for (let i = start; i < end; i++) {
|
|
1123
|
+
const ch = host.charCodeAt(i);
|
|
1124
|
+
if (ch === 46) {
|
|
1125
|
+
value = value << 8 | octet;
|
|
1126
|
+
octet = 0;
|
|
1127
|
+
} else {
|
|
1128
|
+
octet = octet * 10 + ch - 48;
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
return (value << 8 | octet) >>> 0;
|
|
1132
|
+
};
|
|
860
1133
|
/**
|
|
861
1134
|
* Parses an unbracketed numeric host and port, resolving named IPv6 zones using
|
|
862
1135
|
* a supplied map of interface names to numeric scope IDs.
|
|
@@ -889,6 +1162,29 @@ export const inetAddressFromHostString = (host, port, scopeIds) => {
|
|
|
889
1162
|
}
|
|
890
1163
|
return inetAddressFromString(host.includes(":") ? `[${host}]:${port}` : `${host}:${port}`);
|
|
891
1164
|
};
|
|
1165
|
+
/**
|
|
1166
|
+
* Returns the IPv6 scope ID of a network interface from its addresses.
|
|
1167
|
+
*
|
|
1168
|
+
* **Details**
|
|
1169
|
+
*
|
|
1170
|
+
* The first IPv6 address with a positive scope ID supplies the result. Accepts
|
|
1171
|
+
* the addresses of one interface, such as `os.networkInterfaces()[name]` or the
|
|
1172
|
+
* entries of `Deno.networkInterfaces()` with that name, without performing any
|
|
1173
|
+
* operating-system lookup itself.
|
|
1174
|
+
*
|
|
1175
|
+
* @see {@link scopeIdsFromInterfaces} for mapping every interface at once
|
|
1176
|
+
* @stability experimental
|
|
1177
|
+
* @category converting
|
|
1178
|
+
* @since 4.0.0
|
|
1179
|
+
*/
|
|
1180
|
+
export const scopeIdFromInterface = addresses => {
|
|
1181
|
+
for (const address of addresses ?? []) {
|
|
1182
|
+
if (address.family === "IPv6" && typeof address.scopeid === "number" && address.scopeid > 0) {
|
|
1183
|
+
return Option.some(address.scopeid);
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
return Option.none();
|
|
1187
|
+
};
|
|
892
1188
|
/**
|
|
893
1189
|
* Creates a map from interface names to IPv6 scope IDs using supplied interface
|
|
894
1190
|
* entries.
|
|
@@ -909,8 +1205,8 @@ export const inetAddressFromHostString = (host, port, scopeIds) => {
|
|
|
909
1205
|
export const scopeIdsFromInterfaces = interfaces => {
|
|
910
1206
|
const scopeIds = new Map();
|
|
911
1207
|
for (const [name, addresses] of interfaces) {
|
|
912
|
-
const
|
|
913
|
-
if (
|
|
1208
|
+
const scopeId = scopeIdFromInterface(addresses);
|
|
1209
|
+
if (Option.isSome(scopeId)) scopeIds.set(name, scopeId.value);
|
|
914
1210
|
}
|
|
915
1211
|
return scopeIds;
|
|
916
1212
|
};
|
|
@@ -976,6 +1272,40 @@ export const inetAddressFromString = input => {
|
|
|
976
1272
|
* @since 4.0.0
|
|
977
1273
|
*/
|
|
978
1274
|
export const inetAddressFromStringUnsafe = input => Result.getOrThrow(inetAddressFromString(input));
|
|
1275
|
+
/**
|
|
1276
|
+
* Converts an `InetAddressInput` to an internet address, parsing strings like
|
|
1277
|
+
* `inetAddressFromString`, converting parts like `inetAddressV4` and
|
|
1278
|
+
* `inetAddressV6`, and returning internet addresses unchanged.
|
|
1279
|
+
*
|
|
1280
|
+
* @stability unstable
|
|
1281
|
+
* @category constructors
|
|
1282
|
+
* @since 4.0.0
|
|
1283
|
+
*/
|
|
1284
|
+
export const inetAddressFromInput = input => {
|
|
1285
|
+
if (isInetAddress(input)) return Result.succeed(input);
|
|
1286
|
+
if (typeof input === "string") return inetAddressFromString(input);
|
|
1287
|
+
if (!hasProperty(input, "address") || !hasProperty(input, "port")) {
|
|
1288
|
+
return addressError(input, "expected an address and port");
|
|
1289
|
+
}
|
|
1290
|
+
const {
|
|
1291
|
+
port,
|
|
1292
|
+
scopeId
|
|
1293
|
+
} = input;
|
|
1294
|
+
return Result.flatMap(ipFromInput(input.address), address => {
|
|
1295
|
+
if (isIpv6Address(address)) return inetAddressV6(address, port, {
|
|
1296
|
+
scopeId
|
|
1297
|
+
});
|
|
1298
|
+
return (scopeId ?? 0) === 0 ? inetAddressV4(address, port) : addressError(input, "scopeId requires an IPv6 address");
|
|
1299
|
+
});
|
|
1300
|
+
};
|
|
1301
|
+
/**
|
|
1302
|
+
* Converts a trusted `InetAddressInput` to an internet address, throwing on failure.
|
|
1303
|
+
*
|
|
1304
|
+
* @stability unstable
|
|
1305
|
+
* @category unsafe
|
|
1306
|
+
* @since 4.0.0
|
|
1307
|
+
*/
|
|
1308
|
+
export const inetAddressFromInputUnsafe = input => Result.getOrThrow(inetAddressFromInput(input));
|
|
979
1309
|
/**
|
|
980
1310
|
* Formats the numeric host of an internet address without brackets or a port,
|
|
981
1311
|
* preserving a nonzero IPv6 scope ID as a `%` suffix.
|
|
@@ -1137,6 +1467,7 @@ export const formatUrlUnsafe = (self, scheme = "http") => Result.getOrThrow(form
|
|
|
1137
1467
|
*/
|
|
1138
1468
|
export const formatUrlHostString = host => host.includes(":") && !host.startsWith("[") ? `[${host}]` : host;
|
|
1139
1469
|
const UnixPathProto = {
|
|
1470
|
+
...Inspectable.BaseProto,
|
|
1140
1471
|
_tag: "UnixPathAddress",
|
|
1141
1472
|
[TypeId]: TypeId,
|
|
1142
1473
|
[Equal.symbol](that) {
|
|
@@ -1150,9 +1481,6 @@ const UnixPathProto = {
|
|
|
1150
1481
|
},
|
|
1151
1482
|
toJSON() {
|
|
1152
1483
|
return this.toString();
|
|
1153
|
-
},
|
|
1154
|
-
[NodeInspectSymbol]() {
|
|
1155
|
-
return this.toJSON();
|
|
1156
1484
|
}
|
|
1157
1485
|
};
|
|
1158
1486
|
/**
|
|
@@ -1185,24 +1513,20 @@ export const formatUnixPath = self => `unix://${self.path}`;
|
|
|
1185
1513
|
*
|
|
1186
1514
|
* **Details**
|
|
1187
1515
|
*
|
|
1188
|
-
*
|
|
1189
|
-
* literals, ports, and input shapes return a
|
|
1516
|
+
* Internet addresses are converted like `inetAddressFromInput`, without
|
|
1517
|
+
* hostname resolution. Invalid IP literals, ports, and input shapes return a
|
|
1518
|
+
* `NetAddressError`.
|
|
1190
1519
|
*
|
|
1191
1520
|
* @stability unstable
|
|
1192
1521
|
* @category constructors
|
|
1193
1522
|
* @since 4.0.0
|
|
1194
1523
|
*/
|
|
1195
1524
|
export const socketAddressFromInput = input => {
|
|
1196
|
-
if (
|
|
1197
|
-
if (typeof input === "string") return inetAddressFromString(input);
|
|
1525
|
+
if (isUnixPathAddress(input)) return Result.succeed(input);
|
|
1198
1526
|
if (hasProperty(input, "path")) {
|
|
1199
1527
|
return typeof input.path === "string" ? Result.succeed(unixPathAddress(input.path)) : addressError(input, "path must be a string");
|
|
1200
1528
|
}
|
|
1201
|
-
|
|
1202
|
-
return addressError(input, "expected an address and port or a Unix path");
|
|
1203
|
-
}
|
|
1204
|
-
const address = typeof input.address === "string" ? ipFromString(input.address) : isIpAddress(input.address) ? Result.succeed(input.address) : addressError(input, "address must be an IP address or numeric IP string");
|
|
1205
|
-
return Result.flatMap(address, address => inetAddress(address, input.port));
|
|
1529
|
+
return inetAddressFromInput(input);
|
|
1206
1530
|
};
|
|
1207
1531
|
/**
|
|
1208
1532
|
* Converts a trusted `SocketAddress.Input` to a concrete socket address,
|