stream#Writable JavaScript Examples

The following examples show how to use stream#Writable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReactDOMFizzServerNode.js    From ReactSourceCodeAnalyze with MIT License 5 votes vote down vote up
function pipeToNodeWritable(
  children: ReactNodeList,
  destination: Writable,
): void {
  let request = createRequest(children, destination);
  destination.on('drain', createDrainHandler(destination, request));
  startWork(request);
}
Example #2
Source File: S3Store.js    From reaction-file-collections-sa-s3 with MIT License 5 votes vote down vote up
async _getWriteStream(fileKey, options = {}) {
    const opts = {
      Bucket: process.env.AWS_S3_BUCKET,
      Key: `${Date.now()}-${fileKey.filename}`
    };

    debug("S3Store _getWriteStream opts:", opts);
    debug("S3Store _getWriteStream options:", options);
    debug("S3Store _getWriteStream fileKey:", fileKey);
    debug("S3Store _getWriteStream objectACL", this.objectACL);

    let uploadId = "";

    const uploadData = await this.s3.createMultipartUpload({
      ...opts,
      ACL: this.objectACL
    }).promise();

    debug("s3.createMultipartUpload data:", uploadData);

    if (uploadData.UploadId === undefined) {
      throw new Error("Couldn't get upload ID from S3");
    }

    uploadId = uploadData.UploadId;

    let partNumber = 1;
    let totalFileSize = 0;
    const parts = [];

    const writeStream = new Writable({
      write: async (chunk, encoding, callback) => {
        const partData = await this.s3.uploadPart({
          ...opts,
          Body: chunk,
          UploadId: uploadId,
          PartNumber: partNumber
        }).promise();

        parts.push({
          ETag: partData.ETag,
          PartNumber: partNumber
        });

        debug(`Part ${partNumber} successfully uploaded`, parts);

        partNumber += 1;
        totalFileSize += chunk.length;

        callback();
      }
    });

    writeStream.on("finish", async () => {
      debug("S3Store writeStream finish");
      debug("S3Store writeStream totalFileSize:", totalFileSize);

      const uploadedFile = await this.s3.completeMultipartUpload({
        ...opts,
        UploadId: uploadId,
        MultipartUpload: {
          Parts: parts
        }
      }).promise();

      debug("S3 multipart upload completed", uploadedFile);

      // Emit end and return the fileKey, size, and updated date
      writeStream.emit("stored", {
        // Set the generated _id so that we know it for future reads and writes.
        // We store the _id as a string and only convert to ObjectID right before
        // reading, writing, or deleting.
        fileKey: uploadedFile.Key,
        storedAt: new Date(),
        size: totalFileSize
      });
    });

    return writeStream;
  }