Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10-getfolderscontents-not-returning-files-list #11

Merged
merged 3 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 50 additions & 67 deletions dist/index.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
Expand All @@ -10,48 +10,39 @@ var __export = (target, all) => {
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if ((from && typeof from === 'object') || typeof from === 'function') {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
});
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (
(target = mod != null ? __create(__getProtoOf(mod)) : {}),
__copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule
? __defProp(target, 'default', { value: mod, enumerable: true })
: target,
mod,
)
);
var __toCommonJS = (mod) =>
__copyProps(__defProp({}, '__esModule', { value: true }), mod);
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/index.ts
var src_exports = {};
__export(src_exports, {
default: () => src_default,
default: () => src_default
});
module.exports = __toCommonJS(src_exports);

// src/nextdav.class.ts
var import_roarr = require('roarr');
var import_url = require('url');
var import_path = require('path');
var import_got = __toESM(require('got'), 1);
var import_fast_xml_parser = require('fast-xml-parser');
var import_http_proxy_agent = require('http-proxy-agent');
var import_https_proxy_agent = require('https-proxy-agent');
var import_socks_proxy_agent = require('socks-proxy-agent');
var import_roarr = require("roarr");
var import_url = require("url");
var import_path = require("path");
var import_got = __toESM(require("got"), 1);
var import_fast_xml_parser = require("fast-xml-parser");
var import_http_proxy_agent = require("http-proxy-agent");
var import_https_proxy_agent = require("https-proxy-agent");
var import_socks_proxy_agent = require("socks-proxy-agent");
var nextdav = class {
url;
options;
Expand All @@ -61,7 +52,7 @@ var nextdav = class {
this.options = options;
if (username && password) {
this.basicAuth = Buffer.from(`${username}:${password}`).toString(
'base64',
"base64"
);
}
}
Expand All @@ -76,29 +67,26 @@ var nextdav = class {
if ((_a = this.options) == null ? void 0 : _a.proxy) {
switch (this.options.proxy.protocol) {
default:
case 'http':
httpAgent = new import_http_proxy_agent.HttpProxyAgent(
`http://${this.options.proxy.host}`,
{
port: this.options.proxy.port,
},
);
case "http":
httpAgent = new import_http_proxy_agent.HttpProxyAgent(`http://${this.options.proxy.host}`, {
port: this.options.proxy.port
});
break;
case 'https':
case "https":
httpsAgent = new import_https_proxy_agent.HttpsProxyAgent(
`https://${this.options.proxy.host}`,
{
port: this.options.proxy.port,
},
port: this.options.proxy.port
}
);
break;
case 'socks4':
case 'socks5':
case "socks4":
case "socks5":
httpAgent = new import_socks_proxy_agent.SocksProxyAgent(
`${this.options.proxy.protocol}://${this.options.proxy.host}:${this.options.proxy.port}`,
`${this.options.proxy.protocol}://${this.options.proxy.host}:${this.options.proxy.port}`
);
httpsAgent = new import_socks_proxy_agent.SocksProxyAgent(
`${this.options.proxy.protocol}://${this.options.proxy.host}:${this.options.proxy.port}`,
`${this.options.proxy.protocol}://${this.options.proxy.host}:${this.options.proxy.port}`
);
break;
}
Expand All @@ -112,35 +100,32 @@ var nextdav = class {
}
}
if (this.basicAuth) {
headers['Authorization'] = `Basic ${this.basicAuth}`;
headers["Authorization"] = `Basic ${this.basicAuth}`;
}
if (httpsAgent !== void 0 || httpAgent !== void 0) {
(0, import_roarr.Roarr)(
{ application: 'nextdav' },
'Using custom agents',
);
(0, import_roarr.Roarr)({ application: "nextdav" }, "Using custom agents");
}
return import_got.default.extend({
headers,
agent: {
https: httpsAgent,
http: httpAgent,
},
http: httpAgent
}
});
}
/**
* Retrive contents of the provided folder
*/
async getFolderContents(path = '/') {
async getFolderContents(path = "/") {
const fullUrl = (0, import_path.join)(this.url.href, path);
const client = await this.getClient();
try {
const rawResponse = await client(fullUrl, {
method: 'PROPFIND',
method: "PROPFIND"
});
return this.buildContentsObject(rawResponse.body.toString());
} catch (error) {
(0, import_roarr.Roarr)({ application: 'nextdav' }, error.toString());
(0, import_roarr.Roarr)({ application: "nextdav" }, error.toString());
return false;
}
}
Expand All @@ -154,20 +139,21 @@ var nextdav = class {
const response = await client.get(fullUrl);
return response.rawBody;
} catch (error) {
(0, import_roarr.Roarr)({ application: 'nextdav' }, error.toString());
(0, import_roarr.Roarr)({ application: "nextdav" }, error.toString());
return false;
}
}
parseXml(xmlData) {
const parser = new import_fast_xml_parser.XMLParser({
ignoreAttributes: false,
updateTag(tagName) {
return tagName.replace('d:', '').replace('D:', '');
},
return tagName.replace("d:", "").replace("D:", "");
}
});
return parser.parse(xmlData);
}
buildContentsObject(xmlString) {
var _a;
const data = this.parseXml(xmlString);
const collections = [];
const files = [];
Expand All @@ -185,29 +171,26 @@ var nextdav = class {
} else {
propstat = content.propstat;
}
if (propstat.prop.resourcetype !== '') {
if (propstat.prop.resourcetype !== "") {
const name = (0, import_path.basename)(content.href);
if (name) {
collections.push({
name,
lastmod: propstat.prop.getlastmodified,
lastmod: propstat.prop.getlastmodified
});
}
} else {
const name = (0, import_path.basename)(content.href);
const mime = propstat.prop.getcontenttype;
const mime = (_a = propstat.prop.getcontenttype) == null ? void 0 : _a.split(";").at(0);
const length = Number(propstat.prop.getcontentlength);
if (name && mime && length) {
if (name !== void 0 && mime !== void 0 && length !== void 0) {
files.push({
name,
dirname: (0, import_path.dirname)(content.href).replace(
this.url.href,
'/',
),
dirname: content.href.replace(name, "").replace(this.url.href, "/"),
lastmod: propstat.prop.getlastmodified,
mime,
length,
extension: name.split('.').at(-1) || '',
extension: name.split(".").at(-1) || ""
});
}
}
Expand Down
86 changes: 37 additions & 49 deletions dist/index.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,53 @@ import http from 'http';
import https from 'https';

interface Nextdav {
getFolderContents(path: string): Promise<[Collection[], File[]] | boolean>;
getFileAsBuffer(path: string): Promise<Buffer | false>;
getFolderContents(path: string): Promise<[Collection[], File[]] | boolean>;
getFileAsBuffer(path: string): Promise<Buffer | false>;
}
interface Proxy {
host: string;
port: number;
protocol: 'http' | 'https' | 'socks4' | 'socks5';
host: string;
port: number;
protocol: 'http' | 'https' | 'socks4' | 'socks5';
}
interface Options {
proxy?: Proxy;
customAgents?: {
http?: http.Agent;
https?: https.Agent;
};
proxy?: Proxy;
customAgents?: {
http?: http.Agent;
https?: https.Agent;
};
}
interface Collection {
name: string;
lastmod?: Date;
name: string;
lastmod?: Date;
}
interface File {
name: string;
dirname: string;
lastmod?: Date;
mime: string;
length: number;
extension: string;
name: string;
dirname: string;
lastmod?: Date;
mime: string;
length: number;
extension: string;
}

declare class nextdav implements Nextdav {
private url;
private options?;
private basicAuth?;
constructor(
url: string,
username?: string,
password?: string,
options?: Options,
);
/**
* Create WebDav client
*/
private getClient;
/**
* Retrive contents of the provided folder
*/
getFolderContents(path?: string): Promise<[Collection[], File[]] | boolean>;
/**
* Download file as buffer
*/
getFileAsBuffer(path: string): Promise<Buffer | false>;
private parseXml;
private buildContentsObject;
private url;
private options?;
private basicAuth?;
constructor(url: string, username?: string, password?: string, options?: Options);
/**
* Create WebDav client
*/
private getClient;
/**
* Retrive contents of the provided folder
*/
getFolderContents(path?: string): Promise<[Collection[], File[]] | boolean>;
/**
* Download file as buffer
*/
getFileAsBuffer(path: string): Promise<Buffer | false>;
private parseXml;
private buildContentsObject;
}

export {
type Collection,
type File,
type Nextdav,
type Options,
type Proxy,
nextdav as default,
};
export { type Collection, type File, type Nextdav, type Options, type Proxy, nextdav as default };
Loading
Loading