Skip to content

Commit 30ea3b2

Browse files
committed
greatly limit fetched types to reduce memory usage and speed up type loading
1 parent 5f556aa commit 30ea3b2

3 files changed

Lines changed: 47 additions & 8 deletions

File tree

apps/client/src/components/Editor/init.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ const unsubscribe = subscribe(store.apiStatus, () => {
8787
"file:///globals.ts",
8888
)
8989

90+
// Use package whitelist from server
91+
const allowedPackages = new Set(
92+
editorSupport.typeWriter.allowedATAPackages || [],
93+
)
9094
const ata = () => {
9195
return setupTypeAcquisition({
9296
projectName: "CodeGlue",
@@ -115,6 +119,26 @@ const unsubscribe = subscribe(store.apiStatus, () => {
115119
)
116120
},
117121
},
122+
fetcher: async (url) => {
123+
let packageName = null
124+
125+
const urlString = url.toString()
126+
127+
if (urlString.includes("@types/")) {
128+
const match = urlString.match(/@types\/([^@/]+)/)
129+
packageName = match ? match[1] : null
130+
} else if (urlString.includes("/npm/")) {
131+
const match = urlString.match(/\/npm\/(@?[^@/]+(?:\/[^@/]+)?)@/)
132+
packageName = match ? match[1] : null
133+
}
134+
135+
// Skip packages not in whitelist to prevent memory issues
136+
if (packageName && !allowedPackages.has(packageName)) {
137+
return new Response("{}", { status: 200 })
138+
}
139+
140+
return fetch(url)
141+
},
118142
})
119143
}
120144

apps/client/src/store/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const store = proxy({
4040
mappings: "",
4141
registry: "",
4242
services: "",
43+
allowedATAPackages: [] as string[],
4344
},
4445
},
4546
/**
@@ -72,6 +73,7 @@ const setupStore = async () => {
7273
store.editorSupport.typeWriter.mappings = types.mappings
7374
store.editorSupport.typeWriter.registry = types.registry
7475
store.editorSupport.typeWriter.services = types.services
76+
store.editorSupport.typeWriter.allowedATAPackages = types.allowedATAPackages
7577
store.apiStatus.typesReady = true
7678
})
7779
}

apps/server/src/app/services/type-writer.service.mts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1-
import { TServiceParams } from "@digital-alchemy/core";
1+
import type { TServiceParams } from "@digital-alchemy/core";
22

33
export function TypeWriterService({ logger, type_build }: TServiceParams) {
4-
async function build(): Promise<
5-
Record<"mappings" | "registry" | "services", string>
6-
> {
4+
// Packages that ATA is allowed to fetch for Monaco types to make sure all TS edits autocomplete.
5+
const ALLOWED_ATA_PACKAGES = [
6+
"type-fest",
7+
"node",
8+
"tagged-tag",
9+
"@digital-alchemy/core",
10+
"@digital-alchemy/hass",
11+
"@digital-alchemy/synapse",
12+
"@digital-alchemy/automation",
13+
"dayjs",
14+
];
15+
16+
async function build(): Promise<{
17+
mappings: string;
18+
registry: string;
19+
services: string;
20+
allowedATAPackages: string[];
21+
}> {
722
const { mappings, registry, services } = await type_build.build();
823

924
return {
10-
mappings: mappings.replace(
11-
`module "@digital-alchemy/hass"`,
12-
`module "../user.mts"`,
13-
),
25+
allowedATAPackages: ALLOWED_ATA_PACKAGES,
26+
mappings: mappings.replace(`module "@digital-alchemy/hass"`, `module "../user.mts"`),
1427
registry: registry
1528
.replace(`module "@digital-alchemy/hass"`, `module "../user.mts"`)
1629
.replace(`from "@digital-alchemy/hass"`, `from "../merge.mts"`),

0 commit comments

Comments
 (0)