text: import { createReadStream, createWriteStream, existsSync, statSync, writeFileSync } from 'fs';
  import { pipeline } from 'stream';
  import glob from 'tiny-glob';
  import { fileURLToPath } from 'url';
  import { promisify } from 'util';
  import zlib from 'zlib';
  
  const pipe = promisify(pipeline);
  
  const files = fileURLToPath(new URL('./files', import.meta.url).href);
  
  /** @type {import('.')} */
  export default function ({
  	out = 'build',
  	precompress,
  	env: {
  		path: path_env = 'SOCKET_PATH',
  		host: host_env = 'HOST',
  		port: port_env = 'PORT',
  		origin: origin_env = 'ORIGIN',
  		headers: {
  			protocol: protocol_header_env = 'PROTOCOL_HEADER',
  			host: host_header_env = 'HOST_HEADER'
  		} = {}
  	} = {}
  } = {}) {
  	return {
  		name: 'ramiroaisen/svelte-adapter-express',
  
  		async adapt(builder) {
  			builder.rimraf(`${out}/server`);
  
  			builder.log.minor('Copying assets');
  			builder.writeClient(`${out}/client`);
  			builder.writeServer(`${out}/server`);
  			builder.writeStatic(`${out}/static`);
  			builder.writePrerendered(`${out}/prerendered`);
  
  			writeFileSync(
  				`${out}/manifest.js`,
  				`export const manifest = ${builder.generateManifest({
  					relativePath: './server'
  				})};\n`
  			);
  
  			builder.copy(files, out, {
  				replace: {
  					SERVER: './server/index.js',
  					MANIFEST: './manifest.js',
  					PATH_ENV: JSON.stringify(path_env),
  					HOST_ENV: JSON.stringify(host_env),
  					PORT_ENV: JSON.stringify(port_env),
  					ORIGIN: origin_env ? `process.env[${JSON.stringify(origin_env)}]` : 'undefined',
  					PROTOCOL_HEADER: JSON.stringify(protocol_header_env),
  					HOST_HEADER: JSON.stringify(host_header_env)
  				}
  			});
  
  			if (precompress) {
  				builder.log.minor('Compressing assets');
  				await compress(`${out}/client`);
  				await compress(`${out}/static`);
  				await compress(`${out}/prerendered`);
  			}
  		}
  	};
  }
  
  /**
   * @param {string} directory
   */
  async function compress(directory) {
  	if (!existsSync(directory)) {
  		return;
  	}
  
  	const files = await glob('**/*.{html,js,json,css,svg,xml,wasm}', {
  		cwd: directory,
  		dot: true,
  		absolute: true,
  		filesOnly: true
  	});
  
  	await Promise.all(
  		files.map((file) => Promise.all([compress_file(file, 'gz'), compress_file(file, 'br')]))
  	);
  }
  
  /**
   * @param {string} file
   * @param {'gz' | 'br'} format
   */
  async function compress_file(file, format = 'gz') {
  	const compress =
  		format == 'br'
  			? zlib.createBrotliCompress({
  					params: {
  						[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
  						[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
  						[zlib.constants.BROTLI_PARAM_SIZE_HINT]: statSync(file).size
  					}
  			  })
  			: zlib.createGzip({ level: zlib.constants.Z_BEST_COMPRESSION });
  
  	const source = createReadStream(file);
  	const destination = createWriteStream(`${file}.${format}`);
  
  	await pipe(source, compress, destination);
  }
  