jitar 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (227) hide show
  1. package/dist/client.d.ts +3 -0
  2. package/dist/client.js +24 -0
  3. package/dist/core/Context.d.ts +5 -0
  4. package/dist/core/Context.js +7 -0
  5. package/dist/core/ErrorManager.d.ts +3 -0
  6. package/dist/core/ErrorManager.js +11 -0
  7. package/dist/core/Implementation.d.ts +10 -0
  8. package/dist/core/Implementation.js +44 -0
  9. package/dist/core/Procedure.d.ts +13 -0
  10. package/dist/core/Procedure.js +58 -0
  11. package/dist/core/Segment.d.ts +13 -0
  12. package/dist/core/Segment.js +36 -0
  13. package/dist/core/SegmentBuilder.d.ts +6 -0
  14. package/dist/core/SegmentBuilder.js +32 -0
  15. package/dist/core/Version.d.ts +13 -0
  16. package/dist/core/Version.js +50 -0
  17. package/dist/core/definitions/AccessLevel.d.ts +2 -0
  18. package/dist/core/definitions/AccessLevel.js +2 -0
  19. package/dist/core/errors/BadRequest.d.ts +3 -0
  20. package/dist/core/errors/BadRequest.js +6 -0
  21. package/dist/core/errors/Forbidden.d.ts +3 -0
  22. package/dist/core/errors/Forbidden.js +6 -0
  23. package/dist/core/errors/ImplementationNotFound.d.ts +4 -0
  24. package/dist/core/errors/ImplementationNotFound.js +7 -0
  25. package/dist/core/errors/InvalidVersionNumber.d.ts +4 -0
  26. package/dist/core/errors/InvalidVersionNumber.js +7 -0
  27. package/dist/core/errors/MissingParameterValue.d.ts +4 -0
  28. package/dist/core/errors/MissingParameterValue.js +7 -0
  29. package/dist/core/errors/NotFound.d.ts +3 -0
  30. package/dist/core/errors/NotFound.js +6 -0
  31. package/dist/core/errors/NotImplemented.d.ts +3 -0
  32. package/dist/core/errors/NotImplemented.js +6 -0
  33. package/dist/core/errors/PaymentRequired.d.ts +3 -0
  34. package/dist/core/errors/PaymentRequired.js +6 -0
  35. package/dist/core/errors/ProcedureNotFound.d.ts +4 -0
  36. package/dist/core/errors/ProcedureNotFound.js +7 -0
  37. package/dist/core/errors/Teapot.d.ts +3 -0
  38. package/dist/core/errors/Teapot.js +6 -0
  39. package/dist/core/errors/Unauthorized.d.ts +3 -0
  40. package/dist/core/errors/Unauthorized.js +6 -0
  41. package/dist/core/errors/UnknownParameter.d.ts +4 -0
  42. package/dist/core/errors/UnknownParameter.js +7 -0
  43. package/dist/core/interfaces/Runner.d.ts +4 -0
  44. package/dist/core/interfaces/Runner.js +1 -0
  45. package/dist/core/reflection/ParameterParser.d.ts +5 -0
  46. package/dist/core/reflection/ParameterParser.js +51 -0
  47. package/dist/core/reflection/ReflectionHelper.d.ts +13 -0
  48. package/dist/core/reflection/ReflectionHelper.js +69 -0
  49. package/dist/core/reflection/models/ReflectionField.d.ts +8 -0
  50. package/dist/core/reflection/models/ReflectionField.js +14 -0
  51. package/dist/core/reflection/models/ReflectionParameter.d.ts +7 -0
  52. package/dist/core/reflection/models/ReflectionParameter.js +11 -0
  53. package/dist/core/types/Component.d.ts +5 -0
  54. package/dist/core/types/Component.js +1 -0
  55. package/dist/core/types/FlexObject.d.ts +4 -0
  56. package/dist/core/types/FlexObject.js +1 -0
  57. package/dist/core/types/Module.d.ts +3 -0
  58. package/dist/core/types/Module.js +1 -0
  59. package/dist/core/types/SegmentImplementation.d.ts +6 -0
  60. package/dist/core/types/SegmentImplementation.js +1 -0
  61. package/dist/core/types/SegmentModule.d.ts +5 -0
  62. package/dist/core/types/SegmentModule.js +1 -0
  63. package/dist/core/types/SegmentProcedure.d.ts +7 -0
  64. package/dist/core/types/SegmentProcedure.js +1 -0
  65. package/dist/core/utils/FqnBuilder.d.ts +3 -0
  66. package/dist/core/utils/FqnBuilder.js +5 -0
  67. package/dist/hooks.d.ts +2 -0
  68. package/dist/hooks.js +2 -0
  69. package/dist/lib.d.ts +35 -0
  70. package/dist/lib.js +30 -0
  71. package/dist/runtime/ClientId.d.ts +4 -0
  72. package/dist/runtime/ClientId.js +11 -0
  73. package/dist/runtime/Gateway.d.ts +5 -0
  74. package/dist/runtime/Gateway.js +3 -0
  75. package/dist/runtime/LocalGateway.d.ts +14 -0
  76. package/dist/runtime/LocalGateway.js +61 -0
  77. package/dist/runtime/LocalNode.d.ts +18 -0
  78. package/dist/runtime/LocalNode.js +75 -0
  79. package/dist/runtime/LocalRepository.d.ts +17 -0
  80. package/dist/runtime/LocalRepository.js +94 -0
  81. package/dist/runtime/Node.d.ts +3 -0
  82. package/dist/runtime/Node.js +3 -0
  83. package/dist/runtime/NodeBalancer.d.ts +9 -0
  84. package/dist/runtime/NodeBalancer.js +34 -0
  85. package/dist/runtime/NodeMonitor.d.ts +7 -0
  86. package/dist/runtime/NodeMonitor.js +38 -0
  87. package/dist/runtime/ProcedureRunner.d.ts +9 -0
  88. package/dist/runtime/ProcedureRunner.js +11 -0
  89. package/dist/runtime/ProcedureRuntime.d.ts +13 -0
  90. package/dist/runtime/ProcedureRuntime.js +25 -0
  91. package/dist/runtime/Proxy.d.ts +18 -0
  92. package/dist/runtime/Proxy.js +33 -0
  93. package/dist/runtime/Remote.d.ts +15 -0
  94. package/dist/runtime/Remote.js +102 -0
  95. package/dist/runtime/RemoteGateway.d.ts +11 -0
  96. package/dist/runtime/RemoteGateway.js +22 -0
  97. package/dist/runtime/RemoteNode.d.ts +12 -0
  98. package/dist/runtime/RemoteNode.js +29 -0
  99. package/dist/runtime/RemoteRepository.d.ts +14 -0
  100. package/dist/runtime/RemoteRepository.js +34 -0
  101. package/dist/runtime/Repository.d.ts +11 -0
  102. package/dist/runtime/Repository.js +3 -0
  103. package/dist/runtime/Runtime.d.ts +9 -0
  104. package/dist/runtime/Runtime.js +27 -0
  105. package/dist/runtime/caching/CacheBuilder.d.ts +6 -0
  106. package/dist/runtime/caching/CacheBuilder.js +249 -0
  107. package/dist/runtime/caching/ImportRewriter.d.ts +4 -0
  108. package/dist/runtime/caching/ImportRewriter.js +49 -0
  109. package/dist/runtime/caching/RemoteBuilder.d.ts +5 -0
  110. package/dist/runtime/caching/RemoteBuilder.js +31 -0
  111. package/dist/runtime/caching/SourceAppender.d.ts +5 -0
  112. package/dist/runtime/caching/SourceAppender.js +30 -0
  113. package/dist/runtime/caching/definitions/Keywords.d.ts +2 -0
  114. package/dist/runtime/caching/definitions/Keywords.js +2 -0
  115. package/dist/runtime/caching/errors/InvalidSegmentFilename.d.ts +3 -0
  116. package/dist/runtime/caching/errors/InvalidSegmentFilename.js +5 -0
  117. package/dist/runtime/caching/errors/MissingModuleExport.d.ts +3 -0
  118. package/dist/runtime/caching/errors/MissingModuleExport.js +5 -0
  119. package/dist/runtime/caching/errors/SegmentFileNotLoaded.d.ts +3 -0
  120. package/dist/runtime/caching/errors/SegmentFileNotLoaded.js +5 -0
  121. package/dist/runtime/caching/errors/SegmentModuleNotLoaded.d.ts +3 -0
  122. package/dist/runtime/caching/errors/SegmentModuleNotLoaded.js +5 -0
  123. package/dist/runtime/caching/models/ApplicationModule.d.ts +9 -0
  124. package/dist/runtime/caching/models/ApplicationModule.js +19 -0
  125. package/dist/runtime/caching/models/Implementation.d.ts +12 -0
  126. package/dist/runtime/caching/models/Implementation.js +27 -0
  127. package/dist/runtime/caching/models/Procedure.d.ts +9 -0
  128. package/dist/runtime/caching/models/Procedure.js +15 -0
  129. package/dist/runtime/caching/models/Segment.d.ts +10 -0
  130. package/dist/runtime/caching/models/Segment.js +16 -0
  131. package/dist/runtime/caching/models/SegmentModule.d.ts +10 -0
  132. package/dist/runtime/caching/models/SegmentModule.js +14 -0
  133. package/dist/runtime/caching/types/SegmentFile.d.ts +5 -0
  134. package/dist/runtime/caching/types/SegmentFile.js +1 -0
  135. package/dist/runtime/caching/types/SegmentImportProperties.d.ts +6 -0
  136. package/dist/runtime/caching/types/SegmentImportProperties.js +1 -0
  137. package/dist/runtime/caching/types/SegmentImports.d.ts +5 -0
  138. package/dist/runtime/caching/types/SegmentImports.js +1 -0
  139. package/dist/runtime/errors/ClientNotFound.d.ts +3 -0
  140. package/dist/runtime/errors/ClientNotFound.js +6 -0
  141. package/dist/runtime/errors/FileNotFound.d.ts +3 -0
  142. package/dist/runtime/errors/FileNotFound.js +6 -0
  143. package/dist/runtime/errors/InvalidClientId.d.ts +3 -0
  144. package/dist/runtime/errors/InvalidClientId.js +6 -0
  145. package/dist/runtime/errors/InvalidJitarHooks.d.ts +3 -0
  146. package/dist/runtime/errors/InvalidJitarHooks.js +6 -0
  147. package/dist/runtime/errors/InvalidSegmentFile.d.ts +3 -0
  148. package/dist/runtime/errors/InvalidSegmentFile.js +6 -0
  149. package/dist/runtime/errors/ModuleNotLoaded.d.ts +3 -0
  150. package/dist/runtime/errors/ModuleNotLoaded.js +6 -0
  151. package/dist/runtime/errors/NoNodeAvailable.d.ts +3 -0
  152. package/dist/runtime/errors/NoNodeAvailable.js +6 -0
  153. package/dist/runtime/errors/RepositoryNotAvaiable.d.ts +3 -0
  154. package/dist/runtime/errors/RepositoryNotAvaiable.js +6 -0
  155. package/dist/runtime/errors/RuntimeNotAvailable.d.ts +3 -0
  156. package/dist/runtime/errors/RuntimeNotAvailable.js +6 -0
  157. package/dist/runtime/errors/SegmentNotFound.d.ts +3 -0
  158. package/dist/runtime/errors/SegmentNotFound.js +6 -0
  159. package/dist/runtime/hooks/dependencies.d.ts +2 -0
  160. package/dist/runtime/hooks/dependencies.js +9 -0
  161. package/dist/runtime/hooks/runtime.d.ts +3 -0
  162. package/dist/runtime/hooks/runtime.js +16 -0
  163. package/dist/runtime/interfaces/FileManager.d.ts +11 -0
  164. package/dist/runtime/interfaces/FileManager.js +1 -0
  165. package/dist/runtime/interfaces/HealthCheck.d.ts +3 -0
  166. package/dist/runtime/interfaces/HealthCheck.js +1 -0
  167. package/dist/runtime/interfaces/Middleware.d.ts +5 -0
  168. package/dist/runtime/interfaces/Middleware.js +1 -0
  169. package/dist/runtime/middleware/CorsMiddleware.d.ts +8 -0
  170. package/dist/runtime/middleware/CorsMiddleware.js +16 -0
  171. package/dist/runtime/models/File.d.ts +9 -0
  172. package/dist/runtime/models/File.js +14 -0
  173. package/dist/runtime/serialization/ArrayBufferSerializer.d.ts +10 -0
  174. package/dist/runtime/serialization/ArrayBufferSerializer.js +54 -0
  175. package/dist/runtime/serialization/ArraySerializer.d.ts +7 -0
  176. package/dist/runtime/serialization/ArraySerializer.js +15 -0
  177. package/dist/runtime/serialization/ClassSerializer.d.ts +10 -0
  178. package/dist/runtime/serialization/ClassSerializer.js +74 -0
  179. package/dist/runtime/serialization/DateSerializer.d.ts +9 -0
  180. package/dist/runtime/serialization/DateSerializer.js +21 -0
  181. package/dist/runtime/serialization/MapSerializer.d.ts +9 -0
  182. package/dist/runtime/serialization/MapSerializer.js +35 -0
  183. package/dist/runtime/serialization/ObjectSerializer.d.ts +9 -0
  184. package/dist/runtime/serialization/ObjectSerializer.js +21 -0
  185. package/dist/runtime/serialization/SetSerializer.d.ts +9 -0
  186. package/dist/runtime/serialization/SetSerializer.js +23 -0
  187. package/dist/runtime/serialization/ValueSerializer.d.ts +8 -0
  188. package/dist/runtime/serialization/ValueSerializer.js +69 -0
  189. package/dist/runtime/serialization/errors/ClassNotFound.d.ts +3 -0
  190. package/dist/runtime/serialization/errors/ClassNotFound.js +6 -0
  191. package/dist/runtime/serialization/errors/InvalidClass.d.ts +3 -0
  192. package/dist/runtime/serialization/errors/InvalidClass.js +6 -0
  193. package/dist/runtime/serialization/errors/InvalidPropertyType.d.ts +4 -0
  194. package/dist/runtime/serialization/errors/InvalidPropertyType.js +7 -0
  195. package/dist/runtime/serialization/interfaces/Serializer.d.ts +4 -0
  196. package/dist/runtime/serialization/interfaces/Serializer.js +1 -0
  197. package/dist/runtime/serialization/types/SerializableObject.d.ts +3 -0
  198. package/dist/runtime/serialization/types/SerializableObject.js +1 -0
  199. package/dist/runtime/serialization/types/Serialized.d.ts +5 -0
  200. package/dist/runtime/serialization/types/Serialized.js +1 -0
  201. package/dist/runtime/serialization/types/SerializedArrayBuffer.d.ts +6 -0
  202. package/dist/runtime/serialization/types/SerializedArrayBuffer.js +1 -0
  203. package/dist/runtime/serialization/types/SerializedClass.d.ts +8 -0
  204. package/dist/runtime/serialization/types/SerializedClass.js +1 -0
  205. package/dist/runtime/serialization/types/SerializedDate.d.ts +5 -0
  206. package/dist/runtime/serialization/types/SerializedDate.js +1 -0
  207. package/dist/runtime/serialization/types/SerializedMap.d.ts +8 -0
  208. package/dist/runtime/serialization/types/SerializedMap.js +1 -0
  209. package/dist/runtime/serialization/types/SerializedObject.d.ts +4 -0
  210. package/dist/runtime/serialization/types/SerializedObject.js +1 -0
  211. package/dist/runtime/serialization/types/SerializedSet.d.ts +5 -0
  212. package/dist/runtime/serialization/types/SerializedSet.js +1 -0
  213. package/dist/runtime/serialization/types/TypedArray.d.ts +2 -0
  214. package/dist/runtime/serialization/types/TypedArray.js +1 -0
  215. package/dist/runtime/types/JitarHooks.d.ts +6 -0
  216. package/dist/runtime/types/JitarHooks.js +1 -0
  217. package/dist/runtime/types/ModuleImporter.d.ts +3 -0
  218. package/dist/runtime/types/ModuleImporter.js +1 -0
  219. package/dist/runtime/types/NextHandler.d.ts +2 -0
  220. package/dist/runtime/types/NextHandler.js +1 -0
  221. package/dist/runtime/utils/ModuleAnalyser.d.ts +7 -0
  222. package/dist/runtime/utils/ModuleAnalyser.js +34 -0
  223. package/dist/runtime/utils/ModuleLoader.d.ts +8 -0
  224. package/dist/runtime/utils/ModuleLoader.js +30 -0
  225. package/dist/runtime/utils/UrlRewriter.d.ts +4 -0
  226. package/dist/runtime/utils/UrlRewriter.js +36 -0
  227. package/package.json +1 -1
@@ -0,0 +1,6 @@
1
+ export default class InvalidClientId extends Error {
2
+ constructor(clientId) {
3
+ super(`Invalid client id '${clientId}'`);
4
+ }
5
+ }
6
+ InvalidClientId.source = '/jitar/runtime/errors/InvalidClientId.js';
@@ -0,0 +1,3 @@
1
+ export default class InvalidJitarHooks extends Error {
2
+ constructor();
3
+ }
@@ -0,0 +1,6 @@
1
+ export default class InvalidJitarHooks extends Error {
2
+ constructor() {
3
+ super(`Invalid Jitar hooks`);
4
+ }
5
+ }
6
+ InvalidJitarHooks.source = '/jitar/runtime/errors/InvalidJitarHooks.js';
@@ -0,0 +1,3 @@
1
+ export default class InvalidSegmentFile extends Error {
2
+ constructor(filename: string);
3
+ }
@@ -0,0 +1,6 @@
1
+ export default class InvalidSegmentFile extends Error {
2
+ constructor(filename) {
3
+ super(`Missing files array in segment file '${filename}'`);
4
+ }
5
+ }
6
+ InvalidSegmentFile.source = '/jitar/runtime/errors/InvalidSegmentFile.js';
@@ -0,0 +1,3 @@
1
+ export default class ModuleNotLoaded extends Error {
2
+ constructor(url: string, message?: string);
3
+ }
@@ -0,0 +1,6 @@
1
+ export default class ModuleNotLoaded extends Error {
2
+ constructor(url, message) {
3
+ super(`Module '${url}' could not be loaded${message !== undefined ? ` | ${message}` : ''}`);
4
+ }
5
+ }
6
+ ModuleNotLoaded.source = '/jitar/runtime/errors/ModuleNotLoaded.js';
@@ -0,0 +1,3 @@
1
+ export default class NoNodeAvailable extends Error {
2
+ constructor(name: string);
3
+ }
@@ -0,0 +1,6 @@
1
+ export default class NoNodeAvailable extends Error {
2
+ constructor(name) {
3
+ super(`No node available for procedure '${name}'`);
4
+ }
5
+ }
6
+ NoNodeAvailable.source = '/jitar/runtime/errors/NoNodeAvailable.js';
@@ -0,0 +1,3 @@
1
+ export default class RepositoryNotAvailable extends Error {
2
+ constructor();
3
+ }
@@ -0,0 +1,6 @@
1
+ export default class RepositoryNotAvailable extends Error {
2
+ constructor() {
3
+ super(`Repository not available`);
4
+ }
5
+ }
6
+ RepositoryNotAvailable.source = '/jitar/runtime/errors/RepositoryNotAvailable.js';
@@ -0,0 +1,3 @@
1
+ export default class RuntimeNotAvailable extends Error {
2
+ constructor();
3
+ }
@@ -0,0 +1,6 @@
1
+ export default class RuntimeNotAvailable extends Error {
2
+ constructor() {
3
+ super(`Runtime not available`);
4
+ }
5
+ }
6
+ RuntimeNotAvailable.source = '/jitar/runtime/errors/RuntimeNotAvailable.js';
@@ -0,0 +1,3 @@
1
+ export default class SegmentNotFound extends Error {
2
+ constructor(source: string);
3
+ }
@@ -0,0 +1,6 @@
1
+ export default class SegmentNotFound extends Error {
2
+ constructor(source) {
3
+ super(`Segment found for '${source}'`);
4
+ }
5
+ }
6
+ SegmentNotFound.source = '/jitar/runtime/errors/SegmentNotFound.js';
@@ -0,0 +1,2 @@
1
+ export declare function setDependencyLoader(loader: Function): void;
2
+ export declare function getDependency(name: string): Promise<unknown>;
@@ -0,0 +1,9 @@
1
+ import ModuleLoader from '../utils/ModuleLoader.js';
2
+ let _loader = ModuleLoader.import;
3
+ export function setDependencyLoader(loader) {
4
+ _loader = loader;
5
+ }
6
+ export async function getDependency(name) {
7
+ const module = await _loader(name);
8
+ return module.default !== undefined ? module.default : module;
9
+ }
@@ -0,0 +1,3 @@
1
+ import LocalNode from '../LocalNode.js';
2
+ export declare function setRuntime(runtime: LocalNode): void;
3
+ export declare function runProcedure(fqn: string, versionNumber: string, args: object, context?: object): Promise<unknown>;
@@ -0,0 +1,16 @@
1
+ import Context from '../../core/Context.js';
2
+ import Version from '../../core/Version.js';
3
+ import RuntimeNotAvailable from '../errors/RuntimeNotAvailable.js';
4
+ let _runtime;
5
+ export function setRuntime(runtime) {
6
+ _runtime = runtime;
7
+ }
8
+ export async function runProcedure(fqn, versionNumber, args, context) {
9
+ if (_runtime === undefined) {
10
+ throw new RuntimeNotAvailable();
11
+ }
12
+ const version = Version.parse(versionNumber);
13
+ const argsMap = new Map(Object.entries(args));
14
+ const headersMap = context instanceof Context ? context.headers : new Map();
15
+ return _runtime.run(fqn, version, argsMap, headersMap);
16
+ }
@@ -0,0 +1,11 @@
1
+ /// <reference types="node" />
2
+ import File from '../models/File.js';
3
+ export default interface FileManager {
4
+ getRootLocation(): string;
5
+ getAbsoluteLocation(filename: string): string;
6
+ getRelativeLocation(filename: string): string;
7
+ getType(filename: string): Promise<string>;
8
+ getContent(filename: string): Promise<Buffer | string>;
9
+ load(filename: string): Promise<File>;
10
+ store(filename: string, content: string): Promise<void>;
11
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ export default interface HealthCheck {
2
+ isHealthy(): Promise<boolean>;
3
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ import Version from '../../core/Version.js';
2
+ import NextHandler from '../types/NextHandler.js';
3
+ export default interface Middleware {
4
+ handle(fqn: string, version: Version, args: Map<string, unknown>, headers: Map<string, string>, next: NextHandler): Promise<unknown>;
5
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import Middleware from '../interfaces/Middleware.js';
2
+ import Version from '../../core/Version.js';
3
+ import NextHandler from '../types/NextHandler.js';
4
+ export default class CorsMiddleware implements Middleware {
5
+ #private;
6
+ constructor(origin?: string);
7
+ handle(fqn: string, version: Version, args: Map<string, unknown>, headers: Map<string, string>, next: NextHandler): Promise<unknown>;
8
+ }
@@ -0,0 +1,16 @@
1
+ export default class CorsMiddleware {
2
+ #origin;
3
+ #methods = ['GET', 'POST'];
4
+ constructor(origin = '*') {
5
+ this.#origin = origin;
6
+ }
7
+ async handle(fqn, version, args, headers, next) {
8
+ const result = await next();
9
+ this.#setHeaders(headers);
10
+ return result;
11
+ }
12
+ #setHeaders(headers) {
13
+ headers.set('Access-Control-Allow-Origin', this.#origin);
14
+ headers.set('Access-Control-Allow-Methods', this.#methods.join(', '));
15
+ }
16
+ }
@@ -0,0 +1,9 @@
1
+ /// <reference types="node" />
2
+ export default class File {
3
+ #private;
4
+ constructor(location: string, type: string, content: Buffer | string);
5
+ get location(): string;
6
+ get type(): string;
7
+ get content(): string | Buffer;
8
+ get size(): number;
9
+ }
@@ -0,0 +1,14 @@
1
+ export default class File {
2
+ #location;
3
+ #type;
4
+ #content;
5
+ constructor(location, type, content) {
6
+ this.#location = location;
7
+ this.#type = type;
8
+ this.#content = content;
9
+ }
10
+ get location() { return this.#location; }
11
+ get type() { return this.#type; }
12
+ get content() { return this.#content; }
13
+ get size() { return this.#content.length; }
14
+ }
@@ -0,0 +1,10 @@
1
+ import Serializer from './interfaces/Serializer.js';
2
+ import SerializedArrayBuffer from './types/SerializedArrayBuffer.js';
3
+ import TypedArray from './types/TypedArray.js';
4
+ declare class ArrayBufferSerializer implements Serializer {
5
+ #private;
6
+ serialize(data: TypedArray): SerializedArrayBuffer;
7
+ deserialize(object: SerializedArrayBuffer): Promise<TypedArray>;
8
+ }
9
+ declare const instance: ArrayBufferSerializer;
10
+ export default instance;
@@ -0,0 +1,54 @@
1
+ import InvalidPropertyType from './errors/InvalidPropertyType.js';
2
+ import ReflectionHelper from '../../core/reflection/ReflectionHelper.js';
3
+ class ArrayBufferSerializer {
4
+ serialize(data) {
5
+ const type = data.constructor.name;
6
+ const view = new DataView(data.buffer);
7
+ const bytes = [];
8
+ for (let index = 0; index < view.byteLength; index++) {
9
+ bytes.push(view.getUint8(index));
10
+ }
11
+ const returnvalue = { serialized: true, name: 'ArrayBuffer', type: type, bytes: bytes };
12
+ return returnvalue;
13
+ }
14
+ async deserialize(object) {
15
+ this.#validateSerializedArrayBuffer(object);
16
+ const type = object.type;
17
+ const bytes = object.bytes;
18
+ const buffer = new ArrayBuffer(bytes.length);
19
+ const view = new DataView(buffer);
20
+ for (let index = 0; index < bytes.length; index++) {
21
+ view.setUint8(index, bytes[index]);
22
+ }
23
+ const clazz = globalThis[type];
24
+ return ReflectionHelper.createInstance(clazz, [buffer]);
25
+ }
26
+ #validateSerializedArrayBuffer(object) {
27
+ if ((object.bytes instanceof Array) === false) {
28
+ throw new InvalidPropertyType('ArrayBuffer', 'bytes', 'Array');
29
+ }
30
+ if (this.#isValidType(object.type) === false) {
31
+ throw new InvalidPropertyType('ArrayBuffer', 'type', 'TypedArray');
32
+ }
33
+ }
34
+ #isValidType(type) {
35
+ switch (type) {
36
+ case 'Int8Array':
37
+ case 'Uint8Array':
38
+ case 'Uint8ClampedArray':
39
+ case 'Int16Array':
40
+ case 'Uint16Array':
41
+ case 'Int32Array':
42
+ case 'Uint32Array':
43
+ case 'Float32Array':
44
+ case 'Float64Array':
45
+ case 'BigInt64Array':
46
+ case 'BigUint64Array':
47
+ return true;
48
+ default:
49
+ return false;
50
+ }
51
+ }
52
+ }
53
+ const instance = new ArrayBufferSerializer();
54
+ export default instance;
@@ -0,0 +1,7 @@
1
+ import Serializer from './interfaces/Serializer.js';
2
+ declare class ArraySerializer implements Serializer {
3
+ serialize(array: unknown[]): unknown[];
4
+ deserialize(values: unknown[]): Promise<unknown[]>;
5
+ }
6
+ declare const instance: ArraySerializer;
7
+ export default instance;
@@ -0,0 +1,15 @@
1
+ import ValueSerializer from './ValueSerializer.js';
2
+ class ArraySerializer {
3
+ serialize(array) {
4
+ const values = [];
5
+ for (const value of array) {
6
+ values.push(ValueSerializer.serialize(value));
7
+ }
8
+ return values;
9
+ }
10
+ async deserialize(values) {
11
+ return await Promise.all(values.map(async (value) => await ValueSerializer.deserialize(value)));
12
+ }
13
+ }
14
+ const instance = new ArraySerializer();
15
+ export default instance;
@@ -0,0 +1,10 @@
1
+ import Serializer from './interfaces/Serializer.js';
2
+ import SerializableObject from './types/SerializableObject.js';
3
+ import SerializedClass from './types/SerializedClass.js';
4
+ declare class ClassSerializer implements Serializer {
5
+ #private;
6
+ serialize(object: SerializableObject): SerializedClass;
7
+ deserialize(object: SerializedClass): Promise<SerializableObject>;
8
+ }
9
+ declare const instance: ClassSerializer;
10
+ export default instance;
@@ -0,0 +1,74 @@
1
+ import ReflectionHelper from '../../core/reflection/ReflectionHelper.js';
2
+ import ModuleLoader from '../utils/ModuleLoader.js';
3
+ import ClassNotFound from './errors/ClassNotFound.js';
4
+ import InvalidClass from './errors/InvalidClass.js';
5
+ import InvalidPropertyType from './errors/InvalidPropertyType.js';
6
+ import ValueSerializer from './ValueSerializer.js';
7
+ class ClassSerializer {
8
+ serialize(object) {
9
+ const clazz = ReflectionHelper.getObjectClass(object);
10
+ const classFields = ReflectionHelper.getObjectFields(object);
11
+ const constructParameters = ReflectionHelper.getConstructorParameters(clazz);
12
+ const name = clazz.name;
13
+ const source = clazz.source || null;
14
+ const args = [];
15
+ const fields = {};
16
+ for (const parameter of constructParameters) {
17
+ const objectValue = object[parameter.name];
18
+ const classFieldIndex = classFields.findIndex(field => field.name === parameter.name);
19
+ if (classFieldIndex !== -1) {
20
+ classFields.splice(classFieldIndex, 1);
21
+ }
22
+ args.push(ValueSerializer.serialize(objectValue));
23
+ }
24
+ for (const field of classFields) {
25
+ if (field.canGetAndSet === false) {
26
+ continue;
27
+ }
28
+ const objectValue = object[field.name];
29
+ fields[field.name] = ValueSerializer.serialize(objectValue);
30
+ }
31
+ return { serialized: true, name: name, source: source, args: args, fields: fields };
32
+ }
33
+ async deserialize(object) {
34
+ this.#validateSerializedClass(object);
35
+ const clazz = await this.#loadClass(object.source, object.name);
36
+ if (clazz === undefined) {
37
+ throw new ClassNotFound(object.name);
38
+ }
39
+ if ((clazz instanceof Function) === false) {
40
+ throw new InvalidClass(object.name);
41
+ }
42
+ const args = await Promise.all(object.args.map(async (value) => await ValueSerializer.deserialize(value)));
43
+ const instance = ReflectionHelper.createInstance(clazz, args);
44
+ for (const name in object.fields) {
45
+ const fieldValue = object.fields[name];
46
+ instance[name] = await ValueSerializer.deserialize(fieldValue);
47
+ }
48
+ return instance;
49
+ }
50
+ #validateSerializedClass(object) {
51
+ if (typeof object.name !== 'string') {
52
+ throw new InvalidPropertyType('class', 'name', 'string');
53
+ }
54
+ if (object.source !== null && typeof object.source !== 'string') {
55
+ throw new InvalidPropertyType(`class '${object.name}'`, 'source', 'string | null');
56
+ }
57
+ if ((object.args instanceof Array) === false) {
58
+ throw new InvalidPropertyType(`class '${object.name}'`, 'args', 'Array');
59
+ }
60
+ if ((object.fields instanceof Object) === false) {
61
+ throw new InvalidPropertyType(`class '${object.name}'`, 'fields', 'Object');
62
+ }
63
+ }
64
+ async #loadClass(source, name) {
65
+ if (source === null) {
66
+ const system = typeof window !== 'undefined' ? window : global;
67
+ return system[name];
68
+ }
69
+ const module = await ModuleLoader.load(source);
70
+ return module[name] ?? module.default;
71
+ }
72
+ }
73
+ const instance = new ClassSerializer();
74
+ export default instance;
@@ -0,0 +1,9 @@
1
+ import Serializer from './interfaces/Serializer.js';
2
+ import SerializedDate from './types/SerializedDate.js';
3
+ declare class DateSerializer implements Serializer {
4
+ #private;
5
+ serialize(date: Date): SerializedDate;
6
+ deserialize(serializedDate: SerializedDate): Promise<Date>;
7
+ }
8
+ declare const instance: DateSerializer;
9
+ export default instance;
@@ -0,0 +1,21 @@
1
+ import InvalidPropertyType from './errors/InvalidPropertyType.js';
2
+ class DateSerializer {
3
+ serialize(date) {
4
+ return { serialized: true, name: 'Date', value: date.toISOString() };
5
+ }
6
+ async deserialize(serializedDate) {
7
+ this.#validateDate(serializedDate);
8
+ return new Date(serializedDate.value);
9
+ }
10
+ #validateDate(serializedDate) {
11
+ if (typeof serializedDate.value !== 'string') {
12
+ throw new InvalidPropertyType('date', 'value', 'string');
13
+ }
14
+ const date = Date.parse(serializedDate.value);
15
+ if (Number.isNaN(date)) {
16
+ throw new InvalidPropertyType('date', 'value', 'date');
17
+ }
18
+ }
19
+ }
20
+ const instance = new DateSerializer();
21
+ export default instance;
@@ -0,0 +1,9 @@
1
+ import Serializer from './interfaces/Serializer.js';
2
+ import SerializedMap from './types/SerializedMap.js';
3
+ declare class MapSerializer implements Serializer {
4
+ #private;
5
+ serialize(map: Map<unknown, unknown>): SerializedMap;
6
+ deserialize(object: SerializedMap): Promise<Map<unknown, unknown>>;
7
+ }
8
+ declare const instance: MapSerializer;
9
+ export default instance;
@@ -0,0 +1,35 @@
1
+ import InvalidPropertyType from './errors/InvalidPropertyType.js';
2
+ import ValueSerializer from './ValueSerializer.js';
3
+ class MapSerializer {
4
+ serialize(map) {
5
+ const keys = [];
6
+ const values = [];
7
+ for (const [key, value] of map) {
8
+ keys.push(ValueSerializer.serialize(key));
9
+ values.push(ValueSerializer.serialize(value));
10
+ }
11
+ return { serialized: true, name: 'Map', entries: { keys: keys, values: values } };
12
+ }
13
+ async deserialize(object) {
14
+ this.#validateSerializedMap(object);
15
+ const keys = object.entries.keys;
16
+ const values = object.entries.values;
17
+ const result = new Map();
18
+ for (let index = 0; index < keys.length; index++) {
19
+ const key = await ValueSerializer.deserialize(keys[index]);
20
+ const value = await ValueSerializer.deserialize(values[index]);
21
+ result.set(key, value);
22
+ }
23
+ return result;
24
+ }
25
+ #validateSerializedMap(object) {
26
+ if ((object.entries?.keys instanceof Array) === false) {
27
+ throw new InvalidPropertyType('map', 'entries.keys', 'Array');
28
+ }
29
+ if ((object.entries?.values instanceof Array) === false) {
30
+ throw new InvalidPropertyType('map', 'entries.values', 'Array');
31
+ }
32
+ }
33
+ }
34
+ const instance = new MapSerializer();
35
+ export default instance;
@@ -0,0 +1,9 @@
1
+ import Serializer from './interfaces/Serializer.js';
2
+ import SerializableObject from './types/SerializableObject.js';
3
+ import SerializedObject from './types/SerializedObject.js';
4
+ declare class ObjectSerializer implements Serializer {
5
+ serialize(object: SerializableObject): SerializedObject;
6
+ deserialize(object: SerializedObject): Promise<SerializableObject>;
7
+ }
8
+ declare const instance: ObjectSerializer;
9
+ export default instance;
@@ -0,0 +1,21 @@
1
+ import ValueSerializer from './ValueSerializer.js';
2
+ class ObjectSerializer {
3
+ serialize(object) {
4
+ const result = {};
5
+ for (const key in object) {
6
+ const value = object[key];
7
+ result[key] = ValueSerializer.serialize(value);
8
+ }
9
+ return result;
10
+ }
11
+ async deserialize(object) {
12
+ const result = {};
13
+ for (const key in object) {
14
+ const value = object[key];
15
+ result[key] = await ValueSerializer.deserialize(value);
16
+ }
17
+ return result;
18
+ }
19
+ }
20
+ const instance = new ObjectSerializer();
21
+ export default instance;
@@ -0,0 +1,9 @@
1
+ import Serializer from './interfaces/Serializer.js';
2
+ import SerializedSet from './types/SerializedSet.js';
3
+ declare class SetSerializer implements Serializer {
4
+ #private;
5
+ serialize(set: Set<unknown>): SerializedSet;
6
+ deserialize(object: SerializedSet): Promise<Set<unknown>>;
7
+ }
8
+ declare const instance: SetSerializer;
9
+ export default instance;
@@ -0,0 +1,23 @@
1
+ import InvalidPropertyType from './errors/InvalidPropertyType.js';
2
+ import ValueSerializer from './ValueSerializer.js';
3
+ class SetSerializer {
4
+ serialize(set) {
5
+ const values = [];
6
+ for (const value of set.values()) {
7
+ values.push(ValueSerializer.serialize(value));
8
+ }
9
+ return { serialized: true, name: 'Set', values: values };
10
+ }
11
+ async deserialize(object) {
12
+ this.#validateSerializedSet(object);
13
+ const values = await Promise.all(object.values.map(async (value) => await ValueSerializer.deserialize(value)));
14
+ return new Set([...values]);
15
+ }
16
+ #validateSerializedSet(object) {
17
+ if ((object.values instanceof Array) === false) {
18
+ throw new InvalidPropertyType('set', 'values', 'Array');
19
+ }
20
+ }
21
+ }
22
+ const instance = new SetSerializer();
23
+ export default instance;
@@ -0,0 +1,8 @@
1
+ import Serializer from './interfaces/Serializer.js';
2
+ declare class ValueSerializer implements Serializer {
3
+ #private;
4
+ serialize(value: unknown): unknown;
5
+ deserialize(value: unknown): Promise<unknown>;
6
+ }
7
+ declare const instance: ValueSerializer;
8
+ export default instance;
@@ -0,0 +1,69 @@
1
+ import ReflectionHelper from '../../core/reflection/ReflectionHelper.js';
2
+ import ArrayBufferSerializer from './ArrayBufferSerializer.js';
3
+ import ArraySerializer from './ArraySerializer.js';
4
+ import ClassSerializer from './ClassSerializer.js';
5
+ import MapSerializer from './MapSerializer.js';
6
+ import ObjectSerializer from './ObjectSerializer.js';
7
+ import SetSerializer from './SetSerializer.js';
8
+ import DateSerializer from './DateSerializer.js';
9
+ class ValueSerializer {
10
+ serialize(value) {
11
+ if (this.#isTypedArray(value)) {
12
+ return ArrayBufferSerializer.serialize(value);
13
+ }
14
+ else if (value instanceof Array) {
15
+ return ArraySerializer.serialize(value);
16
+ }
17
+ else if (value instanceof Map) {
18
+ return MapSerializer.serialize(value);
19
+ }
20
+ else if (value instanceof Set) {
21
+ return SetSerializer.serialize(value);
22
+ }
23
+ if (value instanceof Date) {
24
+ return DateSerializer.serialize(value);
25
+ }
26
+ else if (value instanceof Error) {
27
+ return ClassSerializer.serialize(value);
28
+ }
29
+ else if (value instanceof Object) {
30
+ return ReflectionHelper.isClassObject(value)
31
+ ? ClassSerializer.serialize(value)
32
+ : ObjectSerializer.serialize(value);
33
+ }
34
+ return value;
35
+ }
36
+ async deserialize(value) {
37
+ if ((value instanceof Object) === false) {
38
+ return value;
39
+ }
40
+ if (value instanceof Array) {
41
+ return await ArraySerializer.deserialize(value);
42
+ }
43
+ if (value.serialized !== true) {
44
+ return await ObjectSerializer.deserialize(value);
45
+ }
46
+ switch (value.name) {
47
+ case 'Map': return await MapSerializer.deserialize(value);
48
+ case 'Set': return await SetSerializer.deserialize(value);
49
+ case 'Date': return await DateSerializer.deserialize(value);
50
+ case 'ArrayBuffer': return await ArrayBufferSerializer.deserialize(value);
51
+ default: return await ClassSerializer.deserialize(value);
52
+ }
53
+ }
54
+ #isTypedArray(value) {
55
+ return (value instanceof Int8Array)
56
+ || (value instanceof Uint8Array)
57
+ || (value instanceof Uint8ClampedArray)
58
+ || (value instanceof Int16Array)
59
+ || (value instanceof Uint16Array)
60
+ || (value instanceof Int32Array)
61
+ || (value instanceof Uint32Array)
62
+ || (value instanceof Float32Array)
63
+ || (value instanceof Float64Array)
64
+ || (value instanceof BigInt64Array)
65
+ || (value instanceof BigUint64Array);
66
+ }
67
+ }
68
+ const instance = new ValueSerializer();
69
+ export default instance;
@@ -0,0 +1,3 @@
1
+ export default class ClassNotFound extends Error {
2
+ constructor(name: string);
3
+ }
@@ -0,0 +1,6 @@
1
+ export default class ClassNotFound extends Error {
2
+ constructor(name) {
3
+ super(`The class '${name}' could not be found`);
4
+ }
5
+ }
6
+ ClassNotFound.source = '/jitar/runtime/serialization/errors/ClassNotFound.js';