write array buffer/base64 conversion

This commit is contained in:
Maxime Cannoodt 2022-08-28 21:04:17 +02:00
parent e9956486fd
commit a2569a2b34
5 changed files with 36 additions and 17 deletions

View File

@ -12,7 +12,6 @@
"@prisma/client": "^4.0.0",
"bloom-filters": "^3.0.0",
"body-parser": "^1.20.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
"crc": "^4.1.1",
"dotenv": "^16.0.1",
@ -939,11 +938,6 @@
"node": ">=10"
}
},
"node_modules/class-transformer": {
"version": "0.5.1",
"resolved": "https://registry.npmjs.org/class-transformer/-/class-transformer-0.5.1.tgz",
"integrity": "sha512-SQa1Ws6hUbfC98vKGxZH3KFY0Y1lm5Zm0SY8XX9zbK7FJCyVEac3ATW0RIpwzW+oOfmHE5PMPufDG9hCfoEOMw=="
},
"node_modules/class-validator": {
"version": "0.13.2",
"resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.13.2.tgz",
@ -5983,11 +5977,6 @@
"resolved": "https://registry.npmjs.org/chownr/-/chownr-2.0.0.tgz",
"integrity": "sha512-bIomtDF5KGpdogkLd9VspvFzk9KfpyyGlS8YFVZl7TGPBHL5snIOnxeshwVgPteQ9b4Eydl+pVbIyE1DcvCWgQ=="
},
"class-transformer": {
"version": "0.5.1",
"resolved": "https://registry.npmjs.org/class-transformer/-/class-transformer-0.5.1.tgz",
"integrity": "sha512-SQa1Ws6hUbfC98vKGxZH3KFY0Y1lm5Zm0SY8XX9zbK7FJCyVEac3ATW0RIpwzW+oOfmHE5PMPufDG9hCfoEOMw=="
},
"class-validator": {
"version": "0.13.2",
"resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.13.2.tgz",

View File

@ -19,7 +19,6 @@
"@prisma/client": "^4.0.0",
"bloom-filters": "^3.0.0",
"body-parser": "^1.20.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
"crc": "^4.1.1",
"dotenv": "^16.0.1",

View File

@ -12,10 +12,10 @@ import {
ValidateIf,
ValidationError,
Matches,
ValidateNested,
IsString,
IsArray,
ValidateNested,
} from "class-validator";
import { Type } from "class-transformer";
export class EncryptedEmbed {
@IsBase64()
@ -54,8 +54,8 @@ export class NotePostRequest {
@Matches("^v[0-9]+$")
crypto_version: string = "v1";
@ValidateNested({ each: true })
@Type(() => EncryptedEmbed)
// validate the shape of each item manually, avoid need for class-transformer package
@IsArray()
embeds: EncryptedEmbed[] = [];
}

View File

@ -11,3 +11,13 @@ export function getConnectingIp(req: Request): string {
req.headers["X-Forwarded-For"] ||
req.socket.remoteAddress) as string;
}
// base64 to array buffer (Node JS api, so don't use atob or btoa)
export function base64ToArrayBuffer(base64: string): ArrayBuffer {
return Buffer.from(base64, "base64");
}
// array buffer to base64 (Node JS api, so don't use atob or btoa)
export function arrayBufferToBase64(buffer: ArrayBuffer): string {
return Buffer.from(buffer).toString("base64");
}

View File

@ -1,5 +1,10 @@
import { describe, it, expect } from "vitest";
import { addDays, getConnectingIp } from "./util";
import {
addDays,
arrayBufferToBase64,
base64ToArrayBuffer,
getConnectingIp,
} from "./util";
describe("addDays()", () => {
it("Should add n days to the input date", () => {
@ -8,3 +13,19 @@ describe("addDays()", () => {
expect(addDays(date, 30)).toEqual(expectedDate);
});
});
describe("converting to/from base64", () => {
it("Should convert a base64 string to an array buffer", () => {
const base64 = "EjRWeJA=";
const expectedBuffer = new Uint8Array([18, 52, 86, 120, 144]);
expect(new Uint8Array(base64ToArrayBuffer(base64))).toStrictEqual(
expectedBuffer
);
});
it("Should convert an array buffer to a base64 string", () => {
const buffer = new Uint8Array([18, 52, 86, 120, 144]);
const expectedBase64 = "EjRWeJA=";
expect(arrayBufferToBase64(buffer)).toEqual(expectedBase64);
});
});