Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade connect version #11

Merged
merged 7 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
- run: corepack enable
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'yarn'
cache-dependency-path: 'yarn.lock'
- run: yarn set version stable
- run: yarn
- run: yarn workspace connect-miniprogram run build
- run: yarn workspace example-mina test-build-result
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
- run: corepack enable
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'yarn'
cache-dependency-path: 'yarn.lock'
registry-url: https://registry.npmjs.org/
- run: yarn set version stable
- run: yarn
- run: yarn workspace connect-miniprogram run build
- run: yarn workspace example-mina test-build-result
Expand Down
3 changes: 2 additions & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
nodeLinker: node-modules

npmScopes:
buf:
npmRegistryServer: https://buf.build/gen/npm/v1
npmRegistryServer: 'https://buf.build/gen/npm/v1'
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
"trailingComma": "all",
"useTabs": false
},
"packageManager": "yarn@4.0.1"
"packageManager": "yarn@4.1.1"
}
28 changes: 14 additions & 14 deletions packages/connect-miniprogram/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "connect-miniprogram",
"version": "2.0.3",
"version": "3.0.0",
"main": "dist/index.js",
"files": [
"dist",
Expand All @@ -25,23 +25,23 @@
"format": "eslint ./src --ext ts,tsx --fix && prettier ./src --write"
},
"dependencies": {
"headers-polyfill": "^4.0.2"
"headers-polyfill": "^4.0.3"
},
"peerDependencies": {
"@bufbuild/protobuf": "^1.4.1",
"@connectrpc/connect": "^1.1.3"
"@bufbuild/protobuf": "~1.8.0",
"@connectrpc/connect": "~1.4.0"
},
"devDependencies": {
"@bufbuild/protobuf": "^1.4.1",
"@connectrpc/connect": "^1.1.3",
"@typescript-eslint/eslint-plugin": "^6.9.0",
"@typescript-eslint/parser": "^6.9.0",
"eslint": "^8.52.0",
"eslint-plugin-simple-import-sort": "^10.0.0",
"@bufbuild/protobuf": "^1.8.0",
"@connectrpc/connect": "^1.4.0",
"@typescript-eslint/eslint-plugin": "^7.4.0",
"@typescript-eslint/parser": "^7.4.0",
"eslint": "^8.57.0",
"eslint-plugin-simple-import-sort": "^12.0.0",
"jest": "^29.7.0",
"miniprogram-api-typings": "^3.12.1",
"prettier": "^3.0.3",
"ts-jest": "^29.1.1",
"typescript": "^5.2.2"
"miniprogram-api-typings": "^3.12.2",
"prettier": "^3.2.5",
"ts-jest": "^29.1.2",
"typescript": "^5.4.3"
}
}
71 changes: 67 additions & 4 deletions packages/connect-miniprogram/src/connect/connect-transport-wx.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @see https://github.com/connectrpc/connect-es/blob/main/packages/connect-web/src/connect-transport.ts
*/

import type {
AnyMessage,
JsonValue,
Expand All @@ -6,28 +10,34 @@ import type {
PartialMessage,
ServiceType,
} from '@bufbuild/protobuf';
import { MethodKind } from '@bufbuild/protobuf';
import type {
ContextValues,
StreamResponse,
Transport,
UnaryResponse,
} from '@connectrpc/connect';
import { appendHeaders } from '@connectrpc/connect';
import { ConnectError } from '@connectrpc/connect';
import type { EnvelopedMessage } from '@connectrpc/connect/protocol';
import {
createClientMethodSerializers,
createMethodUrl,
} from '@connectrpc/connect/protocol';
import { encodeEnvelope } from '@connectrpc/connect/protocol';
import {
errorFromJson,
requestHeader,
trailerDemux,
validateResponse,
} from '@connectrpc/connect/protocol-connect';
import {
endStreamFlag,
endStreamFromJson,
} from '@connectrpc/connect/protocol-connect';
import { headersToObject } from 'headers-polyfill';

import { warnUnsupportedOptions } from './compatbility';
import { createRequestBody } from './message-body/create';
import { parseResponseBody } from './message-body/parse-connect';
import { normalize, normalizeIterable } from './protocol/normalize';
import type { CreateTransportOptions } from './types';
import {
Expand Down Expand Up @@ -77,6 +87,7 @@ export function createConnectTransport(
useBinaryFormat,
timeoutMs,
header,
false,
);

reqHeader.delete('User-Agent');
Expand Down Expand Up @@ -139,6 +150,57 @@ export function createConnectTransport(
options.binaryOptions,
);

async function* parseResponseBody(
body: AsyncGenerator<EnvelopedMessage>,
trailerTarget: Headers,
header: Headers,
) {
try {
let endStreamReceived = false;
for (;;) {
const result = await body.next();
if (result.done) {
break;
}
const { flags, data } = result.value;
if ((flags & endStreamFlag) === endStreamFlag) {
endStreamReceived = true;
const endStream = endStreamFromJson(data);
if (endStream.error) {
const error = endStream.error;
header.forEach((value, key) => {
error.metadata.append(key, value);
});
throw error;
}
endStream.metadata.forEach((value, key) =>
trailerTarget.set(key, value),
);
continue;
}
yield parse(data);
}
if (!endStreamReceived) {
throw 'missing EndStreamResponse';
}
} catch (e) {
throw ConnectError.from(e);
}
}

async function createRequestBody(
input: AsyncIterable<I>,
): Promise<Uint8Array> {
if (method.kind != MethodKind.ServerStreaming) {
throw 'Weixin does not support streaming request bodies';
}
const r = await input[Symbol.asyncIterator]().next();
if (r.done == true) {
throw 'missing request message';
}
return encodeEnvelope(0, serialize(r.value));
}

timeoutMs =
timeoutMs === undefined
? options.defaultTimeoutMs
Expand All @@ -152,12 +214,13 @@ export function createConnectTransport(
useBinaryFormat,
timeoutMs,
header,
false,
);

reqHeader.delete('User-Agent');

const reqMessage = normalizeIterable(method.I, input);
const body = await createRequestBody(reqMessage, serialize, method);
const body = await createRequestBody(reqMessage);
const response = await requestAsAsyncIterable({
url,
header: headersToObject(reqHeader),
Expand All @@ -168,7 +231,7 @@ export function createConnectTransport(
const resMessage = parseResponseBody(
response.messageStream,
trailerTarget,
parse,
response.header,
);
return {
service,
Expand Down
7 changes: 7 additions & 0 deletions packages/connect-miniprogram/src/connect/envelope.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/**
* @see https://github.com/connectrpc/connect-es/blob/main/packages/connect/src/protocol/envelope.ts
*/

import { Code, ConnectError } from '@connectrpc/connect';
import type { EnvelopedMessage } from '@connectrpc/connect/protocol';

/**
* basically the same as `createEnvelopeReadableStream` in the upstream code
*/
export async function* createEnvelopeAsyncGenerator(
stream: AsyncGenerator<Uint8Array>,
): AsyncGenerator<EnvelopedMessage> {
Expand Down
Loading