@valbuild/server 0.58.0 → 0.60.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.
@@ -1,38 +1,54 @@
1
+ /// <reference types="node" />
1
2
  import { Service } from "./Service.js";
2
- import { ApiGetPatchResponse, ApiPostPatchResponse, ApiTreeResponse } from "@valbuild/core";
3
- import { VAL_ENABLE_COOKIE_NAME, VAL_SESSION_COOKIE, VAL_STATE_COOKIE, ValServerJsonResult, ValServerRedirectResult, ValServerResult, ValSession } from "@valbuild/shared/internal";
3
+ import { result } from "@valbuild/core/fp";
4
+ import { Patch } from "./patch/validation.js";
5
+ import { ApiGetPatchResponse, ApiPostPatchResponse, ModuleId, PatchId, ApiDeletePatchResponse } from "@valbuild/core";
6
+ import { VAL_ENABLE_COOKIE_NAME, VAL_SESSION_COOKIE, VAL_STATE_COOKIE, ValServerError, ValServerJsonResult, ValServerRedirectResult, ValServerResult, ValSession } from "@valbuild/shared/internal";
4
7
  import { ValServer, ValServerCallbacks } from "./ValServer.js";
8
+ import { SerializedModuleContent } from "./SerializedModuleContent.js";
5
9
  export type LocalValServerOptions = {
6
10
  service: Service;
7
11
  valEnableRedirectUrl?: string;
8
12
  valDisableRedirectUrl?: string;
13
+ cacheDir?: string;
9
14
  git: {
10
15
  commit?: string;
11
16
  branch?: string;
12
17
  };
13
18
  };
14
- export declare class LocalValServer implements ValServer {
19
+ export interface ValServerBufferHost {
20
+ readBuffer: (fileName: string) => Promise<Buffer | undefined>;
21
+ }
22
+ export declare class LocalValServer extends ValServer {
15
23
  readonly options: LocalValServerOptions;
16
24
  readonly callbacks: ValServerCallbacks;
25
+ private static readonly PATCHES_DIR;
26
+ private static readonly FILES_DIR;
27
+ private readonly patchesRootPath;
17
28
  constructor(options: LocalValServerOptions, callbacks: ValServerCallbacks);
18
29
  session(): Promise<ValServerJsonResult<ValSession>>;
19
- getTree(treePath: string, query: {
20
- patch?: string;
21
- schema?: string;
22
- source?: string;
23
- }): Promise<ValServerJsonResult<ApiTreeResponse>>;
24
- enable(query: {
25
- redirect_to?: string;
26
- }): Promise<ValServerRedirectResult<VAL_ENABLE_COOKIE_NAME>>;
27
- disable(query: {
28
- redirect_to?: string;
29
- }): Promise<ValServerRedirectResult<VAL_ENABLE_COOKIE_NAME>>;
30
+ deletePatches(query: {
31
+ id?: string[];
32
+ }): Promise<ValServerJsonResult<ApiDeletePatchResponse>>;
30
33
  postPatches(body: unknown): Promise<ValServerJsonResult<ApiPostPatchResponse>>;
34
+ getFiles(filePath: string, query: {
35
+ sha256?: string;
36
+ }): Promise<ValServerResult<never, ReadableStream<Uint8Array>>>;
37
+ getPatches(query: {
38
+ id?: string[];
39
+ }): Promise<ValServerJsonResult<ApiGetPatchResponse>>;
40
+ private getFilePath;
41
+ private getFileMetadataPath;
42
+ private getPatchFilePath;
31
43
  private badRequest;
32
- postCommit(): Promise<ValServerJsonResult<{}>>;
44
+ protected ensureRemoteFSInitialized(): Promise<result.Result<undefined, ValServerError>>;
45
+ protected getModule(moduleId: ModuleId): Promise<SerializedModuleContent>;
46
+ protected execCommit(patches: [PatchId, ModuleId, Patch][]): Promise<Record<ModuleId, {
47
+ patches: {
48
+ applied: PatchId[];
49
+ };
50
+ }>>;
33
51
  authorize(): Promise<ValServerRedirectResult<VAL_STATE_COOKIE>>;
34
52
  callback(): Promise<ValServerRedirectResult<VAL_STATE_COOKIE | VAL_SESSION_COOKIE | VAL_ENABLE_COOKIE_NAME>>;
35
53
  logout(): Promise<ValServerResult<VAL_STATE_COOKIE | VAL_SESSION_COOKIE>>;
36
- getFiles(): Promise<ValServerResult<never, ReadableStream<Uint8Array>>>;
37
- getPatches(): Promise<ValServerJsonResult<ApiGetPatchResponse>>;
38
54
  }
@@ -1,7 +1,5 @@
1
1
  import { type Source, type SerializedSchema, ValidationErrors } from "@valbuild/core";
2
- import type { ModuleId, SourcePath } from "@valbuild/core";
3
- export declare const FATAL_ERROR_TYPES: readonly ["no-schema", "no-source", "invalid-id", "no-module"];
4
- export type FatalErrorType = (typeof FATAL_ERROR_TYPES)[number];
2
+ import type { FatalErrorType, ModuleId, SourcePath } from "@valbuild/core";
5
3
  export type SerializedModuleContent = {
6
4
  source: Source;
7
5
  schema: SerializedSchema;
@@ -21,11 +21,11 @@ export type ServiceOptions = {
21
21
  };
22
22
  export declare function createService(projectRoot: string, opts: ServiceOptions, host?: IValFSHost, loader?: ValModuleLoader): Promise<Service>;
23
23
  export declare class Service {
24
- private readonly sourceFileHandler;
24
+ readonly sourceFileHandler: ValSourceFileHandler;
25
25
  private readonly runtime;
26
26
  readonly valConfigPath: string;
27
27
  constructor({ valConfigPath }: ServiceOptions, sourceFileHandler: ValSourceFileHandler, runtime: QuickJSRuntime);
28
- get(moduleId: ModuleId, modulePath: ModulePath): Promise<SerializedModuleContent>;
29
- patch(moduleId: string, patch: Patch): Promise<void>;
28
+ get(moduleId: ModuleId, modulePath?: ModulePath): Promise<SerializedModuleContent>;
29
+ patch(moduleId: ModuleId, patch: Patch): Promise<void>;
30
30
  dispose(): void;
31
31
  }
@@ -1,3 +1,4 @@
1
+ /// <reference types="node" />
1
2
  /**
2
3
  * A filesystem that can update and read files.
3
4
  * It does not support creating new files or directories.
@@ -5,8 +6,9 @@
5
6
  */
6
7
  export interface ValFS {
7
8
  readDirectory(rootDir: string, extensions: readonly string[], excludes: readonly string[] | undefined, includes: readonly string[], depth?: number | undefined): readonly string[];
8
- writeFile(filePath: string, data: string, encoding: "binary" | "utf8"): void;
9
+ writeFile(filePath: string, data: string | Buffer, encoding: "binary" | "utf8"): void;
9
10
  fileExists(filePath: string): boolean;
10
11
  readFile(filePath: string): string | undefined;
12
+ rmFile(filePath: string): void;
11
13
  realpath(path: string): string;
12
14
  }
@@ -1,3 +1,4 @@
1
+ /// <reference types="node" />
1
2
  import ts from "typescript";
2
3
  import { ValFS } from "./ValFS.js";
3
4
  /**
@@ -6,7 +7,8 @@ import { ValFS } from "./ValFS.js";
6
7
  */
7
8
  export interface IValFSHost extends ts.ParseConfigHost, ts.ModuleResolutionHost {
8
9
  useCaseSensitiveFileNames: boolean;
9
- writeFile(fileName: string, text: string, encoding: "binary" | "utf8"): void;
10
+ writeFile(fileName: string, data: string | Buffer, encoding: "binary" | "utf8"): void;
11
+ rmFile(fileName: string): void;
10
12
  }
11
13
  export declare class ValFSHost implements IValFSHost {
12
14
  protected readonly valFS: ValFS;
@@ -14,7 +16,8 @@ export declare class ValFSHost implements IValFSHost {
14
16
  constructor(valFS: ValFS, currentDirectory: string);
15
17
  useCaseSensitiveFileNames: boolean;
16
18
  readDirectory(rootDir: string, extensions: readonly string[], excludes: readonly string[] | undefined, includes: readonly string[], depth?: number | undefined): readonly string[];
17
- writeFile(fileName: string, text: string, encoding: "binary" | "utf8"): void;
19
+ rmFile(fileName: string): void;
20
+ writeFile(fileName: string, text: string | Buffer, encoding: "binary" | "utf8"): void;
18
21
  getCurrentDirectory(): string;
19
22
  getCanonicalFileName(fileName: string): string;
20
23
  fileExists(fileName: string): boolean;
@@ -1,5 +1,89 @@
1
- import { ApiGetPatchResponse, ApiPostPatchResponse, ApiTreeResponse } from "@valbuild/core";
1
+ /// <reference types="node" />
2
+ import { ApiCommitResponse, ApiGetPatchResponse, ApiPostPatchResponse, ApiPostValidationErrorResponse, ApiTreeResponse, ApiDeletePatchResponse, ApiPostValidationResponse } from "@valbuild/core";
2
3
  import { VAL_ENABLE_COOKIE_NAME, VAL_SESSION_COOKIE, VAL_STATE_COOKIE, ValCookies, ValServerError, ValServerJsonResult, ValServerRedirectResult, ValServerResult, ValSession } from "@valbuild/shared/internal";
4
+ import { result } from "@valbuild/core/fp";
5
+ import { Patch } from "./patch/validation.js";
6
+ import { ModuleId, PatchId } from "@valbuild/core";
7
+ import { SerializedModuleContent } from "./SerializedModuleContent.js";
8
+ import { IValFSHost } from "./ValFSHost.js";
9
+ export type ValServerOptions = {
10
+ valEnableRedirectUrl?: string;
11
+ valDisableRedirectUrl?: string;
12
+ git: {
13
+ commit?: string;
14
+ branch?: string;
15
+ };
16
+ };
17
+ export declare abstract class ValServer implements IValServer {
18
+ readonly cwd: string;
19
+ readonly host: IValFSHost;
20
+ readonly options: ValServerOptions;
21
+ readonly callbacks: ValServerCallbacks;
22
+ constructor(cwd: string, host: IValFSHost, options: ValServerOptions, callbacks: ValServerCallbacks);
23
+ enable(query: {
24
+ redirect_to?: string;
25
+ }): Promise<ValServerRedirectResult<VAL_ENABLE_COOKIE_NAME>>;
26
+ disable(query: {
27
+ redirect_to?: string;
28
+ }): Promise<ValServerRedirectResult<VAL_ENABLE_COOKIE_NAME>>;
29
+ private getAllModules;
30
+ getTree(treePath: string, query: {
31
+ patch?: string;
32
+ schema?: string;
33
+ source?: string;
34
+ }, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ApiTreeResponse>>;
35
+ postValidate(rawBody: unknown, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ApiPostValidationResponse | ApiPostValidationErrorResponse>>;
36
+ postCommit(rawBody: unknown, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ApiCommitResponse, ApiPostValidationErrorResponse>>;
37
+ private applyAllPatchesThenValidate;
38
+ private revalidateImageAndFileValidation;
39
+ protected sortPatchIds(patchesByModule: Record<ModuleId, {
40
+ patch: Patch;
41
+ patch_id: PatchId;
42
+ created_at: string;
43
+ commit_sha?: string;
44
+ author?: string;
45
+ }[]>): PatchId[];
46
+ protected readStaticBinaryFile(filePath: string): Promise<Buffer | undefined>;
47
+ private readPatches;
48
+ private validateThenMaybeCommit;
49
+ protected getPatchedModules(patches: [PatchId, ModuleId, Patch][]): Promise<Record<ModuleId, {
50
+ patches: {
51
+ applied: PatchId[];
52
+ };
53
+ }>>;
54
+ /**
55
+ * Runs before remoteFS dependent methods (e.g.getModule, ...) are called to make sure that:
56
+ * 1) The remote FS, if applicable, is initialized
57
+ * 2) The error is returned via API if the remote FS could not be initialized
58
+ * */
59
+ protected abstract ensureRemoteFSInitialized(errorMessageType: string, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<result.Result<undefined, ValServerError>>;
60
+ protected abstract getModule(moduleId: ModuleId): Promise<SerializedModuleContent>;
61
+ protected abstract execCommit(patches: [PatchId, ModuleId, Patch][], cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<Record<ModuleId, {
62
+ patches: {
63
+ applied: PatchId[];
64
+ };
65
+ }>>;
66
+ abstract getFiles(filePath: string, query: {
67
+ sha256?: string;
68
+ }, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerResult<never, ReadableStream<Uint8Array>>>;
69
+ abstract session(cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ValSession>>;
70
+ abstract authorize(query: {
71
+ redirect_to?: string;
72
+ }): Promise<ValServerRedirectResult<VAL_STATE_COOKIE>>;
73
+ abstract logout(): Promise<ValServerResult<VAL_STATE_COOKIE | VAL_SESSION_COOKIE>>;
74
+ abstract callback(query: {
75
+ code?: string;
76
+ state?: string;
77
+ }, cookies: ValCookies<VAL_STATE_COOKIE>): Promise<ValServerRedirectResult<VAL_STATE_COOKIE | VAL_SESSION_COOKIE | VAL_ENABLE_COOKIE_NAME>>;
78
+ abstract deletePatches(query: {
79
+ id?: string[];
80
+ }, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ApiDeletePatchResponse>>;
81
+ abstract postPatches(body: unknown, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ApiPostPatchResponse>>;
82
+ abstract getPatches(query: {
83
+ id?: string[];
84
+ }, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ApiGetPatchResponse>>;
85
+ }
86
+ export declare function bufferToReadableStream(buffer: Buffer): ReadableStream<any>;
3
87
  export declare const ENABLE_COOKIE_VALUE: {
4
88
  readonly value: "true";
5
89
  readonly options: {
@@ -10,12 +94,18 @@ export declare const ENABLE_COOKIE_VALUE: {
10
94
  export declare function getRedirectUrl(query: {
11
95
  redirect_to?: string | undefined;
12
96
  }, overrideHost: string | undefined): string | ValServerError;
97
+ export declare function getMimeTypeFromBase64(content: string): string | null;
98
+ export declare function bufferFromDataUrl(dataUrl: string, contentType: string | null): Buffer | undefined;
99
+ export type PatchFileMetadata = {
100
+ mimeType: string;
101
+ sha256: string;
102
+ };
13
103
  export type ValServerCallbacks = {
14
104
  isEnabled: () => Promise<boolean>;
15
105
  onEnable: (success: boolean) => Promise<void>;
16
106
  onDisable: (success: boolean) => Promise<void>;
17
107
  };
18
- export interface ValServer {
108
+ export interface IValServer {
19
109
  authorize(query: {
20
110
  redirect_to?: string;
21
111
  }): Promise<ValServerRedirectResult<VAL_STATE_COOKIE>>;
@@ -40,8 +130,13 @@ export interface ValServer {
40
130
  id?: string[];
41
131
  }, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ApiGetPatchResponse>>;
42
132
  postPatches(body: unknown, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ApiPostPatchResponse>>;
43
- postCommit(body: unknown, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<{}>>;
44
- getFiles(treePath: string, query: {
133
+ deletePatches(query: {
134
+ id?: string[];
135
+ }, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ApiDeletePatchResponse>>;
136
+ postValidate(body: unknown, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ApiPostValidationResponse | ApiPostValidationErrorResponse>>;
137
+ postCommit(body: unknown, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ApiCommitResponse, ApiPostValidationErrorResponse>>;
138
+ getFiles(filePath: string, query: {
45
139
  sha256?: string;
46
140
  }, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerResult<never, ReadableStream<Uint8Array>>>;
47
141
  }
142
+ export declare function guessMimeTypeFromPath(filePath: string): string | null;
@@ -1,9 +1,9 @@
1
1
  import ts from "typescript";
2
2
  import { IValFSHost } from "./ValFSHost.js";
3
3
  export declare class ValSourceFileHandler {
4
- private readonly projectRoot;
4
+ readonly projectRoot: string;
5
5
  private readonly compilerOptions;
6
- private readonly host;
6
+ readonly host: IValFSHost;
7
7
  constructor(projectRoot: string, compilerOptions: ts.CompilerOptions, host?: IValFSHost);
8
8
  getSourceFile(filePath: string): ts.SourceFile | undefined;
9
9
  writeSourceFile(sourceFile: ts.SourceFile): void;
@@ -1,7 +1,8 @@
1
1
  import { ServiceOptions } from "./Service.js";
2
- import { ValServer, ValServerCallbacks } from "./ValServer.js";
2
+ import { IValServer, ValServerCallbacks } from "./ValServer.js";
3
+ import { ValConfig } from "@valbuild/core";
3
4
  import { ValServerGenericResult } from "@valbuild/shared/internal";
4
- type Opts = ValServerOverrides & ServiceOptions;
5
+ export type ValApiOptions = ValServerOverrides & ServiceOptions & ValConfig;
5
6
  type ValServerOverrides = Partial<{
6
7
  /**
7
8
  * Override the Val API key.
@@ -102,11 +103,15 @@ type ValServerOverrides = Partial<{
102
103
  * @example "/api/draft/enable"
103
104
  */
104
105
  valDisableRedirectUrl?: string;
106
+ /**
107
+ * Disable the cache.
108
+ */
109
+ disableCache?: boolean;
105
110
  }>;
106
- export declare function createValServer(route: string, opts: Opts, callbacks: ValServerCallbacks): Promise<ValServer>;
111
+ export declare function createValServer(route: string, opts: ValApiOptions, callbacks: ValServerCallbacks): Promise<IValServer>;
107
112
  export declare function safeReadGit(cwd: string): Promise<{
108
113
  commit?: string;
109
114
  branch?: string;
110
115
  }>;
111
- export declare function createValApiRouter<Res>(route: string, valServerPromise: Promise<ValServer>, convert: (valServerRes: ValServerGenericResult) => Res): (req: Request) => Promise<Res>;
116
+ export declare function createValApiRouter<Res>(route: string, valServerPromise: Promise<IValServer>, convert: (valServerRes: ValServerGenericResult) => Res): (req: Request) => Promise<Res>;
112
117
  export {};
@@ -11,5 +11,5 @@ export { patchSourceFile } from "./patchValFile.js";
11
11
  export { formatSyntaxErrorTree } from "./patch/ts/syntax.js";
12
12
  export { LocalValServer } from "./LocalValServer.js";
13
13
  export { createFixPatch } from "./createFixPatch.js";
14
- export { PatchJSON } from "./patch/validation.js";
14
+ export { PatchJSON, Patch } from "./patch/validation.js";
15
15
  export * from "./jwt.js";
@@ -1,4 +1,6 @@
1
- import type { PatchJSON as PatchJSONT } from "@valbuild/core/patch";
1
+ import type { PatchJSON as PatchJSONT, Patch as PatchT } from "@valbuild/core/patch";
2
2
  import z from "zod";
3
3
  export declare const PatchJSON: z.ZodType<PatchJSONT>;
4
4
  export type PatchJSON = PatchJSONT;
5
+ export declare const Patch: z.ZodType<PatchT>;
6
+ export type Patch = PatchT;