socket-function 1.2.19 → 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,14 +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>;
1011
- outHeaders?: {
1012
- [key: string]: string | undefined;
1013
- };
1018
+ outResponse?: HttpsResponseInfo;
1014
1019
  }): Promise<Buffer>;
1015
1020
 
1016
1021
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "1.2.19",
3
+ "version": "1.2.20",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "dependencies": {
package/src/https.d.ts CHANGED
@@ -1,11 +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>;
8
- outHeaders?: {
9
- [key: string]: string | undefined;
10
- };
15
+ outResponse?: HttpsResponseInfo;
11
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,8 +55,9 @@ export function httpsRequest(
47
55
  config?: {
48
56
  headers?: { [key: string]: string | undefined },
49
57
  cancel?: Promise<void>;
50
- // Populated with the response headers once they arrive, so the caller can read them.
51
- outHeaders?: { [key: string]: string | undefined };
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;
52
61
  }
53
62
  ): Promise<Buffer> {
54
63
  if (isNode()) {
@@ -98,10 +107,14 @@ export function httpsRequest(
98
107
  autoSelectFamily: true,
99
108
  } as https.RequestOptions,
100
109
  async httpResponse => {
101
- if (config?.outHeaders) {
110
+ if (config?.outResponse) {
111
+ let headers: { [key: string]: string | undefined } = {};
102
112
  for (let [key, value] of Object.entries(httpResponse.headers)) {
103
- config.outHeaders[key] = Array.isArray(value) ? value.join(", ") : value;
113
+ headers[key] = Array.isArray(value) ? value.join(", ") : value;
104
114
  }
115
+ config.outResponse.statusCode = httpResponse.statusCode;
116
+ config.outResponse.statusMessage = httpResponse.statusMessage;
117
+ config.outResponse.headers = headers;
105
118
  }
106
119
 
107
120
  let data: Buffer[] = [];
@@ -166,12 +179,16 @@ export function httpsRequest(
166
179
  }
167
180
  return new Promise((resolve, reject) => {
168
181
  request.onload = () => {
169
- if (config?.outHeaders) {
182
+ if (config?.outResponse) {
183
+ let headers: { [key: string]: string | undefined } = {};
170
184
  for (let line of request.getAllResponseHeaders().trim().split(/[\r\n]+/)) {
171
185
  let index = line.indexOf(":");
172
186
  if (index < 0) continue;
173
- config.outHeaders[line.slice(0, index).trim().toLowerCase()] = line.slice(index + 1).trim();
187
+ headers[line.slice(0, index).trim().toLowerCase()] = line.slice(index + 1).trim();
174
188
  }
189
+ config.outResponse.statusCode = request.status;
190
+ config.outResponse.statusMessage = request.statusText;
191
+ config.outResponse.headers = headers;
175
192
  }
176
193
  if (!request.status.toString().startsWith("2")) {
177
194
  try {