* @param {import('@sveltejs/kit').ServerInitOptions} opts
({ env, read })
| 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); |
no test coverage detected