@api-client/core 0.18.11 → 0.18.13

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.
Files changed (264) hide show
  1. package/build/src/browser.d.ts +0 -3
  2. package/build/src/browser.d.ts.map +1 -1
  3. package/build/src/browser.js +0 -3
  4. package/build/src/browser.js.map +1 -1
  5. package/build/src/index.d.ts +2 -5
  6. package/build/src/index.d.ts.map +1 -1
  7. package/build/src/index.js +2 -5
  8. package/build/src/index.js.map +1 -1
  9. package/build/src/lib/logging/DefaultLogger.d.ts +14 -0
  10. package/build/src/lib/logging/DefaultLogger.d.ts.map +1 -1
  11. package/build/src/lib/logging/DefaultLogger.js +27 -0
  12. package/build/src/lib/logging/DefaultLogger.js.map +1 -1
  13. package/build/src/lib/logging/index.d.ts +4 -0
  14. package/build/src/lib/logging/index.d.ts.map +1 -0
  15. package/build/src/lib/logging/index.js +10 -0
  16. package/build/src/lib/logging/index.js.map +1 -0
  17. package/build/src/modeling/DomainModel.d.ts.map +1 -1
  18. package/build/src/modeling/DomainModel.js +11 -4
  19. package/build/src/modeling/DomainModel.js.map +1 -1
  20. package/build/src/models/ClientCertificate.d.ts +1 -1
  21. package/build/src/models/ClientCertificate.js.map +1 -1
  22. package/build/src/models/RequestConfig.d.ts +1 -1
  23. package/build/src/models/RequestConfig.js.map +1 -1
  24. package/build/src/models/SerializableError.d.ts +1 -1
  25. package/build/src/models/SerializableError.d.ts.map +1 -1
  26. package/build/src/models/SerializableError.js.map +1 -1
  27. package/build/src/proxy/RequestProxy.d.ts.map +1 -1
  28. package/build/src/proxy/RequestProxy.js +2 -2
  29. package/build/src/proxy/RequestProxy.js.map +1 -1
  30. package/build/src/runtime/http-engine/CoreEngine.d.ts +218 -139
  31. package/build/src/runtime/http-engine/CoreEngine.d.ts.map +1 -1
  32. package/build/src/runtime/http-engine/CoreEngine.js +716 -870
  33. package/build/src/runtime/http-engine/CoreEngine.js.map +1 -1
  34. package/build/src/runtime/http-engine/PayloadSupport.d.ts.map +1 -1
  35. package/build/src/runtime/http-engine/PayloadSupport.js +2 -1
  36. package/build/src/runtime/http-engine/PayloadSupport.js.map +1 -1
  37. package/build/src/runtime/http-engine/auth/AuthManager.d.ts +73 -0
  38. package/build/src/runtime/http-engine/auth/AuthManager.d.ts.map +1 -0
  39. package/build/src/runtime/http-engine/auth/AuthManager.js +186 -0
  40. package/build/src/runtime/http-engine/auth/AuthManager.js.map +1 -0
  41. package/build/src/runtime/http-engine/auth/index.d.ts +2 -0
  42. package/build/src/runtime/http-engine/auth/index.d.ts.map +1 -0
  43. package/build/src/runtime/http-engine/auth/index.js +2 -0
  44. package/build/src/runtime/http-engine/auth/index.js.map +1 -0
  45. package/build/src/runtime/http-engine/certificates/CertificateManager.d.ts +11 -0
  46. package/build/src/runtime/http-engine/certificates/CertificateManager.d.ts.map +1 -0
  47. package/build/src/runtime/http-engine/certificates/CertificateManager.js +76 -0
  48. package/build/src/runtime/http-engine/certificates/CertificateManager.js.map +1 -0
  49. package/build/src/runtime/http-engine/certificates/index.d.ts +2 -0
  50. package/build/src/runtime/http-engine/certificates/index.d.ts.map +1 -0
  51. package/build/src/runtime/http-engine/certificates/index.js +2 -0
  52. package/build/src/runtime/http-engine/certificates/index.js.map +1 -0
  53. package/build/src/runtime/http-engine/compression/CompressionManager.d.ts +25 -0
  54. package/build/src/runtime/http-engine/compression/CompressionManager.d.ts.map +1 -0
  55. package/build/src/runtime/http-engine/compression/CompressionManager.js +89 -0
  56. package/build/src/runtime/http-engine/compression/CompressionManager.js.map +1 -0
  57. package/build/src/runtime/http-engine/compression/index.d.ts +2 -0
  58. package/build/src/runtime/http-engine/compression/index.d.ts.map +1 -0
  59. package/build/src/runtime/http-engine/compression/index.js +2 -0
  60. package/build/src/runtime/http-engine/compression/index.js.map +1 -0
  61. package/build/src/runtime/http-engine/connections/ConnectionManager.d.ts +57 -0
  62. package/build/src/runtime/http-engine/connections/ConnectionManager.d.ts.map +1 -0
  63. package/build/src/runtime/http-engine/connections/ConnectionManager.js +78 -0
  64. package/build/src/runtime/http-engine/connections/ConnectionManager.js.map +1 -0
  65. package/build/src/runtime/http-engine/connections/DigestAuthHandler.d.ts +70 -0
  66. package/build/src/runtime/http-engine/connections/DigestAuthHandler.d.ts.map +1 -0
  67. package/build/src/runtime/http-engine/connections/DigestAuthHandler.js +184 -0
  68. package/build/src/runtime/http-engine/connections/DigestAuthHandler.js.map +1 -0
  69. package/build/src/runtime/http-engine/connections/DirectConnection.d.ts +22 -0
  70. package/build/src/runtime/http-engine/connections/DirectConnection.d.ts.map +1 -0
  71. package/build/src/runtime/http-engine/connections/DirectConnection.js +105 -0
  72. package/build/src/runtime/http-engine/connections/DirectConnection.js.map +1 -0
  73. package/build/src/runtime/http-engine/connections/ProxyAuthHandler.d.ts +60 -0
  74. package/build/src/runtime/http-engine/connections/ProxyAuthHandler.d.ts.map +1 -0
  75. package/build/src/runtime/http-engine/connections/ProxyAuthHandler.js +138 -0
  76. package/build/src/runtime/http-engine/connections/ProxyAuthHandler.js.map +1 -0
  77. package/build/src/runtime/http-engine/connections/ProxyConnection.d.ts +14 -0
  78. package/build/src/runtime/http-engine/connections/ProxyConnection.d.ts.map +1 -0
  79. package/build/src/runtime/http-engine/connections/ProxyConnection.js +47 -0
  80. package/build/src/runtime/http-engine/connections/ProxyConnection.js.map +1 -0
  81. package/build/src/runtime/http-engine/connections/TunnelConnection.d.ts +13 -0
  82. package/build/src/runtime/http-engine/connections/TunnelConnection.d.ts.map +1 -0
  83. package/build/src/runtime/http-engine/connections/TunnelConnection.js +175 -0
  84. package/build/src/runtime/http-engine/connections/TunnelConnection.js.map +1 -0
  85. package/build/src/runtime/http-engine/connections/index.d.ts +7 -0
  86. package/build/src/runtime/http-engine/connections/index.d.ts.map +1 -0
  87. package/build/src/runtime/http-engine/connections/index.js +7 -0
  88. package/build/src/runtime/http-engine/connections/index.js.map +1 -0
  89. package/build/src/runtime/http-engine/constants.d.ts +69 -0
  90. package/build/src/runtime/http-engine/constants.d.ts.map +1 -0
  91. package/build/src/runtime/http-engine/constants.js +90 -0
  92. package/build/src/runtime/http-engine/constants.js.map +1 -0
  93. package/build/src/runtime/http-engine/cookies/CookieProcessor.d.ts +5 -0
  94. package/build/src/runtime/http-engine/cookies/CookieProcessor.d.ts.map +1 -0
  95. package/build/src/runtime/http-engine/cookies/CookieProcessor.js +20 -0
  96. package/build/src/runtime/http-engine/cookies/CookieProcessor.js.map +1 -0
  97. package/build/src/runtime/http-engine/cookies/index.d.ts +2 -0
  98. package/build/src/runtime/http-engine/cookies/index.d.ts.map +1 -0
  99. package/build/src/runtime/http-engine/cookies/index.js +2 -0
  100. package/build/src/runtime/http-engine/cookies/index.js.map +1 -0
  101. package/build/src/runtime/http-engine/errors/HttpEngineErrors.d.ts +156 -0
  102. package/build/src/runtime/http-engine/errors/HttpEngineErrors.d.ts.map +1 -0
  103. package/build/src/runtime/http-engine/errors/HttpEngineErrors.js +227 -0
  104. package/build/src/runtime/http-engine/errors/HttpEngineErrors.js.map +1 -0
  105. package/build/src/runtime/http-engine/errors/index.d.ts +2 -0
  106. package/build/src/runtime/http-engine/errors/index.d.ts.map +1 -0
  107. package/build/src/runtime/http-engine/errors/index.js +2 -0
  108. package/build/src/runtime/http-engine/errors/index.js.map +1 -0
  109. package/build/src/runtime/http-engine/message/MessageBuilder.d.ts +66 -0
  110. package/build/src/runtime/http-engine/message/MessageBuilder.d.ts.map +1 -0
  111. package/build/src/runtime/http-engine/message/MessageBuilder.js +161 -0
  112. package/build/src/runtime/http-engine/message/MessageBuilder.js.map +1 -0
  113. package/build/src/runtime/http-engine/message/MessageProcessor.d.ts +27 -0
  114. package/build/src/runtime/http-engine/message/MessageProcessor.d.ts.map +1 -0
  115. package/build/src/runtime/http-engine/message/MessageProcessor.js +51 -0
  116. package/build/src/runtime/http-engine/message/MessageProcessor.js.map +1 -0
  117. package/build/src/runtime/http-engine/message/index.d.ts +3 -0
  118. package/build/src/runtime/http-engine/message/index.d.ts.map +1 -0
  119. package/build/src/runtime/http-engine/message/index.js +3 -0
  120. package/build/src/runtime/http-engine/message/index.js.map +1 -0
  121. package/build/src/runtime/http-engine/ntlm/NtlmAuth.d.ts +2 -8
  122. package/build/src/runtime/http-engine/ntlm/NtlmAuth.d.ts.map +1 -1
  123. package/build/src/runtime/http-engine/ntlm/NtlmAuth.js +11 -5
  124. package/build/src/runtime/http-engine/ntlm/NtlmAuth.js.map +1 -1
  125. package/build/src/runtime/http-engine/ntlm/NtlmMessage.js +6 -6
  126. package/build/src/runtime/http-engine/ntlm/NtlmMessage.js.map +1 -1
  127. package/build/src/runtime/http-engine/parsers/BodyParser.d.ts +39 -0
  128. package/build/src/runtime/http-engine/parsers/BodyParser.d.ts.map +1 -0
  129. package/build/src/runtime/http-engine/parsers/BodyParser.js +145 -0
  130. package/build/src/runtime/http-engine/parsers/BodyParser.js.map +1 -0
  131. package/build/src/runtime/http-engine/parsers/HeadersParser.d.ts +29 -0
  132. package/build/src/runtime/http-engine/parsers/HeadersParser.d.ts.map +1 -0
  133. package/build/src/runtime/http-engine/parsers/HeadersParser.js +88 -0
  134. package/build/src/runtime/http-engine/parsers/HeadersParser.js.map +1 -0
  135. package/build/src/runtime/http-engine/parsers/HttpResponseParser.d.ts +91 -0
  136. package/build/src/runtime/http-engine/parsers/HttpResponseParser.d.ts.map +1 -0
  137. package/build/src/runtime/http-engine/parsers/HttpResponseParser.js +236 -0
  138. package/build/src/runtime/http-engine/parsers/HttpResponseParser.js.map +1 -0
  139. package/build/src/runtime/http-engine/parsers/StatusParser.d.ts +20 -0
  140. package/build/src/runtime/http-engine/parsers/StatusParser.d.ts.map +1 -0
  141. package/build/src/runtime/http-engine/parsers/StatusParser.js +51 -0
  142. package/build/src/runtime/http-engine/parsers/StatusParser.js.map +1 -0
  143. package/build/src/runtime/http-engine/parsers/index.d.ts +5 -0
  144. package/build/src/runtime/http-engine/parsers/index.d.ts.map +1 -0
  145. package/build/src/runtime/http-engine/parsers/index.js +5 -0
  146. package/build/src/runtime/http-engine/parsers/index.js.map +1 -0
  147. package/build/src/runtime/http-engine/response/ResponseProcessor.d.ts +22 -0
  148. package/build/src/runtime/http-engine/response/ResponseProcessor.d.ts.map +1 -0
  149. package/build/src/runtime/http-engine/response/ResponseProcessor.js +25 -0
  150. package/build/src/runtime/http-engine/response/ResponseProcessor.js.map +1 -0
  151. package/build/src/runtime/http-engine/response/index.d.ts +2 -0
  152. package/build/src/runtime/http-engine/response/index.d.ts.map +1 -0
  153. package/build/src/runtime/http-engine/response/index.js +2 -0
  154. package/build/src/runtime/http-engine/response/index.js.map +1 -0
  155. package/build/src/runtime/http-engine/statistics/StatisticsProcessor.d.ts +7 -0
  156. package/build/src/runtime/http-engine/statistics/StatisticsProcessor.d.ts.map +1 -0
  157. package/build/src/runtime/http-engine/statistics/StatisticsProcessor.js +40 -0
  158. package/build/src/runtime/http-engine/statistics/StatisticsProcessor.js.map +1 -0
  159. package/build/src/runtime/http-engine/statistics/index.d.ts +2 -0
  160. package/build/src/runtime/http-engine/statistics/index.d.ts.map +1 -0
  161. package/build/src/runtime/http-engine/statistics/index.js +2 -0
  162. package/build/src/runtime/http-engine/statistics/index.js.map +1 -0
  163. package/build/src/runtime/http-engine/url/UrlProcessor.d.ts +24 -0
  164. package/build/src/runtime/http-engine/url/UrlProcessor.d.ts.map +1 -0
  165. package/build/src/runtime/http-engine/url/UrlProcessor.js +50 -0
  166. package/build/src/runtime/http-engine/url/UrlProcessor.js.map +1 -0
  167. package/build/src/runtime/http-engine/url/index.d.ts +2 -0
  168. package/build/src/runtime/http-engine/url/index.d.ts.map +1 -0
  169. package/build/src/runtime/http-engine/url/index.js +2 -0
  170. package/build/src/runtime/http-engine/url/index.js.map +1 -0
  171. package/build/src/runtime/http-runner/HttpRequestRunner.d.ts +3 -3
  172. package/build/src/runtime/http-runner/HttpRequestRunner.d.ts.map +1 -1
  173. package/build/src/runtime/http-runner/HttpRequestRunner.js.map +1 -1
  174. package/build/src/runtime/node/InteropInterfaces.d.ts +3 -3
  175. package/build/src/runtime/node/InteropInterfaces.d.ts.map +1 -1
  176. package/build/src/runtime/node/InteropInterfaces.js.map +1 -1
  177. package/build/src/runtime/node/ProjectRequestRunner.d.ts +2 -2
  178. package/build/src/runtime/node/ProjectRequestRunner.d.ts.map +1 -1
  179. package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -1
  180. package/build/src/runtime/node/ProjectRunner.d.ts.map +1 -1
  181. package/build/src/runtime/node/ProjectRunner.js +2 -2
  182. package/build/src/runtime/node/ProjectRunner.js.map +1 -1
  183. package/build/tsconfig.tsbuildinfo +1 -1
  184. package/data/models/example-generator-api.json +24 -24
  185. package/package.json +2 -2
  186. package/src/lib/logging/DefaultLogger.ts +32 -0
  187. package/src/modeling/DomainModel.ts +11 -4
  188. package/src/models/ClientCertificate.ts +1 -1
  189. package/src/models/RequestConfig.ts +1 -1
  190. package/src/models/SerializableError.ts +1 -1
  191. package/src/proxy/RequestProxy.ts +2 -2
  192. package/src/runtime/http-engine/CoreEngine.ts +858 -893
  193. package/src/runtime/http-engine/PayloadSupport.ts +2 -1
  194. package/src/runtime/http-engine/auth/AuthManager.ts +242 -0
  195. package/src/runtime/http-engine/certificates/CertificateManager.ts +74 -0
  196. package/src/runtime/http-engine/compression/CompressionManager.ts +99 -0
  197. package/src/runtime/http-engine/connections/ConnectionManager.ts +123 -0
  198. package/src/runtime/http-engine/connections/DigestAuthHandler.ts +238 -0
  199. package/src/runtime/http-engine/connections/DirectConnection.ts +134 -0
  200. package/src/runtime/http-engine/connections/ProxyAuthHandler.ts +179 -0
  201. package/src/runtime/http-engine/connections/ProxyConnection.ts +55 -0
  202. package/src/runtime/http-engine/connections/TunnelConnection.ts +192 -0
  203. package/src/runtime/http-engine/constants.ts +103 -0
  204. package/src/runtime/http-engine/cookies/CookieProcessor.ts +25 -0
  205. package/src/runtime/http-engine/errors/HttpEngineErrors.ts +319 -0
  206. package/src/runtime/http-engine/message/MessageBuilder.ts +201 -0
  207. package/src/runtime/http-engine/message/MessageProcessor.ts +73 -0
  208. package/src/runtime/http-engine/ntlm/NtlmAuth.ts +16 -13
  209. package/src/runtime/http-engine/ntlm/NtlmMessage.ts +6 -6
  210. package/src/runtime/http-engine/parsers/BodyParser.ts +171 -0
  211. package/src/runtime/http-engine/parsers/HeadersParser.ts +103 -0
  212. package/src/runtime/http-engine/parsers/HttpResponseParser.ts +280 -0
  213. package/src/runtime/http-engine/parsers/StatusParser.ts +69 -0
  214. package/src/runtime/http-engine/response/ResponseProcessor.ts +46 -0
  215. package/src/runtime/http-engine/statistics/StatisticsProcessor.ts +52 -0
  216. package/src/runtime/http-engine/url/UrlProcessor.ts +55 -0
  217. package/src/runtime/http-runner/HttpRequestRunner.ts +3 -3
  218. package/src/runtime/node/InteropInterfaces.ts +3 -3
  219. package/src/runtime/node/ProjectRequestRunner.ts +2 -2
  220. package/src/runtime/node/ProjectRunner.ts +2 -2
  221. package/tests/servers/ProxyServer.ts +32 -19
  222. package/tests/servers/express-routes/ApiEndpoint.ts +24 -0
  223. package/tests/servers/express-routes/BasicAuthRoute.ts +36 -0
  224. package/tests/servers/express-routes/BearerAuthRoute.ts +35 -0
  225. package/tests/servers/express-routes/NTLMRoute.ts +2 -3
  226. package/tests/servers/express-routes/PostApi.ts +15 -2
  227. package/tests/servers/express-routes/RedirectsApi.ts +12 -1
  228. package/tests/servers/express-routes/ResponsesApi.ts +1 -1
  229. package/tests/servers/express-routes/StreamApi.ts +19 -0
  230. package/tests/servers/oauth2mock/ServerMock.js +1 -1
  231. package/tests/unit/modeling/domain_model_entities.spec.ts +306 -1
  232. package/tests/unit/runtime/http-engine/HttpResponseParser.spec.ts +337 -0
  233. package/tests/unit/runtime/http-engine/abort.spec.ts +4 -5
  234. package/tests/unit/runtime/http-engine/auth.spec.ts +7 -58
  235. package/tests/unit/runtime/http-engine/certificates/CertificateManager.spec.ts +482 -0
  236. package/tests/unit/runtime/http-engine/certificates.spec.ts +2 -2
  237. package/tests/unit/runtime/http-engine/compression/CompressionManager.spec.ts +498 -0
  238. package/tests/unit/runtime/http-engine/compression.spec.ts +3 -72
  239. package/tests/unit/runtime/http-engine/connections/ConnectionManager.spec.ts +379 -0
  240. package/tests/unit/runtime/http-engine/connections/DigestAuthHandler.spec.ts +164 -0
  241. package/tests/unit/runtime/http-engine/core_engine.spec.ts +561 -0
  242. package/tests/unit/runtime/http-engine/engine_statuses.spec.ts +2 -2
  243. package/tests/unit/runtime/http-engine/events.spec.ts +2 -2
  244. package/tests/unit/runtime/http-engine/headers.spec.ts +2 -88
  245. package/tests/unit/runtime/http-engine/hosts.spec.ts +2 -2
  246. package/tests/unit/runtime/http-engine/http-get.spec.ts +2 -2
  247. package/tests/unit/runtime/http-engine/http-post.spec.ts +2 -2
  248. package/tests/unit/runtime/http-engine/logger.spec.ts +0 -8
  249. package/tests/unit/runtime/http-engine/message.spec.ts +2 -194
  250. package/tests/unit/runtime/http-engine/params.spec.ts +4 -4
  251. package/tests/unit/runtime/http-engine/proxy.spec.ts +15 -14
  252. package/tests/unit/runtime/http-engine/redirects.spec.ts +2 -2
  253. package/tests/unit/runtime/http-engine/responses.spec.ts +170 -277
  254. package/tests/unit/runtime/http-engine/timeout.spec.ts +3 -3
  255. package/tests/unit/runtime/http-engine/timings.spec.ts +2 -2
  256. package/tests/unit/runtime/proxy/HttpProjectProxy.spec.ts +25 -28
  257. package/tests/unit/runtime/runners/project_runner.spec.ts +2 -2
  258. package/tests/unit/runtime/runners/request_runner.spec.ts +2 -2
  259. package/build/src/runtime/http-engine/HttpEngine.d.ts +0 -311
  260. package/build/src/runtime/http-engine/HttpEngine.d.ts.map +0 -1
  261. package/build/src/runtime/http-engine/HttpEngine.js +0 -802
  262. package/build/src/runtime/http-engine/HttpEngine.js.map +0 -1
  263. package/src/runtime/http-engine/HttpEngine.ts +0 -952
  264. package/tests/unit/runtime/http-engine/connecting.spec.ts +0 -140
@@ -1,5 +1,6 @@
1
1
  import formDataConverter from './FormData.js'
2
2
  import { Headers } from '../../lib/headers/Headers.js'
3
+ import { HttpEngineErrorFactory } from './errors/index.js'
3
4
  import {
4
5
  Payload,
5
6
  PayloadSerializer,
@@ -88,5 +89,5 @@ export function payloadToBuffer(headers: Headers, payload?: Payload): Buffer | u
88
89
  return result.buffer
89
90
  }
90
91
 
91
- throw new Error('Unsupported payload message')
92
+ throw HttpEngineErrorFactory.unsupportedPayload(payload.type)
92
93
  }
@@ -0,0 +1,242 @@
1
+ import { Headers } from '../../../lib/headers/Headers.js'
2
+ import { type Logger, type ILogObj } from '../../../lib/logging/index.js'
3
+ import type { INtlmAuthorization, IAuthorizationSettingsUnion } from '../../../models/Authorization.js'
4
+ import { NtlmAuth } from '../ntlm/NtlmAuth.js'
5
+ import type { IRequestAuthState } from '../CoreEngine.js'
6
+ import { HttpEngineErrorFactory } from '../errors/index.js'
7
+ import {
8
+ HEADER_AUTHORIZATION,
9
+ HEADER_CONNECTION,
10
+ HEADER_WWW_AUTHENTICATE,
11
+ NTLM_PREFIX,
12
+ KEEP_ALIVE,
13
+ BASIC_PREFIX,
14
+ } from '../constants.js'
15
+
16
+ export interface ProxyAuthCredentials {
17
+ proxyUsername?: string
18
+ proxyPassword?: string
19
+ proxyAuthorization?: string
20
+ }
21
+
22
+ export interface AuthOptions {
23
+ logger: Logger<ILogObj>
24
+ proxyCredentials?: ProxyAuthCredentials
25
+ }
26
+
27
+ export interface AuthorizationConfig {
28
+ type: string
29
+ enabled: boolean
30
+ config?: IAuthorizationSettingsUnion
31
+ }
32
+
33
+ /**
34
+ * Manages all authentication logic for HTTP requests
35
+ */
36
+ export class AuthManager {
37
+ private logger: Logger<ILogObj>
38
+ private proxyCredentials?: ProxyAuthCredentials
39
+ private authState?: IRequestAuthState
40
+
41
+ constructor(options: AuthOptions) {
42
+ this.logger = options.logger
43
+ this.proxyCredentials = options.proxyCredentials
44
+ }
45
+
46
+ /**
47
+ * Handle all authorization types for request headers.
48
+ * It applied authorization data defined on the request object.
49
+ */
50
+ handleRequestAuthorization(headers: Headers, authorizationConfig?: AuthorizationConfig[], hostname?: string): void {
51
+ if (!authorizationConfig || !Array.isArray(authorizationConfig)) {
52
+ this.logger.debug(`No authorization data on the request. Skipping setting up authorization.`)
53
+ return
54
+ }
55
+
56
+ const enabled = authorizationConfig.filter((i) => i.enabled)
57
+ if (!enabled.length) {
58
+ this.logger.debug(`No enabled authorization data on the request. Skipping setting up authorization.`)
59
+ return
60
+ }
61
+
62
+ const ntlm = enabled.find((i) => i.type === 'ntlm')
63
+ if (ntlm && ntlm.config) {
64
+ this.logger.debug(`Found NTLM authorization config.`)
65
+ this.handleNtlmAuth(ntlm.config as unknown as INtlmAuthorization, headers, hostname)
66
+ }
67
+
68
+ const basic = enabled.find((i) => i.type === 'basic')
69
+ if (basic && basic.config) {
70
+ this.logger.debug(`Found basic authorization config.`)
71
+ const config = basic.config as { username?: string; password?: string }
72
+ if (config.username && config.password) {
73
+ this.handleBasicAuth(config.username, config.password, headers)
74
+ }
75
+ }
76
+
77
+ const bearer = enabled.find((i) => i.type === 'bearer')
78
+ if (bearer && bearer.config) {
79
+ this.logger.debug(`Found bearer authorization config.`)
80
+ const config = bearer.config as { token?: string }
81
+ if (config.token) {
82
+ this.handleBearerAuth(config.token, headers)
83
+ }
84
+ }
85
+ }
86
+
87
+ /**
88
+ * Handle NTLM authentication
89
+ */
90
+ handleNtlmAuth(authData: INtlmAuthorization, headers: Headers, hostname?: string): void {
91
+ try {
92
+ const auth = new NtlmAuth(authData)
93
+ if (!this.authState) {
94
+ this.authState = {
95
+ method: 'ntlm',
96
+ state: 0,
97
+ headers: headers.toString(),
98
+ }
99
+ // Use the provided hostname or empty string for Message1
100
+ const msg = auth.createMessage1(hostname || '')
101
+ headers.set(HEADER_AUTHORIZATION, `${NTLM_PREFIX}${msg.toBase64()}`)
102
+ headers.set(HEADER_CONNECTION, KEEP_ALIVE)
103
+ this.logger.debug('Set NTLM message1 header with hostname:', hostname || '')
104
+ } else if (this.authState && this.authState.state === 1) {
105
+ if (!this.authState.challengeHeader) {
106
+ throw new Error('NTLM challenge header is missing')
107
+ }
108
+ // Use the provided hostname or empty string for Message3
109
+ const msg = auth.createMessage3(this.authState.challengeHeader, hostname || '')
110
+ this.authState.state = 2
111
+ headers.set(HEADER_AUTHORIZATION, `${NTLM_PREFIX}${msg.toBase64()}`)
112
+ this.logger.debug('Set NTLM message3 header with hostname:', hostname || '')
113
+ } else {
114
+ this.logger.warn('Invalid NTLM state:', this.authState?.state)
115
+ }
116
+ } catch (error) {
117
+ this.logger.error('NTLM authentication error:', error)
118
+ delete this.authState
119
+ throw HttpEngineErrorFactory.authenticationError(
120
+ `NTLM authentication failed: ${error instanceof Error ? error.message : 'Unknown error'}`,
121
+ undefined,
122
+ 'ntlm'
123
+ )
124
+ }
125
+ }
126
+
127
+ /**
128
+ * Handle NTLM response from server
129
+ */
130
+ handleNtlmResponse(headers: Headers): boolean {
131
+ if (!this.authState) {
132
+ throw HttpEngineErrorFactory.authenticationError('No auth data.', undefined, 'ntlm')
133
+ }
134
+ this.logger.debug('Handling NTLM response.')
135
+ if (this.authState.state === 0) {
136
+ if (headers.has(HEADER_WWW_AUTHENTICATE)) {
137
+ this.authState.state = 1
138
+ const authHeader = headers.get(HEADER_WWW_AUTHENTICATE)
139
+ // Extract just the base64 part after "NTLM " prefix
140
+ this.authState.challengeHeader = authHeader?.replace(/^NTLM\s+/i, '') || ''
141
+
142
+ if (!this.authState.challengeHeader) {
143
+ this.logger.error('NTLM challenge header is empty after parsing')
144
+ delete this.authState
145
+ return false
146
+ }
147
+
148
+ this.logger.debug('NTLM challenge header was set:', this.authState.challengeHeader)
149
+ return true // Indicates we need to retry with challenge
150
+ }
151
+ }
152
+
153
+ // Authentication complete or failed
154
+ this.logger.debug('NTLM authentication complete or failed.')
155
+ delete this.authState
156
+ return false
157
+ }
158
+
159
+ /**
160
+ * Generate proxy authorization header
161
+ */
162
+ generateProxyAuthHeader(): string | undefined {
163
+ const { proxyUsername, proxyPassword, proxyAuthorization } = this.proxyCredentials || {}
164
+
165
+ if (!proxyUsername || !proxyPassword) {
166
+ if (!proxyAuthorization) {
167
+ return undefined
168
+ }
169
+ return proxyAuthorization
170
+ }
171
+
172
+ if (!proxyAuthorization || proxyAuthorization === 'Basic') {
173
+ // Basic authentication
174
+ const token = Buffer.from(`${proxyUsername}:${proxyPassword}`).toString('base64')
175
+ return `${BASIC_PREFIX}${token}`
176
+ }
177
+
178
+ if (proxyAuthorization === 'Digest') {
179
+ // Note: Digest authentication is handled by ProxyAuthHandler in connections
180
+ // This is just a placeholder for the header format
181
+ this.logger.warn('Digest proxy authentication should be handled by ProxyAuthHandler')
182
+ return undefined
183
+ }
184
+
185
+ throw HttpEngineErrorFactory.authenticationError(
186
+ `Unsupported proxy authorization type: ${proxyAuthorization}`,
187
+ undefined,
188
+ 'proxy'
189
+ )
190
+ }
191
+
192
+ /**
193
+ * Handle Basic authentication
194
+ */
195
+ handleBasicAuth(username: string, password: string, headers: Headers): void {
196
+ const token = Buffer.from(`${username}:${password}`).toString('base64')
197
+ headers.set(HEADER_AUTHORIZATION, `${BASIC_PREFIX}${token}`)
198
+ }
199
+
200
+ /**
201
+ * Handle Bearer token authentication
202
+ */
203
+ handleBearerAuth(token: string, headers: Headers): void {
204
+ headers.set(HEADER_AUTHORIZATION, `Bearer ${token}`)
205
+ }
206
+
207
+ /**
208
+ * Get current authentication state
209
+ */
210
+ getAuthState(): IRequestAuthState | undefined {
211
+ return this.authState
212
+ }
213
+
214
+ /**
215
+ * Set authentication state
216
+ */
217
+ setAuthState(state: IRequestAuthState): void {
218
+ this.authState = state
219
+ }
220
+
221
+ /**
222
+ * Clear authentication state
223
+ */
224
+ clearAuthState(): void {
225
+ delete this.authState
226
+ }
227
+
228
+ /**
229
+ * Check if authentication is in progress
230
+ */
231
+ isAuthenticating(): boolean {
232
+ return !!this.authState
233
+ }
234
+
235
+ /**
236
+ * Check if we have credentials for proxy authentication
237
+ */
238
+ hasProxyCredentials(): boolean {
239
+ const { proxyUsername, proxyPassword, proxyAuthorization } = this.proxyCredentials || {}
240
+ return !!(proxyUsername && proxyPassword) || !!proxyAuthorization
241
+ }
242
+ }
@@ -0,0 +1,74 @@
1
+ import tls from 'tls'
2
+ import { HttpCertificate, IPemCertificate } from '../../../models/ClientCertificate.js'
3
+
4
+ /**
5
+ * Adds client certificate to the request configuration options.
6
+ */
7
+ export function addClientCertificate(certificate: HttpCertificate | null, options: tls.ConnectionOptions): void {
8
+ if (!certificate) {
9
+ return
10
+ }
11
+ const cert = { ...certificate }
12
+ if (cert.type === 'p12') {
13
+ if (!options.pfx) {
14
+ options.pfx = []
15
+ }
16
+ const struct: tls.PxfObject = {
17
+ buf: Buffer.from(cert.cert.data as Buffer),
18
+ }
19
+ if (cert.cert.passphrase) {
20
+ struct.passphrase = cert.cert.passphrase
21
+ }
22
+ if (!Array.isArray(options.pfx)) {
23
+ if (options.pfx) {
24
+ options.pfx = [options.pfx]
25
+ } else {
26
+ options.pfx = []
27
+ }
28
+ }
29
+ options.pfx.push(struct)
30
+ } else if (cert.type === 'pem') {
31
+ const typed = cert as IPemCertificate
32
+ if (!options.cert) {
33
+ options.cert = []
34
+ }
35
+ if (!Array.isArray(options.cert)) {
36
+ if (options.cert) {
37
+ options.cert = [options.cert]
38
+ } else {
39
+ options.cert = []
40
+ }
41
+ }
42
+ const added = Buffer.from(typed.cert.data as Buffer | Uint8Array)
43
+ options.cert.push(added)
44
+ if (typed.certKey) {
45
+ if (!Array.isArray(options.key)) {
46
+ if (options.key) {
47
+ options.key = [options.key]
48
+ } else {
49
+ options.key = []
50
+ }
51
+ }
52
+ const struct: tls.KeyObject = {
53
+ pem: Buffer.from(typed.certKey.data as Buffer | Uint8Array),
54
+ }
55
+ if (typed.certKey.passphrase) {
56
+ struct.passphrase = typed.certKey.passphrase
57
+ }
58
+ options.key.push(struct)
59
+ }
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Checks certificate identity using TLS api.
65
+ */
66
+ export function checkServerIdentity(host: string, cert: tls.PeerCertificate | null): Error | undefined {
67
+ if (!cert) {
68
+ return new Error('Certificate is required')
69
+ }
70
+ const err = tls.checkServerIdentity(host, cert)
71
+ if (err) {
72
+ return err
73
+ }
74
+ }
@@ -0,0 +1,99 @@
1
+ import zlib from 'zlib'
2
+ import { Headers } from '../../../lib/headers/Headers.js'
3
+ import { WithImplicitCoercion } from 'buffer'
4
+
5
+ export interface CompressionOptions {
6
+ aborted?: boolean
7
+ headers?: Headers
8
+ }
9
+
10
+ /**
11
+ * Decompresses received body if `content-encoding` header is set.
12
+ * When multiple encodings are present, they are decoded in reverse order
13
+ * (last applied encoding is decoded first).
14
+ */
15
+ export async function decompress(body?: Buffer, options: CompressionOptions = {}): Promise<Buffer | undefined> {
16
+ const { aborted, headers } = options
17
+ if (aborted || !headers || !body) {
18
+ return
19
+ }
20
+ const ce = headers.get('content-encoding')
21
+ if (!ce) {
22
+ return body
23
+ }
24
+
25
+ // Parse encodings from the header - they are listed in the order they were applied
26
+ const encodings = ce
27
+ .split(',')
28
+ .map((enc) => enc.trim().toLowerCase())
29
+ .filter((enc) => enc.length > 0)
30
+
31
+ if (encodings.length === 0) {
32
+ return body
33
+ }
34
+
35
+ // Decode in reverse order (last applied encoding first)
36
+ let currentBody = body
37
+ for (let i = encodings.length - 1; i >= 0; i--) {
38
+ const encoding = encodings[i]
39
+
40
+ if (encoding === 'deflate') {
41
+ currentBody = await inflate(currentBody)
42
+ } else if (encoding === 'gzip') {
43
+ currentBody = await gunzip(currentBody)
44
+ } else if (encoding === 'br' || encoding === 'brotli') {
45
+ currentBody = await brotli(currentBody)
46
+ }
47
+ // If encoding is unknown, we continue with the current body unchanged
48
+ }
49
+
50
+ return currentBody
51
+ }
52
+
53
+ /**
54
+ * Decompress body with Inflate.
55
+ */
56
+ export function inflate(body: WithImplicitCoercion<ArrayLike<number>>): Promise<Buffer> {
57
+ const buff = Buffer.from(body)
58
+ return new Promise((resolve, reject) => {
59
+ zlib.inflate(buff, (err: Error | null, buffer: Buffer) => {
60
+ if (err) {
61
+ reject(new Error(err.message || String(err)))
62
+ } else {
63
+ resolve(buffer)
64
+ }
65
+ })
66
+ })
67
+ }
68
+
69
+ /**
70
+ * Decompress body with ZLib.
71
+ */
72
+ export function gunzip(body: WithImplicitCoercion<ArrayLike<number>>): Promise<Buffer> {
73
+ const buff = Buffer.from(body)
74
+ return new Promise((resolve, reject) => {
75
+ zlib.gunzip(buff, (err: Error | null, buffer: Buffer) => {
76
+ if (err) {
77
+ reject(new Error(err.message || String(err)))
78
+ } else {
79
+ resolve(buffer)
80
+ }
81
+ })
82
+ })
83
+ }
84
+
85
+ /**
86
+ * Decompress Brotli.
87
+ */
88
+ export function brotli(body: WithImplicitCoercion<ArrayLike<number>>): Promise<Buffer> {
89
+ const buff = Buffer.from(body)
90
+ return new Promise((resolve, reject) => {
91
+ zlib.brotliDecompress(buff, (err: Error | null, buffer: Buffer) => {
92
+ if (err) {
93
+ reject(err)
94
+ } else {
95
+ resolve(buffer)
96
+ }
97
+ })
98
+ })
99
+ }
@@ -0,0 +1,123 @@
1
+ import net from 'net'
2
+ import { type Logger, type ILogObj } from '../../../lib/logging/index.js'
3
+ import type { RequestStats, HttpEngineOptions } from '../CoreEngine.js'
4
+ import { DirectConnection } from './DirectConnection.js'
5
+ import { ProxyConnection } from './ProxyConnection.js'
6
+ import { TunnelConnection } from './TunnelConnection.js'
7
+ import { HttpCertificate } from '../../../models/ClientCertificate.js'
8
+
9
+ export interface ConnectionOptions {
10
+ host: string
11
+ port: number
12
+ protocol?: string
13
+ timeout?: number
14
+ validateCertificates?: boolean
15
+ certificates?: HttpCertificate[]
16
+ logger: Logger<ILogObj>
17
+ stats: RequestStats
18
+ }
19
+
20
+ export interface ProxyConnectionOptions extends ConnectionOptions {
21
+ proxy: string
22
+ proxyIsSsl: boolean
23
+ targetUrl: string
24
+ proxyUsername?: string
25
+ proxyPassword?: string
26
+ /**
27
+ * The type of proxy authentication to use.
28
+ */
29
+ proxyAuthorization?: 'Basic'
30
+ }
31
+
32
+ export interface TunnelConnectionOptions extends ProxyConnectionOptions {
33
+ targetHost: string
34
+ targetPort: number
35
+ }
36
+
37
+ /**
38
+ * Manages different types of HTTP connections
39
+ */
40
+ export class ConnectionManager {
41
+ private logger: Logger<ILogObj>
42
+ private stats: RequestStats
43
+
44
+ constructor(logger: Logger<ILogObj>, stats: RequestStats) {
45
+ this.logger = logger
46
+ this.stats = stats
47
+ }
48
+
49
+ /**
50
+ * Create a direct connection (HTTP or HTTPS)
51
+ */
52
+ createDirectConnection(options: ConnectionOptions): DirectConnection {
53
+ return new DirectConnection(options)
54
+ }
55
+
56
+ /**
57
+ * Create a proxy connection for HTTP requests
58
+ */
59
+ createProxyConnection(options: ProxyConnectionOptions): ProxyConnection {
60
+ return new ProxyConnection(options)
61
+ }
62
+
63
+ /**
64
+ * Create a tunnel connection for HTTPS requests through proxy
65
+ */
66
+ createTunnelConnection(options: TunnelConnectionOptions): TunnelConnection {
67
+ return new TunnelConnection(options)
68
+ }
69
+
70
+ /**
71
+ * Determine the appropriate connection type and create it
72
+ */
73
+ createConnection(
74
+ host: string,
75
+ port: number,
76
+ protocol: string,
77
+ engineOptions: HttpEngineOptions
78
+ ): Promise<net.Socket> {
79
+ const connectionOptions: ConnectionOptions = {
80
+ host,
81
+ port,
82
+ protocol,
83
+ timeout: engineOptions.timeout,
84
+ validateCertificates: engineOptions.validateCertificates,
85
+ certificates: engineOptions.certificates,
86
+ logger: this.logger,
87
+ stats: this.stats,
88
+ }
89
+
90
+ // Direct connection
91
+ if (!engineOptions.proxy) {
92
+ this.logger.debug('Deciding to create a direct connection...')
93
+ const directConnection = this.createDirectConnection(connectionOptions)
94
+ return directConnection.connect()
95
+ }
96
+
97
+ // Proxy connection
98
+ const proxyOptions: ProxyConnectionOptions = {
99
+ ...connectionOptions,
100
+ proxy: engineOptions.proxy,
101
+ proxyIsSsl: engineOptions.proxy.startsWith('https:'),
102
+ targetUrl: `${protocol}//${host}:${port}`,
103
+ proxyUsername: engineOptions.proxyUsername,
104
+ proxyPassword: engineOptions.proxyPassword,
105
+ proxyAuthorization: engineOptions.proxyAuthorization,
106
+ }
107
+
108
+ // Check if we need a tunnel (HTTPS target through proxy)
109
+ if (protocol === 'https:') {
110
+ this.logger.debug('Deciding to create a tunnel connection through the proxy...')
111
+ const tunnelOptions: TunnelConnectionOptions = {
112
+ ...proxyOptions,
113
+ targetHost: host,
114
+ targetPort: port,
115
+ }
116
+ const tunnelConnection = this.createTunnelConnection(tunnelOptions)
117
+ return tunnelConnection.connect()
118
+ }
119
+ this.logger.debug('Deciding to create a proxy connection...')
120
+ const proxyConnection = this.createProxyConnection(proxyOptions)
121
+ return proxyConnection.connect()
122
+ }
123
+ }