Spaces:
Running
Running
File size: 7,050 Bytes
5c2ed06 |
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var net_exports = {};
__export(net_exports, {
HttpError: () => HttpError,
Net: () => Net,
NetRequest: () => NetRequest,
NetStream: () => NetStream
});
module.exports = __toCommonJS(net_exports);
var https = __toESM(require("https"));
var http = __toESM(require("http"));
var url = __toESM(require("url"));
var Streams = __toESM(require("./streams"));
class HttpError extends Error {
constructor(message, statusCode, body) {
super(message);
this.name = "HttpError";
this.statusCode = statusCode;
this.body = body;
Error.captureStackTrace(this, HttpError);
}
}
class NetStream extends Streams.ReadWriteStream {
constructor(uri, opts = null) {
super();
this.statusCode = null;
this.headers = null;
this.uri = uri;
this.opts = opts;
this.response = null;
this.state = "pending";
this.request = this.makeRequest(opts);
}
makeRequest(opts) {
if (!opts)
opts = {};
let body = opts.body;
if (body && typeof body !== "string") {
if (!opts.headers)
opts.headers = {};
if (!opts.headers["Content-Type"]) {
opts.headers["Content-Type"] = "application/x-www-form-urlencoded";
}
body = NetStream.encodeQuery(body);
}
if (opts.query) {
this.uri += (this.uri.includes("?") ? "&" : "?") + NetStream.encodeQuery(opts.query);
}
if (body) {
if (!opts.headers)
opts.headers = {};
if (!opts.headers["Content-Length"]) {
opts.headers["Content-Length"] = Buffer.byteLength(body);
}
}
const protocol = url.parse(this.uri).protocol;
const net = protocol === "https:" ? https : http;
let resolveResponse;
this.response = new Promise((resolve) => {
resolveResponse = resolve;
});
const request = net.request(this.uri, opts, (response) => {
this.state = "open";
this.nodeReadableStream = response;
this.response = response;
this.statusCode = response.statusCode || null;
this.headers = response.headers;
response.setEncoding("utf-8");
resolveResponse(response);
resolveResponse = null;
response.on("data", (data) => {
this.push(data);
});
response.on("end", () => {
if (this.state === "open")
this.state = "success";
if (!this.atEOF)
this.pushEnd();
});
});
request.on("close", () => {
if (!this.atEOF) {
this.state = "error";
this.pushError(new Error("Unexpected connection close"));
}
if (resolveResponse) {
this.response = null;
resolveResponse(null);
resolveResponse = null;
}
});
request.on("error", (error) => {
if (!this.atEOF)
this.pushError(error, true);
});
if (opts.timeout || opts.timeout === void 0) {
request.setTimeout(opts.timeout || 5e3, () => {
this.state = "timeout";
this.pushError(new Error("Request timeout"));
request.abort();
});
}
if (body) {
request.write(body);
request.end();
if (opts.writable) {
throw new Error(`options.body is what you would have written to a NetStream - you must choose one or the other`);
}
} else if (opts.writable) {
this.nodeWritableStream = request;
} else {
request.end();
}
return request;
}
static encodeQuery(data) {
let out = "";
for (const key in data) {
if (out)
out += `&`;
out += `${key}=${encodeURIComponent(`${data[key]}`)}`;
}
return out;
}
_write(data) {
if (!this.nodeWritableStream) {
throw new Error("You must specify opts.writable to write to a request.");
}
const result = this.nodeWritableStream.write(data);
if (result !== false)
return void 0;
if (!this.drainListeners.length) {
this.nodeWritableStream.once("drain", () => {
for (const listener of this.drainListeners)
listener();
this.drainListeners = [];
});
}
return new Promise((resolve) => {
this.drainListeners.push(resolve);
});
}
_read() {
this.nodeReadableStream?.resume();
}
_pause() {
this.nodeReadableStream?.pause();
}
}
class NetRequest {
constructor(uri) {
this.uri = uri;
}
/**
* Makes a http/https get request to the given link and returns a stream.
* The request data itself can be read with ReadStream#readAll().
* The NetStream class also holds headers and statusCode as a property.
*
* @param opts request opts - headers, etc.
* @param body POST body
*/
getStream(opts = {}) {
if (typeof Config !== "undefined" && Config.noNetRequests) {
throw new Error(`Net requests are disabled.`);
}
const stream = new NetStream(this.uri, opts);
return stream;
}
/**
* Makes a basic http/https request to the URI.
* Returns the response data.
*
* Will throw if the response code isn't 200 OK.
*
* @param opts request opts - headers, etc.
*/
async get(opts = {}) {
const stream = this.getStream(opts);
const response = await stream.response;
if (response)
this.response = response;
if (response && response.statusCode !== 200) {
throw new HttpError(response.statusMessage || "Connection error", response.statusCode, await stream.readAll());
}
return stream.readAll();
}
post(opts = {}, body) {
if (!body)
body = opts.body;
return this.get({
...opts,
method: "POST",
body
});
}
}
const Net = Object.assign((path) => new NetRequest(path), {
NetRequest,
NetStream
});
//# sourceMappingURL=net.js.map
|