Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async createMiddleware() {
// The compiler is necessary to build the fallback filesystem
// so UPWARd can use Webpack-generated assets in dev mode.
const compiler = await this.getCompiler();
// Standard filesystem-and-fetch IO.
const defaultIO = upward.IOAdapter.default(this.upwardPath);
// Use Webpack's in-memory file system for UPWARD file retrieval during
// development. Allows for hot reloading of server-side configuration.
const io = {
async readFile(filepath, enc = 'utf8') {
// If a version of the file is output by Webpack into the output folder, prefer that one.
const outputBasedPath = path.resolve(
compiler.options.output.path,
path.relative(compiler.options.context, filepath)
);
// Most likely scenario: UPWARD needs an output asset.
debug(
'compiler.outputFileSystem readFile %s %s',
outputBasedPath,
enc
async networkFetch(path, options) {
debug('networkFetch %s, %o', path, options);
const { protocol } = new url.URL(path);
if (protocol === 'https:') {
return fetch(
path,
Object.assign({ agent: httpsAgent }, options)
);
}
return fetch(path, options);
// Use the https.Agent to allow self-signed certificates.
}
};
this.middleware = await upward.middleware(
this.upwardPath,
this.env,
io
);
}
async getCompiler() {
upward.middleware.mockResolvedValueOnce(upwardHandler);
const noRequestsWaiting = new UpwardDevServerPlugin({}, process.env);
noRequestsWaiting.apply(compiler);
expect(noRequestsWaiting.compiler).toBe(compiler);
const hasRequestsWaiting = new UpwardDevServerPlugin(
devServer,
process.env,
'path/to/upward'
);
devServer.after(app);
const handler = app.use.mock.calls[0][0];
handler(req, res, next);
expect(upward.IOAdapter.default).not.toHaveBeenCalled();
expect(upward.middleware).not.toHaveBeenCalled();
expect(hasRequestsWaiting.middlewarePromise).toBeInstanceOf(Promise);
hasRequestsWaiting.apply(compiler);
await hasRequestsWaiting.middlewarePromise;
expect(upward.IOAdapter.default).toHaveBeenCalledWith('path/to/upward');
expect(upward.middleware).toHaveBeenCalledWith(
'path/to/upward',
process.env,
expect.objectContaining({
readFile: expect.any(Function),
networkFetch: expect.any(Function)
})
);
expect(upwardHandler).toHaveBeenCalledWith(req, res, next);
devServer,
process.env,
'path/to/upward'
);
devServer.after(app);
const handler = app.use.mock.calls[0][0];
handler(req, res, next);
expect(upward.IOAdapter.default).not.toHaveBeenCalled();
expect(upward.middleware).not.toHaveBeenCalled();
expect(hasRequestsWaiting.middlewarePromise).toBeInstanceOf(Promise);
hasRequestsWaiting.apply(compiler);
await hasRequestsWaiting.middlewarePromise;
expect(upward.IOAdapter.default).toHaveBeenCalledWith('path/to/upward');
expect(upward.middleware).toHaveBeenCalledWith(
'path/to/upward',
process.env,
expect.objectContaining({
readFile: expect.any(Function),
networkFetch: expect.any(Function)
})
);
expect(upwardHandler).toHaveBeenCalledWith(req, res, next);
});
dir: __dirname,
interactive: false
})
);
upwardServerOptions.host = hostname;
upwardServerOptions.https = ssl;
upwardServerOptions.port = envPort || ports.staging || 0;
} catch (e) {
console.log(
'Could not configure or access custom host. Using loopback...',
e.message
);
}
}
console.log('Launching UPWARD server\n');
await createUpwardServer(upwardServerOptions);
console.log('\nUPWARD server running.');
}
async function serve() {
const config = loadEnvironment(__dirname);
const upwardServerOptions = Object.assign(
{},
config.section('upwardJs'),
config.section('stagingServer'),
{
env: process.env,
before: app => {
app.use(bestPractices());
}
}
);
const server = await createUpwardServer(upwardServerOptions);
return server;
}
before: app => {
app.use(bestPractices());
}
}
before(app) {
addImgOptMiddleware(
app,
Object.assign(
config.section('magento'),
config.section('imageService')
)
);
app.use(bestPractices());
}
}
devServer.before = (app, ...rest) => {
app.use(upward.bestPractices());
if (oldBefore) oldBefore(app, ...rest);
};
const oldAfter = devServer.after;