-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gate): cache deno imports on deploy (#925)
<!-- Pull requests are squashed and merged using: - their title as the commit message - their description as the commit body Having a good title and description is important for the users to get readable changelog. --> <!-- 1. Explain WHAT the change is about --> - Fixes [MET-766](https://linear.app/metatypedev/issue/MET-766/typescript-import-caching-and-validation-on-deploy) and [MET-746](https://linear.app/metatypedev/issue/MET-746/deno-runtime-timeouts-on-error) <!-- 3. Explain HOW users should update their code --> #### Migration notes --- - [ ] The change comes with new or modified tests - [ ] Hard-to-understand functions have explanatory comments - [ ] End-user documentation is updated to reflect the change --------- Co-authored-by: Natoandro <[email protected]>
- Loading branch information
1 parent
0787087
commit 353c17d
Showing
9 changed files
with
154 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
import { getLogger } from "../../../log.ts"; | ||
import { PushFailure, PushHandler } from "../../../typegate/hooks.ts"; | ||
import { createArtifactMeta } from "../../utils/deno.ts"; | ||
|
||
const logger = getLogger("typegate"); | ||
|
||
export class DenoFailure extends Error { | ||
failure: PushFailure; | ||
|
||
constructor(message: string) { | ||
super(message); | ||
this.failure = { reason: "DenoImportError", message }; | ||
} | ||
} | ||
|
||
function sandboxImport(modulePath: string) { | ||
return new Promise<void>((resolve, reject) => { | ||
const worker = new Worker(new URL("./worker.ts", import.meta.url).href, { | ||
type: "module", | ||
}); | ||
|
||
worker.postMessage({ import: modulePath }); | ||
|
||
worker.onmessage = ({ data }: MessageEvent<{ error?: any }>) => { | ||
if (data.error) { | ||
reject(data.error); | ||
} else { | ||
resolve(); | ||
} | ||
}; | ||
|
||
worker.onerror = (error) => { | ||
reject(error); | ||
}; | ||
}); | ||
} | ||
|
||
export const cacheModules: PushHandler = async ( | ||
typegraph, | ||
_secretManager, | ||
_response, | ||
artifactStore, | ||
) => { | ||
const { title } = typegraph.types[0]; | ||
const { artifacts } = typegraph.meta; | ||
|
||
for (const mat of typegraph.materializers) { | ||
if (mat.name === "module") { | ||
const matData = mat.data; | ||
const entryPoint = artifacts[matData.entryPoint as string]; | ||
const moduleMeta = createArtifactMeta(title, entryPoint); | ||
const depMetas = (matData.deps as string[]).map((dep) => | ||
createArtifactMeta(title, artifacts[dep]), | ||
); | ||
const entryModulePath = await artifactStore.getLocalPath( | ||
moduleMeta, | ||
depMetas, | ||
); | ||
|
||
try { | ||
logger.info(`Caching deno imports for ${title} (${entryPoint.path})`); | ||
await sandboxImport(entryModulePath); | ||
logger.info(`'${entryPoint.path}' was cached`); | ||
} catch (error) { | ||
console.error(error.stack); | ||
|
||
throw new DenoFailure( | ||
`An error occured when trying to import '${entryPoint.path}'`, | ||
); | ||
} | ||
} | ||
} | ||
|
||
return typegraph; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
self.onmessage = async ({ data }: MessageEvent<{ import: string }>) => { | ||
try { | ||
await import(data.import); | ||
self.postMessage({ success: true }); | ||
} catch (error) { | ||
self.postMessage({ error }); | ||
} | ||
|
||
self.close(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
import { ArtifactMeta } from "../../typegate/artifacts/mod.ts"; | ||
import { Artifact } from "../../typegraph/types.ts"; | ||
|
||
export function createArtifactMeta( | ||
typegraphName: string, | ||
artifact: Artifact, | ||
): ArtifactMeta { | ||
return { | ||
typegraphName, | ||
hash: artifact.hash, | ||
sizeInBytes: artifact.size, | ||
relativePath: artifact.path, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters