refact: use nanoid instead of hash
This commit is contained in:
parent
57a173e1b9
commit
bc3757aa09
17
package-lock.json
generated
17
package-lock.json
generated
@ -17,6 +17,7 @@
|
|||||||
"@nestjs/platform-express": "^8.0.0",
|
"@nestjs/platform-express": "^8.0.0",
|
||||||
"@types/keyv": "^3.1.3",
|
"@types/keyv": "^3.1.3",
|
||||||
"keyv": "^4.0.3",
|
"keyv": "^4.0.3",
|
||||||
|
"nanoid": "^3.1.25",
|
||||||
"reflect-metadata": "^0.1.13",
|
"reflect-metadata": "^0.1.13",
|
||||||
"rimraf": "^3.0.2",
|
"rimraf": "^3.0.2",
|
||||||
"rxjs": "^7.2.0"
|
"rxjs": "^7.2.0"
|
||||||
@ -7101,6 +7102,17 @@
|
|||||||
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-2.5.0.tgz",
|
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-2.5.0.tgz",
|
||||||
"integrity": "sha1-2COIrpyWC+y+oMc7uet5tsbOmus="
|
"integrity": "sha1-2COIrpyWC+y+oMc7uet5tsbOmus="
|
||||||
},
|
},
|
||||||
|
"node_modules/nanoid": {
|
||||||
|
"version": "3.1.25",
|
||||||
|
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.25.tgz",
|
||||||
|
"integrity": "sha512-rdwtIXaXCLFAQbnfqDRnI6jaRHp9fTcYBjtFKE8eezcZ7LuLjhUaQGNeMXf1HmRoCH32CLz6XwX0TtxEOS/A3Q==",
|
||||||
|
"bin": {
|
||||||
|
"nanoid": "bin/nanoid.cjs"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/natural-compare": {
|
"node_modules/natural-compare": {
|
||||||
"version": "1.4.0",
|
"version": "1.4.0",
|
||||||
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
|
||||||
@ -15450,6 +15462,11 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"nanoid": {
|
||||||
|
"version": "3.1.25",
|
||||||
|
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.25.tgz",
|
||||||
|
"integrity": "sha512-rdwtIXaXCLFAQbnfqDRnI6jaRHp9fTcYBjtFKE8eezcZ7LuLjhUaQGNeMXf1HmRoCH32CLz6XwX0TtxEOS/A3Q=="
|
||||||
|
},
|
||||||
"natural-compare": {
|
"natural-compare": {
|
||||||
"version": "1.4.0",
|
"version": "1.4.0",
|
||||||
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
|
||||||
|
@ -30,6 +30,7 @@
|
|||||||
"@nestjs/platform-express": "^8.0.0",
|
"@nestjs/platform-express": "^8.0.0",
|
||||||
"@types/keyv": "^3.1.3",
|
"@types/keyv": "^3.1.3",
|
||||||
"keyv": "^4.0.3",
|
"keyv": "^4.0.3",
|
||||||
|
"nanoid": "^3.1.25",
|
||||||
"reflect-metadata": "^0.1.13",
|
"reflect-metadata": "^0.1.13",
|
||||||
"rimraf": "^3.0.2",
|
"rimraf": "^3.0.2",
|
||||||
"rxjs": "^7.2.0"
|
"rxjs": "^7.2.0"
|
||||||
|
@ -3,14 +3,15 @@ import {
|
|||||||
Controller,
|
Controller,
|
||||||
Get,
|
Get,
|
||||||
Header,
|
Header,
|
||||||
|
InternalServerErrorException,
|
||||||
Param,
|
Param,
|
||||||
Post,
|
Post,
|
||||||
Res,
|
Res,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { Response } from 'express';
|
import { Response } from 'express';
|
||||||
import { StorageNamespace, StorageService } from 'src/storage/storage.service';
|
import { StorageNamespace, StorageService } from 'src/storage/storage.service';
|
||||||
import { hash, hexadecimalToDecimal } from 'src/utils';
|
|
||||||
import { Readable } from 'stream';
|
import { Readable } from 'stream';
|
||||||
|
import { nanoid } from 'nanoid';
|
||||||
|
|
||||||
@Controller()
|
@Controller()
|
||||||
export class ScenesController {
|
export class ScenesController {
|
||||||
@ -30,8 +31,12 @@ export class ScenesController {
|
|||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
async create(@Body() payload: Buffer) {
|
async create(@Body() payload: Buffer) {
|
||||||
const drawingHash = hash(payload);
|
const id = nanoid();
|
||||||
const id = hexadecimalToDecimal(drawingHash);
|
|
||||||
|
// Nanoid has similar collision probability as uuid v4, so the following should *never* happen
|
||||||
|
if (await this.storageService.get(id, this.namespace)) {
|
||||||
|
throw new InternalServerErrorException();
|
||||||
|
}
|
||||||
|
|
||||||
await this.storageService.set(id, payload, this.namespace);
|
await this.storageService.set(id, payload, this.namespace);
|
||||||
|
|
||||||
|
12
src/utils.ts
12
src/utils.ts
@ -1,12 +0,0 @@
|
|||||||
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