diff --git a/lib/create-server.mjs b/lib/create-server.mjs index 1e9b82229..efae0a242 100644 --- a/lib/create-server.mjs +++ b/lib/create-server.mjs @@ -103,21 +103,32 @@ function createServer (argv, app) { // Wrap server.listen() to ensure async initialization completes after server starts const originalListen = server.listen.bind(server) server.listen = function (...args) { - // Start listening first - originalListen(...args) + const lastArg = args[args.length - 1] + const hasCallback = typeof lastArg === 'function' + const readyCallback = hasCallback ? lastArg : null - // Then run async initialization (if needed) - if (ldpApp.locals.initFunction) { + if (hasCallback) { + args = args.slice(0, -1) + } + + originalListen(...args, () => { const initFunction = ldpApp.locals.initFunction delete ldpApp.locals.initFunction - // Run initialization after server is listening + if (!initFunction) { + if (readyCallback) readyCallback() + return + } + initFunction() + .then(() => { + if (readyCallback) readyCallback() + }) .catch(err => { console.error('Initialization error:', err) server.emit('error', err) }) - } + }) return server } diff --git a/test/integration/www-account-creation-oidc-test.mjs b/test/integration/www-account-creation-oidc-test.mjs index 9ef4c61f5..b52c8d03c 100644 --- a/test/integration/www-account-creation-oidc-test.mjs +++ b/test/integration/www-account-creation-oidc-test.mjs @@ -227,14 +227,13 @@ describe('AccountManager (OIDC account creation tests)', function () { }) // FIXME: #1502 -describe('Single User signup page', () => { - const serverUri = 'https://localhost:7457' - const port = 7457 - let ldpHttpsServer - rm('resources/accounts/single-user/') - const rootDir = path.normalize(path.join(__dirname, '../resources/accounts/single-user/')) - const configPath = path.normalize(path.join(__dirname, '../resources/config')) - const ldp = ldnode.createServer({ +describe('Single User signup page', () => { + const serverUri = 'https://localhost:7457' + const port = 7457 + let ldpHttpsServer + const rootDir = path.normalize(path.join(__dirname, '../resources/accounts/single-user/')) + const configPath = path.normalize(path.join(__dirname, '../resources/config')) + const ldp = ldnode.createServer({ port, root: rootDir, configPath, @@ -244,12 +243,13 @@ describe('Single User signup page', () => { multiuser: false, strictOrigin: true }) - const server = supertest(serverUri) - - before(function (done) { - ldpHttpsServer = ldp.listen(port, () => server.post('/api/accounts/new') - .send('username=foo&password=12345&acceptToc=true') - .end(done)) + const server = supertest(serverUri) + + before(function (done) { + fs.removeSync(rootDir) + ldpHttpsServer = ldp.listen(port, () => server.post('/api/accounts/new') + .send('username=foo&password=12345&acceptToc=true') + .end(done)) }) after(function () {