@zero-transfer/dropbox 0.3.1 → 0.4.2

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/index.cjs CHANGED
@@ -64,6 +64,7 @@ __export(dropbox_exports, {
64
64
  createLocalProviderFactory: () => createLocalProviderFactory,
65
65
  createMemoryProviderFactory: () => createMemoryProviderFactory,
66
66
  createOAuthTokenSecretSource: () => createOAuthTokenSecretSource,
67
+ createPooledTransferClient: () => createPooledTransferClient,
67
68
  createProgressEvent: () => createProgressEvent,
68
69
  createProviderTransferExecutor: () => createProviderTransferExecutor,
69
70
  createRemoteBrowser: () => createRemoteBrowser,
@@ -1882,6 +1883,186 @@ function summarizeDiagnosticError(error) {
1882
1883
  return { message: String(error) };
1883
1884
  }
1884
1885
 
1886
+ // src/core/ConnectionPool.ts
1887
+ var DEFAULT_MAX_IDLE_PER_KEY = 4;
1888
+ var DEFAULT_IDLE_TIMEOUT_MS = 6e4;
1889
+ function createPooledTransferClient(inner, options = {}) {
1890
+ const maxIdlePerKey = Math.max(1, options.maxIdlePerKey ?? DEFAULT_MAX_IDLE_PER_KEY);
1891
+ const idleTimeoutMs = Math.max(0, options.idleTimeoutMs ?? DEFAULT_IDLE_TIMEOUT_MS);
1892
+ const keyOf = options.keyOf ?? defaultKeyOf;
1893
+ const state = {
1894
+ drained: false,
1895
+ idle: /* @__PURE__ */ new Map()
1896
+ };
1897
+ const release = (key, session, tainted) => {
1898
+ if (tainted || state.drained) {
1899
+ return safelyDisconnect(session);
1900
+ }
1901
+ let bucket = state.idle.get(key);
1902
+ if (bucket === void 0) {
1903
+ bucket = [];
1904
+ state.idle.set(key, bucket);
1905
+ }
1906
+ const entry = { session };
1907
+ if (idleTimeoutMs > 0) {
1908
+ entry.idleTimer = setTimeout(() => {
1909
+ evictEntry(state, key, entry);
1910
+ }, idleTimeoutMs);
1911
+ const timer = entry.idleTimer;
1912
+ if (timer !== void 0 && typeof timer.unref === "function") {
1913
+ timer.unref();
1914
+ }
1915
+ }
1916
+ bucket.push(entry);
1917
+ while (bucket.length > maxIdlePerKey) {
1918
+ const dropped = bucket.shift();
1919
+ if (dropped !== void 0) {
1920
+ clearEntryTimer(dropped);
1921
+ void safelyDisconnect(dropped.session);
1922
+ }
1923
+ }
1924
+ return Promise.resolve();
1925
+ };
1926
+ const acquire = async (profile) => {
1927
+ const key = keyOf(profile);
1928
+ const bucket = state.idle.get(key);
1929
+ if (bucket !== void 0 && bucket.length > 0) {
1930
+ const entry = bucket.pop();
1931
+ if (entry !== void 0) {
1932
+ clearEntryTimer(entry);
1933
+ if (bucket.length === 0) state.idle.delete(key);
1934
+ return { key, session: entry.session };
1935
+ }
1936
+ }
1937
+ const session = await inner.connect(profile);
1938
+ return { key, session };
1939
+ };
1940
+ return {
1941
+ connect: async (profile) => {
1942
+ const { key, session } = await acquire(profile);
1943
+ return wrapPooledSession(session, key, release);
1944
+ },
1945
+ drainPool: async () => {
1946
+ state.drained = true;
1947
+ const entries = [];
1948
+ for (const bucket of state.idle.values()) {
1949
+ for (const entry of bucket) {
1950
+ clearEntryTimer(entry);
1951
+ entries.push(entry);
1952
+ }
1953
+ }
1954
+ state.idle.clear();
1955
+ await Promise.all(entries.map((entry) => safelyDisconnect(entry.session)));
1956
+ },
1957
+ getCapabilities: ((providerId) => {
1958
+ if (providerId === void 0) return inner.getCapabilities();
1959
+ return inner.getCapabilities(providerId);
1960
+ }),
1961
+ hasProvider: (providerId) => inner.hasProvider(providerId),
1962
+ poolSize: () => {
1963
+ let total = 0;
1964
+ for (const bucket of state.idle.values()) total += bucket.length;
1965
+ return total;
1966
+ }
1967
+ };
1968
+ }
1969
+ function defaultKeyOf(profile) {
1970
+ const provider = profile.provider ?? profile.protocol ?? "unknown";
1971
+ const host = profile.host ?? "";
1972
+ const port = profile.port ?? "";
1973
+ const username = typeof profile.username === "string" ? profile.username : "";
1974
+ return `${provider}|${host}|${String(port)}|${username}`;
1975
+ }
1976
+ function evictEntry(state, key, entry) {
1977
+ const bucket = state.idle.get(key);
1978
+ if (bucket === void 0) return;
1979
+ const index = bucket.indexOf(entry);
1980
+ if (index < 0) return;
1981
+ bucket.splice(index, 1);
1982
+ if (bucket.length === 0) state.idle.delete(key);
1983
+ clearEntryTimer(entry);
1984
+ void safelyDisconnect(entry.session);
1985
+ }
1986
+ function clearEntryTimer(entry) {
1987
+ if (entry.idleTimer !== void 0) {
1988
+ clearTimeout(entry.idleTimer);
1989
+ delete entry.idleTimer;
1990
+ }
1991
+ }
1992
+ async function safelyDisconnect(session) {
1993
+ try {
1994
+ await session.disconnect();
1995
+ } catch {
1996
+ }
1997
+ }
1998
+ function isTaintingError(error) {
1999
+ return error instanceof ConnectionError || error instanceof TimeoutError || error instanceof ProtocolError;
2000
+ }
2001
+ function wrapPooledSession(session, key, release) {
2002
+ let tainted = false;
2003
+ let released = false;
2004
+ const guard = (fn) => {
2005
+ let promise;
2006
+ try {
2007
+ promise = fn();
2008
+ } catch (error) {
2009
+ if (isTaintingError(error)) tainted = true;
2010
+ return Promise.reject(error instanceof Error ? error : new Error(String(error)));
2011
+ }
2012
+ return promise.catch((error) => {
2013
+ if (isTaintingError(error)) tainted = true;
2014
+ throw error;
2015
+ });
2016
+ };
2017
+ const fs = wrapFs(session.fs, guard);
2018
+ const transfers = session.transfers === void 0 ? void 0 : wrapTransfers(session.transfers, guard);
2019
+ const wrapped = {
2020
+ capabilities: session.capabilities,
2021
+ disconnect: async () => {
2022
+ if (released) return;
2023
+ released = true;
2024
+ await release(key, session, tainted);
2025
+ },
2026
+ fs,
2027
+ provider: session.provider,
2028
+ ...transfers !== void 0 ? { transfers } : {}
2029
+ };
2030
+ if (typeof session.raw === "function") {
2031
+ const rawFn = session.raw.bind(session);
2032
+ wrapped.raw = () => rawFn();
2033
+ }
2034
+ return wrapped;
2035
+ }
2036
+ function wrapFs(fs, guard) {
2037
+ const wrapped = {
2038
+ list: (path2, options) => guard(() => options !== void 0 ? fs.list(path2, options) : fs.list(path2)),
2039
+ stat: (path2, options) => guard(() => options !== void 0 ? fs.stat(path2, options) : fs.stat(path2))
2040
+ };
2041
+ if (typeof fs.remove === "function") {
2042
+ const remove = fs.remove.bind(fs);
2043
+ wrapped.remove = (path2, options) => guard(() => options !== void 0 ? remove(path2, options) : remove(path2));
2044
+ }
2045
+ if (typeof fs.rename === "function") {
2046
+ const rename2 = fs.rename.bind(fs);
2047
+ wrapped.rename = (from, to, options) => guard(() => options !== void 0 ? rename2(from, to, options) : rename2(from, to));
2048
+ }
2049
+ if (typeof fs.mkdir === "function") {
2050
+ const mkdir2 = fs.mkdir.bind(fs);
2051
+ wrapped.mkdir = (path2, options) => guard(() => options !== void 0 ? mkdir2(path2, options) : mkdir2(path2));
2052
+ }
2053
+ if (typeof fs.rmdir === "function") {
2054
+ const rmdir = fs.rmdir.bind(fs);
2055
+ wrapped.rmdir = (path2, options) => guard(() => options !== void 0 ? rmdir(path2, options) : rmdir(path2));
2056
+ }
2057
+ return wrapped;
2058
+ }
2059
+ function wrapTransfers(transfers, guard) {
2060
+ return {
2061
+ read: (request) => guard(() => Promise.resolve(transfers.read(request))),
2062
+ write: (request) => guard(() => Promise.resolve(transfers.write(request)))
2063
+ };
2064
+ }
2065
+
1885
2066
  // src/providers/local/LocalProvider.ts
1886
2067
  var import_node_fs = require("fs");
1887
2068
  var import_promises2 = require("fs/promises");
@@ -5184,6 +5365,7 @@ async function collectChunks(source) {
5184
5365
  createLocalProviderFactory,
5185
5366
  createMemoryProviderFactory,
5186
5367
  createOAuthTokenSecretSource,
5368
+ createPooledTransferClient,
5187
5369
  createProgressEvent,
5188
5370
  createProviderTransferExecutor,
5189
5371
  createRemoteBrowser,