feat: add in memory storage for exported scenes
This commit is contained in:
parent
de6a0efd3a
commit
ad14da6af7
19
package-lock.json
generated
19
package-lock.json
generated
@ -23,6 +23,7 @@
|
||||
"@types/jest": "^27.0.1",
|
||||
"@types/node": "^16.0.0",
|
||||
"@types/supertest": "^2.0.11",
|
||||
"@types/type-is": "^1.6.3",
|
||||
"@typescript-eslint/eslint-plugin": "^4.28.2",
|
||||
"@typescript-eslint/parser": "^4.28.2",
|
||||
"eslint": "^7.30.0",
|
||||
@ -2150,6 +2151,15 @@
|
||||
"@types/superagent": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/type-is": {
|
||||
"version": "1.6.3",
|
||||
"resolved": "https://registry.npmjs.org/@types/type-is/-/type-is-1.6.3.tgz",
|
||||
"integrity": "sha512-PNs5wHaNcBgCQG5nAeeZ7OvosrEsI9O4W2jAOO9BCCg4ux9ZZvH2+0iSCOIDBiKuQsiNS8CBlmfX9f5YBQ22cA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/yargs": {
|
||||
"version": "16.0.4",
|
||||
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-16.0.4.tgz",
|
||||
@ -10950,6 +10960,15 @@
|
||||
"@types/superagent": "*"
|
||||
}
|
||||
},
|
||||
"@types/type-is": {
|
||||
"version": "1.6.3",
|
||||
"resolved": "https://registry.npmjs.org/@types/type-is/-/type-is-1.6.3.tgz",
|
||||
"integrity": "sha512-PNs5wHaNcBgCQG5nAeeZ7OvosrEsI9O4W2jAOO9BCCg4ux9ZZvH2+0iSCOIDBiKuQsiNS8CBlmfX9f5YBQ22cA==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"@types/yargs": {
|
||||
"version": "16.0.4",
|
||||
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-16.0.4.tgz",
|
||||
|
@ -4,7 +4,7 @@
|
||||
"description": "",
|
||||
"author": "",
|
||||
"private": true,
|
||||
"license": "UNLICENSED",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"prebuild": "rimraf dist",
|
||||
"build": "nest build",
|
||||
@ -36,6 +36,7 @@
|
||||
"@types/jest": "^27.0.1",
|
||||
"@types/node": "^16.0.0",
|
||||
"@types/supertest": "^2.0.11",
|
||||
"@types/type-is": "^1.6.3",
|
||||
"@typescript-eslint/eslint-plugin": "^4.28.2",
|
||||
"@typescript-eslint/parser": "^4.28.2",
|
||||
"eslint": "^7.30.0",
|
||||
|
@ -1,22 +0,0 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
|
||||
describe('AppController', () => {
|
||||
let appController: AppController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const app: TestingModule = await Test.createTestingModule({
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
}).compile();
|
||||
|
||||
appController = app.get<AppController>(AppController);
|
||||
});
|
||||
|
||||
describe('root', () => {
|
||||
it('should return "Hello World!"', () => {
|
||||
expect(appController.getHello()).toBe('Hello World!');
|
||||
});
|
||||
});
|
||||
});
|
@ -1,12 +0,0 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { AppService } from './app.service';
|
||||
|
||||
@Controller()
|
||||
export class AppController {
|
||||
constructor(private readonly appService: AppService) {}
|
||||
|
||||
@Get()
|
||||
getHello(): string {
|
||||
return this.appService.getHello();
|
||||
}
|
||||
}
|
@ -1,10 +1,15 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { MiddlewareConsumer, Module } from '@nestjs/common';
|
||||
import { RawParserMiddleware } from './raw-parser.middleware';
|
||||
import { ScenesController } from './scenes/scenes.controller';
|
||||
import { MemoryService } from './storages/memory.service';
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
controllers: [ScenesController],
|
||||
providers: [MemoryService],
|
||||
})
|
||||
export class AppModule {}
|
||||
export class AppModule {
|
||||
configure(consumer: MiddlewareConsumer) {
|
||||
consumer.apply(RawParserMiddleware).forRoutes('**');
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
getHello(): string {
|
||||
return 'Hello World!';
|
||||
}
|
||||
}
|
@ -2,7 +2,11 @@ import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
await app.listen(3000);
|
||||
const app = await NestFactory.create(AppModule, {
|
||||
cors: true,
|
||||
});
|
||||
app.setGlobalPrefix('api/v2');
|
||||
|
||||
await app.listen(8080);
|
||||
}
|
||||
bootstrap();
|
||||
|
7
src/raw-parser.middleware.spec.ts
Normal file
7
src/raw-parser.middleware.spec.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { RawParserMiddleware } from './raw-parser.middleware';
|
||||
|
||||
describe('RawParserMiddleware', () => {
|
||||
it('should be defined', () => {
|
||||
expect(new RawParserMiddleware()).toBeDefined();
|
||||
});
|
||||
});
|
14
src/raw-parser.middleware.ts
Normal file
14
src/raw-parser.middleware.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { Injectable, NestMiddleware } from '@nestjs/common';
|
||||
import { raw } from 'express';
|
||||
import { hasBody } from 'type-is';
|
||||
|
||||
// Excalidraw Front end doesn't send a Content Type Header
|
||||
// so we tell raw parser to check if there is a body
|
||||
const rawParserMiddleware = raw({ type: hasBody });
|
||||
|
||||
@Injectable()
|
||||
export class RawParserMiddleware implements NestMiddleware {
|
||||
use(req: any, res: any, next: () => void) {
|
||||
rawParserMiddleware(req, res, next);
|
||||
}
|
||||
}
|
18
src/scenes/scenes.controller.spec.ts
Normal file
18
src/scenes/scenes.controller.spec.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { ScenesController } from './scenes.controller';
|
||||
|
||||
describe('ScenesController', () => {
|
||||
let controller: ScenesController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [ScenesController],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<ScenesController>(ScenesController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
42
src/scenes/scenes.controller.ts
Normal file
42
src/scenes/scenes.controller.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Header,
|
||||
Param,
|
||||
Post,
|
||||
Res,
|
||||
} from '@nestjs/common';
|
||||
import { Response } from 'express';
|
||||
import { MemoryService } from 'src/storages/memory.service';
|
||||
import { hash, hexadecimalToDecimal } from 'src/utils';
|
||||
import { Readable } from 'stream';
|
||||
|
||||
@Controller()
|
||||
export class ScenesController {
|
||||
constructor(private storageService: MemoryService) {}
|
||||
@Get(':id')
|
||||
@Header('content-type', 'application/octet-stream')
|
||||
async findOne(@Param() params, @Res() res: Response): Promise<void> {
|
||||
const data = await this.storageService.load(params.id);
|
||||
|
||||
const stream = new Readable();
|
||||
stream.push(data);
|
||||
stream.push(null);
|
||||
stream.pipe(res);
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Body() payload: Buffer) {
|
||||
|
||||
const drawingHash = hash(payload);
|
||||
const id = hexadecimalToDecimal(drawingHash);
|
||||
|
||||
await this.storageService.save(id, payload);
|
||||
|
||||
return {
|
||||
id,
|
||||
data: `http://localhost:8080/api/v2/${id}`,
|
||||
};
|
||||
}
|
||||
}
|
18
src/storages/memory.service.spec.ts
Normal file
18
src/storages/memory.service.spec.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { MemoryService } from './memory.service';
|
||||
|
||||
describe('MemoryService', () => {
|
||||
let service: MemoryService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [MemoryService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<MemoryService>(MemoryService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
17
src/storages/memory.service.ts
Normal file
17
src/storages/memory.service.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { StorageService } from './storageService';
|
||||
|
||||
@Injectable()
|
||||
export class MemoryService implements StorageService {
|
||||
|
||||
scenesMap = new Map<string, Buffer>();
|
||||
|
||||
async save(id: string, data: Buffer): Promise<boolean> {
|
||||
this.scenesMap.set(id, data);
|
||||
return true;
|
||||
}
|
||||
|
||||
async load(id: string): Promise<false | Buffer> {
|
||||
return this.scenesMap.get(id);
|
||||
}
|
||||
}
|
5
src/storages/storageService.ts
Normal file
5
src/storages/storageService.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export interface StorageService {
|
||||
save(id: string, data: Buffer): Promise<boolean>;
|
||||
|
||||
load(id: string): Promise<Buffer | false>;
|
||||
}
|
12
src/utils.ts
Normal file
12
src/utils.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { createHash } from 'crypto';
|
||||
|
||||
export function hash(buffer): string {
|
||||
return createHash(`sha256`).update(buffer).digest(`hex`);
|
||||
}
|
||||
|
||||
// Copied from https://github.com/NMinhNguyen/excalidraw-json
|
||||
export function hexadecimalToDecimal(hexadecimal: string) {
|
||||
// See https://stackoverflow.com/a/53751162
|
||||
const bigInt = BigInt(`0x${hexadecimal}`);
|
||||
return bigInt.toString(10);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user