Skip to content

Add possibility to register ContentCodec with scheme #1412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions packages/core/src/content-serdes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class ContentSerdes {
public static readonly JSON_LD: string = "application/ld+json";

private codecs: Map<string, ContentCodec> = new Map();
private codecsBySchemes: Map<string, ContentCodec> = new Map();
private offered: Set<string> = new Set<string>();

public static get(): ContentSerdes {
Expand Down Expand Up @@ -107,8 +108,17 @@ export class ContentSerdes {
return params;
}

public addCodec(codec: ContentCodec, offered = false): void {
ContentSerdes.get().codecs.set(codec.getMediaType(), codec);
// function to create id for combining internally mediaType and scheme
private getMediaTypeScheme(mt: string, scheme: string): string {
return mt + "|" + scheme;
}

public addCodec(codec: ContentCodec, offered = false, scheme?: string): void {
if (scheme !== undefined) {
ContentSerdes.get().codecsBySchemes.set(this.getMediaTypeScheme(codec.getMediaType(), scheme), codec);
} else {
ContentSerdes.get().codecs.set(codec.getMediaType(), codec);
}
if (offered) ContentSerdes.get().offered.add(codec.getMediaType());
}

Expand All @@ -125,7 +135,7 @@ export class ContentSerdes {
return this.codecs.has(mt);
}

public contentToValue(content: ReadContent, schema: DataSchema): DataSchemaValue | undefined {
public contentToValue(content: ReadContent, schema: DataSchema, scheme?: string): DataSchemaValue | undefined {
if (content.type === undefined) {
if (content.body.byteLength > 0) {
// default to application/json
Expand All @@ -142,9 +152,16 @@ export class ContentSerdes {

// choose codec based on mediaType
if (this.codecs.has(mt)) {
debug(`ContentSerdes deserializing from ${content.type}`);

const codec = this.codecs.get(mt);
let codec: ContentCodec | undefined;
if (scheme !== undefined) {
codec = this.codecsBySchemes.get(this.getMediaTypeScheme(mt, scheme));
}
if (codec === undefined) {
debug(`ContentSerdes deserializing from ${content.type}`);
codec = this.codecs.get(mt);
} else {
debug(`ContentSerdes deserializing from combining ${content.type} and ${mt}`);
}

// use codec to deserialize
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- this.codecs.has(mt) is true
Expand All @@ -160,7 +177,8 @@ export class ContentSerdes {
public valueToContent(
value: DataSchemaValue | ReadableStream,
schema: DataSchema | undefined,
contentType = ContentSerdes.DEFAULT
contentType = ContentSerdes.DEFAULT,
scheme?: string
): Content {
if (value === undefined) warn("ContentSerdes valueToContent got no value");

Expand All @@ -175,7 +193,13 @@ export class ContentSerdes {
const par = ContentSerdes.getMediaTypeParameters(contentType);

// choose codec based on mediaType
const codec = this.codecs.get(mt);
let codec: ContentCodec | undefined;
if (scheme !== undefined) {
codec = this.codecsBySchemes.get(this.getMediaTypeScheme(mt, scheme));
}
if (codec === undefined) {
codec = this.codecs.get(mt);
}
if (codec) {
debug(`ContentSerdes serializing to ${contentType}`);
bytes = codec.valueToBytes(value, schema, par);
Expand Down
24 changes: 24 additions & 0 deletions packages/core/test/ContentSerdesTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ class HodorCodec implements ContentCodec {
}
}

class HodorHttpCodec extends HodorCodec {
bytesToValue(): string {
return "HodorHttp";
}

valueToBytes(): Buffer {
return Buffer.from("HodorHttp");
}
}

@suite("testing OctectStream codec")
class SerdesOctetTests {
@test "OctetStream to value"() {
Expand Down Expand Up @@ -1058,6 +1068,7 @@ class CborSerdesTests {
class SerdesCodecTests {
static before() {
ContentSerdes.addCodec(new HodorCodec());
ContentSerdes.addCodec(new HodorHttpCodec(), false, "http");
}

@test async "new codec should serialize"() {
Expand All @@ -1072,4 +1083,17 @@ class SerdesCodecTests {
"Hodor"
);
}

@test async "new codec based on scheme should serialize"() {
const content = ContentSerdes.valueToContent("The meaning of Life", { type: "string" }, "text/hodor", "http");
const body = await content.toBuffer();
body.toString().should.equal("HodorHttp");
}

@test "new codec based on scheme should deserialize"() {
const buffer = Buffer.from("Some actual meaningful stuff");
expect(
ContentSerdes.contentToValue({ type: "text/hodor", body: buffer }, { type: "string" }, "http")
).to.be.deep.equal("HodorHttp");
}
}
Loading