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