@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
|
@@ -0,0 +1,412 @@
|
|
|
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, IntegerType, BooleanType, DictType, OptionType, VariantType, NullType, StructType, BlobType, SortedMap, compareFor } from "@elaraai/east";
|
|
6
|
+
import { EastError } from "@elaraai/east/internal";
|
|
7
|
+
/**
|
|
8
|
+
* HTTP method variant type.
|
|
9
|
+
*
|
|
10
|
+
* Represents the HTTP request method for fetch operations.
|
|
11
|
+
* Supports standard HTTP methods: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS.
|
|
12
|
+
*/
|
|
13
|
+
export const FetchMethod = VariantType({
|
|
14
|
+
GET: NullType,
|
|
15
|
+
POST: NullType,
|
|
16
|
+
PUT: NullType,
|
|
17
|
+
DELETE: NullType,
|
|
18
|
+
PATCH: NullType,
|
|
19
|
+
HEAD: NullType,
|
|
20
|
+
OPTIONS: NullType,
|
|
21
|
+
});
|
|
22
|
+
/**
|
|
23
|
+
* HTTP request configuration structure.
|
|
24
|
+
*
|
|
25
|
+
* Complete configuration for making HTTP requests with custom methods,
|
|
26
|
+
* headers, and optional body content.
|
|
27
|
+
*/
|
|
28
|
+
export const FetchRequestConfig = StructType({
|
|
29
|
+
url: StringType,
|
|
30
|
+
method: FetchMethod,
|
|
31
|
+
headers: DictType(StringType, StringType),
|
|
32
|
+
body: OptionType(StringType),
|
|
33
|
+
});
|
|
34
|
+
/**
|
|
35
|
+
* HTTP response structure.
|
|
36
|
+
*
|
|
37
|
+
* Complete HTTP response including status code, headers, body, and success indicator.
|
|
38
|
+
* The `ok` field is true for status codes in the 200-299 range.
|
|
39
|
+
*/
|
|
40
|
+
export const FetchResponse = StructType({
|
|
41
|
+
status: IntegerType,
|
|
42
|
+
statusText: StringType,
|
|
43
|
+
headers: DictType(StringType, StringType),
|
|
44
|
+
body: StringType,
|
|
45
|
+
ok: BooleanType,
|
|
46
|
+
});
|
|
47
|
+
/** Orders response header names as East orders its Strings. */
|
|
48
|
+
const compareHeaderNames = compareFor(StringType);
|
|
49
|
+
/**
|
|
50
|
+
* Performs an HTTP GET request and returns the response body as a string.
|
|
51
|
+
*
|
|
52
|
+
* Makes a simple GET request to the specified URL and returns the response body text.
|
|
53
|
+
* This is a convenience function for basic GET requests without custom headers.
|
|
54
|
+
* Throws an error if the response status is not in the 200-299 range.
|
|
55
|
+
*
|
|
56
|
+
* This is a platform function for the East language, enabling HTTP GET requests
|
|
57
|
+
* in East programs running in a browser, through the browser's `fetch`.
|
|
58
|
+
*
|
|
59
|
+
* @param url - The URL to fetch (must be a valid HTTP/HTTPS URL)
|
|
60
|
+
* @returns The response body as a string
|
|
61
|
+
*
|
|
62
|
+
* @throws {EastError} When request fails:
|
|
63
|
+
* - Invalid URL
|
|
64
|
+
* - Network error, or a cross-origin request the server's CORS headers refuse
|
|
65
|
+
* - Non-2xx HTTP status code
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* const fetchData = East.function([], StringType, $ => {
|
|
70
|
+
* const data = $.let(Fetch.get("https://api.example.com/data"));
|
|
71
|
+
* return data;
|
|
72
|
+
* });
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export const fetch_get = East.asyncPlatform("fetch_get", [StringType], StringType);
|
|
76
|
+
/**
|
|
77
|
+
* Performs an HTTP GET request and returns the response body as binary data.
|
|
78
|
+
*
|
|
79
|
+
* Makes a simple GET request to the specified URL and returns the response body
|
|
80
|
+
* as raw bytes (Blob/Uint8Array). Useful for downloading binary files like images,
|
|
81
|
+
* PDFs, or database files.
|
|
82
|
+
* Throws an error if the response status is not in the 200-299 range.
|
|
83
|
+
*
|
|
84
|
+
* This is a platform function for the East language, enabling binary HTTP GET requests
|
|
85
|
+
* in East programs running in a browser, through the browser's `fetch`.
|
|
86
|
+
*
|
|
87
|
+
* @param url - The URL to fetch (must be a valid HTTP/HTTPS URL)
|
|
88
|
+
* @returns The response body as binary data (Blob)
|
|
89
|
+
*
|
|
90
|
+
* @throws {EastError} When request fails:
|
|
91
|
+
* - Invalid URL
|
|
92
|
+
* - Network error, or a cross-origin request the server's CORS headers refuse
|
|
93
|
+
* - Non-2xx HTTP status code
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* const downloadFile = East.function([], BlobType, $ => {
|
|
98
|
+
* const data = $.let(Fetch.getBytes("https://example.com/file.bin"));
|
|
99
|
+
* return data;
|
|
100
|
+
* });
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export const fetch_get_bytes = East.asyncPlatform("fetch_get_bytes", [StringType], BlobType);
|
|
104
|
+
/**
|
|
105
|
+
* Performs an HTTP POST request with a string body.
|
|
106
|
+
*
|
|
107
|
+
* Makes a POST request to the specified URL with the provided string body.
|
|
108
|
+
* Sets Content-Type header to "text/plain" by default. For more control over
|
|
109
|
+
* headers and methods, use {@link fetch_request}.
|
|
110
|
+
* Throws an error if the response status is not in the 200-299 range.
|
|
111
|
+
*
|
|
112
|
+
* This is a platform function for the East language, enabling HTTP POST requests
|
|
113
|
+
* in East programs running in a browser, through the browser's `fetch`.
|
|
114
|
+
*
|
|
115
|
+
* @param url - The URL to post to (must be a valid HTTP/HTTPS URL)
|
|
116
|
+
* @param body - The request body as a string
|
|
117
|
+
* @returns The response body as a string
|
|
118
|
+
*
|
|
119
|
+
* @throws {EastError} When request fails:
|
|
120
|
+
* - Invalid URL
|
|
121
|
+
* - Network error, or a cross-origin request the server's CORS headers refuse
|
|
122
|
+
* - Non-2xx HTTP status code
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* const postData = East.function([], StringType, $ => {
|
|
127
|
+
* const response = $.let(Fetch.post("https://api.example.com/submit", "data"));
|
|
128
|
+
* return response;
|
|
129
|
+
* });
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
export const fetch_post = East.asyncPlatform("fetch_post", [StringType, StringType], StringType);
|
|
133
|
+
/**
|
|
134
|
+
* Performs a full HTTP request with custom configuration.
|
|
135
|
+
*
|
|
136
|
+
* Makes an HTTP request with complete control over method, headers, and body.
|
|
137
|
+
* Returns the full response including status, headers, and body. Unlike {@link fetch_get}
|
|
138
|
+
* and {@link fetch_post}, this does not throw on non-2xx status codes - check the `ok`
|
|
139
|
+
* field in the response instead.
|
|
140
|
+
*
|
|
141
|
+
* This is a platform function for the East language, enabling full HTTP requests
|
|
142
|
+
* in East programs running in a browser, through the browser's `fetch`.
|
|
143
|
+
*
|
|
144
|
+
* @param config - The request configuration (url, method, headers, optional body)
|
|
145
|
+
* @returns The complete HTTP response with status, headers, and body
|
|
146
|
+
*
|
|
147
|
+
* @throws {EastError} When request fails at the network level (not for HTTP errors)
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```ts
|
|
151
|
+
* const makeRequest = East.function([], FetchResponse, $ => {
|
|
152
|
+
* const config = $.let({
|
|
153
|
+
* url: "https://api.example.com/data",
|
|
154
|
+
* method: variant("GET", null),
|
|
155
|
+
* headers: new Map([["Authorization", "Bearer token"]]),
|
|
156
|
+
* body: none,
|
|
157
|
+
* }, FetchRequestConfig);
|
|
158
|
+
* return Fetch.request(config);
|
|
159
|
+
* });
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
* @remarks
|
|
163
|
+
* A browser sends a request only with the headers it allows a page to set,
|
|
164
|
+
* and a cross-origin response shows only the headers its server exposes.
|
|
165
|
+
*/
|
|
166
|
+
export const fetch_request = East.asyncPlatform("fetch_request", [FetchRequestConfig], FetchResponse);
|
|
167
|
+
/**
|
|
168
|
+
* Browser implementation of fetch platform functions.
|
|
169
|
+
*
|
|
170
|
+
* Pass this array to {@link East.compileAsync} to enable fetch operations.
|
|
171
|
+
*/
|
|
172
|
+
const FetchImpl = [
|
|
173
|
+
fetch_get.implement(async (url) => {
|
|
174
|
+
try {
|
|
175
|
+
const response = await fetch(url);
|
|
176
|
+
if (!response.ok) {
|
|
177
|
+
throw new EastError(`HTTP ${response.status}: ${response.statusText}`, {
|
|
178
|
+
location: [{ filename: "fetch_get", line: 0n, column: 0n }]
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
return await response.text();
|
|
182
|
+
}
|
|
183
|
+
catch (err) {
|
|
184
|
+
if (err instanceof EastError)
|
|
185
|
+
throw err;
|
|
186
|
+
throw new EastError(`Failed to fetch ${url}: ${err.message}`, {
|
|
187
|
+
location: [{ filename: "fetch_get", line: 0n, column: 0n }],
|
|
188
|
+
cause: err
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}),
|
|
192
|
+
fetch_get_bytes.implement(async (url) => {
|
|
193
|
+
try {
|
|
194
|
+
const response = await fetch(url);
|
|
195
|
+
if (!response.ok) {
|
|
196
|
+
throw new EastError(`HTTP ${response.status}: ${response.statusText}`, {
|
|
197
|
+
location: [{ filename: "fetch_get_bytes", line: 0n, column: 0n }]
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
201
|
+
return new Uint8Array(arrayBuffer);
|
|
202
|
+
}
|
|
203
|
+
catch (err) {
|
|
204
|
+
if (err instanceof EastError)
|
|
205
|
+
throw err;
|
|
206
|
+
throw new EastError(`Failed to fetch ${url}: ${err.message}`, {
|
|
207
|
+
location: [{ filename: "fetch_get_bytes", line: 0n, column: 0n }],
|
|
208
|
+
cause: err
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}),
|
|
212
|
+
fetch_post.implement(async (url, body) => {
|
|
213
|
+
try {
|
|
214
|
+
const response = await fetch(url, {
|
|
215
|
+
method: 'POST',
|
|
216
|
+
body: body,
|
|
217
|
+
headers: {
|
|
218
|
+
'Content-Type': 'text/plain',
|
|
219
|
+
},
|
|
220
|
+
});
|
|
221
|
+
if (!response.ok) {
|
|
222
|
+
throw new EastError(`HTTP ${response.status}: ${response.statusText}`, {
|
|
223
|
+
location: [{ filename: "fetch_post", line: 0n, column: 0n }]
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
return await response.text();
|
|
227
|
+
}
|
|
228
|
+
catch (err) {
|
|
229
|
+
if (err instanceof EastError)
|
|
230
|
+
throw err;
|
|
231
|
+
throw new EastError(`Failed to post to ${url}: ${err.message}`, {
|
|
232
|
+
location: [{ filename: "fetch_post", line: 0n, column: 0n }],
|
|
233
|
+
cause: err
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
}),
|
|
237
|
+
fetch_request.implement(async (config) => {
|
|
238
|
+
const url = config.url;
|
|
239
|
+
const method = config.method.type; // The variant's tag names the method
|
|
240
|
+
const headers = {};
|
|
241
|
+
// Convert the Dict to a plain object
|
|
242
|
+
for (const [key, value] of config.headers) {
|
|
243
|
+
headers[key] = value;
|
|
244
|
+
}
|
|
245
|
+
try {
|
|
246
|
+
const response = await fetch(url, {
|
|
247
|
+
method,
|
|
248
|
+
headers,
|
|
249
|
+
body: config.body.type === "some" ? config.body.value : null,
|
|
250
|
+
});
|
|
251
|
+
const responseHeaders = new SortedMap([], compareHeaderNames);
|
|
252
|
+
response.headers.forEach((value, key) => {
|
|
253
|
+
responseHeaders.set(key, value);
|
|
254
|
+
});
|
|
255
|
+
const body = await response.text();
|
|
256
|
+
return {
|
|
257
|
+
status: BigInt(response.status),
|
|
258
|
+
statusText: response.statusText,
|
|
259
|
+
headers: responseHeaders,
|
|
260
|
+
body,
|
|
261
|
+
ok: response.ok,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
catch (err) {
|
|
265
|
+
throw new EastError(`Failed to fetch ${url}: ${err.message}`, {
|
|
266
|
+
location: [{ filename: "fetch_request", line: 0n, column: 0n }],
|
|
267
|
+
cause: err
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
}),
|
|
271
|
+
];
|
|
272
|
+
/**
|
|
273
|
+
* Grouped fetch platform functions.
|
|
274
|
+
*
|
|
275
|
+
* Provides HTTP client operations using the browser's fetch API.
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* ```ts
|
|
279
|
+
* import { East, StringType } from "@elaraai/east";
|
|
280
|
+
* import { Fetch } from "@elaraai/east-web-std";
|
|
281
|
+
*
|
|
282
|
+
* const getData = East.function([], StringType, $ => {
|
|
283
|
+
* const response = $.let(Fetch.get("https://api.example.com/data"));
|
|
284
|
+
* return response;
|
|
285
|
+
* });
|
|
286
|
+
*
|
|
287
|
+
* const compiled = await East.compileAsync(getData.toIR(), Fetch.Implementation);
|
|
288
|
+
* await compiled(); // Returns response body as string
|
|
289
|
+
* ```
|
|
290
|
+
*/
|
|
291
|
+
export const Fetch = {
|
|
292
|
+
/**
|
|
293
|
+
* Performs an HTTP GET request and returns the response body.
|
|
294
|
+
*
|
|
295
|
+
* Makes a simple GET request to the specified URL and returns the response body text.
|
|
296
|
+
* Throws an error if the response status is not in the 200-299 range.
|
|
297
|
+
*
|
|
298
|
+
* @param url - The URL to fetch
|
|
299
|
+
* @returns The response body as a string
|
|
300
|
+
* @throws {EastError} When request fails or status is not 2xx
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
* ```ts
|
|
304
|
+
* const fetchData = East.function([], StringType, $ => {
|
|
305
|
+
* return Fetch.get("https://api.example.com/data");
|
|
306
|
+
* });
|
|
307
|
+
*
|
|
308
|
+
* const compiled = await East.compileAsync(fetchData.toIR(), Fetch.Implementation);
|
|
309
|
+
* await compiled(); // Returns: response body as string
|
|
310
|
+
* ```
|
|
311
|
+
*/
|
|
312
|
+
get: fetch_get,
|
|
313
|
+
/**
|
|
314
|
+
* Performs an HTTP GET request and returns the response body as binary data.
|
|
315
|
+
*
|
|
316
|
+
* Makes a simple GET request to the specified URL and returns the response body
|
|
317
|
+
* as raw bytes (Blob/Uint8Array). Useful for downloading binary files.
|
|
318
|
+
* Throws an error if the response status is not in the 200-299 range.
|
|
319
|
+
*
|
|
320
|
+
* @param url - The URL to fetch
|
|
321
|
+
* @returns The response body as binary data (Blob)
|
|
322
|
+
* @throws {EastError} When request fails or status is not 2xx
|
|
323
|
+
*
|
|
324
|
+
* @example
|
|
325
|
+
* ```ts
|
|
326
|
+
* const downloadFile = East.function([], BlobType, $ => {
|
|
327
|
+
* return Fetch.getBytes("https://example.com/file.bin");
|
|
328
|
+
* });
|
|
329
|
+
*
|
|
330
|
+
* const compiled = await East.compileAsync(downloadFile.toIR(), Fetch.Implementation);
|
|
331
|
+
* await compiled(); // Returns: binary data as Uint8Array
|
|
332
|
+
* ```
|
|
333
|
+
*/
|
|
334
|
+
getBytes: fetch_get_bytes,
|
|
335
|
+
/**
|
|
336
|
+
* Performs an HTTP POST request with a string body.
|
|
337
|
+
*
|
|
338
|
+
* Makes a POST request to the specified URL with the provided string body.
|
|
339
|
+
* Sets Content-Type to "text/plain" by default.
|
|
340
|
+
* Throws an error if the response status is not in the 200-299 range.
|
|
341
|
+
*
|
|
342
|
+
* @param url - The URL to post to
|
|
343
|
+
* @param body - The request body as a string
|
|
344
|
+
* @returns The response body as a string
|
|
345
|
+
* @throws {EastError} When request fails or status is not 2xx
|
|
346
|
+
*
|
|
347
|
+
* @example
|
|
348
|
+
* ```ts
|
|
349
|
+
* const postData = East.function([], StringType, $ => {
|
|
350
|
+
* return Fetch.post("https://api.example.com/submit", "data");
|
|
351
|
+
* });
|
|
352
|
+
*
|
|
353
|
+
* const compiled = await East.compileAsync(postData.toIR(), Fetch.Implementation);
|
|
354
|
+
* await compiled(); // Returns: response body as string
|
|
355
|
+
* ```
|
|
356
|
+
*/
|
|
357
|
+
post: fetch_post,
|
|
358
|
+
/**
|
|
359
|
+
* Performs a full HTTP request with custom configuration.
|
|
360
|
+
*
|
|
361
|
+
* Makes an HTTP request with complete control over method, headers, and body.
|
|
362
|
+
* Returns the full response including status, headers, and body.
|
|
363
|
+
* Does not throw on non-2xx status - check the `ok` field instead.
|
|
364
|
+
*
|
|
365
|
+
* @param config - The request configuration
|
|
366
|
+
* @returns The complete HTTP response
|
|
367
|
+
* @throws {EastError} When request fails at the network level
|
|
368
|
+
*
|
|
369
|
+
* @example
|
|
370
|
+
* ```ts
|
|
371
|
+
* const makeRequest = East.function([], FetchResponse, $ => {
|
|
372
|
+
* const config = $.let({
|
|
373
|
+
* url: "https://api.example.com/data",
|
|
374
|
+
* method: variant("GET", null),
|
|
375
|
+
* headers: new Map(),
|
|
376
|
+
* body: none,
|
|
377
|
+
* }, FetchRequestConfig);
|
|
378
|
+
* return Fetch.request(config);
|
|
379
|
+
* });
|
|
380
|
+
*
|
|
381
|
+
* const compiled = await East.compileAsync(makeRequest.toIR(), Fetch.Implementation);
|
|
382
|
+
* const response = await compiled(); // Returns: FetchResponse object
|
|
383
|
+
* ```
|
|
384
|
+
*/
|
|
385
|
+
request: fetch_request,
|
|
386
|
+
/**
|
|
387
|
+
* Browser implementation of fetch platform functions.
|
|
388
|
+
*
|
|
389
|
+
* Pass this to {@link East.compileAsync} to enable fetch operations.
|
|
390
|
+
*/
|
|
391
|
+
Implementation: FetchImpl,
|
|
392
|
+
/**
|
|
393
|
+
* Type definitions for fetch operations.
|
|
394
|
+
*/
|
|
395
|
+
Types: {
|
|
396
|
+
/**
|
|
397
|
+
* HTTP method variant type (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS).
|
|
398
|
+
*/
|
|
399
|
+
Method: FetchMethod,
|
|
400
|
+
/**
|
|
401
|
+
* HTTP request configuration structure.
|
|
402
|
+
*/
|
|
403
|
+
RequestConfig: FetchRequestConfig,
|
|
404
|
+
/**
|
|
405
|
+
* HTTP response structure.
|
|
406
|
+
*/
|
|
407
|
+
Response: FetchResponse,
|
|
408
|
+
},
|
|
409
|
+
};
|
|
410
|
+
// Exported beside `Fetch.Implementation`, as east-node-std exports it
|
|
411
|
+
export { FetchImpl };
|
|
412
|
+
//# sourceMappingURL=fetch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../../src/fetch.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,WAAW,EAAE,WAAW,EAAE,QAAQ,EAAE,UAAU,EAAE,WAAW,EAAE,QAAQ,EAAE,UAAU,EAAE,QAAQ,EAAoB,SAAS,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAEvL,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEnD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,WAAW,CAAC;IACnC,GAAG,EAAE,QAAQ;IACb,IAAI,EAAE,QAAQ;IACd,GAAG,EAAE,QAAQ;IACb,MAAM,EAAE,QAAQ;IAChB,KAAK,EAAE,QAAQ;IACf,IAAI,EAAE,QAAQ;IACd,OAAO,EAAE,QAAQ;CACpB,CAAC,CAAC;AAEH;;;;;GAKG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,UAAU,CAA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;gBACxB,IAAI;gBACJ,EAAE,EAAE,QAAQ,CAAC,EAAE;aAClB,CAAC;QACN,CAAC;QAAC,OAAO,GAAQ,EAAE,CAAC;YAChB,MAAM,IAAI,SAAS,CAAC,mBAAmB,GAAG,KAAK,GAAG,CAAC,OAAO,EAAE,EAAE;gBAC1D,QAAQ,EAAE,CAAC,EAAE,QAAQ,EAAE,eAAe,EAAE,IAAI,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC;gBAC/D,KAAK,EAAE,GAAG;aACb,CAAC,CAAC;QACP,CAAC;IACL,CAAC,CAAC;CACL,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG;IACjB;;;;;;;;;;;;;;;;;;;OAmBG;IACH,GAAG,EAAE,SAAS;IAEd;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,QAAQ,EAAE,eAAe;IAEzB;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,IAAI,EAAE,UAAU;IAEhB;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACH,OAAO,EAAE,aAAa;IAEtB;;;;OAIG;IACH,cAAc,EAAE,SAAS;IAEzB;;OAEG;IACH,KAAK,EAAE;QACH;;WAEG;QACH,MAAM,EAAE,WAAW;QAEnB;;WAEG;QACH,aAAa,EAAE,kBAAkB;QAEjC;;WAEG;QACH,QAAQ,EAAE,aAAa;KAC1B;CACK,CAAC;AAEX,sEAAsE;AACtE,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -0,0 +1,106 @@
|
|
|
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
|
+
* East Web - the standard platform functions for East programs in a browser.
|
|
7
|
+
*
|
|
8
|
+
* The same platform functions, under the same names and East types, as
|
|
9
|
+
* east-node-std's Console, Time, Random, Crypto, Fetch, Path and Test, so an
|
|
10
|
+
* East program written for Node runs in a browser unchanged. It reaches no
|
|
11
|
+
* Node module: only web-standard globals.
|
|
12
|
+
*
|
|
13
|
+
* FileSystem, Env and the large-JSON reader have no browser meaning and are
|
|
14
|
+
* not provided: a program that calls one fails to compile, naming the
|
|
15
|
+
* platform function it called.
|
|
16
|
+
*
|
|
17
|
+
* @packageDocumentation
|
|
18
|
+
*/
|
|
19
|
+
import type { PlatformFunction } from "@elaraai/east/internal";
|
|
20
|
+
export * from "./test.js";
|
|
21
|
+
export * from "./console.js";
|
|
22
|
+
export * from "./path.js";
|
|
23
|
+
export * from "./crypto.js";
|
|
24
|
+
export * from "./time.js";
|
|
25
|
+
export * from "./fetch.js";
|
|
26
|
+
export * from "./random.js";
|
|
27
|
+
import { type ConsoleSink } from "./console.js";
|
|
28
|
+
import { type TestHost } from "./test.js";
|
|
29
|
+
/**
|
|
30
|
+
* What a host gives the platform it runs a program on.
|
|
31
|
+
*/
|
|
32
|
+
export interface WebPlatformOptions {
|
|
33
|
+
/**
|
|
34
|
+
* Where the program's console output goes: the host's standard output and
|
|
35
|
+
* standard error. Defaults to the global `console`
|
|
36
|
+
* ({@link globalConsoleSink}).
|
|
37
|
+
*/
|
|
38
|
+
console?: ConsoleSink;
|
|
39
|
+
/**
|
|
40
|
+
* Runs the suites and tests an East test program declares. Defaults to
|
|
41
|
+
* running them in place (see {@link TestImpl}).
|
|
42
|
+
*/
|
|
43
|
+
test?: TestHost;
|
|
44
|
+
/**
|
|
45
|
+
* Stops the program's waits, for a host that stops a program without
|
|
46
|
+
* ending its thread: once it aborts, a `Time.sleep` under way fails at
|
|
47
|
+
* once, its timer cleared — nothing the program left keeps its thread's
|
|
48
|
+
* event loop alive — and a later one fails as it starts. Unset, a sleep
|
|
49
|
+
* ends once its time has passed, or with its thread.
|
|
50
|
+
*/
|
|
51
|
+
signal?: AbortSignal;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Creates a complete browser platform for one program.
|
|
55
|
+
*
|
|
56
|
+
* The platform holds east-node-std's platform functions that mean the same in
|
|
57
|
+
* a browser — Console, Path, Crypto, Time, Fetch, Random and Test — writing
|
|
58
|
+
* console output to the host's sink and running tests through its host, its
|
|
59
|
+
* sleeps stopped by the host's signal when it gives one. Random starts
|
|
60
|
+
* unseeded, with a generator no other platform shares.
|
|
61
|
+
*
|
|
62
|
+
* @param options - The host's console sink, test host and signal
|
|
63
|
+
* @returns The platform functions, to pass to `compile()` or `compileAsync()`
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* import { East, NullType } from "@elaraai/east";
|
|
68
|
+
* import { Console, createWebPlatform } from "@elaraai/east-web-std";
|
|
69
|
+
*
|
|
70
|
+
* let stdout = "";
|
|
71
|
+
* const fn = East.asyncFunction([], NullType, $ => {
|
|
72
|
+
* $(Console.log("Hello from a browser"));
|
|
73
|
+
* });
|
|
74
|
+
*
|
|
75
|
+
* const platform = createWebPlatform({
|
|
76
|
+
* console: { stdout: text => { stdout += text; }, stderr: text => { stdout += text; } },
|
|
77
|
+
* });
|
|
78
|
+
* await fn.toIR().compile(platform)(); // stdout is now "Hello from a browser\n"
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export declare function createWebPlatform(options?: WebPlatformOptions): PlatformFunction[];
|
|
82
|
+
/**
|
|
83
|
+
* Complete browser platform implementation.
|
|
84
|
+
*
|
|
85
|
+
* Pass this array to `compile()` or `compileAsync()` to enable all the
|
|
86
|
+
* browser's platform functions. Console output goes to the global `console`,
|
|
87
|
+
* tests run in place, and every program compiled with it shares one Random
|
|
88
|
+
* generator; use {@link createWebPlatform} to give a program its own.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```ts
|
|
92
|
+
* import { East, NullType } from "@elaraai/east";
|
|
93
|
+
* import { WebPlatform, Console, Crypto } from "@elaraai/east-web-std";
|
|
94
|
+
*
|
|
95
|
+
* const fn = East.asyncFunction([], NullType, $ => {
|
|
96
|
+
* const id = $.let(Crypto.uuid());
|
|
97
|
+
* $(Console.log(id));
|
|
98
|
+
* });
|
|
99
|
+
*
|
|
100
|
+
* // An async East function, since Time.sleep and Fetch are async
|
|
101
|
+
* const compiled = fn.toIR().compile(WebPlatform);
|
|
102
|
+
* await compiled();
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export declare const WebPlatform: PlatformFunction[];
|
|
106
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAG/D,cAAc,WAAW,CAAC;AAG1B,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAG5B,OAAO,EAAqD,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAMnG,OAAO,EAA4B,KAAK,QAAQ,EAAE,MAAM,WAAW,CAAC;AAEpE;;GAEG;AACH,MAAM,WAAW,kBAAkB;IAC/B;;;;OAIG;IACH,OAAO,CAAC,EAAE,WAAW,CAAC;IAEtB;;;OAGG;IACH,IAAI,CAAC,EAAE,QAAQ,CAAC;IAEhB;;;;;;OAMG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;CACxB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,GAAE,kBAAuB,GAAG,gBAAgB,EAAE,CAUtF;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,WAAW,EAAE,gBAAgB,EAQzC,CAAC"}
|
|
@@ -0,0 +1,93 @@
|
|
|
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
|
+
// Export test utilities
|
|
6
|
+
export * from "./test.js";
|
|
7
|
+
// Export platform function definitions and implementations
|
|
8
|
+
export * from "./console.js";
|
|
9
|
+
export * from "./path.js";
|
|
10
|
+
export * from "./crypto.js";
|
|
11
|
+
export * from "./time.js";
|
|
12
|
+
export * from "./fetch.js";
|
|
13
|
+
export * from "./random.js";
|
|
14
|
+
// Import implementations for combined export
|
|
15
|
+
import { ConsoleImpl, createConsoleImpl, globalConsoleSink } from "./console.js";
|
|
16
|
+
import { PathImpl } from "./path.js";
|
|
17
|
+
import { CryptoImpl } from "./crypto.js";
|
|
18
|
+
import { TimeImpl, createTimeImpl } from "./time.js";
|
|
19
|
+
import { FetchImpl } from "./fetch.js";
|
|
20
|
+
import { RandomImpl, createRandomImpl } from "./random.js";
|
|
21
|
+
import { TestImpl, createTestImpl } from "./test.js";
|
|
22
|
+
/**
|
|
23
|
+
* Creates a complete browser platform for one program.
|
|
24
|
+
*
|
|
25
|
+
* The platform holds east-node-std's platform functions that mean the same in
|
|
26
|
+
* a browser — Console, Path, Crypto, Time, Fetch, Random and Test — writing
|
|
27
|
+
* console output to the host's sink and running tests through its host, its
|
|
28
|
+
* sleeps stopped by the host's signal when it gives one. Random starts
|
|
29
|
+
* unseeded, with a generator no other platform shares.
|
|
30
|
+
*
|
|
31
|
+
* @param options - The host's console sink, test host and signal
|
|
32
|
+
* @returns The platform functions, to pass to `compile()` or `compileAsync()`
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { East, NullType } from "@elaraai/east";
|
|
37
|
+
* import { Console, createWebPlatform } from "@elaraai/east-web-std";
|
|
38
|
+
*
|
|
39
|
+
* let stdout = "";
|
|
40
|
+
* const fn = East.asyncFunction([], NullType, $ => {
|
|
41
|
+
* $(Console.log("Hello from a browser"));
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* const platform = createWebPlatform({
|
|
45
|
+
* console: { stdout: text => { stdout += text; }, stderr: text => { stdout += text; } },
|
|
46
|
+
* });
|
|
47
|
+
* await fn.toIR().compile(platform)(); // stdout is now "Hello from a browser\n"
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export function createWebPlatform(options = {}) {
|
|
51
|
+
return [
|
|
52
|
+
...createConsoleImpl(options.console ?? globalConsoleSink),
|
|
53
|
+
...PathImpl,
|
|
54
|
+
...CryptoImpl,
|
|
55
|
+
...createTimeImpl(options.signal),
|
|
56
|
+
...FetchImpl,
|
|
57
|
+
...createRandomImpl(),
|
|
58
|
+
...createTestImpl(options.test),
|
|
59
|
+
];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Complete browser platform implementation.
|
|
63
|
+
*
|
|
64
|
+
* Pass this array to `compile()` or `compileAsync()` to enable all the
|
|
65
|
+
* browser's platform functions. Console output goes to the global `console`,
|
|
66
|
+
* tests run in place, and every program compiled with it shares one Random
|
|
67
|
+
* generator; use {@link createWebPlatform} to give a program its own.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* import { East, NullType } from "@elaraai/east";
|
|
72
|
+
* import { WebPlatform, Console, Crypto } from "@elaraai/east-web-std";
|
|
73
|
+
*
|
|
74
|
+
* const fn = East.asyncFunction([], NullType, $ => {
|
|
75
|
+
* const id = $.let(Crypto.uuid());
|
|
76
|
+
* $(Console.log(id));
|
|
77
|
+
* });
|
|
78
|
+
*
|
|
79
|
+
* // An async East function, since Time.sleep and Fetch are async
|
|
80
|
+
* const compiled = fn.toIR().compile(WebPlatform);
|
|
81
|
+
* await compiled();
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export const WebPlatform = [
|
|
85
|
+
...ConsoleImpl,
|
|
86
|
+
...PathImpl,
|
|
87
|
+
...CryptoImpl,
|
|
88
|
+
...TimeImpl,
|
|
89
|
+
...FetchImpl,
|
|
90
|
+
...RandomImpl,
|
|
91
|
+
...TestImpl,
|
|
92
|
+
];
|
|
93
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAmBH,wBAAwB;AACxB,cAAc,WAAW,CAAC;AAE1B,2DAA2D;AAC3D,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAE5B,6CAA6C;AAC7C,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,iBAAiB,EAAoB,MAAM,cAAc,CAAC;AACnG,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAiB,MAAM,WAAW,CAAC;AA6BpE;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,iBAAiB,CAAC,UAA8B,EAAE;IAC9D,OAAO;QACH,GAAG,iBAAiB,CAAC,OAAO,CAAC,OAAO,IAAI,iBAAiB,CAAC;QAC1D,GAAG,QAAQ;QACX,GAAG,UAAU;QACb,GAAG,cAAc,CAAC,OAAO,CAAC,MAAM,CAAC;QACjC,GAAG,SAAS;QACZ,GAAG,gBAAgB,EAAE;QACrB,GAAG,cAAc,CAAC,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