Skip to content

Commit f166662

Browse files
committed
feat: always proxy from main branch by default
fixes #2331 fixes #2328 fixes #2219
1 parent 2e4dc1b commit f166662

File tree

2 files changed

+33
-127
lines changed

2 files changed

+33
-127
lines changed

src/up.cmd.js

+19-39
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,16 @@ export default class UpCommand extends AbstractServerCommand {
7474
this.log.info('');
7575

7676
const ref = await GitUtils.getBranch(this.directory);
77-
const gitUrl = await GitUtils.getOriginURL(this.directory, { ref });
78-
let explicitURL = true;
77+
this._gitUrl = await GitUtils.getOriginURL(this.directory, { ref });
7978
if (!this._url) {
80-
explicitURL = false;
81-
// check if remote already has the `ref`
82-
await this.verifyUrl(gitUrl, ref);
79+
await this.verifyUrl(this._gitUrl, ref);
8380
}
8481
this._project.withProxyUrl(this._url);
8582
await this.initSeverOptions();
8683

8784
try {
8885
await this._project.init();
89-
90-
if (!explicitURL) {
91-
this.watchGit();
92-
}
86+
this.watchGit();
9387
} catch (e) {
9488
throw Error(`Unable to start AEM: ${e.message}`);
9589
}
@@ -99,7 +93,7 @@ export default class UpCommand extends AbstractServerCommand {
9993
});
10094
}
10195

102-
async verifyUrl(gitUrl, inref) {
96+
async verifyUrl(gitUrl, ref) {
10397
// check if the site is on helix5
10498
// https://admin.hlx.page/sidekick/adobe/www-aem-live/main/config.json
10599
// {
@@ -116,7 +110,7 @@ export default class UpCommand extends AbstractServerCommand {
116110
// "project": "Helix Website (AEM Live)",
117111
// "testProperty": "header";
118112
// }
119-
const configUrl = `https://admin.hlx.page/sidekick/${gitUrl.owner}/${gitUrl.repo}/${inref}/config.json`;
113+
const configUrl = `https://admin.hlx.page/sidekick/${gitUrl.owner}/${gitUrl.repo}/main/config.json`;
120114
const configResp = await getFetch()(configUrl);
121115
let previewHostBase = 'hlx.page';
122116
if (configResp.ok) {
@@ -128,34 +122,17 @@ export default class UpCommand extends AbstractServerCommand {
128122
}
129123
}
130124

131-
let ref = inref;
132-
133-
// replace `/` by `-` in ref.
134-
ref = ref.replace(/\//g, '-');
135-
this._url = `https://${ref}--${gitUrl.repo}--${gitUrl.owner}.${previewHostBase}`;
125+
const dnsName = `${ref.replace(/\//g, '-')}--${gitUrl.repo}--${gitUrl.owner}`;
136126
// check length limit
137-
if (this._url.split('.')
138-
.map((part) => part.replace(/^https:\/\//, ''))
139-
.some((part) => part.length > 63)) {
140-
this.log.error(chalk`URL {yellow ${this._url}} exceeds the 63 character limit for DNS labels.`);
127+
if (dnsName.length > 63) {
128+
this.log.error(chalk`URL {yellow https://${dnsName}.${previewHostBase}} exceeds the 63 character limit for DNS labels.`);
141129
this.log.error(chalk`Please use a shorter branch name or a shorter repository name.`);
142130
await this.stop();
143131
throw Error('branch name too long');
144132
}
145133

146-
const fstabUrl = `${this._url}/fstab.yaml`;
147-
const resp = await getFetch()(fstabUrl);
148-
await resp.buffer();
149-
if (!resp.ok) {
150-
if (resp.status === 401) {
151-
this.log.warn(chalk`Unable to verify {yellow ${ref}} branch via {blue ${fstabUrl}} for authenticated sites.`);
152-
} else if (ref === 'main') {
153-
this.log.warn(chalk`Unable to verify {yellow main} branch via {blue ${fstabUrl}} (${resp.status}). Maybe not pushed yet?`);
154-
} else {
155-
this.log.warn(chalk`Unable to verify {yellow ${ref}} branch on {blue ${fstabUrl}} (${resp.status}). Fallback to {yellow main} branch.`);
156-
this._url = `https://main--${gitUrl.repo}--${gitUrl.owner}.${previewHostBase}`;
157-
}
158-
}
134+
// always proxy to main
135+
this._url = `https://main--${gitUrl.repo}--${gitUrl.owner}.${previewHostBase}`;
159136
}
160137

161138
/**
@@ -183,14 +160,17 @@ export default class UpCommand extends AbstractServerCommand {
183160
}
184161
try {
185162
// restart if any of the files is not ignored
186-
this.log.info('git HEAD or remotes changed, reconfiguring server...');
187163
const ref = await GitUtils.getBranch(this.directory);
188164
const gitUrl = await GitUtils.getOriginURL(this.directory, { ref });
189-
await this.verifyUrl(gitUrl, ref);
190-
this._project.withProxyUrl(this._url);
191-
await this._project.initHeadHtml();
192-
this.log.info(`Updated proxy to ${this._url}`);
193-
this.emit('changed', this);
165+
if (gitUrl.toString() !== this._gitUrl.toString()) {
166+
this.log.info('git HEAD or remotes changed, reconfiguring server...');
167+
this._gitUrl = gitUrl;
168+
await this.verifyUrl(gitUrl, ref);
169+
this._project.withProxyUrl(this._url);
170+
await this._project.initHeadHtml();
171+
this.log.info(`Updated proxy to ${this._url}`);
172+
this.emit('changed', this);
173+
}
194174
} catch {
195175
// ignore
196176
}

test/up-cmd.test.js

+14-88
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,14 @@ describe('Integration test for up command with helix pages', function suite() {
5757
.withOpen('/')
5858
.withHttpPort(0);
5959

60-
nock('https://master--dummy-foo--adobe.hlx.page')
61-
.get('/fstab.yaml')
62-
.reply(200, 'dummy')
60+
nock('https://main--dummy-foo--adobe.hlx.page')
6361
.get('/index.html')
6462
.reply(200, '## Welcome')
6563
.get('/not-found.txt')
6664
.reply(404);
6765

6866
nock('https://admin.hlx.page:443')
69-
.get('/sidekick/adobe/dummy-foo/master/config.json')
67+
.get('/sidekick/adobe/dummy-foo/main/config.json')
7068
.reply(404);
7169

7270
let port;
@@ -114,20 +112,18 @@ describe('Integration test for up command with helix pages', function suite() {
114112
.withOpen('/')
115113
.withHttpPort(0);
116114

117-
nock('https://master--dummy-foo--adobe.aem.page')
118-
.get('/fstab.yaml')
119-
.reply(200, 'dummy')
115+
nock('https://main--dummy-foo--adobe.aem.page')
120116
.get('/index.html')
121117
.reply(200, '## Welcome')
122118
.get('/not-found.txt')
123119
.reply(404);
124120

125121
nock('https://admin.hlx.page:443')
126-
.get('/sidekick/adobe/dummy-foo/master/config.json')
122+
.get('/sidekick/adobe/dummy-foo/main/config.json')
127123
.reply(200, {
128124
host: 'example.com',
129-
liveHost: 'master--dummy-foo--adobe.aem.live',
130-
previewHost: 'master--dummy-foo--adobe..aem.page',
125+
liveHost: 'main--dummy-foo--adobe.aem.live',
126+
previewHost: 'main--dummy-foo--adobe..aem.page',
131127
project: 'Example Project on Helix 5',
132128
testProperty: 'header',
133129
});
@@ -175,64 +171,14 @@ describe('Integration test for up command with helix pages', function suite() {
175171
return cmd.stop();
176172
};
177173

178-
nock('https://tripod-test--dummy-foo--adobe.hlx.page')
179-
.get('/fstab.yaml')
180-
.reply(200, 'dummy')
181-
.get('/index.html')
182-
.reply(200, '## Welcome')
183-
.get('/not-found.txt')
184-
.reply(404);
185-
186-
nock('https://admin.hlx.page:443')
187-
.get('/sidekick/adobe/dummy-foo/tripod/test/config.json')
188-
.reply(404);
189-
190-
cmd
191-
.on('started', async () => {
192-
try {
193-
let ret = await assertHttp(`http://127.0.0.1:${cmd.project.server.port}/index.html`, 200);
194-
assert.strictEqual(ret.trim(), '## Welcome');
195-
ret = await assertHttp(`http://127.0.0.1:${cmd.project.server.port}/local.txt`, 200);
196-
assert.strictEqual(ret.trim(), 'Hello, world.');
197-
await assertHttp(`http://127.0.0.1:${cmd.project.server.port}/not-found.txt`, 404);
198-
await myDone();
199-
} catch (e) {
200-
await myDone(e);
201-
}
202-
})
203-
.on('stopped', () => {
204-
done(error);
205-
})
206-
.run()
207-
.catch(done);
208-
});
209-
210-
it('up command switches to main branch if needed.', (done) => {
211-
initGit(testDir, 'https://github.com/adobe/dummy-foo.git');
212-
let error = null;
213-
const cmd = new UpCommand()
214-
.withLiveReload(false)
215-
.withDirectory(testDir)
216-
.withOpen(false)
217-
.withHttpPort(0);
218-
219-
const myDone = (err) => {
220-
error = err;
221-
return cmd.stop();
222-
};
223-
224174
nock('https://main--dummy-foo--adobe.hlx.page')
225175
.get('/index.html')
226176
.reply(200, '## Welcome')
227177
.get('/not-found.txt')
228178
.reply(404);
229179

230-
nock('https://master--dummy-foo--adobe.hlx.page')
231-
.get('/fstab.yaml')
232-
.reply(404, 'dummy');
233-
234180
nock('https://admin.hlx.page:443')
235-
.get('/sidekick/adobe/dummy-foo/master/config.json')
181+
.get('/sidekick/adobe/dummy-foo/main/config.json')
236182
.reply(404);
237183

238184
cmd
@@ -275,20 +221,9 @@ describe('Integration test for up command with helix pages', function suite() {
275221
.get('/not-found.txt')
276222
.reply(404);
277223

278-
nock('https://master--dummy-foo--adobe.hlx.page')
279-
.get('/fstab.yaml')
280-
.reply(404, 'dummy');
281-
282-
nock('https://new-branch--dummy-foo--adobe.hlx.page')
283-
.get('/fstab.yaml')
284-
.reply(200, 'yep!');
285-
286224
nock('https://admin.hlx.page:443')
287-
.get('/sidekick/adobe/dummy-foo/master/config.json')
288-
.reply(404);
289-
290-
nock('https://admin.hlx.page:443')
291-
.get('/sidekick/adobe/dummy-foo/new-branch/config.json')
225+
.get('/sidekick/adobe/dummy-foo/main/config.json')
226+
.twice()
292227
.reply(404);
293228

294229
let timer;
@@ -306,7 +241,7 @@ describe('Integration test for up command with helix pages', function suite() {
306241
timer = signal(5000);
307242
await timer;
308243
// eslint-disable-next-line no-underscore-dangle
309-
assert.strictEqual(cmd._url, 'https://new-branch--dummy-foo--adobe.hlx.page');
244+
assert.strictEqual(cmd._url, 'https://main--dummy-foo--adobe.hlx.page');
310245
await myDone();
311246
} catch (e) {
312247
await myDone(e);
@@ -336,16 +271,9 @@ describe('Integration test for up command with helix pages', function suite() {
336271
.get('/not-found.txt')
337272
.reply(404);
338273

339-
nock('https://master--dummy-foo--adobe.hlx.page')
340-
.get('/fstab.yaml')
341-
.reply(404, 'dummy');
342-
343-
nock('https://admin.hlx.page:443')
344-
.get('/sidekick/adobe/dummy-foo/master/config.json')
345-
.reply(404);
346-
347274
nock('https://admin.hlx.page:443')
348-
.get('/sidekick/adobe/dummy-foo/new-and-totally-unreasonably-long-in-fact-too-long-branch/config.json')
275+
.get('/sidekick/adobe/dummy-foo/main/config.json')
276+
.twice()
349277
.reply(404);
350278

351279
let timer;
@@ -411,9 +339,7 @@ describe('Integration test for up command with cache', function suite() {
411339
page2: '## Some different content for different qs',
412340
plain: 'Some plain content',
413341
};
414-
nock('https://master--dummy-foo--adobe.hlx.page')
415-
.get('/fstab.yaml')
416-
.reply(200, 'dummy')
342+
nock('https://main--dummy-foo--adobe.hlx.page')
417343
.get('/index.html')
418344
.reply(200, content.index)
419345
.get('/folder/page.html?foo=bar&baz=qux')
@@ -428,7 +354,7 @@ describe('Integration test for up command with cache', function suite() {
428354
.reply(200, '<link rel="stylesheet" href="/styles.css"/>');
429355

430356
nock('https://admin.hlx.page:443')
431-
.get('/sidekick/adobe/dummy-foo/master/config.json')
357+
.get('/sidekick/adobe/dummy-foo/main/config.json')
432358
.reply(404);
433359

434360
nock.enableNetConnect(/127.0.0.1/);

0 commit comments

Comments
 (0)