@stacksjs/dns 0.65.0 → 0.68.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.
@@ -1,260 +0,0 @@
1
- import type { CreateHostedZoneCommandOutput, HostedZone } from '@aws-sdk/client-route-53'
2
- import type { Result } from '@stacksjs/error-handling'
3
- import type { CommandError, DeployOptions, Subprocess } from '@stacksjs/types'
4
- import { Route53 } from '@aws-sdk/client-route-53'
5
- import { Route53Domains } from '@aws-sdk/client-route-53-domains'
6
- import { runAction } from '@stacksjs/actions'
7
- import { config } from '@stacksjs/config'
8
- import { Action } from '@stacksjs/enums'
9
- import { err, handleError, ok } from '@stacksjs/error-handling'
10
- import { log } from '@stacksjs/logging'
11
- import { path as p } from '@stacksjs/path'
12
- import { fs } from '@stacksjs/storage'
13
-
14
- export async function deleteHostedZone(domainName: string): Promise<Result<string, Error>> {
15
- const route53 = new Route53()
16
-
17
- // First, we need to get the Hosted Zone ID using the domain name
18
- const hostedZones = await route53.listHostedZonesByName({
19
- DNSName: domainName,
20
- })
21
- if (!hostedZones || !hostedZones.HostedZones)
22
- return err(handleError(`No hosted zones found for domain: ${domainName}`))
23
-
24
- const hostedZone = hostedZones.HostedZones.find(zone => zone.Name === `${domainName}.`)
25
- if (!hostedZone)
26
- return err(handleError(`Hosted Zone not found for domain: ${domainName}`))
27
-
28
- // Delete all record sets
29
- const recordSets = await route53.listResourceRecordSets({
30
- HostedZoneId: hostedZone.Id,
31
- })
32
- if (!recordSets || !recordSets.ResourceRecordSets)
33
- return err(handleError(`No DNS records found for domain: ${domainName}`))
34
-
35
- for (const recordSet of recordSets.ResourceRecordSets) {
36
- if (recordSet.Type !== 'NS' && recordSet.Type !== 'SOA') {
37
- await route53.changeResourceRecordSets({
38
- HostedZoneId: hostedZone.Id,
39
- ChangeBatch: {
40
- Changes: [
41
- {
42
- Action: 'DELETE',
43
- ResourceRecordSet: recordSet,
44
- },
45
- ],
46
- },
47
- })
48
- }
49
- }
50
-
51
- // Delete the hosted zone
52
- await route53.deleteHostedZone({ Id: hostedZone.Id })
53
-
54
- log.info(`Deleted Hosted Zone for domain: ${domainName}`)
55
-
56
- return ok('success')
57
- }
58
-
59
- // sometimes it’s useful to delete all records but keep the hosted zone
60
- // for example, if you want to keep the nameservers
61
- export async function deleteHostedZoneRecords(domainName: string): Promise<Result<string, Error>> {
62
- const route53 = new Route53()
63
-
64
- // First, we need to get the Hosted Zone ID using the domain name
65
- const hostedZones = await route53.listHostedZonesByName({
66
- DNSName: domainName,
67
- })
68
- if (!hostedZones || !hostedZones.HostedZones)
69
- return err(handleError(`No hosted zones found for domain: ${domainName}`))
70
-
71
- const hostedZone = hostedZones.HostedZones.find(zone => zone.Name === `${domainName}.`)
72
- if (!hostedZone)
73
- return err(handleError(`Hosted Zone not found for domain: ${domainName}`))
74
-
75
- // Delete all record sets
76
- const recordSets = await route53.listResourceRecordSets({
77
- HostedZoneId: hostedZone.Id,
78
- })
79
- if (!recordSets || !recordSets.ResourceRecordSets)
80
- return err(handleError(`No DNS records found for domain: ${domainName}`))
81
-
82
- for (const recordSet of recordSets.ResourceRecordSets) {
83
- if (recordSet.Type !== 'NS' && recordSet.Type !== 'SOA') {
84
- await route53.changeResourceRecordSets({
85
- HostedZoneId: hostedZone.Id,
86
- ChangeBatch: {
87
- Changes: [
88
- {
89
- Action: 'DELETE',
90
- ResourceRecordSet: recordSet,
91
- },
92
- ],
93
- },
94
- })
95
- }
96
- }
97
-
98
- log.info(`Deleted DNS records for domain: ${domainName}`)
99
-
100
- return ok('success')
101
- }
102
-
103
- export async function createHostedZone(
104
- domainName: string,
105
- ): Promise<Result<HostedZone | CreateHostedZoneCommandOutput | string | null, Error>> {
106
- const route53 = new Route53()
107
-
108
- // Check if the hosted zone already exists
109
- const existingHostedZones = await route53.listHostedZonesByName({
110
- DNSName: domainName,
111
- })
112
- const existingHostedZone = existingHostedZones.HostedZones?.find(zone => zone.Name === `${domainName}.`)
113
-
114
- // if the hosted zone already exists, then we want to
115
- if (existingHostedZone)
116
- return ok(existingHostedZone)
117
-
118
- // Create the hosted zone
119
- const createHostedZoneOutput = await route53.createHostedZone({
120
- Name: domainName,
121
- CallerReference: `${Date.now()}`,
122
- })
123
-
124
- if (!createHostedZoneOutput.HostedZone)
125
- return err(handleError('Failed to create hosted zone'))
126
-
127
- return ok(createHostedZoneOutput)
128
- }
129
-
130
- export function writeNameserversToConfig(nameservers: string[]): void {
131
- try {
132
- const path = p.projectConfigPath('dns.ts')
133
- const fileContent = fs.readFileSync(path, 'utf-8')
134
- const modifiedContent = fileContent.replace(
135
- /nameservers: \[.*?\]/s,
136
- `nameservers: [${nameservers.map(ns => `'${ns}'`).join(', ')}]`,
137
- )
138
- fs.writeFileSync(path, modifiedContent, 'utf-8')
139
-
140
- log.info('Nameservers have been set.')
141
- }
142
- catch (err) {
143
- console.error('Error updating nameservers:', err)
144
- }
145
- }
146
-
147
- export async function findHostedZone(domain: string): Promise<Result<string | null | undefined, Error>> {
148
- try {
149
- const route53 = new Route53()
150
- const { HostedZones } = await route53.listHostedZonesByName({
151
- DNSName: domain,
152
- })
153
-
154
- if (!HostedZones)
155
- return err(handleError(`No hosted zones found for domain ${domain}`))
156
-
157
- // The API returns hosted zones sorted by name in ASCII order,
158
- // so the desired hosted zone (if it exists) should be the first one in the list
159
- const hostedZone = HostedZones[0]
160
-
161
- if (hostedZone && hostedZone.Name === `${domain}.`)
162
- return ok(hostedZone.Id)
163
-
164
- return ok(null)
165
- }
166
- catch (error) {
167
- return err(handleError(`Failed to find hosted zone for domain ${domain}`, error))
168
- }
169
- }
170
-
171
- export async function getNameservers(domainName?: string): Promise<string[] | undefined> {
172
- log.debug('Getting nameservers for domain:', domainName)
173
- if (!domainName)
174
- return []
175
-
176
- try {
177
- const route53Domains = new Route53Domains()
178
- const domainDetail = await route53Domains.getDomainDetail({
179
- DomainName: domainName,
180
- })
181
-
182
- return domainDetail?.Nameservers?.map(ns => ns.Name as string) || []
183
- }
184
- catch (error) {
185
- handleError('Error getting domain detail', error)
186
- }
187
- }
188
-
189
- export async function updateNameservers(
190
- hostedZoneNameservers: string[],
191
- domainName?: string,
192
- ): Promise<boolean | undefined> {
193
- if (!domainName)
194
- domainName = config.app.url
195
-
196
- const domainNameservers = await getNameservers(domainName)
197
- if (
198
- domainNameservers
199
- && hostedZoneNameservers
200
- && JSON.stringify(domainNameservers.sort()) !== JSON.stringify(hostedZoneNameservers.sort())
201
- ) {
202
- log.info('Updating your domain nameservers to match the ones in your hosted zone...')
203
- log.debug('Hosted zone nameservers:', hostedZoneNameservers)
204
- log.debug('Domain nameservers:', domainNameservers)
205
- const route53Domains = new Route53Domains()
206
-
207
- await route53Domains.updateDomainNameservers({
208
- DomainName: domainName,
209
- Nameservers: hostedZoneNameservers.map(ns => ({ Name: ns })),
210
- })
211
-
212
- writeNameserversToConfig(hostedZoneNameservers)
213
-
214
- log.info('Nameservers updated.')
215
- return true
216
- }
217
-
218
- log.success('Your nameservers are up to date.')
219
- }
220
-
221
- // please note, this function also updates the user's nameservers if they are out of date
222
- export async function hasUserDomainBeenAddedToCloud(domainName?: string): Promise<boolean> {
223
- log.debug('Checking if domain has been added to cloud...')
224
- if (!domainName)
225
- domainName = config.app.url
226
- log.debug('domainName:', domainName)
227
-
228
- // check if the hosted zone already exists
229
- const route53 = new Route53()
230
- const existingHostedZones = await route53.listHostedZonesByName({
231
- DNSName: domainName,
232
- })
233
- log.debug('Existing hosted zones:', existingHostedZones)
234
-
235
- if (!existingHostedZones || !existingHostedZones.HostedZones)
236
- return false
237
-
238
- const existingHostedZone = existingHostedZones.HostedZones.find(zone => zone.Name === `${domainName}.`)
239
-
240
- if (existingHostedZone) {
241
- log.debug('Hosted zone found:', existingHostedZone)
242
-
243
- const hostedZoneDetail = await route53.getHostedZone({
244
- Id: existingHostedZone.Id,
245
- })
246
- const hostedZoneNameservers = hostedZoneDetail.DelegationSet?.NameServers || []
247
-
248
- await updateNameservers(hostedZoneNameservers, domainName)
249
-
250
- // need to return true here to indicate that the domain
251
- // has been added to cloud and is properly configured
252
- return true
253
- }
254
-
255
- return false
256
- }
257
-
258
- export async function addDomain(options: DeployOptions): Promise<Result<Subprocess, CommandError>> {
259
- return await runAction(Action.DomainsAdd, options)
260
- }
package/src/index.ts DELETED
@@ -1,19 +0,0 @@
1
- // import { type NestedStackProps } from 'aws-cdk-lib'
2
- // import { NestedStack, aws_route53 as route53 } from 'aws-cdk-lib'
3
- // import { type Construct } from 'constructs'
4
- // import { app } from '@stacksjs/config'
5
-
6
- // export class DnsStack extends NestedStack {
7
- // constructor(scope: Construct, id: string, props?: NestedStackProps) {
8
- // super(scope, id, props)
9
-
10
- // if (!app.url)
11
- // throw new Error('./config app.url is not defined')
12
-
13
- // new route53.PublicHostedZone(this, 'HostedZone', {
14
- // zoneName: app.url,
15
- // })
16
- // }
17
- // }
18
-
19
- export * from './drivers/aws'