All files / estuary-rpc-server multipart.ts

88% Statements 66/75
54.83% Branches 34/62
85.71% Functions 6/7
87.67% Lines 64/73

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  1x 1x 1x         1x 1x                         1x 11x   11x 11x 11x 11x   11x 11x                       11x 11x   11x 11x     11x 11x       18x 18x 17x 17x 17x   1x 1x 1x       32x 32x 32x 32x   32x 35x 17x 17x 17x 17x   18x 18x       32x 17x     17x   15x 4x       32x 32x     32x 17x               17x 17x 17x 17x                     32x 32x 32x 32x         19x     19x 19x 19x 19x 32x     19x         11x      
import { IncomingMessage } from "http";
import * as fs from "fs/promises";
import * as path from "path";
import * as os from "os";
 
import { SimpleFile } from "estuary-rpc";
 
const DISPOS_RX =
  /Content-Disposition: form-data; name="(\w+)"(?:; filename="([\w\.]+)")?/i;
const TYPE_RX = /Content-Type: ([\w\/-]+)/i;
 
async function openTempFile(name: string) {
  const tempPath = path.join(os.tmpdir(), "estuary-");
  const folder = await fs.mkdtemp(tempPath);
  return await fs.open(path.join(folder, name), "w");
}
 
/**
 * Class for parsing multipart/form-data encoded data
 *
 * @group Server
 */
export class MultiPartParser {
  private multipartData: Record<string, unknown> = {};
 
  private readingHeader: boolean = true;
  private boundary: string = "";
  private precedingPartialPart: string = "";
  private buffer: string = "";
  private file: fs.FileHandle | null;
  private simpleFile: SimpleFile = { content: "" };
  private terminus: string = "";
 
  private error: boolean;
 
  /**
   * @param req IncomingMessage that will be parsed in a series of chunks
   * @param persistence If true, files will be directly written to disk and returned with a file path.
   * Otherwise, files will be accumulated in memory into strings
   * @param rawStrings If true, non-file formdata will be returned as raw strings instead of parsed as JSON objects
   */
  constructor(
    req: IncomingMessage,
    public persistence?: boolean,
    public rawStrings?: boolean
  ) {
    const [_, boundary] = req.headers["content-type"]?.split("boundary=") ?? [];
    Iif (!boundary) {
      this.error = true;
    }
    this.boundary = `--${boundary}\r\n`;
    this.terminus = `--${boundary}--\r\n`;
  }
 
  private async parseHeaderLine(line: string) {
    const dispMatch = DISPOS_RX.exec(line);
    if (dispMatch) {
      this.simpleFile.name = dispMatch[1];
      this.simpleFile.contentType = dispMatch[2] && "application/text";
      return;
    }
    const contentTypeMatch = TYPE_RX.exec(line);
    if (contentTypeMatch) {
      this.simpleFile.contentType = contentTypeMatch[1];
    }
  }
  private async parseHeader(data: string) {
    let offset = -this.precedingPartialPart.length;
    this.precedingPartialPart += data;
    const lines = this.precedingPartialPart.split("\r\n");
    const allignedLines = lines[lines.length - 1] === "";
 
    for (let i = 0; i < lines.length - 1; i++) {
      if (lines[i] == "" && i < lines.length - 1) {
        offset += 2;
        this.readingHeader = false;
        this.precedingPartialPart = "";
        break;
      } else {
        offset += lines[i].length + 2;
        await this.parseHeaderLine(lines[i]);
      }
    }
 
    if (!this.readingHeader) {
      Iif (this.simpleFile.contentType && this.persistence) {
        this.file = await openTempFile(this.simpleFile.name ?? "wow");
      }
      return offset;
    }
    if (!allignedLines) {
      this.precedingPartialPart = lines[lines.length - 1];
    }
  }
  private async writePart(data: string) {
    const offset = this.readingHeader ? await this.parseHeader(data) : 0;
    const restOfContent = data.endsWith(this.terminus)
      ? data.slice(offset, -this.terminus.length)
      : data.slice(offset);
    if (this.simpleFile.name !== undefined) {
      Iif (this.persistence && this.simpleFile.contentType) {
        this.file?.write(restOfContent);
        this.file?.close();
        this.multipartData[this.simpleFile.name ?? ""] = this.simpleFile
          .contentType
          ? this.simpleFile
          : this.simpleFile.content;
      } else {
        try {
          this.simpleFile.content += restOfContent;
          this.simpleFile.content = this.simpleFile.content.trim();
          this.multipartData[this.simpleFile.name ?? ""] = this.simpleFile
            .contentType
            ? this.simpleFile
            : this.rawStrings
            ? this.simpleFile.content
            : JSON.parse(this.simpleFile.content || "null");
        } catch {
          // don't do anything
        }
      }
    }
    this.readingHeader = true;
    this.simpleFile = { content: "" };
    this.file = null;
    this.precedingPartialPart = "";
  }
 
  /** Parses a packet of information */
  async parse(data: string) {
    Iif (this.error) {
      return;
    }
    this.buffer += data;
    let currentParts = this.buffer.split(this.boundary);
    const allignedParts = currentParts[currentParts.length - 1] === "";
    for (let i = 0; i < currentParts.length; i++) {
      await this.writePart(currentParts[i]);
    }
 
    this.buffer = allignedParts ? "" : currentParts[currentParts.length - 1];
  }
 
  /** Returns the current Record<string, unknown> representing data-parsed so far */
  get() {
    return this.multipartData;
  }
}