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
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);
});