forked from apache/dubbo-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from apache/dubbo3
init from dubbo3
- Loading branch information
Showing
17 changed files
with
1,426 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/dubbo-serialization/src/__tests__/proto/sub/sub.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
syntax = "proto3"; | ||
package sub; | ||
|
||
message Test1 { | ||
string field1 = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
syntax = "proto3"; | ||
package test; | ||
|
||
message Test1 { | ||
string field1 = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
Oops, something went wrong.