Skip to content

Commit 5a3b351

Browse files
authored
Add docs (#234)
1 parent 6eba028 commit 5a3b351

File tree

4 files changed

+159
-3
lines changed

4 files changed

+159
-3
lines changed

docs/.vitepress/config.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const guidesSidebar = [
4949
{text: 'Scaffolding', link: '/guide/scaffolding'},
5050
{text: 'Security', link: '/guide/security'},
5151
{text: 'Storefront Next', link: '/guide/storefront-next'},
52+
{text: 'MRT Utilities', link: '/guide/mrt-utilities'},
5253
],
5354
},
5455
{

docs/guide/mrt-utilities.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
---
2+
description: Use the mrt-utilities package to simulate a deployed Managed Runtime environment locally with Express middleware and streaming adapters.
3+
---
4+
5+
# MRT Utilities
6+
7+
The `@salesforce/mrt-utilities` package provides middleware and utilities to simulate a deployed Managed Runtime (MRT) environment. Use it when building storefronts or apps that run on MRT so you can develop and test locally with the same request flow, proxy behavior, and static asset paths as in production.
8+
9+
## When to use
10+
11+
- **Local development** of PWA Kit or other MRT-hosted apps: run an Express server that mimics MRT’s request processor, proxying, and static asset serving.
12+
- **Testing** request processor logic and proxy configs before deploying to MRT.
13+
- **Streaming/SSR** on Lambda: use the streaming subpath to adapt Express apps to AWS Lambda with response streaming and compression.
14+
15+
## Prerequisites
16+
17+
- Node.js 22.16.0 or later
18+
- [Express](https://expressjs.com/) 5.x (peer dependency)
19+
20+
## Installation
21+
22+
```bash
23+
pnpm add @salesforce/mrt-utilities express
24+
# or
25+
npm install @salesforce/mrt-utilities express
26+
```
27+
28+
## Package exports
29+
30+
| Export | Description |
31+
|--------|-------------|
32+
| **Main** (`@salesforce/mrt-utilities`) | Middleware factories, `isLocal`, and re-exports from subpaths |
33+
| **Middleware** (`@salesforce/mrt-utilities/middleware`) | MRT-style Express middleware and `ProxyConfig` type |
34+
| **Metrics** (`@salesforce/mrt-utilities/metrics`) | Metrics sending for MRT (e.g. CloudWatch) |
35+
| **Streaming** (`@salesforce/mrt-utilities/streaming`) | Lambda streaming adapter, Express request/response helpers, compression config |
36+
37+
## Basic setup
38+
39+
Wire the middleware in the order your app needs. **Use `createMRTCommonMiddleware` and `createMRTCleanUpMiddleware` in all environments** (local and deployed). For local-only behavior (request processor, proxies, static assets), guard with `isLocal()`.
40+
41+
```typescript
42+
import express from 'express';
43+
import {
44+
createMRTProxyMiddlewares,
45+
createMRTRequestProcessorMiddleware,
46+
createMRTStaticAssetServingMiddleware,
47+
createMRTCommonMiddleware,
48+
createMRTCleanUpMiddleware,
49+
isLocal,
50+
} from '@salesforce/mrt-utilities';
51+
52+
const app = express();
53+
app.disable('x-powered-by');
54+
55+
// Top-most: set up MRT-style headers
56+
app.use(createMRTCommonMiddleware());
57+
58+
if (isLocal()) {
59+
const requestProcessorPath = 'path/to/request-processor.js';
60+
const proxyConfigs = [
61+
{ host: 'https://example.com', path: 'api' },
62+
];
63+
64+
app.use(createMRTRequestProcessorMiddleware(requestProcessorPath, proxyConfigs));
65+
66+
const mrtProxies = createMRTProxyMiddlewares(proxyConfigs);
67+
mrtProxies.forEach(({ path, fn }) => app.use(path, fn));
68+
69+
const staticAssetDir = 'path/to/static';
70+
app.use(
71+
`/mobify/bundle/${process.env.BUNDLE_ID || '1'}/static/`,
72+
createMRTStaticAssetServingMiddleware(staticAssetDir)
73+
);
74+
}
75+
76+
// Clean up headers and set remaining values
77+
app.use(createMRTCleanUpMiddleware());
78+
```
79+
80+
## Middleware
81+
82+
### createMRTCommonMiddleware()
83+
84+
Sets headers and other request/response behavior to match MRT. **Use in all environments** (local and deployed). Mount at the top of your middleware stack.
85+
86+
### createMRTRequestProcessorMiddleware(requestProcessorPath, proxyConfigs)
87+
88+
- **requestProcessorPath**: Path to your request processor module (e.g. `request-processor.js`).
89+
- **proxyConfigs**: Array of `{ host, path }` used for proxy and request-processor routing.
90+
91+
Runs your request processor in the local pipeline so routing and SSR behave like MRT.
92+
93+
### createMRTProxyMiddlewares(proxyConfigs)
94+
95+
Returns an array of `{ path, fn }` for mounting proxy middleware. Each entry proxies under `/mobify/proxy/<path>` to the configured `host`. Mount each with `app.use(path, fn)`.
96+
97+
**ProxyConfig** (from `@salesforce/mrt-utilities/middleware`):
98+
99+
```typescript
100+
interface ProxyConfig {
101+
host: string; // e.g. 'https://example.com'
102+
path: string; // e.g. 'api'
103+
}
104+
```
105+
106+
### createMRTStaticAssetServingMiddleware(staticAssetDir)
107+
108+
Serves static files from `staticAssetDir` under the MRT bundle static path. Use the same path pattern as in production (e.g. `/mobify/bundle/<id>/static/`).
109+
110+
### createMRTCleanUpMiddleware()
111+
112+
Removes internal MRT headers and sets any remaining response headers. **Use in all environments** (local and deployed). Mount after your app logic and before sending the response.
113+
114+
## Environment detection
115+
116+
**isLocal()** returns `true` when not running in AWS Lambda (i.e. when `AWS_LAMBDA_FUNCTION_NAME` is not set). Use it to enable local-only middleware (request processor, proxies, local static assets).
117+
118+
```typescript
119+
import { isLocal } from '@salesforce/mrt-utilities';
120+
121+
if (isLocal()) {
122+
// Use local request processor, proxies, static assets
123+
}
124+
```
125+
126+
## Streaming (Lambda)
127+
128+
For MRT’s Lambda runtime with streaming responses (e.g. SSR), use the **streaming** subpath:
129+
130+
```typescript
131+
import {
132+
createStreamingLambdaAdapter,
133+
type CompressionConfig,
134+
} from '@salesforce/mrt-utilities/streaming';
135+
```
136+
137+
- **createStreamingLambdaAdapter**: Wraps your Express app so it can be invoked from Lambda with streaming support.
138+
- **CompressionConfig**: Options for response compression (e.g. encoding, quality).
139+
140+
See the package source and tests for full adapter usage.
141+
142+
## Metrics
143+
144+
For sending metrics (e.g. to CloudWatch) in an MRT-compatible way:
145+
146+
```typescript
147+
import { MetricsSender } from '@salesforce/mrt-utilities/metrics';
148+
```
149+
150+
Use when you need to emit metrics from the same process that serves requests (e.g. custom middleware or request processor).
151+
152+
## Related
153+
154+
- [MRT CLI commands](/cli/mrt) — manage MRT projects, environments, and bundles from the CLI.
155+
- [Storefront Next](/guide/storefront-next) — end-to-end setup including MRT and local development.

packages/b2c-vs-extension/src/scaffold/scaffold-commands.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ async function runScaffoldWizard(
161161
const stepTitle = `${scaffold.manifest.displayName} (${stepIndex}/${visibleParams.length})`;
162162
log.appendLine(`[Scaffold] Prompting for param: ${param.name} (type: ${param.type})`);
163163

164-
// eslint-disable-next-line no-await-in-loop
165164
const value = await promptForParameter(param, scaffold, projectRoot, configProvider, log, stepTitle);
166165
if (value === undefined) {
167166
log.appendLine('[Scaffold] User cancelled');

packages/mrt-utilities/src/middleware/middleware.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ export const createMRTStaticAssetServingMiddleware = (staticAssetDir: string): R
410410
* Creates a common middleware function that sets the host header based on environment variables.
411411
*
412412
* The host header is set to EXTERNAL_DOMAIN_NAME if available, otherwise defaults to 'localhost:2401'.
413+
* Use this middleware in all environments (local and deployed), at the top of your middleware stack.
413414
*
414415
* @returns Express middleware function
415416
*
@@ -434,8 +435,8 @@ export const createMRTCommonMiddleware = (): RequestHandler => {
434435
* - Removes API Gateway headers that shouldn't be forwarded
435436
* - Optionally updates the path and querystring if the request wasn't processed by the request processor
436437
*
437-
* This middleware should typically be used at the end of the middleware chain to ensure
438-
* all internal headers are removed before the request is handled by the application.
438+
* Use this middleware in all environments (local and deployed), at the end of the middleware chain,
439+
* to ensure all internal headers are removed before the request is handled by the application.
439440
*
440441
* @returns Express middleware function
441442
*

0 commit comments

Comments
 (0)