From 36a702ae9e80d260e5fe087b46787971fa53c972 Mon Sep 17 00:00:00 2001 From: King Wang Date: Tue, 29 Jun 2021 22:18:47 +0800 Subject: [PATCH] transfer-encryption --- .../transfer-encryption/backend/.gitignore | 3 + .../backend/.vscode/launch.json | 30 ++++++ .../backend/.vscode/settings.json | 3 + .../transfer-encryption/backend/README.md | 35 +++++++ .../transfer-encryption/backend/package.json | 23 ++++ .../backend/src/api/ApiAddData.ts | 26 +++++ .../backend/src/api/ApiGetData.ts | 13 +++ .../transfer-encryption/backend/src/index.ts | 39 +++++++ .../backend/src/shared/models/EncryptUtil.ts | 17 +++ .../src/shared/protocols/PtlAddData.ts | 10 ++ .../src/shared/protocols/PtlGetData.ts | 13 +++ .../src/shared/protocols/serviceProto.ts | 98 ++++++++++++++++++ .../transfer-encryption/backend/tsconfig.json | 18 ++++ .../transfer-encryption/frontend/.gitignore | 3 + .../transfer-encryption/frontend/package.json | 24 +++++ .../frontend/public/favicon.ico | Bin 0 -> 4286 bytes .../frontend/public/index.css | 83 +++++++++++++++ .../frontend/public/index.html | 28 +++++ .../transfer-encryption/frontend/src/index.ts | 67 ++++++++++++ .../frontend/src/shared/models/EncryptUtil.ts | 17 +++ .../src/shared/protocols/PtlAddData.ts | 10 ++ .../src/shared/protocols/PtlGetData.ts | 13 +++ .../src/shared/protocols/serviceProto.ts | 98 ++++++++++++++++++ .../frontend/tsconfig.json | 23 ++++ .../frontend/webpack.config.js | 56 ++++++++++ 25 files changed, 750 insertions(+) create mode 100644 examples/transfer-encryption/backend/.gitignore create mode 100644 examples/transfer-encryption/backend/.vscode/launch.json create mode 100644 examples/transfer-encryption/backend/.vscode/settings.json create mode 100644 examples/transfer-encryption/backend/README.md create mode 100644 examples/transfer-encryption/backend/package.json create mode 100644 examples/transfer-encryption/backend/src/api/ApiAddData.ts create mode 100644 examples/transfer-encryption/backend/src/api/ApiGetData.ts create mode 100644 examples/transfer-encryption/backend/src/index.ts create mode 100644 examples/transfer-encryption/backend/src/shared/models/EncryptUtil.ts create mode 100644 examples/transfer-encryption/backend/src/shared/protocols/PtlAddData.ts create mode 100644 examples/transfer-encryption/backend/src/shared/protocols/PtlGetData.ts create mode 100644 examples/transfer-encryption/backend/src/shared/protocols/serviceProto.ts create mode 100644 examples/transfer-encryption/backend/tsconfig.json create mode 100644 examples/transfer-encryption/frontend/.gitignore create mode 100644 examples/transfer-encryption/frontend/package.json create mode 100644 examples/transfer-encryption/frontend/public/favicon.ico create mode 100644 examples/transfer-encryption/frontend/public/index.css create mode 100644 examples/transfer-encryption/frontend/public/index.html create mode 100644 examples/transfer-encryption/frontend/src/index.ts create mode 100644 examples/transfer-encryption/frontend/src/shared/models/EncryptUtil.ts create mode 100644 examples/transfer-encryption/frontend/src/shared/protocols/PtlAddData.ts create mode 100644 examples/transfer-encryption/frontend/src/shared/protocols/PtlGetData.ts create mode 100644 examples/transfer-encryption/frontend/src/shared/protocols/serviceProto.ts create mode 100644 examples/transfer-encryption/frontend/tsconfig.json create mode 100644 examples/transfer-encryption/frontend/webpack.config.js diff --git a/examples/transfer-encryption/backend/.gitignore b/examples/transfer-encryption/backend/.gitignore new file mode 100644 index 0000000..d84f0da --- /dev/null +++ b/examples/transfer-encryption/backend/.gitignore @@ -0,0 +1,3 @@ +node_modules +dist +.DS_STORE \ No newline at end of file diff --git a/examples/transfer-encryption/backend/.vscode/launch.json b/examples/transfer-encryption/backend/.vscode/launch.json new file mode 100644 index 0000000..9ba4218 --- /dev/null +++ b/examples/transfer-encryption/backend/.vscode/launch.json @@ -0,0 +1,30 @@ +{ + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "mocha current file", + "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha", + "args": [ + "${file}" + ], + "internalConsoleOptions": "openOnSessionStart", + "cwd": "${workspaceFolder}" + }, + { + "type": "node", + "request": "launch", + "name": "ts-node current file", + "protocol": "inspector", + "args": [ + "${relativeFile}" + ], + "cwd": "${workspaceRoot}", + "runtimeArgs": [ + "-r", + "ts-node/register" + ], + "internalConsoleOptions": "openOnSessionStart" + } + ] +} \ No newline at end of file diff --git a/examples/transfer-encryption/backend/.vscode/settings.json b/examples/transfer-encryption/backend/.vscode/settings.json new file mode 100644 index 0000000..00ad71f --- /dev/null +++ b/examples/transfer-encryption/backend/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "typescript.tsdk": "node_modules\\typescript\\lib" +} \ No newline at end of file diff --git a/examples/transfer-encryption/backend/README.md b/examples/transfer-encryption/backend/README.md new file mode 100644 index 0000000..7492c22 --- /dev/null +++ b/examples/transfer-encryption/backend/README.md @@ -0,0 +1,35 @@ +# TSRPC Server + +## Run +### Local Dev Server +``` +npm run dev +``` + + + +### Build +``` +npm run build +``` + +--- + +## Files +### Generate ServiceProto +``` +npm run proto +``` + +### Generate API templates +``` +npm run api +``` + +### Sync shared code to client + +``` +npm run sync +``` + +> If you chose symlink when using `create-tsrpc-app`, it would re-create the symlink instead of copy files. diff --git a/examples/transfer-encryption/backend/package.json b/examples/transfer-encryption/backend/package.json new file mode 100644 index 0000000..801c506 --- /dev/null +++ b/examples/transfer-encryption/backend/package.json @@ -0,0 +1,23 @@ +{ + "name": "transfer-encryption-backend", + "version": "0.1.0", + "main": "index.js", + "private": true, + "scripts": { + "proto": "tsrpc proto -i src/shared/protocols -o src/shared/protocols/serviceProto.ts", + "sync": "tsrpc sync --from src/shared --to ../frontend/src/shared", + "api": "tsrpc api -i src/shared/protocols/serviceProto.ts -o src/api", + "dev": "onchange \"src/**/*.ts\" -i -k -- ts-node \"src/index.ts\"", + "build": "tsrpc build" + }, + "devDependencies": { + "@types/node": "^15.12.5", + "onchange": "^7.1.0", + "ts-node": "^10.0.0", + "tsrpc-cli": "^2.0.3", + "typescript": "^4.3.4" + }, + "dependencies": { + "tsrpc": "^3.0.4" + } +} diff --git a/examples/transfer-encryption/backend/src/api/ApiAddData.ts b/examples/transfer-encryption/backend/src/api/ApiAddData.ts new file mode 100644 index 0000000..c064789 --- /dev/null +++ b/examples/transfer-encryption/backend/src/api/ApiAddData.ts @@ -0,0 +1,26 @@ +import { ApiCall } from "tsrpc"; +import { ReqAddData, ResAddData } from "../shared/protocols/PtlAddData"; +import { AllData } from "./ApiGetData"; + +// This is a demo code file +// Feel free to delete it + +export async function ApiAddData(call: ApiCall) { + // Error + if (call.req.content === '') { + call.error('Content is empty'); + return; + } + + let time = new Date(); + AllData.unshift({ + content: call.req.content, + time: time + }) + console.log('AllData', AllData) + + // Success + call.succ({ + time: time + }); +} \ No newline at end of file diff --git a/examples/transfer-encryption/backend/src/api/ApiGetData.ts b/examples/transfer-encryption/backend/src/api/ApiGetData.ts new file mode 100644 index 0000000..2996ee4 --- /dev/null +++ b/examples/transfer-encryption/backend/src/api/ApiGetData.ts @@ -0,0 +1,13 @@ +import { ApiCall } from "tsrpc"; +import { ReqGetData, ResGetData } from "../shared/protocols/PtlGetData"; + +// This is a demo code file +// Feel free to delete it + +export async function ApiGetData(call: ApiCall) { + call.succ({ + data: AllData + }) +} + +export const AllData: { content: string, time: Date }[] = []; \ No newline at end of file diff --git a/examples/transfer-encryption/backend/src/index.ts b/examples/transfer-encryption/backend/src/index.ts new file mode 100644 index 0000000..5b2a4d3 --- /dev/null +++ b/examples/transfer-encryption/backend/src/index.ts @@ -0,0 +1,39 @@ +import * as path from "path"; +import { HttpServer } from "tsrpc"; +import { EncryptUtil } from "./shared/models/EncryptUtil"; +import { serviceProto } from "./shared/protocols/serviceProto"; + +// Create the Server +const server = new HttpServer(serviceProto, { + port: 3000, + cors: '*', + debugBuf: true +}); + +// Encrypt +server.flows.preSendBufferFlow.push(v => { + v.buf = EncryptUtil.encrypt(v.buf); + return v; +}); +// Decrypt +server.flows.preRecvBufferFlow.push(v => { + v.buf = EncryptUtil.decrypt(v.buf); + return v; +}) + +// Entry function +async function main() { + // Auto implement APIs + await server.autoImplementApi(path.resolve(__dirname, 'api')); + + // TODO + // Prepare something... (e.g. connect the db) + + await server.start(); +}; + +main().catch(e => { + // Exit if any error during the startup + server.logger.error(e); + process.exit(-1); +}); \ No newline at end of file diff --git a/examples/transfer-encryption/backend/src/shared/models/EncryptUtil.ts b/examples/transfer-encryption/backend/src/shared/models/EncryptUtil.ts new file mode 100644 index 0000000..734b512 --- /dev/null +++ b/examples/transfer-encryption/backend/src/shared/models/EncryptUtil.ts @@ -0,0 +1,17 @@ +export class EncryptUtil { + + static encrypt(buf: Uint8Array): Uint8Array { + for (let i = 0; i < buf.length; ++i) { + buf[i] -= 1; + } + return buf; + } + + static decrypt(buf: Uint8Array): Uint8Array { + for (let i = 0; i < buf.length; ++i) { + buf[i] += 1; + } + return buf; + } + +} \ No newline at end of file diff --git a/examples/transfer-encryption/backend/src/shared/protocols/PtlAddData.ts b/examples/transfer-encryption/backend/src/shared/protocols/PtlAddData.ts new file mode 100644 index 0000000..7f442ae --- /dev/null +++ b/examples/transfer-encryption/backend/src/shared/protocols/PtlAddData.ts @@ -0,0 +1,10 @@ +// This is a demo code file +// Feel free to delete it + +export interface ReqAddData { + content: string; +} + +export interface ResAddData { + time: Date +} \ No newline at end of file diff --git a/examples/transfer-encryption/backend/src/shared/protocols/PtlGetData.ts b/examples/transfer-encryption/backend/src/shared/protocols/PtlGetData.ts new file mode 100644 index 0000000..8ac35a7 --- /dev/null +++ b/examples/transfer-encryption/backend/src/shared/protocols/PtlGetData.ts @@ -0,0 +1,13 @@ +// This is a demo code file +// Feel free to delete it + +export interface ReqGetData { + +} + +export interface ResGetData { + data: { + content: string, + time: Date + }[] +} \ No newline at end of file diff --git a/examples/transfer-encryption/backend/src/shared/protocols/serviceProto.ts b/examples/transfer-encryption/backend/src/shared/protocols/serviceProto.ts new file mode 100644 index 0000000..e7207a9 --- /dev/null +++ b/examples/transfer-encryption/backend/src/shared/protocols/serviceProto.ts @@ -0,0 +1,98 @@ +import { ServiceProto } from 'tsrpc-proto'; +import { ReqAddData, ResAddData } from './PtlAddData'; +import { ReqGetData, ResGetData } from './PtlGetData'; + +// This is a demo service proto file (auto generated) +// Feel free to delete it + +export interface ServiceType { + api: { + "AddData": { + req: ReqAddData, + res: ResAddData + }, + "GetData": { + req: ReqGetData, + res: ResGetData + } + }, + msg: { + + } +} + +export const serviceProto: ServiceProto = { + "version": 1, + "services": [ + { + "id": 0, + "name": "AddData", + "type": "api" + }, + { + "id": 1, + "name": "GetData", + "type": "api" + } + ], + "types": { + "PtlAddData/ReqAddData": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "content", + "type": { + "type": "String" + } + } + ] + }, + "PtlAddData/ResAddData": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "time", + "type": { + "type": "Date" + } + } + ] + }, + "PtlGetData/ReqGetData": { + "type": "Interface" + }, + "PtlGetData/ResGetData": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "data", + "type": { + "type": "Array", + "elementType": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "content", + "type": { + "type": "String" + } + }, + { + "id": 1, + "name": "time", + "type": { + "type": "Date" + } + } + ] + } + } + } + ] + } + } +}; \ No newline at end of file diff --git a/examples/transfer-encryption/backend/tsconfig.json b/examples/transfer-encryption/backend/tsconfig.json new file mode 100644 index 0000000..d18498f --- /dev/null +++ b/examples/transfer-encryption/backend/tsconfig.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "lib": [ + "es2018" + ], + "module": "commonjs", + "target": "es2018", + "outDir": "dist", + "strict": true, + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true, + "moduleResolution": "node" + }, + "include": [ + "src" + ] +} \ No newline at end of file diff --git a/examples/transfer-encryption/frontend/.gitignore b/examples/transfer-encryption/frontend/.gitignore new file mode 100644 index 0000000..ae16b5a --- /dev/null +++ b/examples/transfer-encryption/frontend/.gitignore @@ -0,0 +1,3 @@ +.DS_STORE +node_modules +dist \ No newline at end of file diff --git a/examples/transfer-encryption/frontend/package.json b/examples/transfer-encryption/frontend/package.json new file mode 100644 index 0000000..4dc52af --- /dev/null +++ b/examples/transfer-encryption/frontend/package.json @@ -0,0 +1,24 @@ +{ + "name": "transfer-encryption-frontend", + "version": "0.1.0", + "private": true, + "scripts": { + "dev": "webpack serve --mode=development --open", + "build": "webpack --mode=production" + }, + "devDependencies": { + "copy-webpack-plugin": "^9.0.1", + "html-webpack-plugin": "^5.3.2", + "ts-loader": "^9.2.3", + "typescript": "^4.3.4", + "webpack": "^5.41.0", + "webpack-cli": "^4.7.2", + "webpack-dev-server": "^3.11.2" + }, + "dependencies": { + "tsrpc-browser": "^3.0.4" + }, + "browserslist": [ + "defaults" + ] +} diff --git a/examples/transfer-encryption/frontend/public/favicon.ico b/examples/transfer-encryption/frontend/public/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..361535cb26507b388a0341c772f36f536f7f24be GIT binary patch literal 4286 zcmb`JYfMvT7{`xu*%o7_S(be;%k;yNxy6uW$;{@Q;3W*Npl%Az#Tkv;2t(%_UJ)n- zLBtGi(=aXq1r@mq=)6#Y&^rf!!XQb6do}FC_3j}_?fS->RR=f|||sH&}>Tu@$RAvP&NzQZGb{STp#5Wf`;{IL&mjva@i`2~=k zmF1Kyemg9f?YF@jKGuBviOMq?o=t0f+!hyp`05adjot`p`}RPVA`fzppMdo2Y$s31 zrhRnuCRjS}GcD%e6;B?jmFicEPM@n5vU&ctsBlP4O@u$QGcl(pA>(L{B=1H3=BNk= z3kiUE0Ut^B0rgwy*_xE2Cri1U6F0xC3NBvP!HxT7s8p)F<_Y;3Ik~VtVil}hItLa9 zeOe+FfBE^kmAS>`9GiOnOnoEV?yy2@4}jim1KkrVC>yUqR$iV@KByPgtzOFUmoJ=d z#~Qr%qK8W5rKyV2N-M=&a#jh=ZI8gv&2ns7_2y@sS~{P?zj`C&7nOMJfok>LvOtdi z?ULEBXwJt8LO%A}gku$_+Y8F8q5h^G^p-)6OL3EgeBOt~)CCpimFPjb=lB}r7Zt;@ zh0{4c)!^&-pPMiTukjvA|Ef`+z1#%aNBu6`VZOr|KhLLC_oN@{nr>h{6drStm9K#1 zi@tR7S1g_d^Jh*8<@wjzJDs`anZni3HRzU49;hBoh6hkkQYPu)a7H?W2Kzhtl!GO6 zr>cSiKHw|}S?>0Fi(Qh>=R@3}E#?8l1E9GFpEWZe2KWw<$ z_k7k|*b=iR&9@1id<)+9>5&zL=U3* z_cVJ#j?3O+gIgWF(AfG2)d72xlOQD_UZmY}nGH3a<6Gfv%tQCR7wTZ>8hQ3OHpZ#L z-n(kFz~ws+p#HXzlj^Eg#O1um(T`mABeD4x<{$+HZN0;J7;*N7V!eW~@=W1cRikpt z$GFo|WHKJjt;n8$Ie03pgC4K%l3RKXVXSvL`!QBC#(Li^mprJoMkpveDdHnrjj_A^ zzZpFop&Z~$VlNCo_xK#&eAok53>}h8VeIt1gZuZn^5rrUvfp(HT<`|j?_&<^{3Y4 zxzWm2^xB{E$%E4eZd#u7K|_mYALKJLGn3q? z&O}|4hS+p}n<2$`nRE0o7Ln8JEE|dYT=b|x*{y>t+_m&OXS4ErEg(DmWf;Ex4f=Q! zH3PK~>r!AaTeYW^s*IF)@A?gP0cLw5iiu$v#(xm?jA15rq1qT`97)MAug9X8=NgSG mX%w$m+>n%bh8j(3!|$TT(REOCO;QYAA0T!Vz~E>Ex5Ix+)2V6z literal 0 HcmV?d00001 diff --git a/examples/transfer-encryption/frontend/public/index.css b/examples/transfer-encryption/frontend/public/index.css new file mode 100644 index 0000000..bdef476 --- /dev/null +++ b/examples/transfer-encryption/frontend/public/index.css @@ -0,0 +1,83 @@ +* { + margin: 0; + padding: 0; + box-sizing: border-box; +} + +body { + text-align: center; +} + +body>* { + margin: 20px auto; +} + +.send, +.list { + width: 370px; +} + +.send>* { + display: block; + width: 100%; + margin: 10px auto; + padding: 10px; + border-radius: 5px; + font-size: 16px; + border: none; + outline: none; +} + +.send>textarea { + height: 80px; + background: #f7f7f7; + border: #eeeeee 1px solid; +} + +.send>textarea:focus { + background: #fff; + border-color: #ccc; +} + +.send>button { + background: #215fa4; + color: white; + cursor: pointer; +} + +.send>button:hover { + background: #4b80bb; +} + +.list { + list-style: none; + border-radius: 5px; + padding: 10px; + background: #f2f2f2; +} + +.list>li { + margin-bottom: 10px; + padding: 10px; + background: #fff; + line-height: 1.5em; + border-radius: 5px; +} + +.list>li>.content { + font-size: 14px; + text-align: left; + white-space: pre-wrap; + word-wrap: break-word; +} + +.list>li>.time { + font-size: 12px; + color: #4b80bb; + text-align: right; +} + +.list>li:last-child { + border-bottom: none; + margin-bottom: 0; +} \ No newline at end of file diff --git a/examples/transfer-encryption/frontend/public/index.html b/examples/transfer-encryption/frontend/public/index.html new file mode 100644 index 0000000..8ac22f5 --- /dev/null +++ b/examples/transfer-encryption/frontend/public/index.html @@ -0,0 +1,28 @@ + + + + + + + + TSRPC Browser + + + + +

TSRPC Guestbook

+ +
+ + +
+ +
    + +
+ + + \ No newline at end of file diff --git a/examples/transfer-encryption/frontend/src/index.ts b/examples/transfer-encryption/frontend/src/index.ts new file mode 100644 index 0000000..dd31d83 --- /dev/null +++ b/examples/transfer-encryption/frontend/src/index.ts @@ -0,0 +1,67 @@ +import { HttpClient } from 'tsrpc-browser'; +import { EncryptUtil } from './shared/models/EncryptUtil'; +import { serviceProto } from './shared/protocols/serviceProto'; + +// Create Client +let client = new HttpClient(serviceProto, { + server: 'http://127.0.0.1:3000', + logger: console, + debugBuf: true +}); + +// Encrypt +client.flows.preSendBufferFlow.push(v => { + v.buf = EncryptUtil.encrypt(v.buf); + return v; +}); +// Decrypt +client.flows.preRecvBufferFlow.push(v => { + v.buf = EncryptUtil.decrypt(v.buf); + return v; +}) + +// Reload message list +async function loadList() { + let ret = await client.callApi('GetData', {}); + + // Error + if (!ret.isSucc) { + alert(ret.err.message); + return; + } + + // Success + const list = document.querySelector('.list')!; + list.innerHTML = ''; + ret.res.data.forEach(v => { + let li = document.createElement('li'); + li.innerHTML = `
`; + (li.querySelector('.content') as HTMLDivElement).innerText = v.content; + (li.querySelector('.time') as HTMLDivElement).innerText = v.time.toLocaleTimeString(); + list.appendChild(li); + }) +} + +// Send Message +async function send() { + const textarea = document.querySelector('.send>textarea') as HTMLTextAreaElement; + let ret = await client.callApi('AddData', { + content: textarea.value + }); + + // Error + if (!ret.isSucc) { + alert(ret.err.message); + return; + } + + // Success + textarea.value = ''; + loadList(); +} + +// Bind Events +(document.querySelector('.send>button') as HTMLButtonElement).onclick = send; + +// Load list after page load +loadList(); \ No newline at end of file diff --git a/examples/transfer-encryption/frontend/src/shared/models/EncryptUtil.ts b/examples/transfer-encryption/frontend/src/shared/models/EncryptUtil.ts new file mode 100644 index 0000000..734b512 --- /dev/null +++ b/examples/transfer-encryption/frontend/src/shared/models/EncryptUtil.ts @@ -0,0 +1,17 @@ +export class EncryptUtil { + + static encrypt(buf: Uint8Array): Uint8Array { + for (let i = 0; i < buf.length; ++i) { + buf[i] -= 1; + } + return buf; + } + + static decrypt(buf: Uint8Array): Uint8Array { + for (let i = 0; i < buf.length; ++i) { + buf[i] += 1; + } + return buf; + } + +} \ No newline at end of file diff --git a/examples/transfer-encryption/frontend/src/shared/protocols/PtlAddData.ts b/examples/transfer-encryption/frontend/src/shared/protocols/PtlAddData.ts new file mode 100644 index 0000000..7f442ae --- /dev/null +++ b/examples/transfer-encryption/frontend/src/shared/protocols/PtlAddData.ts @@ -0,0 +1,10 @@ +// This is a demo code file +// Feel free to delete it + +export interface ReqAddData { + content: string; +} + +export interface ResAddData { + time: Date +} \ No newline at end of file diff --git a/examples/transfer-encryption/frontend/src/shared/protocols/PtlGetData.ts b/examples/transfer-encryption/frontend/src/shared/protocols/PtlGetData.ts new file mode 100644 index 0000000..8ac35a7 --- /dev/null +++ b/examples/transfer-encryption/frontend/src/shared/protocols/PtlGetData.ts @@ -0,0 +1,13 @@ +// This is a demo code file +// Feel free to delete it + +export interface ReqGetData { + +} + +export interface ResGetData { + data: { + content: string, + time: Date + }[] +} \ No newline at end of file diff --git a/examples/transfer-encryption/frontend/src/shared/protocols/serviceProto.ts b/examples/transfer-encryption/frontend/src/shared/protocols/serviceProto.ts new file mode 100644 index 0000000..e7207a9 --- /dev/null +++ b/examples/transfer-encryption/frontend/src/shared/protocols/serviceProto.ts @@ -0,0 +1,98 @@ +import { ServiceProto } from 'tsrpc-proto'; +import { ReqAddData, ResAddData } from './PtlAddData'; +import { ReqGetData, ResGetData } from './PtlGetData'; + +// This is a demo service proto file (auto generated) +// Feel free to delete it + +export interface ServiceType { + api: { + "AddData": { + req: ReqAddData, + res: ResAddData + }, + "GetData": { + req: ReqGetData, + res: ResGetData + } + }, + msg: { + + } +} + +export const serviceProto: ServiceProto = { + "version": 1, + "services": [ + { + "id": 0, + "name": "AddData", + "type": "api" + }, + { + "id": 1, + "name": "GetData", + "type": "api" + } + ], + "types": { + "PtlAddData/ReqAddData": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "content", + "type": { + "type": "String" + } + } + ] + }, + "PtlAddData/ResAddData": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "time", + "type": { + "type": "Date" + } + } + ] + }, + "PtlGetData/ReqGetData": { + "type": "Interface" + }, + "PtlGetData/ResGetData": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "data", + "type": { + "type": "Array", + "elementType": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "content", + "type": { + "type": "String" + } + }, + { + "id": 1, + "name": "time", + "type": { + "type": "Date" + } + } + ] + } + } + } + ] + } + } +}; \ No newline at end of file diff --git a/examples/transfer-encryption/frontend/tsconfig.json b/examples/transfer-encryption/frontend/tsconfig.json new file mode 100644 index 0000000..f7f1d7c --- /dev/null +++ b/examples/transfer-encryption/frontend/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "lib": [ + "dom", + "es2015" + ], + "module": "esnext", + "target": "es5", + "allowSyntheticDefaultImports": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "moduleResolution": "node", + "outDir": "dist", + "skipLibCheck": true, + "strict": true, + "jsx": "react-jsx", + "sourceMap": true, + "isolatedModules": true + }, + "include": [ + "src" + ] +} \ No newline at end of file diff --git a/examples/transfer-encryption/frontend/webpack.config.js b/examples/transfer-encryption/frontend/webpack.config.js new file mode 100644 index 0000000..b0d3703 --- /dev/null +++ b/examples/transfer-encryption/frontend/webpack.config.js @@ -0,0 +1,56 @@ +const webpack = require('webpack'); +const path = require('path'); +const HtmlWebpackPlugin = require('html-webpack-plugin'); +const CopyWebpackPlugin = require('copy-webpack-plugin'); + +const isProduction = process.argv.indexOf('--mode=production') > -1; + +module.exports = { + entry: './src/index.ts', + output: { + filename: 'bundle.[contenthash].js', + path: path.resolve(__dirname, 'dist'), + clean: true + }, + resolve: { + extensions: ['.ts', '.tsx', '.js', '.mjs', '.cjs'] + }, + module: { + rules: [ + { + test: /\.tsx?$/, + use: [{ + loader: 'ts-loader', + options: { + // Compile to ES5 in production mode for better compatibility + // Compile to ES2018 in development for better debugging (like async/await) + compilerOptions: !isProduction ? { + "target": "es2018", + } : undefined + } + }], + exclude: /node_modules/ + }, + ] + }, + plugins: [ + // Copy "public" to "dist" + new CopyWebpackPlugin({ + patterns: [{ + from: 'public', + to: '.', + toType: 'dir', + globOptions: { + gitignore: true, + ignore: [path.resolve(__dirname, 'public/index.html').replace(/\\/g, '/')] + }, + noErrorOnMissing: true + }] + }), + // Auto add