Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/flat-dogs-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@salesforce/mrt-utilities': patch
---

Initial release
2 changes: 1 addition & 1 deletion packages/b2c-tooling-sdk/src/plugins/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function createHookContext(options: HookContextOptions = {}): HookContext
* esbuild transforms `import()` to `require()` in CJS output, which cannot
* load ESM plugins. Using `new Function` preserves the native dynamic import.
*/
// eslint-disable-next-line @typescript-eslint/no-implied-eval

const dynamicImport = new Function('specifier', 'return import(specifier)') as (
specifier: string,
) => Promise<Record<string, unknown>>;
Expand Down
7 changes: 7 additions & 0 deletions packages/mrt-utilities/.c8rc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"all": true,
"src": ["src"],
"exclude": ["test/**", "**/*.d.ts", "**/*.test.ts"],
"reporter": ["text", "text-summary", "html", "lcov"],
"report-dir": "coverage"
}
4 changes: 4 additions & 0 deletions packages/mrt-utilities/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
dist/
coverage/
*.tsbuildinfo
6 changes: 6 additions & 0 deletions packages/mrt-utilities/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"node-option": ["import=tsx", "conditions=development"],
"timeout": 10000,
"recursive": true,
"extension": ["ts"]
}
49 changes: 49 additions & 0 deletions packages/mrt-utilities/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# mrt-utilities

Middleware and utilities to simulate a deployed MRT environment.

## Usage

```
import {
createMRTProxyMiddlewares,
createMRTRequestProcessorMiddleware,
createMRTStaticAssetServingMiddleware,
createMRTCommonMiddleware,
createMRTCleanUpMiddleware,
isLocal,
} from '@salesforce/mrt-utilities';


export const createApp = (): Express => {
const app = express();
app.disable('x-powered-by');

// Top most middleware to set up headers
app.use(createMRTCommonMiddleware());

if (isLocal()) {
const requestProcessorPath = 'path/to/request-processor.js';
const proxyConfigs = [
{
host: 'https://example.com',
path: 'api',
},
];
app.use(createMRTRequestProcessorMiddleware(requestProcessorPath, proxyConfigs));

const mrtProxies = createMRTProxyMiddlewares(proxyConfigs);
mrtProxies.forEach(({ path, fn }) => {
app.use(path, fn);
});

const staticAssetDir = 'path/to/static';
app.use(
`/mobify/bundle/${process.env.BUNDLE_ID || '1'}/static/`,
createMRTStaticAssetServingMiddleware(staticAssetDir)
);
}

// Cleans up any remaining headers and sets any remaining values
app.use(createMRTCleanUpMiddleware());
```
45 changes: 45 additions & 0 deletions packages/mrt-utilities/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
import {includeIgnoreFile} from '@eslint/compat';
import headerPlugin from 'eslint-plugin-header';
import tseslint from 'typescript-eslint';
import path from 'node:path';
import {fileURLToPath} from 'node:url';

import {copyrightHeader, sharedRules, chaiTestRules, prettierPlugin} from '../../eslint.config.mjs';

const gitignorePath = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '.gitignore');
headerPlugin.rules.header.meta.schema = false;

export default [
includeIgnoreFile(gitignorePath),
...tseslint.configs.recommended,
prettierPlugin,
{
files: ['**/*.ts'],
plugins: {
header: headerPlugin,
},
languageOptions: {
parserOptions: {
project: true,
tsconfigRootDir: import.meta.dirname,
},
},
rules: {
'header/header': ['error', 'block', copyrightHeader],
...sharedRules,
},
},
{
files: ['test/**/*.ts'],
rules: {
...chaiTestRules,
// Streaming adapter tests use any for mock streams and event shapes
'@typescript-eslint/no-explicit-any': 'off',
},
},
];
132 changes: 132 additions & 0 deletions packages/mrt-utilities/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
{
"name": "@salesforce/mrt-utilities",
"version": "0.0.1",
"description": "Middleware and utilities to simulate a deployed Managed Runtime environment",
"type": "module",
"author": "Salesforce",
"license": "Apache-2.0",
"repository": "SalesforceCommerceCloud/b2c-developer-tooling",
"keywords": [
"salesforce",
"commerce-cloud",
"mrt",
"middleware",
"express"
],
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/esm/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"development": "./src/index.ts",
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/cjs/index.d.ts",
"default": "./dist/cjs/index.js"
}
},
"./middleware": {
"development": "./src/middleware/index.ts",
"import": {
"types": "./dist/esm/middleware/index.d.ts",
"default": "./dist/esm/middleware/index.js"
},
"require": {
"types": "./dist/cjs/middleware/index.d.ts",
"default": "./dist/cjs/middleware/index.js"
}
},
"./metrics": {
"development": "./src/metrics/index.ts",
"import": {
"types": "./dist/esm/metrics/index.d.ts",
"default": "./dist/esm/metrics/index.js"
},
"require": {
"types": "./dist/cjs/metrics/index.d.ts",
"default": "./dist/cjs/metrics/index.js"
}
},
"./streaming": {
"development": "./src/streaming/index.ts",
"import": {
"types": "./dist/esm/streaming/index.d.ts",
"default": "./dist/esm/streaming/index.js"
},
"require": {
"types": "./dist/cjs/streaming/index.d.ts",
"default": "./dist/cjs/streaming/index.js"
}
}
},
"files": [
"dist"
],
"scripts": {
"build": "pnpm run build:esm && pnpm run build:cjs",
"build:esm": "tsc -p tsconfig.esm.json",
"build:cjs": "tsc -p tsconfig.cjs.json && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
"clean": "shx rm -rf dist",
"lint": "eslint",
"lint:agent": "eslint --quiet",
"typecheck:agent": "tsc --noEmit --pretty false",
"format": "prettier --write .",
"format:check": "prettier --check .",
"pretest": "tsc --noEmit -p test",
"test": "c8 mocha --forbid-only \"test/**/*.test.ts\"",
"test:agent": "mocha --forbid-only --reporter min \"test/**/*.test.ts\"",
"test:watch": "mocha --watch \"test/**/*.test.ts\""
},
"dependencies": {
"@aws-sdk/client-cloudwatch": "3.952.0",
"@aws-sdk/client-dynamodb": "3.980.0",
"@aws-sdk/lib-dynamodb": "3.980.0",
"@h4ad/serverless-adapter": "4.4.0",
"change-case": "5.4.4",
"compressible": "2.0.18",
"http-proxy-middleware": "3.0.5",
"mime-types": "3.0.1",
"negotiator": "1.0.0",
"qs": "6.14.0",
"set-cookie-parser": "2.7.1"
},
"devDependencies": {
"@salesforce/dev-config": "catalog:",
"@serverless/event-mocks": "1.1.1",
"@types/aws-lambda": "8.10.160",
"@types/chai": "catalog:",
"@types/compressible": "2.0.3",
"@types/express": "5.0.3",
"@types/mime-types": "3.0.1",
"@types/mocha": "catalog:",
"@types/negotiator": "0.6.4",
"@types/node": "catalog:",
"@types/qs": "6.14.0",
"@types/set-cookie-parser": "2.4.10",
"@types/sinon": "catalog:",
"c8": "catalog:",
"chai": "catalog:",
"eslint": "catalog:",
"eslint-config-prettier": "catalog:",
"eslint-plugin-header": "catalog:",
"eslint-plugin-prettier": "catalog:",
"express": "5.1.0",
"mocha": "catalog:",
"prettier": "catalog:",
"shx": "catalog:",
"sinon": "catalog:",
"tsx": "catalog:",
"typescript": "catalog:",
"typescript-eslint": "catalog:"
},
"peerDependencies": {
"express": "5.1.0"
},
"engines": {
"node": ">=22.16.0"
}
}
10 changes: 10 additions & 0 deletions packages/mrt-utilities/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/

export * from './middleware/index.js';
export * from './metrics/index.js';
export * from './streaming/index.js';
export {isLocal} from './utils/utils.js';
7 changes: 7 additions & 0 deletions packages/mrt-utilities/src/metrics/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/

export * from './metrics-sender.js';
Loading
Loading