function hapiWrapper() {
  return {
    pkg: {
      name: "webpack-dev-middleware",
    },
    // Allow to have multiple middleware
    multiple: true,
    register(server, options) {
      const { compiler, ...rest } = options;

      if (!compiler) {
        throw new Error("The compiler options is required.");
      }

      const devMiddleware = wdm(compiler, rest);

      // @ts-expect-error
      if (!server.decorations.server.includes("webpackDevMiddleware")) {
        // @ts-expect-error
        server.decorate("server", "webpackDevMiddleware", devMiddleware);
      }

      // @ts-expect-error
      // eslint-disable-next-line id-length
      server.ext("onRequest", (request, h) =>
        new Promise((resolve, reject) => {
          let isFinished = false;

          /**
           * @param {(string | Buffer)=} data
           */

          request.raw.res.send = (data) => {
            isFinished = true;
            request.raw.res.end(data);
          };

          /**
           * @param {(string | Buffer)=} data
           */

          request.raw.res.finish = (data) => {
            isFinished = true;
            request.raw.res.end(data);
          };

          devMiddleware(request.raw.req, request.raw.res, (error) => {
            if (error) {
              reject(error);
              return;
            }

            if (!isFinished) {
              resolve(request);
            }
          });
        })
          .then(() => h.continue)
          .catch((error) => {
            throw error;
          }),
      );
    },
  };
}
@template HapiServer
@template {HapiOptions} HapiOptionsInternal
@returns {HapiPlugin<HapiServer, HapiOptionsInternal>} hapi wrapper