jaydenseric / graphql-upload

Middleware and an Upload scalar to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.
https://npm.im/graphql-upload
MIT License
1.43k stars 131 forks source link

How to properly get file buffer and its byte size in NestJS Typescript? #312

Closed vfa-locltb closed 2 years ago

vfa-locltb commented 2 years ago

I followed the tutorials online and the issue section on this page about ways to get file buffer and size but did not succeed. I've tried the method:

const stream = createReadStream();
stream.on('data', (chunk: Buffer) => {
    console.log('Begin'); // This was never printed on the console
    size += chunk.byteLength;
    buffer = chunk;
    console.log('Success'); // This was never printed on the console
});

But it seems like the code can't even reach what's inside stream.on(); I tried to console.log(stream), and here's what it returns:

<ref *1> ReadStream {
  fd: null,
  path: 'C:\\Users\\<my_user_name>\\AppData\\Local\\Temp\\capacitor-89ead9d5c882ac28ca55150803191ca5.tmp',
  flags: 'r',
  mode: 438,
  start: undefined,
  end: Infinity,
  pos: undefined,
  bytesRead: 0,
  closed: false,
  _readableState: ReadableState {
    objectMode: false,
    highWaterMark: 65536,
    buffer: BufferList { head: null, tail: null, length: 0 },
    length: 0,
    pipes: [],
    flowing: null,
    ended: false,
    endEmitted: false,
    reading: false,
    constructed: false,
    sync: true,
    needReadable: false,
    emittedReadable: false,
    readableListening: false,
    resumeScheduled: false,
    errorEmitted: false,
    emitClose: true,
    autoDestroy: true,
    destroyed: false,
    errored: null,
    closed: false,
    closeEmitted: false,
    defaultEncoding: 'utf8',
    awaitDrainWriters: null,
    multiAwaitDrain: false,
    readingMore: false,
    dataEmitted: false,
    decoder: null,
    encoding: null,
    [Symbol(kPaused)]: null
  },
  _events: [Object: null prototype] {
    error: [Function (anonymous)],
    end: [Function: remove],
    close: [Function: remove],
    [Symbol(kConstruct)]: [Function: bound onceWrapper] { listener: [Function (anonymous)] }
  },
  _eventsCount: 4,
  _maxListeners: undefined,
  name: undefined,
  _writeStream: WriteStream {
    fd: 3,
    path: 'C:\\Users\\<my_user_name>\\AppData\\Local\\Temp\\capacitor-89ead9d5c882ac28ca55150803191ca5.tmp',
    flags: 'w',
    mode: 438,
    start: undefined,
    pos: undefined,
    bytesWritten: 130461,
    closed: false,
    _writableState: WritableState {
      objectMode: false,
      highWaterMark: 16384,
      finalCalled: false,
      needDrain: true,
      ending: false,
      ended: false,
      finished: false,
      destroyed: false,
      decodeStrings: true,
      defaultEncoding: 'utf8',
      length: 65536,
      writing: true,
      corked: 0,
      sync: false,
      bufferProcessing: false,
      onwrite: [Function: bound onwrite],
      writecb: [Function: nop],
      writelen: 65536,
      afterWriteTickInfo: null,
      buffered: [],
      bufferedIndex: 0,
      allBuffers: true,
      allNoop: true,
      pendingcb: 1,
      constructed: true,
      prefinished: false,
      errorEmitted: false,
      emitClose: true,
      autoDestroy: false,
      errored: null,
      closed: false,
      closeEmitted: false,
      [Symbol(kOnFinished)]: []
    },
    _events: [Object: null prototype] {
      error: [Array],
      unpipe: [Function: onunpipe],
      close: [Function],
      finish: [Function],
      drain: [Function: pipeOnDrainFunctionResult]
    },
    _eventsCount: 5,
    _maxListeners: undefined,
    _readStreams: Set(1) { [Circular *1] },
    error: null,
    _cleanupSync: [Function (anonymous)],
    emit: [Function: emit],
    [Symbol(kFs)]: {
      appendFile: [Function: appendFile],
      appendFileSync: [Function: appendFileSync],
      access: [Function: access],
      accessSync: [Function: accessSync],
      chown: [Function: chown],
      chownSync: [Function: chownSync],
      chmod: [Function: chmod],
      chmodSync: [Function: chmodSync],
      close: [Function: close],
      closeSync: [Function: closeSync],
      copyFile: [Function: copyFile],
      copyFileSync: [Function: copyFileSync],
      cp: [Function: cp],
      cpSync: [Function: cpSync],
      createReadStream: [Function: createReadStream],
      createWriteStream: [Function: createWriteStream],
      exists: [Function: exists],
      existsSync: [Function: existsSync],
      fchown: [Function: fchown],
      fchownSync: [Function: fchownSync],
      fchmod: [Function: fchmod],
      fchmodSync: [Function: fchmodSync],
      fdatasync: [Function: fdatasync],
      fdatasyncSync: [Function: fdatasyncSync],
      fstat: [Function: fstat],
      fstatSync: [Function: fstatSync],
      fsync: [Function: fsync],
      fsyncSync: [Function: fsyncSync],
      ftruncate: [Function: ftruncate],
      ftruncateSync: [Function: ftruncateSync],
      futimes: [Function: futimes],
      futimesSync: [Function: futimesSync],
      lchown: [Function: lchown],
      lchownSync: [Function: lchownSync],
      lchmod: undefined,
      lchmodSync: undefined,
      link: [Function: link],
      linkSync: [Function: linkSync],
      lstat: [Function: lstat],
      lstatSync: [Function: lstatSync],
      lutimes: [Function: lutimes],
      lutimesSync: [Function: lutimesSync],
      mkdir: [Function: mkdir],
      mkdirSync: [Function: mkdirSync],
      mkdtemp: [Function: mkdtemp],
      mkdtempSync: [Function: mkdtempSync],
      open: [Function: open],
      openSync: [Function: openSync],
      opendir: [Function: opendir],
      opendirSync: [Function: opendirSync],
      readdir: [Function: readdir],
      readdirSync: [Function: readdirSync],
      read: [Function: read],
      readSync: [Function: readSync],
      readv: [Function: readv],
      readvSync: [Function: readvSync],
      readFile: [Function: readFile],
      readFileSync: [Function: readFileSync],
      readlink: [Function: readlink],
      readlinkSync: [Function: readlinkSync],
      realpath: [Function],
      realpathSync: [Function],
      rename: [Function: rename],
      renameSync: [Function: renameSync],
      rm: [Function: rm],
      rmSync: [Function: rmSync],
      rmdir: [Function: rmdir],
      rmdirSync: [Function: rmdirSync],
      stat: [Function: stat],
      statSync: [Function: statSync],
      symlink: [Function: symlink],
      symlinkSync: [Function: symlinkSync],
      truncate: [Function: truncate],
      truncateSync: [Function: truncateSync],
      unwatchFile: [Function: unwatchFile],
      unlink: [Function: unlink],
      unlinkSync: [Function: unlinkSync],
      utimes: [Function: utimes],
      utimesSync: [Function: utimesSync],
      watch: [Function: watch],
      watchFile: [Function: watchFile],
      writeFile: [Function: writeFile],
      writeFileSync: [Function: writeFileSync],
      write: [Function: write],
      writeSync: [Function: writeSync],
      writev: [Function: writev],
      writevSync: [Function: writevSync],
      Dir: [class Dir],
      Dirent: [class Dirent],
      Stats: [Function: Stats],
      ReadStream: [Getter/Setter],
      WriteStream: [Getter/Setter],
      FileReadStream: [Getter/Setter],
      FileWriteStream: [Getter/Setter],
      _toUnixTimestamp: [Function: toUnixTimestamp],
      F_OK: 0,
      R_OK: 4,
      W_OK: 2,
      X_OK: 1,
      constants: [Object: null prototype],
      promises: [Getter]
    },
    [Symbol(kIsPerformingIO)]: true,
    [Symbol(kCapture)]: false
  },
  error: null,
  [Symbol(kFs)]: {
    appendFile: [Function: appendFile],
    appendFileSync: [Function: appendFileSync],
    access: [Function: access],
    accessSync: [Function: accessSync],
    chown: [Function: chown],
    chownSync: [Function: chownSync],
    chmod: [Function: chmod],
    chmodSync: [Function: chmodSync],
    close: [Function: close],
    closeSync: [Function: closeSync],
    copyFile: [Function: copyFile],
    copyFileSync: [Function: copyFileSync],
    cp: [Function: cp],
    cpSync: [Function: cpSync],
    createReadStream: [Function: createReadStream],
    createWriteStream: [Function: createWriteStream],
    exists: [Function: exists],
    existsSync: [Function: existsSync],
    fchown: [Function: fchown],
    fchownSync: [Function: fchownSync],
    fchmod: [Function: fchmod],
    fchmodSync: [Function: fchmodSync],
    fdatasync: [Function: fdatasync],
    fdatasyncSync: [Function: fdatasyncSync],
    fstat: [Function: fstat],
    fstatSync: [Function: fstatSync],
    fsync: [Function: fsync],
    fsyncSync: [Function: fsyncSync],
    ftruncate: [Function: ftruncate],
    ftruncateSync: [Function: ftruncateSync],
    futimes: [Function: futimes],
    futimesSync: [Function: futimesSync],
    lchown: [Function: lchown],
    lchownSync: [Function: lchownSync],
    lchmod: undefined,
    lchmodSync: undefined,
    link: [Function: link],
    linkSync: [Function: linkSync],
    lstat: [Function: lstat],
    lstatSync: [Function: lstatSync],
    lutimes: [Function: lutimes],
    lutimesSync: [Function: lutimesSync],
    mkdir: [Function: mkdir],
    mkdirSync: [Function: mkdirSync],
    mkdtemp: [Function: mkdtemp],
    mkdtempSync: [Function: mkdtempSync],
    open: [Function: open],
    openSync: [Function: openSync],
    opendir: [Function: opendir],
    opendirSync: [Function: opendirSync],
    readdir: [Function: readdir],
    readdirSync: [Function: readdirSync],
    read: [Function: read],
    readSync: [Function: readSync],
    readv: [Function: readv],
    readvSync: [Function: readvSync],
    readFile: [Function: readFile],
    readFileSync: [Function: readFileSync],
    readlink: [Function: readlink],
    readlinkSync: [Function: readlinkSync],
    realpath: [Function: realpath] { native: [Function (anonymous)] },
    realpathSync: [Function: realpathSync] { native: [Function (anonymous)] },
    rename: [Function: rename],
    renameSync: [Function: renameSync],
    rm: [Function: rm],
    rmSync: [Function: rmSync],
    rmdir: [Function: rmdir],
    rmdirSync: [Function: rmdirSync],
    stat: [Function: stat],
    statSync: [Function: statSync],
    symlink: [Function: symlink],
    symlinkSync: [Function: symlinkSync],
    truncate: [Function: truncate],
    truncateSync: [Function: truncateSync],
    unwatchFile: [Function: unwatchFile],
    unlink: [Function: unlink],
    unlinkSync: [Function: unlinkSync],
    utimes: [Function: utimes],
    utimesSync: [Function: utimesSync],
    watch: [Function: watch],
    watchFile: [Function: watchFile],
    writeFile: [Function: writeFile],
    writeFileSync: [Function: writeFileSync],
    write: [Function: write],
    writeSync: [Function: writeSync],
    writev: [Function: writev],
    writevSync: [Function: writevSync],
    Dir: [class Dir],
    Dirent: [class Dirent],
    Stats: [Function: Stats],
    ReadStream: [Getter/Setter],
    WriteStream: [Getter/Setter],
    FileReadStream: [Getter/Setter],
    FileWriteStream: [Getter/Setter],
    _toUnixTimestamp: [Function: toUnixTimestamp],
    F_OK: 0,
    R_OK: 4,
    W_OK: 2,
    X_OK: 1,
    constants: [Object: null prototype] {
      UV_FS_SYMLINK_DIR: 1,
      UV_FS_SYMLINK_JUNCTION: 2,
      O_RDONLY: 0,
      O_WRONLY: 1,
      O_RDWR: 2,
      UV_DIRENT_UNKNOWN: 0,
      UV_DIRENT_FILE: 1,
      UV_DIRENT_DIR: 2,
      UV_DIRENT_LINK: 3,
      UV_DIRENT_FIFO: 4,
      UV_DIRENT_SOCKET: 5,
      UV_DIRENT_CHAR: 6,
      UV_DIRENT_BLOCK: 7,
      S_IFMT: 61440,
      S_IFREG: 32768,
      S_IFDIR: 16384,
      S_IFCHR: 8192,
      S_IFLNK: 40960,
      O_CREAT: 256,
      O_EXCL: 1024,
      UV_FS_O_FILEMAP: 536870912,
      O_TRUNC: 512,
      O_APPEND: 8,
      F_OK: 0,
      R_OK: 4,
      W_OK: 2,
      X_OK: 1,
      UV_FS_COPYFILE_EXCL: 1,
      COPYFILE_EXCL: 1,
      UV_FS_COPYFILE_FICLONE: 2,
      COPYFILE_FICLONE: 2,
      UV_FS_COPYFILE_FICLONE_FORCE: 4,
      COPYFILE_FICLONE_FORCE: 4
    },
    promises: [Getter]
  },
  [Symbol(kIsPerformingIO)]: false,
  [Symbol(kCapture)]: false
}

I can get the filename, mimetype. I only have problem with the createReadStream() method. Please help. Thank you.

jaydenseric commented 2 years ago

I'm pretty confident this isn't a graphql-upload bug and is more of a Nest.js usage question, which I'm not experienced in and can't really help with. You might find an answer looking at some of the past Nest.js related issues:

https://github.com/jaydenseric/graphql-upload/issues?q=is%3Aissue+nestjs

Even though this issue is closed, if anyone else who uses Nest.js would like to help feel free to comment.

Choco-milk-for-u commented 1 year ago

Hi another nest.js developer! My friend, what is in createReadStream()? Isnt it supposed to be file system (fs) method from node.js? Any way it works for me if i use it in this way: i took file from resolver (nest.js) and use property-method creatReadStream that contains in file we got. After that it gives you stream where you can use .pipe or event .on with chunks. Sorry if i didnt understand all problem i am still new in node.js after all. I really hope I helped you because I got stuck on this a few days ago too. And I stumbled up to this issue in issues section while trying to find the same answer in browser.