@effect/platform 0.59.0 → 0.59.1

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.
@@ -2,7 +2,6 @@ import { GenericTag } from "effect/Context"
2
2
  import * as Effect from "effect/Effect"
3
3
  import { identity } from "effect/Function"
4
4
  import * as Layer from "effect/Layer"
5
- import PathB from "path-browserify"
6
5
  import { BadArgument } from "../Error.js"
7
6
  import type * as Api from "../Path.js"
8
7
 
@@ -12,18 +11,6 @@ export const TypeId: Api.TypeId = Symbol.for("@effect/platform/Path") as Api.Typ
12
11
  /** @internal */
13
12
  export const Path = GenericTag<Api.Path>("@effect/platform/Path")
14
13
 
15
- /** @internal */
16
- export const layer = Layer.succeed(
17
- Path,
18
- Path.of({
19
- [TypeId]: TypeId,
20
- ...PathB,
21
- fromFileUrl,
22
- toFileUrl,
23
- toNamespacedPath: identity
24
- })
25
- )
26
-
27
14
  /**
28
15
  * The following functions are adapted from the Node.js source code:
29
16
  * https://github.com/nodejs/node/blob/main/lib/internal/url.js
@@ -32,6 +19,90 @@ export const layer = Layer.succeed(
32
19
  * - MIT
33
20
  */
34
21
 
22
+ // Resolves . and .. elements in a path with directory names
23
+ function normalizeStringPosix(path: string, allowAboveRoot: boolean) {
24
+ let res = ""
25
+ let lastSegmentLength = 0
26
+ let lastSlash = -1
27
+ let dots = 0
28
+ let code
29
+ for (let i = 0; i <= path.length; ++i) {
30
+ if (i < path.length) {
31
+ code = path.charCodeAt(i)
32
+ } else if (code === 47 /*/*/) {
33
+ break
34
+ } else {
35
+ code = 47 /*/*/
36
+ }
37
+ if (code === 47 /*/*/) {
38
+ if (lastSlash === i - 1 || dots === 1) {
39
+ // NOOP
40
+ } else if (lastSlash !== i - 1 && dots === 2) {
41
+ if (
42
+ res.length < 2 || lastSegmentLength !== 2 || res.charCodeAt(res.length - 1) !== 46 /*.*/ ||
43
+ res.charCodeAt(res.length - 2) !== 46 /*.*/
44
+ ) {
45
+ if (res.length > 2) {
46
+ const lastSlashIndex = res.lastIndexOf("/")
47
+ if (lastSlashIndex !== res.length - 1) {
48
+ if (lastSlashIndex === -1) {
49
+ res = ""
50
+ lastSegmentLength = 0
51
+ } else {
52
+ res = res.slice(0, lastSlashIndex)
53
+ lastSegmentLength = res.length - 1 - res.lastIndexOf("/")
54
+ }
55
+ lastSlash = i
56
+ dots = 0
57
+ continue
58
+ }
59
+ } else if (res.length === 2 || res.length === 1) {
60
+ res = ""
61
+ lastSegmentLength = 0
62
+ lastSlash = i
63
+ dots = 0
64
+ continue
65
+ }
66
+ }
67
+ if (allowAboveRoot) {
68
+ if (res.length > 0) {
69
+ res += "/.."
70
+ } else {
71
+ res = ".."
72
+ }
73
+ lastSegmentLength = 2
74
+ }
75
+ } else {
76
+ if (res.length > 0) {
77
+ res += "/" + path.slice(lastSlash + 1, i)
78
+ } else {
79
+ res = path.slice(lastSlash + 1, i)
80
+ }
81
+ lastSegmentLength = i - lastSlash - 1
82
+ }
83
+ lastSlash = i
84
+ dots = 0
85
+ } else if (code === 46 /*.*/ && dots !== -1) {
86
+ ;++dots
87
+ } else {
88
+ dots = -1
89
+ }
90
+ }
91
+ return res
92
+ }
93
+
94
+ function _format(sep: string, pathObject: Partial<Api.Path.Parsed>) {
95
+ const dir = pathObject.dir || pathObject.root
96
+ const base = pathObject.base || (pathObject.name || "") + (pathObject.ext || "")
97
+ if (!dir) {
98
+ return base
99
+ }
100
+ if (dir === pathObject.root) {
101
+ return dir + base
102
+ }
103
+ return dir + sep + base
104
+ }
105
+
35
106
  function fromFileUrl(url: URL): Effect.Effect<string, BadArgument> {
36
107
  if (url.protocol !== "file:") {
37
108
  return Effect.fail(BadArgument({
@@ -62,11 +133,61 @@ function fromFileUrl(url: URL): Effect.Effect<string, BadArgument> {
62
133
  return Effect.succeed(decodeURIComponent(pathname))
63
134
  }
64
135
 
136
+ const resolve: Api.Path["resolve"] = function resolve() {
137
+ let resolvedPath = ""
138
+ let resolvedAbsolute = false
139
+ let cwd: string | undefined = undefined
140
+
141
+ for (let i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
142
+ let path: string
143
+ if (i >= 0) {
144
+ path = arguments[i]
145
+ } else {
146
+ const process = (globalThis as any).process
147
+ if (
148
+ cwd === undefined && "process" in globalThis &&
149
+ typeof process === "object" &&
150
+ process !== null &&
151
+ typeof process.cwd === "function"
152
+ ) {
153
+ cwd = process.cwd()
154
+ }
155
+ path = cwd!
156
+ }
157
+
158
+ // Skip empty entries
159
+ if (path.length === 0) {
160
+ continue
161
+ }
162
+
163
+ resolvedPath = path + "/" + resolvedPath
164
+ resolvedAbsolute = path.charCodeAt(0) === 47 /*/*/
165
+ }
166
+
167
+ // At this point the path should be resolved to a full absolute path, but
168
+ // handle relative paths to be safe (might happen when process.cwd() fails)
169
+
170
+ // Normalize the path
171
+ resolvedPath = normalizeStringPosix(resolvedPath, !resolvedAbsolute)
172
+
173
+ if (resolvedAbsolute) {
174
+ if (resolvedPath.length > 0) {
175
+ return "/" + resolvedPath
176
+ } else {
177
+ return "/"
178
+ }
179
+ } else if (resolvedPath.length > 0) {
180
+ return resolvedPath
181
+ } else {
182
+ return "."
183
+ }
184
+ }
185
+
65
186
  const CHAR_FORWARD_SLASH = 47
66
187
 
67
188
  function toFileUrl(filepath: string) {
68
189
  const outURL = new URL("file://")
69
- let resolved = PathB.resolve(filepath)
190
+ let resolved = resolve(filepath)
70
191
  // path.resolve strips trailing slashes so we must add them back
71
192
  const filePathLast = filepath.charCodeAt(filepath.length - 1)
72
193
  if (
@@ -103,3 +224,378 @@ function encodePathChars(filepath: string) {
103
224
  }
104
225
  return filepath
105
226
  }
227
+
228
+ const posixImpl = Path.of({
229
+ [TypeId]: TypeId,
230
+ resolve,
231
+ normalize(path) {
232
+ if (path.length === 0) return "."
233
+
234
+ const isAbsolute = path.charCodeAt(0) === 47 /*/*/
235
+ const trailingSeparator = path.charCodeAt(path.length - 1) === 47 /*/*/
236
+
237
+ // Normalize the path
238
+ path = normalizeStringPosix(path, !isAbsolute)
239
+
240
+ if (path.length === 0 && !isAbsolute) path = "."
241
+ if (path.length > 0 && trailingSeparator) path += "/"
242
+
243
+ if (isAbsolute) return "/" + path
244
+ return path
245
+ },
246
+
247
+ isAbsolute(path) {
248
+ return path.length > 0 && path.charCodeAt(0) === 47 /*/*/
249
+ },
250
+
251
+ join() {
252
+ if (arguments.length === 0) {
253
+ return "."
254
+ }
255
+ let joined
256
+ for (let i = 0; i < arguments.length; ++i) {
257
+ const arg = arguments[i]
258
+ if (arg.length > 0) {
259
+ if (joined === undefined) {
260
+ joined = arg
261
+ } else {
262
+ joined += "/" + arg
263
+ }
264
+ }
265
+ }
266
+ if (joined === undefined) {
267
+ return "."
268
+ }
269
+ return posixImpl.normalize(joined)
270
+ },
271
+
272
+ relative(from, to) {
273
+ if (from === to) return ""
274
+
275
+ from = posixImpl.resolve(from)
276
+ to = posixImpl.resolve(to)
277
+
278
+ if (from === to) return ""
279
+
280
+ // Trim any leading backslashes
281
+ let fromStart = 1
282
+ for (; fromStart < from.length; ++fromStart) {
283
+ if (from.charCodeAt(fromStart) !== 47 /*/*/) {
284
+ break
285
+ }
286
+ }
287
+ const fromEnd = from.length
288
+ const fromLen = fromEnd - fromStart
289
+
290
+ // Trim any leading backslashes
291
+ let toStart = 1
292
+ for (; toStart < to.length; ++toStart) {
293
+ if (to.charCodeAt(toStart) !== 47 /*/*/) {
294
+ break
295
+ }
296
+ }
297
+ const toEnd = to.length
298
+ const toLen = toEnd - toStart
299
+
300
+ // Compare paths to find the longest common path from root
301
+ const length = fromLen < toLen ? fromLen : toLen
302
+ let lastCommonSep = -1
303
+ let i = 0
304
+ for (; i <= length; ++i) {
305
+ if (i === length) {
306
+ if (toLen > length) {
307
+ if (to.charCodeAt(toStart + i) === 47 /*/*/) {
308
+ // We get here if `from` is the exact base path for `to`.
309
+ // For example: from='/foo/bar'; to='/foo/bar/baz'
310
+ return to.slice(toStart + i + 1)
311
+ } else if (i === 0) {
312
+ // We get here if `from` is the root
313
+ // For example: from='/'; to='/foo'
314
+ return to.slice(toStart + i)
315
+ }
316
+ } else if (fromLen > length) {
317
+ if (from.charCodeAt(fromStart + i) === 47 /*/*/) {
318
+ // We get here if `to` is the exact base path for `from`.
319
+ // For example: from='/foo/bar/baz'; to='/foo/bar'
320
+ lastCommonSep = i
321
+ } else if (i === 0) {
322
+ // We get here if `to` is the root.
323
+ // For example: from='/foo'; to='/'
324
+ lastCommonSep = 0
325
+ }
326
+ }
327
+ break
328
+ }
329
+ const fromCode = from.charCodeAt(fromStart + i)
330
+ const toCode = to.charCodeAt(toStart + i)
331
+ if (fromCode !== toCode) {
332
+ break
333
+ } else if (fromCode === 47 /*/*/) {
334
+ lastCommonSep = i
335
+ }
336
+ }
337
+
338
+ let out = ""
339
+ // Generate the relative path based on the path difference between `to`
340
+ // and `from`
341
+ for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
342
+ if (i === fromEnd || from.charCodeAt(i) === 47 /*/*/) {
343
+ if (out.length === 0) {
344
+ out += ".."
345
+ } else {
346
+ out += "/.."
347
+ }
348
+ }
349
+ }
350
+
351
+ // Lastly, append the rest of the destination (`to`) path that comes after
352
+ // the common path parts
353
+ if (out.length > 0) {
354
+ return out + to.slice(toStart + lastCommonSep)
355
+ } else {
356
+ toStart += lastCommonSep
357
+ if (to.charCodeAt(toStart) === 47 /*/*/) {
358
+ ;++toStart
359
+ }
360
+ return to.slice(toStart)
361
+ }
362
+ },
363
+
364
+ dirname(path) {
365
+ if (path.length === 0) return "."
366
+ let code = path.charCodeAt(0)
367
+ const hasRoot = code === 47 /*/*/
368
+ let end = -1
369
+ let matchedSlash = true
370
+ for (let i = path.length - 1; i >= 1; --i) {
371
+ code = path.charCodeAt(i)
372
+ if (code === 47 /*/*/) {
373
+ if (!matchedSlash) {
374
+ end = i
375
+ break
376
+ }
377
+ } else {
378
+ // We saw the first non-path separator
379
+ matchedSlash = false
380
+ }
381
+ }
382
+
383
+ if (end === -1) return hasRoot ? "/" : "."
384
+ if (hasRoot && end === 1) return "//"
385
+ return path.slice(0, end)
386
+ },
387
+
388
+ basename(path, ext) {
389
+ let start = 0
390
+ let end = -1
391
+ let matchedSlash = true
392
+ let i
393
+
394
+ if (ext !== undefined && ext.length > 0 && ext.length <= path.length) {
395
+ if (ext.length === path.length && ext === path) return ""
396
+ let extIdx = ext.length - 1
397
+ let firstNonSlashEnd = -1
398
+ for (i = path.length - 1; i >= 0; --i) {
399
+ const code = path.charCodeAt(i)
400
+ if (code === 47 /*/*/) {
401
+ // If we reached a path separator that was not part of a set of path
402
+ // separators at the end of the string, stop now
403
+ if (!matchedSlash) {
404
+ start = i + 1
405
+ break
406
+ }
407
+ } else {
408
+ if (firstNonSlashEnd === -1) {
409
+ // We saw the first non-path separator, remember this index in case
410
+ // we need it if the extension ends up not matching
411
+ matchedSlash = false
412
+ firstNonSlashEnd = i + 1
413
+ }
414
+ if (extIdx >= 0) {
415
+ // Try to match the explicit extension
416
+ if (code === ext.charCodeAt(extIdx)) {
417
+ if (--extIdx === -1) {
418
+ // We matched the extension, so mark this as the end of our path
419
+ // component
420
+ end = i
421
+ }
422
+ } else {
423
+ // Extension does not match, so our result is the entire path
424
+ // component
425
+ extIdx = -1
426
+ end = firstNonSlashEnd
427
+ }
428
+ }
429
+ }
430
+ }
431
+
432
+ if (start === end) end = firstNonSlashEnd
433
+ else if (end === -1) end = path.length
434
+ return path.slice(start, end)
435
+ } else {
436
+ for (i = path.length - 1; i >= 0; --i) {
437
+ if (path.charCodeAt(i) === 47 /*/*/) {
438
+ // If we reached a path separator that was not part of a set of path
439
+ // separators at the end of the string, stop now
440
+ if (!matchedSlash) {
441
+ start = i + 1
442
+ break
443
+ }
444
+ } else if (end === -1) {
445
+ // We saw the first non-path separator, mark this as the end of our
446
+ // path component
447
+ matchedSlash = false
448
+ end = i + 1
449
+ }
450
+ }
451
+
452
+ if (end === -1) return ""
453
+ return path.slice(start, end)
454
+ }
455
+ },
456
+
457
+ extname(path) {
458
+ let startDot = -1
459
+ let startPart = 0
460
+ let end = -1
461
+ let matchedSlash = true
462
+ // Track the state of characters (if any) we see before our first dot and
463
+ // after any path separator we find
464
+ let preDotState = 0
465
+ for (let i = path.length - 1; i >= 0; --i) {
466
+ const code = path.charCodeAt(i)
467
+ if (code === 47 /*/*/) {
468
+ // If we reached a path separator that was not part of a set of path
469
+ // separators at the end of the string, stop now
470
+ if (!matchedSlash) {
471
+ startPart = i + 1
472
+ break
473
+ }
474
+ continue
475
+ }
476
+ if (end === -1) {
477
+ // We saw the first non-path separator, mark this as the end of our
478
+ // extension
479
+ matchedSlash = false
480
+ end = i + 1
481
+ }
482
+ if (code === 46 /*.*/) {
483
+ // If this is our first dot, mark it as the start of our extension
484
+ if (startDot === -1) {
485
+ startDot = i
486
+ } else if (preDotState !== 1) {
487
+ preDotState = 1
488
+ }
489
+ } else if (startDot !== -1) {
490
+ // We saw a non-dot and non-path separator before our dot, so we should
491
+ // have a good chance at having a non-empty extension
492
+ preDotState = -1
493
+ }
494
+ }
495
+
496
+ if (
497
+ startDot === -1 || end === -1 ||
498
+ // We saw a non-dot character immediately before the dot
499
+ preDotState === 0 ||
500
+ // The (right-most) trimmed path component is exactly '..'
501
+ preDotState === 1 && startDot === end - 1 && startDot === startPart + 1
502
+ ) {
503
+ return ""
504
+ }
505
+ return path.slice(startDot, end)
506
+ },
507
+
508
+ format: function format(pathObject) {
509
+ if (pathObject === null || typeof pathObject !== "object") {
510
+ throw new TypeError("The \"pathObject\" argument must be of type Object. Received type " + typeof pathObject)
511
+ }
512
+ return _format("/", pathObject)
513
+ },
514
+
515
+ parse(path) {
516
+ const ret = { root: "", dir: "", base: "", ext: "", name: "" }
517
+ if (path.length === 0) return ret
518
+ let code = path.charCodeAt(0)
519
+ const isAbsolute = code === 47 /*/*/
520
+ let start
521
+ if (isAbsolute) {
522
+ ret.root = "/"
523
+ start = 1
524
+ } else {
525
+ start = 0
526
+ }
527
+ let startDot = -1
528
+ let startPart = 0
529
+ let end = -1
530
+ let matchedSlash = true
531
+ let i = path.length - 1
532
+
533
+ // Track the state of characters (if any) we see before our first dot and
534
+ // after any path separator we find
535
+ let preDotState = 0
536
+
537
+ // Get non-dir info
538
+ for (; i >= start; --i) {
539
+ code = path.charCodeAt(i)
540
+ if (code === 47 /*/*/) {
541
+ // If we reached a path separator that was not part of a set of path
542
+ // separators at the end of the string, stop now
543
+ if (!matchedSlash) {
544
+ startPart = i + 1
545
+ break
546
+ }
547
+ continue
548
+ }
549
+ if (end === -1) {
550
+ // We saw the first non-path separator, mark this as the end of our
551
+ // extension
552
+ matchedSlash = false
553
+ end = i + 1
554
+ }
555
+ if (code === 46 /*.*/) {
556
+ // If this is our first dot, mark it as the start of our extension
557
+ if (startDot === -1) startDot = i
558
+ else if (preDotState !== 1) preDotState = 1
559
+ } else if (startDot !== -1) {
560
+ // We saw a non-dot and non-path separator before our dot, so we should
561
+ // have a good chance at having a non-empty extension
562
+ preDotState = -1
563
+ }
564
+ }
565
+
566
+ if (
567
+ startDot === -1 || end === -1 ||
568
+ // We saw a non-dot character immediately before the dot
569
+ preDotState === 0 ||
570
+ // The (right-most) trimmed path component is exactly '..'
571
+ preDotState === 1 && startDot === end - 1 && startDot === startPart + 1
572
+ ) {
573
+ if (end !== -1) {
574
+ if (startPart === 0 && isAbsolute) ret.base = ret.name = path.slice(1, end)
575
+ else ret.base = ret.name = path.slice(startPart, end)
576
+ }
577
+ } else {
578
+ if (startPart === 0 && isAbsolute) {
579
+ ret.name = path.slice(1, startDot)
580
+ ret.base = path.slice(1, end)
581
+ } else {
582
+ ret.name = path.slice(startPart, startDot)
583
+ ret.base = path.slice(startPart, end)
584
+ }
585
+ ret.ext = path.slice(startDot, end)
586
+ }
587
+
588
+ if (startPart > 0) ret.dir = path.slice(0, startPart - 1)
589
+ else if (isAbsolute) ret.dir = "/"
590
+
591
+ return ret
592
+ },
593
+
594
+ sep: "/",
595
+ fromFileUrl,
596
+ toFileUrl,
597
+ toNamespacedPath: identity
598
+ })
599
+
600
+ /** @internal */
601
+ export const layer = Layer.succeed(Path, posixImpl)