Skip to content

Commit

Permalink
add embed file support
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhongjia committed Apr 5, 2024
1 parent 78dea09 commit 6c6cb10
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
11 changes: 10 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { CString, ptr } from "bun:ffi";
import { CString, ptr, suffix } from "bun:ffi";
import { unlinkSync } from "node:fs";
import os from "os";
import path from "path";

const lib_pos = path.join(os.tmpdir(), `${Date.now()}_libwebui.${suffix}`);

/**
* Convert a String to C-String.
Expand All @@ -19,3 +24,7 @@ export function toCString(value: string): CString {
export function arrayToPtr(array: NodeJS.TypedArray | ArrayBufferLike) {
return ptr(array, 0);
}

export const getTmpLibPath = () => lib_pos;

export const deleteTmpLib = () => unlinkSync(lib_pos);
29 changes: 26 additions & 3 deletions src/webui.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { BunFile } from "bun";
import {
webui_so,
Event,
Expand All @@ -7,12 +8,22 @@ import {
Runtimes,
InterfaceBindCallback,
} from "./types";
import { arrayToPtr, toCString } from "./utils";
import { arrayToPtr, toCString, getTmpLibPath, deleteTmpLib } from "./utils";

// Max windows, servers and threads
const WEBUI_MAX_IDS = 256;

export default function LinkLib(path: string) {
export default async function LinkLib(lib: string | BunFile) {
// this is record the lib whether is a bunfile
const is_file = typeof lib != "string";

const path: string = is_file ? getTmpLibPath() : lib;

if (is_file) {
const tmpLibFile = Bun.file(path);
await Bun.write(tmpLibFile, lib);
}

const webui = webui_so(path);
const windows: Map<number, WebUI> = new Map();

Expand Down Expand Up @@ -51,7 +62,7 @@ export default function LinkLib(path: string) {
constructor(arg?: number) {
if (typeof arg === "number") {
if (arg <= 0 || arg >= WEBUI_MAX_IDS) {
// TODO: this should be error handle
throw new Error(`number must be between 0 and ${WEBUI_MAX_IDS}`);
}
const handle = webui.webui_new_window_id(arg);
this._handle = Number(handle);
Expand All @@ -77,6 +88,18 @@ export default function LinkLib(path: string) {
return Number(n);
}

/**
*
* free the lib file.
* only call when you pass a bun file
*
*/
static freeLibFile() {
if (is_file) {
deleteTmpLib();
}
}

/**
*
* Bind a specific html element click event with a function.
Expand Down
2 changes: 2 additions & 0 deletions types/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ export declare function toCString(value: string): CString;
* @returns pointer
*/
export declare function arrayToPtr(array: NodeJS.TypedArray | ArrayBufferLike): import("bun:ffi").Pointer;
export declare const getTmpLibPath: () => string;
export declare const deleteTmpLib: () => void;
14 changes: 12 additions & 2 deletions types/webui.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/// <reference types="bun-types" />
/// <reference types="bun-types" />
import type { BunFile } from "bun";
import { Event, BindCallback, SetFileHandlerCallbak, Browsers, Runtimes, InterfaceBindCallback } from "./types";
export default function LinkLib(path: string): {
export default function LinkLib(lib: string | BunFile): Promise<{
new (): {};
WebUI: {
new (): {
Expand Down Expand Up @@ -819,6 +822,13 @@ export default function LinkLib(path: string): {
* ```
*/
getNewWindowsID(): number;
/**
*
* free the lib file.
* only call when you pass a bun file
*
*/
freeLibFile(): void;
/**
*
* Wait until all opened windows get closed.
Expand Down Expand Up @@ -1086,4 +1096,4 @@ export default function LinkLib(path: string): {
BindCallback: typeof BindCallback;
SetFileHandlerCallbak: typeof SetFileHandlerCallbak;
InterfaceBindCallback: typeof InterfaceBindCallback;
};
}>;

0 comments on commit 6c6cb10

Please sign in to comment.