@bufbuild/connect-web
Advanced tools
Comparing version 0.8.6 to 0.9.0
@@ -40,5 +40,6 @@ "use strict"; | ||
function createConnectTransport(options) { | ||
var _a; | ||
var _a, _b; | ||
(0, assert_fetch_api_js_1.assertFetchApi)(); | ||
const useBinaryFormat = (_a = options.useBinaryFormat) !== null && _a !== void 0 ? _a : false; | ||
const fetch = (_b = options.fetch) !== null && _b !== void 0 ? _b : globalThis.fetch; | ||
return { | ||
@@ -48,4 +49,7 @@ async unary(service, method, signal, timeoutMs, header, message) { | ||
const { normalize, serialize, parse } = (0, protocol_1.createClientMethodSerializers)(method, useBinaryFormat, options.jsonOptions, options.binaryOptions); | ||
try { | ||
return await (0, connect_1.runUnary)({ | ||
return await (0, protocol_1.runUnaryCall)({ | ||
interceptors: options.interceptors, | ||
signal, | ||
timeoutMs, | ||
req: { | ||
stream: false, | ||
@@ -63,5 +67,14 @@ service, | ||
message: normalize(message), | ||
signal: signal !== null && signal !== void 0 ? signal : new AbortController().signal, | ||
}, async (req) => { | ||
const response = await fetch(req.url, Object.assign(Object.assign({}, req.init), { headers: req.header, signal: req.signal, body: serialize(req.message) })); | ||
}, | ||
next: async (req) => { | ||
const useGet = options.useHttpGet === true && | ||
method.idempotency === protobuf_1.MethodIdempotency.NoSideEffects; | ||
let body = null; | ||
if (useGet) { | ||
req = (0, protocol_connect_1.transformConnectPostToGetRequest)(req, serialize(req.message), useBinaryFormat); | ||
} | ||
else { | ||
body = serialize(req.message); | ||
} | ||
const response = await fetch(req.url, Object.assign(Object.assign({}, req.init), { headers: req.header, signal: req.signal, body })); | ||
const { isUnaryError, unaryError } = (0, protocol_connect_1.validateResponse)(method.kind, useBinaryFormat, response.status, response.headers); | ||
@@ -80,7 +93,4 @@ if (isUnaryError) { | ||
}; | ||
}, options.interceptors); | ||
} | ||
catch (e) { | ||
throw (0, connect_1.connectErrorFromReason)(e, connect_1.Code.Internal); | ||
} | ||
}, | ||
}); | ||
}, | ||
@@ -93,27 +103,22 @@ async stream(service, method, signal, timeoutMs, header, input) { | ||
const reader = (0, protocol_1.createEnvelopeReadableStream)(body).getReader(); | ||
try { | ||
let endStreamReceived = false; | ||
for (;;) { | ||
const result = yield __await(reader.read()); | ||
if (result.done) { | ||
break; | ||
let endStreamReceived = false; | ||
for (;;) { | ||
const result = yield __await(reader.read()); | ||
if (result.done) { | ||
break; | ||
} | ||
const { flags, data } = result.value; | ||
if ((flags & protocol_connect_1.endStreamFlag) === protocol_connect_1.endStreamFlag) { | ||
endStreamReceived = true; | ||
const endStream = (0, protocol_connect_1.endStreamFromJson)(data); | ||
if (endStream.error) { | ||
throw endStream.error; | ||
} | ||
const { flags, data } = result.value; | ||
if ((flags & protocol_connect_1.endStreamFlag) === protocol_connect_1.endStreamFlag) { | ||
endStreamReceived = true; | ||
const endStream = (0, protocol_connect_1.endStreamFromJson)(data); | ||
if (endStream.error) { | ||
throw endStream.error; | ||
} | ||
endStream.metadata.forEach((value, key) => trailerTarget.set(key, value)); | ||
continue; | ||
} | ||
yield yield __await(parse(data)); | ||
endStream.metadata.forEach((value, key) => trailerTarget.set(key, value)); | ||
continue; | ||
} | ||
if (!endStreamReceived) { | ||
throw "missing EndStreamResponse"; | ||
} | ||
yield yield __await(parse(data)); | ||
} | ||
catch (e) { | ||
throw (0, connect_1.connectErrorFromReason)(e); | ||
if (!endStreamReceived) { | ||
throw "missing EndStreamResponse"; | ||
} | ||
@@ -132,18 +137,21 @@ }); | ||
} | ||
return (0, connect_1.runStreaming)({ | ||
stream: true, | ||
service, | ||
method, | ||
url: (0, protocol_1.createMethodUrl)(options.baseUrl, service, method), | ||
init: { | ||
method: "POST", | ||
credentials: (_a = options.credentials) !== null && _a !== void 0 ? _a : "same-origin", | ||
redirect: "error", | ||
mode: "cors", | ||
return await (0, protocol_1.runStreamingCall)({ | ||
interceptors: options.interceptors, | ||
timeoutMs, | ||
signal, | ||
req: { | ||
stream: true, | ||
service, | ||
method, | ||
url: (0, protocol_1.createMethodUrl)(options.baseUrl, service, method), | ||
init: { | ||
method: "POST", | ||
credentials: (_a = options.credentials) !== null && _a !== void 0 ? _a : "same-origin", | ||
redirect: "error", | ||
mode: "cors", | ||
}, | ||
header: (0, protocol_connect_1.requestHeader)(method.kind, useBinaryFormat, timeoutMs, header), | ||
message: input, | ||
}, | ||
signal: signal !== null && signal !== void 0 ? signal : new AbortController().signal, | ||
header: (0, protocol_connect_1.requestHeader)(method.kind, useBinaryFormat, timeoutMs, header), | ||
message: input, | ||
}, async (req) => { | ||
try { | ||
next: async (req) => { | ||
const fRes = await fetch(req.url, Object.assign(Object.assign({}, req.init), { headers: req.header, signal: req.signal, body: await createRequestBody(req.message) })); | ||
@@ -157,7 +165,4 @@ (0, protocol_connect_1.validateResponse)(method.kind, useBinaryFormat, fRes.status, fRes.headers); | ||
return res; | ||
} | ||
catch (e) { | ||
throw (0, connect_1.connectErrorFromReason)(e, connect_1.Code.Internal); | ||
} | ||
}, options.interceptors).catch((e) => Promise.reject((0, connect_1.connectErrorFromReason)(e))); | ||
}, | ||
}); | ||
}, | ||
@@ -164,0 +169,0 @@ }; |
@@ -30,3 +30,2 @@ "use strict"; | ||
const protobuf_1 = require("@bufbuild/protobuf"); | ||
const connect_1 = require("@bufbuild/connect"); | ||
const protocol_1 = require("@bufbuild/connect/protocol"); | ||
@@ -46,5 +45,6 @@ const protocol_grpc_web_1 = require("@bufbuild/connect/protocol-grpc-web"); | ||
function createGrpcWebTransport(options) { | ||
var _a; | ||
var _a, _b; | ||
(0, assert_fetch_api_js_1.assertFetchApi)(); | ||
const useBinaryFormat = (_a = options.useBinaryFormat) !== null && _a !== void 0 ? _a : true; | ||
const fetch = (_b = options.fetch) !== null && _b !== void 0 ? _b : globalThis.fetch; | ||
return { | ||
@@ -54,4 +54,7 @@ async unary(service, method, signal, timeoutMs, header, message) { | ||
const { normalize, serialize, parse } = (0, protocol_1.createClientMethodSerializers)(method, useBinaryFormat, options.jsonOptions, options.binaryOptions); | ||
try { | ||
return await (0, connect_1.runUnary)({ | ||
return await (0, protocol_1.runUnaryCall)({ | ||
interceptors: options.interceptors, | ||
signal, | ||
timeoutMs, | ||
req: { | ||
stream: false, | ||
@@ -69,4 +72,4 @@ service, | ||
message: normalize(message), | ||
signal: signal !== null && signal !== void 0 ? signal : new AbortController().signal, | ||
}, async (req) => { | ||
}, | ||
next: async (req) => { | ||
const response = await fetch(req.url, Object.assign(Object.assign({}, req.init), { headers: req.header, signal: req.signal, body: (0, protocol_1.encodeEnvelope)(0, serialize(req.message)) })); | ||
@@ -114,7 +117,4 @@ (0, protocol_grpc_web_1.validateResponse)(useBinaryFormat, response.status, response.headers); | ||
}; | ||
}, options.interceptors); | ||
} | ||
catch (e) { | ||
throw (0, connect_1.connectErrorFromReason)(e, connect_1.Code.Internal); | ||
} | ||
}, | ||
}); | ||
}, | ||
@@ -127,46 +127,41 @@ async stream(service, method, signal, timeoutMs, header, input) { | ||
const reader = (0, protocol_1.createEnvelopeReadableStream)(body).getReader(); | ||
try { | ||
if (foundStatus) { | ||
// A grpc-status: 0 response header was present. This is a "trailers-only" | ||
// response (a response without a body and no trailers). | ||
// | ||
// The spec seems to disallow a trailers-only response for status 0 - we are | ||
// lenient and only verify that the body is empty. | ||
// | ||
// > [...] Trailers-Only is permitted for calls that produce an immediate error. | ||
// See https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md | ||
if (!(yield __await(reader.read())).done) { | ||
throw "extra data for trailers-only"; | ||
} | ||
return yield __await(void 0); | ||
if (foundStatus) { | ||
// A grpc-status: 0 response header was present. This is a "trailers-only" | ||
// response (a response without a body and no trailers). | ||
// | ||
// The spec seems to disallow a trailers-only response for status 0 - we are | ||
// lenient and only verify that the body is empty. | ||
// | ||
// > [...] Trailers-Only is permitted for calls that produce an immediate error. | ||
// See https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md | ||
if (!(yield __await(reader.read())).done) { | ||
throw "extra data for trailers-only"; | ||
} | ||
let trailerReceived = false; | ||
for (;;) { | ||
const result = yield __await(reader.read()); | ||
if (result.done) { | ||
break; | ||
} | ||
const { flags, data } = result.value; | ||
if ((flags & protocol_grpc_web_1.trailerFlag) === protocol_grpc_web_1.trailerFlag) { | ||
if (trailerReceived) { | ||
throw "extra trailer"; | ||
} | ||
trailerReceived = true; | ||
const trailer = (0, protocol_grpc_web_1.trailerParse)(data); | ||
(0, protocol_grpc_web_1.validateTrailer)(trailer); | ||
trailer.forEach((value, key) => trailerTarget.set(key, value)); | ||
continue; | ||
} | ||
return yield __await(void 0); | ||
} | ||
let trailerReceived = false; | ||
for (;;) { | ||
const result = yield __await(reader.read()); | ||
if (result.done) { | ||
break; | ||
} | ||
const { flags, data } = result.value; | ||
if ((flags & protocol_grpc_web_1.trailerFlag) === protocol_grpc_web_1.trailerFlag) { | ||
if (trailerReceived) { | ||
throw "extra message"; | ||
throw "extra trailer"; | ||
} | ||
yield yield __await(parse(data)); | ||
trailerReceived = true; | ||
const trailer = (0, protocol_grpc_web_1.trailerParse)(data); | ||
(0, protocol_grpc_web_1.validateTrailer)(trailer); | ||
trailer.forEach((value, key) => trailerTarget.set(key, value)); | ||
continue; | ||
} | ||
if (!trailerReceived) { | ||
throw "missing trailer"; | ||
if (trailerReceived) { | ||
throw "extra message"; | ||
} | ||
yield yield __await(parse(data)); | ||
continue; | ||
} | ||
catch (e) { | ||
throw (0, connect_1.connectErrorFromReason)(e); | ||
if (!trailerReceived) { | ||
throw "missing trailer"; | ||
} | ||
@@ -185,26 +180,31 @@ }); | ||
} | ||
return (0, connect_1.runStreaming)({ | ||
stream: true, | ||
service, | ||
method, | ||
url: (0, protocol_1.createMethodUrl)(options.baseUrl, service, method), | ||
init: { | ||
method: "POST", | ||
credentials: (_a = options.credentials) !== null && _a !== void 0 ? _a : "same-origin", | ||
redirect: "error", | ||
mode: "cors", | ||
return (0, protocol_1.runStreamingCall)({ | ||
interceptors: options.interceptors, | ||
signal, | ||
timeoutMs, | ||
req: { | ||
stream: true, | ||
service, | ||
method, | ||
url: (0, protocol_1.createMethodUrl)(options.baseUrl, service, method), | ||
init: { | ||
method: "POST", | ||
credentials: (_a = options.credentials) !== null && _a !== void 0 ? _a : "same-origin", | ||
redirect: "error", | ||
mode: "cors", | ||
}, | ||
header: (0, protocol_grpc_web_1.requestHeader)(useBinaryFormat, timeoutMs, header), | ||
message: input, | ||
}, | ||
signal: signal !== null && signal !== void 0 ? signal : new AbortController().signal, | ||
header: (0, protocol_grpc_web_1.requestHeader)(useBinaryFormat, timeoutMs, header), | ||
message: input, | ||
}, async (req) => { | ||
const fRes = await fetch(req.url, Object.assign(Object.assign({}, req.init), { headers: req.header, signal: req.signal, body: await createRequestBody(req.message) })); | ||
const { foundStatus } = (0, protocol_grpc_web_1.validateResponse)(useBinaryFormat, fRes.status, fRes.headers); | ||
if (!fRes.body) { | ||
throw "missing response body"; | ||
} | ||
const trailer = new Headers(); | ||
const res = Object.assign(Object.assign({}, req), { header: fRes.headers, trailer, message: parseResponseBody(fRes.body, foundStatus, trailer) }); | ||
return res; | ||
}, options.interceptors).catch((e) => Promise.reject((0, connect_1.connectErrorFromReason)(e))); | ||
next: async (req) => { | ||
const fRes = await fetch(req.url, Object.assign(Object.assign({}, req.init), { headers: req.header, signal: req.signal, body: await createRequestBody(req.message) })); | ||
const { foundStatus } = (0, protocol_grpc_web_1.validateResponse)(useBinaryFormat, fRes.status, fRes.headers); | ||
if (!fRes.body) { | ||
throw "missing response body"; | ||
} | ||
const trailer = new Headers(); | ||
const res = Object.assign(Object.assign({}, req), { header: fRes.headers, trailer, message: parseResponseBody(fRes.body, foundStatus, trailer) }); | ||
return res; | ||
}, | ||
}); | ||
}, | ||
@@ -211,0 +211,0 @@ }; |
@@ -16,3 +16,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.decodeBinaryHeader = exports.encodeBinaryHeader = exports.Code = exports.connectErrorFromReason = exports.connectErrorDetails = exports.ConnectError = exports.createCallbackClient = exports.createPromiseClient = exports.createGrpcWebTransport = exports.createConnectTransport = void 0; | ||
exports.createGrpcWebTransport = exports.createConnectTransport = void 0; | ||
var connect_transport_js_1 = require("./connect-transport.js"); | ||
@@ -22,13 +22,1 @@ Object.defineProperty(exports, "createConnectTransport", { enumerable: true, get: function () { return connect_transport_js_1.createConnectTransport; } }); | ||
Object.defineProperty(exports, "createGrpcWebTransport", { enumerable: true, get: function () { return grpc_web_transport_js_1.createGrpcWebTransport; } }); | ||
// all of the following exports will be removed in a future release | ||
// please please import from @bufbuild/connect instead | ||
// TODO(TCN-1261) | ||
const connect_1 = require("@bufbuild/connect"); | ||
Object.defineProperty(exports, "Code", { enumerable: true, get: function () { return connect_1.Code; } }); | ||
Object.defineProperty(exports, "ConnectError", { enumerable: true, get: function () { return connect_1.ConnectError; } }); | ||
Object.defineProperty(exports, "connectErrorDetails", { enumerable: true, get: function () { return connect_1.connectErrorDetails; } }); | ||
Object.defineProperty(exports, "connectErrorFromReason", { enumerable: true, get: function () { return connect_1.connectErrorFromReason; } }); | ||
Object.defineProperty(exports, "createCallbackClient", { enumerable: true, get: function () { return connect_1.createCallbackClient; } }); | ||
Object.defineProperty(exports, "createPromiseClient", { enumerable: true, get: function () { return connect_1.createPromiseClient; } }); | ||
Object.defineProperty(exports, "decodeBinaryHeader", { enumerable: true, get: function () { return connect_1.decodeBinaryHeader; } }); | ||
Object.defineProperty(exports, "encodeBinaryHeader", { enumerable: true, get: function () { return connect_1.encodeBinaryHeader; } }); |
@@ -26,6 +26,6 @@ // Copyright 2021-2023 Buf Technologies, Inc. | ||
}; | ||
import { Message, MethodKind } from "@bufbuild/protobuf"; | ||
import { appendHeaders, Code, connectErrorFromReason, runStreaming, runUnary, } from "@bufbuild/connect"; | ||
import { createClientMethodSerializers, createEnvelopeReadableStream, createMethodUrl, encodeEnvelope, } from "@bufbuild/connect/protocol"; | ||
import { requestHeader, endStreamFlag, endStreamFromJson, errorFromJson, trailerDemux, validateResponse, } from "@bufbuild/connect/protocol-connect"; | ||
import { Message, MethodIdempotency, MethodKind } from "@bufbuild/protobuf"; | ||
import { appendHeaders } from "@bufbuild/connect"; | ||
import { createClientMethodSerializers, createEnvelopeReadableStream, createMethodUrl, encodeEnvelope, runStreamingCall, runUnaryCall, } from "@bufbuild/connect/protocol"; | ||
import { endStreamFlag, endStreamFromJson, errorFromJson, requestHeader, trailerDemux, transformConnectPostToGetRequest, validateResponse, } from "@bufbuild/connect/protocol-connect"; | ||
import { assertFetchApi } from "./assert-fetch-api.js"; | ||
@@ -38,5 +38,6 @@ /** | ||
export function createConnectTransport(options) { | ||
var _a; | ||
var _a, _b; | ||
assertFetchApi(); | ||
const useBinaryFormat = (_a = options.useBinaryFormat) !== null && _a !== void 0 ? _a : false; | ||
const fetch = (_b = options.fetch) !== null && _b !== void 0 ? _b : globalThis.fetch; | ||
return { | ||
@@ -46,4 +47,7 @@ async unary(service, method, signal, timeoutMs, header, message) { | ||
const { normalize, serialize, parse } = createClientMethodSerializers(method, useBinaryFormat, options.jsonOptions, options.binaryOptions); | ||
try { | ||
return await runUnary({ | ||
return await runUnaryCall({ | ||
interceptors: options.interceptors, | ||
signal, | ||
timeoutMs, | ||
req: { | ||
stream: false, | ||
@@ -61,5 +65,14 @@ service, | ||
message: normalize(message), | ||
signal: signal !== null && signal !== void 0 ? signal : new AbortController().signal, | ||
}, async (req) => { | ||
const response = await fetch(req.url, Object.assign(Object.assign({}, req.init), { headers: req.header, signal: req.signal, body: serialize(req.message) })); | ||
}, | ||
next: async (req) => { | ||
const useGet = options.useHttpGet === true && | ||
method.idempotency === MethodIdempotency.NoSideEffects; | ||
let body = null; | ||
if (useGet) { | ||
req = transformConnectPostToGetRequest(req, serialize(req.message), useBinaryFormat); | ||
} | ||
else { | ||
body = serialize(req.message); | ||
} | ||
const response = await fetch(req.url, Object.assign(Object.assign({}, req.init), { headers: req.header, signal: req.signal, body })); | ||
const { isUnaryError, unaryError } = validateResponse(method.kind, useBinaryFormat, response.status, response.headers); | ||
@@ -78,7 +91,4 @@ if (isUnaryError) { | ||
}; | ||
}, options.interceptors); | ||
} | ||
catch (e) { | ||
throw connectErrorFromReason(e, Code.Internal); | ||
} | ||
}, | ||
}); | ||
}, | ||
@@ -91,27 +101,22 @@ async stream(service, method, signal, timeoutMs, header, input) { | ||
const reader = createEnvelopeReadableStream(body).getReader(); | ||
try { | ||
let endStreamReceived = false; | ||
for (;;) { | ||
const result = yield __await(reader.read()); | ||
if (result.done) { | ||
break; | ||
let endStreamReceived = false; | ||
for (;;) { | ||
const result = yield __await(reader.read()); | ||
if (result.done) { | ||
break; | ||
} | ||
const { flags, data } = result.value; | ||
if ((flags & endStreamFlag) === endStreamFlag) { | ||
endStreamReceived = true; | ||
const endStream = endStreamFromJson(data); | ||
if (endStream.error) { | ||
throw endStream.error; | ||
} | ||
const { flags, data } = result.value; | ||
if ((flags & endStreamFlag) === endStreamFlag) { | ||
endStreamReceived = true; | ||
const endStream = endStreamFromJson(data); | ||
if (endStream.error) { | ||
throw endStream.error; | ||
} | ||
endStream.metadata.forEach((value, key) => trailerTarget.set(key, value)); | ||
continue; | ||
} | ||
yield yield __await(parse(data)); | ||
endStream.metadata.forEach((value, key) => trailerTarget.set(key, value)); | ||
continue; | ||
} | ||
if (!endStreamReceived) { | ||
throw "missing EndStreamResponse"; | ||
} | ||
yield yield __await(parse(data)); | ||
} | ||
catch (e) { | ||
throw connectErrorFromReason(e); | ||
if (!endStreamReceived) { | ||
throw "missing EndStreamResponse"; | ||
} | ||
@@ -130,18 +135,21 @@ }); | ||
} | ||
return runStreaming({ | ||
stream: true, | ||
service, | ||
method, | ||
url: createMethodUrl(options.baseUrl, service, method), | ||
init: { | ||
method: "POST", | ||
credentials: (_a = options.credentials) !== null && _a !== void 0 ? _a : "same-origin", | ||
redirect: "error", | ||
mode: "cors", | ||
return await runStreamingCall({ | ||
interceptors: options.interceptors, | ||
timeoutMs, | ||
signal, | ||
req: { | ||
stream: true, | ||
service, | ||
method, | ||
url: createMethodUrl(options.baseUrl, service, method), | ||
init: { | ||
method: "POST", | ||
credentials: (_a = options.credentials) !== null && _a !== void 0 ? _a : "same-origin", | ||
redirect: "error", | ||
mode: "cors", | ||
}, | ||
header: requestHeader(method.kind, useBinaryFormat, timeoutMs, header), | ||
message: input, | ||
}, | ||
signal: signal !== null && signal !== void 0 ? signal : new AbortController().signal, | ||
header: requestHeader(method.kind, useBinaryFormat, timeoutMs, header), | ||
message: input, | ||
}, async (req) => { | ||
try { | ||
next: async (req) => { | ||
const fRes = await fetch(req.url, Object.assign(Object.assign({}, req.init), { headers: req.header, signal: req.signal, body: await createRequestBody(req.message) })); | ||
@@ -155,9 +163,6 @@ validateResponse(method.kind, useBinaryFormat, fRes.status, fRes.headers); | ||
return res; | ||
} | ||
catch (e) { | ||
throw connectErrorFromReason(e, Code.Internal); | ||
} | ||
}, options.interceptors).catch((e) => Promise.reject(connectErrorFromReason(e))); | ||
}, | ||
}); | ||
}, | ||
}; | ||
} |
@@ -27,4 +27,3 @@ // Copyright 2021-2023 Buf Technologies, Inc. | ||
import { Message, MethodKind } from "@bufbuild/protobuf"; | ||
import { Code, connectErrorFromReason, runStreaming, runUnary, } from "@bufbuild/connect"; | ||
import { createClientMethodSerializers, createEnvelopeReadableStream, createMethodUrl, encodeEnvelope, } from "@bufbuild/connect/protocol"; | ||
import { createClientMethodSerializers, createEnvelopeReadableStream, createMethodUrl, encodeEnvelope, runStreamingCall, runUnaryCall, } from "@bufbuild/connect/protocol"; | ||
import { requestHeader, trailerFlag, trailerParse, validateResponse, validateTrailer, } from "@bufbuild/connect/protocol-grpc-web"; | ||
@@ -43,5 +42,6 @@ import { assertFetchApi } from "./assert-fetch-api.js"; | ||
export function createGrpcWebTransport(options) { | ||
var _a; | ||
var _a, _b; | ||
assertFetchApi(); | ||
const useBinaryFormat = (_a = options.useBinaryFormat) !== null && _a !== void 0 ? _a : true; | ||
const fetch = (_b = options.fetch) !== null && _b !== void 0 ? _b : globalThis.fetch; | ||
return { | ||
@@ -51,4 +51,7 @@ async unary(service, method, signal, timeoutMs, header, message) { | ||
const { normalize, serialize, parse } = createClientMethodSerializers(method, useBinaryFormat, options.jsonOptions, options.binaryOptions); | ||
try { | ||
return await runUnary({ | ||
return await runUnaryCall({ | ||
interceptors: options.interceptors, | ||
signal, | ||
timeoutMs, | ||
req: { | ||
stream: false, | ||
@@ -66,4 +69,4 @@ service, | ||
message: normalize(message), | ||
signal: signal !== null && signal !== void 0 ? signal : new AbortController().signal, | ||
}, async (req) => { | ||
}, | ||
next: async (req) => { | ||
const response = await fetch(req.url, Object.assign(Object.assign({}, req.init), { headers: req.header, signal: req.signal, body: encodeEnvelope(0, serialize(req.message)) })); | ||
@@ -111,7 +114,4 @@ validateResponse(useBinaryFormat, response.status, response.headers); | ||
}; | ||
}, options.interceptors); | ||
} | ||
catch (e) { | ||
throw connectErrorFromReason(e, Code.Internal); | ||
} | ||
}, | ||
}); | ||
}, | ||
@@ -124,46 +124,41 @@ async stream(service, method, signal, timeoutMs, header, input) { | ||
const reader = createEnvelopeReadableStream(body).getReader(); | ||
try { | ||
if (foundStatus) { | ||
// A grpc-status: 0 response header was present. This is a "trailers-only" | ||
// response (a response without a body and no trailers). | ||
// | ||
// The spec seems to disallow a trailers-only response for status 0 - we are | ||
// lenient and only verify that the body is empty. | ||
// | ||
// > [...] Trailers-Only is permitted for calls that produce an immediate error. | ||
// See https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md | ||
if (!(yield __await(reader.read())).done) { | ||
throw "extra data for trailers-only"; | ||
} | ||
return yield __await(void 0); | ||
if (foundStatus) { | ||
// A grpc-status: 0 response header was present. This is a "trailers-only" | ||
// response (a response without a body and no trailers). | ||
// | ||
// The spec seems to disallow a trailers-only response for status 0 - we are | ||
// lenient and only verify that the body is empty. | ||
// | ||
// > [...] Trailers-Only is permitted for calls that produce an immediate error. | ||
// See https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md | ||
if (!(yield __await(reader.read())).done) { | ||
throw "extra data for trailers-only"; | ||
} | ||
let trailerReceived = false; | ||
for (;;) { | ||
const result = yield __await(reader.read()); | ||
if (result.done) { | ||
break; | ||
} | ||
const { flags, data } = result.value; | ||
if ((flags & trailerFlag) === trailerFlag) { | ||
if (trailerReceived) { | ||
throw "extra trailer"; | ||
} | ||
trailerReceived = true; | ||
const trailer = trailerParse(data); | ||
validateTrailer(trailer); | ||
trailer.forEach((value, key) => trailerTarget.set(key, value)); | ||
continue; | ||
} | ||
return yield __await(void 0); | ||
} | ||
let trailerReceived = false; | ||
for (;;) { | ||
const result = yield __await(reader.read()); | ||
if (result.done) { | ||
break; | ||
} | ||
const { flags, data } = result.value; | ||
if ((flags & trailerFlag) === trailerFlag) { | ||
if (trailerReceived) { | ||
throw "extra message"; | ||
throw "extra trailer"; | ||
} | ||
yield yield __await(parse(data)); | ||
trailerReceived = true; | ||
const trailer = trailerParse(data); | ||
validateTrailer(trailer); | ||
trailer.forEach((value, key) => trailerTarget.set(key, value)); | ||
continue; | ||
} | ||
if (!trailerReceived) { | ||
throw "missing trailer"; | ||
if (trailerReceived) { | ||
throw "extra message"; | ||
} | ||
yield yield __await(parse(data)); | ||
continue; | ||
} | ||
catch (e) { | ||
throw connectErrorFromReason(e); | ||
if (!trailerReceived) { | ||
throw "missing trailer"; | ||
} | ||
@@ -182,28 +177,33 @@ }); | ||
} | ||
return runStreaming({ | ||
stream: true, | ||
service, | ||
method, | ||
url: createMethodUrl(options.baseUrl, service, method), | ||
init: { | ||
method: "POST", | ||
credentials: (_a = options.credentials) !== null && _a !== void 0 ? _a : "same-origin", | ||
redirect: "error", | ||
mode: "cors", | ||
return runStreamingCall({ | ||
interceptors: options.interceptors, | ||
signal, | ||
timeoutMs, | ||
req: { | ||
stream: true, | ||
service, | ||
method, | ||
url: createMethodUrl(options.baseUrl, service, method), | ||
init: { | ||
method: "POST", | ||
credentials: (_a = options.credentials) !== null && _a !== void 0 ? _a : "same-origin", | ||
redirect: "error", | ||
mode: "cors", | ||
}, | ||
header: requestHeader(useBinaryFormat, timeoutMs, header), | ||
message: input, | ||
}, | ||
signal: signal !== null && signal !== void 0 ? signal : new AbortController().signal, | ||
header: requestHeader(useBinaryFormat, timeoutMs, header), | ||
message: input, | ||
}, async (req) => { | ||
const fRes = await fetch(req.url, Object.assign(Object.assign({}, req.init), { headers: req.header, signal: req.signal, body: await createRequestBody(req.message) })); | ||
const { foundStatus } = validateResponse(useBinaryFormat, fRes.status, fRes.headers); | ||
if (!fRes.body) { | ||
throw "missing response body"; | ||
} | ||
const trailer = new Headers(); | ||
const res = Object.assign(Object.assign({}, req), { header: fRes.headers, trailer, message: parseResponseBody(fRes.body, foundStatus, trailer) }); | ||
return res; | ||
}, options.interceptors).catch((e) => Promise.reject(connectErrorFromReason(e))); | ||
next: async (req) => { | ||
const fRes = await fetch(req.url, Object.assign(Object.assign({}, req.init), { headers: req.header, signal: req.signal, body: await createRequestBody(req.message) })); | ||
const { foundStatus } = validateResponse(useBinaryFormat, fRes.status, fRes.headers); | ||
if (!fRes.body) { | ||
throw "missing response body"; | ||
} | ||
const trailer = new Headers(); | ||
const res = Object.assign(Object.assign({}, req), { header: fRes.headers, trailer, message: parseResponseBody(fRes.body, foundStatus, trailer) }); | ||
return res; | ||
}, | ||
}); | ||
}, | ||
}; | ||
} |
@@ -16,38 +16,1 @@ // Copyright 2021-2023 Buf Technologies, Inc. | ||
export { createGrpcWebTransport } from "./grpc-web-transport.js"; | ||
// all of the following exports will be removed in a future release | ||
// please please import from @bufbuild/connect instead | ||
// TODO(TCN-1261) | ||
import { Code, ConnectError, connectErrorDetails, connectErrorFromReason, createCallbackClient, createPromiseClient, decodeBinaryHeader, encodeBinaryHeader, } from "@bufbuild/connect"; | ||
export { | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
createPromiseClient, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
createCallbackClient, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
ConnectError, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
connectErrorDetails, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
connectErrorFromReason, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
Code, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
encodeBinaryHeader, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
decodeBinaryHeader, }; |
@@ -46,2 +46,11 @@ import type { BinaryReadOptions, BinaryWriteOptions, JsonReadOptions, JsonWriteOptions } from "@bufbuild/protobuf"; | ||
binaryOptions?: Partial<BinaryReadOptions & BinaryWriteOptions>; | ||
/** | ||
* Optional override of the fetch implementation used by the transport. | ||
*/ | ||
fetch?: typeof globalThis.fetch; | ||
/** | ||
* Controls whether or not Connect GET requests should be used when | ||
* available, on side-effect free methods. Defaults to false. | ||
*/ | ||
useHttpGet?: boolean; | ||
} | ||
@@ -48,0 +57,0 @@ /** |
@@ -47,2 +47,6 @@ import type { BinaryReadOptions, BinaryWriteOptions, JsonReadOptions, JsonWriteOptions } from "@bufbuild/protobuf"; | ||
binaryOptions?: Partial<BinaryReadOptions & BinaryWriteOptions>; | ||
/** | ||
* Optional override of the fetch implementation used by the transport. | ||
*/ | ||
fetch?: typeof globalThis.fetch; | ||
} | ||
@@ -49,0 +53,0 @@ /** |
@@ -5,73 +5,1 @@ export { createConnectTransport } from "./connect-transport.js"; | ||
export type { GrpcWebTransportOptions } from "./grpc-web-transport.js"; | ||
import { Code, ConnectError, connectErrorDetails, connectErrorFromReason, createCallbackClient, createPromiseClient, decodeBinaryHeader, encodeBinaryHeader } from "@bufbuild/connect"; | ||
import type { CallbackClient, CallOptions, Interceptor, PromiseClient, StreamRequest, StreamResponse, Transport, UnaryRequest, UnaryResponse } from "@bufbuild/connect"; | ||
export { | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
createPromiseClient, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
createCallbackClient, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
ConnectError, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
connectErrorDetails, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
connectErrorFromReason, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
Code, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
encodeBinaryHeader, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
decodeBinaryHeader, }; | ||
export type { | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
CallbackClient, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
PromiseClient, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
CallOptions, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
Transport, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
Interceptor, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
UnaryRequest, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
UnaryResponse, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
StreamRequest, | ||
/** | ||
* @deprecated please import from \@bufbuild/connect instead | ||
*/ | ||
StreamResponse, }; |
{ | ||
"name": "@bufbuild/connect-web", | ||
"version": "0.8.6", | ||
"version": "0.9.0", | ||
"license": "Apache-2.0", | ||
@@ -26,3 +26,3 @@ "repository": { | ||
"dependencies": { | ||
"@bufbuild/connect": "0.8.6" | ||
"@bufbuild/connect": "0.9.0" | ||
}, | ||
@@ -29,0 +29,0 @@ "peerDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51344
950
+ Added@bufbuild/connect@0.9.0(transitive)
- Removed@bufbuild/connect@0.8.6(transitive)
Updated@bufbuild/connect@0.9.0