@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
package/src/lib/fs/Fs.ts
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { writeFile, mkdir, rm, readdir, stat, access, readFile, copyFile as fsCopyFile } from 'fs/promises';
|
|
2
|
+
import { constants } from 'fs';
|
|
3
|
+
import { join, dirname } from 'path';
|
|
4
|
+
|
|
5
|
+
export interface JsonReadOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Whether it should throw an error when a reading error occurs.
|
|
8
|
+
*/
|
|
9
|
+
throws?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// function statPromise(filePath: string): Promise<fsSync.Stats> {
|
|
13
|
+
// return new Promise((resolve, reject) => {
|
|
14
|
+
// fsSync.stat(filePath, (err, stats) => {
|
|
15
|
+
// if (err) {
|
|
16
|
+
// reject(err);
|
|
17
|
+
// } else {
|
|
18
|
+
// resolve(stats);
|
|
19
|
+
// }
|
|
20
|
+
// });
|
|
21
|
+
// });
|
|
22
|
+
// }
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Checks whether a file exists in the location.
|
|
26
|
+
*/
|
|
27
|
+
export async function pathExists(filePath: string): Promise<boolean> {
|
|
28
|
+
// return new Promise((resolve) => {
|
|
29
|
+
// fsSync.stat(filePath, (err, stats) => {
|
|
30
|
+
// if (err) {
|
|
31
|
+
// resolve(false);
|
|
32
|
+
// } else {
|
|
33
|
+
// resolve(true);
|
|
34
|
+
// }
|
|
35
|
+
// });
|
|
36
|
+
// });
|
|
37
|
+
try {
|
|
38
|
+
await stat(filePath);
|
|
39
|
+
return true;
|
|
40
|
+
} catch (e) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Tests a user's permissions for the file or directory specified by filePath.
|
|
47
|
+
* @param filePath The path to test
|
|
48
|
+
* @returns True when the path can be read by the current user.
|
|
49
|
+
*/
|
|
50
|
+
export async function canRead(filePath: string): Promise<boolean> {
|
|
51
|
+
const exists = await pathExists(filePath);
|
|
52
|
+
if (!exists) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// return new Promise((resolve) => {
|
|
57
|
+
// fsSync.access(filePath, constants.R_OK, (err) => {
|
|
58
|
+
// if (err) {
|
|
59
|
+
// resolve(false);
|
|
60
|
+
// } else {
|
|
61
|
+
// resolve(true);
|
|
62
|
+
// }
|
|
63
|
+
// });
|
|
64
|
+
// });
|
|
65
|
+
try {
|
|
66
|
+
await access(filePath, constants.R_OK);
|
|
67
|
+
return true;
|
|
68
|
+
} catch (e) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Tests a user's permissions for the file or directory specified by filePath.
|
|
75
|
+
* @param filePath The path to test
|
|
76
|
+
* @returns True when the path can be written to by the current user.
|
|
77
|
+
*/
|
|
78
|
+
export async function canWrite(filePath: string): Promise<boolean> {
|
|
79
|
+
const exists = await pathExists(filePath);
|
|
80
|
+
if (!exists) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
// return new Promise((resolve) => {
|
|
84
|
+
// fsSync.access(filePath, constants.W_OK, (err) => {
|
|
85
|
+
// if (err) {
|
|
86
|
+
// resolve(false);
|
|
87
|
+
// } else {
|
|
88
|
+
// resolve(true);
|
|
89
|
+
// }
|
|
90
|
+
// });
|
|
91
|
+
// });
|
|
92
|
+
try {
|
|
93
|
+
await access(filePath, constants.W_OK);
|
|
94
|
+
return true;
|
|
95
|
+
} catch (e) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Reads the contents of a JSON file.
|
|
102
|
+
*
|
|
103
|
+
* @param filePath The path to the JSON file to read.
|
|
104
|
+
* @returns The contents of the file. When `throws` options is not set and error occurs then it returns an empty file.
|
|
105
|
+
*/
|
|
106
|
+
export async function readJson(filePath: string, opts: JsonReadOptions={}): Promise<unknown> {
|
|
107
|
+
const readable = await canRead(filePath);
|
|
108
|
+
if (!readable) {
|
|
109
|
+
if (opts.throws) {
|
|
110
|
+
throw new Error(`Unable to read file: ${filePath}. Access is denied.`);
|
|
111
|
+
}
|
|
112
|
+
return {};
|
|
113
|
+
}
|
|
114
|
+
// return new Promise((resolve, reject) => {
|
|
115
|
+
// fsSync.readFile(filePath, 'utf8', (err, contents) => {
|
|
116
|
+
// if (err) {
|
|
117
|
+
// reject(err);
|
|
118
|
+
// } else {
|
|
119
|
+
// let data = {};
|
|
120
|
+
// try {
|
|
121
|
+
// data = JSON.parse(contents);
|
|
122
|
+
// } catch (e) {
|
|
123
|
+
// if (opts.throws) {
|
|
124
|
+
// const err = new Error(`Invalid JSON contents for file: ${filePath}.`);
|
|
125
|
+
// reject(err);
|
|
126
|
+
// return;
|
|
127
|
+
// }
|
|
128
|
+
// }
|
|
129
|
+
// resolve(data);
|
|
130
|
+
// }
|
|
131
|
+
// });
|
|
132
|
+
// });
|
|
133
|
+
const contents = await readFile(filePath, 'utf8');
|
|
134
|
+
let data = {};
|
|
135
|
+
try {
|
|
136
|
+
data = JSON.parse(contents);
|
|
137
|
+
} catch (e) {
|
|
138
|
+
if (opts.throws) {
|
|
139
|
+
throw new Error(`Invalid JSON contents for file: ${filePath}.`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return data;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Writes the contents to the file.
|
|
147
|
+
*
|
|
148
|
+
* @param filePath The file to write to. It replaces the contents.
|
|
149
|
+
* @param contents The contents to write.
|
|
150
|
+
*/
|
|
151
|
+
export async function writeJson(filePath: string, contents: string|any): Promise<void> {
|
|
152
|
+
const destParent = dirname(filePath);
|
|
153
|
+
await ensureDir(destParent);
|
|
154
|
+
const parentWritable = await canWrite(destParent);
|
|
155
|
+
if (!parentWritable) {
|
|
156
|
+
throw new Error(`Unable to write to location: ${parentWritable}. Access is denied.`);
|
|
157
|
+
}
|
|
158
|
+
const data = typeof contents === 'string' ? contents : JSON.stringify(contents);
|
|
159
|
+
await writeFile(filePath, data);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Ensures the directory exists.
|
|
164
|
+
*/
|
|
165
|
+
export async function ensureDir(dirPath: string): Promise<void> {
|
|
166
|
+
const readable = await canRead(dirPath);
|
|
167
|
+
if (readable) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
await mkdir(dirPath, { recursive: true });
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Removes contents of the directory, leaving the directory in the filesystem.
|
|
175
|
+
*/
|
|
176
|
+
export async function emptyDir(dirPath: string): Promise<void> {
|
|
177
|
+
const exists = await pathExists(dirPath);
|
|
178
|
+
if (!exists) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const writeable = await canWrite(dirPath);
|
|
182
|
+
if (!writeable) {
|
|
183
|
+
throw new Error(`Unable to clear directory: ${dirPath}. Access is denied.`);
|
|
184
|
+
}
|
|
185
|
+
const items = await readdir(dirPath, 'utf8');
|
|
186
|
+
for (const item of items) {
|
|
187
|
+
const file = join(dirPath, item);
|
|
188
|
+
await rm(file, { recursive: true });
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Copies a file
|
|
194
|
+
*/
|
|
195
|
+
async function copyFile(source: string, dest: string): Promise<void> {
|
|
196
|
+
const destParent = dirname(dest);
|
|
197
|
+
await ensureDir(destParent);
|
|
198
|
+
await fsCopyFile(source, dest);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// /**
|
|
202
|
+
// * Copies a file
|
|
203
|
+
// * @param {string} source
|
|
204
|
+
// * @param {string} dest
|
|
205
|
+
// * @returns {Promise<void>}
|
|
206
|
+
// */
|
|
207
|
+
// async function copyFile(source, dest) {
|
|
208
|
+
// const destParent = dirname(dest);
|
|
209
|
+
// await ensureDir(destParent);
|
|
210
|
+
// return new Promise((resolve, reject) => {
|
|
211
|
+
// fsSync.copyFile(source, dest, (err) => {
|
|
212
|
+
// if (err) {
|
|
213
|
+
// reject(err);
|
|
214
|
+
// } else {
|
|
215
|
+
// resolve();
|
|
216
|
+
// }
|
|
217
|
+
// });
|
|
218
|
+
// });
|
|
219
|
+
// }
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Copies a directory and its contents.
|
|
223
|
+
*/
|
|
224
|
+
async function copyDirectory(source: string, dest: string): Promise<void> {
|
|
225
|
+
await ensureDir(dest);
|
|
226
|
+
// const entries = fsSync.readdirSync(source, { withFileTypes: true, encoding: 'utf8' });
|
|
227
|
+
const entries = await readdir(source, { withFileTypes: true, encoding: 'utf8' });
|
|
228
|
+
for (const entry of entries) {
|
|
229
|
+
const srcFile = join(source, entry.name);
|
|
230
|
+
const destFile = join(dest, entry.name);
|
|
231
|
+
const srcStat = await stat(srcFile);
|
|
232
|
+
if (srcStat.isDirectory()) {
|
|
233
|
+
await copyDirectory(srcFile, destFile);
|
|
234
|
+
} else {
|
|
235
|
+
await copyFile(srcFile, destFile);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Copies a file or a directory to the destination location.
|
|
242
|
+
* It creates the destination folder when missing.
|
|
243
|
+
*
|
|
244
|
+
* @param source The source file or folder.
|
|
245
|
+
* @param dest The destination file or folder.
|
|
246
|
+
*/
|
|
247
|
+
export async function copy(source: string, dest: string): Promise<void> {
|
|
248
|
+
const existing = await pathExists(source);
|
|
249
|
+
if (!existing) {
|
|
250
|
+
throw new Error(`Specified path does not exist: ${source}`);
|
|
251
|
+
}
|
|
252
|
+
const srcStat = await stat(source);
|
|
253
|
+
if (srcStat.isDirectory()) {
|
|
254
|
+
await copyDirectory(source, dest);
|
|
255
|
+
} else {
|
|
256
|
+
await copyFile(source, dest);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import http from 'http';
|
|
2
|
+
|
|
3
|
+
/* eslint-disable no-unused-vars */
|
|
4
|
+
interface RawValue {
|
|
5
|
+
name: string;
|
|
6
|
+
value: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Normalizes name of a header.
|
|
11
|
+
*
|
|
12
|
+
* @param name
|
|
13
|
+
* @return Normalized name
|
|
14
|
+
*/
|
|
15
|
+
function normalizeName(name: string): string {
|
|
16
|
+
if (typeof name !== 'string') {
|
|
17
|
+
name = String(name);
|
|
18
|
+
}
|
|
19
|
+
return name.toLowerCase();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Normalizes value of a header.
|
|
24
|
+
* @param value
|
|
25
|
+
* @return Normalized name
|
|
26
|
+
*/
|
|
27
|
+
function normalizeValue(value: string): string {
|
|
28
|
+
if (typeof value !== 'string') {
|
|
29
|
+
value = String(value);
|
|
30
|
+
}
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A generator for list of headers from a string.
|
|
36
|
+
*
|
|
37
|
+
* ```javascript
|
|
38
|
+
* for (let [name, value] of headersStringToList('a:b')) {
|
|
39
|
+
* ...
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
* @param string Headers string to parse
|
|
43
|
+
*/
|
|
44
|
+
function* headersStringToList(string: string): Generator<string[]> {
|
|
45
|
+
if (!string || string.trim() === '') {
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
const headers = string.split(/\n(?=[^ \t]+)/gim);
|
|
49
|
+
for (let i = 0, len = headers.length; i < len; i++) {
|
|
50
|
+
const line = headers[i].trim();
|
|
51
|
+
if (line === '') {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const sepPosition = line.indexOf(':');
|
|
55
|
+
if (sepPosition === -1) {
|
|
56
|
+
yield [line, ''];
|
|
57
|
+
} else {
|
|
58
|
+
const name = line.substr(0, sepPosition);
|
|
59
|
+
const value = line.substr(sepPosition + 1).trim();
|
|
60
|
+
yield [name, value];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The same interface as Web platform's Headers but without
|
|
67
|
+
* CORS restrictions.
|
|
68
|
+
*/
|
|
69
|
+
export class Headers {
|
|
70
|
+
/**
|
|
71
|
+
* The keys are canonical keys and the values are the input values.
|
|
72
|
+
*/
|
|
73
|
+
map: Record<string, RawValue> = {};
|
|
74
|
+
/**
|
|
75
|
+
* @param headers The headers to parse.
|
|
76
|
+
*/
|
|
77
|
+
constructor(headers?: string | Record<string, string> | Headers | http.IncomingHttpHeaders) {
|
|
78
|
+
if (headers instanceof Headers) {
|
|
79
|
+
headers.forEach((value, name) => this.append(name, value));
|
|
80
|
+
} else if (typeof headers === 'string') {
|
|
81
|
+
const iterator = headersStringToList(headers);
|
|
82
|
+
let result = iterator.next();
|
|
83
|
+
while (!result.done) {
|
|
84
|
+
this.append(result.value[0], result.value[1]);
|
|
85
|
+
result = iterator.next();
|
|
86
|
+
}
|
|
87
|
+
} else if (headers) {
|
|
88
|
+
Object.keys(headers).forEach((name) => this.append(name, headers[name]));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Adds value to existing header or creates new header
|
|
94
|
+
*/
|
|
95
|
+
append(name: string, value: string | string[] | undefined): void {
|
|
96
|
+
if (Array.isArray(value)) {
|
|
97
|
+
value.forEach(v => this.append(name, v));
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const normalizedName = normalizeName(name);
|
|
101
|
+
value = value ? normalizeValue(value) : '';
|
|
102
|
+
let item = this.map[normalizedName];
|
|
103
|
+
if (item) {
|
|
104
|
+
const oldValue = item.value;
|
|
105
|
+
item.value = oldValue ? `${oldValue},${value}` : value;
|
|
106
|
+
} else {
|
|
107
|
+
item = {
|
|
108
|
+
name,
|
|
109
|
+
value,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
this.map[normalizedName] = item;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Removes a header from the list of headers.
|
|
117
|
+
* @param name The header name
|
|
118
|
+
*/
|
|
119
|
+
delete(name: string): void {
|
|
120
|
+
delete this.map[normalizeName(name)];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Returns the current value of the header
|
|
125
|
+
* @param name Header name
|
|
126
|
+
*/
|
|
127
|
+
get(name: string): string | undefined {
|
|
128
|
+
name = normalizeName(name);
|
|
129
|
+
return this.has(name) ? this.map[name].value : undefined;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Checks if the header exists.
|
|
134
|
+
*/
|
|
135
|
+
has(name: string): boolean {
|
|
136
|
+
return Object.prototype.hasOwnProperty.call(this.map, normalizeName(name));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Creates a new header. If the header exist it replaces the value.
|
|
141
|
+
*/
|
|
142
|
+
set(name: string, value: string): void {
|
|
143
|
+
const normalizedName = normalizeName(name);
|
|
144
|
+
this.map[normalizedName] = {
|
|
145
|
+
value: normalizeValue(value),
|
|
146
|
+
name,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Iterates over each header.
|
|
152
|
+
*/
|
|
153
|
+
forEach(callback: (value: string, name: string, headers: Headers) => void, thisArg?: unknown): void {
|
|
154
|
+
for (const name in this.map) {
|
|
155
|
+
if (Object.prototype.hasOwnProperty.call(this.map, name)) {
|
|
156
|
+
callback.call(thisArg, this.map[name].value, this.map[name].name, this);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* @return The headers HTTP string
|
|
163
|
+
*/
|
|
164
|
+
toString(): string {
|
|
165
|
+
const result: string[] = [];
|
|
166
|
+
this.forEach((value, name) => {
|
|
167
|
+
let tmp = `${name}: `;
|
|
168
|
+
if (value) {
|
|
169
|
+
tmp += value;
|
|
170
|
+
}
|
|
171
|
+
result.push(tmp);
|
|
172
|
+
});
|
|
173
|
+
return result.join('\n');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Iterates over keys.
|
|
178
|
+
*/
|
|
179
|
+
*keys(): IterableIterator<string> {
|
|
180
|
+
for (const name in this.map) {
|
|
181
|
+
if (Object.prototype.hasOwnProperty.call(this.map, name)) {
|
|
182
|
+
yield this.map[name].name;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Iterates over values.
|
|
189
|
+
*/
|
|
190
|
+
*values(): IterableIterator<string> {
|
|
191
|
+
for (const name in this.map) {
|
|
192
|
+
if (Object.prototype.hasOwnProperty.call(this.map, name)) {
|
|
193
|
+
yield this.map[name].value;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Iterates over headers.
|
|
200
|
+
*/
|
|
201
|
+
*entries(): IterableIterator<string[]> {
|
|
202
|
+
for (const name in this.map) {
|
|
203
|
+
if (Object.prototype.hasOwnProperty.call(this.map, name)) {
|
|
204
|
+
yield [this.map[name].name, this.map[name].value];
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Iterates over headers.
|
|
211
|
+
*/
|
|
212
|
+
*[Symbol.iterator](): IterableIterator<string[]> {
|
|
213
|
+
for (const name in this.map) {
|
|
214
|
+
if (Object.prototype.hasOwnProperty.call(this.map, name)) {
|
|
215
|
+
yield [this.map[name].name, this.map[name].value];
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Logger } from './Logger.js';
|
|
2
|
+
|
|
3
|
+
export class DefaultLogger extends Logger {
|
|
4
|
+
warn(...args: unknown[]): void {
|
|
5
|
+
console.warn(...args);
|
|
6
|
+
}
|
|
7
|
+
info(...args: unknown[]): void {
|
|
8
|
+
console.info(...args);
|
|
9
|
+
}
|
|
10
|
+
error(...args: unknown[]): void {
|
|
11
|
+
console.error(...args);
|
|
12
|
+
}
|
|
13
|
+
log(...args: unknown[]): void {
|
|
14
|
+
console.log(...args);
|
|
15
|
+
}
|
|
16
|
+
debug(...args: unknown[]): void {
|
|
17
|
+
console.debug(...args);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/* eslint-disable no-unused-vars */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
3
|
+
import { Logger } from './Logger.js';
|
|
4
|
+
|
|
5
|
+
export class DummyLogger extends Logger {
|
|
6
|
+
warn(...args: unknown[]): void {
|
|
7
|
+
// ...
|
|
8
|
+
}
|
|
9
|
+
info(...args: unknown[]): void {
|
|
10
|
+
// ...
|
|
11
|
+
}
|
|
12
|
+
error(...args: unknown[]): void {
|
|
13
|
+
// ...
|
|
14
|
+
}
|
|
15
|
+
log(...args: unknown[]): void {
|
|
16
|
+
// ...
|
|
17
|
+
}
|
|
18
|
+
debug(...args: unknown[]): void {
|
|
19
|
+
// ...
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/* eslint-disable no-unused-vars */
|
|
2
|
+
export interface ILogger {
|
|
3
|
+
warn: (...args: unknown[]) => void;
|
|
4
|
+
info: (...args: unknown[]) => void;
|
|
5
|
+
error: (...args: unknown[]) => void;
|
|
6
|
+
log: (...args: unknown[]) => void;
|
|
7
|
+
debug: (...args: unknown[]) => void;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export abstract class Logger {
|
|
11
|
+
abstract warn(...args: unknown[]): void;
|
|
12
|
+
abstract info(...args: unknown[]): void;
|
|
13
|
+
abstract error(...args: unknown[]): void;
|
|
14
|
+
abstract log(...args: unknown[]): void;
|
|
15
|
+
abstract debug(...args: unknown[]): void;
|
|
16
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Awaits the set number of milliseconds before resolving the promise.
|
|
3
|
+
* @param timeout The number of milliseconds to wait.
|
|
4
|
+
*/
|
|
5
|
+
export function sleep(timeout = 0): Promise<void> {
|
|
6
|
+
return new Promise((resolve) => {
|
|
7
|
+
setTimeout(() => resolve(), timeout);
|
|
8
|
+
});
|
|
9
|
+
}
|