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