@valbuild/server 0.60.27 → 0.61.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.
@@ -2,7 +2,7 @@
2
2
  import { Service } from "./Service.js";
3
3
  import { result } from "@valbuild/core/fp";
4
4
  import { Patch } from "./patch/validation.js";
5
- import { ApiGetPatchResponse, ApiPostPatchResponse, ModuleId, PatchId, ApiDeletePatchResponse, FileMetadata, ImageMetadata } from "@valbuild/core";
5
+ import { ApiGetPatchResponse, ApiPostPatchResponse, ModuleId, PatchId, ApiDeletePatchResponse, FileMetadata, ImageMetadata, ValModules } from "@valbuild/core";
6
6
  import { VAL_ENABLE_COOKIE_NAME, VAL_SESSION_COOKIE, VAL_STATE_COOKIE, ValServerError, ValServerJsonResult, ValServerRedirectResult, ValServerResult, ValSession } from "@valbuild/shared/internal";
7
7
  import { ValServer, ValServerCallbacks } from "./ValServer.js";
8
8
  import { SerializedModuleContent } from "./SerializedModuleContent.js";
@@ -20,13 +20,14 @@ export interface ValServerBufferHost {
20
20
  readBuffer: (fileName: string) => Promise<Buffer | undefined>;
21
21
  }
22
22
  export declare class LocalValServer extends ValServer {
23
+ readonly valModules: ValModules;
23
24
  readonly options: LocalValServerOptions;
24
25
  readonly callbacks: ValServerCallbacks;
25
26
  private readonly host;
26
27
  private static readonly PATCHES_DIR;
27
28
  private static readonly FILES_DIR;
28
29
  private readonly patchesRootPath;
29
- constructor(options: LocalValServerOptions, callbacks: ValServerCallbacks);
30
+ constructor(valModules: ValModules, options: LocalValServerOptions, callbacks: ValServerCallbacks);
30
31
  session(): Promise<ValServerJsonResult<ValSession>>;
31
32
  deletePatches(query: {
32
33
  id?: string[];
@@ -44,10 +45,8 @@ export declare class LocalValServer extends ValServer {
44
45
  private getPatchFilePath;
45
46
  private badRequest;
46
47
  protected ensureInitialized(): Promise<result.Result<undefined, ValServerError>>;
47
- protected getModule(moduleId: ModuleId, options: {
48
- source: boolean;
49
- schema: boolean;
50
- }): Promise<SerializedModuleContent>;
48
+ private getSerializedModules;
49
+ protected getModule(moduleId: ModuleId): Promise<SerializedModuleContent>;
51
50
  protected getAllModules(treePath: string): Promise<ModuleId[]>;
52
51
  protected execCommit(patches: [PatchId, ModuleId, Patch][]): Promise<{
53
52
  status: 200;
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { ApiCommitResponse, ApiGetPatchResponse, ApiPostPatchResponse, ApiPostValidationErrorResponse, ApiTreeResponse, ApiDeletePatchResponse, ApiPostValidationResponse } from "@valbuild/core";
2
+ import { ApiCommitResponse, ApiGetPatchResponse, ApiPostPatchResponse, ApiPostValidationErrorResponse, ApiTreeResponse, ApiDeletePatchResponse, ApiPostValidationResponse, ValModules } from "@valbuild/core";
3
3
  import { VAL_ENABLE_COOKIE_NAME, VAL_SESSION_COOKIE, VAL_STATE_COOKIE, ValCookies, ValServerError, ValServerJsonResult, ValServerRedirectResult, ValServerResult, ValSession } from "@valbuild/shared/internal";
4
4
  import { result } from "@valbuild/core/fp";
5
5
  import { Operation } from "@valbuild/core/patch";
@@ -16,9 +16,10 @@ export type ValServerOptions = {
16
16
  };
17
17
  export declare abstract class ValServer implements IValServer {
18
18
  readonly cwd: string;
19
+ readonly valModules: ValModules;
19
20
  readonly options: ValServerOptions;
20
21
  readonly callbacks: ValServerCallbacks;
21
- constructor(cwd: string, options: ValServerOptions, callbacks: ValServerCallbacks);
22
+ constructor(cwd: string, valModules: ValModules, options: ValServerOptions, callbacks: ValServerCallbacks);
22
23
  enable(query: {
23
24
  redirect_to?: string;
24
25
  }): Promise<ValServerRedirectResult<VAL_ENABLE_COOKIE_NAME>>;
@@ -1,6 +1,6 @@
1
1
  import { ServiceOptions } from "./Service.js";
2
2
  import { IValServer, ValServerCallbacks } from "./ValServer.js";
3
- import { ValConfig } from "@valbuild/core";
3
+ import { ValConfig, ValModules } from "@valbuild/core";
4
4
  import { ValServerGenericResult } from "@valbuild/shared/internal";
5
5
  type Versions = {
6
6
  versions?: {
@@ -114,7 +114,7 @@ type ValServerOverrides = Partial<{
114
114
  */
115
115
  disableCache?: boolean;
116
116
  }>;
117
- export declare function createValServer(route: string, opts: ValApiOptions, callbacks: ValServerCallbacks): Promise<IValServer>;
117
+ export declare function createValServer(valModules: ValModules, route: string, opts: ValApiOptions, callbacks: ValServerCallbacks): Promise<IValServer>;
118
118
  export declare function safeReadGit(cwd: string): Promise<{
119
119
  commit?: string;
120
120
  branch?: string;
@@ -1414,8 +1414,9 @@ function getValidationErrorMetadata(validationError) {
1414
1414
 
1415
1415
  const ops = new patch.JSONOps();
1416
1416
  class ValServer {
1417
- constructor(cwd, options, callbacks) {
1417
+ constructor(cwd, valModules, options, callbacks) {
1418
1418
  this.cwd = cwd;
1419
+ this.valModules = valModules;
1419
1420
  this.options = options;
1420
1421
  this.callbacks = callbacks;
1421
1422
  }
@@ -2052,8 +2053,9 @@ const textEncoder = new TextEncoder();
2052
2053
  class LocalValServer extends ValServer {
2053
2054
  static PATCHES_DIR = "patches";
2054
2055
  static FILES_DIR = "files";
2055
- constructor(options, callbacks) {
2056
- super(options.service.sourceFileHandler.projectRoot, options, callbacks);
2056
+ constructor(valModules, options, callbacks) {
2057
+ super(options.service.sourceFileHandler.projectRoot, valModules, options, callbacks);
2058
+ this.valModules = valModules;
2057
2059
  this.options = options;
2058
2060
  this.callbacks = callbacks;
2059
2061
  this.patchesRootPath = options.cacheDir || path__namespace["default"].join(options.service.sourceFileHandler.projectRoot, ".val");
@@ -2322,19 +2324,59 @@ class LocalValServer extends ValServer {
2322
2324
  // No RemoteFS so nothing to ensure
2323
2325
  return fp.result.ok(undefined);
2324
2326
  }
2325
- getModule(moduleId, options) {
2326
- return this.options.service.get(moduleId, "", {
2327
- ...options,
2328
- validate: false
2327
+ getSerializedModules() {
2328
+ return Promise.all(this.valModules.modules.map(({
2329
+ def
2330
+ }, i) => {
2331
+ return def().then(({
2332
+ default: valModule
2333
+ }) => {
2334
+ var _Internal$getSchema;
2335
+ const path = core.Internal.getValPath(valModule);
2336
+ if (!path) {
2337
+ throw Error(`Module defined at pos: ${i} is missing path`);
2338
+ }
2339
+ const source = core.Internal.getSource(valModule);
2340
+ if (!source) {
2341
+ // TODO
2342
+ throw Error(`Module defined at pos: ${i} is missing source`);
2343
+ }
2344
+ const schema = (_Internal$getSchema = core.Internal.getSchema(valModule)) === null || _Internal$getSchema === void 0 ? void 0 : _Internal$getSchema.serialize();
2345
+ if (!schema) {
2346
+ // TODO
2347
+ throw Error(`Module defined at pos: ${i} is missing schema`);
2348
+ }
2349
+ return {
2350
+ path,
2351
+ source: source,
2352
+ errors: false,
2353
+ //valModule[GetSchema]?.validate(path, source),
2354
+ schema: schema
2355
+ };
2356
+ });
2357
+ }));
2358
+ }
2359
+ getModule(moduleId) {
2360
+ // TODO: do not get all modules - we only should only get the ones we need
2361
+ return this.getSerializedModules().then(all => {
2362
+ const found = all.find(valModule => valModule.path === moduleId);
2363
+ if (!found) {
2364
+ throw Error(`Module ${moduleId} not found`);
2365
+ }
2366
+ return found;
2329
2367
  });
2330
2368
  }
2331
2369
  async getAllModules(treePath) {
2332
- const moduleIds = this.host.readDirectory(this.cwd, ["ts", "js"], ["node_modules", ".*"], ["**/*.val.ts", "**/*.val.js"]).filter(file => {
2370
+ const moduleIds = (await this.getSerializedModules()).filter(({
2371
+ path
2372
+ }) => {
2333
2373
  if (treePath) {
2334
- return file.replace(this.cwd, "").startsWith(treePath);
2374
+ return path.startsWith(treePath);
2335
2375
  }
2336
2376
  return true;
2337
- }).map(file => file.replace(this.cwd, "").replace(".val.js", "").replace(".val.ts", "").split(path__namespace["default"].sep).join("/"));
2377
+ }).map(({
2378
+ path
2379
+ }) => path);
2338
2380
  return moduleIds;
2339
2381
  }
2340
2382
  async execCommit(patches) {
@@ -2425,30 +2467,72 @@ function encodeJwt(payload, sessionKey) {
2425
2467
 
2426
2468
  class ProxyValServer extends ValServer {
2427
2469
  moduleCache = null;
2428
- constructor(cwd, options, apiOptions, callbacks) {
2429
- super(cwd, options, callbacks);
2470
+ constructor(cwd, valModules, options, apiOptions, callbacks) {
2471
+ super(cwd, valModules, options, callbacks);
2430
2472
  this.cwd = cwd;
2473
+ this.valModules = valModules;
2431
2474
  this.options = options;
2432
2475
  this.apiOptions = apiOptions;
2433
2476
  this.callbacks = callbacks;
2434
2477
  this.moduleCache = null;
2435
2478
  }
2436
2479
 
2437
- /** Remote FS dependent methods: */
2480
+ // TODO: restructure this
2438
2481
 
2439
- async getModule(moduleId,
2440
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
2441
- _options) {
2442
- if (this.moduleCache) {
2443
- return this.moduleCache[moduleId];
2444
- }
2445
- throw new Error("Module cache not initialized");
2482
+ getSerializedModules() {
2483
+ return Promise.all(this.valModules.modules.map(({
2484
+ def
2485
+ }, i) => {
2486
+ return def().then(({
2487
+ default: valModule
2488
+ }) => {
2489
+ var _Internal$getSchema;
2490
+ const path = core.Internal.getValPath(valModule);
2491
+ if (!path) {
2492
+ throw Error(`Module defined at pos: ${i} is missing path`);
2493
+ }
2494
+ const source = core.Internal.getSource(valModule);
2495
+ if (!source) {
2496
+ // TODO
2497
+ throw Error(`Module defined at pos: ${i} is missing source`);
2498
+ }
2499
+ const schema = (_Internal$getSchema = core.Internal.getSchema(valModule)) === null || _Internal$getSchema === void 0 ? void 0 : _Internal$getSchema.serialize();
2500
+ if (!schema) {
2501
+ // TODO
2502
+ throw Error(`Module defined at pos: ${i} is missing schema`);
2503
+ }
2504
+ return {
2505
+ path,
2506
+ source: source,
2507
+ errors: false,
2508
+ //valModule[GetSchema]?.validate(path, source),
2509
+ schema: schema
2510
+ };
2511
+ });
2512
+ }));
2513
+ }
2514
+ getModule(moduleId) {
2515
+ // TODO: do not get all modules - we only should only get the ones we need
2516
+ return this.getSerializedModules().then(all => {
2517
+ const found = all.find(valModule => valModule.path === moduleId);
2518
+ if (!found) {
2519
+ throw Error(`Module ${moduleId} not found`);
2520
+ }
2521
+ return found;
2522
+ });
2446
2523
  }
2447
2524
  async getAllModules(treePath) {
2448
- if (!this.moduleCache) {
2449
- throw new Error("Module cache not initialized");
2450
- }
2451
- return Object.keys(this.moduleCache).filter(moduleId => moduleId.startsWith(treePath));
2525
+ const moduleIds = (await this.getSerializedModules()).filter(({
2526
+ path
2527
+ }) => {
2528
+ if (treePath) {
2529
+ return path.startsWith(treePath);
2530
+ }
2531
+ return true;
2532
+ }).map(({
2533
+ path
2534
+ }) => path);
2535
+ return moduleIds;
2452
2536
  }
2453
2537
  execCommit(patches, cookies) {
2454
2538
  return withAuth(this.options.valSecret, cookies, "execCommit", async ({
@@ -2489,67 +2573,14 @@ class ProxyValServer extends ValServer {
2489
2573
  }
2490
2574
  });
2491
2575
  }
2492
- async init(commit, token) {
2493
- const params = createParams({
2494
- root: this.apiOptions.root,
2495
- commit,
2496
- ext: ["ts", "js", "json"],
2497
- package: ["@valbuild/core@" + this.options.versions.core, "@valbuild/next@" + this.options.versions.next],
2498
- include: ["**/*.val.{js,ts},package.json,tsconfig.json,jsconfig.json"]
2499
- });
2500
- const url = new URL(`/v1/eval/${this.options.remote}/heads/${this.options.git.branch}/~?${params}`, this.options.valContentUrl);
2501
- try {
2502
- const fetchRes = await fetch(url, {
2503
- headers: getAuthHeaders(token, "application/json")
2504
- });
2505
- if (fetchRes.status === 200) {
2506
- const json = await fetchRes.json();
2507
- let error = false;
2508
- if (typeof json !== "object") {
2509
- error = {
2510
- details: "Invalid response: not an object"
2511
- };
2512
- }
2513
- if (typeof json.git !== "object") {
2514
- error = {
2515
- details: "Invalid response: missing git"
2516
- };
2517
- }
2518
- if (typeof json.git.commit !== "string") {
2519
- error = {
2520
- details: "Invalid response: missing git.commit"
2521
- };
2522
- }
2523
- if (typeof json.modules !== "object" || json.modules === null) {
2524
- error = {
2525
- details: "Invalid response: missing modules"
2526
- };
2527
- }
2528
- if (error) {
2529
- console.error("Could not initialize remote modules", error);
2530
- return {
2531
- status: 500,
2532
- json: {
2533
- message: "Failed to fetch remote modules",
2534
- ...error
2535
- }
2536
- };
2537
- }
2538
- this.moduleCache = json.modules;
2539
- return {
2540
- status: 200
2541
- };
2542
- } else {
2543
- return createJsonError(fetchRes);
2544
- }
2545
- } catch (err) {
2546
- return {
2547
- status: 500,
2548
- json: {
2549
- message: "Failed to fetch: check network connection"
2550
- }
2551
- };
2552
- }
2576
+ async init(
2577
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
2578
+ _commit,
2579
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
2580
+ _token) {
2581
+ return {
2582
+ status: 200
2583
+ };
2553
2584
  }
2554
2585
  async ensureInitialized(errorMessageType, cookies) {
2555
2586
  const commit = this.options.git.commit;
@@ -3118,13 +3149,13 @@ function createParams(params) {
3118
3149
  return paramsString;
3119
3150
  }
3120
3151
 
3121
- async function createValServer(route, opts, callbacks) {
3152
+ async function createValServer(valModules, route, opts, callbacks) {
3122
3153
  const serverOpts = await initHandlerOptions(route, opts);
3123
3154
  if (serverOpts.mode === "proxy") {
3124
3155
  const projectRoot = process.cwd(); //[process.cwd(), opts.root || ""] .filter((seg) => seg) .join("/");
3125
- return new ProxyValServer(projectRoot, serverOpts, opts, callbacks);
3156
+ return new ProxyValServer(projectRoot, valModules, serverOpts, opts, callbacks);
3126
3157
  } else {
3127
- return new LocalValServer(serverOpts, callbacks);
3158
+ return new LocalValServer(valModules, serverOpts, callbacks);
3128
3159
  }
3129
3160
  }
3130
3161
  async function initHandlerOptions(route, opts) {
@@ -1414,8 +1414,9 @@ function getValidationErrorMetadata(validationError) {
1414
1414
 
1415
1415
  const ops = new patch.JSONOps();
1416
1416
  class ValServer {
1417
- constructor(cwd, options, callbacks) {
1417
+ constructor(cwd, valModules, options, callbacks) {
1418
1418
  this.cwd = cwd;
1419
+ this.valModules = valModules;
1419
1420
  this.options = options;
1420
1421
  this.callbacks = callbacks;
1421
1422
  }
@@ -2052,8 +2053,9 @@ const textEncoder = new TextEncoder();
2052
2053
  class LocalValServer extends ValServer {
2053
2054
  static PATCHES_DIR = "patches";
2054
2055
  static FILES_DIR = "files";
2055
- constructor(options, callbacks) {
2056
- super(options.service.sourceFileHandler.projectRoot, options, callbacks);
2056
+ constructor(valModules, options, callbacks) {
2057
+ super(options.service.sourceFileHandler.projectRoot, valModules, options, callbacks);
2058
+ this.valModules = valModules;
2057
2059
  this.options = options;
2058
2060
  this.callbacks = callbacks;
2059
2061
  this.patchesRootPath = options.cacheDir || path__namespace["default"].join(options.service.sourceFileHandler.projectRoot, ".val");
@@ -2322,19 +2324,59 @@ class LocalValServer extends ValServer {
2322
2324
  // No RemoteFS so nothing to ensure
2323
2325
  return fp.result.ok(undefined);
2324
2326
  }
2325
- getModule(moduleId, options) {
2326
- return this.options.service.get(moduleId, "", {
2327
- ...options,
2328
- validate: false
2327
+ getSerializedModules() {
2328
+ return Promise.all(this.valModules.modules.map(({
2329
+ def
2330
+ }, i) => {
2331
+ return def().then(({
2332
+ default: valModule
2333
+ }) => {
2334
+ var _Internal$getSchema;
2335
+ const path = core.Internal.getValPath(valModule);
2336
+ if (!path) {
2337
+ throw Error(`Module defined at pos: ${i} is missing path`);
2338
+ }
2339
+ const source = core.Internal.getSource(valModule);
2340
+ if (!source) {
2341
+ // TODO
2342
+ throw Error(`Module defined at pos: ${i} is missing source`);
2343
+ }
2344
+ const schema = (_Internal$getSchema = core.Internal.getSchema(valModule)) === null || _Internal$getSchema === void 0 ? void 0 : _Internal$getSchema.serialize();
2345
+ if (!schema) {
2346
+ // TODO
2347
+ throw Error(`Module defined at pos: ${i} is missing schema`);
2348
+ }
2349
+ return {
2350
+ path,
2351
+ source: source,
2352
+ errors: false,
2353
+ //valModule[GetSchema]?.validate(path, source),
2354
+ schema: schema
2355
+ };
2356
+ });
2357
+ }));
2358
+ }
2359
+ getModule(moduleId) {
2360
+ // TODO: do not get all modules - we only should only get the ones we need
2361
+ return this.getSerializedModules().then(all => {
2362
+ const found = all.find(valModule => valModule.path === moduleId);
2363
+ if (!found) {
2364
+ throw Error(`Module ${moduleId} not found`);
2365
+ }
2366
+ return found;
2329
2367
  });
2330
2368
  }
2331
2369
  async getAllModules(treePath) {
2332
- const moduleIds = this.host.readDirectory(this.cwd, ["ts", "js"], ["node_modules", ".*"], ["**/*.val.ts", "**/*.val.js"]).filter(file => {
2370
+ const moduleIds = (await this.getSerializedModules()).filter(({
2371
+ path
2372
+ }) => {
2333
2373
  if (treePath) {
2334
- return file.replace(this.cwd, "").startsWith(treePath);
2374
+ return path.startsWith(treePath);
2335
2375
  }
2336
2376
  return true;
2337
- }).map(file => file.replace(this.cwd, "").replace(".val.js", "").replace(".val.ts", "").split(path__namespace["default"].sep).join("/"));
2377
+ }).map(({
2378
+ path
2379
+ }) => path);
2338
2380
  return moduleIds;
2339
2381
  }
2340
2382
  async execCommit(patches) {
@@ -2425,30 +2467,72 @@ function encodeJwt(payload, sessionKey) {
2425
2467
 
2426
2468
  class ProxyValServer extends ValServer {
2427
2469
  moduleCache = null;
2428
- constructor(cwd, options, apiOptions, callbacks) {
2429
- super(cwd, options, callbacks);
2470
+ constructor(cwd, valModules, options, apiOptions, callbacks) {
2471
+ super(cwd, valModules, options, callbacks);
2430
2472
  this.cwd = cwd;
2473
+ this.valModules = valModules;
2431
2474
  this.options = options;
2432
2475
  this.apiOptions = apiOptions;
2433
2476
  this.callbacks = callbacks;
2434
2477
  this.moduleCache = null;
2435
2478
  }
2436
2479
 
2437
- /** Remote FS dependent methods: */
2480
+ // TODO: restructure this
2438
2481
 
2439
- async getModule(moduleId,
2440
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
2441
- _options) {
2442
- if (this.moduleCache) {
2443
- return this.moduleCache[moduleId];
2444
- }
2445
- throw new Error("Module cache not initialized");
2482
+ getSerializedModules() {
2483
+ return Promise.all(this.valModules.modules.map(({
2484
+ def
2485
+ }, i) => {
2486
+ return def().then(({
2487
+ default: valModule
2488
+ }) => {
2489
+ var _Internal$getSchema;
2490
+ const path = core.Internal.getValPath(valModule);
2491
+ if (!path) {
2492
+ throw Error(`Module defined at pos: ${i} is missing path`);
2493
+ }
2494
+ const source = core.Internal.getSource(valModule);
2495
+ if (!source) {
2496
+ // TODO
2497
+ throw Error(`Module defined at pos: ${i} is missing source`);
2498
+ }
2499
+ const schema = (_Internal$getSchema = core.Internal.getSchema(valModule)) === null || _Internal$getSchema === void 0 ? void 0 : _Internal$getSchema.serialize();
2500
+ if (!schema) {
2501
+ // TODO
2502
+ throw Error(`Module defined at pos: ${i} is missing schema`);
2503
+ }
2504
+ return {
2505
+ path,
2506
+ source: source,
2507
+ errors: false,
2508
+ //valModule[GetSchema]?.validate(path, source),
2509
+ schema: schema
2510
+ };
2511
+ });
2512
+ }));
2513
+ }
2514
+ getModule(moduleId) {
2515
+ // TODO: do not get all modules - we only should only get the ones we need
2516
+ return this.getSerializedModules().then(all => {
2517
+ const found = all.find(valModule => valModule.path === moduleId);
2518
+ if (!found) {
2519
+ throw Error(`Module ${moduleId} not found`);
2520
+ }
2521
+ return found;
2522
+ });
2446
2523
  }
2447
2524
  async getAllModules(treePath) {
2448
- if (!this.moduleCache) {
2449
- throw new Error("Module cache not initialized");
2450
- }
2451
- return Object.keys(this.moduleCache).filter(moduleId => moduleId.startsWith(treePath));
2525
+ const moduleIds = (await this.getSerializedModules()).filter(({
2526
+ path
2527
+ }) => {
2528
+ if (treePath) {
2529
+ return path.startsWith(treePath);
2530
+ }
2531
+ return true;
2532
+ }).map(({
2533
+ path
2534
+ }) => path);
2535
+ return moduleIds;
2452
2536
  }
2453
2537
  execCommit(patches, cookies) {
2454
2538
  return withAuth(this.options.valSecret, cookies, "execCommit", async ({
@@ -2489,67 +2573,14 @@ class ProxyValServer extends ValServer {
2489
2573
  }
2490
2574
  });
2491
2575
  }
2492
- async init(commit, token) {
2493
- const params = createParams({
2494
- root: this.apiOptions.root,
2495
- commit,
2496
- ext: ["ts", "js", "json"],
2497
- package: ["@valbuild/core@" + this.options.versions.core, "@valbuild/next@" + this.options.versions.next],
2498
- include: ["**/*.val.{js,ts},package.json,tsconfig.json,jsconfig.json"]
2499
- });
2500
- const url = new URL(`/v1/eval/${this.options.remote}/heads/${this.options.git.branch}/~?${params}`, this.options.valContentUrl);
2501
- try {
2502
- const fetchRes = await fetch(url, {
2503
- headers: getAuthHeaders(token, "application/json")
2504
- });
2505
- if (fetchRes.status === 200) {
2506
- const json = await fetchRes.json();
2507
- let error = false;
2508
- if (typeof json !== "object") {
2509
- error = {
2510
- details: "Invalid response: not an object"
2511
- };
2512
- }
2513
- if (typeof json.git !== "object") {
2514
- error = {
2515
- details: "Invalid response: missing git"
2516
- };
2517
- }
2518
- if (typeof json.git.commit !== "string") {
2519
- error = {
2520
- details: "Invalid response: missing git.commit"
2521
- };
2522
- }
2523
- if (typeof json.modules !== "object" || json.modules === null) {
2524
- error = {
2525
- details: "Invalid response: missing modules"
2526
- };
2527
- }
2528
- if (error) {
2529
- console.error("Could not initialize remote modules", error);
2530
- return {
2531
- status: 500,
2532
- json: {
2533
- message: "Failed to fetch remote modules",
2534
- ...error
2535
- }
2536
- };
2537
- }
2538
- this.moduleCache = json.modules;
2539
- return {
2540
- status: 200
2541
- };
2542
- } else {
2543
- return createJsonError(fetchRes);
2544
- }
2545
- } catch (err) {
2546
- return {
2547
- status: 500,
2548
- json: {
2549
- message: "Failed to fetch: check network connection"
2550
- }
2551
- };
2552
- }
2576
+ async init(
2577
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
2578
+ _commit,
2579
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
2580
+ _token) {
2581
+ return {
2582
+ status: 200
2583
+ };
2553
2584
  }
2554
2585
  async ensureInitialized(errorMessageType, cookies) {
2555
2586
  const commit = this.options.git.commit;
@@ -3118,13 +3149,13 @@ function createParams(params) {
3118
3149
  return paramsString;
3119
3150
  }
3120
3151
 
3121
- async function createValServer(route, opts, callbacks) {
3152
+ async function createValServer(valModules, route, opts, callbacks) {
3122
3153
  const serverOpts = await initHandlerOptions(route, opts);
3123
3154
  if (serverOpts.mode === "proxy") {
3124
3155
  const projectRoot = process.cwd(); //[process.cwd(), opts.root || ""] .filter((seg) => seg) .join("/");
3125
- return new ProxyValServer(projectRoot, serverOpts, opts, callbacks);
3156
+ return new ProxyValServer(projectRoot, valModules, serverOpts, opts, callbacks);
3126
3157
  } else {
3127
- return new LocalValServer(serverOpts, callbacks);
3158
+ return new LocalValServer(valModules, serverOpts, callbacks);
3128
3159
  }
3129
3160
  }
3130
3161
  async function initHandlerOptions(route, opts) {
@@ -1384,8 +1384,9 @@ function getValidationErrorMetadata(validationError) {
1384
1384
 
1385
1385
  const ops = new JSONOps();
1386
1386
  class ValServer {
1387
- constructor(cwd, options, callbacks) {
1387
+ constructor(cwd, valModules, options, callbacks) {
1388
1388
  this.cwd = cwd;
1389
+ this.valModules = valModules;
1389
1390
  this.options = options;
1390
1391
  this.callbacks = callbacks;
1391
1392
  }
@@ -2022,8 +2023,9 @@ const textEncoder = new TextEncoder();
2022
2023
  class LocalValServer extends ValServer {
2023
2024
  static PATCHES_DIR = "patches";
2024
2025
  static FILES_DIR = "files";
2025
- constructor(options, callbacks) {
2026
- super(options.service.sourceFileHandler.projectRoot, options, callbacks);
2026
+ constructor(valModules, options, callbacks) {
2027
+ super(options.service.sourceFileHandler.projectRoot, valModules, options, callbacks);
2028
+ this.valModules = valModules;
2027
2029
  this.options = options;
2028
2030
  this.callbacks = callbacks;
2029
2031
  this.patchesRootPath = options.cacheDir || path__default.join(options.service.sourceFileHandler.projectRoot, ".val");
@@ -2292,19 +2294,59 @@ class LocalValServer extends ValServer {
2292
2294
  // No RemoteFS so nothing to ensure
2293
2295
  return result.ok(undefined);
2294
2296
  }
2295
- getModule(moduleId, options) {
2296
- return this.options.service.get(moduleId, "", {
2297
- ...options,
2298
- validate: false
2297
+ getSerializedModules() {
2298
+ return Promise.all(this.valModules.modules.map(({
2299
+ def
2300
+ }, i) => {
2301
+ return def().then(({
2302
+ default: valModule
2303
+ }) => {
2304
+ var _Internal$getSchema;
2305
+ const path = Internal.getValPath(valModule);
2306
+ if (!path) {
2307
+ throw Error(`Module defined at pos: ${i} is missing path`);
2308
+ }
2309
+ const source = Internal.getSource(valModule);
2310
+ if (!source) {
2311
+ // TODO
2312
+ throw Error(`Module defined at pos: ${i} is missing source`);
2313
+ }
2314
+ const schema = (_Internal$getSchema = Internal.getSchema(valModule)) === null || _Internal$getSchema === void 0 ? void 0 : _Internal$getSchema.serialize();
2315
+ if (!schema) {
2316
+ // TODO
2317
+ throw Error(`Module defined at pos: ${i} is missing schema`);
2318
+ }
2319
+ return {
2320
+ path,
2321
+ source: source,
2322
+ errors: false,
2323
+ //valModule[GetSchema]?.validate(path, source),
2324
+ schema: schema
2325
+ };
2326
+ });
2327
+ }));
2328
+ }
2329
+ getModule(moduleId) {
2330
+ // TODO: do not get all modules - we only should only get the ones we need
2331
+ return this.getSerializedModules().then(all => {
2332
+ const found = all.find(valModule => valModule.path === moduleId);
2333
+ if (!found) {
2334
+ throw Error(`Module ${moduleId} not found`);
2335
+ }
2336
+ return found;
2299
2337
  });
2300
2338
  }
2301
2339
  async getAllModules(treePath) {
2302
- const moduleIds = this.host.readDirectory(this.cwd, ["ts", "js"], ["node_modules", ".*"], ["**/*.val.ts", "**/*.val.js"]).filter(file => {
2340
+ const moduleIds = (await this.getSerializedModules()).filter(({
2341
+ path
2342
+ }) => {
2303
2343
  if (treePath) {
2304
- return file.replace(this.cwd, "").startsWith(treePath);
2344
+ return path.startsWith(treePath);
2305
2345
  }
2306
2346
  return true;
2307
- }).map(file => file.replace(this.cwd, "").replace(".val.js", "").replace(".val.ts", "").split(path__default.sep).join("/"));
2347
+ }).map(({
2348
+ path
2349
+ }) => path);
2308
2350
  return moduleIds;
2309
2351
  }
2310
2352
  async execCommit(patches) {
@@ -2395,30 +2437,72 @@ function encodeJwt(payload, sessionKey) {
2395
2437
 
2396
2438
  class ProxyValServer extends ValServer {
2397
2439
  moduleCache = null;
2398
- constructor(cwd, options, apiOptions, callbacks) {
2399
- super(cwd, options, callbacks);
2440
+ constructor(cwd, valModules, options, apiOptions, callbacks) {
2441
+ super(cwd, valModules, options, callbacks);
2400
2442
  this.cwd = cwd;
2443
+ this.valModules = valModules;
2401
2444
  this.options = options;
2402
2445
  this.apiOptions = apiOptions;
2403
2446
  this.callbacks = callbacks;
2404
2447
  this.moduleCache = null;
2405
2448
  }
2406
2449
 
2407
- /** Remote FS dependent methods: */
2450
+ // TODO: restructure this
2408
2451
 
2409
- async getModule(moduleId,
2410
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
2411
- _options) {
2412
- if (this.moduleCache) {
2413
- return this.moduleCache[moduleId];
2414
- }
2415
- throw new Error("Module cache not initialized");
2452
+ getSerializedModules() {
2453
+ return Promise.all(this.valModules.modules.map(({
2454
+ def
2455
+ }, i) => {
2456
+ return def().then(({
2457
+ default: valModule
2458
+ }) => {
2459
+ var _Internal$getSchema;
2460
+ const path = Internal.getValPath(valModule);
2461
+ if (!path) {
2462
+ throw Error(`Module defined at pos: ${i} is missing path`);
2463
+ }
2464
+ const source = Internal.getSource(valModule);
2465
+ if (!source) {
2466
+ // TODO
2467
+ throw Error(`Module defined at pos: ${i} is missing source`);
2468
+ }
2469
+ const schema = (_Internal$getSchema = Internal.getSchema(valModule)) === null || _Internal$getSchema === void 0 ? void 0 : _Internal$getSchema.serialize();
2470
+ if (!schema) {
2471
+ // TODO
2472
+ throw Error(`Module defined at pos: ${i} is missing schema`);
2473
+ }
2474
+ return {
2475
+ path,
2476
+ source: source,
2477
+ errors: false,
2478
+ //valModule[GetSchema]?.validate(path, source),
2479
+ schema: schema
2480
+ };
2481
+ });
2482
+ }));
2483
+ }
2484
+ getModule(moduleId) {
2485
+ // TODO: do not get all modules - we only should only get the ones we need
2486
+ return this.getSerializedModules().then(all => {
2487
+ const found = all.find(valModule => valModule.path === moduleId);
2488
+ if (!found) {
2489
+ throw Error(`Module ${moduleId} not found`);
2490
+ }
2491
+ return found;
2492
+ });
2416
2493
  }
2417
2494
  async getAllModules(treePath) {
2418
- if (!this.moduleCache) {
2419
- throw new Error("Module cache not initialized");
2420
- }
2421
- return Object.keys(this.moduleCache).filter(moduleId => moduleId.startsWith(treePath));
2495
+ const moduleIds = (await this.getSerializedModules()).filter(({
2496
+ path
2497
+ }) => {
2498
+ if (treePath) {
2499
+ return path.startsWith(treePath);
2500
+ }
2501
+ return true;
2502
+ }).map(({
2503
+ path
2504
+ }) => path);
2505
+ return moduleIds;
2422
2506
  }
2423
2507
  execCommit(patches, cookies) {
2424
2508
  return withAuth(this.options.valSecret, cookies, "execCommit", async ({
@@ -2459,67 +2543,14 @@ class ProxyValServer extends ValServer {
2459
2543
  }
2460
2544
  });
2461
2545
  }
2462
- async init(commit, token) {
2463
- const params = createParams({
2464
- root: this.apiOptions.root,
2465
- commit,
2466
- ext: ["ts", "js", "json"],
2467
- package: ["@valbuild/core@" + this.options.versions.core, "@valbuild/next@" + this.options.versions.next],
2468
- include: ["**/*.val.{js,ts},package.json,tsconfig.json,jsconfig.json"]
2469
- });
2470
- const url = new URL(`/v1/eval/${this.options.remote}/heads/${this.options.git.branch}/~?${params}`, this.options.valContentUrl);
2471
- try {
2472
- const fetchRes = await fetch(url, {
2473
- headers: getAuthHeaders(token, "application/json")
2474
- });
2475
- if (fetchRes.status === 200) {
2476
- const json = await fetchRes.json();
2477
- let error = false;
2478
- if (typeof json !== "object") {
2479
- error = {
2480
- details: "Invalid response: not an object"
2481
- };
2482
- }
2483
- if (typeof json.git !== "object") {
2484
- error = {
2485
- details: "Invalid response: missing git"
2486
- };
2487
- }
2488
- if (typeof json.git.commit !== "string") {
2489
- error = {
2490
- details: "Invalid response: missing git.commit"
2491
- };
2492
- }
2493
- if (typeof json.modules !== "object" || json.modules === null) {
2494
- error = {
2495
- details: "Invalid response: missing modules"
2496
- };
2497
- }
2498
- if (error) {
2499
- console.error("Could not initialize remote modules", error);
2500
- return {
2501
- status: 500,
2502
- json: {
2503
- message: "Failed to fetch remote modules",
2504
- ...error
2505
- }
2506
- };
2507
- }
2508
- this.moduleCache = json.modules;
2509
- return {
2510
- status: 200
2511
- };
2512
- } else {
2513
- return createJsonError(fetchRes);
2514
- }
2515
- } catch (err) {
2516
- return {
2517
- status: 500,
2518
- json: {
2519
- message: "Failed to fetch: check network connection"
2520
- }
2521
- };
2522
- }
2546
+ async init(
2547
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
2548
+ _commit,
2549
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
2550
+ _token) {
2551
+ return {
2552
+ status: 200
2553
+ };
2523
2554
  }
2524
2555
  async ensureInitialized(errorMessageType, cookies) {
2525
2556
  const commit = this.options.git.commit;
@@ -3088,13 +3119,13 @@ function createParams(params) {
3088
3119
  return paramsString;
3089
3120
  }
3090
3121
 
3091
- async function createValServer(route, opts, callbacks) {
3122
+ async function createValServer(valModules, route, opts, callbacks) {
3092
3123
  const serverOpts = await initHandlerOptions(route, opts);
3093
3124
  if (serverOpts.mode === "proxy") {
3094
3125
  const projectRoot = process.cwd(); //[process.cwd(), opts.root || ""] .filter((seg) => seg) .join("/");
3095
- return new ProxyValServer(projectRoot, serverOpts, opts, callbacks);
3126
+ return new ProxyValServer(projectRoot, valModules, serverOpts, opts, callbacks);
3096
3127
  } else {
3097
- return new LocalValServer(serverOpts, callbacks);
3128
+ return new LocalValServer(valModules, serverOpts, callbacks);
3098
3129
  }
3099
3130
  }
3100
3131
  async function initHandlerOptions(route, opts) {
package/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
  "./package.json": "./package.json"
13
13
  },
14
14
  "types": "dist/valbuild-server.cjs.d.ts",
15
- "version": "0.60.27",
15
+ "version": "0.61.0",
16
16
  "scripts": {
17
17
  "typecheck": "tsc --noEmit",
18
18
  "test": "jest",
@@ -24,9 +24,9 @@
24
24
  "concurrently": "^7.6.0"
25
25
  },
26
26
  "dependencies": {
27
- "@valbuild/core": "~0.60.27",
28
- "@valbuild/shared": "~0.60.27",
29
- "@valbuild/ui": "~0.60.27",
27
+ "@valbuild/core": "~0.61.0",
28
+ "@valbuild/shared": "~0.61.0",
29
+ "@valbuild/ui": "~0.61.0",
30
30
  "express": "^4.18.2",
31
31
  "image-size": "^1.0.2",
32
32
  "minimatch": "^3.0.4",