Unable to serve private CF images

Here's the code, 95% copied from the sample code linked above

const EXPIRATION = 60 * 60 * 24; // 1 day

const bufferToHex = (buffer: ArrayBufferLike) =>
    [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('');

export async function generateSignedUrl(imageId: string) {
    const url = new URL(`https://imagedelivery.net/${env.CF_ACCOUNT_HASH}/${imageId}/public`);
    const encoder = new TextEncoder();
    const secretKeyData = encoder.encode(env.CF_IMAGES_SECRET_KEY);
    const key = await crypto.subtle.importKey(
        'raw',
        secretKeyData,
        { name: 'HMAC', hash: 'SHA-256' },
        false,
        ['sign'],
    );

    const expiry = Math.floor(Date.now() / 1000) + EXPIRATION;
    url.searchParams.set('exp', String(expiry));

    const stringToSign = url.pathname + '?' + url.searchParams.toString();

    const mac = await crypto.subtle.sign('HMAC', key, encoder.encode(stringToSign));
    const sig = bufferToHex(new Uint8Array(mac).buffer);
    url.searchParams.set('sig', sig);
    return url;
}
Was this page helpful?