Skip to content

Commit

Permalink
Merge pull request #2 from apache/dubbo3
Browse files Browse the repository at this point in the history
init from dubbo3
  • Loading branch information
fengwei5280 authored Dec 6, 2022
2 parents f5500ba + b010f70 commit 7b76096
Show file tree
Hide file tree
Showing 17 changed files with 1,426 additions and 68 deletions.
21 changes: 21 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
env: {
browser: true,
es2021: true,
node: true
},
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
overrides: [],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module'
},
plugins: ['@typescript-eslint'],
rules: {
indent: ['error', 2],
'linebreak-style': ['error', 'unix'],
quotes: ['error', 'single'],
semi: ['error', 'never']
}
}
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@
"format": "npx prettier --write \"**/*.{ts,js,json,md}\"",
"prepare": "husky install"
},
"dependencies": {},
"devDependencies": {
"@nrwl/cli": "14.8.3",
"@nrwl/workspace": "14.8.3",
"@types/node": "latest",
"pretty-quick": "^1.10.0",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.43.0",
"@vitest/coverage-istanbul": "^0.25.3",
"eslint": "^8.28.0",
"husky": "^6.0.0",
"nx": "14.8.3",
"prettier": "^2.6.2",
"pretty-quick": "^1.10.0",
"typescript": "^4.9.3",
"vite": "^3.1.8",
"vitest": "^0.24.3",
"husky": "^6.0.0"
"vitest": "^0.24.3"
},
"license": "Apache-2.0"
}
8 changes: 7 additions & 1 deletion packages/dubbo-serialization/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
"main": "./lib/index.js",
"types": "./lib/typings/index.d.ts",
"description": "apache dubbo3 client",
"type": "module",
"keywords": [
"dubbo3",
"nodejs",
"dubbo-serialization"
],
"license": "Apache-2.0"
"license": "Apache-2.0",
"dependencies": {
"@types/glob": "^8.0.0",
"glob": "^8.0.3",
"protobufjs": "^7.1.2"
}
}
42 changes: 39 additions & 3 deletions packages/dubbo-serialization/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,44 @@
* limitations under the License.
*/

describe(`dubbo-serialization test suites`, () => {
it('test 1+1=2', () => {
expect(1 + 1).toBe(2)
import { loadProto, lookup, encode, decode } from '..'
import { Type } from 'protobufjs'
import path from 'node:path'

describe('test serialization', () => {
test('loadProto', () => {
const root = loadProto(path.join(__dirname, 'proto'))
// test current folder
expect(root.lookupType('test.Test1')).toBeInstanceOf(Type)
// test sub folder
expect(root.lookupType('sub.Test1')).toBeInstanceOf(Type)
})

test('lookup', () => {
// validate param type
expect(() => lookup(1)).toThrowError(/^typeName must be a string$/)
// TODO : load proto before lookup
// expect(() => lookup('')).toThrowError(/^Please load proto before lookup$/)
// lookupType
const root = loadProto(path.join(__dirname, 'proto'))
expect(root.lookupType('test.Test1')).toBeInstanceOf(Type)
})

test('encode', () => {
loadProto(path.join(__dirname, 'proto'))
// encode error type
expect(() => encode({}, 'test.Test2')).toThrowError(
/^no such type: test.Test2$/
)
expect(encode({ field1: '1' }, 'test.Test1')).toBeInstanceOf(Buffer)
})

test('decode', () => {
loadProto(path.join(__dirname, 'proto'))
const msg = encode({ field1: '1' }, 'test.Test1')
// correct
expect(decode(msg, 'test.Test1')).toMatchObject({ field1: '1' })
// error
expect(decode(msg, 'test.Test1')).not.toMatchObject({ field1: '2' })
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
syntax = "proto3";
package sub;

message Test1 {
string field1 = 1;
}
6 changes: 6 additions & 0 deletions packages/dubbo-serialization/src/__tests__/proto/test.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
syntax = "proto3";
package test;

message Test1 {
string field1 = 1;
}
4 changes: 3 additions & 1 deletion packages/dubbo-serialization/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
* limitations under the License.
*/

console.log('dubbo-serialization')
import { loadProto, lookup, encode, decode } from './proto'

export { loadProto, lookup, encode, decode }
56 changes: 56 additions & 0 deletions packages/dubbo-serialization/src/proto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import path from 'node:path'
import glob from 'glob'
import { loadSync, Root, Type } from 'protobufjs'

let protoCache: Root | undefined = undefined

/**
* 加载所有的proto文件
* @param dir 文件路径
* @returns Root namespace
*/
function loadProto(dir: string) {
const files = glob.sync(path.join(dir, '**', '*.proto').replace(/\\/g, '/'))
protoCache = loadSync(files)
return protoCache
}

/**
* 根据typeName寻找proto
* @param typeName
* @returns Reflected message type
*/
function lookup(typeName: string): Type {
if (!protoCache) {
throw new TypeError('Please load proto before lookup')
}
if (typeof typeName !== 'string') {
throw new TypeError('typeName must be a string')
}
return protoCache.lookupType(typeName)
}

/**
* Creates a new message of this type using the specified properties.
* @param params 参数
* @param protoName 名称
* @returns 类型
*/
function encode<T extends { [k: string]: unknown }>(data: T, type: string) {
// 根据protoName找到对应的message
const Message = lookup(type)
return Message.encode(Message.create(data)).finish()
}

/**
*
* @param data 解码数据
* @param type pb类型
* @returns
*/
function decode<T = { [k: string]: unknown }>(data: Buffer, type: string): T {
const Message = lookup(type)
return Message.decode(data).toJSON() as T
}

export { loadProto, lookup, encode, decode }
6 changes: 5 additions & 1 deletion packages/dubbo-serialization/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import { defineConfig } from 'vite'

export default defineConfig({
test: {
globals: true
globals: true,
coverage: {
provider: 'istanbul',
reporter: ['text', 'json', 'html']
}
}
})
20 changes: 20 additions & 0 deletions packages/dubbo-transport/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "apache-dubbo3-transport",
"version": "0.0.1",
"main": "./lib/index.js",
"types": "./lib/typings/index.d.ts",
"description": "apache dubbo3 transport",
"keywords": [
"dubbo3",
"nodejs",
"dubbo3-transport"
],
"license": "Apache-2.0",
"dependencies": {
"debug": "^4.3.4",
"lodash": "^4.17.21"
},
"devDependencies": {
"@types/lodash": "^4.14.189"
}
}
35 changes: 35 additions & 0 deletions packages/dubbo-transport/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { DubboClientTransport, DubboServerTransport } from '../index'

describe(`dubbo-transport`, () => {
it('test dubbo-transport', () => {
// 创建服务端 transport 实例
const server = new DubboServerTransport({
url: 'http://localhost:3600',
port: 3600
})
expect(server.port).toBe(3600)
// 创建客户端 transport 实例
const client = new DubboClientTransport({
url: 'http://localhost:3600',
port: 3600
})
expect(client.url).toBe('http://localhost:3600')
})
})
57 changes: 57 additions & 0 deletions packages/dubbo-transport/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { debug } from 'debug'
import http2 from 'node:http2'
import { IDubboClientTransport, DubboContext } from './transport'

// init log
const log = debug('dubbo3:transport:client')

export class DubboClientTransport implements IDubboClientTransport {
// transport 实例
private transport: any
private ctx: DubboContext

constructor(opts: DubboContext) {
this.ctx = opts
this.connect()
}

get url() {
return this.ctx.url
}

/**
* 建立连接
*/
connect() {
this.transport = http2.connect(this.url)

this.transport.once('connect', () => {
log('has connected')
})
}

/**
* 发送消息
* @param msg
*/
async send(msg: DubboContext): Promise<void> {
this.transport.request(msg)
}
}
21 changes: 21 additions & 0 deletions packages/dubbo-transport/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { DubboClientTransport } from './client'
import { DubboServerTransport } from './server'

export { DubboClientTransport, DubboServerTransport }
Loading

0 comments on commit 7b76096

Please sign in to comment.