Comparing version 1.0.0-beta.2 to 1.0.0-beta.3
@@ -0,1 +1,2 @@ | ||
#!/usr/bin/env node | ||
"use strict"; | ||
@@ -2,0 +3,0 @@ var __importDefault = (this && this.__importDefault) || function (mod) { |
{ | ||
"name": "moondb", | ||
"version": "1.0.0-beta.2", | ||
"version": "1.0.0-beta.3", | ||
"source": "server.ts", | ||
@@ -19,4 +19,5 @@ "main": "server.js", | ||
"dev": "nodemon bin/moondb.ts", | ||
"build": "parcel build", | ||
"build:clean": "rm server.js*", | ||
"build": "npm run build:tsc&&parcel build", | ||
"shebang": "mv bin/moondb.js bin/moondb.source.js&&echo \"#!/usr/bin/env node\" > shebang.tmp&&cat bin/moondb.source.js >> shebang.tmp&&mv shebang.tmp bin/moondb.js&&chmod +x bin/moondb.js&&rm bin/moondb.source.js", | ||
"build:clean": "npm run build:tsc:clean", | ||
"build:tsc": "tsc -p tsconfig.json", | ||
@@ -23,0 +24,0 @@ "build:tsc:clean": "tsc --build --clean", |
300
server.js
#!/usr/bin/env node | ||
var $h5iPv$koa = require("koa"); | ||
var $h5iPv$koabody = require("koa-body"); | ||
var $h5iPv$koacors = require("@koa/cors"); | ||
var $h5iPv$koastatic = require("koa-static"); | ||
var $h5iPv$maiyatangkoarpcserver = require("@maiyatang/koa-rpc-server"); | ||
var $h5iPv$path = require("path"); | ||
var $h5iPv$mysql2promise = require("mysql2/promise"); | ||
var $h5iPv$fsextra = require("fs-extra"); | ||
var $h5iPv$maiyatangospath = require("@maiyatang/os-path"); | ||
var $h5iPv$crypto = require("crypto"); | ||
function $parcel$interopDefault(a) { | ||
return a && a.__esModule ? a.default : a; | ||
} | ||
function $parcel$export(e, n, v, s) { | ||
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true}); | ||
} | ||
$parcel$export(module.exports, "Server", () => $74b3a7b6ef3d29e4$export$d59771a2e9612de9); | ||
var $a59d8c7fb4379f8a$exports = {}; | ||
$parcel$export($a59d8c7fb4379f8a$exports, "read", () => $a59d8c7fb4379f8a$export$aafa59e2e03f2942); | ||
$parcel$export($a59d8c7fb4379f8a$exports, "save", () => $a59d8c7fb4379f8a$export$a1544dad697b423d); | ||
$parcel$export($a59d8c7fb4379f8a$exports, "validate", () => $a59d8c7fb4379f8a$export$a22775fa5e2eebd9); | ||
$parcel$export($a59d8c7fb4379f8a$exports, "connect", () => $a59d8c7fb4379f8a$export$64605811ab45167f); | ||
$parcel$export($a59d8c7fb4379f8a$exports, "query", () => $a59d8c7fb4379f8a$export$2fa187e846a241c4); | ||
class $9341d027d67a4833$export$2e2bcd8739ae039 extends Error { | ||
constructor(code, message){ | ||
super(message); | ||
this.code = code; | ||
this.message = message; | ||
Error.captureStackTrace(this, this.constructor); | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
} | ||
var $94084674b7d625cf$export$2e2bcd8739ae039 = { | ||
CONNECTION_NOT_EXIST: 1000, | ||
QUERY_ERROR: 2000 | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
const $137088bc79f1e7e5$var$APP = 'moondb'; | ||
const $137088bc79f1e7e5$var$SERVER_FILE = 'server.db'; | ||
const $137088bc79f1e7e5$var$SECRET_FILE = '.secret'; | ||
const $137088bc79f1e7e5$var$getSecret = ()=>{ | ||
const secretFile = $137088bc79f1e7e5$var$getPath($137088bc79f1e7e5$var$SECRET_FILE); | ||
let full, secret, iv; | ||
if (($parcel$interopDefault($h5iPv$fsextra)).existsSync(secretFile)) { | ||
full = ($parcel$interopDefault($h5iPv$fsextra)).readFileSync(secretFile); | ||
secret = full.slice(0, 32); | ||
iv = full.slice(32, 48); | ||
return { | ||
secret: secret, | ||
iv: iv | ||
}; | ||
} | ||
secret = ($parcel$interopDefault($h5iPv$crypto)).randomBytes(32); | ||
iv = ($parcel$interopDefault($h5iPv$crypto)).randomBytes(16); | ||
($parcel$interopDefault($h5iPv$fsextra)).writeFileSync(secretFile, Buffer.concat([ | ||
secret, | ||
iv | ||
])); | ||
return { | ||
secret: secret, | ||
iv: iv | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
const $137088bc79f1e7e5$var$encrypt = (data, secret, iv)=>{ | ||
const cipher = ($parcel$interopDefault($h5iPv$crypto)).createCipheriv('aes-256-gcm', secret, iv); | ||
let encrypted = cipher.update(data); | ||
encrypted = Buffer.concat([ | ||
encrypted, | ||
cipher.final() | ||
]); | ||
encrypted = Buffer.concat([ | ||
cipher.getAuthTag(), | ||
encrypted | ||
]); | ||
return encrypted; | ||
}; | ||
const $137088bc79f1e7e5$var$decrypt = (data, secret, iv)=>{ | ||
const authTag = data.slice(0, 16); | ||
const encrypted = data.slice(16, data.length); | ||
// console.log(data.length, authTag.length, encrypted.length); | ||
const decipher = ($parcel$interopDefault($h5iPv$crypto)).createDecipheriv('aes-256-gcm', secret, iv); | ||
decipher.setAuthTag(authTag); | ||
let decrypted = decipher.update(encrypted, undefined, 'utf8'); | ||
decrypted = decrypted + decipher.final('utf8'); | ||
return decrypted; | ||
}; | ||
const $137088bc79f1e7e5$var$getPath = (filename)=>{ | ||
const dir = $h5iPv$maiyatangospath.applicationData($137088bc79f1e7e5$var$APP); | ||
($parcel$interopDefault($h5iPv$fsextra)).ensureDirSync(dir); | ||
return ($parcel$interopDefault($h5iPv$path)).join(dir, filename); | ||
}; | ||
const $137088bc79f1e7e5$var$getServerKey = (server)=>{ | ||
return server.host + ':' + server.port + ':' + server.user; | ||
}; | ||
const $137088bc79f1e7e5$export$5ab543af460d5a4e = async ()=>{ | ||
const serverFile = $137088bc79f1e7e5$var$getPath($137088bc79f1e7e5$var$SERVER_FILE); | ||
if (!($parcel$interopDefault($h5iPv$fsextra)).existsSync(serverFile)) return { | ||
}; | ||
const key = $137088bc79f1e7e5$var$getSecret(); | ||
const encrypted = ($parcel$interopDefault($h5iPv$fsextra)).readFileSync(serverFile); | ||
// console.log(decrypt(encrypted, key.secret, key.iv)); | ||
try { | ||
const allServer = JSON.parse($137088bc79f1e7e5$var$decrypt(encrypted, key.secret, key.iv)); | ||
return allServer || { | ||
}; | ||
} catch (e) { | ||
console.log('[model] readAllServer error:', e); | ||
return { | ||
}; | ||
} | ||
}; | ||
const $137088bc79f1e7e5$export$680eb78123d6b3c = async (server)=>{ | ||
const allServer = await $137088bc79f1e7e5$export$5ab543af460d5a4e(); | ||
return allServer[$137088bc79f1e7e5$var$getServerKey(server)]; | ||
}; | ||
const $137088bc79f1e7e5$export$c7c0e6035f8c6410 = async (server)=>{ | ||
const allServer = await $137088bc79f1e7e5$export$5ab543af460d5a4e(); | ||
allServer[$137088bc79f1e7e5$var$getServerKey(server)] = server; | ||
const key = $137088bc79f1e7e5$var$getSecret(); | ||
const encrypted = $137088bc79f1e7e5$var$encrypt(JSON.stringify(allServer), key.secret, key.iv); | ||
($parcel$interopDefault($h5iPv$fsextra)).writeFileSync($137088bc79f1e7e5$var$getPath($137088bc79f1e7e5$var$SERVER_FILE), encrypted); | ||
}; | ||
const $a59d8c7fb4379f8a$var$connectionMap = new Map(); | ||
// 使用32位随机数作为id | ||
const $a59d8c7fb4379f8a$var$getRandomId = ()=>{ | ||
const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz0123456789'; | ||
const maxPos = chars.length; | ||
let str = ''; | ||
for(let i = 0; i < 32; i++)str += chars.charAt(Math.floor(Math.random() * maxPos)); | ||
return str; | ||
}; | ||
const $a59d8c7fb4379f8a$export$aafa59e2e03f2942 = async (ctx, next)=>{ | ||
return await $137088bc79f1e7e5$export$5ab543af460d5a4e(); | ||
}; | ||
const $a59d8c7fb4379f8a$export$a1544dad697b423d = async (ctx, next, config)=>{ | ||
await $137088bc79f1e7e5$export$c7c0e6035f8c6410(config); | ||
return 'ok'; | ||
}; | ||
const $a59d8c7fb4379f8a$export$a22775fa5e2eebd9 = async (ctx, next)=>{ | ||
return 'ok'; | ||
}; | ||
const $a59d8c7fb4379f8a$export$64605811ab45167f = async (ctx, next, config)=>{ | ||
const connection = await ($parcel$interopDefault($h5iPv$mysql2promise)).createConnection({ | ||
...config, | ||
charset: 'utf8mb4' | ||
}); | ||
const id = $a59d8c7fb4379f8a$var$getRandomId(); | ||
$a59d8c7fb4379f8a$var$connectionMap.set(id, connection); | ||
connection.on('error', (err)=>{ | ||
console.error(err); | ||
$a59d8c7fb4379f8a$var$connectionMap.delete(id); | ||
}); | ||
return id; | ||
}; | ||
const $a59d8c7fb4379f8a$export$2fa187e846a241c4 = async (ctx, next, id, sql, values)=>{ | ||
const connection = $a59d8c7fb4379f8a$var$connectionMap.get(id); | ||
if (!connection) throw new $9341d027d67a4833$export$2e2bcd8739ae039($94084674b7d625cf$export$2e2bcd8739ae039.CONNECTION_NOT_EXIST, 'mysql connection not found'); | ||
return await connection.query(sql, values); | ||
}; | ||
const $57a47769fc61ef68$export$73693bad9f5880b0 = (token)=>{ | ||
return async (ctx, next)=>{ | ||
if (ctx.request.header['x-moondb-token'] !== token) { | ||
ctx.status = 403; | ||
ctx.body = { | ||
code: 403, | ||
msg: 'forbidden' | ||
}; | ||
return; | ||
} | ||
await next(); | ||
}; | ||
}; | ||
const $89ea68917b768cff$export$e3d3ee93624b0deb = (length)=>{ | ||
const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | ||
let result = ''; | ||
for(let i = length; i > 0; --i)result += chars[Math.floor(Math.random() * chars.length)]; | ||
return result; | ||
}; | ||
class $74b3a7b6ef3d29e4$export$d59771a2e9612de9 { | ||
constructor(config){ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Server = void 0; | ||
const koa_1 = __importDefault(require("koa")); | ||
const koa_body_1 = __importDefault(require("koa-body")); | ||
const cors_1 = __importDefault(require("@koa/cors")); | ||
const koa_static_1 = __importDefault(require("koa-static")); | ||
const koa_rpc_server_1 = __importDefault(require("@maiyatang/koa-rpc-server")); | ||
const mysql = __importStar(require("./rpc/mysql")); | ||
const auth_1 = require("./middlewares/auth"); | ||
const path_1 = __importDefault(require("path")); | ||
const randomStr_1 = require("./utils/randomStr"); | ||
; | ||
class Server { | ||
constructor(config) { | ||
Object.defineProperty(this, "app", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
Object.defineProperty(this, "_token", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
Object.defineProperty(this, "_port", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
Object.defineProperty(this, "_mode", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
this._port = config.port || 3002; | ||
this.app = new ($parcel$interopDefault($h5iPv$koa))(); | ||
this.app = new koa_1.default(); | ||
this._mode = config.mode; | ||
// client mode | ||
if (config.mode === 'full' || config.mode === 'client') this.app.use(($parcel$interopDefault($h5iPv$koastatic))(($parcel$interopDefault($h5iPv$path)).join(__dirname, './public'))); | ||
if (config.mode === 'full' || config.mode === 'client') { | ||
this.app.use((0, koa_static_1.default)(path_1.default.join(__dirname, './public'))); | ||
} | ||
// server mode | ||
if (config.mode === 'full' || config.mode === 'server') { | ||
this.app.use(($parcel$interopDefault($h5iPv$koacors))({ | ||
maxAge: 86400 | ||
this.app.use((0, cors_1.default)({ | ||
maxAge: 86400, | ||
})); | ||
this.app.use(($parcel$interopDefault($h5iPv$koabody))({ | ||
json: true | ||
this.app.use((0, koa_body_1.default)({ | ||
json: true, | ||
})); | ||
let token = ''; | ||
if (config.token) token = config.token; | ||
else token = $89ea68917b768cff$export$e3d3ee93624b0deb(64); | ||
if (config.token) { | ||
token = config.token; | ||
} | ||
else { | ||
token = (0, randomStr_1.genRandomString)(64); | ||
} | ||
this._token = token; | ||
this.app.use($57a47769fc61ef68$export$73693bad9f5880b0(token)); | ||
this.app.use(($parcel$interopDefault($h5iPv$maiyatangkoarpcserver))('/rpc', { | ||
mysql: $a59d8c7fb4379f8a$exports | ||
})); | ||
this.app.use((0, auth_1.auth)(token)); | ||
this.app.use((0, koa_rpc_server_1.default)('/rpc', { mysql })); | ||
} | ||
@@ -237,8 +102,9 @@ return this; | ||
} | ||
if (this._mode === 'full' || this._mode === 'client') console.log('[moondb] client running on http://localhost:' + this._port); | ||
if (this._mode === 'full' || this._mode === 'client') { | ||
console.log('[moondb] client running on http://localhost:' + this._port); | ||
} | ||
return this; | ||
} | ||
} | ||
//# sourceMappingURL=server.js.map | ||
exports.Server = Server; | ||
//# sourceMappingURL=server.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
2
236304
901