@elaraai/east-web-std 1.0.85
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.
- package/CLA.md +26 -0
- package/CONTRIBUTING.md +28 -0
- package/LICENSE.md +31 -0
- package/README.md +191 -0
- package/THIRD_PARTY_NOTICES.md +123 -0
- package/dist/src/compliance.spec.d.ts +6 -0
- package/dist/src/compliance.spec.d.ts.map +1 -0
- package/dist/src/compliance.spec.js +89 -0
- package/dist/src/compliance.spec.js.map +1 -0
- package/dist/src/console.d.ts +237 -0
- package/dist/src/console.d.ts.map +1 -0
- package/dist/src/console.js +260 -0
- package/dist/src/console.js.map +1 -0
- package/dist/src/crypto.d.ts +224 -0
- package/dist/src/crypto.d.ts.map +1 -0
- package/dist/src/crypto.js +295 -0
- package/dist/src/crypto.js.map +1 -0
- package/dist/src/fetch.d.ts +383 -0
- package/dist/src/fetch.d.ts.map +1 -0
- package/dist/src/fetch.js +412 -0
- package/dist/src/fetch.js.map +1 -0
- package/dist/src/index.d.ts +106 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +93 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/path.d.ts +243 -0
- package/dist/src/path.d.ts.map +1 -0
- package/dist/src/path.js +562 -0
- package/dist/src/path.js.map +1 -0
- package/dist/src/random/crypto-rng.d.ts +38 -0
- package/dist/src/random/crypto-rng.d.ts.map +1 -0
- package/dist/src/random/crypto-rng.js +55 -0
- package/dist/src/random/crypto-rng.js.map +1 -0
- package/dist/src/random/distributions/bates.d.ts +17 -0
- package/dist/src/random/distributions/bates.d.ts.map +1 -0
- package/dist/src/random/distributions/bates.js +15 -0
- package/dist/src/random/distributions/bates.js.map +1 -0
- package/dist/src/random/distributions/bernoulli.d.ts +17 -0
- package/dist/src/random/distributions/bernoulli.d.ts.map +1 -0
- package/dist/src/random/distributions/bernoulli.js +13 -0
- package/dist/src/random/distributions/bernoulli.js.map +1 -0
- package/dist/src/random/distributions/binomial.d.ts +17 -0
- package/dist/src/random/distributions/binomial.d.ts.map +1 -0
- package/dist/src/random/distributions/binomial.js +24 -0
- package/dist/src/random/distributions/binomial.js.map +1 -0
- package/dist/src/random/distributions/exponential.d.ts +17 -0
- package/dist/src/random/distributions/exponential.d.ts.map +1 -0
- package/dist/src/random/distributions/exponential.js +13 -0
- package/dist/src/random/distributions/exponential.js.map +1 -0
- package/dist/src/random/distributions/geometric.d.ts +17 -0
- package/dist/src/random/distributions/geometric.d.ts.map +1 -0
- package/dist/src/random/distributions/geometric.js +14 -0
- package/dist/src/random/distributions/geometric.js.map +1 -0
- package/dist/src/random/distributions/irwin-hall.d.ts +17 -0
- package/dist/src/random/distributions/irwin-hall.d.ts.map +1 -0
- package/dist/src/random/distributions/irwin-hall.js +18 -0
- package/dist/src/random/distributions/irwin-hall.js.map +1 -0
- package/dist/src/random/distributions/log-normal.d.ts +17 -0
- package/dist/src/random/distributions/log-normal.d.ts.map +1 -0
- package/dist/src/random/distributions/log-normal.js +12 -0
- package/dist/src/random/distributions/log-normal.js.map +1 -0
- package/dist/src/random/distributions/normal.d.ts +18 -0
- package/dist/src/random/distributions/normal.d.ts.map +1 -0
- package/dist/src/random/distributions/normal.js +16 -0
- package/dist/src/random/distributions/normal.js.map +1 -0
- package/dist/src/random/distributions/pareto.d.ts +17 -0
- package/dist/src/random/distributions/pareto.d.ts.map +1 -0
- package/dist/src/random/distributions/pareto.js +14 -0
- package/dist/src/random/distributions/pareto.js.map +1 -0
- package/dist/src/random/distributions/poisson.d.ts +17 -0
- package/dist/src/random/distributions/poisson.d.ts.map +1 -0
- package/dist/src/random/distributions/poisson.js +81 -0
- package/dist/src/random/distributions/poisson.js.map +1 -0
- package/dist/src/random/distributions/uniform-int.d.ts +17 -0
- package/dist/src/random/distributions/uniform-int.d.ts.map +1 -0
- package/dist/src/random/distributions/uniform-int.js +16 -0
- package/dist/src/random/distributions/uniform-int.js.map +1 -0
- package/dist/src/random/distributions/uniform.d.ts +17 -0
- package/dist/src/random/distributions/uniform.d.ts.map +1 -0
- package/dist/src/random/distributions/uniform.js +10 -0
- package/dist/src/random/distributions/uniform.js.map +1 -0
- package/dist/src/random/rng.d.ts +21 -0
- package/dist/src/random/rng.d.ts.map +1 -0
- package/dist/src/random/rng.js +7 -0
- package/dist/src/random/rng.js.map +1 -0
- package/dist/src/random/xorshift128.d.ts +42 -0
- package/dist/src/random/xorshift128.d.ts.map +1 -0
- package/dist/src/random/xorshift128.js +119 -0
- package/dist/src/random/xorshift128.js.map +1 -0
- package/dist/src/random.d.ts +822 -0
- package/dist/src/random.d.ts.map +1 -0
- package/dist/src/random.js +992 -0
- package/dist/src/random.js.map +1 -0
- package/dist/src/test.d.ts +86 -0
- package/dist/src/test.d.ts.map +1 -0
- package/dist/src/test.js +160 -0
- package/dist/src/test.js.map +1 -0
- package/dist/src/time.d.ts +201 -0
- package/dist/src/time.d.ts.map +1 -0
- package/dist/src/time.js +300 -0
- package/dist/src/time.js.map +1 -0
- package/package.json +64 -0
package/dist/src/path.js
ADDED
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Elara AI Pty Ltd
|
|
3
|
+
* Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
|
|
4
|
+
*/
|
|
5
|
+
import { East, StringType, ArrayType } from "@elaraai/east";
|
|
6
|
+
import { EastError } from "@elaraai/east/internal";
|
|
7
|
+
// Path strings use POSIX (forward-slash) semantics, as east-node-std's pure
|
|
8
|
+
// path ops do on every OS (`node:path/posix`). A browser has no `node:path`,
|
|
9
|
+
// so the functions below port Node.js's POSIX path functions (lib/path.js,
|
|
10
|
+
// MIT — see THIRD_PARTY_NOTICES.md); test/path.spec.ts holds them to
|
|
11
|
+
// `node:path/posix` over a generated corpus of paths.
|
|
12
|
+
/** `/` */
|
|
13
|
+
const SLASH = 47;
|
|
14
|
+
/** `.` */
|
|
15
|
+
const DOT = 46;
|
|
16
|
+
/**
|
|
17
|
+
* The working directory {@link path_resolve} resolves against. A browser has
|
|
18
|
+
* none, so a relative path resolves against the root.
|
|
19
|
+
*/
|
|
20
|
+
const WORKING_DIRECTORY = "/";
|
|
21
|
+
/**
|
|
22
|
+
* Resolves the `.` and `..` segments of a path and drops empty ones. Node's
|
|
23
|
+
* `normalizeString` for the POSIX separator.
|
|
24
|
+
*
|
|
25
|
+
* @param path - The path, without the root that makes it absolute
|
|
26
|
+
* @param allowAboveRoot - Whether `..` segments that climb past the start stay
|
|
27
|
+
*/
|
|
28
|
+
function normalizeString(path, allowAboveRoot) {
|
|
29
|
+
let res = "";
|
|
30
|
+
let lastSegmentLength = 0;
|
|
31
|
+
let lastSlash = -1;
|
|
32
|
+
let dots = 0;
|
|
33
|
+
let code = 0;
|
|
34
|
+
for (let i = 0; i <= path.length; ++i) {
|
|
35
|
+
if (i < path.length) {
|
|
36
|
+
code = path.charCodeAt(i);
|
|
37
|
+
}
|
|
38
|
+
else if (code === SLASH) {
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
code = SLASH;
|
|
43
|
+
}
|
|
44
|
+
if (code === SLASH) {
|
|
45
|
+
if (lastSlash === i - 1 || dots === 1) {
|
|
46
|
+
// NOOP
|
|
47
|
+
}
|
|
48
|
+
else if (dots === 2) {
|
|
49
|
+
if (res.length < 2 || lastSegmentLength !== 2 ||
|
|
50
|
+
res.charCodeAt(res.length - 1) !== DOT ||
|
|
51
|
+
res.charCodeAt(res.length - 2) !== DOT) {
|
|
52
|
+
if (res.length > 2) {
|
|
53
|
+
const lastSlashIndex = res.lastIndexOf("/");
|
|
54
|
+
if (lastSlashIndex === -1) {
|
|
55
|
+
res = "";
|
|
56
|
+
lastSegmentLength = 0;
|
|
57
|
+
}
|
|
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 !== 0) {
|
|
67
|
+
res = "";
|
|
68
|
+
lastSegmentLength = 0;
|
|
69
|
+
lastSlash = i;
|
|
70
|
+
dots = 0;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (allowAboveRoot) {
|
|
75
|
+
res += res.length > 0 ? "/.." : "..";
|
|
76
|
+
lastSegmentLength = 2;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
if (res.length > 0) {
|
|
81
|
+
res += `/${path.slice(lastSlash + 1, i)}`;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
res = path.slice(lastSlash + 1, i);
|
|
85
|
+
}
|
|
86
|
+
lastSegmentLength = i - lastSlash - 1;
|
|
87
|
+
}
|
|
88
|
+
lastSlash = i;
|
|
89
|
+
dots = 0;
|
|
90
|
+
}
|
|
91
|
+
else if (code === DOT && dots !== -1) {
|
|
92
|
+
++dots;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
dots = -1;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return res;
|
|
99
|
+
}
|
|
100
|
+
/** Node's `path.posix.normalize`. */
|
|
101
|
+
function normalize(path) {
|
|
102
|
+
if (path.length === 0) {
|
|
103
|
+
return ".";
|
|
104
|
+
}
|
|
105
|
+
const isAbsolute = path.charCodeAt(0) === SLASH;
|
|
106
|
+
const trailingSeparator = path.charCodeAt(path.length - 1) === SLASH;
|
|
107
|
+
// Normalize the path
|
|
108
|
+
let normalized = normalizeString(path, !isAbsolute);
|
|
109
|
+
if (normalized.length === 0) {
|
|
110
|
+
if (isAbsolute) {
|
|
111
|
+
return "/";
|
|
112
|
+
}
|
|
113
|
+
return trailingSeparator ? "./" : ".";
|
|
114
|
+
}
|
|
115
|
+
if (trailingSeparator) {
|
|
116
|
+
normalized += "/";
|
|
117
|
+
}
|
|
118
|
+
return isAbsolute ? `/${normalized}` : normalized;
|
|
119
|
+
}
|
|
120
|
+
/** Node's `path.posix.join`. */
|
|
121
|
+
function join(segments) {
|
|
122
|
+
if (segments.length === 0) {
|
|
123
|
+
return ".";
|
|
124
|
+
}
|
|
125
|
+
let joined;
|
|
126
|
+
for (const segment of segments) {
|
|
127
|
+
if (segment.length > 0) {
|
|
128
|
+
joined = joined === undefined ? segment : `${joined}/${segment}`;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (joined === undefined) {
|
|
132
|
+
return ".";
|
|
133
|
+
}
|
|
134
|
+
return normalize(joined);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Node's `path.posix.resolve(path)`, with {@link WORKING_DIRECTORY} where
|
|
138
|
+
* Node reads `process.cwd()`.
|
|
139
|
+
*/
|
|
140
|
+
function resolve(path) {
|
|
141
|
+
let resolvedPath = "";
|
|
142
|
+
let resolvedAbsolute = false;
|
|
143
|
+
// The path, then the working directory, until one of them is absolute
|
|
144
|
+
for (const segment of [path, WORKING_DIRECTORY]) {
|
|
145
|
+
if (resolvedAbsolute) {
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
// Skip empty entries
|
|
149
|
+
if (segment.length === 0) {
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
resolvedPath = `${segment}/${resolvedPath}`;
|
|
153
|
+
resolvedAbsolute = segment.charCodeAt(0) === SLASH;
|
|
154
|
+
}
|
|
155
|
+
// Normalize the path
|
|
156
|
+
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute);
|
|
157
|
+
if (resolvedAbsolute) {
|
|
158
|
+
return `/${resolvedPath}`;
|
|
159
|
+
}
|
|
160
|
+
return resolvedPath.length > 0 ? resolvedPath : ".";
|
|
161
|
+
}
|
|
162
|
+
/** Node's `path.posix.dirname`. */
|
|
163
|
+
function dirname(path) {
|
|
164
|
+
if (path.length === 0) {
|
|
165
|
+
return ".";
|
|
166
|
+
}
|
|
167
|
+
const hasRoot = path.charCodeAt(0) === SLASH;
|
|
168
|
+
let end = -1;
|
|
169
|
+
let matchedSlash = true;
|
|
170
|
+
for (let i = path.length - 1; i >= 1; --i) {
|
|
171
|
+
if (path.charCodeAt(i) === SLASH) {
|
|
172
|
+
if (!matchedSlash) {
|
|
173
|
+
end = i;
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
// We saw the first non-path separator
|
|
179
|
+
matchedSlash = false;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (end === -1) {
|
|
183
|
+
return hasRoot ? "/" : ".";
|
|
184
|
+
}
|
|
185
|
+
if (hasRoot && end === 1) {
|
|
186
|
+
return "//";
|
|
187
|
+
}
|
|
188
|
+
return path.slice(0, end);
|
|
189
|
+
}
|
|
190
|
+
/** Node's `path.posix.basename`, without a suffix to strip. */
|
|
191
|
+
function basename(path) {
|
|
192
|
+
let start = 0;
|
|
193
|
+
let end = -1;
|
|
194
|
+
let matchedSlash = true;
|
|
195
|
+
for (let i = path.length - 1; i >= 0; --i) {
|
|
196
|
+
if (path.charCodeAt(i) === SLASH) {
|
|
197
|
+
// If we reached a path separator that was not part of a set of path
|
|
198
|
+
// separators at the end of the string, stop now
|
|
199
|
+
if (!matchedSlash) {
|
|
200
|
+
start = i + 1;
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
else if (end === -1) {
|
|
205
|
+
// We saw the first non-path separator, mark this as the end of our
|
|
206
|
+
// path component
|
|
207
|
+
matchedSlash = false;
|
|
208
|
+
end = i + 1;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (end === -1) {
|
|
212
|
+
return "";
|
|
213
|
+
}
|
|
214
|
+
return path.slice(start, end);
|
|
215
|
+
}
|
|
216
|
+
/** Node's `path.posix.extname`. */
|
|
217
|
+
function extname(path) {
|
|
218
|
+
let startDot = -1;
|
|
219
|
+
let startPart = 0;
|
|
220
|
+
let end = -1;
|
|
221
|
+
let matchedSlash = true;
|
|
222
|
+
// Track the state of characters (if any) we see before our first dot and
|
|
223
|
+
// after any path separator we find
|
|
224
|
+
let preDotState = 0;
|
|
225
|
+
for (let i = path.length - 1; i >= 0; --i) {
|
|
226
|
+
const code = path.charCodeAt(i);
|
|
227
|
+
if (code === SLASH) {
|
|
228
|
+
// If we reached a path separator that was not part of a set of path
|
|
229
|
+
// separators at the end of the string, stop now
|
|
230
|
+
if (!matchedSlash) {
|
|
231
|
+
startPart = i + 1;
|
|
232
|
+
break;
|
|
233
|
+
}
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
if (end === -1) {
|
|
237
|
+
// We saw the first non-path separator, mark this as the end of our
|
|
238
|
+
// extension
|
|
239
|
+
matchedSlash = false;
|
|
240
|
+
end = i + 1;
|
|
241
|
+
}
|
|
242
|
+
if (code === DOT) {
|
|
243
|
+
// If this is our first dot, mark it as the start of our extension
|
|
244
|
+
if (startDot === -1) {
|
|
245
|
+
startDot = i;
|
|
246
|
+
}
|
|
247
|
+
else if (preDotState !== 1) {
|
|
248
|
+
preDotState = 1;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
else if (startDot !== -1) {
|
|
252
|
+
// We saw a non-dot and non-path separator before our dot, so we should
|
|
253
|
+
// have a good chance at having a non-empty extension
|
|
254
|
+
preDotState = -1;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (startDot === -1 ||
|
|
258
|
+
end === -1 ||
|
|
259
|
+
// We saw a non-dot character immediately before the dot
|
|
260
|
+
preDotState === 0 ||
|
|
261
|
+
// The (right-most) trimmed path component is exactly '..'
|
|
262
|
+
(preDotState === 1 &&
|
|
263
|
+
startDot === end - 1 &&
|
|
264
|
+
startDot === startPart + 1)) {
|
|
265
|
+
return "";
|
|
266
|
+
}
|
|
267
|
+
return path.slice(startDot, end);
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Joins path segments into a single path.
|
|
271
|
+
*
|
|
272
|
+
* Combines an array of path segments with a forward-slash (`/`) separator on every platform.
|
|
273
|
+
* Normalizes the resulting path by resolving `.` and `..` segments. Empty segments
|
|
274
|
+
* are ignored.
|
|
275
|
+
*
|
|
276
|
+
* This is a platform function for the East language, enabling path joining
|
|
277
|
+
* in East programs running in a browser.
|
|
278
|
+
*
|
|
279
|
+
* @param segments - Array of path segments to join (e.g., ["dir", "subdir", "file.txt"])
|
|
280
|
+
* @returns The joined path, using forward slashes (`/`) on every platform
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* ```ts
|
|
284
|
+
* const buildPath = East.function([], StringType, $ => {
|
|
285
|
+
* const segments = $.let(["home", "user", "documents", "file.txt"], ArrayType(StringType));
|
|
286
|
+
* return Path.join(segments);
|
|
287
|
+
* // Returns: "home/user/documents/file.txt" on every platform
|
|
288
|
+
* });
|
|
289
|
+
* ```
|
|
290
|
+
*/
|
|
291
|
+
export const path_join = East.platform("path_join", [ArrayType(StringType)], StringType);
|
|
292
|
+
/**
|
|
293
|
+
* Resolves a path to an absolute path.
|
|
294
|
+
*
|
|
295
|
+
* Converts a relative path to an absolute path by resolving it against the current
|
|
296
|
+
* working directory. If the path is already absolute, it is normalized and returned.
|
|
297
|
+
* Resolves `.` and `..` segments to their actual locations.
|
|
298
|
+
*
|
|
299
|
+
* This is a platform function for the East language, enabling path resolution
|
|
300
|
+
* in East programs running in a browser. A browser has no working directory,
|
|
301
|
+
* so a relative path resolves against the root, `/`.
|
|
302
|
+
*
|
|
303
|
+
* @param path - The path to resolve (relative or absolute)
|
|
304
|
+
* @returns The absolute path
|
|
305
|
+
*
|
|
306
|
+
* @example
|
|
307
|
+
* ```ts
|
|
308
|
+
* const getAbsolutePath = East.function([], StringType, $ => {
|
|
309
|
+
* return Path.resolve("documents/../file.txt");
|
|
310
|
+
* // Returns: "/file.txt"
|
|
311
|
+
* });
|
|
312
|
+
* ```
|
|
313
|
+
*/
|
|
314
|
+
export const path_resolve = East.platform("path_resolve", [StringType], StringType);
|
|
315
|
+
/**
|
|
316
|
+
* Gets the directory name from a path.
|
|
317
|
+
*
|
|
318
|
+
* Extracts the directory portion of a path by removing the last segment (file name).
|
|
319
|
+
* Similar to the Unix `dirname` command. Trailing path separators are ignored.
|
|
320
|
+
*
|
|
321
|
+
* This is a platform function for the East language, enabling directory extraction
|
|
322
|
+
* in East programs running in a browser.
|
|
323
|
+
*
|
|
324
|
+
* @param path - The file path to extract directory from
|
|
325
|
+
* @returns The directory portion of the path
|
|
326
|
+
*
|
|
327
|
+
* @example
|
|
328
|
+
* ```ts
|
|
329
|
+
* const getDir = East.function([], StringType, $ => {
|
|
330
|
+
* return Path.dirname("/home/user/documents/file.txt");
|
|
331
|
+
* // Returns: "/home/user/documents"
|
|
332
|
+
* });
|
|
333
|
+
* ```
|
|
334
|
+
*/
|
|
335
|
+
export const path_dirname = East.platform("path_dirname", [StringType], StringType);
|
|
336
|
+
/**
|
|
337
|
+
* Gets the base name (file name) from a path.
|
|
338
|
+
*
|
|
339
|
+
* Extracts the last segment of a path, typically the file name. Similar to the Unix
|
|
340
|
+
* `basename` command. Trailing path separators are ignored.
|
|
341
|
+
*
|
|
342
|
+
* This is a platform function for the East language, enabling filename extraction
|
|
343
|
+
* in East programs running in a browser.
|
|
344
|
+
*
|
|
345
|
+
* @param path - The file path to extract filename from
|
|
346
|
+
* @returns The file name portion of the path (including extension)
|
|
347
|
+
*
|
|
348
|
+
* @example
|
|
349
|
+
* ```ts
|
|
350
|
+
* const getFilename = East.function([], StringType, $ => {
|
|
351
|
+
* return Path.basename("/home/user/documents/file.txt");
|
|
352
|
+
* // Returns: "file.txt"
|
|
353
|
+
* });
|
|
354
|
+
* ```
|
|
355
|
+
*/
|
|
356
|
+
export const path_basename = East.platform("path_basename", [StringType], StringType);
|
|
357
|
+
/**
|
|
358
|
+
* Gets the file extension from a path.
|
|
359
|
+
*
|
|
360
|
+
* Extracts the file extension from a path, including the leading dot. If the path
|
|
361
|
+
* has no extension or ends with a dot, returns an empty string. Only returns the
|
|
362
|
+
* last extension if multiple dots are present.
|
|
363
|
+
*
|
|
364
|
+
* This is a platform function for the East language, enabling extension extraction
|
|
365
|
+
* in East programs running in a browser.
|
|
366
|
+
*
|
|
367
|
+
* @param path - The file path to extract extension from
|
|
368
|
+
* @returns The file extension including the dot (e.g., ".txt"), or empty string if none
|
|
369
|
+
*
|
|
370
|
+
* @example
|
|
371
|
+
* ```ts
|
|
372
|
+
* const getExtension = East.function([], StringType, $ => {
|
|
373
|
+
* return Path.extname("/home/user/documents/file.txt");
|
|
374
|
+
* // Returns: ".txt"
|
|
375
|
+
* });
|
|
376
|
+
* ```
|
|
377
|
+
*/
|
|
378
|
+
export const path_extname = East.platform("path_extname", [StringType], StringType);
|
|
379
|
+
/**
|
|
380
|
+
* Browser implementation of path platform functions.
|
|
381
|
+
*
|
|
382
|
+
* Pass this array to {@link East.compile} to enable path operations.
|
|
383
|
+
*/
|
|
384
|
+
const PathImpl = [
|
|
385
|
+
path_join.implement((segments) => {
|
|
386
|
+
try {
|
|
387
|
+
return join(segments);
|
|
388
|
+
}
|
|
389
|
+
catch (err) {
|
|
390
|
+
throw new EastError(`Failed to join path segments: ${err.message}`, {
|
|
391
|
+
location: [{ filename: "path_join", line: 0n, column: 0n }],
|
|
392
|
+
cause: err
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
}),
|
|
396
|
+
path_resolve.implement((path) => {
|
|
397
|
+
try {
|
|
398
|
+
return resolve(path);
|
|
399
|
+
}
|
|
400
|
+
catch (err) {
|
|
401
|
+
throw new EastError(`Failed to resolve path: ${err.message}`, {
|
|
402
|
+
location: [{ filename: "path_resolve", line: 0n, column: 0n }],
|
|
403
|
+
cause: err
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
}),
|
|
407
|
+
path_dirname.implement((path) => {
|
|
408
|
+
try {
|
|
409
|
+
return dirname(path);
|
|
410
|
+
}
|
|
411
|
+
catch (err) {
|
|
412
|
+
throw new EastError(`Failed to get dirname: ${err.message}`, {
|
|
413
|
+
location: [{ filename: "path_dirname", line: 0n, column: 0n }],
|
|
414
|
+
cause: err
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
}),
|
|
418
|
+
path_basename.implement((path) => {
|
|
419
|
+
try {
|
|
420
|
+
return basename(path);
|
|
421
|
+
}
|
|
422
|
+
catch (err) {
|
|
423
|
+
throw new EastError(`Failed to get basename: ${err.message}`, {
|
|
424
|
+
location: [{ filename: "path_basename", line: 0n, column: 0n }],
|
|
425
|
+
cause: err
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
}),
|
|
429
|
+
path_extname.implement((path) => {
|
|
430
|
+
try {
|
|
431
|
+
return extname(path);
|
|
432
|
+
}
|
|
433
|
+
catch (err) {
|
|
434
|
+
throw new EastError(`Failed to get extname: ${err.message}`, {
|
|
435
|
+
location: [{ filename: "path_extname", line: 0n, column: 0n }],
|
|
436
|
+
cause: err
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
}),
|
|
440
|
+
];
|
|
441
|
+
/**
|
|
442
|
+
* Grouped path manipulation platform functions.
|
|
443
|
+
*
|
|
444
|
+
* Provides path utilities for East programs.
|
|
445
|
+
*
|
|
446
|
+
* @example
|
|
447
|
+
* ```ts
|
|
448
|
+
* import { East, StringType, ArrayType } from "@elaraai/east";
|
|
449
|
+
* import { Path } from "@elaraai/east-web-std";
|
|
450
|
+
*
|
|
451
|
+
* const buildPath = East.function([], StringType, $ => {
|
|
452
|
+
* const segments = $.let(["dir", "subdir", "file.txt"], ArrayType(StringType));
|
|
453
|
+
* const fullPath = $.let(Path.join(segments));
|
|
454
|
+
* const ext = $.let(Path.extname(fullPath));
|
|
455
|
+
* return fullPath;
|
|
456
|
+
* });
|
|
457
|
+
*
|
|
458
|
+
* const compiled = East.compile(buildPath, Path.Implementation);
|
|
459
|
+
* compiled(); // Returns: "dir/subdir/file.txt"
|
|
460
|
+
* ```
|
|
461
|
+
*/
|
|
462
|
+
export const Path = {
|
|
463
|
+
/**
|
|
464
|
+
* Joins path segments into a single path.
|
|
465
|
+
*
|
|
466
|
+
* Combines an array of path segments with a forward-slash (`/`) separator on every platform.
|
|
467
|
+
* Normalizes the resulting path and ignores empty segments.
|
|
468
|
+
*
|
|
469
|
+
* @param segments - Array of path segments to join
|
|
470
|
+
* @returns The joined path, using forward slashes (`/`) on every platform
|
|
471
|
+
*
|
|
472
|
+
* @example
|
|
473
|
+
* ```ts
|
|
474
|
+
* const buildPath = East.function([], StringType, $ => {
|
|
475
|
+
* const segments = $.let(["home", "user", "file.txt"], ArrayType(StringType));
|
|
476
|
+
* return Path.join(segments);
|
|
477
|
+
* });
|
|
478
|
+
* ```
|
|
479
|
+
*/
|
|
480
|
+
join: path_join,
|
|
481
|
+
/**
|
|
482
|
+
* Resolves a path to an absolute path.
|
|
483
|
+
*
|
|
484
|
+
* Converts a relative path to an absolute path by resolving it against the
|
|
485
|
+
* working directory, which in a browser is the root, `/`. Normalizes the
|
|
486
|
+
* path and resolves `.` and `..` segments.
|
|
487
|
+
*
|
|
488
|
+
* @param path - The path to resolve (relative or absolute)
|
|
489
|
+
* @returns The absolute path
|
|
490
|
+
*
|
|
491
|
+
* @example
|
|
492
|
+
* ```ts
|
|
493
|
+
* const getAbsolutePath = East.function([], StringType, $ => {
|
|
494
|
+
* return Path.resolve("documents/../file.txt"); // "/file.txt"
|
|
495
|
+
* });
|
|
496
|
+
* ```
|
|
497
|
+
*/
|
|
498
|
+
resolve: path_resolve,
|
|
499
|
+
/**
|
|
500
|
+
* Gets the directory name from a path.
|
|
501
|
+
*
|
|
502
|
+
* Extracts the directory portion of a path by removing the last segment (file name).
|
|
503
|
+
* Trailing path separators are ignored.
|
|
504
|
+
*
|
|
505
|
+
* @param path - The file path to extract directory from
|
|
506
|
+
* @returns The directory portion of the path
|
|
507
|
+
*
|
|
508
|
+
* @example
|
|
509
|
+
* ```ts
|
|
510
|
+
* const getDir = East.function([], StringType, $ => {
|
|
511
|
+
* return Path.dirname("/home/user/documents/file.txt");
|
|
512
|
+
* });
|
|
513
|
+
* ```
|
|
514
|
+
*/
|
|
515
|
+
dirname: path_dirname,
|
|
516
|
+
/**
|
|
517
|
+
* Gets the base name (file name) from a path.
|
|
518
|
+
*
|
|
519
|
+
* Extracts the last segment of a path, typically the file name.
|
|
520
|
+
* Trailing path separators are ignored.
|
|
521
|
+
*
|
|
522
|
+
* @param path - The file path to extract filename from
|
|
523
|
+
* @returns The file name portion of the path (including extension)
|
|
524
|
+
*
|
|
525
|
+
* @example
|
|
526
|
+
* ```ts
|
|
527
|
+
* const getFilename = East.function([], StringType, $ => {
|
|
528
|
+
* return Path.basename("/home/user/documents/file.txt");
|
|
529
|
+
* });
|
|
530
|
+
* ```
|
|
531
|
+
*/
|
|
532
|
+
basename: path_basename,
|
|
533
|
+
/**
|
|
534
|
+
* Gets the file extension from a path.
|
|
535
|
+
*
|
|
536
|
+
* Extracts the file extension from a path, including the leading dot.
|
|
537
|
+
* Returns empty string if no extension exists.
|
|
538
|
+
*
|
|
539
|
+
* @param path - The file path to extract extension from
|
|
540
|
+
* @returns The file extension including the dot (e.g., ".txt"), or empty string
|
|
541
|
+
*
|
|
542
|
+
* @example
|
|
543
|
+
* ```ts
|
|
544
|
+
* const getExtension = East.function([], StringType, $ => {
|
|
545
|
+
* return Path.extname("file.txt");
|
|
546
|
+
* });
|
|
547
|
+
*
|
|
548
|
+
* const compiled = East.compile(getExtension, Path.Implementation);
|
|
549
|
+
* compiled(); // Returns: ".txt"
|
|
550
|
+
* ```
|
|
551
|
+
*/
|
|
552
|
+
extname: path_extname,
|
|
553
|
+
/**
|
|
554
|
+
* Browser implementation of path platform functions.
|
|
555
|
+
*
|
|
556
|
+
* Pass this to {@link East.compile} to enable path operations.
|
|
557
|
+
*/
|
|
558
|
+
Implementation: PathImpl,
|
|
559
|
+
};
|
|
560
|
+
// Exported beside `Path.Implementation`, as east-node-std exports it
|
|
561
|
+
export { PathImpl };
|
|
562
|
+
//# source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,SAAS,CAAC,0BAA0B,GAAG,CAAC,OAAO,EAAE,EAAE;gBACzD,QAAQ,EAAE,CAAC,EAAE,QAAQ,EAAE,cAAc,EAAE,IAAI,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC;gBAC9D,KAAK,EAAE,GAAG;aACb,CAAC,CAAC;QACP,CAAC;IACL,CAAC,CAAC;CACL,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG;IAChB;;;;;;;;;;;;;;;;OAgBG;IACH,IAAI,EAAE,SAAS;IAEf;;;;;;;;;;;;;;;;OAgBG;IACH,OAAO,EAAE,YAAY;IAErB;;;;;;;;;;;;;;;OAeG;IACH,OAAO,EAAE,YAAY;IAErB;;;;;;;;;;;;;;;OAeG;IACH,QAAQ,EAAE,aAAa;IAEvB;;;;;;;;;;;;;;;;;;OAkBG;IACH,OAAO,EAAE,YAAY;IAErB;;;;OAIG;IACH,cAAc,EAAE,QAAQ;CAClB,CAAC;AAEX,qEAAqE;AACrE,OAAO,EAAE,QAAQ,EAAE,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Elara AI Pty Ltd
|
|
3
|
+
* Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Vendored from npm package 'random' (https://github.com/transitive-bullshit/random)
|
|
7
|
+
* Originally by Travis Fischer, distributed under MIT license
|
|
8
|
+
*
|
|
9
|
+
* Ported from east-node-std's src/random/crypto-rng.ts
|
|
10
|
+
* (libs/east-node/packages/east-node-std/src/random/crypto-rng.ts), modified
|
|
11
|
+
* to draw from the web-standard `crypto.getRandomValues` where east-node-std
|
|
12
|
+
* reads `node:crypto`'s `randomBytes`. It reads the same 48 bits the same
|
|
13
|
+
* way, so the two draw from the same distribution; an unseeded draw is never
|
|
14
|
+
* reproducible on either.
|
|
15
|
+
*/
|
|
16
|
+
import RNG, { type SeedType } from "./rng.js";
|
|
17
|
+
/**
|
|
18
|
+
* Cryptographically secure RNG using the web-standard crypto.getRandomValues
|
|
19
|
+
*
|
|
20
|
+
* Note: Seeding is not supported for cryptographic RNG as it uses the host's entropy source.
|
|
21
|
+
* The seed/clone methods are provided for API compatibility but have no effect.
|
|
22
|
+
*/
|
|
23
|
+
export default class CryptoRNG extends RNG {
|
|
24
|
+
get name(): string;
|
|
25
|
+
/**
|
|
26
|
+
* Generate a random number in [0, 1) using crypto.getRandomValues
|
|
27
|
+
*/
|
|
28
|
+
next(): number;
|
|
29
|
+
/**
|
|
30
|
+
* Seeding is not supported for cryptographic RNG (no-op for API compatibility)
|
|
31
|
+
*/
|
|
32
|
+
seed(_seed?: SeedType, _opts?: Record<string, unknown>): void;
|
|
33
|
+
/**
|
|
34
|
+
* Clone returns a new instance (seeding not supported)
|
|
35
|
+
*/
|
|
36
|
+
clone(_seed?: SeedType, _opts?: Record<string, unknown>): RNG;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=crypto-rng.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto-rng.d.ts","sourceRoot":"","sources":["../../../src/random/crypto-rng.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH;;;;;;;;;;GAUG;AAEH,OAAO,GAAG,EAAE,EAAE,KAAK,QAAQ,EAAE,MAAM,UAAU,CAAC;AAE9C;;;;;GAKG;AACH,MAAM,CAAC,OAAO,OAAO,SAAU,SAAQ,GAAG;IACtC,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;OAEG;IACH,IAAI,IAAI,MAAM;IAad;;OAEG;IACH,IAAI,CAAC,KAAK,CAAC,EAAE,QAAQ,EAAE,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI;IAI7D;;OAEG;IACH,KAAK,CAAC,KAAK,CAAC,EAAE,QAAQ,EAAE,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,GAAG;CAGhE"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Elara AI Pty Ltd
|
|
3
|
+
* Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Vendored from npm package 'random' (https://github.com/transitive-bullshit/random)
|
|
7
|
+
* Originally by Travis Fischer, distributed under MIT license
|
|
8
|
+
*
|
|
9
|
+
* Ported from east-node-std's src/random/crypto-rng.ts
|
|
10
|
+
* (libs/east-node/packages/east-node-std/src/random/crypto-rng.ts), modified
|
|
11
|
+
* to draw from the web-standard `crypto.getRandomValues` where east-node-std
|
|
12
|
+
* reads `node:crypto`'s `randomBytes`. It reads the same 48 bits the same
|
|
13
|
+
* way, so the two draw from the same distribution; an unseeded draw is never
|
|
14
|
+
* reproducible on either.
|
|
15
|
+
*/
|
|
16
|
+
import RNG, {} from "./rng.js";
|
|
17
|
+
/**
|
|
18
|
+
* Cryptographically secure RNG using the web-standard crypto.getRandomValues
|
|
19
|
+
*
|
|
20
|
+
* Note: Seeding is not supported for cryptographic RNG as it uses the host's entropy source.
|
|
21
|
+
* The seed/clone methods are provided for API compatibility but have no effect.
|
|
22
|
+
*/
|
|
23
|
+
export default class CryptoRNG extends RNG {
|
|
24
|
+
get name() {
|
|
25
|
+
return "crypto";
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Generate a random number in [0, 1) using crypto.getRandomValues
|
|
29
|
+
*/
|
|
30
|
+
next() {
|
|
31
|
+
// Use 6 bytes (48 bits) for good precision
|
|
32
|
+
const bytes = globalThis.crypto.getRandomValues(new Uint8Array(6));
|
|
33
|
+
// Read them as one big-endian unsigned integer, as east-node-std's
|
|
34
|
+
// `readUIntBE(0, 6)` does; 2^48 is well inside a double's exact range
|
|
35
|
+
let value = 0;
|
|
36
|
+
for (const byte of bytes) {
|
|
37
|
+
value = value * 256 + byte;
|
|
38
|
+
}
|
|
39
|
+
// Divide by 2^48 to get [0, 1)
|
|
40
|
+
return value / 0x1000000000000;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Seeding is not supported for cryptographic RNG (no-op for API compatibility)
|
|
44
|
+
*/
|
|
45
|
+
seed(_seed, _opts) {
|
|
46
|
+
// No-op: crypto RNG uses the host's entropy source
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Clone returns a new instance (seeding not supported)
|
|
50
|
+
*/
|
|
51
|
+
clone(_seed, _opts) {
|
|
52
|
+
return new CryptoRNG();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=crypto-rng.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto-rng.js","sourceRoot":"","sources":["../../../src/random/crypto-rng.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH;;;;;;;;;;GAUG;AAEH,OAAO,GAAG,EAAE,EAAiB,MAAM,UAAU,CAAC;AAE9C;;;;;GAKG;AACH,MAAM,CAAC,OAAO,OAAO,SAAU,SAAQ,GAAG;IACtC,IAAI,IAAI;QACJ,OAAO,QAAQ,CAAC;IACpB,CAAC;IAED;;OAEG;IACH,IAAI;QACA,2CAA2C;QAC3C,MAAM,KAAK,GAAG,UAAU,CAAC,MAAM,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;QACnE,mEAAmE;QACnE,sEAAsE;QACtE,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACvB,KAAK,GAAG,KAAK,GAAG,GAAG,GAAG,IAAI,CAAC;QAC/B,CAAC;QACD,+BAA+B;QAC/B,OAAO,KAAK,GAAG,eAAe,CAAC;IACnC,CAAC;IAED;;OAEG;IACH,IAAI,CAAC,KAAgB,EAAE,KAA+B;QAClD,mDAAmD;IACvD,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,KAAgB,EAAE,KAA+B;QACnD,OAAO,IAAI,SAAS,EAAE,CAAC;IAC3B,CAAC;CACJ"}
|