@provablehq/sdk 0.6.9
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/README.md +1172 -0
- package/dist/account.d.ts +137 -0
- package/dist/function-key-provider.d.ts +336 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +3049 -0
- package/dist/index.js.map +1 -0
- package/dist/managed-worker.d.ts +3 -0
- package/dist/models/block.d.ts +21 -0
- package/dist/models/confirmed_transaction.d.ts +6 -0
- package/dist/models/execution.d.ts +5 -0
- package/dist/models/input.d.ts +10 -0
- package/dist/models/output.d.ts +6 -0
- package/dist/models/transactionModel.d.ts +6 -0
- package/dist/models/transition.d.ts +13 -0
- package/dist/network-client.d.ts +267 -0
- package/dist/node-polyfill.d.ts +4 -0
- package/dist/node-polyfill.js +302 -0
- package/dist/node-polyfill.js.map +1 -0
- package/dist/node.d.ts +2 -0
- package/dist/node.js +11 -0
- package/dist/node.js.map +1 -0
- package/dist/offline-key-provider.d.ts +347 -0
- package/dist/polyfill/crypto.d.ts +1 -0
- package/dist/polyfill/fetch.d.ts +1 -0
- package/dist/polyfill/worker.d.ts +1 -0
- package/dist/polyfill/xmlhttprequest.d.ts +1 -0
- package/dist/program-manager.d.ts +639 -0
- package/dist/record-provider.d.ts +236 -0
- package/dist/utils.d.ts +2 -0
- package/dist/worker.d.ts +8 -0
- package/dist/worker.js +74 -0
- package/dist/worker.js.map +1 -0
- package/package.json +71 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ConfirmedTransaction } from "./confirmed_transaction";
|
|
2
|
+
export type Block = {
|
|
3
|
+
block_hash: string;
|
|
4
|
+
previous_hash: string;
|
|
5
|
+
header: Header;
|
|
6
|
+
transactions?: (ConfirmedTransaction)[];
|
|
7
|
+
signature: string;
|
|
8
|
+
};
|
|
9
|
+
export type Header = {
|
|
10
|
+
previous_state_root: string;
|
|
11
|
+
transactions_root: string;
|
|
12
|
+
metadata: Metadata;
|
|
13
|
+
};
|
|
14
|
+
export type Metadata = {
|
|
15
|
+
network: number;
|
|
16
|
+
round: number;
|
|
17
|
+
height: number;
|
|
18
|
+
coinbase_target: number;
|
|
19
|
+
proof_target: number;
|
|
20
|
+
timestamp: number;
|
|
21
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Input } from "./input";
|
|
2
|
+
import { Output } from "./output";
|
|
3
|
+
export type Transition = {
|
|
4
|
+
id: string;
|
|
5
|
+
program: string;
|
|
6
|
+
function: string;
|
|
7
|
+
inputs?: (Input)[];
|
|
8
|
+
outputs?: (Output)[];
|
|
9
|
+
proof: string;
|
|
10
|
+
tpk: string;
|
|
11
|
+
tcm: string;
|
|
12
|
+
fee: number;
|
|
13
|
+
};
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { Account, Block, Program, RecordPlaintext, PrivateKey, Transaction, TransactionModel } from "./index";
|
|
2
|
+
type ProgramImports = {
|
|
3
|
+
[key: string]: string | Program;
|
|
4
|
+
};
|
|
5
|
+
interface AleoNetworkClientOptions {
|
|
6
|
+
headers?: {
|
|
7
|
+
[key: string]: string;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Client library that encapsulates REST calls to publicly exposed endpoints of Aleo nodes. The methods provided in this
|
|
12
|
+
* allow users to query public information from the Aleo blockchain and submit transactions to the network.
|
|
13
|
+
*
|
|
14
|
+
* @param {string} host
|
|
15
|
+
* @example
|
|
16
|
+
* // Connection to a local node
|
|
17
|
+
* const localNetworkClient = new AleoNetworkClient("http://localhost:3030");
|
|
18
|
+
*
|
|
19
|
+
* // Connection to a public beacon node
|
|
20
|
+
* const publicnetworkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
|
|
21
|
+
*/
|
|
22
|
+
declare class AleoNetworkClient {
|
|
23
|
+
host: string;
|
|
24
|
+
headers: {
|
|
25
|
+
[key: string]: string;
|
|
26
|
+
};
|
|
27
|
+
account: Account | undefined;
|
|
28
|
+
constructor(host: string, options?: AleoNetworkClientOptions);
|
|
29
|
+
/**
|
|
30
|
+
* Set an account to use in networkClient calls
|
|
31
|
+
*
|
|
32
|
+
* @param {Account} account
|
|
33
|
+
* @example
|
|
34
|
+
* const account = new Account();
|
|
35
|
+
* networkClient.setAccount(account);
|
|
36
|
+
*/
|
|
37
|
+
setAccount(account: Account): void;
|
|
38
|
+
/**
|
|
39
|
+
* Return the Aleo account used in the networkClient
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* const account = networkClient.getAccount();
|
|
43
|
+
*/
|
|
44
|
+
getAccount(): Account | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Set a new host for the networkClient
|
|
47
|
+
*
|
|
48
|
+
* @param {string} host The address of a node hosting the Aleo API
|
|
49
|
+
* @param host
|
|
50
|
+
*/
|
|
51
|
+
setHost(host: string): void;
|
|
52
|
+
fetchData<Type>(url?: string): Promise<Type>;
|
|
53
|
+
/**
|
|
54
|
+
* Attempts to find unspent records in the Aleo blockchain for a specified private key
|
|
55
|
+
* @param {number} startHeight - The height at which to start searching for unspent records
|
|
56
|
+
* @param {number} endHeight - The height at which to stop searching for unspent records
|
|
57
|
+
* @param {string | PrivateKey} privateKey - The private key to use to find unspent records
|
|
58
|
+
* @param {number[]} amounts - The amounts (in microcredits) to search for (eg. [100, 200, 3000])
|
|
59
|
+
* @param {number} maxMicrocredits - The maximum number of microcredits to search for
|
|
60
|
+
* @param {string[]} nonces - The nonces of already found records to exclude from the search
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* // Find all unspent records
|
|
64
|
+
* const privateKey = "[PRIVATE_KEY]";
|
|
65
|
+
* const records = networkClient.findUnspentRecords(0, undefined, privateKey);
|
|
66
|
+
*
|
|
67
|
+
* // Find specific amounts
|
|
68
|
+
* const startHeight = 500000;
|
|
69
|
+
* const amounts = [600000, 1000000];
|
|
70
|
+
* const records = networkClient.findUnspentRecords(startHeight, undefined, privateKey, amounts);
|
|
71
|
+
*
|
|
72
|
+
* // Find specific amounts with a maximum number of cumulative microcredits
|
|
73
|
+
* const maxMicrocredits = 100000;
|
|
74
|
+
* const records = networkClient.findUnspentRecords(startHeight, undefined, privateKey, undefined, maxMicrocredits);
|
|
75
|
+
*/
|
|
76
|
+
findUnspentRecords(startHeight: number, endHeight: number | undefined, privateKey: string | PrivateKey | undefined, amounts: number[] | undefined, maxMicrocredits?: number | undefined, nonces?: string[] | undefined): Promise<Array<RecordPlaintext> | Error>;
|
|
77
|
+
/**
|
|
78
|
+
* Returns the contents of the block at the specified block height
|
|
79
|
+
*
|
|
80
|
+
* @param {number} height
|
|
81
|
+
* @example
|
|
82
|
+
* const block = networkClient.getBlock(1234);
|
|
83
|
+
*/
|
|
84
|
+
getBlock(height: number): Promise<Block | Error>;
|
|
85
|
+
/**
|
|
86
|
+
* Returns a range of blocks between the specified block heights
|
|
87
|
+
*
|
|
88
|
+
* @param {number} start
|
|
89
|
+
* @param {number} end
|
|
90
|
+
* @example
|
|
91
|
+
* const blockRange = networkClient.getBlockRange(2050, 2100);
|
|
92
|
+
*/
|
|
93
|
+
getBlockRange(start: number, end: number): Promise<Array<Block> | Error>;
|
|
94
|
+
/**
|
|
95
|
+
* Returns the deployment transaction id associated with the specified program
|
|
96
|
+
*
|
|
97
|
+
* @param {Program | string} program
|
|
98
|
+
* @returns {TransactionModel | Error}
|
|
99
|
+
*/
|
|
100
|
+
getDeploymentTransactionIDForProgram(program: Program | string): Promise<string | Error>;
|
|
101
|
+
/**
|
|
102
|
+
* Returns the deployment transaction associated with a specified program
|
|
103
|
+
*
|
|
104
|
+
* @param {Program | string} program
|
|
105
|
+
* @returns {TransactionModel | Error}
|
|
106
|
+
*/
|
|
107
|
+
getDeploymentTransactionForProgram(program: Program | string): Promise<TransactionModel | Error>;
|
|
108
|
+
/**
|
|
109
|
+
* Returns the contents of the latest block
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* const latestHeight = networkClient.getLatestBlock();
|
|
113
|
+
*/
|
|
114
|
+
getLatestBlock(): Promise<Block | Error>;
|
|
115
|
+
/**
|
|
116
|
+
* Returns the latest committee
|
|
117
|
+
*
|
|
118
|
+
* @returns {Promise<object>} A javascript object containing the latest committee
|
|
119
|
+
*/
|
|
120
|
+
getLatestCommittee(): Promise<object | Error>;
|
|
121
|
+
/**
|
|
122
|
+
* Returns the latest block height
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* const latestHeight = networkClient.getLatestHeight();
|
|
126
|
+
*/
|
|
127
|
+
getLatestHeight(): Promise<number | Error>;
|
|
128
|
+
/**
|
|
129
|
+
* Returns the source code of a program given a program ID
|
|
130
|
+
*
|
|
131
|
+
* @param {string} programId The program ID of a program deployed to the Aleo Network
|
|
132
|
+
* @return {Promise<string>} Source code of the program
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* const program = networkClient.getProgram("hello_hello.aleo");
|
|
136
|
+
* const expectedSource = "program hello_hello.aleo;\n\nfunction hello:\n input r0 as u32.public;\n input r1 as u32.private;\n add r0 r1 into r2;\n output r2 as u32.private;\n"
|
|
137
|
+
* assert.equal(program, expectedSource);
|
|
138
|
+
*/
|
|
139
|
+
getProgram(programId: string): Promise<string | Error>;
|
|
140
|
+
/**
|
|
141
|
+
* Returns a program object from a program ID or program source code
|
|
142
|
+
*
|
|
143
|
+
* @param {string} inputProgram The program ID or program source code of a program deployed to the Aleo Network
|
|
144
|
+
* @return {Promise<Program | Error>} Source code of the program
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* const programID = "hello_hello.aleo";
|
|
148
|
+
* const programSource = "program hello_hello.aleo;\n\nfunction hello:\n input r0 as u32.public;\n input r1 as u32.private;\n add r0 r1 into r2;\n output r2 as u32.private;\n"
|
|
149
|
+
*
|
|
150
|
+
* // Get program object from program ID or program source code
|
|
151
|
+
* const programObjectFromID = await networkClient.getProgramObject(programID);
|
|
152
|
+
* const programObjectFromSource = await networkClient.getProgramObject(programSource);
|
|
153
|
+
*
|
|
154
|
+
* // Both program objects should be equal
|
|
155
|
+
* assert.equal(programObjectFromID.to_string(), programObjectFromSource.to_string());
|
|
156
|
+
*/
|
|
157
|
+
getProgramObject(inputProgram: string): Promise<Program | Error>;
|
|
158
|
+
/**
|
|
159
|
+
* Returns an object containing the source code of a program and the source code of all programs it imports
|
|
160
|
+
*
|
|
161
|
+
* @param {Program | string} inputProgram The program ID or program source code of a program deployed to the Aleo Network
|
|
162
|
+
* @returns {Promise<ProgramImports>} Object of the form { "program_id": "program_source", .. } containing program id & source code for all program imports
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* const double_test_source = "import multiply_test.aleo;\n\nprogram double_test.aleo;\n\nfunction double_it:\n input r0 as u32.private;\n call multiply_test.aleo/multiply 2u32 r0 into r1;\n output r1 as u32.private;\n"
|
|
166
|
+
* const double_test = Program.fromString(double_test_source);
|
|
167
|
+
* const expectedImports = {
|
|
168
|
+
* "multiply_test.aleo": "program multiply_test.aleo;\n\nfunction multiply:\n input r0 as u32.public;\n input r1 as u32.private;\n mul r0 r1 into r2;\n output r2 as u32.private;\n"
|
|
169
|
+
* }
|
|
170
|
+
*
|
|
171
|
+
* // Imports can be fetched using the program ID, source code, or program object
|
|
172
|
+
* let programImports = await networkClient.getProgramImports("double_test.aleo");
|
|
173
|
+
* assert.deepStrictEqual(programImports, expectedImports);
|
|
174
|
+
*
|
|
175
|
+
* // Using the program source code
|
|
176
|
+
* programImports = await networkClient.getProgramImports(double_test_source);
|
|
177
|
+
* assert.deepStrictEqual(programImports, expectedImports);
|
|
178
|
+
*
|
|
179
|
+
* // Using the program object
|
|
180
|
+
* programImports = await networkClient.getProgramImports(double_test);
|
|
181
|
+
* assert.deepStrictEqual(programImports, expectedImports);
|
|
182
|
+
*/
|
|
183
|
+
getProgramImports(inputProgram: Program | string): Promise<ProgramImports | Error>;
|
|
184
|
+
/**
|
|
185
|
+
* Get a list of the program names that a program imports
|
|
186
|
+
*
|
|
187
|
+
* @param {Program | string} inputProgram - The program id or program source code to get the imports of
|
|
188
|
+
* @returns {string[]} - The list of program names that the program imports
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* const programImportsNames = networkClient.getProgramImports("double_test.aleo");
|
|
192
|
+
* const expectedImportsNames = ["multiply_test.aleo"];
|
|
193
|
+
* assert.deepStrictEqual(programImportsNames, expectedImportsNames);
|
|
194
|
+
*/
|
|
195
|
+
getProgramImportNames(inputProgram: Program | string): Promise<string[] | Error>;
|
|
196
|
+
/**
|
|
197
|
+
* Returns the names of the mappings of a program
|
|
198
|
+
*
|
|
199
|
+
* @param {string} programId - The program ID to get the mappings of (e.g. "credits.aleo")
|
|
200
|
+
* @example
|
|
201
|
+
* const mappings = networkClient.getProgramMappingNames("credits.aleo");
|
|
202
|
+
* const expectedMappings = ["account"];
|
|
203
|
+
* assert.deepStrictEqual(mappings, expectedMappings);
|
|
204
|
+
*/
|
|
205
|
+
getProgramMappingNames(programId: string): Promise<Array<string> | Error>;
|
|
206
|
+
/**
|
|
207
|
+
* Returns the value of a program's mapping for a specific key
|
|
208
|
+
*
|
|
209
|
+
* @param {string} programId - The program ID to get the mapping value of (e.g. "credits.aleo")
|
|
210
|
+
* @param {string} mappingName - The name of the mapping to get the value of (e.g. "account")
|
|
211
|
+
* @param {string} key - The key of the mapping to get the value of (e.g. "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px")
|
|
212
|
+
* @return {Promise<string>} String representation of the value of the mapping
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* // Get public balance of an account
|
|
216
|
+
* const mappingValue = networkClient.getMappingValue("credits.aleo", "account", "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px");
|
|
217
|
+
* const expectedValue = "0u64";
|
|
218
|
+
* assert.equal(mappingValue, expectedValue);
|
|
219
|
+
*/
|
|
220
|
+
getProgramMappingValue(programId: string, mappingName: string, key: string): Promise<string | Error>;
|
|
221
|
+
/**
|
|
222
|
+
* Returns the latest state/merkle root of the Aleo blockchain
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* const stateRoot = networkClient.getStateRoot();
|
|
226
|
+
*/
|
|
227
|
+
getStateRoot(): Promise<string | Error>;
|
|
228
|
+
/**
|
|
229
|
+
* Returns a transaction by its unique identifier
|
|
230
|
+
*
|
|
231
|
+
* @param {string} id
|
|
232
|
+
* @example
|
|
233
|
+
* const transaction = networkClient.getTransaction("at1handz9xjrqeynjrr0xay4pcsgtnczdksz3e584vfsgaz0dh0lyxq43a4wj");
|
|
234
|
+
*/
|
|
235
|
+
getTransaction(id: string): Promise<TransactionModel | Error>;
|
|
236
|
+
/**
|
|
237
|
+
* Returns the transactions present at the specified block height
|
|
238
|
+
*
|
|
239
|
+
* @param {number} height
|
|
240
|
+
* @example
|
|
241
|
+
* const transactions = networkClient.getTransactions(654);
|
|
242
|
+
*/
|
|
243
|
+
getTransactions(height: number): Promise<Array<TransactionModel> | Error>;
|
|
244
|
+
/**
|
|
245
|
+
* Returns the transactions in the memory pool.
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* const transactions = networkClient.getTransactionsInMempool();
|
|
249
|
+
*/
|
|
250
|
+
getTransactionsInMempool(): Promise<Array<TransactionModel> | Error>;
|
|
251
|
+
/**
|
|
252
|
+
* Returns the transition ID of the transition corresponding to the ID of the input or output.
|
|
253
|
+
* @param {string} inputOrOutputID - ID of the input or output.
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* const transitionId = networkClient.getTransitionId("2429232855236830926144356377868449890830704336664550203176918782554219952323field");
|
|
257
|
+
*/
|
|
258
|
+
getTransitionId(inputOrOutputID: string): Promise<string | Error>;
|
|
259
|
+
/**
|
|
260
|
+
* Submit an execute or deployment transaction to the Aleo network
|
|
261
|
+
*
|
|
262
|
+
* @param {Transaction | string} transaction - The transaction to submit to the network
|
|
263
|
+
* @returns {string | Error} - The transaction id of the submitted transaction or the resulting error
|
|
264
|
+
*/
|
|
265
|
+
submitTransaction(transaction: Transaction | string): Promise<string | Error>;
|
|
266
|
+
}
|
|
267
|
+
export { AleoNetworkClient, AleoNetworkClientOptions, ProgramImports };
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { webcrypto } from 'node:crypto';
|
|
2
|
+
import * as $fs from 'node:fs';
|
|
3
|
+
import $mime from 'mime/lite.js';
|
|
4
|
+
import $request from 'sync-request';
|
|
5
|
+
import * as $worker from 'node:worker_threads';
|
|
6
|
+
import * as $os from 'node:os';
|
|
7
|
+
|
|
8
|
+
if (globalThis.crypto == null) {
|
|
9
|
+
globalThis.crypto = webcrypto;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const oldFetch = globalThis.fetch;
|
|
13
|
+
// We always polyfill fetch because Node's fetch doesn't support file URLs.
|
|
14
|
+
globalThis.fetch = async function (resource, options) {
|
|
15
|
+
const request = new Request(resource, options);
|
|
16
|
+
const url = new URL(request.url);
|
|
17
|
+
if (url.protocol === "file:") {
|
|
18
|
+
const readStream = $fs.createReadStream(url);
|
|
19
|
+
const headers = {};
|
|
20
|
+
const type = $mime.getType(url.pathname);
|
|
21
|
+
if (type) {
|
|
22
|
+
headers["Content-Type"] = type;
|
|
23
|
+
}
|
|
24
|
+
return new Response(readStream, {
|
|
25
|
+
status: 200,
|
|
26
|
+
statusText: "OK",
|
|
27
|
+
headers,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
return await oldFetch(request);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
globalThis.XMLHttpRequest = class extends EventTarget {
|
|
36
|
+
static UNSENT = 0;
|
|
37
|
+
static OPENED = 1;
|
|
38
|
+
static HEADERS_RECEIVED = 2;
|
|
39
|
+
static LOADING = 3;
|
|
40
|
+
static DONE = 4;
|
|
41
|
+
UNSENT = XMLHttpRequest.UNSENT;
|
|
42
|
+
OPENED = XMLHttpRequest.OPENED;
|
|
43
|
+
HEADERS_RECEIVED = XMLHttpRequest.HEADERS_RECEIVED;
|
|
44
|
+
LOADING = XMLHttpRequest.LOADING;
|
|
45
|
+
DONE = XMLHttpRequest.DONE;
|
|
46
|
+
responseType;
|
|
47
|
+
withCredentials;
|
|
48
|
+
timeout;
|
|
49
|
+
readyState;
|
|
50
|
+
response;
|
|
51
|
+
responseText;
|
|
52
|
+
responseURL;
|
|
53
|
+
responseXML;
|
|
54
|
+
status;
|
|
55
|
+
statusText;
|
|
56
|
+
upload;
|
|
57
|
+
_url;
|
|
58
|
+
_mime;
|
|
59
|
+
constructor() {
|
|
60
|
+
super();
|
|
61
|
+
this._reset();
|
|
62
|
+
this._mime = "text/xml";
|
|
63
|
+
}
|
|
64
|
+
_reset() {
|
|
65
|
+
this.readyState = XMLHttpRequest.UNSENT;
|
|
66
|
+
this.response = null;
|
|
67
|
+
this.responseText = "";
|
|
68
|
+
this.responseType = "";
|
|
69
|
+
this.responseURL = "";
|
|
70
|
+
this.responseXML = null;
|
|
71
|
+
this.status = 0;
|
|
72
|
+
this.statusText = "";
|
|
73
|
+
this.timeout = 0;
|
|
74
|
+
this.upload = null;
|
|
75
|
+
this.withCredentials = false;
|
|
76
|
+
this._url = null;
|
|
77
|
+
}
|
|
78
|
+
_success() {
|
|
79
|
+
this.readyState = XMLHttpRequest.DONE;
|
|
80
|
+
this.status = 200;
|
|
81
|
+
this.statusText = "OK";
|
|
82
|
+
}
|
|
83
|
+
set onabort(value) {
|
|
84
|
+
throw new Error("Not implemented");
|
|
85
|
+
}
|
|
86
|
+
set onerror(value) {
|
|
87
|
+
throw new Error("Not implemented");
|
|
88
|
+
}
|
|
89
|
+
set onreadystatechange(value) {
|
|
90
|
+
throw new Error("Not implemented");
|
|
91
|
+
}
|
|
92
|
+
set onloadstart(value) {
|
|
93
|
+
throw new Error("Not implemented");
|
|
94
|
+
}
|
|
95
|
+
set onload(value) {
|
|
96
|
+
throw new Error("Not implemented");
|
|
97
|
+
}
|
|
98
|
+
set onloadend(value) {
|
|
99
|
+
throw new Error("Not implemented");
|
|
100
|
+
}
|
|
101
|
+
set onprogress(value) {
|
|
102
|
+
throw new Error("Not implemented");
|
|
103
|
+
}
|
|
104
|
+
set ontimeout(value) {
|
|
105
|
+
throw new Error("Not implemented");
|
|
106
|
+
}
|
|
107
|
+
abort() {
|
|
108
|
+
throw new Error("Not implemented");
|
|
109
|
+
}
|
|
110
|
+
overrideMimeType(mime) {
|
|
111
|
+
this._mime = mime;
|
|
112
|
+
}
|
|
113
|
+
getResponseHeader() {
|
|
114
|
+
throw new Error("Not implemented");
|
|
115
|
+
}
|
|
116
|
+
getAllResponseHeaders() {
|
|
117
|
+
throw new Error("Not implemented");
|
|
118
|
+
}
|
|
119
|
+
setRequestHeader() {
|
|
120
|
+
throw new Error("Not implemented");
|
|
121
|
+
}
|
|
122
|
+
open(method, url, async = true, username, password) {
|
|
123
|
+
if (async) {
|
|
124
|
+
throw new Error("Async XMLHttpRequest is not implemented yet");
|
|
125
|
+
}
|
|
126
|
+
if (method !== "GET") {
|
|
127
|
+
throw new Error("Non-GET requests are not implemented yet");
|
|
128
|
+
}
|
|
129
|
+
this._reset();
|
|
130
|
+
this._url = url;
|
|
131
|
+
}
|
|
132
|
+
send(body = null) {
|
|
133
|
+
if (body !== null) {
|
|
134
|
+
throw new Error("XMLHttpRequest send body is not implemented yet");
|
|
135
|
+
}
|
|
136
|
+
if (!this._url) {
|
|
137
|
+
throw new Error("You must call open before you call send");
|
|
138
|
+
}
|
|
139
|
+
const response = $request("GET", this._url, {
|
|
140
|
+
headers: {
|
|
141
|
+
"Content-Type": this._mime,
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
const buffer = response.body.buffer;
|
|
145
|
+
const responseText = new TextDecoder("iso-8859-5", { fatal: true }).decode(buffer);
|
|
146
|
+
this.response = this.responseText = responseText;
|
|
147
|
+
this._url = null;
|
|
148
|
+
this._success();
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
// This is technically not a part of the Worker polyfill,
|
|
153
|
+
// but Workers are used for multi-threading, so this is often
|
|
154
|
+
// needed when writing Worker code.
|
|
155
|
+
if (globalThis.navigator == null) {
|
|
156
|
+
globalThis.navigator = {
|
|
157
|
+
hardwareConcurrency: $os.cpus().length,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
globalThis.Worker = class Worker extends EventTarget {
|
|
161
|
+
_worker;
|
|
162
|
+
constructor(url, options) {
|
|
163
|
+
super();
|
|
164
|
+
if (url instanceof URL) {
|
|
165
|
+
if (url.protocol !== "file:") {
|
|
166
|
+
throw new Error("Worker only supports file: URLs");
|
|
167
|
+
}
|
|
168
|
+
url = url.href;
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
throw new Error("Filepaths are unreliable, use `new URL(\"...\", import.meta.url)` instead.");
|
|
172
|
+
}
|
|
173
|
+
if (!options || options.type !== "module") {
|
|
174
|
+
throw new Error("Workers must use \`type: \"module\"\`");
|
|
175
|
+
}
|
|
176
|
+
const code = `
|
|
177
|
+
const { workerData } = require("node:worker_threads");
|
|
178
|
+
|
|
179
|
+
import(workerData.polyfill)
|
|
180
|
+
.then(() => import(workerData.url))
|
|
181
|
+
.catch((e) => {
|
|
182
|
+
// TODO maybe it should send a message to the parent?
|
|
183
|
+
console.error(e.stack);
|
|
184
|
+
});
|
|
185
|
+
`;
|
|
186
|
+
this._worker = new $worker.Worker(code, {
|
|
187
|
+
eval: true,
|
|
188
|
+
workerData: {
|
|
189
|
+
url,
|
|
190
|
+
polyfill: new URL("node-polyfill.js", import.meta.url).href,
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
this._worker.on("message", (data) => {
|
|
194
|
+
this.dispatchEvent(new MessageEvent("message", { data }));
|
|
195
|
+
});
|
|
196
|
+
this._worker.on("messageerror", (error) => {
|
|
197
|
+
throw new Error("UNIMPLEMENTED");
|
|
198
|
+
});
|
|
199
|
+
this._worker.on("error", (error) => {
|
|
200
|
+
// TODO attach the error to the event somehow
|
|
201
|
+
const event = new Event("error");
|
|
202
|
+
this.dispatchEvent(event);
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
set onmessage(f) {
|
|
206
|
+
throw new Error("UNIMPLEMENTED");
|
|
207
|
+
}
|
|
208
|
+
set onmessageerror(f) {
|
|
209
|
+
throw new Error("UNIMPLEMENTED");
|
|
210
|
+
}
|
|
211
|
+
set onerror(f) {
|
|
212
|
+
throw new Error("UNIMPLEMENTED");
|
|
213
|
+
}
|
|
214
|
+
postMessage(value, transfer) {
|
|
215
|
+
this._worker.postMessage(value, transfer);
|
|
216
|
+
}
|
|
217
|
+
terminate() {
|
|
218
|
+
this._worker.terminate();
|
|
219
|
+
}
|
|
220
|
+
// This is Node-specific, it allows the process to exit
|
|
221
|
+
// even if the Worker is still running.
|
|
222
|
+
unref() {
|
|
223
|
+
this._worker.unref();
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
if (!$worker.isMainThread) {
|
|
227
|
+
const globals = globalThis;
|
|
228
|
+
// This is used to create the onmessage, onmessageerror, and onerror setters
|
|
229
|
+
const makeSetter = (prop, event) => {
|
|
230
|
+
let oldvalue;
|
|
231
|
+
Object.defineProperty(globals, prop, {
|
|
232
|
+
get() {
|
|
233
|
+
return oldvalue;
|
|
234
|
+
},
|
|
235
|
+
set(value) {
|
|
236
|
+
if (oldvalue) {
|
|
237
|
+
globals.removeEventListener(event, oldvalue);
|
|
238
|
+
}
|
|
239
|
+
oldvalue = value;
|
|
240
|
+
if (oldvalue) {
|
|
241
|
+
globals.addEventListener(event, oldvalue);
|
|
242
|
+
}
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
};
|
|
246
|
+
// This makes sure that `f` is only run once
|
|
247
|
+
const memoize = (f) => {
|
|
248
|
+
let run = false;
|
|
249
|
+
return () => {
|
|
250
|
+
if (!run) {
|
|
251
|
+
run = true;
|
|
252
|
+
f();
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
};
|
|
256
|
+
// We only start listening for messages / errors when the worker calls addEventListener
|
|
257
|
+
const startOnMessage = memoize(() => {
|
|
258
|
+
$worker.parentPort.on("message", (data) => {
|
|
259
|
+
workerEvents.dispatchEvent(new MessageEvent("message", { data }));
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
const startOnMessageError = memoize(() => {
|
|
263
|
+
throw new Error("UNIMPLEMENTED");
|
|
264
|
+
});
|
|
265
|
+
const startOnError = memoize(() => {
|
|
266
|
+
$worker.parentPort.on("error", (data) => {
|
|
267
|
+
workerEvents.dispatchEvent(new Event("error"));
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
// Node workers don't have top-level events, so we have to make our own
|
|
271
|
+
const workerEvents = new EventTarget();
|
|
272
|
+
globals.close = () => {
|
|
273
|
+
process.exit();
|
|
274
|
+
};
|
|
275
|
+
globals.addEventListener = (type, callback, options) => {
|
|
276
|
+
workerEvents.addEventListener(type, callback, options);
|
|
277
|
+
if (type === "message") {
|
|
278
|
+
startOnMessage();
|
|
279
|
+
}
|
|
280
|
+
else if (type === "messageerror") {
|
|
281
|
+
startOnMessageError();
|
|
282
|
+
}
|
|
283
|
+
else if (type === "error") {
|
|
284
|
+
startOnError();
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
globals.removeEventListener = (type, callback, options) => {
|
|
288
|
+
workerEvents.removeEventListener(type, callback, options);
|
|
289
|
+
};
|
|
290
|
+
function postMessage(value, transfer) {
|
|
291
|
+
$worker.parentPort.postMessage(value, transfer);
|
|
292
|
+
}
|
|
293
|
+
globals.postMessage = postMessage;
|
|
294
|
+
makeSetter("onmessage", "message");
|
|
295
|
+
makeSetter("onmessageerror", "messageerror");
|
|
296
|
+
makeSetter("onerror", "error");
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (!globalThis.self) {
|
|
300
|
+
globalThis.self = globalThis;
|
|
301
|
+
}
|
|
302
|
+
//# sourceMappingURL=node-polyfill.js.map
|