@tybys/wasm-util 0.5.3 → 0.7.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 (41) hide show
  1. package/README.md +193 -193
  2. package/dist/tsdoc-metadata.json +1 -1
  3. package/dist/wasm-util.d.ts +1 -1
  4. package/dist/wasm-util.esm-bundler.js +2608 -2482
  5. package/dist/wasm-util.esm.js +2608 -2482
  6. package/dist/wasm-util.esm.min.js +1 -1
  7. package/dist/wasm-util.js +2608 -2482
  8. package/dist/wasm-util.min.js +1 -1
  9. package/lib/cjs/asyncify.js +157 -157
  10. package/lib/cjs/index.js +12 -12
  11. package/lib/cjs/jspi.js +45 -45
  12. package/lib/cjs/load.js +85 -85
  13. package/lib/cjs/memory.js +34 -34
  14. package/lib/cjs/wasi/error.js +102 -102
  15. package/lib/cjs/wasi/fd.js +267 -267
  16. package/lib/cjs/wasi/fs.js +2 -2
  17. package/lib/cjs/wasi/index.js +171 -168
  18. package/lib/cjs/wasi/path.js +173 -173
  19. package/lib/cjs/wasi/preview1.js +1478 -1369
  20. package/lib/cjs/wasi/rights.js +139 -139
  21. package/lib/cjs/wasi/types.js +219 -209
  22. package/lib/cjs/wasi/util.js +121 -67
  23. package/lib/cjs/webassembly.js +12 -12
  24. package/lib/mjs/asyncify.mjs +153 -153
  25. package/lib/mjs/index.mjs +9 -9
  26. package/lib/mjs/jspi.mjs +39 -39
  27. package/lib/mjs/load.mjs +78 -78
  28. package/lib/mjs/memory.mjs +28 -28
  29. package/lib/mjs/wasi/error.mjs +97 -97
  30. package/lib/mjs/wasi/fd.mjs +256 -256
  31. package/lib/mjs/wasi/fs.mjs +1 -1
  32. package/lib/mjs/wasi/index.mjs +167 -164
  33. package/lib/mjs/wasi/path.mjs +168 -168
  34. package/lib/mjs/wasi/preview1.mjs +1474 -1365
  35. package/lib/mjs/wasi/rights.mjs +134 -134
  36. package/lib/mjs/wasi/types.mjs +216 -206
  37. package/lib/mjs/wasi/util.mjs +108 -55
  38. package/lib/mjs/webassembly.mjs +9 -9
  39. package/package.json +58 -57
  40. package/lib/cjs/memfs.js +0 -2689
  41. package/lib/mjs/memfs.mjs +0 -2688
@@ -1,168 +1,168 @@
1
- import { validateString } from "./util.mjs";
2
- const CHAR_DOT = 46; /* . */
3
- const CHAR_FORWARD_SLASH = 47; /* / */
4
- function isPosixPathSeparator(code) {
5
- return code === CHAR_FORWARD_SLASH;
6
- }
7
- function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
8
- let res = '';
9
- let lastSegmentLength = 0;
10
- let lastSlash = -1;
11
- let dots = 0;
12
- let code = 0;
13
- for (let i = 0; i <= path.length; ++i) {
14
- if (i < path.length) {
15
- code = path.charCodeAt(i);
16
- }
17
- else if (isPathSeparator(code)) {
18
- break;
19
- }
20
- else {
21
- code = CHAR_FORWARD_SLASH;
22
- }
23
- if (isPathSeparator(code)) {
24
- if (lastSlash === i - 1 || dots === 1) {
25
- // NOOP
26
- }
27
- else if (dots === 2) {
28
- if (res.length < 2 || lastSegmentLength !== 2 ||
29
- res.charCodeAt(res.length - 1) !== CHAR_DOT ||
30
- res.charCodeAt(res.length - 2) !== CHAR_DOT) {
31
- if (res.length > 2) {
32
- const lastSlashIndex = res.indexOf(separator);
33
- if (lastSlashIndex === -1) {
34
- res = '';
35
- lastSegmentLength = 0;
36
- }
37
- else {
38
- res = res.slice(0, lastSlashIndex);
39
- lastSegmentLength =
40
- res.length - 1 - res.indexOf(separator);
41
- }
42
- lastSlash = i;
43
- dots = 0;
44
- continue;
45
- }
46
- else if (res.length !== 0) {
47
- res = '';
48
- lastSegmentLength = 0;
49
- lastSlash = i;
50
- dots = 0;
51
- continue;
52
- }
53
- }
54
- if (allowAboveRoot) {
55
- res += res.length > 0 ? `${separator}..` : '..';
56
- lastSegmentLength = 2;
57
- }
58
- }
59
- else {
60
- if (res.length > 0) {
61
- res += `${separator}${path.slice(lastSlash + 1, i)}`;
62
- }
63
- else {
64
- res = path.slice(lastSlash + 1, i);
65
- }
66
- lastSegmentLength = i - lastSlash - 1;
67
- }
68
- lastSlash = i;
69
- dots = 0;
70
- }
71
- else if (code === CHAR_DOT && dots !== -1) {
72
- ++dots;
73
- }
74
- else {
75
- dots = -1;
76
- }
77
- }
78
- return res;
79
- }
80
- export function resolve(...args) {
81
- let resolvedPath = '';
82
- let resolvedAbsolute = false;
83
- for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
84
- const path = i >= 0 ? args[i] : '/';
85
- validateString(path, 'path');
86
- // Skip empty entries
87
- if (path.length === 0) {
88
- continue;
89
- }
90
- resolvedPath = `${path}/${resolvedPath}`;
91
- resolvedAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
92
- }
93
- // At this point the path should be resolved to a full absolute path, but
94
- // handle relative paths to be safe (might happen when process.cwd() fails)
95
- // Normalize the path
96
- resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/', isPosixPathSeparator);
97
- if (resolvedAbsolute) {
98
- return `/${resolvedPath}`;
99
- }
100
- return resolvedPath.length > 0 ? resolvedPath : '.';
101
- }
102
- export function relative(from, to) {
103
- validateString(from, 'from');
104
- validateString(to, 'to');
105
- if (from === to)
106
- return '';
107
- // Trim leading forward slashes.
108
- from = resolve(from);
109
- to = resolve(to);
110
- if (from === to)
111
- return '';
112
- const fromStart = 1;
113
- const fromEnd = from.length;
114
- const fromLen = fromEnd - fromStart;
115
- const toStart = 1;
116
- const toLen = to.length - toStart;
117
- // Compare paths to find the longest common path from root
118
- const length = (fromLen < toLen ? fromLen : toLen);
119
- let lastCommonSep = -1;
120
- let i = 0;
121
- for (; i < length; i++) {
122
- const fromCode = from.charCodeAt(fromStart + i);
123
- if (fromCode !== to.charCodeAt(toStart + i)) {
124
- break;
125
- }
126
- else if (fromCode === CHAR_FORWARD_SLASH) {
127
- lastCommonSep = i;
128
- }
129
- }
130
- if (i === length) {
131
- if (toLen > length) {
132
- if (to.charCodeAt(toStart + i) === CHAR_FORWARD_SLASH) {
133
- // We get here if `from` is the exact base path for `to`.
134
- // For example: from='/foo/bar'; to='/foo/bar/baz'
135
- return to.slice(toStart + i + 1);
136
- }
137
- if (i === 0) {
138
- // We get here if `from` is the root
139
- // For example: from='/'; to='/foo'
140
- return to.slice(toStart + i);
141
- }
142
- }
143
- else if (fromLen > length) {
144
- if (from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) {
145
- // We get here if `to` is the exact base path for `from`.
146
- // For example: from='/foo/bar/baz'; to='/foo/bar'
147
- lastCommonSep = i;
148
- }
149
- else if (i === 0) {
150
- // We get here if `to` is the root.
151
- // For example: from='/foo/bar'; to='/'
152
- lastCommonSep = 0;
153
- }
154
- }
155
- }
156
- let out = '';
157
- // Generate the relative path based on the path difference between `to`
158
- // and `from`.
159
- for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
160
- if (i === fromEnd ||
161
- from.charCodeAt(i) === CHAR_FORWARD_SLASH) {
162
- out += out.length === 0 ? '..' : '/..';
163
- }
164
- }
165
- // Lastly, append the rest of the destination (`to`) path that comes after
166
- // the common path parts.
167
- return `${out}${to.slice(toStart + lastCommonSep)}`;
168
- }
1
+ import { validateString } from "./util.mjs";
2
+ const CHAR_DOT = 46; /* . */
3
+ const CHAR_FORWARD_SLASH = 47; /* / */
4
+ function isPosixPathSeparator(code) {
5
+ return code === CHAR_FORWARD_SLASH;
6
+ }
7
+ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
8
+ let res = '';
9
+ let lastSegmentLength = 0;
10
+ let lastSlash = -1;
11
+ let dots = 0;
12
+ let code = 0;
13
+ for (let i = 0; i <= path.length; ++i) {
14
+ if (i < path.length) {
15
+ code = path.charCodeAt(i);
16
+ }
17
+ else if (isPathSeparator(code)) {
18
+ break;
19
+ }
20
+ else {
21
+ code = CHAR_FORWARD_SLASH;
22
+ }
23
+ if (isPathSeparator(code)) {
24
+ if (lastSlash === i - 1 || dots === 1) {
25
+ // NOOP
26
+ }
27
+ else if (dots === 2) {
28
+ if (res.length < 2 || lastSegmentLength !== 2 ||
29
+ res.charCodeAt(res.length - 1) !== CHAR_DOT ||
30
+ res.charCodeAt(res.length - 2) !== CHAR_DOT) {
31
+ if (res.length > 2) {
32
+ const lastSlashIndex = res.indexOf(separator);
33
+ if (lastSlashIndex === -1) {
34
+ res = '';
35
+ lastSegmentLength = 0;
36
+ }
37
+ else {
38
+ res = res.slice(0, lastSlashIndex);
39
+ lastSegmentLength =
40
+ res.length - 1 - res.indexOf(separator);
41
+ }
42
+ lastSlash = i;
43
+ dots = 0;
44
+ continue;
45
+ }
46
+ else if (res.length !== 0) {
47
+ res = '';
48
+ lastSegmentLength = 0;
49
+ lastSlash = i;
50
+ dots = 0;
51
+ continue;
52
+ }
53
+ }
54
+ if (allowAboveRoot) {
55
+ res += res.length > 0 ? `${separator}..` : '..';
56
+ lastSegmentLength = 2;
57
+ }
58
+ }
59
+ else {
60
+ if (res.length > 0) {
61
+ res += `${separator}${path.slice(lastSlash + 1, i)}`;
62
+ }
63
+ else {
64
+ res = path.slice(lastSlash + 1, i);
65
+ }
66
+ lastSegmentLength = i - lastSlash - 1;
67
+ }
68
+ lastSlash = i;
69
+ dots = 0;
70
+ }
71
+ else if (code === CHAR_DOT && dots !== -1) {
72
+ ++dots;
73
+ }
74
+ else {
75
+ dots = -1;
76
+ }
77
+ }
78
+ return res;
79
+ }
80
+ export function resolve(...args) {
81
+ let resolvedPath = '';
82
+ let resolvedAbsolute = false;
83
+ for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
84
+ const path = i >= 0 ? args[i] : '/';
85
+ validateString(path, 'path');
86
+ // Skip empty entries
87
+ if (path.length === 0) {
88
+ continue;
89
+ }
90
+ resolvedPath = `${path}/${resolvedPath}`;
91
+ resolvedAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
92
+ }
93
+ // At this point the path should be resolved to a full absolute path, but
94
+ // handle relative paths to be safe (might happen when process.cwd() fails)
95
+ // Normalize the path
96
+ resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/', isPosixPathSeparator);
97
+ if (resolvedAbsolute) {
98
+ return `/${resolvedPath}`;
99
+ }
100
+ return resolvedPath.length > 0 ? resolvedPath : '.';
101
+ }
102
+ export function relative(from, to) {
103
+ validateString(from, 'from');
104
+ validateString(to, 'to');
105
+ if (from === to)
106
+ return '';
107
+ // Trim leading forward slashes.
108
+ from = resolve(from);
109
+ to = resolve(to);
110
+ if (from === to)
111
+ return '';
112
+ const fromStart = 1;
113
+ const fromEnd = from.length;
114
+ const fromLen = fromEnd - fromStart;
115
+ const toStart = 1;
116
+ const toLen = to.length - toStart;
117
+ // Compare paths to find the longest common path from root
118
+ const length = (fromLen < toLen ? fromLen : toLen);
119
+ let lastCommonSep = -1;
120
+ let i = 0;
121
+ for (; i < length; i++) {
122
+ const fromCode = from.charCodeAt(fromStart + i);
123
+ if (fromCode !== to.charCodeAt(toStart + i)) {
124
+ break;
125
+ }
126
+ else if (fromCode === CHAR_FORWARD_SLASH) {
127
+ lastCommonSep = i;
128
+ }
129
+ }
130
+ if (i === length) {
131
+ if (toLen > length) {
132
+ if (to.charCodeAt(toStart + i) === CHAR_FORWARD_SLASH) {
133
+ // We get here if `from` is the exact base path for `to`.
134
+ // For example: from='/foo/bar'; to='/foo/bar/baz'
135
+ return to.slice(toStart + i + 1);
136
+ }
137
+ if (i === 0) {
138
+ // We get here if `from` is the root
139
+ // For example: from='/'; to='/foo'
140
+ return to.slice(toStart + i);
141
+ }
142
+ }
143
+ else if (fromLen > length) {
144
+ if (from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) {
145
+ // We get here if `to` is the exact base path for `from`.
146
+ // For example: from='/foo/bar/baz'; to='/foo/bar'
147
+ lastCommonSep = i;
148
+ }
149
+ else if (i === 0) {
150
+ // We get here if `to` is the root.
151
+ // For example: from='/foo/bar'; to='/'
152
+ lastCommonSep = 0;
153
+ }
154
+ }
155
+ }
156
+ let out = '';
157
+ // Generate the relative path based on the path difference between `to`
158
+ // and `from`.
159
+ for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
160
+ if (i === fromEnd ||
161
+ from.charCodeAt(i) === CHAR_FORWARD_SLASH) {
162
+ out += out.length === 0 ? '..' : '/..';
163
+ }
164
+ }
165
+ // Lastly, append the rest of the destination (`to`) path that comes after
166
+ // the common path parts.
167
+ return `${out}${to.slice(toStart + lastCommonSep)}`;
168
+ }