"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cli_core_1 = require("@bluebase/cli-core");
const fs_1 = require("fs");
/**
 * Middleware to intercept calls to our offline page to ensure that
 * inline scripts get a nonce value attached to them.
 */
const offlinePageMiddleware = (configs) => (_req, res, _next) => {
    // We should have had a nonce provided to us.  See the server/index.js for
    // more information on what this is.
    if (typeof res.locals.nonce !== 'string') {
        throw new Error('A "nonce" value has not been attached to the response');
    }
    const nonce = res.locals.nonce;
    fs_1.readFile(
    // Path to the offline page.
    cli_core_1.Utils.fromProjectRoot(`./${configs.client.outputPath}/${configs.server.serviceWorker.offlinePageFileName}`), 
    // Charset for read
    'utf-8', 
    // Read handler
    (err, data) => {
        if (err) {
            res.status(500).send('Error returning offline page.');
            return;
            // We replace the placeholder with the actual nonce.
            const offlinePageWithNonce = data.replace('OFFLINE_PAGE_NONCE_PLACEHOLDER', nonce);
            // Send back the page as the response
            res.send(offlinePageWithNonce);
        }
    });
};
exports.default = offlinePageMiddleware;
//# sourceMappingURL=offlinePage.js.map