@alienplatform/bindings 1.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.
- package/AGENTS.md +70 -0
- package/dist/index.d.ts +1770 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +44772 -0
- package/dist/index.js.map +1 -0
- package/package.json +56 -0
- package/scripts/generate-proto.sh +46 -0
- package/src/bindings/AGENTS.md +105 -0
- package/src/bindings/artifact-registry.ts +314 -0
- package/src/bindings/build.ts +195 -0
- package/src/bindings/function.ts +164 -0
- package/src/bindings/index.ts +12 -0
- package/src/bindings/kv.ts +240 -0
- package/src/bindings/queue.ts +191 -0
- package/src/bindings/service-account.ts +113 -0
- package/src/bindings/storage.ts +535 -0
- package/src/bindings/vault.ts +133 -0
- package/src/channel.ts +94 -0
- package/src/commands.ts +76 -0
- package/src/context.ts +368 -0
- package/src/errors.ts +259 -0
- package/src/events.ts +463 -0
- package/src/generated/artifact_registry.ts +1934 -0
- package/src/generated/build.ts +1263 -0
- package/src/generated/container.ts +485 -0
- package/src/generated/control.ts +1574 -0
- package/src/generated/function.ts +741 -0
- package/src/generated/google/protobuf/duration.ts +196 -0
- package/src/generated/google/protobuf/timestamp.ts +225 -0
- package/src/generated/kv.ts +1137 -0
- package/src/generated/queue.ts +729 -0
- package/src/generated/service_account.ts +766 -0
- package/src/generated/storage.ts +3653 -0
- package/src/generated/vault.ts +519 -0
- package/src/generated/wait_until.ts +781 -0
- package/src/global.ts +316 -0
- package/src/grpc-utils.ts +159 -0
- package/src/index.ts +156 -0
- package/src/types.ts +384 -0
- package/src/wait-until.ts +257 -0
- package/tsconfig.json +8 -0
- package/tsdown.config.ts +14 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Queue binding implementation.
|
|
3
|
+
*
|
|
4
|
+
* Provides message queue operations for async task processing.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { type Channel, createClient } from "nice-grpc"
|
|
8
|
+
import {
|
|
9
|
+
type QueueServiceClient as GeneratedQueueServiceClient,
|
|
10
|
+
type MessagePayload,
|
|
11
|
+
type QueueMessage as QueueMessageProto,
|
|
12
|
+
QueueServiceDefinition,
|
|
13
|
+
} from "../generated/queue.js"
|
|
14
|
+
import { wrapGrpcCall } from "../grpc-utils.js"
|
|
15
|
+
import type { ReceivedQueueMessage } from "../types.js"
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Queue binding for message queue operations.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* import { queue } from "@alienplatform/bindings"
|
|
23
|
+
*
|
|
24
|
+
* const tasks = queue("task-queue")
|
|
25
|
+
*
|
|
26
|
+
* // Send a message (JSON)
|
|
27
|
+
* await tasks.send("process-image", { imageId: "123", format: "webp" })
|
|
28
|
+
*
|
|
29
|
+
* // Receive messages
|
|
30
|
+
* const messages = await tasks.receive("process-image", 10)
|
|
31
|
+
* for (const msg of messages) {
|
|
32
|
+
* await processImage(msg.payload)
|
|
33
|
+
* await tasks.ack("process-image", msg.receiptHandle)
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export class Queue {
|
|
38
|
+
private readonly client: GeneratedQueueServiceClient
|
|
39
|
+
private readonly bindingName: string
|
|
40
|
+
|
|
41
|
+
constructor(channel: Channel, bindingName: string) {
|
|
42
|
+
this.client = createClient(QueueServiceDefinition, channel)
|
|
43
|
+
this.bindingName = bindingName
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Send a message to a queue.
|
|
48
|
+
*
|
|
49
|
+
* @param queueName - Name of the queue
|
|
50
|
+
* @param payload - Message payload (object for JSON, string for text)
|
|
51
|
+
*/
|
|
52
|
+
async send(queueName: string, payload: unknown): Promise<void> {
|
|
53
|
+
const message = this.toProtoPayload(payload)
|
|
54
|
+
|
|
55
|
+
await wrapGrpcCall(
|
|
56
|
+
"QueueService",
|
|
57
|
+
"Send",
|
|
58
|
+
async () => {
|
|
59
|
+
await this.client.send({
|
|
60
|
+
bindingName: this.bindingName,
|
|
61
|
+
queue: queueName,
|
|
62
|
+
message,
|
|
63
|
+
})
|
|
64
|
+
},
|
|
65
|
+
{ bindingName: this.bindingName },
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Send a text message to a queue.
|
|
71
|
+
*
|
|
72
|
+
* @param queueName - Name of the queue
|
|
73
|
+
* @param text - Text message
|
|
74
|
+
*/
|
|
75
|
+
async sendText(queueName: string, text: string): Promise<void> {
|
|
76
|
+
await wrapGrpcCall(
|
|
77
|
+
"QueueService",
|
|
78
|
+
"Send",
|
|
79
|
+
async () => {
|
|
80
|
+
await this.client.send({
|
|
81
|
+
bindingName: this.bindingName,
|
|
82
|
+
queue: queueName,
|
|
83
|
+
message: { text },
|
|
84
|
+
})
|
|
85
|
+
},
|
|
86
|
+
{ bindingName: this.bindingName },
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Receive messages from a queue.
|
|
92
|
+
*
|
|
93
|
+
* @param queueName - Name of the queue
|
|
94
|
+
* @param maxMessages - Maximum number of messages to receive (1-10)
|
|
95
|
+
* @returns Array of messages with receipt handles
|
|
96
|
+
*/
|
|
97
|
+
async receive<T = unknown>(
|
|
98
|
+
queueName: string,
|
|
99
|
+
maxMessages = 1,
|
|
100
|
+
): Promise<ReceivedQueueMessage<T>[]> {
|
|
101
|
+
return await wrapGrpcCall(
|
|
102
|
+
"QueueService",
|
|
103
|
+
"Receive",
|
|
104
|
+
async () => {
|
|
105
|
+
const response = await this.client.receive({
|
|
106
|
+
bindingName: this.bindingName,
|
|
107
|
+
queue: queueName,
|
|
108
|
+
maxMessages: Math.min(Math.max(1, maxMessages), 10),
|
|
109
|
+
})
|
|
110
|
+
return response.messages.map(msg => this.fromProtoMessage<T>(msg))
|
|
111
|
+
},
|
|
112
|
+
{ bindingName: this.bindingName },
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Acknowledge a message (remove it from the queue).
|
|
118
|
+
*
|
|
119
|
+
* @param queueName - Name of the queue
|
|
120
|
+
* @param receiptHandle - Receipt handle from the received message
|
|
121
|
+
*/
|
|
122
|
+
async ack(queueName: string, receiptHandle: string): Promise<void> {
|
|
123
|
+
await wrapGrpcCall(
|
|
124
|
+
"QueueService",
|
|
125
|
+
"Ack",
|
|
126
|
+
async () => {
|
|
127
|
+
await this.client.ack({
|
|
128
|
+
bindingName: this.bindingName,
|
|
129
|
+
queue: queueName,
|
|
130
|
+
receiptHandle,
|
|
131
|
+
})
|
|
132
|
+
},
|
|
133
|
+
{ bindingName: this.bindingName },
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Process messages from a queue with automatic acknowledgment.
|
|
139
|
+
*
|
|
140
|
+
* @param queueName - Name of the queue
|
|
141
|
+
* @param handler - Handler function for each message
|
|
142
|
+
* @param options - Processing options
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* await tasks.process("image-tasks", async (payload) => {
|
|
147
|
+
* await processImage(payload.imageId)
|
|
148
|
+
* })
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
async process<T = unknown>(
|
|
152
|
+
queueName: string,
|
|
153
|
+
handler: (payload: T) => Promise<void>,
|
|
154
|
+
options?: { maxMessages?: number },
|
|
155
|
+
): Promise<void> {
|
|
156
|
+
const maxMessages = options?.maxMessages ?? 10
|
|
157
|
+
|
|
158
|
+
const messages = await this.receive<T>(queueName, maxMessages)
|
|
159
|
+
|
|
160
|
+
for (const message of messages) {
|
|
161
|
+
await handler(message.payload)
|
|
162
|
+
await this.ack(queueName, message.receiptHandle)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Private helpers
|
|
167
|
+
|
|
168
|
+
private toProtoPayload(payload: unknown): MessagePayload {
|
|
169
|
+
if (typeof payload === "string") {
|
|
170
|
+
return { text: payload }
|
|
171
|
+
}
|
|
172
|
+
return { json: JSON.stringify(payload) }
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private fromProtoMessage<T>(proto: QueueMessageProto): ReceivedQueueMessage<T> {
|
|
176
|
+
let payload: unknown
|
|
177
|
+
|
|
178
|
+
if (proto.payload?.json) {
|
|
179
|
+
payload = JSON.parse(proto.payload.json)
|
|
180
|
+
} else if (proto.payload?.text) {
|
|
181
|
+
payload = proto.payload.text
|
|
182
|
+
} else {
|
|
183
|
+
payload = null
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
payload: payload as T,
|
|
188
|
+
receiptHandle: proto.receiptHandle,
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ServiceAccount binding implementation.
|
|
3
|
+
*
|
|
4
|
+
* Provides identity and impersonation capabilities.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { type Channel, createClient } from "nice-grpc"
|
|
8
|
+
import {
|
|
9
|
+
type ServiceAccountServiceClient as GeneratedClient,
|
|
10
|
+
type ServiceAccountInfo as ServiceAccountInfoProto,
|
|
11
|
+
ServiceAccountServiceDefinition,
|
|
12
|
+
} from "../generated/service_account.js"
|
|
13
|
+
import { wrapGrpcCall } from "../grpc-utils.js"
|
|
14
|
+
import type { ImpersonationRequest, ServiceAccountInfo } from "../types.js"
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* ServiceAccount binding for identity and impersonation.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import { serviceAccount } from "@alienplatform/bindings"
|
|
22
|
+
*
|
|
23
|
+
* const sa = serviceAccount("deployment-account")
|
|
24
|
+
*
|
|
25
|
+
* // Get account info
|
|
26
|
+
* const info = await sa.getInfo()
|
|
27
|
+
* if (info.platform === "aws") {
|
|
28
|
+
* console.log("Role ARN:", info.roleArn)
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* // Impersonate the service account
|
|
32
|
+
* const config = await sa.impersonate({ durationSeconds: 3600 })
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export class ServiceAccount {
|
|
36
|
+
private readonly client: GeneratedClient
|
|
37
|
+
private readonly bindingName: string
|
|
38
|
+
|
|
39
|
+
constructor(channel: Channel, bindingName: string) {
|
|
40
|
+
this.client = createClient(ServiceAccountServiceDefinition, channel)
|
|
41
|
+
this.bindingName = bindingName
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Get information about the service account.
|
|
46
|
+
*
|
|
47
|
+
* @returns Platform-specific service account information
|
|
48
|
+
*/
|
|
49
|
+
async getInfo(): Promise<ServiceAccountInfo> {
|
|
50
|
+
return await wrapGrpcCall(
|
|
51
|
+
"ServiceAccountService",
|
|
52
|
+
"GetInfo",
|
|
53
|
+
async () => {
|
|
54
|
+
const response = await this.client.getInfo({
|
|
55
|
+
bindingName: this.bindingName,
|
|
56
|
+
})
|
|
57
|
+
return this.fromProtoInfo(response.info!)
|
|
58
|
+
},
|
|
59
|
+
{ bindingName: this.bindingName, bindingType: "ServiceAccount" },
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Impersonate the service account.
|
|
65
|
+
*
|
|
66
|
+
* @param request - Impersonation options
|
|
67
|
+
* @returns Client configuration for the impersonated identity
|
|
68
|
+
*/
|
|
69
|
+
async impersonate(request?: ImpersonationRequest): Promise<unknown> {
|
|
70
|
+
return await wrapGrpcCall(
|
|
71
|
+
"ServiceAccountService",
|
|
72
|
+
"Impersonate",
|
|
73
|
+
async () => {
|
|
74
|
+
const response = await this.client.impersonate({
|
|
75
|
+
bindingName: this.bindingName,
|
|
76
|
+
sessionName: request?.sessionName,
|
|
77
|
+
durationSeconds: request?.durationSeconds,
|
|
78
|
+
scopes: request?.scopes ?? [],
|
|
79
|
+
})
|
|
80
|
+
return JSON.parse(response.clientConfigJson)
|
|
81
|
+
},
|
|
82
|
+
{ bindingName: this.bindingName, bindingType: "ServiceAccount" },
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Private helpers
|
|
87
|
+
|
|
88
|
+
private fromProtoInfo(proto: ServiceAccountInfoProto): ServiceAccountInfo {
|
|
89
|
+
if (proto.aws) {
|
|
90
|
+
return {
|
|
91
|
+
platform: "aws",
|
|
92
|
+
roleName: proto.aws.roleName,
|
|
93
|
+
roleArn: proto.aws.roleArn,
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (proto.gcp) {
|
|
97
|
+
return {
|
|
98
|
+
platform: "gcp",
|
|
99
|
+
email: proto.gcp.email,
|
|
100
|
+
uniqueId: proto.gcp.uniqueId,
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (proto.azure) {
|
|
104
|
+
return {
|
|
105
|
+
platform: "azure",
|
|
106
|
+
clientId: proto.azure.clientId,
|
|
107
|
+
resourceId: proto.azure.resourceId,
|
|
108
|
+
principalId: proto.azure.principalId,
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
throw new Error("Unknown service account platform")
|
|
112
|
+
}
|
|
113
|
+
}
|