This commit is contained in:
2023-09-22 21:13:33 +09:00
parent 295d39327b
commit 438ee85ed2
24 changed files with 539 additions and 40 deletions
+1 -1
View File
@@ -13,7 +13,7 @@
},
"author": "",
"type": "module",
"license": "ISC",
"license": "MIT",
"dependencies": {
"@sammo/crypto": "workspace:^",
"@sammo/secure_token": "workspace:^",
+1 -1
View File
@@ -20,7 +20,7 @@
},
"author": "",
"type": "module",
"license": "ISC",
"license": "MIT",
"dependencies": {
"@sammo/util": "workspace:^"
},
+1 -25
View File
@@ -1,28 +1,4 @@
export class NotYetImplemented extends Error {
public override name = 'NotYetImplemented';
override toString(): string {
if (this.message) {
return this.name + ': ' + this.message;
}
else {
return this.name;
}
}
}
export class InvalidArgument extends Error {
public override name = 'InvalidArgument';
override toString(): string {
if (this.message) {
return this.name + ': ' + this.message;
}
else {
return this.name;
}
}
}
import { InvalidArgument } from '@sammo/util';
export class InvalidVType extends InvalidArgument {
public override name = 'InvalidVType';
+23
View File
@@ -0,0 +1,23 @@
{
"name": "@sammo/gateway",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"build": "tsc --build"
},
"author": "",
"type": "module",
"license": "MIT",
"dependencies": {
"@sammo/api_def": "workspace:^",
"@sammo/server_util": "workspace:^",
"@sammo/util": "workspace:^",
"@strpc/express": "workspace:^",
"dotenv": "^16.3.1",
"mongoose": "^7.4.3"
},
"devDependencies": {
"@types/node": "^20.6.3"
}
}
+25
View File
@@ -0,0 +1,25 @@
import type { StateIncrementer } from '@sammo/server_util';
import SchemaSequence from './schema/SchemaSequence.js';
import { InvalidArgument } from '@sammo/util';
export function MongoSequenceFactory(collectionName: string): StateIncrementer{
return async (increase: number) => {
if(increase <= 0){
throw new InvalidArgument('increase must be > 0');
}
increase = Math.ceil(increase);
const result = await SchemaSequence.findOneAndUpdate({
collectionName,
}, {
$inc: {
nextSeq: increase,
},
}, {
upsert: true,
new: true,
});
return result.nextSeq;
}
}
View File
+22
View File
@@ -0,0 +1,22 @@
import './dotenv.js';
import { connect, Mongoose } from "mongoose";
import { unwrap } from '@sammo/util';
const dbConfig = {
host: unwrap(process.env.GATEWAY_DB_HOST),
port: Number(unwrap(process.env.GATEWAY_DB_PORT)),
user: unwrap(process.env.GATEWAY_DB_USER),
password: unwrap(process.env.GATEWAY_DB_PASSWORD),
database: unwrap(process.env.GATEWAY_DB_DATABASE),
}
const db: Promise<Mongoose> = (async () => {
return await connect(`mongodb://${dbConfig.host}:${dbConfig.port}/${dbConfig.database}`, {
auth:{
username: dbConfig.user,
password: dbConfig.password,
}
});
})();
export default db;
+3
View File
@@ -0,0 +1,3 @@
import { resolve } from "node:path";
export const rootPath = resolve(resolve(), '../..');
+47
View File
@@ -0,0 +1,47 @@
import dotenv from 'dotenv';
import { rootPath } from './constPath.js';
import { unwrap } from '@sammo/util';
let init = false;
const dirPath = rootPath
console.log(rootPath);
function initDotEnv() {
if (process.env['NODE_ENV'] == 'production') {
dotenv.config({ path: `${dirPath}/.env.production.local` });
dotenv.config({ path: `${dirPath}/.env.local` });
dotenv.config({ path: `${dirPath}/.env.production` });
}
else {
dotenv.config({ path: `${dirPath}/.env.development.local` });
dotenv.config({ path: `${dirPath}/.env.local` });
dotenv.config({ path: `${dirPath}/.env.development` });
}
dotenv.config();
init = true;
}
if (!init) {
initDotEnv();
}
let _ownConfig: ReturnType<typeof generateConfig>|undefined = undefined;
function generateConfig() {
return {
port: parseInt(process.env['SERVER_PORT'] ?? "3001"),
sessionSecret: unwrap(process.env['SESSION_SECRET']),
apiRootPath: process.env['API_ROOT_PATH'] ?? '/api/gateway',
} as const;
}
export function ownConfig() {
if(_ownConfig !== undefined){
return _ownConfig;
}
if(!init){
initDotEnv();
}
_ownConfig = generateConfig();
return _ownConfig;
}
+7
View File
@@ -0,0 +1,7 @@
import 'dotenv/config';
import { unwrap } from "@sammo/util";
export const gatewayConfig = {
sessionSecret: unwrap(process.env.GATEWAY_SESSION_SECRET),
port: Number(unwrap(process.env.GATEWAY_PORT)),
};
+34
View File
@@ -0,0 +1,34 @@
import './dotenv.js'
import 'reflect-metadata';
import gatewayDB from './connectDB.js';
import express, { type Request, type Response } from "express"
import session from "express-session";
import { buildAPISystem } from '@strpc/express/generator';
//import { sammoGatewayAPI } from './api/index.js';
import { unwrap } from '@sammo/util';;
import { gatewayConfig } from './gatewayConfig.js';
gatewayDB.then(async (gatewayDB) => {
// create express app
const app = express()
app.use(express.json());
app.use(session({
secret: unwrap(gatewayConfig.sessionSecret),
resave: false,
saveUninitialized: false,
}))
//app.set('etag', false);
//app.use('/gateway_api', buildAPISystem(sammoGatewayAPI));
// start express server
app.listen(gatewayConfig.port)
console.log(`Gateway server has started on port ${gatewayConfig.port}`)
}).catch(error => console.log(error));
export default {};
@@ -0,0 +1,23 @@
import { Schema, model, Types } from 'mongoose';
interface ISchemaSequence {
_id: Types.ObjectId;
collectionName: string;
nextSeq: number;
}
export const SchemaSequence = new Schema<ISchemaSequence>({
collectionName: { type: String, required: true },
nextSeq: {
type: Number, required: true,
get: (v: number) => Math.ceil(v),
set: (v: number) => Math.ceil(v),
default: 0,
},
}, { autoIndex: true, autoCreate: true })
.index({ collectionName: 1 }, { unique: true })
;
export default model<ISchemaSequence>('SchemaSequence', SchemaSequence);
+21
View File
@@ -0,0 +1,21 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
},
"references": [
{
"path": "../../@strpc/express"
},
{
"path": "../util"
},
{
"path": "../crypto"
},
{
"path": "../server_util"
},
]
}
+1 -1
View File
@@ -8,7 +8,7 @@
},
"author": "",
"type": "module",
"license": "ISC",
"license": "MIT",
"dependencies": {
"@sammo/crypto": "workspace:^",
"@sammo/util": "workspace:^"
+23
View File
@@ -0,0 +1,23 @@
{
"name": "@sammo/server_util",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"build": "tsc --build"
},
"type": "module",
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"@sammo/api_def": "workspace:^",
"@sammo/util": "workspace:^"
},
"devDependencies": {
"lodash-es": "^4.17.21"
},
"peerDependencies": {
"lodash-es": "^4.17.21"
}
}
@@ -0,0 +1,198 @@
export interface StateIncrementer {
(increase: number): Promise<number>;
}
export type NumberGroup = {
start: number;
remain: number;
}
const AllocatorGroupBucketSize = 100;
const AllocatorPreserveThreshold = 50;
export function* numberGenerator(numberGroups: NumberGroup[]): Generator<number, null> {
for (const numberGroup of numberGroups) {
for (let i = 0; i < numberGroup.remain; i++) {
yield numberGroup.start + i;
}
}
return null;
}
export class UniqueNumberAllocator {
private preservedNumbers: NumberGroup[];
private incrementer: StateIncrementer;
private totalRemain: number;
private promisedCnt: number;
private promise: Promise<void>;
public constructor(incrementer: StateIncrementer, initialState?: NumberGroup) {
this.incrementer = incrementer;
if (!initialState) {
this.preservedNumbers = [];
this.totalRemain = 0;
}
else {
if (initialState.remain < 1) {
throw new Error('initialState.remain must be greater than 0');
}
this.preservedNumbers = [initialState];
this.totalRemain = initialState.remain;
}
this.promisedCnt = 0;
this.promise = Promise.resolve();
this.preserveNext();
}
public static async generate(incrementer: StateIncrementer): Promise<UniqueNumberAllocator> {
const initialStateEnd = await incrementer(AllocatorGroupBucketSize);
const initialState: NumberGroup = {
start: initialStateEnd - AllocatorGroupBucketSize + 1,
remain: AllocatorGroupBucketSize,
}
return new UniqueNumberAllocator(incrementer, initialState);
}
private preserveNext(): void {
if (this.promisedCnt + this.totalRemain >= AllocatorGroupBucketSize) {
return;
}
if (this.totalRemain >= AllocatorPreserveThreshold) {
return;
}
const waiter = this.promise;
this.promise = (async () => {
this.promisedCnt += AllocatorGroupBucketSize;
const nextP = this.incrementer(AllocatorGroupBucketSize);
await waiter;
const next = await nextP;
if (this.preservedNumbers.length > 0) {
const last = this.preservedNumbers[this.preservedNumbers.length - 1];
if (last.start + last.remain === next - AllocatorGroupBucketSize + 1) {
// 마지막 그룹과 연속됨
last.remain += AllocatorGroupBucketSize;
this.totalRemain += AllocatorGroupBucketSize;
this.promisedCnt -= AllocatorGroupBucketSize;
return;
}
}
const nextGroup: NumberGroup = {
start: next - AllocatorGroupBucketSize + 1,
remain: AllocatorGroupBucketSize,
}
this.preservedNumbers.push(nextGroup);
this.totalRemain += AllocatorGroupBucketSize;
this.promisedCnt -= AllocatorGroupBucketSize;
})();
}
public async allocateOne(): Promise<number> {
if (this.totalRemain > 0) {
const head = this.preservedNumbers[0];
if (head.remain > 1) {
const next = head.start;
head.start++;
head.remain--;
this.totalRemain--;
this.preserveNext();
return next;
}
else {
this.preservedNumbers.shift();
this.totalRemain--;
this.preserveNext();
return head.start;
}
}
const next = (await this.allocate(1)).next().value;
if (next === null) {
throw new Error('incrementer failed to allocate enough numbers');
}
return next;
}
public async allocate(n: number): Promise<Generator<number, null>> {
n = Math.ceil(n);
if (n < 1) {
throw new Error('n must be greater than 0');
}
if (n > this.totalRemain + this.promisedCnt) {
const endP = this.incrementer(n);
this.preserveNext();
const end = await endP;
return numberGenerator([{
start: end - n + 1,
remain: n,
}]);
}
const result: NumberGroup[] = [];
if (this.totalRemain > 0) {
const head = this.preservedNumbers[0];
const headRemain = head.remain;
if (n < headRemain) {
result.push({
start: head.start,
remain: n,
});
head.start += n;
head.remain -= n;
this.totalRemain -= n;
this.preserveNext();
return numberGenerator(result);
}
if (n === headRemain) {
this.preservedNumbers.shift();
result.push(head);
this.totalRemain -= n;
this.preserveNext();
return numberGenerator(result);
}
}
//assert n <= this.totalRemain + this.promisedRemain
if (n > this.totalRemain) {
await this.promise;
if (n > this.totalRemain) {
throw new Error('incrementer failed to allocate enough numbers');
}
}
this.totalRemain -= n;
this.preserveNext();
let remain = n;
let idx = 0;
while (remain > 0) {
const head = this.preservedNumbers[idx];
if (head.remain <= remain) {
remain -= head.remain;
result.push(head);
idx++;
continue;
}
result.push({
start: head.start,
remain: remain,
});
head.start += remain;
head.remain -= remain;
remain = 0;
break;
}
this.preservedNumbers = this.preservedNumbers.slice(idx);
return numberGenerator(result);
}
}
+1
View File
@@ -0,0 +1 @@
export { UniqueNumberAllocator, type StateIncrementer } from './UniqueNumberAllocator.js';
+33
View File
@@ -0,0 +1,33 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "node16",
"moduleResolution": "node16",
"resolveJsonModule": true,
"declaration": true,
"declarationMap": true,
"importHelpers": true,
"isolatedModules": true,
"verbatimModuleSyntax": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"skipLibCheck": true,
"incremental": true,
"rootDir": "./src",
"outDir": "./dist",
"composite": true
},
"references": [
{
"path": "../util"
},
{
"path": "../crypto"
},
]
}
+1 -1
View File
@@ -9,7 +9,7 @@
"type": "module",
"keywords": [],
"author": "",
"license": "ISC",
"license": "MIT",
"devDependencies": {
"lodash-es": "^4.17.21"
},
+26 -1
View File
@@ -15,4 +15,29 @@ export class RuntimeError extends Error {
return this.name;
}
}
}
}
export class NotYetImplemented extends Error {
public override name = 'NotYetImplemented';
override toString(): string {
if (this.message) {
return this.name + ': ' + this.message;
}
else {
return this.name;
}
}
}
export class InvalidArgument extends Error {
public override name = 'InvalidArgument';
override toString(): string {
if (this.message) {
return this.name + ': ' + this.message;
}
else {
return this.name;
}
}
}