From 3c8ad4777333c515d82ddc612c4b22dfa6b40e5e Mon Sep 17 00:00:00 2001 From: Gabriel Pinto Date: Mon, 7 Oct 2024 18:57:43 +0100 Subject: [PATCH] chore: fix image responses and request building; remove ipfs upload of static assets; --- packages/next-on-fleek/package.json | 2 +- .../src/buildApplication/buildWorkerFile.ts | 7 +--- .../src/buildApplication/generateGlobalJs.ts | 14 +++---- .../buildApplication/processVercelOutput.ts | 13 ------ packages/next-on-fleek/src/utils/ipfs.ts | 42 ------------------- .../templates/_worker.js/index.ts | 17 +++++--- 6 files changed, 21 insertions(+), 74 deletions(-) delete mode 100644 packages/next-on-fleek/src/utils/ipfs.ts diff --git a/packages/next-on-fleek/package.json b/packages/next-on-fleek/package.json index b1c254456..d42acfc37 100644 --- a/packages/next-on-fleek/package.json +++ b/packages/next-on-fleek/package.json @@ -1,6 +1,6 @@ { "name": "@fleek-platform/next-on-fleek", - "version": "1.14.4", + "version": "1.14.5", "main": "./dist/index.js", "module": "./dist/index.js", "types": "./dist/src/index.d.ts", diff --git a/packages/next-on-fleek/src/buildApplication/buildWorkerFile.ts b/packages/next-on-fleek/src/buildApplication/buildWorkerFile.ts index bac84a36c..8e84f418b 100644 --- a/packages/next-on-fleek/src/buildApplication/buildWorkerFile.ts +++ b/packages/next-on-fleek/src/buildApplication/buildWorkerFile.ts @@ -47,7 +47,7 @@ export function constructBuildOutputRecord( } export async function buildWorkerFile( - { vercelConfig, vercelOutput, cids }: ProcessedVercelOutput, + { vercelConfig, vercelOutput }: ProcessedVercelOutput, { outputDir, workerJsDir, @@ -85,13 +85,10 @@ export async function buildWorkerFile( const outputFile = join(workerJsDir, 'index.js'); - // print current dir - console.log('current dir', process.cwd()); - await build({ ...defaultBuildOpts, entryPoints: [join(templatesDir, '_worker.js')], - banner: { js: generateGlobalJs(cids) }, + banner: { js: generateGlobalJs() }, bundle: true, inject: [functionsFile], external: ['node:*', './__next-on-pages-dist__/*', 'cloudflare:*'], diff --git a/packages/next-on-fleek/src/buildApplication/generateGlobalJs.ts b/packages/next-on-fleek/src/buildApplication/generateGlobalJs.ts index 9727dae13..7ef898f7e 100644 --- a/packages/next-on-fleek/src/buildApplication/generateGlobalJs.ts +++ b/packages/next-on-fleek/src/buildApplication/generateGlobalJs.ts @@ -4,13 +4,8 @@ * * @returns the plain javascript string that should be added at the top of the the _worker.js file */ -export function generateGlobalJs(cids: { - rootCid: string; - cidMap: Record; -}): string { +export function generateGlobalJs(): string { return ` - globalThis.cid = "${cids.rootCid}"; - const sharedGlobalProperties = new Set([ '_nextOriginalFetch', 'fetch', @@ -106,7 +101,12 @@ export function generateGlobalJs(cids: { globalThis.ASSETS = { fetch: async req => { try { - const { pathname } = new URL(req.url); + let pathname; + if (req instanceof URL) { + pathname = new URL(req).pathname; + } else { + pathname = new URL(req.url).pathname; + } let assetPath = pathname; if (!/\\.[^.]+$/.test(assetPath)) { diff --git a/packages/next-on-fleek/src/buildApplication/processVercelOutput.ts b/packages/next-on-fleek/src/buildApplication/processVercelOutput.ts index 084ea02aa..501a9c523 100644 --- a/packages/next-on-fleek/src/buildApplication/processVercelOutput.ts +++ b/packages/next-on-fleek/src/buildApplication/processVercelOutput.ts @@ -1,6 +1,5 @@ import { dirname, join, relative, resolve } from 'path'; import { copyFile, mkdir, rm } from 'fs/promises'; -import { uploadDir } from '../utils/ipfs'; import { addLeadingSlash, normalizePath, @@ -103,7 +102,6 @@ export async function processOutputDir( } export type ProcessedVercelOutput = { - cids: { rootCid: string; cidMap: Record }; vercelConfig: ProcessedVercelConfig; vercelOutput: ProcessedVercelBuildOutput; }; @@ -130,16 +128,6 @@ export async function processVercelOutput( staticAssets.map(path => [path, { type: 'static' }]), ); - // eslint-disable-next-line no-console - console.log('Uploading static assets to IPFS'); - const cids: { rootCid: string; cidMap: Record } = await uploadDir({ - filePath: join('.vercel', 'output', 'static'), - }); - // eslint-disable-next-line no-console - console.log('Uploaded static assets to IPFS', cids.rootCid); - // eslint-disable-next-line no-console - console.log('Uploaded static assets to IPFS', cids.cidMap); - edgeFunctions.forEach(({ relativePath, outputPath, route }) => { processedOutput.set(route?.path ?? stripFuncExtension(relativePath), { type: 'function', @@ -165,7 +153,6 @@ export async function processVercelOutput( ); return { - cids, vercelConfig: processedConfig, vercelOutput: processedOutput, }; diff --git a/packages/next-on-fleek/src/utils/ipfs.ts b/packages/next-on-fleek/src/utils/ipfs.ts deleted file mode 100644 index a1c8f03b4..000000000 --- a/packages/next-on-fleek/src/utils/ipfs.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { FleekSdk, PersonalAccessTokenService } from '@fleek-platform/sdk'; -import { cliLog } from '../cli'; - -export async function uploadDir(props: { - filePath: string; -}): Promise<{ rootCid: string; cidMap: Record }> { - try { - const personalAccessToken = process.env['FLEEK_PAT'] as string | undefined; - const projectId = process.env['FLEEK_PROJECT_ID'] as string | undefined; - - if (!personalAccessToken) { - throw new Error('Missing personal access token'); - } - - if (!projectId) { - throw new Error('Missing project id'); - } - - const accessTokenService = new PersonalAccessTokenService({ - projectId, - personalAccessToken, - }); - const sdk = new FleekSdk({ accessTokenService }); - - const uploadFileResult = await sdk.storage().uploadDirectory({ - path: props.filePath, - }); - - const result: Record = {}; - - // eslint-disable-next-line no-console - cliLog( - `Successfully uploaded static assets to IPFS: ${uploadFileResult.pin.cid}`, - ); - - return { rootCid: uploadFileResult.pin.cid, cidMap: result }; - } catch (e) { - // eslint-disable-next-line no-console - console.error('Error uploading file to IPFS', JSON.stringify(e, null, 2)); - throw e; - } -} diff --git a/packages/next-on-fleek/templates/_worker.js/index.ts b/packages/next-on-fleek/templates/_worker.js/index.ts index 9fdadf7f5..d0478883c 100644 --- a/packages/next-on-fleek/templates/_worker.js/index.ts +++ b/packages/next-on-fleek/templates/_worker.js/index.ts @@ -1,9 +1,6 @@ import type { FleekRequest, FleekResponse } from '../types'; import { handleRequest } from './handleRequest'; -import { - adjustRequestForVercel, - handleImageResizingRequest, -} from './utils'; +import { adjustRequestForVercel, handleImageResizingRequest } from './utils'; declare const __CONFIG__: ProcessedVercelConfig; @@ -29,7 +26,8 @@ export async function main(fleekRequest: FleekRequest): Promise { assetsFetcher: globalThis.ASSETS, imagesConfig: __CONFIG__.images, }); - return adaptFetchResponseToFleekResponse(res); + return res.bytes(); + // return adaptFetchResponseToFleekResponse(res); } const adjustedRequest = adjustRequestForVercel(request); @@ -55,7 +53,14 @@ export async function main(fleekRequest: FleekRequest): Promise { } function adaptFleekRequestToFetch(fleekRequest: FleekRequest): Request { - return new Request(new URL(`http://0.0.0.0${fleekRequest.path}`), { + const url = new URL(`http://0.0.0.0${fleekRequest.path}`); + + // Add query parameters + for (const [key, value] of Object.entries(fleekRequest.query ?? {})) { + url.searchParams.append(key, value); + } + + return new Request(url, { method: fleekRequest.method, headers: fleekRequest.headers, body: fleekRequest.body,