rewrite node-fetch-cookies for flumm-fetch
This commit is contained in:
parent
ef39af34a9
commit
f2ccce9d0e
|
@ -1,10 +1,10 @@
|
||||||
# node-fetch-cookies
|
# flumm-fetch-cookies
|
||||||
A [node-fetch](https://github.com/bitinn/node-fetch) wrapper with support for cookies.
|
A [flumm-fetch](https://gitfap.de/Flummi/kbotv3-modules/blob/master/src/inc/fetch.mjs) wrapper with support for cookies.
|
||||||
It supports reading/writing from/to a JSON cookie jar and keeps cookies in memory until you call `CookieJar.save()` to reduce disk I/O.
|
It supports reading/writing from/to a JSON cookie jar and keeps cookies in memory until you call `CookieJar.save()` to reduce disk I/O.
|
||||||
|
|
||||||
## Usage Example
|
## Usage Example
|
||||||
```javascript
|
```javascript
|
||||||
import {fetch, CookieJar} from "node-fetch-cookies";
|
import {fetch, CookieJar} from "flumm-fetch-cookies";
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
// creates a CookieJar instance
|
// creates a CookieJar instance
|
||||||
|
|
10
package-lock.json
generated
10
package-lock.json
generated
|
@ -1,13 +1,5 @@
|
||||||
{
|
{
|
||||||
"name": "node-fetch-cookies",
|
"name": "node-fetch-cookies",
|
||||||
"version": "1.0.6",
|
"version": "1.0.6",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1
|
||||||
"requires": true,
|
|
||||||
"dependencies": {
|
|
||||||
"node-fetch": {
|
|
||||||
"version": "2.6.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz",
|
|
||||||
"integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA=="
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
16
package.json
16
package.json
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "node-fetch-cookies",
|
"name": "flumm-fetch-cookies",
|
||||||
"version": "1.0.6",
|
"version": "1.0.6",
|
||||||
"description": "node-fetch wrapper that adds support for cookie-jars",
|
"description": "flumm-fetch wrapper that adds support for cookie-jars",
|
||||||
"main": "src/index.mjs",
|
"main": "src/index.mjs",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=10.0.0"
|
"node": ">=10.0.0"
|
||||||
|
@ -11,21 +11,19 @@
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+https://github.com/jkhsjdhjs/node-fetch-cookies.git"
|
"url": "git+https://gitfap.de/keinBot/flumm-fetch-cookies.git"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"cookie",
|
"cookie",
|
||||||
"cookie-jar",
|
"cookie-jar",
|
||||||
"node-fetch",
|
"flumm-fetch",
|
||||||
"fetch"
|
"fetch"
|
||||||
],
|
],
|
||||||
|
"private": "true",
|
||||||
"author": "jkhsjdhjs",
|
"author": "jkhsjdhjs",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/jkhsjdhjs/node-fetch-cookies/issues"
|
"url": "https://gitfap.de/keinBot/flumm-fetch-cookies/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/jkhsjdhjs/node-fetch-cookies#readme",
|
"homepage": "https://gitfap.de/keinBot/flumm-fetch-cookies#readme"
|
||||||
"dependencies": {
|
|
||||||
"node-fetch": "^2.6.0"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
35
src/fetch.mjs
Normal file
35
src/fetch.mjs
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
import http from "http";
|
||||||
|
import https from "https";
|
||||||
|
import url from "url";
|
||||||
|
import querystring from "querystring";
|
||||||
|
|
||||||
|
const readdata = (res, mode, data = "") => new Promise((resolve, reject) => res
|
||||||
|
.setEncoding("utf8")
|
||||||
|
.on("data", chunk => data += chunk)
|
||||||
|
.on("end", () => {
|
||||||
|
switch(mode) {
|
||||||
|
case "text": resolve(data); break;
|
||||||
|
case "json": try { resolve(JSON.parse(data)); } catch(err) { reject(data); } break;
|
||||||
|
case "buffer": resolve(new Buffer.from(data)); break;
|
||||||
|
default: reject("lol no D:"); break;
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
export default (a, options = {}, link = url.parse(a), body = "") => new Promise((resolve, reject) => {
|
||||||
|
options = {...{ hostname: link.hostname, path: link.path, method: "GET" }, ...options};
|
||||||
|
if(options.method === "POST") {
|
||||||
|
body = querystring.stringify(options.body);
|
||||||
|
delete options.body;
|
||||||
|
options.headers = {...options.headers, ...{
|
||||||
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
|
"Content-Length": Buffer.byteLength(body)
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
(link.protocol === "https:"?https:http).request(options, res => resolve({
|
||||||
|
body: res,
|
||||||
|
headers: res.headers,
|
||||||
|
text: () => readdata(res, "text"),
|
||||||
|
json: () => readdata(res, "json"),
|
||||||
|
buffer: () => readdata(res, "buffer")
|
||||||
|
})).on("error", err => reject(err)).end(body);
|
||||||
|
});
|
|
@ -1,4 +1,4 @@
|
||||||
import fetch from "node-fetch";
|
import fetch from "./fetch";
|
||||||
import CookieJar from "./cookie-jar";
|
import CookieJar from "./cookie-jar";
|
||||||
import Cookie from "./cookie";
|
import Cookie from "./cookie";
|
||||||
|
|
||||||
|
@ -35,8 +35,7 @@ async function cookieFetch(cookieJars, url, options) {
|
||||||
options.headers.cookie = cookies.slice(0, -2);
|
options.headers.cookie = cookies.slice(0, -2);
|
||||||
}
|
}
|
||||||
const result = await fetch(url, options);
|
const result = await fetch(url, options);
|
||||||
// i cannot use headers.get() here because it joins the cookies to a string
|
cookies = result.headers["set-cookie"]);
|
||||||
cookies = result.headers[Object.getOwnPropertySymbols(result.headers)[0]]["set-cookie"];
|
|
||||||
if(cookies && cookieJars) {
|
if(cookies && cookieJars) {
|
||||||
if(Array.isArray(cookieJars)) {
|
if(Array.isArray(cookieJars)) {
|
||||||
cookieJars.forEach(jar => {
|
cookieJars.forEach(jar => {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user