@atlaspack/utils 2.19.2 → 2.19.4-dev-ts-project-refs-d30e9754f.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/LICENSE +201 -0
- package/dist/DefaultMap.js +41 -0
- package/dist/Deferred.js +16 -0
- package/dist/PromiseQueue.js +107 -0
- package/dist/TapStream.js +23 -0
- package/dist/alternatives.js +97 -0
- package/dist/ansi-html.js +12 -0
- package/dist/blob.js +29 -0
- package/dist/bundle-url.js +32 -0
- package/dist/collection.js +106 -0
- package/dist/config.js +138 -0
- package/dist/countLines.js +12 -0
- package/dist/debounce.js +15 -0
- package/dist/debug-tools.js +37 -0
- package/dist/dependency-location.js +22 -0
- package/dist/escape-html.js +19 -0
- package/dist/generateBuildMetrics.js +111 -0
- package/dist/generateCertificate.js +124 -0
- package/dist/getCertificate.js +13 -0
- package/dist/getExisting.js +20 -0
- package/dist/getModuleParts.js +27 -0
- package/dist/getRootDir.js +46 -0
- package/dist/glob.js +133 -0
- package/dist/hash.js +45 -0
- package/dist/http-server.js +55 -0
- package/dist/index.js +146 -0
- package/dist/is-url.js +15 -0
- package/dist/isDirectoryInside.js +11 -0
- package/dist/objectHash.js +20 -0
- package/dist/openInBrowser.js +61 -0
- package/dist/parseCSSImport.js +14 -0
- package/dist/path.js +36 -0
- package/dist/prettifyTime.js +6 -0
- package/dist/prettyDiagnostic.js +93 -0
- package/dist/progress-message.js +31 -0
- package/dist/relativeBundlePath.js +13 -0
- package/dist/relativeUrl.js +11 -0
- package/dist/replaceBundleReferences.js +140 -0
- package/dist/schema.js +389 -0
- package/dist/shared-buffer.js +24 -0
- package/dist/sourcemap.js +114 -0
- package/dist/stream.js +69 -0
- package/dist/throttle.js +12 -0
- package/dist/urlJoin.js +22 -0
- package/package.json +15 -15
- package/tsconfig.json +31 -2
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.default = prettyDiagnostic;
|
|
7
|
+
const codeframe_1 = __importDefault(require("@atlaspack/codeframe"));
|
|
8
|
+
const markdown_ansi_1 = __importDefault(require("@atlaspack/markdown-ansi"));
|
|
9
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
10
|
+
const path_1 = __importDefault(require("path"));
|
|
11
|
+
const terminal_link_1 = __importDefault(require("terminal-link"));
|
|
12
|
+
/* eslint-disable import/no-extraneous-dependencies */
|
|
13
|
+
const snarkdown_1 = __importDefault(require("snarkdown"));
|
|
14
|
+
async function prettyDiagnostic(diagnostic, options, terminalWidth, format = 'ansi') {
|
|
15
|
+
let { origin, message, stack, codeFrames, hints, skipFormatting, documentationURL, } = diagnostic;
|
|
16
|
+
const md = format === 'ansi' ? markdown_ansi_1.default : snarkdown_1.default;
|
|
17
|
+
const terminalLink = format === 'ansi'
|
|
18
|
+
? terminal_link_1.default
|
|
19
|
+
: // eslint-disable-next-line no-unused-vars
|
|
20
|
+
(text, url, _) => `<a href="${url}">${text}</a>`;
|
|
21
|
+
const chalk = format === 'ansi'
|
|
22
|
+
? chalk_1.default
|
|
23
|
+
: {
|
|
24
|
+
gray: {
|
|
25
|
+
underline: (v) => `<span style="color: grey; text-decoration: underline;">${v}</span>`,
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
let result = {
|
|
29
|
+
message: md(`**${origin ?? 'unknown'}**: `) +
|
|
30
|
+
(skipFormatting ? message : md(message)),
|
|
31
|
+
stack: '',
|
|
32
|
+
codeframe: '',
|
|
33
|
+
frames: [],
|
|
34
|
+
hints: [],
|
|
35
|
+
documentation: '',
|
|
36
|
+
};
|
|
37
|
+
if (codeFrames != null) {
|
|
38
|
+
for (let codeFrame of codeFrames) {
|
|
39
|
+
let filePath = codeFrame.filePath;
|
|
40
|
+
if (filePath != null && options && !path_1.default.isAbsolute(filePath)) {
|
|
41
|
+
filePath = path_1.default.join(options.projectRoot, filePath);
|
|
42
|
+
}
|
|
43
|
+
let highlights = codeFrame.codeHighlights;
|
|
44
|
+
let code = codeFrame.code;
|
|
45
|
+
if (code == null && options && filePath != null) {
|
|
46
|
+
code = await options.inputFS.readFile(filePath, 'utf8');
|
|
47
|
+
}
|
|
48
|
+
let formattedCodeFrame = '';
|
|
49
|
+
if (code != null) {
|
|
50
|
+
formattedCodeFrame = (0, codeframe_1.default)(code, highlights, {
|
|
51
|
+
useColor: true,
|
|
52
|
+
syntaxHighlighting: true,
|
|
53
|
+
language: codeFrame.language ||
|
|
54
|
+
(filePath != null ? path_1.default.extname(filePath).substr(1) : undefined),
|
|
55
|
+
terminalWidth,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
let location;
|
|
59
|
+
if (typeof filePath !== 'string') {
|
|
60
|
+
location = '';
|
|
61
|
+
}
|
|
62
|
+
else if (highlights.length === 0) {
|
|
63
|
+
location = filePath;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
location = `${filePath}:${highlights[0].start.line}:${highlights[0].start.column}`;
|
|
67
|
+
}
|
|
68
|
+
result.codeframe += location ? chalk.gray.underline(location) + '\n' : '';
|
|
69
|
+
result.codeframe += formattedCodeFrame;
|
|
70
|
+
if (codeFrame !== codeFrames[codeFrames.length - 1]) {
|
|
71
|
+
result.codeframe += '\n\n';
|
|
72
|
+
}
|
|
73
|
+
result.frames.push({
|
|
74
|
+
location,
|
|
75
|
+
code: formattedCodeFrame,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (stack != null) {
|
|
80
|
+
result.stack = stack;
|
|
81
|
+
}
|
|
82
|
+
if (Array.isArray(hints) && hints.length) {
|
|
83
|
+
result.hints = hints.map((h) => {
|
|
84
|
+
return md(h);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (documentationURL != null) {
|
|
88
|
+
result.documentation = terminalLink('Learn more', documentationURL, {
|
|
89
|
+
fallback: (text, url) => `${text}: ${url}`,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getProgressMessage = getProgressMessage;
|
|
7
|
+
exports.getPackageProgressMessage = getPackageProgressMessage;
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
function getProgressMessage(event) {
|
|
10
|
+
switch (event.phase) {
|
|
11
|
+
case 'transforming':
|
|
12
|
+
return `Building ${path_1.default.basename(event.filePath)}...`;
|
|
13
|
+
case 'bundling':
|
|
14
|
+
return 'Bundling...';
|
|
15
|
+
case 'packaging':
|
|
16
|
+
return `Packaging ${event.bundle.displayName}...`;
|
|
17
|
+
case 'optimizing':
|
|
18
|
+
return `Optimizing ${event.bundle.displayName}...`;
|
|
19
|
+
case 'packagingAndOptimizing': {
|
|
20
|
+
return getPackageProgressMessage(event.completeBundles, event.totalBundles);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
function getPackageProgressMessage(completeBundles, totalBundles) {
|
|
26
|
+
let percent = Math.floor((completeBundles / totalBundles) * 100);
|
|
27
|
+
let completeStr = completeBundles.toString();
|
|
28
|
+
let totalStr = totalBundles.toString();
|
|
29
|
+
let displayBundles = completeStr.padStart(totalStr.length, ' ');
|
|
30
|
+
return `Packaging bundles ${displayBundles}/${totalBundles} (${percent}%)`;
|
|
31
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.relativeBundlePath = relativeBundlePath;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const path_2 = require("./path");
|
|
9
|
+
function relativeBundlePath(from, to, opts = { leadingDotSlash: true }) {
|
|
10
|
+
let fromPath = path_1.default.join(from.target.distDir, from.name);
|
|
11
|
+
let toPath = path_1.default.join(to.target.distDir, to.name);
|
|
12
|
+
return (0, path_2.relativePath)(path_1.default.dirname(fromPath), toPath, opts.leadingDotSlash);
|
|
13
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.default = relativeUrl;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const url_1 = __importDefault(require("url"));
|
|
9
|
+
function relativeUrl(from, to) {
|
|
10
|
+
return url_1.default.format(url_1.default.parse(path_1.default.relative(from, to)));
|
|
11
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.replaceURLReferences = replaceURLReferences;
|
|
7
|
+
exports.replaceInlineReferences = replaceInlineReferences;
|
|
8
|
+
exports.getURLReplacement = getURLReplacement;
|
|
9
|
+
const rust_1 = require("@atlaspack/rust");
|
|
10
|
+
const feature_flags_1 = require("@atlaspack/feature-flags");
|
|
11
|
+
const stream_1 = require("stream");
|
|
12
|
+
const nullthrows_1 = __importDefault(require("nullthrows"));
|
|
13
|
+
const assert_1 = __importDefault(require("assert"));
|
|
14
|
+
const url_1 = __importDefault(require("url"));
|
|
15
|
+
const _1 = require("./");
|
|
16
|
+
/*
|
|
17
|
+
* Replaces references to dependency ids for URL dependencies with:
|
|
18
|
+
* - in the case of an unresolvable url dependency, the original specifier.
|
|
19
|
+
* These are external requests that Parcel did not bundle.
|
|
20
|
+
* - in the case of a reference to another bundle, the relative url to that
|
|
21
|
+
* bundle from the current bundle.
|
|
22
|
+
*/
|
|
23
|
+
function replaceURLReferences({ bundle, bundleGraph, contents, map, getReplacement = (s) => s, relative = true, }) {
|
|
24
|
+
let replacements = new Map();
|
|
25
|
+
let urlDependencies = [];
|
|
26
|
+
bundle.traverse((node) => {
|
|
27
|
+
if (node.type === 'dependency' && node.value.specifierType === 'url') {
|
|
28
|
+
urlDependencies.push(node.value);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
for (let dependency of urlDependencies) {
|
|
32
|
+
if (dependency.specifierType !== 'url') {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
let placeholder = dependency.meta?.placeholder ?? dependency.id;
|
|
36
|
+
(0, assert_1.default)(typeof placeholder === 'string');
|
|
37
|
+
let resolved = bundleGraph.getReferencedBundle(dependency, bundle);
|
|
38
|
+
if (resolved == null) {
|
|
39
|
+
replacements.set(placeholder, {
|
|
40
|
+
from: placeholder,
|
|
41
|
+
to: getReplacement(dependency.specifier),
|
|
42
|
+
});
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (resolved.bundleBehavior === 'inline' ||
|
|
46
|
+
resolved.bundleBehavior === 'inlineIsolated') {
|
|
47
|
+
// If a bundle is inline, it should be replaced with inline contents,
|
|
48
|
+
// not a URL.
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
replacements.set(placeholder, getURLReplacement({
|
|
52
|
+
dependency,
|
|
53
|
+
fromBundle: bundle,
|
|
54
|
+
toBundle: resolved,
|
|
55
|
+
relative,
|
|
56
|
+
getReplacement,
|
|
57
|
+
}));
|
|
58
|
+
}
|
|
59
|
+
return performReplacement(replacements, contents, map);
|
|
60
|
+
}
|
|
61
|
+
/*
|
|
62
|
+
* Replaces references to dependency ids for inline bundles with the packaged
|
|
63
|
+
* contents of that bundle.
|
|
64
|
+
*/
|
|
65
|
+
async function replaceInlineReferences({ bundle, bundleGraph, contents, map, getInlineReplacement, getInlineBundleContents, }) {
|
|
66
|
+
let replacements = new Map();
|
|
67
|
+
let dependencies = [];
|
|
68
|
+
bundle.traverse((node) => {
|
|
69
|
+
if (node.type === 'dependency') {
|
|
70
|
+
dependencies.push(node.value);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
for (let dependency of dependencies) {
|
|
74
|
+
let entryBundle = bundleGraph.getReferencedBundle(dependency, bundle);
|
|
75
|
+
if (entryBundle?.bundleBehavior !== 'inline' &&
|
|
76
|
+
entryBundle?.bundleBehavior !== 'inlineIsolated') {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
let packagedBundle = await getInlineBundleContents(entryBundle, bundleGraph);
|
|
80
|
+
let packagedContents = (packagedBundle.contents instanceof stream_1.Readable
|
|
81
|
+
? await (0, _1.bufferStream)(packagedBundle.contents)
|
|
82
|
+
: packagedBundle.contents).toString();
|
|
83
|
+
let inlineType = (0, nullthrows_1.default)(entryBundle.getMainEntry()).meta.inlineType;
|
|
84
|
+
if (inlineType == null || inlineType === 'string') {
|
|
85
|
+
let placeholder = dependency.meta?.placeholder ?? dependency.id;
|
|
86
|
+
(0, assert_1.default)(typeof placeholder === 'string');
|
|
87
|
+
replacements.set(placeholder, getInlineReplacement(dependency, inlineType, packagedContents));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return performReplacement(replacements, contents, map);
|
|
91
|
+
}
|
|
92
|
+
function getURLReplacement({ dependency, fromBundle, toBundle, relative, getReplacement, }) {
|
|
93
|
+
let to;
|
|
94
|
+
let orig = url_1.default.parse(dependency.specifier);
|
|
95
|
+
if (relative) {
|
|
96
|
+
to = url_1.default.format({
|
|
97
|
+
pathname: (0, _1.relativeBundlePath)(fromBundle, toBundle, {
|
|
98
|
+
leadingDotSlash: false,
|
|
99
|
+
}),
|
|
100
|
+
hash: orig.hash,
|
|
101
|
+
});
|
|
102
|
+
// If the resulting path includes a colon character and doesn't start with a ./ or ../
|
|
103
|
+
// we need to add one so that the first part before the colon isn't parsed as a URL protocol.
|
|
104
|
+
if (to.includes(':') && !to.startsWith('./') && !to.startsWith('../')) {
|
|
105
|
+
to = './' + to;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
to = (0, _1.urlJoin)(toBundle.target.publicUrl, url_1.default.format({
|
|
110
|
+
pathname: (0, nullthrows_1.default)(toBundle.name),
|
|
111
|
+
hash: orig.hash,
|
|
112
|
+
}));
|
|
113
|
+
}
|
|
114
|
+
let placeholder = dependency.meta?.placeholder ?? dependency.id;
|
|
115
|
+
(0, assert_1.default)(typeof placeholder === 'string');
|
|
116
|
+
return {
|
|
117
|
+
from: placeholder,
|
|
118
|
+
to: getReplacement ? getReplacement(to) : to,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function performReplacement(replacements, contents, map) {
|
|
122
|
+
let finalContents = contents;
|
|
123
|
+
if ((0, feature_flags_1.getFeatureFlag)('inlineStringReplacementPerf')) {
|
|
124
|
+
let replacementList = Array.from(replacements.values());
|
|
125
|
+
if (replacementList.length > 0) {
|
|
126
|
+
finalContents = (0, rust_1.performStringReplacements)(contents, replacementList);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
for (let { from, to } of replacements.values()) {
|
|
131
|
+
// Perform replacement
|
|
132
|
+
finalContents = finalContents.split(from).join(to);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
contents: finalContents,
|
|
137
|
+
// TODO: Update sourcemap with adjusted contents
|
|
138
|
+
map,
|
|
139
|
+
};
|
|
140
|
+
}
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.fuzzySearch = fuzzySearch;
|
|
40
|
+
const diagnostic_1 = __importStar(require("@atlaspack/diagnostic"));
|
|
41
|
+
const nullthrows_1 = __importDefault(require("nullthrows"));
|
|
42
|
+
const levenshtein = __importStar(require("fastest-levenshtein"));
|
|
43
|
+
function validateSchema(schema, data) {
|
|
44
|
+
function walk(
|
|
45
|
+
// @ts-expect-error TS7006
|
|
46
|
+
schemaAncestors, dataNode, dataPath) {
|
|
47
|
+
let [schemaNode] = schemaAncestors;
|
|
48
|
+
if (schemaNode.type) {
|
|
49
|
+
let type = Array.isArray(dataNode) ? 'array' : typeof dataNode;
|
|
50
|
+
if (schemaNode.type !== type) {
|
|
51
|
+
return {
|
|
52
|
+
type: 'type',
|
|
53
|
+
dataType: 'value',
|
|
54
|
+
dataPath,
|
|
55
|
+
expectedTypes: [schemaNode.type],
|
|
56
|
+
ancestors: schemaAncestors,
|
|
57
|
+
prettyType: schemaNode.__type,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
switch (schemaNode.type) {
|
|
62
|
+
case 'array': {
|
|
63
|
+
if (schemaNode.items) {
|
|
64
|
+
let results = [];
|
|
65
|
+
// @ts-expect-error TS18046
|
|
66
|
+
for (let i = 0; i < dataNode.length; i++) {
|
|
67
|
+
let result = walk([schemaNode.items].concat(schemaAncestors),
|
|
68
|
+
// @ts-expect-error TS18046
|
|
69
|
+
dataNode[i], dataPath + '/' + i);
|
|
70
|
+
if (result)
|
|
71
|
+
results.push(result);
|
|
72
|
+
}
|
|
73
|
+
if (results.length)
|
|
74
|
+
return results.reduce((acc, v) => acc.concat(v), []);
|
|
75
|
+
}
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
case 'string': {
|
|
79
|
+
// @ts-expect-error TS2322
|
|
80
|
+
let value = dataNode;
|
|
81
|
+
if (schemaNode.enum) {
|
|
82
|
+
if (!schemaNode.enum.includes(value)) {
|
|
83
|
+
return {
|
|
84
|
+
type: 'enum',
|
|
85
|
+
dataType: 'value',
|
|
86
|
+
dataPath,
|
|
87
|
+
expectedValues: schemaNode.enum,
|
|
88
|
+
actualValue: value,
|
|
89
|
+
ancestors: schemaAncestors,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
else if (schemaNode.__validate) {
|
|
94
|
+
let validationError = schemaNode.__validate(value);
|
|
95
|
+
if (typeof validationError == 'string') {
|
|
96
|
+
return {
|
|
97
|
+
type: 'other',
|
|
98
|
+
dataType: 'value',
|
|
99
|
+
dataPath,
|
|
100
|
+
message: validationError,
|
|
101
|
+
actualValue: value,
|
|
102
|
+
ancestors: schemaAncestors,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
case 'number': {
|
|
109
|
+
// @ts-expect-error TS2322
|
|
110
|
+
let value = dataNode;
|
|
111
|
+
if (schemaNode.enum) {
|
|
112
|
+
if (!schemaNode.enum.includes(value)) {
|
|
113
|
+
return {
|
|
114
|
+
type: 'enum',
|
|
115
|
+
dataType: 'value',
|
|
116
|
+
dataPath,
|
|
117
|
+
expectedValues: schemaNode.enum,
|
|
118
|
+
actualValue: value,
|
|
119
|
+
ancestors: schemaAncestors,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
case 'object': {
|
|
126
|
+
let results = [];
|
|
127
|
+
let invalidProps;
|
|
128
|
+
if (schemaNode.__forbiddenProperties) {
|
|
129
|
+
// @ts-expect-error TS2769
|
|
130
|
+
let keys = Object.keys(dataNode);
|
|
131
|
+
// @ts-expect-error TS7006
|
|
132
|
+
invalidProps = schemaNode.__forbiddenProperties.filter((val) => keys.includes(val));
|
|
133
|
+
results.push(...invalidProps.map(
|
|
134
|
+
// @ts-expect-error TS7006
|
|
135
|
+
(k) => ({
|
|
136
|
+
type: 'forbidden-prop',
|
|
137
|
+
dataPath: dataPath + '/' + (0, diagnostic_1.encodeJSONKeyComponent)(k),
|
|
138
|
+
dataType: 'key',
|
|
139
|
+
prop: k,
|
|
140
|
+
expectedProps: Object.keys(schemaNode.properties),
|
|
141
|
+
actualProps: keys,
|
|
142
|
+
ancestors: schemaAncestors,
|
|
143
|
+
})));
|
|
144
|
+
}
|
|
145
|
+
if (schemaNode.required) {
|
|
146
|
+
// @ts-expect-error TS2769
|
|
147
|
+
let keys = Object.keys(dataNode);
|
|
148
|
+
let missingKeys = schemaNode.required.filter(
|
|
149
|
+
// @ts-expect-error TS7006
|
|
150
|
+
(val) => !keys.includes(val));
|
|
151
|
+
results.push(...missingKeys.map(
|
|
152
|
+
// @ts-expect-error TS7006
|
|
153
|
+
(k) => ({
|
|
154
|
+
type: 'missing-prop',
|
|
155
|
+
dataPath,
|
|
156
|
+
dataType: 'value',
|
|
157
|
+
prop: k,
|
|
158
|
+
expectedProps: schemaNode.required,
|
|
159
|
+
actualProps: keys,
|
|
160
|
+
ancestors: schemaAncestors,
|
|
161
|
+
})));
|
|
162
|
+
}
|
|
163
|
+
if (schemaNode.properties) {
|
|
164
|
+
let { additionalProperties = true } = schemaNode;
|
|
165
|
+
// @ts-expect-error TS2407
|
|
166
|
+
for (let k in dataNode) {
|
|
167
|
+
if (invalidProps && invalidProps.includes(k)) {
|
|
168
|
+
// Don't check type on forbidden props
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
else if (k in schemaNode.properties) {
|
|
172
|
+
let result = walk([schemaNode.properties[k]].concat(schemaAncestors),
|
|
173
|
+
// @ts-expect-error TS18046
|
|
174
|
+
dataNode[k], dataPath + '/' + (0, diagnostic_1.encodeJSONKeyComponent)(k));
|
|
175
|
+
if (result)
|
|
176
|
+
results.push(result);
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
if (typeof additionalProperties === 'boolean') {
|
|
180
|
+
if (!additionalProperties) {
|
|
181
|
+
results.push({
|
|
182
|
+
type: 'enum',
|
|
183
|
+
dataType: 'key',
|
|
184
|
+
dataPath: dataPath + '/' + (0, diagnostic_1.encodeJSONKeyComponent)(k),
|
|
185
|
+
expectedValues: Object.keys(schemaNode.properties).filter((p) => !(p in dataNode)),
|
|
186
|
+
actualValue: k,
|
|
187
|
+
ancestors: schemaAncestors,
|
|
188
|
+
prettyType: schemaNode.__type,
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
let result = walk([additionalProperties].concat(schemaAncestors),
|
|
194
|
+
// @ts-expect-error TS18046
|
|
195
|
+
dataNode[k], dataPath + '/' + (0, diagnostic_1.encodeJSONKeyComponent)(k));
|
|
196
|
+
if (result)
|
|
197
|
+
results.push(result);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (results.length)
|
|
203
|
+
return results.reduce((acc, v) => acc.concat(v), []);
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
case 'boolean':
|
|
207
|
+
// NOOP, type was checked already
|
|
208
|
+
break;
|
|
209
|
+
default:
|
|
210
|
+
throw new Error(`Unimplemented schema type ${type}?`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
if (schemaNode.enum && !schemaNode.enum.includes(dataNode)) {
|
|
216
|
+
return {
|
|
217
|
+
type: 'enum',
|
|
218
|
+
dataType: 'value',
|
|
219
|
+
dataPath: dataPath,
|
|
220
|
+
expectedValues: schemaNode.enum,
|
|
221
|
+
actualValue: schemaNode,
|
|
222
|
+
ancestors: schemaAncestors,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
if (schemaNode.oneOf || schemaNode.allOf) {
|
|
226
|
+
let list = schemaNode.oneOf || schemaNode.allOf;
|
|
227
|
+
let results = [];
|
|
228
|
+
for (let f of list) {
|
|
229
|
+
let result = walk([f].concat(schemaAncestors), dataNode, dataPath);
|
|
230
|
+
if (result)
|
|
231
|
+
results.push(result);
|
|
232
|
+
}
|
|
233
|
+
if (schemaNode.oneOf
|
|
234
|
+
? results.length == schemaNode.oneOf.length
|
|
235
|
+
: results.length > 0) {
|
|
236
|
+
// return the result with more values / longer key
|
|
237
|
+
results.sort((a, b) => Array.isArray(a) || Array.isArray(b)
|
|
238
|
+
? Array.isArray(a) && !Array.isArray(b)
|
|
239
|
+
? -1
|
|
240
|
+
: !Array.isArray(a) && Array.isArray(b)
|
|
241
|
+
? 1
|
|
242
|
+
: Array.isArray(a) && Array.isArray(b)
|
|
243
|
+
? b.length - a.length
|
|
244
|
+
: 0
|
|
245
|
+
: b.dataPath.length - a.dataPath.length);
|
|
246
|
+
return results[0];
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
else if (schemaNode.not) {
|
|
250
|
+
let result = walk([schemaNode.not].concat(schemaAncestors), dataNode, dataPath);
|
|
251
|
+
// @ts-expect-error TS2339
|
|
252
|
+
if (!result || result.length == 0) {
|
|
253
|
+
return {
|
|
254
|
+
type: 'other',
|
|
255
|
+
dataPath,
|
|
256
|
+
dataType: null,
|
|
257
|
+
message: schemaNode.__message,
|
|
258
|
+
actualValue: dataNode,
|
|
259
|
+
ancestors: schemaAncestors,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return undefined;
|
|
265
|
+
}
|
|
266
|
+
let result = walk([schema], data, '');
|
|
267
|
+
return Array.isArray(result) ? result : result ? [result] : [];
|
|
268
|
+
}
|
|
269
|
+
exports.default = validateSchema;
|
|
270
|
+
function fuzzySearch(expectedValues, actualValue) {
|
|
271
|
+
let result = expectedValues
|
|
272
|
+
.map((exp) => [exp, levenshtein.distance(exp, actualValue)])
|
|
273
|
+
.filter(
|
|
274
|
+
// Remove if more than half of the string would need to be changed
|
|
275
|
+
// @ts-expect-error TS2769
|
|
276
|
+
([, d]) => d * 2 < actualValue.length);
|
|
277
|
+
// @ts-expect-error TS2345
|
|
278
|
+
result.sort(([, a], [, b]) => a - b);
|
|
279
|
+
// @ts-expect-error TS2345
|
|
280
|
+
return result.map(([v]) => v);
|
|
281
|
+
}
|
|
282
|
+
validateSchema.diagnostic = function (schema, data, origin, message) {
|
|
283
|
+
if ('source' in data &&
|
|
284
|
+
'data' in data &&
|
|
285
|
+
typeof data.source !== 'string' &&
|
|
286
|
+
!data) {
|
|
287
|
+
throw new Error('At least one of data.source and data.data must be defined!');
|
|
288
|
+
}
|
|
289
|
+
// @ts-expect-error TS2339
|
|
290
|
+
let object = data.map
|
|
291
|
+
? // @ts-expect-error TS2339
|
|
292
|
+
data.map.data
|
|
293
|
+
: // @ts-expect-error TS2339
|
|
294
|
+
(data.data ?? JSON.parse(data.source));
|
|
295
|
+
let errors = validateSchema(schema, object);
|
|
296
|
+
if (errors.length) {
|
|
297
|
+
let keys = errors.map((e) => {
|
|
298
|
+
let message;
|
|
299
|
+
if (e.type === 'enum') {
|
|
300
|
+
let { actualValue } = e;
|
|
301
|
+
let expectedValues = e.expectedValues.map(String);
|
|
302
|
+
let likely = actualValue != null
|
|
303
|
+
? fuzzySearch(expectedValues, String(actualValue))
|
|
304
|
+
: [];
|
|
305
|
+
if (likely.length > 0) {
|
|
306
|
+
message = `Did you mean ${likely
|
|
307
|
+
.map((v) => JSON.stringify(v))
|
|
308
|
+
.join(', ')}?`;
|
|
309
|
+
}
|
|
310
|
+
else if (expectedValues.length > 0) {
|
|
311
|
+
message = `Possible values: ${expectedValues
|
|
312
|
+
.map((v) => JSON.stringify(v))
|
|
313
|
+
.join(', ')}`;
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
message = 'Unexpected value';
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
else if (e.type === 'forbidden-prop') {
|
|
320
|
+
let { prop, expectedProps, actualProps } = e;
|
|
321
|
+
let likely = fuzzySearch(expectedProps, prop).filter((v) => !actualProps.includes(v));
|
|
322
|
+
if (likely.length > 0) {
|
|
323
|
+
message = `Did you mean ${likely
|
|
324
|
+
.map((v) => JSON.stringify(v))
|
|
325
|
+
.join(', ')}?`;
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
message = 'Unexpected property';
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
else if (e.type === 'missing-prop') {
|
|
332
|
+
let { prop, actualProps } = e;
|
|
333
|
+
let likely = fuzzySearch(actualProps, prop);
|
|
334
|
+
if (likely.length > 0) {
|
|
335
|
+
message = `Did you mean ${JSON.stringify(prop)}?`;
|
|
336
|
+
e.dataPath += '/' + likely[0];
|
|
337
|
+
e.dataType = 'key';
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
message = `Missing property ${prop}`;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
else if (e.type === 'type') {
|
|
344
|
+
if (e.prettyType != null) {
|
|
345
|
+
message = `Expected ${e.prettyType}`;
|
|
346
|
+
}
|
|
347
|
+
else {
|
|
348
|
+
message = `Expected type ${e.expectedTypes.join(', ')}`;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
message = e.message;
|
|
353
|
+
}
|
|
354
|
+
return { key: e.dataPath, type: e.dataType, message };
|
|
355
|
+
});
|
|
356
|
+
let map, code;
|
|
357
|
+
// @ts-expect-error TS2339
|
|
358
|
+
if (data.map) {
|
|
359
|
+
// @ts-expect-error TS2339
|
|
360
|
+
map = data.map;
|
|
361
|
+
code = data.source;
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
// @ts-expect-error TS2339
|
|
365
|
+
map = data.source ?? JSON.stringify((0, nullthrows_1.default)(data.data), 0, '\t');
|
|
366
|
+
code = map;
|
|
367
|
+
}
|
|
368
|
+
let codeFrames = [
|
|
369
|
+
{
|
|
370
|
+
filePath: data.filePath ?? undefined,
|
|
371
|
+
language: 'json',
|
|
372
|
+
code,
|
|
373
|
+
codeHighlights: (0, diagnostic_1.generateJSONCodeHighlights)(map, keys.map(({ key, type, message }) => ({
|
|
374
|
+
key: (data.prependKey ?? '') + key,
|
|
375
|
+
type: type,
|
|
376
|
+
message: message != null ? (0, diagnostic_1.escapeMarkdown)(message) : message,
|
|
377
|
+
}))),
|
|
378
|
+
},
|
|
379
|
+
];
|
|
380
|
+
throw new diagnostic_1.default({
|
|
381
|
+
diagnostic: {
|
|
382
|
+
message: message,
|
|
383
|
+
origin,
|
|
384
|
+
// @ts-expect-error TS2322
|
|
385
|
+
codeFrames,
|
|
386
|
+
},
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
};
|