@api-client/core 0.3.3 → 0.3.6
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/build/browser.d.ts +3 -0
- package/build/browser.js +9 -0
- package/build/browser.js.map +1 -1
- package/build/index.d.ts +11 -2
- package/build/index.js +20 -2
- package/build/index.js.map +1 -1
- package/build/src/lib/fs/Fs.d.ts +52 -0
- package/build/src/lib/fs/Fs.js +245 -0
- package/build/src/lib/fs/Fs.js.map +1 -0
- package/build/src/lib/timers/Timers.d.ts +5 -0
- package/build/src/lib/timers/Timers.js +10 -0
- package/build/src/lib/timers/Timers.js.map +1 -0
- package/build/src/mocking/ProjectMock.d.ts +13 -0
- package/build/src/mocking/ProjectMock.js +16 -0
- package/build/src/mocking/ProjectMock.js.map +1 -0
- package/build/src/mocking/lib/Request.d.ts +32 -0
- package/build/src/mocking/lib/Request.js +63 -0
- package/build/src/mocking/lib/Request.js.map +1 -0
- package/build/src/mocking/lib/Response.d.ts +33 -0
- package/build/src/mocking/lib/Response.js +79 -0
- package/build/src/mocking/lib/Response.js.map +1 -0
- package/build/src/models/ErrorResponse.d.ts +5 -4
- package/build/src/models/ErrorResponse.js +18 -5
- package/build/src/models/ErrorResponse.js.map +1 -1
- package/build/src/models/SerializableError.d.ts +30 -0
- package/build/src/models/SerializableError.js +63 -0
- package/build/src/models/SerializableError.js.map +1 -0
- package/build/src/runtime/http-engine/ArcEngine.js +8 -4
- package/build/src/runtime/http-engine/ArcEngine.js.map +1 -1
- package/build/src/runtime/http-engine/HttpEngine.d.ts +3 -3
- package/build/src/runtime/http-engine/HttpEngine.js +3 -3
- package/build/src/runtime/http-engine/HttpEngine.js.map +1 -1
- package/build/src/runtime/http-engine/NodeEngine.js +9 -4
- package/build/src/runtime/http-engine/NodeEngine.js.map +1 -1
- package/build/src/runtime/http-engine/NodeEngineDirect.js +8 -2
- package/build/src/runtime/http-engine/NodeEngineDirect.js.map +1 -1
- package/build/src/runtime/node/BaseRunner.d.ts +21 -0
- package/build/src/runtime/node/BaseRunner.js +27 -0
- package/build/src/runtime/node/BaseRunner.js.map +1 -0
- package/build/src/runtime/node/ProjectParallelRunner.d.ts +81 -0
- package/build/src/runtime/node/ProjectParallelRunner.js +173 -0
- package/build/src/runtime/node/ProjectParallelRunner.js.map +1 -0
- package/build/src/runtime/node/ProjectRequestRunner.d.ts +125 -0
- package/build/src/runtime/node/ProjectRequestRunner.js +185 -0
- package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -0
- package/build/src/runtime/node/ProjectRunner.d.ts +166 -64
- package/build/src/runtime/node/ProjectRunner.js +191 -139
- package/build/src/runtime/node/ProjectRunner.js.map +1 -1
- package/build/src/runtime/node/ProjectRunnerWorker.d.ts +1 -0
- package/build/src/runtime/node/ProjectRunnerWorker.js +58 -0
- package/build/src/runtime/node/ProjectRunnerWorker.js.map +1 -0
- package/build/src/runtime/node/ProjectSerialRunner.d.ts +11 -0
- package/build/src/runtime/node/ProjectSerialRunner.js +34 -0
- package/build/src/runtime/node/ProjectSerialRunner.js.map +1 -0
- package/build/src/runtime/reporters/ProjectRunCliReporter.d.ts +7 -0
- package/build/src/runtime/reporters/ProjectRunCliReporter.js +73 -0
- package/build/src/runtime/reporters/ProjectRunCliReporter.js.map +1 -0
- package/build/src/runtime/reporters/Reporter.d.ts +62 -0
- package/build/src/runtime/reporters/Reporter.js +98 -0
- package/build/src/runtime/reporters/Reporter.js.map +1 -0
- package/build/src/testing/TestCliHelper.d.ts +23 -0
- package/build/src/testing/TestCliHelper.js +71 -0
- package/build/src/testing/TestCliHelper.js.map +1 -0
- package/build/src/testing/getPort.d.ts +52 -0
- package/build/src/testing/getPort.js +169 -0
- package/build/src/testing/getPort.js.map +1 -0
- package/package.json +2 -1
- package/src/data/DataReader.ts +11 -0
- package/src/data/DataUtils.ts +108 -0
- package/src/data/JmesparthReader.ts +26 -0
- package/src/data/Json2Xml.ts +190 -0
- package/src/data/JsonReader.ts +41 -0
- package/src/data/PayloadPointer.ts +48 -0
- package/src/data/RequestDataExtractor.ts +133 -0
- package/src/data/UrlEncodedReader.ts +20 -0
- package/src/data/XmlReader.ts +103 -0
- package/src/events/BaseEvents.ts +259 -0
- package/src/events/CustomEvent.ts +27 -0
- package/src/events/EventTypes.ts +19 -0
- package/src/events/Events.ts +19 -0
- package/src/events/authorization/AuthorizationEventTypes.ts +22 -0
- package/src/events/authorization/AuthorizationEvents.ts +61 -0
- package/src/events/cookies/CookieEventTypes.ts +13 -0
- package/src/events/cookies/CookieEvents.ts +157 -0
- package/src/events/encryption/EncryptionEventTypes.ts +4 -0
- package/src/events/encryption/EncryptionEvents.ts +51 -0
- package/src/events/environment/EnvironmentEventTypes.ts +3 -0
- package/src/events/environment/EnvironmentEvents.ts +24 -0
- package/src/events/models/ClientCertificateEvents.ts +87 -0
- package/src/events/models/ModelEventTypes.ts +47 -0
- package/src/events/models/ModelEvents.ts +7 -0
- package/src/events/models/ProjectEvents.ts +331 -0
- package/src/events/process/ProcessEventTypes.ts +5 -0
- package/src/events/process/ProcessEvents.ts +76 -0
- package/src/events/readme.md +22 -0
- package/src/events/reporting/ReportingEventTypes.ts +3 -0
- package/src/events/reporting/ReportingEvents.ts +28 -0
- package/src/events/telemetry/TelemetryEventTypes.ts +10 -0
- package/src/events/telemetry/TelemetryEvents.ts +156 -0
- package/src/lib/cookies/Cookie.ts +312 -0
- package/src/lib/cookies/Cookies.ts +326 -0
- package/src/lib/cookies/Utils.ts +168 -0
- package/src/lib/fs/Fs.ts +258 -0
- package/src/lib/headers/Headers.ts +219 -0
- package/src/lib/logging/DefaultLogger.ts +19 -0
- package/src/lib/logging/DummyLogger.ts +21 -0
- package/src/lib/logging/Logger.ts +16 -0
- package/src/lib/timers/Timers.ts +9 -0
- package/src/lib/transformers/PayloadSerializer.ts +332 -0
- package/src/lib/transformers/Utils.ts +18 -0
- package/src/lib/uuid.ts +40 -0
- package/src/mocking/LegacyInterfaces.ts +52 -0
- package/src/mocking/LegacyMock.ts +37 -0
- package/src/mocking/ProjectMock.ts +20 -0
- package/src/mocking/legacy/Authorization.ts +39 -0
- package/src/mocking/legacy/Certificates.ts +145 -0
- package/src/mocking/legacy/Cookies.ts +51 -0
- package/src/mocking/legacy/HostRules.ts +43 -0
- package/src/mocking/legacy/Http.ts +236 -0
- package/src/mocking/legacy/HttpResponse.ts +106 -0
- package/src/mocking/legacy/RestApi.ts +68 -0
- package/src/mocking/legacy/Urls.ts +44 -0
- package/src/mocking/legacy/Variables.ts +53 -0
- package/src/mocking/lib/Request.ts +85 -0
- package/src/mocking/lib/Response.ts +101 -0
- package/src/models/ArcResponse.ts +166 -0
- package/src/models/Authorization.ts +481 -0
- package/src/models/AuthorizationData.ts +60 -0
- package/src/models/Backend.ts +107 -0
- package/src/models/ClientCertificate.ts +68 -0
- package/src/models/Environment.ts +279 -0
- package/src/models/ErrorResponse.ts +113 -0
- package/src/models/HistoryIndex.ts +76 -0
- package/src/models/HistoryRequest.ts +28 -0
- package/src/models/HostRule.ts +163 -0
- package/src/models/HttpCookie.ts +285 -0
- package/src/models/HttpProject.ts +1294 -0
- package/src/models/HttpProjectListItem.ts +23 -0
- package/src/models/HttpRequest.ts +124 -0
- package/src/models/HttpResponse.ts +143 -0
- package/src/models/License.ts +113 -0
- package/src/models/ProjectDefinitionProperty.ts +40 -0
- package/src/models/ProjectFolder.ts +439 -0
- package/src/models/ProjectItem.ts +135 -0
- package/src/models/ProjectParent.ts +113 -0
- package/src/models/ProjectRequest.ts +277 -0
- package/src/models/ProjectSchema.ts +202 -0
- package/src/models/Property.ts +423 -0
- package/src/models/Provider.ts +98 -0
- package/src/models/README.md +20 -0
- package/src/models/Request.ts +452 -0
- package/src/models/RequestActions.ts +163 -0
- package/src/models/RequestAuthorization.ts +115 -0
- package/src/models/RequestConfig.ts +317 -0
- package/src/models/RequestLog.ts +159 -0
- package/src/models/RequestTime.ts +108 -0
- package/src/models/RequestUiMeta.ts +258 -0
- package/src/models/RequestsSize.ts +65 -0
- package/src/models/ResponseAuthorization.ts +104 -0
- package/src/models/ResponseRedirect.ts +158 -0
- package/src/models/RevisionInfo.ts +37 -0
- package/src/models/SentRequest.ts +125 -0
- package/src/models/SerializableError.ts +80 -0
- package/src/models/SerializablePayload.ts +68 -0
- package/src/models/Server.ts +153 -0
- package/src/models/Thing.ts +110 -0
- package/src/models/Url.ts +90 -0
- package/src/models/User.ts +120 -0
- package/src/models/WebApi.ts +234 -0
- package/src/models/WebApiIndex.ts +122 -0
- package/src/models/Workspace.ts +182 -0
- package/src/models/actions/Action.ts +213 -0
- package/src/models/actions/ActionView.ts +40 -0
- package/src/models/actions/Condition.ts +207 -0
- package/src/models/actions/ConditionView.ts +42 -0
- package/src/models/actions/Enums.ts +29 -0
- package/src/models/actions/RunnableAction.ts +144 -0
- package/src/models/actions/runnable/DeleteCookieAction.ts +113 -0
- package/src/models/actions/runnable/Runnable.ts +9 -0
- package/src/models/actions/runnable/SetCookieAction.ts +216 -0
- package/src/models/actions/runnable/SetVariableAction.ts +81 -0
- package/src/models/legacy/DataExport.ts +172 -0
- package/src/models/legacy/Normalizer.ts +110 -0
- package/src/models/legacy/actions/Actions.ts +269 -0
- package/src/models/legacy/authorization/Authorization.ts +572 -0
- package/src/models/legacy/models/ApiTypes.ts +202 -0
- package/src/models/legacy/models/ArcLegacyProject.ts +39 -0
- package/src/models/legacy/models/AuthData.ts +17 -0
- package/src/models/legacy/models/ClientCertificate.ts +95 -0
- package/src/models/legacy/models/Cookies.ts +52 -0
- package/src/models/legacy/models/HostRule.ts +35 -0
- package/src/models/legacy/models/RestApi.ts +49 -0
- package/src/models/legacy/models/UrlHistory.ts +37 -0
- package/src/models/legacy/models/Variable.ts +43 -0
- package/src/models/legacy/models/base.d.ts +95 -0
- package/src/models/legacy/request/ArcRequest.ts +405 -0
- package/src/models/legacy/request/ArcResponse.ts +177 -0
- package/src/models/legacy/request/HistoryData.ts +47 -0
- package/src/models/legacy/request/Legacy.ts +45 -0
- package/src/models/legacy/request/RequestBody.ts +87 -0
- package/src/models/transformers/ArcDexieTransformer.ts +323 -0
- package/src/models/transformers/ArcLegacyNormalizer.ts +85 -0
- package/src/models/transformers/ArcLegacyTransformer.ts +200 -0
- package/src/models/transformers/ArcPouchTransformer.ts +184 -0
- package/src/models/transformers/BaseTransformer.ts +116 -0
- package/src/models/transformers/ImportUtils.ts +141 -0
- package/src/models/transformers/LegacyDataExportToApiProject.ts +76 -0
- package/src/models/transformers/LegacyExportProcessor.ts +252 -0
- package/src/models/transformers/PostmanBackupTransformer.ts +306 -0
- package/src/models/transformers/PostmanDataTransformer.ts +50 -0
- package/src/models/transformers/PostmanTransformer.ts +106 -0
- package/src/models/transformers/PostmanV21Transformer.ts +311 -0
- package/src/models/transformers/PostmanV2Transformer.ts +308 -0
- package/src/models/transformers/har.ts +865 -0
- package/src/runtime/actions/ActionRunner.ts +83 -0
- package/src/runtime/actions/ConditionRunner.ts +194 -0
- package/src/runtime/actions/RunnableCondition.ts +57 -0
- package/src/runtime/actions/runnable/ActionRunnable.ts +19 -0
- package/src/runtime/actions/runnable/DeleteCookieRunnable.ts +39 -0
- package/src/runtime/actions/runnable/SetCookieRunnable.ts +92 -0
- package/src/runtime/actions/runnable/SetVariableRunnable.ts +53 -0
- package/src/runtime/http-engine/ArcEngine.ts +1068 -0
- package/src/runtime/http-engine/FormData.ts +85 -0
- package/src/runtime/http-engine/HttpEngine.ts +874 -0
- package/src/runtime/http-engine/HttpErrorCodes.ts +270 -0
- package/src/runtime/http-engine/NodeEngine.ts +792 -0
- package/src/runtime/http-engine/NodeEngineDirect.ts +482 -0
- package/src/runtime/http-engine/PayloadSupport.ts +84 -0
- package/src/runtime/http-engine/RequestUtils.ts +164 -0
- package/src/runtime/http-engine/ntlm/Des.ts +345 -0
- package/src/runtime/http-engine/ntlm/MD4.ts +135 -0
- package/src/runtime/http-engine/ntlm/NtlmAuth.ts +186 -0
- package/src/runtime/http-engine/ntlm/NtlmMessage.ts +57 -0
- package/src/runtime/modules/BasicAuthCache.ts +133 -0
- package/src/runtime/modules/ExecutionResponse.ts +4 -0
- package/src/runtime/modules/ModulesRegistry.ts +136 -0
- package/src/runtime/modules/RequestAuthorization.ts +110 -0
- package/src/runtime/modules/RequestCookies.ts +145 -0
- package/src/runtime/node/BaseRunner.ts +29 -0
- package/src/runtime/node/ProjectParallelRunner.ts +234 -0
- package/src/runtime/node/ProjectRequestRunner.ts +281 -0
- package/src/runtime/node/ProjectRunner.ts +374 -0
- package/src/runtime/node/ProjectRunnerWorker.ts +62 -0
- package/src/runtime/node/ProjectSerialRunner.ts +36 -0
- package/src/runtime/node/RequestFactory.ts +422 -0
- package/src/runtime/node/VariablesStore.ts +25 -0
- package/src/runtime/reporters/ProjectRunCliReporter.ts +79 -0
- package/src/runtime/reporters/Reporter.ts +142 -0
- package/src/runtime/store/StoreSdk.ts +838 -0
- package/src/runtime/variables/Cache.ts +53 -0
- package/src/runtime/variables/EvalFunctions.ts +132 -0
- package/src/runtime/variables/ProjectVariables.ts +6 -0
- package/src/runtime/variables/VariablesProcessor.ts +543 -0
- package/src/runtime/variables/VariablesTokenizer.ts +55 -0
- package/src/testing/TestCliHelper.ts +76 -0
- package/src/testing/getPort.ts +212 -0
- package/build/src/runtime/http-engine/Errors.d.ts +0 -10
- package/build/src/runtime/http-engine/Errors.js +0 -14
- package/build/src/runtime/http-engine/Errors.js.map +0 -1
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
3
|
+
import { URL, UrlWithStringQuery } from 'url';
|
|
4
|
+
import http from 'http';
|
|
5
|
+
import https from 'https';
|
|
6
|
+
import net from 'net';
|
|
7
|
+
import { HttpEngine, HttpEngineOptions, ResponseErrorInit, HeadersReceivedDetail } from './HttpEngine.js';
|
|
8
|
+
import { IRequestLog } from 'src/models/RequestLog.js';
|
|
9
|
+
import { IHttpRequest } from '../../models/HttpRequest.js';
|
|
10
|
+
import { ArcResponse } from '../../models/ArcResponse.js';
|
|
11
|
+
import { Headers } from '../../lib/headers/Headers.js';
|
|
12
|
+
import { SerializableError } from '../../models/SerializableError.js';
|
|
13
|
+
import { PayloadSupport } from './PayloadSupport.js';
|
|
14
|
+
import { addContentLength, getPort } from './RequestUtils.js';
|
|
15
|
+
|
|
16
|
+
interface IHttpOutputData {
|
|
17
|
+
data: string | Buffer;
|
|
18
|
+
encoding?: string;
|
|
19
|
+
callback?: Function;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A class that makes HTTP requests using Node's HTTP libraries without a proxy.
|
|
24
|
+
*/
|
|
25
|
+
export class NodeEngineDirect extends HttpEngine {
|
|
26
|
+
responseReported = false;
|
|
27
|
+
|
|
28
|
+
_sentHttpMessage?: string | IHttpOutputData[];
|
|
29
|
+
|
|
30
|
+
client?: http.ClientRequest;
|
|
31
|
+
receivingResponse = false;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The agent used to manage the connection.
|
|
35
|
+
* Note, the agent may change, especially when redirecting between protocols.
|
|
36
|
+
*/
|
|
37
|
+
agent: http.Agent | https.Agent;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Prepared to be send payload part of the HTTP message.
|
|
41
|
+
*/
|
|
42
|
+
httpMessage?: Buffer;
|
|
43
|
+
|
|
44
|
+
constructor(request: IHttpRequest, opts: HttpEngineOptions = {}) {
|
|
45
|
+
super(request, opts);
|
|
46
|
+
|
|
47
|
+
const port = getPort(this.uri.port, this.uri.protocol);
|
|
48
|
+
if (port === 443 || this.uri.protocol === 'https:') {
|
|
49
|
+
this.agent = this.httpsAgent(this.uri);
|
|
50
|
+
} else {
|
|
51
|
+
this.agent = this.httpAgent(this.uri);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
this._connectHandler = this._connectHandler.bind(this);
|
|
55
|
+
this._secureConnectHandler = this._secureConnectHandler.bind(this);
|
|
56
|
+
this._responseHandler = this._responseHandler.bind(this);
|
|
57
|
+
this._timeoutHandler = this._timeoutHandler.bind(this);
|
|
58
|
+
this._errorHandler = this._errorHandler.bind(this);
|
|
59
|
+
this._lookupHandler = this._lookupHandler.bind(this);
|
|
60
|
+
this._closeHandler = this._closeHandler.bind(this);
|
|
61
|
+
this._socketHandler = this._socketHandler.bind(this);
|
|
62
|
+
this._sendEndHandler = this._sendEndHandler.bind(this);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Sends the request
|
|
67
|
+
*/
|
|
68
|
+
async send(): Promise<IRequestLog> {
|
|
69
|
+
const promise = this.wrapExecution();
|
|
70
|
+
this.sendRequest();
|
|
71
|
+
return promise;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
private async sendRequest(): Promise<void> {
|
|
75
|
+
try {
|
|
76
|
+
const headers = new Headers(this.request.headers);
|
|
77
|
+
this.prepareHeaders(headers);
|
|
78
|
+
const message = await this._prepareMessage(headers);
|
|
79
|
+
if (message) {
|
|
80
|
+
this.httpMessage = message;
|
|
81
|
+
}
|
|
82
|
+
this.sentRequest.headers = headers.toString();
|
|
83
|
+
const request = this._connect(message);
|
|
84
|
+
this.client = request;
|
|
85
|
+
const { timeout } = this;
|
|
86
|
+
if (timeout > 0) {
|
|
87
|
+
request.setTimeout(timeout);
|
|
88
|
+
}
|
|
89
|
+
} catch (cause) {
|
|
90
|
+
console.warn(cause);
|
|
91
|
+
const e = cause as any;
|
|
92
|
+
const err = new SerializableError(e.message, { cause: e });
|
|
93
|
+
if (e.code || e.code === 0) {
|
|
94
|
+
err.code = e.code as string;
|
|
95
|
+
}
|
|
96
|
+
this.finalizeRequest(e);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Prepares the request body (the payload) and the headers.
|
|
103
|
+
*
|
|
104
|
+
* @return Resolved promise to a `Buffer`. Undefined when no message.
|
|
105
|
+
*/
|
|
106
|
+
async _prepareMessage(headers: Headers): Promise<Buffer|undefined> {
|
|
107
|
+
const { method='GET' } = this.request;
|
|
108
|
+
let { payload } = this.request;
|
|
109
|
+
if (['get', 'head'].includes(method.toLowerCase())) {
|
|
110
|
+
payload = undefined;
|
|
111
|
+
}
|
|
112
|
+
let buffer: Buffer | undefined;
|
|
113
|
+
if (payload) {
|
|
114
|
+
buffer = await PayloadSupport.payloadToBuffer(payload, headers);
|
|
115
|
+
if (buffer) {
|
|
116
|
+
addContentLength(this.request.method || 'GET', buffer, headers);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return buffer;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Connects to the remote machine.
|
|
124
|
+
*/
|
|
125
|
+
_connect(message?: Buffer): http.ClientRequest {
|
|
126
|
+
const uri = new URL(this.request.url);
|
|
127
|
+
const port = getPort(uri.port, uri.protocol);
|
|
128
|
+
if (port === 443 || uri.protocol === 'https:') {
|
|
129
|
+
return this._connectHttps(uri, message);
|
|
130
|
+
}
|
|
131
|
+
return this._connectHttp(uri, message);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Creates a connection using regular transport.
|
|
136
|
+
*/
|
|
137
|
+
_connectHttp(uri: URL, message?: Buffer): http.ClientRequest {
|
|
138
|
+
if (!uri.port) {
|
|
139
|
+
uri.port = '80';
|
|
140
|
+
}
|
|
141
|
+
const options: http.RequestOptions = {
|
|
142
|
+
agent: this.agent,
|
|
143
|
+
protocol: uri.protocol,
|
|
144
|
+
host: uri.hostname,
|
|
145
|
+
method: this.request.method.toUpperCase(),
|
|
146
|
+
path: `${uri.pathname}${uri.search}`,
|
|
147
|
+
headers: {},
|
|
148
|
+
};
|
|
149
|
+
if (uri.port) {
|
|
150
|
+
options.port = uri.port;
|
|
151
|
+
}
|
|
152
|
+
this.appendHeaders(options);
|
|
153
|
+
const startTime = Date.now();
|
|
154
|
+
this.stats.startTime = startTime;
|
|
155
|
+
this.sentRequest.startTime = startTime;
|
|
156
|
+
|
|
157
|
+
const request = http.request(options);
|
|
158
|
+
this._setListeners(request);
|
|
159
|
+
if (message) {
|
|
160
|
+
request.write(message);
|
|
161
|
+
}
|
|
162
|
+
this.stats.messageStart = Date.now();
|
|
163
|
+
request.end();
|
|
164
|
+
// This is a hack to read sent data.
|
|
165
|
+
// In the https://github.com/nodejs/node/blob/0a62026f32d513a8a5d9ed857481df5f5fa18e8b/lib/_http_outgoing.js#L960
|
|
166
|
+
// library it hold the data until it is flushed.
|
|
167
|
+
// @ts-ignore
|
|
168
|
+
const pending = request.outputData as IHttpOutputData[];
|
|
169
|
+
if (Array.isArray(pending)) {
|
|
170
|
+
this._sentHttpMessage = pending;
|
|
171
|
+
}
|
|
172
|
+
try {
|
|
173
|
+
this.emit('loadstart');
|
|
174
|
+
} catch (_) {
|
|
175
|
+
//
|
|
176
|
+
}
|
|
177
|
+
return request;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Creates a connection using SSL transport.
|
|
182
|
+
*/
|
|
183
|
+
_connectHttps(uri: URL, message?: Buffer): http.ClientRequest {
|
|
184
|
+
if (!uri.port) {
|
|
185
|
+
uri.port = '443';
|
|
186
|
+
}
|
|
187
|
+
// const options = this._createGenericOptions(uri);
|
|
188
|
+
// this._addSslOptions(options);
|
|
189
|
+
|
|
190
|
+
const options: https.RequestOptions = {
|
|
191
|
+
agent: this.agent,
|
|
192
|
+
protocol: uri.protocol,
|
|
193
|
+
host: uri.hostname,
|
|
194
|
+
method: this.request.method.toUpperCase(),
|
|
195
|
+
path: `${uri.pathname}${uri.search}`,
|
|
196
|
+
headers: {},
|
|
197
|
+
};
|
|
198
|
+
if (uri.port) {
|
|
199
|
+
options.port = uri.port;
|
|
200
|
+
}
|
|
201
|
+
this.appendHeaders(options);
|
|
202
|
+
|
|
203
|
+
const startTime = Date.now();
|
|
204
|
+
this.stats.startTime = startTime;
|
|
205
|
+
this.sentRequest.startTime = startTime;
|
|
206
|
+
|
|
207
|
+
const request = https.request(options);
|
|
208
|
+
this._setListeners(request);
|
|
209
|
+
if (message) {
|
|
210
|
+
request.write(message);
|
|
211
|
+
}
|
|
212
|
+
this.stats.messageStart = Date.now();
|
|
213
|
+
this.stats.sentTime = this.stats.messageStart + 1;
|
|
214
|
+
request.end();
|
|
215
|
+
// This is a hack to read sent data.
|
|
216
|
+
// In the https://github.com/nodejs/node/blob/0a62026f32d513a8a5d9ed857481df5f5fa18e8b/lib/_http_outgoing.js#L960
|
|
217
|
+
// library it hold the data until it is flushed.
|
|
218
|
+
// @ts-ignore
|
|
219
|
+
const pending = request.outputData as IHttpOutputData[];
|
|
220
|
+
if (Array.isArray(pending)) {
|
|
221
|
+
this._sentHttpMessage = pending;
|
|
222
|
+
}
|
|
223
|
+
try {
|
|
224
|
+
this.emit('loadstart');
|
|
225
|
+
} catch (_) {
|
|
226
|
+
//
|
|
227
|
+
}
|
|
228
|
+
return request;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Sets listeners on a socket
|
|
233
|
+
* @param request The request object
|
|
234
|
+
*/
|
|
235
|
+
_setListeners(request: http.ClientRequest): void {
|
|
236
|
+
request.once('socket', this._socketHandler);
|
|
237
|
+
request.once('error', this._errorHandler);
|
|
238
|
+
request.once('response', this._responseHandler);
|
|
239
|
+
request.once('close', this._closeHandler);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Handler for connection error.
|
|
244
|
+
*/
|
|
245
|
+
_errorHandler(e: ResponseErrorInit): void {
|
|
246
|
+
if (this.aborted) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
this._errorRequest({
|
|
250
|
+
code: e.code,
|
|
251
|
+
message: e.message,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Handler for DNS lookup.
|
|
257
|
+
*/
|
|
258
|
+
_lookupHandler(): void {
|
|
259
|
+
this.stats.lookupTime = Date.now();
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Handler for connected event.
|
|
264
|
+
*/
|
|
265
|
+
_secureConnectHandler(): void {
|
|
266
|
+
this.stats.secureConnectedTime = Date.now();
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Handler for connecting event.
|
|
271
|
+
*/
|
|
272
|
+
_connectHandler(): void {
|
|
273
|
+
this.stats.connectedTime = Date.now();
|
|
274
|
+
this.stats.secureStartTime = Date.now();
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Handler for sending finished event
|
|
279
|
+
*/
|
|
280
|
+
_sendEndHandler(): void {
|
|
281
|
+
if (!this.stats.sentTime) {
|
|
282
|
+
this.stats.sentTime = Date.now();
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Handler for timeout event
|
|
288
|
+
*/
|
|
289
|
+
_timeoutHandler(): void {
|
|
290
|
+
this._errorRequest({
|
|
291
|
+
code: 7,
|
|
292
|
+
});
|
|
293
|
+
this.abort();
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* A handler for response data event
|
|
298
|
+
*/
|
|
299
|
+
_responseHandler(res: http.IncomingMessage): void {
|
|
300
|
+
this.receivingResponse = true;
|
|
301
|
+
|
|
302
|
+
this.emit('firstbyte');
|
|
303
|
+
this.stats.firstReceiveTime = Date.now();
|
|
304
|
+
this.stats.responseTime = Date.now();
|
|
305
|
+
if (this._sentHttpMessage) {
|
|
306
|
+
this.sentRequest.httpMessage = this._readSentMessage(this._sentHttpMessage);
|
|
307
|
+
} else {
|
|
308
|
+
this.sentRequest.httpMessage = '';
|
|
309
|
+
}
|
|
310
|
+
const status = res.statusCode;
|
|
311
|
+
if (!status) {
|
|
312
|
+
this._errorRequest({
|
|
313
|
+
message: 'The response has no status.',
|
|
314
|
+
});
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
const headers = this.computeResponseHeaders(res);
|
|
318
|
+
const rawHeaders = headers.toString();
|
|
319
|
+
const response = ArcResponse.fromValues(status, res.statusMessage, rawHeaders);
|
|
320
|
+
this.currentResponse = response;
|
|
321
|
+
this.currentHeaders = headers;
|
|
322
|
+
const detail: HeadersReceivedDetail = {
|
|
323
|
+
returnValue: true,
|
|
324
|
+
value: rawHeaders,
|
|
325
|
+
};
|
|
326
|
+
this.emit('headersreceived', detail);
|
|
327
|
+
if (!detail.returnValue) {
|
|
328
|
+
this.abort();
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
res.on('data', (chunk) => {
|
|
332
|
+
if (!this._rawBody) {
|
|
333
|
+
this._rawBody = chunk;
|
|
334
|
+
} else {
|
|
335
|
+
const endTime = Date.now();
|
|
336
|
+
this.stats.lastReceivedTime = endTime;
|
|
337
|
+
this._rawBody = Buffer.concat([this._rawBody, chunk]);
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
res.once('end', () => {
|
|
341
|
+
const endTime = Date.now();
|
|
342
|
+
this.sentRequest.endTime = endTime;
|
|
343
|
+
this.stats.receivingTime = endTime;
|
|
344
|
+
this._reportResponse();
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Handler for connection close event
|
|
350
|
+
*/
|
|
351
|
+
_closeHandler(): void {
|
|
352
|
+
if (this.responseReported || this.receivingResponse || this.aborted || this.redirecting) {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
if (!this.currentResponse) {
|
|
356
|
+
const e = new Error('Connection closed unexpectedly.');
|
|
357
|
+
this._errorRequest(e);
|
|
358
|
+
} else {
|
|
359
|
+
// There is an issue with the response. Size mismatch? Anyway,
|
|
360
|
+
// it tries to create a response from current data.
|
|
361
|
+
this.emit('loadend');
|
|
362
|
+
this._publishResponse();
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
_socketHandler(socket: net.Socket): void {
|
|
367
|
+
this.socket = socket;
|
|
368
|
+
socket.once('lookup', this._lookupHandler);
|
|
369
|
+
socket.once('connect', this._connectHandler);
|
|
370
|
+
socket.once('timeout', this._timeoutHandler);
|
|
371
|
+
socket.once('end', this._sendEndHandler);
|
|
372
|
+
socket.once('secureConnect', this._secureConnectHandler);
|
|
373
|
+
this.stats.connectionTime = Date.now();
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Creates and publishes a response.
|
|
378
|
+
*/
|
|
379
|
+
_reportResponse(): void {
|
|
380
|
+
const { aborted, currentResponse } = this;
|
|
381
|
+
if (aborted || !currentResponse) {
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
const { status } = currentResponse;
|
|
385
|
+
if (status >= 300 && status < 400) {
|
|
386
|
+
if (this.followRedirects !== false && this._reportRedirect(status)) {
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
if (this.responseReported) {
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
this.responseReported = true;
|
|
394
|
+
this.emit('loadend');
|
|
395
|
+
this._publishResponse();
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Transforms a message from the client to a string.
|
|
400
|
+
* It uses `opts.sentMessageLimit` to limit number of data returned
|
|
401
|
+
* by the client.
|
|
402
|
+
*/
|
|
403
|
+
_readSentMessage(messages: string|IHttpOutputData[]): string {
|
|
404
|
+
let result = '';
|
|
405
|
+
if (typeof messages === 'string') {
|
|
406
|
+
result = messages;
|
|
407
|
+
} else {
|
|
408
|
+
for (let i = 0, len = messages.length; i < len; i++) {
|
|
409
|
+
const chunk = messages[i].data;
|
|
410
|
+
if (!chunk) {
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
if (typeof chunk === 'string') {
|
|
414
|
+
result += chunk;
|
|
415
|
+
} else if (chunk instanceof Uint8Array) {
|
|
416
|
+
result += chunk.toString();
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
const limit = this.opts.sentMessageLimit;
|
|
421
|
+
if (limit && limit > 0 && result.length >= limit) {
|
|
422
|
+
result = result.substr(0, limit);
|
|
423
|
+
result += ' ...';
|
|
424
|
+
}
|
|
425
|
+
return result;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
httpAgent(uri: URL | UrlWithStringQuery): http.Agent {
|
|
429
|
+
const init: http.AgentOptions = {
|
|
430
|
+
// keepAlive: true,
|
|
431
|
+
host: uri.hostname || undefined,
|
|
432
|
+
};
|
|
433
|
+
if (uri.port) {
|
|
434
|
+
init.port = Number(uri.port);
|
|
435
|
+
}
|
|
436
|
+
const agent = new http.Agent(init);
|
|
437
|
+
return agent;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
httpsAgent(uri: URL | UrlWithStringQuery): https.Agent {
|
|
441
|
+
const init: https.AgentOptions = {
|
|
442
|
+
// keepAlive: true,
|
|
443
|
+
host: uri.hostname || undefined,
|
|
444
|
+
path: `${uri.pathname}${uri.search}`,
|
|
445
|
+
};
|
|
446
|
+
if (uri.port) {
|
|
447
|
+
init.port = Number(uri.port);
|
|
448
|
+
}
|
|
449
|
+
if (this.opts.validateCertificates) {
|
|
450
|
+
init.checkServerIdentity = this._checkServerIdentity.bind(this);
|
|
451
|
+
} else {
|
|
452
|
+
init.rejectUnauthorized = false;
|
|
453
|
+
// init.requestOCSP = false;
|
|
454
|
+
}
|
|
455
|
+
const certs = this.opts.certificates;
|
|
456
|
+
if (Array.isArray(certs)) {
|
|
457
|
+
certs.forEach(cert => this._addClientCertificate(cert, init));
|
|
458
|
+
}
|
|
459
|
+
const agent = new https.Agent(init);
|
|
460
|
+
return agent;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Appends the list of headers on the request options.
|
|
465
|
+
*
|
|
466
|
+
* @param options The request options to alter.
|
|
467
|
+
*/
|
|
468
|
+
appendHeaders(options: http.RequestOptions | https.RequestOptions): void {
|
|
469
|
+
// Note, the final headers are set on the `sentRequest` object.
|
|
470
|
+
// The `request` object is not changed.
|
|
471
|
+
const headers = new Headers(this.sentRequest.headers);
|
|
472
|
+
if (!headers.has('host') && this.hostHeader) {
|
|
473
|
+
headers.set('host', this.hostHeader);
|
|
474
|
+
}
|
|
475
|
+
if (!options.headers) {
|
|
476
|
+
options.headers = {};
|
|
477
|
+
}
|
|
478
|
+
for (const [key, value] of headers.entries()) {
|
|
479
|
+
options.headers[key] = value;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import formDataConverter from './FormData.js';
|
|
2
|
+
import { Headers } from '../../lib/headers/Headers.js';
|
|
3
|
+
import { Payload, PayloadSerializer, IMultipartBody } from '../../lib/transformers/PayloadSerializer.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A class containing static helper methods to deal with Payload
|
|
7
|
+
* transformations
|
|
8
|
+
*/
|
|
9
|
+
export class PayloadSupport {
|
|
10
|
+
/**
|
|
11
|
+
* NormalizeLineEndingsToCRLF
|
|
12
|
+
* https://code.google.com/p/chromium/codesearch#chromium/src/third_party/WebKit/Source/
|
|
13
|
+
* platform/text/LineEnding.cpp&rcl=1458041387&l=101
|
|
14
|
+
*
|
|
15
|
+
* @param string A string to be normalized.
|
|
16
|
+
* @return normalized string
|
|
17
|
+
*/
|
|
18
|
+
static normalizeString(string: string): string {
|
|
19
|
+
let result = '';
|
|
20
|
+
for (let i = 0; i < string.length; i++) {
|
|
21
|
+
const c = string[i];
|
|
22
|
+
const p = string[i + 1];
|
|
23
|
+
if (c === '\r') {
|
|
24
|
+
// Safe to look ahead because of trailing '\0'.
|
|
25
|
+
if (p && p !== '\n') {
|
|
26
|
+
// Turn CR into CRLF.
|
|
27
|
+
result += '\r';
|
|
28
|
+
result += '\n';
|
|
29
|
+
}
|
|
30
|
+
} else if (c === '\n') {
|
|
31
|
+
result += '\r';
|
|
32
|
+
result += '\n';
|
|
33
|
+
} else {
|
|
34
|
+
// Leave other characters alone.
|
|
35
|
+
result += c;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Transforms the request payload into a `Buffer`
|
|
43
|
+
*
|
|
44
|
+
* @param payload A payload message
|
|
45
|
+
* @param headers A headers object where to append headers when needed
|
|
46
|
+
* @returns A promise resolved to a `Buffer`.
|
|
47
|
+
*/
|
|
48
|
+
static async payloadToBuffer(payload: Payload, headers: Headers): Promise<Buffer|undefined> {
|
|
49
|
+
if (!payload) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (typeof payload === 'string') {
|
|
53
|
+
payload = PayloadSupport.normalizeString(payload);
|
|
54
|
+
return Buffer.from(payload, 'utf8');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (payload.type === 'string') {
|
|
58
|
+
return PayloadSupport.payloadToBuffer(payload.data as string, headers);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (['blob', 'file'].includes(payload.type)) {
|
|
62
|
+
if (!headers.has('content-type') && payload.mime) {
|
|
63
|
+
headers.set('content-type', payload.mime);
|
|
64
|
+
}
|
|
65
|
+
return PayloadSerializer.deserializeBlobBuffer(payload.data as string);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (payload.type === 'buffer') {
|
|
69
|
+
return PayloadSerializer.deserializeBuffer(payload.data as number[]);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (payload.type === 'arraybuffer') {
|
|
73
|
+
return PayloadSerializer.deserializeArrayBufferBuffer(payload.data as number[]);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (payload.type === 'formdata') {
|
|
77
|
+
const result = await formDataConverter(payload.data as IMultipartBody[]);
|
|
78
|
+
headers.set('Content-Type', result.type);
|
|
79
|
+
return result.buffer;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
throw new Error('Unsupported payload message');
|
|
83
|
+
}
|
|
84
|
+
}
|