Socket
Socket
Sign inDemoInstall

parse-path

Package Overview
Dependencies
Maintainers
2
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parse-path - npm Package Compare versions

Comparing version 4.0.4 to 5.0.0

144

lib/index.js
"use strict";
// Dependencies
var protocols = require("protocols"),
isSsh = require("is-ssh"),
qs = require("query-string");
var protocols = require("protocols");

@@ -17,17 +14,18 @@ /**

*
* - `protocols` (Array): An array with the url protocols (usually it has one element).
* - `protocol` (String): The first protocol, `"ssh"` (if the url is a ssh url) or `"file"`.
* - `port` (null|Number): The domain port.
* - `resource` (String): The url domain (including subdomains).
* - `user` (String): The authentication user (usually for ssh urls).
* - `pathname` (String): The url pathname.
* - `hash` (String): The url hash.
* - `search` (String): The url querystring value.
* - `href` (String): The input url.
* - `query` (Object): The url querystring, parsed as object.
* - `protocols` (Array): An array with the url protocols (usually it has one element).
* - `protocol` (String): The first protocol or `"file"`.
* - `port` (String): The domain port (default: `""`).
* - `resource` (String): The url domain (including subdomain and port).
* - `user` (String): The authentication user (default: `""`).
* - `password` (String): The authentication password (default: `""`).
* - `pathname` (String): The url pathname.
* - `hash` (String): The url hash.
* - `search` (String): The url querystring value (excluding `?`).
* - `href` (String): The normalized input url.
* - `query` (Object): The url querystring, parsed as object.
*/
function parsePath(url) {
url = (url || "").trim().replace(/\r?\n|\r/gm, "");
var output = {
protocols: protocols(url),
protocols: [],
protocol: null,

@@ -37,2 +35,3 @@ port: null,

user: "",
password: "",
pathname: "",

@@ -42,93 +41,34 @@ hash: "",

href: url,
query: Object.create(null)
},
protocolIndex = url.indexOf("://"),
resourceIndex = -1,
splits = null,
parts = null;
query: {}
};
if (url.startsWith(".")) {
if (url.startsWith("./")) {
url = url.substring(2);
}
output.pathname = url;
output.protocol = "file";
}
var firstChar = url.charAt(1);
if (!output.protocol) {
try {
var parsed = new URL(url);
output.protocols = protocols(parsed);
output.protocol = output.protocols[0];
if (!output.protocol) {
if (isSsh(url)) {
output.protocol = "ssh";
} else if (firstChar === "/" || firstChar === "~") {
url = url.substring(2);
output.protocol = "file";
} else {
output.protocol = "file";
}
}
}
if (protocolIndex !== -1) {
url = url.substring(protocolIndex + 3);
}
parts = url.split(/\/|\\/);
if (output.protocol !== "file") {
output.resource = parts.shift();
} else {
output.port = parsed.port;
output.resource = parsed.host;
output.user = parsed.username || "";
output.password = parsed.password || "";
output.pathname = parsed.pathname;
output.hash = parsed.hash.slice(1);
output.search = parsed.search.slice(1);
output.href = parsed.href;
output.query = Object.fromEntries(parsed.searchParams);
} catch (e) {
// TODO Maybe check if it is a valid local file path
// In any case, these will be parsed by higher
// level parsers such as parse-url, git-url-parse, git-up
output.protocols = ["file"];
output.protocol = output.protocols[0];
output.port = "";
output.resource = "";
output.user = "";
output.pathname = "";
output.hash = "";
output.search = "";
output.href = url;
output.query = {};
}
// user@domain
splits = output.resource.split("@");
if (splits.length === 2) {
output.user = splits[0];
output.resource = splits[1];
}
// domain.com:port
splits = output.resource.split(":");
if (splits.length === 2) {
output.resource = splits[0];
var port = splits[1];
if (port) {
output.port = Number(port);
if (isNaN(output.port) || port.match(/^\d+$/) === null) {
output.port = null;
parts.unshift(port);
}
} else {
output.port = null;
}
}
// Remove empty elements
parts = parts.filter(Boolean);
// Stringify the pathname
if (output.protocol === "file") {
output.pathname = output.href;
} else {
output.pathname = output.pathname || (output.protocol !== "file" || output.href[0] === "/" ? "/" : "") + parts.join("/");
}
// #some-hash
splits = output.pathname.split("#");
if (splits.length === 2) {
output.pathname = splits[0];
output.hash = splits[1];
}
// ?foo=bar
splits = output.pathname.split("?");
if (splits.length === 2) {
output.pathname = splits[0];
output.search = splits[1];
}
output.query = qs.parse(output.search);
output.href = output.href.replace(/\/$/, "");
output.pathname = output.pathname.replace(/\/$/, "");
return output;

@@ -135,0 +75,0 @@ }

{
"name": "parse-path",
"version": "4.0.4",
"version": "5.0.0",
"description": "Parse paths (local paths, urls: ssh/git/etc)",

@@ -32,9 +32,6 @@ "main": "lib/index.js",

"devDependencies": {
"tester": "^1.3.1"
"tester": "^1.4.5"
},
"dependencies": {
"is-ssh": "^1.3.0",
"protocols": "^1.4.0",
"qs": "^6.9.4",
"query-string": "^6.13.8"
"protocols": "^2.0.0"
},

@@ -41,0 +38,0 @@ "files": [

@@ -81,44 +81,45 @@ <!-- Please do not edit this file. Edit the `blah` field in the `package.json` instead. If in doubt, open an issue. -->

console.log(parsePath("http://ionicabizau.net/blog"))
// { protocols: [ 'http' ],
// {
// protocols: [ 'http' ],
// protocol: 'http',
// port: null,
// port: '',
// resource: 'ionicabizau.net',
// user: '',
// password: '',
// pathname: '/blog',
// hash: '',
// search: '',
// href: 'http://ionicabizau.net/blog' }
// href: 'http://ionicabizau.net/blog',
// query: {}
// }
console.log(parsePath("http://domain.com/path/name?foo=bar&bar=42#some-hash"))
// { protocols: [ 'http' ],
// {
// protocols: [ 'http' ],
// protocol: 'http',
// port: null,
// port: '',
// resource: 'domain.com',
// user: '',
// password: '',
// pathname: '/path/name',
// hash: 'some-hash',
// search: 'foo=bar&bar=42',
// href: 'http://domain.com/path/name?foo=bar&bar=42#some-hash' }
// href: 'http://domain.com/path/name?foo=bar&bar=42#some-hash',
// query: { foo: 'bar', bar: '42' }
// }
console.log(parsePath("git+ssh://git@host.xz/path/name.git"))
// { protocols: [ 'git', 'ssh' ],
// {
// protocols: [ 'git', 'ssh' ],
// protocol: 'git',
// port: null,
// port: '',
// resource: 'host.xz',
// user: 'git',
// password: '',
// pathname: '/path/name.git',
// hash: '',
// search: '',
// href: 'git+ssh://git@host.xz/path/name.git' }
console.log(parsePath("git@github.com:IonicaBizau/git-stats.git"))
// { protocols: [],
// protocol: 'ssh',
// port: null,
// resource: 'github.com',
// user: 'git',
// pathname: '/IonicaBizau/git-stats.git',
// hash: '',
// search: '',
// href: 'git@github.com:IonicaBizau/git-stats.git' }
// href: 'git+ssh://git@host.xz/path/name.git',
// query: {}
// }
```

@@ -162,12 +163,13 @@

- **Object** An object containing the following fields:
- `protocols` (Array): An array with the url protocols (usually it has one element).
- `protocol` (String): The first protocol, `"ssh"` (if the url is a ssh url) or `"file"`.
- `port` (null|Number): The domain port.
- `resource` (String): The url domain (including subdomains).
- `user` (String): The authentication user (usually for ssh urls).
- `pathname` (String): The url pathname.
- `hash` (String): The url hash.
- `search` (String): The url querystring value.
- `href` (String): The input url.
- `query` (Object): The url querystring, parsed as object.
- `protocols` (Array): An array with the url protocols (usually it has one element).
- `protocol` (String): The first protocol or `"file"`.
- `port` (String): The domain port (default: `""`).
- `resource` (String): The url domain (including subdomain and port).
- `user` (String): The authentication user (default: `""`).
- `password` (String): The authentication password (default: `""`).
- `pathname` (String): The url pathname.
- `hash` (String): The url hash.
- `search` (String): The url querystring value (excluding `?`).
- `href` (String): The normalized input url.
- `query` (Object): The url querystring, parsed as object.

@@ -233,18 +235,18 @@

- `@hemith/react-native-tnk`
- `native-kakao-login`
- `npm_one_1_2_3`
- `native-kakao-login`
- `rn-adyen-dropin`
- `react-fsm-router`
- `react-native-arunmeena1987`
- `react-native-biometric-authenticate`
- `react-native-contact-list`
- `react-native-biometric-authenticate`
- `react-native-arunmeena1987`
- `react-native-is7`
- `react-native-payu-payment-testing`
- `react-native-kakao-maps`
- `react-native-my-first-try-arun-ramya`
- `react-native-payu-payment-testing`
- `react-native-ytximkit`
- `rn-adyen-dropin`
- `tria-prima`
- `sm-versioning`
- `@positionex/position-sdk`
- `@corelmax/react-native-my2c2p-sdk`
- `@datalogic/react-native-datalogic-module`
- `@felipesimmi/react-native-datalogic-module`

@@ -256,13 +258,10 @@ - `@hawkingnetwork/react-native-tab-view`

- `npm_qwerty`
- `react-native-bubble-chart`
- `react-native-arunjeyam1987`
- `react-native-bubble-chart`
- `react-native-flyy`
- `@alphy11/semantic-release-gitlab`
- `@apardellass/react-native-audio-stream`
- `@fgreinacher/semantic-release-gitlab`
- `@geeky-apo/react-native-advanced-clipboard`
- `@j4s0n/semantic-release-gitlab`
- `@saad27/react-native-bottom-tab-tour`
- `@xudong/semantic-release-gitlab`
- `candlelabssdk`
- `@fgreinacher/semantic-release-gitlab`
- `@geeky-apo/react-native-advanced-clipboard`
- `react-native-dsphoto-module`

@@ -278,17 +277,18 @@ - `react-native-responsive-size`

- `semantic-release-gitlab-plugin`
- `@pvm/plugin-conventional-changelog`
- `@pvm/github`
- `@pvm/plugin-conventional-changelog`
- `@devdiary/semantic-devdiary-release`
- `birken-react-native-community-image-editor`
- `luojia-cli-dev`
- `@con-test/react-native-concent-common`
- `reac-native-arun-ramya-test`
- `react-native-arun-ramya-test`
- `react-native-arunramya151`
- `react-native-plugpag-wrapper`
- `react-native-pulsator-native`
- `react-native-arun-ramya-test`
- `react-native-arunramya151`
- `react-native-transtracker-library`
- `semantic-release-version`
- `@devdiary/semantic-devdiary-release`
- `luojia-cli-dev`
- `birken-react-native-community-image-editor`
- `@screeb/react-native`
- `@buganto/client`
- `@cloudoki/donderflow`
- `@buganto/client`
- `@tjoussen/semantic-release-gitlab-mr`

@@ -302,7 +302,8 @@ - `astra-ufo-sdk`

- `raact-native-arunramya151`
- `react-native-modal-progress-bar`
- `react-native-test-module-hhh`
- `reddit-title-has-verbatim-quote`
- `react-native-jsi-device-info`
- `react-native-badge-control`
- `rn-tm-notify`
- `react-native-jsi-device-info`

@@ -309,0 +310,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc