@valbuild/server 0.12.0 → 0.13.3

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.
@@ -0,0 +1,49 @@
1
+ import {
2
+ TestQuickJSWASMModule,
3
+ newQuickJSAsyncWASMModule,
4
+ } from "quickjs-emscripten";
5
+ import { readValFile } from "./readValFile";
6
+ import path from "path";
7
+ import { createModuleLoader } from "./ValModuleLoader";
8
+ import { newValQuickJSRuntime } from "./ValQuickJSRuntime";
9
+
10
+ const TestCaseDir = "../test/example-projects";
11
+ const TestCases = [
12
+ { name: "basic-next-typescript", valConfigPath: "./val.config" },
13
+ {
14
+ name: "basic-next-src-typescript",
15
+ valConfigPath: "./src/val.config",
16
+ },
17
+ { name: "basic-next-javascript", valConfigPath: "./val.config" },
18
+ { name: "typescript-description-files", valConfigPath: "./val.config" },
19
+ ];
20
+
21
+ describe("read val file", () => {
22
+ // We cannot, currently use TestQuickJSWASMModule
23
+ let QuickJS: TestQuickJSWASMModule;
24
+
25
+ beforeEach(async () => {
26
+ QuickJS = new TestQuickJSWASMModule(await newQuickJSAsyncWASMModule());
27
+ });
28
+
29
+ afterEach(() => {
30
+ QuickJS.disposeAll();
31
+ QuickJS.assertNoMemoryAllocated();
32
+ });
33
+
34
+ test.each(TestCases)("read basic val file from: $name", async (testCase) => {
35
+ const rootDir = path.resolve(__dirname, TestCaseDir, testCase.name);
36
+ const loader = createModuleLoader(rootDir);
37
+ const testRuntime = await newValQuickJSRuntime(QuickJS, loader, {
38
+ maxStackSize: 1024 * 640,
39
+ memoryLimit: 1024 * 640,
40
+ });
41
+ const result = await readValFile(
42
+ "/pages/blogs",
43
+ testCase.valConfigPath,
44
+ testRuntime
45
+ );
46
+ expect(result).toHaveProperty("source");
47
+ expect(result).toHaveProperty("schema");
48
+ });
49
+ });
@@ -0,0 +1,73 @@
1
+ import path from "path";
2
+ import { QuickJSRuntime } from "quickjs-emscripten";
3
+ import { SerializedModuleContent } from "./SerializedModuleContent";
4
+
5
+ export const readValFile = async (
6
+ id: string,
7
+ valConfigPath: string,
8
+ runtime: QuickJSRuntime
9
+ ): Promise<SerializedModuleContent> => {
10
+ const context = runtime.newContext();
11
+ try {
12
+ const modulePath = `.${id}.val`;
13
+ const code = `import * as valModule from ${JSON.stringify(modulePath)};
14
+ import { Internal } from "@valbuild/core";
15
+ globalThis.valModule = {
16
+ id: valModule?.default && Internal.getValPath(valModule?.default),
17
+ schema: valModule?.default && Internal.getSchema(valModule?.default)?.serialize(),
18
+ source: valModule?.default && Internal.getRawSource(valModule?.default),
19
+ };
20
+ `;
21
+ const result = context.evalCode(
22
+ code,
23
+ // Synthetic module name
24
+ path.join(path.dirname(valConfigPath), "<val>")
25
+ );
26
+ if (result.error) {
27
+ const error = result.error.consume(context.dump);
28
+ console.error("Got error", error); // TODO: use this to figure out how to strip out QuickJS specific errors and get the actual stack
29
+
30
+ throw new Error(
31
+ `Could not read val id: ${id}. Cause:\n${error.name}: ${error.message}${
32
+ error.stack ? error.stack : ""
33
+ }`
34
+ );
35
+ } else {
36
+ result.value.dispose();
37
+ const valModule = context
38
+ .getProp(context.global, "valModule")
39
+ .consume(context.dump);
40
+
41
+ const errors: string[] = [];
42
+
43
+ if (!valModule) {
44
+ errors.push(`Could not find any modules at: ${id}`);
45
+ } else {
46
+ if (valModule.id !== id) {
47
+ errors.push(`Expected val id: '${id}' but got: '${valModule.id}'`);
48
+ }
49
+ if (!valModule?.schema) {
50
+ errors.push(`Expected val id: '${id}' to have a schema`);
51
+ }
52
+ if (!valModule?.source) {
53
+ errors.push(`Expected val id: '${id}' to have a source`);
54
+ }
55
+ }
56
+
57
+ if (errors.length > 0) {
58
+ throw Error(
59
+ `While processing module of id: ${id}, we got the following errors:\n${errors.join(
60
+ "\n"
61
+ )}`
62
+ );
63
+ }
64
+ return {
65
+ path: valModule.id, // This might not be the asked id/path, however, that should be handled further up in the call chain
66
+ source: valModule.source,
67
+ schema: valModule.schema,
68
+ };
69
+ }
70
+ } finally {
71
+ context.dispose();
72
+ }
73
+ };