From 834928dbe9c799eeb31f4f2f31669f2ecc882f4e Mon Sep 17 00:00:00 2001 From: Twilight Date: Fri, 24 May 2024 09:49:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=20Streaming?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/HelloWorld.vue | 20 ++++++++- src/proto/document/document.client.ts | 13 ++++++ src/proto/document/document.ts | 59 ++++++++++++++++++++++++++- 3 files changed, 89 insertions(+), 3 deletions(-) diff --git a/src/components/HelloWorld.vue b/src/components/HelloWorld.vue index 048df17..58bac73 100644 --- a/src/components/HelloWorld.vue +++ b/src/components/HelloWorld.vue @@ -10,26 +10,42 @@ import { ref } from "vue"; import { useUserStore } from "@/store/user"; import {GrpcWebFetchTransport} from "@protobuf-ts/grpcweb-transport" import {LibraryServiceClient} from "@/proto/library/library.client.ts" +import {DocumentServiceClient} from "@/proto/document/document.client.ts" import * as libraryProto from "@/proto/library/library.ts"; +import * as documentProto from "@/proto/document/document.ts"; let listLibrary: libraryProto.ListLibrariesRequest = { page: 1 } +let empty: documentProto.Empty = {} + let transport = new GrpcWebFetchTransport({ baseUrl: "https://test.ivampiresp.com", format: "text", meta: { "Authorization": "Bearer 1", }, + timeout: 100 * 1000, }); let client = new LibraryServiceClient(transport) +let documentClient = new DocumentServiceClient(transport) +let s = documentClient.testStream(empty) -let out = client.listLibraries(listLibrary, {}) -out.then((r) => console.log(r)) +s.responses.onMessage(e => { + console.log(e) +}) + +s.then(res => { + console.log(res) +}) +console.log(s) + +// let out = client.listLibraries(listLibrary, {}) +// out.then((r) => console.log(r)) const userStore = useUserStore(); const token = ref(""); diff --git a/src/proto/document/document.client.ts b/src/proto/document/document.client.ts index 73b34dc..c23a748 100644 --- a/src/proto/document/document.client.ts +++ b/src/proto/document/document.client.ts @@ -4,6 +4,8 @@ import type { RpcTransport } from "@protobuf-ts/runtime-rpc"; import type { ServiceInfo } from "@protobuf-ts/runtime-rpc"; import { DocumentService } from "./document"; +import type { TestStreamResponse } from "./document"; +import type { ServerStreamingCall } from "@protobuf-ts/runtime-rpc"; import type { DocumentChunkList } from "./document"; import type { VectorSearchByTextInDocumentRequest } from "./document"; import type { Empty } from "./document"; @@ -47,6 +49,10 @@ export interface IDocumentServiceClient { * @generated from protobuf rpc: VectorSearchByTextInDocument(DocumentService.VectorSearchByTextInDocumentRequest) returns (DocumentService.DocumentChunkList); */ vectorSearchByTextInDocument(input: VectorSearchByTextInDocumentRequest, options?: RpcOptions): UnaryCall; + /** + * @generated from protobuf rpc: TestStream(DocumentService.Empty) returns (stream DocumentService.TestStreamResponse); + */ + testStream(input: Empty, options?: RpcOptions): ServerStreamingCall; } /** * @generated from protobuf service DocumentService.DocumentService @@ -101,4 +107,11 @@ export class DocumentServiceClient implements IDocumentServiceClient, ServiceInf const method = this.methods[5], opt = this._transport.mergeOptions(options); return stackIntercept("unary", this._transport, method, opt, input); } + /** + * @generated from protobuf rpc: TestStream(DocumentService.Empty) returns (stream DocumentService.TestStreamResponse); + */ + testStream(input: Empty, options?: RpcOptions): ServerStreamingCall { + const method = this.methods[6], opt = this._transport.mergeOptions(options); + return stackIntercept("serverStreaming", this._transport, method, opt, input); + } } diff --git a/src/proto/document/document.ts b/src/proto/document/document.ts index 934a0c1..5228e8c 100644 --- a/src/proto/document/document.ts +++ b/src/proto/document/document.ts @@ -221,6 +221,15 @@ export interface DocumentChunkList { */ libraryId: number; } +/** + * @generated from protobuf message DocumentService.TestStreamResponse + */ +export interface TestStreamResponse { + /** + * @generated from protobuf field: string name = 1; + */ + name: string; +} // @generated message type with reflection information, may provide speed optimized methods class Empty$Type extends MessageType { constructor() { @@ -1033,6 +1042,53 @@ class DocumentChunkList$Type extends MessageType { * @generated MessageType for protobuf message DocumentService.DocumentChunkList */ export const DocumentChunkList = new DocumentChunkList$Type(); +// @generated message type with reflection information, may provide speed optimized methods +class TestStreamResponse$Type extends MessageType { + constructor() { + super("DocumentService.TestStreamResponse", [ + { no: 1, name: "name", kind: "scalar", T: 9 /*ScalarType.STRING*/ } + ]); + } + create(value?: PartialMessage): TestStreamResponse { + const message = globalThis.Object.create((this.messagePrototype!)); + message.name = ""; + if (value !== undefined) + reflectionMergePartial(this, message, value); + return message; + } + internalBinaryRead(reader: IBinaryReader, length: number, options: BinaryReadOptions, target?: TestStreamResponse): TestStreamResponse { + let message = target ?? this.create(), end = reader.pos + length; + while (reader.pos < end) { + let [fieldNo, wireType] = reader.tag(); + switch (fieldNo) { + case /* string name */ 1: + message.name = reader.string(); + break; + default: + let u = options.readUnknownField; + if (u === "throw") + throw new globalThis.Error(`Unknown field ${fieldNo} (wire type ${wireType}) for ${this.typeName}`); + let d = reader.skip(wireType); + if (u !== false) + (u === true ? UnknownFieldHandler.onRead : u)(this.typeName, message, fieldNo, wireType, d); + } + } + return message; + } + internalBinaryWrite(message: TestStreamResponse, writer: IBinaryWriter, options: BinaryWriteOptions): IBinaryWriter { + /* string name = 1; */ + if (message.name !== "") + writer.tag(1, WireType.LengthDelimited).string(message.name); + let u = options.writeUnknownFields; + if (u !== false) + (u == true ? UnknownFieldHandler.onWrite : u)(this.typeName, message, writer); + return writer; + } +} +/** + * @generated MessageType for protobuf message DocumentService.TestStreamResponse + */ +export const TestStreamResponse = new TestStreamResponse$Type(); /** * @generated ServiceType for protobuf service DocumentService.DocumentService */ @@ -1042,5 +1098,6 @@ export const DocumentService = new ServiceType("DocumentService.DocumentService" { name: "GetDocument", options: {}, I: GetDocumentRequest, O: Document }, { name: "UpdateDocument", options: {}, I: UpdateDocumentRequest, O: Document }, { name: "DeleteDocument", options: {}, I: DeleteDocumentRequest, O: Empty }, - { name: "VectorSearchByTextInDocument", options: {}, I: VectorSearchByTextInDocumentRequest, O: DocumentChunkList } + { name: "VectorSearchByTextInDocument", options: {}, I: VectorSearchByTextInDocumentRequest, O: DocumentChunkList }, + { name: "TestStream", serverStreaming: true, options: {}, I: Empty, O: TestStreamResponse } ]);