forked from ThePandaParade/hastebin-post
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hastebin.js
38 lines (32 loc) · 1.14 KB
/
hastebin.js
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
/* jshint esversion: 6 */
(() => "use strict")();
const https = require("https");
module.exports = (string) => {
return new Promise((resolve, reject) => {
if (!string) return reject("Argument 'string' not found.");
if (typeof(string) != "string") return reject("Argument 'string' must be of type string.");
if (string.length <= 0) return reject("Argument 'string' is empty, please consider adding some text!");
const request = https.request({
host: "hastebin.com",
path: "/documents",
method: "POST",
headers: {
"Content-Type": "text/plain; charset=utf-8",
"Content-Length": Buffer.byteLength(string)
}
});
request
.once("response", (response) => {
const body = [ ];
response
.on("data", (chunk) => body.push(chunk))
.on("error", (err) => reject(err))
.on("end", () => {
const key = JSON.parse(Buffer.concat(body)).key;
return resolve(`https://hastebin.com/${key}`);
});
})
.once("error", (error) => reject(error));
return request.end(string);
});
};