@atlaspack/utils 2.17.3-typescript-e99c742e9.0 → 2.17.3-typescript-5b4d3ad41.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/lib/DefaultMap.d.ts +13 -0
- package/lib/DefaultMap.js +42 -0
- package/lib/Deferred.d.ts +8 -0
- package/lib/Deferred.js +30 -0
- package/lib/PromiseQueue.d.ts +25 -0
- package/lib/PromiseQueue.js +112 -0
- package/lib/TapStream.d.ts +6 -0
- package/lib/TapStream.js +34 -0
- package/lib/alternatives.d.ts +3 -0
- package/lib/alternatives.js +116 -0
- package/lib/ansi-html.d.ts +1 -0
- package/lib/ansi-html.js +20 -0
- package/lib/blob.d.ts +4 -0
- package/lib/blob.js +40 -0
- package/lib/bundle-url.d.ts +4 -0
- package/lib/bundle-url.js +34 -0
- package/lib/collection.d.ts +33 -0
- package/lib/collection.js +111 -0
- package/lib/config.d.ts +17 -0
- package/lib/config.js +174 -0
- package/lib/countLines.d.ts +1 -0
- package/lib/countLines.js +15 -0
- package/lib/debounce.d.ts +1 -0
- package/lib/debounce.js +18 -0
- package/lib/debug-tools.d.ts +6 -0
- package/lib/debug-tools.js +37 -0
- package/lib/dependency-location.d.ts +14 -0
- package/lib/dependency-location.js +21 -0
- package/lib/escape-html.d.ts +1 -0
- package/lib/escape-html.js +22 -0
- package/lib/generateBuildMetrics.d.ts +18 -0
- package/lib/generateBuildMetrics.js +121 -0
- package/lib/generateCertificate.d.ts +5 -0
- package/lib/generateCertificate.js +129 -0
- package/lib/getCertificate.d.ts +6 -0
- package/lib/getCertificate.js +18 -0
- package/lib/getExisting.d.ts +8 -0
- package/lib/getExisting.js +25 -0
- package/lib/getModuleParts.d.ts +4 -0
- package/lib/getModuleParts.js +30 -0
- package/lib/getRootDir.d.ts +2 -0
- package/lib/getRootDir.js +52 -0
- package/lib/glob.d.ts +10 -0
- package/lib/glob.js +118 -0
- package/lib/hash.d.ts +7 -0
- package/lib/hash.js +50 -0
- package/lib/http-server.d.ts +19 -0
- package/lib/http-server.js +85 -0
- package/lib/index.d.ts +48 -0
- package/lib/index.js +665 -0
- package/lib/is-url.d.ts +1 -0
- package/lib/is-url.js +24 -0
- package/lib/isDirectoryInside.d.ts +2 -0
- package/lib/isDirectoryInside.js +18 -0
- package/lib/objectHash.d.ts +3 -0
- package/lib/objectHash.js +26 -0
- package/lib/openInBrowser.d.ts +1 -0
- package/lib/openInBrowser.js +74 -0
- package/lib/parseCSSImport.d.ts +1 -0
- package/lib/parseCSSImport.js +15 -0
- package/lib/path.d.ts +8 -0
- package/lib/path.js +39 -0
- package/lib/prettifyTime.d.ts +1 -0
- package/lib/prettifyTime.js +9 -0
- package/lib/prettyDiagnostic.d.ts +17 -0
- package/lib/prettyDiagnostic.js +134 -0
- package/lib/progress-message.d.ts +3 -0
- package/lib/progress-message.js +36 -0
- package/lib/relativeBundlePath.d.ts +4 -0
- package/lib/relativeBundlePath.js +22 -0
- package/lib/relativeUrl.d.ts +1 -0
- package/lib/relativeUrl.js +24 -0
- package/lib/replaceBundleReferences.d.ts +39 -0
- package/lib/replaceBundleReferences.js +199 -0
- package/lib/schema.d.ts +107 -0
- package/lib/schema.js +355 -0
- package/lib/shared-buffer.d.ts +2 -0
- package/lib/shared-buffer.js +31 -0
- package/lib/sourcemap.d.ts +16 -0
- package/lib/sourcemap.js +127 -0
- package/lib/stream.d.ts +8 -0
- package/lib/stream.js +76 -0
- package/lib/throttle.d.ts +1 -0
- package/lib/throttle.js +15 -0
- package/lib/urlJoin.d.ts +5 -0
- package/lib/urlJoin.js +35 -0
- package/package.json +9 -9
package/lib/schema.d.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { Mapping } from '@mischnic/json-sourcemap';
|
|
2
|
+
export type SchemaEntity = SchemaObject | SchemaArray | SchemaBoolean | SchemaString | SchemaNumber | SchemaEnum | SchemaOneOf | SchemaAllOf | SchemaNot | SchemaAny;
|
|
3
|
+
export type SchemaArray = {
|
|
4
|
+
type: 'array';
|
|
5
|
+
items?: SchemaEntity;
|
|
6
|
+
__type?: string;
|
|
7
|
+
};
|
|
8
|
+
export type SchemaBoolean = {
|
|
9
|
+
type: 'boolean';
|
|
10
|
+
__type?: string;
|
|
11
|
+
};
|
|
12
|
+
export type SchemaOneOf = {
|
|
13
|
+
oneOf: Array<SchemaEntity>;
|
|
14
|
+
};
|
|
15
|
+
export type SchemaAllOf = {
|
|
16
|
+
allOf: Array<SchemaEntity>;
|
|
17
|
+
};
|
|
18
|
+
export type SchemaNot = {
|
|
19
|
+
not: SchemaEntity;
|
|
20
|
+
__message: string;
|
|
21
|
+
};
|
|
22
|
+
export type SchemaString = {
|
|
23
|
+
type: 'string';
|
|
24
|
+
enum?: Array<string>;
|
|
25
|
+
__validate?: (val: string) => string | null | undefined;
|
|
26
|
+
__type?: string;
|
|
27
|
+
};
|
|
28
|
+
export type SchemaNumber = {
|
|
29
|
+
type: 'number';
|
|
30
|
+
enum?: Array<number>;
|
|
31
|
+
__type?: string;
|
|
32
|
+
};
|
|
33
|
+
export type SchemaEnum = {
|
|
34
|
+
enum: Array<unknown>;
|
|
35
|
+
};
|
|
36
|
+
export type SchemaObject = {
|
|
37
|
+
type: 'object';
|
|
38
|
+
properties: {
|
|
39
|
+
[key: string]: SchemaEntity;
|
|
40
|
+
};
|
|
41
|
+
additionalProperties?: boolean | SchemaEntity;
|
|
42
|
+
required?: Array<string>;
|
|
43
|
+
__forbiddenProperties?: Array<string>;
|
|
44
|
+
__type?: string;
|
|
45
|
+
};
|
|
46
|
+
export type SchemaAny = Record<any, any>;
|
|
47
|
+
export type SchemaError = {
|
|
48
|
+
type: 'type';
|
|
49
|
+
expectedTypes: Array<string>;
|
|
50
|
+
dataType: 'key' | null | undefined | 'value';
|
|
51
|
+
dataPath: string;
|
|
52
|
+
ancestors: Array<SchemaEntity>;
|
|
53
|
+
prettyType?: string;
|
|
54
|
+
} | {
|
|
55
|
+
type: 'enum';
|
|
56
|
+
expectedValues: Array<unknown>;
|
|
57
|
+
dataType: 'key' | 'value';
|
|
58
|
+
actualValue: unknown;
|
|
59
|
+
dataPath: string;
|
|
60
|
+
ancestors: Array<SchemaEntity>;
|
|
61
|
+
prettyType?: string;
|
|
62
|
+
} | {
|
|
63
|
+
type: 'forbidden-prop';
|
|
64
|
+
prop: string;
|
|
65
|
+
expectedProps: Array<string>;
|
|
66
|
+
actualProps: Array<string>;
|
|
67
|
+
dataType: 'key';
|
|
68
|
+
dataPath: string;
|
|
69
|
+
ancestors: Array<SchemaEntity>;
|
|
70
|
+
prettyType?: string;
|
|
71
|
+
} | {
|
|
72
|
+
type: 'missing-prop';
|
|
73
|
+
prop: string;
|
|
74
|
+
expectedProps: Array<string>;
|
|
75
|
+
actualProps: Array<string>;
|
|
76
|
+
dataType: 'key' | 'value';
|
|
77
|
+
dataPath: string;
|
|
78
|
+
ancestors: Array<SchemaEntity>;
|
|
79
|
+
prettyType?: string;
|
|
80
|
+
} | {
|
|
81
|
+
type: 'other';
|
|
82
|
+
actualValue: unknown;
|
|
83
|
+
dataType: 'key' | null | undefined | 'value';
|
|
84
|
+
message?: string;
|
|
85
|
+
dataPath: string;
|
|
86
|
+
ancestors: Array<SchemaEntity>;
|
|
87
|
+
};
|
|
88
|
+
declare function validateSchema(schema: SchemaEntity, data: unknown): Array<SchemaError>;
|
|
89
|
+
declare namespace validateSchema {
|
|
90
|
+
var diagnostic: (schema: SchemaEntity, data: ({
|
|
91
|
+
source?: string | null | undefined;
|
|
92
|
+
data?: unknown;
|
|
93
|
+
} | {
|
|
94
|
+
source: string;
|
|
95
|
+
map: {
|
|
96
|
+
data: unknown;
|
|
97
|
+
pointers: {
|
|
98
|
+
[key: string]: Mapping;
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
}) & {
|
|
102
|
+
filePath?: string | null | undefined;
|
|
103
|
+
prependKey?: string | null | undefined;
|
|
104
|
+
}, origin: string, message: string) => undefined;
|
|
105
|
+
}
|
|
106
|
+
export default validateSchema;
|
|
107
|
+
export declare function fuzzySearch(expectedValues: Array<string>, actualValue: string): Array<string>;
|
package/lib/schema.js
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
exports.fuzzySearch = fuzzySearch;
|
|
8
|
+
function _diagnostic() {
|
|
9
|
+
const data = _interopRequireWildcard(require("@atlaspack/diagnostic"));
|
|
10
|
+
_diagnostic = function () {
|
|
11
|
+
return data;
|
|
12
|
+
};
|
|
13
|
+
return data;
|
|
14
|
+
}
|
|
15
|
+
function _nullthrows() {
|
|
16
|
+
const data = _interopRequireDefault(require("nullthrows"));
|
|
17
|
+
_nullthrows = function () {
|
|
18
|
+
return data;
|
|
19
|
+
};
|
|
20
|
+
return data;
|
|
21
|
+
}
|
|
22
|
+
function levenshtein() {
|
|
23
|
+
const data = _interopRequireWildcard(require("fastest-levenshtein"));
|
|
24
|
+
levenshtein = function () {
|
|
25
|
+
return data;
|
|
26
|
+
};
|
|
27
|
+
return data;
|
|
28
|
+
}
|
|
29
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
30
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
31
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
32
|
+
function validateSchema(schema, data) {
|
|
33
|
+
function walk(
|
|
34
|
+
// @ts-expect-error TS7006
|
|
35
|
+
schemaAncestors, dataNode, dataPath) {
|
|
36
|
+
let [schemaNode] = schemaAncestors;
|
|
37
|
+
if (schemaNode.type) {
|
|
38
|
+
let type = Array.isArray(dataNode) ? 'array' : typeof dataNode;
|
|
39
|
+
if (schemaNode.type !== type) {
|
|
40
|
+
return {
|
|
41
|
+
type: 'type',
|
|
42
|
+
dataType: 'value',
|
|
43
|
+
dataPath,
|
|
44
|
+
expectedTypes: [schemaNode.type],
|
|
45
|
+
ancestors: schemaAncestors,
|
|
46
|
+
prettyType: schemaNode.__type
|
|
47
|
+
};
|
|
48
|
+
} else {
|
|
49
|
+
switch (schemaNode.type) {
|
|
50
|
+
case 'array':
|
|
51
|
+
{
|
|
52
|
+
if (schemaNode.items) {
|
|
53
|
+
let results = [];
|
|
54
|
+
// @ts-expect-error TS18046
|
|
55
|
+
for (let i = 0; i < dataNode.length; i++) {
|
|
56
|
+
let result = walk([schemaNode.items].concat(schemaAncestors),
|
|
57
|
+
// @ts-expect-error TS18046
|
|
58
|
+
dataNode[i], dataPath + '/' + i);
|
|
59
|
+
if (result) results.push(result);
|
|
60
|
+
}
|
|
61
|
+
if (results.length) return results.reduce((acc, v) => acc.concat(v), []);
|
|
62
|
+
}
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
case 'string':
|
|
66
|
+
{
|
|
67
|
+
// @ts-expect-error TS2322
|
|
68
|
+
let value = dataNode;
|
|
69
|
+
if (schemaNode.enum) {
|
|
70
|
+
if (!schemaNode.enum.includes(value)) {
|
|
71
|
+
return {
|
|
72
|
+
type: 'enum',
|
|
73
|
+
dataType: 'value',
|
|
74
|
+
dataPath,
|
|
75
|
+
expectedValues: schemaNode.enum,
|
|
76
|
+
actualValue: value,
|
|
77
|
+
ancestors: schemaAncestors
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
} else if (schemaNode.__validate) {
|
|
81
|
+
let validationError = schemaNode.__validate(value);
|
|
82
|
+
if (typeof validationError == 'string') {
|
|
83
|
+
return {
|
|
84
|
+
type: 'other',
|
|
85
|
+
dataType: 'value',
|
|
86
|
+
dataPath,
|
|
87
|
+
message: validationError,
|
|
88
|
+
actualValue: value,
|
|
89
|
+
ancestors: schemaAncestors
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
case 'number':
|
|
96
|
+
{
|
|
97
|
+
// @ts-expect-error TS2322
|
|
98
|
+
let value = dataNode;
|
|
99
|
+
if (schemaNode.enum) {
|
|
100
|
+
if (!schemaNode.enum.includes(value)) {
|
|
101
|
+
return {
|
|
102
|
+
type: 'enum',
|
|
103
|
+
dataType: 'value',
|
|
104
|
+
dataPath,
|
|
105
|
+
expectedValues: schemaNode.enum,
|
|
106
|
+
actualValue: value,
|
|
107
|
+
ancestors: schemaAncestors
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
case 'object':
|
|
114
|
+
{
|
|
115
|
+
let results = [];
|
|
116
|
+
let invalidProps;
|
|
117
|
+
if (schemaNode.__forbiddenProperties) {
|
|
118
|
+
// @ts-expect-error TS2769
|
|
119
|
+
let keys = Object.keys(dataNode);
|
|
120
|
+
// @ts-expect-error TS7006
|
|
121
|
+
invalidProps = schemaNode.__forbiddenProperties.filter(val => keys.includes(val));
|
|
122
|
+
results.push(...invalidProps.map(
|
|
123
|
+
// @ts-expect-error TS7006
|
|
124
|
+
k => ({
|
|
125
|
+
type: 'forbidden-prop',
|
|
126
|
+
dataPath: dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k),
|
|
127
|
+
dataType: 'key',
|
|
128
|
+
prop: k,
|
|
129
|
+
expectedProps: Object.keys(schemaNode.properties),
|
|
130
|
+
actualProps: keys,
|
|
131
|
+
ancestors: schemaAncestors
|
|
132
|
+
})));
|
|
133
|
+
}
|
|
134
|
+
if (schemaNode.required) {
|
|
135
|
+
// @ts-expect-error TS2769
|
|
136
|
+
let keys = Object.keys(dataNode);
|
|
137
|
+
let missingKeys = schemaNode.required.filter(
|
|
138
|
+
// @ts-expect-error TS7006
|
|
139
|
+
val => !keys.includes(val));
|
|
140
|
+
results.push(...missingKeys.map(
|
|
141
|
+
// @ts-expect-error TS7006
|
|
142
|
+
k => ({
|
|
143
|
+
type: 'missing-prop',
|
|
144
|
+
dataPath,
|
|
145
|
+
dataType: 'value',
|
|
146
|
+
prop: k,
|
|
147
|
+
expectedProps: schemaNode.required,
|
|
148
|
+
actualProps: keys,
|
|
149
|
+
ancestors: schemaAncestors
|
|
150
|
+
})));
|
|
151
|
+
}
|
|
152
|
+
if (schemaNode.properties) {
|
|
153
|
+
let {
|
|
154
|
+
additionalProperties = true
|
|
155
|
+
} = schemaNode;
|
|
156
|
+
// @ts-expect-error TS2407
|
|
157
|
+
for (let k in dataNode) {
|
|
158
|
+
if (invalidProps && invalidProps.includes(k)) {
|
|
159
|
+
// Don't check type on forbidden props
|
|
160
|
+
continue;
|
|
161
|
+
} else if (k in schemaNode.properties) {
|
|
162
|
+
let result = walk([schemaNode.properties[k]].concat(schemaAncestors),
|
|
163
|
+
// @ts-expect-error TS18046
|
|
164
|
+
dataNode[k], dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k));
|
|
165
|
+
if (result) results.push(result);
|
|
166
|
+
} else {
|
|
167
|
+
if (typeof additionalProperties === 'boolean') {
|
|
168
|
+
if (!additionalProperties) {
|
|
169
|
+
results.push({
|
|
170
|
+
type: 'enum',
|
|
171
|
+
dataType: 'key',
|
|
172
|
+
dataPath: dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k),
|
|
173
|
+
expectedValues: Object.keys(schemaNode.properties
|
|
174
|
+
// @ts-expect-error TS18046
|
|
175
|
+
).filter(p => !(p in dataNode)),
|
|
176
|
+
actualValue: k,
|
|
177
|
+
ancestors: schemaAncestors,
|
|
178
|
+
prettyType: schemaNode.__type
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
} else {
|
|
182
|
+
let result = walk([additionalProperties].concat(schemaAncestors),
|
|
183
|
+
// @ts-expect-error TS18046
|
|
184
|
+
dataNode[k], dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k));
|
|
185
|
+
if (result) results.push(result);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (results.length) return results.reduce((acc, v) => acc.concat(v), []);
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
case 'boolean':
|
|
194
|
+
// NOOP, type was checked already
|
|
195
|
+
break;
|
|
196
|
+
default:
|
|
197
|
+
throw new Error(`Unimplemented schema type ${type}?`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
} else {
|
|
201
|
+
if (schemaNode.enum && !schemaNode.enum.includes(dataNode)) {
|
|
202
|
+
return {
|
|
203
|
+
type: 'enum',
|
|
204
|
+
dataType: 'value',
|
|
205
|
+
dataPath: dataPath,
|
|
206
|
+
expectedValues: schemaNode.enum,
|
|
207
|
+
actualValue: schemaNode,
|
|
208
|
+
ancestors: schemaAncestors
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
if (schemaNode.oneOf || schemaNode.allOf) {
|
|
212
|
+
let list = schemaNode.oneOf || schemaNode.allOf;
|
|
213
|
+
let results = [];
|
|
214
|
+
for (let f of list) {
|
|
215
|
+
let result = walk([f].concat(schemaAncestors), dataNode, dataPath);
|
|
216
|
+
if (result) results.push(result);
|
|
217
|
+
}
|
|
218
|
+
if (schemaNode.oneOf ? results.length == schemaNode.oneOf.length : results.length > 0) {
|
|
219
|
+
// return the result with more values / longer key
|
|
220
|
+
results.sort((a, b) => Array.isArray(a) || Array.isArray(b) ? Array.isArray(a) && !Array.isArray(b) ? -1 : !Array.isArray(a) && Array.isArray(b) ? 1 : Array.isArray(a) && Array.isArray(b) ? b.length - a.length : 0 : b.dataPath.length - a.dataPath.length);
|
|
221
|
+
return results[0];
|
|
222
|
+
}
|
|
223
|
+
} else if (schemaNode.not) {
|
|
224
|
+
let result = walk([schemaNode.not].concat(schemaAncestors), dataNode, dataPath);
|
|
225
|
+
// @ts-expect-error TS2339
|
|
226
|
+
if (!result || result.length == 0) {
|
|
227
|
+
return {
|
|
228
|
+
type: 'other',
|
|
229
|
+
dataPath,
|
|
230
|
+
dataType: null,
|
|
231
|
+
message: schemaNode.__message,
|
|
232
|
+
actualValue: dataNode,
|
|
233
|
+
ancestors: schemaAncestors
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return undefined;
|
|
239
|
+
}
|
|
240
|
+
let result = walk([schema], data, '');
|
|
241
|
+
return Array.isArray(result) ? result : result ? [result] : [];
|
|
242
|
+
}
|
|
243
|
+
var _default = exports.default = validateSchema;
|
|
244
|
+
function fuzzySearch(expectedValues, actualValue) {
|
|
245
|
+
let result = expectedValues.map(exp => [exp, levenshtein().distance(exp, actualValue)]).filter(
|
|
246
|
+
// Remove if more than half of the string would need to be changed
|
|
247
|
+
// @ts-expect-error TS2769
|
|
248
|
+
([, d]) => d * 2 < actualValue.length);
|
|
249
|
+
// @ts-expect-error TS2345
|
|
250
|
+
result.sort(([, a], [, b]) => a - b);
|
|
251
|
+
// @ts-expect-error TS2345
|
|
252
|
+
return result.map(([v]) => v);
|
|
253
|
+
}
|
|
254
|
+
validateSchema.diagnostic = function (schema, data, origin, message) {
|
|
255
|
+
if ('source' in data && 'data' in data && typeof data.source !== 'string' && !data) {
|
|
256
|
+
throw new Error('At least one of data.source and data.data must be defined!');
|
|
257
|
+
}
|
|
258
|
+
// @ts-expect-error TS2339
|
|
259
|
+
let object = data.map ?
|
|
260
|
+
// @ts-expect-error TS2339
|
|
261
|
+
data.map.data :
|
|
262
|
+
// @ts-expect-error TS2339
|
|
263
|
+
data.data ?? JSON.parse(data.source);
|
|
264
|
+
let errors = validateSchema(schema, object);
|
|
265
|
+
if (errors.length) {
|
|
266
|
+
let keys = errors.map(e => {
|
|
267
|
+
let message;
|
|
268
|
+
if (e.type === 'enum') {
|
|
269
|
+
let {
|
|
270
|
+
actualValue
|
|
271
|
+
} = e;
|
|
272
|
+
let expectedValues = e.expectedValues.map(String);
|
|
273
|
+
let likely = actualValue != null ? fuzzySearch(expectedValues, String(actualValue)) : [];
|
|
274
|
+
if (likely.length > 0) {
|
|
275
|
+
message = `Did you mean ${likely.map(v => JSON.stringify(v)).join(', ')}?`;
|
|
276
|
+
} else if (expectedValues.length > 0) {
|
|
277
|
+
message = `Possible values: ${expectedValues.map(v => JSON.stringify(v)).join(', ')}`;
|
|
278
|
+
} else {
|
|
279
|
+
message = 'Unexpected value';
|
|
280
|
+
}
|
|
281
|
+
} else if (e.type === 'forbidden-prop') {
|
|
282
|
+
let {
|
|
283
|
+
prop,
|
|
284
|
+
expectedProps,
|
|
285
|
+
actualProps
|
|
286
|
+
} = e;
|
|
287
|
+
let likely = fuzzySearch(expectedProps, prop).filter(v => !actualProps.includes(v));
|
|
288
|
+
if (likely.length > 0) {
|
|
289
|
+
message = `Did you mean ${likely.map(v => JSON.stringify(v)).join(', ')}?`;
|
|
290
|
+
} else {
|
|
291
|
+
message = 'Unexpected property';
|
|
292
|
+
}
|
|
293
|
+
} else if (e.type === 'missing-prop') {
|
|
294
|
+
let {
|
|
295
|
+
prop,
|
|
296
|
+
actualProps
|
|
297
|
+
} = e;
|
|
298
|
+
let likely = fuzzySearch(actualProps, prop);
|
|
299
|
+
if (likely.length > 0) {
|
|
300
|
+
message = `Did you mean ${JSON.stringify(prop)}?`;
|
|
301
|
+
e.dataPath += '/' + likely[0];
|
|
302
|
+
e.dataType = 'key';
|
|
303
|
+
} else {
|
|
304
|
+
message = `Missing property ${prop}`;
|
|
305
|
+
}
|
|
306
|
+
} else if (e.type === 'type') {
|
|
307
|
+
if (e.prettyType != null) {
|
|
308
|
+
message = `Expected ${e.prettyType}`;
|
|
309
|
+
} else {
|
|
310
|
+
message = `Expected type ${e.expectedTypes.join(', ')}`;
|
|
311
|
+
}
|
|
312
|
+
} else {
|
|
313
|
+
message = e.message;
|
|
314
|
+
}
|
|
315
|
+
return {
|
|
316
|
+
key: e.dataPath,
|
|
317
|
+
type: e.dataType,
|
|
318
|
+
message
|
|
319
|
+
};
|
|
320
|
+
});
|
|
321
|
+
let map, code;
|
|
322
|
+
// @ts-expect-error TS2339
|
|
323
|
+
if (data.map) {
|
|
324
|
+
// @ts-expect-error TS2339
|
|
325
|
+
map = data.map;
|
|
326
|
+
code = data.source;
|
|
327
|
+
} else {
|
|
328
|
+
// @ts-expect-error TS2339
|
|
329
|
+
map = data.source ?? JSON.stringify((0, _nullthrows().default)(data.data), 0, '\t');
|
|
330
|
+
code = map;
|
|
331
|
+
}
|
|
332
|
+
let codeFrames = [{
|
|
333
|
+
filePath: data.filePath ?? undefined,
|
|
334
|
+
language: 'json',
|
|
335
|
+
code,
|
|
336
|
+
codeHighlights: (0, _diagnostic().generateJSONCodeHighlights)(map, keys.map(({
|
|
337
|
+
key,
|
|
338
|
+
type,
|
|
339
|
+
message
|
|
340
|
+
}) => ({
|
|
341
|
+
key: (data.prependKey ?? '') + key,
|
|
342
|
+
type: type,
|
|
343
|
+
message: message != null ? (0, _diagnostic().escapeMarkdown)(message) : message
|
|
344
|
+
})))
|
|
345
|
+
}];
|
|
346
|
+
throw new (_diagnostic().default)({
|
|
347
|
+
diagnostic: {
|
|
348
|
+
message: message,
|
|
349
|
+
origin,
|
|
350
|
+
// @ts-expect-error TS2322
|
|
351
|
+
codeFrames
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.SharedBuffer = void 0;
|
|
7
|
+
// flow-to-ts helpers
|
|
8
|
+
|
|
9
|
+
// /flow-to-ts helpers
|
|
10
|
+
|
|
11
|
+
let SharedBuffer = exports.SharedBuffer = void 0;
|
|
12
|
+
|
|
13
|
+
// @ts-expect-error process.browser is a browser-specific property
|
|
14
|
+
if (process.browser) {
|
|
15
|
+
exports.SharedBuffer = SharedBuffer = ArrayBuffer;
|
|
16
|
+
// Safari has removed the constructor
|
|
17
|
+
if (typeof SharedArrayBuffer !== 'undefined') {
|
|
18
|
+
let channel = new MessageChannel();
|
|
19
|
+
try {
|
|
20
|
+
// Firefox might throw when sending the Buffer over a MessagePort
|
|
21
|
+
channel.port1.postMessage(new SharedArrayBuffer(0));
|
|
22
|
+
exports.SharedBuffer = SharedBuffer = SharedArrayBuffer;
|
|
23
|
+
} catch (_) {
|
|
24
|
+
// NOOP
|
|
25
|
+
}
|
|
26
|
+
channel.port1.close();
|
|
27
|
+
channel.port2.close();
|
|
28
|
+
}
|
|
29
|
+
} else {
|
|
30
|
+
exports.SharedBuffer = SharedBuffer = SharedArrayBuffer;
|
|
31
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { SourceLocation } from '@atlaspack/types';
|
|
2
|
+
import type { FileSystem } from '@atlaspack/fs';
|
|
3
|
+
import SourceMap from '@parcel/source-map';
|
|
4
|
+
export declare const SOURCEMAP_RE: RegExp;
|
|
5
|
+
export declare const SOURCEMAP_EXTENSIONS: Set<string>;
|
|
6
|
+
export declare function matchSourceMappingURL(contents: string): RegExpMatchArray | null;
|
|
7
|
+
export declare function loadSourceMapUrl(fs: FileSystem, filename: string, contents: string): Promise<{
|
|
8
|
+
filename: string;
|
|
9
|
+
map: any;
|
|
10
|
+
url: string;
|
|
11
|
+
} | null | undefined>;
|
|
12
|
+
export declare function loadSourceMap(filename: string, contents: string, options: {
|
|
13
|
+
fs: FileSystem;
|
|
14
|
+
projectRoot: string;
|
|
15
|
+
}): Promise<SourceMap | null | undefined>;
|
|
16
|
+
export declare function remapSourceLocation(loc: SourceLocation, originalMap: SourceMap): SourceLocation;
|
package/lib/sourcemap.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.SOURCEMAP_RE = exports.SOURCEMAP_EXTENSIONS = void 0;
|
|
7
|
+
exports.loadSourceMap = loadSourceMap;
|
|
8
|
+
exports.loadSourceMapUrl = loadSourceMapUrl;
|
|
9
|
+
exports.matchSourceMappingURL = matchSourceMappingURL;
|
|
10
|
+
exports.remapSourceLocation = remapSourceLocation;
|
|
11
|
+
function _sourceMap() {
|
|
12
|
+
const data = _interopRequireDefault(require("@parcel/source-map"));
|
|
13
|
+
_sourceMap = function () {
|
|
14
|
+
return data;
|
|
15
|
+
};
|
|
16
|
+
return data;
|
|
17
|
+
}
|
|
18
|
+
function _path() {
|
|
19
|
+
const data = _interopRequireDefault(require("path"));
|
|
20
|
+
_path = function () {
|
|
21
|
+
return data;
|
|
22
|
+
};
|
|
23
|
+
return data;
|
|
24
|
+
}
|
|
25
|
+
var _path2 = require("./path");
|
|
26
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
27
|
+
const SOURCEMAP_RE = exports.SOURCEMAP_RE = /(?:\/\*|\/\/)\s*[@#]\s*sourceMappingURL\s*=\s*([^\s*]+)(?:\s*\*\/)?\s*$/;
|
|
28
|
+
const DATA_URL_RE = /^data:[^;]+(?:;charset=[^;]+)?;base64,(.*)/;
|
|
29
|
+
const SOURCEMAP_EXTENSIONS = exports.SOURCEMAP_EXTENSIONS = new Set(['css', 'es', 'es6', 'js', 'jsx', 'mjs', 'ts', 'tsx']);
|
|
30
|
+
function matchSourceMappingURL(contents) {
|
|
31
|
+
return contents.match(SOURCEMAP_RE);
|
|
32
|
+
}
|
|
33
|
+
async function loadSourceMapUrl(fs, filename, contents) {
|
|
34
|
+
let match = matchSourceMappingURL(contents);
|
|
35
|
+
if (match) {
|
|
36
|
+
let url = match[1].trim();
|
|
37
|
+
let dataURLMatch = url.match(DATA_URL_RE);
|
|
38
|
+
let mapFilePath;
|
|
39
|
+
if (dataURLMatch) {
|
|
40
|
+
mapFilePath = filename;
|
|
41
|
+
} else {
|
|
42
|
+
mapFilePath = url.replace(/^file:\/\//, '');
|
|
43
|
+
mapFilePath = (0, _path2.isAbsolute)(mapFilePath) ? mapFilePath : _path().default.join(_path().default.dirname(filename), mapFilePath);
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
url,
|
|
47
|
+
filename: mapFilePath,
|
|
48
|
+
map: JSON.parse(dataURLMatch ? Buffer.from(dataURLMatch[1], 'base64').toString() : await fs.readFile(mapFilePath, 'utf8'))
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async function loadSourceMap(filename, contents, options) {
|
|
53
|
+
let foundMap = await loadSourceMapUrl(options.fs, filename, contents);
|
|
54
|
+
if (foundMap) {
|
|
55
|
+
let mapSourceRoot = _path().default.dirname(filename);
|
|
56
|
+
if (foundMap.map.sourceRoot && !(0, _path2.normalizeSeparators)(foundMap.map.sourceRoot).startsWith('/')) {
|
|
57
|
+
mapSourceRoot = _path().default.join(mapSourceRoot, foundMap.map.sourceRoot);
|
|
58
|
+
}
|
|
59
|
+
let sourcemapInstance = new (_sourceMap().default)(options.projectRoot);
|
|
60
|
+
sourcemapInstance.addVLQMap({
|
|
61
|
+
...foundMap.map,
|
|
62
|
+
sources: foundMap.map.sources.map(s => {
|
|
63
|
+
return _path().default.join(mapSourceRoot, s);
|
|
64
|
+
})
|
|
65
|
+
});
|
|
66
|
+
return sourcemapInstance;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function remapSourceLocation(loc, originalMap) {
|
|
70
|
+
let {
|
|
71
|
+
filePath,
|
|
72
|
+
start: {
|
|
73
|
+
line: startLine,
|
|
74
|
+
column: startCol
|
|
75
|
+
},
|
|
76
|
+
end: {
|
|
77
|
+
line: endLine,
|
|
78
|
+
column: endCol
|
|
79
|
+
}
|
|
80
|
+
} = loc;
|
|
81
|
+
let lineDiff = endLine - startLine;
|
|
82
|
+
let colDiff = endCol - startCol;
|
|
83
|
+
let start = originalMap.findClosestMapping(startLine, startCol - 1);
|
|
84
|
+
let end = originalMap.findClosestMapping(endLine, endCol - 1);
|
|
85
|
+
if (start !== null && start !== void 0 && start.original) {
|
|
86
|
+
if (start.source) {
|
|
87
|
+
filePath = start.source;
|
|
88
|
+
}
|
|
89
|
+
({
|
|
90
|
+
line: startLine,
|
|
91
|
+
column: startCol
|
|
92
|
+
} = start.original);
|
|
93
|
+
startCol++; // source map columns are 0-based
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (end !== null && end !== void 0 && end.original) {
|
|
97
|
+
({
|
|
98
|
+
line: endLine,
|
|
99
|
+
column: endCol
|
|
100
|
+
} = end.original);
|
|
101
|
+
endCol++; // source map columns are 0-based
|
|
102
|
+
|
|
103
|
+
if (endLine < startLine) {
|
|
104
|
+
endLine = startLine;
|
|
105
|
+
endCol = startCol;
|
|
106
|
+
} else if (endLine === startLine && endCol < startCol && lineDiff === 0) {
|
|
107
|
+
endCol = startCol + colDiff;
|
|
108
|
+
} else if (endLine === startLine && startCol === endCol && lineDiff === 0) {
|
|
109
|
+
// Prevent 0-length ranges
|
|
110
|
+
endCol = startCol + 1;
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
endLine = startLine;
|
|
114
|
+
endCol = startCol;
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
filePath,
|
|
118
|
+
start: {
|
|
119
|
+
line: startLine,
|
|
120
|
+
column: startCol
|
|
121
|
+
},
|
|
122
|
+
end: {
|
|
123
|
+
line: endLine,
|
|
124
|
+
column: endCol
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
package/lib/stream.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Readable } from 'stream';
|
|
2
|
+
import type { Blob } from '@atlaspack/types';
|
|
3
|
+
export declare function measureStreamLength(stream: Readable): Promise<number>;
|
|
4
|
+
export declare function readableFromStringOrBuffer(str: string | Buffer): Readable;
|
|
5
|
+
export declare function bufferStream(stream: Readable): Promise<Buffer>;
|
|
6
|
+
export declare function blobToStream(blob: Blob): Readable;
|
|
7
|
+
export declare function streamFromPromise(promise: Promise<Blob>): Readable;
|
|
8
|
+
export declare function fallbackStream(stream: Readable, fallback: () => Readable): Readable;
|