@valbuild/server 0.61.0 → 0.62.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,5 +1,5 @@
1
1
  import { type Source, type SerializedSchema, ValidationErrors } from "@valbuild/core";
2
- import type { FatalErrorType, ModuleId, SourcePath } from "@valbuild/core";
2
+ import type { FatalErrorType, ModuleFilePath, SourcePath } from "@valbuild/core";
3
3
  export type SerializedModuleContent = {
4
4
  source: Source;
5
5
  schema: SerializedSchema;
@@ -10,7 +10,7 @@ export type SerializedModuleContent = {
10
10
  schema?: SerializedSchema;
11
11
  path: SourcePath;
12
12
  errors: {
13
- invalidModuleId?: ModuleId;
13
+ invalidModulePath?: ModuleFilePath;
14
14
  validation?: ValidationErrors;
15
15
  fatal?: {
16
16
  message: string;
@@ -4,7 +4,7 @@ import { ValModuleLoader } from "./ValModuleLoader.js";
4
4
  import { ValSourceFileHandler } from "./ValSourceFileHandler.js";
5
5
  import { IValFSHost } from "./ValFSHost.js";
6
6
  import { SerializedModuleContent } from "./SerializedModuleContent.js";
7
- import { ModuleId, ModulePath } from "@valbuild/core";
7
+ import { ModuleFilePath, ModulePath } from "@valbuild/core";
8
8
  export type ServiceOptions = {
9
9
  /**
10
10
  * Disable cache for transpilation
@@ -19,11 +19,11 @@ export declare class Service {
19
19
  private readonly runtime;
20
20
  readonly projectRoot: string;
21
21
  constructor(projectRoot: string, sourceFileHandler: ValSourceFileHandler, runtime: QuickJSRuntime);
22
- get(moduleId: ModuleId, modulePath: ModulePath, options?: {
22
+ get(moduleFilePath: ModuleFilePath, modulePath: ModulePath, options?: {
23
23
  validate: boolean;
24
24
  source: boolean;
25
25
  schema: boolean;
26
26
  }): Promise<SerializedModuleContent>;
27
- patch(moduleId: ModuleId, patch: Patch): Promise<void>;
27
+ patch(moduleFilePath: ModuleFilePath, patch: Patch): Promise<void>;
28
28
  dispose(): void;
29
29
  }
@@ -1,96 +1,112 @@
1
1
  /// <reference types="node" />
2
- import { ApiCommitResponse, ApiGetPatchResponse, ApiPostPatchResponse, ApiPostValidationErrorResponse, ApiTreeResponse, ApiDeletePatchResponse, ApiPostValidationResponse, ValModules } from "@valbuild/core";
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 { Operation } from "@valbuild/core/patch";
6
- import { Patch } from "./patch/validation.js";
7
- import { ModuleId, PatchId, FileMetadata, ImageMetadata } from "@valbuild/core";
8
- import { SerializedModuleContent } from "./SerializedModuleContent.js";
2
+ import { ApiCommitResponse, ApiGetPatchResponse, ApiPostValidationErrorResponse, ApiTreeResponse, ApiDeletePatchResponse, ValModules, ApiSchemaResponse } from "@valbuild/core";
3
+ import { VAL_ENABLE_COOKIE_NAME, VAL_SESSION_COOKIE, VAL_STATE_COOKIE, ValCookies, ValServerError, ValServerJsonResult, ValServerRedirectResult, ValServerResult, ValServerResultCookies, ValSession } from "@valbuild/shared/internal";
4
+ import { z } from "zod";
9
5
  export type ValServerOptions = {
6
+ route: string;
10
7
  valEnableRedirectUrl?: string;
11
8
  valDisableRedirectUrl?: string;
12
- git: {
13
- commit?: string;
14
- branch?: string;
15
- };
9
+ formatter?: (code: string, filePath: string) => string;
10
+ valBuildUrl?: string;
11
+ valSecret?: string;
12
+ apiKey?: string;
13
+ project?: string;
16
14
  };
17
- export declare abstract class ValServer implements IValServer {
18
- readonly cwd: string;
15
+ export type ValServerConfig = ValServerOptions & ({
16
+ mode: "fs";
17
+ cwd: string;
18
+ formatter?: (code: string, filePath: string) => string;
19
+ } | {
20
+ mode: "http";
21
+ valContentUrl: string;
22
+ apiKey: string;
23
+ project: string;
24
+ commit: string;
25
+ branch: string;
26
+ root?: string;
27
+ });
28
+ export declare class ValServer {
19
29
  readonly valModules: ValModules;
20
- readonly options: ValServerOptions;
30
+ private readonly options;
21
31
  readonly callbacks: ValServerCallbacks;
22
- constructor(cwd: string, valModules: ValModules, options: ValServerOptions, callbacks: ValServerCallbacks);
32
+ private serverOps;
33
+ constructor(valModules: ValModules, options: ValServerConfig, callbacks: ValServerCallbacks);
23
34
  enable(query: {
24
35
  redirect_to?: string;
25
36
  }): Promise<ValServerRedirectResult<VAL_ENABLE_COOKIE_NAME>>;
26
37
  disable(query: {
27
38
  redirect_to?: string;
28
39
  }): Promise<ValServerRedirectResult<VAL_ENABLE_COOKIE_NAME>>;
29
- getTree(treePath: string, query: {
30
- patch?: string;
31
- schema?: string;
32
- source?: string;
33
- }, cookies: ValCookies<VAL_SESSION_COOKIE>, requestHeaders: RequestHeaders): Promise<ValServerJsonResult<ApiTreeResponse>>;
34
- postValidate(rawBody: unknown, cookies: ValCookies<VAL_SESSION_COOKIE>, requestHeaders: RequestHeaders): Promise<ValServerJsonResult<ApiPostValidationResponse | ApiPostValidationErrorResponse>>;
35
- postCommit(rawBody: unknown, cookies: ValCookies<VAL_SESSION_COOKIE>, requestHeaders: RequestHeaders): Promise<ValServerJsonResult<ApiCommitResponse, ApiPostValidationErrorResponse>>;
36
- private applyAllPatchesThenValidate;
37
- abstract getMetadata(fileRef: string, sha256?: string): Promise<FileMetadata | ImageMetadata | undefined>;
38
- private revalidateImageAndFileValidation;
39
- protected abstract getAllModules(treePath: string): Promise<ModuleId[]>;
40
- protected sortPatchIds(patchesByModule: Record<ModuleId, {
41
- patch: Patch;
42
- patch_id: PatchId;
43
- created_at: string;
44
- commit_sha?: string;
45
- author?: string;
46
- }[]>): PatchId[];
47
- protected readStaticBinaryFile(filePath: string): Promise<Buffer | undefined>;
48
- private readPatches;
49
- private validateThenMaybeCommit;
50
- protected getPatchedModules(patches: [PatchId, ModuleId, Patch][]): Promise<Record<ModuleId, {
51
- patches: {
52
- applied: PatchId[];
53
- };
54
- }>>;
55
- /**
56
- * Runs before remoteFS dependent methods (e.g.getModule, ...) are called to make sure that:
57
- * 1) The remote FS, if applicable, is initialized
58
- * 2) The error is returned via API if the remote FS could not be initialized
59
- * */
60
- protected abstract ensureInitialized(errorMessageType: string, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<result.Result<undefined, ValServerError>>;
61
- protected abstract getModule(moduleId: ModuleId, options: {
62
- source: boolean;
63
- schema: boolean;
64
- }): Promise<SerializedModuleContent>;
65
- protected abstract execCommit(patches: [PatchId, ModuleId, Patch][], cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<{
66
- status: 200;
67
- json: Record<ModuleId, {
68
- patches: {
69
- applied: PatchId[];
70
- };
71
- }>;
72
- } | ValServerError>;
73
- abstract getFiles(filePath: string, query: {
74
- sha256?: string;
75
- }, cookies: ValCookies<VAL_SESSION_COOKIE>, requestHeaders: RequestHeaders): Promise<ValServerResult<never, ReadableStream<Uint8Array>> | ValServerRedirectResult<VAL_ENABLE_COOKIE_NAME>>;
76
- abstract session(cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ValSession>>;
77
- abstract authorize(query: {
40
+ authorize(query: {
78
41
  redirect_to?: string;
79
42
  }): Promise<ValServerRedirectResult<VAL_STATE_COOKIE>>;
80
- abstract logout(): Promise<ValServerResult<VAL_STATE_COOKIE | VAL_SESSION_COOKIE>>;
81
- abstract callback(query: {
43
+ callback(query: {
82
44
  code?: string;
83
45
  state?: string;
84
- }, cookies: ValCookies<VAL_STATE_COOKIE>): Promise<ValServerRedirectResult<VAL_STATE_COOKIE | VAL_SESSION_COOKIE | VAL_ENABLE_COOKIE_NAME>>;
85
- abstract deletePatches(query: {
86
- id?: string[];
87
- }, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ApiDeletePatchResponse>>;
88
- abstract postPatches(body: unknown, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ApiPostPatchResponse>>;
89
- abstract getPatches(query: {
46
+ }, cookies: ValCookies<"val_state">): Promise<ValServerRedirectResult<"val_state" | "val_session" | "val_enable">>;
47
+ log(): Promise<ValServerResult<VAL_SESSION_COOKIE | VAL_STATE_COOKIE>>;
48
+ session(cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ValSession>>;
49
+ private consumeCode;
50
+ private getAuthorizeUrl;
51
+ private getAppErrorUrl;
52
+ getAuth(cookies: Partial<Record<"val_session", string>>): {
53
+ error: string;
54
+ } | {
55
+ id: string;
56
+ error?: undefined;
57
+ } | {
58
+ error: null;
59
+ id: null;
60
+ };
61
+ logout(): Promise<ValServerResult<VAL_SESSION_COOKIE | VAL_STATE_COOKIE>>;
62
+ getPatches(query: {
63
+ authors?: string[];
64
+ }, cookies: Partial<Record<"val_session", string>>): Promise<ValServerJsonResult<ApiGetPatchResponse>>;
65
+ deletePatches(query: {
90
66
  id?: string[];
91
- }, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ApiGetPatchResponse>>;
67
+ }, cookies: Partial<Record<"val_session", string>>): Promise<ValServerJsonResult<ApiDeletePatchResponse>>;
68
+ getSchema(cookies: Partial<Record<"val_session", string>>): Promise<ValServerJsonResult<ApiSchemaResponse>>;
69
+ putTree(body: unknown, treePath: string, query: {
70
+ patches_sha?: string;
71
+ validate_all?: string;
72
+ validate_sources?: string;
73
+ validate_binary_files?: string;
74
+ }, cookies: Partial<Record<"val_session", string>>): Promise<ValServerJsonResult<ApiTreeResponse>>;
75
+ postSave(body: unknown, cookies: Partial<Record<"val_session", string>>): Promise<ValServerJsonResult<ApiCommitResponse, ApiPostValidationErrorResponse>>;
76
+ getFiles(filePath: string, query: {
77
+ patch_id?: string;
78
+ }): Promise<ValServerError | {
79
+ status: 200 | 201;
80
+ headers?: Record<string, string>;
81
+ cookies?: ValServerResultCookies<never>;
82
+ body?: ReadableStream<Uint8Array>;
83
+ }>;
92
84
  }
93
- export declare function bufferToReadableStream(buffer: Buffer): ReadableStream<any>;
85
+ export type ValServerCallbacks = {
86
+ isEnabled: () => Promise<boolean>;
87
+ onEnable: (success: boolean) => Promise<void>;
88
+ onDisable: (success: boolean) => Promise<void>;
89
+ };
90
+ declare const IntegratedServerJwtPayload: z.ZodObject<{
91
+ sub: z.ZodString;
92
+ exp: z.ZodNumber;
93
+ token: z.ZodString;
94
+ org: z.ZodString;
95
+ project: z.ZodString;
96
+ }, "strip", z.ZodTypeAny, {
97
+ project: string;
98
+ sub: string;
99
+ exp: number;
100
+ token: string;
101
+ org: string;
102
+ }, {
103
+ project: string;
104
+ sub: string;
105
+ exp: number;
106
+ token: string;
107
+ org: string;
108
+ }>;
109
+ export type IntegratedServerJwtPayload = z.infer<typeof IntegratedServerJwtPayload>;
94
110
  export declare const ENABLE_COOKIE_VALUE: {
95
111
  readonly value: "true";
96
112
  readonly options: {
@@ -98,66 +114,9 @@ export declare const ENABLE_COOKIE_VALUE: {
98
114
  readonly sameSite: "lax";
99
115
  };
100
116
  };
117
+ export declare function bufferToReadableStream(buffer: Buffer): ReadableStream<any>;
101
118
  export declare function getRedirectUrl(query: {
102
119
  redirect_to?: string | undefined;
103
120
  }, overrideHost: string | undefined): string | ValServerError;
104
- export declare function getMimeTypeFromBase64(content: string): string | null;
105
- export declare function bufferFromDataUrl(dataUrl: string, contentType: string | null): Buffer | undefined;
106
- export type PatchFileMetadata = {
107
- mimeType: string;
108
- sha256: string;
109
- };
110
- export type ValServerCallbacks = {
111
- isEnabled: () => Promise<boolean>;
112
- onEnable: (success: boolean) => Promise<void>;
113
- onDisable: (success: boolean) => Promise<void>;
114
- };
115
- export interface IValServer {
116
- authorize(query: {
117
- redirect_to?: string;
118
- }): Promise<ValServerRedirectResult<VAL_STATE_COOKIE>>;
119
- callback(query: {
120
- code?: string;
121
- state?: string;
122
- }, cookies: ValCookies<VAL_STATE_COOKIE>): Promise<ValServerRedirectResult<VAL_STATE_COOKIE | VAL_SESSION_COOKIE | VAL_ENABLE_COOKIE_NAME>>;
123
- enable(query: {
124
- redirect_to?: string;
125
- }): Promise<ValServerRedirectResult<VAL_ENABLE_COOKIE_NAME>>;
126
- disable(query: {
127
- redirect_to?: string;
128
- }): Promise<ValServerRedirectResult<VAL_ENABLE_COOKIE_NAME>>;
129
- logout(): Promise<ValServerResult<VAL_STATE_COOKIE | VAL_SESSION_COOKIE>>;
130
- session(cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ValSession>>;
131
- getTree(treePath: string, query: {
132
- patch?: string;
133
- schema?: string;
134
- source?: string;
135
- validate?: string;
136
- }, cookies: ValCookies<VAL_SESSION_COOKIE>, requestHeaders: RequestHeaders): Promise<ValServerJsonResult<ApiTreeResponse>>;
137
- getPatches(query: {
138
- id?: string[];
139
- }, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ApiGetPatchResponse>>;
140
- postPatches(body: unknown, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ApiPostPatchResponse>>;
141
- deletePatches(query: {
142
- id?: string[];
143
- }, cookies: ValCookies<VAL_SESSION_COOKIE>): Promise<ValServerJsonResult<ApiDeletePatchResponse>>;
144
- postValidate(body: unknown, cookies: ValCookies<VAL_SESSION_COOKIE>, requestHeaders: RequestHeaders): Promise<ValServerJsonResult<ApiPostValidationResponse | ApiPostValidationErrorResponse>>;
145
- postCommit(body: unknown, cookies: ValCookies<VAL_SESSION_COOKIE>, requestHeaders: RequestHeaders): Promise<ValServerJsonResult<ApiCommitResponse, ApiPostValidationErrorResponse>>;
146
- getFiles(filePath: string, query: {
147
- sha256?: string;
148
- }, cookies: ValCookies<VAL_SESSION_COOKIE>, requestHeaders: RequestHeaders): Promise<ValServerResult<never, ReadableStream<Uint8Array>> | ValServerRedirectResult<VAL_ENABLE_COOKIE_NAME>>;
149
- }
150
121
  export declare function guessMimeTypeFromPath(filePath: string): string | null;
151
- export declare function isCachedPatchFileOp(op: Operation): op is {
152
- op: "file";
153
- path: string[];
154
- filePath: string;
155
- value: {
156
- sha256: string;
157
- };
158
- };
159
- export type RequestHeaders = {
160
- host?: string | null;
161
- "x-forwarded-proto"?: string | null;
162
- };
163
- export declare function debugTiming<R>(id: string, fn: () => Promise<R>): Promise<R>;
122
+ export {};
@@ -1,14 +1,13 @@
1
- import { ServiceOptions } from "./Service.js";
2
- import { IValServer, ValServerCallbacks } from "./ValServer.js";
3
1
  import { ValConfig, ValModules } from "@valbuild/core";
4
2
  import { ValServerGenericResult } from "@valbuild/shared/internal";
3
+ import { ValServer, ValServerCallbacks } from "./ValServer.js";
5
4
  type Versions = {
6
5
  versions?: {
7
6
  core?: string;
8
7
  next?: string;
9
8
  };
10
9
  };
11
- export type ValApiOptions = ValServerOverrides & ServiceOptions & ValConfig & Versions;
10
+ export type ValApiOptions = ValServerOverrides & ValConfig & Versions;
12
11
  type ValServerOverrides = Partial<{
13
12
  /**
14
13
  * Override the Val API key.
@@ -84,11 +83,11 @@ type ValServerOverrides = Partial<{
84
83
  */
85
84
  valContentUrl: string;
86
85
  /**
87
- * The cloud name of this Val project.
86
+ * The full project name of the Val project.
88
87
  *
89
88
  * @example "myorg/my-project"
90
89
  */
91
- remote: string;
90
+ project: string;
92
91
  /**
93
92
  * After Val is enabled, redirect to this url.
94
93
  *
@@ -114,10 +113,10 @@ type ValServerOverrides = Partial<{
114
113
  */
115
114
  disableCache?: boolean;
116
115
  }>;
117
- export declare function createValServer(valModules: ValModules, route: string, opts: ValApiOptions, callbacks: ValServerCallbacks): Promise<IValServer>;
116
+ export declare function createValServer(valModules: ValModules, route: string, opts: ValApiOptions, callbacks: ValServerCallbacks, formatter?: (code: string, filePath: string) => string): Promise<ValServer>;
118
117
  export declare function safeReadGit(cwd: string): Promise<{
119
118
  commit?: string;
120
119
  branch?: string;
121
120
  }>;
122
- export declare function createValApiRouter<Res>(route: string, valServerPromise: Promise<IValServer>, convert: (valServerRes: ValServerGenericResult) => Res): (req: Request) => Promise<Res>;
121
+ export declare function createValApiRouter<Res>(route: string, valServerPromise: Promise<ValServer>, convert: (valServerRes: ValServerGenericResult) => Res): (req: Request) => Promise<Res>;
123
122
  export {};
@@ -9,7 +9,6 @@ export type { IValFSHost } from "./ValFSHost.js";
9
9
  export type { ValFS } from "./ValFS.js";
10
10
  export { patchSourceFile } from "./patchValFile.js";
11
11
  export { formatSyntaxErrorTree } from "./patch/ts/syntax.js";
12
- export { LocalValServer } from "./LocalValServer.js";
13
12
  export { createFixPatch } from "./createFixPatch.js";
14
13
  export { PatchJSON, Patch } from "./patch/validation.js";
15
14
  export * from "./jwt.js";