@stryke/http 0.10.0 → 0.11.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
@@ -26,7 +26,7 @@ class Agent extends u.Agent {
26
26
  stack: e
27
27
  } = new Error(" ");
28
28
  return (0, _typeChecks.isString)(e) ? e.split(`
29
- `).some(s => s.includes("(https.js:") || s.includes("node:https:")) : !1;
29
+ `).some(r => r.includes("(https.js:") || r.includes("node:https:")) : !1;
30
30
  }
31
31
  incrementSockets(t) {
32
32
  if (this.maxSockets === 1 / 0 && this.maxTotalSockets === 1 / 0) return null;
@@ -38,29 +38,29 @@ class Agent extends u.Agent {
38
38
  }
39
39
  decrementSockets(t, e) {
40
40
  if (!this.sockets[t] || e === null) return;
41
- const s = this.sockets[t],
42
- o = s.indexOf(e);
43
- o !== -1 && (s.splice(o, 1), this.totalSocketCount--, s.length === 0 && delete this.sockets[t]);
41
+ const r = this.sockets[t],
42
+ o = r.indexOf(e);
43
+ o !== -1 && (r.splice(o, 1), this.totalSocketCount--, r.length === 0 && delete this.sockets[t]);
44
44
  }
45
45
  getName(t) {
46
46
  return this.isSecureEndpoint(t) ? HttpsAgent.prototype.getName.call(this, t) : super.getName(t);
47
47
  }
48
- createSocket(t, e, s) {
48
+ createSocket(t, e, r) {
49
49
  const o = {
50
50
  ...e,
51
51
  secureEndpoint: this.isSecureEndpoint(e)
52
52
  },
53
53
  c = this.getName(o),
54
54
  i = this.incrementSockets(c);
55
- Promise.resolve().then(async () => this.connect(t, o)).then(r => {
56
- if (this.decrementSockets(c, i), r instanceof u.Agent) try {
57
- return r.addRequest(t, o);
55
+ Promise.resolve().then(async () => this.connect(t, o)).then(s => {
56
+ if (this.decrementSockets(c, i), s instanceof u.Agent) try {
57
+ return s.addRequest(t, o);
58
58
  } catch (l) {
59
- return s(l);
59
+ return r(l);
60
60
  }
61
- this[n].currentSocket = r, super.createSocket(t, e, s);
62
- }, r => {
63
- this.decrementSockets(c, i), s(r);
61
+ this[n].currentSocket = s, super.createSocket(t, e, r);
62
+ }, s => {
63
+ this.decrementSockets(c, i), r(s);
64
64
  });
65
65
  }
66
66
  createConnection() {
package/dist/agent.mjs CHANGED
@@ -1,2 +1,2 @@
1
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(`
2
- `).some(s=>s.includes("(https.js:")||s.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 s=this.sockets[t],o=s.indexOf(e);o!==-1&&(s.splice(o,1),this.totalSocketCount--,s.length===0&&delete this.sockets[t])}getName(t){return this.isSecureEndpoint(t)?HttpsAgent.prototype.getName.call(this,t):super.getName(t)}createSocket(t,e,s){const o={...e,secureEndpoint:this.isSecureEndpoint(e)},c=this.getName(o),i=this.incrementSockets(c);Promise.resolve().then(async()=>this.connect(t,o)).then(r=>{if(this.decrementSockets(c,i),r instanceof u.Agent)try{return r.addRequest(t,o)}catch(l){return s(l)}this[n].currentSocket=r,super.createSocket(t,e,s)},r=>{this.decrementSockets(c,i),s(r)})}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)}}
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
@@ -3,36 +3,37 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.$fetch = $fetch;
7
6
  exports.fetch = void 0;
7
+ exports.fetchRequest = fetchRequest;
8
+ var _typeChecks = require("@stryke/type-checks");
9
+ var _defu = require("defu");
8
10
  var _nodeBuffer = require("node:buffer");
9
11
  var _nodeHttp = _interopRequireDefault(require("node:http"));
10
12
  var _nodeHttps = _interopRequireDefault(require("node:https"));
11
13
  var _proxyAgent = require("./proxy-agent.cjs");
12
14
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
- async function $fetch(o, f = {}) {
14
- return new Promise((c, n) => {
15
- const {
16
- protocol: a
17
- } = new URL(o),
18
- u = a === "https:" ? _nodeHttps.default : _nodeHttp.default,
19
- r = f.isDev ? 3e3 : void 0,
20
- e = u.request(o, {
15
+ async function fetchRequest(e, n = {}) {
16
+ return new Promise((m, i) => {
17
+ let r = "http:";
18
+ (0, _typeChecks.isString)(e) ? r = new URL(e).protocol : r = e.protocol;
19
+ const c = r === "https:" ? _nodeHttps.default : _nodeHttp.default,
20
+ s = n.timeout ?? 3e3,
21
+ o = c.request(e, (0, _defu.defu)(n, {
21
22
  agent: (0, _proxyAgent.getProxyAgent)(),
22
23
  headers: {
23
24
  "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"
24
25
  }
25
- }, t => {
26
+ }), t => {
26
27
  if (t.statusCode !== 200) {
27
- n(new Error(`Request failed: ${o} (status: ${t.statusCode})`));
28
+ i(new Error(`Request failed: ${e.toString()} (status: ${t.statusCode})`));
28
29
  return;
29
30
  }
30
- const s = [];
31
- t.on("data", m => s.push(_nodeBuffer.Buffer.from(m))), t.on("end", () => c(_nodeBuffer.Buffer.concat(s)));
31
+ const p = [];
32
+ t.on("data", u => p.push(_nodeBuffer.Buffer.from(u))), t.on("end", () => m(_nodeBuffer.Buffer.concat(p)));
32
33
  });
33
- r && e.setTimeout(r, () => {
34
- e.destroy(new Error(`Request timed out after ${r}ms`));
35
- }), e.on("error", t => n(t)), e.end();
34
+ s && o.setTimeout(s, () => {
35
+ o.destroy(new Error(`Request timed out after ${s}ms`));
36
+ }), o.on("error", t => i(t)), o.end();
36
37
  });
37
38
  }
38
- const fetch = exports.fetch = $fetch;
39
+ const fetch = exports.fetch = fetchRequest;
package/dist/fetch.d.ts CHANGED
@@ -1,19 +1,21 @@
1
1
  import { Buffer } from "node:buffer";
2
- export interface FetchRequestOptions {
3
- isDev?: boolean;
2
+ import type { RequestOptions } from "node:https";
3
+ export interface FetchRequestOptions extends RequestOptions {
4
+ /**
5
+ * Timeout in milliseconds
6
+ *
7
+ * @defaultValue 3000
8
+ */
9
+ timeout?: number;
4
10
  }
5
11
  /**
6
12
  * Fetches a resource from a URL.
7
13
  *
8
14
  * @remarks
9
- * Makes a simple GET request and returns the entire response as a Buffer.
10
- *
11
- * Features:
12
- * - Throws if the response status is not 200.
13
- * - Applies a 3000 ms timeout when `isDev` is `true`.
15
+ * Makes a simple GET request and returns the entire response as a Buffer. Throws if the response status is not 200.
14
16
  *
15
17
  * @param url - The URL to fetch.
16
18
  * @returns A promise that resolves to the response body as a Buffer.
17
19
  */
18
- export declare function $fetch(url: string, options?: FetchRequestOptions): Promise<Buffer>;
19
- export declare const fetch: typeof $fetch;
20
+ export declare function fetchRequest(url: string | URL, options?: FetchRequestOptions): Promise<Buffer>;
21
+ export declare const fetch: typeof fetchRequest;
package/dist/fetch.mjs CHANGED
@@ -1 +1 @@
1
- import{Buffer as i}from"node:buffer";import p from"node:http";import h from"node:https";import{getProxyAgent as d}from"./proxy-agent";export async function $fetch(o,f={}){return new Promise((c,n)=>{const{protocol:a}=new URL(o),u=a==="https:"?h:p,r=f.isDev?3e3:void 0,e=u.request(o,{agent:d(),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"}},t=>{if(t.statusCode!==200){n(new Error(`Request failed: ${o} (status: ${t.statusCode})`));return}const s=[];t.on("data",m=>s.push(i.from(m))),t.on("end",()=>c(i.concat(s)))});r&&e.setTimeout(r,()=>{e.destroy(new Error(`Request timed out after ${r}ms`))}),e.on("error",t=>n(t)),e.end()})}export const fetch=$fetch;
1
+ import{isString as a}from"@stryke/type-checks";import{defu as d}from"defu";import{Buffer as f}from"node:buffer";import h from"node:http";import l from"node:https";import{getProxyAgent as q}from"./proxy-agent";export async function fetchRequest(e,n={}){return new Promise((m,i)=>{let r="http:";a(e)?r=new URL(e).protocol:r=e.protocol;const c=r==="https:"?l:h,s=n.timeout??3e3,o=c.request(e,d(n,{agent:q(),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"}}),t=>{if(t.statusCode!==200){i(new Error(`Request failed: ${e.toString()} (status: ${t.statusCode})`));return}const p=[];t.on("data",u=>p.push(f.from(u))),t.on("end",()=>m(f.concat(p)))});s&&o.setTimeout(s,()=>{o.destroy(new Error(`Request timed out after ${s}ms`))}),o.on("error",t=>i(t)),o.end()})}export const fetch=fetchRequest;
@@ -40,9 +40,7 @@ export interface BufferConversionsInterface {
40
40
  * ```
41
41
  *
42
42
  * @param convert - Conversion functions
43
- * @param uri - Data URI to turn into a Buffer instance
43
+ * @returns An object containing the parsed data URI properties and the decoded data as a Buffer instance.
44
44
  * @throws `TypeError` if `uri` is not a valid Data URI
45
- * @returns An object containing the parsed data URI properties
46
- * and the decoded data as a Buffer instance.
47
45
  */
48
46
  export declare const makeDataUriToBuffer: (convert: BufferConversionsInterface) => (uri: string | URL) => ParsedDataURI;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/http",
3
- "version": "0.10.0",
3
+ "version": "0.11.1",
4
4
  "type": "module",
5
5
  "description": "A package containing HTTP communication utilities used by Storm Software.",
6
6
  "repository": {
@@ -9,9 +9,9 @@
9
9
  "directory": "packages/http"
10
10
  },
11
11
  "private": false,
12
- "devDependencies": { "@types/node": "^22.14.0" },
12
+ "dependencies": { "defu": "^6.1.4", "@stryke/type-checks": "^0.3.10" },
13
+ "devDependencies": { "@types/node": "^22.18.13" },
13
14
  "publishConfig": { "access": "public" },
14
- "dependencies": { "@stryke/type-checks": "^0.3.10" },
15
15
  "sideEffects": false,
16
16
  "files": ["dist/**/*"],
17
17
  "homepage": "https://stormsoftware.com",
@@ -176,5 +176,5 @@
176
176
  "main": "./dist/index.cjs",
177
177
  "module": "./dist/index.mjs",
178
178
  "types": "./dist/index.d.ts",
179
- "gitHead": "4074c4a56f6e2c42ca4c39c30df5309c19024c7b"
179
+ "gitHead": "1764a8373c1db0b1c11bed7439292236edfd9e4e"
180
180
  }