@distilled.cloud/namespacelabs 0.0.0-stage → 1.0.0-rc.20

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.
Files changed (216) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +125 -2
  3. package/lib/.lazy.json +1 -0
  4. package/lib/_bundle/lazy.js +4 -0
  5. package/lib/_bundle/sdk.js +2 -0
  6. package/lib/_bundle/sdk.js.map +1 -0
  7. package/lib/credentials.d.ts +55 -0
  8. package/lib/credentials.d.ts.map +1 -0
  9. package/lib/credentials.js +1 -0
  10. package/lib/errors.d.ts +33 -0
  11. package/lib/errors.d.ts.map +1 -0
  12. package/lib/errors.js +1 -0
  13. package/lib/index.d.ts +42 -0
  14. package/lib/index.d.ts.map +1 -0
  15. package/lib/index.js +7 -0
  16. package/lib/index.lazy.js +7 -0
  17. package/lib/index.lazy.js.map +1 -0
  18. package/lib/pagination.d.ts +2 -0
  19. package/lib/pagination.d.ts.map +1 -0
  20. package/lib/pagination.js +1 -0
  21. package/lib/protocol.d.ts +15 -0
  22. package/lib/protocol.d.ts.map +1 -0
  23. package/lib/protocol.js +1 -0
  24. package/lib/retry.d.ts +12 -0
  25. package/lib/retry.d.ts.map +1 -0
  26. package/lib/retry.js +1 -0
  27. package/lib/services/artifacts.d.ts +260 -0
  28. package/lib/services/artifacts.d.ts.map +1 -0
  29. package/lib/services/artifacts.js +2 -0
  30. package/lib/services/artifacts.js.map +1 -0
  31. package/lib/services/audit.d.ts +149 -0
  32. package/lib/services/audit.d.ts.map +1 -0
  33. package/lib/services/audit.js +2 -0
  34. package/lib/services/audit.js.map +1 -0
  35. package/lib/services/bazel.d.ts +173 -0
  36. package/lib/services/bazel.d.ts.map +1 -0
  37. package/lib/services/bazel.js +2 -0
  38. package/lib/services/bazel.js.map +1 -0
  39. package/lib/services/bazel_cache.d.ts +118 -0
  40. package/lib/services/bazel_cache.d.ts.map +1 -0
  41. package/lib/services/bazel_cache.js +2 -0
  42. package/lib/services/bazel_cache.js.map +1 -0
  43. package/lib/services/builder.d.ts +234 -0
  44. package/lib/services/builder.d.ts.map +1 -0
  45. package/lib/services/builder.js +2 -0
  46. package/lib/services/builder.js.map +1 -0
  47. package/lib/services/buildkite_jobs.d.ts +121 -0
  48. package/lib/services/buildkite_jobs.d.ts.map +1 -0
  49. package/lib/services/buildkite_jobs.js +2 -0
  50. package/lib/services/buildkite_jobs.js.map +1 -0
  51. package/lib/services/buildkite_queue.d.ts +178 -0
  52. package/lib/services/buildkite_queue.d.ts.map +1 -0
  53. package/lib/services/buildkite_queue.js +2 -0
  54. package/lib/services/buildkite_queue.js.map +1 -0
  55. package/lib/services/compute.d.ts +1057 -0
  56. package/lib/services/compute.d.ts.map +1 -0
  57. package/lib/services/compute.js +43 -0
  58. package/lib/services/compute.js.map +1 -0
  59. package/lib/services/compute_command.d.ts +112 -0
  60. package/lib/services/compute_command.d.ts.map +1 -0
  61. package/lib/services/compute_command.js +2 -0
  62. package/lib/services/compute_command.js.map +1 -0
  63. package/lib/services/compute_observability.d.ts +157 -0
  64. package/lib/services/compute_observability.d.ts.map +1 -0
  65. package/lib/services/compute_observability.js +2 -0
  66. package/lib/services/compute_observability.js.map +1 -0
  67. package/lib/services/compute_storage.d.ts +371 -0
  68. package/lib/services/compute_storage.d.ts.map +1 -0
  69. package/lib/services/compute_storage.js +33 -0
  70. package/lib/services/compute_storage.js.map +1 -0
  71. package/lib/services/compute_usage.d.ts +275 -0
  72. package/lib/services/compute_usage.d.ts.map +1 -0
  73. package/lib/services/compute_usage.js +2 -0
  74. package/lib/services/compute_usage.js.map +1 -0
  75. package/lib/services/egress.d.ts +183 -0
  76. package/lib/services/egress.d.ts.map +1 -0
  77. package/lib/services/egress.js +2 -0
  78. package/lib/services/egress.js.map +1 -0
  79. package/lib/services/experimental_compute_reservation.d.ts +662 -0
  80. package/lib/services/experimental_compute_reservation.d.ts.map +1 -0
  81. package/lib/services/experimental_compute_reservation.js +23 -0
  82. package/lib/services/experimental_compute_reservation.js.map +1 -0
  83. package/lib/services/experimental_remote_results.d.ts +488 -0
  84. package/lib/services/experimental_remote_results.d.ts.map +1 -0
  85. package/lib/services/experimental_remote_results.js +2 -0
  86. package/lib/services/experimental_remote_results.js.map +1 -0
  87. package/lib/services/github_image.d.ts +154 -0
  88. package/lib/services/github_image.d.ts.map +1 -0
  89. package/lib/services/github_image.js +2 -0
  90. package/lib/services/github_image.js.map +1 -0
  91. package/lib/services/github_jobs.d.ts +175 -0
  92. package/lib/services/github_jobs.d.ts.map +1 -0
  93. package/lib/services/github_jobs.js +2 -0
  94. package/lib/services/github_jobs.js.map +1 -0
  95. package/lib/services/github_profile.d.ts +288 -0
  96. package/lib/services/github_profile.d.ts.map +1 -0
  97. package/lib/services/github_profile.js +2 -0
  98. package/lib/services/github_profile.js.map +1 -0
  99. package/lib/services/gradle_cache.d.ts +117 -0
  100. package/lib/services/gradle_cache.d.ts.map +1 -0
  101. package/lib/services/gradle_cache.js +2 -0
  102. package/lib/services/gradle_cache.js.map +1 -0
  103. package/lib/services/httpcache.d.ts +117 -0
  104. package/lib/services/httpcache.d.ts.map +1 -0
  105. package/lib/services/httpcache.js +2 -0
  106. package/lib/services/httpcache.js.map +1 -0
  107. package/lib/services/iam_tenant.d.ts +373 -0
  108. package/lib/services/iam_tenant.d.ts.map +1 -0
  109. package/lib/services/iam_tenant.js +39 -0
  110. package/lib/services/iam_tenant.js.map +1 -0
  111. package/lib/services/iam_tenant_membership.d.ts +106 -0
  112. package/lib/services/iam_tenant_membership.d.ts.map +1 -0
  113. package/lib/services/iam_tenant_membership.js +2 -0
  114. package/lib/services/iam_tenant_membership.js.map +1 -0
  115. package/lib/services/iam_token.d.ts +249 -0
  116. package/lib/services/iam_token.d.ts.map +1 -0
  117. package/lib/services/iam_token.js +2 -0
  118. package/lib/services/iam_token.js.map +1 -0
  119. package/lib/services/image.d.ts +214 -0
  120. package/lib/services/image.d.ts.map +1 -0
  121. package/lib/services/image.js +2 -0
  122. package/lib/services/image.js.map +1 -0
  123. package/lib/services/index.d.ts +30 -0
  124. package/lib/services/index.d.ts.map +1 -0
  125. package/lib/services/index.js +29 -0
  126. package/lib/services/private_devbox.d.ts +1156 -0
  127. package/lib/services/private_devbox.d.ts.map +1 -0
  128. package/lib/services/private_devbox.js +81 -0
  129. package/lib/services/private_devbox.js.map +1 -0
  130. package/lib/services/private_user_sessions.d.ts +137 -0
  131. package/lib/services/private_user_sessions.d.ts.map +1 -0
  132. package/lib/services/private_user_sessions.js +2 -0
  133. package/lib/services/private_user_sessions.js.map +1 -0
  134. package/lib/services/registry.d.ts +365 -0
  135. package/lib/services/registry.d.ts.map +1 -0
  136. package/lib/services/registry.js +34 -0
  137. package/lib/services/registry.js.map +1 -0
  138. package/lib/services/testing.d.ts +152 -0
  139. package/lib/services/testing.d.ts.map +1 -0
  140. package/lib/services/testing.js +2 -0
  141. package/lib/services/testing.js.map +1 -0
  142. package/lib/services/vault.d.ts +195 -0
  143. package/lib/services/vault.d.ts.map +1 -0
  144. package/lib/services/vault.js +2 -0
  145. package/lib/services/vault.js.map +1 -0
  146. package/lib/services.lazy/artifacts.js +1 -0
  147. package/lib/services.lazy/audit.js +1 -0
  148. package/lib/services.lazy/bazel.js +1 -0
  149. package/lib/services.lazy/bazel_cache.js +1 -0
  150. package/lib/services.lazy/builder.js +1 -0
  151. package/lib/services.lazy/buildkite_jobs.js +1 -0
  152. package/lib/services.lazy/buildkite_queue.js +1 -0
  153. package/lib/services.lazy/compute.js +4 -0
  154. package/lib/services.lazy/compute_command.js +1 -0
  155. package/lib/services.lazy/compute_observability.js +1 -0
  156. package/lib/services.lazy/compute_storage.js +4 -0
  157. package/lib/services.lazy/compute_usage.js +1 -0
  158. package/lib/services.lazy/egress.js +1 -0
  159. package/lib/services.lazy/experimental_compute_reservation.js +4 -0
  160. package/lib/services.lazy/experimental_remote_results.js +1 -0
  161. package/lib/services.lazy/github_image.js +1 -0
  162. package/lib/services.lazy/github_jobs.js +1 -0
  163. package/lib/services.lazy/github_profile.js +1 -0
  164. package/lib/services.lazy/gradle_cache.js +1 -0
  165. package/lib/services.lazy/httpcache.js +1 -0
  166. package/lib/services.lazy/iam_tenant.js +4 -0
  167. package/lib/services.lazy/iam_tenant_membership.js +1 -0
  168. package/lib/services.lazy/iam_token.js +1 -0
  169. package/lib/services.lazy/image.js +1 -0
  170. package/lib/services.lazy/index.js +3 -0
  171. package/lib/services.lazy/private_devbox.js +4 -0
  172. package/lib/services.lazy/private_user_sessions.js +1 -0
  173. package/lib/services.lazy/registry.js +4 -0
  174. package/lib/services.lazy/testing.js +1 -0
  175. package/lib/services.lazy/vault.js +1 -0
  176. package/lib/traits.d.ts +3 -0
  177. package/lib/traits.d.ts.map +1 -0
  178. package/lib/traits.js +1 -0
  179. package/package.json +87 -3
  180. package/src/credentials.ts +164 -0
  181. package/src/errors.ts +47 -0
  182. package/src/index.ts +48 -0
  183. package/src/pagination.ts +1 -0
  184. package/src/protocol.ts +82 -0
  185. package/src/retry.ts +28 -0
  186. package/src/services/artifacts.ts +761 -0
  187. package/src/services/audit.ts +315 -0
  188. package/src/services/bazel.ts +415 -0
  189. package/src/services/bazel_cache.ts +211 -0
  190. package/src/services/builder.ts +568 -0
  191. package/src/services/buildkite_jobs.ts +222 -0
  192. package/src/services/buildkite_queue.ts +445 -0
  193. package/src/services/compute.ts +2794 -0
  194. package/src/services/compute_command.ts +212 -0
  195. package/src/services/compute_observability.ts +428 -0
  196. package/src/services/compute_storage.ts +1159 -0
  197. package/src/services/compute_usage.ts +730 -0
  198. package/src/services/egress.ts +508 -0
  199. package/src/services/experimental_compute_reservation.ts +1488 -0
  200. package/src/services/experimental_remote_results.ts +1157 -0
  201. package/src/services/github_image.ts +293 -0
  202. package/src/services/github_jobs.ts +381 -0
  203. package/src/services/github_profile.ts +721 -0
  204. package/src/services/gradle_cache.ts +263 -0
  205. package/src/services/httpcache.ts +263 -0
  206. package/src/services/iam_tenant.ts +1355 -0
  207. package/src/services/iam_tenant_membership.ts +245 -0
  208. package/src/services/iam_token.ts +780 -0
  209. package/src/services/image.ts +594 -0
  210. package/src/services/index.ts +30 -0
  211. package/src/services/private_devbox.ts +4217 -0
  212. package/src/services/private_user_sessions.ts +402 -0
  213. package/src/services/registry.ts +1234 -0
  214. package/src/services/testing.ts +386 -0
  215. package/src/services/vault.ts +514 -0
  216. package/src/traits.ts +36 -0
@@ -0,0 +1,164 @@
1
+ import * as EffectConfig from "@distilled.cloud/core/config";
2
+ import { ConfigError } from "@distilled.cloud/core/errors";
3
+ import * as Context from "effect/Context";
4
+ import * as Effect from "effect/Effect";
5
+ import * as FileSystem from "effect/FileSystem";
6
+ import * as Layer from "effect/Layer";
7
+ import * as Option from "effect/Option";
8
+ import * as Redacted from "effect/Redacted";
9
+
10
+ export const DEFAULT_REGION = "us";
11
+ export const DEFAULT_GLOBAL_BASE_URL = "https://global.namespaceapis.com";
12
+ export const DEFAULT_PRIVATE_API_BASE_URL = "https://private-api.global.namespaceapis.com";
13
+ /** Where a Namespace instance or runner mounts its workload token. */
14
+ export const DEFAULT_TOKEN_FILE = "/var/run/nsc/token.json";
15
+
16
+ export const computeBaseUrlFor = (region: string): string =>
17
+ `https://${region}.compute.namespaceapis.com`;
18
+ export const storageBaseUrlFor = (region: string): string =>
19
+ `https://${region}.storage.namespaceapis.com`;
20
+
21
+ export interface Config {
22
+ readonly token: Redacted.Redacted<string>;
23
+ readonly region: string;
24
+ /** `namespace.cloud.compute.*`, `builder`, `experimental.compute`: `https://<region>.compute.namespaceapis.com`. */
25
+ readonly computeBaseUrl: string;
26
+ /** `namespace.cloud.storage.*` (artifacts): `https://<region>.storage.namespaceapis.com`. */
27
+ readonly storageBaseUrl: string;
28
+ /** IAM, registry, vault, images, caches, egress and everything else global. */
29
+ readonly globalBaseUrl: string;
30
+ /** `namespace.private.*` (devboxes, sessions) and `namespace.cloud.github.*`. */
31
+ readonly privateApiBaseUrl: string;
32
+ }
33
+
34
+ export interface Hosts {
35
+ readonly region?: string;
36
+ readonly computeBaseUrl?: string;
37
+ readonly storageBaseUrl?: string;
38
+ readonly globalBaseUrl?: string;
39
+ readonly privateApiBaseUrl?: string;
40
+ }
41
+
42
+ export interface Options extends Hosts {
43
+ readonly token: Redacted.Redacted<string>;
44
+ }
45
+
46
+ export const resolveConfig = (options: Options): Config => {
47
+ const region = options.region ?? DEFAULT_REGION;
48
+ return {
49
+ token: options.token,
50
+ region,
51
+ computeBaseUrl: options.computeBaseUrl ?? computeBaseUrlFor(region),
52
+ storageBaseUrl: options.storageBaseUrl ?? storageBaseUrlFor(region),
53
+ globalBaseUrl: options.globalBaseUrl ?? DEFAULT_GLOBAL_BASE_URL,
54
+ privateApiBaseUrl: options.privateApiBaseUrl ?? DEFAULT_PRIVATE_API_BASE_URL,
55
+ };
56
+ };
57
+
58
+ export class Credentials
59
+ extends /*@__PURE__*/ Context.Service<Credentials, Effect.Effect<Config, ConfigError>>()(
60
+ "NamespaceLabsCredentials",
61
+ ) {}
62
+
63
+ const optionalString = (name: string) =>
64
+ EffectConfig.option(EffectConfig.String(name)).pipe(EffectConfig.map(Option.getOrUndefined));
65
+
66
+ /**
67
+ * `NSC_REGION` (default `us`) selects the compute and storage hosts.
68
+ * `NSC_COMPUTE_URL` (or `NSC_ENDPOINT`, which Namespace sets inside an
69
+ * instance), `NSC_STORAGE_URL`, `NSC_GLOBAL_URL` and `NSC_PRIVATE_API_URL`
70
+ * override a host.
71
+ */
72
+ const hostsConfig = /*@__PURE__*/ EffectConfig.all({
73
+ region: /*@__PURE__*/ EffectConfig.String("NSC_REGION").pipe(
74
+ /*@__PURE__*/ EffectConfig.withDefault(DEFAULT_REGION),
75
+ ),
76
+ computeBaseUrl: /*@__PURE__*/ EffectConfig.String("NSC_COMPUTE_URL").pipe(
77
+ /*@__PURE__*/ EffectConfig.orElse(() => EffectConfig.String("NSC_ENDPOINT")),
78
+ EffectConfig.option,
79
+ /*@__PURE__*/ EffectConfig.map(Option.getOrUndefined),
80
+ ),
81
+ storageBaseUrl: /*@__PURE__*/ optionalString("NSC_STORAGE_URL"),
82
+ globalBaseUrl: /*@__PURE__*/ optionalString("NSC_GLOBAL_URL"),
83
+ privateApiBaseUrl: /*@__PURE__*/ optionalString("NSC_PRIVATE_API_URL"),
84
+ });
85
+
86
+ const configError = (message: string) => () => new ConfigError({ message });
87
+
88
+ /** Credentials from the environment: `NSC_TOKEN` (required) plus the host variables. */
89
+ export const CredentialsFromEnv: Layer.Layer<Credentials> = /*@__PURE__*/ Layer.succeed(
90
+ Credentials,
91
+ /*@__PURE__*/ EffectConfig.all({
92
+ token: /*@__PURE__*/ EffectConfig.Redacted("NSC_TOKEN"),
93
+ hosts: hostsConfig,
94
+ }).pipe(
95
+ /*@__PURE__*/ Effect.map(({ token, hosts }) => resolveConfig({ token, ...hosts })),
96
+ /*@__PURE__*/ Effect.mapError(configError("NSC_TOKEN environment variable is required")),
97
+ ),
98
+ );
99
+
100
+ const readTokenFile = (
101
+ fs: FileSystem.FileSystem,
102
+ path: string,
103
+ ): Effect.Effect<Redacted.Redacted<string>, ConfigError> =>
104
+ fs.readFileString(path).pipe(
105
+ Effect.mapError(configError(`Namespace token file ${path} could not be read`)),
106
+ Effect.flatMap((text) => {
107
+ const parsed = Effect.try({
108
+ try: () => JSON.parse(text) as { readonly bearer_token?: unknown },
109
+ catch: configError(`Namespace token file ${path} is not JSON`),
110
+ });
111
+ return Effect.flatMap(parsed, ({ bearer_token }) =>
112
+ typeof bearer_token === "string" && bearer_token.length > 0
113
+ ? Effect.succeed(Redacted.make(bearer_token))
114
+ : Effect.fail(
115
+ new ConfigError({
116
+ message: `Namespace token file ${path} has no bearer_token; exchange a session token with privateUserSessions.issueTenantTokenFromSession`,
117
+ }),
118
+ ),
119
+ );
120
+ }),
121
+ );
122
+
123
+ /**
124
+ * Credentials from the token file a Namespace workload is given: `NSC_TOKEN_FILE`,
125
+ * else `/var/run/nsc/token.json`. The file is re-read on every resolution because
126
+ * Namespace refreshes it; host variables as in {@link CredentialsFromEnv}.
127
+ */
128
+ export const CredentialsFromTokenFile: Layer.Layer<Credentials, never, FileSystem.FileSystem> =
129
+ /*@__PURE__*/ Layer.effect(
130
+ Credentials,
131
+ /*@__PURE__*/ Effect.map(FileSystem.FileSystem, (fs) =>
132
+ EffectConfig.all({
133
+ path: EffectConfig.String("NSC_TOKEN_FILE").pipe(
134
+ EffectConfig.withDefault(DEFAULT_TOKEN_FILE),
135
+ ),
136
+ hosts: hostsConfig,
137
+ }).pipe(
138
+ Effect.mapError(configError("Namespace host configuration could not be read")),
139
+ Effect.flatMap(({ path, hosts }) =>
140
+ Effect.map(readTokenFile(fs, path), (token) => resolveConfig({ token, ...hosts })),
141
+ ),
142
+ ),
143
+ ),
144
+ );
145
+
146
+ /** Convenience layer from a tenant token, an optional region and host overrides. */
147
+ export const credentials = (options: Options): Layer.Layer<Credentials> =>
148
+ Layer.succeed(Credentials, Effect.succeed(resolveConfig(options)));
149
+
150
+ /** Convenience layer from a tenant token alone. */
151
+ export const fromToken = (token: Redacted.Redacted<string>): Layer.Layer<Credentials> =>
152
+ credentials({ token });
153
+
154
+ /** Convenience layer reading `bearer_token` from a token file on every resolution. */
155
+ export const fromTokenFile = (
156
+ path: string,
157
+ hosts: Hosts = {},
158
+ ): Layer.Layer<Credentials, never, FileSystem.FileSystem> =>
159
+ Layer.effect(
160
+ Credentials,
161
+ Effect.map(FileSystem.FileSystem, (fs) =>
162
+ Effect.map(readTokenFile(fs, path), (token) => resolveConfig({ token, ...hosts })),
163
+ ),
164
+ );
package/src/errors.ts ADDED
@@ -0,0 +1,47 @@
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
+ import * as Category from "@distilled.cloud/core/category";
20
+ import type { DefaultErrors as CoreDefaultErrors } from "@distilled.cloud/core/errors";
21
+ import * as Schema from "@distilled.cloud/core/schema-lite-typed";
22
+
23
+ /**
24
+ * A failed response no declared error class matches. `code` is the Connect
25
+ * error code (`unknown`, `data_loss`, ...) and `details` the Connect error
26
+ * details, which carry the request id.
27
+ */
28
+ export class UnknownNamespaceLabsError
29
+ extends /*@__PURE__*/ Schema.TaggedError<UnknownNamespaceLabsError>()(
30
+ "UnknownNamespaceLabsError",
31
+ {
32
+ code: /*@__PURE__*/ Schema.optional(Schema.String),
33
+ message: /*@__PURE__*/ Schema.optional(Schema.String),
34
+ details: /*@__PURE__*/ Schema.optional(Schema.Unknown),
35
+ body: Schema.Unknown,
36
+ },
37
+ ).pipe(Category.withServerError) {}
38
+
39
+ export class NamespaceLabsParseError
40
+ extends /*@__PURE__*/ Schema.TaggedError<NamespaceLabsParseError>()("NamespaceLabsParseError", {
41
+ body: Schema.Unknown,
42
+ cause: Schema.Unknown,
43
+ }).pipe(Category.withParseError) {}
44
+
45
+ export type ClientErrors = UnknownNamespaceLabsError | NamespaceLabsParseError;
46
+
47
+ export type DefaultErrors = CoreDefaultErrors | ClientErrors;
package/src/index.ts ADDED
@@ -0,0 +1,48 @@
1
+ /**
2
+ * @distilled.cloud/namespacelabs -- Namespace (namespace.so) SDK for Effect.
3
+ *
4
+ * `./services` is generated by `scripts/generate.ts` from the Smithy models
5
+ * that `scripts/convert.ts` converts from Namespace's public protobuf API
6
+ * (`buf.build/namespace/cloud`). 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 NamespaceLabs from "@distilled.cloud/namespacelabs";
13
+ *
14
+ * const program = Effect.gen(function* () {
15
+ * const created = yield* NamespaceLabs.compute.createInstance({
16
+ * shape: { virtualCpu: 2, memoryMegabytes: 4096, os: "linux", machineArch: "amd64" },
17
+ * deadline: new Date(Date.now() + 10 * 60 * 1000).toISOString(),
18
+ * });
19
+ * const instanceId = created.metadata?.instanceId;
20
+ * if (instanceId === undefined) return undefined;
21
+ * const instance = yield* NamespaceLabs.compute.describeInstance({ instanceId });
22
+ * yield* NamespaceLabs.compute.destroyInstance({ instanceId });
23
+ * return instance.metadata?.status;
24
+ * });
25
+ *
26
+ * const Live = Layer.mergeAll(
27
+ * FetchHttpClient.layer,
28
+ * NamespaceLabs.CredentialsFromEnv,
29
+ * NamespaceLabs.NamespaceLabsProtocol,
30
+ * );
31
+ *
32
+ * program.pipe(Effect.provide(Live), Effect.runPromise);
33
+ * ```
34
+ */
35
+ export * from "./credentials.ts";
36
+ export * from "./errors.ts";
37
+ export * as T from "./traits.ts";
38
+ export {
39
+ NamespaceLabsProtocol,
40
+ baseUrlFor,
41
+ hostFor,
42
+ type Host,
43
+ type NamespaceLabsOpError,
44
+ type NamespaceLabsOpContext,
45
+ } from "./protocol.ts";
46
+ export { paginateCursor } from "./pagination.ts";
47
+ export * as Retry from "./retry.ts";
48
+ export * from "./services/index.ts";
@@ -0,0 +1 @@
1
+ export { paginateCursor } from "@distilled.cloud/core/pagination";
@@ -0,0 +1,82 @@
1
+ import type * as API from "@distilled.cloud/core/api";
2
+ import type { ConfigError, InvalidInput } from "@distilled.cloud/core/errors";
3
+ import { makeRestProtocol, type RestErrorEnvelope } from "@distilled.cloud/core/protocol-rest";
4
+ import * as Effect from "effect/Effect";
5
+ import type * as HttpClient from "effect/http/HttpClient";
6
+ import type * as HttpClientError from "effect/http/HttpClientError";
7
+ import type * as Layer from "effect/Layer";
8
+ import * as Redacted from "effect/Redacted";
9
+ import { Credentials, type Config } from "./credentials.ts";
10
+ import {
11
+ UnknownNamespaceLabsError,
12
+ type DefaultErrors,
13
+ NamespaceLabsParseError,
14
+ } from "./errors.ts";
15
+
16
+ export type NamespaceLabsOpError =
17
+ | DefaultErrors
18
+ | ConfigError
19
+ | HttpClientError.HttpClientError
20
+ | InvalidInput;
21
+
22
+ export type NamespaceLabsOpContext = Credentials | HttpClient.HttpClient;
23
+
24
+ export type Host = "compute" | "storage" | "global" | "privateApi";
25
+
26
+ const HOST_BY_PACKAGE_PREFIX: ReadonlyArray<readonly [prefix: string, host: Host]> = [
27
+ ["namespace.cloud.compute.", "compute"],
28
+ ["namespace.cloud.builder.", "compute"],
29
+ ["namespace.experimental.compute.", "compute"],
30
+ ["namespace.cloud.storage.", "storage"],
31
+ ["namespace.cloud.github.", "privateApi"],
32
+ ["namespace.private.", "privateApi"],
33
+ ];
34
+
35
+ /** The host serving `uri` (`/<package>.<Service>/<Method>`), by proto package. */
36
+ export const hostFor = (uri: string): Host => {
37
+ const service = uri.replace(/^\//, "").split("/")[0] ?? "";
38
+ return HOST_BY_PACKAGE_PREFIX.find(([prefix]) => service.startsWith(prefix))?.[1] ?? "global";
39
+ };
40
+
41
+ export const baseUrlFor = (creds: Config, uri: string): string => {
42
+ switch (hostFor(uri)) {
43
+ case "compute":
44
+ return creds.computeBaseUrl;
45
+ case "storage":
46
+ return creds.storageBaseUrl;
47
+ case "privateApi":
48
+ return creds.privateApiBaseUrl;
49
+ case "global":
50
+ return creds.globalBaseUrl;
51
+ }
52
+ };
53
+
54
+ /** Connect error bodies are `{ code, message, details? }`. */
55
+ const errorEnvelope = (body: unknown): RestErrorEnvelope | undefined => {
56
+ if (body === null || typeof body !== "object") return undefined;
57
+ const b = body as Record<string, unknown>;
58
+ return {
59
+ code: typeof b.code === "string" ? b.code : undefined,
60
+ message: typeof b.message === "string" ? b.message : undefined,
61
+ };
62
+ };
63
+
64
+ const errorDetails = (body: unknown): unknown =>
65
+ body !== null && typeof body === "object" ? (body as Record<string, unknown>).details : undefined;
66
+
67
+ export const NamespaceLabsProtocol: Layer.Layer<API.Protocol> =
68
+ /*@__PURE__*/ makeRestProtocol<Config>({
69
+ credentials: /*@__PURE__*/ Effect.flatMap(Credentials, (resolve) => resolve),
70
+ baseUrl: (creds, target) => baseUrlFor(creds, target.uri),
71
+ headers: (creds) => ({ Authorization: `Bearer ${Redacted.value(creds.token)}` }),
72
+ emptyJsonBody: true,
73
+ errorEnvelope,
74
+ unknownError: ({ code, message, body }) =>
75
+ new UnknownNamespaceLabsError({
76
+ code: typeof code === "string" ? code : code !== undefined ? String(code) : undefined,
77
+ message,
78
+ details: errorDetails(body),
79
+ body,
80
+ }),
81
+ parseError: ({ body, cause }) => new NamespaceLabsParseError({ body, cause }),
82
+ });
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>()("NamespaceLabsRetry") {}
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);