@heybox/hb-sdk-protocol 0.8.1-alpha.11 → 0.8.1-alpha.12

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.
@@ -0,0 +1,213 @@
1
+ export {
2
+ MINI_PROGRAM_NATIVE_CHUNK_MAX_BYTES,
3
+ MINI_PROGRAM_NATIVE_HOST_CONTRACT_REVISION,
4
+ MINI_PROGRAM_NATIVE_HOST_DESCRIPTION,
5
+ MINI_PROGRAM_NATIVE_HOST_PROTOCOL_VERSION,
6
+ MINI_PROGRAM_NATIVE_OPERATIONS,
7
+ } from './native-host-contract.generated';
8
+ import { MINI_PROGRAM_NATIVE_CHUNK_MAX_BYTES } from './native-host-contract.generated';
9
+ /** Android WebMessageListener object 与 iOS WKScriptMessageHandler 共用名称。 */
10
+ export const MINI_PROGRAM_NATIVE_HOST_OBJECT = 'hbMiniProgramHost' as const;
11
+ /** Native 通过主文档 CustomEvent 回送 progress/result/error。 */
12
+ export const MINI_PROGRAM_NATIVE_HOST_MESSAGE_EVENT = 'heybox:miniprogram-native-host-message' as const;
13
+
14
+ export type MiniProgramNativeHostDomain = 'system' | 'network' | 'files';
15
+
16
+ /** H5 主文档发给 Native Host 的 operation 请求。 */
17
+ export interface MiniProgramNativeHostRequest {
18
+ requestId: string;
19
+ domain: MiniProgramNativeHostDomain;
20
+ operation: string;
21
+ payload?: unknown;
22
+ }
23
+
24
+ /** 取消独立引用原请求,重复取消必须是幂等操作。 */
25
+ export interface MiniProgramNativeHostCancel {
26
+ type: 'cancel';
27
+ requestId: string;
28
+ }
29
+
30
+ export interface MiniProgramNativeHostError {
31
+ code: string;
32
+ message: string;
33
+ }
34
+
35
+ export interface MiniProgramNativeHostDescription {
36
+ protocolVersion: number;
37
+ contractRevision: number;
38
+ operations: Readonly<Record<MiniProgramNativeHostDomain, readonly string[]>>;
39
+ }
40
+
41
+ export type MiniProgramNativeHostMessage =
42
+ | { requestId: string; type: 'progress'; payload: unknown }
43
+ | { requestId: string; type: 'result'; payload?: unknown }
44
+ | { requestId: string; type: 'error'; error: MiniProgramNativeHostError };
45
+
46
+ export interface MiniProgramNativeBinaryChunk {
47
+ resourceId: string;
48
+ data: string;
49
+ byteLength: number;
50
+ eof: boolean;
51
+ }
52
+
53
+ export type MiniProgramNativeFileRef =
54
+ | { scope: 'sandbox'; kind: 'file' | 'directory'; relativePath: string }
55
+ | { scope: 'grant'; id: string; kind: 'file' | 'directory'; relativePath: string };
56
+
57
+ export interface MiniProgramNativeFileDescriptor {
58
+ kind: 'file' | 'directory';
59
+ mode: 'read' | 'readwrite';
60
+ name: string;
61
+ ref: MiniProgramNativeFileRef;
62
+ }
63
+
64
+ export interface MiniProgramNativeNetworkOpenPayload {
65
+ purpose: 'request' | 'download';
66
+ url: string;
67
+ method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
68
+ params?: Record<string, unknown>;
69
+ data?: unknown;
70
+ headers?: Record<string, string>;
71
+ bodyEncoding?: 'json' | 'form';
72
+ timeoutMs: number;
73
+ }
74
+
75
+ export interface MiniProgramNativeNetworkOpenResult {
76
+ status: number;
77
+ statusText?: string;
78
+ headers: Record<string, string[]>;
79
+ resourceId: string;
80
+ }
81
+
82
+ export interface MiniProgramNativeNetworkReadPayload {
83
+ resourceId: string;
84
+ maxBytes: number;
85
+ }
86
+
87
+ export interface MiniProgramNativeNetworkReleasePayload {
88
+ resourceId: string;
89
+ }
90
+
91
+ export interface MiniProgramNativeNetworkTransferPayload {
92
+ resourceId: string;
93
+ target: MiniProgramNativeFileRef;
94
+ mode: 'replace-existing' | 'create-exclusive' | 'upsert';
95
+ maxBytes?: number;
96
+ expectedBytes?: number;
97
+ }
98
+
99
+ export interface MiniProgramNativeNetworkTransferResult {
100
+ committedBytes: number;
101
+ }
102
+
103
+ export interface MiniProgramNativeFilesCreatePayload {
104
+ ref: MiniProgramNativeFileRef;
105
+ recursive: boolean;
106
+ exclusive: boolean;
107
+ }
108
+
109
+ export interface MiniProgramNativeFilesRemovePayload {
110
+ ref: MiniProgramNativeFileRef;
111
+ recursive?: boolean;
112
+ }
113
+
114
+ export interface MiniProgramNativeFilesRefPayload {
115
+ ref: MiniProgramNativeFileRef;
116
+ }
117
+
118
+ export interface MiniProgramNativeFilesOpenPayload extends MiniProgramNativeFilesRefPayload {
119
+ maxBytes: number;
120
+ }
121
+
122
+ export interface MiniProgramNativeFilesOpenResult {
123
+ resourceId: string;
124
+ }
125
+
126
+ /** openWrite 后按序发送;eof=true 的最后一块触发 atomic commit。 */
127
+ export interface MiniProgramNativeFilesWritePayload extends MiniProgramNativeBinaryChunk {}
128
+
129
+ export type MiniProgramNativeFilesStatResult =
130
+ | { kind: 'file'; size: number; modifiedAt?: number; createdAt?: number }
131
+ | { kind: 'directory'; modifiedAt?: number; createdAt?: number };
132
+
133
+ export interface MiniProgramNativeFilesListPayload extends MiniProgramNativeFilesRefPayload {
134
+ limit: number;
135
+ }
136
+
137
+ export interface MiniProgramNativeFilesPickFilesPayload {
138
+ mode: 'read' | 'readwrite';
139
+ accept?: string[];
140
+ multiple: boolean;
141
+ }
142
+
143
+ export interface MiniProgramNativeFilesPickDirectoryPayload {
144
+ mode: 'read' | 'readwrite';
145
+ }
146
+
147
+ export interface MiniProgramNativeFilesSaveFilePayload {
148
+ suggestedName: string;
149
+ }
150
+
151
+ export interface MiniProgramNativeTransferProgress {
152
+ resourceId: string;
153
+ loaded: number;
154
+ }
155
+
156
+ export function isMiniProgramNativeHostMessage(value: unknown): value is MiniProgramNativeHostMessage {
157
+ if (!isRecord(value) || !isNonEmptyString(value.requestId)) return false;
158
+ if (value.type === 'progress') return Object.prototype.hasOwnProperty.call(value, 'payload');
159
+ if (value.type === 'result') return !Object.prototype.hasOwnProperty.call(value, 'error');
160
+ if (value.type !== 'error' || !isRecord(value.error)) return false;
161
+ return isNonEmptyString(value.error.code) && isNonEmptyString(value.error.message);
162
+ }
163
+
164
+ export function isMiniProgramNativeHostDescription(value: unknown): value is MiniProgramNativeHostDescription {
165
+ if (
166
+ !isRecord(value) ||
167
+ !isPositiveSafeInteger(value.protocolVersion) ||
168
+ !isPositiveSafeInteger(value.contractRevision) ||
169
+ !isRecord(value.operations)
170
+ )
171
+ return false;
172
+ const operations = value.operations;
173
+ if (
174
+ !isOperationList(operations.system) ||
175
+ !isOperationList(operations.network) ||
176
+ !isOperationList(operations.files)
177
+ )
178
+ return false;
179
+ return operations.system.includes('describe');
180
+ }
181
+
182
+ export function isMiniProgramNativeBinaryChunk(value: unknown): value is MiniProgramNativeBinaryChunk {
183
+ return (
184
+ isRecord(value) &&
185
+ isNonEmptyString(value.resourceId) &&
186
+ typeof value.data === 'string' &&
187
+ Number.isSafeInteger(value.byteLength) &&
188
+ Number(value.byteLength) >= 0 &&
189
+ Number(value.byteLength) <= MINI_PROGRAM_NATIVE_CHUNK_MAX_BYTES &&
190
+ typeof value.eof === 'boolean' &&
191
+ (Number(value.byteLength) > 0 || value.eof)
192
+ );
193
+ }
194
+
195
+ function isRecord(value: unknown): value is Record<string, unknown> {
196
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
197
+ }
198
+
199
+ function isNonEmptyString(value: unknown): value is string {
200
+ return typeof value === 'string' && value.length > 0;
201
+ }
202
+
203
+ function isPositiveSafeInteger(value: unknown): value is number {
204
+ return Number.isSafeInteger(value) && Number(value) > 0;
205
+ }
206
+
207
+ function isOperationList(value: unknown): value is string[] {
208
+ return (
209
+ Array.isArray(value) &&
210
+ value.every(operation => isNonEmptyString(operation) && operation === operation.trim()) &&
211
+ new Set(value).size === value.length
212
+ );
213
+ }
package/types/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export * from './bridge';
2
2
  export * from './capabilities';
3
3
  export * from './constants';
4
4
  export * from './guards';
5
+ export * from './native-host';
5
6
  export * from './payloads';
6
7
  export * from './permission-catalog';
7
8
  export * from './permissions';
@@ -0,0 +1,18 @@
1
+ /** 由 scripts/generate-native-host-contract.mjs 生成,请勿手改。 */
2
+ export declare const MINI_PROGRAM_NATIVE_HOST_PROTOCOL_VERSION: 1;
3
+ export declare const MINI_PROGRAM_NATIVE_HOST_CONTRACT_REVISION: 1;
4
+ export declare const MINI_PROGRAM_NATIVE_CHUNK_MAX_BYTES: 65536;
5
+ export declare const MINI_PROGRAM_NATIVE_OPERATIONS: {
6
+ readonly system: readonly ["describe"];
7
+ readonly network: readonly ["open", "read", "release", "transfer"];
8
+ readonly files: readonly ["create", "remove", "exists", "openRead", "openWrite", "read", "release", "write", "stat", "list", "pickFiles", "pickDirectory", "saveFile"];
9
+ };
10
+ export declare const MINI_PROGRAM_NATIVE_HOST_DESCRIPTION: {
11
+ readonly protocolVersion: 1;
12
+ readonly contractRevision: 1;
13
+ readonly operations: {
14
+ readonly system: readonly ["describe"];
15
+ readonly network: readonly ["open", "read", "release", "transfer"];
16
+ readonly files: readonly ["create", "remove", "exists", "openRead", "openWrite", "read", "release", "write", "stat", "list", "pickFiles", "pickDirectory", "saveFile"];
17
+ };
18
+ };
@@ -0,0 +1,147 @@
1
+ export { MINI_PROGRAM_NATIVE_CHUNK_MAX_BYTES, MINI_PROGRAM_NATIVE_HOST_CONTRACT_REVISION, MINI_PROGRAM_NATIVE_HOST_DESCRIPTION, MINI_PROGRAM_NATIVE_HOST_PROTOCOL_VERSION, MINI_PROGRAM_NATIVE_OPERATIONS, } from './native-host-contract.generated';
2
+ /** Android WebMessageListener object 与 iOS WKScriptMessageHandler 共用名称。 */
3
+ export declare const MINI_PROGRAM_NATIVE_HOST_OBJECT: "hbMiniProgramHost";
4
+ /** Native 通过主文档 CustomEvent 回送 progress/result/error。 */
5
+ export declare const MINI_PROGRAM_NATIVE_HOST_MESSAGE_EVENT: "heybox:miniprogram-native-host-message";
6
+ export type MiniProgramNativeHostDomain = 'system' | 'network' | 'files';
7
+ /** H5 主文档发给 Native Host 的 operation 请求。 */
8
+ export interface MiniProgramNativeHostRequest {
9
+ requestId: string;
10
+ domain: MiniProgramNativeHostDomain;
11
+ operation: string;
12
+ payload?: unknown;
13
+ }
14
+ /** 取消独立引用原请求,重复取消必须是幂等操作。 */
15
+ export interface MiniProgramNativeHostCancel {
16
+ type: 'cancel';
17
+ requestId: string;
18
+ }
19
+ export interface MiniProgramNativeHostError {
20
+ code: string;
21
+ message: string;
22
+ }
23
+ export interface MiniProgramNativeHostDescription {
24
+ protocolVersion: number;
25
+ contractRevision: number;
26
+ operations: Readonly<Record<MiniProgramNativeHostDomain, readonly string[]>>;
27
+ }
28
+ export type MiniProgramNativeHostMessage = {
29
+ requestId: string;
30
+ type: 'progress';
31
+ payload: unknown;
32
+ } | {
33
+ requestId: string;
34
+ type: 'result';
35
+ payload?: unknown;
36
+ } | {
37
+ requestId: string;
38
+ type: 'error';
39
+ error: MiniProgramNativeHostError;
40
+ };
41
+ export interface MiniProgramNativeBinaryChunk {
42
+ resourceId: string;
43
+ data: string;
44
+ byteLength: number;
45
+ eof: boolean;
46
+ }
47
+ export type MiniProgramNativeFileRef = {
48
+ scope: 'sandbox';
49
+ kind: 'file' | 'directory';
50
+ relativePath: string;
51
+ } | {
52
+ scope: 'grant';
53
+ id: string;
54
+ kind: 'file' | 'directory';
55
+ relativePath: string;
56
+ };
57
+ export interface MiniProgramNativeFileDescriptor {
58
+ kind: 'file' | 'directory';
59
+ mode: 'read' | 'readwrite';
60
+ name: string;
61
+ ref: MiniProgramNativeFileRef;
62
+ }
63
+ export interface MiniProgramNativeNetworkOpenPayload {
64
+ purpose: 'request' | 'download';
65
+ url: string;
66
+ method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
67
+ params?: Record<string, unknown>;
68
+ data?: unknown;
69
+ headers?: Record<string, string>;
70
+ bodyEncoding?: 'json' | 'form';
71
+ timeoutMs: number;
72
+ }
73
+ export interface MiniProgramNativeNetworkOpenResult {
74
+ status: number;
75
+ statusText?: string;
76
+ headers: Record<string, string[]>;
77
+ resourceId: string;
78
+ }
79
+ export interface MiniProgramNativeNetworkReadPayload {
80
+ resourceId: string;
81
+ maxBytes: number;
82
+ }
83
+ export interface MiniProgramNativeNetworkReleasePayload {
84
+ resourceId: string;
85
+ }
86
+ export interface MiniProgramNativeNetworkTransferPayload {
87
+ resourceId: string;
88
+ target: MiniProgramNativeFileRef;
89
+ mode: 'replace-existing' | 'create-exclusive' | 'upsert';
90
+ maxBytes?: number;
91
+ expectedBytes?: number;
92
+ }
93
+ export interface MiniProgramNativeNetworkTransferResult {
94
+ committedBytes: number;
95
+ }
96
+ export interface MiniProgramNativeFilesCreatePayload {
97
+ ref: MiniProgramNativeFileRef;
98
+ recursive: boolean;
99
+ exclusive: boolean;
100
+ }
101
+ export interface MiniProgramNativeFilesRemovePayload {
102
+ ref: MiniProgramNativeFileRef;
103
+ recursive?: boolean;
104
+ }
105
+ export interface MiniProgramNativeFilesRefPayload {
106
+ ref: MiniProgramNativeFileRef;
107
+ }
108
+ export interface MiniProgramNativeFilesOpenPayload extends MiniProgramNativeFilesRefPayload {
109
+ maxBytes: number;
110
+ }
111
+ export interface MiniProgramNativeFilesOpenResult {
112
+ resourceId: string;
113
+ }
114
+ /** openWrite 后按序发送;eof=true 的最后一块触发 atomic commit。 */
115
+ export interface MiniProgramNativeFilesWritePayload extends MiniProgramNativeBinaryChunk {
116
+ }
117
+ export type MiniProgramNativeFilesStatResult = {
118
+ kind: 'file';
119
+ size: number;
120
+ modifiedAt?: number;
121
+ createdAt?: number;
122
+ } | {
123
+ kind: 'directory';
124
+ modifiedAt?: number;
125
+ createdAt?: number;
126
+ };
127
+ export interface MiniProgramNativeFilesListPayload extends MiniProgramNativeFilesRefPayload {
128
+ limit: number;
129
+ }
130
+ export interface MiniProgramNativeFilesPickFilesPayload {
131
+ mode: 'read' | 'readwrite';
132
+ accept?: string[];
133
+ multiple: boolean;
134
+ }
135
+ export interface MiniProgramNativeFilesPickDirectoryPayload {
136
+ mode: 'read' | 'readwrite';
137
+ }
138
+ export interface MiniProgramNativeFilesSaveFilePayload {
139
+ suggestedName: string;
140
+ }
141
+ export interface MiniProgramNativeTransferProgress {
142
+ resourceId: string;
143
+ loaded: number;
144
+ }
145
+ export declare function isMiniProgramNativeHostMessage(value: unknown): value is MiniProgramNativeHostMessage;
146
+ export declare function isMiniProgramNativeHostDescription(value: unknown): value is MiniProgramNativeHostDescription;
147
+ export declare function isMiniProgramNativeBinaryChunk(value: unknown): value is MiniProgramNativeBinaryChunk;