Skip to content

Commit f5975d0

Browse files
authored
Add samples encoder / decoder libraries(#38)
* add samples-encoder * change tsconfig * add SfuSampleEncoder * add decoder library * use decodeFromBytes * readme change
1 parent 8ccf4eb commit f5975d0

File tree

167 files changed

+31170
-1618
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

167 files changed

+31170
-1618
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Release Samples Decoder Library
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
branches:
8+
- main
9+
10+
jobs:
11+
publish-npm:
12+
runs-on: ubuntu-latest
13+
env:
14+
working-directory: ./npm-samples-decoder
15+
steps:
16+
- uses: actions/checkout@v2
17+
- uses: actions/setup-node@v1
18+
with:
19+
node-version: 16
20+
21+
- run: npm install -g yarn
22+
- name: install packages
23+
run: yarn
24+
working-directory: ${{env.working-directory}}
25+
26+
- name: Setup Custom Contexts
27+
shell: bash
28+
run: |
29+
echo "##[set-output name=branch;]$(echo ${GITHUB_REF##*/})"
30+
echo "##[set-output name=version;]$(cat ./package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[",]//g' | tr -d '[[:space:]]')"
31+
echo "##[set-output name=short_sha;]$(git rev-parse --short HEAD)"
32+
id: job_context
33+
working-directory: ${{env.working-directory}}
34+
35+
- name: Make snapshot version
36+
if: steps.job_context.outputs.branch != 'main'
37+
run: |
38+
npm version prerelease --preid=${{ steps.job_context.outputs.short_sha }} --git-tag-version false
39+
working-directory: ${{env.working-directory}}
40+
41+
- name: Publish package to latest
42+
if: steps.job_context.outputs.branch == 'main'
43+
working-directory: ${{env.working-directory}}
44+
run: |
45+
npm config set //registry.npmjs.org/:_authToken ${NPM_TOKEN}
46+
npm publish --ignore-scripts --access public
47+
env:
48+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
49+
50+
- name: Publish package to branch-tag
51+
if: steps.job_context.outputs.branch != 'main'
52+
working-directory: ${{env.working-directory}}
53+
run: |
54+
npm config set //registry.npmjs.org/:_authToken ${NPM_TOKEN}
55+
npm publish --ignore-scripts --access public --tag $(echo "develop-${{ steps.job_context.outputs.version }}-rc" | sed 's/[.]//g')
56+
env:
57+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
58+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Release Samples Encoder Library
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
branches:
8+
- main
9+
10+
jobs:
11+
publish-npm:
12+
runs-on: ubuntu-latest
13+
env:
14+
working-directory: ./npm-samples-encoder
15+
steps:
16+
- uses: actions/checkout@v2
17+
- uses: actions/setup-node@v1
18+
with:
19+
node-version: 16
20+
21+
- run: npm install -g yarn
22+
- name: install packages
23+
run: yarn
24+
working-directory: ${{env.working-directory}}
25+
26+
- name: Setup Custom Contexts
27+
shell: bash
28+
run: |
29+
echo "##[set-output name=branch;]$(echo ${GITHUB_REF##*/})"
30+
echo "##[set-output name=version;]$(cat ./package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[",]//g' | tr -d '[[:space:]]')"
31+
echo "##[set-output name=short_sha;]$(git rev-parse --short HEAD)"
32+
id: job_context
33+
working-directory: ${{env.working-directory}}
34+
35+
- name: Make snapshot version
36+
if: steps.job_context.outputs.branch != 'main'
37+
run: |
38+
npm version prerelease --preid=${{ steps.job_context.outputs.short_sha }} --git-tag-version false
39+
working-directory: ${{env.working-directory}}
40+
41+
- name: Publish package to latest
42+
if: steps.job_context.outputs.branch == 'main'
43+
working-directory: ${{env.working-directory}}
44+
run: |
45+
npm config set //registry.npmjs.org/:_authToken ${NPM_TOKEN}
46+
npm publish --ignore-scripts --access public
47+
env:
48+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
49+
50+
- name: Publish package to branch-tag
51+
if: steps.job_context.outputs.branch != 'main'
52+
working-directory: ${{env.working-directory}}
53+
run: |
54+
npm config set //registry.npmjs.org/:_authToken ${NPM_TOKEN}
55+
npm publish --ignore-scripts --access public --tag $(echo "develop-${{ steps.job_context.outputs.version }}-rc" | sed 's/[.]//g')
56+
env:
57+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
58+

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
2-
keys/
2+
keys/
3+
temp/

NpmSamplesDecoderLib.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import fs from "fs";
2+
import path from "path";
3+
// import * as typedoc from "typedoc";
4+
5+
const PACKAGE_JSON_FILE = "package.json";
6+
7+
export class NpmSamplesDecoderLib {
8+
constructor() {
9+
this._basePath = './npm-samples-decoder';
10+
this._srcPath = path.join(this._basePath, "src");
11+
this._inputSamplesPath = path.join(this._srcPath, "InputSamples.ts");
12+
this._outputSamplesPath = path.join(this._srcPath, "OutputSamples.ts");
13+
this._samplesTsCode = undefined;
14+
this._protobufSamplesTsCode = undefined;
15+
}
16+
17+
addSamplesTsCode(tsCode) {
18+
this._samplesTsCode = tsCode;
19+
}
20+
21+
addSamplesProtobufTsCode(protobufTsCode) {
22+
this._protobufSamplesTsCode = protobufTsCode;
23+
}
24+
25+
set version(value) {
26+
this._version = value
27+
}
28+
29+
make() {
30+
fs.writeFileSync(this._inputSamplesPath, this._protobufSamplesTsCode);
31+
fs.writeFileSync(this._outputSamplesPath, this._samplesTsCode);
32+
33+
if (this._version) {
34+
const packagePath = path.join(this._basePath, PACKAGE_JSON_FILE);
35+
let packageText = fs.readFileSync(packagePath);
36+
const packageJson = JSON.parse(packageText);
37+
packageJson["version"] = this._version;
38+
packageText = JSON.stringify(packageJson, null, 2);
39+
fs.writeFileSync(packagePath, packageText);
40+
}
41+
}
42+
43+
clear() {
44+
if (fs.existsSync(this._inputSamplesPath)) {
45+
fs.rmSync(this._inputSamplesPath);
46+
}
47+
if (fs.existsSync(this._outputSamplesPath)) {
48+
fs.rmSync(this._outputSamplesPath);
49+
}
50+
}
51+
}

NpmSamplesEncoderLib.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import fs from "fs";
2+
import path from "path";
3+
// import * as typedoc from "typedoc";
4+
5+
const PACKAGE_JSON_FILE = "package.json";
6+
7+
export class NpmSampleEncoderLib {
8+
constructor() {
9+
this._basePath = './npm-samples-encoder';
10+
this._srcPath = path.join(this._basePath, "src");
11+
this._inputSamplesPath = path.join(this._srcPath, "InputSamples.ts");
12+
this._outputSamplesPath = path.join(this._srcPath, "OutputSamples.ts");
13+
this._samplesTsCode = undefined;
14+
this._protobufSamplesTsCode = undefined;
15+
}
16+
17+
addSamplesTsCode(tsCode) {
18+
this._samplesTsCode = tsCode;
19+
}
20+
21+
addSamplesProtobufTsCode(protobufTsCode) {
22+
this._protobufSamplesTsCode = protobufTsCode;
23+
}
24+
25+
set version(value) {
26+
this._version = value
27+
}
28+
29+
make() {
30+
fs.writeFileSync(this._inputSamplesPath, this._samplesTsCode);
31+
fs.writeFileSync(this._outputSamplesPath, this._protobufSamplesTsCode);
32+
33+
if (this._version) {
34+
const packagePath = path.join(this._basePath, PACKAGE_JSON_FILE);
35+
let packageText = fs.readFileSync(packagePath);
36+
const packageJson = JSON.parse(packageText);
37+
packageJson["version"] = this._version;
38+
packageText = JSON.stringify(packageJson, null, 2);
39+
fs.writeFileSync(packagePath, packageText);
40+
}
41+
}
42+
43+
clear() {
44+
if (fs.existsSync(this._inputSamplesPath)) {
45+
fs.rmSync(this._inputSamplesPath);
46+
}
47+
if (fs.existsSync(this._outputSamplesPath)) {
48+
fs.rmSync(this._outputSamplesPath);
49+
}
50+
}
51+
}

ProtobufConverterV3.js

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11

22
export class ProtobufConverterV3 {
33

4-
static from(schema, version) {
4+
static from(schema, version, uuidFields, allOptional = false) {
55
const result = new ProtobufConverterV3(0, version);
66
result._name = schema.name;
7+
result._allOptional = allOptional;
8+
if (uuidFields) {
9+
result._uuidFields = uuidFields;
10+
}
711
for (const field of schema.fields) {
812
try {
913
result.add(field);
@@ -13,14 +17,33 @@ export class ProtobufConverterV3 {
1317
}
1418
return result;
1519
}
20+
21+
static convertEnum(name, symbols) {
22+
const enumName = name.toUpperCase();
23+
const lines = [
24+
`enum ${enumName} {`
25+
];
26+
for (let fieldNum = 0; fieldNum < symbols.length; ++fieldNum) {
27+
lines.push(`\t${symbols[fieldNum].toUpperCase()} = ${fieldNum};`)
28+
}
29+
lines.push(`}`);
30+
return {
31+
name: enumName,
32+
toLines:() => lines
33+
}
34+
}
35+
1636
constructor(level = 0, version) {
1737
this._name = undefined;
1838
this._version = version;
1939
this._level = level;
40+
this._classEnum = undefined;
41+
this._symbols = new Map();
2042
this._fields = [];
2143
this._nestedClasses = [];
2244
this._builderFields = null;
2345
this._uuidFields = new Set();
46+
this._allOptional = false;
2447
}
2548

2649
get name() {
@@ -52,16 +75,28 @@ export class ProtobufConverterV3 {
5275
}
5376
isObject = typeof type === "object";
5477
if (isObject && type.type === "enum") {
78+
this._symbols.set(field.name, type.symbols);
79+
if (!this._classEnum) {
80+
this._classEnum = `${this._name}Enum`;
81+
}
5582
this._addField({
56-
doc: field.doc ? field.doc : "" + "Possible values: " + type.symbols.join(", "),
5783
name: field.name,
58-
type: "string",
59-
required,
84+
type: this._classEnum,
6085
});
86+
87+
// const nestedClass = ProtobufConverterV3.convertEnum(field.name, type.symbols);
88+
// // console.log(type.symbols);
89+
// this._nestedClasses.push(nestedClass);
90+
// this._addField({
91+
// doc: field.doc ? field.doc : "" + "Possible values: " + type.symbols.join(", "),
92+
// name: field.name,
93+
// type: nestedClass.name,
94+
// required,
95+
// });
6196
return;
6297
}
6398
if (isObject) {
64-
const nestedClass = ProtobufConverterV3.from(type, !!this._builderFields, this._uuidFields);
99+
const nestedClass = ProtobufConverterV3.from(type, !!this._builderFields, this._uuidFields, this._allOptional);
65100
nestedClass.level = this._level + 1;
66101
// console.log("nestedBuilder", this._builderFields, !!this._builderFields);
67102
this._nestedClasses.push(nestedClass);
@@ -76,7 +111,7 @@ export class ProtobufConverterV3 {
76111
}
77112
let protoType = undefined;
78113
if (this._uuidFields.has(field.name)) {
79-
protoType = "UUID";
114+
protoType = "bytes";
80115
} else {
81116
protoType = this._mapPrimitive(type);
82117
}
@@ -123,9 +158,10 @@ export class ProtobufConverterV3 {
123158
let result = [];
124159

125160
if (isArray) result.push(`repeated`)
161+
else if (this._allOptional) result.push(`optional`);
126162
// else if (required) result.push(`required`);
127163
// else result.push(`optional`);
128-
164+
129165
result.push(type);
130166
result.push(`${name} = ${fieldNum}`);
131167

@@ -179,6 +215,9 @@ export class ProtobufConverterV3 {
179215
);
180216
}
181217
result.push(`message ${this.name} {`);
218+
if (this._classEnum) {
219+
this._nestedClasses.push(this._renderEnum());
220+
}
182221
for (const nestedClass of this._nestedClasses) {
183222
for (const nestedLine of nestedClass.toLines()) {
184223
result.push(`\t${nestedLine}`);
@@ -188,4 +227,29 @@ export class ProtobufConverterV3 {
188227
result.push(`}`);
189228
return result;
190229
}
230+
231+
_renderEnum() {
232+
const lines = [
233+
`enum ${this._classEnum} {`
234+
];
235+
let fieldNum = 0;
236+
const values = new Set();
237+
for (const [fieldName, symbols] of this._symbols.entries()) {
238+
lines.push(`\t/* For ${fieldName} */`);
239+
for (let i = 0; i < symbols.length; ++i, fieldNum) {
240+
const symbol = symbols[i].toUpperCase();
241+
if (values.has(symbol)) {
242+
continue;
243+
}
244+
values.add(symbol);
245+
lines.push(`\t${symbol} = ${fieldNum++};`)
246+
}
247+
}
248+
249+
lines.push(`}`);
250+
return {
251+
name: this._classEnum,
252+
toLines:() => lines
253+
}
254+
}
191255
}

0 commit comments

Comments
 (0)