@midscene/shared
Advanced tools
Comparing version 0.7.2-beta-20241024103907.0 to 0.7.2-beta-20241024113439.0
@@ -6,7 +6,3 @@ // src/fs/index.ts | ||
var pkg; | ||
var ifInBrowser = typeof window !== "undefined"; | ||
function getRunningPkgInfo(dir) { | ||
if (ifInBrowser) { | ||
return null; | ||
} | ||
function getMidscenePkgInfo(dir) { | ||
if (pkg) { | ||
@@ -24,3 +20,3 @@ return pkg; | ||
return { | ||
name: "midscene-unknown-package-name", | ||
name: "midscene-unknown-page-name", | ||
version: "0.0.0", | ||
@@ -43,3 +39,3 @@ dir: pkgDir | ||
findNearestPackageJson, | ||
getRunningPkgInfo | ||
getMidscenePkgInfo | ||
}; |
@@ -1,70 +0,26 @@ | ||
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 as Buffer2 } from "buffer"; | ||
import { Buffer } from "buffer"; | ||
import { readFileSync } from "fs"; | ||
// 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; | ||
}); | ||
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/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 }; | ||
}); | ||
async function imageInfoOfBase64(imageBase64) { | ||
const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, ""); | ||
return imageInfo(Buffer.from(base64Data, "base64")); | ||
} | ||
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) { | ||
@@ -88,36 +44,28 @@ const imageBuffer = readFileSync(image); | ||
// src/img/transform.ts | ||
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); | ||
}); | ||
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); | ||
} | ||
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 transformImgPathToBase64(inputPath) { | ||
const image = await Jimp2.read(inputPath); | ||
const buffer = await image.getBufferAsync(Jimp2.MIME_PNG); | ||
return buffer.toString("base64"); | ||
} | ||
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; | ||
}); | ||
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; | ||
} | ||
@@ -143,26 +91,23 @@ function calculateNewDimensions(originalWidth, originalHeight) { | ||
} | ||
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 | ||
}; | ||
}); | ||
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 | ||
}; | ||
} | ||
@@ -172,6 +117,7 @@ | ||
import assert2 from "assert"; | ||
import { Buffer as Buffer3 } from "buffer"; | ||
import Jimp3 from "jimp"; | ||
var createSvgOverlay = (elements, imageWidth, imageHeight) => { | ||
const createPngOverlay = (elements2, imageWidth2, imageHeight2) => __async(void 0, null, function* () { | ||
const Jimp = yield getJimp(); | ||
const image = new Jimp(imageWidth2, imageHeight2, 0); | ||
const createPngOverlay = async (elements2, imageWidth2, imageHeight2) => { | ||
const image = new Jimp3(imageWidth2, imageHeight2, 0); | ||
const colors = [ | ||
@@ -257,3 +203,3 @@ { rect: 4278190335, text: 4294967295 } | ||
}); | ||
const font = yield Jimp.loadFont(Jimp.FONT_SANS_16_WHITE); | ||
const font = await Jimp3.loadFont(Jimp3.FONT_SANS_16_WHITE); | ||
image.print( | ||
@@ -265,4 +211,4 @@ font, | ||
text: element.indexId.toString(), | ||
alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER, | ||
alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE | ||
alignmentX: Jimp3.HORIZONTAL_ALIGN_CENTER, | ||
alignmentY: Jimp3.VERTICAL_ALIGN_MIDDLE | ||
}, | ||
@@ -273,15 +219,15 @@ rectWidth, | ||
} | ||
return image.getBufferAsync(Jimp.MIME_PNG); | ||
}); | ||
return image.getBufferAsync(Jimp3.MIME_PNG); | ||
}; | ||
return createPngOverlay(elements, imageWidth, imageHeight); | ||
}; | ||
var compositeElementInfoImg = (options) => __async(void 0, null, function* () { | ||
const Jimp = yield getJimp(); | ||
var compositeElementInfoImg = async (options) => { | ||
const { inputImgBase64, elementsPositionInfo } = options; | ||
const imageBuffer = yield bufferFromBase64(inputImgBase64); | ||
const { width, height } = yield imageInfo(imageBuffer); | ||
const imageBuffer = Buffer3.from(inputImgBase64, "base64"); | ||
const image = await Jimp3.read(imageBuffer); | ||
const { width, height } = image.bitmap; | ||
if (!width || !height) { | ||
throw Error("Image processing failed because width or height is undefined"); | ||
} | ||
const svgOverlay = yield createSvgOverlay( | ||
const svgOverlay = await createSvgOverlay( | ||
elementsPositionInfo, | ||
@@ -291,11 +237,11 @@ width, | ||
); | ||
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, | ||
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, | ||
opacitySource: 1, | ||
opacityDest: 1 | ||
}); | ||
})).then((compositeImage) => { | ||
return compositeImage.getBufferAsync(Jimp.MIME_PNG); | ||
}).then((compositeImage) => { | ||
return compositeImage.getBufferAsync(Jimp3.MIME_PNG); | ||
}).then((buffer) => { | ||
@@ -306,4 +252,4 @@ return buffer.toString("base64"); | ||
}); | ||
}); | ||
var processImageElementInfo = (options) => __async(void 0, null, function* () { | ||
}; | ||
var processImageElementInfo = async (options) => { | ||
const base64Image = options.inputImgBase64.split(";base64,").pop(); | ||
@@ -314,3 +260,3 @@ assert2(base64Image, "base64Image is undefined"); | ||
compositeElementInfoImgWithoutTextBase64 | ||
] = yield Promise.all([ | ||
] = await Promise.all([ | ||
compositeElementInfoImg({ | ||
@@ -329,7 +275,6 @@ inputImgBase64: options.inputImgBase64, | ||
}; | ||
}); | ||
}; | ||
export { | ||
base64Encoded, | ||
base64ToPngFormat, | ||
bufferFromBase64, | ||
calculateNewDimensions, | ||
@@ -336,0 +281,0 @@ compositeElementInfoImg, |
// src/index.ts | ||
var src_default = {}; | ||
function src_default() { | ||
return "hello world"; | ||
} | ||
export { | ||
src_default as default | ||
}; |
@@ -34,3 +34,3 @@ "use strict"; | ||
findNearestPackageJson: () => findNearestPackageJson, | ||
getRunningPkgInfo: () => getRunningPkgInfo | ||
getMidscenePkgInfo: () => getMidscenePkgInfo | ||
}); | ||
@@ -42,7 +42,3 @@ module.exports = __toCommonJS(fs_exports); | ||
var pkg; | ||
var ifInBrowser = typeof window !== "undefined"; | ||
function getRunningPkgInfo(dir) { | ||
if (ifInBrowser) { | ||
return null; | ||
} | ||
function getMidscenePkgInfo(dir) { | ||
if (pkg) { | ||
@@ -60,3 +56,3 @@ return pkg; | ||
return { | ||
name: "midscene-unknown-package-name", | ||
name: "midscene-unknown-page-name", | ||
version: "0.0.0", | ||
@@ -80,3 +76,3 @@ dir: pkgDir | ||
findNearestPackageJson, | ||
getRunningPkgInfo | ||
getMidscenePkgInfo | ||
}); |
@@ -29,22 +29,2 @@ "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()); | ||
}); | ||
}; | ||
@@ -56,3 +36,2 @@ // src/img/index.ts | ||
base64ToPngFormat: () => base64ToPngFormat, | ||
bufferFromBase64: () => bufferFromBase64, | ||
calculateNewDimensions: () => calculateNewDimensions, | ||
@@ -74,46 +53,23 @@ compositeElementInfoImg: () => compositeElementInfoImg, | ||
var import_node_fs = require("fs"); | ||
// 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; | ||
}); | ||
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/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 }; | ||
}); | ||
async function imageInfoOfBase64(imageBase64) { | ||
const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, ""); | ||
return imageInfo(import_node_buffer.Buffer.from(base64Data, "base64")); | ||
} | ||
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) { | ||
@@ -138,35 +94,27 @@ const imageBuffer = (0, import_node_fs.readFileSync)(image); | ||
var import_node_buffer2 = require("buffer"); | ||
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); | ||
}); | ||
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 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 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 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; | ||
}); | ||
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; | ||
} | ||
@@ -192,26 +140,23 @@ function calculateNewDimensions(originalWidth, originalHeight) { | ||
} | ||
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 | ||
}; | ||
}); | ||
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 | ||
}; | ||
} | ||
@@ -221,6 +166,7 @@ | ||
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 = (elements2, imageWidth2, imageHeight2) => __async(void 0, null, function* () { | ||
const Jimp = yield getJimp(); | ||
const image = new Jimp(imageWidth2, imageHeight2, 0); | ||
const createPngOverlay = async (elements2, imageWidth2, imageHeight2) => { | ||
const image = new import_jimp3.default(imageWidth2, imageHeight2, 0); | ||
const colors = [ | ||
@@ -306,3 +252,3 @@ { rect: 4278190335, text: 4294967295 } | ||
}); | ||
const font = yield Jimp.loadFont(Jimp.FONT_SANS_16_WHITE); | ||
const font = await import_jimp3.default.loadFont(import_jimp3.default.FONT_SANS_16_WHITE); | ||
image.print( | ||
@@ -314,4 +260,4 @@ font, | ||
text: element.indexId.toString(), | ||
alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER, | ||
alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE | ||
alignmentX: import_jimp3.default.HORIZONTAL_ALIGN_CENTER, | ||
alignmentY: import_jimp3.default.VERTICAL_ALIGN_MIDDLE | ||
}, | ||
@@ -322,15 +268,15 @@ rectWidth, | ||
} | ||
return image.getBufferAsync(Jimp.MIME_PNG); | ||
}); | ||
return image.getBufferAsync(import_jimp3.default.MIME_PNG); | ||
}; | ||
return createPngOverlay(elements, imageWidth, imageHeight); | ||
}; | ||
var compositeElementInfoImg = (options) => __async(void 0, null, function* () { | ||
const Jimp = yield getJimp(); | ||
var compositeElementInfoImg = async (options) => { | ||
const { inputImgBase64, elementsPositionInfo } = options; | ||
const imageBuffer = yield bufferFromBase64(inputImgBase64); | ||
const { width, height } = yield imageInfo(imageBuffer); | ||
const imageBuffer = import_node_buffer3.Buffer.from(inputImgBase64, "base64"); | ||
const image = await import_jimp3.default.read(imageBuffer); | ||
const { width, height } = image.bitmap; | ||
if (!width || !height) { | ||
throw Error("Image processing failed because width or height is undefined"); | ||
} | ||
const svgOverlay = yield createSvgOverlay( | ||
const svgOverlay = await createSvgOverlay( | ||
elementsPositionInfo, | ||
@@ -340,11 +286,11 @@ width, | ||
); | ||
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, | ||
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, | ||
opacitySource: 1, | ||
opacityDest: 1 | ||
}); | ||
})).then((compositeImage) => { | ||
return compositeImage.getBufferAsync(Jimp.MIME_PNG); | ||
}).then((compositeImage) => { | ||
return compositeImage.getBufferAsync(import_jimp3.default.MIME_PNG); | ||
}).then((buffer) => { | ||
@@ -355,4 +301,4 @@ return buffer.toString("base64"); | ||
}); | ||
}); | ||
var processImageElementInfo = (options) => __async(void 0, null, function* () { | ||
}; | ||
var processImageElementInfo = async (options) => { | ||
const base64Image = options.inputImgBase64.split(";base64,").pop(); | ||
@@ -363,3 +309,3 @@ (0, import_node_assert2.default)(base64Image, "base64Image is undefined"); | ||
compositeElementInfoImgWithoutTextBase64 | ||
] = yield Promise.all([ | ||
] = await Promise.all([ | ||
compositeElementInfoImg({ | ||
@@ -378,3 +324,3 @@ inputImgBase64: options.inputImgBase64, | ||
}; | ||
}); | ||
}; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -384,3 +330,2 @@ 0 && (module.exports = { | ||
base64ToPngFormat, | ||
bufferFromBase64, | ||
calculateNewDimensions, | ||
@@ -387,0 +332,0 @@ compositeElementInfoImg, |
@@ -26,2 +26,4 @@ "use strict"; | ||
module.exports = __toCommonJS(src_exports); | ||
var src_default = {}; | ||
function src_default() { | ||
return "hello world"; | ||
} |
@@ -1,3 +0,3 @@ | ||
declare const _default: {}; | ||
declare function export_default(): string; | ||
export { _default as default }; | ||
export { export_default as default }; |
{ | ||
"name": "@midscene/shared", | ||
"version": "0.7.2-beta-20241024103907.0", | ||
"version": "0.7.2-beta-20241024113439.0", | ||
"repository": "https://github.com/web-infra-dev/midscene", | ||
"homepage": "https://midscenejs.com/", | ||
"types": "./src/index.ts", | ||
"type": "commonjs", | ||
"types": "./dist/types/index.d.ts", | ||
"main": "./dist/lib/index.js", | ||
@@ -12,36 +11,21 @@ "module": "./dist/es/index.js", | ||
".": { | ||
"types": "./dist/lib/index.d.ts", | ||
"require": "./dist/lib/index.js", | ||
"types": "./dist/types/index.d.ts", | ||
"import": "./dist/es/index.js", | ||
"browser": "./dist/browser/index.js" | ||
"require": "./dist/lib/index.js" | ||
}, | ||
"./constants": { | ||
"types": "./dist/lib/constants.d.ts", | ||
"require": "./dist/lib/constants.js", | ||
"types": "./dist/types/constants.d.ts", | ||
"import": "./dist/es/constants.js", | ||
"browser": "./dist/browser/constants.js" | ||
"require": "./dist/lib/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/lib/img.d.ts", | ||
"require": "./dist/lib/img.js", | ||
"types": "./dist/types/img.d.ts", | ||
"import": "./dist/es/img.js", | ||
"browser": "./dist/browser/img.js" | ||
"require": "./dist/lib/img.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" | ||
"./fs": { | ||
"types": "./dist/types/fs.d.ts", | ||
"import": "./dist/es/fs.js", | ||
"require": "./dist/lib/fs.js" | ||
} | ||
}, | ||
@@ -51,21 +35,13 @@ "typesVersions": { | ||
".": [ | ||
"./src/index.ts" | ||
"./dist/types/index.d.ts" | ||
], | ||
"constants": [ | ||
"./src/constants/index.ts" | ||
"./dist/types/constants.d.ts" | ||
], | ||
"img": [ | ||
"./src/img/index.ts" | ||
"./dist/types/img.d.ts" | ||
], | ||
"fs": [ | ||
"./src/fs/index.ts" | ||
], | ||
"utils": [ | ||
"./src/utils.ts" | ||
"./dist/types/fs.d.ts" | ||
] | ||
}, | ||
"./browser": { | ||
"*": [ | ||
"./dist/browser/*" | ||
] | ||
} | ||
@@ -72,0 +48,0 @@ }, |
@@ -12,7 +12,3 @@ import assert from 'node:assert'; | ||
let pkg: PkgInfo | undefined; | ||
const ifInBrowser = typeof window !== 'undefined'; | ||
export function getRunningPkgInfo(dir?: string): PkgInfo | null { | ||
if (ifInBrowser) { | ||
return null; | ||
} | ||
export function getMidscenePkgInfo(dir: string): PkgInfo { | ||
if (pkg) { | ||
@@ -32,3 +28,3 @@ return pkg; | ||
return { | ||
name: 'midscene-unknown-package-name', | ||
name: 'midscene-unknown-page-name', | ||
version: '0.0.0', | ||
@@ -35,0 +31,0 @@ dir: pkgDir, |
import assert from 'node:assert'; | ||
import type { Buffer } from 'node:buffer'; | ||
import { Buffer } from 'node:buffer'; | ||
import type { Rect } from '@/types'; | ||
import type Jimp from 'jimp'; | ||
import Jimp from 'jimp'; | ||
import type { NodeType } from '../constants'; | ||
import getJimp from './get-jimp'; | ||
import { bufferFromBase64, imageInfo } from './index'; | ||
@@ -37,3 +35,2 @@ // Define picture path | ||
) => { | ||
const Jimp = await getJimp(); | ||
const image = new Jimp(imageWidth, imageHeight, 0x00000000); | ||
@@ -199,6 +196,6 @@ | ||
}) => { | ||
const Jimp = await getJimp(); | ||
const { inputImgBase64, elementsPositionInfo } = options; | ||
const imageBuffer = await bufferFromBase64(inputImgBase64); | ||
const { width, height } = await imageInfo(imageBuffer); | ||
const imageBuffer = Buffer.from(inputImgBase64, 'base64'); | ||
const image = await Jimp.read(imageBuffer); | ||
const { width, height } = image.bitmap; | ||
@@ -205,0 +202,0 @@ if (!width || !height) { |
export { | ||
imageInfo, | ||
imageInfoOfBase64, | ||
bufferFromBase64, | ||
base64Encoded, | ||
@@ -6,0 +5,0 @@ base64ToPngFormat, |
import assert from 'node:assert'; | ||
import { Buffer } from 'node:buffer'; | ||
import { readFileSync } from 'node:fs'; | ||
import getJimp from './get-jimp'; | ||
import Jimp from 'jimp'; | ||
@@ -19,4 +19,3 @@ export interface Size { | ||
export async function imageInfo(image: string | Buffer): Promise<Size> { | ||
const Jimp = await getJimp(); | ||
let jimpImage; | ||
let jimpImage: Jimp; | ||
if (typeof image === 'string') { | ||
@@ -45,13 +44,7 @@ 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 | ||
const buffer = await bufferFromBase64(imageBase64); | ||
return imageInfo(buffer); | ||
return imageInfo(Buffer.from(base64Data, 'base64')); | ||
} | ||
export async function bufferFromBase64(imageBase64: string): Promise<Buffer> { | ||
const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, ''); | ||
return Buffer.from(base64Data, 'base64'); | ||
} | ||
/** | ||
@@ -58,0 +51,0 @@ * Encodes an image file to a base64 encoded string |
import { Buffer } from 'node:buffer'; | ||
import getJimp from './get-jimp'; | ||
import Jimp from 'jimp'; | ||
/** | ||
/** | ||
* Saves a Base64-encoded image to a file | ||
@@ -25,3 +24,2 @@ * | ||
// Use Jimp to process the image and save it to the specified location | ||
const Jimp = await getJimp(); | ||
const image = await Jimp.read(imageBuffer); | ||
@@ -38,3 +36,2 @@ await image.writeAsync(outputPath); | ||
// Use Jimp to process images and generate base64 data | ||
const Jimp = await getJimp(); | ||
const image = await Jimp.read(inputPath); | ||
@@ -64,3 +61,2 @@ const buffer = await image.getBufferAsync(Jimp.MIME_PNG); | ||
const Jimp = await getJimp(); | ||
const image = await Jimp.read(imageBuffer); | ||
@@ -137,3 +133,2 @@ const { width, height } = image.bitmap; | ||
} | null> { | ||
const Jimp = await getJimp(); | ||
const jimpImage = await Jimp.read( | ||
@@ -140,0 +135,0 @@ Buffer.isBuffer(image) ? image : Buffer.from(image), |
@@ -1,1 +0,3 @@ | ||
export default {}; | ||
export default function () { | ||
return 'hello world'; | ||
} |
5
60187
24
1562
196
33
196