MCPcopy
hub / github.com/sveltejs/kit / init

Method init

packages/kit/src/runtime/server/index.js:41–156  ·  view source on GitHub ↗

* @param {import('@sveltejs/kit').ServerInitOptions} opts

({ env, read })

Source from the content-addressed store, hash-verified

39 * @param {import('@sveltejs/kit').ServerInitOptions} opts
40 */
41 async init({ env, read }) {
42 // Take care: Some adapters may have to call `Server.init` per-request to set env vars,
43 // so anything that shouldn't be rerun should be wrapped in an `if` block to make sure it hasn't
44 // been done already.
45
46 // set env, in case it's used in initialisation
47 const prefixes = {
48 public_prefix: this.#options.env_public_prefix,
49 private_prefix: this.#options.env_private_prefix
50 };
51
52 const private_env = filter_private_env(env, prefixes);
53 const public_env = filter_public_env(env, prefixes);
54
55 set_private_env(
56 prerendering ? new Proxy({ type: 'private' }, prerender_env_handler) : private_env
57 );
58 set_public_env(
59 prerendering ? new Proxy({ type: 'public' }, prerender_env_handler) : public_env
60 );
61 set_safe_public_env(public_env);
62
63 if (read) {
64 // Wrap the read function to handle MaybePromise<ReadableStream>
65 // and ensure the public API stays synchronous
66 /** @param {string} file */
67 const wrapped_read = (file) => {
68 const result = read(file);
69 if (result instanceof ReadableStream) {
70 return result;
71 } else {
72 return new ReadableStream({
73 async start(controller) {
74 try {
75 const stream = await Promise.resolve(result);
76 if (!stream) {
77 controller.close();
78 return;
79 }
80
81 const reader = stream.getReader();
82
83 while (true) {
84 const { done, value } = await reader.read();
85 if (done) break;
86 controller.enqueue(value);
87 }
88
89 controller.close();
90 } catch (error) {
91 controller.error(error);
92 }
93 }
94 });
95 }
96 };
97
98 set_read_implementation(wrapped_read);

Callers 11

prerenderFunction · 0.95
generate_fallbackFunction · 0.95
devFunction · 0.95
previewFunction · 0.95
initFunction · 0.95
serverless.jsFile · 0.80
edge.jsFile · 0.80
worker.jsFile · 0.80
handler.jsFile · 0.80
startFunction · 0.80
edge.jsFile · 0.80

Calls 9

filter_private_envFunction · 0.90
filter_public_envFunction · 0.90
set_private_envFunction · 0.90
set_public_envFunction · 0.90
set_safe_public_envFunction · 0.90
set_appFunction · 0.90
resolveFunction · 0.70
errorMethod · 0.65
fetchFunction · 0.50

Tested by

no test coverage detected