oblaka-iac 0.0.6 → 0.0.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oblaka-iac",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "main": "./src/index.ts",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -20,7 +20,8 @@
20
20
  "format:check": "dprint check",
21
21
  "lint": "biome check ./src",
22
22
  "lint:fix": "biome check --write ./src",
23
- "typecheck": "tsc --noEmit"
23
+ "typecheck": "tsc --noEmit",
24
+ "test": "bun test"
24
25
  },
25
26
  "devDependencies": {
26
27
  "@biomejs/biome": "^2.4.6",
@@ -20,7 +20,7 @@ export class CloudflareDeployExecutor implements ResourceApplier {
20
20
  apiToken: input.apiToken,
21
21
  })
22
22
 
23
- const stateStore = new KVStateStorage(cfClient, input.stateNamespace)
23
+ const stateStore = new KVStateStorage(cfClient, input.stateNamespace, input.stateNamespaceId)
24
24
  const stateKey = { env: input.env }
25
25
  const state = await stateStore.get(stateKey)
26
26
 
@@ -1,26 +1,41 @@
1
- import { parseArgs } from './utils/args'
1
+ import { parseArgs } from 'node:util'
2
2
 
3
- const requireString = (value: string | boolean | undefined, name: string): string => {
4
- if (typeof value === 'string') {
5
- return value
3
+ export const input = (() => {
4
+ const { values, positionals } = parseArgs({
5
+ args: process.argv.slice(2),
6
+ allowPositionals: true,
7
+ options: {
8
+ env: { type: 'string' },
9
+ 'state-namespace': { type: 'string' },
10
+ 'state-namespace-id': { type: 'string' },
11
+ 'account-id': { type: 'string' },
12
+ 'api-token': { type: 'string' },
13
+ 'dry-run': { type: 'boolean' },
14
+ remote: { type: 'boolean' },
15
+ destroy: { type: 'boolean' },
16
+ validate: { type: 'boolean' },
17
+ 'out-state': { type: 'string' },
18
+ },
19
+ })
20
+
21
+ const main = positionals[0]
22
+ if (!main) {
23
+ console.error('Missing config entrypoint argument')
24
+ process.exit(1)
6
25
  }
7
- throw new Error(`Missing required string argument: ${name}`)
8
- }
9
26
 
10
- export const input = (() => {
11
- const { named, positional } = parseArgs(process.argv.slice(2))
12
27
  return {
13
- main: requireString(positional[0], 'config entrypoint'),
14
- env: requireString(named.env || process.env.CLOUDFLARE_ENV || 'local', 'environment'),
15
- stateNamespace: requireString(named['state-namespace'] || 'cf-state', 'state namespace'),
16
- configPath: requireString(named.config || 'wrangler.jsonc', 'config path'),
17
- accountId: requireString(named['account-id'] || process.env.CLOUDFLARE_ACCOUNT_ID || '', 'account id'),
18
- apiToken: requireString(named['api-token'] || process.env.CLOUDFLARE_API_TOKEN || '', 'api token'),
19
- dryRun: !!named['dry-run'],
20
- remote: !!named.remote,
21
- destroy: !!named.destroy,
22
- validate: !!named.validate,
23
- outStatePath: named['out-state'] === true ? 'out-state.json' : named['out-state'],
28
+ main,
29
+ env: values.env || process.env.CLOUDFLARE_ENV || 'local',
30
+ stateNamespace: values['state-namespace'] || 'cf-state',
31
+ stateNamespaceId: values['state-namespace-id'],
32
+ accountId: values['account-id'] || process.env.CLOUDFLARE_ACCOUNT_ID || '',
33
+ apiToken: values['api-token'] || process.env.CLOUDFLARE_API_TOKEN || '',
34
+ dryRun: values['dry-run'] ?? false,
35
+ remote: values.remote ?? false,
36
+ destroy: values.destroy ?? false,
37
+ validate: values.validate ?? false,
38
+ outStatePath: values['out-state'],
24
39
  }
25
40
  })()
26
41
 
@@ -38,7 +38,7 @@ export class ResourceProcessor {
38
38
  }
39
39
  const configPath = path.join(worker.options.dir, 'wrangler.jsonc')
40
40
  const existingConfig = jsoncParser.parse((await tryReadFile(configPath)) || '{}') as Config
41
- const { bindings, dir, ...workerConfig } = worker?.options ?? {}
41
+ const { bindings, dir, deleteDurableObjectsOnRemoval, ...workerConfig } = worker.options
42
42
 
43
43
  let config: Config = {
44
44
  ...workerConfig,
@@ -56,7 +56,7 @@ export class ResourceProcessor {
56
56
  config = binding.configureBinding({ config, binding: bindingName, state, env })
57
57
  }
58
58
 
59
- if (worker.options.deleteDurableObjectsOnRemoval !== false) {
59
+ if (deleteDurableObjectsOnRemoval !== false) {
60
60
  const oldClasses = new Set((existingConfig.durable_objects?.bindings ?? []).map((b: { class_name: string }) => b.class_name))
61
61
  const newClasses = new Set((config.durable_objects?.bindings ?? []).map((b: { class_name: string }) => b.class_name))
62
62
  const deletedClasses = [...oldClasses].filter(c => !newClasses.has(c))
@@ -10,6 +10,14 @@ registerSignalHandler()
10
10
  if (input.validate) {
11
11
  await CloudflareValidateExecutor.execute({ input })
12
12
  } else if (input.remote) {
13
+ if (!input.accountId) {
14
+ console.error('Missing --account-id or CLOUDFLARE_ACCOUNT_ID environment variable')
15
+ process.exit(1)
16
+ }
17
+ if (!input.apiToken) {
18
+ console.error('Missing --api-token or CLOUDFLARE_API_TOKEN environment variable')
19
+ process.exit(1)
20
+ }
13
21
  await CloudflareDeployExecutor.execute({ input })
14
22
  } else {
15
23
  await CloudflareConfigureExecutor.execute({ input })
@@ -5,6 +5,7 @@ import { DurableObjectDestroyer } from './do'
5
5
  import { KVNamespaceDestroyer } from './kv'
6
6
  import { QueueDestroyer } from './queues'
7
7
  import { R2BucketDestroyer } from './r2'
8
+ import { VectorizeIndexDestroyer } from './vectorize'
8
9
  import { WorkerDestroyer } from './worker'
9
10
  import { WorkflowDestroyer } from './workflow'
10
11
 
@@ -18,6 +19,7 @@ export * from './kv'
18
19
  export * from './queues'
19
20
  export * from './r2'
20
21
  export * from './service-reference'
22
+ export * from './vectorize'
21
23
  export * from './version-metadata'
22
24
  export * from './worker'
23
25
  export * from './workflow'
@@ -31,5 +33,6 @@ export const destroyers: Destroyers = {
31
33
  queue: QueueDestroyer,
32
34
  r2_bucket: R2BucketDestroyer,
33
35
  workflow: WorkflowDestroyer,
36
+ vectorize_index: VectorizeIndexDestroyer,
34
37
  worker: WorkerDestroyer,
35
38
  } satisfies Record<ResourceKind, ResourceDestroyer<any>>
@@ -0,0 +1,71 @@
1
+ import type { BindableResource, Config, Context, ResourceDestroyer } from '../types'
2
+
3
+ export type VectorizeMetric = 'cosine' | 'euclidean' | 'dot-product'
4
+
5
+ export interface VectorizeIndexState {
6
+ name: string
7
+ }
8
+
9
+ export class VectorizeIndex implements BindableResource<VectorizeIndexState> {
10
+ constructor(
11
+ public readonly options: {
12
+ name: string
13
+ config: {
14
+ dimensions: number
15
+ metric: VectorizeMetric
16
+ }
17
+ },
18
+ ) {}
19
+
20
+ getId() {
21
+ return {
22
+ resource: 'vectorize_index' as const,
23
+ id: this.options.name,
24
+ }
25
+ }
26
+
27
+ configureBinding(args: { config: Config; binding: string; state?: VectorizeIndexState; env: string }): Config {
28
+ return {
29
+ ...args.config,
30
+ vectorize: [
31
+ ...args.config?.vectorize ?? [],
32
+ {
33
+ binding: args.binding,
34
+ index_name: args.state?.name || this.options.name,
35
+ },
36
+ ],
37
+ }
38
+ }
39
+
40
+ async apply(args: { state?: VectorizeIndexState; context: Context; dryRun: boolean }): Promise<VectorizeIndexState> {
41
+ if (args.state) {
42
+ return args.state
43
+ }
44
+ const remoteName = `${args.context.env}-${this.options.name}`
45
+
46
+ if (args.dryRun) {
47
+ return {
48
+ name: remoteName,
49
+ }
50
+ }
51
+
52
+ await args.context.client.fetch({
53
+ url: `/vectorize/v2/indexes`,
54
+ method: 'POST',
55
+ body: {
56
+ name: remoteName,
57
+ config: this.options.config,
58
+ },
59
+ })
60
+ return {
61
+ name: remoteName,
62
+ }
63
+ }
64
+ }
65
+
66
+ export const VectorizeIndexDestroyer: ResourceDestroyer<VectorizeIndexState> = async (args): Promise<void> => {
67
+ await args.context.client.fetch({
68
+ url: `/vectorize/v2/indexes/${args.state.name}`,
69
+ method: 'DELETE',
70
+ })
71
+ }
package/src/state.ts CHANGED
@@ -20,18 +20,20 @@ export class KVStateStorage implements StateStorage {
20
20
  constructor(
21
21
  private readonly cfClient: CloudflareClient,
22
22
  private readonly namespace: string,
23
+ namespaceId?: string,
23
24
  ) {
25
+ this.id = namespaceId
24
26
  }
25
27
 
26
28
  private async resolveKvId(): Promise<string> {
27
29
  if (this.id) {
28
30
  return this.id
29
31
  }
30
- const id = await this.cfClient.fetch<{ title: string; id: string }[]>({
31
- url: `/storage/kv/namespaces`,
32
+ const namespaces = await this.cfClient.fetch<{ title: string; id: string }[]>({
33
+ url: `/storage/kv/namespaces?per_page=1000`,
32
34
  method: 'GET',
33
35
  })
34
- const existing = id.find(i => i.title === this.namespace)
36
+ const existing = namespaces.find(i => i.title === this.namespace)
35
37
  if (existing) {
36
38
  this.id = existing.id
37
39
  return existing.id
package/src/types.ts CHANGED
@@ -24,6 +24,7 @@ export type ResourceKind =
24
24
  | 'container'
25
25
  | 'workflow'
26
26
  | 'analytics_engine'
27
+ | 'vectorize_index'
27
28
 
28
29
  export interface Resource<TState> {
29
30
  getId(): { resource: ResourceKind; id: string }
@@ -1,20 +0,0 @@
1
- export const parseArgs = (args: string[]): { named: { [key: string]: string | true }; positional: string[] } => {
2
- const named: { [key: string]: string | true } = {}
3
- const positional: string[] = []
4
- for (let i = 0; i < args.length; i++) {
5
- const arg = args[i]
6
- if (arg.startsWith('--')) {
7
- const key = arg.slice(2)
8
- const value = args[i + 1]
9
- if (value && !value.startsWith('--')) {
10
- named[key] = value
11
- i++
12
- } else {
13
- named[key] = true
14
- }
15
- } else {
16
- positional.push(arg)
17
- }
18
- }
19
- return { named, positional }
20
- }