Comparing version 0.1.4 to 0.1.5
@@ -53,2 +53,3 @@ "use strict"; | ||
abort_controller_x_1.throwIfAborted(signal); | ||
call.cancel(); | ||
if (pipeError) { | ||
@@ -75,15 +76,18 @@ throw pipeError; | ||
return { | ||
async *[Symbol.asyncIterator]() { | ||
while (true) { | ||
const result = await iterator.next(); | ||
if (!result.done) { | ||
yield result.value; | ||
} | ||
else { | ||
if (result.value != null) { | ||
throw new Error('A middleware returned a message, but expected to return void for bidirectional streaming method'); | ||
[Symbol.asyncIterator]() { | ||
return { | ||
async next() { | ||
const result = await iterator.next(); | ||
if (result.done && result.value != null) { | ||
return await iterator.throw(new Error('A middleware returned a message, but expected to return void for bidirectional streaming method')); | ||
} | ||
break; | ||
} | ||
} | ||
return result; | ||
}, | ||
return() { | ||
return iterator.return(); | ||
}, | ||
throw(err) { | ||
return iterator.throw(err); | ||
}, | ||
}; | ||
}, | ||
@@ -90,0 +94,0 @@ }; |
@@ -66,10 +66,14 @@ "use strict"; | ||
const iterator = iterable[Symbol.asyncIterator](); | ||
const result = await iterator.next(); | ||
if (!result.done) { | ||
throw new Error('A middleware yielded a message, but expected to only return a message for client streaming method'); | ||
let result = await iterator.next(); | ||
while (true) { | ||
if (!result.done) { | ||
result = await iterator.throw(new Error('A middleware yielded a message, but expected to only return a message for client streaming method')); | ||
continue; | ||
} | ||
if (result.value == null) { | ||
result = await iterator.throw(new Error('A middleware returned void, but expected to return a message for client streaming method')); | ||
continue; | ||
} | ||
return result.value; | ||
} | ||
if (result.value == null) { | ||
throw new Error('A middleware returned void, but expected to return a message for client streaming method'); | ||
} | ||
return result.value; | ||
}; | ||
@@ -76,0 +80,0 @@ } |
@@ -39,2 +39,3 @@ "use strict"; | ||
abort_controller_x_1.throwIfAborted(signal); | ||
call.cancel(); | ||
} | ||
@@ -58,15 +59,18 @@ } | ||
return { | ||
async *[Symbol.asyncIterator]() { | ||
while (true) { | ||
const result = await iterator.next(); | ||
if (!result.done) { | ||
yield result.value; | ||
} | ||
else { | ||
if (result.value != null) { | ||
throw new Error('A middleware returned a message, but expected to return void for server streaming method'); | ||
[Symbol.asyncIterator]() { | ||
return { | ||
async next() { | ||
const result = await iterator.next(); | ||
if (result.done && result.value != null) { | ||
return await iterator.throw(new Error('A middleware returned a message, but expected to return void for server streaming method')); | ||
} | ||
break; | ||
} | ||
} | ||
return result; | ||
}, | ||
return() { | ||
return iterator.return(); | ||
}, | ||
throw(err) { | ||
return iterator.throw(err); | ||
}, | ||
}; | ||
}, | ||
@@ -73,0 +77,0 @@ }; |
@@ -53,10 +53,14 @@ "use strict"; | ||
const iterator = iterable[Symbol.asyncIterator](); | ||
const result = await iterator.next(); | ||
if (!result.done) { | ||
throw new Error('A middleware yielded a message, but expected to only return a message for unary method'); | ||
let result = await iterator.next(); | ||
while (true) { | ||
if (!result.done) { | ||
result = await iterator.throw(new Error('A middleware yielded a message, but expected to only return a message for unary method')); | ||
continue; | ||
} | ||
if (result.value == null) { | ||
result = await iterator.throw(new Error('A middleware returned void, but expected to return a message for unary method')); | ||
continue; | ||
} | ||
return result.value; | ||
} | ||
if (result.value == null) { | ||
throw new Error('A middleware returned void, but expected to return a message for unary method'); | ||
} | ||
return result.value; | ||
}; | ||
@@ -63,0 +67,0 @@ } |
@@ -32,16 +32,25 @@ "use strict"; | ||
const iterator = iterable[Symbol.asyncIterator](); | ||
let result = await iterator.next(); | ||
while (true) { | ||
const result = await iterator.next(); | ||
if (!result.done) { | ||
const shouldContinue = call.write(result.value); | ||
if (!shouldContinue) { | ||
await abort_controller_x_1.waitForEvent(context.signal, call, 'drain'); | ||
try { | ||
const shouldContinue = call.write(result.value); | ||
if (!shouldContinue) { | ||
await abort_controller_x_1.waitForEvent(context.signal, call, 'drain'); | ||
} | ||
} | ||
} | ||
else { | ||
if (result.value != null) { | ||
throw new Error('A middleware returned a message, but expected to return void for bidirectional streaming method'); | ||
catch (err) { | ||
result = abort_controller_x_1.isAbortError(err) | ||
? await iterator.return() | ||
: await iterator.throw(err); | ||
continue; | ||
} | ||
break; | ||
result = await iterator.next(); | ||
continue; | ||
} | ||
if (result.value != null) { | ||
result = await iterator.throw(new Error('A middleware returned a message, but expected to return void for bidirectional streaming method')); | ||
continue; | ||
} | ||
break; | ||
} | ||
@@ -48,0 +57,0 @@ }) |
@@ -31,10 +31,14 @@ "use strict"; | ||
const iterator = iterable[Symbol.asyncIterator](); | ||
const result = await iterator.next(); | ||
if (!result.done) { | ||
throw new Error('A middleware yielded a message, but expected to only return a message for client streaming method'); | ||
let result = await iterator.next(); | ||
while (true) { | ||
if (!result.done) { | ||
result = await iterator.throw(new Error('A middleware yielded a message, but expected to only return a message for client streaming method')); | ||
continue; | ||
} | ||
if (result.value == null) { | ||
result = await iterator.throw(new Error('A middleware returned void, but expected to return a message for client streaming method')); | ||
continue; | ||
} | ||
return result.value; | ||
} | ||
if (result.value == null) { | ||
throw new Error('A middleware returned void, but expected to return a message for client streaming method'); | ||
} | ||
return result.value; | ||
}) | ||
@@ -41,0 +45,0 @@ .then(res => { |
@@ -31,16 +31,25 @@ "use strict"; | ||
const iterator = iterable[Symbol.asyncIterator](); | ||
let result = await iterator.next(); | ||
while (true) { | ||
const result = await iterator.next(); | ||
if (!result.done) { | ||
const shouldContinue = call.write(result.value); | ||
if (!shouldContinue) { | ||
await abort_controller_x_1.waitForEvent(context.signal, call, 'drain'); | ||
try { | ||
const shouldContinue = call.write(result.value); | ||
if (!shouldContinue) { | ||
await abort_controller_x_1.waitForEvent(context.signal, call, 'drain'); | ||
} | ||
} | ||
} | ||
else { | ||
if (result.value != null) { | ||
throw new Error('A middleware returned a message, but expected to return void for server streaming method'); | ||
catch (err) { | ||
result = abort_controller_x_1.isAbortError(err) | ||
? await iterator.return() | ||
: await iterator.throw(err); | ||
continue; | ||
} | ||
break; | ||
result = await iterator.next(); | ||
continue; | ||
} | ||
if (result.value != null) { | ||
result = await iterator.throw(new Error('A middleware returned a message, but expected to return void for server streaming method')); | ||
continue; | ||
} | ||
break; | ||
} | ||
@@ -47,0 +56,0 @@ }) |
@@ -30,10 +30,14 @@ "use strict"; | ||
const iterator = iterable[Symbol.asyncIterator](); | ||
const result = await iterator.next(); | ||
if (!result.done) { | ||
throw new Error('A middleware yielded a message, but expected to only return a message for unary method'); | ||
let result = await iterator.next(); | ||
while (true) { | ||
if (!result.done) { | ||
result = await iterator.throw(new Error('A middleware yielded a message, but expected to only return a message for unary method')); | ||
continue; | ||
} | ||
if (result.value == null) { | ||
result = await iterator.throw(new Error('A middleware returned void, but expected to return a message for unary method')); | ||
continue; | ||
} | ||
return result.value; | ||
} | ||
if (result.value == null) { | ||
throw new Error('A middleware returned void, but expected to return a message for unary method'); | ||
} | ||
return result.value; | ||
}) | ||
@@ -40,0 +44,0 @@ .then(res => { |
{ | ||
"name": "nice-grpc", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "A gRPC library that is nice to you", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -129,10 +129,13 @@ # nice-grpc [![npm version][npm-image]][npm-url] | ||
import {ServiceImplementation} from 'nice-grpc'; | ||
import {IExampleService} from './compiled_proto/example_grpc_pb'; | ||
import {ExampleRequest, ExampleResponse} from './compiled_proto/example_pb'; | ||
import { | ||
ExampleService, | ||
ExampleRequest, | ||
ExampleResponse, | ||
} from './compiled_proto/example'; | ||
const exampleServiceImpl: ServiceImplementation<IExampleService> = { | ||
const exampleServiceImpl: ServiceImplementation<typeof ExampleService> = { | ||
async exampleUnaryMethod(request: ExampleRequest): Promise<ExampleResponse> { | ||
// ... method logic | ||
return new ExampleResponse(); | ||
return ExampleResponse.fromPartial({}); | ||
}, | ||
@@ -145,7 +148,8 @@ }; | ||
```ts | ||
class ExampleServiceImpl implements ServiceImplementation<IExampleService> { | ||
class ExampleServiceImpl | ||
implements ServiceImplementation<typeof ExampleService> { | ||
async exampleUnaryMethod(request: ExampleRequest): Promise<ExampleResponse> { | ||
// ... method logic | ||
return new ExampleResponse(); | ||
return ExampleResponse.fromPartial({}); | ||
} | ||
@@ -159,3 +163,3 @@ } | ||
import {createServer} from 'nice-grpc'; | ||
import {ExampleService} from './compiled_proto/example_grpc_pb'; | ||
import {ExampleService} from './compiled_proto/example'; | ||
@@ -190,3 +194,3 @@ const server = createServer(); | ||
const exampleServiceImpl: ServiceImplementation<IExampleService> = { | ||
const exampleServiceImpl: ServiceImplementation<typeof ExampleService> = { | ||
async exampleUnaryMethod(request: ExampleRequest): Promise<ExampleResponse> { | ||
@@ -206,3 +210,3 @@ // ... method logic | ||
```ts | ||
const exampleServiceImpl: ServiceImplementation<IExampleService> = { | ||
const exampleServiceImpl: ServiceImplementation<typeof ExampleService> = { | ||
async exampleUnaryMethod( | ||
@@ -223,3 +227,3 @@ request: ExampleRequest, | ||
return new ExampleResponse(); | ||
return ExampleResponse.fromPartial({}); | ||
}, | ||
@@ -239,3 +243,3 @@ }; | ||
const exampleServiceImpl: ServiceImplementation<IExampleService> = { | ||
const exampleServiceImpl: ServiceImplementation<typeof ExampleService> = { | ||
async exampleUnaryMethod( | ||
@@ -270,3 +274,3 @@ request: ExampleRequest, | ||
const exampleServiceImpl: ServiceImplementation<IExampleService> = { | ||
const exampleServiceImpl: ServiceImplementation<typeof ExampleService> = { | ||
async *exampleStreamingMethod( | ||
@@ -279,3 +283,3 @@ request: ExampleRequest, | ||
yield new ExampleResponse(); | ||
yield ExampleResponse.fromPartial({}); | ||
} | ||
@@ -292,3 +296,3 @@ }, | ||
const exampleServiceImpl: ServiceImplementation<IExampleService> = { | ||
const exampleServiceImpl: ServiceImplementation<typeof ExampleService> = { | ||
async *exampleStreamingMethod( | ||
@@ -300,3 +304,3 @@ request: ExampleRequest, | ||
withAbort(context.signal), | ||
map(() => new ExampleResponse()), | ||
map(() => ExampleResponse.fromPartial({})), | ||
); | ||
@@ -314,3 +318,3 @@ }, | ||
const exampleServiceImpl: ServiceImplementation<IExampleService> = { | ||
const exampleServiceImpl: ServiceImplementation<typeof ExampleService> = { | ||
async *exampleStreamingMethod( | ||
@@ -341,3 +345,3 @@ request: ExampleRequest, | ||
```ts | ||
const exampleServiceImpl: ServiceImplementation<IExampleService> = { | ||
const exampleServiceImpl: ServiceImplementation<typeof ExampleService> = { | ||
async exampleUnaryMethod( | ||
@@ -350,3 +354,3 @@ request: AsyncIterable<ExampleRequest>, | ||
return new ExampleResponse(); | ||
return ExampleResponse.fromPartial({}); | ||
}, | ||
@@ -432,2 +436,3 @@ }; | ||
```ts | ||
import {status} from '@grpc/grpc-js'; | ||
import {isAbortError} from 'abort-controller-x'; | ||
@@ -468,2 +473,3 @@ | ||
```ts | ||
import {status} from '@grpc/grpc-js'; | ||
import {isAbortError} from 'abort-controller-x'; | ||
@@ -499,2 +505,3 @@ | ||
```ts | ||
import {status} from '@grpc/grpc-js'; | ||
import createRemoteJWKSet from 'jose/jwks/remote'; | ||
@@ -520,3 +527,3 @@ import jwtVerify, {JWTPayload} from 'jose/jwt/verify'; | ||
throw new ServerError( | ||
grpc.status.UNAUTHENTICATED, | ||
status.UNAUTHENTICATED, | ||
'Missing Authorization metadata', | ||
@@ -530,3 +537,3 @@ ); | ||
throw new ServerError( | ||
grpc.status.UNAUTHENTICATED, | ||
status.UNAUTHENTICATED, | ||
'Invalid Authorization metadata format. Expected "Bearer <token>"', | ||
@@ -540,3 +547,3 @@ ); | ||
if (error instanceof JOSEError) { | ||
throw new ServerError(grpc.status.UNAUTHENTICATED, error.message); | ||
throw new ServerError(status.UNAUTHENTICATED, error.message); | ||
} else { | ||
@@ -558,3 +565,3 @@ throw error; | ||
const exampleServiceImpl: ServiceImplementation< | ||
IExampleService, | ||
typeof ExampleService, | ||
AuthCallContextExt | ||
@@ -603,3 +610,3 @@ > = { | ||
import {createChannel, createClient} from 'nice-grpc'; | ||
import {ExampleService} from './compiled_proto/example_grpc_pb'; | ||
import {ExampleService} from './compiled_proto/example'; | ||
@@ -617,6 +624,6 @@ const channel = createChannel('localhost:8080'); | ||
```ts | ||
import {ExampleRequest, ExampleResponse} from './compiled_proto/example_pb'; | ||
import {ExampleRequest, ExampleResponse} from './compiled_proto/example'; | ||
const response: ExampleResponse = await client.exampleUnaryMethod( | ||
new ExampleRequest(), | ||
ExampleRequest.fromPartial({}), | ||
); | ||
@@ -656,3 +663,3 @@ ``` | ||
Client can send request metadata and receive response headers and trailers: | ||
Client can send request metadata and receive response header and trailer: | ||
@@ -665,11 +672,14 @@ ```ts | ||
const response = await client.exampleUnaryMethod(new ExampleRequest(), { | ||
metadata, | ||
onHeader(header: Metadata) { | ||
// ... | ||
const response = await client.exampleUnaryMethod( | ||
ExampleRequest.fromPartial({}), | ||
{ | ||
metadata, | ||
onHeader(header: Metadata) { | ||
// ... | ||
}, | ||
onTrailer(trailer: Metadata) { | ||
// ... | ||
}, | ||
}, | ||
onTrailer(trailer: Metadata) { | ||
// ... | ||
}, | ||
}); | ||
); | ||
``` | ||
@@ -689,3 +699,3 @@ | ||
try { | ||
response = await client.exampleUnaryMethod(new ExampleRequest()); | ||
response = await client.exampleUnaryMethod(ExampleRequest.fromPartial({})); | ||
} catch (error: unknown) { | ||
@@ -712,3 +722,3 @@ if (error instanceof ClientError && error.code === status.NOT_FOUND) { | ||
client | ||
.exampleUnaryMethod(new ExampleRequest(), { | ||
.exampleUnaryMethod(ExampleRequest.fromPartial({}), { | ||
signal: abortController.signal, | ||
@@ -737,5 +747,8 @@ }) | ||
try { | ||
const response = await client.exampleUnaryMethod(new ExampleRequest(), { | ||
deadline: addSeconds(new Date(), 15), | ||
}); | ||
const response = await client.exampleUnaryMethod( | ||
ExampleRequest.fromPartial({}), | ||
{ | ||
deadline: addSeconds(new Date(), 15), | ||
}, | ||
); | ||
} catch (error: unknown) { | ||
@@ -765,3 +778,3 @@ if (error instanceof ClientError && error.code === status.DEADLINE_EXCEEDED) { | ||
for await (const response of client.exampleStreamingMethod( | ||
new ExampleRequest(), | ||
ExampleRequest.fromPartial({}), | ||
)) { | ||
@@ -788,3 +801,3 @@ // ... | ||
for (let i = 0; i < 10; i++) { | ||
yield new ExampleRequest(); | ||
yield ExampleRequest.fromPartial({}); | ||
} | ||
@@ -854,2 +867,3 @@ } | ||
```ts | ||
import {status} from '@grpc/grpc-js'; | ||
import {ClientMiddlewareCall, CallOptions, ClientError} from 'nice-grpc'; | ||
@@ -935,3 +949,3 @@ import {isAbortError} from 'abort-controller-x'; | ||
```ts | ||
await client.exampleUnaryMethod(new ExampleRequest(), { | ||
await client.exampleUnaryMethod(ExampleRequest.fromPartial({}), { | ||
timeout: '15s', | ||
@@ -938,0 +952,0 @@ }); |
@@ -93,2 +93,4 @@ import { | ||
call.cancel(); | ||
if (pipeError) { | ||
@@ -123,18 +125,24 @@ throw pipeError; | ||
return { | ||
async *[Symbol.asyncIterator]() { | ||
while (true) { | ||
const result = await iterator.next(); | ||
[Symbol.asyncIterator]() { | ||
return { | ||
async next() { | ||
const result = await iterator.next(); | ||
if (!result.done) { | ||
yield result.value; | ||
} else { | ||
if (result.value != null) { | ||
throw new Error( | ||
'A middleware returned a message, but expected to return void for bidirectional streaming method', | ||
if (result.done && result.value != null) { | ||
return await iterator.throw( | ||
new Error( | ||
'A middleware returned a message, but expected to return void for bidirectional streaming method', | ||
), | ||
); | ||
} | ||
break; | ||
} | ||
} | ||
return result; | ||
}, | ||
return() { | ||
return iterator.return(); | ||
}, | ||
throw(err) { | ||
return iterator.throw(err); | ||
}, | ||
}; | ||
}, | ||
@@ -141,0 +149,0 @@ }; |
@@ -118,17 +118,27 @@ import { | ||
const result = await iterator.next(); | ||
let result = await iterator.next(); | ||
if (!result.done) { | ||
throw new Error( | ||
'A middleware yielded a message, but expected to only return a message for client streaming method', | ||
); | ||
} | ||
while (true) { | ||
if (!result.done) { | ||
result = await iterator.throw( | ||
new Error( | ||
'A middleware yielded a message, but expected to only return a message for client streaming method', | ||
), | ||
); | ||
if (result.value == null) { | ||
throw new Error( | ||
'A middleware returned void, but expected to return a message for client streaming method', | ||
); | ||
continue; | ||
} | ||
if (result.value == null) { | ||
result = await iterator.throw( | ||
new Error( | ||
'A middleware returned void, but expected to return a message for client streaming method', | ||
), | ||
); | ||
continue; | ||
} | ||
return result.value; | ||
} | ||
return result.value; | ||
}; | ||
@@ -135,0 +145,0 @@ } |
@@ -67,2 +67,3 @@ import {Client, Metadata, MethodDefinition} from '@grpc/grpc-js'; | ||
throwIfAborted(signal); | ||
call.cancel(); | ||
} | ||
@@ -94,18 +95,24 @@ } | ||
return { | ||
async *[Symbol.asyncIterator]() { | ||
while (true) { | ||
const result = await iterator.next(); | ||
[Symbol.asyncIterator]() { | ||
return { | ||
async next() { | ||
const result = await iterator.next(); | ||
if (!result.done) { | ||
yield result.value; | ||
} else { | ||
if (result.value != null) { | ||
throw new Error( | ||
'A middleware returned a message, but expected to return void for server streaming method', | ||
if (result.done && result.value != null) { | ||
return await iterator.throw( | ||
new Error( | ||
'A middleware returned a message, but expected to return void for server streaming method', | ||
), | ||
); | ||
} | ||
break; | ||
} | ||
} | ||
return result; | ||
}, | ||
return() { | ||
return iterator.return(); | ||
}, | ||
throw(err) { | ||
return iterator.throw(err); | ||
}, | ||
}; | ||
}, | ||
@@ -112,0 +119,0 @@ }; |
@@ -89,18 +89,28 @@ import {Client, Metadata, MethodDefinition} from '@grpc/grpc-js'; | ||
const result = await iterator.next(); | ||
let result = await iterator.next(); | ||
if (!result.done) { | ||
throw new Error( | ||
'A middleware yielded a message, but expected to only return a message for unary method', | ||
); | ||
} | ||
while (true) { | ||
if (!result.done) { | ||
result = await iterator.throw( | ||
new Error( | ||
'A middleware yielded a message, but expected to only return a message for unary method', | ||
), | ||
); | ||
if (result.value == null) { | ||
throw new Error( | ||
'A middleware returned void, but expected to return a message for unary method', | ||
); | ||
continue; | ||
} | ||
if (result.value == null) { | ||
result = await iterator.throw( | ||
new Error( | ||
'A middleware returned void, but expected to return a message for unary method', | ||
), | ||
); | ||
continue; | ||
} | ||
return result.value; | ||
} | ||
return result.value; | ||
}; | ||
} |
import {handleBidiStreamingCall, MethodDefinition} from '@grpc/grpc-js'; | ||
import {waitForEvent} from 'abort-controller-x'; | ||
import {isAbortError, waitForEvent} from 'abort-controller-x'; | ||
import {isAsyncIterable} from '../utils/isAsyncIterable'; | ||
@@ -52,20 +52,36 @@ import {readableToAsyncIterable} from '../utils/readableToAsyncIterable'; | ||
let result = await iterator.next(); | ||
while (true) { | ||
const result = await iterator.next(); | ||
if (!result.done) { | ||
const shouldContinue = call.write(result.value); | ||
try { | ||
const shouldContinue = call.write(result.value); | ||
if (!shouldContinue) { | ||
await waitForEvent(context.signal, call, 'drain'); | ||
if (!shouldContinue) { | ||
await waitForEvent(context.signal, call, 'drain'); | ||
} | ||
} catch (err) { | ||
result = isAbortError(err) | ||
? await iterator.return() | ||
: await iterator.throw(err); | ||
continue; | ||
} | ||
} else { | ||
if (result.value != null) { | ||
throw new Error( | ||
result = await iterator.next(); | ||
continue; | ||
} | ||
if (result.value != null) { | ||
result = await iterator.throw( | ||
new Error( | ||
'A middleware returned a message, but expected to return void for bidirectional streaming method', | ||
); | ||
} | ||
), | ||
); | ||
break; | ||
continue; | ||
} | ||
break; | ||
} | ||
@@ -72,0 +88,0 @@ }) |
@@ -51,17 +51,27 @@ import {handleClientStreamingCall, MethodDefinition} from '@grpc/grpc-js'; | ||
const result = await iterator.next(); | ||
let result = await iterator.next(); | ||
if (!result.done) { | ||
throw new Error( | ||
'A middleware yielded a message, but expected to only return a message for client streaming method', | ||
); | ||
} | ||
while (true) { | ||
if (!result.done) { | ||
result = await iterator.throw( | ||
new Error( | ||
'A middleware yielded a message, but expected to only return a message for client streaming method', | ||
), | ||
); | ||
if (result.value == null) { | ||
throw new Error( | ||
'A middleware returned void, but expected to return a message for client streaming method', | ||
); | ||
continue; | ||
} | ||
if (result.value == null) { | ||
result = await iterator.throw( | ||
new Error( | ||
'A middleware returned void, but expected to return a message for client streaming method', | ||
), | ||
); | ||
continue; | ||
} | ||
return result.value; | ||
} | ||
return result.value; | ||
}) | ||
@@ -68,0 +78,0 @@ .then( |
import {handleServerStreamingCall, MethodDefinition} from '@grpc/grpc-js'; | ||
import {waitForEvent} from 'abort-controller-x'; | ||
import {isAbortError, waitForEvent} from 'abort-controller-x'; | ||
import {isAsyncIterable} from '../utils/isAsyncIterable'; | ||
@@ -51,20 +51,36 @@ import {CallContext, createCallContext} from './CallContext'; | ||
let result = await iterator.next(); | ||
while (true) { | ||
const result = await iterator.next(); | ||
if (!result.done) { | ||
const shouldContinue = call.write(result.value); | ||
try { | ||
const shouldContinue = call.write(result.value); | ||
if (!shouldContinue) { | ||
await waitForEvent(context.signal, call, 'drain'); | ||
if (!shouldContinue) { | ||
await waitForEvent(context.signal, call, 'drain'); | ||
} | ||
} catch (err) { | ||
result = isAbortError(err) | ||
? await iterator.return() | ||
: await iterator.throw(err); | ||
continue; | ||
} | ||
} else { | ||
if (result.value != null) { | ||
throw new Error( | ||
result = await iterator.next(); | ||
continue; | ||
} | ||
if (result.value != null) { | ||
result = await iterator.throw( | ||
new Error( | ||
'A middleware returned a message, but expected to return void for server streaming method', | ||
); | ||
} | ||
), | ||
); | ||
break; | ||
continue; | ||
} | ||
break; | ||
} | ||
@@ -71,0 +87,0 @@ }) |
@@ -47,17 +47,27 @@ import {handleUnaryCall, MethodDefinition} from '@grpc/grpc-js'; | ||
const result = await iterator.next(); | ||
let result = await iterator.next(); | ||
if (!result.done) { | ||
throw new Error( | ||
'A middleware yielded a message, but expected to only return a message for unary method', | ||
); | ||
} | ||
while (true) { | ||
if (!result.done) { | ||
result = await iterator.throw( | ||
new Error( | ||
'A middleware yielded a message, but expected to only return a message for unary method', | ||
), | ||
); | ||
if (result.value == null) { | ||
throw new Error( | ||
'A middleware returned void, but expected to return a message for unary method', | ||
); | ||
continue; | ||
} | ||
if (result.value == null) { | ||
result = await iterator.throw( | ||
new Error( | ||
'A middleware returned void, but expected to return a message for unary method', | ||
), | ||
); | ||
continue; | ||
} | ||
return result.value; | ||
} | ||
return result.value; | ||
}) | ||
@@ -64,0 +74,0 @@ .then( |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
167926
3011
925