socket-function 1.2.18 → 1.2.19

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
@@ -1008,6 +1008,9 @@ declare module "socket-function/src/https" {
1008
1008
  [key: string]: string | undefined;
1009
1009
  };
1010
1010
  cancel?: Promise<void>;
1011
+ outHeaders?: {
1012
+ [key: string]: string | undefined;
1013
+ };
1011
1014
  }): Promise<Buffer>;
1012
1015
 
1013
1016
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "1.2.18",
3
+ "version": "1.2.19",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "dependencies": {
package/src/https.d.ts CHANGED
@@ -5,4 +5,7 @@ export declare function httpsRequest(url: string, payload?: Buffer | Buffer[], m
5
5
  [key: string]: string | undefined;
6
6
  };
7
7
  cancel?: Promise<void>;
8
+ outHeaders?: {
9
+ [key: string]: string | undefined;
10
+ };
8
11
  }): Promise<Buffer>;
package/src/https.ts CHANGED
@@ -47,6 +47,8 @@ export function httpsRequest(
47
47
  config?: {
48
48
  headers?: { [key: string]: string | undefined },
49
49
  cancel?: Promise<void>;
50
+ // Populated with the response headers once they arrive, so the caller can read them.
51
+ outHeaders?: { [key: string]: string | undefined };
50
52
  }
51
53
  ): Promise<Buffer> {
52
54
  if (isNode()) {
@@ -96,6 +98,12 @@ export function httpsRequest(
96
98
  autoSelectFamily: true,
97
99
  } as https.RequestOptions,
98
100
  async httpResponse => {
101
+ if (config?.outHeaders) {
102
+ for (let [key, value] of Object.entries(httpResponse.headers)) {
103
+ config.outHeaders[key] = Array.isArray(value) ? value.join(", ") : value;
104
+ }
105
+ }
106
+
99
107
  let data: Buffer[] = [];
100
108
  httpResponse.on("data", chunk => {
101
109
  data.push(chunk);
@@ -158,6 +166,13 @@ export function httpsRequest(
158
166
  }
159
167
  return new Promise((resolve, reject) => {
160
168
  request.onload = () => {
169
+ if (config?.outHeaders) {
170
+ for (let line of request.getAllResponseHeaders().trim().split(/[\r\n]+/)) {
171
+ let index = line.indexOf(":");
172
+ if (index < 0) continue;
173
+ config.outHeaders[line.slice(0, index).trim().toLowerCase()] = line.slice(index + 1).trim();
174
+ }
175
+ }
161
176
  if (!request.status.toString().startsWith("2")) {
162
177
  try {
163
178
  // It should be an error.stack. But if it isn't... just throw the status text...