@steerprotocol/app-loader 1.0.0 → 1.0.1

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.
@@ -1,12 +1,13 @@
1
1
  declare function responseHandler(body: ArrayBuffer, statusCode: number, redirected: number, callbackID: number): void;
2
2
  declare function initialize(config: string): void;
3
3
  declare function config(): string;
4
- declare function version(): string;
4
+ declare function version(): number;
5
5
  declare function transform(): string;
6
6
  declare function execute(...params: number[] | string[] | ArrayBuffer[] | null[]): string;
7
7
  declare function __new(size: number, id: number): number;
8
8
  declare function __pin(ptr: number): number;
9
9
  declare function __unpin(ptr: number): number;
10
+ declare function reset(): void;
10
11
  export type WasmModule = {
11
12
  responseHandler: typeof responseHandler;
12
13
  initialize: typeof initialize;
@@ -18,5 +19,6 @@ export type WasmModule = {
18
19
  __new: typeof __new;
19
20
  __pin: typeof __pin;
20
21
  __unpin: typeof __unpin;
22
+ reset: typeof reset;
21
23
  };
22
24
  export {};
package/lib/index.d.ts CHANGED
@@ -9,4 +9,10 @@ export { Candle, RawTradeData, WasmModule };
9
9
  * @returns
10
10
  */
11
11
  export declare function loadWasmSync(input: ArrayBuffer, imports?: {}): WasmModule;
12
+ /**
13
+ * Load a wasm bundle asynchronously. Accepts the actual binary data or file path.
14
+ * @param input - Wasm bundle data or path
15
+ * @param imports - Imports
16
+ * @returns
17
+ */
12
18
  export declare function loadWasm(input: string | ArrayBuffer, imports?: {}): Promise<WasmModule>;
package/lib/index.js CHANGED
@@ -44,10 +44,6 @@ const RawTradeData_1 = require("./RawTradeData");
44
44
  Object.defineProperty(exports, "RawTradeData", { enumerable: true, get: function () { return RawTradeData_1.RawTradeData; } });
45
45
  const ARRAYBUFFER_ID = 1;
46
46
  const STRING_ID = 2;
47
- // Pointers (for asyncify)
48
- let _fetchGETSyncPtr = 0;
49
- let _fetchPOSTSyncPtr = 0;
50
- let _ohlcvPtr = 0;
51
47
  // Globals
52
48
  let WASM_MEMORY;
53
49
  let WASM_EXPORTS;
@@ -56,6 +52,15 @@ let WASM_DV;
56
52
  let ASYNCIFY_PTR = 0;
57
53
  let ASYNCIFY_MEM;
58
54
  let ASYNCIFY_INITIALIZED = false;
55
+ let fetchFn = null;
56
+ let ccxtFn = null;
57
+ var State;
58
+ (function (State) {
59
+ State[State["None"] = 0] = "None";
60
+ State[State["Unwinding"] = 1] = "Unwinding";
61
+ State[State["Rewinding"] = 2] = "Rewinding";
62
+ })(State || (State = {}));
63
+ let detachedValue = null;
59
64
  /**
60
65
  * Load a wasm bundle synchronously. Only accepts the actual binary data.
61
66
  * @param input - Wasm bundle data
@@ -66,6 +71,12 @@ function loadWasmSync(input, imports = {}) {
66
71
  return instantiate(new WebAssembly.Module(input), imports);
67
72
  }
68
73
  exports.loadWasmSync = loadWasmSync;
74
+ /**
75
+ * Load a wasm bundle asynchronously. Accepts the actual binary data or file path.
76
+ * @param input - Wasm bundle data or path
77
+ * @param imports - Imports
78
+ * @returns
79
+ */
69
80
  function loadWasm(input, imports = {}) {
70
81
  return __awaiter(this, void 0, void 0, function* () {
71
82
  const mod = instantiate(yield (() => __awaiter(this, void 0, void 0, function* () {
@@ -107,7 +118,6 @@ function instantiate(module, imports = {}) {
107
118
  return;
108
119
  ASYNCIFY_PTR = frame_ptr;
109
120
  ASYNCIFY_MEM[ASYNCIFY_PTR >> 2] = ASYNCIFY_PTR + 8;
110
- // I don't know if I need to reserve all this memory...
111
121
  ASYNCIFY_MEM[(ASYNCIFY_PTR + 4) >> 2] = stack_ptr;
112
122
  ASYNCIFY_INITIALIZED = true;
113
123
  },
@@ -121,47 +131,33 @@ function instantiate(module, imports = {}) {
121
131
  console.log(__liftString(text));
122
132
  },
123
133
  ccxt_fetchOHLCV: (exchangeId, symbol, timeframe, limit, since) => {
124
- // Since may need to be passed as 64-bit?
125
- // Timestamps are large
126
134
  // @ts-ignore
127
135
  const currentState = WASM_EXPORTS.asyncify_get_state();
128
- if (currentState === 2) {
129
- //console.log("asyncify_stop_rewind() [resume wasm]");
136
+ if (currentState === State.Rewinding) {
130
137
  // @ts-ignore
131
138
  WASM_EXPORTS.asyncify_stop_rewind();
132
- return _ohlcvPtr;
133
- }
134
- else if (currentState === 0) {
135
- //console.log("asyncify_start_unwind() [pause wasm]");
136
139
  // @ts-ignore
137
- WASM_EXPORTS.asyncify_start_unwind(ASYNCIFY_PTR);
138
- // How about we pass an Enum back and forth?
139
- const _exchange = __liftString(exchangeId);
140
- const _symbol = __liftString(symbol);
141
- const _timeframe = __liftString(timeframe);
140
+ const ptr = __lowerStaticArray((pointer, value) => {
141
+ __setU32(pointer, __lowerStaticArray(__setF64, 7, 3, value, Float64Array));
142
+ }, 8, 2, detachedValue);
143
+ return ptr;
144
+ }
145
+ const _exchange = __liftString(exchangeId);
146
+ const _symbol = __liftString(symbol);
147
+ const _timeframe = __liftString(timeframe);
148
+ ccxtFn = (() => __awaiter(this, void 0, void 0, function* () {
142
149
  if (!_exchange || !_symbol || !_timeframe)
143
150
  throw new Error('Exchange, Symbol, or Timeframe not provided when fetching OHCLV data.');
144
151
  // @ts-ignore
145
- new ccxt_1.default[_exchange]({
152
+ const data = yield new ccxt_1.default[_exchange]({
146
153
  apiKey: '',
147
154
  secret: '',
148
- })
149
- .fetchOHLCV(_symbol, _timeframe, since, limit)
150
- .then((data) => {
151
- //console.log("asyncify_stop_unwind() [unpause wasm]");
152
- // @ts-ignore
153
- WASM_EXPORTS.asyncify_stop_unwind();
154
- // @ts-ignore: __new is defined
155
- _ohlcvPtr = __lowerStaticArray((pointer, value) => {
156
- __setU32(pointer, __lowerStaticArray(__setF64, 7, 3, value, Float64Array));
157
- }, 8, 2, data);
158
- //console.log("asyncify_start_rewind() [resuming wasm]");
159
- // @ts-ignore
160
- WASM_EXPORTS.asyncify_start_rewind(ASYNCIFY_PTR);
161
- WASM_EXPORTS.execute();
162
- });
163
- }
164
- },
155
+ }).fetchOHLCV(_symbol, _timeframe, since, limit);
156
+ return data;
157
+ }));
158
+ // @ts-ignore
159
+ WASM_EXPORTS.asyncify_start_unwind(ASYNCIFY_PTR);
160
+ }
165
161
  },
166
162
  'as-fetch': {
167
163
  _initAsyncify(frame_ptr, stack_ptr) {
@@ -177,77 +173,49 @@ function instantiate(module, imports = {}) {
177
173
  ASYNCIFY_INITIALIZED = true;
178
174
  },
179
175
  _fetchPOSTSync(url, mode, headers, body) {
180
- var _a;
181
176
  // @ts-ignore
182
177
  const currentState = WASM_EXPORTS.asyncify_get_state();
183
- if (currentState === 2) {
184
- //console.log("asyncify_stop_rewind() [resume wasm]");
178
+ if (currentState === State.Rewinding) {
185
179
  // @ts-ignore
186
180
  WASM_EXPORTS.asyncify_stop_rewind();
187
- const ptr = _fetchPOSTSyncPtr;
188
- _fetchPOSTSyncPtr = 0;
181
+ const ptr = __lowerBuffer(detachedValue);
182
+ detachedValue = null;
189
183
  return ptr;
190
184
  }
191
- else if (currentState === 0) {
192
- //console.log("asyncify_start_unwind() [pause wasm]");
193
- // @ts-ignore
194
- WASM_EXPORTS.asyncify_start_unwind(ASYNCIFY_PTR);
195
- fetch((_a = __liftString(url)) !== null && _a !== void 0 ? _a : '', {
185
+ fetchFn = (() => __awaiter(this, void 0, void 0, function* () {
186
+ const res = yield fetch(__liftString(url) || '', {
196
187
  method: 'POST',
197
188
  mode: modeToString(mode) || 'cors',
198
189
  headers: __liftArray((pointer) => __liftArray((pointer) => __liftString(__getU32(pointer)), 2, __getU32(pointer)), 2, headers) || [],
199
- body: __liftBuffer(body),
200
- }).then((res) => __awaiter(this, void 0, void 0, function* () {
201
- const value = yield res.arrayBuffer();
202
- //console.log("asyncify_stop_unwind() [unpause wasm]");
203
- // @ts-ignore
204
- WASM_EXPORTS.asyncify_stop_unwind();
205
- // @ts-ignore
206
- _fetchPOSTSyncPtr = __lowerBuffer(value);
207
- //wasm_exports.__new(value.byteLength, 1);
208
- //new Uint8Array(wasm_exports.memory.buffer).set(new Uint8Array(value), _fetchPOSTSyncPtr);
209
- //console.log("asyncify_start_rewind() [resuming wasm]");
210
- // @ts-ignore
211
- WASM_EXPORTS.asyncify_start_rewind(ASYNCIFY_PTR);
212
- // @ts-ignore
213
- // Set to your execute function that your fetch calls are in. I would go and create a _start func
214
- WASM_EXPORTS.execute();
215
- }));
216
- }
190
+ body: __liftBuffer(body)
191
+ });
192
+ const value = yield res.arrayBuffer();
193
+ return value;
194
+ }));
195
+ // @ts-ignore
196
+ WASM_EXPORTS.asyncify_start_unwind(ASYNCIFY_PTR);
217
197
  },
218
198
  _fetchGETSync(url, mode, headers) {
219
199
  // @ts-ignore
220
200
  const currentState = WASM_EXPORTS.asyncify_get_state();
221
- if (currentState === 2) {
201
+ if (currentState === State.Rewinding) {
222
202
  // @ts-ignore
223
- //console.log("asyncify_stop_rewind() [resume wasm]");
224
203
  WASM_EXPORTS.asyncify_stop_rewind();
225
- const ptr = _fetchGETSyncPtr;
226
- _fetchGETSyncPtr = 0;
204
+ const ptr = __lowerBuffer(detachedValue);
205
+ detachedValue = null;
227
206
  return ptr;
228
207
  }
229
- else if (currentState === 0) {
230
- // @ts-ignore
231
- //console.log("asyncify_start_unwind() [pause wasm]");
232
- WASM_EXPORTS.asyncify_start_unwind(ASYNCIFY_PTR);
233
- fetch(__liftString(url) || '', {
208
+ fetchFn = (() => __awaiter(this, void 0, void 0, function* () {
209
+ const res = yield fetch(__liftString(url) || '', {
234
210
  method: 'GET',
235
211
  mode: modeToString(mode) || 'cors',
236
212
  headers: __liftArray((pointer) => __liftArray((pointer) => __liftString(__getU32(pointer)), 2, __getU32(pointer)), 2, headers) || [],
237
- }).then((res) => __awaiter(this, void 0, void 0, function* () {
238
- const value = yield res.arrayBuffer();
239
- // @ts-ignore
240
- //console.log("asyncify_stop_unwind() [unpause wasm]");
241
- WASM_EXPORTS.asyncify_stop_unwind();
242
- // @ts-ignore
243
- _fetchGETSyncPtr = __lowerBuffer(value);
244
- // @ts-ignore
245
- //console.log("asyncify_start_rewind() [resuming wasm]");
246
- WASM_EXPORTS.asyncify_start_rewind(ASYNCIFY_PTR);
247
- // @ts-ignore
248
- WASM_EXPORTS.execute();
249
- }));
250
- }
213
+ });
214
+ const value = yield res.arrayBuffer();
215
+ return value;
216
+ }));
217
+ // @ts-ignore
218
+ WASM_EXPORTS.asyncify_start_unwind(ASYNCIFY_PTR);
251
219
  },
252
220
  _fetchGET(url, mode, headers, callbackID) {
253
221
  fetch(__liftString(url) || '', {
@@ -289,23 +257,61 @@ function instantiate(module, imports = {}) {
289
257
  }
290
258
  },
291
259
  initialize(config) {
292
- // @ts-ignore
293
- WASM_EXPORTS.initialize(__lowerString(config));
294
- },
295
- execute(...params) {
296
- const loweredArgs = new Array(params.length);
297
- for (let i = 0; i < params.length; i++) {
298
- loweredArgs[i] = __lowerRetained(params[i]);
260
+ try {
261
+ // @ts-ignore
262
+ WASM_EXPORTS.initialize(__lowerStringRetained(config));
299
263
  }
300
- for (let i = 0; i < loweredArgs.length; i++) {
301
- if (typeof params[i] !== 'number')
302
- __release(loweredArgs[i]);
264
+ catch (e) {
265
+ console.error(e);
266
+ throw new Error('Unable to call .initialize on wasm module. Are you sure this is a data connector?');
303
267
  }
304
- // @ts-ignore
305
- return __liftString(WASM_EXPORTS.execute(...loweredArgs));
268
+ },
269
+ execute(...params) {
270
+ return __awaiter(this, void 0, void 0, function* () {
271
+ try {
272
+ const loweredArgs = new Array(params.length);
273
+ for (let i = 0; i < params.length; i++) {
274
+ loweredArgs[i] = __lowerRetained(params[i]);
275
+ }
276
+ /*for (let i = 0; i < loweredArgs.length; i++) {
277
+ if (typeof params[i] !== 'number') __release(loweredArgs[i]);
278
+ }*/
279
+ let result = WASM_EXPORTS.execute(...loweredArgs);
280
+ if (ASYNCIFY_INITIALIZED) {
281
+ // @ts-ignore
282
+ while (WASM_EXPORTS.asyncify_get_state() === State.Unwinding) {
283
+ // @ts-ignore
284
+ WASM_EXPORTS.asyncify_stop_unwind();
285
+ if (fetchFn)
286
+ detachedValue = yield fetchFn();
287
+ if (ccxtFn)
288
+ detachedValue = yield ccxtFn();
289
+ // @ts-ignore
290
+ WASM_EXPORTS.asyncify_start_rewind(ASYNCIFY_PTR);
291
+ result = WASM_EXPORTS.execute();
292
+ }
293
+ }
294
+ if (fetchFn)
295
+ fetchFn = null;
296
+ if (ccxtFn)
297
+ ccxtFn = null;
298
+ // @ts-ignore
299
+ return result ? __liftString(result) : null;
300
+ }
301
+ catch (e) {
302
+ console.error(e);
303
+ throw new Error('Unable to call .execute on wasm module. Are you sure this is a data connector?');
304
+ }
305
+ });
306
306
  },
307
307
  config() {
308
- return __liftString(WASM_EXPORTS.config());
308
+ try {
309
+ return __liftString(WASM_EXPORTS.config());
310
+ }
311
+ catch (e) {
312
+ console.error(e);
313
+ throw new Error('Unable to call .config on wasm module. Are you sure this is a data connector?');
314
+ }
309
315
  },
310
316
  transform() {
311
317
  try {
@@ -316,6 +322,14 @@ function instantiate(module, imports = {}) {
316
322
  throw new Error('Unable to call .transform on wasm module. Are you sure this is a data connector?');
317
323
  }
318
324
  },
325
+ reset() {
326
+ try {
327
+ WASM_EXPORTS.reset();
328
+ }
329
+ catch (e) {
330
+ console.log(e);
331
+ }
332
+ }
319
333
  }, WASM_EXPORTS);
320
334
  WASM_DV = new DataView(WASM_MEMORY.buffer);
321
335
  return handledExports;
@@ -331,6 +345,7 @@ function __retain(ptr) {
331
345
  }
332
346
  return ptr;
333
347
  }
348
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars
334
349
  function __release(ptr) {
335
350
  if (ptr) {
336
351
  const refcount = refcounts.get(ptr);
@@ -391,6 +406,7 @@ function __lowerStringRetained(str) {
391
406
  memoryU16[(ptr >>> 1) + i] = str.charCodeAt(i);
392
407
  return ptr;
393
408
  }
409
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
394
410
  function __liftArray(liftElement, align, ptr) {
395
411
  if (!ptr)
396
412
  return null;
@@ -408,6 +424,7 @@ function __liftArray(liftElement, align, ptr) {
408
424
  values[i] = liftElement(dataStart + ((i << align) >>> 0));
409
425
  return values;
410
426
  }
427
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars
411
428
  function __lowerArray(lowerElement, id, align, values) {
412
429
  if (values == null)
413
430
  return 0;
@@ -445,6 +462,7 @@ function __lowerBufferRetained(buf) {
445
462
  new Uint8Array(WASM_MEMORY.buffer).set(new Uint8Array(buf), ptr);
446
463
  return ptr;
447
464
  }
465
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
448
466
  function __lowerStaticArray(lowerElement, id, align, values, typedConstructor) {
449
467
  if (values == null)
450
468
  return 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steerprotocol/app-loader",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "App Loader for Steer Protocol",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",