@elaraai/east-node-std 0.0.1-beta.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.
- package/CLA.md +26 -0
- package/CONTRIBUTING.md +28 -0
- package/README.md +135 -0
- package/dist/console.d.ts +192 -0
- package/dist/console.d.ts.map +1 -0
- package/dist/console.js +228 -0
- package/dist/console.js.map +1 -0
- package/dist/crypto.d.ts +212 -0
- package/dist/crypto.d.ts.map +1 -0
- package/dist/crypto.js +259 -0
- package/dist/crypto.js.map +1 -0
- package/dist/fetch.d.ts +310 -0
- package/dist/fetch.d.ts.map +1 -0
- package/dist/fetch.js +336 -0
- package/dist/fetch.js.map +1 -0
- package/dist/fs.d.ts +520 -0
- package/dist/fs.d.ts.map +1 -0
- package/dist/fs.js +630 -0
- package/dist/fs.js.map +1 -0
- package/dist/index.d.ts +59 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +86 -0
- package/dist/index.js.map +1 -0
- package/dist/path.d.ts +241 -0
- package/dist/path.d.ts.map +1 -0
- package/dist/path.js +299 -0
- package/dist/path.js.map +1 -0
- package/dist/random/crypto-rng.d.ts +31 -0
- package/dist/random/crypto-rng.d.ts.map +1 -0
- package/dist/random/crypto-rng.js +44 -0
- package/dist/random/crypto-rng.js.map +1 -0
- package/dist/random/distributions/bates.d.ts +12 -0
- package/dist/random/distributions/bates.d.ts.map +1 -0
- package/dist/random/distributions/bates.js +15 -0
- package/dist/random/distributions/bates.js.map +1 -0
- package/dist/random/distributions/bernoulli.d.ts +12 -0
- package/dist/random/distributions/bernoulli.d.ts.map +1 -0
- package/dist/random/distributions/bernoulli.js +13 -0
- package/dist/random/distributions/bernoulli.js.map +1 -0
- package/dist/random/distributions/binomial.d.ts +12 -0
- package/dist/random/distributions/binomial.d.ts.map +1 -0
- package/dist/random/distributions/binomial.js +24 -0
- package/dist/random/distributions/binomial.js.map +1 -0
- package/dist/random/distributions/exponential.d.ts +12 -0
- package/dist/random/distributions/exponential.d.ts.map +1 -0
- package/dist/random/distributions/exponential.js +13 -0
- package/dist/random/distributions/exponential.js.map +1 -0
- package/dist/random/distributions/geometric.d.ts +12 -0
- package/dist/random/distributions/geometric.d.ts.map +1 -0
- package/dist/random/distributions/geometric.js +14 -0
- package/dist/random/distributions/geometric.js.map +1 -0
- package/dist/random/distributions/irwin-hall.d.ts +12 -0
- package/dist/random/distributions/irwin-hall.d.ts.map +1 -0
- package/dist/random/distributions/irwin-hall.js +18 -0
- package/dist/random/distributions/irwin-hall.js.map +1 -0
- package/dist/random/distributions/log-normal.d.ts +12 -0
- package/dist/random/distributions/log-normal.d.ts.map +1 -0
- package/dist/random/distributions/log-normal.js +12 -0
- package/dist/random/distributions/log-normal.js.map +1 -0
- package/dist/random/distributions/normal.d.ts +13 -0
- package/dist/random/distributions/normal.d.ts.map +1 -0
- package/dist/random/distributions/normal.js +16 -0
- package/dist/random/distributions/normal.js.map +1 -0
- package/dist/random/distributions/pareto.d.ts +12 -0
- package/dist/random/distributions/pareto.d.ts.map +1 -0
- package/dist/random/distributions/pareto.js +14 -0
- package/dist/random/distributions/pareto.js.map +1 -0
- package/dist/random/distributions/poisson.d.ts +12 -0
- package/dist/random/distributions/poisson.d.ts.map +1 -0
- package/dist/random/distributions/poisson.js +81 -0
- package/dist/random/distributions/poisson.js.map +1 -0
- package/dist/random/distributions/uniform-int.d.ts +12 -0
- package/dist/random/distributions/uniform-int.d.ts.map +1 -0
- package/dist/random/distributions/uniform-int.js +16 -0
- package/dist/random/distributions/uniform-int.js.map +1 -0
- package/dist/random/distributions/uniform.d.ts +12 -0
- package/dist/random/distributions/uniform.d.ts.map +1 -0
- package/dist/random/distributions/uniform.js +10 -0
- package/dist/random/distributions/uniform.js.map +1 -0
- package/dist/random/rng.d.ts +17 -0
- package/dist/random/rng.d.ts.map +1 -0
- package/dist/random/rng.js +7 -0
- package/dist/random/rng.js.map +1 -0
- package/dist/random/xorshift128.d.ts +35 -0
- package/dist/random/xorshift128.d.ts.map +1 -0
- package/dist/random/xorshift128.js +112 -0
- package/dist/random/xorshift128.js.map +1 -0
- package/dist/random.d.ts +773 -0
- package/dist/random.d.ts.map +1 -0
- package/dist/random.js +934 -0
- package/dist/random.js.map +1 -0
- package/dist/test.d.ts +253 -0
- package/dist/test.d.ts.map +1 -0
- package/dist/test.js +407 -0
- package/dist/test.js.map +1 -0
- package/dist/time.d.ts +130 -0
- package/dist/time.d.ts.map +1 -0
- package/dist/time.js +154 -0
- package/dist/time.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +84 -0
package/dist/crypto.d.ts
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
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 { StringType, IntegerType, BlobType } from "@elaraai/east";
|
|
6
|
+
import type { PlatformFunction, PlatformFunctionDef } from "@elaraai/east/internal";
|
|
7
|
+
/**
|
|
8
|
+
* Generates cryptographically secure random bytes.
|
|
9
|
+
*
|
|
10
|
+
* Produces random bytes using a cryptographically secure pseudo-random number generator (CSPRNG).
|
|
11
|
+
* Suitable for generating encryption keys, initialization vectors, tokens, and other security-sensitive
|
|
12
|
+
* random data.
|
|
13
|
+
*
|
|
14
|
+
* This is a platform function for the East language, enabling secure random generation
|
|
15
|
+
* in East programs running on Node.js.
|
|
16
|
+
*
|
|
17
|
+
* @param length - The number of random bytes to generate (must be positive)
|
|
18
|
+
* @returns Random bytes as a Blob (Uint8Array)
|
|
19
|
+
*
|
|
20
|
+
* @throws {EastError} When random generation fails:
|
|
21
|
+
* - Negative or invalid length
|
|
22
|
+
* - System entropy pool unavailable
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const generateToken = East.function([], BlobType, $ => {
|
|
27
|
+
* return Crypto.randomBytes(32n); // 256-bit token
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare const crypto_random_bytes: PlatformFunctionDef<[typeof IntegerType], typeof BlobType>;
|
|
32
|
+
/**
|
|
33
|
+
* Computes SHA-256 hash of a string.
|
|
34
|
+
*
|
|
35
|
+
* Calculates the SHA-256 cryptographic hash of a UTF-8 encoded string and returns
|
|
36
|
+
* the result as a lowercase hexadecimal string (64 characters). SHA-256 is a one-way
|
|
37
|
+
* hash function commonly used for data integrity verification.
|
|
38
|
+
*
|
|
39
|
+
* This is a platform function for the East language, enabling cryptographic hashing
|
|
40
|
+
* in East programs running on Node.js.
|
|
41
|
+
*
|
|
42
|
+
* @param data - The string to hash (will be UTF-8 encoded)
|
|
43
|
+
* @returns The SHA-256 hash as a lowercase hexadecimal string (64 characters)
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* const hashPassword = East.function([StringType], StringType, ($, password) => {
|
|
48
|
+
* return Crypto.hashSha256(password);
|
|
49
|
+
* // Returns: "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"
|
|
50
|
+
* });
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare const crypto_hash_sha256: PlatformFunctionDef<[typeof StringType], typeof StringType>;
|
|
54
|
+
/**
|
|
55
|
+
* Computes SHA-256 hash of binary data.
|
|
56
|
+
*
|
|
57
|
+
* Calculates the SHA-256 cryptographic hash of binary data and returns the result
|
|
58
|
+
* as raw bytes (32 bytes / 256 bits). This is the binary variant of {@link crypto_hash_sha256},
|
|
59
|
+
* useful when working with binary protocols or when you need the raw hash bytes.
|
|
60
|
+
*
|
|
61
|
+
* This is a platform function for the East language, enabling cryptographic hashing
|
|
62
|
+
* in East programs running on Node.js.
|
|
63
|
+
*
|
|
64
|
+
* @param data - The binary data to hash (Blob/Uint8Array)
|
|
65
|
+
* @returns The SHA-256 hash as binary data (32 bytes)
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* const hashFile = East.function([BlobType], BlobType, ($, fileData) => {
|
|
70
|
+
* return Crypto.hashSha256Bytes(fileData);
|
|
71
|
+
* // Returns: 32 bytes of hash data
|
|
72
|
+
* });
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export declare const crypto_hash_sha256_bytes: PlatformFunctionDef<[typeof BlobType], typeof BlobType>;
|
|
76
|
+
/**
|
|
77
|
+
* Generates a random UUID v4.
|
|
78
|
+
*
|
|
79
|
+
* Creates a version 4 UUID (Universally Unique Identifier) using cryptographically
|
|
80
|
+
* secure random numbers. UUIDs are 128-bit identifiers formatted as 36-character
|
|
81
|
+
* strings (e.g., "550e8400-e29b-41d4-a716-446655440000").
|
|
82
|
+
*
|
|
83
|
+
* This is a platform function for the East language, enabling UUID generation
|
|
84
|
+
* in East programs running on Node.js.
|
|
85
|
+
*
|
|
86
|
+
* @returns A UUID v4 string in standard format (8-4-4-4-12 hex digits)
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* const createRecord = East.function([], StringType, $ => {
|
|
91
|
+
* const id = $.let(Crypto.uuid());
|
|
92
|
+
* return id;
|
|
93
|
+
* // Returns: "550e8400-e29b-41d4-a716-446655440000"
|
|
94
|
+
* });
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export declare const crypto_uuid: PlatformFunctionDef<[], typeof StringType>;
|
|
98
|
+
/**
|
|
99
|
+
* Node.js implementation of cryptographic platform functions.
|
|
100
|
+
*
|
|
101
|
+
* Pass this array to {@link East.compile} to enable crypto operations.
|
|
102
|
+
*/
|
|
103
|
+
declare const CryptoImpl: PlatformFunction[];
|
|
104
|
+
/**
|
|
105
|
+
* Grouped cryptographic platform functions.
|
|
106
|
+
*
|
|
107
|
+
* Provides cryptographic operations for East programs.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* import { East, StringType } from "@elaraai/east";
|
|
112
|
+
* import { Crypto } from "@elaraai/east-node-std";
|
|
113
|
+
*
|
|
114
|
+
* const generateId = East.function([], StringType, $ => {
|
|
115
|
+
* return Crypto.uuid();
|
|
116
|
+
* });
|
|
117
|
+
*
|
|
118
|
+
* const compiled = East.compile(generateId.toIR(), Crypto.Implementation);
|
|
119
|
+
* compiled(); // "550e8400-e29b-41d4-a716-446655440000"
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
export declare const Crypto: {
|
|
123
|
+
/**
|
|
124
|
+
* Generates cryptographically secure random bytes.
|
|
125
|
+
*
|
|
126
|
+
* Produces random bytes using a CSPRNG, suitable for generating encryption keys,
|
|
127
|
+
* tokens, and other security-sensitive random data.
|
|
128
|
+
*
|
|
129
|
+
* @param length - The number of random bytes to generate (must be positive)
|
|
130
|
+
* @returns Random bytes as a Blob (Uint8Array)
|
|
131
|
+
* @throws {EastError} When random generation fails
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```ts
|
|
135
|
+
* const generateToken = East.function([], BlobType, $ => {
|
|
136
|
+
* return Crypto.randomBytes(32n);
|
|
137
|
+
* });
|
|
138
|
+
*
|
|
139
|
+
* const compiled = East.compile(generateToken.toIR(), Crypto.Implementation);
|
|
140
|
+
* compiled(); // Uint8Array(32) [...]
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
readonly randomBytes: PlatformFunctionDef<[IntegerType], BlobType>;
|
|
144
|
+
/**
|
|
145
|
+
* Computes SHA-256 hash of a string.
|
|
146
|
+
*
|
|
147
|
+
* Calculates the SHA-256 cryptographic hash of a UTF-8 encoded string and returns
|
|
148
|
+
* the result as a lowercase hexadecimal string (64 characters).
|
|
149
|
+
*
|
|
150
|
+
* @param data - The string to hash (will be UTF-8 encoded)
|
|
151
|
+
* @returns The SHA-256 hash as a lowercase hexadecimal string (64 characters)
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* ```ts
|
|
155
|
+
* const hashPassword = East.function([StringType], StringType, ($, password) => {
|
|
156
|
+
* return Crypto.hashSha256(password);
|
|
157
|
+
* });
|
|
158
|
+
*
|
|
159
|
+
* const compiled = East.compile(hashPassword.toIR(), Crypto.Implementation);
|
|
160
|
+
* compiled("password"); // "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
readonly hashSha256: PlatformFunctionDef<[StringType], StringType>;
|
|
164
|
+
/**
|
|
165
|
+
* Computes SHA-256 hash of binary data.
|
|
166
|
+
*
|
|
167
|
+
* Calculates the SHA-256 cryptographic hash of binary data and returns the result
|
|
168
|
+
* as raw bytes (32 bytes).
|
|
169
|
+
*
|
|
170
|
+
* @param data - The binary data to hash (Blob/Uint8Array)
|
|
171
|
+
* @returns The SHA-256 hash as binary data (32 bytes)
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* const hashFile = East.function([BlobType], BlobType, ($, fileData) => {
|
|
176
|
+
* return Crypto.hashSha256Bytes(fileData);
|
|
177
|
+
* });
|
|
178
|
+
*
|
|
179
|
+
* const compiled = East.compile(hashFile.toIR(), Crypto.Implementation);
|
|
180
|
+
* const fileData = new Uint8Array([1, 2, 3]);
|
|
181
|
+
* compiled(fileData); // Uint8Array(32) [...]
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
readonly hashSha256Bytes: PlatformFunctionDef<[BlobType], BlobType>;
|
|
185
|
+
/**
|
|
186
|
+
* Generates a random UUID v4.
|
|
187
|
+
*
|
|
188
|
+
* Creates a version 4 UUID using cryptographically secure random numbers.
|
|
189
|
+
* Returns a 36-character string in standard format.
|
|
190
|
+
*
|
|
191
|
+
* @returns A UUID v4 string in standard format (8-4-4-4-12 hex digits)
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```ts
|
|
195
|
+
* const createRecord = East.function([], StringType, $ => {
|
|
196
|
+
* return Crypto.uuid();
|
|
197
|
+
* });
|
|
198
|
+
*
|
|
199
|
+
* const compiled = East.compile(createRecord.toIR(), Crypto.Implementation);
|
|
200
|
+
* compiled(); // "550e8400-e29b-41d4-a716-446655440000"
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
readonly uuid: PlatformFunctionDef<[], StringType>;
|
|
204
|
+
/**
|
|
205
|
+
* Node.js implementation of cryptographic platform functions.
|
|
206
|
+
*
|
|
207
|
+
* Pass this to {@link East.compile} to enable crypto operations.
|
|
208
|
+
*/
|
|
209
|
+
readonly Implementation: PlatformFunction[];
|
|
210
|
+
};
|
|
211
|
+
export { CryptoImpl };
|
|
212
|
+
//# sourceMappingURL=crypto.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAQ,UAAU,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACxE,OAAO,KAAK,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAIpF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,mBAAmB,EAAE,mBAAmB,CAAC,CAAC,OAAO,WAAW,CAAC,EAAE,OAAO,QAAQ,CAAiE,CAAC;AAE7J;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,kBAAkB,EAAE,mBAAmB,CAAC,CAAC,OAAO,UAAU,CAAC,EAAE,OAAO,UAAU,CAAiE,CAAC;AAE7J;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,wBAAwB,EAAE,mBAAmB,CAAC,CAAC,OAAO,QAAQ,CAAC,EAAE,OAAO,QAAQ,CAAmE,CAAC;AAEjK;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,WAAW,EAAE,mBAAmB,CAAC,EAAE,EAAE,OAAO,UAAU,CAAgD,CAAC;AAEpH;;;;GAIG;AACH,QAAA,MAAM,UAAU,EAAE,gBAAgB,EAyCjC,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,MAAM;IACf;;;;;;;;;;;;;;;;;;;OAmBG;;IAGH;;;;;;;;;;;;;;;;;;OAkBG;;IAGH;;;;;;;;;;;;;;;;;;;OAmBG;;IAGH;;;;;;;;;;;;;;;;;OAiBG;;IAGH;;;;OAIG;;CAEG,CAAC;AAGX,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
package/dist/crypto.js
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
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, BlobType } from "@elaraai/east";
|
|
6
|
+
import { EastError } from "@elaraai/east/internal";
|
|
7
|
+
import { randomBytes, createHash, randomUUID } from "node:crypto";
|
|
8
|
+
/**
|
|
9
|
+
* Generates cryptographically secure random bytes.
|
|
10
|
+
*
|
|
11
|
+
* Produces random bytes using a cryptographically secure pseudo-random number generator (CSPRNG).
|
|
12
|
+
* Suitable for generating encryption keys, initialization vectors, tokens, and other security-sensitive
|
|
13
|
+
* random data.
|
|
14
|
+
*
|
|
15
|
+
* This is a platform function for the East language, enabling secure random generation
|
|
16
|
+
* in East programs running on Node.js.
|
|
17
|
+
*
|
|
18
|
+
* @param length - The number of random bytes to generate (must be positive)
|
|
19
|
+
* @returns Random bytes as a Blob (Uint8Array)
|
|
20
|
+
*
|
|
21
|
+
* @throws {EastError} When random generation fails:
|
|
22
|
+
* - Negative or invalid length
|
|
23
|
+
* - System entropy pool unavailable
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const generateToken = East.function([], BlobType, $ => {
|
|
28
|
+
* return Crypto.randomBytes(32n); // 256-bit token
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export const crypto_random_bytes = East.platform("crypto_random_bytes", [IntegerType], BlobType);
|
|
33
|
+
/**
|
|
34
|
+
* Computes SHA-256 hash of a string.
|
|
35
|
+
*
|
|
36
|
+
* Calculates the SHA-256 cryptographic hash of a UTF-8 encoded string and returns
|
|
37
|
+
* the result as a lowercase hexadecimal string (64 characters). SHA-256 is a one-way
|
|
38
|
+
* hash function commonly used for data integrity verification.
|
|
39
|
+
*
|
|
40
|
+
* This is a platform function for the East language, enabling cryptographic hashing
|
|
41
|
+
* in East programs running on Node.js.
|
|
42
|
+
*
|
|
43
|
+
* @param data - The string to hash (will be UTF-8 encoded)
|
|
44
|
+
* @returns The SHA-256 hash as a lowercase hexadecimal string (64 characters)
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* const hashPassword = East.function([StringType], StringType, ($, password) => {
|
|
49
|
+
* return Crypto.hashSha256(password);
|
|
50
|
+
* // Returns: "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export const crypto_hash_sha256 = East.platform("crypto_hash_sha256", [StringType], StringType);
|
|
55
|
+
/**
|
|
56
|
+
* Computes SHA-256 hash of binary data.
|
|
57
|
+
*
|
|
58
|
+
* Calculates the SHA-256 cryptographic hash of binary data and returns the result
|
|
59
|
+
* as raw bytes (32 bytes / 256 bits). This is the binary variant of {@link crypto_hash_sha256},
|
|
60
|
+
* useful when working with binary protocols or when you need the raw hash bytes.
|
|
61
|
+
*
|
|
62
|
+
* This is a platform function for the East language, enabling cryptographic hashing
|
|
63
|
+
* in East programs running on Node.js.
|
|
64
|
+
*
|
|
65
|
+
* @param data - The binary data to hash (Blob/Uint8Array)
|
|
66
|
+
* @returns The SHA-256 hash as binary data (32 bytes)
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* const hashFile = East.function([BlobType], BlobType, ($, fileData) => {
|
|
71
|
+
* return Crypto.hashSha256Bytes(fileData);
|
|
72
|
+
* // Returns: 32 bytes of hash data
|
|
73
|
+
* });
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export const crypto_hash_sha256_bytes = East.platform("crypto_hash_sha256_bytes", [BlobType], BlobType);
|
|
77
|
+
/**
|
|
78
|
+
* Generates a random UUID v4.
|
|
79
|
+
*
|
|
80
|
+
* Creates a version 4 UUID (Universally Unique Identifier) using cryptographically
|
|
81
|
+
* secure random numbers. UUIDs are 128-bit identifiers formatted as 36-character
|
|
82
|
+
* strings (e.g., "550e8400-e29b-41d4-a716-446655440000").
|
|
83
|
+
*
|
|
84
|
+
* This is a platform function for the East language, enabling UUID generation
|
|
85
|
+
* in East programs running on Node.js.
|
|
86
|
+
*
|
|
87
|
+
* @returns A UUID v4 string in standard format (8-4-4-4-12 hex digits)
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* const createRecord = East.function([], StringType, $ => {
|
|
92
|
+
* const id = $.let(Crypto.uuid());
|
|
93
|
+
* return id;
|
|
94
|
+
* // Returns: "550e8400-e29b-41d4-a716-446655440000"
|
|
95
|
+
* });
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export const crypto_uuid = East.platform("crypto_uuid", [], StringType);
|
|
99
|
+
/**
|
|
100
|
+
* Node.js implementation of cryptographic platform functions.
|
|
101
|
+
*
|
|
102
|
+
* Pass this array to {@link East.compile} to enable crypto operations.
|
|
103
|
+
*/
|
|
104
|
+
const CryptoImpl = [
|
|
105
|
+
crypto_random_bytes.implement((length) => {
|
|
106
|
+
try {
|
|
107
|
+
return randomBytes(Number(length));
|
|
108
|
+
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
throw new EastError(`Failed to generate random bytes: ${err.message}`, {
|
|
111
|
+
location: { filename: "crypto_random_bytes", line: 0n, column: 0n },
|
|
112
|
+
cause: err
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}),
|
|
116
|
+
crypto_hash_sha256.implement((data) => {
|
|
117
|
+
try {
|
|
118
|
+
return createHash('sha256').update(data, 'utf-8').digest('hex');
|
|
119
|
+
}
|
|
120
|
+
catch (err) {
|
|
121
|
+
throw new EastError(`Failed to compute SHA-256 hash: ${err.message}`, {
|
|
122
|
+
location: { filename: "crypto_hash_sha256", line: 0n, column: 0n },
|
|
123
|
+
cause: err
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}),
|
|
127
|
+
crypto_hash_sha256_bytes.implement((data) => {
|
|
128
|
+
try {
|
|
129
|
+
return createHash('sha256').update(data).digest();
|
|
130
|
+
}
|
|
131
|
+
catch (err) {
|
|
132
|
+
throw new EastError(`Failed to compute SHA-256 hash: ${err.message}`, {
|
|
133
|
+
location: { filename: "crypto_hash_sha256_bytes", line: 0n, column: 0n },
|
|
134
|
+
cause: err
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}),
|
|
138
|
+
crypto_uuid.implement(() => {
|
|
139
|
+
try {
|
|
140
|
+
return randomUUID();
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
throw new EastError(`Failed to generate UUID: ${err.message}`, {
|
|
144
|
+
location: { filename: "crypto_uuid", line: 0n, column: 0n },
|
|
145
|
+
cause: err
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}),
|
|
149
|
+
];
|
|
150
|
+
/**
|
|
151
|
+
* Grouped cryptographic platform functions.
|
|
152
|
+
*
|
|
153
|
+
* Provides cryptographic operations for East programs.
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```ts
|
|
157
|
+
* import { East, StringType } from "@elaraai/east";
|
|
158
|
+
* import { Crypto } from "@elaraai/east-node-std";
|
|
159
|
+
*
|
|
160
|
+
* const generateId = East.function([], StringType, $ => {
|
|
161
|
+
* return Crypto.uuid();
|
|
162
|
+
* });
|
|
163
|
+
*
|
|
164
|
+
* const compiled = East.compile(generateId.toIR(), Crypto.Implementation);
|
|
165
|
+
* compiled(); // "550e8400-e29b-41d4-a716-446655440000"
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
export const Crypto = {
|
|
169
|
+
/**
|
|
170
|
+
* Generates cryptographically secure random bytes.
|
|
171
|
+
*
|
|
172
|
+
* Produces random bytes using a CSPRNG, suitable for generating encryption keys,
|
|
173
|
+
* tokens, and other security-sensitive random data.
|
|
174
|
+
*
|
|
175
|
+
* @param length - The number of random bytes to generate (must be positive)
|
|
176
|
+
* @returns Random bytes as a Blob (Uint8Array)
|
|
177
|
+
* @throws {EastError} When random generation fails
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* const generateToken = East.function([], BlobType, $ => {
|
|
182
|
+
* return Crypto.randomBytes(32n);
|
|
183
|
+
* });
|
|
184
|
+
*
|
|
185
|
+
* const compiled = East.compile(generateToken.toIR(), Crypto.Implementation);
|
|
186
|
+
* compiled(); // Uint8Array(32) [...]
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
randomBytes: crypto_random_bytes,
|
|
190
|
+
/**
|
|
191
|
+
* Computes SHA-256 hash of a string.
|
|
192
|
+
*
|
|
193
|
+
* Calculates the SHA-256 cryptographic hash of a UTF-8 encoded string and returns
|
|
194
|
+
* the result as a lowercase hexadecimal string (64 characters).
|
|
195
|
+
*
|
|
196
|
+
* @param data - The string to hash (will be UTF-8 encoded)
|
|
197
|
+
* @returns The SHA-256 hash as a lowercase hexadecimal string (64 characters)
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```ts
|
|
201
|
+
* const hashPassword = East.function([StringType], StringType, ($, password) => {
|
|
202
|
+
* return Crypto.hashSha256(password);
|
|
203
|
+
* });
|
|
204
|
+
*
|
|
205
|
+
* const compiled = East.compile(hashPassword.toIR(), Crypto.Implementation);
|
|
206
|
+
* compiled("password"); // "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
hashSha256: crypto_hash_sha256,
|
|
210
|
+
/**
|
|
211
|
+
* Computes SHA-256 hash of binary data.
|
|
212
|
+
*
|
|
213
|
+
* Calculates the SHA-256 cryptographic hash of binary data and returns the result
|
|
214
|
+
* as raw bytes (32 bytes).
|
|
215
|
+
*
|
|
216
|
+
* @param data - The binary data to hash (Blob/Uint8Array)
|
|
217
|
+
* @returns The SHA-256 hash as binary data (32 bytes)
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```ts
|
|
221
|
+
* const hashFile = East.function([BlobType], BlobType, ($, fileData) => {
|
|
222
|
+
* return Crypto.hashSha256Bytes(fileData);
|
|
223
|
+
* });
|
|
224
|
+
*
|
|
225
|
+
* const compiled = East.compile(hashFile.toIR(), Crypto.Implementation);
|
|
226
|
+
* const fileData = new Uint8Array([1, 2, 3]);
|
|
227
|
+
* compiled(fileData); // Uint8Array(32) [...]
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
230
|
+
hashSha256Bytes: crypto_hash_sha256_bytes,
|
|
231
|
+
/**
|
|
232
|
+
* Generates a random UUID v4.
|
|
233
|
+
*
|
|
234
|
+
* Creates a version 4 UUID using cryptographically secure random numbers.
|
|
235
|
+
* Returns a 36-character string in standard format.
|
|
236
|
+
*
|
|
237
|
+
* @returns A UUID v4 string in standard format (8-4-4-4-12 hex digits)
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```ts
|
|
241
|
+
* const createRecord = East.function([], StringType, $ => {
|
|
242
|
+
* return Crypto.uuid();
|
|
243
|
+
* });
|
|
244
|
+
*
|
|
245
|
+
* const compiled = East.compile(createRecord.toIR(), Crypto.Implementation);
|
|
246
|
+
* compiled(); // "550e8400-e29b-41d4-a716-446655440000"
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
uuid: crypto_uuid,
|
|
250
|
+
/**
|
|
251
|
+
* Node.js implementation of cryptographic platform functions.
|
|
252
|
+
*
|
|
253
|
+
* Pass this to {@link East.compile} to enable crypto operations.
|
|
254
|
+
*/
|
|
255
|
+
Implementation: CryptoImpl,
|
|
256
|
+
};
|
|
257
|
+
// Export for backwards compatibility
|
|
258
|
+
export { CryptoImpl };
|
|
259
|
+
//# sourceMappingURL=crypto.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAExE,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAElE;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAA+D,IAAI,CAAC,QAAQ,CAAC,qBAAqB,EAAE,CAAC,WAAW,CAAC,EAAE,QAAQ,CAAC,CAAC;AAE7J;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAgE,IAAI,CAAC,QAAQ,CAAC,oBAAoB,EAAE,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC,CAAC;AAE7J;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAA4D,IAAI,CAAC,QAAQ,CAAC,0BAA0B,EAAE,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC,CAAC;AAEjK;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,CAAC,MAAM,WAAW,GAA+C,IAAI,CAAC,QAAQ,CAAC,aAAa,EAAE,EAAE,EAAE,UAAU,CAAC,CAAC;AA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