urllib 4.3.0 → 4.4.0

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/src/utils.ts CHANGED
@@ -2,6 +2,9 @@ import { randomBytes, createHash } from 'node:crypto';
2
2
  import { Readable } from 'node:stream';
3
3
  import { performance } from 'node:perf_hooks';
4
4
  import type { FixJSONCtlChars } from './Request.js';
5
+ import { SocketInfo } from './Response.js';
6
+ import symbols from './symbols.js';
7
+ import { IncomingHttpHeaders } from './IncomingHttpHeaders.js';
5
8
 
6
9
  const JSONCtlCharsMap: Record<string, string> = {
7
10
  '"': '\\"', // \u0022
@@ -151,3 +154,54 @@ export function isReadable(stream: any) {
151
154
  && typeof stream._read === 'function'
152
155
  && typeof stream._readableState === 'object';
153
156
  }
157
+
158
+ export function updateSocketInfo(socketInfo: SocketInfo, internalOpaque: any, err?: any) {
159
+ const socket = internalOpaque[symbols.kRequestSocket] ?? err?.[symbols.kErrorSocket];
160
+ if (socket) {
161
+ socketInfo.id = socket[symbols.kSocketId];
162
+ socketInfo.handledRequests = socket[symbols.kHandledRequests];
163
+ socketInfo.handledResponses = socket[symbols.kHandledResponses];
164
+ if (socket[symbols.kSocketLocalAddress]) {
165
+ socketInfo.localAddress = socket[symbols.kSocketLocalAddress];
166
+ socketInfo.localPort = socket[symbols.kSocketLocalPort];
167
+ }
168
+ if (socket.remoteAddress) {
169
+ socketInfo.remoteAddress = socket.remoteAddress;
170
+ socketInfo.remotePort = socket.remotePort;
171
+ socketInfo.remoteFamily = socket.remoteFamily;
172
+ }
173
+ socketInfo.bytesRead = socket.bytesRead;
174
+ socketInfo.bytesWritten = socket.bytesWritten;
175
+ if (socket[symbols.kSocketConnectErrorTime]) {
176
+ socketInfo.connectErrorTime = socket[symbols.kSocketConnectErrorTime];
177
+ if (Array.isArray(socket.autoSelectFamilyAttemptedAddresses)) {
178
+ socketInfo.attemptedRemoteAddresses = socket.autoSelectFamilyAttemptedAddresses;
179
+ }
180
+ socketInfo.connectProtocol = socket[symbols.kSocketConnectProtocol];
181
+ socketInfo.connectHost = socket[symbols.kSocketConnectHost];
182
+ socketInfo.connectPort = socket[symbols.kSocketConnectPort];
183
+ }
184
+ if (socket[symbols.kSocketConnectedTime]) {
185
+ socketInfo.connectedTime = socket[symbols.kSocketConnectedTime];
186
+ }
187
+ if (socket[symbols.kSocketRequestEndTime]) {
188
+ socketInfo.lastRequestEndTime = socket[symbols.kSocketRequestEndTime];
189
+ }
190
+ socket[symbols.kSocketRequestEndTime] = new Date();
191
+ }
192
+ }
193
+
194
+ export function convertHeader(headers: Headers): IncomingHttpHeaders {
195
+ const res: IncomingHttpHeaders = {};
196
+ for (const [ key, value ] of headers.entries()) {
197
+ if (res[key]) {
198
+ if (!Array.isArray(res[key])) {
199
+ res[key] = [ res[key] ];
200
+ }
201
+ res[key].push(value);
202
+ } else {
203
+ res[key] = value;
204
+ }
205
+ }
206
+ return res;
207
+ }