next-ssr-middleware 1.1.1 → 1.1.3
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 +1 -1
- package/dist/API.d.ts +1 -1
- package/dist/API.js +22 -54
- package/dist/API.js.map +1 -1
- package/dist/SSR/compose.js +5 -22
- package/dist/SSR/compose.js.map +1 -1
- package/dist/SSR/index.js +3 -6
- package/dist/SSR/index.js.map +1 -1
- package/dist/SSR/middleware/index.js +2 -5
- package/dist/SSR/middleware/index.js.map +1 -1
- package/dist/SSR/middleware/local.js +65 -114
- package/dist/SSR/middleware/local.js.map +1 -1
- package/dist/SSR/middleware/remote.d.ts +2 -0
- package/dist/SSR/middleware/remote.js +67 -105
- package/dist/SSR/middleware/remote.js.map +1 -1
- package/dist/SSR/shim.js +42 -84
- package/dist/SSR/shim.js.map +1 -1
- package/dist/index.js +2 -5
- package/dist/index.js.map +1 -1
- package/package.json +9 -14
- package/source/SSR/middleware/local.ts +8 -5
- package/source/SSR/middleware/remote.ts +16 -4
- package/tsconfig.json +1 -1
package/ReadMe.md
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
| :-----------: | :----------: | :-------: | :---------: | :------------: | :---: |
|
|
14
14
|
| `>=1` | ✅developing | `>=15` | ❌ | ❌ | `3.x` |
|
|
15
15
|
| `>=0.10 <1` | ❌deprecated | `>=15` | `>=6.11` | `>=0.5` | `2.x` |
|
|
16
|
-
| `>=0.9 <0.10` |
|
|
16
|
+
| `>=0.9 <0.10` | ❌deprecated | `>=15` | `>=6.11` | `>=0.5` | ❌ |
|
|
17
17
|
| `>=0.7 <0.9` | ❌deprecated | `>=9 <15` | `>=6.11` | `>=0.5` | ❌ |
|
|
18
18
|
| `<0.7` | ❌deprecated | `>=9 <15` | `>=4 <6.11` | `<0.5` | ❌ |
|
|
19
19
|
|
package/dist/API.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export type KoaOption = ConstructorParameters<typeof Koa>[0];
|
|
|
4
4
|
export type KoaCallback = ReturnType<(typeof Koa)['prototype']['callback']>;
|
|
5
5
|
export declare function withKoa<S, C>(...middlewares: Middleware<S, C>[]): KoaCallback;
|
|
6
6
|
export declare function withKoa<S, C>(option: KoaOption, ...middlewares: Middleware<S, C>[]): KoaCallback;
|
|
7
|
-
export declare const pageApiRouteOf: (path: string) => string;
|
|
7
|
+
export declare const pageApiRouteOf: (path: string) => string | undefined;
|
|
8
8
|
export declare const createKoaRouter: <S, C extends RouterContext<S>>(moduleURI: string, option?: RouterOptions) => Router<S, C>;
|
|
9
9
|
export declare function withKoaRouter<S, C extends RouterContext<S>>(router: RouterInstance<S, C>, ...middlewares: Middleware<S, C>[]): KoaCallback;
|
|
10
10
|
export declare function withKoaRouter<S, C extends RouterContext<S>>(option: KoaOption, router: RouterInstance<S, C>, ...middlewares: Middleware<S, C>[]): KoaCallback;
|
package/dist/API.js
CHANGED
|
@@ -1,65 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var bodyparser_1 = tslib_1.__importDefault(require("@koa/bodyparser"));
|
|
8
|
-
var router_1 = require("@koa/router");
|
|
9
|
-
var koa_1 = tslib_1.__importDefault(require("koa"));
|
|
10
|
-
function withKoa() {
|
|
11
|
-
var _a, e_1, _b;
|
|
12
|
-
var parameters = [];
|
|
13
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
14
|
-
parameters[_i] = arguments[_i];
|
|
15
|
-
}
|
|
16
|
-
var option;
|
|
17
|
-
var middlewares = [];
|
|
1
|
+
import BodyParser from '@koa/bodyparser';
|
|
2
|
+
import { Router } from '@koa/router';
|
|
3
|
+
import Koa from 'koa';
|
|
4
|
+
export function withKoa(...parameters) {
|
|
5
|
+
let option;
|
|
6
|
+
let middlewares = [];
|
|
18
7
|
if (typeof parameters[0] === 'function') {
|
|
19
8
|
option = undefined;
|
|
20
9
|
middlewares = parameters;
|
|
21
10
|
}
|
|
22
11
|
else
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
var middleware = middlewares_1_1.value;
|
|
28
|
-
app.use(middleware);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
32
|
-
finally {
|
|
33
|
-
try {
|
|
34
|
-
if (middlewares_1_1 && !middlewares_1_1.done && (_b = middlewares_1.return)) _b.call(middlewares_1);
|
|
35
|
-
}
|
|
36
|
-
finally { if (e_1) throw e_1.error; }
|
|
37
|
-
}
|
|
12
|
+
[option, ...middlewares] = parameters;
|
|
13
|
+
const app = new Koa(option).use(BodyParser());
|
|
14
|
+
for (const middleware of middlewares)
|
|
15
|
+
app.use(middleware);
|
|
38
16
|
return app.callback();
|
|
39
17
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
var _a, _b;
|
|
50
|
-
var parameters = [];
|
|
51
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
52
|
-
parameters[_i] = arguments[_i];
|
|
53
|
-
}
|
|
54
|
-
var option;
|
|
55
|
-
var router;
|
|
56
|
-
var middlewares = [];
|
|
18
|
+
export const pageApiRouteOf = (path) => decodeURI(path)
|
|
19
|
+
.match(/pages(\/api\/.+)\.(j|t)s/)?.[1]
|
|
20
|
+
?.split('/[')[0]
|
|
21
|
+
?.replace(/\/index$/, '');
|
|
22
|
+
export const createKoaRouter = (moduleURI, option) => new Router({ ...option, prefix: pageApiRouteOf(moduleURI) });
|
|
23
|
+
export function withKoaRouter(...parameters) {
|
|
24
|
+
let option;
|
|
25
|
+
let router;
|
|
26
|
+
let middlewares = [];
|
|
57
27
|
if (!parameters[1] || typeof parameters[1] === 'function')
|
|
58
|
-
|
|
28
|
+
[router, ...middlewares] = parameters;
|
|
59
29
|
else
|
|
60
|
-
|
|
61
|
-
return withKoa.
|
|
62
|
-
router.routes(),
|
|
63
|
-
router.allowedMethods()], tslib_1.__read(middlewares), false));
|
|
30
|
+
[option, router, ...middlewares] = parameters;
|
|
31
|
+
return withKoa(option, router.routes(), router.allowedMethods(), ...middlewares);
|
|
64
32
|
}
|
|
65
33
|
//# sourceMappingURL=API.js.map
|
package/dist/API.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"API.js","sourceRoot":"","sources":["../source/API.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"API.js","sourceRoot":"","sources":["../source/API.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,iBAAiB,CAAC;AACzC,OAAO,EACH,MAAM,EAIT,MAAM,aAAa,CAAC;AACrB,OAAO,GAAmB,MAAM,KAAK,CAAC;AAWtC,MAAM,UAAU,OAAO,CAAO,GAAG,UAAiB;IAC9C,IAAI,MAA6B,CAAC;IAClC,IAAI,WAAW,GAAuB,EAAE,CAAC;IAEzC,IAAI,OAAO,UAAU,CAAC,CAAC,CAAC,KAAK,UAAU,EAAE,CAAC;QACtC,MAAM,GAAG,SAAS,CAAC;QACnB,WAAW,GAAG,UAAU,CAAC;IAC7B,CAAC;;QAAM,CAAC,MAAM,EAAE,GAAG,WAAW,CAAC,GAAG,UAAU,CAAC;IAE7C,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,UAAU,EAAE,CAAC,CAAC;IAE9C,KAAK,MAAM,UAAU,IAAI,WAAW;QAAE,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAE1D,OAAO,GAAG,CAAC,QAAQ,EAAE,CAAC;AAC1B,CAAC;AAED,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,IAAY,EAAE,EAAE,CAC3C,SAAS,CAAC,IAAI,CAAC;KACV,KAAK,CAAC,0BAA0B,CAAC,EAAE,CAAC,CAAC,CAAC;IACvC,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAChB,EAAE,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC;AAElC,MAAM,CAAC,MAAM,eAAe,GAAG,CAC3B,SAAiB,EACjB,MAAsB,EACxB,EAAE,CAAC,IAAI,MAAM,CAAO,EAAE,GAAG,MAAM,EAAE,MAAM,EAAE,cAAc,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;AAWxE,MAAM,UAAU,aAAa,CACzB,GAAG,UAAiB;IAEpB,IAAI,MAA6B,CAAC;IAClC,IAAI,MAA4B,CAAC;IACjC,IAAI,WAAW,GAAuB,EAAE,CAAC;IAEzC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,OAAO,UAAU,CAAC,CAAC,CAAC,KAAK,UAAU;QACrD,CAAC,MAAM,EAAE,GAAG,WAAW,CAAC,GAAG,UAAU,CAAC;;QACrC,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,WAAW,CAAC,GAAG,UAAU,CAAC;IAEnD,OAAO,OAAO,CACV,MAAM,EACN,MAAM,CAAC,MAAM,EAAE,EACf,MAAM,CAAC,cAAc,EAAE,EACvB,GAAG,WAAW,CACjB,CAAC;AACN,CAAC"}
|
package/dist/SSR/compose.js
CHANGED
|
@@ -1,25 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var _this = this;
|
|
7
|
-
var middlewares = [];
|
|
8
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
9
|
-
middlewares[_i] = arguments[_i];
|
|
10
|
-
}
|
|
11
|
-
return (function (context) {
|
|
12
|
-
var _a = tslib_1.__read(middlewares), first = _a[0], rest = _a.slice(1);
|
|
13
|
-
var next = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
14
|
-
var _a;
|
|
15
|
-
return tslib_1.__generator(this, function (_b) {
|
|
16
|
-
switch (_b.label) {
|
|
17
|
-
case 0: return [4 /*yield*/, ((_a = rest.shift()) === null || _a === void 0 ? void 0 : _a(context, next))];
|
|
18
|
-
case 1: return [2 /*return*/, (_b.sent()) ||
|
|
19
|
-
{ props: {} }];
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
}); };
|
|
1
|
+
export function compose(...middlewares) {
|
|
2
|
+
return (context => {
|
|
3
|
+
const [first, ...rest] = middlewares;
|
|
4
|
+
const next = async () => (await rest.shift()?.(context, next)) ||
|
|
5
|
+
{ props: {} };
|
|
23
6
|
return first(context, next);
|
|
24
7
|
});
|
|
25
8
|
}
|
package/dist/SSR/compose.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compose.js","sourceRoot":"","sources":["../../source/SSR/compose.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"compose.js","sourceRoot":"","sources":["../../source/SSR/compose.ts"],"names":[],"mappings":"AAaA,MAAM,UAAU,OAAO,CAIrB,GAAG,WAA+B;IAChC,OAAO,CAAC,OAAO,CAAC,EAAE;QACd,MAAM,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,GAAG,WAAW,CAAC;QAErC,MAAM,IAAI,GAAG,KAAK,IAAI,EAAE,CACpB,CAAC,MAAM,IAAI,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;YACpC,EAAE,KAAK,EAAE,EAAE,EAAkC,CAAC;QAEnD,OAAO,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IAChC,CAAC,CAAM,CAAC;AACZ,CAAC"}
|
package/dist/SSR/index.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
tslib_1.__exportStar(require("./compose"), exports);
|
|
5
|
-
tslib_1.__exportStar(require("./middleware"), exports);
|
|
6
|
-
tslib_1.__exportStar(require("./shim"), exports);
|
|
1
|
+
export * from './compose';
|
|
2
|
+
export * from './middleware';
|
|
3
|
+
export * from './shim';
|
|
7
4
|
//# sourceMappingURL=index.js.map
|
package/dist/SSR/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/SSR/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/SSR/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,cAAc,QAAQ,CAAC"}
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var tslib_1 = require("tslib");
|
|
4
|
-
tslib_1.__exportStar(require("./local"), exports);
|
|
5
|
-
tslib_1.__exportStar(require("./remote"), exports);
|
|
1
|
+
export * from './local';
|
|
2
|
+
export * from './remote';
|
|
6
3
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../source/SSR/middleware/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../source/SSR/middleware/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC"}
|
|
@@ -1,122 +1,73 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
_a.trys.push([0, 2, , 3]);
|
|
18
|
-
return [4 /*yield*/, next()];
|
|
19
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
20
|
-
case 2:
|
|
21
|
-
error_1 = _a.sent();
|
|
22
|
-
console.error(error_1);
|
|
23
|
-
if (error_1 instanceof koajax_1.HTTPError) {
|
|
24
|
-
status_1 = error_1.response.status;
|
|
25
|
-
if (status_1 === 404)
|
|
26
|
-
return [2 /*return*/, { notFound: true, props: {} }];
|
|
27
|
-
}
|
|
28
|
-
throw error_1;
|
|
29
|
-
case 3: return [2 /*return*/];
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
function router(context, next) {
|
|
35
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
36
|
-
var options, resolvedUrl, params, query, locales;
|
|
37
|
-
return tslib_1.__generator(this, function (_a) {
|
|
38
|
-
switch (_a.label) {
|
|
39
|
-
case 0: return [4 /*yield*/, next()];
|
|
40
|
-
case 1:
|
|
41
|
-
options = (_a.sent()) || {}, resolvedUrl = context.resolvedUrl, params = context.params, query = context.query, locales = context.locales;
|
|
42
|
-
return [2 /*return*/, tslib_1.__assign(tslib_1.__assign({}, options), { props: tslib_1.__assign(tslib_1.__assign({}, ('props' in options ? options.props : {})), { route: JSON.parse(JSON.stringify({ resolvedUrl: resolvedUrl, params: params, query: query, locales: locales })) }) })];
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
});
|
|
1
|
+
import { verify } from 'jsonwebtoken';
|
|
2
|
+
import { HTTPError } from 'koajax';
|
|
3
|
+
import { Day, Second } from 'web-utility';
|
|
4
|
+
export async function errorLogger(context, next) {
|
|
5
|
+
try {
|
|
6
|
+
return await next();
|
|
7
|
+
}
|
|
8
|
+
catch (error) {
|
|
9
|
+
console.error(error);
|
|
10
|
+
if (error instanceof HTTPError) {
|
|
11
|
+
const { status } = error.response;
|
|
12
|
+
if (status === 404)
|
|
13
|
+
return { notFound: true, props: {} };
|
|
14
|
+
}
|
|
15
|
+
throw error;
|
|
16
|
+
}
|
|
46
17
|
}
|
|
47
|
-
function
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
switch (_d.label) {
|
|
57
|
-
case 0:
|
|
58
|
-
token = cookies[tokenKey];
|
|
59
|
-
try {
|
|
60
|
-
jwtPayload = (0, jsonwebtoken_1.verify)(token, process.env[secretKey], options);
|
|
61
|
-
}
|
|
62
|
-
catch (error) {
|
|
63
|
-
console.error(url, error);
|
|
64
|
-
}
|
|
65
|
-
return [4 /*yield*/, next()];
|
|
66
|
-
case 1:
|
|
67
|
-
data = _d.sent();
|
|
68
|
-
if ('props' in data && jwtPayload)
|
|
69
|
-
data.props['jwtPayload'] = jwtPayload;
|
|
70
|
-
return [2 /*return*/, data];
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
}); };
|
|
18
|
+
export async function router(context, next) {
|
|
19
|
+
const options = (await next()) || {}, { resolvedUrl, params, query, locales } = context;
|
|
20
|
+
return {
|
|
21
|
+
...options,
|
|
22
|
+
props: {
|
|
23
|
+
...('props' in options ? options.props : {}),
|
|
24
|
+
route: JSON.parse(JSON.stringify({ resolvedUrl, params, query, locales }))
|
|
25
|
+
}
|
|
26
|
+
};
|
|
74
27
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (allInterval === void 0) { allInterval = web_utility_1.Day; }
|
|
80
|
-
function cleanCache() {
|
|
81
|
-
var e_1, _a;
|
|
28
|
+
export function jwtVerifier(tokenKey = 'token', secretKey = 'JWT_SECRET', options = {}) {
|
|
29
|
+
return async ({ req: { url, cookies } }, next) => {
|
|
30
|
+
const token = cookies[tokenKey] || '', secret = process.env[secretKey] || '';
|
|
31
|
+
let jwtPayload;
|
|
82
32
|
try {
|
|
83
|
-
|
|
84
|
-
var _d = tslib_1.__read(_c.value, 2), URI = _d[0], expiredAt = _d[1].expiredAt;
|
|
85
|
-
if (Date.now() - expiredAt > allInterval)
|
|
86
|
-
delete serverRenderCache[URI];
|
|
87
|
-
}
|
|
33
|
+
jwtPayload = verify(token, secret, options);
|
|
88
34
|
}
|
|
89
|
-
catch (
|
|
90
|
-
|
|
91
|
-
try {
|
|
92
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
93
|
-
}
|
|
94
|
-
finally { if (e_1) throw e_1.error; }
|
|
35
|
+
catch (error) {
|
|
36
|
+
console.error(url, error);
|
|
95
37
|
}
|
|
38
|
+
const data = await next();
|
|
39
|
+
if ('props' in data && jwtPayload)
|
|
40
|
+
data.props['jwtPayload'] = jwtPayload;
|
|
41
|
+
return data;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
const serverRenderCache = {};
|
|
45
|
+
export function cache(oneInterval = 30 * Second, allInterval = Day) {
|
|
46
|
+
function cleanCache() {
|
|
47
|
+
for (const [URI, { expiredAt = 0 }] of Object.entries(serverRenderCache))
|
|
48
|
+
if (Date.now() - expiredAt > allInterval)
|
|
49
|
+
delete serverRenderCache[URI];
|
|
96
50
|
}
|
|
97
|
-
return (
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
cache.
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
return [2 /*return*/, data || cache.buffer];
|
|
119
|
-
});
|
|
120
|
-
}); });
|
|
51
|
+
return (async (context, next) => {
|
|
52
|
+
const { resolvedUrl, req } = context;
|
|
53
|
+
const cache = (serverRenderCache[resolvedUrl] ||= {}), noCache = req.headers['cache-control'] === 'no-cache', title = `[SSR cache] ${resolvedUrl}`;
|
|
54
|
+
const { data, expiredAt = 0 } = cache;
|
|
55
|
+
if ((!data || expiredAt < Date.now() || noCache) && !cache.buffer) {
|
|
56
|
+
console.time(title);
|
|
57
|
+
cache.buffer = next()
|
|
58
|
+
.then(data => {
|
|
59
|
+
cache.data = data;
|
|
60
|
+
cache.expiredAt = Date.now() + oneInterval;
|
|
61
|
+
console.log(cache);
|
|
62
|
+
return data;
|
|
63
|
+
})
|
|
64
|
+
.finally(() => {
|
|
65
|
+
delete cache.buffer;
|
|
66
|
+
cleanCache();
|
|
67
|
+
console.timeEnd(title);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return data || cache.buffer;
|
|
71
|
+
});
|
|
121
72
|
}
|
|
122
73
|
//# sourceMappingURL=local.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"local.js","sourceRoot":"","sources":["../../../source/SSR/middleware/local.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"local.js","sourceRoot":"","sources":["../../../source/SSR/middleware/local.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6B,MAAM,EAAE,MAAM,cAAc,CAAC;AACjE,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AAGnC,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAI1C,MAAM,CAAC,KAAK,UAAU,WAAW,CAI7B,OAAqC,EACrC,IAAgD;IAEhD,IAAI,CAAC;QACD,OAAO,MAAM,IAAI,EAAE,CAAC;IACxB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAErB,IAAI,KAAK,YAAY,SAAS,EAAE,CAAC;YAC7B,MAAM,EAAE,MAAM,EAAE,GAAG,KAAK,CAAC,QAAQ,CAAC;YAElC,IAAI,MAAM,KAAK,GAAG;gBAAE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,KAAK,EAAE,EAAO,EAAE,CAAC;QAClE,CAAC;QACD,MAAM,KAAK,CAAC;IAChB,CAAC;AACL,CAAC;AASD,MAAM,CAAC,KAAK,UAAU,MAAM,CACxB,OAAqC,EACrC,IAAgD;IAEhD,MAAM,OAAO,GAAG,CAAC,MAAM,IAAI,EAAE,CAAC,IAAK,EAAmC,EAClE,EAAE,WAAW,EAAE,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AO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;YAC3B,CAAC,CAAC,CAAC;QACX,CAAC;QACD,OAAO,IAAI,IAAI,KAAK,CAAC,MAAM,CAAC;IAChC,CAAC,CAAqB,CAAC;AAC3B,CAAC"}
|
|
@@ -14,6 +14,8 @@ export interface OAuth2Props<T extends DataObject> {
|
|
|
14
14
|
token: string;
|
|
15
15
|
user: T;
|
|
16
16
|
}
|
|
17
|
+
export declare const RemoteDomain: string | undefined;
|
|
18
|
+
export declare const CurrentHost: string;
|
|
17
19
|
export declare function oauth2Signer<I extends DataObject, O extends DataObject = {}>({ signInURL, accessToken, userProfile, tokenKey }: OAuth2Option): Middleware<I, O>;
|
|
18
20
|
export type GitHubAPIOperation = 'admin' | 'write' | 'read' | 'delete';
|
|
19
21
|
export type GitHubOAuthScope = `repo${'' | ':status' | '_deployment' | ':invite'}` | 'public_repo' | 'security_events' | `${Exclude<GitHubAPIOperation, 'delete'>}:${'repo_hook' | 'org' | 'public_key' | 'gpg_key'}` | 'admin:org_hook' | 'gist' | 'notifications' | `user${'' | ':email' | ':follow'}` | `read:${'user' | 'project'}` | 'project' | 'delete_repo' | `${Exclude<GitHubAPIOperation, 'admin'>}:packages` | 'codespace' | 'workflow';
|
|
@@ -1,114 +1,76 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return tslib_1.__generator(this, function (_d) {
|
|
16
|
-
switch (_d.label) {
|
|
17
|
-
case 0:
|
|
18
|
-
token = cookies[tokenKey] || query[tokenKey];
|
|
19
|
-
pageURL = new URL(url || '/', headers['origin'] || Host);
|
|
20
|
-
if (!query.code) return [3 /*break*/, 2];
|
|
21
|
-
return [4 /*yield*/, accessToken(query)];
|
|
22
|
-
case 1:
|
|
23
|
-
token_1 = _d.sent(), searchParams = pageURL.searchParams;
|
|
24
|
-
try {
|
|
25
|
-
res.setHeader('Set-Cookie', "".concat(tokenKey, "=").concat(token_1, "; Path=/"));
|
|
26
|
-
}
|
|
27
|
-
catch (_e) {
|
|
28
|
-
searchParams.set(tokenKey, token_1);
|
|
29
|
-
}
|
|
30
|
-
searchParams.delete('code');
|
|
31
|
-
if (searchParams.get('state') === '')
|
|
32
|
-
searchParams.delete('state');
|
|
33
|
-
return [2 /*return*/, {
|
|
34
|
-
redirect: { destination: pageURL + '', permanent: false },
|
|
35
|
-
props: {}
|
|
36
|
-
}];
|
|
37
|
-
case 2:
|
|
38
|
-
if (!token) return [3 /*break*/, 5];
|
|
39
|
-
return [4 /*yield*/, (userProfile === null || userProfile === void 0 ? void 0 : userProfile(token + ''))];
|
|
40
|
-
case 3:
|
|
41
|
-
user = _d.sent();
|
|
42
|
-
return [4 /*yield*/, next()];
|
|
43
|
-
case 4:
|
|
44
|
-
data = _d.sent();
|
|
45
|
-
props = 'props' in data ? tslib_1.__assign(tslib_1.__assign({}, data.props), { token: token, user: user }) : {};
|
|
46
|
-
return [2 /*return*/, tslib_1.__assign(tslib_1.__assign({}, data), { props: props })];
|
|
47
|
-
case 5: return [2 /*return*/, {
|
|
48
|
-
redirect: {
|
|
49
|
-
destination: signInURL(pageURL + ''),
|
|
50
|
-
permanent: false
|
|
51
|
-
},
|
|
52
|
-
props: {}
|
|
53
|
-
}];
|
|
1
|
+
import { buildURLData } from 'web-utility';
|
|
2
|
+
const { NODE_ENV, VERCEL_ENV = NODE_ENV, VERCEL_URL, VERCEL_PROJECT_PRODUCTION_URL } = process.env;
|
|
3
|
+
export const RemoteDomain = VERCEL_ENV === 'production' ? VERCEL_PROJECT_PRODUCTION_URL : VERCEL_URL;
|
|
4
|
+
export const CurrentHost = RemoteDomain
|
|
5
|
+
? `https://${RemoteDomain}`
|
|
6
|
+
: 'http://localhost:3000';
|
|
7
|
+
export function oauth2Signer({ signInURL, accessToken, userProfile, tokenKey = 'token' }) {
|
|
8
|
+
return async ({ req: { url, headers, cookies }, query, res }, next) => {
|
|
9
|
+
const token = cookies[tokenKey] || query[tokenKey];
|
|
10
|
+
const pageURL = new URL(url || '/', headers['origin'] || CurrentHost);
|
|
11
|
+
if (query.code) {
|
|
12
|
+
const token = await accessToken(query), { searchParams } = pageURL;
|
|
13
|
+
try {
|
|
14
|
+
res.setHeader('Set-Cookie', `${tokenKey}=${token}; Path=/`);
|
|
54
15
|
}
|
|
55
|
-
|
|
56
|
-
|
|
16
|
+
catch {
|
|
17
|
+
searchParams.set(tokenKey, token);
|
|
18
|
+
}
|
|
19
|
+
searchParams.delete('code');
|
|
20
|
+
if (searchParams.get('state') === '')
|
|
21
|
+
searchParams.delete('state');
|
|
22
|
+
return {
|
|
23
|
+
redirect: { destination: pageURL + '', permanent: false },
|
|
24
|
+
props: {}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
if (token) {
|
|
28
|
+
const user = await userProfile?.(token + ''), data = await next();
|
|
29
|
+
const props = 'props' in data ? { ...data.props, token, user } : {};
|
|
30
|
+
return { ...data, props };
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
redirect: {
|
|
34
|
+
destination: signInURL(pageURL + ''),
|
|
35
|
+
permanent: false
|
|
36
|
+
},
|
|
37
|
+
props: {}
|
|
38
|
+
};
|
|
39
|
+
};
|
|
57
40
|
}
|
|
58
|
-
function githubOAuth2(
|
|
59
|
-
var _this = this;
|
|
60
|
-
var _b = _a.rootBaseURL, rootBaseURL = _b === void 0 ? 'https://github.com' : _b, _c = _a.apiBaseURL, apiBaseURL = _c === void 0 ? 'https://api.github.com' : _c, client_id = _a.client_id, client_secret = _a.client_secret, scopes = _a.scopes, _d = _a.allow_signup, allow_signup = _d === void 0 ? true : _d;
|
|
41
|
+
export function githubOAuth2({ rootBaseURL = 'https://github.com', apiBaseURL = 'https://api.github.com', client_id, client_secret, scopes, allow_signup = true }) {
|
|
61
42
|
return oauth2Signer({
|
|
62
|
-
signInURL:
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
method: 'POST',
|
|
77
|
-
headers: {
|
|
78
|
-
Accept: 'application/json',
|
|
79
|
-
'Content-Type': 'application/json'
|
|
80
|
-
},
|
|
81
|
-
body: JSON.stringify({ client_id: client_id, client_secret: client_secret, code: code })
|
|
82
|
-
})];
|
|
83
|
-
case 1:
|
|
84
|
-
response = _d.sent();
|
|
85
|
-
return [4 /*yield*/, response.json()];
|
|
86
|
-
case 2:
|
|
87
|
-
_c = _d.sent(), error = _c.error, access_token = _c.access_token;
|
|
88
|
-
if (error)
|
|
89
|
-
throw new URIError(error);
|
|
90
|
-
return [2 /*return*/, access_token];
|
|
91
|
-
}
|
|
43
|
+
signInURL: redirect_uri => `https://github.com/login/oauth/authorize?${buildURLData({
|
|
44
|
+
client_id,
|
|
45
|
+
scope: scopes?.join(' '),
|
|
46
|
+
allow_signup,
|
|
47
|
+
redirect_uri
|
|
48
|
+
})}`,
|
|
49
|
+
accessToken: async ({ code }) => {
|
|
50
|
+
const response = await fetch(new URL('login/oauth/access_token', rootBaseURL), {
|
|
51
|
+
method: 'POST',
|
|
52
|
+
headers: {
|
|
53
|
+
Accept: 'application/json',
|
|
54
|
+
'Content-Type': 'application/json'
|
|
55
|
+
},
|
|
56
|
+
body: JSON.stringify({ client_id, client_secret, code })
|
|
92
57
|
});
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
})];
|
|
104
|
-
case 1:
|
|
105
|
-
response = _a.sent();
|
|
106
|
-
if (response.status < 300)
|
|
107
|
-
return [2 /*return*/, response.json()];
|
|
108
|
-
throw new URIError('Invalid GitHub access token');
|
|
58
|
+
const { error, access_token } = await response.json();
|
|
59
|
+
if (error)
|
|
60
|
+
throw new URIError(error);
|
|
61
|
+
return access_token;
|
|
62
|
+
},
|
|
63
|
+
userProfile: async (token) => {
|
|
64
|
+
const response = await fetch(new URL('user', apiBaseURL), {
|
|
65
|
+
headers: {
|
|
66
|
+
Authorization: `Bearer ${token}`,
|
|
67
|
+
Accept: 'application/json'
|
|
109
68
|
}
|
|
110
69
|
});
|
|
111
|
-
|
|
70
|
+
if (response.status < 300)
|
|
71
|
+
return response.json();
|
|
72
|
+
throw new URIError('Invalid GitHub access token');
|
|
73
|
+
}
|
|
112
74
|
});
|
|
113
75
|
}
|
|
114
76
|
//# sourceMappingURL=remote.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remote.js","sourceRoot":"","sources":["../../../source/SSR/middleware/remote.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"remote.js","sourceRoot":"","sources":["../../../source/SSR/middleware/remote.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAsB3C,MAAM,EACF,QAAQ,EACR,UAAU,GAAG,QAAQ,EACrB,UAAU,EACV,6BAA6B,EAChC,GAAG,OAAO,CAAC,GAAG,CAAC;AAEhB,MAAM,CAAC,MAAM,YAAY,GACrB,UAAU,KAAK,YAAY,CAAC,CAAC,CAAC,6BAA6B,CAAC,CAAC,CAAC,UAAU,CAAC;AAE7E,MAAM,CAAC,MAAM,WAAW,GAAG,YAAY;IACnC,CAAC,CAAC,WAAW,YAAY,EAAE;IAC3B,CAAC,CAAC,uBAAuB,CAAC;AAE9B,MAAM,UAAU,YAAY,CAAkD,EAC1E,SAAS,EACT,WAAW,EACX,WAAW,EACX,QAAQ,GAAG,OAAO,EACP;IACX,OAAO,KAAK,EAAE,EAAE,GAAG,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,KAA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AC,EAAE;gBACtD,OAAO,EAAE;oBACL,aAAa,EAAE,UAAU,KAAK,EAAE;oBAChC,MAAM,EAAE,kBAAkB;iBAC7B;aACJ,CAAC,CAAC;YACH,IAAI,QAAQ,CAAC,MAAM,GAAG,GAAG;gBAAE,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC;YAElD,MAAM,IAAI,QAAQ,CAAC,6BAA6B,CAAC,CAAC;QACtD,CAAC;KACJ,CAAC,CAAC;AACP,CAAC"}
|
package/dist/SSR/shim.js
CHANGED
|
@@ -1,95 +1,53 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.withMiddleware = void 0;
|
|
4
|
-
exports.patchHeaders = patchHeaders;
|
|
5
|
-
var tslib_1 = require("tslib");
|
|
6
1
|
/**
|
|
7
2
|
* @see {@link https://www.propelauth.com/post/getting-url-in-next-server-components}
|
|
8
3
|
*/
|
|
9
|
-
function patchHeaders(request) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
var locales = ((_a = domainLocale === null || domainLocale === void 0 ? void 0 : domainLocale.locales) === null || _a === void 0 ? void 0 : _a.join(',')) || locale;
|
|
4
|
+
export function patchHeaders(request) {
|
|
5
|
+
const resolvedUrl = request.nextUrl + '', { locale, defaultLocale = '', domainLocale } = request.nextUrl, headers = new Headers(request.headers);
|
|
6
|
+
const locales = domainLocale?.locales?.join(',') || locale;
|
|
13
7
|
headers.set('x-resolved-url', resolvedUrl);
|
|
14
8
|
headers.set('x-locale', locale);
|
|
15
9
|
headers.set('x-locales', locales);
|
|
16
10
|
headers.set('x-default-locale', defaultLocale);
|
|
17
11
|
return headers;
|
|
18
12
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
preview: false,
|
|
59
|
-
previewData: {},
|
|
60
|
-
draftMode: isEnabled,
|
|
61
|
-
resolvedUrl: url,
|
|
62
|
-
locale: locale,
|
|
63
|
-
locales: locales,
|
|
64
|
-
defaultLocale: defaultLocale
|
|
65
|
-
};
|
|
66
|
-
return [4 /*yield*/, getServerSideProps(context)];
|
|
67
|
-
case 7:
|
|
68
|
-
result = _f.sent();
|
|
69
|
-
if (!('redirect' in result)) return [3 /*break*/, 9];
|
|
70
|
-
return [4 /*yield*/, Promise.resolve().then(function () { return tslib_1.__importStar(require('next/navigation')); })];
|
|
71
|
-
case 8:
|
|
72
|
-
_d = _f.sent(), redirect = _d.redirect, permanentRedirect = _d.permanentRedirect;
|
|
73
|
-
isPermanent = 'statusCode' in result.redirect
|
|
74
|
-
? result.redirect.statusCode === 308
|
|
75
|
-
: 'permanent' in result.redirect &&
|
|
76
|
-
result.redirect.permanent;
|
|
77
|
-
destination = result.redirect.destination;
|
|
78
|
-
if (isPermanent)
|
|
79
|
-
permanentRedirect(destination);
|
|
80
|
-
else
|
|
81
|
-
redirect(destination);
|
|
82
|
-
_f.label = 9;
|
|
83
|
-
case 9:
|
|
84
|
-
if (!('props' in result)) return [3 /*break*/, 11];
|
|
85
|
-
return [4 /*yield*/, result.props];
|
|
86
|
-
case 10:
|
|
87
|
-
props = _f.sent();
|
|
88
|
-
return [2 /*return*/, serverComponent(tslib_1.__assign({ params: params, searchParams: searchParams }, props))];
|
|
89
|
-
case 11: return [2 /*return*/];
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
}); };
|
|
13
|
+
export const withMiddleware = (getServerSideProps, serverComponent) => async ({ params, searchParams }) => {
|
|
14
|
+
const { headers, cookies, draftMode } = await import('next/headers');
|
|
15
|
+
const resolvedParams = await params, resolvedSearchParams = await searchParams, header = await headers(), cookie = await cookies(), { isEnabled } = await draftMode();
|
|
16
|
+
const url = header.get('x-resolved-url') || '', locale = header.get('x-locale') || '', locales = header.get('x-locales')?.split(',') || [], defaultLocale = header.get('x-default-locale') || '';
|
|
17
|
+
const req = {
|
|
18
|
+
url,
|
|
19
|
+
headers: Object.fromEntries([...header]),
|
|
20
|
+
cookies: Object.fromEntries(cookie.getAll().map(({ name, value }) => [name, value]))
|
|
21
|
+
};
|
|
22
|
+
const context = {
|
|
23
|
+
req,
|
|
24
|
+
res: {},
|
|
25
|
+
params: resolvedParams,
|
|
26
|
+
query: resolvedSearchParams,
|
|
27
|
+
preview: false,
|
|
28
|
+
previewData: {},
|
|
29
|
+
draftMode: isEnabled,
|
|
30
|
+
resolvedUrl: url,
|
|
31
|
+
locale,
|
|
32
|
+
locales,
|
|
33
|
+
defaultLocale
|
|
34
|
+
};
|
|
35
|
+
const result = await getServerSideProps(context);
|
|
36
|
+
if ('redirect' in result) {
|
|
37
|
+
const { redirect, permanentRedirect } = await import('next/navigation');
|
|
38
|
+
const isPermanent = 'statusCode' in result.redirect
|
|
39
|
+
? result.redirect.statusCode === 308
|
|
40
|
+
: 'permanent' in result.redirect &&
|
|
41
|
+
result.redirect.permanent;
|
|
42
|
+
const { destination } = result.redirect;
|
|
43
|
+
if (isPermanent)
|
|
44
|
+
permanentRedirect(destination);
|
|
45
|
+
else
|
|
46
|
+
redirect(destination);
|
|
47
|
+
}
|
|
48
|
+
if ('props' in result) {
|
|
49
|
+
const props = await result.props;
|
|
50
|
+
return serverComponent({ params, searchParams, ...props });
|
|
51
|
+
}
|
|
93
52
|
};
|
|
94
|
-
exports.withMiddleware = withMiddleware;
|
|
95
53
|
//# sourceMappingURL=shim.js.map
|
package/dist/SSR/shim.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shim.js","sourceRoot":"","sources":["../../source/SSR/shim.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"shim.js","sourceRoot":"","sources":["../../source/SSR/shim.ts"],"names":[],"mappings":"AAWA;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,OAAoB;IAC7C,MAAM,WAAW,GAAG,OAAO,CAAC,OAAO,GAAG,EAAE,EACpC,EAAE,MAAM,EAAE,aAAa,GAAG,EAAE,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC,OAAO,EAC9D,OAAO,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAC3C,MAAM,OAAO,GAAG,YAAY,EAAE,OAAO,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,MAAM,CAAC;IAE3D,OAAO,CAAC,GAAG,CAAC,gBAAgB,EAAE,WAAW,CAAC,CAAC;IAC3C,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IAChC,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;IAClC,OAAO,CAAC,GAAG,CAAC,kBAAkB,EAAE,aAAa,CAAC,CAAC;IAE/C,OAAO,OAAO,CAAC;AACnB,CAAC;AAeD,MAAM,CAAC,MAAM,cAAc,GACvB,CACI,kBAA4C,EAC5C,eAAsC,EACpB,EAAE,CACxB,KAAK,EAAE,EAAE,MAAM,EAAE,YAAY,EAAE,EAAE,EAAE;IAC/B,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,GAAG,MAAM,MAAM,CAAC,cAAc,CAAC,CAAC;IAErE,MAAM,cAAc,GAAG,MAAM,MAAM,EAC/B,oBAAoB,GAAG,MAAM,YAAY,EACzC,MAAM,GAAG,MAAM,OAAO,EAAE,EACxB,MAAM,GAAG,MAAM,O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C,UAAU,KAAK,GAAG;YACpC,CAAC,CAAC,WAAW,IAAI,MAAM,CAAC,QAAQ;gBAC9B,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC;QACpC,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,CAAC,QAAQ,CAAC;QAExC,IAAI,WAAW;YAAE,iBAAiB,CAAC,WAAW,CAAC,CAAC;;YAC3C,QAAQ,CAAC,WAAW,CAAC,CAAC;IAC/B,CAAC;IACD,IAAI,OAAO,IAAI,MAAM,EAAE,CAAC;QACpB,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,KAAK,CAAC;QAEjC,OAAO,eAAe,CAAC,EAAE,MAAM,EAAE,YAAY,EAAE,GAAG,KAAK,EAAE,CAAC,CAAC;IAC/D,CAAC;AACL,CAAC,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var tslib_1 = require("tslib");
|
|
4
|
-
tslib_1.__exportStar(require("./API"), exports);
|
|
5
|
-
tslib_1.__exportStar(require("./SSR"), exports);
|
|
1
|
+
export * from './API';
|
|
2
|
+
export * from './SSR';
|
|
6
3
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../source/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../source/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,OAAO,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "next-ssr-middleware",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"license": "LGPL-3.0-or-later",
|
|
5
5
|
"author": "shiy2008@gmail.com",
|
|
6
6
|
"description": "Koa-like middlewares for Next.js Back-end API & Server Side Rendering",
|
|
@@ -26,31 +26,26 @@
|
|
|
26
26
|
"@koa/router": "^15.7.0",
|
|
27
27
|
"@types/jsonwebtoken": "^9.0.10",
|
|
28
28
|
"@types/koa": "^3.0.3",
|
|
29
|
-
"@types/react": "^19.
|
|
29
|
+
"@types/react": "^19.3.0",
|
|
30
30
|
"jsonwebtoken": "^9.0.3",
|
|
31
31
|
"koa": "^3.2.1",
|
|
32
32
|
"tslib": "^2.8.1",
|
|
33
33
|
"web-utility": "^4.7.2"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"next": ">=15.5.
|
|
36
|
+
"next": ">=15.5.24",
|
|
37
37
|
"react": ">=18"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@types/node": "^24.13.
|
|
40
|
+
"@types/node": "^24.13.5",
|
|
41
41
|
"husky": "^9.1.7",
|
|
42
|
-
"koajax": "^3.3.
|
|
43
|
-
"lint-staged": "^17.
|
|
44
|
-
"next": "^16.
|
|
42
|
+
"koajax": "^3.3.1",
|
|
43
|
+
"lint-staged": "^17.5.1",
|
|
44
|
+
"next": "^16.3.5",
|
|
45
45
|
"open-cli": "^9.0.0",
|
|
46
|
-
"prettier": "^3.9.
|
|
46
|
+
"prettier": "^3.9.7",
|
|
47
47
|
"typedoc": "^0.28.20",
|
|
48
|
-
"typescript": "~
|
|
49
|
-
},
|
|
50
|
-
"pnpm": {
|
|
51
|
-
"onlyBuiltDependencies": [
|
|
52
|
-
"sharp"
|
|
53
|
-
]
|
|
48
|
+
"typescript": "~6.0.3"
|
|
54
49
|
},
|
|
55
50
|
"prettier": {
|
|
56
51
|
"singleQuote": true,
|
|
@@ -62,17 +62,18 @@ export function jwtVerifier<I extends DataObject, O extends DataObject = {}>(
|
|
|
62
62
|
options: VerifyOptions = {}
|
|
63
63
|
): Middleware<I, O & JWTProps> {
|
|
64
64
|
return async ({ req: { url, cookies } }, next) => {
|
|
65
|
-
const token = cookies[tokenKey]
|
|
66
|
-
|
|
65
|
+
const token = cookies[tokenKey] || '',
|
|
66
|
+
secret = process.env[secretKey] || '';
|
|
67
|
+
let jwtPayload: (JwtPayload & I) | undefined;
|
|
67
68
|
try {
|
|
68
|
-
|
|
69
|
+
jwtPayload = verify(token, secret, options) as JwtPayload & I;
|
|
69
70
|
} catch (error) {
|
|
70
71
|
console.error(url, error);
|
|
71
72
|
}
|
|
72
73
|
const data = await next();
|
|
73
74
|
|
|
74
75
|
if ('props' in data && jwtPayload)
|
|
75
|
-
data.props['jwtPayload'] = jwtPayload;
|
|
76
|
+
(data.props as O & JWTProps<I>)['jwtPayload'] = jwtPayload;
|
|
76
77
|
|
|
77
78
|
return data;
|
|
78
79
|
};
|
|
@@ -91,7 +92,9 @@ export function cache<I extends DataObject, O extends DataObject = {}>(
|
|
|
91
92
|
allInterval = Day
|
|
92
93
|
) {
|
|
93
94
|
function cleanCache() {
|
|
94
|
-
for (const [URI, { expiredAt }] of Object.entries(
|
|
95
|
+
for (const [URI, { expiredAt = 0 }] of Object.entries(
|
|
96
|
+
serverRenderCache
|
|
97
|
+
))
|
|
95
98
|
if (Date.now() - expiredAt > allInterval)
|
|
96
99
|
delete serverRenderCache[URI];
|
|
97
100
|
}
|
|
@@ -20,8 +20,19 @@ export interface OAuth2Props<T extends DataObject> {
|
|
|
20
20
|
user: T;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
const
|
|
24
|
-
|
|
23
|
+
const {
|
|
24
|
+
NODE_ENV,
|
|
25
|
+
VERCEL_ENV = NODE_ENV,
|
|
26
|
+
VERCEL_URL,
|
|
27
|
+
VERCEL_PROJECT_PRODUCTION_URL
|
|
28
|
+
} = process.env;
|
|
29
|
+
|
|
30
|
+
export const RemoteDomain =
|
|
31
|
+
VERCEL_ENV === 'production' ? VERCEL_PROJECT_PRODUCTION_URL : VERCEL_URL;
|
|
32
|
+
|
|
33
|
+
export const CurrentHost = RemoteDomain
|
|
34
|
+
? `https://${RemoteDomain}`
|
|
35
|
+
: 'http://localhost:3000';
|
|
25
36
|
|
|
26
37
|
export function oauth2Signer<I extends DataObject, O extends DataObject = {}>({
|
|
27
38
|
signInURL,
|
|
@@ -31,7 +42,7 @@ export function oauth2Signer<I extends DataObject, O extends DataObject = {}>({
|
|
|
31
42
|
}: OAuth2Option): Middleware<I, O> {
|
|
32
43
|
return async ({ req: { url, headers, cookies }, query, res }, next) => {
|
|
33
44
|
const token = cookies[tokenKey] || query[tokenKey];
|
|
34
|
-
const pageURL = new URL(url || '/', headers['origin'] ||
|
|
45
|
+
const pageURL = new URL(url || '/', headers['origin'] || CurrentHost);
|
|
35
46
|
|
|
36
47
|
if (query.code) {
|
|
37
48
|
const token = await accessToken(query as OAuth2Ticket),
|
|
@@ -89,7 +100,8 @@ export type GitHubOAuthScope =
|
|
|
89
100
|
* @see {@link https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/authorizing-oauth-apps#web-application-flow}
|
|
90
101
|
*/
|
|
91
102
|
export interface GitHubOAuthOption
|
|
92
|
-
extends
|
|
103
|
+
extends
|
|
104
|
+
Partial<Record<'rootBaseURL' | 'apiBaseURL' | 'login', string>>,
|
|
93
105
|
Record<'client_id' | 'client_secret', string> {
|
|
94
106
|
scopes?: GitHubOAuthScope[];
|
|
95
107
|
allow_signup?: boolean;
|
package/tsconfig.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"esModuleInterop": true,
|
|
4
|
-
"downlevelIteration": true,
|
|
5
4
|
"importHelpers": true,
|
|
6
5
|
"lib": ["ESNext", "DOM", "DOM.Iterable"],
|
|
7
6
|
"skipLibCheck": true,
|
|
8
7
|
"declaration": true,
|
|
9
8
|
"sourceMap": true,
|
|
9
|
+
"rootDir": "source",
|
|
10
10
|
"outDir": "dist"
|
|
11
11
|
},
|
|
12
12
|
"include": ["source/*.ts"],
|