@spyglassmc/core 0.1.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.
Files changed (186) hide show
  1. package/README.md +10 -0
  2. package/lib/common/Heap.d.ts +1 -0
  3. package/lib/common/Heap.js +2 -0
  4. package/lib/common/index.d.ts +2 -0
  5. package/lib/common/index.js +14 -0
  6. package/lib/common/util.d.ts +120 -0
  7. package/lib/common/util.js +292 -0
  8. package/lib/index.d.ts +8 -0
  9. package/lib/index.js +20 -0
  10. package/lib/node/AstNode.d.ts +64 -0
  11. package/lib/node/AstNode.js +108 -0
  12. package/lib/node/BooleanNode.d.ts +13 -0
  13. package/lib/node/BooleanNode.js +20 -0
  14. package/lib/node/CommentNode.d.ts +12 -0
  15. package/lib/node/CommentNode.js +11 -0
  16. package/lib/node/ErrorNode.d.ts +8 -0
  17. package/lib/node/ErrorNode.js +12 -0
  18. package/lib/node/FileNode.d.ts +21 -0
  19. package/lib/node/FileNode.js +11 -0
  20. package/lib/node/FloatNode.d.ts +13 -0
  21. package/lib/node/FloatNode.js +21 -0
  22. package/lib/node/IntegerNode.d.ts +13 -0
  23. package/lib/node/IntegerNode.js +20 -0
  24. package/lib/node/ListNode.d.ts +15 -0
  25. package/lib/node/ListNode.js +11 -0
  26. package/lib/node/LiteralNode.d.ts +19 -0
  27. package/lib/node/LiteralNode.js +22 -0
  28. package/lib/node/LongNode.d.ts +13 -0
  29. package/lib/node/LongNode.js +20 -0
  30. package/lib/node/RecordNode.d.ts +20 -0
  31. package/lib/node/RecordNode.js +11 -0
  32. package/lib/node/ResourceLocationNode.d.ts +41 -0
  33. package/lib/node/ResourceLocationNode.js +65 -0
  34. package/lib/node/Sequence.d.ts +22 -0
  35. package/lib/node/Sequence.js +11 -0
  36. package/lib/node/StringNode.d.ts +63 -0
  37. package/lib/node/StringNode.js +43 -0
  38. package/lib/node/SymbolNode.d.ts +21 -0
  39. package/lib/node/SymbolNode.js +22 -0
  40. package/lib/node/TableNode.d.ts +17 -0
  41. package/lib/node/TableNode.js +11 -0
  42. package/lib/node/index.d.ts +16 -0
  43. package/lib/node/index.js +28 -0
  44. package/lib/parser/Parser.d.ts +16 -0
  45. package/lib/parser/Parser.js +5 -0
  46. package/lib/parser/boolean.d.ts +4 -0
  47. package/lib/parser/boolean.js +11 -0
  48. package/lib/parser/comment.d.ts +12 -0
  49. package/lib/parser/comment.js +34 -0
  50. package/lib/parser/empty.d.ts +3 -0
  51. package/lib/parser/empty.js +7 -0
  52. package/lib/parser/error.d.ts +8 -0
  53. package/lib/parser/error.js +22 -0
  54. package/lib/parser/file.d.ts +9 -0
  55. package/lib/parser/file.js +37 -0
  56. package/lib/parser/float.d.ts +33 -0
  57. package/lib/parser/float.js +57 -0
  58. package/lib/parser/index.d.ts +17 -0
  59. package/lib/parser/index.js +36 -0
  60. package/lib/parser/integer.d.ts +33 -0
  61. package/lib/parser/integer.js +46 -0
  62. package/lib/parser/list.d.ts +12 -0
  63. package/lib/parser/list.js +70 -0
  64. package/lib/parser/literal.d.ts +5 -0
  65. package/lib/parser/literal.js +41 -0
  66. package/lib/parser/long.d.ts +33 -0
  67. package/lib/parser/long.js +50 -0
  68. package/lib/parser/record.d.ts +22 -0
  69. package/lib/parser/record.js +97 -0
  70. package/lib/parser/resourceLocation.d.ts +4 -0
  71. package/lib/parser/resourceLocation.js +67 -0
  72. package/lib/parser/string.d.ts +18 -0
  73. package/lib/parser/string.js +129 -0
  74. package/lib/parser/symbol.d.ts +12 -0
  75. package/lib/parser/symbol.js +33 -0
  76. package/lib/parser/table.d.ts +22 -0
  77. package/lib/parser/table.js +97 -0
  78. package/lib/parser/util.d.ts +134 -0
  79. package/lib/parser/util.js +224 -0
  80. package/lib/processor/ColorInfoProvider.d.ts +87 -0
  81. package/lib/processor/ColorInfoProvider.js +141 -0
  82. package/lib/processor/InlayHintProvider.d.ts +8 -0
  83. package/lib/processor/InlayHintProvider.js +3 -0
  84. package/lib/processor/SignatureHelpProvider.d.ts +18 -0
  85. package/lib/processor/SignatureHelpProvider.js +3 -0
  86. package/lib/processor/checker/Checker.d.ts +7 -0
  87. package/lib/processor/checker/Checker.js +7 -0
  88. package/lib/processor/checker/builtin.d.ts +23 -0
  89. package/lib/processor/checker/builtin.js +83 -0
  90. package/lib/processor/checker/index.d.ts +3 -0
  91. package/lib/processor/checker/index.js +28 -0
  92. package/lib/processor/colorizer/Colorizer.d.ts +23 -0
  93. package/lib/processor/colorizer/Colorizer.js +70 -0
  94. package/lib/processor/colorizer/builtin.d.ts +17 -0
  95. package/lib/processor/colorizer/builtin.js +84 -0
  96. package/lib/processor/colorizer/index.d.ts +3 -0
  97. package/lib/processor/colorizer/index.js +28 -0
  98. package/lib/processor/completer/Completer.d.ts +75 -0
  99. package/lib/processor/completer/Completer.js +92 -0
  100. package/lib/processor/completer/builtin.d.ts +30 -0
  101. package/lib/processor/completer/builtin.js +172 -0
  102. package/lib/processor/completer/index.d.ts +3 -0
  103. package/lib/processor/completer/index.js +28 -0
  104. package/lib/processor/formatter/Formatter.d.ts +6 -0
  105. package/lib/processor/formatter/Formatter.js +19 -0
  106. package/lib/processor/formatter/builtin.d.ts +15 -0
  107. package/lib/processor/formatter/builtin.js +60 -0
  108. package/lib/processor/formatter/index.d.ts +3 -0
  109. package/lib/processor/formatter/index.js +28 -0
  110. package/lib/processor/index.d.ts +10 -0
  111. package/lib/processor/index.js +22 -0
  112. package/lib/processor/linter/Linter.d.ts +4 -0
  113. package/lib/processor/linter/Linter.js +3 -0
  114. package/lib/processor/linter/builtin/undeclaredSymbol.d.ts +4 -0
  115. package/lib/processor/linter/builtin/undeclaredSymbol.js +73 -0
  116. package/lib/processor/linter/builtin.d.ts +15 -0
  117. package/lib/processor/linter/builtin.js +120 -0
  118. package/lib/processor/linter/index.d.ts +3 -0
  119. package/lib/processor/linter/index.js +28 -0
  120. package/lib/processor/util.d.ts +17 -0
  121. package/lib/processor/util.js +32 -0
  122. package/lib/service/CacheService.d.ts +54 -0
  123. package/lib/service/CacheService.js +195 -0
  124. package/lib/service/CommandExecutor.d.ts +3 -0
  125. package/lib/service/CommandExecutor.js +7 -0
  126. package/lib/service/Config.d.ts +218 -0
  127. package/lib/service/Config.js +264 -0
  128. package/lib/service/Context.d.ts +143 -0
  129. package/lib/service/Context.js +143 -0
  130. package/lib/service/Dependency.d.ts +10 -0
  131. package/lib/service/Dependency.js +11 -0
  132. package/lib/service/Downloader.d.ts +67 -0
  133. package/lib/service/Downloader.js +185 -0
  134. package/lib/service/ErrorReporter.d.ts +27 -0
  135. package/lib/service/ErrorReporter.js +49 -0
  136. package/lib/service/FileService.d.ts +96 -0
  137. package/lib/service/FileService.js +224 -0
  138. package/lib/service/Hover.d.ts +10 -0
  139. package/lib/service/Hover.js +16 -0
  140. package/lib/service/Logger.d.ts +31 -0
  141. package/lib/service/Logger.js +30 -0
  142. package/lib/service/MetaRegistry.d.ts +106 -0
  143. package/lib/service/MetaRegistry.js +192 -0
  144. package/lib/service/Operations.d.ts +8 -0
  145. package/lib/service/Operations.js +26 -0
  146. package/lib/service/Profiler.d.ts +33 -0
  147. package/lib/service/Profiler.js +76 -0
  148. package/lib/service/Project.d.ts +173 -0
  149. package/lib/service/Project.js +593 -0
  150. package/lib/service/Service.d.ts +55 -0
  151. package/lib/service/Service.js +207 -0
  152. package/lib/service/SymbolLocations.d.ts +17 -0
  153. package/lib/service/SymbolLocations.js +16 -0
  154. package/lib/service/SymbolRegistrar.d.ts +5 -0
  155. package/lib/service/SymbolRegistrar.js +3 -0
  156. package/lib/service/fileUtil.d.ts +121 -0
  157. package/lib/service/fileUtil.js +268 -0
  158. package/lib/service/index.d.ts +18 -0
  159. package/lib/service/index.js +33 -0
  160. package/lib/source/IndexMap.d.ts +16 -0
  161. package/lib/source/IndexMap.js +46 -0
  162. package/lib/source/LanguageError.d.ts +27 -0
  163. package/lib/source/LanguageError.js +15 -0
  164. package/lib/source/Location.d.ts +19 -0
  165. package/lib/source/Location.js +25 -0
  166. package/lib/source/Offset.d.ts +13 -0
  167. package/lib/source/Offset.js +25 -0
  168. package/lib/source/Position.d.ts +23 -0
  169. package/lib/source/Position.js +43 -0
  170. package/lib/source/PositionRange.d.ts +38 -0
  171. package/lib/source/PositionRange.js +83 -0
  172. package/lib/source/Range.d.ts +68 -0
  173. package/lib/source/Range.js +155 -0
  174. package/lib/source/Source.d.ts +110 -0
  175. package/lib/source/Source.js +222 -0
  176. package/lib/source/index.d.ts +9 -0
  177. package/lib/source/index.js +21 -0
  178. package/lib/symbol/Symbol.d.ts +219 -0
  179. package/lib/symbol/Symbol.js +324 -0
  180. package/lib/symbol/SymbolUtil.d.ts +407 -0
  181. package/lib/symbol/SymbolUtil.js +880 -0
  182. package/lib/symbol/UriBinder.d.ts +3 -0
  183. package/lib/symbol/UriBinder.js +3 -0
  184. package/lib/symbol/index.d.ts +4 -0
  185. package/lib/symbol/index.js +16 -0
  186. package/package.json +41 -0
@@ -0,0 +1,143 @@
1
+ import type { TextDocument } from 'vscode-languageserver-textdocument';
2
+ import type { LinterErrorReporter } from '.';
3
+ import type { Range } from '../source';
4
+ import { ReadonlySource } from '../source';
5
+ import type { SymbolTable, SymbolUtil } from '../symbol';
6
+ import type { Config } from './Config';
7
+ import { ErrorReporter } from './ErrorReporter';
8
+ import type { FileService } from './FileService';
9
+ import type { RootUriString } from './fileUtil';
10
+ import type { Logger } from './Logger';
11
+ import type { MetaRegistry } from './MetaRegistry';
12
+ import { Operations } from './Operations';
13
+ import type { DocAndNode, ProjectData } from './Project';
14
+ export interface ContextBase {
15
+ fs: FileService;
16
+ getDocAndNode: (uri: string) => DocAndNode | undefined;
17
+ global: SymbolTable;
18
+ logger: Logger;
19
+ meta: MetaRegistry;
20
+ project: Record<string, string>;
21
+ roots: readonly RootUriString[];
22
+ }
23
+ export declare namespace ContextBase {
24
+ function create(project: ProjectData): ContextBase;
25
+ }
26
+ export interface ParserContext extends ContextBase {
27
+ config: Config;
28
+ doc: TextDocument;
29
+ err: ErrorReporter;
30
+ /** @deprecated */
31
+ symbols: SymbolUtil;
32
+ }
33
+ interface ParserContextOptions {
34
+ doc: TextDocument;
35
+ err?: ErrorReporter;
36
+ }
37
+ export declare namespace ParserContext {
38
+ function create(project: ProjectData, opts: ParserContextOptions): ParserContext;
39
+ }
40
+ export interface ProcessorContext extends ContextBase {
41
+ config: Config;
42
+ doc: TextDocument;
43
+ src: ReadonlySource;
44
+ /** @deprecated */
45
+ symbols: SymbolUtil;
46
+ }
47
+ interface ProcessorContextOptions {
48
+ doc: TextDocument;
49
+ src?: ReadonlySource;
50
+ }
51
+ export declare namespace ProcessorContext {
52
+ function create(project: ProjectData, opts: ProcessorContextOptions): ProcessorContext;
53
+ }
54
+ interface ProcessorWithRangeContext extends ProcessorContext {
55
+ range?: Range;
56
+ }
57
+ interface ProcessorWithRangeContextOptions extends ProcessorContextOptions {
58
+ range?: Range;
59
+ }
60
+ declare namespace ProcessorWithRangeContext {
61
+ function create(project: ProjectData, opts: ProcessorWithRangeContextOptions): ProcessorWithRangeContext;
62
+ }
63
+ interface ProcessorWithOffsetContext extends ProcessorContext {
64
+ offset: number;
65
+ }
66
+ interface ProcessorWithOffsetContextOptions extends ProcessorContextOptions {
67
+ offset: number;
68
+ }
69
+ declare namespace ProcessorWithOffsetContext {
70
+ function create(project: ProjectData, opts: ProcessorWithOffsetContextOptions): ProcessorWithOffsetContext;
71
+ }
72
+ export interface CheckerContext extends ProcessorContext {
73
+ err: ErrorReporter;
74
+ ops: Operations;
75
+ ensureChecked: (this: void, uri: string) => Promise<unknown>;
76
+ }
77
+ interface CheckerContextOptions extends ProcessorContextOptions {
78
+ err?: ErrorReporter;
79
+ ops?: Operations;
80
+ }
81
+ export declare namespace CheckerContext {
82
+ function create(project: ProjectData, opts: CheckerContextOptions): CheckerContext;
83
+ }
84
+ export interface LinterContext extends ProcessorContext {
85
+ err: LinterErrorReporter;
86
+ ruleName: string;
87
+ ruleValue: unknown;
88
+ }
89
+ interface LinterContextOptions extends ProcessorContextOptions {
90
+ err: LinterErrorReporter;
91
+ ruleName: string;
92
+ ruleValue: unknown;
93
+ }
94
+ export declare namespace LinterContext {
95
+ function create(project: ProjectData, opts: LinterContextOptions): LinterContext;
96
+ }
97
+ export interface FormatterContext extends ProcessorContext {
98
+ tabSize: number;
99
+ insertSpaces: boolean;
100
+ indentLevel: number;
101
+ indent: (additionalLevels?: number) => string;
102
+ }
103
+ interface FormatterContextOptions extends ProcessorContextOptions {
104
+ tabSize: number;
105
+ insertSpaces: boolean;
106
+ }
107
+ export declare namespace FormatterContext {
108
+ function create(project: ProjectData, opts: FormatterContextOptions): FormatterContext;
109
+ }
110
+ export interface ColorizerContext extends ProcessorWithRangeContext {
111
+ }
112
+ export interface ColorizerContextOptions extends ProcessorWithRangeContextOptions {
113
+ }
114
+ export declare namespace ColorizerContext {
115
+ function create(project: ProjectData, opts: ColorizerContextOptions): ColorizerContext;
116
+ }
117
+ export interface CompleterContext extends ProcessorContext {
118
+ offset: number;
119
+ triggerCharacter?: string;
120
+ }
121
+ interface CompleterContextOptions extends ProcessorContextOptions {
122
+ offset: number;
123
+ triggerCharacter?: string;
124
+ }
125
+ export declare namespace CompleterContext {
126
+ function create(project: ProjectData, opts: CompleterContextOptions): CompleterContext;
127
+ }
128
+ export interface SignatureHelpProviderContext extends ProcessorWithOffsetContext {
129
+ }
130
+ export interface SignatureHelpProviderContextOptions extends ProcessorWithOffsetContextOptions {
131
+ }
132
+ export declare namespace SignatureHelpProviderContext {
133
+ function create(project: ProjectData, opts: SignatureHelpProviderContextOptions): SignatureHelpProviderContext;
134
+ }
135
+ export interface UriBinderContext extends ContextBase {
136
+ /** @deprecated */
137
+ symbols: SymbolUtil;
138
+ }
139
+ export declare namespace UriBinderContext {
140
+ function create(project: ProjectData): UriBinderContext;
141
+ }
142
+ export {};
143
+ //# sourceMappingURL=Context.d.ts.map
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ /* istanbul ignore file */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.UriBinderContext = exports.SignatureHelpProviderContext = exports.CompleterContext = exports.ColorizerContext = exports.FormatterContext = exports.LinterContext = exports.CheckerContext = exports.ProcessorContext = exports.ParserContext = exports.ContextBase = void 0;
5
+ const processor_1 = require("../processor");
6
+ const source_1 = require("../source");
7
+ const ErrorReporter_1 = require("./ErrorReporter");
8
+ const Operations_1 = require("./Operations");
9
+ var ContextBase;
10
+ (function (ContextBase) {
11
+ function create(project) {
12
+ return {
13
+ fs: project.fs,
14
+ getDocAndNode: project.get.bind(project),
15
+ global: project.symbols.global,
16
+ logger: project.logger,
17
+ meta: project.meta,
18
+ roots: project.roots,
19
+ project: project.ctx,
20
+ };
21
+ }
22
+ ContextBase.create = create;
23
+ })(ContextBase = exports.ContextBase || (exports.ContextBase = {}));
24
+ var ParserContext;
25
+ (function (ParserContext) {
26
+ function create(project, opts) {
27
+ return {
28
+ ...ContextBase.create(project),
29
+ config: project.config,
30
+ doc: opts.doc,
31
+ err: opts.err ?? new ErrorReporter_1.ErrorReporter(),
32
+ symbols: project.symbols,
33
+ };
34
+ }
35
+ ParserContext.create = create;
36
+ })(ParserContext = exports.ParserContext || (exports.ParserContext = {}));
37
+ var ProcessorContext;
38
+ (function (ProcessorContext) {
39
+ function create(project, opts) {
40
+ return {
41
+ ...ContextBase.create(project),
42
+ config: project.config,
43
+ doc: opts.doc,
44
+ src: opts.src ?? new source_1.ReadonlySource(opts.doc.getText()),
45
+ symbols: project.symbols,
46
+ };
47
+ }
48
+ ProcessorContext.create = create;
49
+ })(ProcessorContext = exports.ProcessorContext || (exports.ProcessorContext = {}));
50
+ var ProcessorWithRangeContext;
51
+ (function (ProcessorWithRangeContext) {
52
+ function create(project, opts) {
53
+ return {
54
+ ...ProcessorContext.create(project, opts),
55
+ range: opts.range,
56
+ };
57
+ }
58
+ ProcessorWithRangeContext.create = create;
59
+ })(ProcessorWithRangeContext || (ProcessorWithRangeContext = {}));
60
+ var ProcessorWithOffsetContext;
61
+ (function (ProcessorWithOffsetContext) {
62
+ function create(project, opts) {
63
+ return {
64
+ ...ProcessorContext.create(project, opts),
65
+ offset: opts.offset,
66
+ };
67
+ }
68
+ ProcessorWithOffsetContext.create = create;
69
+ })(ProcessorWithOffsetContext || (ProcessorWithOffsetContext = {}));
70
+ var CheckerContext;
71
+ (function (CheckerContext) {
72
+ function create(project, opts) {
73
+ return {
74
+ ...ProcessorContext.create(project, opts),
75
+ err: opts.err ?? new ErrorReporter_1.ErrorReporter(),
76
+ ops: opts.ops ?? new Operations_1.Operations(),
77
+ ensureChecked: project.ensureParsedAndChecked?.bind(project),
78
+ };
79
+ }
80
+ CheckerContext.create = create;
81
+ })(CheckerContext = exports.CheckerContext || (exports.CheckerContext = {}));
82
+ var LinterContext;
83
+ (function (LinterContext) {
84
+ function create(project, opts) {
85
+ return {
86
+ ...ProcessorContext.create(project, opts),
87
+ err: opts.err,
88
+ ruleName: opts.ruleName,
89
+ ruleValue: opts.ruleValue,
90
+ };
91
+ }
92
+ LinterContext.create = create;
93
+ })(LinterContext = exports.LinterContext || (exports.LinterContext = {}));
94
+ var FormatterContext;
95
+ (function (FormatterContext) {
96
+ function create(project, opts) {
97
+ return {
98
+ ...ProcessorContext.create(project, opts),
99
+ ...opts,
100
+ indentLevel: 0,
101
+ indent(additionalLevels) {
102
+ return (0, processor_1.formatterContextIndentation)(this, additionalLevels);
103
+ },
104
+ };
105
+ }
106
+ FormatterContext.create = create;
107
+ })(FormatterContext = exports.FormatterContext || (exports.FormatterContext = {}));
108
+ var ColorizerContext;
109
+ (function (ColorizerContext) {
110
+ function create(project, opts) {
111
+ return ProcessorWithRangeContext.create(project, opts);
112
+ }
113
+ ColorizerContext.create = create;
114
+ })(ColorizerContext = exports.ColorizerContext || (exports.ColorizerContext = {}));
115
+ var CompleterContext;
116
+ (function (CompleterContext) {
117
+ function create(project, opts) {
118
+ return {
119
+ ...ProcessorContext.create(project, opts),
120
+ offset: opts.offset,
121
+ triggerCharacter: opts.triggerCharacter,
122
+ };
123
+ }
124
+ CompleterContext.create = create;
125
+ })(CompleterContext = exports.CompleterContext || (exports.CompleterContext = {}));
126
+ var SignatureHelpProviderContext;
127
+ (function (SignatureHelpProviderContext) {
128
+ function create(project, opts) {
129
+ return ProcessorWithOffsetContext.create(project, opts);
130
+ }
131
+ SignatureHelpProviderContext.create = create;
132
+ })(SignatureHelpProviderContext = exports.SignatureHelpProviderContext || (exports.SignatureHelpProviderContext = {}));
133
+ var UriBinderContext;
134
+ (function (UriBinderContext) {
135
+ function create(project) {
136
+ return {
137
+ ...ContextBase.create(project),
138
+ symbols: project.symbols,
139
+ };
140
+ }
141
+ UriBinderContext.create = create;
142
+ })(UriBinderContext = exports.UriBinderContext || (exports.UriBinderContext = {}));
143
+ //# sourceMappingURL=Context.js.map
@@ -0,0 +1,10 @@
1
+ export declare type Dependency = {
2
+ uri: string;
3
+ info?: Record<string, any>;
4
+ };
5
+ export declare type DependencyKey = `@${string}`;
6
+ export declare namespace DependencyKey {
7
+ function is(value: string): value is DependencyKey;
8
+ }
9
+ export declare type DependencyProvider = () => PromiseLike<Dependency> | Dependency;
10
+ //# sourceMappingURL=Dependency.d.ts.map
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DependencyKey = void 0;
4
+ var DependencyKey;
5
+ (function (DependencyKey) {
6
+ function is(value) {
7
+ return value.startsWith('@');
8
+ }
9
+ DependencyKey.is = is;
10
+ })(DependencyKey = exports.DependencyKey || (exports.DependencyKey = {}));
11
+ //# sourceMappingURL=Dependency.js.map
@@ -0,0 +1,67 @@
1
+ /// <reference types="node" />
2
+ import type { Logger } from './Logger';
3
+ declare type RemoteProtocol = 'http:' | 'https:';
4
+ export declare type RemoteUriString = `${RemoteProtocol}${string}`;
5
+ export declare namespace RemoteUriString {
6
+ function getProtocol(uri: RemoteUriString): RemoteProtocol;
7
+ }
8
+ export interface DownloaderDownloadOut {
9
+ cachePath?: string;
10
+ checksum?: string;
11
+ }
12
+ export declare class Downloader {
13
+ #private;
14
+ private readonly cacheRoot;
15
+ private readonly logger;
16
+ private readonly lld;
17
+ constructor(cacheRoot: string, logger: Logger, lld?: LowLevelDownloader);
18
+ download<R>(job: Job<R>, out?: DownloaderDownloadOut): Promise<R | undefined>;
19
+ }
20
+ interface Job<R> {
21
+ /**
22
+ * A unique ID for the cache.
23
+ *
24
+ * It also determines where the file is cached. Use slashes (`/`) to create directories.
25
+ */
26
+ id: string;
27
+ uri: RemoteUriString;
28
+ cache?: {
29
+ /**
30
+ * A download {@link Job} that will return a checksum of the latest remote data.
31
+ */
32
+ checksumJob: Omit<Job<string>, 'cache' | 'id'>;
33
+ checksumExtension: `.${string}`;
34
+ serializer?: (data: Buffer) => Buffer;
35
+ deserializer?: (cache: Buffer) => Buffer;
36
+ };
37
+ transformer: (data: Buffer) => PromiseLike<R> | R;
38
+ options?: LowLevelDownloadOptions;
39
+ ttl?: number;
40
+ }
41
+ interface LowLevelDownloadOptions {
42
+ /**
43
+ * Use an string array to set multiple values to the header.
44
+ */
45
+ headers?: Record<string, string | string[]>;
46
+ timeout?: number;
47
+ }
48
+ export interface LowLevelDownloader {
49
+ /**
50
+ * @throws
51
+ */
52
+ get(uri: RemoteUriString, options?: LowLevelDownloadOptions): Promise<Buffer>;
53
+ }
54
+ export declare namespace LowLevelDownloader {
55
+ function create(): LowLevelDownloader;
56
+ function mock(options: LowLevelDownloaderMockOptions): LowLevelDownloader;
57
+ }
58
+ interface LowLevelDownloaderMockOptions {
59
+ /**
60
+ * A record from URIs to fixture data. The {@link LowLevelDownloader.get} only returns a {@link Buffer},
61
+ * therefore `string` fixtures will be turned into a `Buffer` and `object` fixtures will be transformed
62
+ * into JSON and then turned into a `Buffer`.
63
+ */
64
+ fixtures: Record<RemoteUriString, string | Buffer | object>;
65
+ }
66
+ export {};
67
+ //# sourceMappingURL=Downloader.d.ts.map
@@ -0,0 +1,185 @@
1
+ "use strict";
2
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
+ };
7
+ var __importDefault = (this && this.__importDefault) || function (mod) {
8
+ return (mod && mod.__esModule) ? mod : { "default": mod };
9
+ };
10
+ var _Downloader_memoryCache;
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.LowLevelDownloader = exports.Downloader = exports.RemoteUriString = void 0;
13
+ const follow_redirects_1 = require("follow-redirects");
14
+ const fs_1 = require("fs");
15
+ const path_1 = __importDefault(require("path"));
16
+ const perf_hooks_1 = require("perf_hooks");
17
+ const common_1 = require("../common");
18
+ const fileUtil_1 = require("./fileUtil");
19
+ var RemoteUriString;
20
+ (function (RemoteUriString) {
21
+ function getProtocol(uri) {
22
+ return uri.slice(0, uri.indexOf(':') + 1);
23
+ }
24
+ RemoteUriString.getProtocol = getProtocol;
25
+ })(RemoteUriString = exports.RemoteUriString || (exports.RemoteUriString = {}));
26
+ class Downloader {
27
+ constructor(cacheRoot, logger, lld = LowLevelDownloader.create()) {
28
+ this.cacheRoot = cacheRoot;
29
+ this.logger = logger;
30
+ this.lld = lld;
31
+ _Downloader_memoryCache.set(this, new Map());
32
+ }
33
+ async download(job, out = {}) {
34
+ const { id, cache, uri, options, transformer, ttl } = job;
35
+ if (ttl && __classPrivateFieldGet(this, _Downloader_memoryCache, "f").has(uri)) {
36
+ const { buffer, time } = __classPrivateFieldGet(this, _Downloader_memoryCache, "f").get(uri);
37
+ if (time <= perf_hooks_1.performance.now() + ttl) {
38
+ this.logger.info(`[Downloader] [${id}] Skipped thanks to valid cache in memory`);
39
+ return await transformer(buffer);
40
+ }
41
+ else {
42
+ __classPrivateFieldGet(this, _Downloader_memoryCache, "f").delete(uri);
43
+ }
44
+ }
45
+ let checksum;
46
+ let cachePath;
47
+ let cacheChecksumPath;
48
+ if (cache) {
49
+ const { checksumJob, checksumExtension } = cache;
50
+ out.cachePath = cachePath = path_1.default.join(this.cacheRoot, 'downloader', id);
51
+ cacheChecksumPath = path_1.default.join(this.cacheRoot, 'downloader', id + checksumExtension);
52
+ try {
53
+ out.checksum = checksum = await this.download({ ...checksumJob, id: id + checksumExtension });
54
+ try {
55
+ const cacheChecksum = (0, common_1.bufferToString)(await fileUtil_1.fileUtil.readFile(fileUtil_1.fileUtil.pathToFileUri(cacheChecksumPath)))
56
+ .slice(0, -1); // Remove ending newline
57
+ if (checksum === cacheChecksum) {
58
+ try {
59
+ const cachedBuffer = await fileUtil_1.fileUtil.readFile(fileUtil_1.fileUtil.pathToFileUri(cachePath));
60
+ if (ttl) {
61
+ __classPrivateFieldGet(this, _Downloader_memoryCache, "f").set(uri, { buffer: cachedBuffer, time: perf_hooks_1.performance.now() });
62
+ }
63
+ const deserializer = cache.deserializer ?? (b => b);
64
+ const ans = await transformer(deserializer(cachedBuffer));
65
+ this.logger.info(`[Downloader] [${id}] Skipped downloading thanks to cache ${cacheChecksum}`);
66
+ return ans;
67
+ }
68
+ catch (e) {
69
+ this.logger.error(`[Downloader] [${id}] Loading cached file “${cachePath}”`, e);
70
+ if ((0, common_1.isEnoent)(e)) {
71
+ // Cache checksum exists, but cached file doesn't.
72
+ // Remove the invalid cache checksum.
73
+ try {
74
+ await fs_1.promises.unlink(cacheChecksumPath);
75
+ }
76
+ catch (e) {
77
+ this.logger.error(`[Downloader] [${id}] Removing invalid cache checksum “${cacheChecksumPath}”`, e);
78
+ }
79
+ }
80
+ }
81
+ }
82
+ }
83
+ catch (e) {
84
+ if (!(0, common_1.isEnoent)(e)) {
85
+ this.logger.error(`[Downloader] [${id}] Loading cache checksum “${cacheChecksumPath}”`, e);
86
+ }
87
+ }
88
+ }
89
+ catch (e) {
90
+ this.logger.error(`[Downloader] [${id}] Fetching latest checksum “${checksumJob.uri}”`, e);
91
+ }
92
+ }
93
+ try {
94
+ const buffer = await this.lld.get(uri, options);
95
+ if (ttl) {
96
+ __classPrivateFieldGet(this, _Downloader_memoryCache, "f").set(uri, { buffer, time: perf_hooks_1.performance.now() });
97
+ }
98
+ if (cache && cachePath && cacheChecksumPath) {
99
+ if (checksum) {
100
+ try {
101
+ await fileUtil_1.fileUtil.writeFile(fileUtil_1.fileUtil.pathToFileUri(cacheChecksumPath), `${checksum}\n`);
102
+ }
103
+ catch (e) {
104
+ this.logger.error(`[Downloader] [${id}] Saving cache checksum “${cacheChecksumPath}”`, e);
105
+ }
106
+ }
107
+ try {
108
+ const serializer = cache.serializer ?? (b => b);
109
+ await fileUtil_1.fileUtil.writeFile(fileUtil_1.fileUtil.pathToFileUri(cachePath), serializer(buffer));
110
+ }
111
+ catch (e) {
112
+ this.logger.error(`[Downloader] [${id}] Caching file “${cachePath}”`, e);
113
+ }
114
+ }
115
+ this.logger.info(`[Downloader] [${id}] Downloaded from “${uri}”`);
116
+ return await transformer(buffer);
117
+ }
118
+ catch (e) {
119
+ this.logger.error(`[Downloader] [${id}] Downloading “${uri}”`, e);
120
+ if (cache && cachePath) {
121
+ try {
122
+ const cachedBuffer = await fileUtil_1.fileUtil.readFile(fileUtil_1.fileUtil.pathToFileUri(cachePath));
123
+ const deserializer = cache.deserializer ?? (b => b);
124
+ const ans = await transformer(deserializer(cachedBuffer));
125
+ this.logger.warn(`[Downloader] [${id}] Fell back to cached file “${cachePath}”`);
126
+ return ans;
127
+ }
128
+ catch (e) {
129
+ this.logger.error(`[Downloader] [${id}] Fallback: loading cached file “${cachePath}”`, e);
130
+ }
131
+ }
132
+ }
133
+ return undefined;
134
+ }
135
+ }
136
+ exports.Downloader = Downloader;
137
+ _Downloader_memoryCache = new WeakMap();
138
+ var LowLevelDownloader;
139
+ (function (LowLevelDownloader) {
140
+ function create() {
141
+ return new LowLevelDownloaderImpl();
142
+ }
143
+ LowLevelDownloader.create = create;
144
+ function mock(options) {
145
+ return new LowLevelDownloaderMock(options);
146
+ }
147
+ LowLevelDownloader.mock = mock;
148
+ })(LowLevelDownloader = exports.LowLevelDownloader || (exports.LowLevelDownloader = {}));
149
+ class LowLevelDownloaderImpl {
150
+ get(uri, options = {}) {
151
+ const protocol = RemoteUriString.getProtocol(uri);
152
+ return new Promise((resolve, reject) => {
153
+ const backend = protocol === 'http:' ? follow_redirects_1.http : follow_redirects_1.https;
154
+ backend.get(uri, options, (res) => {
155
+ if (res.statusCode !== 200) {
156
+ reject(new Error(`Status code ${res.statusCode}: ${res.statusMessage}`));
157
+ }
158
+ else {
159
+ resolve((0, common_1.promisifyAsyncIterable)(res, chunks => Buffer.concat(chunks)));
160
+ }
161
+ });
162
+ });
163
+ }
164
+ }
165
+ class LowLevelDownloaderMock {
166
+ constructor(options) {
167
+ this.options = options;
168
+ }
169
+ async get(uri) {
170
+ if (!this.options.fixtures[uri]) {
171
+ throw new Error(`404 not found: ${uri}`);
172
+ }
173
+ const fixture = this.options.fixtures[uri];
174
+ if (Buffer.isBuffer(fixture)) {
175
+ return fixture;
176
+ }
177
+ else if (typeof fixture === 'string') {
178
+ return Buffer.from(fixture, 'utf-8');
179
+ }
180
+ else {
181
+ return Buffer.from(JSON.stringify(fixture), 'utf-8');
182
+ }
183
+ }
184
+ }
185
+ //# sourceMappingURL=Downloader.js.map
@@ -0,0 +1,27 @@
1
+ import type { LanguageErrorInfo, RangeLike } from '../source';
2
+ import { ErrorSeverity, LanguageError } from '../source';
3
+ export declare class ErrorReporter {
4
+ errors: LanguageError[];
5
+ constructor();
6
+ /**
7
+ * Reports a new error.
8
+ */
9
+ report(message: string, range: RangeLike, severity?: ErrorSeverity, info?: LanguageErrorInfo): void;
10
+ /**
11
+ * @returns All reported errors, and then clears the error stack.
12
+ */
13
+ dump(): readonly LanguageError[];
14
+ /**
15
+ * Adds all errors from another reporter's error stack to the current reporter.
16
+ * This method does not affect the passed-in reporter.
17
+ */
18
+ absorb(reporter: ErrorReporter): void;
19
+ }
20
+ export declare class LinterErrorReporter extends ErrorReporter {
21
+ ruleName: string;
22
+ ruleSeverity: ErrorSeverity;
23
+ constructor(ruleName: string, ruleSeverity: ErrorSeverity);
24
+ lint(message: string, range: RangeLike, info?: LanguageErrorInfo, severityOverride?: ErrorSeverity): void;
25
+ static fromErrorReporter(reporter: ErrorReporter, ruleName: string, ruleSeverity: ErrorSeverity): LinterErrorReporter;
26
+ }
27
+ //# sourceMappingURL=ErrorReporter.d.ts.map
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LinterErrorReporter = exports.ErrorReporter = void 0;
4
+ const locales_1 = require("@spyglassmc/locales");
5
+ const source_1 = require("../source");
6
+ class ErrorReporter {
7
+ constructor() {
8
+ this.errors = [];
9
+ }
10
+ /**
11
+ * Reports a new error.
12
+ */
13
+ report(message, range, severity = 3 /* Error */, info) {
14
+ this.errors.push(source_1.LanguageError.create(message, source_1.Range.get(range), severity, info));
15
+ }
16
+ /**
17
+ * @returns All reported errors, and then clears the error stack.
18
+ */
19
+ dump() {
20
+ const ans = Object.freeze(this.errors);
21
+ this.errors = [];
22
+ return ans;
23
+ }
24
+ /**
25
+ * Adds all errors from another reporter's error stack to the current reporter.
26
+ * This method does not affect the passed-in reporter.
27
+ */
28
+ absorb(reporter) {
29
+ this.errors.push(...reporter.errors);
30
+ }
31
+ }
32
+ exports.ErrorReporter = ErrorReporter;
33
+ class LinterErrorReporter extends ErrorReporter {
34
+ constructor(ruleName, ruleSeverity) {
35
+ super();
36
+ this.ruleName = ruleName;
37
+ this.ruleSeverity = ruleSeverity;
38
+ }
39
+ lint(message, range, info, severityOverride) {
40
+ return this.report((0, locales_1.localize)('linter.diagnostic-message-wrapper', message, this.ruleName), range, severityOverride ?? this.ruleSeverity, info);
41
+ }
42
+ static fromErrorReporter(reporter, ruleName, ruleSeverity) {
43
+ const ans = new LinterErrorReporter(ruleName, ruleSeverity);
44
+ ans.errors = reporter.errors;
45
+ return ans;
46
+ }
47
+ }
48
+ exports.LinterErrorReporter = LinterErrorReporter;
49
+ //# sourceMappingURL=ErrorReporter.js.map