-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: server build-in grpc reflection api
- Loading branch information
Showing
15 changed files
with
366 additions
and
16 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
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,77 @@ | ||
import { asyncStreamLoader as loader } from './loader.js' | ||
|
||
function timeout(ms) { | ||
return new Promise((resolve, reject) => setTimeout(resolve, ms)) | ||
} | ||
|
||
class Stream { | ||
async unaryHello(call) { | ||
return { message: 'hello ' + call.request.message } | ||
} | ||
|
||
async clientStreamHello(call) { | ||
const metadata = call.metadata.clone() | ||
metadata.add('x-timestamp-server', 'received=' + new Date().toISOString()) | ||
call.sendMetadata(metadata) | ||
|
||
for await (const data of call.readAll()) { | ||
console.log(data) | ||
} | ||
return { message: "Hello! I'm fine, thank you!" } | ||
} | ||
|
||
async serverStreamHello(call) { | ||
const metadata = call.metadata.clone() | ||
metadata.add('x-timestamp-server', 'received=' + new Date().toISOString()) | ||
call.sendMetadata(metadata) | ||
|
||
console.log(call.request.message) | ||
call.write({ message: 'Hello! I got you message.' }) | ||
call.write({ message: "I'm fine, thank you" }) | ||
call.writeAll([{ message: 'other thing x' }, { message: 'other thing y' }]) | ||
call.end() | ||
} | ||
|
||
async mutualStreamHello(call) { | ||
const metadata = call.metadata.clone() | ||
metadata.add('x-timestamp-server', 'received=' + new Date().toISOString()) | ||
call.sendMetadata(metadata) | ||
|
||
call.write({ message: 'emmm...' }) | ||
|
||
for await (const data of call.readAll()) { | ||
console.log(data.message) | ||
if (data.message === 'Hello!') { | ||
call.write({ message: 'Hello too.' }) | ||
} else if (data.message === 'How are you?') { | ||
call.write({ message: "I'm fine, thank you" }) | ||
await timeout(1000) | ||
call.write({ message: 'delay 1s' }) | ||
call.writeAll([{ message: 'emm... ' }, { message: 'emm......' }]) | ||
} else { | ||
call.write({ message: 'pardon?' }) | ||
} | ||
} | ||
|
||
call.end() | ||
} | ||
} | ||
|
||
const start = async (addr) => { | ||
await loader.init({ | ||
isDev: true, | ||
packagePrefix: 'test' | ||
}) | ||
const reflection = await loader.initReflection() | ||
|
||
const server = await loader.initServer() | ||
|
||
server.add('stream.Hellor', new Stream()) | ||
|
||
server.inject(reflection) | ||
|
||
await server.listen(addr) | ||
console.log('start:', addr) | ||
} | ||
|
||
start('localhost:9097') |
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,107 @@ | ||
import { helloworldLoader as loader } from './loader.js' | ||
|
||
const timeout = (ms) => { | ||
return new Promise((resolve, reject) => setTimeout(resolve, ms)) | ||
} | ||
|
||
class Greeter { | ||
init(server) { | ||
server.add('helloworld.Greeter', this, { exclude: ['init'] }) | ||
} | ||
|
||
async sayGreet(call) { | ||
const metadata = call.metadata.clone() | ||
metadata.add('x-timestamp-server', 'received=' + new Date().toISOString()) | ||
call.sendMetadata(metadata) | ||
if (metadata.get('x-throw-error').length > 0) { | ||
throw new Error('throw error because x-throw-error') | ||
} | ||
|
||
if (metadata.get('x-long-delay').length > 0) { | ||
await new Promise((resolve) => setTimeout(resolve, 1000 * 10)) | ||
} | ||
await timeout(1000) | ||
|
||
return { | ||
message: `hello, ${call.request.name || 'world'}` | ||
} | ||
} | ||
|
||
async sayGreet2(call) { | ||
return this.sayGreet(call) | ||
} | ||
} | ||
|
||
class Hellor { | ||
constructor() { | ||
this.count = 0 | ||
} | ||
|
||
init(server) { | ||
server.add('helloworld.Hellor', this, { exclude: ['init'] }) | ||
} | ||
|
||
async SayHello(call) { | ||
const metadata = call.metadata.clone() | ||
metadata.add('x-timestamp-server', 'received=' + new Date().toISOString()) | ||
call.sendMetadata(metadata) | ||
if (metadata.get('x-throw-error').length > 0) { | ||
throw new Error('throw error because x-throw-error') | ||
} | ||
|
||
if (metadata.get('x-long-delay').length > 0) { | ||
await new Promise((resolve) => setTimeout(resolve, 1000 * 10)) | ||
} | ||
|
||
this.count++ | ||
|
||
return { | ||
message: `hello, ${call.request.name || 'world'}`, | ||
count: this.count | ||
} | ||
} | ||
|
||
async SayHello2(call) { | ||
return this.SayHello(call) | ||
} | ||
} | ||
|
||
const middlewareA = async (ctx, next) => { | ||
const beginTime = new Date().getTime() | ||
console.log('middlewareA: 1', ctx, beginTime) | ||
await timeout(1000) | ||
await next() | ||
await timeout(1000) | ||
const endTime = new Date().getTime() | ||
console.log('middlewareA: 2', ctx, endTime, endTime - beginTime) | ||
} | ||
|
||
const middlewareB = async (ctx, next) => { | ||
const beginTime = new Date().getTime() | ||
console.log('middlewareB: 1', ctx, beginTime) | ||
await next() | ||
const endTime = new Date().getTime() | ||
console.log('middlewareB: 2', ctx, endTime, endTime - beginTime) | ||
} | ||
|
||
const start = async (addr) => { | ||
await loader.init({ | ||
isDev: true, | ||
packagePrefix: 'test' | ||
}) | ||
|
||
const reflection = await loader.initReflection() | ||
|
||
const server = await loader.initServer() | ||
server.use(middlewareA, middlewareB) | ||
|
||
const servicers = [new Greeter(), new Hellor()] | ||
servicers.map((s) => s.init(server)) | ||
|
||
server.inject(reflection) | ||
|
||
await server.listen(addr) | ||
console.log('start:', addr) | ||
} | ||
|
||
start('localhost:9098') |
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,17 @@ | ||
import { ProtoLoader } from '../../lib/index.js' | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
|
||
// get this file dir path | ||
const __dirname = path.dirname(fileURLToPath(import.meta.url)) | ||
|
||
export const helloworldLoader = new ProtoLoader({ | ||
location: path.join(__dirname, '../proto'), | ||
files: ['helloworld/service.proto'] | ||
}) | ||
|
||
export const asyncStreamLoader = new ProtoLoader({ | ||
location: path.join(__dirname, '../proto'), | ||
files: ['stream/service.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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Oops, something went wrong.