Skip to content

Commit

Permalink
feat: always proxy from main branch by default
Browse files Browse the repository at this point in the history
fixes #2331

fixes #2328

fixes #2219
  • Loading branch information
tripodsan committed Mar 19, 2024
1 parent 2e4dc1b commit f166662
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 127 deletions.
58 changes: 19 additions & 39 deletions src/up.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,16 @@ export default class UpCommand extends AbstractServerCommand {
this.log.info('');

const ref = await GitUtils.getBranch(this.directory);
const gitUrl = await GitUtils.getOriginURL(this.directory, { ref });
let explicitURL = true;
this._gitUrl = await GitUtils.getOriginURL(this.directory, { ref });
if (!this._url) {
explicitURL = false;
// check if remote already has the `ref`
await this.verifyUrl(gitUrl, ref);
await this.verifyUrl(this._gitUrl, ref);
}
this._project.withProxyUrl(this._url);
await this.initSeverOptions();

try {
await this._project.init();

if (!explicitURL) {
this.watchGit();
}
this.watchGit();
} catch (e) {
throw Error(`Unable to start AEM: ${e.message}`);
}
Expand All @@ -99,7 +93,7 @@ export default class UpCommand extends AbstractServerCommand {
});
}

async verifyUrl(gitUrl, inref) {
async verifyUrl(gitUrl, ref) {
// check if the site is on helix5
// https://admin.hlx.page/sidekick/adobe/www-aem-live/main/config.json
// {
Expand All @@ -116,7 +110,7 @@ export default class UpCommand extends AbstractServerCommand {
// "project": "Helix Website (AEM Live)",
// "testProperty": "header";
// }
const configUrl = `https://admin.hlx.page/sidekick/${gitUrl.owner}/${gitUrl.repo}/${inref}/config.json`;
const configUrl = `https://admin.hlx.page/sidekick/${gitUrl.owner}/${gitUrl.repo}/main/config.json`;
const configResp = await getFetch()(configUrl);
let previewHostBase = 'hlx.page';
if (configResp.ok) {
Expand All @@ -128,34 +122,17 @@ export default class UpCommand extends AbstractServerCommand {
}
}

let ref = inref;

// replace `/` by `-` in ref.
ref = ref.replace(/\//g, '-');
this._url = `https://${ref}--${gitUrl.repo}--${gitUrl.owner}.${previewHostBase}`;
const dnsName = `${ref.replace(/\//g, '-')}--${gitUrl.repo}--${gitUrl.owner}`;
// check length limit
if (this._url.split('.')
.map((part) => part.replace(/^https:\/\//, ''))
.some((part) => part.length > 63)) {
this.log.error(chalk`URL {yellow ${this._url}} exceeds the 63 character limit for DNS labels.`);
if (dnsName.length > 63) {
this.log.error(chalk`URL {yellow https://${dnsName}.${previewHostBase}} exceeds the 63 character limit for DNS labels.`);
this.log.error(chalk`Please use a shorter branch name or a shorter repository name.`);
await this.stop();
throw Error('branch name too long');
}

const fstabUrl = `${this._url}/fstab.yaml`;
const resp = await getFetch()(fstabUrl);
await resp.buffer();
if (!resp.ok) {
if (resp.status === 401) {
this.log.warn(chalk`Unable to verify {yellow ${ref}} branch via {blue ${fstabUrl}} for authenticated sites.`);
} else if (ref === 'main') {
this.log.warn(chalk`Unable to verify {yellow main} branch via {blue ${fstabUrl}} (${resp.status}). Maybe not pushed yet?`);
} else {
this.log.warn(chalk`Unable to verify {yellow ${ref}} branch on {blue ${fstabUrl}} (${resp.status}). Fallback to {yellow main} branch.`);
this._url = `https://main--${gitUrl.repo}--${gitUrl.owner}.${previewHostBase}`;
}
}
// always proxy to main
this._url = `https://main--${gitUrl.repo}--${gitUrl.owner}.${previewHostBase}`;
}

/**
Expand Down Expand Up @@ -183,14 +160,17 @@ export default class UpCommand extends AbstractServerCommand {
}
try {
// restart if any of the files is not ignored
this.log.info('git HEAD or remotes changed, reconfiguring server...');
const ref = await GitUtils.getBranch(this.directory);
const gitUrl = await GitUtils.getOriginURL(this.directory, { ref });
await this.verifyUrl(gitUrl, ref);
this._project.withProxyUrl(this._url);
await this._project.initHeadHtml();
this.log.info(`Updated proxy to ${this._url}`);
this.emit('changed', this);
if (gitUrl.toString() !== this._gitUrl.toString()) {
this.log.info('git HEAD or remotes changed, reconfiguring server...');
this._gitUrl = gitUrl;
await this.verifyUrl(gitUrl, ref);
this._project.withProxyUrl(this._url);
await this._project.initHeadHtml();
this.log.info(`Updated proxy to ${this._url}`);
this.emit('changed', this);
}
} catch {
// ignore
}
Expand Down
102 changes: 14 additions & 88 deletions test/up-cmd.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,14 @@ describe('Integration test for up command with helix pages', function suite() {
.withOpen('/')
.withHttpPort(0);

nock('https://master--dummy-foo--adobe.hlx.page')
.get('/fstab.yaml')
.reply(200, 'dummy')
nock('https://main--dummy-foo--adobe.hlx.page')
.get('/index.html')
.reply(200, '## Welcome')
.get('/not-found.txt')
.reply(404);

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

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

nock('https://master--dummy-foo--adobe.aem.page')
.get('/fstab.yaml')
.reply(200, 'dummy')
nock('https://main--dummy-foo--adobe.aem.page')
.get('/index.html')
.reply(200, '## Welcome')
.get('/not-found.txt')
.reply(404);

nock('https://admin.hlx.page:443')
.get('/sidekick/adobe/dummy-foo/master/config.json')
.get('/sidekick/adobe/dummy-foo/main/config.json')
.reply(200, {
host: 'example.com',
liveHost: 'master--dummy-foo--adobe.aem.live',
previewHost: 'master--dummy-foo--adobe..aem.page',
liveHost: 'main--dummy-foo--adobe.aem.live',
previewHost: 'main--dummy-foo--adobe..aem.page',
project: 'Example Project on Helix 5',
testProperty: 'header',
});
Expand Down Expand Up @@ -175,64 +171,14 @@ describe('Integration test for up command with helix pages', function suite() {
return cmd.stop();
};

nock('https://tripod-test--dummy-foo--adobe.hlx.page')
.get('/fstab.yaml')
.reply(200, 'dummy')
.get('/index.html')
.reply(200, '## Welcome')
.get('/not-found.txt')
.reply(404);

nock('https://admin.hlx.page:443')
.get('/sidekick/adobe/dummy-foo/tripod/test/config.json')
.reply(404);

cmd
.on('started', async () => {
try {
let ret = await assertHttp(`http://127.0.0.1:${cmd.project.server.port}/index.html`, 200);
assert.strictEqual(ret.trim(), '## Welcome');
ret = await assertHttp(`http://127.0.0.1:${cmd.project.server.port}/local.txt`, 200);
assert.strictEqual(ret.trim(), 'Hello, world.');
await assertHttp(`http://127.0.0.1:${cmd.project.server.port}/not-found.txt`, 404);
await myDone();
} catch (e) {
await myDone(e);
}
})
.on('stopped', () => {
done(error);
})
.run()
.catch(done);
});

it('up command switches to main branch if needed.', (done) => {
initGit(testDir, 'https://github.com/adobe/dummy-foo.git');
let error = null;
const cmd = new UpCommand()
.withLiveReload(false)
.withDirectory(testDir)
.withOpen(false)
.withHttpPort(0);

const myDone = (err) => {
error = err;
return cmd.stop();
};

nock('https://main--dummy-foo--adobe.hlx.page')
.get('/index.html')
.reply(200, '## Welcome')
.get('/not-found.txt')
.reply(404);

nock('https://master--dummy-foo--adobe.hlx.page')
.get('/fstab.yaml')
.reply(404, 'dummy');

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

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

nock('https://master--dummy-foo--adobe.hlx.page')
.get('/fstab.yaml')
.reply(404, 'dummy');

nock('https://new-branch--dummy-foo--adobe.hlx.page')
.get('/fstab.yaml')
.reply(200, 'yep!');

nock('https://admin.hlx.page:443')
.get('/sidekick/adobe/dummy-foo/master/config.json')
.reply(404);

nock('https://admin.hlx.page:443')
.get('/sidekick/adobe/dummy-foo/new-branch/config.json')
.get('/sidekick/adobe/dummy-foo/main/config.json')
.twice()
.reply(404);

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

nock('https://master--dummy-foo--adobe.hlx.page')
.get('/fstab.yaml')
.reply(404, 'dummy');

nock('https://admin.hlx.page:443')
.get('/sidekick/adobe/dummy-foo/master/config.json')
.reply(404);

nock('https://admin.hlx.page:443')
.get('/sidekick/adobe/dummy-foo/new-and-totally-unreasonably-long-in-fact-too-long-branch/config.json')
.get('/sidekick/adobe/dummy-foo/main/config.json')
.twice()
.reply(404);

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

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

nock.enableNetConnect(/127.0.0.1/);
Expand Down

0 comments on commit f166662

Please sign in to comment.