Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions schemas/WebViewOptions.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scripts/generate-schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async function main() {
if (!language || language === "typescript") {
const tsContent = generateTypeScript(
doc,
name,
doc.title,
relativePath,
);
const tsFilePath = join(tsSchemaDir, `${name}.ts`);
Expand Down
4 changes: 2 additions & 2 deletions src/bin/generate_schemas.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use schemars::schema_for;
use std::fs::File;
use std::io::Write;
use webview::{Message, Request, Response, WebViewOptions};
use webview::{Message, Options, Request, Response};

fn main() {
let schemas = [
("WebViewOptions", schema_for!(WebViewOptions)),
("WebViewOptions", schema_for!(Options)),
("WebViewMessage", schema_for!(Message)),
("WebViewRequest", schema_for!(Request)),
("WebViewResponse", schema_for!(Response)),
Expand Down
4 changes: 2 additions & 2 deletions src/bin/webview.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::env;
use tracing::error;
use webview::{run, WebViewOptions};
use webview::{run, Options};

fn main() {
let subscriber = tracing_subscriber::fmt()
Expand All @@ -11,7 +11,7 @@ fn main() {

let args: Vec<String> = env::args().collect();

let webview_options: WebViewOptions = match serde_json::from_str(&args[1]) {
let webview_options: Options = match serde_json::from_str(&args[1]) {
Ok(options) => options,
Err(e) => {
error!("Failed to parse webview options: {:?}", e);
Expand Down
22 changes: 11 additions & 11 deletions src/clients/deno/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
*/

import { EventEmitter } from "node:events";
import { WebViewMessage } from "./schemas/WebViewMessage.ts";
import type { WebViewOptions } from "./schemas/WebViewOptions.ts";
import type { WebViewRequest } from "./schemas/WebViewRequest.ts";
import { WebViewResponse } from "./schemas/WebViewResponse.ts";
import { Message } from "./schemas/WebViewMessage.ts";
import type { Options } from "./schemas/WebViewOptions.ts";
import type { Request as WebViewRequest } from "./schemas/WebViewRequest.ts";
import { Response as WebViewResponse } from "./schemas/WebViewResponse.ts";
import { monotonicUlid as ulid } from "jsr:@std/ulid";
import type { Except, Simplify } from "npm:type-fest";
import { join } from "jsr:@std/path";
Expand All @@ -50,14 +50,14 @@ if (
FmtSubscriber.setGlobalDefault({ level, color: true });
}

export type { WebViewOptions } from "./schemas/WebViewOptions.ts";
export type { Options } from "./schemas/WebViewOptions.ts";

// Should match the cargo package version
/** The version of the webview binary that's expected */
export const BIN_VERSION = "0.1.14";

type WebViewNotification = Extract<
WebViewMessage,
Message,
{ $type: "notification" }
>["data"];

Expand Down Expand Up @@ -102,7 +102,7 @@ const returnAck = (result: WebViewResponse) => {
});
};

async function getWebViewBin(options: WebViewOptions) {
async function getWebViewBin(options: Options) {
if (
Deno.permissions.querySync({ name: "env", variable: "WEBVIEW_BIN" })
.state === "granted"
Expand Down Expand Up @@ -177,7 +177,7 @@ function getCacheDir(): string {
*
* Will automatically fetch the webview binary if it's not already downloaded
*/
export async function createWebView(options: WebViewOptions): Promise<WebView> {
export async function createWebView(options: Options): Promise<WebView> {
const binPath = await getWebViewBin(options);
return new WebView(options, binPath);
}
Expand All @@ -197,15 +197,15 @@ export class WebView implements Disposable {
#internalEvent = new EventEmitter();
#externalEvent = new EventEmitter();
#messageLoop: Promise<void>;
#options: WebViewOptions;
#options: Options;

/**
* Creates a new webview window.
*
* @param options - The options for the webview.
* @param webviewBinaryPath - The path to the webview binary.
*/
constructor(options: WebViewOptions, webviewBinaryPath: string) {
constructor(options: Options, webviewBinaryPath: string) {
this.#options = options;
this.#process = new Deno.Command(webviewBinaryPath, {
args: [JSON.stringify(options)],
Expand Down Expand Up @@ -252,7 +252,7 @@ export class WebView implements Disposable {
continue;
}
trace("buffer", { buffer: this.#buffer });
const result = WebViewMessage.safeParse(
const result = Message.safeParse(
JSON.parse(this.#buffer.slice(0, newlineIndex)),
);
this.#buffer = this.#buffer.slice(newlineIndex + 1);
Expand Down
13 changes: 5 additions & 8 deletions src/clients/deno/schemas/WebViewMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export type Response =
/**
* Complete definition of all outbound messages from the webview to the client.
*/
export type WebViewMessage =
export type Message =
| {
$type: "notification";

Expand Down Expand Up @@ -120,10 +120,7 @@ export const Response: z.ZodType<Response> = z.discriminatedUnion("$type", [
z.object({ $type: z.literal("err"), id: z.string(), message: z.string() }),
]);

export const WebViewMessage: z.ZodType<WebViewMessage> = z.discriminatedUnion(
"$type",
[
z.object({ $type: z.literal("notification"), data: Notification }),
z.object({ $type: z.literal("response"), data: Response }),
],
);
export const Message: z.ZodType<Message> = z.discriminatedUnion("$type", [
z.object({ $type: z.literal("notification"), data: Notification }),
z.object({ $type: z.literal("response"), data: Response }),
]);
36 changes: 18 additions & 18 deletions src/clients/deno/schemas/WebViewOptions.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
// DO NOT EDIT: This file is auto-generated by generate-schema/index.ts
import { z } from "npm:zod";

export type Size = {
/** The height of the window in logical pixels. */
height: number;
/** The width of the window in logical pixels. */
width: number;
};

/**
* The content to load into the webview.
*/
export type WebViewContent =
export type Content =
| {
/** Optional headers to send with the request. */
headers?: Record<string, string>;
Expand All @@ -25,13 +18,20 @@ export type WebViewContent =
origin?: string;
};

export type Size = {
/** The height of the window in logical pixels. */
height: number;
/** The width of the window in logical pixels. */
width: number;
};

export type WindowSizeStates = "maximized" | "fullscreen";
export type WindowSize = WindowSizeStates | Size;

/**
* Options for creating a webview.
*/
export type WebViewOptions = {
export type Options = {
/** Sets whether clicking an inactive window also clicks through to the webview. Default is false. */
acceptFirstMouse?: boolean;
/** When true, all media can be played without user interaction. Default is false. */
Expand Down Expand Up @@ -63,7 +63,7 @@ export type WebViewOptions = {
/** Sets whether host should be able to receive messages from the webview via `window.ipc.postMessage`. */
ipc?: boolean;
/** The content to load into the webview. */
load?: WebViewContent;
load?: Content;
/** The size of the window. */
size?: WindowSize;
/** Sets the title of the window. */
Expand All @@ -74,19 +74,19 @@ export type WebViewOptions = {
userAgent?: string;
};

export const Size: z.ZodType<Size> = z.object({
height: z.number(),
width: z.number(),
});

export const WebViewContent: z.ZodType<WebViewContent> = z.union([
export const Content: z.ZodType<Content> = z.union([
z.object({
headers: z.record(z.string(), z.string()).optional(),
url: z.string(),
}),
z.object({ html: z.string(), origin: z.string() }),
]);

export const Size: z.ZodType<Size> = z.object({
height: z.number(),
width: z.number(),
});

export const WindowSizeStates: z.ZodType<WindowSizeStates> = z.enum([
"maximized",
"fullscreen",
Expand All @@ -96,7 +96,7 @@ export const WindowSize: z.ZodType<WindowSize> = z.union([
Size,
]);

export const WebViewOptions: z.ZodType<WebViewOptions> = z.object({
export const Options: z.ZodType<Options> = z.object({
acceptFirstMouse: z.boolean().optional(),
autoplay: z.boolean().optional(),
clipboard: z.boolean().optional(),
Expand All @@ -106,7 +106,7 @@ export const WebViewOptions: z.ZodType<WebViewOptions> = z.object({
incognito: z.boolean().optional(),
initializationScript: z.string(),
ipc: z.boolean().optional(),
load: WebViewContent.optional(),
load: Content.optional(),
size: WindowSize.optional(),
title: z.string(),
transparent: z.boolean().optional(),
Expand Down
Loading
Loading