sandlot 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 (47) hide show
  1. package/README.md +616 -0
  2. package/dist/bundler.d.ts +148 -0
  3. package/dist/bundler.d.ts.map +1 -0
  4. package/dist/commands.d.ts +179 -0
  5. package/dist/commands.d.ts.map +1 -0
  6. package/dist/fs.d.ts +125 -0
  7. package/dist/fs.d.ts.map +1 -0
  8. package/dist/index.d.ts +16 -0
  9. package/dist/index.d.ts.map +1 -0
  10. package/dist/index.js +2920 -0
  11. package/dist/internal.d.ts +74 -0
  12. package/dist/internal.d.ts.map +1 -0
  13. package/dist/internal.js +1897 -0
  14. package/dist/loader.d.ts +164 -0
  15. package/dist/loader.d.ts.map +1 -0
  16. package/dist/packages.d.ts +199 -0
  17. package/dist/packages.d.ts.map +1 -0
  18. package/dist/react.d.ts +159 -0
  19. package/dist/react.d.ts.map +1 -0
  20. package/dist/react.js +149 -0
  21. package/dist/sandbox-manager.d.ts +249 -0
  22. package/dist/sandbox-manager.d.ts.map +1 -0
  23. package/dist/sandbox.d.ts +193 -0
  24. package/dist/sandbox.d.ts.map +1 -0
  25. package/dist/shared-modules.d.ts +129 -0
  26. package/dist/shared-modules.d.ts.map +1 -0
  27. package/dist/shared-resources.d.ts +105 -0
  28. package/dist/shared-resources.d.ts.map +1 -0
  29. package/dist/ts-libs.d.ts +98 -0
  30. package/dist/ts-libs.d.ts.map +1 -0
  31. package/dist/typechecker.d.ts +127 -0
  32. package/dist/typechecker.d.ts.map +1 -0
  33. package/package.json +64 -0
  34. package/src/bundler.ts +513 -0
  35. package/src/commands.ts +733 -0
  36. package/src/fs.ts +935 -0
  37. package/src/index.ts +149 -0
  38. package/src/internal.ts +116 -0
  39. package/src/loader.ts +229 -0
  40. package/src/packages.ts +936 -0
  41. package/src/react.tsx +331 -0
  42. package/src/sandbox-manager.ts +490 -0
  43. package/src/sandbox.ts +402 -0
  44. package/src/shared-modules.ts +210 -0
  45. package/src/shared-resources.ts +169 -0
  46. package/src/ts-libs.ts +320 -0
  47. package/src/typechecker.ts +635 -0
@@ -0,0 +1,98 @@
1
+ /**
2
+ * TypeScript standard library fetcher and cache.
3
+ *
4
+ * Fetches TypeScript's lib.*.d.ts files from jsDelivr CDN and caches
5
+ * them in IndexedDB for reuse. These files provide types for built-in
6
+ * JavaScript APIs (Array, Number, String) and browser APIs (console, window, document).
7
+ */
8
+ /**
9
+ * Default libs for browser environment with ES2020 target.
10
+ * These provide types for console, DOM APIs, and modern JS features.
11
+ */
12
+ export declare function getDefaultBrowserLibs(): string[];
13
+ /**
14
+ * Parse `/// <reference lib="..." />` directives from a lib file.
15
+ * These directives indicate dependencies on other lib files.
16
+ *
17
+ * @param content - The content of a lib.*.d.ts file
18
+ * @returns Array of lib names referenced (without "lib." prefix or ".d.ts" suffix)
19
+ */
20
+ export declare function parseLibReferences(content: string): string[];
21
+ /**
22
+ * Convert a lib name to its filename.
23
+ * e.g., "es2020" -> "lib.es2020.d.ts"
24
+ */
25
+ export declare function libNameToFileName(name: string): string;
26
+ /**
27
+ * Extract lib name from a file path.
28
+ * e.g., "/node_modules/typescript/lib/lib.es2020.d.ts" -> "es2020"
29
+ * "lib.dom.d.ts" -> "dom"
30
+ */
31
+ export declare function extractLibName(filePath: string): string | null;
32
+ /**
33
+ * Fetch a single lib file from the CDN.
34
+ *
35
+ * @param name - The lib name (e.g., "es2020", "dom")
36
+ * @returns The content of the lib file
37
+ * @throws Error if the fetch fails
38
+ */
39
+ export declare function fetchLibFile(name: string): Promise<string>;
40
+ /**
41
+ * Recursively fetch all lib files needed for the given libs.
42
+ * Parses `/// <reference lib="..." />` directives and fetches dependencies.
43
+ *
44
+ * @param libs - Initial lib names to fetch (e.g., ["es2020", "dom"])
45
+ * @returns Map of lib name to content
46
+ */
47
+ export declare function fetchAllLibs(libs: string[]): Promise<Map<string, string>>;
48
+ /**
49
+ * LibCache provides IndexedDB-backed caching for TypeScript lib files.
50
+ *
51
+ * Usage:
52
+ * ```ts
53
+ * const cache = await LibCache.create();
54
+ * const libs = await cache.getOrFetch(getDefaultBrowserLibs());
55
+ * ```
56
+ */
57
+ export declare class LibCache {
58
+ private db;
59
+ private constructor();
60
+ /**
61
+ * Create and initialize a LibCache instance.
62
+ */
63
+ static create(): Promise<LibCache>;
64
+ /**
65
+ * Get cached libs if available, otherwise fetch from CDN and cache.
66
+ *
67
+ * @param libs - Lib names to fetch (e.g., ["es2020", "dom"])
68
+ * @returns Map of lib name to content (includes all transitive dependencies)
69
+ */
70
+ getOrFetch(libs: string[]): Promise<Map<string, string>>;
71
+ /**
72
+ * Get cached libs if available.
73
+ */
74
+ get(): Promise<Map<string, string> | null>;
75
+ /**
76
+ * Store libs in the cache.
77
+ */
78
+ set(libs: Map<string, string>): Promise<void>;
79
+ /**
80
+ * Clear all cached libs.
81
+ */
82
+ clear(): Promise<void>;
83
+ /**
84
+ * Close the database connection.
85
+ */
86
+ close(): void;
87
+ }
88
+ /**
89
+ * Convenience function to fetch and cache libs in one call.
90
+ * Creates a temporary LibCache, fetches libs, and returns the result.
91
+ *
92
+ * For repeated use, prefer creating a LibCache instance directly.
93
+ *
94
+ * @param libs - Lib names to fetch (defaults to getDefaultBrowserLibs())
95
+ * @returns Map of lib name to content
96
+ */
97
+ export declare function fetchAndCacheLibs(libs?: string[]): Promise<Map<string, string>>;
98
+ //# sourceMappingURL=ts-libs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ts-libs.d.ts","sourceRoot":"","sources":["../src/ts-libs.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAoBH;;;GAGG;AACH,wBAAgB,qBAAqB,IAAI,MAAM,EAAE,CAEhD;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,CAY5D;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEtD;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAG9D;AAED;;;;;;GAMG;AACH,wBAAsB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAUhE;AAED;;;;;;GAMG;AACH,wBAAsB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CA6C/E;AA+CD;;;;;;;;GAQG;AACH,qBAAa,QAAQ;IACnB,OAAO,CAAC,EAAE,CAAc;IAExB,OAAO;IAIP;;OAEG;WACU,MAAM,IAAI,OAAO,CAAC,QAAQ,CAAC;IAKxC;;;;;OAKG;IACG,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAwB9D;;OAEG;IACG,GAAG,IAAI,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI,CAAC;IAmBhD;;OAEG;IACG,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAcnD;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAM5B;;OAEG;IACH,KAAK,IAAI,IAAI;CAGd;AAED;;;;;;;;GAQG;AACH,wBAAsB,iBAAiB,CACrC,IAAI,GAAE,MAAM,EAA4B,GACvC,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAO9B"}
@@ -0,0 +1,127 @@
1
+ import type { IFileSystem } from "just-bash/browser";
2
+ /**
3
+ * Options for type checking
4
+ */
5
+ export interface TypecheckOptions {
6
+ /**
7
+ * The virtual filesystem to read source files from
8
+ */
9
+ fs: IFileSystem;
10
+ /**
11
+ * Entry point path (absolute path in the virtual filesystem).
12
+ * TypeScript will discover all imported files from this root.
13
+ */
14
+ entryPoint: string;
15
+ /**
16
+ * Path to tsconfig.json in the virtual filesystem.
17
+ * Default: "/tsconfig.json"
18
+ */
19
+ tsconfigPath?: string;
20
+ /**
21
+ * Pre-loaded TypeScript lib files (e.g., lib.dom.d.ts, lib.es2020.d.ts).
22
+ * Map from lib name (e.g., "dom", "es2020") to file content.
23
+ * If provided, enables proper type checking for built-in APIs.
24
+ * Use fetchAndCacheLibs() from ts-libs to fetch these.
25
+ */
26
+ libFiles?: Map<string, string>;
27
+ }
28
+ /**
29
+ * A single diagnostic message from type checking
30
+ */
31
+ export interface Diagnostic {
32
+ /**
33
+ * File path where the error occurred (null for global errors)
34
+ */
35
+ file: string | null;
36
+ /**
37
+ * 1-based line number (null if not applicable)
38
+ */
39
+ line: number | null;
40
+ /**
41
+ * 1-based column number (null if not applicable)
42
+ */
43
+ column: number | null;
44
+ /**
45
+ * TypeScript error code (e.g., 2322 for type mismatch)
46
+ */
47
+ code: number;
48
+ /**
49
+ * Severity category
50
+ */
51
+ category: "error" | "warning" | "suggestion" | "message";
52
+ /**
53
+ * Human-readable error message
54
+ */
55
+ message: string;
56
+ }
57
+ /**
58
+ * Result of type checking
59
+ */
60
+ export interface TypecheckResult {
61
+ /**
62
+ * All diagnostics from type checking
63
+ */
64
+ diagnostics: Diagnostic[];
65
+ /**
66
+ * True if there are any errors (not just warnings)
67
+ */
68
+ hasErrors: boolean;
69
+ /**
70
+ * List of files that were checked
71
+ */
72
+ checkedFiles: string[];
73
+ }
74
+ /**
75
+ * Type check TypeScript files from a virtual filesystem
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * const fs = IndexedDbFs.createInMemory({
80
+ * initialFiles: {
81
+ * "/tsconfig.json": JSON.stringify({
82
+ * compilerOptions: {
83
+ * target: "ES2020",
84
+ * module: "ESNext",
85
+ * strict: true
86
+ * }
87
+ * }),
88
+ * "/src/index.ts": `
89
+ * const x: number = "hello"; // Type error!
90
+ * export { x };
91
+ * `,
92
+ * }
93
+ * });
94
+ *
95
+ * const result = await typecheck({
96
+ * fs,
97
+ * entryPoint: "/src/index.ts",
98
+ * });
99
+ *
100
+ * console.log(result.hasErrors); // true
101
+ * console.log(result.diagnostics[0].message); // Type 'string' is not assignable...
102
+ * ```
103
+ */
104
+ export declare function typecheck(options: TypecheckOptions): Promise<TypecheckResult>;
105
+ /**
106
+ * Format diagnostics as a human-readable string
107
+ *
108
+ * @example
109
+ * ```ts
110
+ * const result = await typecheck({ fs, entryPoint: "/src/index.ts" });
111
+ * console.log(formatDiagnostics(result.diagnostics));
112
+ * // /src/index.ts:3:7 - error TS2322: Type 'string' is not assignable to type 'number'.
113
+ * ```
114
+ */
115
+ export declare function formatDiagnostics(diagnostics: Diagnostic[]): string;
116
+ /**
117
+ * Format diagnostics in a concise format suitable for AI agents
118
+ *
119
+ * @example
120
+ * ```ts
121
+ * const result = await typecheck({ fs, entryPoint: "/src/index.ts" });
122
+ * console.log(formatDiagnosticsForAgent(result.diagnostics));
123
+ * // Error in /src/index.ts at line 3: Type 'string' is not assignable to type 'number'. (TS2322)
124
+ * ```
125
+ */
126
+ export declare function formatDiagnosticsForAgent(diagnostics: Diagnostic[]): string;
127
+ //# sourceMappingURL=typechecker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typechecker.d.ts","sourceRoot":"","sources":["../src/typechecker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,EAAE,EAAE,WAAW,CAAC;IAEhB;;;OAGG;IACH,UAAU,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAChC;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB;;OAEG;IACH,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAEpB;;OAEG;IACH,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAEpB;;OAEG;IACH,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAEtB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,QAAQ,EAAE,OAAO,GAAG,SAAS,GAAG,YAAY,GAAG,SAAS,CAAC;IAEzD;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;OAEG;IACH,WAAW,EAAE,UAAU,EAAE,CAAC;IAE1B;;OAEG;IACH,SAAS,EAAE,OAAO,CAAC;IAEnB;;OAEG;IACH,YAAY,EAAE,MAAM,EAAE,CAAC;CACxB;AAkZD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAsB,SAAS,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,eAAe,CAAC,CA2DnF;AAED;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,UAAU,EAAE,GAAG,MAAM,CASnE;AAED;;;;;;;;;GASG;AACH,wBAAgB,yBAAyB,CAAC,WAAW,EAAE,UAAU,EAAE,GAAG,MAAM,CAwB3E"}
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "sandlot",
3
+ "version": "0.1.0",
4
+ "description": "Browser-based TypeScript sandbox with esbuild bundling and type checking",
5
+ "author": "blindmansion",
6
+ "license": "MIT",
7
+ "keywords": [
8
+ "sandbox",
9
+ "typescript",
10
+ "esbuild",
11
+ "browser",
12
+ "bundler",
13
+ "playground",
14
+ "agent"
15
+ ],
16
+ "type": "module",
17
+ "main": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "import": "./dist/index.js",
23
+ "default": "./dist/index.js"
24
+ },
25
+ "./react": {
26
+ "types": "./dist/react.d.ts",
27
+ "import": "./dist/react.js",
28
+ "default": "./dist/react.js"
29
+ },
30
+ "./internal": {
31
+ "types": "./dist/internal.d.ts",
32
+ "import": "./dist/internal.js",
33
+ "default": "./dist/internal.js"
34
+ }
35
+ },
36
+ "files": [
37
+ "dist",
38
+ "src"
39
+ ],
40
+ "scripts": {
41
+ "build": "bun run build:js && bun run build:types",
42
+ "build:js": "bun build ./src/index.ts ./src/react.tsx ./src/internal.ts --outdir ./dist --format esm --packages external",
43
+ "build:types": "tsc --emitDeclarationOnly",
44
+ "prepublishOnly": "bun run build"
45
+ },
46
+ "dependencies": {
47
+ "esbuild-wasm": "^0.27.2",
48
+ "just-bash": "^2.6.0",
49
+ "typescript": "^5.9.3"
50
+ },
51
+ "peerDependencies": {
52
+ "react": ">=17.0.0"
53
+ },
54
+ "peerDependenciesMeta": {
55
+ "react": {
56
+ "optional": true
57
+ }
58
+ },
59
+ "devDependencies": {
60
+ "@types/bun": "latest",
61
+ "@types/react": "^19",
62
+ "react": "^19.2.3"
63
+ }
64
+ }