Skip to content

Commit

Permalink
Replace probe-image-size with image-dimensions and fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
fabon-f committed Dec 20, 2023
1 parent 06ca843 commit 6228ff5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 87 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ High-performance metadata scraper for Node.js

* Don't download whole contents to get site metadata.
* Fetch and parse the content of the `head` element only. Interrupt HTTP request when the `<body>` element starts.
* Download only first few kilobytes to determine image size (by `probe-image-size` package)
* Download only first few kilobytes to determine image size (by `image-dimensions` package)

## Install

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"dependencies": {
"entities": "^4.5.0",
"html-rewriter-wasm": "^0.4.1",
"probe-image-size": "^7.2.3"
"image-dimensions": "^2.3.0"
},
"devDependencies": {
"@tsconfig/strictest": "^2.0.1",
Expand Down
88 changes: 10 additions & 78 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/fetch-image-size.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { imageDimensionsFromStream } from 'image-dimensions'

export default async function fetchImageSize(url: string, options: RequestInit) {
const controller = new AbortController()
const response = await fetch(url, {
...options,
signal: controller.signal
})
if (!response.body) {
return undefined
}
const result = await imageDimensionsFromStream(response.body)
controller.abort()
return result
}
13 changes: 6 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import imageSize from 'probe-image-size'
import fetchImageSize from './fetch-image-size.js'

import extractMetadata from './extract-metadata.js'
import type { ImageInfo, Metadata } from './extract-metadata.js'
Expand Down Expand Up @@ -38,7 +38,7 @@ const defaultFavicon = async (baseUrl: string, fetchOptions: RequestInit) => {
return response.ok ? defaultFaviconUrl : undefined
}

const fetchImageInfo = async (info: ImageInfo) => {
const fetchImageInfo = async (info: ImageInfo, fetchOptions: RequestInit) => {
const intRegex = /0|[1-9][0-9]*/
if (info.width !== undefined && info.height !== undefined && intRegex.test(info.width) && intRegex.test(info.height)) {
return {
Expand All @@ -48,17 +48,16 @@ const fetchImageInfo = async (info: ImageInfo) => {
alt: info.alt
}
}
try {
const actualSize = await imageSize(info.src)
const actualSize = await fetchImageSize(info.src, fetchOptions)
if (actualSize) {
return {
src: info.src,
width: actualSize.width.toString(),
height: actualSize.height.toString(),
alt: info.alt
}
} catch {
return info
}
return info
}

type IntrinsicOptions = {
Expand Down Expand Up @@ -104,7 +103,7 @@ export default async function fetchSiteMetadata(url: string | URL, options: Opti

const [iconUrl, imageInfo] = await Promise.all([
typeof metadata.icon === 'string' ? Promise.resolve(metadata.icon) : defaultFavicon(urlString, fetchOptions),
metadata.image === undefined ? Promise.resolve(undefined) : fetchImageInfo(metadata.image)
metadata.image === undefined ? Promise.resolve(undefined) : fetchImageInfo(metadata.image, fetchOptions)
])

return {
Expand Down

0 comments on commit 6228ff5

Please sign in to comment.