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

Realworld update #290

Merged
merged 30 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
54dcbd1
store user info in JWT. yeah, yeah, i know
Rich-Harris Dec 20, 2020
f99169b
use prefetching promise if available, reload on query change
Rich-Harris Dec 21, 2020
c7efcb7
inject credentials in server-side fetch
Rich-Harris Dec 21, 2020
e003e75
start updating realworld example
Rich-Harris Dec 21, 2020
6db72d6
always supply a body when posting
Rich-Harris Dec 21, 2020
cc58597
swap tabs
Rich-Harris Dec 21, 2020
3f0fe81
simplify setup
Rich-Harris Dec 21, 2020
6363b2f
simplify settings page
Rich-Harris Dec 21, 2020
6b59ee8
replace/pushState on goto
Rich-Harris Dec 21, 2020
8f49289
handle redirects
Rich-Harris Dec 21, 2020
8d1569f
update lockfile
Rich-Harris Dec 21, 2020
1a088b2
add preloading indicator
Rich-Harris Dec 21, 2020
d1d9f26
fix prettier config
Rich-Harris Dec 21, 2020
40010ee
remove double layout
Rich-Harris Dec 21, 2020
1e7e67a
update manifest/logo
Rich-Harris Dec 21, 2020
23ad4b5
remove unwanted segment
Rich-Harris Dec 21, 2020
08180a4
fix settings page
Rich-Harris Dec 21, 2020
5744e5b
fix following logic
Rich-Harris Dec 21, 2020
298518b
various
Rich-Harris Dec 23, 2020
fd37ff3
only insist on a body for GET requests
Rich-Harris Dec 23, 2020
096e19b
only attempt 304s for get requests
Rich-Harris Dec 23, 2020
36bea7b
allow empty responses for non-GET requests
Rich-Harris Dec 23, 2020
48d7a38
allow forms to override method with ?_method=delete etc
Rich-Harris Dec 23, 2020
bcbb033
fix comment submission/deleting, including progressive enhancement
Rich-Harris Dec 23, 2020
39a6aa0
some changes i apparently made before christmas
Rich-Harris Jan 4, 2021
e4f39e8
merge master -> realworld-update
Rich-Harris Mar 23, 2021
d4d9b43
update imports
Rich-Harris Mar 24, 2021
20c59c6
minor fixes
Rich-Harris Mar 24, 2021
fb58287
fix imports
Rich-Harris Mar 25, 2021
a8f277b
Merge branch 'master' into realworld-update
Rich-Harris Mar 25, 2021
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
Prev Previous commit
Next Next commit
inject credentials in server-side fetch
  • Loading branch information
Rich-Harris committed Dec 21, 2020
commit c7efcb7f521cc906ebe03fa040a8fc489190156d
1 change: 1 addition & 0 deletions packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@types/rimraf": "^3.0.0",
"@types/sade": "^1.7.2",
"amphtml-validator": "^1.0.33",
"cookie": "^0.4.1",
"eslint": "^7.14.0",
"esm": "^3.2.25",
"estree-walker": "^2.0.1",
Expand Down
12 changes: 3 additions & 9 deletions packages/kit/src/renderer/endpoint.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { normalize_headers } from './utils';

export default function render_route(request, context, options) {
const route = options.manifest.endpoints.find((route) => route.pattern.test(request.path));
if (!route) return null;
Expand Down Expand Up @@ -36,7 +38,7 @@ export default function render_route(request, context, options) {

let { status = 200, body, headers = {} } = response;

headers = lowercase_keys(headers);
headers = normalize_headers(headers);

if (
(typeof body === 'object' && !('content-type' in headers)) ||
Expand All @@ -63,11 +65,3 @@ export default function render_route(request, context, options) {
}
});
}

function lowercase_keys(obj) {
const clone = {};
for (const key in obj) {
clone[key.toLowerCase()] = obj[key];
}
return clone;
}
8 changes: 5 additions & 3 deletions packages/kit/src/renderer/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createHash } from 'crypto';
import render_page from './page';
import render_endpoint from './endpoint';
import { normalize_headers } from './utils';

function md5(body) {
return createHash('md5').update(body).digest('hex');
Expand All @@ -18,12 +19,13 @@ export async function render(request, options) {
};
}

const { context, headers = {} } =
(await (options.setup.prepare && options.setup.prepare(request.headers))) || {};
const prepared = (await (options.setup.prepare && options.setup.prepare(request.headers))) || {};
const context = prepared.context;
const headers = normalize_headers(prepared.headers);

try {
const response = await (render_endpoint(request, context, options) ||
render_page(request, context, options));
render_page(request, context, options, headers));

if (response) {
// inject ETags for 200 responses
Expand Down
47 changes: 44 additions & 3 deletions packages/kit/src/renderer/page.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import devalue from 'devalue';
import fetch, { Response } from 'node-fetch';
import { writable } from 'svelte/store';
import * as cookie from 'cookie';
import { parse, resolve, URLSearchParams } from 'url';
import { render } from './index';

async function get_response({ request, options, $session, route, status = 200, error }) {
async function get_response({
request,
options,
response_headers,
$session,
route,
status = 200,
error
}) {
const host = options.host || request.headers[options.host_header];

const dependencies = {};
Expand Down Expand Up @@ -64,11 +73,41 @@ async function get_response({ request, options, $session, route, status = 200, e
}

if (!response) {
const headers = opts.headers ? { ...opts.headers } : {};

if (opts.credentials !== 'omit') {
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
const cookies = Object.assign(
{},
cookie.parse(request.headers.cookie || ''),
cookie.parse(headers.cookie || '')
);

// In some cases, a cookie might be set in `src/setup.js`, in which
// case we need to honour it if a credentialled fetch is made immediately
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
const set_cookie = response_headers['set-cookie'];
if (set_cookie) {
set_cookie.split(', ').forEach((s) => {
const m = /([^=]+)=([^;]+)/.exec(s);
if (m) cookies[m[1]] = m[2];
});
}

const str = Object.keys(cookies)
.map((key) => `${key}=${cookies[key]}`)
.join('; ');

headers.cookie = str;

if (!headers.authorization && request.headers.authorization) {
headers.authorization = request.headers.authorization;
}
}

const rendered = await render(
{
host: request.host,
method: opts.method || 'GET',
headers: opts.headers || {}, // TODO inject credentials...
headers,
path: resolved,
body: opts.body,
query: new URLSearchParams(parsed.query || '')
Expand Down Expand Up @@ -262,7 +301,7 @@ async function get_response({ request, options, $session, route, status = 200, e
};
}

export default async function render_page(request, context, options) {
export default async function render_page(request, context, options, response_headers) {
const route = options.manifest.pages.find((route) => route.pattern.test(request.path));

const $session = await (options.setup.getSession && options.setup.getSession(context));
Expand All @@ -277,6 +316,7 @@ export default async function render_page(request, context, options) {
return await get_response({
request,
options,
response_headers,
$session,
route,
status: 200,
Expand All @@ -291,6 +331,7 @@ export default async function render_page(request, context, options) {
return await get_response({
request,
options,
response_headers,
$session,
route,
status,
Expand Down
7 changes: 7 additions & 0 deletions packages/kit/src/renderer/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function normalize_headers(headers) {
const normalized = {};
for (const key in headers) {
normalized[key.toLowerCase()] = headers[key];
}
return normalized;
}