@nprpc/adapter-sveltekit
v1.0.0
Published
SvelteKit adapter for NPRPC - serves SSR via shared memory IPC
Maintainers
Readme
@nprpc/adapter-sveltekit
A SvelteKit adapter that serves SSR (Server-Side Rendering) via shared memory IPC with an NPRPC C++ server.
Overview
This adapter enables high-performance SSR by:
- Building your SvelteKit app for production
- Creating a Node.js handler that communicates via shared memory
- The C++ NPRPC server handles HTTP requests and forwards them to the Node.js handler
- Responses are returned through the same shared memory channel
This eliminates HTTP overhead for SSR, achieving near-native performance.
Installation
npm install @nprpc/adapter-sveltekitUsage
svelte.config.js
import adapter from '@nprpc/adapter-sveltekit';
export default {
kit: {
adapter: adapter({
// Optional: output directory (default: 'build')
out: 'build',
// Optional: fixed channel ID (default: from environment)
channelId: 'my-app-channel',
// Optional: precompress static assets (default: true)
precompress: true
})
}
};Building
npm run buildRunning
The built app expects a C++ NPRPC server to be running with the shared memory channel:
# Set the channel ID (if not using fixed channelId in config)
export NPRPC_CHANNEL_ID=my-app-channel
# Start the Node.js handler
node build/index.jsArchitecture
┌─────────────────┐ HTTP ┌───────────────────┐
│ Browser │ ◄──────────► │ NPRPC C++ Server │
└─────────────────┘ └────────┬──────────┘
│
Shared Memory
│
┌────────▼────────┐
│ SvelteKit SSR │
│ (Node.js) │
└─────────────────┘Request Flow
- Browser sends HTTP request to C++ server
- C++ server serializes request to JSON
- Request sent via shared memory to Node.js
- SvelteKit processes request (SSR)
- Response sent back via shared memory
- C++ server returns HTTP response to browser
Message Format
C++ Server Integration
The C++ server needs to:
- Create a shared memory channel with
SharedMemoryChannel - For SSR requests, serialize the HTTP request to JSON
- Send via shared memory and wait for response
- Deserialize response and return to client
Example (simplified):
#include <nprpc/impl/shared_memory_channel.hpp>
#include <nlohmann/json.hpp>
class SsrHandler {
nprpc::impl::SharedMemoryChannel channel_;
public:
SsrHandler(boost::asio::io_context& ioc, const std::string& channel_id)
: channel_(ioc, channel_id, true, true) // server, create
{}
std::string handle_ssr_request(
const std::string& method,
const std::string& url,
const std::map<std::string, std::string>& headers,
const std::string& body
) {
// Serialize request
nlohmann::json req = {
{"type", "request"},
{"id", next_request_id_++},
{"method", method},
{"url", url},
{"headers", headers},
{"body", base64_encode(body)}
};
// Send and wait for response
channel_.send(req.dump());
// ... wait for response ...
return base64_decode(response["body"]);
}
};Performance
Shared memory IPC provides:
- Zero network overhead - No TCP/HTTP stack for SSR
- ~1+ GB/s throughput - Limited only by memory bandwidth
- Sub-millisecond latency - Direct memory access
License
MIT
