Skip to content

Commit

Permalink
fix: remove resolution server middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Nov 29, 2020
1 parent 20dcd3c commit fc34a73
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 34 deletions.
7 changes: 1 addition & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path'
import fs from 'fs-extra'
import { ModuleOptions } from 'types'
import defu from 'defu'
import { downloadImage, getProviders, hash, resolutionServerMiddleware } from './utils'
import { downloadImage, getProviders, hash } from './utils'
import { cleanDoubleSlashes, getFileExtension } from './runtime/utils'
export type { Provider, RuntimeProvider } from 'types'

Expand Down Expand Up @@ -47,11 +47,6 @@ async function imageModule (moduleOptions: ModuleOptions) {
}
}

addServerMiddleware({
path: '/_image_resolution',
handler: resolutionServerMiddleware
})

addPlugin({
fileName: 'image.js',
src: path.resolve(__dirname, '../templates/plugin.js'),
Expand Down
25 changes: 14 additions & 11 deletions src/runtime/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,20 +185,23 @@ export function createImage (context, { providers, defaultProvider, presets, int
}

$img.getResolution = async (source: string, options: ImageOptions = {} as any) => {
const { image } = parseImage(source, options)
const { image } = parseImage(source, {
...options,
modifiers: {
...options.modifiers,
// these modifiers are used to reduce files size and keep resolution
format: 'jpg',
quality: 1
}
})

const internalUrl = context.ssrContext ? context.ssrContext.internalUrl : ''
const absoluteUrl = isRemoteUrl(image.url) ? image.url : internalUrl + image.url
try {
const resolution = await fetch(`${internalUrl}/_image_resolution?url=${absoluteUrl}`).then(res => res.json())
return resolution
} catch (err) {
// eslint-disable-next-line no-console
console.error('Failed to get image meta for ' + source, err + '')
return {
width: 1,
height: 0
}
const { width, height } = await getMeta(absoluteUrl, getCache(context))

return {
width,
height
}
}

Expand Down
17 changes: 0 additions & 17 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,3 @@ export function loadProvider (nuxt, key: string, provider: any) {
importName: 'runtime_' + hash(runtime).substr(0, 8)
}
}

export async function resolutionServerMiddleware (req, res) {
const reqURL = new URL(requrl(req))
const host = `http${isHttps(req) ? 's' : ''}://${reqURL.host}/`

const { searchParams } = new URL(req.url, host)
const imageAddress = url.format(new URL(searchParams.get('url'), host))

const imageMeta = require('image-meta').default
const data: Buffer = await fetch(imageAddress).then((res: any) => res.buffer())
const { width, height } = await imageMeta(data)
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({
width,
height
}))
}

0 comments on commit fc34a73

Please sign in to comment.