Skip to content

Commit

Permalink
feat: add support for quality and position args (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic authored Mar 1, 2022
1 parent 7567507 commit 1f65eb7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@netlify/functions": "^0.7.2",
"etag": "^1.8.1",
"fs-extra": "^10.0.0",
"ipx": "^0.9.2",
"ipx": "^0.9.4",
"mkdirp": "^1.0.4",
"murmurhash": "^2.0.0",
"node-fetch": "^2.0.0",
Expand Down
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import { decodeBase64Params } from './utils'

export function createIPXHandler ({
cacheDir = join(tmpdir(), 'ipx-cache'),
basePath = '/_ipx',
basePath = '/_ipx/',
propsEncoding,
...opts
}: Partial<IPXOptions> & { cacheDir?: string; basePath?: string, propsEncoding?: 'base64' } = {}) {
const ipx = createIPX({ ...opts, dir: join(cacheDir, 'cache') })

if (!basePath.endsWith('/')) {
basePath = `${basePath}/`
}
const handler: Handler = async (event, _context) => {
const host = event.headers.host
const protocol = event.headers['x-forwarded-proto'] || 'http'
Expand All @@ -23,7 +25,7 @@ export function createIPXHandler ({
const url = event.path.replace(basePath, '')

// eslint-disable-next-line prefer-const
let [modifiers = '_', ...segments] = url.slice(1).split('/')
let [modifiers = '_', ...segments] = url.split('/')
let id = decodeURIComponent(segments.join('/'))

if (propsEncoding === 'base64') {
Expand Down
18 changes: 14 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,26 @@ export function decodeBase64Params (path:string) {

// [ipx modifier name, gatsby modifier name]
const props = [
['width', 'w'],
['height', 'h'],
['format', 'fm']
['f', 'fm'],
['crop', 'pos'],
['q', 'q']
]

const modifiers: Array<string> = []
const w = params.get('w')
const h = params.get('h')
if (w && h) {
modifiers.push(`s_${w}x${h}`)
} else {
props.push(['w', 'w'], ['h', 'h'])
}

for (const [modifier, prop] of props) {
const value = params.get(prop)
let value = params.get(prop)
if (value) {
if (prop === 'pos') {
value = value.replace(',', ' ')
}
modifiers.push(`${modifier}_${value}`)
}
}
Expand Down
64 changes: 32 additions & 32 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -793,10 +793,10 @@ color-string@^1.9.0:
color-name "^1.0.0"
simple-swizzle "^0.2.2"

color@^4.0.1:
version "4.2.0"
resolved "https://registry.yarnpkg.com/color/-/color-4.2.0.tgz#0c782459a3e98838ea01e4bc0fb43310ca35af78"
integrity sha512-hHTcrbvEnGjC7WBMk6ibQWFVDgEFTVmjrz2Q5HlU6ltwxv0JJN2Z8I7uRbWeQLF04dikxs8zgyZkazRJvSMtyQ==
color@^4.2.0:
version "4.2.1"
resolved "https://registry.yarnpkg.com/color/-/color-4.2.1.tgz#498aee5fce7fc982606c8875cab080ac0547c884"
integrity sha512-MFJr0uY4RvTQUKvPq7dh9grVOTYSFeXja2mBXioCGjnjJoXrAp9jJ1NQTDR73c9nwBSAQiNKloKl5zq9WB9UPw==
dependencies:
color-convert "^2.0.1"
color-string "^1.9.0"
Expand Down Expand Up @@ -1144,6 +1144,11 @@ defu@^5.0.0:
resolved "https://registry.yarnpkg.com/defu/-/defu-5.0.0.tgz#5768f0d402a555bfc4c267246b20f82ce8b5a10b"
integrity sha512-VHg73EDeRXlu7oYWRmmrNp/nl7QkdXUxkQQKig0Zk8daNmm84AbGoC8Be6/VVLJEKxn12hR0UBmz8O+xQiAPKQ==

defu@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/defu/-/defu-5.0.1.tgz#a034278f9b032bf0845d261aa75e9ad98da878ac"
integrity sha512-EPS1carKg+dkEVy3qNTqIdp2qV7mUP08nIsupfwQpz++slCVRw7qbQyWvSTig+kFPwz2XXp5/kIIkH+CwrJKkQ==

delayed-stream@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
Expand All @@ -1169,11 +1174,6 @@ detect-indent@^6.0.0:
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-6.1.0.tgz#592485ebbbf6b3b1ab2be175c8393d04ca0d57e6"
integrity sha512-reYkTUJAZb9gUuZ2RvVCNhVHdg62RHnJ7WJl8ftMi4diZ6NWlciOzQN88pUhSELEwflJht4oQDv0F0BMlwaYtA==

detect-libc@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-1.0.3.tgz#fa137c4bd698edf55cd5cd02ac559f91a4c4ba9b"
integrity sha1-+hN8S9aY7fVc1c0CrFWfkaTEups=

detect-libc@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-2.0.0.tgz#c528bc09bc6d1aa30149228240917c225448f204"
Expand Down Expand Up @@ -2039,22 +2039,22 @@ ioredis@^4.27.9:
redis-parser "^3.0.0"
standard-as-callback "^2.1.0"

ipx@^0.9.2:
version "0.9.2"
resolved "https://registry.yarnpkg.com/ipx/-/ipx-0.9.2.tgz#91c43de659df36c5082944b1145ad050d852513c"
integrity sha512-AxFfZUBobGvqepvqjznzq7VElind7cecYwmxv/E21kHzpFHXHusiNNpGzTbK6NpwWY3E0JhWmqBAUDx3slSUKg==
ipx@^0.9.4:
version "0.9.4"
resolved "https://registry.yarnpkg.com/ipx/-/ipx-0.9.4.tgz#0f89b950e59b7e4b04ebc0ea3cbed19fab712e2a"
integrity sha512-DsdnKPkV0nJMui8Mi68L+yPyokCinh9N93SrstozNQW+O5J4tXvwG+1tpDxWgmYsU89zLdVZMabo5lQqXr5KhA==
dependencies:
consola "^2.15.3"
defu "^5.0.0"
defu "^5.0.1"
destr "^1.1.0"
etag "^1.8.1"
image-meta "^0.1.1"
is-valid-path "^0.1.1"
listhen "^0.2.5"
ohmyfetch "^0.4.2"
listhen "^0.2.6"
ohmyfetch "^0.4.15"
pathe "^0.2.0"
sharp "^0.29.0"
ufo "^0.7.9"
sharp "^0.30.1"
ufo "^0.7.10"
xss "^1.0.10"

is-arrayish@^0.2.1:
Expand Down Expand Up @@ -2348,7 +2348,7 @@ lines-and-columns@^1.1.6:
resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.2.4.tgz#eca284f75d2965079309dc0ad9255abb2ebc1632"
integrity sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==

listhen@^0.2.4, listhen@^0.2.5:
listhen@^0.2.4, listhen@^0.2.6:
version "0.2.6"
resolved "https://registry.yarnpkg.com/listhen/-/listhen-0.2.6.tgz#1205766d6cf6a4232c0bbb3cbce239dc2ae1a9eb"
integrity sha512-/xpN3784qRxyxS9wjiVB0vWE9NqoT+5nvOw5KBRB4tXtsjp7y83CJe6Gi/WtoTkkDqXwhSNStqaJ3r/7J0ZCDA==
Expand Down Expand Up @@ -2603,7 +2603,7 @@ node-abi@^3.3.0:
dependencies:
semver "^7.3.5"

node-addon-api@^4.2.0:
node-addon-api@^4.3.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-4.3.0.tgz#52a1a0b475193e0928e98e0426a0d1254782b77f"
integrity sha512-73sE9+3UaLYYFmDsFZnqCInzPyh3MqIwZO9cw58yIqAZhONrrabrYyYe3TuIqtIiOuTXVhsGau8hcrhhwSsDIQ==
Expand Down Expand Up @@ -2734,7 +2734,7 @@ ohmyfetch@^0.3.1:
node-fetch "^2.6.1"
ufo "^0.7.9"

ohmyfetch@^0.4.2:
ohmyfetch@^0.4.15:
version "0.4.15"
resolved "https://registry.yarnpkg.com/ohmyfetch/-/ohmyfetch-0.4.15.tgz#c7059d2952abacfaaaebd82efab754aca53a5c04"
integrity sha512-AJKiMh4s+VYLLiAQi3tZn4Zh/xYr94nOX9FLGVVdbXo7QWrrKIfGweACIKaB06PjQdZtkICzcmNN+CQSR+9vQQ==
Expand Down Expand Up @@ -2949,7 +2949,7 @@ pluralize@^8.0.0:
resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-8.0.0.tgz#1a6fa16a38d12a1901e0320fa017051c539ce3b1"
integrity sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA==

prebuild-install@^7.0.0:
prebuild-install@^7.0.1:
version "7.0.1"
resolved "https://registry.yarnpkg.com/prebuild-install/-/prebuild-install-7.0.1.tgz#c10075727c318efe72412f333e0ef625beaf3870"
integrity sha512-QBSab31WqkyxpnMWQxubYAHR5S9B2+r81ucocew34Fkl98FhvKIF50jIJnNOBmAZfyNV7vE5T6gd3hTVWgY6tg==
Expand Down Expand Up @@ -3223,17 +3223,17 @@ set-blocking@~2.0.0:
resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"
integrity sha1-BF+XgtARrppoA93TgrJDkrPYkPc=

sharp@^0.29.0:
version "0.29.3"
resolved "https://registry.yarnpkg.com/sharp/-/sharp-0.29.3.tgz#0da183d626094c974516a48fab9b3e4ba92eb5c2"
integrity sha512-fKWUuOw77E4nhpyzCCJR1ayrttHoFHBT2U/kR/qEMRhvPEcluG4BKj324+SCO1e84+knXHwhJ1HHJGnUt4ElGA==
sharp@^0.30.1:
version "0.30.1"
resolved "https://registry.yarnpkg.com/sharp/-/sharp-0.30.1.tgz#203efaf9acfc5c15c8a343800254621e56011c12"
integrity sha512-ycpz81q8AeVjz1pGvvirQBeJcYE2sXAjcLXR/69LWOe/oxavBLOrenZcTzvTXn83jqAGqY+OuwF+2kFXzbKtDA==
dependencies:
color "^4.0.1"
detect-libc "^1.0.3"
node-addon-api "^4.2.0"
prebuild-install "^7.0.0"
color "^4.2.0"
detect-libc "^2.0.0"
node-addon-api "^4.3.0"
prebuild-install "^7.0.1"
semver "^7.3.5"
simple-get "^4.0.0"
simple-get "^4.0.1"
tar-fs "^2.1.1"
tunnel-agent "^0.6.0"

Expand All @@ -3259,7 +3259,7 @@ simple-concat@^1.0.0:
resolved "https://registry.yarnpkg.com/simple-concat/-/simple-concat-1.0.1.tgz#f46976082ba35c2263f1c8ab5edfe26c41c9552f"
integrity sha512-cSFtAPtRhljv69IK0hTVZQ+OfE9nePi/rtJmw5UjHeVyVroEqJXP1sFztKUy1qU+xvz3u/sfYJLa947b7nAN2Q==

simple-get@^4.0.0:
simple-get@^4.0.0, simple-get@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/simple-get/-/simple-get-4.0.1.tgz#4a39db549287c979d352112fa03fd99fd6bc3543"
integrity sha512-brv7p5WgH0jmQJr1ZDDfKDOSeWWg+OVypG99A/5vYGPqJ6pxiaHLy8nxtFjBA7oMa01ebA9gfh1uMCFqOuXxvA==
Expand Down

0 comments on commit 1f65eb7

Please sign in to comment.