@steerprotocol/app-loader 2.1.1 → 3.0.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/lib/esm/index.js DELETED
@@ -1,397 +0,0 @@
1
- import { ethers } from 'ethers';
2
- import { Candle, generateCandles } from './Candle';
3
- import { RawTradeData } from './RawTradeData';
4
- const isNode = typeof window === 'undefined' && typeof process !== 'undefined';
5
- let fetchImpl = null;
6
- let fsImpl = null;
7
- let ccxt = null;
8
- if (globalThis['fetch'])
9
- fetchImpl = fetch;
10
- export { Candle, RawTradeData };
11
- var State;
12
- (function (State) {
13
- State[State["None"] = 0] = "None";
14
- State[State["Unwinding"] = 1] = "Unwinding";
15
- State[State["Rewinding"] = 2] = "Rewinding";
16
- })(State || (State = {}));
17
- /**
18
- * Load a wasm bundle synchronously. Only accepts the actual binary data.
19
- * @param input - Wasm bundle data
20
- * @param imports - Imports
21
- * @returns
22
- */
23
- export function loadWasmSync(input, imports = {}) {
24
- return instantiate(new WebAssembly.Module(input), imports);
25
- }
26
- /**
27
- * Load a wasm bundle asynchronously. Accepts the actual binary data or file path.
28
- * @param input - Wasm bundle data or path
29
- * @param imports - Imports
30
- * @returns
31
- */
32
- export async function loadWasm(input, imports = {}) {
33
- const mod = instantiate(await (async () => {
34
- if (typeof input === 'string') {
35
- if (isNode) {
36
- if (!fetchImpl)
37
- fetchImpl = (await import('undici')).fetch;
38
- if (!fsImpl)
39
- fsImpl = await import('fs/promises');
40
- if (!ccxt)
41
- ccxt = (await import('ccxt')).default;
42
- return WebAssembly.compile(await fsImpl.readFile(input));
43
- }
44
- return await WebAssembly.compileStreaming(globalThis.fetch(input));
45
- }
46
- else {
47
- return WebAssembly.compile(input);
48
- }
49
- })(), imports);
50
- return mod;
51
- }
52
- function instantiate(module, imports = {}) {
53
- let WASM_MEMORY;
54
- let WASM_EXPORTS;
55
- let WASM_DV;
56
- let ASYNCIFY_PTR = 0;
57
- let ASYNCIFY_MEM;
58
- let ASYNCIFY_INITIALIZED = false;
59
- let fetchFn = null;
60
- let ccxtFn = null;
61
- let detachedValue = null;
62
- const adaptedImports = Object.assign({
63
- console: {
64
- log(text) {
65
- console.log(__liftString(text));
66
- },
67
- },
68
- ethers: {
69
- keccak256_str(str_ptr) {
70
- const str = __liftString(str_ptr);
71
- const hash = ethers.keccak256(str);
72
- return __lowerString(hash);
73
- },
74
- keccak256_buf(buf_ptr) {
75
- const buf = __liftBuffer(buf_ptr);
76
- const hash = ethers.keccak256(new Uint8Array(buf));
77
- return __lowerString(hash);
78
- },
79
- },
80
- // @ts-ignore
81
- env: {
82
- abort(message, fileName, lineNumber, columnNumber) {
83
- (() => {
84
- throw Error(`${__liftString(message)} in ${__liftString(fileName)}:${lineNumber}:${columnNumber}`);
85
- })();
86
- },
87
- _initAsyncify(frame_ptr, stack_ptr) {
88
- // @ts-ignore
89
- if (!WASM_EXPORTS['asyncify_get_state'])
90
- throw new Error('Asyncify initialized, but not enabled when run! Compile with the --runPasses asyncify flag or asyncify-shim transform!');
91
- if (ASYNCIFY_INITIALIZED)
92
- return;
93
- ASYNCIFY_PTR = frame_ptr;
94
- ASYNCIFY_MEM[ASYNCIFY_PTR >> 2] = ASYNCIFY_PTR + 8;
95
- ASYNCIFY_MEM[(ASYNCIFY_PTR + 4) >> 2] = stack_ptr;
96
- ASYNCIFY_INITIALIZED = true;
97
- },
98
- generateCandles(data, candleSize) {
99
- const _data = __liftString(data) || '[]';
100
- const _candleSize = __liftString(candleSize) || '69m';
101
- const candles = generateCandles(JSON.parse(_data), _candleSize);
102
- return __lowerString(JSON.stringify(candles));
103
- },
104
- 'console.log': (text) => {
105
- console.log(__liftString(text));
106
- },
107
- ccxt_fetchOHLCV: (exchangeId, symbol, timeframe, limit, since) => {
108
- // @ts-ignore
109
- const currentState = WASM_EXPORTS.asyncify_get_state();
110
- if (currentState === State.Rewinding) {
111
- // @ts-ignore
112
- WASM_EXPORTS.asyncify_stop_rewind();
113
- // @ts-ignore
114
- const ptr = __lowerStaticArray((pointer, value) => {
115
- __setU32(pointer, __lowerStaticArray(__setF64, 7, 3, value, Float64Array));
116
- }, 8, 2, detachedValue);
117
- return ptr;
118
- }
119
- const _exchange = __liftString(exchangeId);
120
- const _symbol = __liftString(symbol);
121
- const _timeframe = __liftString(timeframe);
122
- ccxtFn = async () => {
123
- if (!_exchange || !_symbol || !_timeframe)
124
- throw new Error('Exchange, Symbol, or Timeframe not provided when fetching OHCLV data.');
125
- // @ts-ignore
126
- const data = await new ccxt[_exchange]({
127
- apiKey: '',
128
- secret: '',
129
- }).fetchOHLCV(_symbol, _timeframe, since, limit);
130
- return data;
131
- };
132
- // @ts-ignore
133
- WASM_EXPORTS.asyncify_start_unwind(ASYNCIFY_PTR);
134
- },
135
- },
136
- 'as-fetch': {
137
- _initAsyncify(frame_ptr, stack_ptr) {
138
- // @ts-ignore
139
- if (!WASM_EXPORTS['asyncify_get_state'])
140
- throw new Error('Asyncify initialized, but not enabled when run! Compile with the --runPasses asyncify flag or asyncify-shim transform!');
141
- if (ASYNCIFY_INITIALIZED)
142
- return;
143
- ASYNCIFY_PTR = frame_ptr;
144
- ASYNCIFY_MEM[ASYNCIFY_PTR >> 2] = ASYNCIFY_PTR + 8;
145
- // I don"t know if I need to reserve all this memory...
146
- ASYNCIFY_MEM[(ASYNCIFY_PTR + 4) >> 2] = stack_ptr;
147
- ASYNCIFY_INITIALIZED = true;
148
- },
149
- _fetchPOSTSync(url, mode, headers, body) {
150
- // @ts-ignore
151
- const currentState = WASM_EXPORTS.asyncify_get_state();
152
- if (currentState === State.Rewinding) {
153
- // @ts-ignore
154
- WASM_EXPORTS.asyncify_stop_rewind();
155
- const ptr = __lowerBuffer(detachedValue);
156
- detachedValue = null;
157
- return ptr;
158
- }
159
- fetchFn = async () => {
160
- const res = await fetchImpl(__liftString(url) || '', {
161
- method: 'POST',
162
- mode: modeToString(mode) || 'cors',
163
- headers: __liftArray((pointer) => __liftArray((pointer) => __liftString(__getU32(pointer)), 2, __getU32(pointer)), 2, headers) || [],
164
- body: __liftBuffer(body),
165
- });
166
- const value = await res.arrayBuffer();
167
- return value;
168
- };
169
- // @ts-ignore
170
- WASM_EXPORTS.asyncify_start_unwind(ASYNCIFY_PTR);
171
- },
172
- _fetchGETSync(url, mode, headers) {
173
- // @ts-ignore
174
- const currentState = WASM_EXPORTS.asyncify_get_state();
175
- if (currentState === State.Rewinding) {
176
- // @ts-ignore
177
- WASM_EXPORTS.asyncify_stop_rewind();
178
- const ptr = __lowerBuffer(detachedValue);
179
- detachedValue = null;
180
- return ptr;
181
- }
182
- fetchFn = async () => {
183
- const res = await fetchImpl(__liftString(url) || '', {
184
- method: 'GET',
185
- mode: modeToString(mode) || 'cors',
186
- headers: __liftArray((pointer) => __liftArray((pointer) => __liftString(__getU32(pointer)), 2, __getU32(pointer)), 2, headers) || [],
187
- });
188
- const value = await res.arrayBuffer();
189
- return value;
190
- };
191
- // @ts-ignore
192
- WASM_EXPORTS.asyncify_start_unwind(ASYNCIFY_PTR);
193
- },
194
- _fetchGET(url, mode, headers, callbackID) {
195
- fetchImpl(__liftString(url) || '', {
196
- method: 'GET',
197
- mode: modeToString(mode) || 'cors',
198
- headers: __liftArray((pointer) => __liftArray((pointer) => __liftString(__getU32(pointer)), 2, __getU32(pointer)), 2, headers) || [],
199
- }).then(async (res) => {
200
- const body = await res.arrayBuffer();
201
- WASM_EXPORTS.responseHandler(body, res.status, res.redirected ? 1 : 0, callbackID);
202
- });
203
- },
204
- _fetchPOST(url, mode, headers, body, callbackID) {
205
- fetchImpl(__liftString(url) || '', {
206
- method: 'POST',
207
- mode: modeToString(mode) || 'cors',
208
- body: body,
209
- headers: __liftArray((pointer) => __liftArray((pointer) => __liftString(__getU32(pointer)), 2, __getU32(pointer)), 2, headers) || [],
210
- }).then(async (res) => {
211
- const body = await res.arrayBuffer();
212
- WASM_EXPORTS.responseHandler(body, res.status, res.redirected ? 1 : 0, callbackID);
213
- });
214
- },
215
- },
216
- }, imports);
217
- const mod = new WebAssembly.Instance(module, adaptedImports);
218
- WASM_EXPORTS = mod.exports;
219
- // @ts-ignore
220
- WASM_MEMORY = WASM_EXPORTS.memory || adaptedImports.env.memory;
221
- ASYNCIFY_MEM = new Uint32Array(WASM_MEMORY.buffer);
222
- const handledExports = Object.setPrototypeOf({
223
- responseHandler(body, statusCode, redirected, callbackID) {
224
- if (!WASM_EXPORTS['responseHandler'])
225
- throw new Error('Unable to call .responseHandler on wasm module. Add the line export { responseHandler } from "as-fetch/assembly" to your entry file.');
226
- // @ts-ignore
227
- WASM_EXPORTS.responseHandler(__lowerBuffer(body), statusCode, redirected ? 1 : 0, callbackID);
228
- },
229
- initialize(config) {
230
- if (!WASM_EXPORTS['initialize'])
231
- throw new Error('Unable to call .initialize on wasm module. Are you sure this is a data connector?');
232
- try {
233
- // @ts-ignore
234
- WASM_EXPORTS.initialize(__lowerString(config));
235
- return true;
236
- }
237
- catch (e) {
238
- console.error(e);
239
- return false;
240
- }
241
- },
242
- async execute(...params) {
243
- if (!WASM_EXPORTS['execute'])
244
- throw new Error('Unable to call .execute on wasm module. Are you sure this is a data connector?');
245
- const loweredArgs = new Array(params.length);
246
- for (let i = 0; i < params.length; i++) {
247
- loweredArgs[i] = __lower(params[i]);
248
- }
249
- let result = WASM_EXPORTS.execute(...loweredArgs);
250
- if (ASYNCIFY_INITIALIZED) {
251
- // @ts-ignore
252
- while (WASM_EXPORTS.asyncify_get_state() === State.Unwinding) {
253
- // @ts-ignore
254
- WASM_EXPORTS.asyncify_stop_unwind();
255
- if (fetchFn)
256
- detachedValue = await fetchFn();
257
- if (ccxtFn)
258
- detachedValue = await ccxtFn();
259
- // @ts-ignore
260
- WASM_EXPORTS.asyncify_start_rewind(ASYNCIFY_PTR);
261
- result = WASM_EXPORTS.execute();
262
- }
263
- }
264
- if (fetchFn)
265
- fetchFn = null;
266
- if (ccxtFn)
267
- ccxtFn = null;
268
- // @ts-ignore
269
- return __liftString(result);
270
- },
271
- config() {
272
- if (!WASM_EXPORTS['config'])
273
- throw new Error('Unable to call .config on wasm module. Are you sure this is a data connector?');
274
- return __liftString(WASM_EXPORTS.config());
275
- },
276
- transform() {
277
- if (!WASM_EXPORTS['transform'])
278
- throw new Error('Unable to call .transform on wasm module. Are you sure this is a data connector?');
279
- return __liftString(WASM_EXPORTS.transform());
280
- },
281
- reset() {
282
- if (WASM_EXPORTS['reset'])
283
- WASM_EXPORTS.reset();
284
- },
285
- }, WASM_EXPORTS);
286
- WASM_DV = new DataView(WASM_MEMORY.buffer);
287
- function __lower(val) {
288
- if (typeof val === 'number') {
289
- return val;
290
- }
291
- else if (typeof val === 'string') {
292
- return __lowerString(val);
293
- }
294
- else if (val instanceof ArrayBuffer) {
295
- return __lowerBuffer(val);
296
- }
297
- else {
298
- return 0;
299
- }
300
- }
301
- function __liftString(ptr) {
302
- if (!ptr)
303
- return null;
304
- const end = (ptr + new Uint32Array(WASM_MEMORY.buffer)[(ptr - 4) >>> 2]) >>> 1;
305
- const memoryU16 = new Uint16Array(WASM_MEMORY.buffer);
306
- let start = ptr >>> 1;
307
- let string = '';
308
- while (end - start > 1024)
309
- string += String.fromCharCode(...memoryU16.subarray(start, (start += 1024)));
310
- return string + String.fromCharCode(...memoryU16.subarray(start, end));
311
- }
312
- function __lowerString(value) {
313
- if (value == null)
314
- return 0;
315
- const length = value.length;
316
- const ptr = WASM_EXPORTS.__pin(WASM_EXPORTS.__new(length << 1, 2) >>> 0);
317
- const memoryU16 = new Uint16Array(WASM_MEMORY.buffer);
318
- for (let i = 0; i < length; ++i)
319
- memoryU16[(ptr >>> 1) + i] = value.charCodeAt(i);
320
- return ptr;
321
- }
322
- function __liftBuffer(ptr) {
323
- if (!ptr)
324
- return null;
325
- return WASM_MEMORY.buffer.slice(ptr, ptr + new Uint32Array(WASM_MEMORY.buffer)[(ptr - 4) >>> 2]);
326
- }
327
- function __lowerBuffer(value) {
328
- if (value == null)
329
- return 0;
330
- const ptr = WASM_EXPORTS.__pin(WASM_EXPORTS.__new(value.byteLength, 1) >>> 0);
331
- new Uint8Array(WASM_MEMORY.buffer).set(new Uint8Array(value), ptr);
332
- return ptr;
333
- }
334
- function __liftArray(liftElement, align, ptr) {
335
- if (!ptr)
336
- return null;
337
- const dataStart = __getU32(ptr + 4);
338
- const length = WASM_DV.getUint32(ptr + 12, true);
339
- const values = new Array(length);
340
- for (let i = 0; i < length; ++i)
341
- values[i] = liftElement(dataStart + ((i << align) >>> 0));
342
- return values;
343
- }
344
- function __lowerStaticArray(lowerElement, id, align, values, typedConstructor = null) {
345
- if (values == null)
346
- return 0;
347
- const length = values.length;
348
- const buffer = WASM_EXPORTS.__pin(WASM_EXPORTS.__new(length << align, id)) >>> 0;
349
- if (typedConstructor) {
350
- new typedConstructor(WASM_MEMORY.buffer, buffer, length).set(values);
351
- }
352
- else {
353
- for (let i = 0; i < length; i++)
354
- lowerElement(buffer + ((i << align) >>> 0), values[i]);
355
- }
356
- return buffer;
357
- }
358
- function __setU32(ptr, value) {
359
- try {
360
- WASM_DV.setUint32(ptr, value, true);
361
- }
362
- catch {
363
- WASM_DV = new DataView(WASM_MEMORY.buffer);
364
- WASM_DV.setUint32(ptr, value, true);
365
- }
366
- }
367
- function __setF64(ptr, value) {
368
- try {
369
- WASM_DV.setFloat64(ptr, value, true);
370
- }
371
- catch {
372
- WASM_DV = new DataView(WASM_MEMORY.buffer);
373
- WASM_DV.setFloat64(ptr, value, true);
374
- }
375
- }
376
- function __getU32(ptr) {
377
- try {
378
- return WASM_DV.getUint32(ptr, true);
379
- }
380
- catch {
381
- WASM_DV = new DataView(WASM_MEMORY.buffer);
382
- return WASM_DV.getUint32(ptr, true);
383
- }
384
- }
385
- return handledExports;
386
- }
387
- function modeToString(mode) {
388
- if (mode == 1)
389
- return 'cors';
390
- if (mode == 2)
391
- return 'no-cors';
392
- if (mode == 3)
393
- return 'same-origin';
394
- if (mode == 4)
395
- return 'navigate';
396
- return null;
397
- }
@@ -1 +0,0 @@
1
- {"type": "module"}
@@ -1,9 +0,0 @@
1
- interface TimestringOptions {
2
- hoursPerDay?: number;
3
- daysPerWeek?: number;
4
- weeksPerMonth?: number;
5
- monthsPerYear?: number;
6
- daysPerYear?: number;
7
- }
8
- export default function parseTimestring(value: string | number, returnUnit?: string, opts?: TimestringOptions): number;
9
- export {};
@@ -1,83 +0,0 @@
1
- const DEFAULT_OPTS = {
2
- hoursPerDay: 24,
3
- daysPerWeek: 7,
4
- weeksPerMonth: 4,
5
- monthsPerYear: 12,
6
- daysPerYear: 365.25,
7
- };
8
- const UNIT_MAP = {
9
- ms: ['ms', 'milli', 'millisecond', 'milliseconds'],
10
- s: ['s', 'sec', 'secs', 'second', 'seconds'],
11
- m: ['m', 'min', 'mins', 'minute', 'minutes'],
12
- h: ['h', 'hr', 'hrs', 'hour', 'hours'],
13
- d: ['d', 'day', 'days'],
14
- w: ['w', 'week', 'weeks'],
15
- mth: ['mon', 'mth', 'mths', 'month', 'months'],
16
- y: ['y', 'yr', 'yrs', 'year', 'years'],
17
- };
18
- export default function parseTimestring(value, returnUnit, opts) {
19
- const options = Object.assign({}, DEFAULT_OPTS, opts || {});
20
- let strValue;
21
- if (typeof value === 'number') {
22
- strValue = parseInt(value.toString()) + 'ms';
23
- }
24
- else if (value.match(/^[-+]?[0-9.]+$/g)) {
25
- strValue = parseInt(value) + 'ms';
26
- }
27
- else {
28
- strValue = value;
29
- }
30
- let totalSeconds = 0;
31
- const unitValues = getUnitValues(options);
32
- const groups = strValue
33
- .toLowerCase()
34
- .replace(/[^.\w+-]+/g, '')
35
- .match(/[-+]?[0-9.]+[a-z]+/g);
36
- if (groups === null) {
37
- throw new Error(`The value [${strValue}] could not be parsed by timestring`);
38
- }
39
- groups.forEach((group) => {
40
- const valMatch = group.match(/[0-9.]+/g);
41
- const unitMatch = group.match(/[a-z]+/g);
42
- if (valMatch && unitMatch) {
43
- const val = parseFloat(valMatch[0]);
44
- const unit = unitMatch[0];
45
- totalSeconds += getSeconds(val, unit, unitValues);
46
- }
47
- });
48
- if (returnUnit) {
49
- return convert(totalSeconds, returnUnit, unitValues);
50
- }
51
- return totalSeconds;
52
- }
53
- function getUnitValues(opts) {
54
- const unitValues = {
55
- ms: 0.001,
56
- s: 1,
57
- m: 60,
58
- h: 3600,
59
- d: 0,
60
- w: 0,
61
- mth: 0,
62
- y: 0,
63
- };
64
- unitValues.d = opts.hoursPerDay * unitValues.h;
65
- unitValues.w = opts.daysPerWeek * unitValues.d;
66
- unitValues.mth = (opts.daysPerYear / opts.monthsPerYear) * unitValues.d;
67
- unitValues.y = opts.daysPerYear * unitValues.d;
68
- return unitValues;
69
- }
70
- function getUnitKey(unit) {
71
- for (const key of Object.keys(UNIT_MAP)) {
72
- if (UNIT_MAP[key].indexOf(unit) > -1) {
73
- return key;
74
- }
75
- }
76
- throw new Error(`The unit [${unit}] is not supported by timestring`);
77
- }
78
- function getSeconds(value, unit, unitValues) {
79
- return value * unitValues[getUnitKey(unit)];
80
- }
81
- function convert(value, unit, unitValues) {
82
- return value / unitValues[getUnitKey(unit)];
83
- }