Skip to content
Merged
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: 1 addition & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
"files.exclude": {
"**/node_modules": true,
// "**/dist": true,
"**/plugins": true,
"**/sample": true,
"**/scripts": true,
"**/template": true,
// "**/plugins": true,
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"plugins/static",
"plugins/websocket",
"plugins/session",
"plugins/rate-limit"
"plugins/rate-limit",
"plugins/edge"
],
"author": "angga7togk",
"license": "MIT",
Expand All @@ -40,6 +41,7 @@
"@types/ws": "^8.18.1",
"chokidar": "^3.6.0",
"cookie": "^1.0.2",
"edge.js": "^6.3.0",
"esbuild": "^0.25.8",
"esbuild-plugin-file-path-extensions": "^2.1.4",
"install": "^0.13.0",
Expand Down
7 changes: 7 additions & 0 deletions plugins/edge/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @gaman/ejs

## 1.0.3

### Patch Changes

- 8f847a3: update
9 changes: 9 additions & 0 deletions plugins/edge/compose.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Edge } from 'edge.js';

export type EdgeHandlerFactory = (edge: Edge) => void | Promise<void>;

export function composeEdgeHandler(
factory: EdgeHandlerFactory,
): EdgeHandlerFactory {
return factory;
}
94 changes: 94 additions & 0 deletions plugins/edge/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* @module
* GamanJS integration for the Edge view engine.
*
* @ID Integrasi GamanJS dengan Edge view engine.
*/

import { DefaultMiddlewareOptions } from '@gaman/common';
import { composeMiddleware } from '@gaman/core/index.js';
export * from './compose.js';

import { Edge } from 'edge.js';
import { EdgeHandlerFactory } from './compose.js';
import { EdgeOptions } from 'edge.js/types';
import { join } from 'path';

/**
* Edge Engine options.
* These options are passed directly to the Edge Engine.
* You can find the full list of supported options at:
* @url https://edgejs.dev/docs/getting_started
*
* @ID Opsi konfigurasi untuk Edge Engine.
* Opsi ini diteruskan langsung ke Edge Engine.
* Daftar lengkap opsi bisa dilihat di:
* @url https://edgejs.dev/docs/getting_started
*/
export interface GamanEdgeOptions
extends EdgeOptions,
DefaultMiddlewareOptions {
/**
* Directory path for views.
* Specifies the root directory where Edge templates are located.
* Default: `src/views`.
*
* @ID Path direktori untuk view.
* Menentukan direktori utama tempat file template Edge disimpan.
* Default: `src/views`.
*/
viewPath?: string;

/**
* Custom handler for the Edge instance.
* Example: `(edge) => { edge.mount()... }`
*
* @ID Handler kustom untuk instance Edge.
* Contoh: `(edge) => { edge.mount()... }`
*/
handler?: EdgeHandlerFactory;
}

/**
* Creates an Edge view engine middleware for GamanJS.
*
* @ID Membuat middleware view engine Edge untuk GamanJS.
*/
export function edge(ops: GamanEdgeOptions = {}) {
const { viewPath = 'src/views', handler } = ops;
let edge: Edge;

const middleware = composeMiddleware(async (_, next) => {
if (!edge) {
edge = Edge.create({
cache: ops.cache,
loader: ops.loader,
});
edge.mount(join(process.cwd(), viewPath));

await handler?.(edge);
}

// ? Continue request chain
const res = await next();

const renderData = res.view;
if (renderData == null) return res;

const rendered = await edge.render(
renderData.getName(),
renderData.getData(),
);

res.headers.set('Content-Type', 'text/html');
res.body = rendered;

return res;
});

return middleware({
priority: ops.priority,
includes: ops.includes,
excludes: ops.excludes,
});
}
24 changes: 24 additions & 0 deletions plugins/edge/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@gaman/edge",
"version": "0.0.1",
"type": "module",
"main": "index.js",
"author": "angga7togk",
"license": "MIT",
"repository": {
"url": "git+https://github.com/7TogkID/gaman.git",
"directory": "plugins/edge"
},
"bugs": {
"url": "https://github.com/7TogkID/gaman/issues"
},
"homepage": "https://gaman.7togk.id",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
},
"peerDependencies": {
"edge.js": "*"
},
"gitHead": "8d1bf3bbac4b72847e9157e2c95d75b5e90c89cf"
}
9 changes: 9 additions & 0 deletions plugins/edge/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "nodenext",
"composite": true,
"declaration": true,
"declarationMap": false
}
}
3 changes: 3 additions & 0 deletions scripts/yalc-push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ yalc push
cd ../rate-limit
yalc push

cd ../edge
yalc push

# Return to the root directory
cd ../..

Expand Down
Loading