@dgxo/sdk-mashaguard
v4.0.0
Published
Official Node.js SDK for MashaGuard — Runtime JavaScript Protection Platform
Maintainers
Readme
@dgxo/sdk-mashaguard
Official Node.js SDK for MashaGuard — Runtime JavaScript Protection Platform.
Installation
npm install @dgxo/sdk-mashaguardEnvironment Variables
MASHAGUARD_API_KEY=MG-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXUsage
Express
const { MashaGuard } = require('@dgxo/sdk-mashaguard');
const masha = new MashaGuard(process.env.MASHAGUARD_API_KEY);
// 1. Expose handshake endpoint + auto-inject loader ke semua halaman HTML
app.use(masha.protect());
app.use(masha.injectLoader());Atau kalau mau manual inject per-route:
app.use(masha.protect());
app.get('/', (req, res) => {
res.send(`
<html>
<head>
${masha.getLoaderScript()}
</head>
<body>...</body>
</html>
`);
});Next.js App Router
// middleware.js
import { MashaGuard } from '@dgxo/sdk-mashaguard';
const masha = new MashaGuard(process.env.MASHAGUARD_API_KEY);
export default masha.protect();
export const config = { matcher: ['/.masha-handshake'] };Di layout/page, tambahkan loader script:
// app/layout.js
import { MashaGuard } from '@dgxo/sdk-mashaguard';
const masha = new MashaGuard(process.env.MASHAGUARD_API_KEY);
export default function RootLayout({ children }) {
return (
<html>
<head>
<div dangerouslySetInnerHTML={{ __html: masha.getLoaderScript() }} />
</head>
<body>{children}</body>
</html>
);
}Next.js Pages Router
// pages/api/[...masha].js
import { MashaGuard } from '@dgxo/sdk-mashaguard';
const masha = new MashaGuard(process.env.MASHAGUARD_API_KEY);
export default masha.handler();Di _document.js:
import { MashaGuard } from '@dgxo/sdk-mashaguard';
const masha = new MashaGuard(process.env.MASHAGUARD_API_KEY);
export default function Document() {
return (
<Html>
<Head>
<div dangerouslySetInnerHTML={{ __html: masha.getLoaderScript() }} />
</Head>
<body><Main /><NextScript /></body>
</Html>
);
}Fastify
const { MashaGuard } = require('@dgxo/sdk-mashaguard');
const masha = new MashaGuard(process.env.MASHAGUARD_API_KEY);
fastify.register(masha.protect());API Reference
| Method | Keterangan |
|---|---|
| protect() | Expose endpoint /.masha-handshake di server |
| injectLoader() | Express middleware — auto-inject loader ke semua HTML response |
| getLoaderScript() | Return string HTML <script> untuk di-inject manual ke <head> |
| handler() | Next.js Pages Router handler |
| handshake(req, res) | Low-level handshake method |
License
Apache-2.0 © DGXO
