hono 1.6.4 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -768
- package/dist/compose.d.ts +1 -5
- package/dist/compose.js +5 -5
- package/dist/context.d.ts +30 -5
- package/dist/context.js +25 -9
- package/dist/hono.d.ts +2 -2
- package/dist/hono.js +3 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -3
- package/dist/middleware/basic-auth/index.js +0 -9
- package/dist/middleware/compress/index.d.ts +8 -0
- package/dist/middleware/compress/index.js +19 -0
- package/dist/middleware/cors/index.d.ts +1 -1
- package/dist/middleware/jsx/index.js +21 -1
- package/dist/middleware/jwt/index.js +3 -0
- package/dist/middleware/logger/index.d.ts +3 -5
- package/dist/middleware/logger/index.js +15 -16
- package/dist/middleware/serve-static/bun.d.ts +7 -0
- package/dist/middleware/serve-static/bun.js +38 -0
- package/dist/middleware/serve-static/module.mjs +1 -0
- package/dist/request.d.ts +9 -0
- package/dist/request.js +19 -0
- package/dist/utils/buffer.d.ts +1 -1
- package/dist/utils/cloudflare.d.ts +1 -1
- package/dist/utils/cookie.d.ts +13 -0
- package/dist/{middleware/cookie/index.js → utils/cookie.js} +3 -22
- package/dist/utils/jwt/jwt.js +4 -1
- package/dist/utils/jwt/types.d.ts +6 -1
- package/dist/utils/jwt/types.js +9 -4
- package/package.json +13 -23
- package/dist/middleware/body-parse/index.d.ts +0 -8
- package/dist/middleware/body-parse/index.js +0 -11
- package/dist/middleware/cookie/index.d.ts +0 -27
- package/dist/middleware/graphql-server/index.d.ts +0 -28
- package/dist/middleware/graphql-server/index.js +0 -174
- package/dist/middleware/graphql-server/parse-body.d.ts +0 -1
- package/dist/middleware/graphql-server/parse-body.js +0 -31
- package/dist/middleware/mustache/index.d.ts +0 -1
- package/dist/middleware/mustache/index.js +0 -5
- package/dist/middleware/mustache/module.d.mts +0 -5
- package/dist/middleware/mustache/module.mjs +0 -12
- package/dist/middleware/mustache/mustache.d.ts +0 -14
- package/dist/middleware/mustache/mustache.js +0 -53
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "Ultrafast web framework for Cloudflare Workers.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -9,32 +9,30 @@
|
|
|
9
9
|
],
|
|
10
10
|
"scripts": {
|
|
11
11
|
"test": "jest",
|
|
12
|
+
"test:deno": "deno test --allow-read deno_test",
|
|
13
|
+
"test:bun": "bun wiptest bun_test/index.test.ts",
|
|
12
14
|
"lint": "eslint --ext js,ts src .eslintrc.js",
|
|
13
15
|
"lint:fix": "eslint --ext js,ts src .eslintrc.js --fix",
|
|
14
16
|
"denoify": "rimraf deno_dist && denoify && rimraf 'deno_dist/**/*.test.ts'",
|
|
15
17
|
"build": "rimraf dist && tsc --project tsconfig.build.esm.json && tsc --project tsconfig.build.json",
|
|
16
18
|
"watch": "tsc --project tsconfig.build.json -w",
|
|
17
|
-
"prerelease": "yarn denoify && yarn build",
|
|
19
|
+
"prerelease": "yarn denoify && yarn test:deno && yarn build",
|
|
18
20
|
"release": "np"
|
|
19
21
|
},
|
|
20
22
|
"exports": {
|
|
21
23
|
".": "./dist/index.js",
|
|
22
24
|
"./basic-auth": "./dist/middleware/basic-auth/index.js",
|
|
23
25
|
"./bearer-auth": "./dist/middleware/bearer-auth/index.js",
|
|
24
|
-
"./body-parse": "./dist/middleware/body-parse/index.js",
|
|
25
|
-
"./cookie": "./dist/middleware/cookie/index.js",
|
|
26
26
|
"./cors": "./dist/middleware/cors/index.js",
|
|
27
27
|
"./etag": "./dist/middleware/etag/index.js",
|
|
28
|
-
"./graphql-server": "./dist/middleware/graphql-server/index.js",
|
|
29
28
|
"./html": "./dist/middleware/html/index.js",
|
|
30
29
|
"./jsx": "./dist/middleware/jsx/index.js",
|
|
31
30
|
"./jwt": "./dist/middleware/jwt/index.js",
|
|
32
31
|
"./logger": "./dist/middleware/logger/index.js",
|
|
33
|
-
"./mustache": "./dist/middleware/mustache/index.js",
|
|
34
|
-
"./mustache.module": "./dist/middleware/mustache/module.mjs",
|
|
35
32
|
"./powered-by": "./dist/middleware/powered-by/index.js",
|
|
36
33
|
"./pretty-json": "./dist/middleware/pretty-json/index.js",
|
|
37
34
|
"./serve-static": "./dist/middleware/serve-static/index.js",
|
|
35
|
+
"./serve-static.bun": "./dist/middleware/serve-static/bun.js",
|
|
38
36
|
"./serve-static.module": "./dist/middleware/serve-static/module.mjs",
|
|
39
37
|
"./router/trie-router": "./dist/router/trie-router/index.js",
|
|
40
38
|
"./router/reg-exp-router": "./dist/router/reg-exp-router/index.js",
|
|
@@ -49,9 +47,6 @@
|
|
|
49
47
|
"bearer-auth": [
|
|
50
48
|
"./dist/middleware/bearer-auth"
|
|
51
49
|
],
|
|
52
|
-
"body-parse": [
|
|
53
|
-
"./dist/middleware/body-parse"
|
|
54
|
-
],
|
|
55
50
|
"cookie": [
|
|
56
51
|
"./dist/middleware/cookie"
|
|
57
52
|
],
|
|
@@ -61,9 +56,6 @@
|
|
|
61
56
|
"etag": [
|
|
62
57
|
"./dist/middleware/etag"
|
|
63
58
|
],
|
|
64
|
-
"graphql-server": [
|
|
65
|
-
"./dist/middleware/graphql-server"
|
|
66
|
-
],
|
|
67
59
|
"html": [
|
|
68
60
|
"./dist/middleware/html"
|
|
69
61
|
],
|
|
@@ -76,12 +68,6 @@
|
|
|
76
68
|
"logger": [
|
|
77
69
|
"./dist/middleware/logger"
|
|
78
70
|
],
|
|
79
|
-
"mustache": [
|
|
80
|
-
"./dist/middleware/mustache"
|
|
81
|
-
],
|
|
82
|
-
"mustache.module": [
|
|
83
|
-
"./dist/middleware/mustache/module.d.mts"
|
|
84
|
-
],
|
|
85
71
|
"powered-by": [
|
|
86
72
|
"./dist/middleware/powered-by"
|
|
87
73
|
],
|
|
@@ -91,6 +77,9 @@
|
|
|
91
77
|
"serve-static": [
|
|
92
78
|
"./dist/middleware/serve-static/index.d.ts"
|
|
93
79
|
],
|
|
80
|
+
"serve-static.bun": [
|
|
81
|
+
"./dist/middleware/serve-static/bun.d.ts"
|
|
82
|
+
],
|
|
94
83
|
"serve-static.module": [
|
|
95
84
|
"./dist/middleware/serve-static/module.d.mts"
|
|
96
85
|
],
|
|
@@ -119,6 +108,7 @@
|
|
|
119
108
|
},
|
|
120
109
|
"homepage": "https://github.com/honojs/hono",
|
|
121
110
|
"keywords": [
|
|
111
|
+
"hono",
|
|
122
112
|
"web",
|
|
123
113
|
"app",
|
|
124
114
|
"http",
|
|
@@ -128,7 +118,9 @@
|
|
|
128
118
|
"cloudflare",
|
|
129
119
|
"workers",
|
|
130
120
|
"fastly",
|
|
131
|
-
"compute@edge"
|
|
121
|
+
"compute@edge",
|
|
122
|
+
"deno",
|
|
123
|
+
"bun"
|
|
132
124
|
],
|
|
133
125
|
"devDependencies": {
|
|
134
126
|
"@cloudflare/workers-types": "^3.7.1",
|
|
@@ -149,13 +141,11 @@
|
|
|
149
141
|
"eslint-plugin-import": "^2.26.0",
|
|
150
142
|
"eslint-plugin-node": "^11.1.0",
|
|
151
143
|
"form-data": "^4.0.0",
|
|
152
|
-
"graphql": "^16.4.0",
|
|
153
144
|
"jest": "27.5.1",
|
|
154
|
-
"jest-environment-miniflare": "^2.
|
|
145
|
+
"jest-environment-miniflare": "^2.6.0",
|
|
155
146
|
"mustache": "^4.2.0",
|
|
156
147
|
"np": "^7.6.2",
|
|
157
148
|
"prettier": "^2.6.2",
|
|
158
|
-
"prettier-plugin-md-nocjsp": "^1.2.0",
|
|
159
149
|
"rimraf": "^3.0.2",
|
|
160
150
|
"ts-jest": "^27.1.4",
|
|
161
151
|
"typescript": "^4.6.3"
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.bodyParse = void 0;
|
|
4
|
-
const body_1 = require("../../utils/body");
|
|
5
|
-
const bodyParse = () => {
|
|
6
|
-
return async (ctx, next) => {
|
|
7
|
-
ctx.req.parsedBody = await (0, body_1.parseBody)(ctx.req);
|
|
8
|
-
await next();
|
|
9
|
-
};
|
|
10
|
-
};
|
|
11
|
-
exports.bodyParse = bodyParse;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { Context } from '../../context';
|
|
2
|
-
import type { Next } from '../../hono';
|
|
3
|
-
declare global {
|
|
4
|
-
interface Request {
|
|
5
|
-
cookie: {
|
|
6
|
-
(name: string): string;
|
|
7
|
-
(): Record<string, string>;
|
|
8
|
-
};
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
declare module '../../context' {
|
|
12
|
-
interface Context {
|
|
13
|
-
cookie: (name: string, value: string, options?: CookieOptions) => void;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
export declare type Cookie = Record<string, string>;
|
|
17
|
-
export declare type CookieOptions = {
|
|
18
|
-
domain?: string;
|
|
19
|
-
expires?: Date;
|
|
20
|
-
httpOnly?: boolean;
|
|
21
|
-
maxAge?: number;
|
|
22
|
-
path?: string;
|
|
23
|
-
secure?: boolean;
|
|
24
|
-
signed?: boolean;
|
|
25
|
-
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
26
|
-
};
|
|
27
|
-
export declare const cookie: () => (c: Context, next: Next) => Promise<void>;
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { GraphQLError } from 'graphql';
|
|
2
|
-
import type { GraphQLSchema, ValidationRule, GraphQLFormattedError } from 'graphql';
|
|
3
|
-
import type { Context } from '../../context';
|
|
4
|
-
import type { Next } from '../../hono';
|
|
5
|
-
declare type Options = {
|
|
6
|
-
schema: GraphQLSchema;
|
|
7
|
-
rootValue?: unknown;
|
|
8
|
-
pretty?: boolean;
|
|
9
|
-
validationRules?: ReadonlyArray<ValidationRule>;
|
|
10
|
-
};
|
|
11
|
-
export declare const graphqlServer: (options: Options) => (c: Context, next: Next) => Promise<Response>;
|
|
12
|
-
export interface GraphQLParams {
|
|
13
|
-
query: string | null;
|
|
14
|
-
variables: {
|
|
15
|
-
readonly [name: string]: unknown;
|
|
16
|
-
} | null;
|
|
17
|
-
operationName: string | null;
|
|
18
|
-
raw: boolean;
|
|
19
|
-
}
|
|
20
|
-
export declare const getGraphQLParams: (request: Request) => Promise<GraphQLParams>;
|
|
21
|
-
export declare const errorMessages: (messages: string[], graphqlErrors?: readonly GraphQLError[] | readonly GraphQLFormattedError[] | undefined) => {
|
|
22
|
-
errors: readonly GraphQLError[] | readonly GraphQLFormattedError[];
|
|
23
|
-
} | {
|
|
24
|
-
errors: {
|
|
25
|
-
message: string;
|
|
26
|
-
}[];
|
|
27
|
-
};
|
|
28
|
-
export {};
|
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// Based on the code in the `express-graphql` package.
|
|
3
|
-
// https://github.com/graphql/express-graphql/blob/main/src/index.ts
|
|
4
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
exports.errorMessages = exports.getGraphQLParams = exports.graphqlServer = void 0;
|
|
6
|
-
const graphql_1 = require("graphql");
|
|
7
|
-
const parse_body_1 = require("./parse-body");
|
|
8
|
-
const graphqlServer = (options) => {
|
|
9
|
-
const schema = options.schema;
|
|
10
|
-
const rootValue = options.rootValue;
|
|
11
|
-
const pretty = options.pretty ?? false;
|
|
12
|
-
const validationRules = options.validationRules ?? [];
|
|
13
|
-
// const showGraphiQL = options.graphiql ?? false
|
|
14
|
-
return async (c, next) => {
|
|
15
|
-
// GraphQL HTTP only supports GET and POST methods.
|
|
16
|
-
if (c.req.method !== 'GET' && c.req.method !== 'POST') {
|
|
17
|
-
return c.json((0, exports.errorMessages)(['GraphQL only supports GET and POST requests.']), 405, {
|
|
18
|
-
Allow: 'GET, POST',
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
let params;
|
|
22
|
-
try {
|
|
23
|
-
params = await (0, exports.getGraphQLParams)(c.req);
|
|
24
|
-
}
|
|
25
|
-
catch (e) {
|
|
26
|
-
if (e instanceof Error) {
|
|
27
|
-
console.error(`${e.stack || e.message}`);
|
|
28
|
-
return c.json((0, exports.errorMessages)([e.message], [e]), 400);
|
|
29
|
-
}
|
|
30
|
-
throw e;
|
|
31
|
-
}
|
|
32
|
-
const { query, variables, operationName } = params;
|
|
33
|
-
if (query == null) {
|
|
34
|
-
return c.json((0, exports.errorMessages)(['Must provide query string.']), 400);
|
|
35
|
-
}
|
|
36
|
-
const schemaValidationErrors = (0, graphql_1.validateSchema)(schema);
|
|
37
|
-
if (schemaValidationErrors.length > 0) {
|
|
38
|
-
// Return 500: Internal Server Error if invalid schema.
|
|
39
|
-
return c.json((0, exports.errorMessages)(['GraphQL schema validation error.'], schemaValidationErrors), 500);
|
|
40
|
-
}
|
|
41
|
-
let documentAST;
|
|
42
|
-
try {
|
|
43
|
-
documentAST = (0, graphql_1.parse)(new graphql_1.Source(query, 'GraphQL request'));
|
|
44
|
-
}
|
|
45
|
-
catch (syntaxError) {
|
|
46
|
-
// Return 400: Bad Request if any syntax errors errors exist.
|
|
47
|
-
if (syntaxError instanceof Error) {
|
|
48
|
-
console.error(`${syntaxError.stack || syntaxError.message}`);
|
|
49
|
-
const e = new graphql_1.GraphQLError(syntaxError.message, {
|
|
50
|
-
originalError: syntaxError,
|
|
51
|
-
});
|
|
52
|
-
return c.json((0, exports.errorMessages)(['GraphQL syntax error.'], [e]), 400);
|
|
53
|
-
}
|
|
54
|
-
throw syntaxError;
|
|
55
|
-
}
|
|
56
|
-
// Validate AST, reporting any errors.
|
|
57
|
-
const validationErrors = (0, graphql_1.validate)(schema, documentAST, [...graphql_1.specifiedRules, ...validationRules]);
|
|
58
|
-
if (validationErrors.length > 0) {
|
|
59
|
-
// Return 400: Bad Request if any validation errors exist.
|
|
60
|
-
return c.json((0, exports.errorMessages)(['GraphQL validation error.'], validationErrors), 400);
|
|
61
|
-
}
|
|
62
|
-
if (c.req.method === 'GET') {
|
|
63
|
-
// Determine if this GET request will perform a non-query.
|
|
64
|
-
const operationAST = (0, graphql_1.getOperationAST)(documentAST, operationName);
|
|
65
|
-
if (operationAST && operationAST.operation !== 'query') {
|
|
66
|
-
/*
|
|
67
|
-
Now , does not support GraphiQL
|
|
68
|
-
if (showGraphiQL) {
|
|
69
|
-
//return respondWithGraphiQL(response, graphiqlOptions, params)
|
|
70
|
-
}
|
|
71
|
-
*/
|
|
72
|
-
// Otherwise, report a 405: Method Not Allowed error.
|
|
73
|
-
return c.json((0, exports.errorMessages)([
|
|
74
|
-
`Can only perform a ${operationAST.operation} operation from a POST request.`,
|
|
75
|
-
]), 405, { Allow: 'POST' });
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
let result;
|
|
79
|
-
try {
|
|
80
|
-
result = await (0, graphql_1.execute)({
|
|
81
|
-
schema,
|
|
82
|
-
document: documentAST,
|
|
83
|
-
rootValue,
|
|
84
|
-
variableValues: variables,
|
|
85
|
-
operationName: operationName,
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
catch (contextError) {
|
|
89
|
-
if (contextError instanceof Error) {
|
|
90
|
-
console.error(`${contextError.stack || contextError.message}`);
|
|
91
|
-
const e = new graphql_1.GraphQLError(contextError.message, {
|
|
92
|
-
originalError: contextError,
|
|
93
|
-
nodes: documentAST,
|
|
94
|
-
});
|
|
95
|
-
// Return 400: Bad Request if any execution context errors exist.
|
|
96
|
-
return c.json((0, exports.errorMessages)(['GraphQL execution context error.'], [e]), 400);
|
|
97
|
-
}
|
|
98
|
-
throw contextError;
|
|
99
|
-
}
|
|
100
|
-
if (!result.data) {
|
|
101
|
-
if (result.errors) {
|
|
102
|
-
return c.json((0, exports.errorMessages)([result.errors.toString()], result.errors), 500);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
/*
|
|
106
|
-
Now, does not support GraphiQL
|
|
107
|
-
if (showGraphiQL) {
|
|
108
|
-
}
|
|
109
|
-
*/
|
|
110
|
-
if (pretty) {
|
|
111
|
-
const payload = JSON.stringify(result, null, pretty ? 2 : 0);
|
|
112
|
-
return c.text(payload, 200, {
|
|
113
|
-
'Content-Type': 'application/json',
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
return c.json(result);
|
|
118
|
-
}
|
|
119
|
-
await next(); // XXX
|
|
120
|
-
};
|
|
121
|
-
};
|
|
122
|
-
exports.graphqlServer = graphqlServer;
|
|
123
|
-
const getGraphQLParams = async (request) => {
|
|
124
|
-
const urlData = new URLSearchParams(request.url.split('?')[1]);
|
|
125
|
-
const bodyData = await (0, parse_body_1.parseBody)(request);
|
|
126
|
-
// GraphQL Query string.
|
|
127
|
-
let query = urlData.get('query') ?? bodyData.query;
|
|
128
|
-
if (typeof query !== 'string') {
|
|
129
|
-
query = null;
|
|
130
|
-
}
|
|
131
|
-
// Parse the variables if needed.
|
|
132
|
-
let variables = (urlData.get('variables') ?? bodyData.variables);
|
|
133
|
-
if (typeof variables === 'string') {
|
|
134
|
-
try {
|
|
135
|
-
variables = JSON.parse(variables);
|
|
136
|
-
}
|
|
137
|
-
catch {
|
|
138
|
-
throw Error('Variables are invalid JSON.');
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
else if (typeof variables !== 'object') {
|
|
142
|
-
variables = null;
|
|
143
|
-
}
|
|
144
|
-
// Name of GraphQL operation to execute.
|
|
145
|
-
let operationName = urlData.get('operationName') ?? bodyData.operationName;
|
|
146
|
-
if (typeof operationName !== 'string') {
|
|
147
|
-
operationName = null;
|
|
148
|
-
}
|
|
149
|
-
const raw = urlData.get('raw') != null || bodyData.raw !== undefined;
|
|
150
|
-
const params = {
|
|
151
|
-
query: query,
|
|
152
|
-
variables: variables,
|
|
153
|
-
operationName: operationName,
|
|
154
|
-
raw: raw,
|
|
155
|
-
};
|
|
156
|
-
return params;
|
|
157
|
-
};
|
|
158
|
-
exports.getGraphQLParams = getGraphQLParams;
|
|
159
|
-
const errorMessages = (messages, graphqlErrors) => {
|
|
160
|
-
if (graphqlErrors) {
|
|
161
|
-
return {
|
|
162
|
-
errors: graphqlErrors,
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
return {
|
|
166
|
-
errors: messages.map((message) => {
|
|
167
|
-
return {
|
|
168
|
-
message: message,
|
|
169
|
-
};
|
|
170
|
-
}),
|
|
171
|
-
};
|
|
172
|
-
};
|
|
173
|
-
exports.errorMessages = errorMessages;
|
|
174
|
-
// export const graphiQLResponse = () => {}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function parseBody(req: Request): Promise<Record<string, unknown>>;
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseBody = void 0;
|
|
4
|
-
async function parseBody(req) {
|
|
5
|
-
const contentType = req.headers.get('content-type');
|
|
6
|
-
switch (contentType) {
|
|
7
|
-
case 'application/graphql':
|
|
8
|
-
return { query: await req.text() };
|
|
9
|
-
case 'application/json':
|
|
10
|
-
try {
|
|
11
|
-
return await req.json();
|
|
12
|
-
}
|
|
13
|
-
catch (e) {
|
|
14
|
-
if (e instanceof Error) {
|
|
15
|
-
console.error(`${e.stack || e.message}`);
|
|
16
|
-
}
|
|
17
|
-
throw Error(`POST body sent invalid JSON: ${e}`);
|
|
18
|
-
}
|
|
19
|
-
case 'application/x-www-form-urlencoded':
|
|
20
|
-
return parseFormURL(req);
|
|
21
|
-
}
|
|
22
|
-
return {};
|
|
23
|
-
}
|
|
24
|
-
exports.parseBody = parseBody;
|
|
25
|
-
const parseFormURL = async (req) => {
|
|
26
|
-
const text = await req.text();
|
|
27
|
-
const searchParams = new URLSearchParams(text);
|
|
28
|
-
const res = {};
|
|
29
|
-
searchParams.forEach((v, k) => (res[k] = v));
|
|
30
|
-
return res;
|
|
31
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { mustache } from './mustache';
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
2
|
-
// @ts-ignore
|
|
3
|
-
// For ES module mode
|
|
4
|
-
import manifest from '__STATIC_CONTENT_MANIFEST';
|
|
5
|
-
import { mustache } from './mustache';
|
|
6
|
-
const module = (options = { root: '' }) => {
|
|
7
|
-
return mustache({
|
|
8
|
-
root: options.root,
|
|
9
|
-
manifest: options.manifest ? options.manifest : manifest,
|
|
10
|
-
});
|
|
11
|
-
};
|
|
12
|
-
export { module as mustache };
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import type { Context } from '../../context';
|
|
3
|
-
import type { Next } from '../../hono';
|
|
4
|
-
declare module '../../context' {
|
|
5
|
-
interface Context {
|
|
6
|
-
render: (content: string, params?: object, options?: object) => Response | Promise<Response>;
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
export declare type MustacheOptions = {
|
|
10
|
-
root: string;
|
|
11
|
-
manifest?: object | string;
|
|
12
|
-
namespace?: KVNamespace;
|
|
13
|
-
};
|
|
14
|
-
export declare const mustache: (init?: MustacheOptions) => (c: Context, next: Next) => Promise<void>;
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.mustache = void 0;
|
|
7
|
-
const mustache_1 = __importDefault(require("mustache"));
|
|
8
|
-
const buffer_1 = require("../../utils/buffer");
|
|
9
|
-
const cloudflare_1 = require("../../utils/cloudflare");
|
|
10
|
-
const filepath_1 = require("../../utils/filepath");
|
|
11
|
-
const EXTENSION = '.mustache';
|
|
12
|
-
const DEFAULT_DOCUMENT = 'index.mustache';
|
|
13
|
-
const mustache = (init = { root: '' }) => {
|
|
14
|
-
const { root } = init;
|
|
15
|
-
return async (c, next) => {
|
|
16
|
-
c.render = async (filename, params = {}, options) => {
|
|
17
|
-
const path = (0, filepath_1.getFilePath)({
|
|
18
|
-
filename: `${filename}${EXTENSION}`,
|
|
19
|
-
root: root,
|
|
20
|
-
defaultDocument: DEFAULT_DOCUMENT,
|
|
21
|
-
});
|
|
22
|
-
const kvAssetOptions = {
|
|
23
|
-
manifest: init.manifest,
|
|
24
|
-
namespace: init.namespace ? init.namespace : c.env ? c.env.__STATIC_CONTENT : undefined,
|
|
25
|
-
};
|
|
26
|
-
const buffer = await (0, cloudflare_1.getContentFromKVAsset)(path, kvAssetOptions);
|
|
27
|
-
if (!buffer) {
|
|
28
|
-
throw new Error(`Template "${path}" is not found or blank.`);
|
|
29
|
-
}
|
|
30
|
-
const content = (0, buffer_1.bufferToString)(buffer);
|
|
31
|
-
const partialArgs = {};
|
|
32
|
-
if (options) {
|
|
33
|
-
const partials = options;
|
|
34
|
-
for (const key of Object.keys(partials)) {
|
|
35
|
-
const partialPath = (0, filepath_1.getFilePath)({
|
|
36
|
-
filename: `${partials[key]}${EXTENSION}`,
|
|
37
|
-
root: root,
|
|
38
|
-
defaultDocument: DEFAULT_DOCUMENT,
|
|
39
|
-
});
|
|
40
|
-
const partialBuffer = await (0, cloudflare_1.getContentFromKVAsset)(partialPath, kvAssetOptions);
|
|
41
|
-
if (!partialBuffer) {
|
|
42
|
-
throw new Error(`Partial Template "${partialPath}" is not found or blank.`);
|
|
43
|
-
}
|
|
44
|
-
partialArgs[key] = (0, buffer_1.bufferToString)(partialBuffer);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
const output = mustache_1.default.render(content, params, partialArgs);
|
|
48
|
-
return c.html(output);
|
|
49
|
-
};
|
|
50
|
-
await next();
|
|
51
|
-
};
|
|
52
|
-
};
|
|
53
|
-
exports.mustache = mustache;
|