|
import { respond } from './respond.js'; |
|
import { set_private_env, set_public_env, set_safe_public_env } from '../shared-server.js'; |
|
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_implementation, set_manifest } from '__sveltekit/server'; |
|
|
|
|
|
const prerender_env_handler = { |
|
get({ type }, prop) { |
|
throw new Error( |
|
`Cannot read values from $env/dynamic/${type} while prerendering (attempted to read env.${prop.toString()}). Use $env/static/${type} instead` |
|
); |
|
} |
|
}; |
|
|
|
export class Server { |
|
|
|
#options; |
|
|
|
|
|
#manifest; |
|
|
|
|
|
constructor(manifest) { |
|
|
|
this.#options = options; |
|
this.#manifest = manifest; |
|
|
|
set_manifest(manifest); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async init({ env, read }) { |
|
|
|
|
|
|
|
|
|
|
|
const prefixes = { |
|
public_prefix: this.#options.env_public_prefix, |
|
private_prefix: this.#options.env_private_prefix |
|
}; |
|
|
|
const private_env = filter_private_env(env, prefixes); |
|
const public_env = filter_public_env(env, prefixes); |
|
|
|
set_private_env( |
|
prerendering ? new Proxy({ type: 'private' }, prerender_env_handler) : private_env |
|
); |
|
set_public_env( |
|
prerendering ? new Proxy({ type: 'public' }, prerender_env_handler) : public_env |
|
); |
|
set_safe_public_env(public_env); |
|
|
|
if (read) { |
|
set_read_implementation(read); |
|
} |
|
|
|
if (!this.#options.hooks) { |
|
try { |
|
const module = await get_hooks(); |
|
|
|
this.#options.hooks = { |
|
handle: module.handle || (({ event, resolve }) => resolve(event)), |
|
handleError: module.handleError || (({ error }) => console.error(error)), |
|
handleFetch: module.handleFetch || (({ request, fetch }) => fetch(request)), |
|
reroute: module.reroute || (() => {}) |
|
}; |
|
} catch (error) { |
|
if (DEV) { |
|
this.#options.hooks = { |
|
handle: () => { |
|
throw error; |
|
}, |
|
handleError: ({ error }) => console.error(error), |
|
handleFetch: ({ request, fetch }) => fetch(request), |
|
reroute: () => {} |
|
}; |
|
} else { |
|
throw error; |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
async respond(request, options) { |
|
return respond(request, this.#options, this.#manifest, { |
|
...options, |
|
error: false, |
|
depth: 0 |
|
}); |
|
} |
|
} |
|
|