sandlot 0.1.4 → 0.2.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 (115) hide show
  1. package/dist/browser/bundler.d.ts +68 -0
  2. package/dist/browser/bundler.d.ts.map +1 -0
  3. package/dist/browser/executor.d.ts +46 -0
  4. package/dist/browser/executor.d.ts.map +1 -0
  5. package/dist/browser/index.d.ts +9 -0
  6. package/dist/browser/index.d.ts.map +1 -0
  7. package/dist/browser/index.js +2692 -0
  8. package/dist/browser/preset.d.ts +63 -0
  9. package/dist/browser/preset.d.ts.map +1 -0
  10. package/dist/commands/index.d.ts +20 -11
  11. package/dist/commands/index.d.ts.map +1 -1
  12. package/dist/commands/types.d.ts +31 -132
  13. package/dist/commands/types.d.ts.map +1 -1
  14. package/dist/core/bundler-utils.d.ts +142 -0
  15. package/dist/core/bundler-utils.d.ts.map +1 -0
  16. package/dist/core/esm-types-resolver.d.ts +125 -0
  17. package/dist/core/esm-types-resolver.d.ts.map +1 -0
  18. package/dist/core/executor.d.ts +35 -0
  19. package/dist/core/executor.d.ts.map +1 -0
  20. package/dist/{fs.d.ts → core/fs.d.ts} +27 -29
  21. package/dist/core/fs.d.ts.map +1 -0
  22. package/dist/core/sandbox.d.ts +30 -0
  23. package/dist/core/sandbox.d.ts.map +1 -0
  24. package/dist/core/sandlot.d.ts +30 -0
  25. package/dist/core/sandlot.d.ts.map +1 -0
  26. package/dist/core/shared-module-registry.d.ts +46 -0
  27. package/dist/core/shared-module-registry.d.ts.map +1 -0
  28. package/dist/core/typechecker.d.ts +60 -0
  29. package/dist/core/typechecker.d.ts.map +1 -0
  30. package/dist/index.d.ts +11 -16
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +1399 -2010
  33. package/dist/node/bundler.d.ts +48 -0
  34. package/dist/node/bundler.d.ts.map +1 -0
  35. package/dist/node/executor.d.ts +48 -0
  36. package/dist/node/executor.d.ts.map +1 -0
  37. package/dist/node/index.d.ts +9 -0
  38. package/dist/node/index.d.ts.map +1 -0
  39. package/dist/node/index.js +2646 -0
  40. package/dist/node/preset.d.ts +62 -0
  41. package/dist/node/preset.d.ts.map +1 -0
  42. package/dist/types.d.ts +525 -0
  43. package/dist/types.d.ts.map +1 -0
  44. package/package.json +16 -6
  45. package/src/browser/bundler.ts +294 -0
  46. package/src/browser/executor.ts +71 -0
  47. package/src/browser/index.ts +57 -0
  48. package/src/browser/preset.ts +179 -0
  49. package/src/commands/index.ts +526 -43
  50. package/src/commands/types.ts +82 -146
  51. package/src/core/bundler-utils.ts +630 -0
  52. package/src/core/esm-types-resolver.ts +432 -0
  53. package/src/core/executor.ts +161 -0
  54. package/src/{fs.ts → core/fs.ts} +59 -37
  55. package/src/core/sandbox.ts +621 -0
  56. package/src/core/sandlot.ts +77 -0
  57. package/src/core/shared-module-registry.ts +138 -0
  58. package/src/core/typechecker.ts +607 -0
  59. package/src/index.ts +104 -139
  60. package/src/node/bundler.ts +194 -0
  61. package/src/node/executor.ts +87 -0
  62. package/src/node/index.ts +39 -0
  63. package/src/node/preset.ts +178 -0
  64. package/src/types.ts +668 -0
  65. package/README.md +0 -243
  66. package/dist/build-emitter.d.ts +0 -47
  67. package/dist/build-emitter.d.ts.map +0 -1
  68. package/dist/builder.d.ts +0 -370
  69. package/dist/builder.d.ts.map +0 -1
  70. package/dist/bundler.d.ts +0 -152
  71. package/dist/bundler.d.ts.map +0 -1
  72. package/dist/commands/compile.d.ts +0 -13
  73. package/dist/commands/compile.d.ts.map +0 -1
  74. package/dist/commands/packages.d.ts +0 -17
  75. package/dist/commands/packages.d.ts.map +0 -1
  76. package/dist/commands/run.d.ts +0 -40
  77. package/dist/commands/run.d.ts.map +0 -1
  78. package/dist/commands.d.ts +0 -179
  79. package/dist/commands.d.ts.map +0 -1
  80. package/dist/fs.d.ts.map +0 -1
  81. package/dist/internal.d.ts +0 -79
  82. package/dist/internal.d.ts.map +0 -1
  83. package/dist/internal.js +0 -1942
  84. package/dist/loader.d.ts +0 -164
  85. package/dist/loader.d.ts.map +0 -1
  86. package/dist/packages.d.ts +0 -199
  87. package/dist/packages.d.ts.map +0 -1
  88. package/dist/runner.d.ts +0 -314
  89. package/dist/runner.d.ts.map +0 -1
  90. package/dist/sandbox-manager.d.ts +0 -261
  91. package/dist/sandbox-manager.d.ts.map +0 -1
  92. package/dist/sandbox.d.ts +0 -267
  93. package/dist/sandbox.d.ts.map +0 -1
  94. package/dist/shared-modules.d.ts +0 -148
  95. package/dist/shared-modules.d.ts.map +0 -1
  96. package/dist/shared-resources.d.ts +0 -102
  97. package/dist/shared-resources.d.ts.map +0 -1
  98. package/dist/ts-libs.d.ts +0 -85
  99. package/dist/ts-libs.d.ts.map +0 -1
  100. package/dist/typechecker.d.ts +0 -127
  101. package/dist/typechecker.d.ts.map +0 -1
  102. package/src/build-emitter.ts +0 -64
  103. package/src/builder.ts +0 -498
  104. package/src/bundler.ts +0 -575
  105. package/src/commands/compile.ts +0 -236
  106. package/src/commands/packages.ts +0 -154
  107. package/src/commands/run.ts +0 -245
  108. package/src/internal.ts +0 -119
  109. package/src/loader.ts +0 -229
  110. package/src/packages.ts +0 -936
  111. package/src/sandbox.ts +0 -398
  112. package/src/shared-modules.ts +0 -280
  113. package/src/shared-resources.ts +0 -166
  114. package/src/ts-libs.ts +0 -218
  115. package/src/typechecker.ts +0 -635
@@ -0,0 +1,138 @@
1
+ import type { ISharedModuleRegistry } from "../types";
2
+
3
+ /**
4
+ * Generate a unique instance ID for a registry.
5
+ * Uses crypto.randomUUID if available, otherwise falls back to a simple counter + timestamp.
6
+ */
7
+ let instanceCounter = 0;
8
+ function generateInstanceId(): string {
9
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
10
+ return crypto.randomUUID().slice(0, 8);
11
+ }
12
+ // Fallback for environments without crypto.randomUUID
13
+ return `${Date.now().toString(36)}_${(++instanceCounter).toString(36)}`;
14
+ }
15
+
16
+ /**
17
+ * Simple shared module registry implementation.
18
+ * Created internally by Sandlot from the sharedModules option.
19
+ *
20
+ * Each registry instance has a unique key (e.g., `__sandlot_abc123__`) that
21
+ * isolates it from other Sandlot instances. This allows multiple Sandlot
22
+ * instances to run concurrently with separate shared module configurations.
23
+ *
24
+ * The registry must be exposed globally for bundled code to access shared modules
25
+ * at runtime. Call `exposeGlobally()` after creation, or use `createSharedModuleRegistry()`
26
+ * which does this automatically.
27
+ */
28
+ export class SharedModuleRegistry implements ISharedModuleRegistry {
29
+ private modules: Map<string, unknown>;
30
+ private exportNamesMap: Map<string, string[]>;
31
+ private _registryKey: string;
32
+
33
+ constructor(modules: Record<string, unknown>) {
34
+ this._registryKey = `__sandlot_${generateInstanceId()}__`;
35
+ this.modules = new Map(Object.entries(modules));
36
+ this.exportNamesMap = new Map();
37
+
38
+ // Introspect exports for each module
39
+ for (const [id, mod] of this.modules) {
40
+ this.exportNamesMap.set(id, this.introspectExports(mod));
41
+ }
42
+ }
43
+
44
+ /**
45
+ * The unique global key where this registry is exposed.
46
+ * Bundled code accesses the registry via `globalThis[registryKey]`.
47
+ */
48
+ get registryKey(): string {
49
+ return this._registryKey;
50
+ }
51
+
52
+ /**
53
+ * Expose this registry globally so bundled code can access shared modules.
54
+ * This sets globalThis[this.registryKey] = this.
55
+ */
56
+ exposeGlobally(): this {
57
+ (globalThis as Record<string, unknown>)[this._registryKey] = this;
58
+ return this;
59
+ }
60
+
61
+ /**
62
+ * Remove this registry from global scope.
63
+ */
64
+ removeFromGlobal(): void {
65
+ if ((globalThis as Record<string, unknown>)[this._registryKey] === this) {
66
+ delete (globalThis as Record<string, unknown>)[this._registryKey];
67
+ }
68
+ }
69
+
70
+ get(moduleId: string): unknown {
71
+ const mod = this.modules.get(moduleId);
72
+ if (mod === undefined && !this.modules.has(moduleId)) {
73
+ throw new Error(`Shared module "${moduleId}" not registered.`);
74
+ }
75
+ return mod;
76
+ }
77
+
78
+ has(moduleId: string): boolean {
79
+ return this.modules.has(moduleId);
80
+ }
81
+
82
+ getExportNames(moduleId: string): string[] {
83
+ return this.exportNamesMap.get(moduleId) ?? [];
84
+ }
85
+
86
+ list(): string[] {
87
+ return [...this.modules.keys()];
88
+ }
89
+
90
+ private introspectExports(module: unknown): string[] {
91
+ if (module === null || module === undefined) {
92
+ return [];
93
+ }
94
+ if (typeof module !== "object" && typeof module !== "function") {
95
+ return [];
96
+ }
97
+
98
+ const exports: string[] = [];
99
+ for (const key of Object.keys(module as object)) {
100
+ if (this.isValidIdentifier(key)) {
101
+ exports.push(key);
102
+ }
103
+ }
104
+ return exports;
105
+ }
106
+
107
+ private isValidIdentifier(name: string): boolean {
108
+ if (name.length === 0) return false;
109
+ if (!/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name)) return false;
110
+ const reserved = [
111
+ "default",
112
+ "class",
113
+ "function",
114
+ "var",
115
+ "let",
116
+ "const",
117
+ "import",
118
+ "export",
119
+ ];
120
+ return !reserved.includes(name);
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Create a shared module registry from a modules object.
126
+ * Automatically exposes the registry globally for bundled code to access.
127
+ * Returns null if no modules are provided.
128
+ */
129
+ export function createSharedModuleRegistry(
130
+ modules?: Record<string, unknown>
131
+ ): SharedModuleRegistry | null {
132
+ if (!modules || Object.keys(modules).length === 0) {
133
+ return null;
134
+ }
135
+ const registry = new SharedModuleRegistry(modules);
136
+ registry.exposeGlobally();
137
+ return registry;
138
+ }