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

feat: implement read #11649

Merged
merged 59 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
f53ca45
feat: implement readAsset
Rich-Harris Jan 16, 2024
716c594
lint etc
Rich-Harris Jan 16, 2024
a294256
maybe make it work on netlify? no idea how to test, manual deploys do…
Rich-Harris Jan 16, 2024
15880de
set length/type from manifest
Rich-Harris Jan 16, 2024
5163e36
tidy up
Rich-Harris Jan 16, 2024
289155a
regenerate types
Rich-Harris Jan 16, 2024
da88785
lint
Rich-Harris Jan 16, 2024
caf010f
missed a spot
Rich-Harris Jan 17, 2024
4eba560
more efficient manifest generation
Rich-Harris Jan 17, 2024
6045dfc
working on vercel
Rich-Harris Jan 17, 2024
5c6f2df
lint/fix
Rich-Harris Jan 17, 2024
c9bfc03
Update packages/adapter-vercel/index.js
Rich-Harris Jan 17, 2024
41514d3
fix
Rich-Harris Jan 17, 2024
c4ecda4
createReadable helper
Rich-Harris Jan 17, 2024
fbd02c8
Merge branch 'read-asset' of github.com:sveltejs/kit into read-asset
Rich-Harris Jan 17, 2024
809fafd
more future-proof API
Rich-Harris Jan 17, 2024
d559345
account for basepath
Rich-Harris Jan 17, 2024
d5b98da
lint
Rich-Harris Jan 17, 2024
8f5fd84
rename to just `read`
Rich-Harris Jan 17, 2024
08fd1b1
inline docs
Rich-Harris Jan 17, 2024
8d606a2
it is already deprecated, we just need to remove it
Rich-Harris Jan 17, 2024
7609aa0
read_asset -> read_implementation
Rich-Harris Jan 17, 2024
b3dfa65
add test
Rich-Harris Jan 17, 2024
38e1419
Apply suggestions from code review
Rich-Harris Jan 17, 2024
2c0f579
improve searchability
Rich-Harris Jan 17, 2024
0169191
Merge branch 'read-asset' of github.com:sveltejs/kit into read-asset
Rich-Harris Jan 17, 2024
8ec59df
prevent $app/server being imported client-side
Rich-Harris Jan 17, 2024
2c4a2cc
regenerate types
Rich-Harris Jan 17, 2024
a2c469c
add dev time feature tracking mechanism
Rich-Harris Jan 17, 2024
e338d78
test feature support at build time
Rich-Harris Jan 17, 2024
12c0e46
lint
Rich-Harris Jan 17, 2024
55c9637
lint
Rich-Harris Jan 17, 2024
2447b56
regenerate types
Rich-Harris Jan 17, 2024
75f22a3
account for hooks.server.js, mostly
Rich-Harris Jan 17, 2024
12b91b5
regenerate types
Rich-Harris Jan 17, 2024
501491f
fix
Rich-Harris Jan 18, 2024
5c140b3
bump peerdeps, add changesets
Rich-Harris Jan 18, 2024
2f2bccc
createReadable -> createReadableStream
Rich-Harris Jan 18, 2024
73bd682
Apply suggestions from code review
Rich-Harris Jan 18, 2024
39d4e6d
remove unnecessary if
Rich-Harris Jan 18, 2024
3353c1f
replace docs for find_server_assets
Rich-Harris Jan 18, 2024
8409574
update adapter author docs
Rich-Harris Jan 18, 2024
f8df6cb
regenerate types
Rich-Harris Jan 18, 2024
aea2992
explain what __SVELTEKIT_TRACK__ does
Rich-Harris Jan 18, 2024
2b44e59
mention `$app/server` on server-only modules page
Rich-Harris Jan 18, 2024
2e38c71
minor details
Rich-Harris Jan 18, 2024
36a39fb
oh ffs
Rich-Harris Jan 18, 2024
2bd0faa
exclude prerendered routes from feature detection, handle /@fs assets…
Rich-Harris Jan 18, 2024
501fa0e
use read to populate content.json
Rich-Harris Jan 18, 2024
1a35f7f
fix prerendering
Rich-Harris Jan 18, 2024
10be71f
simplify
Rich-Harris Jan 18, 2024
984e9b4
simplify docs logic
Rich-Harris Jan 18, 2024
0eebe9f
fix
Rich-Harris Jan 18, 2024
b85d699
style
Rich-Harris Jan 18, 2024
92fb9f6
simplify
Rich-Harris Jan 18, 2024
71ba727
lockfile
Rich-Harris Jan 18, 2024
86c22cc
Apply suggestions from code review
Rich-Harris Jan 18, 2024
a53c0b6
capitalize
Rich-Harris Jan 18, 2024
2b3746c
Apply suggestions from code review
Rich-Harris Jan 18, 2024
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
9 changes: 7 additions & 2 deletions packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ import path from 'node:path';
import sirv from 'sirv';
import { fileURLToPath } from 'node:url';
import { parse as polka_url_parser } from '@polka/url';
import { getRequest, setResponse } from '@sveltejs/kit/node';
import { getRequest, setResponse, readFile } from '@sveltejs/kit/node';
import { Server } from 'SERVER';
import { manifest, prerendered } from 'MANIFEST';
import { env } from 'ENV';

/* global ENV_PREFIX */

const server = new Server(manifest);
await server.init({ env: process.env });

await server.init({
env: process.env,
readAsset: (file) => readFile(path.join(dir, 'client', file))
});

const origin = env('ORIGIN', undefined);
const xff_depth = parseInt(env('XFF_DEPTH', '1'));
const address_header = env('ADDRESS_HEADER', '').toLowerCase();
Expand Down
1 change: 1 addition & 0 deletions packages/kit/scripts/generate-dts.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ await createBundle({
'$app/forms': 'src/runtime/app/forms.js',
'$app/navigation': 'src/runtime/app/navigation.js',
'$app/paths': 'src/runtime/app/paths/types.d.ts',
'$app/server': 'src/runtime/app/server/index.js',
'$app/stores': 'src/runtime/app/stores.js'
},
include: ['src']
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/core/sync/write_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const server_template = ({
import root from '../root.${isSvelte5Plus() ? 'js' : 'svelte'}';
import { set_building, set_prerendering } from '__sveltekit/environment';
import { set_assets } from '__sveltekit/paths';
import { set_read_asset } from '__sveltekit/server';
import { set_private_env, set_public_env, set_safe_public_env } from '${runtime_directory}/shared-server.js';

export const options = {
Expand Down Expand Up @@ -68,7 +69,7 @@ export async function get_hooks() {
};
}

export { set_assets, set_building, set_prerendering, set_private_env, set_public_env, set_safe_public_env };
export { set_assets, set_building, set_prerendering, set_private_env, set_public_env, set_read_asset, set_safe_public_env };
`;

// TODO need to re-run this whenever src/app.html or src/error.html are
Expand Down
22 changes: 22 additions & 0 deletions packages/kit/src/exports/node/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import fs from 'node:fs';
import { Readable } from 'node:stream';
import * as mime from 'mrmime';
import * as set_cookie_parser from 'set-cookie-parser';
import { SvelteKitError } from '../../runtime/control.js';

Expand Down Expand Up @@ -189,3 +192,22 @@ export async function setResponse(res, response) {
}
}
}

/**
* TODO
* @param {string} file
*/
export function readFile(file) {
const stats = fs.statSync(file);
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
const body = Readable.toWeb(fs.createReadStream(file));

// @ts-expect-error I think the types are wrong
const response = new Response(body, {
headers: {
'Content-Length': stats.size.toString(),
'Content-Type': mime.lookup(file) ?? 'application/octet-stream'
}
});

return response;
}
1 change: 1 addition & 0 deletions packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,7 @@ export class Server {

export interface ServerInitOptions {
env: Record<string, string>;
readAsset?: (file: string) => Response;
}

export interface SSRManifest {
Expand Down
7 changes: 5 additions & 2 deletions packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { URL } from 'node:url';
import colors from 'kleur';
import sirv from 'sirv';
import { isCSSRequest, loadEnv, buildErrorMessage } from 'vite';
import { getRequest, setResponse } from '../../../exports/node/index.js';
import { getRequest, readFile, setResponse } from '../../../exports/node/index.js';
import { installPolyfills } from '../../../exports/node/polyfills.js';
import { coalesce_to_error } from '../../../utils/error.js';
import { posixify, resolve_entry, to_fs } from '../../../utils/filesystem.js';
Expand Down Expand Up @@ -470,7 +470,10 @@ export async function dev(vite, vite_config, svelte_config) {

const server = new Server(manifest);

await server.init({ env });
await server.init({
env,
readAsset: (file) => readFile(`.${file}`)
});

const request = await getRequest({
base,
Expand Down
13 changes: 12 additions & 1 deletion packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import {
env_static_public,
service_worker,
sveltekit_environment,
sveltekit_paths
sveltekit_paths,
sveltekit_server
} from './module_ids.js';
import { pathToFileURL } from 'node:url';

Expand Down Expand Up @@ -492,6 +493,16 @@ async function kit({ svelte_config }) {
}
`;
}

case sveltekit_server: {
return dedent`
export let read_asset = null;

export function set_read_asset(fn) {
read_asset = fn;
}
`;
}
}
}
};
Expand Down
5 changes: 4 additions & 1 deletion packages/kit/src/exports/vite/module_ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ export const env_static_private = '\0virtual:$env/static/private';
export const env_static_public = '\0virtual:$env/static/public';
export const env_dynamic_private = '\0virtual:$env/dynamic/private';
export const env_dynamic_public = '\0virtual:$env/dynamic/public';

export const service_worker = '\0virtual:$service-worker';
export const sveltekit_paths = '\0virtual:__sveltekit/paths';

export const sveltekit_environment = '\0virtual:__sveltekit/environment';
export const sveltekit_paths = '\0virtual:__sveltekit/paths';
export const sveltekit_server = '\0virtual:__sveltekit/server';
5 changes: 3 additions & 2 deletions packages/kit/src/exports/vite/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { pathToFileURL } from 'node:url';
import { lookup } from 'mrmime';
import sirv from 'sirv';
import { loadEnv, normalizePath } from 'vite';
import { getRequest, setResponse } from '../../../exports/node/index.js';
import { getRequest, readFile, setResponse } from '../../../exports/node/index.js';
import { installPolyfills } from '../../../exports/node/polyfills.js';
import { SVELTE_KIT_ASSETS } from '../../../constants.js';
import { not_found } from '../utils.js';
Expand Down Expand Up @@ -47,7 +47,8 @@ export async function preview(vite, vite_config, svelte_config) {

const server = new Server(manifest);
await server.init({
env: loadEnv(vite_config.mode, svelte_config.kit.env.dir, '')
env: loadEnv(vite_config.mode, svelte_config.kit.env.dir, ''),
readAsset: (file) => readFile(dir + file)
});

return () => {
Expand Down
32 changes: 32 additions & 0 deletions packages/kit/src/runtime/app/server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { read_asset } from '__sveltekit/server';

/**
* TODO docs
* @param {string} file
* @returns {Response}
*/
export function readAsset(file) {
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
if (!read_asset) {
throw new Error(
'No readAsset implementation was provided. Please ensure that your adapter is up to date and supports this feature'
);
}

// handle inline assets internally
if (file.startsWith('data:')) {
const [prelude, data] = file.split(';');
const type = prelude.slice(5) || 'application/octet-stream';

const decoded = data.startsWith('base64,') ? atob(data.slice(7)) : decodeURIComponent(data);

return new Response(decoded, {
headers: {
'Content-Type': type,
'Content-Length': decoded.length.toString()
}
});
}

// for everything else, delegate to adapter
return read_asset(file);
}
10 changes: 8 additions & 2 deletions packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { options, get_hooks } from '__SERVER__/internal.js';
import { DEV } from 'esm-env';
import { filter_private_env, filter_public_env } from '../../utils/env.js';
import { prerendering } from '__sveltekit/environment';
import { set_read_asset } from '__sveltekit/server';

/** @type {ProxyHandler<{ type: 'public' | 'private' }>} */
const prerender_env_handler = {
Expand All @@ -30,10 +31,11 @@ export class Server {

/**
* @param {{
* env: Record<string, string>
* env: Record<string, string>;
* readAsset?: (file: string) => Response;
* }} opts
*/
async init({ env }) {
async init({ env, readAsset }) {
// Take care: Some adapters may have to call `Server.init` per-request to set env vars,
// so anything that shouldn't be rerun should be wrapped in an `if` block to make sure it hasn't
// been done already.
Expand All @@ -55,6 +57,10 @@ export class Server {
);
set_safe_public_env(public_env);

if (readAsset) {
set_read_asset(readAsset);
}

if (!this.#options.hooks) {
try {
const module = await get_hooks();
Expand Down
6 changes: 6 additions & 0 deletions packages/kit/src/types/ambient-private.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ declare module '__sveltekit/paths' {
export function override(paths: { base: string; assets: string }): void;
export function set_assets(path: string): void;
}

/** Internal version of $app/server */
declare module '__sveltekit/server' {
export function read_asset(path: string): Response;
export function set_read_asset(fn: (path: string) => Response): void;
}
1 change: 1 addition & 0 deletions packages/kit/src/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface ServerInternalModule {
set_prerendering(): void;
set_private_env(environment: Record<string, string>): void;
set_public_env(environment: Record<string, string>): void;
set_read_asset(implementation: (path: string) => Response): void;
set_safe_public_env(environment: Record<string, string>): void;
set_version(version: string): void;
set_fix_stack_trace(fix_stack_trace: (error: unknown) => string): void;
Expand Down
12 changes: 12 additions & 0 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,7 @@ declare module '@sveltejs/kit' {

export interface ServerInitOptions {
env: Record<string, string>;
readAsset?: (file: string) => Response;
}

export interface SSRManifest {
Expand Down Expand Up @@ -1873,6 +1874,10 @@ declare module '@sveltejs/kit/node' {
}): Promise<Request>;

export function setResponse(res: import('http').ServerResponse, response: Response): Promise<void>;
/**
* TODO
* */
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
export function readFile(file: string): Response;
}

declare module '@sveltejs/kit/node/polyfills' {
Expand Down Expand Up @@ -2107,6 +2112,13 @@ declare module '$app/paths' {
export function resolveRoute(id: string, params: Record<string, string | undefined>): string;
}

declare module '$app/server' {
/**
* TODO docs
* */
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
export function readAsset(file: string): Response;
}

declare module '$app/stores' {
export function getStores(): {

Expand Down
Loading