@hamur/image 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/index.js +59 -28
- package/dist/types/src/adapters/Img.svelte +33 -0
- package/dist/types/src/adapters/Picture.svelte +39 -0
- package/dist/types/src/adapters/angular-img.d.ts +24 -21
- package/dist/types/src/adapters/angular-picture.d.ts +24 -21
- package/dist/types/src/server/endpoint.d.ts +4 -2
- package/package.json +45 -18
- package/src/adapters/angular-img.ts +135 -136
- package/src/adapters/angular-picture.ts +133 -107
- package/src/build/platform.ts +18 -1
- package/src/build/variants.ts +17 -0
- package/src/server/endpoint.ts +32 -10
- package/src/server/handler.ts +2 -1
package/src/server/endpoint.ts
CHANGED
|
@@ -59,6 +59,7 @@ const NOT_MADE: Record<string, string> = { gif: "GIF", svg: "SVG", avif: "AVIF",
|
|
|
59
59
|
const SOURCE_FILE = /\.(?:png|jpe?g|gif|webp|avif|svg)$/i
|
|
60
60
|
const IMMUTABLE = "public, max-age=31536000, immutable"
|
|
61
61
|
const HOUR = "public, max-age=3600"
|
|
62
|
+
const PRIVATE = "private, no-store"
|
|
62
63
|
const REMOTE_TIMEOUT = 10_000
|
|
63
64
|
const REMOTE_CONNECT_TIMEOUT = 2_500
|
|
64
65
|
/** How long a remote URL's source ETag is trusted without asking upstream, as long as `HOUR` lets a browser keep it. */
|
|
@@ -197,8 +198,13 @@ interface Fetched {
|
|
|
197
198
|
tag: string
|
|
198
199
|
}
|
|
199
200
|
|
|
200
|
-
/**
|
|
201
|
-
export
|
|
201
|
+
/** Throws the refusal of the access rule on the public file at `path`; true when a rule covers it. */
|
|
202
|
+
export type SourceAccess = (path: string) => Promise<boolean>
|
|
203
|
+
|
|
204
|
+
/** The `/_hamur/image/**` answer. Every 200 is bytes this endpoint encoded, never a source's own. An image has the access of its source. */
|
|
205
|
+
export function createImageEndpoint(
|
|
206
|
+
env: EndpointEnvironment,
|
|
207
|
+
): (request: Request, access?: SourceAccess) => Promise<Response> {
|
|
202
208
|
const { options, sources, endpoint } = env
|
|
203
209
|
const transform = env.transform ?? transformImage
|
|
204
210
|
const prefix = `${options.baseURL}_hamur/image/`
|
|
@@ -342,7 +348,12 @@ export function createImageEndpoint(env: EndpointEnvironment): (request: Request
|
|
|
342
348
|
return { tag: `${shown}@${entry[3]}`, width: entry[0], pinned, shown, load: async () => (bytes ??= await read()) }
|
|
343
349
|
}
|
|
344
350
|
|
|
345
|
-
async function produce(
|
|
351
|
+
async function produce(
|
|
352
|
+
key: string,
|
|
353
|
+
request: ImageRequest,
|
|
354
|
+
found: Found,
|
|
355
|
+
guarded: boolean,
|
|
356
|
+
): Promise<Uint8Array | ImageRefusal> {
|
|
346
357
|
const bytes = await found.load()
|
|
347
358
|
if (!bytes) return refuse(404, `source ${JSON.stringify(found.shown)} not found`)
|
|
348
359
|
if (isRefusal(bytes)) return bytes
|
|
@@ -354,6 +365,7 @@ export function createImageEndpoint(env: EndpointEnvironment): (request: Request
|
|
|
354
365
|
} catch (error) {
|
|
355
366
|
return refusalOf(error)
|
|
356
367
|
}
|
|
368
|
+
if (guarded) return output
|
|
357
369
|
try {
|
|
358
370
|
await env.cache().setItemRaw(key, output)
|
|
359
371
|
} catch (error) {
|
|
@@ -367,10 +379,16 @@ export function createImageEndpoint(env: EndpointEnvironment): (request: Request
|
|
|
367
379
|
return output
|
|
368
380
|
}
|
|
369
381
|
|
|
370
|
-
async function answerFound(
|
|
382
|
+
async function answerFound(
|
|
383
|
+
parsed: ImageRequest,
|
|
384
|
+
found: Found,
|
|
385
|
+
incoming: Request,
|
|
386
|
+
head: boolean,
|
|
387
|
+
guarded: boolean,
|
|
388
|
+
): Promise<Response> {
|
|
371
389
|
const again = async (reread: NonNullable<Found["reread"]>) => {
|
|
372
390
|
const fresh = await reread()
|
|
373
|
-
return isRefusal(fresh) ? answerText(fresh, head) : answerFound(parsed, fresh, incoming, head)
|
|
391
|
+
return isRefusal(fresh) ? answerText(fresh, head) : answerFound(parsed, fresh, incoming, head, guarded)
|
|
374
392
|
}
|
|
375
393
|
if (parsed.w !== undefined && !options.widths.includes(parsed.w)) {
|
|
376
394
|
if (found.width === undefined && found.reread) return again(found.reread)
|
|
@@ -395,17 +413,17 @@ export function createImageEndpoint(env: EndpointEnvironment): (request: Request
|
|
|
395
413
|
const headers = {
|
|
396
414
|
"content-type": TYPES[parsed.extension],
|
|
397
415
|
etag,
|
|
398
|
-
"cache-control": endpoint.dev ? "no-cache" : found.pinned ? IMMUTABLE : HOUR,
|
|
416
|
+
"cache-control": guarded ? PRIVATE : endpoint.dev ? "no-cache" : found.pinned ? IMMUTABLE : HOUR,
|
|
399
417
|
"x-content-type-options": "nosniff",
|
|
400
418
|
}
|
|
401
419
|
if (matchesTag(incoming.headers.get("if-none-match"), etag)) return new Response(null, { status: 304, headers })
|
|
402
420
|
|
|
403
|
-
let output: Uint8Array | ImageRefusal | undefined = await env.cache().getItemRaw(key)
|
|
421
|
+
let output: Uint8Array | ImageRefusal | undefined = guarded ? undefined : await env.cache().getItemRaw(key)
|
|
404
422
|
if (!output && found.reread) return again(found.reread)
|
|
405
423
|
if (!output) {
|
|
406
424
|
let work = running.get(key)
|
|
407
425
|
if (work === undefined) {
|
|
408
|
-
work = produce(key, parsed, found).finally(() => running.delete(key))
|
|
426
|
+
work = produce(key, parsed, found, guarded).finally(() => running.delete(key))
|
|
409
427
|
running.set(key, work)
|
|
410
428
|
}
|
|
411
429
|
output = await work
|
|
@@ -414,15 +432,19 @@ export function createImageEndpoint(env: EndpointEnvironment): (request: Request
|
|
|
414
432
|
return new Response(head ? null : owned(output), { status: 200, headers })
|
|
415
433
|
}
|
|
416
434
|
|
|
417
|
-
return async (incoming) => {
|
|
435
|
+
return async (incoming, access) => {
|
|
418
436
|
const head = incoming.method === "HEAD"
|
|
419
437
|
const { pathname, search } = new URL(incoming.url)
|
|
420
438
|
const parsed = pathname.startsWith(prefix)
|
|
421
439
|
? parseImagePath(pathname.slice(prefix.length))
|
|
422
440
|
: refuse(404, "not found")
|
|
423
441
|
if (isRefusal(parsed)) return answerText(parsed, head)
|
|
442
|
+
const guarded =
|
|
443
|
+
parsed.path !== undefined &&
|
|
444
|
+
access !== undefined &&
|
|
445
|
+
(await access(`/${parsed.path.split("/").map(encodeURIComponent).join("/")}`))
|
|
424
446
|
const found = parsed.remote === undefined ? await findLocal(parsed) : await findRemote(parsed.remote, search)
|
|
425
447
|
if (isRefusal(found)) return answerText(found, head)
|
|
426
|
-
return answerFound(parsed, found, incoming, head)
|
|
448
|
+
return answerFound(parsed, found, incoming, head, guarded)
|
|
427
449
|
}
|
|
428
450
|
}
|
package/src/server/handler.ts
CHANGED
|
@@ -3,6 +3,7 @@ import endpoint, { useBlob } from "#hamur/image/endpoint"
|
|
|
3
3
|
import options from "#hamur/image/options"
|
|
4
4
|
import sources from "#hamur/image/sources"
|
|
5
5
|
import { defineHandler, readAsset, useLogger, useStorage } from "hamur/server"
|
|
6
|
+
import { requireFileAccess } from "hamur/server/internal"
|
|
6
7
|
import { createImageEndpoint, readFromDirs } from "./endpoint.ts"
|
|
7
8
|
|
|
8
9
|
const bytesOf = async (response: Response): Promise<Uint8Array | undefined> =>
|
|
@@ -29,5 +30,5 @@ export const imageEndpoint = defineHandler({
|
|
|
29
30
|
route: "/_hamur/image/**:path",
|
|
30
31
|
method: "GET",
|
|
31
32
|
raw: true,
|
|
32
|
-
handler: (_, { event }) => answer(event.request),
|
|
33
|
+
handler: (_, { event }) => answer(event.request, (path) => requireFileAccess(event, path)),
|
|
33
34
|
})
|