Skip to content

Commit

Permalink
Update dependency version (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangcheng authored Aug 5, 2023
1 parent e01c243 commit 4f1129c
Show file tree
Hide file tree
Showing 11 changed files with 1,896 additions and 390 deletions.
22 changes: 22 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/recommended"
],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint", "import"],
"rules": {
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/consistent-type-imports": "error",
"import/order": "error"
},
"root": true,
"ignorePatterns": ["*.js", "dist"],
"settings": {
"import/resolver": {
"typescript": true,
"node": true
}
}
}
21 changes: 13 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@
"headers-polyfill": "^3.1.2"
},
"peerDependencies": {
"@bufbuild/connect": "^0.8.6",
"@bufbuild/protobuf": "^1.2.0"
"@bufbuild/connect": "^0.9.0",
"@bufbuild/protobuf": "^1.0.0"
},
"devDependencies": {
"@bufbuild/connect": "^0.8.6",
"@bufbuild/protobuf": "^1.2.0",
"jest": "^29.5.0",
"miniprogram-api-typings": "^3.9.1",
"ts-jest": "^29.1.0",
"typescript": "^5.0.4"
"@bufbuild/connect": "^0.12.0",
"@bufbuild/protobuf": "^1.3.0",
"@typescript-eslint/eslint-plugin": "^6.2.1",
"@typescript-eslint/parser": "^6.2.1",
"eslint": "^8.46.0",
"eslint-import-resolver-typescript": "^3.5.5",
"eslint-plugin-import": "^2.28.0",
"jest": "^29.6.2",
"miniprogram-api-typings": "^3.11.0",
"ts-jest": "^29.1.1",
"typescript": "^5.1.6"
}
}
18 changes: 7 additions & 11 deletions src/connect/connect-transport-wx.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { headersToObject } from 'headers-polyfill';
import { Message } from '@bufbuild/protobuf';
import type {
Message,
AnyMessage,
JsonValue,
MethodInfo,
PartialMessage,
ServiceType,
} from '@bufbuild/protobuf';
import { Code, appendHeaders, connectErrorFromReason } from '@bufbuild/connect';
import { Code, appendHeaders, ConnectError } from '@bufbuild/connect';
import type {
StreamResponse,
Transport,
Expand All @@ -18,16 +18,16 @@ import {
createMethodUrl,
} from '@bufbuild/connect/protocol';
import {
errorFromJson,
requestHeader,
validateResponse,
trailerDemux,
errorFromJson,
validateResponse,
} from '@bufbuild/connect/protocol-connect';

import { createWxRequestAsAsyncGenerator } from './wx-request';
import { parseResponseBody } from './message-body/parse-connect';
import { createRequestBody } from './message-body/create';
import { CreateTransportOptions } from './types';
import type { CreateTransportOptions } from './types';

export function createConnectTransport(
options: CreateTransportOptions,
Expand Down Expand Up @@ -61,7 +61,6 @@ export function createConnectTransport(
const headers = new Headers(response.header);
const { isUnaryError, unaryError } = validateResponse(
method.kind,
useBinaryFormat,
response.statusCode,
headers,
);
Expand Down Expand Up @@ -95,16 +94,13 @@ export function createConnectTransport(
...options.requestOptions,
success: onSuccess,
fail: (e) => {
reject(connectErrorFromReason(e, Code.Internal));
reject(ConnectError.from(e, Code.Internal));
},
});
});
}

const requestAsAsyncIterable = createWxRequestAsAsyncGenerator(
options.request,
options.requestOptions,
);
const requestAsAsyncIterable = createWxRequestAsAsyncGenerator(options);

async function stream<I extends Message<I>, O extends Message<O>>(
service: ServiceType,
Expand Down
2 changes: 1 addition & 1 deletion src/connect/envelope.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EnvelopedMessage } from '@bufbuild/connect/protocol';
import type { EnvelopedMessage } from '@bufbuild/connect/protocol';
import { Code, ConnectError } from '@bufbuild/connect';

export async function* createEnvelopeAsyncGenerator(
Expand Down
17 changes: 5 additions & 12 deletions src/connect/grpc-web-transport-wx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { headersToObject } from 'headers-polyfill';
import { Message } from '@bufbuild/protobuf';
import type {
Message,
AnyMessage,
MethodInfo,
PartialMessage,
Expand All @@ -21,7 +21,7 @@ import {
validateResponse,
} from '@bufbuild/connect/protocol-grpc-web';

import { CreateTransportOptions } from './types';
import type { CreateTransportOptions } from './types';
import { createWxRequestAsAsyncGenerator } from './wx-request';
import {
parseStreamResponseBody,
Expand All @@ -34,10 +34,7 @@ export function createGrpcWebTransport(
): Transport {
const useBinaryFormat = options.useBinaryFormat ?? true;

const requestAsAsyncIterable = createWxRequestAsAsyncGenerator(
options.request,
options.requestOptions,
);
const requestAsAsyncIterable = createWxRequestAsAsyncGenerator(options);

async function unary<
I extends Message<I> = AnyMessage,
Expand Down Expand Up @@ -69,7 +66,7 @@ export function createGrpcWebTransport(
data: req.buffer,
});

validateResponse(useBinaryFormat, statusCode, header);
validateResponse(statusCode, header);

const { trailer, message } = await parseUaryResponseBody(
messageStream,
Expand Down Expand Up @@ -112,11 +109,7 @@ export function createGrpcWebTransport(
data: body.buffer,
});

const { foundStatus } = validateResponse(
useBinaryFormat,
statusCode,
header,
);
const { foundStatus } = validateResponse(statusCode, header);

const trailerTarget = new Headers();

Expand Down
6 changes: 3 additions & 3 deletions src/connect/message-body/parse-connect.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { EnvelopedMessage } from '@bufbuild/connect/protocol';
import type { EnvelopedMessage } from '@bufbuild/connect/protocol';
import {
endStreamFlag,
endStreamFromJson,
} from '@bufbuild/connect/protocol-connect';
import { connectErrorFromReason } from '@bufbuild/connect';
import { ConnectError } from '@bufbuild/connect';

export async function* parseResponseBody<O>(
body: AsyncGenerator<EnvelopedMessage>,
Expand Down Expand Up @@ -35,6 +35,6 @@ export async function* parseResponseBody<O>(
throw 'missing EndStreamResponse';
}
} catch (e) {
throw connectErrorFromReason(e);
throw ConnectError.from(e);
}
}
6 changes: 3 additions & 3 deletions src/connect/message-body/parse-grpc.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { EnvelopedMessage } from '@bufbuild/connect/protocol';
import type { EnvelopedMessage } from '@bufbuild/connect/protocol';
import {
trailerFlag,
trailerParse,
validateTrailer,
} from '@bufbuild/connect/protocol-grpc-web';
import { connectErrorFromReason } from '@bufbuild/connect';
import { ConnectError } from '@bufbuild/connect';

export async function* parseStreamResponseBody<O>(
input: AsyncGenerator<EnvelopedMessage>,
Expand Down Expand Up @@ -53,7 +53,7 @@ export async function* parseStreamResponseBody<O>(
throw 'missing trailer';
}
} catch (e) {
throw connectErrorFromReason(e);
throw ConnectError.from(e);
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/connect/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export interface CreateTransportOptions {
*/
request: typeof wx.request;

/**
* Weixin devtool has a bug if enableChunked is true.
* https://developers.weixin.qq.com/community/develop/doc/000e44fc464560a0a6bf4188f56800
*/
isDevTool?: boolean;

/**
* Base URI for all HTTP requests.
*
Expand Down
14 changes: 7 additions & 7 deletions src/connect/wx-request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AdditionalRequestOptions } from './types';
import type { AdditionalRequestOptions, CreateTransportOptions } from './types';
import { createEnvelopeAsyncGenerator } from './envelope';
import { createAsyncGeneratorFromEventPattern } from './async-generator';

Expand All @@ -22,7 +22,7 @@ type RequestEvent = HeadersReceivedEvent | ChunkReceivedEvent;

export class WeixinRequestError extends Error {
errno: number;
constructor(err: WechatMiniprogram.Err) {
constructor(err: WechatMiniprogram.RequestFailCallbackErr) {
super(err.errMsg);
this.errno = err.errno;
}
Expand Down Expand Up @@ -116,15 +116,15 @@ async function demuxStream(iterator: AsyncGenerator<RequestEvent>) {
};
}

export function createWxRequestAsAsyncGenerator(
request: typeof wx.request,
requestOptions?: AdditionalRequestOptions,
) {
export function createWxRequestAsAsyncGenerator({
request,
isDevTool,
requestOptions,
}: CreateTransportOptions) {
/**
* Weixin devtool has a bug if enableChunked is true.
* https://developers.weixin.qq.com/community/develop/doc/000e44fc464560a0a6bf4188f56800
*/
const isDevTool = wx.getSystemInfoSync().platform === 'devtools';
const reqFn = isDevTool
? createWithoutChunked(request, requestOptions)
: create(request, requestOptions);
Expand Down
14 changes: 10 additions & 4 deletions src/polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Headers } from 'headers-polyfill';
import { FastTextDecoder, FastTextEncoder } from '../fast-text-encoding';

declare global {
var GameGlobal: any;
const GameGlobal: unknown;
}

function polyfill(target: any = global || window) {
Expand All @@ -15,11 +15,17 @@ function polyfill(target: any = global || window) {

try {
polyfill();
} catch (e) {}
} catch (e) {
/* empty */
}
try {
polyfill(GameGlobal);
} catch (e) {}
} catch (e) {
/* empty */
}
try {
window = window || {};
polyfill(window);
} catch (e) {}
} catch (e) {
/* empty */
}
Loading

0 comments on commit 4f1129c

Please sign in to comment.