@nmtjs/json-format 0.14.4 → 0.15.0-beta.1

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.
package/LICENSE.md CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2024 Denis Ilchyshyn
1
+ Copyright (c) 2025 Denys Ilchyshyn
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
4
 
package/README.md CHANGED
@@ -6,4 +6,4 @@
6
6
  - binary data streaming and event subscriptions
7
7
  - contract-based API
8
8
  - end-to-end type safety
9
- - CPU-intensive task execution on separate workers
9
+ - CPU-intensive task execution on separate workers
package/package.json CHANGED
@@ -2,33 +2,27 @@
2
2
  "name": "@nmtjs/json-format",
3
3
  "type": "module",
4
4
  "exports": {
5
- "./client": {
6
- "types": "./dist/client.d.ts",
7
- "import": "./dist/client.js",
8
- "module-sync": "./dist/client.js"
9
- },
10
- "./server": {
11
- "types": "./dist/server.d.ts",
12
- "import": "./dist/server.js",
13
- "module-sync": "./dist/server.js"
14
- }
5
+ "./client": "./dist/client.js",
6
+ "./server": "./dist/server.js"
15
7
  },
16
8
  "dependencies": {
17
- "@nmtjs/common": "0.14.4",
18
- "@nmtjs/protocol": "0.14.4"
9
+ "@nmtjs/common": "0.15.0-beta.1",
10
+ "@nmtjs/protocol": "0.15.0-beta.1"
19
11
  },
20
12
  "peerDependencies": {
21
- "@nmtjs/common": "0.14.4",
22
- "@nmtjs/protocol": "0.14.4"
13
+ "@nmtjs/common": "0.15.0-beta.1",
14
+ "@nmtjs/protocol": "0.15.0-beta.1"
23
15
  },
24
16
  "files": [
25
17
  "dist",
26
18
  "LICENSE.md",
27
19
  "README.md"
28
20
  ],
29
- "version": "0.14.4",
21
+ "version": "0.15.0-beta.1",
30
22
  "scripts": {
23
+ "clean-build": "rm -rf ./dist",
31
24
  "build": "tsc",
25
+ "dev": "tsc --watch",
32
26
  "type-check": "tsc --noEmit"
33
27
  }
34
28
  }
package/dist/client.d.ts DELETED
@@ -1,50 +0,0 @@
1
- import type { DecodeRPCContext, EncodeRPCContext, ProtocolRPC } from '@nmtjs/protocol';
2
- import type { ProtocolClientBlobStream, ProtocolServerBlobStream } from '@nmtjs/protocol/client';
3
- import { BaseClientFormat } from '@nmtjs/protocol/client';
4
- import type { StreamsMetadata } from './common.ts';
5
- /**
6
- * Custom JSON encoding format with support for Neemata streams.
7
- */
8
- export declare class JsonFormat extends BaseClientFormat {
9
- contentType: string;
10
- encode(data: any): ArrayBuffer;
11
- encodeRPC(rpc: ProtocolRPC, context: EncodeRPCContext): {
12
- buffer: ArrayBuffer;
13
- streams: Record<number, ProtocolClientBlobStream>;
14
- };
15
- decode(data: ArrayBuffer): any;
16
- decodeRPC(buffer: ArrayBuffer, context: DecodeRPCContext): {
17
- callId: number;
18
- error: import("@nmtjs/protocol").BaseProtocolError;
19
- result?: undefined;
20
- streams?: undefined;
21
- } | {
22
- callId: number;
23
- result: any;
24
- streams: Record<number, ProtocolServerBlobStream>;
25
- error?: undefined;
26
- };
27
- }
28
- /**
29
- * Standard JSON encoding format with no Neemata streams support.
30
- */
31
- export declare class StandardJsonFormat extends BaseClientFormat {
32
- contentType: string;
33
- encode(data: any): ArrayBuffer;
34
- encodeRPC(rpc: ProtocolRPC): {
35
- buffer: ArrayBuffer;
36
- streams: Record<number, ProtocolClientBlobStream>;
37
- };
38
- decode(data: ArrayBuffer): any;
39
- decodeRPC(buffer: ArrayBuffer): {
40
- callId: number;
41
- error: import("@nmtjs/protocol").BaseProtocolError;
42
- result?: undefined;
43
- streams?: undefined;
44
- } | {
45
- callId: number;
46
- result: StreamsMetadata;
47
- streams: Record<number, ProtocolServerBlobStream>;
48
- error?: undefined;
49
- };
50
- }
package/dist/client.js DELETED
@@ -1,93 +0,0 @@
1
- import { decodeText, encodeText, ProtocolBlob } from '@nmtjs/protocol';
2
- import { BaseClientFormat } from '@nmtjs/protocol/client';
3
- import { deserializeStreamId, isStreamId, serializeStreamId } from "./common.js";
4
- /**
5
- * Custom JSON encoding format with support for Neemata streams.
6
- */
7
- export class JsonFormat extends BaseClientFormat {
8
- contentType = 'application/x-neemata-json';
9
- encode(data) {
10
- return encodeText(JSON.stringify(data));
11
- }
12
- encodeRPC(rpc, context) {
13
- const { callId, procedure } = rpc;
14
- const streamsMetadata = {};
15
- const streams = {};
16
- const replacer = (_key, value) => {
17
- if (value instanceof ProtocolBlob) {
18
- const stream = context.addStream(value);
19
- streamsMetadata[stream.id] = stream.metadata;
20
- streams[stream.id] = stream;
21
- return serializeStreamId(stream.id);
22
- }
23
- return value;
24
- };
25
- const payload = typeof rpc.payload === 'undefined'
26
- ? undefined
27
- : JSON.stringify(rpc.payload, replacer);
28
- const buffer = typeof payload === 'undefined'
29
- ? this.encode([
30
- callId,
31
- procedure,
32
- streamsMetadata,
33
- ])
34
- : this.encode([
35
- callId,
36
- procedure,
37
- streamsMetadata,
38
- payload,
39
- ]);
40
- return { buffer, streams };
41
- }
42
- decode(data) {
43
- return JSON.parse(decodeText(data));
44
- }
45
- decodeRPC(buffer, context) {
46
- const streams = {};
47
- const [callId, error, streamsMetadata, payload] = this.decode(buffer);
48
- if (error)
49
- return { callId, error };
50
- else {
51
- const replacer = (_key, value) => {
52
- if (typeof value === 'string' && isStreamId(value)) {
53
- const id = deserializeStreamId(value);
54
- const metadata = streamsMetadata[id];
55
- const stream = context.addStream(id, callId, metadata);
56
- streams[id] = stream;
57
- return stream;
58
- }
59
- return value;
60
- };
61
- const result = typeof payload === 'undefined'
62
- ? undefined
63
- : JSON.parse(payload, replacer);
64
- return { callId, result, streams };
65
- }
66
- }
67
- }
68
- /**
69
- * Standard JSON encoding format with no Neemata streams support.
70
- */
71
- export class StandardJsonFormat extends BaseClientFormat {
72
- contentType = 'application/json';
73
- encode(data) {
74
- return encodeText(JSON.stringify(data));
75
- }
76
- encodeRPC(rpc) {
77
- const { callId, procedure, payload } = rpc;
78
- const streams = {};
79
- const buffer = this.encode([callId, procedure, payload]);
80
- return { buffer, streams };
81
- }
82
- decode(data) {
83
- return JSON.parse(decodeText(data));
84
- }
85
- decodeRPC(buffer) {
86
- const streams = {};
87
- const [callId, error, result] = this.decode(buffer);
88
- if (error)
89
- return { callId, error };
90
- else
91
- return { callId, result, streams };
92
- }
93
- }
package/dist/common.d.ts DELETED
@@ -1,12 +0,0 @@
1
- import type { BaseProtocolError, ProtocolBlobMetadata } from '@nmtjs/protocol';
2
- export declare const serializeStreamId: (id: number) => string;
3
- export declare const deserializeStreamId: (value: string) => number;
4
- export declare const isStreamId: (value: any) => boolean | 0;
5
- export type StreamsMetadata = Record<number, ProtocolBlobMetadata>;
6
- export type ClientEncodedRPC = [
7
- callId: number,
8
- procedure: string,
9
- streams: StreamsMetadata,
10
- payload?: any
11
- ];
12
- export type ServerEncodedRPC = [callId: number, error: BaseProtocolError] | [callId: number, error: null, streams: StreamsMetadata, payload?: any];
package/dist/common.js DELETED
@@ -1,14 +0,0 @@
1
- // TODO: is this a good way to serialize streams within json?
2
- const STREAM_SERIALIZE_KEY = '%neemata:stream:%\f';
3
- export const serializeStreamId = (id) => {
4
- return `${STREAM_SERIALIZE_KEY}${id}`;
5
- };
6
- export const deserializeStreamId = (value) => {
7
- const streamId = value.slice(STREAM_SERIALIZE_KEY.length);
8
- return Number.parseInt(streamId);
9
- };
10
- export const isStreamId = (value) => {
11
- return (typeof value === 'string' &&
12
- value.length &&
13
- value.startsWith(STREAM_SERIALIZE_KEY));
14
- };
package/dist/server.d.ts DELETED
@@ -1,33 +0,0 @@
1
- import type { DecodeRPCContext, EncodeRPCContext, ProtocolRPCResponse } from '@nmtjs/protocol';
2
- import { BaseServerFormat } from '@nmtjs/protocol/server';
3
- import type { StreamsMetadata } from './common.ts';
4
- /**
5
- * Custom JSON encoding format with Neemata streams support.
6
- */
7
- export declare class JsonFormat extends BaseServerFormat {
8
- contentType: string;
9
- accept: string[];
10
- encode(data: any): ArrayBuffer;
11
- encodeRPC(rpc: ProtocolRPCResponse, context: EncodeRPCContext): ArrayBuffer;
12
- decode(data: ArrayBuffer): any;
13
- decodeRPC(buffer: ArrayBuffer, context: DecodeRPCContext): {
14
- callId: number;
15
- procedure: string;
16
- payload: any;
17
- };
18
- }
19
- /**
20
- * Standard JSON encoding format with no Neemata streams support.
21
- */
22
- export declare class StandardJsonFormat extends BaseServerFormat {
23
- contentType: string;
24
- accept: string[];
25
- encode(data: any): ArrayBuffer;
26
- encodeRPC(rpc: ProtocolRPCResponse): ArrayBuffer;
27
- decode(buffer: ArrayBuffer): any;
28
- decodeRPC(buffer: ArrayBuffer): {
29
- callId: number;
30
- procedure: string;
31
- payload: StreamsMetadata;
32
- };
33
- }
package/dist/server.js DELETED
@@ -1,77 +0,0 @@
1
- import { decodeText, encodeText, ProtocolBlob } from '@nmtjs/protocol';
2
- import { BaseServerFormat } from '@nmtjs/protocol/server';
3
- import { deserializeStreamId, isStreamId, serializeStreamId } from "./common.js";
4
- /**
5
- * Custom JSON encoding format with Neemata streams support.
6
- */
7
- export class JsonFormat extends BaseServerFormat {
8
- contentType = 'application/x-neemata-json';
9
- accept = ['application/x-neemata-json'];
10
- encode(data) {
11
- return encodeText(JSON.stringify(data));
12
- }
13
- encodeRPC(rpc, context) {
14
- const { callId, error } = rpc;
15
- if (error)
16
- return this.encode([callId, error]);
17
- else {
18
- const streams = {};
19
- const replacer = (_key, value) => {
20
- if (value instanceof ProtocolBlob) {
21
- const stream = context.addStream(value);
22
- streams[stream.id] = stream.metadata;
23
- return serializeStreamId(stream.id);
24
- }
25
- return value;
26
- };
27
- const isUndefined = typeof rpc.result === 'undefined';
28
- const payload = JSON.stringify(rpc.result, replacer);
29
- return this.encode(isUndefined
30
- ? [callId, null, streams]
31
- : [callId, null, streams, payload]);
32
- }
33
- }
34
- decode(data) {
35
- return JSON.parse(decodeText(data));
36
- }
37
- decodeRPC(buffer, context) {
38
- const [callId, procedure, streams, formatPayload] = this.decode(buffer);
39
- const replacer = (_key, value) => {
40
- if (typeof value === 'string' && isStreamId(value)) {
41
- const id = deserializeStreamId(value);
42
- const metadata = streams[id];
43
- return context.addStream(id, callId, metadata);
44
- }
45
- return value;
46
- };
47
- const payload = typeof formatPayload === 'undefined'
48
- ? undefined
49
- : JSON.parse(formatPayload, replacer);
50
- return { callId, procedure, payload };
51
- }
52
- }
53
- /**
54
- * Standard JSON encoding format with no Neemata streams support.
55
- */
56
- export class StandardJsonFormat extends BaseServerFormat {
57
- contentType = 'application/json';
58
- accept = ['application/json', 'application/vnd.api+json'];
59
- encode(data) {
60
- return encodeText(JSON.stringify(data));
61
- }
62
- encodeRPC(rpc) {
63
- const { callId, error } = rpc;
64
- if (error)
65
- return this.encode([callId, error, null]);
66
- else {
67
- return this.encode([callId, null, rpc.result]);
68
- }
69
- }
70
- decode(buffer) {
71
- return JSON.parse(decodeText(buffer));
72
- }
73
- decodeRPC(buffer) {
74
- const [callId, procedure, payload] = this.decode(buffer);
75
- return { callId, procedure, payload };
76
- }
77
- }