hono 1.6.4 → 2.0.0
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 +20 -765
- package/dist/compose.js +3 -3
- package/dist/context.d.ts +26 -2
- package/dist/context.js +8 -3
- package/dist/hono.d.ts +1 -1
- package/dist/hono.js +1 -1
- 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 +7 -0
- package/dist/middleware/compress/index.js +19 -0
- package/dist/middleware/jsx/index.js +21 -1
- package/dist/middleware/jwt/index.js +3 -0
- package/dist/middleware/serve-static/bun.d.ts +7 -0
- package/dist/middleware/serve-static/bun.js +38 -0
- package/dist/request.d.ts +9 -0
- package/dist/request.js +19 -0
- 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 +3 -0
- 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
|
@@ -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;
|