@midscene/shared
Advanced tools
Comparing version 0.7.2-beta-20241024113439.0 to 0.7.2-beta-20241025012947.0
@@ -5,8 +5,13 @@ // src/fs/index.ts | ||
import { dirname, join } from "path"; | ||
var pkg; | ||
function getMidscenePkgInfo(dir) { | ||
if (pkg) { | ||
return pkg; | ||
var pkgCacheMap = {}; | ||
var ifInBrowser = typeof window !== "undefined"; | ||
function getRunningPkgInfo(dir) { | ||
if (ifInBrowser) { | ||
return null; | ||
} | ||
const pkgDir = findNearestPackageJson(dir || process.cwd()); | ||
const dirToCheck = dir || process.cwd(); | ||
if (pkgCacheMap[dirToCheck]) { | ||
return pkgCacheMap[dirToCheck]; | ||
} | ||
const pkgDir = findNearestPackageJson(dirToCheck); | ||
assert(pkgDir, "package.json not found"); | ||
@@ -16,7 +21,7 @@ const pkgJsonFile = join(pkgDir, "package.json"); | ||
const { name, version } = JSON.parse(readFileSync(pkgJsonFile, "utf-8")); | ||
pkg = { name, version, dir: pkgDir }; | ||
return pkg; | ||
pkgCacheMap[dirToCheck] = { name, version, dir: pkgDir }; | ||
return pkgCacheMap[dirToCheck]; | ||
} | ||
return { | ||
name: "midscene-unknown-page-name", | ||
name: "midscene-unknown-package-name", | ||
version: "0.0.0", | ||
@@ -39,3 +44,3 @@ dir: pkgDir | ||
findNearestPackageJson, | ||
getMidscenePkgInfo | ||
getRunningPkgInfo | ||
}; |
@@ -0,26 +1,70 @@ | ||
var __async = (__this, __arguments, generator) => { | ||
return new Promise((resolve, reject) => { | ||
var fulfilled = (value) => { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var rejected = (value) => { | ||
try { | ||
step(generator.throw(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); | ||
step((generator = generator.apply(__this, __arguments)).next()); | ||
}); | ||
}; | ||
// src/img/info.ts | ||
import assert from "assert"; | ||
import { Buffer } from "buffer"; | ||
import { Buffer as Buffer2 } from "buffer"; | ||
import { readFileSync } from "fs"; | ||
import Jimp from "jimp"; | ||
async function imageInfo(image) { | ||
let jimpImage; | ||
if (typeof image === "string") { | ||
jimpImage = await Jimp.read(image); | ||
} else if (Buffer.isBuffer(image)) { | ||
jimpImage = await Jimp.read(image); | ||
} else { | ||
throw new Error("Invalid image input: must be a string path or a Buffer"); | ||
} | ||
const { width, height } = jimpImage.bitmap; | ||
assert( | ||
width && height, | ||
`Invalid image: ${typeof image === "string" ? image : "Buffer"}` | ||
); | ||
return { width, height }; | ||
// src/img/get-jimp.ts | ||
var ifInBrowser = typeof window !== "undefined"; | ||
function getJimp() { | ||
return __async(this, null, function* () { | ||
if (ifInBrowser) { | ||
return (yield import("jimp/browser/lib/jimp.js")).default; | ||
} | ||
return (yield import("jimp")).default; | ||
}); | ||
} | ||
async function imageInfoOfBase64(imageBase64) { | ||
const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, ""); | ||
return imageInfo(Buffer.from(base64Data, "base64")); | ||
// src/img/info.ts | ||
function imageInfo(image) { | ||
return __async(this, null, function* () { | ||
const Jimp = yield getJimp(); | ||
let jimpImage; | ||
if (typeof image === "string") { | ||
jimpImage = yield Jimp.read(image); | ||
} else if (Buffer2.isBuffer(image)) { | ||
jimpImage = yield Jimp.read(image); | ||
} else { | ||
throw new Error("Invalid image input: must be a string path or a Buffer"); | ||
} | ||
const { width, height } = jimpImage.bitmap; | ||
assert( | ||
width && height, | ||
`Invalid image: ${typeof image === "string" ? image : "Buffer"}` | ||
); | ||
return { width, height }; | ||
}); | ||
} | ||
function imageInfoOfBase64(imageBase64) { | ||
return __async(this, null, function* () { | ||
const buffer = yield bufferFromBase64(imageBase64); | ||
return imageInfo(buffer); | ||
}); | ||
} | ||
function bufferFromBase64(imageBase64) { | ||
return __async(this, null, function* () { | ||
const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, ""); | ||
return Buffer2.from(base64Data, "base64"); | ||
}); | ||
} | ||
function base64Encoded(image, withHeader = true) { | ||
@@ -44,28 +88,36 @@ const imageBuffer = readFileSync(image); | ||
// src/img/transform.ts | ||
import { Buffer as Buffer2 } from "buffer"; | ||
import Jimp2 from "jimp"; | ||
async function saveBase64Image(options) { | ||
const { base64Data, outputPath } = options; | ||
const base64Image = base64Data.split(";base64,").pop() || base64Data; | ||
const imageBuffer = Buffer2.from(base64Image, "base64"); | ||
const image = await Jimp2.read(imageBuffer); | ||
await image.writeAsync(outputPath); | ||
import { Buffer as Buffer3 } from "buffer"; | ||
function saveBase64Image(options) { | ||
return __async(this, null, function* () { | ||
const { base64Data, outputPath } = options; | ||
const base64Image = base64Data.split(";base64,").pop() || base64Data; | ||
const imageBuffer = Buffer3.from(base64Image, "base64"); | ||
const Jimp = yield getJimp(); | ||
const image = yield Jimp.read(imageBuffer); | ||
yield image.writeAsync(outputPath); | ||
}); | ||
} | ||
async function transformImgPathToBase64(inputPath) { | ||
const image = await Jimp2.read(inputPath); | ||
const buffer = await image.getBufferAsync(Jimp2.MIME_PNG); | ||
return buffer.toString("base64"); | ||
function transformImgPathToBase64(inputPath) { | ||
return __async(this, null, function* () { | ||
const Jimp = yield getJimp(); | ||
const image = yield Jimp.read(inputPath); | ||
const buffer = yield image.getBufferAsync(Jimp.MIME_PNG); | ||
return buffer.toString("base64"); | ||
}); | ||
} | ||
async function resizeImg(inputData, newSize) { | ||
const isBase64 = typeof inputData === "string"; | ||
const imageBuffer = isBase64 ? Buffer2.from(inputData.split(";base64,").pop() || inputData, "base64") : inputData; | ||
const image = await Jimp2.read(imageBuffer); | ||
const { width, height } = image.bitmap; | ||
if (!width || !height) { | ||
throw Error("Undefined width or height from the input image."); | ||
} | ||
const finalNewSize = newSize || calculateNewDimensions(width, height); | ||
image.resize(finalNewSize.width, finalNewSize.height); | ||
const resizedBuffer = await image.getBufferAsync(Jimp2.MIME_PNG); | ||
return isBase64 ? resizedBuffer.toString("base64") : resizedBuffer; | ||
function resizeImg(inputData, newSize) { | ||
return __async(this, null, function* () { | ||
const isBase64 = typeof inputData === "string"; | ||
const imageBuffer = isBase64 ? Buffer3.from(inputData.split(";base64,").pop() || inputData, "base64") : inputData; | ||
const Jimp = yield getJimp(); | ||
const image = yield Jimp.read(imageBuffer); | ||
const { width, height } = image.bitmap; | ||
if (!width || !height) { | ||
throw Error("Undefined width or height from the input image."); | ||
} | ||
const finalNewSize = newSize || calculateNewDimensions(width, height); | ||
image.resize(finalNewSize.width, finalNewSize.height); | ||
const resizedBuffer = yield image.getBufferAsync(Jimp.MIME_PNG); | ||
return isBase64 ? resizedBuffer.toString("base64") : resizedBuffer; | ||
}); | ||
} | ||
@@ -91,23 +143,26 @@ function calculateNewDimensions(originalWidth, originalHeight) { | ||
} | ||
async function trimImage(image) { | ||
const jimpImage = await Jimp2.read( | ||
Buffer2.isBuffer(image) ? image : Buffer2.from(image) | ||
); | ||
const { width, height } = jimpImage.bitmap; | ||
if (width <= 3 || height <= 3) { | ||
return null; | ||
} | ||
const trimmedImage = jimpImage.autocrop(); | ||
const { width: trimmedWidth, height: trimmedHeight } = trimmedImage.bitmap; | ||
const trimOffsetLeft = (width - trimmedWidth) / 2; | ||
const trimOffsetTop = (height - trimmedHeight) / 2; | ||
if (trimOffsetLeft === 0 && trimOffsetTop === 0) { | ||
return null; | ||
} | ||
return { | ||
trimOffsetLeft: -trimOffsetLeft, | ||
trimOffsetTop: -trimOffsetTop, | ||
width: trimmedWidth, | ||
height: trimmedHeight | ||
}; | ||
function trimImage(image) { | ||
return __async(this, null, function* () { | ||
const Jimp = yield getJimp(); | ||
const jimpImage = yield Jimp.read( | ||
Buffer3.isBuffer(image) ? image : Buffer3.from(image) | ||
); | ||
const { width, height } = jimpImage.bitmap; | ||
if (width <= 3 || height <= 3) { | ||
return null; | ||
} | ||
const trimmedImage = jimpImage.autocrop(); | ||
const { width: trimmedWidth, height: trimmedHeight } = trimmedImage.bitmap; | ||
const trimOffsetLeft = (width - trimmedWidth) / 2; | ||
const trimOffsetTop = (height - trimmedHeight) / 2; | ||
if (trimOffsetLeft === 0 && trimOffsetTop === 0) { | ||
return null; | ||
} | ||
return { | ||
trimOffsetLeft: -trimOffsetLeft, | ||
trimOffsetTop: -trimOffsetTop, | ||
width: trimmedWidth, | ||
height: trimmedHeight | ||
}; | ||
}); | ||
} | ||
@@ -117,7 +172,6 @@ | ||
import assert2 from "assert"; | ||
import { Buffer as Buffer3 } from "buffer"; | ||
import Jimp3 from "jimp"; | ||
var createSvgOverlay = (elements, imageWidth, imageHeight) => { | ||
const createPngOverlay = async (elements2, imageWidth2, imageHeight2) => { | ||
const image = new Jimp3(imageWidth2, imageHeight2, 0); | ||
const createPngOverlay = (elements2, imageWidth2, imageHeight2) => __async(void 0, null, function* () { | ||
const Jimp = yield getJimp(); | ||
const image = new Jimp(imageWidth2, imageHeight2, 0); | ||
const colors = [ | ||
@@ -203,3 +257,3 @@ { rect: 4278190335, text: 4294967295 } | ||
}); | ||
const font = await Jimp3.loadFont(Jimp3.FONT_SANS_16_WHITE); | ||
const font = yield Jimp.loadFont(Jimp.FONT_SANS_16_WHITE); | ||
image.print( | ||
@@ -211,4 +265,4 @@ font, | ||
text: element.indexId.toString(), | ||
alignmentX: Jimp3.HORIZONTAL_ALIGN_CENTER, | ||
alignmentY: Jimp3.VERTICAL_ALIGN_MIDDLE | ||
alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER, | ||
alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE | ||
}, | ||
@@ -219,15 +273,15 @@ rectWidth, | ||
} | ||
return image.getBufferAsync(Jimp3.MIME_PNG); | ||
}; | ||
return image.getBufferAsync(Jimp.MIME_PNG); | ||
}); | ||
return createPngOverlay(elements, imageWidth, imageHeight); | ||
}; | ||
var compositeElementInfoImg = async (options) => { | ||
var compositeElementInfoImg = (options) => __async(void 0, null, function* () { | ||
const Jimp = yield getJimp(); | ||
const { inputImgBase64, elementsPositionInfo } = options; | ||
const imageBuffer = Buffer3.from(inputImgBase64, "base64"); | ||
const image = await Jimp3.read(imageBuffer); | ||
const { width, height } = image.bitmap; | ||
const imageBuffer = yield bufferFromBase64(inputImgBase64); | ||
const { width, height } = yield imageInfo(imageBuffer); | ||
if (!width || !height) { | ||
throw Error("Image processing failed because width or height is undefined"); | ||
} | ||
const svgOverlay = await createSvgOverlay( | ||
const svgOverlay = yield createSvgOverlay( | ||
elementsPositionInfo, | ||
@@ -237,11 +291,11 @@ width, | ||
); | ||
return await Jimp3.read(imageBuffer).then(async (image2) => { | ||
const svgImage = await Jimp3.read(svgOverlay); | ||
return image2.composite(svgImage, 0, 0, { | ||
mode: Jimp3.BLEND_SOURCE_OVER, | ||
return yield Jimp.read(imageBuffer).then((image) => __async(void 0, null, function* () { | ||
const svgImage = yield Jimp.read(svgOverlay); | ||
return image.composite(svgImage, 0, 0, { | ||
mode: Jimp.BLEND_SOURCE_OVER, | ||
opacitySource: 1, | ||
opacityDest: 1 | ||
}); | ||
}).then((compositeImage) => { | ||
return compositeImage.getBufferAsync(Jimp3.MIME_PNG); | ||
})).then((compositeImage) => { | ||
return compositeImage.getBufferAsync(Jimp.MIME_PNG); | ||
}).then((buffer) => { | ||
@@ -252,4 +306,4 @@ return buffer.toString("base64"); | ||
}); | ||
}; | ||
var processImageElementInfo = async (options) => { | ||
}); | ||
var processImageElementInfo = (options) => __async(void 0, null, function* () { | ||
const base64Image = options.inputImgBase64.split(";base64,").pop(); | ||
@@ -260,3 +314,3 @@ assert2(base64Image, "base64Image is undefined"); | ||
compositeElementInfoImgWithoutTextBase64 | ||
] = await Promise.all([ | ||
] = yield Promise.all([ | ||
compositeElementInfoImg({ | ||
@@ -275,6 +329,7 @@ inputImgBase64: options.inputImgBase64, | ||
}; | ||
}; | ||
}); | ||
export { | ||
base64Encoded, | ||
base64ToPngFormat, | ||
bufferFromBase64, | ||
calculateNewDimensions, | ||
@@ -281,0 +336,0 @@ compositeElementInfoImg, |
// src/index.ts | ||
function src_default() { | ||
return "hello world"; | ||
} | ||
var src_default = {}; | ||
export { | ||
src_default as default | ||
}; |
@@ -34,3 +34,3 @@ "use strict"; | ||
findNearestPackageJson: () => findNearestPackageJson, | ||
getMidscenePkgInfo: () => getMidscenePkgInfo | ||
getRunningPkgInfo: () => getRunningPkgInfo | ||
}); | ||
@@ -41,8 +41,13 @@ module.exports = __toCommonJS(fs_exports); | ||
var import_node_path = require("path"); | ||
var pkg; | ||
function getMidscenePkgInfo(dir) { | ||
if (pkg) { | ||
return pkg; | ||
var pkgCacheMap = {}; | ||
var ifInBrowser = typeof window !== "undefined"; | ||
function getRunningPkgInfo(dir) { | ||
if (ifInBrowser) { | ||
return null; | ||
} | ||
const pkgDir = findNearestPackageJson(dir || process.cwd()); | ||
const dirToCheck = dir || process.cwd(); | ||
if (pkgCacheMap[dirToCheck]) { | ||
return pkgCacheMap[dirToCheck]; | ||
} | ||
const pkgDir = findNearestPackageJson(dirToCheck); | ||
(0, import_node_assert.default)(pkgDir, "package.json not found"); | ||
@@ -52,7 +57,7 @@ const pkgJsonFile = (0, import_node_path.join)(pkgDir, "package.json"); | ||
const { name, version } = JSON.parse((0, import_node_fs.readFileSync)(pkgJsonFile, "utf-8")); | ||
pkg = { name, version, dir: pkgDir }; | ||
return pkg; | ||
pkgCacheMap[dirToCheck] = { name, version, dir: pkgDir }; | ||
return pkgCacheMap[dirToCheck]; | ||
} | ||
return { | ||
name: "midscene-unknown-page-name", | ||
name: "midscene-unknown-package-name", | ||
version: "0.0.0", | ||
@@ -76,3 +81,3 @@ dir: pkgDir | ||
findNearestPackageJson, | ||
getMidscenePkgInfo | ||
getRunningPkgInfo | ||
}); |
@@ -29,2 +29,22 @@ "use strict"; | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
var __async = (__this, __arguments, generator) => { | ||
return new Promise((resolve, reject) => { | ||
var fulfilled = (value) => { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var rejected = (value) => { | ||
try { | ||
step(generator.throw(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); | ||
step((generator = generator.apply(__this, __arguments)).next()); | ||
}); | ||
}; | ||
@@ -36,2 +56,3 @@ // src/img/index.ts | ||
base64ToPngFormat: () => base64ToPngFormat, | ||
bufferFromBase64: () => bufferFromBase64, | ||
calculateNewDimensions: () => calculateNewDimensions, | ||
@@ -53,23 +74,46 @@ compositeElementInfoImg: () => compositeElementInfoImg, | ||
var import_node_fs = require("fs"); | ||
var import_jimp = __toESM(require("jimp")); | ||
async function imageInfo(image) { | ||
let jimpImage; | ||
if (typeof image === "string") { | ||
jimpImage = await import_jimp.default.read(image); | ||
} else if (import_node_buffer.Buffer.isBuffer(image)) { | ||
jimpImage = await import_jimp.default.read(image); | ||
} else { | ||
throw new Error("Invalid image input: must be a string path or a Buffer"); | ||
} | ||
const { width, height } = jimpImage.bitmap; | ||
(0, import_node_assert.default)( | ||
width && height, | ||
`Invalid image: ${typeof image === "string" ? image : "Buffer"}` | ||
); | ||
return { width, height }; | ||
// src/img/get-jimp.ts | ||
var ifInBrowser = typeof window !== "undefined"; | ||
function getJimp() { | ||
return __async(this, null, function* () { | ||
if (ifInBrowser) { | ||
return (yield import("jimp/browser/lib/jimp.js")).default; | ||
} | ||
return (yield import("jimp")).default; | ||
}); | ||
} | ||
async function imageInfoOfBase64(imageBase64) { | ||
const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, ""); | ||
return imageInfo(import_node_buffer.Buffer.from(base64Data, "base64")); | ||
// src/img/info.ts | ||
function imageInfo(image) { | ||
return __async(this, null, function* () { | ||
const Jimp = yield getJimp(); | ||
let jimpImage; | ||
if (typeof image === "string") { | ||
jimpImage = yield Jimp.read(image); | ||
} else if (import_node_buffer.Buffer.isBuffer(image)) { | ||
jimpImage = yield Jimp.read(image); | ||
} else { | ||
throw new Error("Invalid image input: must be a string path or a Buffer"); | ||
} | ||
const { width, height } = jimpImage.bitmap; | ||
(0, import_node_assert.default)( | ||
width && height, | ||
`Invalid image: ${typeof image === "string" ? image : "Buffer"}` | ||
); | ||
return { width, height }; | ||
}); | ||
} | ||
function imageInfoOfBase64(imageBase64) { | ||
return __async(this, null, function* () { | ||
const buffer = yield bufferFromBase64(imageBase64); | ||
return imageInfo(buffer); | ||
}); | ||
} | ||
function bufferFromBase64(imageBase64) { | ||
return __async(this, null, function* () { | ||
const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, ""); | ||
return import_node_buffer.Buffer.from(base64Data, "base64"); | ||
}); | ||
} | ||
function base64Encoded(image, withHeader = true) { | ||
@@ -94,27 +138,35 @@ const imageBuffer = (0, import_node_fs.readFileSync)(image); | ||
var import_node_buffer2 = require("buffer"); | ||
var import_jimp2 = __toESM(require("jimp")); | ||
async function saveBase64Image(options) { | ||
const { base64Data, outputPath } = options; | ||
const base64Image = base64Data.split(";base64,").pop() || base64Data; | ||
const imageBuffer = import_node_buffer2.Buffer.from(base64Image, "base64"); | ||
const image = await import_jimp2.default.read(imageBuffer); | ||
await image.writeAsync(outputPath); | ||
function saveBase64Image(options) { | ||
return __async(this, null, function* () { | ||
const { base64Data, outputPath } = options; | ||
const base64Image = base64Data.split(";base64,").pop() || base64Data; | ||
const imageBuffer = import_node_buffer2.Buffer.from(base64Image, "base64"); | ||
const Jimp = yield getJimp(); | ||
const image = yield Jimp.read(imageBuffer); | ||
yield image.writeAsync(outputPath); | ||
}); | ||
} | ||
async function transformImgPathToBase64(inputPath) { | ||
const image = await import_jimp2.default.read(inputPath); | ||
const buffer = await image.getBufferAsync(import_jimp2.default.MIME_PNG); | ||
return buffer.toString("base64"); | ||
function transformImgPathToBase64(inputPath) { | ||
return __async(this, null, function* () { | ||
const Jimp = yield getJimp(); | ||
const image = yield Jimp.read(inputPath); | ||
const buffer = yield image.getBufferAsync(Jimp.MIME_PNG); | ||
return buffer.toString("base64"); | ||
}); | ||
} | ||
async function resizeImg(inputData, newSize) { | ||
const isBase64 = typeof inputData === "string"; | ||
const imageBuffer = isBase64 ? import_node_buffer2.Buffer.from(inputData.split(";base64,").pop() || inputData, "base64") : inputData; | ||
const image = await import_jimp2.default.read(imageBuffer); | ||
const { width, height } = image.bitmap; | ||
if (!width || !height) { | ||
throw Error("Undefined width or height from the input image."); | ||
} | ||
const finalNewSize = newSize || calculateNewDimensions(width, height); | ||
image.resize(finalNewSize.width, finalNewSize.height); | ||
const resizedBuffer = await image.getBufferAsync(import_jimp2.default.MIME_PNG); | ||
return isBase64 ? resizedBuffer.toString("base64") : resizedBuffer; | ||
function resizeImg(inputData, newSize) { | ||
return __async(this, null, function* () { | ||
const isBase64 = typeof inputData === "string"; | ||
const imageBuffer = isBase64 ? import_node_buffer2.Buffer.from(inputData.split(";base64,").pop() || inputData, "base64") : inputData; | ||
const Jimp = yield getJimp(); | ||
const image = yield Jimp.read(imageBuffer); | ||
const { width, height } = image.bitmap; | ||
if (!width || !height) { | ||
throw Error("Undefined width or height from the input image."); | ||
} | ||
const finalNewSize = newSize || calculateNewDimensions(width, height); | ||
image.resize(finalNewSize.width, finalNewSize.height); | ||
const resizedBuffer = yield image.getBufferAsync(Jimp.MIME_PNG); | ||
return isBase64 ? resizedBuffer.toString("base64") : resizedBuffer; | ||
}); | ||
} | ||
@@ -140,23 +192,26 @@ function calculateNewDimensions(originalWidth, originalHeight) { | ||
} | ||
async function trimImage(image) { | ||
const jimpImage = await import_jimp2.default.read( | ||
import_node_buffer2.Buffer.isBuffer(image) ? image : import_node_buffer2.Buffer.from(image) | ||
); | ||
const { width, height } = jimpImage.bitmap; | ||
if (width <= 3 || height <= 3) { | ||
return null; | ||
} | ||
const trimmedImage = jimpImage.autocrop(); | ||
const { width: trimmedWidth, height: trimmedHeight } = trimmedImage.bitmap; | ||
const trimOffsetLeft = (width - trimmedWidth) / 2; | ||
const trimOffsetTop = (height - trimmedHeight) / 2; | ||
if (trimOffsetLeft === 0 && trimOffsetTop === 0) { | ||
return null; | ||
} | ||
return { | ||
trimOffsetLeft: -trimOffsetLeft, | ||
trimOffsetTop: -trimOffsetTop, | ||
width: trimmedWidth, | ||
height: trimmedHeight | ||
}; | ||
function trimImage(image) { | ||
return __async(this, null, function* () { | ||
const Jimp = yield getJimp(); | ||
const jimpImage = yield Jimp.read( | ||
import_node_buffer2.Buffer.isBuffer(image) ? image : import_node_buffer2.Buffer.from(image) | ||
); | ||
const { width, height } = jimpImage.bitmap; | ||
if (width <= 3 || height <= 3) { | ||
return null; | ||
} | ||
const trimmedImage = jimpImage.autocrop(); | ||
const { width: trimmedWidth, height: trimmedHeight } = trimmedImage.bitmap; | ||
const trimOffsetLeft = (width - trimmedWidth) / 2; | ||
const trimOffsetTop = (height - trimmedHeight) / 2; | ||
if (trimOffsetLeft === 0 && trimOffsetTop === 0) { | ||
return null; | ||
} | ||
return { | ||
trimOffsetLeft: -trimOffsetLeft, | ||
trimOffsetTop: -trimOffsetTop, | ||
width: trimmedWidth, | ||
height: trimmedHeight | ||
}; | ||
}); | ||
} | ||
@@ -166,7 +221,6 @@ | ||
var import_node_assert2 = __toESM(require("assert")); | ||
var import_node_buffer3 = require("buffer"); | ||
var import_jimp3 = __toESM(require("jimp")); | ||
var createSvgOverlay = (elements, imageWidth, imageHeight) => { | ||
const createPngOverlay = async (elements2, imageWidth2, imageHeight2) => { | ||
const image = new import_jimp3.default(imageWidth2, imageHeight2, 0); | ||
const createPngOverlay = (elements2, imageWidth2, imageHeight2) => __async(void 0, null, function* () { | ||
const Jimp = yield getJimp(); | ||
const image = new Jimp(imageWidth2, imageHeight2, 0); | ||
const colors = [ | ||
@@ -252,3 +306,3 @@ { rect: 4278190335, text: 4294967295 } | ||
}); | ||
const font = await import_jimp3.default.loadFont(import_jimp3.default.FONT_SANS_16_WHITE); | ||
const font = yield Jimp.loadFont(Jimp.FONT_SANS_16_WHITE); | ||
image.print( | ||
@@ -260,4 +314,4 @@ font, | ||
text: element.indexId.toString(), | ||
alignmentX: import_jimp3.default.HORIZONTAL_ALIGN_CENTER, | ||
alignmentY: import_jimp3.default.VERTICAL_ALIGN_MIDDLE | ||
alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER, | ||
alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE | ||
}, | ||
@@ -268,15 +322,15 @@ rectWidth, | ||
} | ||
return image.getBufferAsync(import_jimp3.default.MIME_PNG); | ||
}; | ||
return image.getBufferAsync(Jimp.MIME_PNG); | ||
}); | ||
return createPngOverlay(elements, imageWidth, imageHeight); | ||
}; | ||
var compositeElementInfoImg = async (options) => { | ||
var compositeElementInfoImg = (options) => __async(void 0, null, function* () { | ||
const Jimp = yield getJimp(); | ||
const { inputImgBase64, elementsPositionInfo } = options; | ||
const imageBuffer = import_node_buffer3.Buffer.from(inputImgBase64, "base64"); | ||
const image = await import_jimp3.default.read(imageBuffer); | ||
const { width, height } = image.bitmap; | ||
const imageBuffer = yield bufferFromBase64(inputImgBase64); | ||
const { width, height } = yield imageInfo(imageBuffer); | ||
if (!width || !height) { | ||
throw Error("Image processing failed because width or height is undefined"); | ||
} | ||
const svgOverlay = await createSvgOverlay( | ||
const svgOverlay = yield createSvgOverlay( | ||
elementsPositionInfo, | ||
@@ -286,11 +340,11 @@ width, | ||
); | ||
return await import_jimp3.default.read(imageBuffer).then(async (image2) => { | ||
const svgImage = await import_jimp3.default.read(svgOverlay); | ||
return image2.composite(svgImage, 0, 0, { | ||
mode: import_jimp3.default.BLEND_SOURCE_OVER, | ||
return yield Jimp.read(imageBuffer).then((image) => __async(void 0, null, function* () { | ||
const svgImage = yield Jimp.read(svgOverlay); | ||
return image.composite(svgImage, 0, 0, { | ||
mode: Jimp.BLEND_SOURCE_OVER, | ||
opacitySource: 1, | ||
opacityDest: 1 | ||
}); | ||
}).then((compositeImage) => { | ||
return compositeImage.getBufferAsync(import_jimp3.default.MIME_PNG); | ||
})).then((compositeImage) => { | ||
return compositeImage.getBufferAsync(Jimp.MIME_PNG); | ||
}).then((buffer) => { | ||
@@ -301,4 +355,4 @@ return buffer.toString("base64"); | ||
}); | ||
}; | ||
var processImageElementInfo = async (options) => { | ||
}); | ||
var processImageElementInfo = (options) => __async(void 0, null, function* () { | ||
const base64Image = options.inputImgBase64.split(";base64,").pop(); | ||
@@ -309,3 +363,3 @@ (0, import_node_assert2.default)(base64Image, "base64Image is undefined"); | ||
compositeElementInfoImgWithoutTextBase64 | ||
] = await Promise.all([ | ||
] = yield Promise.all([ | ||
compositeElementInfoImg({ | ||
@@ -324,3 +378,3 @@ inputImgBase64: options.inputImgBase64, | ||
}; | ||
}; | ||
}); | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -330,2 +384,3 @@ 0 && (module.exports = { | ||
base64ToPngFormat, | ||
bufferFromBase64, | ||
calculateNewDimensions, | ||
@@ -332,0 +387,0 @@ compositeElementInfoImg, |
@@ -26,4 +26,2 @@ "use strict"; | ||
module.exports = __toCommonJS(src_exports); | ||
function src_default() { | ||
return "hello world"; | ||
} | ||
var src_default = {}; |
@@ -1,3 +0,3 @@ | ||
declare function export_default(): string; | ||
declare const _default: {}; | ||
export { export_default as default }; | ||
export { _default as default }; |
{ | ||
"name": "@midscene/shared", | ||
"version": "0.7.2-beta-20241024113439.0", | ||
"version": "0.7.2-beta-20241025012947.0", | ||
"repository": "https://github.com/web-infra-dev/midscene", | ||
"homepage": "https://midscenejs.com/", | ||
"types": "./dist/types/index.d.ts", | ||
"types": "./src/index.ts", | ||
"type": "commonjs", | ||
"main": "./dist/lib/index.js", | ||
@@ -11,21 +12,36 @@ "module": "./dist/es/index.js", | ||
".": { | ||
"types": "./dist/types/index.d.ts", | ||
"types": "./dist/lib/index.d.ts", | ||
"require": "./dist/lib/index.js", | ||
"import": "./dist/es/index.js", | ||
"require": "./dist/lib/index.js" | ||
"browser": "./dist/browser/index.js" | ||
}, | ||
"./constants": { | ||
"types": "./dist/types/constants.d.ts", | ||
"types": "./dist/lib/constants.d.ts", | ||
"require": "./dist/lib/constants.js", | ||
"import": "./dist/es/constants.js", | ||
"require": "./dist/lib/constants.js" | ||
"browser": "./dist/browser/constants.js" | ||
}, | ||
"./fs": { | ||
"types": "./dist/lib/fs.d.ts", | ||
"require": "./dist/lib/fs.js", | ||
"import": "./dist/es/fs.js", | ||
"browser": "./dist/browser/fs.js" | ||
}, | ||
"./img": { | ||
"types": "./dist/types/img.d.ts", | ||
"types": "./dist/lib/img.d.ts", | ||
"require": "./dist/lib/img.js", | ||
"import": "./dist/es/img.js", | ||
"require": "./dist/lib/img.js" | ||
"browser": "./dist/browser/img.js" | ||
}, | ||
"./fs": { | ||
"types": "./dist/types/fs.d.ts", | ||
"import": "./dist/es/fs.js", | ||
"require": "./dist/lib/fs.js" | ||
} | ||
"./utils": { | ||
"types": "./dist/lib/utils.d.ts", | ||
"require": "./dist/lib/utils.js", | ||
"import": "./dist/es/utils.js", | ||
"browser": "./dist/browser/utils.js" | ||
}, | ||
"./browser": "./dist/browser/index.js", | ||
"./browser/constants": "./dist/browser/constants.js", | ||
"./browser/fs": "./dist/browser/fs.js", | ||
"./browser/img": "./dist/browser/img.js", | ||
"./browser/utils": "./dist/browser/utils.js" | ||
}, | ||
@@ -35,13 +51,21 @@ "typesVersions": { | ||
".": [ | ||
"./dist/types/index.d.ts" | ||
"./src/index.ts" | ||
], | ||
"constants": [ | ||
"./dist/types/constants.d.ts" | ||
"./src/constants/index.ts" | ||
], | ||
"img": [ | ||
"./dist/types/img.d.ts" | ||
"./src/img/index.ts" | ||
], | ||
"fs": [ | ||
"./dist/types/fs.d.ts" | ||
"./src/fs/index.ts" | ||
], | ||
"utils": [ | ||
"./src/utils.ts" | ||
] | ||
}, | ||
"./browser": { | ||
"*": [ | ||
"./dist/browser/*" | ||
] | ||
} | ||
@@ -48,0 +72,0 @@ }, |
@@ -11,9 +11,14 @@ import assert from 'node:assert'; | ||
let pkg: PkgInfo | undefined; | ||
export function getMidscenePkgInfo(dir: string): PkgInfo { | ||
if (pkg) { | ||
return pkg; | ||
const pkgCacheMap: Record<string, PkgInfo> = {}; | ||
const ifInBrowser = typeof window !== 'undefined'; | ||
export function getRunningPkgInfo(dir?: string): PkgInfo | null { | ||
if (ifInBrowser) { | ||
return null; | ||
} | ||
const dirToCheck = dir || process.cwd(); | ||
if (pkgCacheMap[dirToCheck]) { | ||
return pkgCacheMap[dirToCheck]; | ||
} | ||
const pkgDir = findNearestPackageJson(dir || process.cwd()); | ||
const pkgDir = findNearestPackageJson(dirToCheck); | ||
assert(pkgDir, 'package.json not found'); | ||
@@ -24,7 +29,7 @@ const pkgJsonFile = join(pkgDir, 'package.json'); | ||
const { name, version } = JSON.parse(readFileSync(pkgJsonFile, 'utf-8')); | ||
pkg = { name, version, dir: pkgDir }; | ||
return pkg; | ||
pkgCacheMap[dirToCheck] = { name, version, dir: pkgDir }; | ||
return pkgCacheMap[dirToCheck]; | ||
} | ||
return { | ||
name: 'midscene-unknown-page-name', | ||
name: 'midscene-unknown-package-name', | ||
version: '0.0.0', | ||
@@ -31,0 +36,0 @@ dir: pkgDir, |
import assert from 'node:assert'; | ||
import { Buffer } from 'node:buffer'; | ||
import type { Buffer } from 'node:buffer'; | ||
import type { Rect } from '@/types'; | ||
import Jimp from 'jimp'; | ||
import type Jimp from 'jimp'; | ||
import type { NodeType } from '../constants'; | ||
import getJimp from './get-jimp'; | ||
import { bufferFromBase64, imageInfo } from './index'; | ||
@@ -35,2 +37,3 @@ // Define picture path | ||
) => { | ||
const Jimp = await getJimp(); | ||
const image = new Jimp(imageWidth, imageHeight, 0x00000000); | ||
@@ -196,6 +199,6 @@ | ||
}) => { | ||
const Jimp = await getJimp(); | ||
const { inputImgBase64, elementsPositionInfo } = options; | ||
const imageBuffer = Buffer.from(inputImgBase64, 'base64'); | ||
const image = await Jimp.read(imageBuffer); | ||
const { width, height } = image.bitmap; | ||
const imageBuffer = await bufferFromBase64(inputImgBase64); | ||
const { width, height } = await imageInfo(imageBuffer); | ||
@@ -202,0 +205,0 @@ if (!width || !height) { |
export { | ||
imageInfo, | ||
imageInfoOfBase64, | ||
bufferFromBase64, | ||
base64Encoded, | ||
@@ -5,0 +6,0 @@ base64ToPngFormat, |
import assert from 'node:assert'; | ||
import { Buffer } from 'node:buffer'; | ||
import { readFileSync } from 'node:fs'; | ||
import Jimp from 'jimp'; | ||
import getJimp from './get-jimp'; | ||
@@ -19,3 +19,4 @@ export interface Size { | ||
export async function imageInfo(image: string | Buffer): Promise<Size> { | ||
let jimpImage: Jimp; | ||
const Jimp = await getJimp(); | ||
let jimpImage; | ||
if (typeof image === 'string') { | ||
@@ -44,7 +45,13 @@ jimpImage = await Jimp.read(image); | ||
export async function imageInfoOfBase64(imageBase64: string): Promise<Size> { | ||
const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, ''); | ||
// const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, ''); | ||
// Call the imageInfo function to get the dimensions of the image | ||
return imageInfo(Buffer.from(base64Data, 'base64')); | ||
const buffer = await bufferFromBase64(imageBase64); | ||
return imageInfo(buffer); | ||
} | ||
export async function bufferFromBase64(imageBase64: string): Promise<Buffer> { | ||
const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, ''); | ||
return Buffer.from(base64Data, 'base64'); | ||
} | ||
/** | ||
@@ -51,0 +58,0 @@ * Encodes an image file to a base64 encoded string |
import { Buffer } from 'node:buffer'; | ||
import Jimp from 'jimp'; | ||
import getJimp from './get-jimp'; | ||
/** | ||
/** | ||
* Saves a Base64-encoded image to a file | ||
@@ -24,2 +25,3 @@ * | ||
// Use Jimp to process the image and save it to the specified location | ||
const Jimp = await getJimp(); | ||
const image = await Jimp.read(imageBuffer); | ||
@@ -36,2 +38,3 @@ await image.writeAsync(outputPath); | ||
// Use Jimp to process images and generate base64 data | ||
const Jimp = await getJimp(); | ||
const image = await Jimp.read(inputPath); | ||
@@ -61,2 +64,3 @@ const buffer = await image.getBufferAsync(Jimp.MIME_PNG); | ||
const Jimp = await getJimp(); | ||
const image = await Jimp.read(imageBuffer); | ||
@@ -133,2 +137,3 @@ const { width, height } = image.bitmap; | ||
} | null> { | ||
const Jimp = await getJimp(); | ||
const jimpImage = await Jimp.read( | ||
@@ -135,0 +140,0 @@ Buffer.isBuffer(image) ? image : Buffer.from(image), |
@@ -1,3 +0,1 @@ | ||
export default function () { | ||
return 'hello world'; | ||
} | ||
export default {}; |
99724
46
2611
7