Files
simplex-chat/packages/simplex-chat-nodejs/src/core.ts
T
fbca2cae81 core, ui: resize images in the core for sharper previews and images (#7941)
* WIP: Haskell impl of resizeImageToStrSize

* WIP: add FFI export wrapper

* WIP: switch ios resizeImageToStrSizeSync to backend

* WIP: fix resizeImageToStrSizeSync

* WIP: add JNI wrappers

* Add non-base64 resize and cWriteImage

* WIP: add chat_write_image

* fix resizeToStr

* fixes

* fix syntax, raise jpeg quality back

* add chatWriteImage

* Fix cfArgs in tests

* Use jpeg-turbo encoder

* add nix pins

* bump jpeg-turbo

* add CI deps

* add encoding funs to android lib

* bump jpeg-turbo for 20.04 fix

* jpeg-turbo fix for 8.10

* android fixes

* Bump jpeg-turbo

* set static flag for jpeg-turbo

* Fix nix for aarch64/android

* Fix android C api

* Fix resize in android

* Bump hs-libjpeg-turbo

* target correct override

* override android lib from android pkgs

* add macos build tweaks

* bump hs-jpeg-turbo for 8.10 fixes

* more build fixes

* make ios compile

* Always use pkgconfig for libjpeg-turbo

* WIP: debug libjpeg in ubuntu pkg-config CI

* Add pkg-config to dockerfiles

* Add libturbojpeg to dockerfiles

* Use -dev version of libturbojpeg in dockerfiles

* Install pkg-config in windows CI

* Disable flag for custom libturbojpeg linking options

Workarounds relocation error on x86_64-linux with gcc:
  /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/libturbojpeg.a(turbojpeg.c.o): relocation R_X86_64_TPOFF32 against `errStr' can not be used when making a shared object; recompile with -fPIC
  /usr/bin/ld: failed to set dynamic section sizes: bad value

* WIP: debug libjpeg in windows pkg-config CI

* WIP: debug libjpeg package in windows CI

* core: image resize module and buffer FFI

* ci: link libjpeg-turbo statically on all platforms

* ui: resize images in the core

* core: rename image resize functions to chat_resize_image and chat_resize_image_uri

* ci: restore whitespace in build.yml

* ci: build libjpeg-turbo 3.1.4.1 from a submodule

* core: hs-jpeg-turbo from github.com/simplex-chat

* nodejs: resize images

* python: resize images

* core: hs-jpeg-turbo from gitlab.com, the GitHub import lacks LFS objects

* core: generate /img previews

* ci: link libjpeg-turbo on Windows without pkg-config

* ci: build libjpeg-turbo on Windows with the GHC toolchain

* docs: plan for image resizing in the core

* docs: update image resizing plan

* ios: prevent crash when wallpaper image cannot be resized

* android, desktop: raise an error when an image cannot be resized

* docs: update spec references for image resizing

* docker: build the CLI image from a clone without submodules

* docs: apply review fixes to image resizing plan

* docker: build the CLI from a git worktree with the submodule present

* docs: apply second review to image resizing plan

* docs: point image resizing plan at the rewritten commit

* core: choose image format by transparency in chat_resize_image

* nodejs: resize images to the app sizes

* python: resize images to the app sizes

* nodejs, python: describe the core modules as used for image resizing

* docs: add license notices of the image libraries

* ci: describe the avatar method without attributing it to the apps

* ci, ios, nix: remove the libjpeg-turbo build outside cabal

* python: name resize_image in the core module text

* nodejs, python: describe resizing to the app image sizes

* core: refuse images larger than the supported size

* core: resize to JPEG images with a side above 65500 pixels

* docs: record image resizing decisions for the bundled libjpeg-turbo

* core, docs: build libjpeg-turbo and stb from the fork submodules

* docs: record the fork pins in the image resizing plan

* core: resize images to at most 4320 pixels per side, refuse above 16384

* docs: record the image side limits in the image resizing plan

* core: pin the rebased hs-jpeg-turbo fork

* core: pin the hs-jpeg-turbo fork without package.yaml

* docs: plan scaled JPEG decoding and EXIF orientation of images

* docs: copy the JuicyPixels-stbir licence of the pinned fork

* core: pin the hs-jpeg-turbo fork with scaled JPEG decoding

* core: decode JPEG at the display size, up to 65500 pixels per side

* core: rotate JPEG images by their EXIF orientation

* docs: refer the image plan to the JPEG decoding code

* docs: record the approved image processing decisions

* core: limit JPEG decoding memory to 1024 MB outside iOS and Android

* docs: name JPEG decoding in the libjpeg-turbo notice

* docs: plan image resizing from files and the decisions of 26 Oct

* core: scale images in one Catmull-Rom pass with sharpening

* docs: plan image sizes at a fixed quality

* core: resize images at a fixed quality to fill the size limit

* core: resize images from a file path or a file descriptor

* ios, android, desktop: resize picked images from their files

* core: encode resized images with mozjpeg

* tests: check descriptor closing by the file it refers to

* core: raise JPEG quality only for a full-size image below 97% of the limit

* ci: name the mozjpeg submodule in the Windows cleanup comments

* docs: refer the image plan to the current code

* docs: refer the image plan to the code merged with master

* core: export every image resizing function from each library

* core: close the file descriptor when its handle cannot be created

* docs: count the C sources of mozjpeg with the arithmetic decoder

* core: use the reviewed hs-jpeg-turbo and JuicyPixels-stbir forks

* ci: build the armv7a Android core library on request

* core: round image heights half up in the size search

* ios: describe the file image resizing functions in SimpleX.h

* ci: pick, preview and send images in the Claude app tests

* core: decode PNG images with libspng and refuse other formats

* core: encode PNG with libspng, keeping the smaller filter choice

* ci: pick, preview and send images in the scripted app smoke tests

* flake: build the PNG decoder package in nix builds

* ios: remove the copy of a picked image after sending it

* core: refuse image files larger than the decode memory limit

* core: scale a JPEG image to its largest size once per resize

* core: bisect JPEG quality without a step function

* core: reduce the memory used to sharpen and to decode PNG images

* android, desktop: pass the image format to the core as an integer

* android, desktop: fuse the preview conditions of picked media

* android, desktop, ci: check that the core resized the smoke test images

* python: remove the docstrings of the explicit image resizing functions

* docs: add the second review fixes to the image plan

* ci: tap the iOS attachment button by its accessibility label

* core: keep the colour profiles of resized images

* docs: add the colour profiles to the image plan

* ci: wait up to 30 s for the new group screens in the iOS smoke test

* ci: pick the Android smoke test images from their own photo folders

* ci: tap the iOS photo picker by position, as its tree is not shown

* ci: pick the test card and the logo by their iOS picker positions

* core: update JuicyPixels-stbir with the stb_image_resize licence terms

* core: write a fixed Display P3 profile for Display P3 images only

* core: do not keep the input image in memory for its colour profile

* core: read the JPEG segments once for the orientation and the profile

* core: read an image file into one buffer of its size

* docs: add the third review fixes to the image plan

* core: resize images with Display P3 or no profile, not profile bytes

* ci: check the pixel sizes of the images sent in the iOS smoke test

* core: update hs-jpeg-turbo with PNG chunk limits and encoder fixes

* docs: list Compact ICC Profiles, libspng and miniz in the dependencies

* android, desktop: reword the picked media preview comment

* docs: refer the image plan to the code merged with master

* ci: render the iOS snapshots before the images case

* ci: dismiss the snapshot tests' camera alert before the iOS images case

* core: update hs-jpeg-turbo with tests and the JuicyPixels-spng synopsis

* core: pin hs-jpeg-turbo and JuicyPixels-stbir in simplex-chat repositories

* update ios core library

* core, ui: accept only JPEG and PNG image data

* core: update JuicyPixels-stbir

* docs: plan for the iOS copy of picked images

---------

Co-authored-by: IC Rainbow <aenor.realm@gmail.com>
Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com>
Co-authored-by: Alexandre Esteves <alexfmpe@proton.me>
2026-10-30 12:14:51 +00:00

287 lines
8.7 KiB
TypeScript

import {ChatEvent, ChatResponse, T} from "@simplex-chat/types"
import * as libs from "./libs"
import * as simplex from "./simplex"
export type Backend = "sqlite" | "postgres"
let loading: {backend: Backend, promise: Promise<void>} | undefined
/**
* Resolve (downloading on first use) and load libsimplex for the backend.
* One libsimplex per process: the Haskell runtime is initialized once, and another backend is rejected.
*/
export function loadLibrary(backend: Backend): Promise<void> {
if (loading) {
if (loading.backend === backend) return loading.promise
return Promise.reject(new Error(`libsimplex already loaded with backend=${loading.backend}; cannot switch to ${backend} in the same process`))
}
const promise = Promise.all([libs.resolveLibsDir(backend), libs.resolveAddonPath()])
.then(([dir, addon]) => simplex.load(addon, libs.libPath(dir)))
loading = {backend, promise}
// a failed download or load can be retried
promise.catch(() => { loading = undefined })
return promise
}
/**
* Initialize chat controller
* @param {number} [queueSize] - Size of internal queues, the core default is used when omitted.
*/
export async function chatMigrateInit(dbPath: string, dbKey: string, confirm: MigrationConfirmation, queueSize?: number): Promise<bigint> {
const [ctrl, res] = queueSize === undefined
? await simplex.chat_migrate_init(dbPath, dbKey, confirm)
: await simplex.chat_migrate_init_queue(dbPath, dbKey, confirm, queueSize)
const json = JSON.parse(res)
if (json.type === 'ok') return ctrl
throw new ChatInitError("Database or migration error (see dbMigrationError property)", json as DBMigrationError)
}
/**
* Close chat store
*/
export async function chatCloseStore(ctrl: bigint): Promise<void> {
const res = await simplex.chat_close_store(ctrl)
if (res !== "") throw new Error(res)
}
/**
* Send chat command as string
*/
export async function chatSendCmd(ctrl: bigint, cmd: string): Promise<ChatResponse> {
const res = await simplex.chat_send_cmd(ctrl, cmd)
const json = JSON.parse(res) as APIResult<ChatResponse>
// console.log(cmd.slice(0, 16), json.result?.type || json.error)
if (typeof json.result === 'object') return json.result
if (typeof json.error === 'object') throw new ChatAPIError("Chat command error (see chatError property)", json.error as T.ChatError)
throw new ChatAPIError("Invalid chat command result")
}
/**
* Receive chat event
*/
export async function chatRecvMsgWait(ctrl: bigint, wait: number): Promise<ChatEvent | undefined> {
const res = await simplex.chat_recv_msg_wait(ctrl, wait)
if (res === "") return undefined
const json = JSON.parse(res) as APIResult<ChatEvent>
// if (json.result) console.log("event", json.result.type)
if (typeof json.result === 'object') return json.result
if (typeof json.error === 'object') throw new ChatAPIError("Chat event error (see chatError property)", json.error as T.ChatError)
throw new ChatAPIError("Invalid chat event")
}
/**
* Write buffer to encrypted file
*/
export async function chatWriteFile(ctrl: bigint, path: string, buffer: ArrayBuffer | Uint8Array): Promise<CryptoArgs> {
const res = await simplex.chat_write_file(ctrl, path, buffer)
return cryptoArgsResult(res)
}
/**
* Read buffer from encrypted file
*/
export async function chatReadFile(path: string, {fileKey, fileNonce}: CryptoArgs): Promise<Buffer> {
return await simplex.chat_read_file(path, fileKey, fileNonce)
}
/**
* Encrypt file
*/
export async function chatEncryptFile(ctrl: bigint, fromPath: string, toPath: string): Promise<CryptoArgs> {
const res = await simplex.chat_encrypt_file(ctrl, fromPath, toPath)
return cryptoArgsResult(res)
}
/**
* Decrypt file
*/
export async function chatDecryptFile(fromPath: string, {fileKey, fileNonce}: CryptoArgs, toPath: string): Promise<void> {
const res = await simplex.chat_decrypt_file(fromPath, fileKey, fileNonce, toPath)
if (res !== "") throw new Error(res)
}
function cryptoArgsResult(res: string): CryptoArgs {
const json = JSON.parse(res)
switch (json.type) {
case "result": return json.cryptoArgs as CryptoArgs
case "error": throw Error(json.writeError)
default: throw Error("unexpected chat_write_file result: " + res)
}
}
/**
* Resize image to a data URI of at most maxSize characters
*/
export async function chatResizeImageUri(image: ArrayBuffer | Uint8Array, maxSize: number): Promise<string> {
return resizedImage(await simplex.chat_resize_image_uri(image, maxSize))
}
/**
* Resize image to JPEG or PNG of at most maxSize bytes
*/
export async function chatResizeImage(image: ArrayBuffer | Uint8Array, maxSize: number, format: "jpeg" | "png"): Promise<Buffer> {
return resizeImageData(image, maxSize, format === "png" ? 1 : 0)
}
/**
* Resize image to the app size limit: JPEG or PNG for an image, a data URI for a preview
*/
export function resizeImage(image: ArrayBuffer | Uint8Array, size: ImageSize.Image): Promise<Buffer>
export function resizeImage(image: ArrayBuffer | Uint8Array, size: ImageSize.ProfilePreview | ImageSize.MessagePreview): Promise<string>
export function resizeImage(image: ArrayBuffer | Uint8Array, size: ImageSize): Promise<Buffer | string>
export async function resizeImage(image: ArrayBuffer | Uint8Array, size: ImageSize): Promise<Buffer | string> {
switch (size) {
case ImageSize.Image: return resizeImageData(image, 261120, -1)
case ImageSize.ProfilePreview: return chatResizeImageUri(image, 12500)
case ImageSize.MessagePreview: return chatResizeImageUri(image, 14000)
}
}
async function resizeImageData(image: ArrayBuffer | Uint8Array, maxSize: number, png: number): Promise<Buffer> {
return Buffer.from(resizedImage(await simplex.chat_resize_image(image, maxSize, png)), "base64")
}
function resizedImage(res: string): string {
if (res === "") throw new Error("image cannot be decoded or does not fit maxSize")
return res
}
export interface APIResult<R> {
result?: R
error?: T.ChatError
}
export class ChatAPIError extends Error {
constructor(public message: string, public chatError: T.ChatError | undefined = undefined) {
super(message)
}
}
/**
* Migration confirmation mode
*/
export enum MigrationConfirmation {
YesUp = "yesUp",
YesUpDown = "yesUpDown",
Console = "console",
Error = "error"
}
/**
* App image size limit
*/
export enum ImageSize {
Image = "image",
ProfilePreview = "profilePreview",
MessagePreview = "messagePreview"
}
/**
* File encryption key and nonce
*/
export interface CryptoArgs {
fileKey: string
fileNonce: string
}
export class ChatInitError extends Error {
constructor(public message: string, public dbMigrationError: DBMigrationError) {
super(message)
}
}
export type DBMigrationError =
| DBMigrationError.InvalidConfirmation
| DBMigrationError.InvalidQueueSize
| DBMigrationError.ErrorNotADatabase // invalid/corrupt database file or incorrect encryption key
| DBMigrationError.ErrorMigration
| DBMigrationError.ErrorSQL
export namespace DBMigrationError {
export type Tag = "invalidConfirmation" | "invalidQueueSize" | "errorNotADatabase" | "errorMigration" | "errorSQL"
interface Interface {
type: Tag
}
export interface InvalidConfirmation extends Interface {
type: "invalidConfirmation"
}
export interface InvalidQueueSize extends Interface {
type: "invalidQueueSize"
}
export interface ErrorNotADatabase extends Interface {
type: "errorNotADatabase"
dbFile: string
}
export interface ErrorMigration extends Interface {
type: "errorMigration"
dbFile: string
migrationError: MigrationError
}
export interface ErrorSQL extends Interface {
type: "errorSQL"
dbFile: string
migrationSQLError: string
}
}
export type MigrationError =
| MigrationError.MEUpgrade
| MigrationError.MEDowngrade
| MigrationError.MigrationError
export namespace MigrationError {
export type Tag = "upgrade" | "downgrade" | "migrationError"
interface Interface {
type: Tag
}
export interface MEUpgrade extends Interface {
type: "upgrade"
upMigrations: UpMigration[]
}
export interface MEDowngrade extends Interface {
type: "downgrade"
downMigrations: string[]
}
export interface MigrationError extends Interface {
type: "migrationError"
mtrError: MTRError
}
}
export interface UpMigration {
upName: string
withDown: boolean
}
export type MTRError =
| MTRError.MTRENoDown
| MTRError.MTREDifferent
export namespace MTRError {
export type Tag = "noDown" | "different"
interface Interface {
type: Tag
}
export interface MTRENoDown extends Interface {
type: "noDown"
dbMigrations: string[]
}
export interface MTREDifferent extends Interface {
type: "different"
downMigrations: string[]
}
}