@stryke/http 0.11.10 → 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.d.ts CHANGED
@@ -12,9 +12,9 @@ interface HttpsConnectOpts extends tls.ConnectionOptions {
12
12
  port: number;
13
13
  }
14
14
  export type AgentConnectOpts = HttpConnectOpts | HttpsConnectOpts;
15
- declare const INTERNAL: unique symbol;
15
+ declare const SYMBOL_INTERNAL: unique symbol;
16
16
  export declare abstract class Agent extends http.Agent {
17
- private [INTERNAL];
17
+ private [SYMBOL_INTERNAL];
18
18
  options: Partial<net.TcpNetConnectOpts & tls.ConnectionOptions>;
19
19
  keepAlive: boolean;
20
20
  constructor(opts?: http.AgentOptions);
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
@@ -5,35 +5,23 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.fetch = void 0;
7
7
  exports.fetchRequest = fetchRequest;
8
- var _typeChecks = require("@stryke/type-checks");
8
+ var _isSetString = require("@stryke/type-checks/is-set-string");
9
+ var _isUrl = require("@stryke/type-checks/is-url");
10
+ var _helpers = require("@stryke/url/helpers");
9
11
  var _defu = require("defu");
10
- var _nodeBuffer = require("node:buffer");
11
- var _nodeHttp = _interopRequireDefault(require("node:http"));
12
- var _nodeHttps = _interopRequireDefault(require("node:https"));
12
+ var _undici = require("undici");
13
13
  var _proxyAgent = require("./proxy-agent.cjs");
14
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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, {
22
- agent: (0, _proxyAgent.getProxyAgent)(),
23
- headers: {
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"
25
- }
26
- }), t => {
27
- if (t.statusCode !== 200) {
28
- i(new Error(`Request failed: ${e.toString()} (status: ${t.statusCode})`));
29
- return;
30
- }
31
- const p = [];
32
- t.on("data", u => p.push(_nodeBuffer.Buffer.from(u))), t.on("end", () => m(_nodeBuffer.Buffer.concat(p)));
33
- });
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();
37
- });
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"
24
+ }
25
+ }));
38
26
  }
39
27
  const fetch = exports.fetch = fetchRequest;
package/dist/fetch.d.ts CHANGED
@@ -1,21 +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
- }
12
+ };
11
13
  /**
12
14
  * Fetches a resource from a URL.
13
15
  *
14
- * @remarks
15
- * Makes a simple GET request and returns the entire response as a Buffer. Throws if the response status is not 200.
16
- *
17
16
  * @param url - The URL to fetch.
18
- * @returns A promise that resolves to the response body as a Buffer.
17
+ * @returns A promise that resolves to the response.
19
18
  */
20
- export declare function fetchRequest(url: string | URL, options?: FetchRequestOptions): Promise<Buffer>;
19
+ export declare function fetchRequest<T = null>(url: string | URL | UrlObject, options?: FetchRequestOptions<T>): Promise<Dispatcher.ResponseData<T>>;
21
20
  export declare const fetch: typeof fetchRequest;
package/dist/fetch.mjs CHANGED
@@ -1 +1 @@
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;
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;
@@ -3,13 +3,14 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.getFreePort = void 0;
6
+ exports.getFreePort = getFreePort;
7
7
  var _nodeHttp = require("node:http");
8
- const getFreePort = async () => new Promise((s, o) => {
9
- const r = (0, _nodeHttp.createServer)(() => {});
10
- r.listen(0, () => {
11
- const e = r.address();
12
- r.close(), e && typeof e == "object" ? s(e.port) : o(new Error(`invalid address from server: ${e?.toString()}`));
8
+ async function getFreePort() {
9
+ return new Promise((o, s) => {
10
+ const r = (0, _nodeHttp.createServer)(() => {});
11
+ r.listen(0, () => {
12
+ const e = r.address();
13
+ r.close(), e && typeof e == "object" ? o(e.port) : s(new Error(`invalid address from server: ${e?.toString()}`));
14
+ });
13
15
  });
14
- });
15
- exports.getFreePort = getFreePort;
16
+ }
@@ -3,4 +3,4 @@
3
3
  *
4
4
  * @returns A promise that resolves to a free port number.
5
5
  */
6
- export declare const getFreePort: () => Promise<number>;
6
+ export declare function getFreePort(): Promise<number>;
@@ -1 +1 @@
1
- import{createServer as t}from"node:http";export const getFreePort=async()=>new Promise((s,o)=>{const r=t(()=>{});r.listen(0,()=>{const e=r.address();r.close(),e&&typeof e=="object"?s(e.port):o(new Error(`invalid address from server: ${e?.toString()}`))})});
1
+ import{createServer as t}from"node:http";export async function getFreePort(){return new Promise((o,s)=>{const r=t(()=>{});r.listen(0,()=>{const e=r.address();r.close(),e&&typeof e=="object"?o(e.port):s(new Error(`invalid address from server: ${e?.toString()}`))})})}
@@ -23,8 +23,7 @@ interface HttpProxyAgentClientRequest extends ClientRequest {
23
23
  _implicitHeader: () => void;
24
24
  }
25
25
  /**
26
- * The `HttpProxyAgent` implements an HTTP Agent subclass that connects
27
- * to the specified "HTTP proxy server" in order to proxy HTTP requests.
26
+ * The `HttpProxyAgent` implements an HTTP Agent subclass that connects to the specified "HTTP proxy server" in order to proxy HTTP requests.
28
27
  */
29
28
  export declare class HttpProxyAgent<Uri extends string> extends Agent {
30
29
  static protocols: readonly ["http", "https"];
@@ -35,8 +35,7 @@ export declare class HttpsProxyAgent<Uri extends string> extends Agent {
35
35
  connectOpts: net.TcpNetConnectOpts & tls.ConnectionOptions;
36
36
  constructor(proxy: Uri | URL, opts?: HttpsProxyAgentOptions<Uri>);
37
37
  /**
38
- * Called when the node-core HTTP client library is creating a
39
- * new HTTP request.
38
+ * Called when the node-core HTTP client library is creating a new HTTP request.
40
39
  */
41
40
  connect(req: http.ClientRequest, opts: AgentConnectOpts): Promise<net.Socket>;
42
41
  }
@@ -6,6 +6,12 @@ export interface ConnectResponse {
6
6
  statusText: string;
7
7
  headers: IncomingHttpHeaders;
8
8
  }
9
+ /**
10
+ * Parses the proxy CONNECT response from the given socket.
11
+ *
12
+ * @param socket - The socket to read the response from.
13
+ * @returns A promise that resolves to the CONNECT response and any buffered data.
14
+ */
9
15
  export declare function parseProxyResponse(socket: Readable): Promise<{
10
16
  connect: ConnectResponse;
11
17
  buffered: Buffer;
@@ -1,5 +1,7 @@
1
1
  import type { Agent } from "node:https";
2
2
  /**
3
3
  * If the http(s)_proxy environment variables is set, return a proxy agent.
4
+ *
5
+ * @returns The proxy agent or undefined if no proxy is set.
4
6
  */
5
7
  export declare function getProxyAgent(): Agent | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/http",
3
- "version": "0.11.10",
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": {
@@ -9,8 +9,13 @@
9
9
  "directory": "packages/http"
10
10
  },
11
11
  "private": false,
12
- "dependencies": { "defu": "^6.1.4", "@stryke/type-checks": "^0.4.0" },
13
- "devDependencies": { "@types/node": "^24.10.1" },
12
+ "dependencies": {
13
+ "@stryke/type-checks": "^0.4.2",
14
+ "@stryke/url": "^0.3.2",
15
+ "defu": "^6.1.4",
16
+ "undici": "^7.16.0"
17
+ },
18
+ "devDependencies": { "@types/node": "^24.10.1", "undici-types": "^7.16.0" },
14
19
  "publishConfig": { "access": "public" },
15
20
  "sideEffects": false,
16
21
  "files": ["dist/**/*"],
@@ -176,5 +181,5 @@
176
181
  "main": "./dist/index.cjs",
177
182
  "module": "./dist/index.mjs",
178
183
  "types": "./dist/index.d.ts",
179
- "gitHead": "cd16bb3da577f7adb6f5c4ca6f12b90b17fe7061"
184
+ "gitHead": "4f9ec36edfefa375090353856bca8d00e4e778e2"
180
185
  }