@lingxia/bridge 0.5.1 → 0.6.3
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/dist/bridge-runtime.es2020.js +1 -1
- package/dist/bridge-runtime.es5.js +1 -1
- package/dist/bridge.d.ts +5 -2
- package/dist/es2020/bridge.js +107 -84
- package/dist/es2020/index.js +2 -3
- package/dist/es2020/invocation.js +20 -0
- package/dist/es5/bridge.js +174 -96
- package/dist/es5/index.js +2 -3
- package/dist/es5/invocation.js +27 -0
- package/dist/index.d.ts +3 -2
- package/dist/invocation.d.ts +2 -0
- package/dist/types.d.ts +41 -15
- package/package.json +1 -1
- package/dist/es2020/image-proxy.js +0 -82
- package/dist/es5/image-proxy.js +0 -83
- package/dist/image-proxy.d.ts +0 -1
package/dist/es5/invocation.js
CHANGED
|
@@ -13,6 +13,17 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
13
13
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
14
14
|
};
|
|
15
15
|
})();
|
|
16
|
+
var __assign = (this && this.__assign) || function () {
|
|
17
|
+
__assign = Object.assign || function(t) {
|
|
18
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
19
|
+
s = arguments[i];
|
|
20
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
21
|
+
t[p] = s[p];
|
|
22
|
+
}
|
|
23
|
+
return t;
|
|
24
|
+
};
|
|
25
|
+
return __assign.apply(this, arguments);
|
|
26
|
+
};
|
|
16
27
|
import { BRIDGE_ERROR } from "./types";
|
|
17
28
|
var BridgeInvocationError = /** @class */ (function (_super) {
|
|
18
29
|
__extends(BridgeInvocationError, _super);
|
|
@@ -45,6 +56,22 @@ export function toBridgeError(err) {
|
|
|
45
56
|
var message = err instanceof Error ? err.message : typeof err === "string" ? err : "Unknown error";
|
|
46
57
|
return new BridgeInvocationError(BRIDGE_ERROR.INTERNAL_ERROR, message);
|
|
47
58
|
}
|
|
59
|
+
export function toNativeError(err) {
|
|
60
|
+
var bridgeError = toBridgeError(err);
|
|
61
|
+
return __assign({ code: String(bridgeError.code || BRIDGE_ERROR.INTERNAL_ERROR), message: typeof bridgeError.message === "string" && bridgeError.message.trim() !== ""
|
|
62
|
+
? bridgeError.message
|
|
63
|
+
: "Unknown error" }, (bridgeError.data === undefined ? {} : { data: bridgeError.data }));
|
|
64
|
+
}
|
|
65
|
+
export function isNativeError(error, code) {
|
|
66
|
+
if (!error || typeof error !== "object")
|
|
67
|
+
return false;
|
|
68
|
+
var candidate = error;
|
|
69
|
+
if (typeof candidate.code !== "string" || candidate.code.trim() === "")
|
|
70
|
+
return false;
|
|
71
|
+
if (typeof candidate.message !== "string")
|
|
72
|
+
return false;
|
|
73
|
+
return code === undefined || candidate.code === code;
|
|
74
|
+
}
|
|
48
75
|
export function resolveParams(source) {
|
|
49
76
|
if (typeof source === "function") {
|
|
50
77
|
return source();
|
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
* - Bridge initialization for native communication
|
|
6
6
|
* - Error UI rendering when page cannot be loaded
|
|
7
7
|
*/
|
|
8
|
-
export type { BridgeConfig, CallOptions, ChannelOpenOptions, BridgeErrorCode, ChannelEventName, DataSubscriber, ErrorInfo,
|
|
9
|
-
export { LingXiaBridge,
|
|
8
|
+
export type { BridgeConfig, CallOptions, ChannelCloseEvent, ChannelOptions, ChannelOpenOptions, BridgeErrorCode, ChannelEventName, DataSubscriber, ErrorInfo, LingXiaBridgeInterface, LxChannel, LxBridgeError, LxMethod, LxMethodParams, LxMethodResult, LxMethodStreamData, LxStream, NativeComponentMessage, NativeChannel, NativeError, NativeStream, NotifyOptions, InvokeOptions, StateInfo, StreamCallOptions, StreamOptions, StreamEventName, } from './types';
|
|
9
|
+
export { LingXiaBridge, channel, initBridge, invoke, notify, stream, } from './bridge';
|
|
10
|
+
export { isNativeError } from './invocation';
|
|
10
11
|
export { renderErrorUI, hasError, getErrorInfo } from './error';
|
|
11
12
|
export { boot, bootWhenReady } from './boot';
|
package/dist/invocation.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ export type StreamResult<TMethod> = TMethod extends (...args: any[]) => LxStream
|
|
|
6
6
|
export type ChannelIn<TMethod> = TMethod extends (...args: any[]) => Promise<LxChannel<infer TIn, any>> ? TIn : never;
|
|
7
7
|
export type ChannelOut<TMethod> = TMethod extends (...args: any[]) => Promise<LxChannel<any, infer TOut>> ? TOut : never;
|
|
8
8
|
export declare function toBridgeError(err: unknown): LxBridgeError;
|
|
9
|
+
export declare function toNativeError(err: unknown): import("./types").NativeError;
|
|
10
|
+
export declare function isNativeError(error: unknown, code?: string): error is import("./types").NativeError;
|
|
9
11
|
export declare function resolveParams<T>(source?: ParamsSource<T>): T | undefined;
|
|
10
12
|
export declare function stableParamKey(value: unknown): string;
|
|
11
13
|
export declare function invokeMethod<TMethod extends (...args: any[]) => any>(method: TMethod, params: unknown): ReturnType<TMethod>;
|
package/dist/types.d.ts
CHANGED
|
@@ -31,6 +31,11 @@ export interface LxBridgeError {
|
|
|
31
31
|
message?: string;
|
|
32
32
|
data?: unknown;
|
|
33
33
|
}
|
|
34
|
+
export interface NativeError {
|
|
35
|
+
code: string;
|
|
36
|
+
message: string;
|
|
37
|
+
data?: unknown;
|
|
38
|
+
}
|
|
34
39
|
export interface StateInfo {
|
|
35
40
|
rev: number;
|
|
36
41
|
initial: boolean;
|
|
@@ -79,12 +84,11 @@ export interface LxStream<TData = unknown, TResult = unknown> extends AsyncItera
|
|
|
79
84
|
readonly id: string;
|
|
80
85
|
readonly result: Promise<TResult>;
|
|
81
86
|
}
|
|
82
|
-
export interface
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
[namespace: string]: unknown;
|
|
87
|
+
export interface NativeStream<TEvent = unknown, TResult = void> {
|
|
88
|
+
onEvent(listener: (event: TEvent) => void): () => void;
|
|
89
|
+
onError(listener: (error: NativeError) => void): () => void;
|
|
90
|
+
result: Promise<TResult>;
|
|
91
|
+
cancel(): void;
|
|
88
92
|
}
|
|
89
93
|
export interface LxChannel<TIn = unknown, TOut = TIn> extends AsyncIterable<TIn> {
|
|
90
94
|
send(payload: TOut): void;
|
|
@@ -95,6 +99,16 @@ export interface LxChannel<TIn = unknown, TOut = TIn> extends AsyncIterable<TIn>
|
|
|
95
99
|
[Symbol.asyncIterator](): AsyncIterator<TIn, void, void>;
|
|
96
100
|
readonly id: string;
|
|
97
101
|
}
|
|
102
|
+
export interface ChannelCloseEvent {
|
|
103
|
+
code?: string;
|
|
104
|
+
reason?: string;
|
|
105
|
+
}
|
|
106
|
+
export interface NativeChannel<TIn = unknown, TOut = unknown> {
|
|
107
|
+
send(message: TIn): void;
|
|
108
|
+
onMessage(listener: (message: TOut) => void): () => void;
|
|
109
|
+
onClose(listener: (event: ChannelCloseEvent) => void): () => void;
|
|
110
|
+
close(code?: string, reason?: string): void;
|
|
111
|
+
}
|
|
98
112
|
declare global {
|
|
99
113
|
interface Window {
|
|
100
114
|
__LX_BRIDGE_CFG?: BridgeConfig;
|
|
@@ -114,7 +128,6 @@ declare global {
|
|
|
114
128
|
NativeComponentBridge?: {
|
|
115
129
|
postMessage: (message: string) => void;
|
|
116
130
|
};
|
|
117
|
-
host?: HostApi;
|
|
118
131
|
webkit?: {
|
|
119
132
|
messageHandlers: {
|
|
120
133
|
[key: string]: {
|
|
@@ -125,17 +138,14 @@ declare global {
|
|
|
125
138
|
}
|
|
126
139
|
}
|
|
127
140
|
export interface LingXiaBridgeInterface {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
141
|
+
invoke<TResult = unknown, TInput = void>(route: string, input?: TInput, options?: InvokeOptions): Promise<TResult>;
|
|
142
|
+
stream<TEvent = unknown, TResult = void, TInput = void>(route: string, input?: TInput, options?: StreamOptions): NativeStream<TEvent, TResult>;
|
|
143
|
+
notify<TInput = void>(route: string, input?: TInput, options?: NotifyOptions): void;
|
|
144
|
+
channel<TIn = unknown, TOut = unknown>(route: string, input?: unknown, options?: ChannelOptions): Promise<NativeChannel<TIn, TOut>>;
|
|
145
|
+
raw: LingXiaBridgeRawInterface;
|
|
133
146
|
state: {
|
|
134
147
|
subscribe(callback: DataSubscriber): () => void;
|
|
135
148
|
};
|
|
136
|
-
channel: {
|
|
137
|
-
open<TIn = unknown, TOut = TIn>(topic: string, params?: unknown, options?: ChannelOpenOptions): Promise<LxChannel<TIn, TOut>>;
|
|
138
|
-
};
|
|
139
149
|
_connectWebMessagePort(port: MessagePort): void;
|
|
140
150
|
_receiveEvaluateMessage(messageString: string): void;
|
|
141
151
|
debug: {
|
|
@@ -163,3 +173,19 @@ export interface LingXiaBridgeInterface {
|
|
|
163
173
|
};
|
|
164
174
|
isReady(): boolean;
|
|
165
175
|
}
|
|
176
|
+
export interface LingXiaBridgeRawInterface {
|
|
177
|
+
call(method: string, params?: unknown, options?: CallOptions): Promise<unknown>;
|
|
178
|
+
call<M extends LxMethod>(method: M, params?: LxMethodParams<M>, options?: CallOptions): Promise<LxMethodResult<M>>;
|
|
179
|
+
stream(method: string, params?: unknown, options?: StreamCallOptions): LxStream<unknown, unknown>;
|
|
180
|
+
stream<M extends LxMethod>(method: M, params?: LxMethodParams<M>, options?: StreamCallOptions): LxStream<LxMethodStreamData<M>, LxMethodResult<M>>;
|
|
181
|
+
notify(method: string, params?: unknown, options?: NotifyOptions): void;
|
|
182
|
+
channel: {
|
|
183
|
+
open<TIn = unknown, TOut = TIn>(topic: string, params?: unknown, options?: ChannelOpenOptions): Promise<LxChannel<TIn, TOut>>;
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
export interface InvokeOptions extends CallOptions {
|
|
187
|
+
}
|
|
188
|
+
export interface StreamOptions extends StreamCallOptions {
|
|
189
|
+
}
|
|
190
|
+
export interface ChannelOptions extends ChannelOpenOptions {
|
|
191
|
+
}
|
package/package.json
CHANGED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { getPlatformOS } from './runtime-env';
|
|
2
|
-
const PROXY_PREFIX = 'lx://proxy/';
|
|
3
|
-
function shouldProxyImageUrl(trimmed) {
|
|
4
|
-
if (trimmed.length === 0) {
|
|
5
|
-
return false;
|
|
6
|
-
}
|
|
7
|
-
if (trimmed.startsWith('lx://') ||
|
|
8
|
-
trimmed.startsWith('data:') ||
|
|
9
|
-
trimmed.startsWith('blob:') ||
|
|
10
|
-
trimmed.startsWith('file:')) {
|
|
11
|
-
return false;
|
|
12
|
-
}
|
|
13
|
-
return trimmed.startsWith('http://') || trimmed.startsWith('https://');
|
|
14
|
-
}
|
|
15
|
-
function toProxyUrl(url) {
|
|
16
|
-
const b64 = btoa(url);
|
|
17
|
-
const urlSafe = b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '');
|
|
18
|
-
return `${PROXY_PREFIX}${urlSafe}`;
|
|
19
|
-
}
|
|
20
|
-
function proxyImageUrl(url) {
|
|
21
|
-
const trimmed = url.trim();
|
|
22
|
-
if (!shouldProxyImageUrl(trimmed))
|
|
23
|
-
return url;
|
|
24
|
-
return toProxyUrl(trimmed);
|
|
25
|
-
}
|
|
26
|
-
export function setupImageProxy() {
|
|
27
|
-
const os = getPlatformOS();
|
|
28
|
-
if (os !== 'iOS' && os !== 'macOS')
|
|
29
|
-
return;
|
|
30
|
-
if (typeof window === 'undefined' || typeof document === 'undefined')
|
|
31
|
-
return;
|
|
32
|
-
if (typeof HTMLImageElement === 'undefined')
|
|
33
|
-
return;
|
|
34
|
-
const proto = HTMLImageElement.prototype;
|
|
35
|
-
if (proto.__lxProxyPatched)
|
|
36
|
-
return;
|
|
37
|
-
proto.__lxProxyPatched = true;
|
|
38
|
-
const originalSetAttribute = proto.setAttribute;
|
|
39
|
-
proto.setAttribute = function (name, value) {
|
|
40
|
-
if (typeof name === 'string' && name.toLowerCase() === 'src' && typeof value === 'string') {
|
|
41
|
-
return originalSetAttribute.call(this, name, proxyImageUrl(value));
|
|
42
|
-
}
|
|
43
|
-
return originalSetAttribute.call(this, name, value);
|
|
44
|
-
};
|
|
45
|
-
const srcDescriptor = Object.getOwnPropertyDescriptor(proto, 'src');
|
|
46
|
-
if (srcDescriptor?.set) {
|
|
47
|
-
Object.defineProperty(proto, 'src', {
|
|
48
|
-
configurable: srcDescriptor.configurable,
|
|
49
|
-
enumerable: srcDescriptor.enumerable,
|
|
50
|
-
get: srcDescriptor.get,
|
|
51
|
-
set(value) {
|
|
52
|
-
if (typeof value === 'string') {
|
|
53
|
-
srcDescriptor.set?.call(this, proxyImageUrl(value));
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
srcDescriptor.set?.call(this, value);
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
const rewriteExisting = () => {
|
|
62
|
-
const imgs = document.querySelectorAll('img[src]');
|
|
63
|
-
imgs.forEach((img) => {
|
|
64
|
-
const src = img.getAttribute('src');
|
|
65
|
-
if (!src)
|
|
66
|
-
return;
|
|
67
|
-
const proxied = proxyImageUrl(src);
|
|
68
|
-
if (proxied !== src) {
|
|
69
|
-
originalSetAttribute.call(img, 'src', proxied);
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
};
|
|
73
|
-
const domReadyListener = {
|
|
74
|
-
handleEvent: () => rewriteExisting(),
|
|
75
|
-
};
|
|
76
|
-
if (document.readyState === 'loading') {
|
|
77
|
-
document.addEventListener('DOMContentLoaded', domReadyListener);
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
rewriteExisting();
|
|
81
|
-
}
|
|
82
|
-
}
|
package/dist/es5/image-proxy.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { getPlatformOS } from './runtime-env';
|
|
2
|
-
var PROXY_PREFIX = 'lx://proxy/';
|
|
3
|
-
function shouldProxyImageUrl(trimmed) {
|
|
4
|
-
if (trimmed.length === 0) {
|
|
5
|
-
return false;
|
|
6
|
-
}
|
|
7
|
-
if (trimmed.startsWith('lx://') ||
|
|
8
|
-
trimmed.startsWith('data:') ||
|
|
9
|
-
trimmed.startsWith('blob:') ||
|
|
10
|
-
trimmed.startsWith('file:')) {
|
|
11
|
-
return false;
|
|
12
|
-
}
|
|
13
|
-
return trimmed.startsWith('http://') || trimmed.startsWith('https://');
|
|
14
|
-
}
|
|
15
|
-
function toProxyUrl(url) {
|
|
16
|
-
var b64 = btoa(url);
|
|
17
|
-
var urlSafe = b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '');
|
|
18
|
-
return "".concat(PROXY_PREFIX).concat(urlSafe);
|
|
19
|
-
}
|
|
20
|
-
function proxyImageUrl(url) {
|
|
21
|
-
var trimmed = url.trim();
|
|
22
|
-
if (!shouldProxyImageUrl(trimmed))
|
|
23
|
-
return url;
|
|
24
|
-
return toProxyUrl(trimmed);
|
|
25
|
-
}
|
|
26
|
-
export function setupImageProxy() {
|
|
27
|
-
var os = getPlatformOS();
|
|
28
|
-
if (os !== 'iOS' && os !== 'macOS')
|
|
29
|
-
return;
|
|
30
|
-
if (typeof window === 'undefined' || typeof document === 'undefined')
|
|
31
|
-
return;
|
|
32
|
-
if (typeof HTMLImageElement === 'undefined')
|
|
33
|
-
return;
|
|
34
|
-
var proto = HTMLImageElement.prototype;
|
|
35
|
-
if (proto.__lxProxyPatched)
|
|
36
|
-
return;
|
|
37
|
-
proto.__lxProxyPatched = true;
|
|
38
|
-
var originalSetAttribute = proto.setAttribute;
|
|
39
|
-
proto.setAttribute = function (name, value) {
|
|
40
|
-
if (typeof name === 'string' && name.toLowerCase() === 'src' && typeof value === 'string') {
|
|
41
|
-
return originalSetAttribute.call(this, name, proxyImageUrl(value));
|
|
42
|
-
}
|
|
43
|
-
return originalSetAttribute.call(this, name, value);
|
|
44
|
-
};
|
|
45
|
-
var srcDescriptor = Object.getOwnPropertyDescriptor(proto, 'src');
|
|
46
|
-
if (srcDescriptor === null || srcDescriptor === void 0 ? void 0 : srcDescriptor.set) {
|
|
47
|
-
Object.defineProperty(proto, 'src', {
|
|
48
|
-
configurable: srcDescriptor.configurable,
|
|
49
|
-
enumerable: srcDescriptor.enumerable,
|
|
50
|
-
get: srcDescriptor.get,
|
|
51
|
-
set: function (value) {
|
|
52
|
-
var _a, _b;
|
|
53
|
-
if (typeof value === 'string') {
|
|
54
|
-
(_a = srcDescriptor.set) === null || _a === void 0 ? void 0 : _a.call(this, proxyImageUrl(value));
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
(_b = srcDescriptor.set) === null || _b === void 0 ? void 0 : _b.call(this, value);
|
|
58
|
-
}
|
|
59
|
-
},
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
var rewriteExisting = function () {
|
|
63
|
-
var imgs = document.querySelectorAll('img[src]');
|
|
64
|
-
imgs.forEach(function (img) {
|
|
65
|
-
var src = img.getAttribute('src');
|
|
66
|
-
if (!src)
|
|
67
|
-
return;
|
|
68
|
-
var proxied = proxyImageUrl(src);
|
|
69
|
-
if (proxied !== src) {
|
|
70
|
-
originalSetAttribute.call(img, 'src', proxied);
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
};
|
|
74
|
-
var domReadyListener = {
|
|
75
|
-
handleEvent: function () { return rewriteExisting(); },
|
|
76
|
-
};
|
|
77
|
-
if (document.readyState === 'loading') {
|
|
78
|
-
document.addEventListener('DOMContentLoaded', domReadyListener);
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
rewriteExisting();
|
|
82
|
-
}
|
|
83
|
-
}
|
package/dist/image-proxy.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function setupImageProxy(): void;
|