@dxos/codec-protobuf 2.33.8 → 2.33.9-dev.0628f3ab

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/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "@dxos/codec-protobuf",
3
- "version": "2.33.8",
3
+ "version": "2.33.9-dev.0628f3ab",
4
4
  "license": "MIT",
5
5
  "author": "DXOS.org",
6
6
  "main": "dist/src/index.js",
7
7
  "types": "dist/src/index.d.ts",
8
+ "browser": {
9
+ "./dist/src/index.js": "./dist/browser.js",
10
+ "node:assert": "assert"
11
+ },
8
12
  "files": [
9
13
  "dist",
10
14
  "src"
@@ -23,19 +27,20 @@
23
27
  "dependencies": {
24
28
  "assert": "^2.0.0",
25
29
  "debug": "^4.3.3",
26
- "lodash.merge": "^4.6.2",
27
- "protobufjs": "^6.9.0"
30
+ "lodash.merge": "^4.6.2"
28
31
  },
29
32
  "devDependencies": {
30
33
  "@dxos/eslint-plugin": "~1.0.34",
31
- "@types/assert": "^1.5.4",
32
34
  "@types/debug": "^4.1.7",
33
- "@types/jest": "^26.0.7",
35
+ "@types/jest": "^28.1.6",
34
36
  "@types/lodash.merge": "^4.6.6",
35
37
  "@types/node": "^16.11.27",
38
+ "esbuild": "^0.14.49",
39
+ "esbuild-node-externals": "^1.4.1",
36
40
  "eslint": "^7.12.1",
37
- "jest": "^26.6.3",
38
- "ts-jest": "^26.5.6",
41
+ "jest": "^28.1.3",
42
+ "protobufjs": "^6.9.0",
43
+ "ts-jest": "^28.0.7",
39
44
  "ts-node": "^10.8.2",
40
45
  "typescript": "^4.7.2"
41
46
  },
@@ -43,7 +48,7 @@
43
48
  "access": "public"
44
49
  },
45
50
  "scripts": {
46
- "build": "tsc",
51
+ "build": "tsc && ts-node build.ts",
47
52
  "build:test": "pnpm run build && pnpm run lint && pnpm run test",
48
53
  "lint": "eslint '{src,test}/**/*.ts'",
49
54
  "test": "jest 2>&1"
@@ -0,0 +1,9 @@
1
+ //
2
+ // Copyright 2021 DXOS.org
3
+ //
4
+
5
+ import * as protobuf from 'protobufjs';
6
+
7
+ export const encodeProtobuf = (root: protobuf.Root): string => JSON.stringify(root.toJSON());
8
+
9
+ export const decodeProtobuf = (json: string): protobuf.Root => protobuf.Root.fromJSON(JSON.parse(json));
package/src/index.ts CHANGED
@@ -5,7 +5,9 @@
5
5
  export * from './buffer-patch';
6
6
  export * from './codec';
7
7
  export * from './common';
8
+ export * from './encoding';
8
9
  export * from './interface';
10
+ export * from './mapping';
9
11
  export * from './sanitizer';
10
12
  export * from './schema';
11
13
  export * from './service';
package/src/mapping.ts CHANGED
@@ -2,6 +2,9 @@
2
2
  // Copyright 2020 DXOS.org
3
3
  //
4
4
 
5
+ import assert from 'node:assert';
6
+ import protobufjs from 'protobufjs';
7
+
5
8
  import { Substitutions } from './common';
6
9
 
7
10
  export type MapingDescriptors = Partial<Record<string, (value: any, ...extraArgs: any) => any>>
@@ -23,3 +26,60 @@ export const createMappingDescriptors = (substitutions: Substitutions): Bidirect
23
26
  decode
24
27
  };
25
28
  };
29
+
30
+ export type FieldMapper = (value: any, typeName: string) => Promise<any>;
31
+
32
+ export const mapMessage = async (type: protobufjs.Type, mapper: FieldMapper, obj: any) => {
33
+ const res: any = {};
34
+ for (const field of type.fieldsArray) {
35
+ if (!(field.name in obj)) {
36
+ continue;
37
+ }
38
+ res[field.name] = await mapField(field, mapper, obj[field.name]);
39
+ }
40
+
41
+ return res;
42
+ };
43
+
44
+ const mapField = async (field: protobufjs.Field, mapper: FieldMapper, value: any) => {
45
+ if (!field.required && (value === null || value === undefined)) {
46
+ return value;
47
+ } else if (field.repeated) {
48
+ return await Promise.all(value.map((value: any) => mapScalarField(field, mapper, value)));
49
+ } else if (field.map) {
50
+ assert(field instanceof protobufjs.MapField);
51
+ return await asyncObjectMap((value) => mapScalarField(field, mapper, value), value);
52
+ } else {
53
+ return mapScalarField(field, mapper, value);
54
+ }
55
+ };
56
+
57
+ const mapScalarField = async (field: protobufjs.Field, mapper: FieldMapper, value: any) => {
58
+ if (!field.resolved) {
59
+ field.resolve();
60
+ }
61
+
62
+ const typeName = field.resolvedType?.fullName.slice(1); // Name of the protobuf message type if the field type is not primitive.
63
+ if (typeName) {
64
+ return await mapper(value, typeName);
65
+ }
66
+
67
+ if (field.resolvedType && field.resolvedType instanceof protobufjs.Type) {
68
+ return await mapMessage(field.resolvedType, mapper, value);
69
+ }
70
+
71
+ return value;
72
+ };
73
+
74
+ const asyncObjectMap = async <K extends keyof any, T, U>(
75
+ map: (value: T, key: K) => Promise<U>,
76
+ record: Record<K, T>
77
+ ): Promise<Record<K, U>> => {
78
+ const res: Record<K, U> = {} as any;
79
+
80
+ await Promise.all(Object.entries(record).map(async ([key, value]) => {
81
+ res[key as keyof typeof res] = await map(value as T, key as K);
82
+ }));
83
+
84
+ return res;
85
+ };
@@ -2,7 +2,7 @@
2
2
  // Copyright 2022 DXOS.org
3
3
  //
4
4
 
5
- import assert from 'assert';
5
+ import assert from 'node:assert';
6
6
  import * as pb from 'protobufjs';
7
7
 
8
8
  import { MapingDescriptors } from '../mapping';
package/src/service.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  // Copyright 2021 DXOS.org
3
3
  //
4
4
 
5
- import assert from 'assert';
5
+ import assert from 'node:assert';
6
6
  import pb from 'protobufjs';
7
7
 
8
8
  import type { Schema } from './schema';
package/src/stream.ts CHANGED
@@ -2,8 +2,8 @@
2
2
  // Copyright 2021 DXOS.org
3
3
  //
4
4
 
5
- import assert from 'assert';
6
5
  import debug from 'debug';
6
+ import assert from 'node:assert';
7
7
 
8
8
  const log = debug('dxos:codec-protobuf:stream');
9
9