socket-function 1.2.18 → 1.2.20

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/index.d.ts CHANGED
@@ -1003,11 +1003,19 @@ declare module "socket-function/src/getUniqueTime" {
1003
1003
  declare module "socket-function/src/https" {
1004
1004
  /// <reference types="node" />
1005
1005
  /// <reference types="node" />
1006
+ export interface HttpsResponseInfo {
1007
+ statusCode?: number;
1008
+ statusMessage?: string;
1009
+ headers: {
1010
+ [key: string]: string | undefined;
1011
+ };
1012
+ }
1006
1013
  export declare function httpsRequest(url: string, payload?: Buffer | Buffer[], method?: string, sendSessionCookies?: boolean, config?: {
1007
1014
  headers?: {
1008
1015
  [key: string]: string | undefined;
1009
1016
  };
1010
1017
  cancel?: Promise<void>;
1018
+ outResponse?: HttpsResponseInfo;
1011
1019
  }): Promise<Buffer>;
1012
1020
 
1013
1021
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "1.2.18",
3
+ "version": "1.2.20",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "dependencies": {
package/src/https.d.ts CHANGED
@@ -1,8 +1,16 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
+ export interface HttpsResponseInfo {
4
+ statusCode?: number;
5
+ statusMessage?: string;
6
+ headers: {
7
+ [key: string]: string | undefined;
8
+ };
9
+ }
3
10
  export declare function httpsRequest(url: string, payload?: Buffer | Buffer[], method?: string, sendSessionCookies?: boolean, config?: {
4
11
  headers?: {
5
12
  [key: string]: string | undefined;
6
13
  };
7
14
  cancel?: Promise<void>;
15
+ outResponse?: HttpsResponseInfo;
8
16
  }): Promise<Buffer>;
package/src/https.ts CHANGED
@@ -7,6 +7,14 @@ import { dnsCacheLookup, reportConnectionFailure } from "./dnsCache";
7
7
  const textEncoder = new TextEncoder();
8
8
  const textDecoder = new TextDecoder();
9
9
 
10
+ // Response metadata surfaced back to the caller. Extend this rather than adding new out-params, so callers
11
+ // can read whatever they need without the signature churning.
12
+ export interface HttpsResponseInfo {
13
+ statusCode?: number;
14
+ statusMessage?: string;
15
+ headers: { [key: string]: string | undefined };
16
+ }
17
+
10
18
  // Error codes that mean the TCP connection was never established (so no request bytes reached the server
11
19
  // and a full retry — even of a non-idempotent method — is safe). These are the failures a stale DNS
12
20
  // answer produces, and the only ones we re-resolve + retry on.
@@ -47,6 +55,9 @@ export function httpsRequest(
47
55
  config?: {
48
56
  headers?: { [key: string]: string | undefined },
49
57
  cancel?: Promise<void>;
58
+ // Populated with the response status code, message, and headers, so the caller can read them
59
+ // without us adding a new out-param per field.
60
+ outResponse?: HttpsResponseInfo;
50
61
  }
51
62
  ): Promise<Buffer> {
52
63
  if (isNode()) {
@@ -96,6 +107,16 @@ export function httpsRequest(
96
107
  autoSelectFamily: true,
97
108
  } as https.RequestOptions,
98
109
  async httpResponse => {
110
+ if (config?.outResponse) {
111
+ let headers: { [key: string]: string | undefined } = {};
112
+ for (let [key, value] of Object.entries(httpResponse.headers)) {
113
+ headers[key] = Array.isArray(value) ? value.join(", ") : value;
114
+ }
115
+ config.outResponse.statusCode = httpResponse.statusCode;
116
+ config.outResponse.statusMessage = httpResponse.statusMessage;
117
+ config.outResponse.headers = headers;
118
+ }
119
+
99
120
  let data: Buffer[] = [];
100
121
  httpResponse.on("data", chunk => {
101
122
  data.push(chunk);
@@ -158,6 +179,17 @@ export function httpsRequest(
158
179
  }
159
180
  return new Promise((resolve, reject) => {
160
181
  request.onload = () => {
182
+ if (config?.outResponse) {
183
+ let headers: { [key: string]: string | undefined } = {};
184
+ for (let line of request.getAllResponseHeaders().trim().split(/[\r\n]+/)) {
185
+ let index = line.indexOf(":");
186
+ if (index < 0) continue;
187
+ headers[line.slice(0, index).trim().toLowerCase()] = line.slice(index + 1).trim();
188
+ }
189
+ config.outResponse.statusCode = request.status;
190
+ config.outResponse.statusMessage = request.statusText;
191
+ config.outResponse.headers = headers;
192
+ }
161
193
  if (!request.status.toString().startsWith("2")) {
162
194
  try {
163
195
  // It should be an error.stack. But if it isn't... just throw the status text...