@jsenv/filesystem 3.2.2 → 4.0.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.
Files changed (55) hide show
  1. package/README.md +1 -16
  2. package/main.js +0 -28
  3. package/package.json +14 -35
  4. package/src/assertAndNormalizeDirectoryUrl.js +6 -4
  5. package/src/assertAndNormalizeFileUrl.js +1 -2
  6. package/src/assertDirectoryPresence.js +2 -2
  7. package/src/assertFilePresence.js +2 -2
  8. package/src/collectDirectoryMatchReport.js +10 -17
  9. package/src/collectFiles.js +7 -15
  10. package/src/copyDirectoryContent.js +1 -2
  11. package/src/copyEntry.js +9 -7
  12. package/src/ensureEmptyDirectory.js +2 -2
  13. package/src/ensureParentDirectories.js +1 -1
  14. package/src/ensureWindowsDriveLetter.js +1 -1
  15. package/src/getRealFileSystemUrlSync.js +1 -3
  16. package/src/listFilesMatching.js +1 -5
  17. package/src/moveDirectoryContent.js +1 -2
  18. package/src/moveEntry.js +1 -2
  19. package/src/readDirectory.js +2 -2
  20. package/src/readEntryPermissions.js +1 -1
  21. package/src/readEntryStat.js +1 -1
  22. package/src/readFile.js +9 -3
  23. package/src/readSymbolicLink.js +3 -7
  24. package/src/registerDirectoryLifecycle.js +11 -18
  25. package/src/registerFileLifecycle.js +2 -2
  26. package/src/removeEntry.js +6 -4
  27. package/src/testEntryPermissions.js +1 -3
  28. package/src/writeDirectory.js +2 -2
  29. package/src/writeEntryModificationTime.js +1 -3
  30. package/src/writeEntryPermissions.js +1 -7
  31. package/src/writeFile.js +15 -6
  32. package/src/writeSymbolicLink.js +6 -4
  33. package/LICENSE +0 -21
  34. package/src/fileSystemPathToUrl.js +0 -9
  35. package/src/internal/ensureUrlTrailingSlash.js +0 -3
  36. package/src/internal/getCommonPathname.js +0 -42
  37. package/src/internal/getCommonPathname.test.js +0 -62
  38. package/src/internal/pathnameToParentPathname.js +0 -8
  39. package/src/internal/replaceBackSlashesWithSlashes.js +0 -2
  40. package/src/isFileSystemPath.js +0 -23
  41. package/src/memoize.js +0 -24
  42. package/src/moveUrl.js +0 -10
  43. package/src/resolveDirectoryUrl.js +0 -7
  44. package/src/resolveUrl.js +0 -7
  45. package/src/urlIsInsideOf.js +0 -17
  46. package/src/urlToBasename.js +0 -9
  47. package/src/urlToExtension.js +0 -19
  48. package/src/urlToFileSystemPath.js +0 -12
  49. package/src/urlToFilename.js +0 -14
  50. package/src/urlToOrigin.js +0 -7
  51. package/src/urlToParentUrl.js +0 -31
  52. package/src/urlToPathname.js +0 -19
  53. package/src/urlToRelativeUrl.js +0 -49
  54. package/src/urlToRessource.js +0 -21
  55. package/src/urlToScheme.js +0 -10
@@ -1,12 +1,14 @@
1
1
  import { unlink, rmdir, openSync, closeSync } from "node:fs"
2
2
  import { Abort } from "@jsenv/abort"
3
+ import {
4
+ ensurePathnameTrailingSlash,
5
+ urlToFileSystemPath,
6
+ resolveUrl,
7
+ } from "@jsenv/urls"
3
8
 
4
- import { ensureUrlTrailingSlash } from "./internal/ensureUrlTrailingSlash.js"
5
9
  import { assertAndNormalizeFileUrl } from "./assertAndNormalizeFileUrl.js"
6
- import { urlToFileSystemPath } from "./urlToFileSystemPath.js"
7
10
  import { readEntryStat } from "./readEntryStat.js"
8
11
  import { readDirectory } from "./readDirectory.js"
9
- import { resolveUrl } from "./resolveUrl.js"
10
12
 
11
13
  export const removeEntry = async (
12
14
  source,
@@ -54,7 +56,7 @@ export const removeEntry = async (
54
56
  },
55
57
  )
56
58
  } else if (sourceStats.isDirectory()) {
57
- await removeDirectory(ensureUrlTrailingSlash(sourceUrl), {
59
+ await removeDirectory(ensurePathnameTrailingSlash(sourceUrl), {
58
60
  signal: removeOperation.signal,
59
61
  recursive,
60
62
  maxRetries,
@@ -1,7 +1,6 @@
1
1
  import { promises, constants } from "node:fs"
2
2
 
3
3
  import { assertAndNormalizeFileUrl } from "./assertAndNormalizeFileUrl.js"
4
- import { urlToFileSystemPath } from "./urlToFileSystemPath.js"
5
4
 
6
5
  const { access } = promises
7
6
  const {
@@ -21,7 +20,6 @@ export const testEntryPermissions = async (
21
20
  } = {},
22
21
  ) => {
23
22
  const sourceUrl = assertAndNormalizeFileUrl(source)
24
- const sourcePath = urlToFileSystemPath(sourceUrl)
25
23
  let binaryFlags = 0
26
24
 
27
25
  // if (visible) binaryFlags |= F_OK
@@ -30,7 +28,7 @@ export const testEntryPermissions = async (
30
28
  if (execute) binaryFlags |= X_OK
31
29
 
32
30
  try {
33
- await access(sourcePath, binaryFlags)
31
+ await access(new URL(sourceUrl), binaryFlags)
34
32
  return true
35
33
  } catch (error) {
36
34
  if (error.code === "ENOENT") {
@@ -1,8 +1,8 @@
1
1
  import { promises } from "node:fs"
2
+ import { urlToFileSystemPath } from "@jsenv/urls"
2
3
 
3
- import { statsToType } from "./internal/statsToType.js"
4
4
  import { assertAndNormalizeDirectoryUrl } from "./assertAndNormalizeDirectoryUrl.js"
5
- import { urlToFileSystemPath } from "./urlToFileSystemPath.js"
5
+ import { statsToType } from "./internal/statsToType.js"
6
6
  import { readEntryStat } from "./readEntryStat.js"
7
7
 
8
8
  // https://nodejs.org/dist/latest-v13.x/docs/api/fs.html#fs_fspromises_mkdir_path_options
@@ -1,11 +1,9 @@
1
1
  import { utimes } from "node:fs"
2
2
 
3
3
  import { assertAndNormalizeFileUrl } from "./assertAndNormalizeFileUrl.js"
4
- import { urlToFileSystemPath } from "./urlToFileSystemPath.js"
5
4
 
6
5
  export const writeEntryModificationTime = (source, mtime) => {
7
6
  const sourceUrl = assertAndNormalizeFileUrl(source)
8
- const sourcePath = urlToFileSystemPath(sourceUrl)
9
7
  const mtimeValue =
10
8
  typeof mtime === "number" ? new Date(Math.floor(mtime)) : mtime
11
9
  // reading atime mutates its value so there is no use case I can think of
@@ -13,7 +11,7 @@ export const writeEntryModificationTime = (source, mtime) => {
13
11
  const atimeValue = mtimeValue
14
12
 
15
13
  return new Promise((resolve, reject) => {
16
- utimes(sourcePath, atimeValue, mtimeValue, (error) => {
14
+ utimes(new URL(sourceUrl), atimeValue, mtimeValue, (error) => {
17
15
  if (error) {
18
16
  reject(error)
19
17
  } else {
@@ -2,11 +2,9 @@ import { chmod } from "node:fs"
2
2
 
3
3
  import { permissionsToBinaryFlags } from "./internal/permissions.js"
4
4
  import { assertAndNormalizeFileUrl } from "./assertAndNormalizeFileUrl.js"
5
- import { urlToFileSystemPath } from "./urlToFileSystemPath.js"
6
5
 
7
6
  export const writeEntryPermissions = async (source, permissions) => {
8
7
  const sourceUrl = assertAndNormalizeFileUrl(source)
9
- const sourcePath = urlToFileSystemPath(sourceUrl)
10
8
 
11
9
  let binaryFlags
12
10
  if (typeof permissions === "object") {
@@ -36,12 +34,8 @@ export const writeEntryPermissions = async (source, permissions) => {
36
34
  binaryFlags = permissions
37
35
  }
38
36
 
39
- return chmodNaive(sourcePath, binaryFlags)
40
- }
41
-
42
- const chmodNaive = (fileSystemPath, binaryFlags) => {
43
37
  return new Promise((resolve, reject) => {
44
- chmod(fileSystemPath, binaryFlags, (error) => {
38
+ chmod(new URL(sourceUrl), binaryFlags, (error) => {
45
39
  if (error) {
46
40
  reject(error)
47
41
  } else {
package/src/writeFile.js CHANGED
@@ -1,22 +1,31 @@
1
- import { promises } from "node:fs"
1
+ import { writeFile as writeFileNode } from "node:fs"
2
2
 
3
3
  import { assertAndNormalizeFileUrl } from "./assertAndNormalizeFileUrl.js"
4
4
  import { ensureParentDirectories } from "./ensureParentDirectories.js"
5
5
 
6
- // https://nodejs.org/dist/latest-v13.x/docs/api/fs.html#fs_fspromises_writefile_file_data_options
7
- const { writeFile: writeFileNode } = promises
8
-
9
6
  export const writeFile = async (destination, content = "") => {
10
7
  const destinationUrl = assertAndNormalizeFileUrl(destination)
11
8
  const destinationUrlObject = new URL(destinationUrl)
12
9
  try {
13
- await writeFileNode(destinationUrlObject, content)
10
+ await writeFileNaive(destinationUrlObject, content)
14
11
  } catch (error) {
15
12
  if (error.code === "ENOENT") {
16
13
  await ensureParentDirectories(destinationUrl)
17
- await writeFileNode(destinationUrlObject, content)
14
+ await writeFileNaive(destinationUrlObject, content)
18
15
  return
19
16
  }
20
17
  throw error
21
18
  }
22
19
  }
20
+
21
+ const writeFileNaive = (urlObject, content) => {
22
+ return new Promise((resolve, reject) => {
23
+ writeFileNode(urlObject, content, (error) => {
24
+ if (error) {
25
+ reject(error)
26
+ } else {
27
+ resolve()
28
+ }
29
+ })
30
+ })
31
+ }
@@ -1,10 +1,12 @@
1
1
  import { promises } from "node:fs"
2
+ import {
3
+ fileSystemPathToUrl,
4
+ urlToFileSystemPath,
5
+ resolveUrl,
6
+ isFileSystemPath,
7
+ } from "@jsenv/urls"
2
8
 
3
9
  import { assertAndNormalizeFileUrl } from "./assertAndNormalizeFileUrl.js"
4
- import { fileSystemPathToUrl } from "./fileSystemPathToUrl.js"
5
- import { urlToFileSystemPath } from "./urlToFileSystemPath.js"
6
- import { resolveUrl } from "./resolveUrl.js"
7
- import { isFileSystemPath } from "./isFileSystemPath.js"
8
10
  import { ensureParentDirectories } from "./ensureParentDirectories.js"
9
11
  import { readEntryStat } from "./readEntryStat.js"
10
12
  import { readSymbolicLink } from "./readSymbolicLink.js"
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2019 jsenv
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,9 +0,0 @@
1
- import { pathToFileURL } from "url"
2
- import { isFileSystemPath } from "./isFileSystemPath.js"
3
-
4
- export const fileSystemPathToUrl = (value) => {
5
- if (!isFileSystemPath(value)) {
6
- throw new Error(`received an invalid value for fileSystemPath: ${value}`)
7
- }
8
- return String(pathToFileURL(value))
9
- }
@@ -1,3 +0,0 @@
1
- export const ensureUrlTrailingSlash = (url) => {
2
- return url.endsWith("/") ? url : `${url}/`
3
- }
@@ -1,42 +0,0 @@
1
- export const getCommonPathname = (pathname, otherPathname) => {
2
- const firstDifferentCharacterIndex = findFirstDifferentCharacterIndex(
3
- pathname,
4
- otherPathname,
5
- )
6
-
7
- // pathname and otherpathname are exactly the same
8
- if (firstDifferentCharacterIndex === -1) {
9
- return pathname
10
- }
11
-
12
- const commonString = pathname.slice(0, firstDifferentCharacterIndex + 1)
13
- // the first different char is at firstDifferentCharacterIndex
14
- if (pathname.charAt(firstDifferentCharacterIndex) === "/") {
15
- return commonString
16
- }
17
-
18
- if (otherPathname.charAt(firstDifferentCharacterIndex) === "/") {
19
- return commonString
20
- }
21
-
22
- const firstDifferentSlashIndex = commonString.lastIndexOf("/")
23
- return pathname.slice(0, firstDifferentSlashIndex + 1)
24
- }
25
-
26
- const findFirstDifferentCharacterIndex = (string, otherString) => {
27
- const maxCommonLength = Math.min(string.length, otherString.length)
28
- let i = 0
29
- while (i < maxCommonLength) {
30
- const char = string.charAt(i)
31
- const otherChar = otherString.charAt(i)
32
- if (char !== otherChar) {
33
- return i
34
- }
35
- i++
36
- }
37
- if (string.length === otherString.length) {
38
- return -1
39
- }
40
- // they differ at maxCommonLength
41
- return maxCommonLength
42
- }
@@ -1,62 +0,0 @@
1
- import { assert } from "@jsenv/assert"
2
- import { getCommonPathname } from "./getCommonPathname.js"
3
-
4
- {
5
- const actual = getCommonPathname("/a/whatever.js", "/a/whatever.js")
6
- const expected = "/a/whatever.js"
7
- assert({ actual, expected })
8
- }
9
-
10
- {
11
- const actual = getCommonPathname("/a/whatever.js", "/a/")
12
- const expected = "/a/"
13
- assert({ actual, expected })
14
- }
15
-
16
- {
17
- const actual = getCommonPathname("/a/whatever.js", "/a/base.js")
18
- const expected = "/a/"
19
- assert({ actual, expected })
20
- }
21
-
22
- {
23
- const actual = getCommonPathname("/a/whatever.js", "/b/base.js")
24
- const expected = "/"
25
- assert({ actual, expected })
26
- }
27
-
28
- {
29
- const actual = getCommonPathname("/a/b/whatever.js", "/a/b/base.js")
30
- const expected = "/a/b/"
31
- assert({ actual, expected })
32
- }
33
-
34
- {
35
- const actual = getCommonPathname("/a/b/c/d/whatever.js", "/a/b/base.js")
36
- const expected = "/a/b/"
37
- assert({ actual, expected })
38
- }
39
-
40
- {
41
- const actual = getCommonPathname("/a/", "/a/base.js")
42
- const expected = "/a/"
43
- assert({ actual, expected })
44
- }
45
-
46
- {
47
- const actual = getCommonPathname("/", "/")
48
- const expected = "/"
49
- assert({ actual, expected })
50
- }
51
-
52
- {
53
- const actual = getCommonPathname("/whatever.js", "/")
54
- const expected = "/"
55
- assert({ actual, expected })
56
- }
57
-
58
- {
59
- const actual = getCommonPathname("/", "/base.js")
60
- const expected = "/"
61
- assert({ actual, expected })
62
- }
@@ -1,8 +0,0 @@
1
- export const pathnameToParentPathname = (pathname) => {
2
- const slashLastIndex = pathname.lastIndexOf("/")
3
- if (slashLastIndex === -1) {
4
- return "/"
5
- }
6
-
7
- return pathname.slice(0, slashLastIndex + 1)
8
- }
@@ -1,2 +0,0 @@
1
- export const replaceBackSlashesWithSlashes = (string) =>
2
- string.replace(/\\/g, "/")
@@ -1,23 +0,0 @@
1
- export const isFileSystemPath = (value) => {
2
- if (typeof value !== "string") {
3
- throw new TypeError(
4
- `isFileSystemPath first arg must be a string, got ${value}`,
5
- )
6
- }
7
-
8
- if (value[0] === "/") {
9
- return true
10
- }
11
-
12
- return startsWithWindowsDriveLetter(value)
13
- }
14
-
15
- const startsWithWindowsDriveLetter = (string) => {
16
- const firstChar = string[0]
17
- if (!/[a-zA-Z]/.test(firstChar)) return false
18
-
19
- const secondChar = string[1]
20
- if (secondChar !== ":") return false
21
-
22
- return true
23
- }
package/src/memoize.js DELETED
@@ -1,24 +0,0 @@
1
- export const memoize = (compute) => {
2
- let memoized = false
3
- let memoizedValue
4
-
5
- const fnWithMemoization = (...args) => {
6
- if (memoized) {
7
- return memoizedValue
8
- }
9
- // if compute is recursive wait for it to be fully done before storing the value
10
- // so set memoized boolean after the call
11
- memoizedValue = compute(...args)
12
- memoized = true
13
- return memoizedValue
14
- }
15
-
16
- fnWithMemoization.forget = () => {
17
- const value = memoizedValue
18
- memoized = false
19
- memoizedValue = undefined
20
- return value
21
- }
22
-
23
- return fnWithMemoization
24
- }
package/src/moveUrl.js DELETED
@@ -1,10 +0,0 @@
1
- import { urlToRelativeUrl } from "./urlToRelativeUrl.js"
2
-
3
- export const moveUrl = ({ url, from, to, preferAbsolute = false }) => {
4
- const relativeUrl = urlToRelativeUrl(url, from)
5
- const absoluteUrl = new URL(relativeUrl, to).href
6
- if (preferAbsolute) {
7
- return absoluteUrl
8
- }
9
- return urlToRelativeUrl(absoluteUrl, to)
10
- }
@@ -1,7 +0,0 @@
1
- import { ensureUrlTrailingSlash } from "./internal/ensureUrlTrailingSlash.js"
2
- import { resolveUrl } from "./resolveUrl.js"
3
-
4
- export const resolveDirectoryUrl = (specifier, baseUrl) => {
5
- const url = resolveUrl(specifier, baseUrl)
6
- return ensureUrlTrailingSlash(url)
7
- }
package/src/resolveUrl.js DELETED
@@ -1,7 +0,0 @@
1
- export const resolveUrl = (specifier, baseUrl) => {
2
- if (typeof baseUrl === "undefined") {
3
- throw new TypeError(`baseUrl missing to resolve ${specifier}`)
4
- }
5
-
6
- return String(new URL(specifier, baseUrl))
7
- }
@@ -1,17 +0,0 @@
1
- export const urlIsInsideOf = (url, otherUrl) => {
2
- const urlObject = new URL(url)
3
- const otherUrlObject = new URL(otherUrl)
4
-
5
- if (urlObject.origin !== otherUrlObject.origin) {
6
- return false
7
- }
8
-
9
- const urlPathname = urlObject.pathname
10
- const otherUrlPathname = otherUrlObject.pathname
11
- if (urlPathname === otherUrlPathname) {
12
- return false
13
- }
14
-
15
- const isInside = urlPathname.startsWith(otherUrlPathname)
16
- return isInside
17
- }
@@ -1,9 +0,0 @@
1
- import { urlToFilename } from "./urlToFilename.js"
2
-
3
- export const urlToBasename = (url) => {
4
- const filename = urlToFilename(url)
5
- const dotLastIndex = filename.lastIndexOf(".")
6
- const basename =
7
- dotLastIndex === -1 ? filename : filename.slice(0, dotLastIndex)
8
- return basename
9
- }
@@ -1,19 +0,0 @@
1
- import { urlToPathname } from "./urlToPathname.js"
2
-
3
- export const urlToExtension = (url) => {
4
- const pathname = urlToPathname(url)
5
- return pathnameToExtension(pathname)
6
- }
7
-
8
- const pathnameToExtension = (pathname) => {
9
- const slashLastIndex = pathname.lastIndexOf("/")
10
- if (slashLastIndex !== -1) {
11
- pathname = pathname.slice(slashLastIndex + 1)
12
- }
13
-
14
- const dotLastIndex = pathname.lastIndexOf(".")
15
- if (dotLastIndex === -1) return ""
16
- // if (dotLastIndex === pathname.length - 1) return ""
17
- const extension = pathname.slice(dotLastIndex)
18
- return extension
19
- }
@@ -1,12 +0,0 @@
1
- import { fileURLToPath } from "url"
2
-
3
- export const urlToFileSystemPath = (url) => {
4
- let urlString = String(url)
5
- if (urlString[urlString.length - 1] === "/") {
6
- // remove trailing / so that nodejs path becomes predictable otherwise it logs
7
- // the trailing slash on linux but does not on windows
8
- urlString = urlString.slice(0, -1)
9
- }
10
- const fileSystemPath = fileURLToPath(urlString)
11
- return fileSystemPath
12
- }
@@ -1,14 +0,0 @@
1
- import { urlToPathname } from "./urlToPathname.js"
2
-
3
- export const urlToFilename = (url) => {
4
- const pathname = urlToPathname(url)
5
- const pathnameBeforeLastSlash = pathname.endsWith("/")
6
- ? pathname.slice(0, -1)
7
- : pathname
8
- const slashLastIndex = pathnameBeforeLastSlash.lastIndexOf("/")
9
- const filename =
10
- slashLastIndex === -1
11
- ? pathnameBeforeLastSlash
12
- : pathnameBeforeLastSlash.slice(slashLastIndex + 1)
13
- return filename
14
- }
@@ -1,7 +0,0 @@
1
- export const urlToOrigin = (url) => {
2
- const urlString = String(url)
3
- if (urlString.startsWith("file://")) {
4
- return `file://`
5
- }
6
- return new URL(urlString).origin
7
- }
@@ -1,31 +0,0 @@
1
- import { urlToOrigin } from "./urlToOrigin.js"
2
- import { urlToRessource } from "./urlToRessource.js"
3
-
4
- export const urlToParentUrl = (url) => {
5
- const ressource = urlToRessource(url)
6
- const slashLastIndex = ressource.lastIndexOf("/")
7
- if (slashLastIndex === -1) {
8
- const urlAsString = String(url)
9
- return urlAsString
10
- }
11
-
12
- const lastCharacterIndex = ressource.length - 1
13
- if (slashLastIndex === lastCharacterIndex) {
14
- const slashPreviousIndex = ressource.lastIndexOf(
15
- "/",
16
- lastCharacterIndex - 1,
17
- )
18
- if (slashPreviousIndex === -1) {
19
- const urlAsString = String(url)
20
- return urlAsString
21
- }
22
-
23
- const origin = urlToOrigin(url)
24
- const parentUrl = `${origin}${ressource.slice(0, slashPreviousIndex + 1)}`
25
- return parentUrl
26
- }
27
-
28
- const origin = urlToOrigin(url)
29
- const parentUrl = `${origin}${ressource.slice(0, slashLastIndex + 1)}`
30
- return parentUrl
31
- }
@@ -1,19 +0,0 @@
1
- import { urlToRessource } from "./urlToRessource.js"
2
-
3
- export const urlToPathname = (url) => {
4
- const ressource = urlToRessource(url)
5
- const pathname = ressourceToPathname(ressource)
6
- return pathname
7
- }
8
-
9
- const ressourceToPathname = (ressource) => {
10
- const searchSeparatorIndex = ressource.indexOf("?")
11
- if (searchSeparatorIndex > -1) {
12
- return ressource.slice(0, searchSeparatorIndex)
13
- }
14
- const hashIndex = ressource.indexOf("#")
15
- if (hashIndex > -1) {
16
- return ressource.slice(0, hashIndex)
17
- }
18
- return ressource
19
- }
@@ -1,49 +0,0 @@
1
- import { getCommonPathname } from "./internal/getCommonPathname.js"
2
- import { pathnameToParentPathname } from "./internal/pathnameToParentPathname.js"
3
-
4
- export const urlToRelativeUrl = (url, baseUrl) => {
5
- const urlObject = new URL(url)
6
- const baseUrlObject = new URL(baseUrl)
7
-
8
- if (urlObject.protocol !== baseUrlObject.protocol) {
9
- const urlAsString = String(url)
10
- return urlAsString
11
- }
12
-
13
- if (
14
- urlObject.username !== baseUrlObject.username ||
15
- urlObject.password !== baseUrlObject.password ||
16
- urlObject.host !== baseUrlObject.host
17
- ) {
18
- const afterUrlScheme = String(url).slice(urlObject.protocol.length)
19
- return afterUrlScheme
20
- }
21
-
22
- const { pathname, hash, search } = urlObject
23
- if (pathname === "/") {
24
- const baseUrlRessourceWithoutLeadingSlash = baseUrlObject.pathname.slice(1)
25
- return baseUrlRessourceWithoutLeadingSlash
26
- }
27
-
28
- const basePathname = baseUrlObject.pathname
29
- const commonPathname = getCommonPathname(pathname, basePathname)
30
- if (!commonPathname) {
31
- const urlAsString = String(url)
32
- return urlAsString
33
- }
34
- const specificPathname = pathname.slice(commonPathname.length)
35
- const baseSpecificPathname = basePathname.slice(commonPathname.length)
36
- if (baseSpecificPathname.includes("/")) {
37
- const baseSpecificParentPathname =
38
- pathnameToParentPathname(baseSpecificPathname)
39
- const relativeDirectoriesNotation = baseSpecificParentPathname.replace(
40
- /.*?\//g,
41
- "../",
42
- )
43
- const relativeUrl = `${relativeDirectoriesNotation}${specificPathname}${search}${hash}`
44
- return relativeUrl
45
- }
46
-
47
- const relativeUrl = `${specificPathname}${search}${hash}`
48
- return relativeUrl
49
- }
@@ -1,21 +0,0 @@
1
- import { urlToScheme } from "./urlToScheme.js"
2
-
3
- export const urlToRessource = (url) => {
4
- const scheme = urlToScheme(url)
5
-
6
- if (scheme === "file") {
7
- const urlAsStringWithoutFileProtocol = String(url).slice("file://".length)
8
- return urlAsStringWithoutFileProtocol
9
- }
10
-
11
- if (scheme === "https" || scheme === "http") {
12
- // remove origin
13
- const afterProtocol = String(url).slice(scheme.length + "://".length)
14
- const pathnameSlashIndex = afterProtocol.indexOf("/", "://".length)
15
- const urlAsStringWithoutOrigin = afterProtocol.slice(pathnameSlashIndex)
16
- return urlAsStringWithoutOrigin
17
- }
18
-
19
- const urlAsStringWithoutProtocol = String(url).slice(scheme.length + 1)
20
- return urlAsStringWithoutProtocol
21
- }
@@ -1,10 +0,0 @@
1
- export const urlToScheme = (url) => {
2
- const urlString = String(url)
3
- const colonIndex = urlString.indexOf(":")
4
- if (colonIndex === -1) {
5
- return ""
6
- }
7
-
8
- const scheme = urlString.slice(0, colonIndex)
9
- return scheme
10
- }