From 3fa96aa05acf292311faa5fbbc83f1f1bd120f68 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 18 Nov 2023 05:59:59 -0500 Subject: [PATCH] fix: update how maps are packed and add missing serialization for media, directory, and files --- src/serialization/metadata/directory.ts | 8 ++++---- src/serialization/metadata/media.ts | 2 +- src/serialization/pack.ts | 27 +++++++++++++++++++++---- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/serialization/metadata/directory.ts b/src/serialization/metadata/directory.ts index 15e7d91..b277474 100644 --- a/src/serialization/metadata/directory.ts +++ b/src/serialization/metadata/directory.ts @@ -141,7 +141,7 @@ class DirectoryMetadataDetails { } } -class DirectoryReference { +export class DirectoryReference { created: number; name: string; encryptedWriteKey: Uint8Array; @@ -214,7 +214,7 @@ class DirectoryReference { return map; } } -class FileReference { +export class FileReference { created: number; file: FileVersion; history: Map | null; @@ -315,7 +315,7 @@ class FileReference { } } -class FileVersion { +export class FileVersion { ts: number; encryptedCID?: EncryptedCID; plaintextCID?: CID; @@ -382,7 +382,7 @@ class FileVersion { }; } } -class FileVersionThumbnail { +export class FileVersionThumbnail { imageType: string | null; aspectRatio: number; cid: EncryptedCID; diff --git a/src/serialization/metadata/media.ts b/src/serialization/metadata/media.ts index e6d4c95..7f4a7ff 100644 --- a/src/serialization/metadata/media.ts +++ b/src/serialization/metadata/media.ts @@ -153,7 +153,7 @@ class MediaMetadataDetails { } } -class MediaFormat { +export class MediaFormat { subtype: string; role: string | null; ext: string | null; diff --git a/src/serialization/pack.ts b/src/serialization/pack.ts index d5f1b7e..6328a8a 100644 --- a/src/serialization/pack.ts +++ b/src/serialization/pack.ts @@ -1,4 +1,13 @@ import NodeId from "../nodeId.js"; +import CID from "#cid.js"; +import { MediaFormat } from "#serialization/metadata/media.js"; +import { + DirectoryReference, + FileReference, + FileVersion, + FileVersionThumbnail, +} from "#serialization/metadata/directory.js"; +import { Buffer } from "buffer"; export default class Packer { private _bufSize: number; @@ -236,7 +245,7 @@ export default class Packer { return Buffer.concat(this._builder); } - public pack(v: any) { + public pack(v: any): this { if (v === null) { this.packNull(); } else if (typeof v === "number") { @@ -258,14 +267,24 @@ export default class Packer { } } else if (v instanceof Map) { this.packMapLength(v.size); - for (const [key, value] of v.entries()) { + v.forEach((value, key) => { this.pack(key); this.pack(value); - } + }); + } else if ( + v instanceof MediaFormat || + v instanceof DirectoryReference || + v instanceof FileReference || + v instanceof FileVersion || + v instanceof FileVersionThumbnail + ) { + this.pack(v.encode()); + } else if (v instanceof CID) { + this.pack(v.toBytes()); } else if (v instanceof NodeId) { this.pack(v.bytes); } else { - throw new Error(`Could not pack ${v.constructor.name}`); + throw new Error(`Could not pack type: ${typeof v}`); } return this;