This commit is contained in:
Manav Rathi 2024-04-30 13:58:15 +05:30
parent 333f9c58f2
commit 72b9113d30
No known key found for this signature in database
5 changed files with 17 additions and 6 deletions

View file

@ -3,7 +3,7 @@ import fs from "node:fs/promises";
import type { ZipItem } from "../../types/ipc";
import log from "../log";
import { execAsync } from "../utils";
import { withTimeout } from "../utils/common";
import { ensure, withTimeout } from "../utils/common";
import {
deleteTempFile,
makeFileForDataOrPathOrZipItem,
@ -110,5 +110,5 @@ const ffmpegBinaryPath = () => {
// This substitution of app.asar by app.asar.unpacked is suggested by the
// ffmpeg-static library author themselves:
// https://github.com/eugeneware/ffmpeg-static/issues/16
return pathToFfmpeg.replace("app.asar", "app.asar.unpacked");
return ensure(pathToFfmpeg).replace("app.asar", "app.asar.unpacked");
};

View file

@ -1,6 +1,7 @@
/**
* @file file system related functions exposed over the context bridge.
*/
import { existsSync } from "node:fs";
import fs from "node:fs/promises";

View file

@ -11,6 +11,7 @@ import * as ort from "onnxruntime-node";
import Tokenizer from "../../thirdparty/clip-bpe-ts/mod";
import log from "../log";
import { writeStream } from "../stream";
import { ensure } from "../utils/common";
import { deleteTempFile, makeTempFilePath } from "../utils/temp";
import { makeCachedInferenceSession } from "./ml";
@ -22,8 +23,7 @@ const cachedCLIPImageSession = makeCachedInferenceSession(
export const clipImageEmbedding = async (jpegImageData: Uint8Array) => {
const tempFilePath = await makeTempFilePath();
const imageStream = new Response(jpegImageData.buffer).body;
if (!imageStream) throw new Error("Missing body that we just fed data to");
await writeStream(tempFilePath, imageStream);
await writeStream(tempFilePath, ensure(imageStream));
try {
return await clipImageEmbedding_(tempFilePath);
} finally {

View file

@ -5,6 +5,15 @@
* currently a common package that both of them share.
*/
/**
* Throw an exception if the given value is `null` or `undefined`.
*/
export const ensure = <T>(v: T | null | undefined): T => {
if (v === null) throw new Error("Required value was null");
if (v === undefined) throw new Error("Required value was not found");
return v;
};
/**
* Wait for {@link ms} milliseconds
*

View file

@ -1,7 +1,8 @@
/**
* Throw an exception if the given value is undefined.
* Throw an exception if the given value is `null` or `undefined`.
*/
export const ensure = <T>(v: T | undefined): T => {
export const ensure = <T>(v: T | null | undefined): T => {
if (v === null) throw new Error("Required value was null");
if (v === undefined) throw new Error("Required value was not found");
return v;
};