oblaka-iac 0.0.1
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.md +21 -0
- package/package.json +35 -0
- package/src/client.ts +43 -0
- package/src/commands/configure.ts +23 -0
- package/src/commands/deploy.ts +124 -0
- package/src/commands/input.ts +26 -0
- package/src/commands/resource-processor.ts +63 -0
- package/src/commands/run.ts +12 -0
- package/src/commands/utils/args.ts +20 -0
- package/src/commands/utils/fs.ts +12 -0
- package/src/commands/utils/pm.ts +47 -0
- package/src/index.ts +7 -0
- package/src/resources/analytics-engine.ts +45 -0
- package/src/resources/browser.ts +10 -0
- package/src/resources/container.ts +86 -0
- package/src/resources/d1.ts +71 -0
- package/src/resources/do.ts +54 -0
- package/src/resources/images.ts +10 -0
- package/src/resources/index.ts +35 -0
- package/src/resources/kv.ts +66 -0
- package/src/resources/queues.ts +134 -0
- package/src/resources/r2.ts +66 -0
- package/src/resources/service-reference.ts +27 -0
- package/src/resources/version-metadata.ts +12 -0
- package/src/resources/worker.ts +121 -0
- package/src/resources/workflow.ts +45 -0
- package/src/state.ts +66 -0
- package/src/types.ts +45 -0
- package/src/utils/migrations.ts +12 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { BindableResource, Config, ResourceDestroyer } from '../types'
|
|
2
|
+
import { getNextMigrationTag } from '../utils/migrations'
|
|
3
|
+
|
|
4
|
+
export interface DurableObjectState {
|
|
5
|
+
name: string
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class DurableObject implements BindableResource<DurableObjectState> {
|
|
9
|
+
constructor(
|
|
10
|
+
public readonly options: {
|
|
11
|
+
name: string
|
|
12
|
+
className: string
|
|
13
|
+
},
|
|
14
|
+
) {}
|
|
15
|
+
|
|
16
|
+
getId() {
|
|
17
|
+
return { resource: 'durable_object' as const, id: this.options.name }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async apply({ state }: { state?: DurableObjectState }): Promise<DurableObjectState> {
|
|
21
|
+
return state ?? {
|
|
22
|
+
name: this.options.name,
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
configureBinding(args: { config: Config; state?: DurableObjectState; binding: string; env: string }): Config {
|
|
27
|
+
const hasMigration = args.config?.migrations?.find(it => it.new_sqlite_classes?.includes(this.options.className))
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
...args.config,
|
|
31
|
+
migrations: [
|
|
32
|
+
...(args.config.migrations ?? []),
|
|
33
|
+
...(hasMigration ? [] : [{
|
|
34
|
+
tag: getNextMigrationTag(args.config),
|
|
35
|
+
new_sqlite_classes: [this.options.className],
|
|
36
|
+
}]),
|
|
37
|
+
],
|
|
38
|
+
durable_objects: {
|
|
39
|
+
...(args.config?.durable_objects ?? {}),
|
|
40
|
+
bindings: [
|
|
41
|
+
...(args.config?.durable_objects?.bindings ?? []),
|
|
42
|
+
{
|
|
43
|
+
name: args.binding,
|
|
44
|
+
class_name: this.options.className,
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
},
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const DurableObjectDestroyer: ResourceDestroyer<DurableObjectState> = async (args): Promise<void> => {
|
|
53
|
+
// nothing to do
|
|
54
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Destroyers, ResourceDestroyer, ResourceKind } from '../types'
|
|
2
|
+
import { ContainerDestroyer } from './container'
|
|
3
|
+
import { D1Destroyer as D1DatabaseDestroyer } from './d1'
|
|
4
|
+
import { DurableObjectDestroyer } from './do'
|
|
5
|
+
import { KVNamespaceDestroyer } from './kv'
|
|
6
|
+
import { QueueDestroyer } from './queues'
|
|
7
|
+
import { R2BucketDestroyer } from './r2'
|
|
8
|
+
import { WorkerDestroyer } from './worker'
|
|
9
|
+
import { WorkflowDestroyer } from './workflow'
|
|
10
|
+
|
|
11
|
+
export * from './analytics-engine'
|
|
12
|
+
export * from './browser'
|
|
13
|
+
export * from './container'
|
|
14
|
+
export * from './d1'
|
|
15
|
+
export * from './do'
|
|
16
|
+
export * from './images'
|
|
17
|
+
export * from './kv'
|
|
18
|
+
export * from './queues'
|
|
19
|
+
export * from './r2'
|
|
20
|
+
export * from './service-reference'
|
|
21
|
+
export * from './version-metadata'
|
|
22
|
+
export * from './worker'
|
|
23
|
+
export * from './workflow'
|
|
24
|
+
|
|
25
|
+
export const destroyers: Destroyers = {
|
|
26
|
+
analytics_engine: async () => {}, // Analytics Engine datasets are managed automatically by Cloudflare
|
|
27
|
+
container: ContainerDestroyer,
|
|
28
|
+
d1_database: D1DatabaseDestroyer,
|
|
29
|
+
durable_object: DurableObjectDestroyer,
|
|
30
|
+
kv_namespace: KVNamespaceDestroyer,
|
|
31
|
+
queue: QueueDestroyer,
|
|
32
|
+
r2_bucket: R2BucketDestroyer,
|
|
33
|
+
workflow: WorkflowDestroyer,
|
|
34
|
+
worker: WorkerDestroyer,
|
|
35
|
+
} satisfies Record<ResourceKind, ResourceDestroyer<any>>
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { BindableResource, Config, Context, ResourceDestroyer } from '../types'
|
|
2
|
+
|
|
3
|
+
export interface KVNamespaceState {
|
|
4
|
+
name: string
|
|
5
|
+
id: string
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class KVNamespace implements BindableResource<KVNamespaceState> {
|
|
9
|
+
constructor(
|
|
10
|
+
public readonly options: {
|
|
11
|
+
name: string
|
|
12
|
+
},
|
|
13
|
+
) {}
|
|
14
|
+
|
|
15
|
+
getId() {
|
|
16
|
+
return {
|
|
17
|
+
resource: 'kv_namespace' as const,
|
|
18
|
+
id: this.options.name,
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
configureBinding(args: { config: Config; binding: string; state?: KVNamespaceState; env: string }): Config {
|
|
23
|
+
return {
|
|
24
|
+
...args.config,
|
|
25
|
+
kv_namespaces: [
|
|
26
|
+
...args.config?.kv_namespaces ?? [],
|
|
27
|
+
{
|
|
28
|
+
binding: args.binding,
|
|
29
|
+
id: args.state?.id || this.options.name,
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async apply(args: { state?: KVNamespaceState; context: Context; dryRun: boolean }): Promise<KVNamespaceState> {
|
|
36
|
+
if (args.state) {
|
|
37
|
+
return args.state
|
|
38
|
+
}
|
|
39
|
+
const remoteName = `${args.context.env}-${this.options.name}`
|
|
40
|
+
if (args.dryRun) {
|
|
41
|
+
return {
|
|
42
|
+
name: remoteName,
|
|
43
|
+
id: 'dry-run-id-' + Math.random().toString(36).substring(2, 9),
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const result = await args.context.client.fetch<{ id: string }>({
|
|
48
|
+
url: `/storage/kv/namespaces`,
|
|
49
|
+
method: 'POST',
|
|
50
|
+
body: {
|
|
51
|
+
title: remoteName,
|
|
52
|
+
},
|
|
53
|
+
})
|
|
54
|
+
return {
|
|
55
|
+
name: remoteName,
|
|
56
|
+
id: result.id,
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const KVNamespaceDestroyer: ResourceDestroyer<KVNamespaceState> = async (args): Promise<void> => {
|
|
62
|
+
await args.context.client.fetch({
|
|
63
|
+
url: `/storage/kv/namespaces/${args.state.id}`,
|
|
64
|
+
method: 'DELETE',
|
|
65
|
+
})
|
|
66
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import type { BindableResource, Config, Context, ResourceDestroyer } from '../types'
|
|
2
|
+
|
|
3
|
+
export interface QueueState {
|
|
4
|
+
id: string
|
|
5
|
+
name: string
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
type QueueBindingMode = 'producer' | 'consumer' | 'both'
|
|
9
|
+
|
|
10
|
+
interface QueueProducerBindingOptions {
|
|
11
|
+
deliveryDelay?: number
|
|
12
|
+
remote?: boolean
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface QueueConsumerBindingOptions {
|
|
16
|
+
type?: string
|
|
17
|
+
maxBatchSize?: number
|
|
18
|
+
maxBatchTimeout?: number
|
|
19
|
+
maxRetries?: number
|
|
20
|
+
deadLetterQueue?: string
|
|
21
|
+
maxConcurrency?: number | null
|
|
22
|
+
visibilityTimeoutMs?: number
|
|
23
|
+
retryDelay?: number
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class Queue implements BindableResource<QueueState> {
|
|
27
|
+
constructor(
|
|
28
|
+
public readonly options: {
|
|
29
|
+
name: string
|
|
30
|
+
binding?: QueueBindingMode
|
|
31
|
+
producer?: QueueProducerBindingOptions | false
|
|
32
|
+
consumer?: QueueConsumerBindingOptions
|
|
33
|
+
},
|
|
34
|
+
) {}
|
|
35
|
+
|
|
36
|
+
public getId() {
|
|
37
|
+
return {
|
|
38
|
+
resource: 'queue' as const,
|
|
39
|
+
id: this.options.name,
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public async apply(args: { state?: QueueState; context: Context; dryRun: boolean }): Promise<QueueState> {
|
|
44
|
+
if (args.state) {
|
|
45
|
+
return args.state
|
|
46
|
+
}
|
|
47
|
+
const remoteName = `${args.context.env}-${this.options.name}`
|
|
48
|
+
|
|
49
|
+
if (args.dryRun) {
|
|
50
|
+
return {
|
|
51
|
+
id: 'dry-run-id-' + Math.random().toString(36).substring(2, 11),
|
|
52
|
+
name: remoteName,
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const result = await args.context.client.fetch<{ queue_id: string }>({
|
|
57
|
+
url: `/queues`,
|
|
58
|
+
method: 'POST',
|
|
59
|
+
body: {
|
|
60
|
+
queue_name: remoteName,
|
|
61
|
+
},
|
|
62
|
+
})
|
|
63
|
+
return {
|
|
64
|
+
id: result.queue_id,
|
|
65
|
+
name: remoteName,
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public configureBinding(args: { config: Config; binding: string; state?: QueueState; env: string }): Config {
|
|
70
|
+
const queueName = args.state?.name ?? this.options.name
|
|
71
|
+
const producer = this.getProducerConfig(args.binding, queueName)
|
|
72
|
+
const consumer = this.getConsumerConfig(queueName)
|
|
73
|
+
return {
|
|
74
|
+
...args.config,
|
|
75
|
+
queues: {
|
|
76
|
+
...args.config.queues,
|
|
77
|
+
producers: [
|
|
78
|
+
...args.config.queues?.producers || [],
|
|
79
|
+
...(producer ? [producer] : []),
|
|
80
|
+
],
|
|
81
|
+
consumers: [
|
|
82
|
+
...args.config.queues?.consumers || [],
|
|
83
|
+
...(consumer ? [consumer] : []),
|
|
84
|
+
],
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private getProducerConfig(binding: string, queueName: string): Exclude<Exclude<Config['queues'], undefined>['producers'], undefined>[0] | null {
|
|
90
|
+
const includeProducer = this.options.binding
|
|
91
|
+
? this.options.binding === 'producer' || this.options.binding === 'both'
|
|
92
|
+
: this.options.producer !== false
|
|
93
|
+
if (!includeProducer) {
|
|
94
|
+
return null
|
|
95
|
+
}
|
|
96
|
+
const producerOptions = typeof this.options.producer === 'object' && this.options.producer !== null
|
|
97
|
+
? this.options.producer
|
|
98
|
+
: {}
|
|
99
|
+
return {
|
|
100
|
+
binding,
|
|
101
|
+
queue: queueName,
|
|
102
|
+
...(producerOptions.deliveryDelay !== undefined ? { delivery_delay: producerOptions.deliveryDelay } : {}),
|
|
103
|
+
...(producerOptions.remote !== undefined ? { remote: producerOptions.remote } : {}),
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private getConsumerConfig(queueName: string): Exclude<Exclude<Config['queues'], undefined>['consumers'], undefined>[0] | null {
|
|
108
|
+
const includeConsumer = this.options.binding
|
|
109
|
+
? this.options.binding === 'consumer' || this.options.binding === 'both'
|
|
110
|
+
: this.options.consumer !== undefined
|
|
111
|
+
if (!includeConsumer) {
|
|
112
|
+
return null
|
|
113
|
+
}
|
|
114
|
+
const consumerOptions = this.options.consumer ?? {}
|
|
115
|
+
return {
|
|
116
|
+
queue: queueName,
|
|
117
|
+
...(consumerOptions.type ? { type: consumerOptions.type } : {}),
|
|
118
|
+
...(consumerOptions.maxBatchSize !== undefined ? { max_batch_size: consumerOptions.maxBatchSize } : {}),
|
|
119
|
+
...(consumerOptions.maxBatchTimeout !== undefined ? { max_batch_timeout: consumerOptions.maxBatchTimeout } : {}),
|
|
120
|
+
...(consumerOptions.maxRetries !== undefined ? { max_retries: consumerOptions.maxRetries } : {}),
|
|
121
|
+
...(consumerOptions.deadLetterQueue !== undefined ? { dead_letter_queue: consumerOptions.deadLetterQueue } : {}),
|
|
122
|
+
...(consumerOptions.maxConcurrency !== undefined ? { max_concurrency: consumerOptions.maxConcurrency } : {}),
|
|
123
|
+
...(consumerOptions.visibilityTimeoutMs !== undefined ? { visibility_timeout_ms: consumerOptions.visibilityTimeoutMs } : {}),
|
|
124
|
+
...(consumerOptions.retryDelay !== undefined ? { retry_delay: consumerOptions.retryDelay } : {}),
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export const QueueDestroyer: ResourceDestroyer<QueueState> = async (args): Promise<void> => {
|
|
130
|
+
await args.context.client.fetch({
|
|
131
|
+
url: `/queues/${args.state.id}`,
|
|
132
|
+
method: 'DELETE',
|
|
133
|
+
})
|
|
134
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { BindableResource, Config, Context, Region, ResourceDestroyer } from '../types'
|
|
2
|
+
|
|
3
|
+
export interface R2BucketState {
|
|
4
|
+
name: string
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export class R2Bucket implements BindableResource<R2BucketState> {
|
|
8
|
+
constructor(
|
|
9
|
+
public readonly options: {
|
|
10
|
+
name: string
|
|
11
|
+
locationHint?: Region
|
|
12
|
+
},
|
|
13
|
+
) {}
|
|
14
|
+
|
|
15
|
+
configureBinding(args: { config: Config; binding: string; state?: R2BucketState; env: string }): Config {
|
|
16
|
+
return {
|
|
17
|
+
...args.config,
|
|
18
|
+
r2_buckets: [
|
|
19
|
+
...args.config?.r2_buckets ?? [],
|
|
20
|
+
{
|
|
21
|
+
binding: args.binding,
|
|
22
|
+
bucket_name: args.state?.name || this.options.name,
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
getId() {
|
|
29
|
+
return {
|
|
30
|
+
resource: 'r2_bucket' as const,
|
|
31
|
+
id: this.options.name,
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async apply(args: { state?: R2BucketState; context: Context; dryRun: boolean }): Promise<R2BucketState> {
|
|
36
|
+
if (args.state) {
|
|
37
|
+
return args.state
|
|
38
|
+
}
|
|
39
|
+
const remoteName = `${args.context.env}-${this.options.name}`
|
|
40
|
+
|
|
41
|
+
if (args.dryRun) {
|
|
42
|
+
return {
|
|
43
|
+
name: remoteName,
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
await args.context.client.fetch({
|
|
48
|
+
url: `/r2/buckets`,
|
|
49
|
+
method: 'POST',
|
|
50
|
+
body: {
|
|
51
|
+
name: remoteName,
|
|
52
|
+
locationHint: this.options.locationHint,
|
|
53
|
+
},
|
|
54
|
+
})
|
|
55
|
+
return {
|
|
56
|
+
name: remoteName,
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const R2BucketDestroyer: ResourceDestroyer<R2BucketState> = async (args): Promise<void> => {
|
|
62
|
+
await args.context.client.fetch({
|
|
63
|
+
url: `/r2/buckets/${args.state.name}`,
|
|
64
|
+
method: 'DELETE',
|
|
65
|
+
})
|
|
66
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Bindable, Config } from '../types'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Reference to another worker service by name.
|
|
5
|
+
* Used when a child worker needs to call back to its parent or another worker.
|
|
6
|
+
*/
|
|
7
|
+
export class ServiceReference implements Bindable {
|
|
8
|
+
constructor(
|
|
9
|
+
private readonly serviceName: string,
|
|
10
|
+
private readonly entrypoint?: string,
|
|
11
|
+
) {}
|
|
12
|
+
|
|
13
|
+
configureBinding(args: { config: Config; binding: string; env: string }): Config {
|
|
14
|
+
const remoteName = args.env === 'local' ? this.serviceName : `${args.env}-${this.serviceName}`
|
|
15
|
+
return {
|
|
16
|
+
...args.config,
|
|
17
|
+
services: [
|
|
18
|
+
...(args.config.services ?? []),
|
|
19
|
+
{
|
|
20
|
+
binding: args.binding,
|
|
21
|
+
service: remoteName,
|
|
22
|
+
entrypoint: this.entrypoint,
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Bindable, Config } from '../types'
|
|
2
|
+
|
|
3
|
+
export class VersionMetadata implements Bindable {
|
|
4
|
+
configureBinding(args: { config: Config; binding: string; env: string }): Config {
|
|
5
|
+
return {
|
|
6
|
+
...args.config,
|
|
7
|
+
version_metadata: {
|
|
8
|
+
binding: args.binding,
|
|
9
|
+
},
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { AnyBindable, BindableResource, CompatibilityFlags, Config, Context, ResourceDestroyer } from '../types'
|
|
2
|
+
|
|
3
|
+
export interface WorkerState {
|
|
4
|
+
id: string
|
|
5
|
+
name: string
|
|
6
|
+
}
|
|
7
|
+
export class Worker implements BindableResource<WorkerState> {
|
|
8
|
+
constructor(
|
|
9
|
+
public readonly options:
|
|
10
|
+
& {
|
|
11
|
+
dir: string
|
|
12
|
+
name: string
|
|
13
|
+
compatibility_flags: CompatibilityFlags[]
|
|
14
|
+
bindings: Record<string, AnyBindable>
|
|
15
|
+
}
|
|
16
|
+
& Partial<
|
|
17
|
+
Pick<
|
|
18
|
+
Config,
|
|
19
|
+
| 'main'
|
|
20
|
+
| 'compatibility_date'
|
|
21
|
+
| 'vars'
|
|
22
|
+
| 'find_additional_modules'
|
|
23
|
+
| 'base_dir'
|
|
24
|
+
| 'preview_urls'
|
|
25
|
+
| 'routes'
|
|
26
|
+
| 'route'
|
|
27
|
+
| 'observability'
|
|
28
|
+
| 'logpush'
|
|
29
|
+
| 'rules'
|
|
30
|
+
| 'limits'
|
|
31
|
+
| 'no_bundle'
|
|
32
|
+
| 'keep_names'
|
|
33
|
+
| 'first_party_worker'
|
|
34
|
+
| 'minify'
|
|
35
|
+
| 'assets'
|
|
36
|
+
| 'compliance_region'
|
|
37
|
+
| 'build'
|
|
38
|
+
| 'define'
|
|
39
|
+
| 'jsx_factory'
|
|
40
|
+
| 'jsx_fragment'
|
|
41
|
+
| 'triggers'
|
|
42
|
+
| 'upload_source_maps'
|
|
43
|
+
>
|
|
44
|
+
>,
|
|
45
|
+
) {}
|
|
46
|
+
|
|
47
|
+
getId() {
|
|
48
|
+
return {
|
|
49
|
+
resource: 'worker' as const,
|
|
50
|
+
id: this.options.name,
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
configureSelf(args: { config: Config; state?: WorkerState | undefined }): Config {
|
|
55
|
+
return {
|
|
56
|
+
...args.config,
|
|
57
|
+
name: args.state?.name ?? this.options.name,
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
configureBinding(args: { config: Config; binding: string; state?: WorkerState | undefined; env: string }): Config {
|
|
62
|
+
return {
|
|
63
|
+
...args.config,
|
|
64
|
+
services: [
|
|
65
|
+
...(args.config.services ?? []),
|
|
66
|
+
{
|
|
67
|
+
binding: args.binding,
|
|
68
|
+
service: args.state?.name ?? (args.env === 'local' ? this.options.name : `${args.env}-${this.options.name}`),
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async apply(args: { state?: WorkerState | undefined; context: Context; dryRun: boolean }): Promise<WorkerState> {
|
|
75
|
+
if (args.state) {
|
|
76
|
+
return args.state
|
|
77
|
+
}
|
|
78
|
+
const remoteName = `${args.context.env}-${this.options.name}`
|
|
79
|
+
|
|
80
|
+
if (args.dryRun) {
|
|
81
|
+
return {
|
|
82
|
+
id: 'dry-run-id-' + Math.random().toString(36).substring(2, 9),
|
|
83
|
+
name: remoteName,
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const result = await args.context.client.fetch<{ id: string }>({
|
|
88
|
+
method: 'POST',
|
|
89
|
+
url: `/workers/workers`,
|
|
90
|
+
body: {
|
|
91
|
+
name: remoteName,
|
|
92
|
+
},
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
await args.context.client.fetch({
|
|
96
|
+
method: 'POST',
|
|
97
|
+
url: `/workers/workers/${result.id}/versions`,
|
|
98
|
+
body: {
|
|
99
|
+
main_module: 'index.js',
|
|
100
|
+
modules: [
|
|
101
|
+
{
|
|
102
|
+
name: 'index.js',
|
|
103
|
+
content_type: 'application/javascript+module',
|
|
104
|
+
content_base64: btoa(`export default {fetch: () => new Response("Hello")}`),
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
},
|
|
108
|
+
})
|
|
109
|
+
return {
|
|
110
|
+
id: result.id,
|
|
111
|
+
name: remoteName,
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export const WorkerDestroyer: ResourceDestroyer<WorkerState> = async ({ state, context }) => {
|
|
117
|
+
await context.client.fetch({
|
|
118
|
+
method: 'DELETE',
|
|
119
|
+
url: `/workers/workers/${state.id}`,
|
|
120
|
+
})
|
|
121
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { BindableResource, Config, Context, ResourceDestroyer } from '../types'
|
|
2
|
+
|
|
3
|
+
export interface WorkflowState {
|
|
4
|
+
name: string
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export class Workflow implements BindableResource<WorkflowState> {
|
|
8
|
+
constructor(
|
|
9
|
+
public readonly options: {
|
|
10
|
+
name: string
|
|
11
|
+
className: string
|
|
12
|
+
},
|
|
13
|
+
) {}
|
|
14
|
+
|
|
15
|
+
getId() {
|
|
16
|
+
return {
|
|
17
|
+
resource: 'workflow' as const,
|
|
18
|
+
id: this.options.name,
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
configureBinding(args: { config: Config; binding: string; state?: WorkflowState; env: string }): Config {
|
|
23
|
+
return {
|
|
24
|
+
...args.config,
|
|
25
|
+
workflows: [
|
|
26
|
+
...args.config?.workflows ?? [],
|
|
27
|
+
{
|
|
28
|
+
binding: args.binding,
|
|
29
|
+
class_name: this.options.className,
|
|
30
|
+
name: args.state?.name ?? this.options.name,
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async apply({ state, context }: { state?: WorkflowState; context: Context }): Promise<WorkflowState> {
|
|
37
|
+
return state ?? {
|
|
38
|
+
name: `${context.env}-${this.options.name}`,
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const WorkflowDestroyer: ResourceDestroyer<WorkflowState> = async (args) => {
|
|
44
|
+
// nothing to do
|
|
45
|
+
}
|
package/src/state.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { CloudflareClient } from './client'
|
|
2
|
+
|
|
3
|
+
export interface State {
|
|
4
|
+
version: number
|
|
5
|
+
resources: Record<string, any>
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface StateKey {
|
|
9
|
+
env: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface StateStorage {
|
|
13
|
+
get(key: StateKey): Promise<State | null>
|
|
14
|
+
set(key: StateKey, state: State): Promise<void>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class KVStateStorage implements StateStorage {
|
|
18
|
+
private id: string | undefined = undefined
|
|
19
|
+
|
|
20
|
+
constructor(
|
|
21
|
+
private readonly cfClient: CloudflareClient,
|
|
22
|
+
private readonly namespace: string,
|
|
23
|
+
) {
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
private async resolveKvId(): Promise<string> {
|
|
27
|
+
if (this.id) {
|
|
28
|
+
return this.id
|
|
29
|
+
}
|
|
30
|
+
const id = await this.cfClient.fetch<{ title: string; id: string }[]>({
|
|
31
|
+
url: `/storage/kv/namespaces`,
|
|
32
|
+
method: 'GET',
|
|
33
|
+
})
|
|
34
|
+
const existing = id.find(i => i.title === this.namespace)
|
|
35
|
+
if (existing) {
|
|
36
|
+
this.id = existing.id
|
|
37
|
+
return existing.id
|
|
38
|
+
}
|
|
39
|
+
const createResult = await this.cfClient.fetch<{ id: string }>({
|
|
40
|
+
url: `/storage/kv/namespaces`,
|
|
41
|
+
method: 'POST',
|
|
42
|
+
body: { title: this.namespace },
|
|
43
|
+
})
|
|
44
|
+
this.id = createResult.id
|
|
45
|
+
return createResult.id
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async get(key: StateKey): Promise<State> {
|
|
49
|
+
const value = await this.cfClient.fetch<{ values: Record<string, string> }>({
|
|
50
|
+
url: `/storage/kv/namespaces/${await this.resolveKvId()}/bulk/get`,
|
|
51
|
+
method: 'POST',
|
|
52
|
+
body: {
|
|
53
|
+
keys: [key.env],
|
|
54
|
+
},
|
|
55
|
+
})
|
|
56
|
+
return value.values?.[key.env] ? JSON.parse(value.values[key.env]) : { version: 1, resources: {} }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async set(key: StateKey, state: State): Promise<void> {
|
|
60
|
+
await this.cfClient.fetch({
|
|
61
|
+
url: `/storage/kv/namespaces/${await this.resolveKvId()}/values/${key.env}`,
|
|
62
|
+
method: 'PUT',
|
|
63
|
+
body: state,
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { RawEnvironment } from '@cloudflare/workers-utils'
|
|
2
|
+
import type { CloudflareClient } from './client'
|
|
3
|
+
import type { Worker } from './resources'
|
|
4
|
+
|
|
5
|
+
export type Region = 'wnam' | 'enam' | 'weur' | 'eeur' | 'apac' | 'oc'
|
|
6
|
+
|
|
7
|
+
export type CompatibilityFlags = 'nodejs_compat' | (string & {})
|
|
8
|
+
export type AnyBindable = BindableResource<any> | Bindable
|
|
9
|
+
|
|
10
|
+
export interface Context {
|
|
11
|
+
env: string
|
|
12
|
+
client: CloudflareClient
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type Config = RawEnvironment
|
|
16
|
+
|
|
17
|
+
export type ResourceKind =
|
|
18
|
+
| 'worker'
|
|
19
|
+
| 'durable_object'
|
|
20
|
+
| 'd1_database'
|
|
21
|
+
| 'kv_namespace'
|
|
22
|
+
| 'r2_bucket'
|
|
23
|
+
| 'queue'
|
|
24
|
+
| 'container'
|
|
25
|
+
| 'workflow'
|
|
26
|
+
| 'analytics_engine'
|
|
27
|
+
|
|
28
|
+
export interface Resource<TState> {
|
|
29
|
+
getId(): { resource: ResourceKind; id: string }
|
|
30
|
+
apply(args: { state?: TState; context: Context; dryRun: boolean }): Promise<TState>
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type ResourceDestroyer<TState> = (args: { state: TState; context: Context }) => Promise<void>
|
|
34
|
+
|
|
35
|
+
export type Destroyers = Record<string, ResourceDestroyer<any>>
|
|
36
|
+
|
|
37
|
+
export interface BindableResource<TState> extends Resource<TState> {
|
|
38
|
+
configureBinding(args: { config: Config; binding: string; state?: TState; env: string }): Config
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface Bindable {
|
|
42
|
+
configureBinding(args: { config: Config; binding: string; env: string }): Config
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type DefineFn = (config: { env: string }) => Worker | undefined
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const getNextMigrationTag = (config: { migrations?: { tag: string }[] }) => {
|
|
2
|
+
const latestTag = config.migrations?.map(it => it.tag).sort().pop() ?? 'v0000'
|
|
3
|
+
|
|
4
|
+
const match = latestTag.match(/^v(\d{4})$/)
|
|
5
|
+
if (!match) {
|
|
6
|
+
throw new Error(`Invalid migration tag format: ${latestTag}`)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const currentVersion = parseInt(match[1], 10)
|
|
10
|
+
const nextVersion = currentVersion + 1
|
|
11
|
+
return `v${String(nextVersion).padStart(4, '0')}`
|
|
12
|
+
}
|