Skip to content
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

feat(server): add custom serializers #325

Merged
merged 3 commits into from
Jun 22, 2023
Merged
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
26 changes: 20 additions & 6 deletions documentation/docs/internals/data-serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,27 @@ We try to support all JavaScript built-in types. Currently we support:
* Objects (plain and class instances)
* Collections (Array, Map and Set)
* Data buffers (Typed Arrays)
* Others (Date and URL)
* Others (BigInt, Date, RegExp and URL)

And we have planned to add support for:
## Adding your own

* Big integers
* Regular expressions
The serialization package is open for implementing your own serializers in case your application has special (de)serialization needs. This can be done by creating a custom serializer and adding it to the server. Documentation how to build a custom serializer can be found in the [readme](https://github.com/MaskingTechnology/jitar/blob/main/packages/serialization/README.md){target="_blank"} of the serialization package.

## Adding your own
```ts
//src/jitar.ts
import { buildServer } from 'jitar';

import CustomSerializer from './CustomSerializer.js';

const moduleImporter = async (specifier: string) => import(specifier);

const server = await buildServer(moduleImporter);
server.addSerializer(new CustomSerializer());
server.start();
```

Jitar uses a default class loader. If you need the class loader in your custom serializer you can get it from the server instance.

The serialization package is open for implementing your own serializers in case your application has special (de)serialization needs. Jitar currently does not support adding custom serializers, but will be supported in future versions.
```ts
const classLoader = server.classLoader;
```
16 changes: 14 additions & 2 deletions packages/server-nodejs/src/JitarServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import express, { Express } from 'express';
import { Logger } from 'tslog';

import { HealthCheck, LocalGateway, LocalNode, LocalRepository, Middleware, ProcedureRuntime, Proxy, Runtime, RemoteClassLoader } from '@jitar/runtime';
import { Serializer, SerializerBuilder } from '@jitar/serialization';
import { ClassLoader, Serializer, SerializerBuilder, ValueSerializer } from '@jitar/serialization';

import ServerOptions from './configuration/ServerOptions.js';

Expand Down Expand Up @@ -44,6 +44,7 @@ export default class JitarServer
#app: Express;
#runtime?: Runtime;
#serializer: Serializer;
#classLoader: ClassLoader;

#options: ServerOptions;
#configuration: RuntimeConfiguration;
Expand All @@ -53,7 +54,8 @@ export default class JitarServer

constructor()
{
this.#serializer = SerializerBuilder.build(new RemoteClassLoader());
this.#classLoader = new RemoteClassLoader();
this.#serializer = SerializerBuilder.build(this.#classLoader);

this.#app = express();

Expand All @@ -67,6 +69,11 @@ export default class JitarServer
this.#logger = LogBuilder.build(this.#options.loglevel);
}

get classLoader(): ClassLoader
{
return this.#classLoader;
}

async build(): Promise<void>
{
this.#runtime = await RuntimeConfigurator.configure(this.#configuration);
Expand Down Expand Up @@ -96,6 +103,11 @@ export default class JitarServer
this.#registeredHealthChecks.set(name, healthCheck);
}

addSerializer(serializer: ValueSerializer): void
{
this.#serializer.addSerializer(serializer);
}

addMiddleware(middleware: Middleware): void
{
if (this.#runtime === undefined)
Expand Down