Skip to content

Commit

Permalink
fix: Externalize app initialization to adapters (#1804)
Browse files Browse the repository at this point in the history
* fix: Externalize app initialization to adapters

Fixes #1784

* fix(lint)

* chore: spacing changes

* chore: Add todo in adapter-begin
  • Loading branch information
GrygrFlzr authored Jul 2, 2021
1 parent 7256768 commit 9f0c54a
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 13 deletions.
9 changes: 9 additions & 0 deletions .changeset/polite-ducks-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@sveltejs/adapter-cloudflare-workers': patch
'@sveltejs/adapter-netlify': patch
'@sveltejs/adapter-node': patch
'@sveltejs/adapter-vercel': patch
'@sveltejs/kit': patch
---

Externalize app initialization to adapters
2 changes: 2 additions & 0 deletions packages/adapter-begin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import url from 'url';
import app from '@architect/shared/app.js'; // eslint-disable-line import/no-unresolved

// TODO: run init() on the app before handling routes

async function handler(event) {
const { host, rawPath: path, httpMethod, rawQueryString, headers, body } = event;

Expand Down
4 changes: 3 additions & 1 deletion packages/adapter-cloudflare-workers/files/entry.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// TODO hardcoding the relative location makes this brittle
import { render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { init, render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { getAssetFromKV, NotFoundError } from '@cloudflare/kv-asset-handler'; // eslint-disable-line import/no-unresolved

init();

addEventListener('fetch', (event) => {
event.respondWith(handle(event));
});
Expand Down
4 changes: 3 additions & 1 deletion packages/adapter-netlify/files/entry.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import '@sveltejs/kit/install-fetch'; // eslint-disable-line import/no-unresolved

// TODO hardcoding the relative location makes this brittle
import { render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { init, render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved

init();

export async function handler(event) {
const { path, httpMethod, headers, rawQuery, body, isBase64Encoded } = event;
Expand Down
4 changes: 3 additions & 1 deletion packages/adapter-node/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import './require_shim';
import { createServer } from './server';
// TODO hardcoding the relative location makes this brittle
import { render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { init, render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { host, port } from './env.js'; // eslint-disable-line import/no-unresolved

init();

const instance = createServer({ render }).listen(port, host, () => {
console.log(`Listening on port ${port}`);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-node/src/require_shim.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import { createRequire } from 'module';
global.require = createRequire(import.meta.url);
globalThis.require = createRequire(import.meta.url);
4 changes: 3 additions & 1 deletion packages/adapter-vercel/files/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { getRawBody } from '@sveltejs/kit/node'; // eslint-disable-line import/n
import '@sveltejs/kit/install-fetch'; // eslint-disable-line import/no-unresolved

// TODO hardcoding the relative location makes this brittle
import { render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { init, render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved

init();

export default async (req, res) => {
const { pathname, searchParams } = new URL(req.url || '', 'http://localhost');
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/src/core/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,11 @@ async function build_server(
let options = null;
const default_settings = { paths: ${s(config.kit.paths)} };
// allow paths to be overridden in svelte-kit preview
// and in prerendering
export function init(settings) {
export function init(settings = default_settings) {
set_paths(settings.paths);
set_prerendering(settings.prerendering || false);
Expand Down Expand Up @@ -380,8 +382,6 @@ async function build_server(
};
}
init({ paths: ${s(config.kit.paths)} });
export function render(request, {
prerender
} = {}) {
Expand Down
23 changes: 18 additions & 5 deletions packages/kit/src/install-fetch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
// @ts-nocheck
import fetch, { Response, Request, Headers } from 'node-fetch';

globalThis.fetch = fetch;
globalThis.Response = Response;
globalThis.Request = Request;
globalThis.Headers = Headers;
Object.defineProperties(globalThis, {
fetch: {
enumerable: true,
value: fetch
},
Response: {
enumerable: true,
value: Response
},
Request: {
enumerable: true,
value: Request
},
Headers: {
enumerable: true,
value: Headers
}
});

0 comments on commit 9f0c54a

Please sign in to comment.