@zabaca/zbc 0.13.0 → 0.15.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/CHANGELOG.md +117 -6
- package/package.json +5 -2
- package/src/commands/add.ts +13 -0
- package/src/commands/apply.ts +33 -9
- package/src/commands/destroy.ts +1 -1
- package/src/commands/init.ts +1 -0
- package/src/engine/apply.ts +110 -22
- package/src/engine/destroy.ts +183 -14
- package/src/engine/discover.ts +1 -1
- package/src/engine/resolve.ts +113 -14
- package/src/utils/copy-template.ts +10 -0
- package/templates/CLAUDE.md +1 -1
- package/templates/apps/inbox/package.json +3 -1
- package/templates/apps/inbox/tsconfig.json +3 -1
- package/templates/apps/inbox/worker/api.ts +197 -0
- package/templates/apps/inbox/worker/commands.ts +299 -0
- package/templates/apps/inbox/worker/inbox-do.ts +53 -10
- package/templates/apps/inbox/worker/index.ts +15 -202
- package/templates/apps/inbox/worker/mcp.ts +49 -100
- package/templates/infra/modules/cloudflare/index.ts +29 -73
- package/templates/infra/modules/cloudflare-access/index.ts +3 -3
- package/templates/infra/modules/cloudflare-api/index.ts +144 -32
- package/templates/infra/modules/cloudflare-api/registry.json +1 -1
- package/templates/infra/modules/cloudflare-email/index.ts +103 -144
- package/templates/infra/modules/cloudflare-email/registry.json +1 -1
- package/templates/infra/modules/cloudflare-token/index.ts +10 -32
- package/templates/infra/modules/cloudflare-token/registry.json +1 -1
- package/templates/infra/modules/cloudflare-tunnel/index.ts +3 -3
- package/templates/infra/modules/cloudflare-zone/index.ts +2 -2
- package/templates/infra/modules/fly/index.ts +17 -42
- package/templates/infra/modules/fly/registry.json +1 -1
- package/templates/infra/modules/host-file/index.ts +6 -7
- package/templates/infra/modules/provision-core/index.ts +21 -15
- package/templates/infra/modules/r2/index.ts +40 -71
- package/templates/infra/modules/r2/registry.json +1 -1
- package/templates/infra/modules/remote-provision/index.ts +1 -3
- package/templates/infra/modules/turso/index.ts +2 -15
- package/templates/infra/modules/vm-provision/index.ts +10 -3
- package/templates/infra/src/context.ts +115 -0
- package/templates/infra/src/define-module.ts +41 -2
- package/templates/infra/src/index.ts +11 -1
- package/templates/infra/src/types.ts +85 -3
- package/src/commands/add-registry.test.ts +0 -92
- package/src/commands/secret.test.ts +0 -717
- package/src/commands/vendor-mode.test.ts +0 -183
- package/src/infra-types.ts +0 -38
- package/src/utils/subtree.test.ts +0 -127
- package/templates/apps/inbox/worker/send-op.ts +0 -174
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import { afterEach, describe, expect, test } from 'bun:test'
|
|
2
|
-
import { execSync, spawnSync } from 'node:child_process'
|
|
3
|
-
import * as fs from 'node:fs'
|
|
4
|
-
import * as os from 'node:os'
|
|
5
|
-
import * as path from 'node:path'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* End-to-end tests for subtree (vendor) mode, spawning the real CLI against a
|
|
9
|
-
* throwaway consumer repo + a local stand-in for Zabaca/zbc-core. No network:
|
|
10
|
-
* the core "URL" is a local path and the fake module declares no dependencies.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
const CLI = path.resolve(import.meta.dir, '../index.ts')
|
|
14
|
-
|
|
15
|
-
const roots: string[] = []
|
|
16
|
-
afterEach(() => {
|
|
17
|
-
for (const r of roots.splice(0)) fs.rmSync(r, { recursive: true, force: true })
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
function sh(cwd: string, cmd: string): string {
|
|
21
|
-
return execSync(cmd, { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] })
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function tmpdir(prefix: string): string {
|
|
25
|
-
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix))
|
|
26
|
-
roots.push(dir)
|
|
27
|
-
return dir
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function makeRepo(dir: string): void {
|
|
31
|
-
sh(dir, 'git init -q -b main')
|
|
32
|
-
sh(dir, 'git config user.email t@t && git config user.name t')
|
|
33
|
-
fs.writeFileSync(path.join(dir, '.keep'), '')
|
|
34
|
-
sh(dir, 'git add . && git commit -qm init')
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function makeCore(): string {
|
|
38
|
-
const dir = tmpdir('zbc-core-')
|
|
39
|
-
makeRepo(dir)
|
|
40
|
-
fs.mkdirSync(path.join(dir, 'src'), { recursive: true })
|
|
41
|
-
fs.writeFileSync(path.join(dir, 'src/define-module.ts'), 'export const defineModule = 1\n')
|
|
42
|
-
fs.mkdirSync(path.join(dir, 'modules/faketool'), { recursive: true })
|
|
43
|
-
fs.writeFileSync(path.join(dir, 'modules/faketool/index.ts'), 'export const faketool = 1\n')
|
|
44
|
-
fs.writeFileSync(
|
|
45
|
-
path.join(dir, 'modules/faketool/registry.json'),
|
|
46
|
-
JSON.stringify({
|
|
47
|
-
name: 'faketool',
|
|
48
|
-
description: 'test module',
|
|
49
|
-
files: [{ path: 'index.ts' }],
|
|
50
|
-
secrets: ['FAKETOOL_TOKEN'],
|
|
51
|
-
instructions: 'faketool post-install text',
|
|
52
|
-
}),
|
|
53
|
-
)
|
|
54
|
-
sh(dir, 'git add . && git commit -qm core-content')
|
|
55
|
-
return dir
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function zbc(cwd: string, args: string[]): { status: number; out: string } {
|
|
59
|
-
const res = spawnSync('bun', [CLI, ...args], { cwd, encoding: 'utf8' })
|
|
60
|
-
return { status: res.status ?? -1, out: `${res.stdout}\n${res.stderr}` }
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function initSubtree(core: string): string {
|
|
64
|
-
const consumer = tmpdir('consumer-')
|
|
65
|
-
makeRepo(consumer)
|
|
66
|
-
const res = zbc(consumer, [
|
|
67
|
-
'init',
|
|
68
|
-
'testproj',
|
|
69
|
-
'--subtree',
|
|
70
|
-
'--core-url',
|
|
71
|
-
core,
|
|
72
|
-
'--core-ref',
|
|
73
|
-
'main',
|
|
74
|
-
])
|
|
75
|
-
expect(res.status).toBe(0)
|
|
76
|
-
return consumer
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
describe('zbc init --subtree', () => {
|
|
80
|
-
test('vendors core at vendor/zbc and skips copying the engine into packages/infra', () => {
|
|
81
|
-
const consumer = initSubtree(makeCore())
|
|
82
|
-
expect(fs.existsSync(path.join(consumer, 'vendor/zbc/src/define-module.ts'))).toBe(true)
|
|
83
|
-
expect(fs.existsSync(path.join(consumer, 'vendor/zbc/modules/faketool/index.ts'))).toBe(true)
|
|
84
|
-
// Engine comes from the vendor tree — no copied src/, no split-repo README.
|
|
85
|
-
expect(fs.existsSync(path.join(consumer, 'packages/infra/src'))).toBe(false)
|
|
86
|
-
expect(fs.existsSync(path.join(consumer, 'packages/infra/README.md'))).toBe(false)
|
|
87
|
-
// Rest of the scaffold still lands.
|
|
88
|
-
expect(fs.existsSync(path.join(consumer, 'zbc.config.ts'))).toBe(true)
|
|
89
|
-
expect(fs.existsSync(path.join(consumer, 'packages/infra/package.json'))).toBe(true)
|
|
90
|
-
// zbc.config.ts must import the engine from the vendor tree — the copied
|
|
91
|
-
// '@<project>/infra' package points at a src/ that subtree mode skips.
|
|
92
|
-
const config = fs.readFileSync(path.join(consumer, 'zbc.config.ts'), 'utf8')
|
|
93
|
-
expect(config).toContain("from './vendor/zbc/src/index'")
|
|
94
|
-
expect(config).not.toContain('@testproj/infra')
|
|
95
|
-
// Subtree bookkeeping recorded.
|
|
96
|
-
expect(sh(consumer, 'git log --format=%B')).toContain('git-subtree-dir: vendor/zbc')
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
test('fresh subtree init leaves no dangling engine exports in packages/infra', () => {
|
|
100
|
-
const consumer = initSubtree(makeCore())
|
|
101
|
-
const pkg = JSON.parse(
|
|
102
|
-
fs.readFileSync(path.join(consumer, 'packages/infra/package.json'), 'utf8'),
|
|
103
|
-
) as Record<string, unknown>
|
|
104
|
-
// exports "." → ./src/index.ts would be unresolvable — src/ isn't copied.
|
|
105
|
-
expect(pkg.exports).toBeUndefined()
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
test('brownfield repo with an existing copy-mode zbc.config.ts gets its import rewritten', () => {
|
|
109
|
-
const core = makeCore()
|
|
110
|
-
const consumer = tmpdir('consumer-')
|
|
111
|
-
makeRepo(consumer)
|
|
112
|
-
fs.writeFileSync(
|
|
113
|
-
path.join(consumer, 'zbc.config.ts'),
|
|
114
|
-
"import { defineConfig } from '@oldname/infra'\nexport default defineConfig({ project: 'oldname', environments: ['production'] })\n",
|
|
115
|
-
)
|
|
116
|
-
sh(consumer, 'git add . && git commit -qm existing-config')
|
|
117
|
-
const res = zbc(consumer, ['init', '--subtree', '--core-url', core, '--core-ref', 'main'])
|
|
118
|
-
expect(res.status).toBe(0)
|
|
119
|
-
const config = fs.readFileSync(path.join(consumer, 'zbc.config.ts'), 'utf8')
|
|
120
|
-
expect(config).toContain("from './vendor/zbc/src/index'")
|
|
121
|
-
expect(config).not.toContain('/infra')
|
|
122
|
-
})
|
|
123
|
-
|
|
124
|
-
test('dirty tree → nonzero exit naming uncommitted changes, no vendor dir', () => {
|
|
125
|
-
const core = makeCore()
|
|
126
|
-
const consumer = tmpdir('consumer-')
|
|
127
|
-
makeRepo(consumer)
|
|
128
|
-
fs.writeFileSync(path.join(consumer, 'wip.txt'), 'x')
|
|
129
|
-
const res = zbc(consumer, ['init', '--subtree', '--core-url', core, '--core-ref', 'main'])
|
|
130
|
-
expect(res.status).not.toBe(0)
|
|
131
|
-
expect(res.out).toMatch(/uncommitted/i)
|
|
132
|
-
expect(fs.existsSync(path.join(consumer, 'vendor/zbc'))).toBe(false)
|
|
133
|
-
})
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
describe('zbc add in vendor mode', () => {
|
|
137
|
-
test('resolves the module from vendor/zbc, copies nothing, prints vendor import path', () => {
|
|
138
|
-
const consumer = initSubtree(makeCore())
|
|
139
|
-
const res = zbc(consumer, ['add', 'faketool', '--no-prompt'])
|
|
140
|
-
expect(res.status).toBe(0)
|
|
141
|
-
// Nothing vendored into packages/infra/modules — the module lives in vendor/zbc.
|
|
142
|
-
expect(fs.existsSync(path.join(consumer, 'packages/infra/modules/faketool'))).toBe(false)
|
|
143
|
-
expect(res.out).toContain('faketool post-install text')
|
|
144
|
-
// Full relative path from packages/infra/environments/<env>/ — four .. to
|
|
145
|
-
// the repo root. (A bare substring match would be satisfied by the
|
|
146
|
-
// "already vendored at vendor/zbc/..." log line and miss an off-by-one.)
|
|
147
|
-
expect(res.out).toContain('../../../../vendor/zbc/modules/faketool')
|
|
148
|
-
})
|
|
149
|
-
|
|
150
|
-
test('unknown module still errors clearly', () => {
|
|
151
|
-
const consumer = initSubtree(makeCore())
|
|
152
|
-
const res = zbc(consumer, ['add', 'no-such-module', '--no-prompt'])
|
|
153
|
-
expect(res.status).not.toBe(0)
|
|
154
|
-
expect(res.out).toContain('no-such-module')
|
|
155
|
-
})
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
describe('zbc update', () => {
|
|
159
|
-
test('pulls newer core commits into vendor/zbc', () => {
|
|
160
|
-
const core = makeCore()
|
|
161
|
-
const consumer = initSubtree(core)
|
|
162
|
-
// Commit the init scaffold so the tree is clean for the pull.
|
|
163
|
-
sh(consumer, 'git add . && git commit -qm scaffold')
|
|
164
|
-
|
|
165
|
-
fs.mkdirSync(path.join(core, 'modules/newmod'), { recursive: true })
|
|
166
|
-
fs.writeFileSync(path.join(core, 'modules/newmod/index.ts'), 'export const n = 1\n')
|
|
167
|
-
sh(core, 'git add . && git commit -qm add-newmod')
|
|
168
|
-
|
|
169
|
-
const res = zbc(consumer, ['update', '--core-url', core, '--core-ref', 'main'])
|
|
170
|
-
expect(res.status).toBe(0)
|
|
171
|
-
expect(fs.existsSync(path.join(consumer, 'vendor/zbc/modules/newmod/index.ts'))).toBe(true)
|
|
172
|
-
})
|
|
173
|
-
|
|
174
|
-
test('outside vendor mode (copy-mode project) → clear error', () => {
|
|
175
|
-
const consumer = tmpdir('consumer-')
|
|
176
|
-
makeRepo(consumer)
|
|
177
|
-
// A copy-mode zbc project: config present, no vendor/zbc.
|
|
178
|
-
fs.writeFileSync(path.join(consumer, 'zbc.config.ts'), 'export default {}\n')
|
|
179
|
-
const res = zbc(consumer, ['update', '--core-url', makeCore(), '--core-ref', 'main'])
|
|
180
|
-
expect(res.status).not.toBe(0)
|
|
181
|
-
expect(res.out).toMatch(/vendor|subtree/i)
|
|
182
|
-
})
|
|
183
|
-
})
|
package/src/infra-types.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
// Mirror of packages/infra/src/types.ts. Inlined here so the published CLI
|
|
2
|
-
// has no runtime/workspace dependency on @zbc/infra. Keep in sync.
|
|
3
|
-
import type { z } from 'zod'
|
|
4
|
-
|
|
5
|
-
export type ApplyFn<TConfig, TOutputs> = (config: TConfig, ctx: ApplyContext) => Promise<TOutputs>
|
|
6
|
-
|
|
7
|
-
export type DestroyFn<TConfig> = (config: TConfig, ctx: ApplyContext) => Promise<void>
|
|
8
|
-
|
|
9
|
-
export interface ApplyContext {
|
|
10
|
-
secrets: Record<string, string>
|
|
11
|
-
imports: Record<string, unknown>
|
|
12
|
-
projectRoot: string
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface ModuleDefinition<TConfig extends z.ZodType, TOutputs extends z.ZodType> {
|
|
16
|
-
name: string
|
|
17
|
-
configSchema: TConfig
|
|
18
|
-
outputsSchema: TOutputs
|
|
19
|
-
apply: ApplyFn<z.infer<TConfig>, z.infer<TOutputs>>
|
|
20
|
-
destroy?: DestroyFn<z.infer<TConfig>>
|
|
21
|
-
instance: (opts: InstanceOptions<TConfig>) => ModuleInstance<TOutputs>
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface InstanceOptions<TConfig extends z.ZodType> {
|
|
25
|
-
name: string
|
|
26
|
-
config: z.input<TConfig>
|
|
27
|
-
imports?: ModuleInstance[]
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export interface ModuleInstance<TOutputs extends z.ZodType = z.ZodType> {
|
|
31
|
-
name: string
|
|
32
|
-
moduleName: string
|
|
33
|
-
config: unknown
|
|
34
|
-
imports: ModuleInstance[]
|
|
35
|
-
_outputsSchema: TOutputs
|
|
36
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
37
|
-
_definition: ModuleDefinition<any, TOutputs>
|
|
38
|
-
}
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { afterEach, describe, expect, test } from 'bun:test'
|
|
2
|
-
import { execSync } from 'node:child_process'
|
|
3
|
-
import * as fs from 'node:fs'
|
|
4
|
-
import * as os from 'node:os'
|
|
5
|
-
import * as path from 'node:path'
|
|
6
|
-
import {
|
|
7
|
-
coreRefForVersion,
|
|
8
|
-
ensureCleanGitTree,
|
|
9
|
-
isVendorMode,
|
|
10
|
-
subtreeAdd,
|
|
11
|
-
subtreePull,
|
|
12
|
-
VENDOR_PREFIX,
|
|
13
|
-
} from './subtree'
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Real-git tests: a throwaway "core" repo stands in for Zabaca/zbc-core (local
|
|
17
|
-
* path in place of the URL — git subtree accepts any repository), and a
|
|
18
|
-
* throwaway consumer repo vendors it. No network.
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
const roots: string[] = []
|
|
22
|
-
afterEach(() => {
|
|
23
|
-
for (const r of roots.splice(0)) fs.rmSync(r, { recursive: true, force: true })
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
function sh(cwd: string, cmd: string): string {
|
|
27
|
-
return execSync(cmd, { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] })
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function tmpdir(prefix: string): string {
|
|
31
|
-
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix))
|
|
32
|
-
roots.push(dir)
|
|
33
|
-
return dir
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** Init a git repo with identity + one commit so subtree has a base. */
|
|
37
|
-
function makeRepo(dir: string): void {
|
|
38
|
-
sh(dir, 'git init -q -b main')
|
|
39
|
-
sh(dir, 'git config user.email t@t && git config user.name t')
|
|
40
|
-
fs.writeFileSync(path.join(dir, '.keep'), '')
|
|
41
|
-
sh(dir, 'git add . && git commit -qm init')
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/** A fake zbc-core: src/define-module.ts + one dep-free module. */
|
|
45
|
-
function makeCore(): string {
|
|
46
|
-
const dir = tmpdir('zbc-core-')
|
|
47
|
-
makeRepo(dir)
|
|
48
|
-
fs.mkdirSync(path.join(dir, 'src'), { recursive: true })
|
|
49
|
-
fs.writeFileSync(path.join(dir, 'src/define-module.ts'), 'export const defineModule = 1\n')
|
|
50
|
-
fs.mkdirSync(path.join(dir, 'modules/faketool'), { recursive: true })
|
|
51
|
-
fs.writeFileSync(path.join(dir, 'modules/faketool/index.ts'), 'export const faketool = 1\n')
|
|
52
|
-
fs.writeFileSync(
|
|
53
|
-
path.join(dir, 'modules/faketool/registry.json'),
|
|
54
|
-
JSON.stringify({ name: 'faketool', description: 'test module', files: [{ path: 'index.ts' }] }),
|
|
55
|
-
)
|
|
56
|
-
sh(dir, 'git add . && git commit -qm core-content')
|
|
57
|
-
return dir
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
describe('coreRefForVersion', () => {
|
|
61
|
-
test('maps a CLI version to its core tag', () => {
|
|
62
|
-
expect(coreRefForVersion('0.9.0')).toBe('zbc-core-v0.9.0')
|
|
63
|
-
})
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
describe('ensureCleanGitTree', () => {
|
|
67
|
-
test('passes on a clean tree', () => {
|
|
68
|
-
const dir = tmpdir('clean-')
|
|
69
|
-
makeRepo(dir)
|
|
70
|
-
expect(() => ensureCleanGitTree(dir)).not.toThrow()
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
test('throws on a dirty tree, naming the offending state', () => {
|
|
74
|
-
const dir = tmpdir('dirty-')
|
|
75
|
-
makeRepo(dir)
|
|
76
|
-
fs.writeFileSync(path.join(dir, 'uncommitted.txt'), 'x')
|
|
77
|
-
expect(() => ensureCleanGitTree(dir)).toThrow(/uncommitted/i)
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
test('throws outside a git repository', () => {
|
|
81
|
-
const dir = tmpdir('nogit-')
|
|
82
|
-
expect(() => ensureCleanGitTree(dir)).toThrow(/git/i)
|
|
83
|
-
})
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
describe('subtreeAdd / isVendorMode / subtreePull', () => {
|
|
87
|
-
test('vendors core under vendor/zbc with a squash commit; isVendorMode flips', async () => {
|
|
88
|
-
const core = makeCore()
|
|
89
|
-
const consumer = tmpdir('consumer-')
|
|
90
|
-
makeRepo(consumer)
|
|
91
|
-
|
|
92
|
-
expect(await isVendorMode(consumer)).toBe(false)
|
|
93
|
-
subtreeAdd(consumer, { url: core, ref: 'main' })
|
|
94
|
-
|
|
95
|
-
expect(fs.existsSync(path.join(consumer, VENDOR_PREFIX, 'src/define-module.ts'))).toBe(true)
|
|
96
|
-
expect(
|
|
97
|
-
fs.existsSync(path.join(consumer, VENDOR_PREFIX, 'modules/faketool/registry.json')),
|
|
98
|
-
).toBe(true)
|
|
99
|
-
expect(await isVendorMode(consumer)).toBe(true)
|
|
100
|
-
// --squash bookkeeping present (subtree pull depends on it)
|
|
101
|
-
const log = sh(consumer, 'git log --format=%B')
|
|
102
|
-
expect(log).toContain(`git-subtree-dir: ${VENDOR_PREFIX}`)
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
test('subtreePull brings later core commits into the vendored copy', () => {
|
|
106
|
-
const core = makeCore()
|
|
107
|
-
const consumer = tmpdir('consumer-')
|
|
108
|
-
makeRepo(consumer)
|
|
109
|
-
subtreeAdd(consumer, { url: core, ref: 'main' })
|
|
110
|
-
|
|
111
|
-
fs.mkdirSync(path.join(core, 'modules/newmod'), { recursive: true })
|
|
112
|
-
fs.writeFileSync(path.join(core, 'modules/newmod/index.ts'), 'export const n = 1\n')
|
|
113
|
-
sh(core, 'git add . && git commit -qm add-newmod')
|
|
114
|
-
|
|
115
|
-
subtreePull(consumer, { url: core, ref: 'main' })
|
|
116
|
-
expect(fs.existsSync(path.join(consumer, VENDOR_PREFIX, 'modules/newmod/index.ts'))).toBe(true)
|
|
117
|
-
})
|
|
118
|
-
|
|
119
|
-
test('subtreeAdd on a dirty tree throws before touching anything', () => {
|
|
120
|
-
const core = makeCore()
|
|
121
|
-
const consumer = tmpdir('consumer-')
|
|
122
|
-
makeRepo(consumer)
|
|
123
|
-
fs.writeFileSync(path.join(consumer, 'wip.txt'), 'x')
|
|
124
|
-
expect(() => subtreeAdd(consumer, { url: core, ref: 'main' })).toThrow(/uncommitted/i)
|
|
125
|
-
expect(fs.existsSync(path.join(consumer, VENDOR_PREFIX))).toBe(false)
|
|
126
|
-
})
|
|
127
|
-
})
|
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
import type { Inbox } from './inbox-do'
|
|
2
|
-
import { type MailEnv, emitWebhook, makeSnippet, newId, normalizeMsgId, sendMail } from './shared'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* The one send operation, shared by POST /api/send and the MCP send_email
|
|
6
|
-
* tool (a Worker cannot fetch its own routes, so this is a function, not a
|
|
7
|
-
* self-request). Validates, dispatches now or queues for the DO alarm
|
|
8
|
-
* (sendAt), records the outbound row, and emits the message.sent webhook.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
export interface SendBody {
|
|
12
|
-
to: string
|
|
13
|
-
subject: string
|
|
14
|
-
text?: string
|
|
15
|
-
html?: string
|
|
16
|
-
from?: string
|
|
17
|
-
/** RFC 5322 Message-ID of the message being replied to → In-Reply-To/References. */
|
|
18
|
-
inReplyTo?: string
|
|
19
|
-
/**
|
|
20
|
-
* ISO timestamp: instead of sending now, queue the message for the DO
|
|
21
|
-
* alarm to dispatch at (or shortly after) this time.
|
|
22
|
-
*/
|
|
23
|
-
sendAt?: string
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export interface SendEnv extends MailEnv {
|
|
27
|
-
DEFAULT_FROM?: string
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export interface SendOutcome {
|
|
31
|
-
status: number
|
|
32
|
-
body: Record<string, unknown>
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export async function performSend(
|
|
36
|
-
env: SendEnv,
|
|
37
|
-
stub: DurableObjectStub<Inbox>,
|
|
38
|
-
body: SendBody,
|
|
39
|
-
): Promise<SendOutcome> {
|
|
40
|
-
if (!body.to || !body.subject || (!body.text && !body.html)) {
|
|
41
|
-
return { status: 400, body: { error: 'required: to, subject, and text or html' } }
|
|
42
|
-
}
|
|
43
|
-
const from = body.from ?? env.DEFAULT_FROM
|
|
44
|
-
if (!from) {
|
|
45
|
-
return { status: 400, body: { error: 'no from address (set DEFAULT_FROM or pass from)' } }
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// Email Sending only accepts whitelisted custom headers (a Message-ID of
|
|
49
|
-
// our own is rejected), so the wire Message-ID is provider-chosen and
|
|
50
|
-
// unknown to us. We store a synthetic one for OUR send chain; recipient
|
|
51
|
-
// replies thread via the subject+participant fallback.
|
|
52
|
-
const id = newId()
|
|
53
|
-
const messageId = `${id}@${from.split('@')[1] ?? 'localhost'}`
|
|
54
|
-
const inReplyTo = normalizeMsgId(body.inReplyTo)
|
|
55
|
-
const text = body.text ?? ''
|
|
56
|
-
const snippet = makeSnippet(text, body.html ?? '')
|
|
57
|
-
|
|
58
|
-
// sendAt → queue for the DO alarm instead of dispatching now.
|
|
59
|
-
if (body.sendAt !== undefined) {
|
|
60
|
-
const at = new Date(body.sendAt)
|
|
61
|
-
if (Number.isNaN(at.getTime())) {
|
|
62
|
-
return { status: 400, body: { error: 'sendAt is not a valid date' } }
|
|
63
|
-
}
|
|
64
|
-
await stub.insert({
|
|
65
|
-
id,
|
|
66
|
-
status: 'scheduled',
|
|
67
|
-
scheduled_at: at.toISOString(),
|
|
68
|
-
direction: 'outbound',
|
|
69
|
-
in_reply_to: inReplyTo,
|
|
70
|
-
message_id: messageId,
|
|
71
|
-
from_addr: from,
|
|
72
|
-
to_addr: body.to,
|
|
73
|
-
subject: body.subject,
|
|
74
|
-
date: at.toISOString(),
|
|
75
|
-
snippet,
|
|
76
|
-
text_body: text,
|
|
77
|
-
html_body: body.html ?? '',
|
|
78
|
-
r2_key: '',
|
|
79
|
-
attachments_json: '[]',
|
|
80
|
-
size: text.length + (body.html?.length ?? 0),
|
|
81
|
-
})
|
|
82
|
-
return { status: 202, body: { ok: true, id, status: 'scheduled', sendAt: at.toISOString() } }
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
try {
|
|
86
|
-
await sendMail(env, {
|
|
87
|
-
from,
|
|
88
|
-
to: body.to,
|
|
89
|
-
subject: body.subject,
|
|
90
|
-
text: body.text,
|
|
91
|
-
html: body.html,
|
|
92
|
-
inReplyTo,
|
|
93
|
-
})
|
|
94
|
-
} catch (err) {
|
|
95
|
-
return { status: 502, body: { error: `send failed: ${(err as Error).message}` } }
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Record the outbound message so the inbox shows both sides of a
|
|
99
|
-
// conversation. No raw MIME (we never see the provider's final wire form),
|
|
100
|
-
// so r2_key stays empty and /raw 404s for these.
|
|
101
|
-
const { threadId } = await stub.insert({
|
|
102
|
-
id,
|
|
103
|
-
direction: 'outbound',
|
|
104
|
-
in_reply_to: inReplyTo,
|
|
105
|
-
message_id: messageId,
|
|
106
|
-
from_addr: from,
|
|
107
|
-
to_addr: body.to,
|
|
108
|
-
subject: body.subject,
|
|
109
|
-
date: new Date().toISOString(),
|
|
110
|
-
snippet,
|
|
111
|
-
text_body: text,
|
|
112
|
-
html_body: body.html ?? '',
|
|
113
|
-
r2_key: '',
|
|
114
|
-
attachments_json: '[]',
|
|
115
|
-
size: text.length + (body.html?.length ?? 0),
|
|
116
|
-
})
|
|
117
|
-
await emitWebhook(env, 'message.sent', {
|
|
118
|
-
id,
|
|
119
|
-
threadId,
|
|
120
|
-
from,
|
|
121
|
-
to: body.to,
|
|
122
|
-
subject: body.subject,
|
|
123
|
-
snippet,
|
|
124
|
-
direction: 'outbound',
|
|
125
|
-
})
|
|
126
|
-
return { status: 200, body: { ok: true, id, from, to: body.to, messageId, threadId } }
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/** Send an existing draft: validate completeness, dispatch, flip to sent. */
|
|
130
|
-
export async function performDraftSend(
|
|
131
|
-
env: SendEnv,
|
|
132
|
-
stub: DurableObjectStub<Inbox>,
|
|
133
|
-
draftId: string,
|
|
134
|
-
): Promise<SendOutcome> {
|
|
135
|
-
const draft = await stub.get(draftId)
|
|
136
|
-
if (!draft || draft.status !== 'draft') return { status: 404, body: { error: 'not a draft' } }
|
|
137
|
-
const from = draft.from_addr || env.DEFAULT_FROM
|
|
138
|
-
if (!from) {
|
|
139
|
-
return { status: 400, body: { error: 'no from address (set DEFAULT_FROM or the draft from)' } }
|
|
140
|
-
}
|
|
141
|
-
if (!draft.to_addr || !draft.subject || (!draft.text_body && !draft.html_body)) {
|
|
142
|
-
return { status: 400, body: { error: 'draft incomplete: needs to, subject, and text or html' } }
|
|
143
|
-
}
|
|
144
|
-
try {
|
|
145
|
-
await sendMail(env, {
|
|
146
|
-
from,
|
|
147
|
-
to: draft.to_addr,
|
|
148
|
-
subject: draft.subject,
|
|
149
|
-
text: draft.text_body || undefined,
|
|
150
|
-
html: draft.html_body || undefined,
|
|
151
|
-
inReplyTo: draft.in_reply_to,
|
|
152
|
-
})
|
|
153
|
-
} catch (err) {
|
|
154
|
-
return { status: 502, body: { error: `send failed: ${(err as Error).message}` } }
|
|
155
|
-
}
|
|
156
|
-
// Persist the from that was actually used (may have come from
|
|
157
|
-
// DEFAULT_FROM), then flip the row to sent — thread resolves NOW.
|
|
158
|
-
if (from !== draft.from_addr) await stub.updateDraft(draftId, { from_addr: from })
|
|
159
|
-
const sent = await stub.markDraftSent(draftId, new Date().toISOString())
|
|
160
|
-
if (!sent) return { status: 500, body: { error: 'draft vanished during send' } }
|
|
161
|
-
await emitWebhook(env, 'message.sent', {
|
|
162
|
-
id: draftId,
|
|
163
|
-
threadId: sent.threadId,
|
|
164
|
-
from,
|
|
165
|
-
to: draft.to_addr,
|
|
166
|
-
subject: draft.subject,
|
|
167
|
-
snippet: draft.snippet,
|
|
168
|
-
direction: 'outbound',
|
|
169
|
-
})
|
|
170
|
-
return {
|
|
171
|
-
status: 200,
|
|
172
|
-
body: { ok: true, id: draftId, from, to: draft.to_addr, threadId: sent.threadId },
|
|
173
|
-
}
|
|
174
|
-
}
|