Last active
July 14, 2019 00:32
-
-
Save dupski/8608a71cdc5cd2f64b1f1605356605c4 to your computer and use it in GitHub Desktop.
Simple NodeJS class for creating a Readable Stream from a String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Readable } from 'stream' | |
class ReadableString extends Readable { | |
private sent = false | |
constructor(private str: string) { | |
super(); | |
} | |
_read() { | |
if (!this.sent) { | |
this.push(Buffer.from(this.str)); | |
this.sent = true | |
} | |
else { | |
this.push(null) | |
} | |
} | |
} | |
export async function uploadJson() { | |
const contentStream = new ReadableString(JSON.stringify({ | |
my_json_data: true | |
})) | |
const result = await drive.files.create({ | |
... | |
media: { | |
mimeType: 'application/json', | |
body: contentStream, | |
}, | |
}) | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment