Skip to content

Commit

Permalink
fix: reading 'log' error
Browse files Browse the repository at this point in the history
  • Loading branch information
co3k committed Mar 12, 2022
1 parent 163f9ba commit 7e709ba
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 31 deletions.
10 changes: 10 additions & 0 deletions src/files/entry.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare module "APP" {
import { App } from "@sveltejs/kit";
export { App };
}

declare module "MANIFEST" {
import { SSRManifest } from "@sveltejs/kit";

export const manifest: SSRManifest;
}
11 changes: 5 additions & 6 deletions src/files/entry.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// @ts-expect-error will be resolve by https://github.com/sveltejs/kit/pull/2285
import * as App from '../output/server/app.js';
import {App} from 'APP';
import {manifest} from 'MANIFEST';
import {toSvelteKitRequest} from './firebase-to-svelte-kit.js';

/** @type {import('@sveltejs/kit').App} */
const app = App;

app.init();
const app = new App(manifest);

/**
* Firebase Cloud Function handler for SvelteKit
Expand All @@ -21,8 +19,9 @@ app.init();
*/
export default async function svelteKit(request, response) {
const rendered = await app.render(toSvelteKitRequest(request));
const body = await rendered.text();

return rendered
? response.writeHead(rendered.status, rendered.headers).end(rendered.body)
? response.writeHead(rendered.status, rendered.headers).end(body)
: response.writeHead(404, 'Not Found').end();
}
11 changes: 4 additions & 7 deletions src/files/firebase-to-svelte-kit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@
*/
export function toSvelteKitRequest(request) {
const host = `${request.headers['x-forwarded-proto']}://${request.headers.host}`;
const {pathname, searchParams: searchParameters} = new URL(request.url || '', host);
const {href, pathname, searchParams: searchParameters} = new URL(request.url || '', host);

return {
return new Request(href, {
method: request.method,
headers: toSvelteKitHeaders(request.headers),
rawBody: request.rawBody
body: request.rawBody
? request.rawBody
: null,
host,
path: pathname,
query: searchParameters,
};
});
}

/**
Expand Down
40 changes: 22 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {readFileSync} from 'fs';
import {readFileSync, writeFileSync} from 'fs';
import path from 'path';
import process from 'process';
import {fileURLToPath} from 'url';
Expand All @@ -14,17 +14,17 @@ import {
const entrypoint = function (options = {}) {
return {
name: 'svelte-adapter-firebase',
async adapt({utils, config}) {
async adapt(builder) {
const {
esbuildOptions = undefined,
firebaseJsonPath = 'firebase.json',
target = undefined,
sourceRewriteMatch = '**',
} = options;

utils.log.minor(`Adapter configuration:\n\t${JSON.stringify(options)}`);
builder.log.minor(`Adapter configuration:\n\t${JSON.stringify(options)}`);
const {functions, publicDir} = parseFirebaseConfiguration({firebaseJsonPath, target, sourceRewriteMatch});
ensureStaticResourceDirsDiffer({source: path.join(process.cwd(), config.kit.files.assets), dest: publicDir});
ensureStaticResourceDirsDiffer({source: path.join(process.cwd(), builder.getStaticDirectory()), dest: publicDir});

const functionsPackageJson = JSON.parse(readFileSync(path.join(functions.source, 'package.json'), 'utf-8'));
if (!functionsPackageJson?.main) {
Expand All @@ -42,14 +42,15 @@ const entrypoint = function (options = {}) {
svelteSSR: dirs.serverDirname.replace(/\W/g, '') + 'Server',
};

const relativePath = path.posix.relative(dirs.tmp, builder.getServerDirectory());
const runtimeVersion = ensureCompatibleCloudFunctionVersion({
functionsPackageJsonEngine: functionsPackageJson?.engines?.node,
firebaseJsonFunctionsRuntime: functions.runtime,
});
utils.rimraf(dirs.tmp);
utils.rimraf(dirs.serverPath);
utils.copy(path.join(dirs.files, 'entry.js'), path.join(dirs.tmp, 'entry.js'));
utils.copy(path.join(dirs.files, 'firebase-to-svelte-kit.js'), path.join(dirs.tmp, 'firebase-to-svelte-kit.js'));
builder.rimraf(dirs.tmp);
builder.rimraf(dirs.serverPath);
builder.copy(path.join(dirs.files, 'entry.js'), path.join(dirs.tmp, 'entry.js'), {replace: {APP: `${relativePath}/app.js`, MANIFEST: `${relativePath}/manifest.js`}});
builder.copy(path.join(dirs.files, 'firebase-to-svelte-kit.js'), path.join(dirs.tmp, 'firebase-to-svelte-kit.js'));

/** @type {esbuild.BuildOptions} */
const defaultOptions = {
Expand All @@ -65,12 +66,12 @@ const entrypoint = function (options = {}) {
? await esbuildOptions(defaultOptions)
: defaultOptions;
await esbuild.build(buildOptions);
utils.log.minor(logRelativeDir('Writing Cloud Function server assets to', dirs.serverPath));
builder.log.minor(logRelativeDir('Writing Cloud Function server assets to', dirs.serverPath));

try {
if (!readFileSync(ssrFunc.entrypoint, 'utf-8').includes(`${functions.name} =`)) {
utils.log.warn(`Add the following Cloud Function to ${ssrFunc.entrypoint}`);
utils.log.warn(`
builder.log.warn(`Add the following Cloud Function to ${ssrFunc.entrypoint}`);
builder.log.warn(`
let ${ssrFunc.svelteSSR};
exports.${functions.name} = functions.region("us-central1").https.onRequest(async (request, response) => {
if (!${ssrFunc.svelteSSR}) {
Expand All @@ -87,15 +88,18 @@ exports.${functions.name} = functions.region("us-central1").https.onRequest(asyn
throw new Error(`Error reading Cloud Function entrypoint file: ${ssrFunc.entrypoint}. ${error.message}`);
}

utils.log.minor(logRelativeDir('Erasing destination static asset dir before processing', publicDir));
utils.rimraf(publicDir);
builder.log.minor(logRelativeDir('Erasing destination static asset dir before processing', publicDir));
builder.rimraf(publicDir);

utils.log.minor(logRelativeDir('Writing client application to', publicDir));
utils.copy_static_files(publicDir);
utils.copy_client_files(publicDir);
builder.log.minor(logRelativeDir('Writing client application to', publicDir));
builder.writeStatic(publicDir);
builder.writeClient(publicDir);

utils.log.minor(logRelativeDir('Prerendering static pages to', publicDir));
await utils.prerender({dest: publicDir});
builder.log.minor(logRelativeDir('Prerendering static pages to', publicDir));
const {paths} = await builder.prerender({dest: publicDir});
writeFileSync(`${dirs.tmp}/manifest.js`, `export const manifest = ${builder.generateManifest({
relativePath,
})};\n\nexport const prerendered = new Set(${JSON.stringify(paths)});\n`);
},
};
};
Expand Down

0 comments on commit 7e709ba

Please sign in to comment.