@stryke/http 0.12.0 → 0.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/agent.cjs CHANGED
@@ -9,7 +9,7 @@ var u = _interopRequireWildcard(require("node:http"));
9
9
  var h = _interopRequireWildcard(require("node:net"));
10
10
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
11
11
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
12
- const n = Symbol("AgentBaseInternalState");
12
+ const n = Symbol("Agent");
13
13
  class Agent extends u.Agent {
14
14
  [n];
15
15
  options;
package/dist/agent.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import{isBoolean as a,isString as p}from"@stryke/type-checks";import*as u from"node:http";import*as h from"node:net";const n=Symbol("AgentBaseInternalState");export class Agent extends u.Agent{[n];options;keepAlive;constructor(t){super(t),this[n]={}}isSecureEndpoint(t){if(t){if(a(t.secureEndpoint))return t.secureEndpoint;if(p(t.protocol))return t.protocol==="https:"}const{stack:e}=new Error(" ");return p(e)?e.split(`
1
+ import{isBoolean as a,isString as p}from"@stryke/type-checks";import*as u from"node:http";import*as h from"node:net";const n=Symbol("Agent");export class Agent extends u.Agent{[n];options;keepAlive;constructor(t){super(t),this[n]={}}isSecureEndpoint(t){if(t){if(a(t.secureEndpoint))return t.secureEndpoint;if(p(t.protocol))return t.protocol==="https:"}const{stack:e}=new Error(" ");return p(e)?e.split(`
2
2
  `).some(r=>r.includes("(https.js:")||r.includes("node:https:")):!1}incrementSockets(t){if(this.maxSockets===1/0&&this.maxTotalSockets===1/0)return null;this.sockets[t]??=[];const e=new h.Socket({writable:!1});return this.sockets[t].push(e),this.totalSocketCount++,e}decrementSockets(t,e){if(!this.sockets[t]||e===null)return;const r=this.sockets[t],o=r.indexOf(e);o!==-1&&(r.splice(o,1),this.totalSocketCount--,r.length===0&&delete this.sockets[t])}getName(t){return this.isSecureEndpoint(t)?HttpsAgent.prototype.getName.call(this,t):super.getName(t)}createSocket(t,e,r){const o={...e,secureEndpoint:this.isSecureEndpoint(e)},c=this.getName(o),i=this.incrementSockets(c);Promise.resolve().then(async()=>this.connect(t,o)).then(s=>{if(this.decrementSockets(c,i),s instanceof u.Agent)try{return s.addRequest(t,o)}catch(l){return r(l)}this[n].currentSocket=s,super.createSocket(t,e,r)},s=>{this.decrementSockets(c,i),r(s)})}createConnection(){const t=this[n].currentSocket;if(this[n].currentSocket=void 0,!t)throw new Error("No socket was returned in the `connect()` function");return t}get defaultPort(){return this[n].defaultPort??(this.protocol==="https:"?443:80)}set defaultPort(t){this[n]&&(this[n].defaultPort=t)}get protocol(){return this[n].protocol??(this.isSecureEndpoint()?"https:":"http:")}set protocol(t){this[n]&&(this[n].protocol=t)}}
package/dist/fetch.cjs CHANGED
@@ -4,60 +4,24 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.fetch = void 0;
7
- exports.fetchJSON = fetchJSON;
8
7
  exports.fetchRequest = fetchRequest;
9
- exports.fetchString = fetchString;
10
- var _bufferToString = require("@stryke/convert/buffer-to-string");
11
- var _stormJson = require("@stryke/json/storm-json");
12
8
  var _isSetString = require("@stryke/type-checks/is-set-string");
13
9
  var _isUrl = require("@stryke/type-checks/is-url");
14
10
  var _helpers = require("@stryke/url/helpers");
15
11
  var _defu = require("defu");
16
- var _nodeBuffer = require("node:buffer");
17
- var _nodeHttp = _interopRequireDefault(require("node:http"));
18
- var _nodeHttps = _interopRequireDefault(require("node:https"));
12
+ var _undici = require("undici");
19
13
  var _proxyAgent = require("./proxy-agent.cjs");
20
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
21
- async function fetchRequest(t, r = {}) {
22
- return new Promise((i, s) => {
23
- let p = "http:";
24
- if ((0, _isSetString.isSetString)(t)) {
25
- if (!(0, _helpers.isValidURL)(t)) {
26
- s(new Error(`Invalid URL format provided: ${t}`));
27
- return;
28
- }
29
- p = new URL(t).protocol;
30
- } else if ((0, _isUrl.isURL)(t)) p = t.protocol;else {
31
- s(new Error("Invalid URL provided to fetch"));
32
- return;
14
+ async function fetchRequest(t, e = {}) {
15
+ if ((0, _isSetString.isSetString)(t)) {
16
+ if (!(0, _helpers.isValidURL)(t)) throw new Error(`Invalid URL format provided: ${t}`);
17
+ } else if (!(0, _isUrl.isURL)(t)) throw new Error("Invalid URL provided to fetch");
18
+ const r = new AbortController();
19
+ return setTimeout(() => r.abort(), e.timeout ?? 5e3), (0, _undici.request)(t, (0, _defu.defu)(e, {
20
+ agent: (0, _proxyAgent.getProxyAgent)(),
21
+ signal: r.signal,
22
+ headers: {
23
+ "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"
33
24
  }
34
- const m = p === "https:" ? _nodeHttps.default : _nodeHttp.default,
35
- c = r.timeout ?? 3e3,
36
- n = m.request(t, (0, _defu.defu)(r, {
37
- agent: (0, _proxyAgent.getProxyAgent)(),
38
- headers: {
39
- "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"
40
- }
41
- }), e => {
42
- if (e.statusCode !== 200) {
43
- s(new Error(`Request failed: ${t.toString()} (status: ${e.statusCode})`));
44
- return;
45
- }
46
- const f = [];
47
- e.on("data", o => f.push(_nodeBuffer.Buffer.from(o))), e.on("end", () => {
48
- const o = _nodeBuffer.Buffer.concat(f);
49
- o.text = () => (0, _bufferToString.bufferToString)(o), o.json = () => _stormJson.StormJSON.parse(o.text()), i(o);
50
- });
51
- });
52
- c && n.setTimeout(c, () => {
53
- n.destroy(new Error(`Request timed out after ${c}ms`));
54
- }), n.on("error", e => s(e)), n.end();
55
- });
25
+ }));
56
26
  }
57
- const fetch = exports.fetch = fetchRequest;
58
- async function fetchString(t, r = {}) {
59
- return (await fetchRequest(t, r)).text();
60
- }
61
- async function fetchJSON(t, r = {}) {
62
- return (await fetchRequest(t, r)).json();
63
- }
27
+ const fetch = exports.fetch = fetchRequest;
package/dist/fetch.d.ts CHANGED
@@ -1,50 +1,20 @@
1
- import { Buffer } from "node:buffer";
2
- import type { RequestOptions } from "node:https";
3
- export interface FetchRequestOptions extends RequestOptions {
1
+ import type { UrlObject } from "node:url";
2
+ import type { Dispatcher } from "undici";
3
+ export type FetchRequestOptions<T = null> = {
4
+ dispatcher?: Dispatcher;
5
+ } & Omit<Dispatcher.RequestOptions<T>, "origin" | "path" | "method"> & Partial<Pick<Dispatcher.RequestOptions, "method">> & {
4
6
  /**
5
7
  * Timeout in milliseconds
6
8
  *
7
- * @defaultValue 3000
9
+ * @defaultValue 5000
8
10
  */
9
11
  timeout?: number;
10
- }
11
- export interface FetchResponse extends Buffer {
12
- /**
13
- * Parses the response body as JSON.
14
- *
15
- * @typeParam T - The type to parse the JSON as.
16
- * @returns The parsed JSON object.
17
- */
18
- json: <T extends object = Record<string, any>>() => T;
19
- /**
20
- * Converts the response body to a string.
21
- *
22
- * @returns The response body as a string.
23
- */
24
- text: () => string;
25
- }
12
+ };
26
13
  /**
27
14
  * Fetches a resource from a URL.
28
15
  *
29
- * @remarks
30
- * Makes a simple GET request and returns the entire response as a Buffer. Throws if the response status is not 200.
31
- *
32
16
  * @param url - The URL to fetch.
33
- * @returns A promise that resolves to the response body as a Buffer.
17
+ * @returns A promise that resolves to the response.
34
18
  */
35
- export declare function fetchRequest(url: string | URL, options?: FetchRequestOptions): Promise<FetchResponse>;
19
+ export declare function fetchRequest<T = null>(url: string | URL | UrlObject, options?: FetchRequestOptions<T>): Promise<Dispatcher.ResponseData<T>>;
36
20
  export declare const fetch: typeof fetchRequest;
37
- /**
38
- * Fetches a string resource from a URL.
39
- *
40
- * @param url - The URL to fetch.
41
- * @returns A promise that resolves to the response body as a Buffer.
42
- */
43
- export declare function fetchString(url: string | URL, options?: FetchRequestOptions): Promise<string>;
44
- /**
45
- * Fetches a JSON resource from a URL.
46
- *
47
- * @param url - The URL to fetch.
48
- * @returns A promise that resolves to the response body as a Buffer.
49
- */
50
- export declare function fetchJSON<T extends object = Record<string, any>>(url: string | URL, options?: FetchRequestOptions): Promise<T>;
package/dist/fetch.mjs CHANGED
@@ -1 +1 @@
1
- import{bufferToString as d}from"@stryke/convert/buffer-to-string";import{StormJSON as u}from"@stryke/json/storm-json";import{isSetString as R}from"@stryke/type-checks/is-set-string";import{isURL as h}from"@stryke/type-checks/is-url";import{isValidURL as g}from"@stryke/url/helpers";import{defu as x}from"defu";import{Buffer as a}from"node:buffer";import l from"node:http";import q from"node:https";import{getProxyAgent as y}from"./proxy-agent";export async function fetchRequest(t,r={}){return new Promise((i,s)=>{let p="http:";if(R(t)){if(!g(t)){s(new Error(`Invalid URL format provided: ${t}`));return}p=new URL(t).protocol}else if(h(t))p=t.protocol;else{s(new Error("Invalid URL provided to fetch"));return}const m=p==="https:"?q:l,c=r.timeout??3e3,n=m.request(t,x(r,{agent:y(),headers:{"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"}}),e=>{if(e.statusCode!==200){s(new Error(`Request failed: ${t.toString()} (status: ${e.statusCode})`));return}const f=[];e.on("data",o=>f.push(a.from(o))),e.on("end",()=>{const o=a.concat(f);o.text=()=>d(o),o.json=()=>u.parse(o.text()),i(o)})});c&&n.setTimeout(c,()=>{n.destroy(new Error(`Request timed out after ${c}ms`))}),n.on("error",e=>s(e)),n.end()})}export const fetch=fetchRequest;export async function fetchString(t,r={}){return(await fetchRequest(t,r)).text()}export async function fetchJSON(t,r={}){return(await fetchRequest(t,r)).json()}
1
+ import{isSetString as o}from"@stryke/type-checks/is-set-string";import{isURL as i}from"@stryke/type-checks/is-url";import{isValidURL as n}from"@stryke/url/helpers";import{defu as s}from"defu";import{request as p}from"undici";import{getProxyAgent as m}from"./proxy-agent";export async function fetchRequest(t,e={}){if(o(t)){if(!n(t))throw new Error(`Invalid URL format provided: ${t}`)}else if(!i(t))throw new Error("Invalid URL provided to fetch");const r=new AbortController;return setTimeout(()=>r.abort(),e.timeout??5e3),p(t,s(e,{agent:m(),signal:r.signal,headers:{"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"}}))}export const fetch=fetchRequest;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/http",
3
- "version": "0.12.0",
3
+ "version": "0.12.1",
4
4
  "type": "module",
5
5
  "description": "A package containing HTTP communication utilities used by Storm Software.",
6
6
  "repository": {
@@ -10,13 +10,12 @@
10
10
  },
11
11
  "private": false,
12
12
  "dependencies": {
13
- "@stryke/convert": "^0.6.10",
14
- "@stryke/type-checks": "^0.4.1",
15
- "@stryke/url": "^0.3.1",
13
+ "@stryke/type-checks": "^0.4.2",
14
+ "@stryke/url": "^0.3.2",
16
15
  "defu": "^6.1.4",
17
- "@stryke/json": "^0.9.13"
16
+ "undici": "^7.16.0"
18
17
  },
19
- "devDependencies": { "@types/node": "^24.10.1" },
18
+ "devDependencies": { "@types/node": "^24.10.1", "undici-types": "^7.16.0" },
20
19
  "publishConfig": { "access": "public" },
21
20
  "sideEffects": false,
22
21
  "files": ["dist/**/*"],
@@ -182,5 +181,5 @@
182
181
  "main": "./dist/index.cjs",
183
182
  "module": "./dist/index.mjs",
184
183
  "types": "./dist/index.d.ts",
185
- "gitHead": "bd066535c2758c8e05be32ad1e292cab7dcdf2d3"
184
+ "gitHead": "4f9ec36edfefa375090353856bca8d00e4e778e2"
186
185
  }