@dropgate/core 2.0.0-beta.1
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 +288 -0
- package/dist/index.browser.js +3 -0
- package/dist/index.browser.js.map +1 -0
- package/dist/index.cjs +1577 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +883 -0
- package/dist/index.d.ts +883 -0
- package/dist/index.js +1509 -0
- package/dist/index.js.map +1 -0
- package/dist/p2p/index.cjs +552 -0
- package/dist/p2p/index.cjs.map +1 -0
- package/dist/p2p/index.d.cts +369 -0
- package/dist/p2p/index.d.ts +369 -0
- package/dist/p2p/index.js +518 -0
- package/dist/p2p/index.js.map +1 -0
- package/package.json +88 -0
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server upload capabilities returned from the server info endpoint.
|
|
3
|
+
*/
|
|
4
|
+
interface UploadCapabilities {
|
|
5
|
+
/** Whether hosted uploads are enabled on the server. */
|
|
6
|
+
enabled: boolean;
|
|
7
|
+
/** Maximum file size in megabytes (0 = unlimited). */
|
|
8
|
+
maxSizeMB?: number;
|
|
9
|
+
/** Maximum file lifetime in hours (0 = unlimited). */
|
|
10
|
+
maxLifetimeHours?: number;
|
|
11
|
+
/** Whether end-to-end encryption is supported. */
|
|
12
|
+
e2ee?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Server P2P (direct transfer) capabilities.
|
|
16
|
+
*/
|
|
17
|
+
interface P2PCapabilities {
|
|
18
|
+
/** Whether P2P transfers are enabled on the server. */
|
|
19
|
+
enabled: boolean;
|
|
20
|
+
/** Path to the PeerJS signaling server. */
|
|
21
|
+
peerjsPath?: string;
|
|
22
|
+
/** ICE servers for WebRTC connectivity. */
|
|
23
|
+
iceServers?: RTCIceServer[];
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Server Web UI capabilities.
|
|
27
|
+
*/
|
|
28
|
+
interface WebUICapabilities {
|
|
29
|
+
/** Whether the Web UI is enabled on the server. */
|
|
30
|
+
enabled: boolean;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Combined server capabilities object.
|
|
34
|
+
*/
|
|
35
|
+
interface ServerCapabilities {
|
|
36
|
+
/** Hosted upload capabilities. */
|
|
37
|
+
upload?: UploadCapabilities;
|
|
38
|
+
/** P2P transfer capabilities. */
|
|
39
|
+
p2p?: P2PCapabilities;
|
|
40
|
+
/** Web UI capabilities. */
|
|
41
|
+
webUI?: WebUICapabilities;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Server information returned from the /api/info endpoint.
|
|
45
|
+
*/
|
|
46
|
+
interface ServerInfo {
|
|
47
|
+
/** Display name of the server. */
|
|
48
|
+
name?: string;
|
|
49
|
+
/** Server version string. */
|
|
50
|
+
version: string;
|
|
51
|
+
/** Server capabilities. */
|
|
52
|
+
capabilities?: ServerCapabilities;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Crypto adapter interface compatible with the Web Crypto API.
|
|
56
|
+
* Used for encryption operations and secure random generation.
|
|
57
|
+
*/
|
|
58
|
+
interface CryptoAdapter {
|
|
59
|
+
/** SubtleCrypto interface for cryptographic operations. */
|
|
60
|
+
readonly subtle: SubtleCrypto;
|
|
61
|
+
/** Fill an array with cryptographically secure random values. */
|
|
62
|
+
getRandomValues<T extends ArrayBufferView | null>(array: T): T;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* File source abstraction for cross-environment compatibility.
|
|
66
|
+
* Works with browser File/Blob and can be implemented for Node.js streams.
|
|
67
|
+
*/
|
|
68
|
+
interface FileSource {
|
|
69
|
+
/** File name. */
|
|
70
|
+
readonly name: string;
|
|
71
|
+
/** File size in bytes. */
|
|
72
|
+
readonly size: number;
|
|
73
|
+
/** MIME type of the file. */
|
|
74
|
+
readonly type?: string;
|
|
75
|
+
/** Extract a slice of the file. */
|
|
76
|
+
slice(start: number, end: number): FileSource;
|
|
77
|
+
/** Read the entire file as an ArrayBuffer. */
|
|
78
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* PeerJS Peer constructor interface.
|
|
83
|
+
* Consumer must provide this constructor to P2P functions.
|
|
84
|
+
*/
|
|
85
|
+
interface PeerConstructor {
|
|
86
|
+
new (id?: string, options?: PeerOptions): PeerInstance;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* PeerJS connection options.
|
|
90
|
+
*/
|
|
91
|
+
interface PeerOptions {
|
|
92
|
+
/** PeerJS server hostname. */
|
|
93
|
+
host?: string;
|
|
94
|
+
/** PeerJS server port. */
|
|
95
|
+
port?: number;
|
|
96
|
+
/** PeerJS server path. */
|
|
97
|
+
path?: string;
|
|
98
|
+
/** Whether to use secure WebSocket connection. */
|
|
99
|
+
secure?: boolean;
|
|
100
|
+
/** WebRTC configuration. */
|
|
101
|
+
config?: {
|
|
102
|
+
/** ICE servers for NAT traversal. */
|
|
103
|
+
iceServers?: RTCIceServer[];
|
|
104
|
+
};
|
|
105
|
+
/** PeerJS debug level (0-3). */
|
|
106
|
+
debug?: number;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* PeerJS Peer instance interface.
|
|
110
|
+
* Represents a connection to the PeerJS signaling server.
|
|
111
|
+
*/
|
|
112
|
+
interface PeerInstance {
|
|
113
|
+
/** Register an event handler. */
|
|
114
|
+
on(event: 'open', callback: (id: string) => void): void;
|
|
115
|
+
on(event: 'connection', callback: (conn: DataConnection) => void): void;
|
|
116
|
+
on(event: 'error', callback: (err: Error) => void): void;
|
|
117
|
+
on(event: 'close', callback: () => void): void;
|
|
118
|
+
on(event: string, callback: (...args: unknown[]) => void): void;
|
|
119
|
+
/** Connect to another peer by ID. */
|
|
120
|
+
connect(peerId: string, options?: {
|
|
121
|
+
reliable?: boolean;
|
|
122
|
+
}): DataConnection;
|
|
123
|
+
/** Destroy this peer and close all connections. */
|
|
124
|
+
destroy(): void;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* PeerJS DataConnection interface.
|
|
128
|
+
* Represents a WebRTC data channel connection between peers.
|
|
129
|
+
*/
|
|
130
|
+
interface DataConnection {
|
|
131
|
+
/** Register an event handler. */
|
|
132
|
+
on(event: 'open', callback: () => void): void;
|
|
133
|
+
on(event: 'data', callback: (data: unknown) => void): void;
|
|
134
|
+
on(event: 'close', callback: () => void): void;
|
|
135
|
+
on(event: 'error', callback: (err: Error) => void): void;
|
|
136
|
+
on(event: string, callback: (...args: unknown[]) => void): void;
|
|
137
|
+
/** Send data to the connected peer. */
|
|
138
|
+
send(data: unknown): void;
|
|
139
|
+
/** Close the data connection. */
|
|
140
|
+
close(): void;
|
|
141
|
+
/** Internal WebRTC data channel (for buffer monitoring). */
|
|
142
|
+
_dc?: RTCDataChannel;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Options for starting a P2P send session.
|
|
146
|
+
*/
|
|
147
|
+
interface P2PSendOptions {
|
|
148
|
+
/** File to send */
|
|
149
|
+
file: FileSource;
|
|
150
|
+
/** PeerJS Peer constructor - REQUIRED */
|
|
151
|
+
Peer: PeerConstructor;
|
|
152
|
+
/** Server info (optional, for capability checking) */
|
|
153
|
+
serverInfo?: ServerInfo;
|
|
154
|
+
/** PeerJS server host */
|
|
155
|
+
host?: string;
|
|
156
|
+
/** PeerJS server port */
|
|
157
|
+
port?: number;
|
|
158
|
+
/** PeerJS server path (default: /peerjs) */
|
|
159
|
+
peerjsPath?: string;
|
|
160
|
+
/** Whether to use secure connection */
|
|
161
|
+
secure?: boolean;
|
|
162
|
+
/** ICE servers for WebRTC */
|
|
163
|
+
iceServers?: RTCIceServer[];
|
|
164
|
+
/** Custom code generator function */
|
|
165
|
+
codeGenerator?: (cryptoObj?: CryptoAdapter) => string;
|
|
166
|
+
/** Crypto object for secure code generation */
|
|
167
|
+
cryptoObj?: CryptoAdapter;
|
|
168
|
+
/** Max attempts to register a peer ID */
|
|
169
|
+
maxAttempts?: number;
|
|
170
|
+
/** Chunk size for data transfer */
|
|
171
|
+
chunkSize?: number;
|
|
172
|
+
/** Timeout waiting for receiver ready signal */
|
|
173
|
+
readyTimeoutMs?: number;
|
|
174
|
+
/** Timeout waiting for end acknowledgment */
|
|
175
|
+
endAckTimeoutMs?: number;
|
|
176
|
+
/** Buffer high water mark for flow control */
|
|
177
|
+
bufferHighWaterMark?: number;
|
|
178
|
+
/** Buffer low water mark for flow control */
|
|
179
|
+
bufferLowWaterMark?: number;
|
|
180
|
+
/** Callback when code is generated */
|
|
181
|
+
onCode?: (code: string, attempt: number) => void;
|
|
182
|
+
/** Callback for status updates */
|
|
183
|
+
onStatus?: (evt: {
|
|
184
|
+
phase: string;
|
|
185
|
+
message: string;
|
|
186
|
+
}) => void;
|
|
187
|
+
/** Callback for progress updates */
|
|
188
|
+
onProgress?: (evt: {
|
|
189
|
+
sent: number;
|
|
190
|
+
total: number;
|
|
191
|
+
percent: number;
|
|
192
|
+
}) => void;
|
|
193
|
+
/** Callback when transfer completes */
|
|
194
|
+
onComplete?: () => void;
|
|
195
|
+
/** Callback on error */
|
|
196
|
+
onError?: (err: Error) => void;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Return value from startP2PSend containing session control.
|
|
200
|
+
*/
|
|
201
|
+
interface P2PSendSession {
|
|
202
|
+
/** The PeerJS peer instance. */
|
|
203
|
+
peer: PeerInstance;
|
|
204
|
+
/** The generated sharing code. */
|
|
205
|
+
code: string;
|
|
206
|
+
/** Stop the session and clean up resources. */
|
|
207
|
+
stop: () => void;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Options for starting a P2P receive session.
|
|
211
|
+
*/
|
|
212
|
+
interface P2PReceiveOptions {
|
|
213
|
+
/** Sharing code to connect to */
|
|
214
|
+
code: string;
|
|
215
|
+
/** PeerJS Peer constructor - REQUIRED */
|
|
216
|
+
Peer: PeerConstructor;
|
|
217
|
+
/** Server info (optional, for capability checking) */
|
|
218
|
+
serverInfo?: ServerInfo;
|
|
219
|
+
/** PeerJS server host */
|
|
220
|
+
host?: string;
|
|
221
|
+
/** PeerJS server port */
|
|
222
|
+
port?: number;
|
|
223
|
+
/** PeerJS server path (default: /peerjs) */
|
|
224
|
+
peerjsPath?: string;
|
|
225
|
+
/** Whether to use secure connection */
|
|
226
|
+
secure?: boolean;
|
|
227
|
+
/** ICE servers for WebRTC */
|
|
228
|
+
iceServers?: RTCIceServer[];
|
|
229
|
+
/** Callback for status updates */
|
|
230
|
+
onStatus?: (evt: {
|
|
231
|
+
phase: string;
|
|
232
|
+
message: string;
|
|
233
|
+
}) => void;
|
|
234
|
+
/** Callback when file metadata is received */
|
|
235
|
+
onMeta?: (evt: {
|
|
236
|
+
name: string;
|
|
237
|
+
total: number;
|
|
238
|
+
}) => void;
|
|
239
|
+
/** Callback when data chunk is received - consumer handles file writing */
|
|
240
|
+
onData?: (chunk: Uint8Array) => Promise<void> | void;
|
|
241
|
+
/** Callback for progress updates */
|
|
242
|
+
onProgress?: (evt: {
|
|
243
|
+
received: number;
|
|
244
|
+
total: number;
|
|
245
|
+
percent: number;
|
|
246
|
+
}) => void;
|
|
247
|
+
/** Callback when transfer completes */
|
|
248
|
+
onComplete?: (evt: {
|
|
249
|
+
received: number;
|
|
250
|
+
total: number;
|
|
251
|
+
}) => void;
|
|
252
|
+
/** Callback on error */
|
|
253
|
+
onError?: (err: Error) => void;
|
|
254
|
+
/** Callback when sender disconnects */
|
|
255
|
+
onDisconnect?: () => void;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Return value from startP2PReceive containing session control.
|
|
259
|
+
*/
|
|
260
|
+
interface P2PReceiveSession {
|
|
261
|
+
/** The PeerJS peer instance. */
|
|
262
|
+
peer: PeerInstance;
|
|
263
|
+
/** Stop the session and clean up resources. */
|
|
264
|
+
stop: () => void;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Start a direct transfer (P2P) sender session.
|
|
269
|
+
*
|
|
270
|
+
* IMPORTANT: Consumer must provide the PeerJS Peer constructor.
|
|
271
|
+
* This removes DOM coupling (no script injection).
|
|
272
|
+
*
|
|
273
|
+
* Example:
|
|
274
|
+
* ```js
|
|
275
|
+
* import Peer from 'peerjs';
|
|
276
|
+
* import { startP2PSend } from '@dropgate/core/p2p';
|
|
277
|
+
*
|
|
278
|
+
* const session = await startP2PSend({
|
|
279
|
+
* file: myFile,
|
|
280
|
+
* Peer,
|
|
281
|
+
* host: 'dropgate.link',
|
|
282
|
+
* secure: true,
|
|
283
|
+
* onCode: (code) => console.log('Share this code:', code),
|
|
284
|
+
* onProgress: (evt) => console.log(`${evt.percent}% sent`),
|
|
285
|
+
* onComplete: () => console.log('Done!'),
|
|
286
|
+
* });
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
289
|
+
declare function startP2PSend(opts: P2PSendOptions): Promise<P2PSendSession>;
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Start a direct transfer (P2P) receiver session.
|
|
293
|
+
*
|
|
294
|
+
* IMPORTANT: Consumer must provide the PeerJS Peer constructor and handle file writing.
|
|
295
|
+
* This removes DOM coupling (no streamSaver).
|
|
296
|
+
*
|
|
297
|
+
* Example:
|
|
298
|
+
* ```js
|
|
299
|
+
* import Peer from 'peerjs';
|
|
300
|
+
* import { startP2PReceive } from '@dropgate/core/p2p';
|
|
301
|
+
*
|
|
302
|
+
* let writer;
|
|
303
|
+
* const session = await startP2PReceive({
|
|
304
|
+
* code: 'ABCD-1234',
|
|
305
|
+
* Peer,
|
|
306
|
+
* host: 'dropgate.link',
|
|
307
|
+
* secure: true,
|
|
308
|
+
* onMeta: ({ name, total }) => {
|
|
309
|
+
* // Consumer creates file writer
|
|
310
|
+
* writer = createWriteStream(name);
|
|
311
|
+
* },
|
|
312
|
+
* onData: async (chunk) => {
|
|
313
|
+
* // Consumer writes data
|
|
314
|
+
* await writer.write(chunk);
|
|
315
|
+
* },
|
|
316
|
+
* onComplete: () => {
|
|
317
|
+
* writer.close();
|
|
318
|
+
* console.log('Done!');
|
|
319
|
+
* },
|
|
320
|
+
* });
|
|
321
|
+
* ```
|
|
322
|
+
*/
|
|
323
|
+
declare function startP2PReceive(opts: P2PReceiveOptions): Promise<P2PReceiveSession>;
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Check if a hostname is localhost
|
|
327
|
+
*/
|
|
328
|
+
declare function isLocalhostHostname(hostname: string): boolean;
|
|
329
|
+
/**
|
|
330
|
+
* Check if the current context allows P2P (HTTPS or localhost)
|
|
331
|
+
*/
|
|
332
|
+
declare function isSecureContextForP2P(hostname?: string, isSecureContext?: boolean): boolean;
|
|
333
|
+
/**
|
|
334
|
+
* Generate a P2P sharing code using cryptographically secure random.
|
|
335
|
+
* Format: XXXX-0000 (4 letters + 4 digits)
|
|
336
|
+
*/
|
|
337
|
+
declare function generateP2PCode(cryptoObj?: CryptoAdapter): string;
|
|
338
|
+
/**
|
|
339
|
+
* Check if a string looks like a P2P sharing code
|
|
340
|
+
*/
|
|
341
|
+
declare function isP2PCodeLike(code: string): boolean;
|
|
342
|
+
|
|
343
|
+
interface BuildPeerOptionsInput {
|
|
344
|
+
host?: string;
|
|
345
|
+
port?: number;
|
|
346
|
+
peerjsPath?: string;
|
|
347
|
+
secure?: boolean;
|
|
348
|
+
iceServers?: RTCIceServer[];
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Build PeerJS connection options
|
|
352
|
+
*/
|
|
353
|
+
declare function buildPeerOptions(opts?: BuildPeerOptionsInput): PeerOptions;
|
|
354
|
+
interface CreatePeerWithRetriesOptions {
|
|
355
|
+
code?: string | null;
|
|
356
|
+
codeGenerator: () => string;
|
|
357
|
+
maxAttempts: number;
|
|
358
|
+
buildPeer: (id: string) => PeerInstance;
|
|
359
|
+
onCode?: (code: string, attempt: number) => void;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Create a peer with retries if the code is already taken
|
|
363
|
+
*/
|
|
364
|
+
declare function createPeerWithRetries(opts: CreatePeerWithRetriesOptions): Promise<{
|
|
365
|
+
peer: PeerInstance;
|
|
366
|
+
code: string;
|
|
367
|
+
}>;
|
|
368
|
+
|
|
369
|
+
export { type DataConnection, type P2PReceiveOptions, type P2PReceiveSession, type P2PSendOptions, type P2PSendSession, type PeerConstructor, type PeerInstance, type PeerOptions, buildPeerOptions, createPeerWithRetries, generateP2PCode, isLocalhostHostname, isP2PCodeLike, isSecureContextForP2P, startP2PReceive, startP2PSend };
|