mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
framework: remove lodash and moment-timezone from deps
This commit is contained in:
3 files changed
+15
-12
No files matched your search
@@ -1,12 +1,17 @@
|
|||||||
import { PassThrough } from 'stream';
|
import { PassThrough } from 'stream';
|
||||||
import type { Next } from 'koa';
|
import type { Next } from 'koa';
|
||||||
import { pick } from 'lodash';
|
|
||||||
import {
|
import {
|
||||||
HydroRequest, HydroResponse, KoaContext, serializer,
|
HydroRequest, HydroResponse, KoaContext, serializer,
|
||||||
} from '@hydrooj/framework';
|
} from '@hydrooj/framework';
|
||||||
import { errorMessage } from '@hydrooj/utils/lib/utils';
|
import { errorMessage } from '@hydrooj/utils/lib/utils';
|
||||||
import { SystemError, UserFacingError } from './error';
|
import { SystemError, UserFacingError } from './error';
|
||||||
|
|
||||||
|
const pick = <T extends object, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> => {
|
||||||
|
const result: Partial<Pick<T, K>> = {};
|
||||||
|
for (const key of keys) result[key] = obj[key];
|
||||||
|
return result as Pick<T, K>;
|
||||||
|
};
|
||||||
|
|
||||||
export default (logger, xff, xhost) => async (ctx: KoaContext, next: Next) => {
|
export default (logger, xff, xhost) => async (ctx: KoaContext, next: Next) => {
|
||||||
// Base Layer
|
// Base Layer
|
||||||
const request: HydroRequest = {
|
const request: HydroRequest = {
|
||||||
@@ -112,7 +117,7 @@ export default (logger, xff, xhost) => async (ctx: KoaContext, next: Next) => {
|
|||||||
ctx.response.status = 302;
|
ctx.response.status = 302;
|
||||||
ctx.redirect(response.redirect);
|
ctx.redirect(response.redirect);
|
||||||
} else if (response.body) {
|
} else if (response.body) {
|
||||||
ctx.body = response.body instanceof Blob ? Buffer.from(await response.body.arrayBuffer()) : response.body;
|
ctx.body = response.body;
|
||||||
ctx.response.status = response.status || 200;
|
ctx.response.status = response.status || 200;
|
||||||
ctx.response.type = response.type
|
ctx.response.type = response.type
|
||||||
|| (request.json
|
|| (request.json
|
||||||
|
|||||||
@@ -14,8 +14,6 @@
|
|||||||
"koa-body": "^6.0.1",
|
"koa-body": "^6.0.1",
|
||||||
"koa-compress": "^5.1.1",
|
"koa-compress": "^5.1.1",
|
||||||
"koa-router": "^13.0.1",
|
"koa-router": "^13.0.1",
|
||||||
"lodash": "^4.17.21",
|
|
||||||
"moment-timezone": "^0.5.48",
|
|
||||||
"path-to-regexp": "^8.2.0",
|
"path-to-regexp": "^8.2.0",
|
||||||
"sanitize-filename": "^1.6.3",
|
"sanitize-filename": "^1.6.3",
|
||||||
"saslprep": "^1.0.3",
|
"saslprep": "^1.0.3",
|
||||||
@@ -28,7 +26,6 @@
|
|||||||
"@types/koa": "^2.15.0",
|
"@types/koa": "^2.15.0",
|
||||||
"@types/koa-compress": "^4.0.6",
|
"@types/koa-compress": "^4.0.6",
|
||||||
"@types/koa-router": "^7.4.8",
|
"@types/koa-router": "^7.4.8",
|
||||||
"@types/lodash": "^4.17.17",
|
|
||||||
"@types/saslprep": "^1.0.3",
|
"@types/saslprep": "^1.0.3",
|
||||||
"@types/ws": "^8.18.1"
|
"@types/ws": "^8.18.1"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import assert from 'assert';
|
import assert from 'assert';
|
||||||
import emojiRegex from 'emoji-regex';
|
import emojiRegex from 'emoji-regex';
|
||||||
import { isSafeInteger } from 'lodash';
|
|
||||||
import moment from 'moment-timezone';
|
|
||||||
import sanitize from 'sanitize-filename';
|
import sanitize from 'sanitize-filename';
|
||||||
import saslprep from 'saslprep';
|
import saslprep from 'saslprep';
|
||||||
import Schema from 'schemastery';
|
import Schema from 'schemastery';
|
||||||
@@ -107,9 +105,9 @@ export const Types = {
|
|||||||
ShortString: basicString(/^.{1,255}$/),
|
ShortString: basicString(/^.{1,255}$/),
|
||||||
String: basicString(),
|
String: basicString(),
|
||||||
|
|
||||||
Int: [(v) => +v, (v) => /^[+-]?[0-9]+$/.test(v.toString().trim()) && isSafeInteger(+v)],
|
Int: [(v) => +v, (v) => /^[+-]?[0-9]+$/.test(v.toString().trim()) && Number.isSafeInteger(+v)],
|
||||||
UnsignedInt: [(v) => +v, (v) => /^(-0|\+?[0-9]+)$/.test(v.toString().trim()) && isSafeInteger(+v)],
|
UnsignedInt: [(v) => +v, (v) => /^(-0|\+?[0-9]+)$/.test(v.toString().trim()) && Number.isSafeInteger(+v)],
|
||||||
PositiveInt: [(v) => +v, (v) => /^\+?[1-9][0-9]*$/.test(v.toString().trim()) && isSafeInteger(+v)],
|
PositiveInt: [(v) => +v, (v) => /^\+?[1-9][0-9]*$/.test(v.toString().trim()) && Number.isSafeInteger(+v)],
|
||||||
Float: [(v) => +v, (v) => Number.isFinite(+v)],
|
Float: [(v) => +v, (v) => Number.isFinite(+v)],
|
||||||
|
|
||||||
ObjectId: [() => { throw new Error('mongodb package not found'); }, () => true],
|
ObjectId: [() => { throw new Error('mongodb package not found'); }, () => true],
|
||||||
@@ -118,13 +116,15 @@ export const Types = {
|
|||||||
(v) => {
|
(v) => {
|
||||||
const d = v.split('-');
|
const d = v.split('-');
|
||||||
assert(d.length === 3);
|
assert(d.length === 3);
|
||||||
|
assert(d[0].length === 4);
|
||||||
return `${d[0]}-${d[1].length === 1 ? '0' : ''}${d[1]}-${d[2].length === 1 ? '0' : ''}${d[2]}`;
|
return `${d[0]}-${d[1].length === 1 ? '0' : ''}${d[1]}-${d[2].length === 1 ? '0' : ''}${d[2]}`;
|
||||||
},
|
},
|
||||||
(v) => {
|
(v) => {
|
||||||
const d = v.toString().split('-');
|
const d = v.toString().split('-');
|
||||||
if (d.length !== 3) return false;
|
if (d.length !== 3) return false;
|
||||||
|
if (d[0].length !== 4) return false;
|
||||||
const st = `${d[0]}-${d[1].length === 1 ? '0' : ''}${d[1]}-${d[2].length === 1 ? '0' : ''}${d[2]}`;
|
const st = `${d[0]}-${d[1].length === 1 ? '0' : ''}${d[1]}-${d[2].length === 1 ? '0' : ''}${d[2]}`;
|
||||||
return moment(st).isValid();
|
return Number.isFinite(new Date(st).getTime());
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
Time: [
|
Time: [
|
||||||
@@ -136,7 +136,8 @@ export const Types = {
|
|||||||
(v) => {
|
(v) => {
|
||||||
const t = v.toString().split(':');
|
const t = v.toString().split(':');
|
||||||
if (t.length !== 2) return false;
|
if (t.length !== 2) return false;
|
||||||
return moment(`2020-01-01 ${(t[0].length === 1 ? '0' : '') + t[0]}:${t[1].length === 1 ? '0' : ''}${t[1]}`).isValid();
|
const d = new Date(`2020-01-01 ${(t[0].length === 1 ? '0' : '') + t[0]}:${t[1].length === 1 ? '0' : ''}${t[1]}`);
|
||||||
|
return Number.isFinite(d.getTime());
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
Range: (range) => [
|
Range: (range) => [
|
||||||
|
|||||||
Reference in new issue
Block a user