Skip to content

fix: add README.md for @rep-protocol/next plugin; enhance keys management#42

Merged
olamide226 merged 1 commit intomainfrom
fix/next-plugin
Mar 19, 2026
Merged

fix: add README.md for @rep-protocol/next plugin; enhance keys management#42
olamide226 merged 1 commit intomainfrom
fix/next-plugin

Conversation

@olamide226
Copy link
Copy Markdown
Contributor

This pull request introduces a Next.js plugin for the Runtime Environment Protocol (REP) and improves the handling of ephemeral cryptographic keys to ensure singleton behavior across the development server. The main changes include adding comprehensive documentation for the plugin and refactoring the key management logic to use a process-wide singleton pattern.

Plugin Documentation and Usage

  • Added a detailed README.md for the @rep-protocol/next plugin, including installation instructions, setup steps, security considerations, troubleshooting, and usage examples for integrating REP environment variables into Next.js projects.

Key Management Improvements

  • Refactored the getOrCreateKeys function in keys.ts to use globalThis and a unique symbol (Symbol.for('__rep_next_keys__')) for storing ephemeral cryptographic keys, ensuring singleton behavior even when modules are duplicated by bundlers in Next.js development.
  • Updated comments and logic to clarify that both RepScript and the session-key route handler now reliably share the same keys, matching the gateway's behavior and preventing key mismatches across module instances. in keys.ts

Copilot AI review requested due to automatic review settings March 19, 2026 01:45
@olamide226 olamide226 merged commit 708ced3 into main Mar 19, 2026
11 checks passed
@olamide226 olamide226 deleted the fix/next-plugin branch March 19, 2026 01:48
@github-actions github-actions bot mentioned this pull request Mar 19, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds documentation and improves dev-time key sharing for the @rep-protocol/next plugin so that REP payload encryption and /rep/session-key decryption remain consistent within a single Next.js dev server process.

Changes:

  • Added a new plugins/next/README.md with install/setup, security notes, and troubleshooting guidance.
  • Refactored getOrCreateKeys() to use a globalThis + Symbol.for(...) process-wide singleton to avoid key mismatches when modules are duplicated by bundlers.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
plugins/next/src/keys.ts Switches ephemeral key storage from module-scoped to process-wide singleton via globalThis + Symbol.for.
plugins/next/README.md Introduces usage/setup docs and troubleshooting notes for the Next.js REP plugin.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


### 1. Add `<RepScript />` to your root layout

`RepScript` is a React Server Component that injects a `<script id="__rep__">` tag containing your REP variables. It only renders in development; in production (`NODE_ENV=production`) it returns `null`.

| Prop | Type | Default | Description |
| -------- | --------- | -------------- | ------------------------------------------------ |
| `env` | `string` | `".env.local"` | Path to env file, relative to project root. |

The handler returns the decryption key in development and a `404` in production (the gateway serves this endpoint in prod with rate limiting and single-use tokens).

**Static export (`output: "export"`):** Add `force-static` so the export build doesn't reject the route:

## How it works

1. **`RepScript`** reads `.env.local` at render time, classifies variables by prefix, encrypts sensitive vars with AES-256-GCM, and outputs a `<script type="application/json">` tag.
module.exports = nextConfig;
```

> With `output: "export"`, Next.js warns that rewrites don't apply to exported builds — this is expected. The rewrite only needs to work during `next dev`; in production the gateway serves `/rep/session-key` directly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants