@alienplatform/bindings 1.0.0 → 2.0.0

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 (56) hide show
  1. package/LICENSE.md +105 -0
  2. package/README.md +59 -0
  3. package/dist/errors.d.ts +56 -0
  4. package/dist/errors.d.ts.map +1 -0
  5. package/dist/factories.d.ts +26 -0
  6. package/dist/factories.d.ts.map +1 -0
  7. package/dist/index.d.ts +19 -1768
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +23 -44772
  10. package/dist/loader.d.ts +171 -0
  11. package/dist/loader.d.ts.map +1 -0
  12. package/dist/loader.js +3712 -0
  13. package/dist/native.d.ts +41 -0
  14. package/dist/native.d.ts.map +1 -0
  15. package/dist/native.js +46 -0
  16. package/dist/types.d.ts +150 -0
  17. package/dist/types.d.ts.map +1 -0
  18. package/package.json +39 -21
  19. package/AGENTS.md +0 -70
  20. package/dist/index.js.map +0 -1
  21. package/scripts/generate-proto.sh +0 -46
  22. package/src/bindings/AGENTS.md +0 -105
  23. package/src/bindings/artifact-registry.ts +0 -314
  24. package/src/bindings/build.ts +0 -195
  25. package/src/bindings/function.ts +0 -164
  26. package/src/bindings/index.ts +0 -12
  27. package/src/bindings/kv.ts +0 -240
  28. package/src/bindings/queue.ts +0 -191
  29. package/src/bindings/service-account.ts +0 -113
  30. package/src/bindings/storage.ts +0 -535
  31. package/src/bindings/vault.ts +0 -133
  32. package/src/channel.ts +0 -94
  33. package/src/commands.ts +0 -76
  34. package/src/context.ts +0 -368
  35. package/src/errors.ts +0 -259
  36. package/src/events.ts +0 -463
  37. package/src/generated/artifact_registry.ts +0 -1934
  38. package/src/generated/build.ts +0 -1263
  39. package/src/generated/container.ts +0 -485
  40. package/src/generated/control.ts +0 -1574
  41. package/src/generated/function.ts +0 -741
  42. package/src/generated/google/protobuf/duration.ts +0 -196
  43. package/src/generated/google/protobuf/timestamp.ts +0 -225
  44. package/src/generated/kv.ts +0 -1137
  45. package/src/generated/queue.ts +0 -729
  46. package/src/generated/service_account.ts +0 -766
  47. package/src/generated/storage.ts +0 -3653
  48. package/src/generated/vault.ts +0 -519
  49. package/src/generated/wait_until.ts +0 -781
  50. package/src/global.ts +0 -316
  51. package/src/grpc-utils.ts +0 -159
  52. package/src/index.ts +0 -156
  53. package/src/types.ts +0 -384
  54. package/src/wait-until.ts +0 -257
  55. package/tsconfig.json +0 -8
  56. package/tsdown.config.ts +0 -14
package/src/global.ts DELETED
@@ -1,316 +0,0 @@
1
- /**
2
- * Global convenience functions for Alien bindings.
3
- *
4
- * These functions use a singleton AlienContext for easy access.
5
- */
6
-
7
- import type { ArtifactRegistry } from "./bindings/artifact-registry.js"
8
- import type { Build } from "./bindings/build.js"
9
- import type { FunctionBinding } from "./bindings/function.js"
10
- import type { Kv } from "./bindings/kv.js"
11
- import type { Queue } from "./bindings/queue.js"
12
- import type { ServiceAccount } from "./bindings/service-account.js"
13
- import type { Storage } from "./bindings/storage.js"
14
- import type { Vault } from "./bindings/vault.js"
15
- import { command as commandImpl } from "./commands.js"
16
- import { AlienContext } from "./context.js"
17
- import {
18
- type CronEvent,
19
- type QueueMessageEvent,
20
- type StorageEvent,
21
- onCronEvent as onCronEventImpl,
22
- onQueueMessage as onQueueMessageImpl,
23
- onStorageEvent as onStorageEventImpl,
24
- } from "./events.js"
25
- import { waitUntil as waitUntilImpl } from "./wait-until.js"
26
-
27
- // Lazy-initialized global context
28
- let globalContext: AlienContext | undefined
29
-
30
- /**
31
- * Get or create the global AlienContext.
32
- */
33
- async function getGlobalContext(): Promise<AlienContext> {
34
- if (!globalContext) {
35
- globalContext = await AlienContext.fromEnv()
36
- }
37
- return globalContext
38
- }
39
-
40
- // ============================================================================
41
- // Binding Accessors
42
- // ============================================================================
43
-
44
- /**
45
- * Get a storage binding.
46
- *
47
- * @param name - Binding name
48
- * @returns Storage binding instance
49
- *
50
- * @example
51
- * ```typescript
52
- * import { storage } from "@alienplatform/bindings"
53
- *
54
- * const bucket = await storage("my-bucket")
55
- * await bucket.put("hello.txt", "Hello, World!")
56
- * ```
57
- */
58
- export async function storage(name: string): Promise<Storage> {
59
- return (await getGlobalContext()).storage(name)
60
- }
61
-
62
- /**
63
- * Get a KV binding.
64
- *
65
- * @param name - Binding name
66
- * @returns KV binding instance
67
- *
68
- * @example
69
- * ```typescript
70
- * import { kv } from "@alienplatform/bindings"
71
- *
72
- * const cache = await kv("my-cache")
73
- * await cache.set("key", "value")
74
- * const value = await cache.getText("key")
75
- * ```
76
- */
77
- export async function kv(name: string): Promise<Kv> {
78
- return (await getGlobalContext()).kv(name)
79
- }
80
-
81
- /**
82
- * Get a queue binding.
83
- *
84
- * @param name - Binding name
85
- * @returns Queue binding instance
86
- *
87
- * @example
88
- * ```typescript
89
- * import { queue } from "@alienplatform/bindings"
90
- *
91
- * const tasks = await queue("task-queue")
92
- * await tasks.send("job-type", { jobId: "123" })
93
- * ```
94
- */
95
- export async function queue(name: string): Promise<Queue> {
96
- return (await getGlobalContext()).queue(name)
97
- }
98
-
99
- /**
100
- * Get a vault binding.
101
- *
102
- * @param name - Binding name
103
- * @returns Vault binding instance
104
- *
105
- * @example
106
- * ```typescript
107
- * import { vault } from "@alienplatform/bindings"
108
- *
109
- * const secrets = await vault("app-secrets")
110
- * const apiKey = await secrets.get("API_KEY")
111
- * ```
112
- */
113
- export async function vault(name: string): Promise<Vault> {
114
- return (await getGlobalContext()).vault(name)
115
- }
116
-
117
- /**
118
- * Get a build binding.
119
- *
120
- * @param name - Binding name
121
- * @returns Build binding instance
122
- *
123
- * @example
124
- * ```typescript
125
- * import { build } from "@alienplatform/bindings"
126
- *
127
- * const builder = await build("my-builder")
128
- * const execution = await builder.start({ script: "npm run build" })
129
- * ```
130
- */
131
- export async function build(name: string): Promise<Build> {
132
- return (await getGlobalContext()).build(name)
133
- }
134
-
135
- /**
136
- * Get an artifact registry binding.
137
- *
138
- * @param name - Binding name
139
- * @returns ArtifactRegistry binding instance
140
- *
141
- * @example
142
- * ```typescript
143
- * import { artifactRegistry } from "@alienplatform/bindings"
144
- *
145
- * const registry = await artifactRegistry("my-registry")
146
- * const repo = await registry.createRepository("my-app")
147
- * ```
148
- */
149
- export async function artifactRegistry(name: string): Promise<ArtifactRegistry> {
150
- return (await getGlobalContext()).artifactRegistry(name)
151
- }
152
-
153
- /**
154
- * Get a function binding.
155
- *
156
- * @param name - Binding name
157
- * @returns Function binding instance
158
- *
159
- * @example
160
- * ```typescript
161
- * import { func } from "@alienplatform/bindings"
162
- *
163
- * const processor = await func("image-processor")
164
- * const result = await processor.invokeJson("resize", { width: 800 })
165
- * ```
166
- */
167
- export async function func(name: string): Promise<FunctionBinding> {
168
- return (await getGlobalContext()).func(name)
169
- }
170
-
171
- /**
172
- * Get a service account binding.
173
- *
174
- * @param name - Binding name
175
- * @returns ServiceAccount binding instance
176
- *
177
- * @example
178
- * ```typescript
179
- * import { serviceAccount } from "@alienplatform/bindings"
180
- *
181
- * const sa = await serviceAccount("deployment-account")
182
- * const info = await sa.getInfo()
183
- * ```
184
- */
185
- export async function serviceAccount(name: string): Promise<ServiceAccount> {
186
- return (await getGlobalContext()).serviceAccount(name)
187
- }
188
-
189
- // ============================================================================
190
- // Event Handlers
191
- // ============================================================================
192
-
193
- /**
194
- * Register a storage event handler.
195
- *
196
- * @param bucket - Bucket name
197
- * @param handler - Event handler
198
- * @param options - Handler options
199
- * @returns Unsubscribe function
200
- *
201
- * @example
202
- * ```typescript
203
- * import { onStorageEvent } from "@alienplatform/bindings"
204
- *
205
- * onStorageEvent("uploads", async (event) => {
206
- * console.log("File uploaded:", event.objectKey)
207
- * })
208
- * ```
209
- */
210
- export function onStorageEvent(
211
- bucket: string,
212
- handler: (event: StorageEvent) => Promise<void>,
213
- options?: { prefix?: string },
214
- ): () => void {
215
- return onStorageEventImpl(bucket, handler, options)
216
- }
217
-
218
- /**
219
- * Register a cron event handler.
220
- *
221
- * @param schedule - Cron schedule expression
222
- * @param handler - Event handler
223
- * @returns Unsubscribe function
224
- *
225
- * @example
226
- * ```typescript
227
- * import { onCronEvent } from "@alienplatform/bindings"
228
- *
229
- * onCronEvent("0 * * * *", async (event) => {
230
- * console.log("Hourly task running at:", event.timestamp)
231
- * })
232
- * ```
233
- */
234
- export function onCronEvent(
235
- schedule: string,
236
- handler: (event: CronEvent) => Promise<void>,
237
- ): () => void {
238
- return onCronEventImpl(schedule, handler)
239
- }
240
-
241
- /**
242
- * Register a queue message handler.
243
- *
244
- * @param queueName - Queue name
245
- * @param handler - Message handler
246
- * @returns Unsubscribe function
247
- *
248
- * @example
249
- * ```typescript
250
- * import { onQueueMessage } from "@alienplatform/bindings"
251
- *
252
- * onQueueMessage("tasks", async (message) => {
253
- * console.log("Processing:", message.payload)
254
- * })
255
- * ```
256
- */
257
- export function onQueueMessage<T = unknown>(
258
- queueName: string,
259
- handler: (message: QueueMessageEvent<T>) => Promise<void>,
260
- ): () => void {
261
- return onQueueMessageImpl(queueName, handler)
262
- }
263
-
264
- // ============================================================================
265
- // Commands
266
- // ============================================================================
267
-
268
- /**
269
- * Register a CLI command.
270
- *
271
- * @param name - Command name
272
- * @param description - Command description
273
- * @param handler - Command handler
274
- *
275
- * @example
276
- * ```typescript
277
- * import { command } from "@alienplatform/bindings"
278
- *
279
- * command("migrate", "Run database migrations", async (args) => {
280
- * await runMigrations()
281
- * return 0
282
- * })
283
- * ```
284
- */
285
- export function command(
286
- name: string,
287
- _description: string,
288
- handler: (args: string[]) => Promise<number>,
289
- ): void {
290
- commandImpl(name, handler)
291
- }
292
-
293
- // ============================================================================
294
- // WaitUntil
295
- // ============================================================================
296
-
297
- /**
298
- * Register a background task to continue after the response.
299
- *
300
- * @param promise - The promise to track
301
- *
302
- * @example
303
- * ```typescript
304
- * import { waitUntil } from "@alienplatform/bindings"
305
- *
306
- * export default {
307
- * async fetch(request: Request): Promise<Response> {
308
- * waitUntil(sendAnalytics(request))
309
- * return Response.json({ status: "ok" })
310
- * }
311
- * }
312
- * ```
313
- */
314
- export function waitUntil(promise: Promise<unknown>): void {
315
- waitUntilImpl(promise)
316
- }
package/src/grpc-utils.ts DELETED
@@ -1,159 +0,0 @@
1
- /**
2
- * gRPC utility functions for error handling and type conversion.
3
- */
4
-
5
- import { AlienError } from "@alienplatform/core"
6
- import { Status } from "nice-grpc"
7
- import {
8
- BindingNotFoundError,
9
- GrpcCallError,
10
- KvInvalidKeyError,
11
- KvInvalidValueError,
12
- KvKeyNotFoundError,
13
- SecretNotFoundError,
14
- StorageObjectExistsError,
15
- StorageObjectNotFoundError,
16
- StoragePreconditionError,
17
- } from "./errors.js"
18
-
19
- /**
20
- * Convert a gRPC error to an AlienError with proper chaining.
21
- */
22
- export async function grpcErrorToAlienError(
23
- error: unknown,
24
- service: string,
25
- method: string,
26
- context?: {
27
- bindingName?: string
28
- path?: string
29
- key?: string
30
- secretName?: string
31
- bindingType?: string
32
- },
33
- ): Promise<AlienError<any>> {
34
- // Start with the original error wrapped as AlienError
35
- const baseError = await AlienError.from(error)
36
-
37
- // Handle nice-grpc errors
38
- if (error && typeof error === "object" && "code" in error) {
39
- const grpcError = error as { code: number; details?: string; message?: string }
40
- const details = grpcError.details ?? grpcError.message ?? "Unknown error"
41
- const code = grpcError.code
42
-
43
- // Map specific gRPC status codes to domain errors
44
- if (code === Status.NOT_FOUND) {
45
- // Determine the appropriate not-found error based on context
46
- if (context?.path && context?.bindingName) {
47
- return baseError.withContext(
48
- StorageObjectNotFoundError.create({
49
- bindingName: context.bindingName,
50
- path: context.path,
51
- }),
52
- )
53
- }
54
- if (context?.key && context?.bindingName) {
55
- return baseError.withContext(
56
- KvKeyNotFoundError.create({
57
- bindingName: context.bindingName,
58
- key: context.key,
59
- }),
60
- )
61
- }
62
- if (context?.secretName && context?.bindingName) {
63
- return baseError.withContext(
64
- SecretNotFoundError.create({
65
- bindingName: context.bindingName,
66
- secretName: context.secretName,
67
- }),
68
- )
69
- }
70
- if (context?.bindingName && context?.bindingType) {
71
- return baseError.withContext(
72
- BindingNotFoundError.create({
73
- bindingName: context.bindingName,
74
- bindingType: context.bindingType,
75
- }),
76
- )
77
- }
78
- }
79
-
80
- if (code === Status.FAILED_PRECONDITION && context?.path && context?.bindingName) {
81
- return baseError.withContext(
82
- StoragePreconditionError.create({
83
- bindingName: context.bindingName,
84
- path: context.path,
85
- condition: details,
86
- }),
87
- )
88
- }
89
-
90
- if (code === Status.ALREADY_EXISTS && context?.path && context?.bindingName) {
91
- return baseError.withContext(
92
- StorageObjectExistsError.create({
93
- bindingName: context.bindingName,
94
- path: context.path,
95
- }),
96
- )
97
- }
98
-
99
- if (code === Status.INVALID_ARGUMENT) {
100
- if (context?.key) {
101
- return baseError.withContext(
102
- KvInvalidKeyError.create({
103
- key: context.key,
104
- reason: details,
105
- }),
106
- )
107
- }
108
- if (service === "KvService") {
109
- return baseError.withContext(
110
- KvInvalidValueError.create({
111
- reason: details,
112
- }),
113
- )
114
- }
115
- }
116
-
117
- // Generic gRPC error
118
- return baseError.withContext(
119
- GrpcCallError.create({
120
- service,
121
- method,
122
- grpcCode: Status[code] ?? String(code),
123
- details,
124
- }),
125
- )
126
- }
127
-
128
- // Fallback for non-gRPC errors
129
- return baseError.withContext(
130
- GrpcCallError.create({
131
- service,
132
- method,
133
- grpcCode: "UNKNOWN",
134
- details: error instanceof Error ? error.message : String(error),
135
- }),
136
- )
137
- }
138
-
139
- /**
140
- * Wrap an async gRPC call with error handling.
141
- */
142
- export async function wrapGrpcCall<T>(
143
- service: string,
144
- method: string,
145
- call: () => Promise<T>,
146
- context?: {
147
- bindingName?: string
148
- path?: string
149
- key?: string
150
- secretName?: string
151
- bindingType?: string
152
- },
153
- ): Promise<T> {
154
- try {
155
- return await call()
156
- } catch (error) {
157
- throw await grpcErrorToAlienError(error, service, method, context)
158
- }
159
- }
package/src/index.ts DELETED
@@ -1,156 +0,0 @@
1
- /**
2
- * @alienplatform/bindings - TypeScript SDK for Alien bindings.
3
- *
4
- * This package provides type-safe access to Alien resources including
5
- * Storage, KV, Queue, Vault, Build, ArtifactRegistry, Function, and ServiceAccount.
6
- *
7
- * @example
8
- * ```typescript
9
- * // Global convenience functions (most common usage)
10
- * import { storage, kv, onStorageEvent, waitUntil } from "@alienplatform/bindings"
11
- *
12
- * const bucket = storage("my-bucket")
13
- * await bucket.put("hello.txt", "Hello, World!")
14
- *
15
- * // Or use AlienContext for more control
16
- * import { AlienContext } from "@alienplatform/bindings"
17
- *
18
- * const ctx = AlienContext.fromEnv()
19
- * const bucket = ctx.storage("my-bucket")
20
- * ```
21
- *
22
- * @packageDocumentation
23
- */
24
-
25
- // ============================================================================
26
- // Main Entry Point
27
- // ============================================================================
28
-
29
- export { AlienContext } from "./context.js"
30
-
31
- // ============================================================================
32
- // Global Convenience Functions
33
- // ============================================================================
34
-
35
- export {
36
- // Binding accessors
37
- storage,
38
- kv,
39
- queue,
40
- vault,
41
- build,
42
- artifactRegistry,
43
- func,
44
- serviceAccount,
45
- // Event handlers
46
- onStorageEvent,
47
- onCronEvent,
48
- onQueueMessage,
49
- // Commands
50
- command,
51
- // WaitUntil
52
- waitUntil,
53
- } from "./global.js"
54
-
55
- // Event types - StorageEvent, QueueMessage, ScheduledEvent come from @alienplatform/core
56
- // CronEvent and QueueMessageEvent are bindings-specific extensions
57
- export type {
58
- StorageEvent,
59
- StorageEventType,
60
- QueueMessage,
61
- ScheduledEvent,
62
- CronEvent,
63
- QueueMessageEvent,
64
- } from "./events.js"
65
-
66
- // ============================================================================
67
- // Binding Classes (for advanced usage)
68
- // ============================================================================
69
-
70
- export { Storage } from "./bindings/storage.js"
71
- export { Kv } from "./bindings/kv.js"
72
- export { Queue } from "./bindings/queue.js"
73
- export { Vault } from "./bindings/vault.js"
74
- export { Build } from "./bindings/build.js"
75
- export { ArtifactRegistry } from "./bindings/artifact-registry.js"
76
- export { FunctionBinding } from "./bindings/function.js"
77
- export { ServiceAccount } from "./bindings/service-account.js"
78
-
79
- // ============================================================================
80
- // Types
81
- // ============================================================================
82
-
83
- export type {
84
- // Storage
85
- StorageGetOptions,
86
- StoragePutOptions,
87
- StorageObjectMeta,
88
- StorageGetResult,
89
- StorageListResult,
90
- SignedUrlOptions,
91
- SignedUrlResult,
92
- // KV
93
- KvPutOptions,
94
- KvScanResult,
95
- // Queue
96
- ReceivedQueueMessage,
97
- // Build (BuildStatus and ComputeType come from @alienplatform/core)
98
- BuildStartConfig,
99
- BuildExecution,
100
- // Artifact Registry
101
- ArtifactRegistryPermissions,
102
- RepositoryInfo,
103
- ArtifactRegistryCredentials,
104
- ComputeServiceType,
105
- AwsCrossAccountAccess,
106
- GcpCrossAccountAccess,
107
- CrossAccountAccess,
108
- CrossAccountPermissions,
109
- // Function
110
- FunctionInvokeRequest,
111
- FunctionInvokeResponse,
112
- // Service Account
113
- ServiceAccountInfo,
114
- AwsServiceAccountInfo,
115
- GcpServiceAccountInfo,
116
- AzureServiceAccountInfo,
117
- ImpersonationRequest,
118
- // Configuration
119
- AlienBindingsConfig,
120
- AlienBindingsProvider,
121
- } from "./types.js"
122
-
123
- // ============================================================================
124
- // Errors
125
- // ============================================================================
126
-
127
- export {
128
- GrpcConnectionError,
129
- GrpcCallError,
130
- BindingNotFoundError,
131
- ContextNotInitializedError,
132
- MissingEnvVarError,
133
- StorageObjectNotFoundError,
134
- StoragePreconditionError,
135
- StorageObjectExistsError,
136
- KvKeyNotFoundError,
137
- KvInvalidKeyError,
138
- KvInvalidValueError,
139
- SecretNotFoundError,
140
- QueueOperationError,
141
- EventHandlerAlreadyRegisteredError,
142
- CommandAlreadyRegisteredError,
143
- InvalidBindingConfigError,
144
- } from "./errors.js"
145
-
146
- // ============================================================================
147
- // Commands
148
- // ============================================================================
149
-
150
- export { runCommand, getCommands, type CommandDefinition } from "./commands.js"
151
-
152
- // ============================================================================
153
- // Re-export from core for convenience
154
- // ============================================================================
155
-
156
- // Note: BuildStatus, ComputeType are re-exported via types.ts