filepizza-client 2.0.1 → 2.1.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 +50 -4
- package/dist/filepizza-downloader.d.ts +18 -100
- package/dist/filepizza-downloader.js +88 -215
- package/dist/filepizza-downloader.js.map +1 -1
- package/dist/filepizza-uploader.d.ts +14 -95
- package/dist/filepizza-uploader.js +79 -249
- package/dist/filepizza-uploader.js.map +1 -1
- package/dist/peerjs.d.ts +12 -0
- package/dist/peerjs.js +102 -0
- package/dist/peerjs.js.map +1 -0
- package/dist/types.d.ts +6 -2
- package/dist/types.js +4 -2
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
- package/src/filepizza-downloader.ts +373 -459
- package/src/filepizza-uploader.ts +238 -392
- package/src/peerjs.ts +147 -0
- package/src/types.ts +37 -28
package/dist/peerjs.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// src/peerjs.ts
|
|
2
|
+
import Peer from 'peerjs';
|
|
3
|
+
const DEFAULT_ICE_SERVERS = [
|
|
4
|
+
{ urls: 'stun:stun.l.google.com:19302' },
|
|
5
|
+
];
|
|
6
|
+
function normalizeBaseUrl(url) {
|
|
7
|
+
return url.replace(/\/+$/, '');
|
|
8
|
+
}
|
|
9
|
+
function parsePeerJSServerUrl(serverUrlString) {
|
|
10
|
+
const serverUrl = new URL(serverUrlString);
|
|
11
|
+
const secure = serverUrl.protocol === 'https:';
|
|
12
|
+
return {
|
|
13
|
+
host: serverUrl.hostname,
|
|
14
|
+
port: serverUrl.port
|
|
15
|
+
? Number.parseInt(serverUrl.port, 10)
|
|
16
|
+
: secure
|
|
17
|
+
? 443
|
|
18
|
+
: 80,
|
|
19
|
+
path: serverUrl.pathname,
|
|
20
|
+
secure,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
const iceCache = new Map();
|
|
24
|
+
function fetchIceEndpoint(filePizzaServerUrl) {
|
|
25
|
+
const baseUrl = normalizeBaseUrl(filePizzaServerUrl);
|
|
26
|
+
const cached = iceCache.get(baseUrl);
|
|
27
|
+
if (cached) {
|
|
28
|
+
return cached;
|
|
29
|
+
}
|
|
30
|
+
const promise = (async () => {
|
|
31
|
+
const response = await fetch(`${baseUrl}/api/ice`, { method: 'POST' });
|
|
32
|
+
if (!response.ok) {
|
|
33
|
+
throw new Error(`Failed to fetch /api/ice: ${response.status}`);
|
|
34
|
+
}
|
|
35
|
+
return (await response.json());
|
|
36
|
+
})();
|
|
37
|
+
promise.catch(() => iceCache.delete(baseUrl));
|
|
38
|
+
iceCache.set(baseUrl, promise);
|
|
39
|
+
return promise;
|
|
40
|
+
}
|
|
41
|
+
export async function getIceServers(filePizzaServerUrl) {
|
|
42
|
+
try {
|
|
43
|
+
const data = await fetchIceEndpoint(filePizzaServerUrl);
|
|
44
|
+
return data.iceServers || DEFAULT_ICE_SERVERS;
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
console.error('Error getting ICE servers:', error);
|
|
48
|
+
return DEFAULT_ICE_SERVERS;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export async function discoverPeerJSSignalingServer(filePizzaServerUrl) {
|
|
52
|
+
try {
|
|
53
|
+
const data = await fetchIceEndpoint(filePizzaServerUrl);
|
|
54
|
+
if (data.host) {
|
|
55
|
+
return {
|
|
56
|
+
host: data.host,
|
|
57
|
+
path: data.path,
|
|
58
|
+
port: data.port,
|
|
59
|
+
secure: data.secure,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
const firstServer = data.servers?.[0];
|
|
63
|
+
if (!firstServer) {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
return parsePeerJSServerUrl(firstServer);
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
console.error('Error discovering PeerJS signaling server:', error);
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
export async function buildPeerOptions({ filePizzaServerUrl, iceServers, peerJSSignalingServer, discoverPeerJSSignalingServer: shouldDiscover = true, }) {
|
|
74
|
+
const resolvedIceServers = iceServers || (await getIceServers(filePizzaServerUrl));
|
|
75
|
+
const discoveredPeerJSSignalingServer = peerJSSignalingServer || shouldDiscover
|
|
76
|
+
? peerJSSignalingServer ||
|
|
77
|
+
(await discoverPeerJSSignalingServer(filePizzaServerUrl))
|
|
78
|
+
: undefined;
|
|
79
|
+
return {
|
|
80
|
+
...discoveredPeerJSSignalingServer,
|
|
81
|
+
config: {
|
|
82
|
+
iceServers: resolvedIceServers,
|
|
83
|
+
},
|
|
84
|
+
debug: 2,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
export async function createPeer(options) {
|
|
88
|
+
const peerOptions = await buildPeerOptions(options);
|
|
89
|
+
const peer = new Peer(peerOptions);
|
|
90
|
+
if (peer.id) {
|
|
91
|
+
return peer;
|
|
92
|
+
}
|
|
93
|
+
await new Promise((resolve) => {
|
|
94
|
+
const onOpen = () => {
|
|
95
|
+
peer.off('open', onOpen);
|
|
96
|
+
resolve();
|
|
97
|
+
};
|
|
98
|
+
peer.on('open', onOpen);
|
|
99
|
+
});
|
|
100
|
+
return peer;
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=peerjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"peerjs.js","sourceRoot":"","sources":["../src/peerjs.ts"],"names":[],"mappings":"AAAA,gBAAgB;AAChB,OAAO,IAAqB,MAAM,QAAQ,CAAA;AAG1C,MAAM,mBAAmB,GAAmB;IACxC,EAAE,IAAI,EAAE,8BAA8B,EAAE;CAC3C,CAAA;AAkBD,SAAS,gBAAgB,CAAC,GAAW;IACjC,OAAO,GAAG,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;AAClC,CAAC;AAED,SAAS,oBAAoB,CAAC,eAAuB;IACjD,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,eAAe,CAAC,CAAA;I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CAAC,CAAC,CAAA;IAE3D,MAAM,+BAA+B,GACjC,qBAAqB,IAAI,cAAc;QACnC,CAAC,CAAC,qBAAqB;YACvB,CAAC,MAAM,6BAA6B,CAAC,kBAAkB,CAAC,CAAC;QACzD,CAAC,CAAC,SAAS,CAAA;IAEnB,OAAO;QACH,GAAG,+BAA+B;QAClC,MAAM,EAAE;YACJ,UAAU,EAAE,kBAAkB;SACjC;QACD,KAAK,EAAE,CAAC;KACX,CAAA;AACL,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,OAA4B;IACzD,MAAM,WAAW,GAAG,MAAM,gBAAgB,CAAC,OAAO,CAAC,CAAA;IACnD,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,WAAW,CAAC,CAAA;IAElC,IAAI,IAAI,CAAC,EAAE,EAAE,CAAC;QACV,OAAO,IAAI,CAAA;IACf,CAAC;IAED,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;QAChC,MAAM,MAAM,GAAG,GAAG,EAAE;YAChB,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;YACxB,OAAO,EAAE,CAAA;QACb,CAAC,CAAA;QACD,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC3B,CAAC,CAAC,CAAA;IAEF,OAAO,IAAI,CAAA;AACf,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import type { PeerOptions } from 'peerjs';
|
|
1
2
|
export interface EventEmitter {
|
|
2
3
|
on(event: string, listener: (...args: any[]) => void): this;
|
|
3
4
|
off(event: string, listener: (...args: any[]) => void): this;
|
|
4
5
|
emit(event: string, ...args: any[]): boolean;
|
|
5
6
|
}
|
|
7
|
+
export type PeerJSSignalingServer = Pick<PeerOptions, 'host' | 'port' | 'path' | 'secure' | 'key' | 'token' | 'pingInterval'>;
|
|
6
8
|
/**
|
|
7
9
|
* Connection status
|
|
8
10
|
*/
|
|
@@ -59,15 +61,17 @@ export interface ConnectionInfo {
|
|
|
59
61
|
mobileModel?: string;
|
|
60
62
|
}
|
|
61
63
|
/**
|
|
62
|
-
* Message types for peer-to-peer communication
|
|
64
|
+
* Message types for peer-to-peer communication.
|
|
65
|
+
*
|
|
66
|
+
* This mirrors the server frontend protocol in filepizza-server/src/messages.ts.
|
|
63
67
|
*/
|
|
64
68
|
export declare enum MessageType {
|
|
65
69
|
RequestInfo = "RequestInfo",
|
|
66
70
|
Info = "Info",
|
|
67
71
|
Start = "Start",
|
|
68
72
|
Chunk = "Chunk",
|
|
73
|
+
ChunkAck = "ChunkAck",
|
|
69
74
|
Pause = "Pause",
|
|
70
|
-
Resume = "Resume",
|
|
71
75
|
Done = "Done",
|
|
72
76
|
Error = "Error",
|
|
73
77
|
PasswordRequired = "PasswordRequired",
|
package/dist/types.js
CHANGED
|
@@ -15,7 +15,9 @@ export var ConnectionStatus;
|
|
|
15
15
|
ConnectionStatus["Error"] = "ERROR";
|
|
16
16
|
})(ConnectionStatus || (ConnectionStatus = {}));
|
|
17
17
|
/**
|
|
18
|
-
* Message types for peer-to-peer communication
|
|
18
|
+
* Message types for peer-to-peer communication.
|
|
19
|
+
*
|
|
20
|
+
* This mirrors the server frontend protocol in filepizza-server/src/messages.ts.
|
|
19
21
|
*/
|
|
20
22
|
export var MessageType;
|
|
21
23
|
(function (MessageType) {
|
|
@@ -23,8 +25,8 @@ export var MessageType;
|
|
|
23
25
|
MessageType["Info"] = "Info";
|
|
24
26
|
MessageType["Start"] = "Start";
|
|
25
27
|
MessageType["Chunk"] = "Chunk";
|
|
28
|
+
MessageType["ChunkAck"] = "ChunkAck";
|
|
26
29
|
MessageType["Pause"] = "Pause";
|
|
27
|
-
MessageType["Resume"] = "Resume";
|
|
28
30
|
MessageType["Done"] = "Done";
|
|
29
31
|
MessageType["Error"] = "Error";
|
|
30
32
|
MessageType["PasswordRequired"] = "PasswordRequired";
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAcA;;GAEG;AACH,MAAM,CAAN,IAAY,gBAWX;AAXD,WAAY,gBAAgB;IAC1B,uCAAmB,CAAA;IACnB,mCAAe,CAAA;IACf,qCAAiB,CAAA;IACjB,2CAAuB,CAAA;IACvB,+CAA2B,CAAA;IAC3B,iCAAa,CAAA;IACb,qDAAiC,CAAA;IACjC,wDAAoC,CAAA;IACpC,qCAAiB,CAAA;IACjB,mCAAe,CAAA;AACjB,CAAC,EAXW,gBAAgB,KAAhB,gBAAgB,QAW3B;AA8CD;;;;GAIG;AACH,MAAM,CAAN,IAAY,WAYX;AAZD,WAAY,WAAW;IACrB,0CAA2B,CAAA;IAC3B,4BAAa,CAAA;IACb,8BAAe,CAAA;IACf,8BAAe,CAAA;IACf,oCAAqB,CAAA;IACrB,8BAAe,CAAA;IACf,4BAAa,CAAA;IACb,8BAAe,CAAA;IACf,oDAAqC,CAAA;IACrC,0CAA2B,CAAA;IAC3B,gCAAiB,CAAA;AACnB,CAAC,EAZW,WAAW,KAAX,WAAW,QAYtB"}
|
package/package.json
CHANGED