@rocketmq/serializer 0.1.0

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.
@@ -0,0 +1,21 @@
1
+
2
+
3
+ > @rocketmq/serializer@0.1.0 build /home/edilson/learnspace/rocketmq-broker/client-ts/packages/serializer
4
+ > tsup
5
+
6
+ CLI Building entry: src/index.ts
7
+ CLI Using tsconfig: tsconfig.json
8
+ CLI tsup v8.5.1
9
+ CLI Using tsup config: /home/edilson/learnspace/rocketmq-broker/client-ts/packages/serializer/tsup.config.ts
10
+ CLI Target: es2022
11
+ CLI Cleaning output folder
12
+ ESM Build start
13
+ CJS Build start
14
+ CJS dist/index.cjs 1.25 KB
15
+ CJS ⚡️ Build success in 10ms
16
+ ESM dist/index.js 248.00 B
17
+ ESM ⚡️ Build success in 10ms
18
+ DTS Build start
19
+ DTS ⚡️ Build success in 1037ms
20
+ DTS dist/index.d.ts 1.17 KB
21
+ DTS dist/index.d.cts 1.17 KB
package/dist/index.cjs ADDED
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ JsonSerializer: () => JsonSerializer
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+
27
+ // src/json.ts
28
+ var JsonSerializer = class {
29
+ contentType = "application/json";
30
+ serialize(value) {
31
+ return Buffer.from(JSON.stringify(value));
32
+ }
33
+ deserialize(buf) {
34
+ return JSON.parse(buf.toString());
35
+ }
36
+ };
37
+ // Annotate the CommonJS export names for ESM import in node:
38
+ 0 && (module.exports = {
39
+ JsonSerializer
40
+ });
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Pluggable serialization interface.
3
+ *
4
+ * Decouples the SDK from any specific encoding format so users can
5
+ * swap JSON for Protobuf, Avro, or custom encodings without touching
6
+ * the publish/consume pipeline.
7
+ *
8
+ * Usage:
9
+ * class MySerializer implements Serializer { ... }
10
+ * const mq = await connect({ serializer: new MySerializer() });
11
+ */
12
+ interface Serializer {
13
+ /** MIME content type sent in AMQP message properties. */
14
+ readonly contentType: string;
15
+ /** Encodes a value to a Buffer for AMQP transmission. */
16
+ serialize(value: unknown): Buffer;
17
+ /** Decodes a Buffer back into a structured value. */
18
+ deserialize(buf: Buffer): unknown;
19
+ }
20
+
21
+ /**
22
+ * JSON serializer — default encoding for RocketMQ SDK.
23
+ *
24
+ * Produces `application/json` payloads that the Rust broker validates
25
+ * by checking all proto schema fields are present in the JSON object.
26
+ *
27
+ * Usage:
28
+ * const s = new JsonSerializer();
29
+ * const buf = s.serialize({ id: "1" });
30
+ */
31
+
32
+ declare class JsonSerializer implements Serializer {
33
+ readonly contentType = "application/json";
34
+ serialize(value: unknown): Buffer;
35
+ deserialize(buf: Buffer): unknown;
36
+ }
37
+
38
+ export { JsonSerializer, type Serializer };
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Pluggable serialization interface.
3
+ *
4
+ * Decouples the SDK from any specific encoding format so users can
5
+ * swap JSON for Protobuf, Avro, or custom encodings without touching
6
+ * the publish/consume pipeline.
7
+ *
8
+ * Usage:
9
+ * class MySerializer implements Serializer { ... }
10
+ * const mq = await connect({ serializer: new MySerializer() });
11
+ */
12
+ interface Serializer {
13
+ /** MIME content type sent in AMQP message properties. */
14
+ readonly contentType: string;
15
+ /** Encodes a value to a Buffer for AMQP transmission. */
16
+ serialize(value: unknown): Buffer;
17
+ /** Decodes a Buffer back into a structured value. */
18
+ deserialize(buf: Buffer): unknown;
19
+ }
20
+
21
+ /**
22
+ * JSON serializer — default encoding for RocketMQ SDK.
23
+ *
24
+ * Produces `application/json` payloads that the Rust broker validates
25
+ * by checking all proto schema fields are present in the JSON object.
26
+ *
27
+ * Usage:
28
+ * const s = new JsonSerializer();
29
+ * const buf = s.serialize({ id: "1" });
30
+ */
31
+
32
+ declare class JsonSerializer implements Serializer {
33
+ readonly contentType = "application/json";
34
+ serialize(value: unknown): Buffer;
35
+ deserialize(buf: Buffer): unknown;
36
+ }
37
+
38
+ export { JsonSerializer, type Serializer };
package/dist/index.js ADDED
@@ -0,0 +1,13 @@
1
+ // src/json.ts
2
+ var JsonSerializer = class {
3
+ contentType = "application/json";
4
+ serialize(value) {
5
+ return Buffer.from(JSON.stringify(value));
6
+ }
7
+ deserialize(buf) {
8
+ return JSON.parse(buf.toString());
9
+ }
10
+ };
11
+ export {
12
+ JsonSerializer
13
+ };
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@rocketmq/serializer",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": {
7
+ "import": "./dist/index.js",
8
+ "types": "./dist/index.d.ts"
9
+ }
10
+ },
11
+ "scripts": {
12
+ "build": "tsup",
13
+ "test": "vitest run"
14
+ }
15
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export type { Serializer } from './types.js';
2
+ export { JsonSerializer } from './json.js';
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Tests for JsonSerializer.
3
+ *
4
+ * Covers serialize/deserialize round-trip, content type,
5
+ * and edge cases (empty object, nested, arrays).
6
+ */
7
+
8
+ import { describe, it, expect } from 'vitest';
9
+ import { JsonSerializer } from './json.js';
10
+
11
+ describe('JsonSerializer', () => {
12
+ const serializer = new JsonSerializer();
13
+
14
+ it('has content type application/json', () => {
15
+ expect(serializer.contentType).toBe('application/json');
16
+ });
17
+
18
+ it('serializes an object to a Buffer', () => {
19
+ const buf = serializer.serialize({ id: '1', qty: 5 });
20
+ expect(Buffer.isBuffer(buf)).toBe(true);
21
+ expect(buf.toString()).toBe('{"id":"1","qty":5}');
22
+ });
23
+
24
+ it('deserializes a Buffer back to the original object', () => {
25
+ const original = { name: 'Alice', age: 30 };
26
+ const buf = serializer.serialize(original);
27
+ const result = serializer.deserialize(buf);
28
+ expect(result).toEqual(original);
29
+ });
30
+
31
+ it('round-trips empty object', () => {
32
+ const buf = serializer.serialize({});
33
+ expect(serializer.deserialize(buf)).toEqual({});
34
+ });
35
+
36
+ it('round-trips nested objects', () => {
37
+ const nested = { a: { b: { c: 1 } } };
38
+ const buf = serializer.serialize(nested);
39
+ expect(serializer.deserialize(buf)).toEqual(nested);
40
+ });
41
+
42
+ it('round-trips arrays', () => {
43
+ const arr = [1, 2, 3];
44
+ const buf = serializer.serialize(arr);
45
+ expect(serializer.deserialize(buf)).toEqual(arr);
46
+ });
47
+
48
+ it('serializes null and primitives', () => {
49
+ expect(serializer.deserialize(serializer.serialize(null))).toBeNull();
50
+ expect(serializer.deserialize(serializer.serialize(42))).toBe(42);
51
+ expect(serializer.deserialize(serializer.serialize('hello'))).toBe('hello');
52
+ expect(serializer.deserialize(serializer.serialize(true))).toBe(true);
53
+ });
54
+ });
package/src/json.ts ADDED
@@ -0,0 +1,24 @@
1
+ /**
2
+ * JSON serializer — default encoding for RocketMQ SDK.
3
+ *
4
+ * Produces `application/json` payloads that the Rust broker validates
5
+ * by checking all proto schema fields are present in the JSON object.
6
+ *
7
+ * Usage:
8
+ * const s = new JsonSerializer();
9
+ * const buf = s.serialize({ id: "1" });
10
+ */
11
+
12
+ import type { Serializer } from './types.js';
13
+
14
+ export class JsonSerializer implements Serializer {
15
+ readonly contentType = 'application/json';
16
+
17
+ serialize(value: unknown): Buffer {
18
+ return Buffer.from(JSON.stringify(value));
19
+ }
20
+
21
+ deserialize(buf: Buffer): unknown {
22
+ return JSON.parse(buf.toString());
23
+ }
24
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Tests for Serializer interface compliance.
3
+ *
4
+ * Verifies that JsonSerializer satisfies the Serializer contract.
5
+ */
6
+
7
+ import { describe, it, expect } from 'vitest';
8
+ import { JsonSerializer } from './json.js';
9
+ import type { Serializer } from './types.js';
10
+
11
+ describe('Serializer interface', () => {
12
+ it('JsonSerializer satisfies the Serializer interface', () => {
13
+ const s: Serializer = new JsonSerializer();
14
+ expect(s.contentType).toBeDefined();
15
+ expect(typeof s.serialize).toBe('function');
16
+ expect(typeof s.deserialize).toBe('function');
17
+ });
18
+ });
package/src/types.ts ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Pluggable serialization interface.
3
+ *
4
+ * Decouples the SDK from any specific encoding format so users can
5
+ * swap JSON for Protobuf, Avro, or custom encodings without touching
6
+ * the publish/consume pipeline.
7
+ *
8
+ * Usage:
9
+ * class MySerializer implements Serializer { ... }
10
+ * const mq = await connect({ serializer: new MySerializer() });
11
+ */
12
+ export interface Serializer {
13
+ /** MIME content type sent in AMQP message properties. */
14
+ readonly contentType: string;
15
+
16
+ /** Encodes a value to a Buffer for AMQP transmission. */
17
+ serialize(value: unknown): Buffer;
18
+
19
+ /** Decodes a Buffer back into a structured value. */
20
+ deserialize(buf: Buffer): unknown;
21
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "rootDir": "src"
6
+ },
7
+ "include": ["src"]
8
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { defineConfig } from "tsup";
2
+
3
+ export default defineConfig({
4
+ entry: ["src/index.ts"],
5
+ format: ["esm", "cjs"],
6
+ dts: true,
7
+ clean: true,
8
+ });