-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadapter.ts
175 lines (152 loc) · 4.83 KB
/
adapter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { writeFileSync } from 'fs'
import { join } from 'path'
import * as url from 'url'
import {
LocalProgramArgs,
LocalWorkspace,
} from '@pulumi/pulumi/automation/index.js'
import {
buildServer,
buildOptions,
buildRouter,
} from 'sveltekit-adapter-aws-base'
const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
export interface AWSAdapterProps {
artifactPath?: string
autoDeploy?: boolean
cachePolicy?: string
defaultHeaders?: string[]
esbuildOptions?: any
extraHeaders?: string[]
FQDN?: string
memorySize?: number
pulumiPaths?: string[]
region?: string
serverStreaming?: boolean
stackName: string
}
export function adapter({
artifactPath = 'build',
autoDeploy = false,
cachePolicy = 'CachingOptimized',
defaultHeaders = [
'Accept',
'Accept-Language',
'If-None-Match',
'Host',
'Origin',
'Referer',
'X-Forwarded-Host',
],
extraHeaders = [],
esbuildOptions = {},
FQDN,
memorySize = 128,
region = 'us-east-2',
serverStreaming = false,
stackName = 'dev',
}: AWSAdapterProps) {
/** @type {import('@sveltejs/kit').Adapter} */
return {
name: 'adapter-aws-pulumi',
async adapt(builder: any) {
const { server_directory, static_directory, prerendered_directory } =
await buildServer(
builder,
artifactPath,
esbuildOptions,
serverStreaming,
)
const options_directory = await buildOptions(builder, artifactPath)
if (autoDeploy) {
builder.log.minor('Deploy using Pulumi.')
// Setup server stack.
const serverPath = join(__dirname, 'stacks', 'server')
const serverArgs: LocalProgramArgs = {
stackName: stackName,
workDir: serverPath,
}
const serverStack = await LocalWorkspace.createOrSelectStack(serverArgs)
await serverStack.setAllConfig({
'aws:region': { value: region },
projectPath: { value: process.cwd() },
serverPath: { value: server_directory },
optionsPath: { value: options_directory },
memorySize: { value: String(memorySize) },
})
if (serverStreaming) {
await serverStack.setConfig('serverInvokeMode', {
value: 'RESPONSE_STREAM',
})
} else {
await serverStack.removeConfig('serverInvokeMode')
}
await serverStack.refresh()
const serverStackUpResult = await serverStack.up({
onOutput: console.info,
})
const edge_directory = await buildRouter(
builder,
static_directory,
prerendered_directory,
serverStackUpResult.outputs.serverDomain.value,
serverStackUpResult.outputs.optionsDomain.value,
artifactPath,
)
// Setup main stack.
const mainPath = join(__dirname, 'stacks', 'main')
const mainArgs: LocalProgramArgs = {
stackName: stackName,
workDir: mainPath,
}
const mainStack = await LocalWorkspace.createOrSelectStack(mainArgs)
await mainStack.setAllConfig({
'aws:region': { value: region },
cachePolicy: { value: `Managed-${cachePolicy}` },
edgePath: { value: edge_directory },
staticPath: { value: static_directory },
prerenderedPath: { value: prerendered_directory },
serverArn: { value: serverStackUpResult.outputs.serverArn.value },
optionsArn: { value: serverStackUpResult.outputs.optionsArn.value },
})
if (FQDN) {
await mainStack.setConfig('FQDN', { value: FQDN })
} else {
await mainStack.removeConfig('FQDN')
}
let serverHeaders: string[] = [...defaultHeaders]
if (extraHeaders) {
serverHeaders = serverHeaders.concat(extraHeaders)
}
if (serverHeaders.length > 0) {
await mainStack.setConfig('serverHeaders', {
value: JSON.stringify(serverHeaders),
})
} else {
await mainStack.removeConfig('serverHeaders')
}
await mainStack.refresh()
const mainStackUpResult = await mainStack.up({ onOutput: console.info })
const mainAllowedOrigins = JSON.stringify(
mainStackUpResult.outputs.allowedOrigins.value,
)
// Call the server stack setting the allowed origins
await serverStack.setConfig('allowedOrigins', {
value: mainAllowedOrigins,
})
await serverStack.up({
onOutput: console.info,
})
const adapterProps: AWSAdapterProps = {
pulumiPaths: [serverPath, mainPath],
stackName: stackName,
}
writeFileSync(
join(artifactPath, '.adapterprops.json'),
JSON.stringify(adapterProps),
)
builder.log.minor('Pulumi deployment done.')
}
},
}
}