All files / src/util extract.ts

17.17% Statements 34/198
100% Branches 0/0
0% Functions 0/2
17.17% Lines 34/198

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 1991x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                                                                                                                                                                                 1x 1x                          
/**
 * This file is based on the extract-zip package (see https://github.com/max-mapper/extract-zip)
 * The original code has been migrated to TypeScript, and the dependency get-stream has been removed.
 * The dependency yauzl has been updated to the latest version.
 */
 
import { createWriteStream, promises as fs } from "node:fs";
import path from "node:path";
import { Readable, pipeline as nodePipeline } from "node:stream";
import { promisify } from "node:util";
import yauzl, { Entry, ZipFile } from "yauzl";
import { ensureError } from "./misc.js";
import { Response } from "undici";
 
const openZip = promisify(yauzl.open) as (
  zipPath: string,
  options: { lazyEntries: true },
) => Promise<ZipFile>;
 
const pipeline = promisify(nodePipeline) as (
  source: Readable,
  destination: NodeJS.WritableStream,
) => Promise<void>;
 
export interface ExtractOptions {
  dir: string;
  defaultDirMode?: string | number;
  defaultFileMode?: string | number;
  onEntry?: (entry: Entry, zipfile: ZipFile) => void;
}
 
class Extractor {
  private zipfile!: ZipFile;
  private canceled = false;

  constructor(
    private readonly zipPath: string,
    private readonly opts: ExtractOptions,
  ) {}

  async extract(): Promise<void> {
    this.zipfile = await openZip(this.zipPath, { lazyEntries: true });
    this.canceled = false;

    return new Promise<void>((resolve, reject) => {
      this.zipfile.on("error", (error: Error) => {
        this.canceled = true;
        reject(error);
      });

      this.zipfile.readEntry();

      this.zipfile.on("close", () => {
        if (!this.canceled) {
          resolve();
        }
      });

      // eslint-disable-next-line @typescript-eslint/no-misused-promises
      this.zipfile.on("entry", async (entry: Entry) => {
        /* istanbul ignore if */
        if (this.canceled) {
          return;
        }

        if (entry.fileName.startsWith("__MACOSX/")) {
          this.zipfile.readEntry();
          return;
        }

        const destDir = path.dirname(path.join(this.opts.dir, entry.fileName));

        try {
          await fs.mkdir(destDir, { recursive: true });

          const canonicalDestDir = await fs.realpath(destDir);
          const relativeDestDir = path.relative(
            this.opts.dir,
            canonicalDestDir,
          );

          if (relativeDestDir.split(path.sep).includes("..")) {
            throw new Error(
              `Out of bound path "${canonicalDestDir}" found while processing file ${entry.fileName}`,
            );
          }

          await this.extractEntry(entry);
          this.zipfile.readEntry();
        } catch (error) {
          this.canceled = true;
          this.zipfile.close();
          reject(ensureError(error));
        }
      });
    });
  }

  private async extractEntry(entry: Entry): Promise<void> {
    /* istanbul ignore if */
    if (this.canceled) {
      return;
    }

    if (this.opts.onEntry) {
      this.opts.onEntry(entry, this.zipfile);
    }

    const dest = path.join(this.opts.dir, entry.fileName);

    const mode = (entry.externalFileAttributes >> 16) & 0xffff;
    const IFMT = 0o170000;
    const IFDIR = 0o040000;
    const IFLNK = 0o120000;

    const symlink = (mode & IFMT) === IFLNK;
    let isDir = (mode & IFMT) === IFDIR;

    if (!isDir && entry.fileName.endsWith("/")) {
      isDir = true;
    }

    const madeBy = entry.versionMadeBy >> 8;
    if (!isDir) {
      isDir = madeBy === 0 && entry.externalFileAttributes === 16;
    }

    const procMode = this.getExtractedMode(mode, isDir) & 0o777;
    const destDir = isDir ? dest : path.dirname(dest);

    const mkdirOptions: { recursive: true; mode?: number } = {
      recursive: true,
    };
    if (isDir) {
      mkdirOptions.mode = procMode;
    }

    await fs.mkdir(destDir, mkdirOptions);

    if (isDir) {
      return;
    }

    const openReadStream = promisify(
      this.zipfile.openReadStream.bind(this.zipfile),
    );

    const readStream = await openReadStream(entry);

    if (symlink) {
      const link = await new Response(readStream).text();
      await fs.symlink(link, dest);
      return;
    }

    await pipeline(readStream, createWriteStream(dest, { mode: procMode }));
  }

  private getExtractedMode(entryMode: number, isDir: boolean): number {
    let mode = entryMode;

    if (mode === 0) {
      if (isDir) {
        if (this.opts.defaultDirMode !== undefined) {
          mode = Number.parseInt(String(this.opts.defaultDirMode), 10);
        }

        if (!mode) {
          mode = 0o755;
        }
      } else {
        if (this.opts.defaultFileMode !== undefined) {
          mode = Number.parseInt(String(this.opts.defaultFileMode), 10);
        }

        if (!mode) {
          mode = 0o644;
        }
      }
    }

    return mode;
  }
}
 
export default async function extract(
  zipPath: string,
  opts: ExtractOptions,
): Promise<void> {
  if (!path.isAbsolute(opts.dir)) {
    throw new Error("Target directory is expected to be absolute");
  }

  await fs.mkdir(opts.dir, { recursive: true });
  opts.dir = await fs.realpath(opts.dir);

  return new Extractor(zipPath, opts).extract();
}