next: esbuild loader

This commit is contained in:
undefined committed 2021-07-29 02:59:50 +08:00
1 parent cca54dc92a
commit 5eaec48002
28 files changed
+171 -113

No files matched your search

+3 -3
View File
@@ -1,7 +1,7 @@
import db from 'hydrooj/dist/service/db';
import db from 'hydrooj/src/service/db';
import {
Handler, post, Route, Types,
} from 'hydrooj/dist/service/server';
} from 'hydrooj/src/service/server';
import crypto from 'crypto';
import yaml from 'js-yaml';
@@ -13,7 +13,7 @@ function decrypt(encrypted: string) {
return decrypted;
}
declare module 'hydrooj/dist/interface' {
declare module 'hydrooj/src/interface' {
interface Collections {
dataReport: any;
}
+3 -3
View File
@@ -1,7 +1,7 @@
import yaml from 'js-yaml';
import * as bus from 'hydrooj/dist/service/bus';
import * as system from 'hydrooj/dist/model/system';
import i18n from 'hydrooj/dist/lib/i18n';
import * as bus from 'hydrooj/src/service/bus';
import * as system from 'hydrooj/src/model/system';
import i18n from 'hydrooj/src/lib/i18n';
bus.once('app/started', () => {
const langs = system.get('easy-locale.langs');
+7 -7
View File
@@ -4,13 +4,13 @@ import xml2js from 'xml2js';
import fs from 'fs-extra';
import { filter } from 'lodash';
import decodeHTML from 'decode-html';
import { Route, Handler } from 'hydrooj/dist/service/server';
import { buildContent } from 'hydrooj/dist/lib/content';
import { ProblemAdd } from 'hydrooj/dist/lib/ui';
import solution from 'hydrooj/dist/model/solution';
import problem from 'hydrooj/dist/model/problem';
import { PERM } from 'hydrooj/dist/model/builtin';
import { FileTooLargeError, ValidationError } from 'hydrooj/dist/error';
import { Route, Handler } from 'hydrooj/src/service/server';
import { buildContent } from 'hydrooj/src/lib/content';
import { ProblemAdd } from 'hydrooj/src/lib/ui';
import solution from 'hydrooj/src/model/solution';
import problem from 'hydrooj/src/model/problem';
import { PERM } from 'hydrooj/src/model/builtin';
import { FileTooLargeError, ValidationError } from 'hydrooj/src/error';
import type { ContentNode, ProblemConfigFile } from 'hydrooj';
class FpsProblemImportHandler extends Handler {
+3 -3
View File
@@ -5,8 +5,8 @@ import path from 'path';
import { ObjectID } from 'mongodb';
import fs from 'fs-extra';
import { noop } from 'lodash';
import { Logger } from 'hydrooj/dist/logger';
import * as monitor from 'hydrooj/dist/service/monitor';
import { Logger } from 'hydrooj/src/logger';
import * as monitor from 'hydrooj/src/service/monitor';
import * as sysinfo from './sysinfo';
import * as tmpfs from './tmpfs';
import { FormatError, CompileError, SystemError } from './error';
@@ -15,7 +15,7 @@ import { compilerText, md5 } from './utils';
import readCases from './cases';
import { getConfig } from './config';
declare module 'hydrooj/dist/interface' {
declare module 'hydrooj/src/interface' {
interface SystemKeys {
'hydrojudge.cache_dir': string,
'hydrojudge.tmp_dir': string,
+31 -2
View File
@@ -1,11 +1,40 @@
#!/usr/bin/env node
/* eslint-disable consistent-return */
const os = require('os');
const path = require('path');
const cluster = require('cluster');
const fs = require('fs-extra');
const argv = require('cac')().parse();
const child = require('child_process');
const esbuild = require('esbuild');
let transformTimeUsage = 0;
let displayTimeout;
function transform(type, filename) {
const start = new Date();
const result = esbuild.buildSync({
entryPoints: [filename],
sourcemap: 'inline',
platform: 'node',
format: 'cjs',
target: `node${process.version.split('.')[0].split('v')[1]}`,
jsx: 'transform',
write: false,
});
if (result.warnings.length) console.warn(result.warnings);
transformTimeUsage += new Date().getTime() - start.getTime();
if (displayTimeout) clearTimeout(displayTimeout);
displayTimeout = setTimeout(() => console.log(`Code transform took ${transformTimeUsage}ms`), 1000);
return result.outputFiles[0].text;
}
require.extensions['.ts'] = function loader(module, filename) {
return module._compile(transform('ts', filename), filename);
};
require.extensions['.tsx'] = function loader(module, filename) {
return module._compile(transform('tsx', filename), filename);
};
function buildUrl(opts) {
let mongourl = `${opts.protocol || 'mongodb'}://`;
@@ -16,7 +45,7 @@ function buildUrl(opts) {
}
if (!cluster.isMaster) {
const hydro = require('../dist/loader');
const hydro = require('../src/loader');
// Forked by hydro
hydro.load().catch((e) => {
console.error(e);
@@ -78,7 +107,7 @@ if (!cluster.isMaster) {
fs.writeFileSync(addonPath, JSON.stringify(addons, null, 2));
}
} else {
const hydro = require('../dist/loader');
const hydro = require('../src/loader');
addons = Array.from(new Set(addons));
for (const addon of addons) hydro.addon(addon);
(argv.args[0] === 'cli' ? hydro.loadCli : hydro.load)().catch((e) => {
+2 -2
View File
@@ -2,8 +2,8 @@
"name": "hydrooj",
"version": "2.29.46",
"bin": "bin/hydrooj.js",
"main": "dist/loader.js",
"typings": "dist/loader.d.ts",
"main": "src/loader",
"module": "src/loader",
"repository": "https://github.com/hydro-dev/Hydro.git",
"author": "undefined <i@undefined.moe>",
"license": "AGPL-3.0-or-later",
+10 -5
View File
@@ -46,7 +46,8 @@ function getFiles(folder: string, base = ''): string[] {
export async function handler(pending: string[], fail: string[]) {
for (const i of pending) {
const p = path.resolve(i, 'handler.js');
let p = path.resolve(i, 'handler.ts');
if (!fs.existsSync(p)) p = path.resolve(i, 'handler.js');
if (fs.existsSync(p) && !fail.includes(i)) {
try {
logger.info('Handler init: %s', i);
@@ -161,7 +162,8 @@ export async function uistatic(pending: string[], fail: string[]) {
export async function model(pending: string[], fail: string[]) {
for (const i of pending) {
const p = path.resolve(i, 'model.js');
let p = path.resolve(i, 'model.ts');
if (!fs.existsSync(p)) p = path.resolve(i, 'model.js');
if (fs.existsSync(p) && !fail.includes(i)) {
try {
logger.info('Model init: %s', i);
@@ -178,7 +180,8 @@ export async function model(pending: string[], fail: string[]) {
export async function lib(pending: string[], fail: string[]) {
for (const i of pending) {
const p = path.resolve(i, 'lib.js');
let p = path.resolve(i, 'lib.ts');
if (!fs.existsSync(p)) p = path.resolve(i, 'lib.js');
if (fs.existsSync(p) && !fail.includes(i)) {
try {
logger.info('Lib init: %s', i);
@@ -195,7 +198,8 @@ export async function lib(pending: string[], fail: string[]) {
export async function service(pending: string[], fail: string[]) {
for (const i of pending) {
const p = path.resolve(i, 'service.js');
let p = path.resolve(i, 'service.ts');
if (!fs.existsSync(p)) p = path.resolve(i, 'service.js');
if (fs.existsSync(p) && !fail.includes(i)) {
try {
logger.info('Service init: %s', i);
@@ -216,7 +220,8 @@ export async function service(pending: string[], fail: string[]) {
export async function script(pending: string[], fail: string[], active: string[]) {
for (const i of pending) {
const p = path.resolve(i, 'script.js');
let p = path.resolve(i, 'script.ts');
if (!fs.existsSync(p)) p = path.resolve(i, 'script.js');
if (await fs.pathExists(p) && !fail.includes(i)) {
try {
logger.info('Script init: %s', i);
+37
View File
@@ -0,0 +1,37 @@
const versionNum = +process.version.replace(/v/gim, '').split('.')[0];
if (versionNum < 14) throw new Error('NodeJS >=v14 required');
if (!global.Hydro) {
global.Hydro = {
version: {
node: process.version,
hydrooj: require('hydrooj/package.json').version,
},
stat: { reqCount: 0 },
handler: {},
// @ts-ignore
service: {},
// @ts-ignore
model: {},
script: {},
// @ts-ignore
lib: {},
// @ts-ignore
ui: {
manifest: {},
nodes: {
nav: [],
problem_add: [],
user_dropdown: [],
},
template: {},
},
// @ts-ignore
error: {},
locales: {},
};
global.addons = [];
}
+8 -47
View File
@@ -1,64 +1,25 @@
import 'reflect-metadata';
/* eslint-disable import/first */
/* eslint-disable no-continue */
/* eslint-disable import/no-dynamic-require */
/* eslint-disable no-await-in-loop */
/* eslint-disable no-eval */
const versionNum = +process.version.replace(/v/gim, '').split('.')[0];
if (versionNum < 14) throw new Error('NodeJS >=v14 required');
import cac from 'cac';
const argv = cac().parse();
if (argv.options.debug) {
process.env.NODE_ENV = 'development';
process.env.DEV = 'on';
} else process.env.NODE_ENV = process.env.NODE_ENV || 'production';
if (!global.Hydro) {
global.Hydro = {
version: {
node: process.version,
hydrooj: require('hydrooj/package.json').version,
},
stat: { reqCount: 0 },
handler: {},
// @ts-ignore
service: {},
// @ts-ignore
model: {},
script: {},
// @ts-ignore
lib: {},
// @ts-ignore
ui: {
manifest: {},
nodes: {
nav: [],
problem_add: [],
user_dropdown: [],
},
template: {},
},
// @ts-ignore
error: {},
locales: {},
};
global.addons = [];
}
import 'reflect-metadata';
import './init';
import './interface';
import os from 'os';
import path from 'path';
import cluster from 'cluster';
import fs from 'fs-extra';
import './utils';
import cac from 'cac';
import { Logger } from './logger';
import './ui';
import * as bus from './service/bus';
export * from './interface';
const argv = cac().parse();
if (argv.options.debug) {
process.env.NODE_ENV = 'development';
process.env.DEV = 'on';
} else process.env.NODE_ENV = process.env.NODE_ENV || 'production';
const logger = new Logger('loader');
logger.debug('%o', argv);
+1
View File
@@ -42,6 +42,7 @@ if (cluster.isMaster) {
});
if (process.env.NODE_ENV !== 'test') {
process.stdin.setEncoding('utf-8');
process.stdin.setRawMode(false);
process.stdin.on('data', (buf) => {
const input = buf.toString();
if (input[0] === '@') {
+6 -6
View File
@@ -4,12 +4,12 @@ import os from 'os';
import fs from 'fs-extra';
import AdmZip from 'adm-zip';
import yaml from 'js-yaml';
import { Route, Handler } from 'hydrooj/dist/service/server';
import { BadRequestError, ValidationError } from 'hydrooj/dist/error';
import { ProblemAdd } from 'hydrooj/dist/lib/ui';
import problem from 'hydrooj/dist/model/problem';
import { PERM } from 'hydrooj/dist/model/builtin';
import { buildContent } from 'hydrooj/dist/lib/content';
import { Route, Handler } from 'hydrooj/src/service/server';
import { BadRequestError, ValidationError } from 'hydrooj/src/error';
import { ProblemAdd } from 'hydrooj/src/lib/ui';
import problem from 'hydrooj/src/model/problem';
import { PERM } from 'hydrooj/src/model/builtin';
import { buildContent } from 'hydrooj/src/lib/content';
import type { ContentNode, ProblemConfigFile } from 'hydrooj';
fs.ensureDirSync('/tmp/hydro/import-qduoj');
+2 -2
View File
@@ -1,7 +1,7 @@
import 'hydrooj';
import 'hydrooj/dist/utils';
import 'hydrooj/src/utils';
declare module 'hydrooj' {
declare module 'hydrooj/src/interface' {
interface Lib {
oauth_qq: typeof import('./lib'),
}
+1 -1
View File
@@ -1,5 +1,5 @@
import { } from 'hydrooj';
import * as bus from 'hydrooj/dist/service/bus';
import * as bus from 'hydrooj/src/service/bus';
import WebSocket from 'ws';
declare module 'hydrooj' {
+1 -1
View File
@@ -2,7 +2,7 @@
/* eslint-disable no-await-in-loop */
import mysql from 'mysql';
import 'hydrooj';
import { buildContent } from 'hydrooj/dist/lib/content';
import { buildContent } from 'hydrooj/src/lib/content';
const { user, problem, system } = global.Hydro.model;
+3 -3
View File
@@ -1,7 +1,7 @@
import { UiContextBase } from 'hydrooj/dist/service/server';
import * as system from 'hydrooj/dist/model/system';
import { UiContextBase } from 'hydrooj/src/service/server';
import * as system from 'hydrooj/src/model/system';
declare module 'hydrooj/dist/service/server' {
declare module 'hydrooj/src/service/server' {
interface UiContextBase {
onlyofficeApi: string;
}
+4 -4
View File
@@ -1,8 +1,8 @@
import superagent from 'superagent';
import { ValidationError, ForbiddenError } from 'hydrooj/dist/error';
import { PRIV } from 'hydrooj/dist/model/builtin';
import * as system from 'hydrooj/dist/model/system';
import * as bus from 'hydrooj/dist/service/bus';
import { ValidationError, ForbiddenError } from 'hydrooj/src/error';
import { PRIV } from 'hydrooj/src/model/builtin';
import * as system from 'hydrooj/src/model/system';
import * as bus from 'hydrooj/src/service/bus';
bus.on('handler/before/UserRegister', async (thisArg) => {
if (!system.get('recaptcha.key')) return;
+2 -2
View File
@@ -1,5 +1,5 @@
import * as bus from 'hydrooj/dist/service/bus';
import * as system from 'hydrooj/dist/model/system';
import * as bus from 'hydrooj/src/service/bus';
import * as system from 'hydrooj/src/model/system';
import sonic from './service';
bus.on('problem/add', async (doc, docId) => {
+1 -1
View File
@@ -1,4 +1,4 @@
import { iterateAllProblem, iterateAllProblemInDomain } from 'hydrooj/dist/pipelineUtils';
import { iterateAllProblem, iterateAllProblemInDomain } from 'hydrooj/src/pipelineUtils';
import sonic from './service';
export const description = 'Sonic problem search re-index';
+4 -4
View File
@@ -1,13 +1,13 @@
import { BaseService } from 'hydrooj';
import * as system from 'hydrooj/dist/model/system';
import { Logger } from 'hydrooj/dist/logger';
import * as system from 'hydrooj/src/model/system';
import { Logger } from 'hydrooj/src/logger';
import {
Search, Ingest, QueryOptions, SuggestOptions, PushOptions,
} from 'sonic-channel';
const logger = new Logger('sonic');
declare module 'hydrooj/dist/interface' {
declare module 'hydrooj/src/interface' {
interface SystemKeys {
'sonic.host': string;
'sonic.port': number;
@@ -110,7 +110,7 @@ class SonicService implements BaseService {
const service = new SonicService();
global.Hydro.service.sonic = service;
export = service;
declare module 'hydrooj/dist/interface' {
declare module 'hydrooj/src/interface' {
interface Service {
sonic: typeof service
}
+1 -1
View File
@@ -30,7 +30,7 @@ module.exports = {
webpack: {
config: {
resolve: {
extensions: ['.js', '.jsx'],
extensions: ['.js', '.jsx', '.ts', '.tsx'],
alias: {
vj: path.resolve(__dirname),
},
@@ -1,4 +1,6 @@
const fs = require('fs');
const esbuild = require('esbuild');
const { renderToString } = require('react-dom/server');
const path = require('path');
const yaml = require('js-yaml');
const serialize = require('serialize-javascript');
+2 -2
View File
@@ -5,8 +5,8 @@ const { join } = require('path');
const crypto = require('crypto');
const { tmpdir } = require('os');
const { ObjectID } = require('mongodb');
const bus = require('hydrooj/dist/service/bus');
const { PERM } = require('hydrooj/dist/model/builtin');
const bus = require('hydrooj/src/service/bus');
const { PERM } = require('hydrooj/src/model/builtin');
const markdown = require('./backendlib/markdown');
const {
+1
View File
@@ -113,6 +113,7 @@
"webpackbar": "^5.0.0-3"
},
"dependencies": {
"esbuild": "^0.12.16",
"js-yaml": "^4.1.0",
"jsesc": "^3.0.2",
"katex": "^0.13.13",
+22
View File
@@ -0,0 +1,22 @@
(() => {
const { NamedPage } = window.Hydro;
const { $ } = window.node_modules;
const { request, i18n, tpl } = window.Hydro.utils;
const { ConfirmDialog } = window.Hydro.components;
const page = new NamedPage('course_detail', async () => {
$(document).on('click', '[name="course_buy"]', async (ev) => {
const message = '购买课程';
const action = await new ConfirmDialog({
$body: tpl`
<div class="typo">
<h3>${i18n(message)}</h3>
</div>`,
}).open();
if (action !== 'yes') return;
window.open(`/course/buy/${UiContext.cdoc._id}`, '_blank');
});
});
window.Hydro.extraPages.push(page);
})();
+1 -1
View File
@@ -7,7 +7,7 @@ export interface RemoteAccount {
endpoint?: string;
proxy?: string;
}
declare module 'hydrooj/dist/interface' {
declare module 'hydrooj/src/interface' {
interface Collections {
vjudge: RemoteAccount;
}
+8 -8
View File
@@ -1,12 +1,12 @@
/* eslint-disable no-await-in-loop */
import { Logger } from 'hydrooj/dist/logger';
import db from 'hydrooj/dist/service/db';
import * as bus from 'hydrooj/dist/service/bus';
import * as Judge from 'hydrooj/dist/handler/judge';
import TaskModel from 'hydrooj/dist/model/task';
import DomainModel from 'hydrooj/dist/model/domain';
import ProblemModel from 'hydrooj/dist/model/problem';
import { STATUS } from 'hydrooj/dist/model/builtin';
import { Logger } from 'hydrooj/src/logger';
import db from 'hydrooj/src/service/db';
import * as bus from 'hydrooj/src/service/bus';
import * as Judge from 'hydrooj/src/handler/judge';
import TaskModel from 'hydrooj/src/model/task';
import DomainModel from 'hydrooj/src/model/domain';
import ProblemModel from 'hydrooj/src/model/problem';
import { STATUS } from 'hydrooj/src/model/builtin';
import { sleep } from '@hydrooj/utils/lib/utils';
import providers from './providers/index';
import { BasicProvider, IBasicProvider, RemoteAccount } from './interface';
+3 -3
View File
@@ -4,9 +4,9 @@ import proxy from 'superagent-proxy';
import { JSDOM } from 'jsdom';
import { STATUS } from '@hydrooj/utils/lib/status';
import { sleep } from '@hydrooj/utils/lib/utils';
import { buildContent } from 'hydrooj/dist/lib/content';
import * as setting from 'hydrooj/dist/model/setting';
import { Logger } from 'hydrooj/dist/logger';
import { buildContent } from 'hydrooj/src/lib/content';
import * as setting from 'hydrooj/src/model/setting';
import { Logger } from 'hydrooj/src/logger';
import { PassThrough } from 'stream';
import { IBasicProvider, RemoteAccount } from '../interface';
+2 -2
View File
@@ -5,8 +5,8 @@ import { JSDOM } from 'jsdom';
import { PassThrough } from 'stream';
import { STATUS } from '@hydrooj/utils/lib/status';
import { parseMemoryMB, sleep } from '@hydrooj/utils/lib/utils';
import * as setting from 'hydrooj/dist/model/setting';
import { Logger } from 'hydrooj/dist/logger';
import * as setting from 'hydrooj/src/model/setting';
import { Logger } from 'hydrooj/src/logger';
import { IBasicProvider, RemoteAccount } from '../interface';
proxy(superagent);