toilscript 0.0.1 → 0.1.0

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 (120) hide show
  1. package/LICENSE +201 -201
  2. package/NOTICE +94 -94
  3. package/README.md +101 -114
  4. package/bin/asc.js +0 -0
  5. package/bin/asinit.js +0 -0
  6. package/dist/asc.generated.d.ts +10022 -0
  7. package/dist/asc.js +24457 -0
  8. package/dist/asc.js.map +7 -0
  9. package/dist/importmap.json +9 -0
  10. package/dist/toilscript.generated.d.ts +11237 -0
  11. package/dist/toilscript.js +337 -0
  12. package/dist/toilscript.js.map +7 -0
  13. package/dist/web.js +22 -0
  14. package/lib/binaryen.d.ts +2 -2
  15. package/lib/binaryen.js +2 -2
  16. package/package.json +115 -114
  17. package/std/README.md +6 -6
  18. package/std/assembly/array.ts +550 -550
  19. package/std/assembly/arraybuffer.ts +77 -77
  20. package/std/assembly/atomics.ts +127 -127
  21. package/std/assembly/bindings/asyncify.ts +16 -16
  22. package/std/assembly/bindings/dom.ts +291 -291
  23. package/std/assembly/bindings/node.ts +6 -6
  24. package/std/assembly/bitflags.ts +53 -53
  25. package/std/assembly/builtins.ts +2650 -2650
  26. package/std/assembly/byteslice.ts +177 -177
  27. package/std/assembly/compat.ts +2 -2
  28. package/std/assembly/console.ts +42 -42
  29. package/std/assembly/crypto.ts +9 -9
  30. package/std/assembly/dataview.ts +181 -181
  31. package/std/assembly/date.ts +375 -375
  32. package/std/assembly/diagnostics.ts +11 -11
  33. package/std/assembly/encoding.ts +151 -151
  34. package/std/assembly/endian.ts +45 -45
  35. package/std/assembly/error.ts +44 -44
  36. package/std/assembly/fixedarray.ts +173 -173
  37. package/std/assembly/fixedmap.ts +326 -326
  38. package/std/assembly/fixedset.ts +275 -275
  39. package/std/assembly/function.ts +42 -42
  40. package/std/assembly/index.d.ts +2891 -2891
  41. package/std/assembly/iterator.ts +35 -35
  42. package/std/assembly/map.ts +269 -269
  43. package/std/assembly/math.ts +3289 -3289
  44. package/std/assembly/memory.ts +123 -123
  45. package/std/assembly/number.ts +388 -388
  46. package/std/assembly/object.ts +36 -36
  47. package/std/assembly/performance.ts +9 -9
  48. package/std/assembly/pointer.ts +80 -80
  49. package/std/assembly/polyfills.ts +27 -27
  50. package/std/assembly/process.ts +50 -50
  51. package/std/assembly/reference.ts +48 -48
  52. package/std/assembly/regexp.ts +12 -12
  53. package/std/assembly/rt/README.md +83 -83
  54. package/std/assembly/rt/common.ts +81 -81
  55. package/std/assembly/rt/index-incremental.ts +2 -2
  56. package/std/assembly/rt/index-memory.ts +1 -1
  57. package/std/assembly/rt/index-minimal.ts +2 -2
  58. package/std/assembly/rt/index-stub.ts +1 -1
  59. package/std/assembly/rt/index.d.ts +37 -37
  60. package/std/assembly/rt/itcms.ts +419 -419
  61. package/std/assembly/rt/memory-runtime.ts +94 -94
  62. package/std/assembly/rt/rtrace.ts +15 -15
  63. package/std/assembly/rt/stub.ts +133 -133
  64. package/std/assembly/rt/tcms.ts +254 -254
  65. package/std/assembly/rt/tlsf.ts +592 -592
  66. package/std/assembly/rt.ts +90 -90
  67. package/std/assembly/set.ts +225 -225
  68. package/std/assembly/shared/feature.ts +68 -68
  69. package/std/assembly/shared/runtime.ts +13 -13
  70. package/std/assembly/shared/target.ts +11 -11
  71. package/std/assembly/shared/tsconfig.json +11 -11
  72. package/std/assembly/shared/typeinfo.ts +72 -72
  73. package/std/assembly/staticarray.ts +423 -423
  74. package/std/assembly/string.ts +850 -850
  75. package/std/assembly/symbol.ts +114 -114
  76. package/std/assembly/table.ts +16 -16
  77. package/std/assembly/tsconfig.json +6 -6
  78. package/std/assembly/typedarray.ts +1954 -1954
  79. package/std/assembly/uri.ts +17 -17
  80. package/std/assembly/util/bytes.ts +107 -107
  81. package/std/assembly/util/casemap.ts +497 -497
  82. package/std/assembly/util/error.ts +58 -58
  83. package/std/assembly/util/hash.ts +117 -117
  84. package/std/assembly/util/math.ts +1922 -1922
  85. package/std/assembly/util/memory.ts +290 -290
  86. package/std/assembly/util/number.ts +873 -873
  87. package/std/assembly/util/sort.ts +313 -313
  88. package/std/assembly/util/string.ts +1202 -1202
  89. package/std/assembly/util/uri.ts +275 -275
  90. package/std/assembly/vector.ts +4 -4
  91. package/std/assembly.json +16 -16
  92. package/std/portable/index.d.ts +461 -461
  93. package/std/portable/index.js +416 -416
  94. package/std/portable.json +11 -11
  95. package/std/types/assembly/index.d.ts +1 -1
  96. package/std/types/assembly/package.json +2 -2
  97. package/std/types/portable/index.d.ts +1 -1
  98. package/std/types/portable/package.json +2 -2
  99. package/tsconfig-base.json +13 -13
  100. package/util/README.md +23 -23
  101. package/util/browser/fs.js +1 -1
  102. package/util/browser/module.js +5 -5
  103. package/util/browser/path.js +520 -520
  104. package/util/browser/process.js +59 -59
  105. package/util/browser/url.js +23 -23
  106. package/util/cpu.d.ts +9 -9
  107. package/util/cpu.js +42 -42
  108. package/util/find.d.ts +6 -6
  109. package/util/find.js +20 -20
  110. package/util/node.d.ts +21 -21
  111. package/util/node.js +34 -34
  112. package/util/options.d.ts +70 -70
  113. package/util/options.js +262 -262
  114. package/util/terminal.d.ts +52 -52
  115. package/util/terminal.js +35 -35
  116. package/util/text.d.ts +26 -26
  117. package/util/text.js +114 -114
  118. package/util/tsconfig.json +9 -9
  119. package/util/web.d.ts +11 -11
  120. package/util/web.js +33 -33
@@ -1,520 +1,520 @@
1
- import * as process from "./process.js";
2
-
3
- // https://github.com/browserify/path-browserify v1.0.1
4
- //
5
- // Copyright (c) 2013 James Halliday
6
- // Copyright Joyent, Inc. and other Node contributors.
7
- //
8
- // Permission is hereby granted, free of charge, to any person obtaining a
9
- // copy of this software and associated documentation files (the
10
- // "Software"), to deal in the Software without restriction, including
11
- // without limitation the rights to use, copy, modify, merge, publish,
12
- // distribute, sublicense, and/or sell copies of the Software, and to permit
13
- // persons to whom the Software is furnished to do so, subject to the
14
- // following conditions:
15
- //
16
- // The above copyright notice and this permission notice shall be included
17
- // in all copies or substantial portions of the Software.
18
- //
19
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20
- // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
22
- // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
23
- // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24
- // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25
- // USE OR OTHER DEALINGS IN THE SOFTWARE.
26
-
27
- function assertPath(path) {
28
- if (typeof path !== 'string') {
29
- throw new TypeError('Path must be a string. Received ' + JSON.stringify(path));
30
- }
31
- }
32
-
33
- // Resolves . and .. elements in a path with directory names
34
- function normalizeStringPosix(path, allowAboveRoot) {
35
- var res = '';
36
- var lastSegmentLength = 0;
37
- var lastSlash = -1;
38
- var dots = 0;
39
- var code;
40
- for (var i = 0; i <= path.length; ++i) {
41
- if (i < path.length)
42
- code = path.charCodeAt(i);
43
- else if (code === 47 /*/*/)
44
- break;
45
- else
46
- code = 47 /*/*/;
47
- if (code === 47 /*/*/) {
48
- if (lastSlash === i - 1 || dots === 1) {
49
- // NOOP
50
- } else if (lastSlash !== i - 1 && dots === 2) {
51
- if (res.length < 2 || lastSegmentLength !== 2 || res.charCodeAt(res.length - 1) !== 46 || res.charCodeAt(res.length - 2) !== 46) {
52
- if (res.length > 2) {
53
- var lastSlashIndex = res.lastIndexOf('/');
54
- if (lastSlashIndex !== res.length - 1) {
55
- if (lastSlashIndex === -1) {
56
- res = '';
57
- lastSegmentLength = 0;
58
- } else {
59
- res = res.slice(0, lastSlashIndex);
60
- lastSegmentLength = res.length - 1 - res.lastIndexOf('/');
61
- }
62
- lastSlash = i;
63
- dots = 0;
64
- continue;
65
- }
66
- } else if (res.length === 2 || res.length === 1) {
67
- res = '';
68
- lastSegmentLength = 0;
69
- lastSlash = i;
70
- dots = 0;
71
- continue;
72
- }
73
- }
74
- if (allowAboveRoot) {
75
- if (res.length > 0)
76
- res += '/..';
77
- else
78
- res = '..';
79
- lastSegmentLength = 2;
80
- }
81
- } else {
82
- if (res.length > 0)
83
- res += '/' + path.slice(lastSlash + 1, i);
84
- else
85
- res = path.slice(lastSlash + 1, i);
86
- lastSegmentLength = i - lastSlash - 1;
87
- }
88
- lastSlash = i;
89
- dots = 0;
90
- } else if (code === 46 && dots !== -1) {
91
- ++dots;
92
- } else {
93
- dots = -1;
94
- }
95
- }
96
- return res;
97
- }
98
-
99
- function _format(sep, pathObject) {
100
- var dir = pathObject.dir || pathObject.root;
101
- var base = pathObject.base || (pathObject.name || '') + (pathObject.ext || '');
102
- if (!dir) {
103
- return base;
104
- }
105
- if (dir === pathObject.root) {
106
- return dir + base;
107
- }
108
- return dir + sep + base;
109
- }
110
-
111
- // path.resolve([from ...], to)
112
- export function resolve() {
113
- var resolvedPath = '';
114
- var resolvedAbsolute = false;
115
- var cwd;
116
-
117
- for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
118
- var path;
119
- if (i >= 0)
120
- path = arguments[i];
121
- else {
122
- if (cwd === undefined)
123
- cwd = process.cwd();
124
- path = cwd;
125
- }
126
-
127
- assertPath(path);
128
-
129
- // Skip empty entries
130
- if (path.length === 0) {
131
- continue;
132
- }
133
-
134
- resolvedPath = path + '/' + resolvedPath;
135
- resolvedAbsolute = path.charCodeAt(0) === 47 /*/*/;
136
- }
137
-
138
- // At this point the path should be resolved to a full absolute path, but
139
- // handle relative paths to be safe (might happen when process.cwd() fails)
140
-
141
- // Normalize the path
142
- resolvedPath = normalizeStringPosix(resolvedPath, !resolvedAbsolute);
143
-
144
- if (resolvedAbsolute) {
145
- if (resolvedPath.length > 0)
146
- return '/' + resolvedPath;
147
- else
148
- return '/';
149
- } else if (resolvedPath.length > 0) {
150
- return resolvedPath;
151
- } else {
152
- return '.';
153
- }
154
- }
155
-
156
- export function normalize(path) {
157
- assertPath(path);
158
-
159
- if (path.length === 0) return '.';
160
-
161
- var isAbsolute = path.charCodeAt(0) === 47 /*/*/;
162
- var trailingSeparator = path.charCodeAt(path.length - 1) === 47 /*/*/;
163
-
164
- // Normalize the path
165
- path = normalizeStringPosix(path, !isAbsolute);
166
-
167
- if (path.length === 0 && !isAbsolute) path = '.';
168
- if (path.length > 0 && trailingSeparator) path += '/';
169
-
170
- if (isAbsolute) return '/' + path;
171
- return path;
172
- }
173
-
174
- export function isAbsolute(path) {
175
- assertPath(path);
176
- return path.length > 0 && path.charCodeAt(0) === 47 /*/*/;
177
- }
178
-
179
- export function join() {
180
- if (arguments.length === 0)
181
- return '.';
182
- var joined;
183
- for (var i = 0; i < arguments.length; ++i) {
184
- var arg = arguments[i];
185
- assertPath(arg);
186
- if (arg.length > 0) {
187
- if (joined === undefined)
188
- joined = arg;
189
- else
190
- joined += '/' + arg;
191
- }
192
- }
193
- if (joined === undefined)
194
- return '.';
195
- return normalize(joined);
196
- }
197
-
198
- export function relative(from, to) {
199
- assertPath(from);
200
- assertPath(to);
201
-
202
- if (from === to) return '';
203
-
204
- from = resolve(from);
205
- to = resolve(to);
206
-
207
- if (from === to) return '';
208
-
209
- if (from === ".") return to; // FIX for 'odule.ts' (see issue #1398)
210
-
211
- // Trim any leading backslashes
212
- var fromStart = 1;
213
- for (; fromStart < from.length; ++fromStart) {
214
- if (from.charCodeAt(fromStart) !== 47 /*/*/)
215
- break;
216
- }
217
- var fromEnd = from.length;
218
- var fromLen = fromEnd - fromStart;
219
-
220
- // Trim any leading backslashes
221
- var toStart = 1;
222
- for (; toStart < to.length; ++toStart) {
223
- if (to.charCodeAt(toStart) !== 47 /*/*/)
224
- break;
225
- }
226
- var toEnd = to.length;
227
- var toLen = toEnd - toStart;
228
-
229
- // Compare paths to find the longest common path from root
230
- var length = fromLen < toLen ? fromLen : toLen;
231
- var lastCommonSep = -1;
232
- var i = 0;
233
- for (; i <= length; ++i) {
234
- if (i === length) {
235
- if (toLen > length) {
236
- if (to.charCodeAt(toStart + i) === 47 /*/*/) {
237
- // We get here if `from` is the exact base path for `to`.
238
- // For example: from='/foo/bar'; to='/foo/bar/baz'
239
- return to.slice(toStart + i + 1);
240
- } else if (i === 0) {
241
- // We get here if `from` is the root
242
- // For example: from='/'; to='/foo'
243
- return to.slice(toStart + i);
244
- }
245
- } else if (fromLen > length) {
246
- if (from.charCodeAt(fromStart + i) === 47 /*/*/) {
247
- // We get here if `to` is the exact base path for `from`.
248
- // For example: from='/foo/bar/baz'; to='/foo/bar'
249
- lastCommonSep = i;
250
- } else if (i === 0) {
251
- // We get here if `to` is the root.
252
- // For example: from='/foo'; to='/'
253
- lastCommonSep = 0;
254
- }
255
- }
256
- break;
257
- }
258
- var fromCode = from.charCodeAt(fromStart + i);
259
- var toCode = to.charCodeAt(toStart + i);
260
- if (fromCode !== toCode)
261
- break;
262
- else if (fromCode === 47 /*/*/)
263
- lastCommonSep = i;
264
- }
265
-
266
- var out = '';
267
- // Generate the relative path based on the path difference between `to`
268
- // and `from`
269
- for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
270
- if (i === fromEnd || from.charCodeAt(i) === 47 /*/*/) {
271
- if (out.length === 0)
272
- out += '..';
273
- else
274
- out += '/..';
275
- }
276
- }
277
-
278
- // Lastly, append the rest of the destination (`to`) path that comes after
279
- // the common path parts
280
- if (out.length > 0)
281
- return out + to.slice(toStart + lastCommonSep);
282
- else {
283
- toStart += lastCommonSep;
284
- if (to.charCodeAt(toStart) === 47 /*/*/)
285
- ++toStart;
286
- return to.slice(toStart);
287
- }
288
- }
289
-
290
- export function dirname(path) {
291
- assertPath(path);
292
- if (path.length === 0) return '.';
293
- var code = path.charCodeAt(0);
294
- var hasRoot = code === 47 /*/*/;
295
- var end = -1;
296
- var matchedSlash = true;
297
- for (var i = path.length - 1; i >= 1; --i) {
298
- code = path.charCodeAt(i);
299
- if (code === 47 /*/*/) {
300
- if (!matchedSlash) {
301
- end = i;
302
- break;
303
- }
304
- } else {
305
- // We saw the first non-path separator
306
- matchedSlash = false;
307
- }
308
- }
309
-
310
- if (end === -1) return hasRoot ? '/' : '.';
311
- if (hasRoot && end === 1) return '//';
312
- return path.slice(0, end);
313
- }
314
-
315
- export function basename(path, ext) {
316
- if (ext !== undefined && typeof ext !== 'string') throw new TypeError('"ext" argument must be a string');
317
- assertPath(path);
318
-
319
- var start = 0;
320
- var end = -1;
321
- var matchedSlash = true;
322
- var i;
323
-
324
- if (ext !== undefined && ext.length > 0 && ext.length <= path.length) {
325
- if (ext.length === path.length && ext === path) return '';
326
- var extIdx = ext.length - 1;
327
- var firstNonSlashEnd = -1;
328
- for (i = path.length - 1; i >= 0; --i) {
329
- var code = path.charCodeAt(i);
330
- if (code === 47 /*/*/) {
331
- // If we reached a path separator that was not part of a set of path
332
- // separators at the end of the string, stop now
333
- if (!matchedSlash) {
334
- start = i + 1;
335
- break;
336
- }
337
- } else {
338
- if (firstNonSlashEnd === -1) {
339
- // We saw the first non-path separator, remember this index in case
340
- // we need it if the extension ends up not matching
341
- matchedSlash = false;
342
- firstNonSlashEnd = i + 1;
343
- }
344
- if (extIdx >= 0) {
345
- // Try to match the explicit extension
346
- if (code === ext.charCodeAt(extIdx)) {
347
- if (--extIdx === -1) {
348
- // We matched the extension, so mark this as the end of our path
349
- // component
350
- end = i;
351
- }
352
- } else {
353
- // Extension does not match, so our result is the entire path
354
- // component
355
- extIdx = -1;
356
- end = firstNonSlashEnd;
357
- }
358
- }
359
- }
360
- }
361
-
362
- if (start === end) end = firstNonSlashEnd;else if (end === -1) end = path.length;
363
- return path.slice(start, end);
364
- } else {
365
- for (i = path.length - 1; i >= 0; --i) {
366
- if (path.charCodeAt(i) === 47 /*/*/) {
367
- // If we reached a path separator that was not part of a set of path
368
- // separators at the end of the string, stop now
369
- if (!matchedSlash) {
370
- start = i + 1;
371
- break;
372
- }
373
- } else if (end === -1) {
374
- // We saw the first non-path separator, mark this as the end of our
375
- // path component
376
- matchedSlash = false;
377
- end = i + 1;
378
- }
379
- }
380
-
381
- if (end === -1) return '';
382
- return path.slice(start, end);
383
- }
384
- }
385
-
386
- export function extname(path) {
387
- assertPath(path);
388
- var startDot = -1;
389
- var startPart = 0;
390
- var end = -1;
391
- var matchedSlash = true;
392
- // Track the state of characters (if any) we see before our first dot and
393
- // after any path separator we find
394
- var preDotState = 0;
395
- for (var i = path.length - 1; i >= 0; --i) {
396
- var code = path.charCodeAt(i);
397
- if (code === 47 /*/*/) {
398
- // If we reached a path separator that was not part of a set of path
399
- // separators at the end of the string, stop now
400
- if (!matchedSlash) {
401
- startPart = i + 1;
402
- break;
403
- }
404
- continue;
405
- }
406
- if (end === -1) {
407
- // We saw the first non-path separator, mark this as the end of our
408
- // extension
409
- matchedSlash = false;
410
- end = i + 1;
411
- }
412
- if (code === 46) {
413
- // If this is our first dot, mark it as the start of our extension
414
- if (startDot === -1)
415
- startDot = i;
416
- else if (preDotState !== 1)
417
- preDotState = 1;
418
- } else if (startDot !== -1) {
419
- // We saw a non-dot and non-path separator before our dot, so we should
420
- // have a good chance at having a non-empty extension
421
- preDotState = -1;
422
- }
423
- }
424
-
425
- if (startDot === -1 || end === -1 ||
426
- // We saw a non-dot character immediately before the dot
427
- preDotState === 0 ||
428
- // The (right-most) trimmed path component is exactly '..'
429
- preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) {
430
- return '';
431
- }
432
- return path.slice(startDot, end);
433
- }
434
-
435
- export function format(pathObject) {
436
- if (pathObject === null || typeof pathObject !== 'object') {
437
- throw new TypeError('The "pathObject" argument must be of type Object. Received type ' + typeof pathObject);
438
- }
439
- return _format('/', pathObject);
440
- }
441
-
442
- export function parse(path) {
443
- assertPath(path);
444
-
445
- var ret = { root: '', dir: '', base: '', ext: '', name: '' };
446
- if (path.length === 0) return ret;
447
- var code = path.charCodeAt(0);
448
- var isAbsolute = code === 47 /*/*/;
449
- var start;
450
- if (isAbsolute) {
451
- ret.root = '/';
452
- start = 1;
453
- } else {
454
- start = 0;
455
- }
456
- var startDot = -1;
457
- var startPart = 0;
458
- var end = -1;
459
- var matchedSlash = true;
460
- var i = path.length - 1;
461
-
462
- // Track the state of characters (if any) we see before our first dot and
463
- // after any path separator we find
464
- var preDotState = 0;
465
-
466
- // Get non-dir info
467
- for (; i >= start; --i) {
468
- code = path.charCodeAt(i);
469
- if (code === 47 /*/*/) {
470
- // If we reached a path separator that was not part of a set of path
471
- // separators at the end of the string, stop now
472
- if (!matchedSlash) {
473
- startPart = i + 1;
474
- break;
475
- }
476
- continue;
477
- }
478
- if (end === -1) {
479
- // We saw the first non-path separator, mark this as the end of our
480
- // extension
481
- matchedSlash = false;
482
- end = i + 1;
483
- }
484
- if (code === 46) {
485
- // If this is our first dot, mark it as the start of our extension
486
- if (startDot === -1) startDot = i;else if (preDotState !== 1) preDotState = 1;
487
- } else if (startDot !== -1) {
488
- // We saw a non-dot and non-path separator before our dot, so we should
489
- // have a good chance at having a non-empty extension
490
- preDotState = -1;
491
- }
492
- }
493
-
494
- if (startDot === -1 || end === -1 ||
495
- // We saw a non-dot character immediately before the dot
496
- preDotState === 0 ||
497
- // The (right-most) trimmed path component is exactly '..'
498
- preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) {
499
- if (end !== -1) {
500
- if (startPart === 0 && isAbsolute) ret.base = ret.name = path.slice(1, end);else ret.base = ret.name = path.slice(startPart, end);
501
- }
502
- } else {
503
- if (startPart === 0 && isAbsolute) {
504
- ret.name = path.slice(1, startDot);
505
- ret.base = path.slice(1, end);
506
- } else {
507
- ret.name = path.slice(startPart, startDot);
508
- ret.base = path.slice(startPart, end);
509
- }
510
- ret.ext = path.slice(startDot, end);
511
- }
512
-
513
- if (startPart > 0) ret.dir = path.slice(0, startPart - 1);else if (isAbsolute) ret.dir = '/';
514
-
515
- return ret;
516
- }
517
-
518
- export const sep = '/';
519
- export const delimiter = ':';
520
- export const win32 = null;
1
+ import * as process from "./process.js";
2
+
3
+ // https://github.com/browserify/path-browserify v1.0.1
4
+ //
5
+ // Copyright (c) 2013 James Halliday
6
+ // Copyright Joyent, Inc. and other Node contributors.
7
+ //
8
+ // Permission is hereby granted, free of charge, to any person obtaining a
9
+ // copy of this software and associated documentation files (the
10
+ // "Software"), to deal in the Software without restriction, including
11
+ // without limitation the rights to use, copy, modify, merge, publish,
12
+ // distribute, sublicense, and/or sell copies of the Software, and to permit
13
+ // persons to whom the Software is furnished to do so, subject to the
14
+ // following conditions:
15
+ //
16
+ // The above copyright notice and this permission notice shall be included
17
+ // in all copies or substantial portions of the Software.
18
+ //
19
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20
+ // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
22
+ // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
23
+ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24
+ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25
+ // USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+
27
+ function assertPath(path) {
28
+ if (typeof path !== 'string') {
29
+ throw new TypeError('Path must be a string. Received ' + JSON.stringify(path));
30
+ }
31
+ }
32
+
33
+ // Resolves . and .. elements in a path with directory names
34
+ function normalizeStringPosix(path, allowAboveRoot) {
35
+ var res = '';
36
+ var lastSegmentLength = 0;
37
+ var lastSlash = -1;
38
+ var dots = 0;
39
+ var code;
40
+ for (var i = 0; i <= path.length; ++i) {
41
+ if (i < path.length)
42
+ code = path.charCodeAt(i);
43
+ else if (code === 47 /*/*/)
44
+ break;
45
+ else
46
+ code = 47 /*/*/;
47
+ if (code === 47 /*/*/) {
48
+ if (lastSlash === i - 1 || dots === 1) {
49
+ // NOOP
50
+ } else if (lastSlash !== i - 1 && dots === 2) {
51
+ if (res.length < 2 || lastSegmentLength !== 2 || res.charCodeAt(res.length - 1) !== 46 || res.charCodeAt(res.length - 2) !== 46) {
52
+ if (res.length > 2) {
53
+ var lastSlashIndex = res.lastIndexOf('/');
54
+ if (lastSlashIndex !== res.length - 1) {
55
+ if (lastSlashIndex === -1) {
56
+ res = '';
57
+ lastSegmentLength = 0;
58
+ } else {
59
+ res = res.slice(0, lastSlashIndex);
60
+ lastSegmentLength = res.length - 1 - res.lastIndexOf('/');
61
+ }
62
+ lastSlash = i;
63
+ dots = 0;
64
+ continue;
65
+ }
66
+ } else if (res.length === 2 || res.length === 1) {
67
+ res = '';
68
+ lastSegmentLength = 0;
69
+ lastSlash = i;
70
+ dots = 0;
71
+ continue;
72
+ }
73
+ }
74
+ if (allowAboveRoot) {
75
+ if (res.length > 0)
76
+ res += '/..';
77
+ else
78
+ res = '..';
79
+ lastSegmentLength = 2;
80
+ }
81
+ } else {
82
+ if (res.length > 0)
83
+ res += '/' + path.slice(lastSlash + 1, i);
84
+ else
85
+ res = path.slice(lastSlash + 1, i);
86
+ lastSegmentLength = i - lastSlash - 1;
87
+ }
88
+ lastSlash = i;
89
+ dots = 0;
90
+ } else if (code === 46 && dots !== -1) {
91
+ ++dots;
92
+ } else {
93
+ dots = -1;
94
+ }
95
+ }
96
+ return res;
97
+ }
98
+
99
+ function _format(sep, pathObject) {
100
+ var dir = pathObject.dir || pathObject.root;
101
+ var base = pathObject.base || (pathObject.name || '') + (pathObject.ext || '');
102
+ if (!dir) {
103
+ return base;
104
+ }
105
+ if (dir === pathObject.root) {
106
+ return dir + base;
107
+ }
108
+ return dir + sep + base;
109
+ }
110
+
111
+ // path.resolve([from ...], to)
112
+ export function resolve() {
113
+ var resolvedPath = '';
114
+ var resolvedAbsolute = false;
115
+ var cwd;
116
+
117
+ for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
118
+ var path;
119
+ if (i >= 0)
120
+ path = arguments[i];
121
+ else {
122
+ if (cwd === undefined)
123
+ cwd = process.cwd();
124
+ path = cwd;
125
+ }
126
+
127
+ assertPath(path);
128
+
129
+ // Skip empty entries
130
+ if (path.length === 0) {
131
+ continue;
132
+ }
133
+
134
+ resolvedPath = path + '/' + resolvedPath;
135
+ resolvedAbsolute = path.charCodeAt(0) === 47 /*/*/;
136
+ }
137
+
138
+ // At this point the path should be resolved to a full absolute path, but
139
+ // handle relative paths to be safe (might happen when process.cwd() fails)
140
+
141
+ // Normalize the path
142
+ resolvedPath = normalizeStringPosix(resolvedPath, !resolvedAbsolute);
143
+
144
+ if (resolvedAbsolute) {
145
+ if (resolvedPath.length > 0)
146
+ return '/' + resolvedPath;
147
+ else
148
+ return '/';
149
+ } else if (resolvedPath.length > 0) {
150
+ return resolvedPath;
151
+ } else {
152
+ return '.';
153
+ }
154
+ }
155
+
156
+ export function normalize(path) {
157
+ assertPath(path);
158
+
159
+ if (path.length === 0) return '.';
160
+
161
+ var isAbsolute = path.charCodeAt(0) === 47 /*/*/;
162
+ var trailingSeparator = path.charCodeAt(path.length - 1) === 47 /*/*/;
163
+
164
+ // Normalize the path
165
+ path = normalizeStringPosix(path, !isAbsolute);
166
+
167
+ if (path.length === 0 && !isAbsolute) path = '.';
168
+ if (path.length > 0 && trailingSeparator) path += '/';
169
+
170
+ if (isAbsolute) return '/' + path;
171
+ return path;
172
+ }
173
+
174
+ export function isAbsolute(path) {
175
+ assertPath(path);
176
+ return path.length > 0 && path.charCodeAt(0) === 47 /*/*/;
177
+ }
178
+
179
+ export function join() {
180
+ if (arguments.length === 0)
181
+ return '.';
182
+ var joined;
183
+ for (var i = 0; i < arguments.length; ++i) {
184
+ var arg = arguments[i];
185
+ assertPath(arg);
186
+ if (arg.length > 0) {
187
+ if (joined === undefined)
188
+ joined = arg;
189
+ else
190
+ joined += '/' + arg;
191
+ }
192
+ }
193
+ if (joined === undefined)
194
+ return '.';
195
+ return normalize(joined);
196
+ }
197
+
198
+ export function relative(from, to) {
199
+ assertPath(from);
200
+ assertPath(to);
201
+
202
+ if (from === to) return '';
203
+
204
+ from = resolve(from);
205
+ to = resolve(to);
206
+
207
+ if (from === to) return '';
208
+
209
+ if (from === ".") return to; // FIX for 'odule.ts' (see issue #1398)
210
+
211
+ // Trim any leading backslashes
212
+ var fromStart = 1;
213
+ for (; fromStart < from.length; ++fromStart) {
214
+ if (from.charCodeAt(fromStart) !== 47 /*/*/)
215
+ break;
216
+ }
217
+ var fromEnd = from.length;
218
+ var fromLen = fromEnd - fromStart;
219
+
220
+ // Trim any leading backslashes
221
+ var toStart = 1;
222
+ for (; toStart < to.length; ++toStart) {
223
+ if (to.charCodeAt(toStart) !== 47 /*/*/)
224
+ break;
225
+ }
226
+ var toEnd = to.length;
227
+ var toLen = toEnd - toStart;
228
+
229
+ // Compare paths to find the longest common path from root
230
+ var length = fromLen < toLen ? fromLen : toLen;
231
+ var lastCommonSep = -1;
232
+ var i = 0;
233
+ for (; i <= length; ++i) {
234
+ if (i === length) {
235
+ if (toLen > length) {
236
+ if (to.charCodeAt(toStart + i) === 47 /*/*/) {
237
+ // We get here if `from` is the exact base path for `to`.
238
+ // For example: from='/foo/bar'; to='/foo/bar/baz'
239
+ return to.slice(toStart + i + 1);
240
+ } else if (i === 0) {
241
+ // We get here if `from` is the root
242
+ // For example: from='/'; to='/foo'
243
+ return to.slice(toStart + i);
244
+ }
245
+ } else if (fromLen > length) {
246
+ if (from.charCodeAt(fromStart + i) === 47 /*/*/) {
247
+ // We get here if `to` is the exact base path for `from`.
248
+ // For example: from='/foo/bar/baz'; to='/foo/bar'
249
+ lastCommonSep = i;
250
+ } else if (i === 0) {
251
+ // We get here if `to` is the root.
252
+ // For example: from='/foo'; to='/'
253
+ lastCommonSep = 0;
254
+ }
255
+ }
256
+ break;
257
+ }
258
+ var fromCode = from.charCodeAt(fromStart + i);
259
+ var toCode = to.charCodeAt(toStart + i);
260
+ if (fromCode !== toCode)
261
+ break;
262
+ else if (fromCode === 47 /*/*/)
263
+ lastCommonSep = i;
264
+ }
265
+
266
+ var out = '';
267
+ // Generate the relative path based on the path difference between `to`
268
+ // and `from`
269
+ for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
270
+ if (i === fromEnd || from.charCodeAt(i) === 47 /*/*/) {
271
+ if (out.length === 0)
272
+ out += '..';
273
+ else
274
+ out += '/..';
275
+ }
276
+ }
277
+
278
+ // Lastly, append the rest of the destination (`to`) path that comes after
279
+ // the common path parts
280
+ if (out.length > 0)
281
+ return out + to.slice(toStart + lastCommonSep);
282
+ else {
283
+ toStart += lastCommonSep;
284
+ if (to.charCodeAt(toStart) === 47 /*/*/)
285
+ ++toStart;
286
+ return to.slice(toStart);
287
+ }
288
+ }
289
+
290
+ export function dirname(path) {
291
+ assertPath(path);
292
+ if (path.length === 0) return '.';
293
+ var code = path.charCodeAt(0);
294
+ var hasRoot = code === 47 /*/*/;
295
+ var end = -1;
296
+ var matchedSlash = true;
297
+ for (var i = path.length - 1; i >= 1; --i) {
298
+ code = path.charCodeAt(i);
299
+ if (code === 47 /*/*/) {
300
+ if (!matchedSlash) {
301
+ end = i;
302
+ break;
303
+ }
304
+ } else {
305
+ // We saw the first non-path separator
306
+ matchedSlash = false;
307
+ }
308
+ }
309
+
310
+ if (end === -1) return hasRoot ? '/' : '.';
311
+ if (hasRoot && end === 1) return '//';
312
+ return path.slice(0, end);
313
+ }
314
+
315
+ export function basename(path, ext) {
316
+ if (ext !== undefined && typeof ext !== 'string') throw new TypeError('"ext" argument must be a string');
317
+ assertPath(path);
318
+
319
+ var start = 0;
320
+ var end = -1;
321
+ var matchedSlash = true;
322
+ var i;
323
+
324
+ if (ext !== undefined && ext.length > 0 && ext.length <= path.length) {
325
+ if (ext.length === path.length && ext === path) return '';
326
+ var extIdx = ext.length - 1;
327
+ var firstNonSlashEnd = -1;
328
+ for (i = path.length - 1; i >= 0; --i) {
329
+ var code = path.charCodeAt(i);
330
+ if (code === 47 /*/*/) {
331
+ // If we reached a path separator that was not part of a set of path
332
+ // separators at the end of the string, stop now
333
+ if (!matchedSlash) {
334
+ start = i + 1;
335
+ break;
336
+ }
337
+ } else {
338
+ if (firstNonSlashEnd === -1) {
339
+ // We saw the first non-path separator, remember this index in case
340
+ // we need it if the extension ends up not matching
341
+ matchedSlash = false;
342
+ firstNonSlashEnd = i + 1;
343
+ }
344
+ if (extIdx >= 0) {
345
+ // Try to match the explicit extension
346
+ if (code === ext.charCodeAt(extIdx)) {
347
+ if (--extIdx === -1) {
348
+ // We matched the extension, so mark this as the end of our path
349
+ // component
350
+ end = i;
351
+ }
352
+ } else {
353
+ // Extension does not match, so our result is the entire path
354
+ // component
355
+ extIdx = -1;
356
+ end = firstNonSlashEnd;
357
+ }
358
+ }
359
+ }
360
+ }
361
+
362
+ if (start === end) end = firstNonSlashEnd;else if (end === -1) end = path.length;
363
+ return path.slice(start, end);
364
+ } else {
365
+ for (i = path.length - 1; i >= 0; --i) {
366
+ if (path.charCodeAt(i) === 47 /*/*/) {
367
+ // If we reached a path separator that was not part of a set of path
368
+ // separators at the end of the string, stop now
369
+ if (!matchedSlash) {
370
+ start = i + 1;
371
+ break;
372
+ }
373
+ } else if (end === -1) {
374
+ // We saw the first non-path separator, mark this as the end of our
375
+ // path component
376
+ matchedSlash = false;
377
+ end = i + 1;
378
+ }
379
+ }
380
+
381
+ if (end === -1) return '';
382
+ return path.slice(start, end);
383
+ }
384
+ }
385
+
386
+ export function extname(path) {
387
+ assertPath(path);
388
+ var startDot = -1;
389
+ var startPart = 0;
390
+ var end = -1;
391
+ var matchedSlash = true;
392
+ // Track the state of characters (if any) we see before our first dot and
393
+ // after any path separator we find
394
+ var preDotState = 0;
395
+ for (var i = path.length - 1; i >= 0; --i) {
396
+ var code = path.charCodeAt(i);
397
+ if (code === 47 /*/*/) {
398
+ // If we reached a path separator that was not part of a set of path
399
+ // separators at the end of the string, stop now
400
+ if (!matchedSlash) {
401
+ startPart = i + 1;
402
+ break;
403
+ }
404
+ continue;
405
+ }
406
+ if (end === -1) {
407
+ // We saw the first non-path separator, mark this as the end of our
408
+ // extension
409
+ matchedSlash = false;
410
+ end = i + 1;
411
+ }
412
+ if (code === 46) {
413
+ // If this is our first dot, mark it as the start of our extension
414
+ if (startDot === -1)
415
+ startDot = i;
416
+ else if (preDotState !== 1)
417
+ preDotState = 1;
418
+ } else if (startDot !== -1) {
419
+ // We saw a non-dot and non-path separator before our dot, so we should
420
+ // have a good chance at having a non-empty extension
421
+ preDotState = -1;
422
+ }
423
+ }
424
+
425
+ if (startDot === -1 || end === -1 ||
426
+ // We saw a non-dot character immediately before the dot
427
+ preDotState === 0 ||
428
+ // The (right-most) trimmed path component is exactly '..'
429
+ preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) {
430
+ return '';
431
+ }
432
+ return path.slice(startDot, end);
433
+ }
434
+
435
+ export function format(pathObject) {
436
+ if (pathObject === null || typeof pathObject !== 'object') {
437
+ throw new TypeError('The "pathObject" argument must be of type Object. Received type ' + typeof pathObject);
438
+ }
439
+ return _format('/', pathObject);
440
+ }
441
+
442
+ export function parse(path) {
443
+ assertPath(path);
444
+
445
+ var ret = { root: '', dir: '', base: '', ext: '', name: '' };
446
+ if (path.length === 0) return ret;
447
+ var code = path.charCodeAt(0);
448
+ var isAbsolute = code === 47 /*/*/;
449
+ var start;
450
+ if (isAbsolute) {
451
+ ret.root = '/';
452
+ start = 1;
453
+ } else {
454
+ start = 0;
455
+ }
456
+ var startDot = -1;
457
+ var startPart = 0;
458
+ var end = -1;
459
+ var matchedSlash = true;
460
+ var i = path.length - 1;
461
+
462
+ // Track the state of characters (if any) we see before our first dot and
463
+ // after any path separator we find
464
+ var preDotState = 0;
465
+
466
+ // Get non-dir info
467
+ for (; i >= start; --i) {
468
+ code = path.charCodeAt(i);
469
+ if (code === 47 /*/*/) {
470
+ // If we reached a path separator that was not part of a set of path
471
+ // separators at the end of the string, stop now
472
+ if (!matchedSlash) {
473
+ startPart = i + 1;
474
+ break;
475
+ }
476
+ continue;
477
+ }
478
+ if (end === -1) {
479
+ // We saw the first non-path separator, mark this as the end of our
480
+ // extension
481
+ matchedSlash = false;
482
+ end = i + 1;
483
+ }
484
+ if (code === 46) {
485
+ // If this is our first dot, mark it as the start of our extension
486
+ if (startDot === -1) startDot = i;else if (preDotState !== 1) preDotState = 1;
487
+ } else if (startDot !== -1) {
488
+ // We saw a non-dot and non-path separator before our dot, so we should
489
+ // have a good chance at having a non-empty extension
490
+ preDotState = -1;
491
+ }
492
+ }
493
+
494
+ if (startDot === -1 || end === -1 ||
495
+ // We saw a non-dot character immediately before the dot
496
+ preDotState === 0 ||
497
+ // The (right-most) trimmed path component is exactly '..'
498
+ preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) {
499
+ if (end !== -1) {
500
+ if (startPart === 0 && isAbsolute) ret.base = ret.name = path.slice(1, end);else ret.base = ret.name = path.slice(startPart, end);
501
+ }
502
+ } else {
503
+ if (startPart === 0 && isAbsolute) {
504
+ ret.name = path.slice(1, startDot);
505
+ ret.base = path.slice(1, end);
506
+ } else {
507
+ ret.name = path.slice(startPart, startDot);
508
+ ret.base = path.slice(startPart, end);
509
+ }
510
+ ret.ext = path.slice(startDot, end);
511
+ }
512
+
513
+ if (startPart > 0) ret.dir = path.slice(0, startPart - 1);else if (isAbsolute) ret.dir = '/';
514
+
515
+ return ret;
516
+ }
517
+
518
+ export const sep = '/';
519
+ export const delimiter = ':';
520
+ export const win32 = null;