/
githubmirror
/
tesseract.js
Обзор
Документация
Войти
/
githubmirror
/
tesseract.js
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/worker/node/loadImage.js
38 строк
1 KB
Balearica
Limited use of node-fetch package to Node.js versions that require it (#1013)
05 апр 2025, 04:25
Не верифицирован
05 апр 2025, 04:25
8322fee
Код
Авторство
О чём код?
'use strict'; const util = require('util'); const fs = require('fs'); // Use built-in fetch if available, otherwise fallback to node-fetch const fetch = global.fetch || require('node-fetch'); const isURL = require('is-url'); const readFile = util.promisify(fs.readFile); /** * loadImage * * @name loadImage * @function load image from different source * @access public */ module.exports = async (image) => { let data = image; if (typeof image === 'undefined') { return image; } if (typeof image === 'string') { if (isURL(image) || image.startsWith('moz-extension://') || image.startsWith('chrome-extension://') || image.startsWith('file://')) { const resp = await fetch(image); data = await resp.arrayBuffer(); } else if (/data:image\/([a-zA-Z]*);base64,([^"]*)/.test(image)) { data = Buffer.from(image.split(',')[1], 'base64'); } else { data = await readFile(image); } } else if (Buffer.isBuffer(image)) { data = image; } return new Uint8Array(data); };