@atlaspack/utils 2.14.5-canary.14 → 2.14.5-canary.141

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.
Files changed (147) hide show
  1. package/CHANGELOG.md +146 -0
  2. package/benchmark.js +23 -0
  3. package/lib/DefaultMap.d.ts +13 -0
  4. package/lib/DefaultMap.js +42 -0
  5. package/lib/Deferred.d.ts +8 -0
  6. package/lib/Deferred.js +30 -0
  7. package/lib/PromiseQueue.d.ts +25 -0
  8. package/lib/PromiseQueue.js +112 -0
  9. package/lib/TapStream.d.ts +6 -0
  10. package/lib/TapStream.js +34 -0
  11. package/lib/alternatives.d.ts +3 -0
  12. package/lib/alternatives.js +116 -0
  13. package/lib/ansi-html.d.ts +1 -0
  14. package/lib/ansi-html.js +20 -0
  15. package/lib/blob.d.ts +4 -0
  16. package/lib/blob.js +40 -0
  17. package/lib/bundle-url.d.ts +4 -0
  18. package/lib/bundle-url.js +34 -0
  19. package/lib/collection.d.ts +33 -0
  20. package/lib/collection.js +111 -0
  21. package/lib/config.d.ts +17 -0
  22. package/lib/config.js +174 -0
  23. package/lib/countLines.d.ts +1 -0
  24. package/lib/countLines.js +15 -0
  25. package/lib/debounce.d.ts +1 -0
  26. package/lib/debounce.js +18 -0
  27. package/lib/debug-tools.d.ts +6 -0
  28. package/lib/debug-tools.js +37 -0
  29. package/lib/dependency-location.d.ts +14 -0
  30. package/lib/dependency-location.js +21 -0
  31. package/lib/escape-html.d.ts +1 -0
  32. package/lib/escape-html.js +22 -0
  33. package/lib/generateBuildMetrics.d.ts +18 -0
  34. package/lib/generateBuildMetrics.js +121 -0
  35. package/lib/generateCertificate.d.ts +5 -0
  36. package/lib/generateCertificate.js +129 -0
  37. package/lib/getCertificate.d.ts +6 -0
  38. package/lib/getCertificate.js +18 -0
  39. package/lib/getExisting.d.ts +8 -0
  40. package/lib/getExisting.js +25 -0
  41. package/lib/getModuleParts.d.ts +4 -0
  42. package/lib/getModuleParts.js +30 -0
  43. package/lib/getRootDir.d.ts +2 -0
  44. package/lib/getRootDir.js +52 -0
  45. package/lib/glob.d.ts +10 -0
  46. package/lib/glob.js +118 -0
  47. package/lib/hash.d.ts +7 -0
  48. package/lib/hash.js +50 -0
  49. package/lib/http-server.d.ts +19 -0
  50. package/lib/http-server.js +85 -0
  51. package/lib/index.d.ts +48 -0
  52. package/lib/index.js +635 -37310
  53. package/lib/is-url.d.ts +1 -0
  54. package/lib/is-url.js +24 -0
  55. package/lib/isDirectoryInside.d.ts +2 -0
  56. package/lib/isDirectoryInside.js +18 -0
  57. package/lib/objectHash.d.ts +3 -0
  58. package/lib/objectHash.js +26 -0
  59. package/lib/openInBrowser.d.ts +1 -0
  60. package/lib/openInBrowser.js +74 -0
  61. package/lib/parseCSSImport.d.ts +1 -0
  62. package/lib/parseCSSImport.js +15 -0
  63. package/lib/path.d.ts +8 -0
  64. package/lib/path.js +39 -0
  65. package/lib/prettifyTime.d.ts +1 -0
  66. package/lib/prettifyTime.js +9 -0
  67. package/lib/prettyDiagnostic.d.ts +17 -0
  68. package/lib/prettyDiagnostic.js +134 -0
  69. package/lib/progress-message.d.ts +3 -0
  70. package/lib/progress-message.js +36 -0
  71. package/lib/relativeBundlePath.d.ts +4 -0
  72. package/lib/relativeBundlePath.js +22 -0
  73. package/lib/relativeUrl.d.ts +1 -0
  74. package/lib/relativeUrl.js +24 -0
  75. package/lib/replaceBundleReferences.d.ts +39 -0
  76. package/lib/replaceBundleReferences.js +199 -0
  77. package/lib/schema.d.ts +107 -0
  78. package/lib/schema.js +355 -0
  79. package/lib/shared-buffer.d.ts +2 -0
  80. package/lib/shared-buffer.js +31 -0
  81. package/lib/sourcemap.d.ts +16 -0
  82. package/lib/sourcemap.js +127 -0
  83. package/lib/stream.d.ts +8 -0
  84. package/lib/stream.js +76 -0
  85. package/lib/throttle.d.ts +1 -0
  86. package/lib/throttle.js +15 -0
  87. package/lib/urlJoin.d.ts +5 -0
  88. package/lib/urlJoin.js +35 -0
  89. package/package.json +24 -17
  90. package/src/{DefaultMap.js → DefaultMap.ts} +8 -12
  91. package/src/Deferred.ts +26 -0
  92. package/src/{PromiseQueue.js → PromiseQueue.ts} +40 -35
  93. package/src/{TapStream.js → TapStream.ts} +8 -7
  94. package/src/{alternatives.js → alternatives.ts} +14 -15
  95. package/src/{ansi-html.js → ansi-html.ts} +1 -1
  96. package/src/{blob.js → blob.ts} +2 -4
  97. package/src/{bundle-url.js → bundle-url.ts} +3 -5
  98. package/src/{collection.js → collection.ts} +23 -20
  99. package/src/{config.js → config.ts} +18 -19
  100. package/src/{countLines.js → countLines.ts} +0 -2
  101. package/src/{debounce.js → debounce.ts} +3 -5
  102. package/src/debug-tools.ts +43 -0
  103. package/src/{dependency-location.js → dependency-location.ts} +15 -11
  104. package/src/{escape-html.js → escape-html.ts} +5 -3
  105. package/src/{generateBuildMetrics.js → generateBuildMetrics.ts} +16 -18
  106. package/src/{generateCertificate.js → generateCertificate.ts} +7 -5
  107. package/src/{getCertificate.js → getCertificate.ts} +5 -3
  108. package/src/{getExisting.js → getExisting.ts} +4 -3
  109. package/src/{getModuleParts.js → getModuleParts.ts} +3 -2
  110. package/src/{getRootDir.js → getRootDir.ts} +0 -2
  111. package/src/{glob.js → glob.ts} +14 -10
  112. package/src/{hash.js → hash.ts} +22 -17
  113. package/src/{http-server.js → http-server.ts} +32 -38
  114. package/src/{index.js → index.ts} +13 -9
  115. package/src/{is-url.js → is-url.ts} +1 -2
  116. package/src/{isDirectoryInside.js → isDirectoryInside.ts} +0 -1
  117. package/src/{objectHash.js → objectHash.ts} +1 -4
  118. package/src/{openInBrowser.js → openInBrowser.ts} +2 -4
  119. package/src/{parseCSSImport.js → parseCSSImport.ts} +0 -2
  120. package/src/{path.js → path.ts} +1 -3
  121. package/src/{prettifyTime.js → prettifyTime.ts} +0 -2
  122. package/src/{prettyDiagnostic.js → prettyDiagnostic.ts} +22 -20
  123. package/src/progress-message.ts +38 -0
  124. package/src/{relativeBundlePath.js → relativeBundlePath.ts} +3 -3
  125. package/src/{relativeUrl.js → relativeUrl.ts} +0 -1
  126. package/src/{replaceBundleReferences.js → replaceBundleReferences.ts} +54 -36
  127. package/src/{schema.js → schema.ts} +158 -141
  128. package/src/{shared-buffer.js → shared-buffer.ts} +5 -3
  129. package/src/{sourcemap.js → sourcemap.ts} +16 -6
  130. package/src/{stream.js → stream.ts} +29 -21
  131. package/src/throttle.ts +13 -0
  132. package/src/{urlJoin.js → urlJoin.ts} +1 -3
  133. package/test/{DefaultMap.test.js → DefaultMap.test.ts} +4 -6
  134. package/test/{PromiseQueue.test.js → PromiseQueue.test.ts} +5 -6
  135. package/test/{collection.test.js → collection.test.ts} +0 -2
  136. package/test/{config.test.js → config.test.ts} +0 -3
  137. package/test/{objectHash.test.js → objectHash.test.ts} +4 -5
  138. package/test/{prettifyTime.test.js → prettifyTime.test.ts} +0 -1
  139. package/test/{replaceBundleReferences.test.js → replaceBundleReferences.test.ts} +0 -32
  140. package/test/{sourcemap.test.js → sourcemap.test.ts} +0 -1
  141. package/test/{throttle.test.js → throttle.test.ts} +1 -3
  142. package/test/{urlJoin.test.js → urlJoin.test.ts} +0 -2
  143. package/tsconfig.json +4 -0
  144. package/lib/index.js.map +0 -1
  145. package/src/Deferred.js +0 -23
  146. package/src/progress-message.js +0 -22
  147. package/src/throttle.js +0 -15
@@ -0,0 +1,199 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getURLReplacement = getURLReplacement;
7
+ exports.replaceInlineReferences = replaceInlineReferences;
8
+ exports.replaceURLReferences = replaceURLReferences;
9
+ function _rust() {
10
+ const data = require("@atlaspack/rust");
11
+ _rust = function () {
12
+ return data;
13
+ };
14
+ return data;
15
+ }
16
+ function _featureFlags() {
17
+ const data = require("@atlaspack/feature-flags");
18
+ _featureFlags = function () {
19
+ return data;
20
+ };
21
+ return data;
22
+ }
23
+ function _stream() {
24
+ const data = require("stream");
25
+ _stream = function () {
26
+ return data;
27
+ };
28
+ return data;
29
+ }
30
+ function _nullthrows() {
31
+ const data = _interopRequireDefault(require("nullthrows"));
32
+ _nullthrows = function () {
33
+ return data;
34
+ };
35
+ return data;
36
+ }
37
+ function _assert() {
38
+ const data = _interopRequireDefault(require("assert"));
39
+ _assert = function () {
40
+ return data;
41
+ };
42
+ return data;
43
+ }
44
+ function _url() {
45
+ const data = _interopRequireDefault(require("url"));
46
+ _url = function () {
47
+ return data;
48
+ };
49
+ return data;
50
+ }
51
+ var _ = require("./");
52
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
53
+ /*
54
+ * Replaces references to dependency ids for URL dependencies with:
55
+ * - in the case of an unresolvable url dependency, the original specifier.
56
+ * These are external requests that Parcel did not bundle.
57
+ * - in the case of a reference to another bundle, the relative url to that
58
+ * bundle from the current bundle.
59
+ */
60
+ function replaceURLReferences({
61
+ bundle,
62
+ bundleGraph,
63
+ contents,
64
+ map,
65
+ getReplacement = s => s,
66
+ relative = true
67
+ }) {
68
+ let replacements = new Map();
69
+ let urlDependencies = [];
70
+ bundle.traverse(node => {
71
+ if (node.type === 'dependency' && node.value.specifierType === 'url') {
72
+ urlDependencies.push(node.value);
73
+ }
74
+ });
75
+ for (let dependency of urlDependencies) {
76
+ var _dependency$meta;
77
+ if (dependency.specifierType !== 'url') {
78
+ continue;
79
+ }
80
+ let placeholder = ((_dependency$meta = dependency.meta) === null || _dependency$meta === void 0 ? void 0 : _dependency$meta.placeholder) ?? dependency.id;
81
+ (0, _assert().default)(typeof placeholder === 'string');
82
+ let resolved = bundleGraph.getReferencedBundle(dependency, bundle);
83
+ if (resolved == null) {
84
+ replacements.set(placeholder, {
85
+ from: placeholder,
86
+ to: getReplacement(dependency.specifier)
87
+ });
88
+ continue;
89
+ }
90
+ if (resolved.bundleBehavior === 'inline') {
91
+ // If a bundle is inline, it should be replaced with inline contents,
92
+ // not a URL.
93
+ continue;
94
+ }
95
+ replacements.set(placeholder, getURLReplacement({
96
+ dependency,
97
+ fromBundle: bundle,
98
+ toBundle: resolved,
99
+ relative,
100
+ getReplacement
101
+ }));
102
+ }
103
+ return performReplacement(replacements, contents, map);
104
+ }
105
+
106
+ /*
107
+ * Replaces references to dependency ids for inline bundles with the packaged
108
+ * contents of that bundle.
109
+ */
110
+ async function replaceInlineReferences({
111
+ bundle,
112
+ bundleGraph,
113
+ contents,
114
+ map,
115
+ getInlineReplacement,
116
+ getInlineBundleContents
117
+ }) {
118
+ let replacements = new Map();
119
+ let dependencies = [];
120
+ bundle.traverse(node => {
121
+ if (node.type === 'dependency') {
122
+ dependencies.push(node.value);
123
+ }
124
+ });
125
+ for (let dependency of dependencies) {
126
+ let entryBundle = bundleGraph.getReferencedBundle(dependency, bundle);
127
+ if ((entryBundle === null || entryBundle === void 0 ? void 0 : entryBundle.bundleBehavior) !== 'inline') {
128
+ continue;
129
+ }
130
+ let packagedBundle = await getInlineBundleContents(entryBundle, bundleGraph);
131
+ let packagedContents = (packagedBundle.contents instanceof _stream().Readable ? await (0, _.bufferStream)(packagedBundle.contents) : packagedBundle.contents).toString();
132
+ let inlineType = (0, _nullthrows().default)(entryBundle.getMainEntry()).meta.inlineType;
133
+ if (inlineType == null || inlineType === 'string') {
134
+ var _dependency$meta2;
135
+ let placeholder = ((_dependency$meta2 = dependency.meta) === null || _dependency$meta2 === void 0 ? void 0 : _dependency$meta2.placeholder) ?? dependency.id;
136
+ (0, _assert().default)(typeof placeholder === 'string');
137
+ replacements.set(placeholder, getInlineReplacement(dependency, inlineType, packagedContents));
138
+ }
139
+ }
140
+ return performReplacement(replacements, contents, map);
141
+ }
142
+ function getURLReplacement({
143
+ dependency,
144
+ fromBundle,
145
+ toBundle,
146
+ relative,
147
+ getReplacement
148
+ }) {
149
+ var _dependency$meta3;
150
+ let to;
151
+ let orig = _url().default.parse(dependency.specifier);
152
+ if (relative) {
153
+ to = _url().default.format({
154
+ pathname: (0, _.relativeBundlePath)(fromBundle, toBundle, {
155
+ leadingDotSlash: false
156
+ }),
157
+ hash: orig.hash
158
+ });
159
+
160
+ // If the resulting path includes a colon character and doesn't start with a ./ or ../
161
+ // we need to add one so that the first part before the colon isn't parsed as a URL protocol.
162
+ if (to.includes(':') && !to.startsWith('./') && !to.startsWith('../')) {
163
+ to = './' + to;
164
+ }
165
+ } else {
166
+ to = (0, _.urlJoin)(toBundle.target.publicUrl, _url().default.format({
167
+ pathname: (0, _nullthrows().default)(toBundle.name),
168
+ hash: orig.hash
169
+ }));
170
+ }
171
+ let placeholder = ((_dependency$meta3 = dependency.meta) === null || _dependency$meta3 === void 0 ? void 0 : _dependency$meta3.placeholder) ?? dependency.id;
172
+ (0, _assert().default)(typeof placeholder === 'string');
173
+ return {
174
+ from: placeholder,
175
+ to: getReplacement ? getReplacement(to) : to
176
+ };
177
+ }
178
+ function performReplacement(replacements, contents, map) {
179
+ let finalContents = contents;
180
+ if ((0, _featureFlags().getFeatureFlag)('inlineStringReplacementPerf')) {
181
+ let replacementList = Array.from(replacements.values());
182
+ if (replacementList.length > 0) {
183
+ finalContents = (0, _rust().performStringReplacements)(contents, replacementList);
184
+ }
185
+ } else {
186
+ for (let {
187
+ from,
188
+ to
189
+ } of replacements.values()) {
190
+ // Perform replacement
191
+ finalContents = finalContents.split(from).join(to);
192
+ }
193
+ }
194
+ return {
195
+ contents: finalContents,
196
+ // TODO: Update sourcemap with adjusted contents
197
+ map
198
+ };
199
+ }
@@ -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,2 @@
1
+ export type Class<T> = new (...args: any[]) => T;
2
+ export declare let SharedBuffer: Class<ArrayBuffer> | Class<SharedArrayBuffer>;
@@ -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
+ }