Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add browser preview template for both image & avatar endpoints #178

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"dependencies": {
"@adraffy/ens-normalize": "^1.10.0",
"@ensdomains/ens-avatar": "^1.0.0-alpha.2.ethers.6",
"@ensdomains/ens-avatar": "^1.0.0-alpha.3.ethers.6",
"@ensdomains/ensjs": "^3.7.0",
"@types/lodash": "^4.14.170",
"btoa": "^1.2.1",
Expand Down
10 changes: 10 additions & 0 deletions src/controller/avatarImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { RESPONSE_TIMEOUT } from '../config';
import { getAvatarImage } from '../service/avatar';
import getNetwork, { NetworkName } from '../service/network';
import createDocumentfromTemplate from '../template-document';

export async function avatarImage(req: Request, res: Response) {
// #swagger.description = 'ENS avatar image'
Expand All @@ -29,6 +30,15 @@ export async function avatarImage(req: Request, res: Response) {
description: 'Image file'
} */
if (!res.headersSent) {
if (req.header('sec-fetch-dest') === 'document') {
const documentTemplate = createDocumentfromTemplate({ buffer, metadata: { name, network: networkName }, mimeType });
res
.writeHead(200, {
'Content-Type': 'text/html',
})
.end(documentTemplate);
return;
}
res
.writeHead(200, {
'Content-Type': mimeType,
Expand Down
10 changes: 10 additions & 0 deletions src/controller/ensImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { RESPONSE_TIMEOUT } from '../config';
import { checkContract } from '../service/contract';
import { getDomain } from '../service/domain';
import getNetwork, { NetworkName } from '../service/network';
import createDocumentfromTemplate from '../template-document';

/* istanbul ignore next */
export async function ensImage(req: Request, res: Response) {
Expand Down Expand Up @@ -39,6 +40,15 @@ export async function ensImage(req: Request, res: Response) {
version
);
if (result.image_url) {
if (req.header('sec-fetch-dest') === 'document') {
const documentTemplate = createDocumentfromTemplate({ metadata: {...result, network: networkName }});
res
.writeHead(200, {
'Content-Type': 'text/html',
})
.end(documentTemplate);
return;
}
const base64 = result.image_url.replace('data:image/svg+xml;base64,', '');
const buffer = Buffer.from(base64, 'base64');
if (!res.headersSent) {
Expand Down
3 changes: 2 additions & 1 deletion src/controller/ensMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export async function ensMetadata(req: Request, res: Response) {
ETH_REGISTRY_ABI,
provider
);
if (!tokenId || !version) {

if (!tokenId || version?.valueOf() === undefined) {
throw 'Missing parameters to construct namehash';
}
const _namehash = constructEthNameHash(tokenId, version as Version);
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const setCacheHeader = function (
`public, max-age=${period}, s-maxage=${period}`
);
}
res.append('Vary', 'Sec-Fetch-Dest');

next();
};
Expand Down
6 changes: 3 additions & 3 deletions src/service/avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class AvatarMetadata {
this.uri = uri;
}

async getImage() {
async getImage(): Promise<[Buffer, string]> {
let avatarURI;
try {
avatarURI = await this.avtResolver.getAvatar(this.uri, {
Expand Down Expand Up @@ -102,7 +102,7 @@ export class AvatarMetadata {

assert(!!response, 'Response is empty');

const mimeType = response?.headers.get('Content-Type');
const mimeType = response?.headers.get('Content-Type') || '';
const data = await response?.buffer();

if (mimeType?.includes('svg') || isSvg(data.toString())) {
Expand Down Expand Up @@ -189,7 +189,7 @@ export async function getAvatarMeta(
export async function getAvatarImage(
provider: JsonRpcProvider,
name: string
): Promise<any> {
): Promise<[Buffer, string]> {
const avatar = new AvatarMetadata(provider, name);
return await avatar.getImage();
}
2 changes: 1 addition & 1 deletion src/service/rasterize.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GoogleAuth } from 'google-auth-library';

const auth = new GoogleAuth();
const grRasterize = 'https://rasterize-y3ur7hmkna-uc.a.run.app/rasterize'
const grRasterize = 'https://us-central1-ens-metadata-service.cloudfunctions.net/rasterize'

export function rasterize(
contractAddress: string,
Expand Down
Loading
Loading