@spyglassmc/core 0.1.2 → 0.4.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 (205) hide show
  1. package/README.md +7 -0
  2. package/lib/browser.d.ts +2 -0
  3. package/lib/browser.js +2 -0
  4. package/lib/common/Dev.d.ts +11 -0
  5. package/lib/common/Dev.js +90 -0
  6. package/lib/{service → common}/Logger.d.ts +0 -0
  7. package/lib/{service → common}/Logger.js +2 -5
  8. package/lib/common/Operations.d.ts +12 -0
  9. package/lib/common/Operations.js +33 -0
  10. package/lib/common/ReadonlyProxy.d.ts +9 -0
  11. package/lib/common/ReadonlyProxy.js +23 -0
  12. package/lib/common/StateProxy.d.ts +35 -0
  13. package/lib/common/StateProxy.js +69 -0
  14. package/lib/common/TwoWayMap.js +4 -10
  15. package/lib/common/externals/BrowserExternals.d.ts +3 -0
  16. package/lib/common/externals/BrowserExternals.js +191 -0
  17. package/lib/common/externals/NodeJsExternals.d.ts +3 -0
  18. package/lib/common/externals/NodeJsExternals.js +152 -0
  19. package/lib/common/externals/downloader.d.ts +31 -0
  20. package/lib/common/externals/downloader.js +32 -0
  21. package/lib/common/externals/index.d.ts +96 -0
  22. package/lib/common/externals/index.js +2 -0
  23. package/lib/common/index.d.ts +8 -1
  24. package/lib/common/index.js +8 -17
  25. package/lib/common/util.d.ts +48 -20
  26. package/lib/common/util.js +71 -86
  27. package/lib/index.d.ts +7 -7
  28. package/lib/index.js +7 -23
  29. package/lib/node/AstNode.d.ts +12 -11
  30. package/lib/node/AstNode.js +10 -16
  31. package/lib/node/BooleanNode.d.ts +2 -2
  32. package/lib/node/BooleanNode.js +4 -7
  33. package/lib/node/CommentNode.d.ts +6 -5
  34. package/lib/node/CommentNode.js +4 -9
  35. package/lib/node/ErrorNode.d.ts +1 -1
  36. package/lib/node/ErrorNode.js +2 -5
  37. package/lib/node/FileNode.d.ts +8 -4
  38. package/lib/node/FileNode.js +3 -6
  39. package/lib/node/FloatNode.d.ts +3 -3
  40. package/lib/node/FloatNode.js +4 -7
  41. package/lib/node/IntegerNode.d.ts +3 -3
  42. package/lib/node/IntegerNode.js +4 -7
  43. package/lib/node/ListNode.d.ts +2 -2
  44. package/lib/node/ListNode.js +2 -5
  45. package/lib/node/LiteralNode.d.ts +4 -4
  46. package/lib/node/LiteralNode.js +4 -7
  47. package/lib/node/LongNode.d.ts +3 -3
  48. package/lib/node/LongNode.js +4 -7
  49. package/lib/node/RecordNode.d.ts +2 -2
  50. package/lib/node/RecordNode.js +2 -5
  51. package/lib/node/ResourceLocationNode.d.ts +7 -7
  52. package/lib/node/ResourceLocationNode.js +9 -12
  53. package/lib/node/Sequence.d.ts +2 -2
  54. package/lib/node/Sequence.js +4 -7
  55. package/lib/node/StringNode.d.ts +5 -5
  56. package/lib/node/StringNode.js +9 -12
  57. package/lib/node/SymbolNode.d.ts +4 -4
  58. package/lib/node/SymbolNode.js +4 -7
  59. package/lib/node/index.d.ts +15 -15
  60. package/lib/node/index.js +15 -31
  61. package/lib/nodejs.d.ts +2 -0
  62. package/lib/nodejs.js +2 -0
  63. package/lib/parser/Parser.d.ts +3 -3
  64. package/lib/parser/Parser.js +1 -4
  65. package/lib/parser/boolean.d.ts +2 -2
  66. package/lib/parser/boolean.js +3 -6
  67. package/lib/parser/comment.d.ts +2 -2
  68. package/lib/parser/comment.js +5 -9
  69. package/lib/parser/empty.d.ts +1 -1
  70. package/lib/parser/empty.js +1 -5
  71. package/lib/parser/error.d.ts +2 -2
  72. package/lib/parser/error.js +5 -9
  73. package/lib/parser/file.d.ts +3 -3
  74. package/lib/parser/file.js +9 -13
  75. package/lib/parser/float.d.ts +4 -4
  76. package/lib/parser/float.js +12 -16
  77. package/lib/parser/index.d.ts +16 -16
  78. package/lib/parser/index.js +16 -38
  79. package/lib/parser/integer.d.ts +4 -4
  80. package/lib/parser/integer.js +10 -14
  81. package/lib/parser/list.d.ts +2 -2
  82. package/lib/parser/list.js +15 -19
  83. package/lib/parser/literal.d.ts +2 -2
  84. package/lib/parser/literal.js +5 -9
  85. package/lib/parser/long.d.ts +4 -4
  86. package/lib/parser/long.js +10 -14
  87. package/lib/parser/record.d.ts +3 -3
  88. package/lib/parser/record.js +20 -24
  89. package/lib/parser/resourceLocation.d.ts +2 -2
  90. package/lib/parser/resourceLocation.js +12 -24
  91. package/lib/parser/string.d.ts +7 -7
  92. package/lib/parser/string.js +36 -42
  93. package/lib/parser/symbol.d.ts +3 -3
  94. package/lib/parser/symbol.js +4 -14
  95. package/lib/parser/util.d.ts +6 -5
  96. package/lib/parser/util.js +40 -60
  97. package/lib/processor/ColorInfoProvider.d.ts +1 -1
  98. package/lib/processor/ColorInfoProvider.js +6 -9
  99. package/lib/processor/InlayHintProvider.d.ts +4 -3
  100. package/lib/processor/InlayHintProvider.js +1 -2
  101. package/lib/processor/SignatureHelpProvider.d.ts +4 -3
  102. package/lib/processor/SignatureHelpProvider.js +1 -2
  103. package/lib/processor/binder/Binder.d.ts +26 -0
  104. package/lib/processor/binder/Binder.js +18 -0
  105. package/lib/processor/binder/builtin.d.ts +27 -0
  106. package/lib/processor/binder/builtin.js +116 -0
  107. package/lib/processor/binder/index.d.ts +3 -0
  108. package/lib/processor/binder/index.js +3 -0
  109. package/lib/processor/checker/Checker.d.ts +2 -2
  110. package/lib/processor/checker/Checker.js +1 -5
  111. package/lib/processor/checker/builtin.d.ts +4 -4
  112. package/lib/processor/checker/builtin.js +22 -33
  113. package/lib/processor/checker/index.d.ts +2 -2
  114. package/lib/processor/checker/index.js +2 -31
  115. package/lib/processor/colorizer/Colorizer.d.ts +6 -5
  116. package/lib/processor/colorizer/Colorizer.js +8 -11
  117. package/lib/processor/colorizer/builtin.d.ts +3 -3
  118. package/lib/processor/colorizer/builtin.js +34 -46
  119. package/lib/processor/colorizer/index.d.ts +2 -2
  120. package/lib/processor/colorizer/index.js +2 -31
  121. package/lib/processor/completer/Completer.d.ts +6 -5
  122. package/lib/processor/completer/Completer.js +14 -33
  123. package/lib/processor/completer/builtin.d.ts +10 -9
  124. package/lib/processor/completer/builtin.js +47 -63
  125. package/lib/processor/completer/index.d.ts +2 -2
  126. package/lib/processor/completer/index.js +2 -31
  127. package/lib/processor/formatter/Formatter.d.ts +4 -3
  128. package/lib/processor/formatter/Formatter.js +2 -7
  129. package/lib/processor/formatter/builtin.d.ts +3 -3
  130. package/lib/processor/formatter/builtin.js +22 -36
  131. package/lib/processor/formatter/index.d.ts +2 -2
  132. package/lib/processor/formatter/index.js +2 -31
  133. package/lib/processor/index.d.ts +10 -9
  134. package/lib/processor/index.js +10 -25
  135. package/lib/processor/linter/Linter.d.ts +4 -3
  136. package/lib/processor/linter/Linter.js +1 -2
  137. package/lib/processor/linter/builtin/undeclaredSymbol.d.ts +2 -2
  138. package/lib/processor/linter/builtin/undeclaredSymbol.js +20 -24
  139. package/lib/processor/linter/builtin.d.ts +4 -3
  140. package/lib/processor/linter/builtin.js +19 -26
  141. package/lib/processor/linter/index.d.ts +2 -2
  142. package/lib/processor/linter/index.js +2 -31
  143. package/lib/processor/util.d.ts +4 -14
  144. package/lib/processor/util.js +1 -16
  145. package/lib/service/CacheService.d.ts +13 -8
  146. package/lib/service/CacheService.js +45 -57
  147. package/lib/service/Config.d.ts +11 -12
  148. package/lib/service/Config.js +50 -45
  149. package/lib/service/Context.d.ts +23 -20
  150. package/lib/service/Context.js +39 -36
  151. package/lib/service/Dependency.js +2 -5
  152. package/lib/service/Downloader.d.ts +9 -40
  153. package/lib/service/Downloader.js +37 -110
  154. package/lib/service/ErrorReporter.d.ts +2 -2
  155. package/lib/service/ErrorReporter.js +10 -14
  156. package/lib/service/FileService.d.ts +15 -14
  157. package/lib/service/FileService.js +55 -92
  158. package/lib/service/Hover.d.ts +2 -2
  159. package/lib/service/Hover.js +4 -7
  160. package/lib/service/MetaRegistry.d.ts +22 -12
  161. package/lib/service/MetaRegistry.js +81 -73
  162. package/lib/service/Profiler.d.ts +3 -2
  163. package/lib/service/Profiler.js +81 -45
  164. package/lib/service/Project.d.ts +105 -76
  165. package/lib/service/Project.js +449 -370
  166. package/lib/service/Service.d.ts +17 -27
  167. package/lib/service/Service.js +37 -43
  168. package/lib/service/SymbolLocations.d.ts +3 -3
  169. package/lib/service/SymbolLocations.js +4 -7
  170. package/lib/service/SymbolRegistrar.d.ts +1 -1
  171. package/lib/service/SymbolRegistrar.js +1 -2
  172. package/lib/service/UriProcessor.d.ts +5 -0
  173. package/lib/service/UriProcessor.js +2 -0
  174. package/lib/service/fileUtil.d.ts +15 -45
  175. package/lib/service/fileUtil.js +38 -143
  176. package/lib/service/index.d.ts +16 -17
  177. package/lib/service/index.js +16 -35
  178. package/lib/source/IndexMap.d.ts +1 -1
  179. package/lib/source/IndexMap.js +7 -10
  180. package/lib/source/LanguageError.d.ts +20 -6
  181. package/lib/source/LanguageError.js +16 -9
  182. package/lib/source/Location.d.ts +3 -3
  183. package/lib/source/Location.js +6 -9
  184. package/lib/source/Offset.d.ts +1 -1
  185. package/lib/source/Offset.js +4 -7
  186. package/lib/source/Position.js +2 -5
  187. package/lib/source/PositionRange.d.ts +3 -3
  188. package/lib/source/PositionRange.js +17 -20
  189. package/lib/source/Range.d.ts +1 -1
  190. package/lib/source/Range.js +7 -10
  191. package/lib/source/Source.d.ts +5 -3
  192. package/lib/source/Source.js +34 -29
  193. package/lib/source/index.d.ts +8 -8
  194. package/lib/source/index.js +8 -24
  195. package/lib/symbol/Symbol.d.ts +5 -4
  196. package/lib/symbol/Symbol.js +49 -65
  197. package/lib/symbol/SymbolUtil.d.ts +29 -31
  198. package/lib/symbol/SymbolUtil.js +178 -157
  199. package/lib/symbol/index.d.ts +2 -3
  200. package/lib/symbol/index.js +2 -19
  201. package/package.json +7 -4
  202. package/lib/service/Operations.d.ts +0 -8
  203. package/lib/service/Operations.js +0 -26
  204. package/lib/symbol/UriBinder.d.ts +0 -3
  205. package/lib/symbol/UriBinder.js +0 -3
package/README.md CHANGED
@@ -8,3 +8,10 @@
8
8
  [![Gitmoji](https://img.shields.io/badge/gitmoji-%20😜%20😍-FFDD67.svg?style=flat-square)](https://gitmoji.carloscuesta.me/)
9
9
 
10
10
  This package is the core of the Spyglass Project. It defines the structure of a parser or validator, and provides the `SpyglassCore` class to help manage those things.
11
+
12
+ ## Exports
13
+
14
+ * `@spyglassmc/core`: Contains all core functionalities.
15
+ * `@spyglassmc/core/lib/browser.js`: Contains browser-specific features.
16
+ * `@spyglassmc/core/lib/nodejs.js`: Contains Node.js-specific features.
17
+ * `@spyglassmc/core/test-out/utils.js`: For internal development use only. Does not exist in the package published to npm.
@@ -0,0 +1,2 @@
1
+ export * from './common/externals/BrowserExternals.js';
2
+ //# sourceMappingURL=browser.d.ts.map
package/lib/browser.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './common/externals/BrowserExternals.js';
2
+ //# sourceMappingURL=browser.js.map
@@ -0,0 +1,11 @@
1
+ export declare const Dev: Readonly<{
2
+ assertDefined<T>(value: T): asserts value is Exclude<T, undefined>;
3
+ assertNever(value: never): never;
4
+ assertTrue(value: boolean, message: string): void;
5
+ /**
6
+ * @returns An estimate of the memory taken by the given value, assuming objects are stored as array-like structures instead of dictionaries in the V8 engine.
7
+ */
8
+ estimateMemoryUsage(value: unknown): number;
9
+ stringify(value: unknown): string;
10
+ }>;
11
+ //# sourceMappingURL=Dev.d.ts.map
@@ -0,0 +1,90 @@
1
+ export const Dev = Object.freeze({
2
+ assertDefined(value) {
3
+ if (value === undefined) {
4
+ throw new Error(`'${Dev.stringify(value)}' is 'undefined'`);
5
+ }
6
+ },
7
+ assertNever(value) {
8
+ throw new Error(`'${Dev.stringify(value)}' is not of type 'never'`);
9
+ },
10
+ assertTrue(value, message) {
11
+ if (!value) {
12
+ throw new Error(`Assertion failed: ${message}. '${Dev.stringify(value)}' should be true.`);
13
+ }
14
+ },
15
+ /**
16
+ * @returns An estimate of the memory taken by the given value, assuming objects are stored as array-like structures instead of dictionaries in the V8 engine.
17
+ */
18
+ estimateMemoryUsage(value) {
19
+ const ByteToBits = 8;
20
+ const PointerSize = 8;
21
+ let ans = 0;
22
+ const calculatedObjects = new Set();
23
+ const stack = [value];
24
+ while (stack.length) {
25
+ const current = stack.pop();
26
+ switch (typeof current) {
27
+ case 'bigint': {
28
+ const bits = Math.ceil(Math.log2(Number(current)));
29
+ ans += (2 + Math.ceil(bits / (ByteToBits * PointerSize))) * PointerSize; // https://stackoverflow.com/a/54298760
30
+ break;
31
+ }
32
+ case 'boolean':
33
+ ans += PointerSize; // Aggressive alignment estimation.
34
+ break;
35
+ case 'number':
36
+ ans += 8;
37
+ break;
38
+ case 'object':
39
+ ans += PointerSize; // Reference pointer.
40
+ if (!current || calculatedObjects.has(current)) {
41
+ break;
42
+ }
43
+ ans += PointerSize; // Header pointer.
44
+ for (const value of Object.values(current)) {
45
+ stack.push(value);
46
+ ans += PointerSize; // Aggressive padding estimation.
47
+ }
48
+ calculatedObjects.add(current);
49
+ break;
50
+ case 'string':
51
+ ans += current.length * 2;
52
+ break;
53
+ case 'symbol':
54
+ ans += PointerSize;
55
+ break;
56
+ default:
57
+ break;
58
+ }
59
+ }
60
+ return ans;
61
+ },
62
+ stringify(value) {
63
+ if (value && typeof value === 'object') {
64
+ try {
65
+ const seen = new Set();
66
+ return JSON.stringify(value, (_k, v) => {
67
+ if (v && typeof v === 'object') {
68
+ return seen.has(v) ? '[Circular]' : (seen.add(v), v);
69
+ }
70
+ else {
71
+ return v;
72
+ }
73
+ });
74
+ }
75
+ catch (ignored) {
76
+ // Most likely "Maximum callstack size exceeded".
77
+ // Fall back to a shallow string representation.
78
+ return `{ ${Object.entries(value).map(([k, v]) => `'${k}': '${String(v)}'`).join(', ')} }`;
79
+ }
80
+ }
81
+ else if (typeof value === 'symbol') {
82
+ // JavaScript does not convert `Symbol`s to strings implicitly.
83
+ return String(value);
84
+ }
85
+ else {
86
+ return `${value}`;
87
+ }
88
+ },
89
+ });
90
+ //# sourceMappingURL=Dev.js.map
File without changes
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Logger = void 0;
4
- var Logger;
1
+ export var Logger;
5
2
  (function (Logger) {
6
3
  /* istanbul ignore next */
7
4
  /**
@@ -20,7 +17,7 @@ var Logger;
20
17
  return new NoopLogger();
21
18
  }
22
19
  Logger.noop = noop;
23
- })(Logger = exports.Logger || (exports.Logger = {}));
20
+ })(Logger || (Logger = {}));
24
21
  class NoopLogger {
25
22
  error() { }
26
23
  info() { }
@@ -0,0 +1,12 @@
1
+ export declare class Operations {
2
+ private readonly parent?;
3
+ constructor(parent?: Operations | undefined);
4
+ private readonly undoOps;
5
+ private readonly redoOps;
6
+ private addUndoOp;
7
+ private addRedoOp;
8
+ set<O extends object, K extends keyof O>(obj: O, key: K, value: O[K], receiver?: any): void;
9
+ undo(): void;
10
+ redo(): void;
11
+ }
12
+ //# sourceMappingURL=Operations.d.ts.map
@@ -0,0 +1,33 @@
1
+ export class Operations {
2
+ parent;
3
+ constructor(parent) {
4
+ this.parent = parent;
5
+ }
6
+ undoOps = [];
7
+ redoOps = [];
8
+ addUndoOp(op) {
9
+ this.undoOps.push(op);
10
+ this.parent?.addUndoOp(op);
11
+ }
12
+ addRedoOp(op) {
13
+ this.redoOps.push(op);
14
+ this.parent?.addRedoOp(op);
15
+ }
16
+ set(obj, key, value, receiver = obj) {
17
+ const oldValue = Reflect.get(obj, key, receiver);
18
+ const op = () => { Reflect.set(obj, key, value, receiver); };
19
+ const undoOp = () => { Reflect.set(obj, key, oldValue, receiver); };
20
+ this.addRedoOp(op);
21
+ this.addUndoOp(undoOp);
22
+ op();
23
+ }
24
+ undo() {
25
+ for (let i = this.undoOps.length - 1; i >= 0; i--) {
26
+ this.undoOps[i]();
27
+ }
28
+ }
29
+ redo() {
30
+ this.redoOps.forEach(op => op());
31
+ }
32
+ }
33
+ //# sourceMappingURL=Operations.js.map
@@ -0,0 +1,9 @@
1
+ declare type Wrap<T> = T extends object ? DeepReadonly<T> : T;
2
+ export declare type DeepReadonly<T extends object> = {
3
+ readonly [K in keyof T]: Wrap<T[K]>;
4
+ };
5
+ export declare const ReadonlyProxy: Readonly<{
6
+ create<T extends object>(obj: T): DeepReadonly<T>;
7
+ }>;
8
+ export {};
9
+ //# sourceMappingURL=ReadonlyProxy.d.ts.map
@@ -0,0 +1,23 @@
1
+ import { emplaceMap, isObject } from './util.js';
2
+ export const ReadonlyProxy = Object.freeze({
3
+ create(obj) {
4
+ return new Proxy(obj, new ReadonlyProxyHandler());
5
+ },
6
+ });
7
+ class ReadonlyProxyHandler {
8
+ map = new Map();
9
+ get(target, p, receiver) {
10
+ const value = Reflect.get(target, p, receiver);
11
+ if (p !== 'prototype' && isObject(value)) {
12
+ return emplaceMap(this.map, p, { insert: () => ReadonlyProxy.create(value) });
13
+ }
14
+ return value;
15
+ }
16
+ set(_target, p, _value, _receiver) {
17
+ throw new TypeError(`Cannot set property '${String(p)}' on a readonly proxy`);
18
+ }
19
+ deleteProperty(_target, p) {
20
+ throw new TypeError(`Cannot delete property '${String(p)}' on a readonly proxy`);
21
+ }
22
+ }
23
+ //# sourceMappingURL=ReadonlyProxy.js.map
@@ -0,0 +1,35 @@
1
+ declare const BranchOff: unique symbol;
2
+ declare const Is: unique symbol;
3
+ declare const Origin: unique symbol;
4
+ declare const Redo: unique symbol;
5
+ declare const Undo: unique symbol;
6
+ declare type Wrap<T> = T extends object ? StateProxy<T> : T;
7
+ /**
8
+ * A proxy wrapped around an arbitrary object value.
9
+ * You can access and mutate the value as normal, but you also have the ability to revert all changes ever since the
10
+ * proxy was created using {@link StateProxy.redoChanges} and {@link StateProxy.undoChanges}.
11
+ *
12
+ * A new proxy can be branched off of an existing proxy using {@link StateProxy.branchOff} to have finer control
13
+ * over what changes to be reverted.
14
+ */
15
+ export declare type StateProxy<T extends object> = {
16
+ [K in keyof T]: Wrap<T[K]>;
17
+ } & {
18
+ [BranchOff]: () => StateProxy<T>;
19
+ [Is]: true;
20
+ [Origin]: T;
21
+ [Redo]: () => void;
22
+ [Undo]: () => void;
23
+ };
24
+ export declare const StateProxy: Readonly<{
25
+ branchOff<T extends object>(proxy: StateProxy<T>): StateProxy<T>;
26
+ create<T_1 extends object>(obj: T_1): T_1 extends StateProxy<any> ? void & {
27
+ _cannotCreateProxyFromProxy: never;
28
+ } : StateProxy<T_1>;
29
+ dereference<T_2 extends object>(value: T_2 | StateProxy<T_2>): T_2;
30
+ is(obj: any): obj is StateProxy<object>;
31
+ redoChanges(proxy: StateProxy<object>): void;
32
+ undoChanges(proxy: StateProxy<object>): void;
33
+ }>;
34
+ export {};
35
+ //# sourceMappingURL=StateProxy.d.ts.map
@@ -0,0 +1,69 @@
1
+ import { emplaceMap, isObject } from '../common/index.js';
2
+ import { Operations } from './Operations.js';
3
+ const BranchOff = Symbol('StateBranchOff');
4
+ const Is = Symbol('IsStateProxy');
5
+ const Origin = Symbol('OriginState');
6
+ const Redo = Symbol('RedoStateChanges');
7
+ const Undo = Symbol('UndoStateChanges');
8
+ export const StateProxy = Object.freeze({
9
+ branchOff(proxy) {
10
+ return proxy[BranchOff]();
11
+ },
12
+ create(obj) {
13
+ if (StateProxy.is(obj)) {
14
+ throw new TypeError('Cannot create a proxy over a proxy. You might want to use branchOff instead.');
15
+ }
16
+ return _createStateProxy(obj, new Operations());
17
+ },
18
+ dereference(value) {
19
+ return StateProxy.is(value) ? value[Origin] : value;
20
+ },
21
+ is(obj) {
22
+ return obj?.[Is];
23
+ },
24
+ redoChanges(proxy) {
25
+ proxy[Redo]();
26
+ },
27
+ undoChanges(proxy) {
28
+ proxy[Undo]();
29
+ },
30
+ });
31
+ class StateProxyHandler {
32
+ rootOps;
33
+ map = new Map();
34
+ constructor(
35
+ /**
36
+ * Shared across all handlers created for the same root state object under the same branch.
37
+ */
38
+ rootOps) {
39
+ this.rootOps = rootOps;
40
+ }
41
+ #branchOff(target) {
42
+ return _createStateProxy(target, new Operations(this.rootOps));
43
+ }
44
+ get(target, p, receiver) {
45
+ switch (p) {
46
+ case BranchOff: return () => this.#branchOff(target);
47
+ case Is: return true;
48
+ case Origin: return target;
49
+ case Redo: return () => this.rootOps.redo();
50
+ case Undo: return () => this.rootOps.undo();
51
+ }
52
+ const value = Reflect.get(target, p, receiver);
53
+ if (p !== 'prototype' && isObject(value)) {
54
+ return emplaceMap(this.map, p, { insert: () => _createStateProxy(value, this.rootOps) });
55
+ }
56
+ return value;
57
+ }
58
+ set(target, p, value, receiver) {
59
+ if (p === BranchOff || p === Is || p === Origin || p === Redo || p === Undo) {
60
+ throw new TypeError(`Cannot set ${String(p)}`);
61
+ }
62
+ this.rootOps.set(target, p, StateProxy.dereference(value), receiver);
63
+ return true;
64
+ }
65
+ }
66
+ function _createStateProxy(target, operations) {
67
+ return new Proxy(target, new StateProxyHandler(operations));
68
+ }
69
+ //# sourceMappingURL=StateProxy.js.map
@@ -1,12 +1,7 @@
1
- "use strict";
2
- var _a;
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.TwoWayMap = void 0;
5
- class TwoWayMap {
1
+ export class TwoWayMap {
2
+ _map = new Map();
3
+ _reversedMap = new Map();
6
4
  constructor(values = []) {
7
- this._map = new Map();
8
- this._reversedMap = new Map();
9
- this[_a] = 'TwoWayMap';
10
5
  for (const [k, v] of values) {
11
6
  this._map.set(k, v);
12
7
  this._reversedMap.set(v, k);
@@ -69,7 +64,6 @@ class TwoWayMap {
69
64
  [Symbol.iterator]() {
70
65
  return this._map.entries();
71
66
  }
67
+ [Symbol.toStringTag] = 'TwoWayMap';
72
68
  }
73
- exports.TwoWayMap = TwoWayMap;
74
- _a = Symbol.toStringTag;
75
69
  //# sourceMappingURL=TwoWayMap.js.map
@@ -0,0 +1,3 @@
1
+ import type { Externals } from './index.js';
2
+ export declare const BrowserExternals: Externals;
3
+ //# sourceMappingURL=BrowserExternals.d.ts.map
@@ -0,0 +1,191 @@
1
+ import { decode as arrayBufferFromBase64, encode as arrayBufferToBase64 } from 'base64-arraybuffer';
2
+ import pako from 'pako';
3
+ import { fileUtil } from '../../service/fileUtil.js';
4
+ import { Uri } from '../index.js';
5
+ class BrowserEventEmitter {
6
+ #listeners = new Map();
7
+ emit(eventName, ...args) {
8
+ const listeners = this.#listeners.get(eventName);
9
+ if (!listeners?.all?.size) {
10
+ return false;
11
+ }
12
+ for (const listener of listeners.all) {
13
+ listener(...args);
14
+ if (listeners.once.has(listener)) {
15
+ listeners.all.delete(listener);
16
+ listeners.once.delete(listener);
17
+ }
18
+ }
19
+ return false;
20
+ }
21
+ on(eventName, listener) {
22
+ if (!this.#listeners.has(eventName)) {
23
+ this.#listeners.set(eventName, { all: new Set(), once: new Set() });
24
+ }
25
+ const listeners = this.#listeners.get(eventName);
26
+ listeners.all.add(listener);
27
+ return this;
28
+ }
29
+ once(eventName, listener) {
30
+ if (!this.#listeners.has(eventName)) {
31
+ this.#listeners.set(eventName, { all: new Set(), once: new Set() });
32
+ }
33
+ const listeners = this.#listeners.get(eventName);
34
+ listeners.all.add(listener);
35
+ listeners.once.add(listener);
36
+ return this;
37
+ }
38
+ }
39
+ class BrowserExternalDownloader {
40
+ async get(uri, options = {}) {
41
+ const headers = new Headers();
42
+ for (const [name, value] of Object.entries(options?.headers ?? {})) {
43
+ const values = typeof value === 'string' ? [value] : value;
44
+ for (const v of values) {
45
+ headers.append(name, v);
46
+ }
47
+ }
48
+ const res = await fetch(uri, {
49
+ headers,
50
+ redirect: 'follow',
51
+ });
52
+ if (!res.ok) {
53
+ throw new Error(`Status code ${res.status}: ${res.ok}`);
54
+ }
55
+ else {
56
+ return new Uint8Array(await res.arrayBuffer());
57
+ }
58
+ }
59
+ }
60
+ class BrowserFsWatcher {
61
+ on(event, listener) {
62
+ if (event === 'ready') {
63
+ listener();
64
+ }
65
+ return this;
66
+ }
67
+ once(event, listener) {
68
+ if (event === 'ready') {
69
+ listener();
70
+ }
71
+ return this;
72
+ }
73
+ async close() {
74
+ }
75
+ }
76
+ class BrowserFileSystem {
77
+ static LocalStorageKey = 'spyglassmc-browser-fs';
78
+ states;
79
+ constructor() {
80
+ this.states = JSON.parse(localStorage.getItem(BrowserFileSystem.LocalStorageKey) ?? '{}');
81
+ }
82
+ saveStates() {
83
+ localStorage.setItem(BrowserFileSystem.LocalStorageKey, JSON.stringify(this.states));
84
+ }
85
+ async chmod(_location, _mode) {
86
+ return;
87
+ }
88
+ async getAllFiles(_location) {
89
+ return [];
90
+ }
91
+ async mkdir(location, _options) {
92
+ location = fileUtil.ensureEndingSlash(location.toString());
93
+ if (this.states[location]) {
94
+ throw new Error(`EEXIST: ${location}`);
95
+ }
96
+ this.states[location] = { type: 'directory' };
97
+ this.saveStates();
98
+ }
99
+ async readFile(location) {
100
+ location = location.toString();
101
+ const entry = this.states[location];
102
+ if (!entry) {
103
+ throw new Error(`ENOENT: ${location}`);
104
+ }
105
+ else if (entry.type === 'directory') {
106
+ throw new Error(`EISDIR: ${location}`);
107
+ }
108
+ return new Uint8Array(arrayBufferFromBase64(entry.content));
109
+ }
110
+ async showFile(_path) {
111
+ throw new Error('showFile not supported on browser');
112
+ }
113
+ async stat(location) {
114
+ location = location.toString();
115
+ const entry = this.states[location];
116
+ if (!entry) {
117
+ throw new Error(`ENOENT: ${location}`);
118
+ }
119
+ return {
120
+ isDirectory: () => entry.type === 'directory',
121
+ isFile: () => entry.type === 'file',
122
+ };
123
+ }
124
+ async unlink(location) {
125
+ location = location.toString();
126
+ const entry = this.states[location];
127
+ if (!entry) {
128
+ throw new Error(`ENOENT: ${location}`);
129
+ }
130
+ delete this.states[location];
131
+ this.saveStates();
132
+ }
133
+ watch(_location) {
134
+ return new BrowserFsWatcher();
135
+ }
136
+ async writeFile(location, data, _options) {
137
+ location = location.toString();
138
+ if (typeof data === 'string') {
139
+ data = new TextEncoder().encode(data);
140
+ }
141
+ data = arrayBufferToBase64(data);
142
+ this.states[location] = { type: 'file', content: data };
143
+ this.saveStates();
144
+ }
145
+ }
146
+ export const BrowserExternals = {
147
+ archive: {
148
+ decompressBall(_buffer, _options) {
149
+ throw new Error('decompressBall not supported on browser.');
150
+ },
151
+ async gunzip(buffer) {
152
+ return pako.inflate(buffer);
153
+ },
154
+ async gzip(buffer) {
155
+ return pako.gzip(buffer);
156
+ },
157
+ },
158
+ crypto: {
159
+ async getSha1(data) {
160
+ if (typeof data === 'string') {
161
+ data = new TextEncoder().encode(data);
162
+ }
163
+ const hash = await crypto.subtle.digest('SHA-1', data);
164
+ return uint8ArrayToHex(new Uint8Array(hash));
165
+ },
166
+ },
167
+ downloader: new BrowserExternalDownloader(),
168
+ error: {
169
+ isKind(e, kind) {
170
+ return e instanceof Error && e.message.startsWith(kind);
171
+ },
172
+ },
173
+ event: {
174
+ EventEmitter: BrowserEventEmitter,
175
+ },
176
+ fs: new BrowserFileSystem(),
177
+ uri: {
178
+ normalize(uri) {
179
+ return new Uri(uri).toString();
180
+ },
181
+ },
182
+ };
183
+ function uint8ArrayToHex(array) {
184
+ let ans = '';
185
+ for (const v of array) {
186
+ ans += v.toString(16).padStart(2, '0');
187
+ }
188
+ return ans;
189
+ }
190
+ Object.freeze(BrowserExternals);
191
+ //# sourceMappingURL=BrowserExternals.js.map
@@ -0,0 +1,3 @@
1
+ import type { Externals } from './index.js';
2
+ export declare const NodeJsExternals: Externals;
3
+ //# sourceMappingURL=NodeJsExternals.d.ts.map