Skip to content

Commit

Permalink
chore: fix image responses and request building; remove ipfs upload o…
Browse files Browse the repository at this point in the history
…f static assets;
  • Loading branch information
gabrielmpinto committed Oct 7, 2024
1 parent b1071d9 commit 3c8ad47
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 74 deletions.
2 changes: 1 addition & 1 deletion packages/next-on-fleek/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function constructBuildOutputRecord(
}

export async function buildWorkerFile(
{ vercelConfig, vercelOutput, cids }: ProcessedVercelOutput,
{ vercelConfig, vercelOutput }: ProcessedVercelOutput,
{
outputDir,
workerJsDir,
Expand Down Expand Up @@ -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:*'],
Expand Down
14 changes: 7 additions & 7 deletions packages/next-on-fleek/src/buildApplication/generateGlobalJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, string>;
}): string {
export function generateGlobalJs(): string {
return `
globalThis.cid = "${cids.rootCid}";
const sharedGlobalProperties = new Set([
'_nextOriginalFetch',
'fetch',
Expand Down Expand Up @@ -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)) {
Expand Down
13 changes: 0 additions & 13 deletions packages/next-on-fleek/src/buildApplication/processVercelOutput.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -103,7 +102,6 @@ export async function processOutputDir(
}

export type ProcessedVercelOutput = {
cids: { rootCid: string; cidMap: Record<string, string> };
vercelConfig: ProcessedVercelConfig;
vercelOutput: ProcessedVercelBuildOutput;
};
Expand All @@ -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<string, string> } = 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',
Expand All @@ -165,7 +153,6 @@ export async function processVercelOutput(
);

return {
cids,
vercelConfig: processedConfig,
vercelOutput: processedOutput,
};
Expand Down
42 changes: 0 additions & 42 deletions packages/next-on-fleek/src/utils/ipfs.ts

This file was deleted.

17 changes: 11 additions & 6 deletions packages/next-on-fleek/templates/_worker.js/index.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -29,7 +26,8 @@ export async function main(fleekRequest: FleekRequest): Promise<FleekResponse> {
assetsFetcher: globalThis.ASSETS,
imagesConfig: __CONFIG__.images,
});
return adaptFetchResponseToFleekResponse(res);
return res.bytes();
// return adaptFetchResponseToFleekResponse(res);
}

const adjustedRequest = adjustRequestForVercel(request);
Expand All @@ -55,7 +53,14 @@ export async function main(fleekRequest: FleekRequest): Promise<FleekResponse> {
}

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,
Expand Down

0 comments on commit 3c8ad47

Please sign in to comment.