@distilled.cloud/tailscale 0.0.0-stage → 1.0.0-rc.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +85 -2
- package/lib/.lazy.json +1 -0
- package/lib/_bundle/lazy.js +4 -0
- package/lib/_bundle/sdk.js +2 -0
- package/lib/_bundle/sdk.js.map +1 -0
- package/lib/credentials.d.ts +52 -0
- package/lib/credentials.d.ts.map +1 -0
- package/lib/credentials.js +1 -0
- package/lib/errors.d.ts +25 -0
- package/lib/errors.d.ts.map +1 -0
- package/lib/errors.js +1 -0
- package/lib/index.d.ts +34 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +8 -0
- package/lib/index.lazy.js +8 -0
- package/lib/index.lazy.js.map +1 -0
- package/lib/pagination.d.ts +2 -0
- package/lib/pagination.d.ts.map +1 -0
- package/lib/pagination.js +1 -0
- package/lib/protocol.d.ts +17 -0
- package/lib/protocol.d.ts.map +1 -0
- package/lib/protocol.js +1 -0
- package/lib/retry.d.ts +12 -0
- package/lib/retry.d.ts.map +1 -0
- package/lib/retry.js +1 -0
- package/lib/services/index.d.ts +2 -0
- package/lib/services/index.d.ts.map +1 -0
- package/lib/services/index.js +1 -0
- package/lib/services/tailscale.d.ts +2148 -0
- package/lib/services/tailscale.d.ts.map +1 -0
- package/lib/services/tailscale.js +136 -0
- package/lib/services/tailscale.js.map +1 -0
- package/lib/services.lazy/index.js +3 -0
- package/lib/services.lazy/tailscale.js +4 -0
- package/lib/traits.d.ts +3 -0
- package/lib/traits.d.ts.map +1 -0
- package/lib/traits.js +1 -0
- package/package.json +87 -3
- package/src/credentials.ts +228 -0
- package/src/errors.ts +39 -0
- package/src/index.ts +40 -0
- package/src/pagination.ts +1 -0
- package/src/protocol.ts +120 -0
- package/src/retry.ts +28 -0
- package/src/services/index.ts +2 -0
- package/src/services/tailscale.ts +6000 -0
- package/src/traits.ts +37 -0
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import * as EffectConfig from "@distilled.cloud/core/config";
|
|
2
|
+
import { ConfigError } from "@distilled.cloud/core/errors";
|
|
3
|
+
import * as Clock from "effect/Clock";
|
|
4
|
+
import * as Context from "effect/Context";
|
|
5
|
+
import * as Effect from "effect/Effect";
|
|
6
|
+
import * as Equal from "effect/Equal";
|
|
7
|
+
import * as HttpClient from "effect/http/HttpClient";
|
|
8
|
+
import * as HttpClientRequest from "effect/http/HttpClientRequest";
|
|
9
|
+
import * as HttpClientResponse from "effect/http/HttpClientResponse";
|
|
10
|
+
import * as Layer from "effect/Layer";
|
|
11
|
+
import * as Option from "effect/Option";
|
|
12
|
+
import * as Redacted from "effect/Redacted";
|
|
13
|
+
import * as Schema from "effect/Schema";
|
|
14
|
+
import * as SynchronizedRef from "effect/SynchronizedRef";
|
|
15
|
+
|
|
16
|
+
export const DEFAULT_API_BASE_URL = "https://api.tailscale.com/api/v2";
|
|
17
|
+
|
|
18
|
+
const TOKEN_REFRESH_WINDOW_MS = 60_000;
|
|
19
|
+
|
|
20
|
+
export interface Config {
|
|
21
|
+
readonly token: Redacted.Redacted<string>;
|
|
22
|
+
readonly apiBaseUrl: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class Credentials extends Context.Service<Credentials, Effect.Effect<Config, ConfigError>>()(
|
|
26
|
+
"TailscaleCredentials",
|
|
27
|
+
) {}
|
|
28
|
+
|
|
29
|
+
export const fromApiKey = (config: {
|
|
30
|
+
readonly apiKey: Redacted.Redacted<string>;
|
|
31
|
+
/**
|
|
32
|
+
* The API root, including `/api/v2`, used as given. Default
|
|
33
|
+
* `DEFAULT_API_BASE_URL`. Unlike `TAILSCALE_BASE_URL`, it is not an origin.
|
|
34
|
+
*/
|
|
35
|
+
readonly apiBaseUrl?: string;
|
|
36
|
+
}): Layer.Layer<Credentials> =>
|
|
37
|
+
Layer.succeed(
|
|
38
|
+
Credentials,
|
|
39
|
+
Effect.succeed({
|
|
40
|
+
token: config.apiKey,
|
|
41
|
+
apiBaseUrl: config.apiBaseUrl ?? DEFAULT_API_BASE_URL,
|
|
42
|
+
}),
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
export interface ClientCredentialsConfig {
|
|
46
|
+
readonly clientId: string;
|
|
47
|
+
readonly clientSecret: Redacted.Redacted<string>;
|
|
48
|
+
readonly scope?: string;
|
|
49
|
+
/**
|
|
50
|
+
* The API root, including `/api/v2`, used as given. Default
|
|
51
|
+
* `DEFAULT_API_BASE_URL`. Unlike `TAILSCALE_BASE_URL`, it is not an origin.
|
|
52
|
+
*/
|
|
53
|
+
readonly apiBaseUrl?: string;
|
|
54
|
+
/** Sent as the exchange's `tailnet` parameter to scope the token to an API-only tailnet. */
|
|
55
|
+
readonly tokenTailnet?: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const TokenResponse = Schema.Struct({
|
|
59
|
+
access_token: Schema.Redacted(Schema.String),
|
|
60
|
+
expires_in: Schema.Number,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
interface CachedToken {
|
|
64
|
+
readonly config: Config;
|
|
65
|
+
readonly refreshAt: number;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Builds the OAuth client-credentials token cache. The returned effect posts
|
|
70
|
+
* the exchange through the captured `HttpClient`, reuses the token until
|
|
71
|
+
* 60 seconds before `expires_in`, and serializes refreshes so concurrent
|
|
72
|
+
* resolutions of a stale token make one exchange.
|
|
73
|
+
*/
|
|
74
|
+
export const makeClientCredentials = (
|
|
75
|
+
config: ClientCredentialsConfig,
|
|
76
|
+
): Effect.Effect<Effect.Effect<Config, ConfigError>, never, HttpClient.HttpClient> =>
|
|
77
|
+
Effect.gen(function* () {
|
|
78
|
+
const client = yield* HttpClient.HttpClient;
|
|
79
|
+
const apiBaseUrl = config.apiBaseUrl ?? DEFAULT_API_BASE_URL;
|
|
80
|
+
const exchange = client
|
|
81
|
+
.execute(
|
|
82
|
+
HttpClientRequest.post(`${apiBaseUrl}/oauth/token`).pipe(
|
|
83
|
+
HttpClientRequest.bodyUrlParams({
|
|
84
|
+
grant_type: "client_credentials",
|
|
85
|
+
client_id: config.clientId,
|
|
86
|
+
client_secret: Redacted.value(config.clientSecret),
|
|
87
|
+
...(config.scope ? { scope: config.scope } : {}),
|
|
88
|
+
...(config.tokenTailnet ? { tailnet: config.tokenTailnet } : {}),
|
|
89
|
+
}),
|
|
90
|
+
),
|
|
91
|
+
)
|
|
92
|
+
.pipe(
|
|
93
|
+
Effect.flatMap(HttpClientResponse.filterStatusOk),
|
|
94
|
+
Effect.flatMap(HttpClientResponse.schemaBodyJson(TokenResponse)),
|
|
95
|
+
Effect.mapError(
|
|
96
|
+
(cause) =>
|
|
97
|
+
new ConfigError({
|
|
98
|
+
message: `OAuth token exchange failed: ${cause.message}`,
|
|
99
|
+
}),
|
|
100
|
+
),
|
|
101
|
+
);
|
|
102
|
+
const cache = yield* SynchronizedRef.make(Option.none<CachedToken>());
|
|
103
|
+
return SynchronizedRef.modifyEffect(cache, (cached) =>
|
|
104
|
+
Effect.gen(function* () {
|
|
105
|
+
const now = yield* Clock.currentTimeMillis;
|
|
106
|
+
if (Option.isSome(cached) && now < cached.value.refreshAt) {
|
|
107
|
+
return [cached.value.config, cached] as const;
|
|
108
|
+
}
|
|
109
|
+
const token = yield* exchange;
|
|
110
|
+
const next: CachedToken = {
|
|
111
|
+
config: {
|
|
112
|
+
token: token.access_token,
|
|
113
|
+
apiBaseUrl,
|
|
114
|
+
},
|
|
115
|
+
refreshAt: now + token.expires_in * 1000 - TOKEN_REFRESH_WINDOW_MS,
|
|
116
|
+
};
|
|
117
|
+
return [next.config, Option.some(next)] as const;
|
|
118
|
+
}),
|
|
119
|
+
);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
export const fromClientCredentials = (
|
|
123
|
+
config: ClientCredentialsConfig,
|
|
124
|
+
): Layer.Layer<Credentials, never, HttpClient.HttpClient> =>
|
|
125
|
+
Layer.effect(Credentials, makeClientCredentials(config));
|
|
126
|
+
|
|
127
|
+
/** `TAILSCALE_BASE_URL` is the origin. An explicit API root ending in `/api/v2` is kept. */
|
|
128
|
+
const apiBaseUrlFromOrigin = (origin: string): string => {
|
|
129
|
+
const trimmed = origin.replace(/\/+$/, "");
|
|
130
|
+
return trimmed.endsWith("/api/v2") ? trimmed : `${trimmed}/api/v2`;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const BOTH_METHODS =
|
|
134
|
+
"TAILSCALE_API_KEY and TAILSCALE_OAUTH_CLIENT_ID/TAILSCALE_OAUTH_CLIENT_SECRET cannot both be set";
|
|
135
|
+
|
|
136
|
+
/** An empty variable counts as unset. */
|
|
137
|
+
const nonEmpty = (value: Option.Option<Redacted.Redacted<string>>) =>
|
|
138
|
+
Option.filter(value, (v) => Redacted.value(v).length > 0);
|
|
139
|
+
|
|
140
|
+
const EnvConfig = /*@__PURE__*/ EffectConfig.all({
|
|
141
|
+
apiKey: EffectConfig.option(EffectConfig.Redacted("TAILSCALE_API_KEY")).pipe(
|
|
142
|
+
EffectConfig.map(nonEmpty),
|
|
143
|
+
),
|
|
144
|
+
clientId: EffectConfig.option(EffectConfig.String("TAILSCALE_OAUTH_CLIENT_ID")),
|
|
145
|
+
clientSecret: EffectConfig.option(EffectConfig.Redacted("TAILSCALE_OAUTH_CLIENT_SECRET")).pipe(
|
|
146
|
+
EffectConfig.map(nonEmpty),
|
|
147
|
+
),
|
|
148
|
+
origin: EffectConfig.option(EffectConfig.String("TAILSCALE_BASE_URL")),
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
interface EnvOAuthConfig {
|
|
152
|
+
readonly clientId: string;
|
|
153
|
+
readonly clientSecret: Redacted.Redacted<string>;
|
|
154
|
+
readonly apiBaseUrl: string;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
interface EnvOAuth {
|
|
158
|
+
readonly config: EnvOAuthConfig;
|
|
159
|
+
readonly resolve: Effect.Effect<Config, ConfigError>;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Reads `TAILSCALE_API_KEY`, `TAILSCALE_OAUTH_CLIENT_ID`,
|
|
164
|
+
* `TAILSCALE_OAUTH_CLIENT_SECRET` and `TAILSCALE_BASE_URL`
|
|
165
|
+
* through `@distilled.cloud/core/config` on every resolution, so a provided
|
|
166
|
+
* `ConfigProvider` supplies them. The OAuth token cache belongs to the layer
|
|
167
|
+
* and is rebuilt when the OAuth variables change.
|
|
168
|
+
*/
|
|
169
|
+
export const CredentialsFromEnv: Layer.Layer<Credentials, never, HttpClient.HttpClient> =
|
|
170
|
+
/*@__PURE__*/ Layer.effect(
|
|
171
|
+
Credentials,
|
|
172
|
+
Effect.gen(function* () {
|
|
173
|
+
const client = yield* HttpClient.HttpClient;
|
|
174
|
+
const oauth = yield* SynchronizedRef.make(Option.none<EnvOAuth>());
|
|
175
|
+
const resolveOAuth = (config: EnvOAuthConfig) =>
|
|
176
|
+
SynchronizedRef.modifyEffect(oauth, (current) =>
|
|
177
|
+
Option.isSome(current) && Equal.equals(current.value.config, config)
|
|
178
|
+
? Effect.succeed([current.value.resolve, current] as const)
|
|
179
|
+
: makeClientCredentials(config).pipe(
|
|
180
|
+
Effect.provideService(HttpClient.HttpClient, client),
|
|
181
|
+
Effect.map((resolve) => [resolve, Option.some({ config, resolve })] as const),
|
|
182
|
+
),
|
|
183
|
+
).pipe(Effect.flatten);
|
|
184
|
+
return Effect.gen(function* () {
|
|
185
|
+
const env = yield* EnvConfig.pipe(
|
|
186
|
+
Effect.mapError(
|
|
187
|
+
(cause) =>
|
|
188
|
+
new ConfigError({
|
|
189
|
+
message: `Failed to read Tailscale configuration: ${cause.message}`,
|
|
190
|
+
}),
|
|
191
|
+
),
|
|
192
|
+
);
|
|
193
|
+
const apiKey = Option.getOrUndefined(env.apiKey);
|
|
194
|
+
const clientId = Option.getOrUndefined(env.clientId);
|
|
195
|
+
const clientSecret = Option.getOrUndefined(env.clientSecret);
|
|
196
|
+
const apiBaseUrl = Option.match(env.origin, {
|
|
197
|
+
onNone: () => DEFAULT_API_BASE_URL,
|
|
198
|
+
onSome: apiBaseUrlFromOrigin,
|
|
199
|
+
});
|
|
200
|
+
if (apiKey && (clientId || clientSecret)) {
|
|
201
|
+
return yield* new ConfigError({ message: BOTH_METHODS });
|
|
202
|
+
}
|
|
203
|
+
if (apiKey) {
|
|
204
|
+
return {
|
|
205
|
+
token: apiKey,
|
|
206
|
+
apiBaseUrl,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
if (clientId && clientSecret) {
|
|
210
|
+
return yield* resolveOAuth({
|
|
211
|
+
clientId,
|
|
212
|
+
clientSecret,
|
|
213
|
+
apiBaseUrl,
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
if (clientId || clientSecret) {
|
|
217
|
+
return yield* new ConfigError({
|
|
218
|
+
message:
|
|
219
|
+
"TAILSCALE_OAUTH_CLIENT_ID and TAILSCALE_OAUTH_CLIENT_SECRET are both required",
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
return yield* new ConfigError({
|
|
223
|
+
message:
|
|
224
|
+
"TAILSCALE_API_KEY or TAILSCALE_OAUTH_CLIENT_ID and TAILSCALE_OAUTH_CLIENT_SECRET is required",
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
}),
|
|
228
|
+
);
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export {
|
|
2
|
+
API_ERRORS,
|
|
3
|
+
BadGateway,
|
|
4
|
+
BadRequest,
|
|
5
|
+
Conflict,
|
|
6
|
+
ConfigError,
|
|
7
|
+
DEFAULT_ERRORS,
|
|
8
|
+
Forbidden,
|
|
9
|
+
GatewayTimeout,
|
|
10
|
+
HTTP_STATUS_MAP,
|
|
11
|
+
InternalServerError,
|
|
12
|
+
Locked,
|
|
13
|
+
NotFound,
|
|
14
|
+
ServiceUnavailable,
|
|
15
|
+
TooManyRequests,
|
|
16
|
+
Unauthorized,
|
|
17
|
+
UnprocessableEntity,
|
|
18
|
+
} from "@distilled.cloud/core/errors";
|
|
19
|
+
export type { DefaultErrors } from "@distilled.cloud/core/errors";
|
|
20
|
+
|
|
21
|
+
import * as Category from "@distilled.cloud/core/category";
|
|
22
|
+
import * as Schema from "@distilled.cloud/core/schema-lite-typed";
|
|
23
|
+
|
|
24
|
+
export class UnknownTailscaleError extends Schema.TaggedError<UnknownTailscaleError>()(
|
|
25
|
+
"UnknownTailscaleError",
|
|
26
|
+
{
|
|
27
|
+
code: Schema.optional(Schema.String),
|
|
28
|
+
message: Schema.optional(Schema.String),
|
|
29
|
+
body: Schema.Unknown,
|
|
30
|
+
},
|
|
31
|
+
).pipe(Category.withServerError) {}
|
|
32
|
+
|
|
33
|
+
export class TailscaleParseError extends Schema.TaggedError<TailscaleParseError>()(
|
|
34
|
+
"TailscaleParseError",
|
|
35
|
+
{
|
|
36
|
+
body: Schema.Unknown,
|
|
37
|
+
cause: Schema.Unknown,
|
|
38
|
+
},
|
|
39
|
+
).pipe(Category.withParseError) {}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @distilled.cloud/tailscale -- Tailscale SDK for Effect.
|
|
3
|
+
*
|
|
4
|
+
* `./services` is generated by `scripts/generate.ts` from the Smithy model
|
|
5
|
+
* in `.generated-specs` (written by `scripts/convert.ts` from the OpenAPI
|
|
6
|
+
* spec). Everything else in this folder is hand-written.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { Effect, Layer } from "effect";
|
|
11
|
+
* import * as FetchHttpClient from "effect/http/FetchHttpClient";
|
|
12
|
+
* import * as Tailscale from "@distilled.cloud/tailscale";
|
|
13
|
+
*
|
|
14
|
+
* const program = Effect.gen(function* () {
|
|
15
|
+
* const policy = yield* Tailscale.getPolicyFile({ tailnet: "-" });
|
|
16
|
+
* return policy.eTag;
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* const Live = Tailscale.CredentialsFromEnv.pipe(
|
|
20
|
+
* Layer.provideMerge(FetchHttpClient.layer),
|
|
21
|
+
* );
|
|
22
|
+
*
|
|
23
|
+
* program.pipe(Effect.provide(Live), Effect.runPromise);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export * from "./credentials.ts";
|
|
27
|
+
export * from "./errors.ts";
|
|
28
|
+
export * as T from "./traits.ts";
|
|
29
|
+
export { TailscaleProtocol, type TailscaleOpError, type TailscaleOpContext } from "./protocol.ts";
|
|
30
|
+
export { paginateCursor } from "./pagination.ts";
|
|
31
|
+
export * as Retry from "./retry.ts";
|
|
32
|
+
export * from "./services/tailscale.ts";
|
|
33
|
+
export {
|
|
34
|
+
BadRequest,
|
|
35
|
+
Conflict,
|
|
36
|
+
Forbidden,
|
|
37
|
+
NotFound,
|
|
38
|
+
PreconditionFailed,
|
|
39
|
+
UnprocessableEntity,
|
|
40
|
+
} from "./services/tailscale.ts";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { paginateCursor } from "@distilled.cloud/core/pagination";
|
package/src/protocol.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import * as API from "@distilled.cloud/core/api";
|
|
2
|
+
import type { ConfigError } from "@distilled.cloud/core/errors";
|
|
3
|
+
import {
|
|
4
|
+
getAnn,
|
|
5
|
+
getProps,
|
|
6
|
+
hasPropAnn,
|
|
7
|
+
resolveNode,
|
|
8
|
+
responseHeaderMembers,
|
|
9
|
+
withResponseHeaders,
|
|
10
|
+
} from "@distilled.cloud/core/protocol-http";
|
|
11
|
+
import {
|
|
12
|
+
makeRestProtocol,
|
|
13
|
+
rawResponseRootSymbol,
|
|
14
|
+
rawResponseSymbol,
|
|
15
|
+
validateSuccess,
|
|
16
|
+
} from "@distilled.cloud/core/protocol-rest";
|
|
17
|
+
import * as Effect from "effect/Effect";
|
|
18
|
+
import type * as HttpClient from "effect/http/HttpClient";
|
|
19
|
+
import type * as HttpClientError from "effect/http/HttpClientError";
|
|
20
|
+
import type * as HttpClientResponse from "effect/http/HttpClientResponse";
|
|
21
|
+
import * as Layer from "effect/Layer";
|
|
22
|
+
import * as Redacted from "effect/Redacted";
|
|
23
|
+
import type * as AST from "effect/SchemaAST";
|
|
24
|
+
import { Credentials, type Config } from "./credentials.ts";
|
|
25
|
+
import { API_ERRORS } from "./errors.ts";
|
|
26
|
+
import { TailscaleParseError, UnknownTailscaleError } from "./errors.ts";
|
|
27
|
+
|
|
28
|
+
export type TailscaleOpError =
|
|
29
|
+
| InstanceType<(typeof API_ERRORS)[number]>
|
|
30
|
+
| UnknownTailscaleError
|
|
31
|
+
| ConfigError
|
|
32
|
+
| HttpClientError.HttpClientError
|
|
33
|
+
| TailscaleParseError;
|
|
34
|
+
|
|
35
|
+
export type TailscaleOpContext = Credentials | HttpClient.HttpClient;
|
|
36
|
+
|
|
37
|
+
const isStringAst = (ast: AST.AST): boolean => resolveNode(ast)._tag === "String";
|
|
38
|
+
|
|
39
|
+
const isRawRoot = (outputAst: AST.AST): boolean =>
|
|
40
|
+
getAnn(outputAst, rawResponseRootSymbol) !== undefined;
|
|
41
|
+
|
|
42
|
+
/** The output member that receives the whole body as text (a policy file's HuJSON). */
|
|
43
|
+
const rawStringMember = (outputAst: AST.AST): AST.PropertySignature | undefined =>
|
|
44
|
+
getProps(outputAst).find((prop) => hasPropAnn(prop, rawResponseSymbol) && isStringAst(prop.type));
|
|
45
|
+
|
|
46
|
+
// The protocol's decode has no typed error channel; the generated operation
|
|
47
|
+
// annotations reintroduce it for callers (same as core protocol-rest).
|
|
48
|
+
const fail = (e: unknown): Effect.Effect<never> => Effect.fail(e) as Effect.Effect<never>;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A policy file is HuJSON text. It stays text even when it happens to be
|
|
52
|
+
* valid JSON, so the core REST decode, which parses JSON bodies, can't read it.
|
|
53
|
+
*/
|
|
54
|
+
const decodeRawString = (
|
|
55
|
+
response: HttpClientResponse.HttpClientResponse,
|
|
56
|
+
outputAst: AST.AST,
|
|
57
|
+
member: AST.PropertySignature | undefined,
|
|
58
|
+
): Effect.Effect<unknown> =>
|
|
59
|
+
Effect.flatMap(response.text, (text) =>
|
|
60
|
+
validateSuccess(
|
|
61
|
+
outputAst,
|
|
62
|
+
{
|
|
63
|
+
value:
|
|
64
|
+
member === undefined
|
|
65
|
+
? text
|
|
66
|
+
: withResponseHeaders(
|
|
67
|
+
responseHeaderMembers(outputAst),
|
|
68
|
+
{ [member.name]: text },
|
|
69
|
+
response.headers as Record<string, string | undefined>,
|
|
70
|
+
),
|
|
71
|
+
body: text,
|
|
72
|
+
nonJson: false,
|
|
73
|
+
},
|
|
74
|
+
(info) => new TailscaleParseError(info),
|
|
75
|
+
),
|
|
76
|
+
).pipe(Effect.catch(fail));
|
|
77
|
+
|
|
78
|
+
const TailscaleProtocolRest: Layer.Layer<API.Protocol> = /*@__PURE__*/ makeRestProtocol<Config>({
|
|
79
|
+
credentials: /*@__PURE__*/ Effect.flatMap(Credentials, (resolve) => resolve),
|
|
80
|
+
baseUrl: (creds) => creds.apiBaseUrl,
|
|
81
|
+
headers: (creds) => ({
|
|
82
|
+
Authorization: `Bearer ${Redacted.value(creds.token)}`,
|
|
83
|
+
}),
|
|
84
|
+
unknownError: ({ code, message, body }) =>
|
|
85
|
+
new UnknownTailscaleError({
|
|
86
|
+
code: typeof code === "string" ? code : code !== undefined ? String(code) : undefined,
|
|
87
|
+
message,
|
|
88
|
+
body,
|
|
89
|
+
}),
|
|
90
|
+
parseError: ({ body, cause }) => new TailscaleParseError({ body, cause }),
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Core's REST protocol, plus two things the Tailscale API does differently on
|
|
95
|
+
* a 2xx: policy files are HuJSON text, and an empty list can arrive as the
|
|
96
|
+
* JSON `null` (a Go nil slice), which decodes as `[]`.
|
|
97
|
+
*/
|
|
98
|
+
export const TailscaleProtocol: Layer.Layer<API.Protocol> = /*@__PURE__*/ Layer.effect(
|
|
99
|
+
API.Protocol,
|
|
100
|
+
Effect.map(API.Protocol, (rest) =>
|
|
101
|
+
API.Protocol.of({
|
|
102
|
+
encode: rest.encode,
|
|
103
|
+
decode: (args) => {
|
|
104
|
+
if (args.response.status < 200 || args.response.status >= 300) return rest.decode(args);
|
|
105
|
+
const { outputAst } = args;
|
|
106
|
+
if (isRawRoot(outputAst) && isStringAst(outputAst)) {
|
|
107
|
+
return decodeRawString(args.response, outputAst, undefined);
|
|
108
|
+
}
|
|
109
|
+
const member = rawStringMember(outputAst);
|
|
110
|
+
if (member !== undefined) return decodeRawString(args.response, outputAst, member);
|
|
111
|
+
if (isRawRoot(outputAst) && resolveNode(outputAst)._tag === "Arrays") {
|
|
112
|
+
return Effect.flatMap(args.response.text, (text) =>
|
|
113
|
+
text.trim() === "null" ? Effect.succeed([]) : rest.decode(args),
|
|
114
|
+
).pipe(Effect.catch(fail));
|
|
115
|
+
}
|
|
116
|
+
return rest.decode(args);
|
|
117
|
+
},
|
|
118
|
+
}),
|
|
119
|
+
),
|
|
120
|
+
).pipe(Layer.provide(TailscaleProtocolRest));
|
package/src/retry.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type Policy, throttlingFactory, transientFactory } from "@distilled.cloud/core/retry";
|
|
2
|
+
import * as Context from "effect/Context";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import * as Layer from "effect/Layer";
|
|
5
|
+
|
|
6
|
+
export {
|
|
7
|
+
type Factory,
|
|
8
|
+
type Options,
|
|
9
|
+
type Policy,
|
|
10
|
+
capped,
|
|
11
|
+
jittered,
|
|
12
|
+
makeDefault,
|
|
13
|
+
throttlingFactory,
|
|
14
|
+
throttlingOptions,
|
|
15
|
+
transientFactory,
|
|
16
|
+
transientOptions,
|
|
17
|
+
} from "@distilled.cloud/core/retry";
|
|
18
|
+
|
|
19
|
+
export class Retry extends Context.Service<Retry, Policy>()("TailscaleRetry") {}
|
|
20
|
+
|
|
21
|
+
export const policy = (optionsOrFactory: Policy) =>
|
|
22
|
+
Effect.provide(Layer.succeed(Retry, optionsOrFactory));
|
|
23
|
+
|
|
24
|
+
export const none = Effect.provide(Layer.succeed(Retry, { while: () => false }));
|
|
25
|
+
|
|
26
|
+
export const throttling = policy(throttlingFactory);
|
|
27
|
+
|
|
28
|
+
export const transient = policy(transientFactory);
|