@scalar/openapi-parser 0.8.6 → 0.8.8
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/CHANGELOG.md +12 -0
- package/README.md +1 -1
- package/dist/configuration/index.js +27 -1
- package/dist/index.js +26 -1
- package/dist/lib/Validator/Validator.js +127 -1
- package/dist/{utils/load/plugins → plugins/fetch-urls}/fetchUrls.d.ts +1 -1
- package/dist/plugins/fetch-urls/fetchUrls.d.ts.map +1 -0
- package/dist/plugins/fetch-urls/fetchUrls.js +45 -0
- package/dist/plugins/fetch-urls/index.d.ts +2 -0
- package/dist/plugins/fetch-urls/index.d.ts.map +1 -0
- package/dist/plugins/fetch-urls/index.js +1 -0
- package/dist/plugins/read-files/index.d.ts +2 -0
- package/dist/plugins/read-files/index.d.ts.map +1 -0
- package/dist/plugins/read-files/index.js +1 -0
- package/dist/plugins/read-files/readFiles.d.ts +3 -0
- package/dist/plugins/read-files/readFiles.d.ts.map +1 -0
- package/dist/plugins/read-files/readFiles.js +56 -0
- package/dist/polyfills/path.js +120 -1
- package/dist/schemas/v2.0/schema.json.js +1623 -1
- package/dist/schemas/v3.0/schema.json.js +1673 -1
- package/dist/schemas/v3.1/schema.json.js +1451 -1
- package/dist/utils/betterAjvErrors/helpers.js +108 -1
- package/dist/utils/betterAjvErrors/index.js +15 -1
- package/dist/utils/betterAjvErrors/utils.js +31 -1
- package/dist/utils/betterAjvErrors/validation-errors/additional-prop.js +20 -1
- package/dist/utils/betterAjvErrors/validation-errors/base.js +34 -1
- package/dist/utils/betterAjvErrors/validation-errors/default.js +20 -1
- package/dist/utils/betterAjvErrors/validation-errors/enum.js +58 -1
- package/dist/utils/betterAjvErrors/validation-errors/pattern.js +20 -1
- package/dist/utils/betterAjvErrors/validation-errors/required.js +19 -1
- package/dist/utils/betterAjvErrors/validation-errors/unevaluated-prop.js +20 -1
- package/dist/utils/dereference.js +21 -1
- package/dist/utils/details.js +25 -1
- package/dist/utils/escapeJsonPointer.js +10 -1
- package/dist/utils/filter.js +17 -1
- package/dist/utils/getEntrypoint.js +8 -1
- package/dist/utils/getListOfReferences.js +27 -1
- package/dist/utils/getSegmentsFromPath.js +18 -1
- package/dist/utils/isFilesystem.js +11 -1
- package/dist/utils/isJson.js +11 -1
- package/dist/utils/isObject.js +6 -1
- package/dist/utils/isYaml.js +19 -1
- package/dist/utils/load/load.js +121 -1
- package/dist/utils/makeFilesystem.js +25 -1
- package/dist/utils/normalize.js +25 -1
- package/dist/utils/openapi/actions/details.js +13 -1
- package/dist/utils/openapi/actions/files.js +11 -1
- package/dist/utils/openapi/actions/get.js +13 -1
- package/dist/utils/openapi/actions/toJson.js +13 -1
- package/dist/utils/openapi/actions/toYaml.js +13 -1
- package/dist/utils/openapi/commands/dereferenceCommand.js +29 -1
- package/dist/utils/openapi/commands/filterCommand.js +28 -1
- package/dist/utils/openapi/commands/loadCommand.js +44 -1
- package/dist/utils/openapi/commands/upgradeCommand.js +31 -1
- package/dist/utils/openapi/commands/validateCommand.js +35 -1
- package/dist/utils/openapi/openapi.js +18 -1
- package/dist/utils/openapi/utils/queueTask.js +11 -1
- package/dist/utils/openapi/utils/workThroughQueue.js +67 -1
- package/dist/utils/resolveReferences.js +153 -1
- package/dist/utils/toJson.js +3 -1
- package/dist/utils/toYaml.js +5 -1
- package/dist/utils/transformErrors.js +23 -1
- package/dist/utils/traverse.js +26 -1
- package/dist/utils/unescapeJsonPointer.js +11 -1
- package/dist/utils/upgrade.js +20 -1
- package/dist/utils/upgradeFromThreeToThreeOne.js +122 -1
- package/dist/utils/upgradeFromTwoToThree.js +174 -1
- package/dist/utils/validate.js +18 -1
- package/package.json +15 -13
- package/dist/utils/load/plugins/fetchUrls.d.ts.map +0 -1
- package/dist/utils/load/plugins/fetchUrls.js +0 -1
- package/dist/utils/load/plugins/readFiles.d.ts +0 -3
- package/dist/utils/load/plugins/readFiles.d.ts.map +0 -1
- package/dist/utils/load/plugins/readFiles.js +0 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -242,4 +242,4 @@ Thanks a ton for all the help and inspiration:
|
|
|
242
242
|
|
|
243
243
|
## License
|
|
244
244
|
|
|
245
|
-
The source code in this repository is licensed under [MIT](https://github.com/scalar/
|
|
245
|
+
The source code in this repository is licensed under [MIT](https://github.com/scalar/scalar/blob/main/LICENSE).
|
|
@@ -1 +1,27 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Swagger20 from '../schemas/v2.0/schema.json.js';
|
|
2
|
+
import OpenApi30 from '../schemas/v3.0/schema.json.js';
|
|
3
|
+
import OpenApi31 from '../schemas/v3.1/schema.json.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A list of the supported OpenAPI specifications
|
|
7
|
+
*/
|
|
8
|
+
const OpenApiSpecifications = {
|
|
9
|
+
'2.0': Swagger20,
|
|
10
|
+
'3.0': OpenApi30,
|
|
11
|
+
'3.1': OpenApi31,
|
|
12
|
+
};
|
|
13
|
+
const OpenApiVersions = Object.keys(OpenApiSpecifications);
|
|
14
|
+
/**
|
|
15
|
+
* List of error messages used in the Validator
|
|
16
|
+
*/
|
|
17
|
+
const ERRORS = {
|
|
18
|
+
EMPTY_OR_INVALID: 'Can’t find JSON, YAML or filename in data',
|
|
19
|
+
// URI_MUST_BE_STRING: 'uri parameter or $id attribute must be a string',
|
|
20
|
+
OPENAPI_VERSION_NOT_SUPPORTED: 'Can’t find supported Swagger/OpenAPI version in specification, version must be a string.',
|
|
21
|
+
INVALID_REFERENCE: 'Can’t resolve reference: %s',
|
|
22
|
+
EXTERNAL_REFERENCE_NOT_FOUND: 'Can’t resolve external reference: %s',
|
|
23
|
+
FILE_DOES_NOT_EXIST: 'File does not exist: %s',
|
|
24
|
+
NO_CONTENT: 'No content found',
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export { ERRORS, OpenApiSpecifications, OpenApiVersions };
|
package/dist/index.js
CHANGED
|
@@ -1 +1,26 @@
|
|
|
1
|
-
export{ERRORS,OpenApiSpecifications,OpenApiVersions}from
|
|
1
|
+
export { ERRORS, OpenApiSpecifications, OpenApiVersions } from './configuration/index.js';
|
|
2
|
+
export { Validator, jsonSchemaVersions } from './lib/Validator/Validator.js';
|
|
3
|
+
export { resolveReferences } from './utils/resolveReferences.js';
|
|
4
|
+
export { dereference } from './utils/dereference.js';
|
|
5
|
+
export { details } from './utils/details.js';
|
|
6
|
+
export { escapeJsonPointer } from './utils/escapeJsonPointer.js';
|
|
7
|
+
export { filter } from './utils/filter.js';
|
|
8
|
+
export { getEntrypoint } from './utils/getEntrypoint.js';
|
|
9
|
+
export { getListOfReferences } from './utils/getListOfReferences.js';
|
|
10
|
+
export { getSegmentsFromPath } from './utils/getSegmentsFromPath.js';
|
|
11
|
+
export { isFilesystem } from './utils/isFilesystem.js';
|
|
12
|
+
export { isJson } from './utils/isJson.js';
|
|
13
|
+
export { isObject } from './utils/isObject.js';
|
|
14
|
+
export { isYaml } from './utils/isYaml.js';
|
|
15
|
+
export { load } from './utils/load/load.js';
|
|
16
|
+
export { normalize } from './utils/normalize.js';
|
|
17
|
+
export { openapi } from './utils/openapi/openapi.js';
|
|
18
|
+
export { toJson } from './utils/toJson.js';
|
|
19
|
+
export { toYaml } from './utils/toYaml.js';
|
|
20
|
+
export { transformErrors } from './utils/transformErrors.js';
|
|
21
|
+
export { traverse } from './utils/traverse.js';
|
|
22
|
+
export { unescapeJsonPointer } from './utils/unescapeJsonPointer.js';
|
|
23
|
+
export { upgrade } from './utils/upgrade.js';
|
|
24
|
+
export { isSchemaPath, upgradeFromThreeToThreeOne } from './utils/upgradeFromThreeToThreeOne.js';
|
|
25
|
+
export { upgradeFromTwoToThree } from './utils/upgradeFromTwoToThree.js';
|
|
26
|
+
export { validate } from './utils/validate.js';
|
|
@@ -1 +1,127 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Ajv04 from 'ajv-draft-04';
|
|
2
|
+
import addFormats from 'ajv-formats';
|
|
3
|
+
import Ajv2020 from 'ajv/dist/2020.js';
|
|
4
|
+
import { OpenApiVersions, ERRORS, OpenApiSpecifications } from '../../configuration/index.js';
|
|
5
|
+
import { details } from '../../utils/details.js';
|
|
6
|
+
import { resolveReferences } from '../../utils/resolveReferences.js';
|
|
7
|
+
import { transformErrors } from '../../utils/transformErrors.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Configure available JSON Schema versions
|
|
11
|
+
*/
|
|
12
|
+
const jsonSchemaVersions = {
|
|
13
|
+
'http://json-schema.org/draft-04/schema#': Ajv04,
|
|
14
|
+
'https://json-schema.org/draft/2020-12/schema': Ajv2020,
|
|
15
|
+
};
|
|
16
|
+
class Validator {
|
|
17
|
+
version;
|
|
18
|
+
static supportedVersions = OpenApiVersions;
|
|
19
|
+
// Object with function *or* object { errors: string }
|
|
20
|
+
ajvValidators = {};
|
|
21
|
+
errors;
|
|
22
|
+
specificationVersion;
|
|
23
|
+
specificationType;
|
|
24
|
+
specification;
|
|
25
|
+
/**
|
|
26
|
+
* Checks whether a specification is valid and all references can be resolved.
|
|
27
|
+
*/
|
|
28
|
+
async validate(filesystem, options) {
|
|
29
|
+
const entrypoint = filesystem.find((file) => file.isEntrypoint);
|
|
30
|
+
const specification = entrypoint?.specification;
|
|
31
|
+
// TODO: How does this work with a filesystem?
|
|
32
|
+
this.specification = specification;
|
|
33
|
+
// TODO: defaulting info.version to keep parser compatible with the previous one
|
|
34
|
+
// we should bubble this error up and not throw on it
|
|
35
|
+
if (this.specification?.info && !this.specification.info.version) {
|
|
36
|
+
this.specification.info.version = '0.0.1';
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
// AnyObject is empty or invalid
|
|
40
|
+
if (specification === undefined || specification === null) {
|
|
41
|
+
if (options?.throwOnError) {
|
|
42
|
+
throw new Error(ERRORS.EMPTY_OR_INVALID);
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
valid: false,
|
|
46
|
+
errors: transformErrors(entrypoint, ERRORS.EMPTY_OR_INVALID),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
// Meta data about the specification
|
|
50
|
+
const { version, specificationType, specificationVersion } = details(specification);
|
|
51
|
+
this.version = version;
|
|
52
|
+
this.specificationVersion = specificationVersion;
|
|
53
|
+
this.specificationType = specificationType;
|
|
54
|
+
// AnyObject is not supported
|
|
55
|
+
if (!version) {
|
|
56
|
+
if (options?.throwOnError) {
|
|
57
|
+
throw new Error(ERRORS.OPENAPI_VERSION_NOT_SUPPORTED);
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
valid: false,
|
|
61
|
+
errors: transformErrors(entrypoint, ERRORS.OPENAPI_VERSION_NOT_SUPPORTED),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
// Get the correct OpenAPI validator
|
|
65
|
+
const validateSchema = await this.getAjvValidator(version);
|
|
66
|
+
const schemaResult = validateSchema(specification);
|
|
67
|
+
// Error handling
|
|
68
|
+
if (validateSchema.errors) {
|
|
69
|
+
if (validateSchema.errors.length > 0) {
|
|
70
|
+
if (options?.throwOnError) {
|
|
71
|
+
throw new Error(validateSchema.errors[0]);
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
valid: false,
|
|
75
|
+
errors: transformErrors(entrypoint, validateSchema.errors),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// Check if the references are valid
|
|
80
|
+
const resolvedReferences = resolveReferences(filesystem, options);
|
|
81
|
+
return {
|
|
82
|
+
valid: schemaResult && resolvedReferences.valid,
|
|
83
|
+
errors: [...(schemaResult.errors ?? []), ...resolvedReferences.errors],
|
|
84
|
+
schema: resolvedReferences.schema,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
// Something went horribly wrong!
|
|
89
|
+
if (options?.throwOnError) {
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
valid: false,
|
|
94
|
+
errors: transformErrors(entrypoint, error.message ?? error),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Ajv JSON schema validator
|
|
100
|
+
*/
|
|
101
|
+
async getAjvValidator(version) {
|
|
102
|
+
// Schema loaded already
|
|
103
|
+
if (this.ajvValidators[version]) {
|
|
104
|
+
return this.ajvValidators[version];
|
|
105
|
+
}
|
|
106
|
+
// Load OpenAPI Schema
|
|
107
|
+
const schema = OpenApiSpecifications[version];
|
|
108
|
+
// Load JSON Schema
|
|
109
|
+
const AjvClass = jsonSchemaVersions[schema.$schema];
|
|
110
|
+
// Get the correct Ajv validator
|
|
111
|
+
const ajv = new AjvClass({
|
|
112
|
+
// Ajv is a bit too strict in its strict validation of OpenAPI schemas.
|
|
113
|
+
// Switch strict mode off.
|
|
114
|
+
strict: false,
|
|
115
|
+
});
|
|
116
|
+
// Register formats
|
|
117
|
+
// https://ajv.js.org/packages/ajv-formats.html#formats
|
|
118
|
+
addFormats(ajv);
|
|
119
|
+
// OpenAPI 3.1 uses media-range format
|
|
120
|
+
if (version === '3.1') {
|
|
121
|
+
ajv.addFormat('media-range', true);
|
|
122
|
+
}
|
|
123
|
+
return (this.ajvValidators[version] = ajv.compile(schema));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export { Validator, jsonSchemaVersions };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetchUrls.d.ts","sourceRoot":"","sources":["../../../src/plugins/fetch-urls/fetchUrls.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAElD,eAAO,MAAM,6BAA6B;;CAEzC,CAAA;AAED,eAAO,MAAM,SAAS,EAAE,CAAC,mBAAmB,CAAC,EAAE;IAC7C;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,GAAG,KAAK,CAAA;IACtB;;OAEG;IACH,KAAK,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAA;CAC3C,KAAK,UAiDL,CAAA"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const fetchUrlsDefaultConfiguration = {
|
|
2
|
+
limit: 20,
|
|
3
|
+
};
|
|
4
|
+
const fetchUrls = (customConfiguration) => {
|
|
5
|
+
// State
|
|
6
|
+
let numberOfRequests = 0;
|
|
7
|
+
// Configuration
|
|
8
|
+
const configuration = {
|
|
9
|
+
...fetchUrlsDefaultConfiguration,
|
|
10
|
+
...customConfiguration,
|
|
11
|
+
};
|
|
12
|
+
return {
|
|
13
|
+
check(value) {
|
|
14
|
+
// Not a string
|
|
15
|
+
if (typeof value !== 'string') {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
// Not http/https
|
|
19
|
+
if (!value.startsWith('http://') && !value.startsWith('https://')) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
return true;
|
|
23
|
+
},
|
|
24
|
+
async get(value) {
|
|
25
|
+
// Limit ht enumber of requests
|
|
26
|
+
if (configuration?.limit !== false &&
|
|
27
|
+
numberOfRequests >= configuration?.limit) {
|
|
28
|
+
console.warn(`[fetchUrls] Maximum number of requests reeached (${configuration?.limit}), skipping request`);
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
numberOfRequests++;
|
|
33
|
+
const response = await (configuration?.fetch
|
|
34
|
+
? configuration.fetch(value)
|
|
35
|
+
: fetch(value));
|
|
36
|
+
return await response.text();
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
console.error('[fetchUrls]', error.message, `(${value})`);
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export { fetchUrls, fetchUrlsDefaultConfiguration };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/plugins/fetch-urls/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { fetchUrls, fetchUrlsDefaultConfiguration } from './fetchUrls.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/plugins/read-files/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { readFiles } from './readFiles.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readFiles.d.ts","sourceRoot":"","sources":["../../../src/plugins/read-files/readFiles.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAElD,eAAO,MAAM,SAAS,EAAE,MAAM,UAoD7B,CAAA"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import { ERRORS } from '../../configuration/index.js';
|
|
3
|
+
import { dirname, join } from '../../polyfills/path.js';
|
|
4
|
+
import { isJson } from '../../utils/isJson.js';
|
|
5
|
+
import { isYaml } from '../../utils/isYaml.js';
|
|
6
|
+
|
|
7
|
+
const readFiles = () => {
|
|
8
|
+
return {
|
|
9
|
+
check(value) {
|
|
10
|
+
// Not a string
|
|
11
|
+
if (typeof value !== 'string') {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
// URL
|
|
15
|
+
if (value.startsWith('http://') || value.startsWith('https://')) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
// Line breaks
|
|
19
|
+
if (value.includes('\n')) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
// JSON
|
|
23
|
+
if (isJson(value)) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
// YAML (run through YAML.parse)
|
|
27
|
+
if (isYaml(value)) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
return true;
|
|
31
|
+
},
|
|
32
|
+
async get(value) {
|
|
33
|
+
if (!fs.existsSync(value)) {
|
|
34
|
+
throw new Error(ERRORS.FILE_DOES_NOT_EXIST.replace('%s', value));
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
return fs.readFileSync(value, 'utf-8');
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
console.error('[readFiles]', error);
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
resolvePath(value, reference) {
|
|
44
|
+
const dir = dirname(value);
|
|
45
|
+
return join(dir, reference);
|
|
46
|
+
},
|
|
47
|
+
getDir(value) {
|
|
48
|
+
return dirname(value);
|
|
49
|
+
},
|
|
50
|
+
getFilename(value) {
|
|
51
|
+
return value.split('/').pop();
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export { readFiles };
|
package/dist/polyfills/path.js
CHANGED
|
@@ -1 +1,120 @@
|
|
|
1
|
-
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
// Copyright Joyent, Inc. and other Node contributors.
|
|
3
|
+
//
|
|
4
|
+
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
5
|
+
// copy of this software and associated documentation files (the
|
|
6
|
+
// "Software"), to deal in the Software without restriction, including
|
|
7
|
+
// without limitation the rights to use, copy, modify, merge, publish,
|
|
8
|
+
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
9
|
+
// persons to whom the Software is furnished to do so, subject to the
|
|
10
|
+
// following conditions:
|
|
11
|
+
//
|
|
12
|
+
// The above copyright notice and this permission notice shall be included
|
|
13
|
+
// in all copies or substantial portions of the Software.
|
|
14
|
+
//
|
|
15
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
16
|
+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
18
|
+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
19
|
+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
20
|
+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
21
|
+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
// resolves . and .. elements in a path array with directory names there
|
|
23
|
+
// must be no slashes, empty elements, or device names (c:\) in the array
|
|
24
|
+
// (so also no leading and trailing slashes - it does not distinguish
|
|
25
|
+
// relative and absolute paths)
|
|
26
|
+
function normalizeArray(parts, allowAboveRoot) {
|
|
27
|
+
// if the path tries to go above the root, `up` ends up > 0
|
|
28
|
+
let up = 0;
|
|
29
|
+
for (let i = parts.length - 1; i >= 0; i--) {
|
|
30
|
+
const last = parts[i];
|
|
31
|
+
if (last === '.') {
|
|
32
|
+
parts.splice(i, 1);
|
|
33
|
+
}
|
|
34
|
+
else if (last === '..') {
|
|
35
|
+
parts.splice(i, 1);
|
|
36
|
+
up++;
|
|
37
|
+
}
|
|
38
|
+
else if (up) {
|
|
39
|
+
parts.splice(i, 1);
|
|
40
|
+
up--;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// if the path is allowed to go above the root, restore leading ..s
|
|
44
|
+
if (allowAboveRoot) {
|
|
45
|
+
for (; up--; up) {
|
|
46
|
+
parts.unshift('..');
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return parts;
|
|
50
|
+
}
|
|
51
|
+
// Split a filename into [root, dir, basename, ext], unix version
|
|
52
|
+
// 'root' is just a slash, or nothing.
|
|
53
|
+
const splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^/]+?|)(\.[^./]*|))(?:[/]*)$/;
|
|
54
|
+
const splitPath = function (filename) {
|
|
55
|
+
return splitPathRe.exec(filename).slice(1);
|
|
56
|
+
};
|
|
57
|
+
// path.normalize(path)
|
|
58
|
+
// posix version
|
|
59
|
+
function normalize(path) {
|
|
60
|
+
const isPathAbsolute = isAbsolute(path), trailingSlash = substr(path, -1) === '/';
|
|
61
|
+
// Normalize the path
|
|
62
|
+
path = normalizeArray(filter(path.split('/'), function (p) {
|
|
63
|
+
return !!p;
|
|
64
|
+
}), !isPathAbsolute).join('/');
|
|
65
|
+
if (!path && !isPathAbsolute) {
|
|
66
|
+
path = '.';
|
|
67
|
+
}
|
|
68
|
+
if (path && trailingSlash) {
|
|
69
|
+
path += '/';
|
|
70
|
+
}
|
|
71
|
+
return (isPathAbsolute ? '/' : '') + path;
|
|
72
|
+
}
|
|
73
|
+
// posix version
|
|
74
|
+
function isAbsolute(path) {
|
|
75
|
+
return path.charAt(0) === '/';
|
|
76
|
+
}
|
|
77
|
+
// posix version
|
|
78
|
+
function join(...paths) {
|
|
79
|
+
return normalize(filter(paths, function (p, index) {
|
|
80
|
+
if (typeof p !== 'string') {
|
|
81
|
+
throw new TypeError('Arguments to path.join must be strings');
|
|
82
|
+
}
|
|
83
|
+
return p;
|
|
84
|
+
}).join('/'));
|
|
85
|
+
}
|
|
86
|
+
function dirname(path) {
|
|
87
|
+
const result = splitPath(path), root = result[0];
|
|
88
|
+
let dir = result[1];
|
|
89
|
+
if (!root && !dir) {
|
|
90
|
+
// No dirname whatsoever
|
|
91
|
+
return '.';
|
|
92
|
+
}
|
|
93
|
+
if (dir) {
|
|
94
|
+
// It has a dirname, strip trailing slash
|
|
95
|
+
dir = dir.substr(0, dir.length - 1);
|
|
96
|
+
}
|
|
97
|
+
return root + dir;
|
|
98
|
+
}
|
|
99
|
+
function filter(xs, f) {
|
|
100
|
+
if (xs.filter)
|
|
101
|
+
return xs.filter(f);
|
|
102
|
+
const res = [];
|
|
103
|
+
for (let i = 0; i < xs.length; i++) {
|
|
104
|
+
if (f(xs[i], i, xs))
|
|
105
|
+
res.push(xs[i]);
|
|
106
|
+
}
|
|
107
|
+
return res;
|
|
108
|
+
}
|
|
109
|
+
// String.prototype.substr - negative index don't work in IE8
|
|
110
|
+
const substr = 'ab'.substr(-1) === 'b'
|
|
111
|
+
? function (str, start, len) {
|
|
112
|
+
return str.substr(start, len);
|
|
113
|
+
}
|
|
114
|
+
: function (str, start, len) {
|
|
115
|
+
if (start < 0)
|
|
116
|
+
start = str.length + start;
|
|
117
|
+
return str.substr(start, len);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export { dirname, isAbsolute, join, normalize };
|