@xdcobra/react-native-grpc 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +176 -6
- package/android/src/main/java/com/reactnativegrpc/GrpcModule.java +233 -88
- package/ios/Grpc.h +1 -1
- package/ios/Grpc.m +210 -24
- package/lib/commonjs/channel.js +84 -0
- package/lib/commonjs/channel.js.map +1 -0
- package/lib/commonjs/client.js +531 -128
- package/lib/commonjs/client.js.map +1 -1
- package/lib/commonjs/create-client.js +206 -0
- package/lib/commonjs/create-client.js.map +1 -0
- package/lib/commonjs/index.js +32 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/interceptors.js +116 -0
- package/lib/commonjs/interceptors.js.map +1 -0
- package/lib/commonjs/native.js +24 -0
- package/lib/commonjs/native.js.map +1 -0
- package/lib/commonjs/retry.js +153 -0
- package/lib/commonjs/retry.js.map +1 -0
- package/lib/commonjs/status.js +54 -0
- package/lib/commonjs/status.js.map +1 -0
- package/lib/commonjs/types.js +29 -0
- package/lib/commonjs/types.js.map +1 -1
- package/lib/module/channel.js +76 -0
- package/lib/module/channel.js.map +1 -0
- package/lib/module/client.js +518 -122
- package/lib/module/client.js.map +1 -1
- package/lib/module/create-client.js +201 -0
- package/lib/module/create-client.js.map +1 -0
- package/lib/module/index.js +3 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/interceptors.js +104 -0
- package/lib/module/interceptors.js.map +1 -0
- package/lib/module/native.js +17 -0
- package/lib/module/native.js.map +1 -0
- package/lib/module/retry.js +141 -0
- package/lib/module/retry.js.map +1 -0
- package/lib/module/status.js +46 -0
- package/lib/module/status.js.map +1 -0
- package/lib/module/types.js +17 -0
- package/lib/module/types.js.map +1 -1
- package/lib/typescript/channel.d.ts +27 -0
- package/lib/typescript/channel.d.ts.map +1 -0
- package/lib/typescript/client.d.ts +56 -49
- package/lib/typescript/client.d.ts.map +1 -1
- package/lib/typescript/create-client.d.ts +65 -0
- package/lib/typescript/create-client.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +5 -0
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/interceptors.d.ts +43 -0
- package/lib/typescript/interceptors.d.ts.map +1 -0
- package/lib/typescript/native.d.ts +53 -0
- package/lib/typescript/native.d.ts.map +1 -0
- package/lib/typescript/retry.d.ts +56 -0
- package/lib/typescript/retry.d.ts.map +1 -0
- package/lib/typescript/status.d.ts +24 -0
- package/lib/typescript/status.d.ts.map +1 -0
- package/lib/typescript/types.d.ts +60 -0
- package/lib/typescript/types.d.ts.map +1 -1
- package/package.json +48 -7
- package/src/channel.ts +110 -0
- package/src/client.ts +725 -200
- package/src/create-client.ts +391 -0
- package/src/index.ts +17 -0
- package/src/interceptors.ts +167 -0
- package/src/native.ts +99 -0
- package/src/retry.ts +208 -0
- package/src/status.ts +50 -0
- package/src/types.ts +64 -0
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { GrpcInterceptor } from './interceptors';
|
|
2
|
+
import type { GrpcHedgingPolicy, GrpcRetryPolicy } from './retry';
|
|
1
3
|
export type GrpcMetadata = Record<string, string>;
|
|
2
4
|
/** Options for unary / streaming RPCs. */
|
|
3
5
|
export type GrpcCallOptions = {
|
|
@@ -7,11 +9,27 @@ export type GrpcCallOptions = {
|
|
|
7
9
|
* `0` = no deadline (Android); iOS still applies a positive timeout when set.
|
|
8
10
|
*/
|
|
9
11
|
deadlineSeconds?: number;
|
|
12
|
+
/**
|
|
13
|
+
* Extra interceptors for this RPC only (appended after channel interceptors).
|
|
14
|
+
* Prefer channel-level `interceptors` / `setInterceptors` for shared middleware.
|
|
15
|
+
*/
|
|
16
|
+
interceptors?: GrpcInterceptor[];
|
|
17
|
+
/**
|
|
18
|
+
* Unary-only retry policy override, or `false` to disable channel retry for this call.
|
|
19
|
+
* Mutually exclusive with `hedging`.
|
|
20
|
+
*/
|
|
21
|
+
retry?: GrpcRetryPolicy | false;
|
|
22
|
+
/**
|
|
23
|
+
* Unary-only hedging policy override, or `false` to disable channel hedging for this call.
|
|
24
|
+
* Mutually exclusive with `retry`.
|
|
25
|
+
*/
|
|
26
|
+
hedging?: GrpcHedgingPolicy | false;
|
|
10
27
|
};
|
|
11
28
|
/**
|
|
12
29
|
* Channel TLS configuration. Each `setTlsOptions` call **replaces** the prior
|
|
13
30
|
* config (omitted / null / undefined fields are cleared). Call before
|
|
14
31
|
* `initGrpcChannel()`. Ignored while `setInsecure(true)`.
|
|
32
|
+
* Prefer binding TLS at `createChannel({ tls })` for multi-host apps.
|
|
15
33
|
*/
|
|
16
34
|
export type GrpcTlsOptions = {
|
|
17
35
|
/** PEM trust roots (one or more certs). Clears → platform/gRPC defaults. */
|
|
@@ -31,6 +49,48 @@ export type GrpcTlsOptions = {
|
|
|
31
49
|
*/
|
|
32
50
|
spkiSha256Pins?: string[] | null;
|
|
33
51
|
};
|
|
52
|
+
/** Immutable config for {@link createChannel}. Reconfigure by creating a new channel. */
|
|
53
|
+
export type GrpcChannelConfig = {
|
|
54
|
+
/** Target host, e.g. `api.example.com:443` or `10.0.2.2:50051`. */
|
|
55
|
+
host: string;
|
|
56
|
+
/** Plaintext (dev). Default `false`. */
|
|
57
|
+
insecure?: boolean;
|
|
58
|
+
/** TLS options (ignored when `insecure` is true). */
|
|
59
|
+
tls?: GrpcTlsOptions;
|
|
60
|
+
/** Default per-call deadline in seconds. Default 120. */
|
|
61
|
+
callDeadlineSeconds?: number;
|
|
62
|
+
/** Max inbound message size in bytes (Android / iOS). */
|
|
63
|
+
responseSizeLimit?: number;
|
|
64
|
+
/** Message compression. */
|
|
65
|
+
compression?: {
|
|
66
|
+
enable: boolean;
|
|
67
|
+
compressorName: string;
|
|
68
|
+
};
|
|
69
|
+
/** HTTP/2 keepalive (Android primarily). */
|
|
70
|
+
keepAlive?: {
|
|
71
|
+
enable: boolean;
|
|
72
|
+
keepAliveTime: number;
|
|
73
|
+
keepAliveTimeOut: number;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Request/response middleware for all RPCs on this channel (immutable).
|
|
77
|
+
* Per-RPC extras: `GrpcCallOptions.interceptors`.
|
|
78
|
+
*/
|
|
79
|
+
interceptors?: GrpcInterceptor[];
|
|
80
|
+
/**
|
|
81
|
+
* Unary-only retry policy (A6-style). Mutually exclusive with `hedging`.
|
|
82
|
+
*/
|
|
83
|
+
retry?: GrpcRetryPolicy;
|
|
84
|
+
/**
|
|
85
|
+
* Unary-only hedging policy (A6-style). Mutually exclusive with `retry`.
|
|
86
|
+
*/
|
|
87
|
+
hedging?: GrpcHedgingPolicy;
|
|
88
|
+
};
|
|
89
|
+
/** Id of the singleton / legacy channel used by `GrpcClient` setters. */
|
|
90
|
+
export declare const DEFAULT_CHANNEL_ID = "default";
|
|
91
|
+
export type { GrpcInterceptStart, GrpcInterceptor } from './interceptors';
|
|
92
|
+
export type { GrpcHedgingPolicy, GrpcRetryPolicy } from './retry';
|
|
93
|
+
export { GrpcStatusCode } from './status';
|
|
34
94
|
export type RemoveListener = () => void;
|
|
35
95
|
export interface GrpcServerInputStream {
|
|
36
96
|
send(data: Uint8Array): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAElD,0CAA0C;AAC1C,MAAM,MAAM,eAAe,GAAG;IAC5B;;;;OAIG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,KAAK,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAElE,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM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;IAC3C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,cAAc,EAAE,YAAY,CAAC;IACtC,QAAQ,CAAC,OAAO,CAAC,EAAE,YAAY,CAAC;IAChC,QAAQ,CAAC,SAAS,CAAC,EAAE,sBAAsB,CAAC;IAC5C,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,CAAC,EAAE,YAAY,CAAC;CAClC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xdcobra/react-native-grpc",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "gRPC for react-native (fork of @krishnafkh/react-native-grpc / Mitch528)",
|
|
5
5
|
"main": "lib/commonjs/index",
|
|
6
6
|
"module": "lib/module/index",
|
|
7
7
|
"types": "lib/typescript/index.d.ts",
|
|
8
8
|
"react-native": "src/index",
|
|
9
9
|
"source": "src/index",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"react-native": "./src/index.ts",
|
|
13
|
+
"types": "./lib/typescript/index.d.ts",
|
|
14
|
+
"module": "./lib/module/index.js",
|
|
15
|
+
"default": "./lib/commonjs/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./create-client": {
|
|
18
|
+
"react-native": "./src/create-client.ts",
|
|
19
|
+
"types": "./lib/typescript/create-client.d.ts",
|
|
20
|
+
"module": "./lib/module/create-client.js",
|
|
21
|
+
"default": "./lib/commonjs/create-client.js"
|
|
22
|
+
},
|
|
23
|
+
"./package.json": "./package.json"
|
|
24
|
+
},
|
|
10
25
|
"files": [
|
|
11
26
|
"src",
|
|
12
27
|
"lib",
|
|
@@ -43,7 +58,10 @@
|
|
|
43
58
|
"android",
|
|
44
59
|
"grpc"
|
|
45
60
|
],
|
|
46
|
-
"repository":
|
|
61
|
+
"repository": {
|
|
62
|
+
"type": "git",
|
|
63
|
+
"url": "git+https://github.com/XDcobra/react-native-grpc.git"
|
|
64
|
+
},
|
|
47
65
|
"author": "XDcobra (fork; original Krishna / Mitch528)",
|
|
48
66
|
"license": "MIT",
|
|
49
67
|
"bugs": {
|
|
@@ -52,13 +70,15 @@
|
|
|
52
70
|
"homepage": "https://github.com/XDcobra/react-native-grpc#readme",
|
|
53
71
|
"publishConfig": {
|
|
54
72
|
"access": "public",
|
|
55
|
-
"registry": "https://registry.npmjs.org/"
|
|
73
|
+
"registry": "https://registry.npmjs.org/",
|
|
74
|
+
"provenance": true
|
|
56
75
|
},
|
|
57
76
|
"packageManager": "yarn@1.22.22",
|
|
58
77
|
"engines": {
|
|
59
78
|
"node": ">=18"
|
|
60
79
|
},
|
|
61
80
|
"devDependencies": {
|
|
81
|
+
"@bufbuild/protobuf": "^2.13.0",
|
|
62
82
|
"@react-native-community/eslint-config": "^3.0.0",
|
|
63
83
|
"@release-it/keep-a-changelog": "^3.0.0",
|
|
64
84
|
"@types/jest": "^26.0.23",
|
|
@@ -80,9 +100,15 @@
|
|
|
80
100
|
"xcode": "^3.0.1"
|
|
81
101
|
},
|
|
82
102
|
"peerDependencies": {
|
|
103
|
+
"@bufbuild/protobuf": "^2.0.0",
|
|
83
104
|
"react": "*",
|
|
84
105
|
"react-native": "*"
|
|
85
106
|
},
|
|
107
|
+
"peerDependenciesMeta": {
|
|
108
|
+
"@bufbuild/protobuf": {
|
|
109
|
+
"optional": true
|
|
110
|
+
}
|
|
111
|
+
},
|
|
86
112
|
"jest": {
|
|
87
113
|
"preset": "react-native",
|
|
88
114
|
"testMatch": [
|
|
@@ -91,6 +117,17 @@
|
|
|
91
117
|
"modulePathIgnorePatterns": [
|
|
92
118
|
"<rootDir>/example/node_modules",
|
|
93
119
|
"<rootDir>/lib/"
|
|
120
|
+
],
|
|
121
|
+
"moduleNameMapper": {
|
|
122
|
+
"^@bufbuild/protobuf/codegenv2$": "<rootDir>/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv2/index.js",
|
|
123
|
+
"^@bufbuild/protobuf/codegenv1$": "<rootDir>/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv1/index.js",
|
|
124
|
+
"^@bufbuild/protobuf/reflect$": "<rootDir>/node_modules/@bufbuild/protobuf/dist/commonjs/reflect/index.js",
|
|
125
|
+
"^@bufbuild/protobuf/wkt$": "<rootDir>/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/index.js",
|
|
126
|
+
"^@bufbuild/protobuf/wire$": "<rootDir>/node_modules/@bufbuild/protobuf/dist/commonjs/wire/index.js",
|
|
127
|
+
"^@bufbuild/protobuf$": "<rootDir>/node_modules/@bufbuild/protobuf/dist/commonjs/index.js"
|
|
128
|
+
},
|
|
129
|
+
"transformIgnorePatterns": [
|
|
130
|
+
"node_modules/(?!((jest-)?react-native|@react-native(-community)?|@bufbuild)/)"
|
|
94
131
|
]
|
|
95
132
|
},
|
|
96
133
|
"husky": {
|
|
@@ -101,13 +138,15 @@
|
|
|
101
138
|
"release-it": {
|
|
102
139
|
"git": {
|
|
103
140
|
"commitMessage": "chore: release ${version}",
|
|
104
|
-
"tagName": "v${version}"
|
|
141
|
+
"tagName": "v${version}",
|
|
142
|
+
"allowEmptyCommit": true,
|
|
143
|
+
"push": true
|
|
105
144
|
},
|
|
106
145
|
"npm": {
|
|
107
|
-
"publish":
|
|
146
|
+
"publish": false
|
|
108
147
|
},
|
|
109
148
|
"github": {
|
|
110
|
-
"release":
|
|
149
|
+
"release": false
|
|
111
150
|
},
|
|
112
151
|
"plugins": {
|
|
113
152
|
"@release-it/keep-a-changelog": {
|
|
@@ -137,7 +176,9 @@
|
|
|
137
176
|
"eslintIgnore": [
|
|
138
177
|
"node_modules/",
|
|
139
178
|
"lib/",
|
|
140
|
-
"example/src/_proto/"
|
|
179
|
+
"example/src/_proto/",
|
|
180
|
+
"example/src/gen/",
|
|
181
|
+
"src/__fixtures__/"
|
|
141
182
|
],
|
|
142
183
|
"prettier": {
|
|
143
184
|
"quoteProps": "consistent",
|
package/src/channel.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { GrpcCallApi, normalizeTlsOptions } from './client';
|
|
2
|
+
import type { GrpcInterceptor } from './interceptors';
|
|
3
|
+
import { nativeGrpc } from './native';
|
|
4
|
+
import {
|
|
5
|
+
assertExclusivePolicies,
|
|
6
|
+
normalizeHedgingPolicy,
|
|
7
|
+
normalizeRetryPolicy,
|
|
8
|
+
} from './retry';
|
|
9
|
+
import { GrpcChannelConfig } from './types';
|
|
10
|
+
|
|
11
|
+
let channelCtr = 1;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Independent gRPC channel bound to a host (and optional TLS / deadline / keepalive).
|
|
15
|
+
* Create via {@link createChannel}; close when finished. Concurrent channels are supported.
|
|
16
|
+
*/
|
|
17
|
+
export class GrpcChannel extends GrpcCallApi {
|
|
18
|
+
private closed = false;
|
|
19
|
+
|
|
20
|
+
constructor(
|
|
21
|
+
channelId: string,
|
|
22
|
+
private readonly host: string,
|
|
23
|
+
interceptors: readonly GrpcInterceptor[] = [],
|
|
24
|
+
channelDeadlineSeconds = 120,
|
|
25
|
+
channelRetry: import('./retry').GrpcRetryPolicy | null = null,
|
|
26
|
+
channelHedging: import('./retry').GrpcHedgingPolicy | null = null
|
|
27
|
+
) {
|
|
28
|
+
super(
|
|
29
|
+
channelId,
|
|
30
|
+
interceptors,
|
|
31
|
+
channelDeadlineSeconds,
|
|
32
|
+
channelRetry,
|
|
33
|
+
channelHedging
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Host this channel was created with. */
|
|
38
|
+
getHost(): Promise<string> {
|
|
39
|
+
return Promise.resolve(this.host);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Native channel id (for diagnostics). */
|
|
43
|
+
getChannelId(): string {
|
|
44
|
+
return this.channelId;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Tear down this channel. Further RPCs will fail.
|
|
49
|
+
* Does not affect the singleton `GrpcClient` default channel.
|
|
50
|
+
*/
|
|
51
|
+
close(): void {
|
|
52
|
+
if (this.closed) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
this.closed = true;
|
|
56
|
+
nativeGrpc().closeChannel(this.channelId);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Create a concurrent channel with immutable config.
|
|
62
|
+
* Prefer this over mutating `GrpcClient.setHost` when talking to multiple hosts.
|
|
63
|
+
*/
|
|
64
|
+
export function createChannel(config: GrpcChannelConfig): GrpcChannel {
|
|
65
|
+
if (!config?.host || typeof config.host !== 'string' || !config.host.trim()) {
|
|
66
|
+
throw new Error('createChannel requires a non-empty host');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
assertExclusivePolicies(config.retry, config.hedging);
|
|
70
|
+
if (config.retry) {
|
|
71
|
+
normalizeRetryPolicy(config.retry);
|
|
72
|
+
}
|
|
73
|
+
if (config.hedging) {
|
|
74
|
+
normalizeHedgingPolicy(config.hedging);
|
|
75
|
+
}
|
|
76
|
+
const retry = config.retry ?? null;
|
|
77
|
+
const hedging = config.hedging ?? null;
|
|
78
|
+
|
|
79
|
+
const tls = config.tls ? normalizeTlsOptions(config.tls) : null;
|
|
80
|
+
const channelId = `ch_${channelCtr++}`;
|
|
81
|
+
const compression = config.compression;
|
|
82
|
+
const keepAlive = config.keepAlive;
|
|
83
|
+
const deadline =
|
|
84
|
+
config.callDeadlineSeconds !== undefined
|
|
85
|
+
? Math.max(0, config.callDeadlineSeconds)
|
|
86
|
+
: 120;
|
|
87
|
+
|
|
88
|
+
nativeGrpc().createChannel(channelId, {
|
|
89
|
+
host: config.host.trim(),
|
|
90
|
+
insecure: !!config.insecure,
|
|
91
|
+
tls,
|
|
92
|
+
callDeadlineSeconds: deadline,
|
|
93
|
+
responseSizeLimit:
|
|
94
|
+
config.responseSizeLimit !== undefined ? config.responseSizeLimit : null,
|
|
95
|
+
compressionEnable: !!compression?.enable,
|
|
96
|
+
compressorName: compression?.compressorName ?? '',
|
|
97
|
+
keepAliveEnable: !!keepAlive?.enable,
|
|
98
|
+
keepAliveTime: keepAlive?.keepAliveTime ?? 0,
|
|
99
|
+
keepAliveTimeOut: keepAlive?.keepAliveTimeOut ?? 0,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
return new GrpcChannel(
|
|
103
|
+
channelId,
|
|
104
|
+
config.host.trim(),
|
|
105
|
+
config.interceptors ?? [],
|
|
106
|
+
deadline,
|
|
107
|
+
retry,
|
|
108
|
+
hedging
|
|
109
|
+
);
|
|
110
|
+
}
|