scrapfly-sdk 0.10.3 → 0.10.4

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 (92) hide show
  1. package/package.json +1 -1
  2. package/src/_dnt.shims.ts +64 -0
  3. package/src/batch.ts +272 -0
  4. package/src/browserconfig.ts +118 -0
  5. package/src/client.ts +1609 -0
  6. package/src/crawl.ts +251 -0
  7. package/src/crawlerconfig.ts +363 -0
  8. package/src/crawlerresult.ts +471 -0
  9. package/src/deps/jsr.io/@std/path/1.0.1/_common/assert_path.ts +10 -0
  10. package/src/deps/jsr.io/@std/path/1.0.1/_common/basename.ts +53 -0
  11. package/src/deps/jsr.io/@std/path/1.0.1/_common/common.ts +26 -0
  12. package/src/deps/jsr.io/@std/path/1.0.1/_common/constants.ts +49 -0
  13. package/src/deps/jsr.io/@std/path/1.0.1/_common/dirname.ts +9 -0
  14. package/src/deps/jsr.io/@std/path/1.0.1/_common/format.ts +25 -0
  15. package/src/deps/jsr.io/@std/path/1.0.1/_common/from_file_url.ts +10 -0
  16. package/src/deps/jsr.io/@std/path/1.0.1/_common/glob_to_reg_exp.ts +290 -0
  17. package/src/deps/jsr.io/@std/path/1.0.1/_common/normalize.ts +9 -0
  18. package/src/deps/jsr.io/@std/path/1.0.1/_common/normalize_string.ts +74 -0
  19. package/src/deps/jsr.io/@std/path/1.0.1/_common/relative.ts +10 -0
  20. package/src/deps/jsr.io/@std/path/1.0.1/_common/strip_trailing_separators.ts +25 -0
  21. package/src/deps/jsr.io/@std/path/1.0.1/_common/to_file_url.ts +17 -0
  22. package/src/deps/jsr.io/@std/path/1.0.1/_os.ts +31 -0
  23. package/src/deps/jsr.io/@std/path/1.0.1/basename.ts +35 -0
  24. package/src/deps/jsr.io/@std/path/1.0.1/common.ts +35 -0
  25. package/src/deps/jsr.io/@std/path/1.0.1/constants.ts +18 -0
  26. package/src/deps/jsr.io/@std/path/1.0.1/dirname.ts +28 -0
  27. package/src/deps/jsr.io/@std/path/1.0.1/extname.ts +27 -0
  28. package/src/deps/jsr.io/@std/path/1.0.1/format.ts +30 -0
  29. package/src/deps/jsr.io/@std/path/1.0.1/from_file_url.ts +30 -0
  30. package/src/deps/jsr.io/@std/path/1.0.1/glob_to_regexp.ts +94 -0
  31. package/src/deps/jsr.io/@std/path/1.0.1/is_absolute.ts +30 -0
  32. package/src/deps/jsr.io/@std/path/1.0.1/is_glob.ts +49 -0
  33. package/src/deps/jsr.io/@std/path/1.0.1/join.ts +28 -0
  34. package/src/deps/jsr.io/@std/path/1.0.1/join_globs.ts +42 -0
  35. package/src/deps/jsr.io/@std/path/1.0.1/mod.ts +55 -0
  36. package/src/deps/jsr.io/@std/path/1.0.1/normalize.ts +31 -0
  37. package/src/deps/jsr.io/@std/path/1.0.1/normalize_glob.ts +45 -0
  38. package/src/deps/jsr.io/@std/path/1.0.1/parse.ts +44 -0
  39. package/src/deps/jsr.io/@std/path/1.0.1/posix/_util.ts +10 -0
  40. package/src/deps/jsr.io/@std/path/1.0.1/posix/basename.ts +39 -0
  41. package/src/deps/jsr.io/@std/path/1.0.1/posix/constants.ts +15 -0
  42. package/src/deps/jsr.io/@std/path/1.0.1/posix/dirname.ts +55 -0
  43. package/src/deps/jsr.io/@std/path/1.0.1/posix/extname.ts +73 -0
  44. package/src/deps/jsr.io/@std/path/1.0.1/posix/format.ts +31 -0
  45. package/src/deps/jsr.io/@std/path/1.0.1/posix/from_file_url.ts +25 -0
  46. package/src/deps/jsr.io/@std/path/1.0.1/posix/glob_to_regexp.ts +94 -0
  47. package/src/deps/jsr.io/@std/path/1.0.1/posix/is_absolute.ts +25 -0
  48. package/src/deps/jsr.io/@std/path/1.0.1/posix/join.ts +27 -0
  49. package/src/deps/jsr.io/@std/path/1.0.1/posix/join_globs.ts +44 -0
  50. package/src/deps/jsr.io/@std/path/1.0.1/posix/normalize.ts +41 -0
  51. package/src/deps/jsr.io/@std/path/1.0.1/posix/normalize_glob.ts +42 -0
  52. package/src/deps/jsr.io/@std/path/1.0.1/posix/parse.ts +121 -0
  53. package/src/deps/jsr.io/@std/path/1.0.1/posix/relative.ts +103 -0
  54. package/src/deps/jsr.io/@std/path/1.0.1/posix/resolve.ts +69 -0
  55. package/src/deps/jsr.io/@std/path/1.0.1/posix/to_file_url.ts +32 -0
  56. package/src/deps/jsr.io/@std/path/1.0.1/posix/to_namespaced_path.ts +21 -0
  57. package/src/deps/jsr.io/@std/path/1.0.1/relative.ts +32 -0
  58. package/src/deps/jsr.io/@std/path/1.0.1/resolve.ts +32 -0
  59. package/src/deps/jsr.io/@std/path/1.0.1/to_file_url.ts +30 -0
  60. package/src/deps/jsr.io/@std/path/1.0.1/to_namespaced_path.ts +31 -0
  61. package/src/deps/jsr.io/@std/path/1.0.1/types.ts +40 -0
  62. package/src/deps/jsr.io/@std/path/1.0.1/windows/_util.ts +28 -0
  63. package/src/deps/jsr.io/@std/path/1.0.1/windows/basename.ts +48 -0
  64. package/src/deps/jsr.io/@std/path/1.0.1/windows/constants.ts +15 -0
  65. package/src/deps/jsr.io/@std/path/1.0.1/windows/dirname.ts +114 -0
  66. package/src/deps/jsr.io/@std/path/1.0.1/windows/extname.ts +86 -0
  67. package/src/deps/jsr.io/@std/path/1.0.1/windows/format.ts +31 -0
  68. package/src/deps/jsr.io/@std/path/1.0.1/windows/from_file_url.ts +34 -0
  69. package/src/deps/jsr.io/@std/path/1.0.1/windows/glob_to_regexp.ts +92 -0
  70. package/src/deps/jsr.io/@std/path/1.0.1/windows/is_absolute.ts +40 -0
  71. package/src/deps/jsr.io/@std/path/1.0.1/windows/join.ts +72 -0
  72. package/src/deps/jsr.io/@std/path/1.0.1/windows/join_globs.ts +45 -0
  73. package/src/deps/jsr.io/@std/path/1.0.1/windows/normalize.ts +132 -0
  74. package/src/deps/jsr.io/@std/path/1.0.1/windows/normalize_glob.ts +42 -0
  75. package/src/deps/jsr.io/@std/path/1.0.1/windows/parse.ts +184 -0
  76. package/src/deps/jsr.io/@std/path/1.0.1/windows/relative.ts +128 -0
  77. package/src/deps/jsr.io/@std/path/1.0.1/windows/resolve.ts +174 -0
  78. package/src/deps/jsr.io/@std/path/1.0.1/windows/to_file_url.ts +38 -0
  79. package/src/deps/jsr.io/@std/path/1.0.1/windows/to_namespaced_path.ts +60 -0
  80. package/src/deps.ts +4 -0
  81. package/src/errors.ts +55 -0
  82. package/src/extractionconfig.ts +175 -0
  83. package/src/logger.ts +51 -0
  84. package/src/main.ts +61 -0
  85. package/src/monitoringconfig.ts +50 -0
  86. package/src/polyfill.ts +37 -0
  87. package/src/result.ts +338 -0
  88. package/src/scrapeconfig.ts +448 -0
  89. package/src/screenshotconfig.ts +204 -0
  90. package/src/types.ts +2 -0
  91. package/src/utils.ts +86 -0
  92. package/src/webhookserver.ts +66 -0
@@ -0,0 +1,72 @@
1
+ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2
+ // This module is browser compatible.
3
+
4
+ import { assertPath } from "../_common/assert_path.js";
5
+ import { isPathSeparator } from "./_util.js";
6
+ import { normalize } from "./normalize.js";
7
+
8
+ /**
9
+ * Join all given a sequence of `paths`,then normalizes the resulting path.
10
+ *
11
+ * @example Usage
12
+ * ```ts
13
+ * import { join } from "@std/path/windows/join";
14
+ * import { assertEquals } from "@std/assert";
15
+ *
16
+ * const joined = join("C:\\foo", "bar", "baz\\..");
17
+ * assertEquals(joined, "C:\\foo\\bar");
18
+ * ```
19
+ *
20
+ * @param paths The paths to join.
21
+ * @returns The joined path.
22
+ */
23
+ export function join(...paths: string[]): string {
24
+ paths.forEach((path) => assertPath(path));
25
+ paths = paths.filter((path) => path.length > 0);
26
+ if (paths.length === 0) return ".";
27
+
28
+ // Make sure that the joined path doesn't start with two slashes, because
29
+ // normalize() will mistake it for an UNC path then.
30
+ //
31
+ // This step is skipped when it is very clear that the user actually
32
+ // intended to point at an UNC path. This is assumed when the first
33
+ // non-empty string arguments starts with exactly two slashes followed by
34
+ // at least one more non-slash character.
35
+ //
36
+ // Note that for normalize() to treat a path as an UNC path it needs to
37
+ // have at least 2 components, so we don't filter for that here.
38
+ // This means that the user can use join to construct UNC paths from
39
+ // a server name and a share name; for example:
40
+ // path.join('//server', 'share') -> '\\\\server\\share\\'
41
+ let needsReplace = true;
42
+ let slashCount = 0;
43
+ const firstPart = paths[0]!;
44
+ if (isPathSeparator(firstPart.charCodeAt(0))) {
45
+ ++slashCount;
46
+ const firstLen = firstPart.length;
47
+ if (firstLen > 1) {
48
+ if (isPathSeparator(firstPart.charCodeAt(1))) {
49
+ ++slashCount;
50
+ if (firstLen > 2) {
51
+ if (isPathSeparator(firstPart.charCodeAt(2))) ++slashCount;
52
+ else {
53
+ // We matched a UNC path in the first part
54
+ needsReplace = false;
55
+ }
56
+ }
57
+ }
58
+ }
59
+ }
60
+ let joined = paths.join("\\");
61
+ if (needsReplace) {
62
+ // Find any more consecutive slashes we need to replace
63
+ for (; slashCount < joined.length; ++slashCount) {
64
+ if (!isPathSeparator(joined.charCodeAt(slashCount))) break;
65
+ }
66
+
67
+ // Replace the slashes if needed
68
+ if (slashCount >= 2) joined = `\\${joined.slice(slashCount)}`;
69
+ }
70
+
71
+ return normalize(joined);
72
+ }
@@ -0,0 +1,45 @@
1
+ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2
+ // This module is browser compatible.
3
+
4
+ import type { GlobOptions } from "../_common/glob_to_reg_exp.js";
5
+ import { join } from "./join.js";
6
+ import { SEPARATOR } from "./constants.js";
7
+ import { normalizeGlob } from "./normalize_glob.js";
8
+
9
+ export type { GlobOptions };
10
+
11
+ /**
12
+ * Like join(), but doesn't collapse "**\/.." when `globstar` is true.
13
+ *
14
+ * @example Usage
15
+ *
16
+ * ```ts
17
+ * import { joinGlobs } from "@std/path/windows/join-globs";
18
+ * import { assertEquals } from "@std/assert";
19
+ *
20
+ * const joined = joinGlobs(["foo", "**", "bar"], { globstar: true });
21
+ * assertEquals(joined, "foo\\**\\bar");
22
+ * ```
23
+ *
24
+ * @param globs The globs to join.
25
+ * @param options The options for glob pattern.
26
+ * @returns The joined glob pattern.
27
+ */
28
+ export function joinGlobs(
29
+ globs: string[],
30
+ { extended = true, globstar = false }: GlobOptions = {},
31
+ ): string {
32
+ if (!globstar || globs.length === 0) {
33
+ return join(...globs);
34
+ }
35
+ let joined: string | undefined;
36
+ for (const glob of globs) {
37
+ const path = glob;
38
+ if (path.length > 0) {
39
+ if (!joined) joined = path;
40
+ else joined += `${SEPARATOR}${path}`;
41
+ }
42
+ }
43
+ if (!joined) return ".";
44
+ return normalizeGlob(joined, { extended, globstar });
45
+ }
@@ -0,0 +1,132 @@
1
+ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2
+ // This module is browser compatible.
3
+
4
+ import { assertArg } from "../_common/normalize.js";
5
+ import { CHAR_COLON } from "../_common/constants.js";
6
+ import { normalizeString } from "../_common/normalize_string.js";
7
+ import { isPathSeparator, isWindowsDeviceRoot } from "./_util.js";
8
+
9
+ /**
10
+ * Normalize the `path`, resolving `'..'` and `'.'` segments.
11
+ * Note that resolving these segments does not necessarily mean that all will be eliminated.
12
+ * A `'..'` at the top-level will be preserved, and an empty path is canonically `'.'`.
13
+ *
14
+ * @example Usage
15
+ * ```ts
16
+ * import { normalize } from "@std/path/windows/normalize";
17
+ * import { assertEquals } from "@std/assert";
18
+ *
19
+ * const normalized = normalize("C:\\foo\\..\\bar");
20
+ * assertEquals(normalized, "C:\\bar");
21
+ * ```
22
+ *
23
+ * @param path The path to normalize
24
+ * @returns The normalized path
25
+ */
26
+ export function normalize(path: string): string {
27
+ assertArg(path);
28
+
29
+ const len = path.length;
30
+ let rootEnd = 0;
31
+ let device: string | undefined;
32
+ let isAbsolute = false;
33
+ const code = path.charCodeAt(0);
34
+
35
+ // Try to match a root
36
+ if (len > 1) {
37
+ if (isPathSeparator(code)) {
38
+ // Possible UNC root
39
+
40
+ // If we started with a separator, we know we at least have an absolute
41
+ // path of some kind (UNC or otherwise)
42
+ isAbsolute = true;
43
+
44
+ if (isPathSeparator(path.charCodeAt(1))) {
45
+ // Matched double path separator at beginning
46
+ let j = 2;
47
+ let last = j;
48
+ // Match 1 or more non-path separators
49
+ for (; j < len; ++j) {
50
+ if (isPathSeparator(path.charCodeAt(j))) break;
51
+ }
52
+ if (j < len && j !== last) {
53
+ const firstPart = path.slice(last, j);
54
+ // Matched!
55
+ last = j;
56
+ // Match 1 or more path separators
57
+ for (; j < len; ++j) {
58
+ if (!isPathSeparator(path.charCodeAt(j))) break;
59
+ }
60
+ if (j < len && j !== last) {
61
+ // Matched!
62
+ last = j;
63
+ // Match 1 or more non-path separators
64
+ for (; j < len; ++j) {
65
+ if (isPathSeparator(path.charCodeAt(j))) break;
66
+ }
67
+ if (j === len) {
68
+ // We matched a UNC root only
69
+ // Return the normalized version of the UNC root since there
70
+ // is nothing left to process
71
+
72
+ return `\\\\${firstPart}\\${path.slice(last)}\\`;
73
+ } else if (j !== last) {
74
+ // We matched a UNC root with leftovers
75
+
76
+ device = `\\\\${firstPart}\\${path.slice(last, j)}`;
77
+ rootEnd = j;
78
+ }
79
+ }
80
+ }
81
+ } else {
82
+ rootEnd = 1;
83
+ }
84
+ } else if (isWindowsDeviceRoot(code)) {
85
+ // Possible device root
86
+
87
+ if (path.charCodeAt(1) === CHAR_COLON) {
88
+ device = path.slice(0, 2);
89
+ rootEnd = 2;
90
+ if (len > 2) {
91
+ if (isPathSeparator(path.charCodeAt(2))) {
92
+ // Treat separator following drive name as an absolute path
93
+ // indicator
94
+ isAbsolute = true;
95
+ rootEnd = 3;
96
+ }
97
+ }
98
+ }
99
+ }
100
+ } else if (isPathSeparator(code)) {
101
+ // `path` contains just a path separator, exit early to avoid unnecessary
102
+ // work
103
+ return "\\";
104
+ }
105
+
106
+ let tail: string;
107
+ if (rootEnd < len) {
108
+ tail = normalizeString(
109
+ path.slice(rootEnd),
110
+ !isAbsolute,
111
+ "\\",
112
+ isPathSeparator,
113
+ );
114
+ } else {
115
+ tail = "";
116
+ }
117
+ if (tail.length === 0 && !isAbsolute) tail = ".";
118
+ if (tail.length > 0 && isPathSeparator(path.charCodeAt(len - 1))) {
119
+ tail += "\\";
120
+ }
121
+ if (device === undefined) {
122
+ if (isAbsolute) {
123
+ if (tail.length > 0) return `\\${tail}`;
124
+ else return "\\";
125
+ }
126
+ return tail;
127
+ } else if (isAbsolute) {
128
+ if (tail.length > 0) return `${device}\\${tail}`;
129
+ else return `${device}\\`;
130
+ }
131
+ return device + tail;
132
+ }
@@ -0,0 +1,42 @@
1
+ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2
+ // This module is browser compatible.
3
+
4
+ import type { GlobOptions } from "../_common/glob_to_reg_exp.js";
5
+ import { normalize } from "./normalize.js";
6
+ import { SEPARATOR_PATTERN } from "./constants.js";
7
+
8
+ export type { GlobOptions };
9
+
10
+ /**
11
+ * Like normalize(), but doesn't collapse "**\/.." when `globstar` is true.
12
+ *
13
+ * @example Usage
14
+ * ```ts
15
+ * import { normalizeGlob } from "@std/path/windows/normalize-glob";
16
+ * import { assertEquals } from "@std/assert";
17
+ *
18
+ * const normalized = normalizeGlob("**\\foo\\..\\bar", { globstar: true });
19
+ * assertEquals(normalized, "**\\bar");
20
+ * ```
21
+ *
22
+ * @param glob The glob pattern to normalize.
23
+ * @param options The options for glob pattern.
24
+ * @returns The normalized glob pattern.
25
+ */
26
+ export function normalizeGlob(
27
+ glob: string,
28
+ { globstar = false }: GlobOptions = {},
29
+ ): string {
30
+ if (glob.match(/\0/g)) {
31
+ throw new Error(`Glob contains invalid characters: "${glob}"`);
32
+ }
33
+ if (!globstar) {
34
+ return normalize(glob);
35
+ }
36
+ const s = SEPARATOR_PATTERN.source;
37
+ const badParentPattern = new RegExp(
38
+ `(?<=(${s}|^)\\*\\*${s})\\.\\.(?=${s}|$)`,
39
+ "g",
40
+ );
41
+ return normalize(glob.replace(badParentPattern, "\0")).replace(/\0/g, "..");
42
+ }
@@ -0,0 +1,184 @@
1
+ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2
+ // This module is browser compatible.
3
+
4
+ import { CHAR_COLON, CHAR_DOT } from "../_common/constants.js";
5
+ import type { ParsedPath } from "../types.js";
6
+ import { assertPath } from "../_common/assert_path.js";
7
+ import { isPathSeparator, isWindowsDeviceRoot } from "./_util.js";
8
+
9
+ export type { ParsedPath } from "../types.js";
10
+
11
+ /**
12
+ * Return a `ParsedPath` object of the `path`.
13
+ *
14
+ * @example Usage
15
+ * ```ts
16
+ * import { parse } from "@std/path/windows/parse";
17
+ * import { assertEquals } from "@std/assert";
18
+ *
19
+ * const parsed = parse("C:\\foo\\bar\\baz.ext");
20
+ * assertEquals(parsed, {
21
+ * root: "C:\\",
22
+ * dir: "C:\\foo\\bar",
23
+ * base: "baz.ext",
24
+ * ext: ".ext",
25
+ * name: "baz",
26
+ * });
27
+ * ```
28
+ *
29
+ * @param path The path to parse.
30
+ * @returns The `ParsedPath` object.
31
+ */
32
+ export function parse(path: string): ParsedPath {
33
+ assertPath(path);
34
+
35
+ const ret: ParsedPath = { root: "", dir: "", base: "", ext: "", name: "" };
36
+
37
+ const len = path.length;
38
+ if (len === 0) return ret;
39
+
40
+ let rootEnd = 0;
41
+ let code = path.charCodeAt(0);
42
+
43
+ // Try to match a root
44
+ if (len > 1) {
45
+ if (isPathSeparator(code)) {
46
+ // Possible UNC root
47
+
48
+ rootEnd = 1;
49
+ if (isPathSeparator(path.charCodeAt(1))) {
50
+ // Matched double path separator at beginning
51
+ let j = 2;
52
+ let last = j;
53
+ // Match 1 or more non-path separators
54
+ for (; j < len; ++j) {
55
+ if (isPathSeparator(path.charCodeAt(j))) break;
56
+ }
57
+ if (j < len && j !== last) {
58
+ // Matched!
59
+ last = j;
60
+ // Match 1 or more path separators
61
+ for (; j < len; ++j) {
62
+ if (!isPathSeparator(path.charCodeAt(j))) break;
63
+ }
64
+ if (j < len && j !== last) {
65
+ // Matched!
66
+ last = j;
67
+ // Match 1 or more non-path separators
68
+ for (; j < len; ++j) {
69
+ if (isPathSeparator(path.charCodeAt(j))) break;
70
+ }
71
+ if (j === len) {
72
+ // We matched a UNC root only
73
+
74
+ rootEnd = j;
75
+ } else if (j !== last) {
76
+ // We matched a UNC root with leftovers
77
+
78
+ rootEnd = j + 1;
79
+ }
80
+ }
81
+ }
82
+ }
83
+ } else if (isWindowsDeviceRoot(code)) {
84
+ // Possible device root
85
+
86
+ if (path.charCodeAt(1) === CHAR_COLON) {
87
+ rootEnd = 2;
88
+ if (len > 2) {
89
+ if (isPathSeparator(path.charCodeAt(2))) {
90
+ if (len === 3) {
91
+ // `path` contains just a drive root, exit early to avoid
92
+ // unnecessary work
93
+ ret.root = ret.dir = path;
94
+ ret.base = "\\";
95
+ return ret;
96
+ }
97
+ rootEnd = 3;
98
+ }
99
+ } else {
100
+ // `path` contains just a relative drive root, exit early to avoid
101
+ // unnecessary work
102
+ ret.root = ret.dir = path;
103
+ return ret;
104
+ }
105
+ }
106
+ }
107
+ } else if (isPathSeparator(code)) {
108
+ // `path` contains just a path separator, exit early to avoid
109
+ // unnecessary work
110
+ ret.root = ret.dir = path;
111
+ ret.base = "\\";
112
+ return ret;
113
+ }
114
+
115
+ if (rootEnd > 0) ret.root = path.slice(0, rootEnd);
116
+
117
+ let startDot = -1;
118
+ let startPart = rootEnd;
119
+ let end = -1;
120
+ let matchedSlash = true;
121
+ let i = path.length - 1;
122
+
123
+ // Track the state of characters (if any) we see before our first dot and
124
+ // after any path separator we find
125
+ let preDotState = 0;
126
+
127
+ // Get non-dir info
128
+ for (; i >= rootEnd; --i) {
129
+ code = path.charCodeAt(i);
130
+ if (isPathSeparator(code)) {
131
+ // If we reached a path separator that was not part of a set of path
132
+ // separators at the end of the string, stop now
133
+ if (!matchedSlash) {
134
+ startPart = i + 1;
135
+ break;
136
+ }
137
+ continue;
138
+ }
139
+ if (end === -1) {
140
+ // We saw the first non-path separator, mark this as the end of our
141
+ // extension
142
+ matchedSlash = false;
143
+ end = i + 1;
144
+ }
145
+ if (code === CHAR_DOT) {
146
+ // If this is our first dot, mark it as the start of our extension
147
+ if (startDot === -1) startDot = i;
148
+ else if (preDotState !== 1) preDotState = 1;
149
+ } else if (startDot !== -1) {
150
+ // We saw a non-dot and non-path separator before our dot, so we should
151
+ // have a good chance at having a non-empty extension
152
+ preDotState = -1;
153
+ }
154
+ }
155
+
156
+ if (
157
+ startDot === -1 ||
158
+ end === -1 ||
159
+ // We saw a non-dot character immediately before the dot
160
+ preDotState === 0 ||
161
+ // The (right-most) trimmed path component is exactly '..'
162
+ (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)
163
+ ) {
164
+ if (end !== -1) {
165
+ ret.base = ret.name = path.slice(startPart, end);
166
+ }
167
+ } else {
168
+ ret.name = path.slice(startPart, startDot);
169
+ ret.base = path.slice(startPart, end);
170
+ ret.ext = path.slice(startDot, end);
171
+ }
172
+
173
+ // Fallback to '\' in case there is no basename
174
+ ret.base = ret.base || "\\";
175
+
176
+ // If the directory is the root, use the entire root as the `dir` including
177
+ // the trailing slash if any (`C:\abc` -> `C:\`). Otherwise, strip out the
178
+ // trailing slash (`C:\abc\def` -> `C:\abc`).
179
+ if (startPart > 0 && startPart !== rootEnd) {
180
+ ret.dir = path.slice(0, startPart - 1);
181
+ } else ret.dir = ret.root;
182
+
183
+ return ret;
184
+ }
@@ -0,0 +1,128 @@
1
+ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2
+ // This module is browser compatible.
3
+
4
+ import { CHAR_BACKWARD_SLASH } from "../_common/constants.js";
5
+ import { resolve } from "./resolve.js";
6
+ import { assertArgs } from "../_common/relative.js";
7
+
8
+ /**
9
+ * Return the relative path from `from` to `to` based on current working directory.
10
+ *
11
+ * An example in windws, for instance:
12
+ * from = 'C:\\orandea\\test\\aaa'
13
+ * to = 'C:\\orandea\\impl\\bbb'
14
+ * The output of the function should be: '..\\..\\impl\\bbb'
15
+ *
16
+ * @example Usage
17
+ * ```ts
18
+ * import { relative } from "@std/path/windows/relative";
19
+ * import { assertEquals } from "@std/assert";
20
+ *
21
+ * const relativePath = relative("C:\\foobar\\test\\aaa", "C:\\foobar\\impl\\bbb");
22
+ * assertEquals(relativePath, "..\\..\\impl\\bbb");
23
+ * ```
24
+ *
25
+ * @param from The path from which to calculate the relative path
26
+ * @param to The path to which to calculate the relative path
27
+ * @returns The relative path from `from` to `to`
28
+ */
29
+ export function relative(from: string, to: string): string {
30
+ assertArgs(from, to);
31
+
32
+ const fromOrig = resolve(from);
33
+ const toOrig = resolve(to);
34
+
35
+ if (fromOrig === toOrig) return "";
36
+
37
+ from = fromOrig.toLowerCase();
38
+ to = toOrig.toLowerCase();
39
+
40
+ if (from === to) return "";
41
+
42
+ // Trim any leading backslashes
43
+ let fromStart = 0;
44
+ let fromEnd = from.length;
45
+ for (; fromStart < fromEnd; ++fromStart) {
46
+ if (from.charCodeAt(fromStart) !== CHAR_BACKWARD_SLASH) break;
47
+ }
48
+ // Trim trailing backslashes (applicable to UNC paths only)
49
+ for (; fromEnd - 1 > fromStart; --fromEnd) {
50
+ if (from.charCodeAt(fromEnd - 1) !== CHAR_BACKWARD_SLASH) break;
51
+ }
52
+ const fromLen = fromEnd - fromStart;
53
+
54
+ // Trim any leading backslashes
55
+ let toStart = 0;
56
+ let toEnd = to.length;
57
+ for (; toStart < toEnd; ++toStart) {
58
+ if (to.charCodeAt(toStart) !== CHAR_BACKWARD_SLASH) break;
59
+ }
60
+ // Trim trailing backslashes (applicable to UNC paths only)
61
+ for (; toEnd - 1 > toStart; --toEnd) {
62
+ if (to.charCodeAt(toEnd - 1) !== CHAR_BACKWARD_SLASH) break;
63
+ }
64
+ const toLen = toEnd - toStart;
65
+
66
+ // Compare paths to find the longest common path from root
67
+ const length = fromLen < toLen ? fromLen : toLen;
68
+ let lastCommonSep = -1;
69
+ let i = 0;
70
+ for (; i <= length; ++i) {
71
+ if (i === length) {
72
+ if (toLen > length) {
73
+ if (to.charCodeAt(toStart + i) === CHAR_BACKWARD_SLASH) {
74
+ // We get here if `from` is the exact base path for `to`.
75
+ // For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
76
+ return toOrig.slice(toStart + i + 1);
77
+ } else if (i === 2) {
78
+ // We get here if `from` is the device root.
79
+ // For example: from='C:\\'; to='C:\\foo'
80
+ return toOrig.slice(toStart + i);
81
+ }
82
+ }
83
+ if (fromLen > length) {
84
+ if (from.charCodeAt(fromStart + i) === CHAR_BACKWARD_SLASH) {
85
+ // We get here if `to` is the exact base path for `from`.
86
+ // For example: from='C:\\foo\\bar'; to='C:\\foo'
87
+ lastCommonSep = i;
88
+ } else if (i === 2) {
89
+ // We get here if `to` is the device root.
90
+ // For example: from='C:\\foo\\bar'; to='C:\\'
91
+ lastCommonSep = 3;
92
+ }
93
+ }
94
+ break;
95
+ }
96
+ const fromCode = from.charCodeAt(fromStart + i);
97
+ const toCode = to.charCodeAt(toStart + i);
98
+ if (fromCode !== toCode) break;
99
+ else if (fromCode === CHAR_BACKWARD_SLASH) lastCommonSep = i;
100
+ }
101
+
102
+ // We found a mismatch before the first common path separator was seen, so
103
+ // return the original `to`.
104
+ if (i !== length && lastCommonSep === -1) {
105
+ return toOrig;
106
+ }
107
+
108
+ let out = "";
109
+ if (lastCommonSep === -1) lastCommonSep = 0;
110
+ // Generate the relative path based on the path difference between `to` and
111
+ // `from`
112
+ for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
113
+ if (i === fromEnd || from.charCodeAt(i) === CHAR_BACKWARD_SLASH) {
114
+ if (out.length === 0) out += "..";
115
+ else out += "\\..";
116
+ }
117
+ }
118
+
119
+ // Lastly, append the rest of the destination (`to`) path that comes after
120
+ // the common path parts
121
+ if (out.length > 0) {
122
+ return out + toOrig.slice(toStart + lastCommonSep, toEnd);
123
+ } else {
124
+ toStart += lastCommonSep;
125
+ if (toOrig.charCodeAt(toStart) === CHAR_BACKWARD_SLASH) ++toStart;
126
+ return toOrig.slice(toStart, toEnd);
127
+ }
128
+ }