Skip to content

Commit

Permalink
Merge pull request #1575 from demergent-labs/node_apis
Browse files Browse the repository at this point in the history
Node apis
  • Loading branch information
lastmjs authored Jan 19, 2024
2 parents 04c583e + f794d9c commit be60909
Show file tree
Hide file tree
Showing 13 changed files with 404 additions and 36 deletions.
381 changes: 374 additions & 7 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@dfinity/principal": "^0.19.3",
"@types/uuid": "^9.0.4",
"buffer": "^6.0.3",
"crypto-browserify": "^3.12.0",
"esbuild": "^0.19.3",
"fs-extra": "10.0.1",
"js-sha256": "0.9.0",
Expand Down
14 changes: 12 additions & 2 deletions src/compiler/compile_typescript_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export function bundleFromString(ts: TypeScript): JavaScript {
util: `${GLOBAL_AZLE_WASMEDGE_QUICKJS_DIR}/modules/util`,
fs: `${GLOBAL_AZLE_WASMEDGE_QUICKJS_DIR}/modules/fs`,
fmt: `${GLOBAL_AZLE_WASMEDGE_QUICKJS_DIR}/modules/fmt`,
assert: `${GLOBAL_AZLE_WASMEDGE_QUICKJS_DIR}/modules/assert.js`,
buffer: `${GLOBAL_AZLE_WASMEDGE_QUICKJS_DIR}/modules/buffer.js`,
path: `${GLOBAL_AZLE_WASMEDGE_QUICKJS_DIR}/modules/path.js`,
stream: `${GLOBAL_AZLE_WASMEDGE_QUICKJS_DIR}/modules/stream.js`,
Expand All @@ -81,7 +82,10 @@ export function bundleFromString(ts: TypeScript): JavaScript {
punycode: `${GLOBAL_AZLE_WASMEDGE_QUICKJS_DIR}/modules/punycode.js`,
querystring: `${GLOBAL_AZLE_WASMEDGE_QUICKJS_DIR}/modules/querystring.js`,
whatwg_url: `${GLOBAL_AZLE_WASMEDGE_QUICKJS_DIR}/modules/whatwg_url.js`,
encoding: `${GLOBAL_AZLE_WASMEDGE_QUICKJS_DIR}/modules/encoding.js`
encoding: `${GLOBAL_AZLE_WASMEDGE_QUICKJS_DIR}/modules/encoding.js`,
crypto: 'crypto-browserify',
'internal/deps/acorn/acorn/dist/acorn': `crypto-browserify`, // TODO this is a bug, wasmedge-quickjs should probably add these files
'internal/deps/acorn/acorn-walk/dist/walk': `crypto-browserify` // TODO this is a bug, wasmedge-quickjs should probably add these files
},
external: ['_node:fs', '_encoding']
// TODO tsconfig was here to attempt to set importsNotUsedAsValues to true to force Principal to always be bundled
Expand All @@ -92,5 +96,11 @@ export function bundleFromString(ts: TypeScript): JavaScript {
const bundleArray = buildResult.outputFiles[0].contents;
const bundleString = Buffer.from(bundleArray).toString('utf-8');

return bundleString;
// TODO consuming code tries to require assert.js which is now an ES module
// TODO in wasmedge-quickjs, so the expected output is now on the .default property
// TODO this has only come up with assert for now
return bundleString.replace(
/__toCommonJS\(assert_exports\)\);/g,
`__toCommonJS(assert_exports)).default;`
);
}
2 changes: 0 additions & 2 deletions src/compiler/rust/canister/src/ic/accept_message.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::convert::TryFrom;

use wasmedge_quickjs::{Context, JsFn, JsValue};

pub struct NativeFunction;
Expand Down
2 changes: 0 additions & 2 deletions src/compiler/rust/canister/src/ic/arg_data_raw_size.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::convert::TryFrom;

use wasmedge_quickjs::{Context, JsFn, JsValue};

pub struct NativeFunction;
Expand Down
2 changes: 0 additions & 2 deletions src/compiler/rust/canister/src/ic/candid_decode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::convert::TryInto;

use wasmedge_quickjs::{Context, JsFn, JsValue};

pub struct NativeFunction;
Expand Down
2 changes: 0 additions & 2 deletions src/compiler/rust/canister/src/ic/candid_encode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::convert::TryInto;

use wasmedge_quickjs::{Context, JsArrayBuffer, JsFn, JsValue};

pub struct NativeFunction;
Expand Down
2 changes: 0 additions & 2 deletions src/compiler/rust/canister/src/ic/reply_raw.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::convert::TryInto;

use wasmedge_quickjs::{Context, JsArrayBuffer, JsFn, JsValue};

pub struct NativeFunction;
Expand Down
2 changes: 0 additions & 2 deletions src/compiler/rust/canister/src/ic/set_timer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::convert::TryInto;

use slotmap::Key;
use wasmedge_quickjs::{AsObject, Context, JsFn, JsValue};

Expand Down
2 changes: 0 additions & 2 deletions src/compiler/rust/canister/src/ic/set_timer_interval.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::convert::TryInto;

use slotmap::Key;
use wasmedge_quickjs::{AsObject, Context, JsFn, JsValue};

Expand Down
2 changes: 0 additions & 2 deletions src/compiler/rust/canister/src/ic/stable64_write.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::convert::TryInto;

use wasmedge_quickjs::{Context, JsFn, JsValue};

pub struct NativeFunction;
Expand Down
2 changes: 0 additions & 2 deletions src/compiler/rust/canister/src/ic/stable_write.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::convert::TryInto;

use wasmedge_quickjs::{Context, JsFn, JsValue};

pub struct NativeFunction;
Expand Down
26 changes: 17 additions & 9 deletions src/lib/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ globalThis._azleInsideCanister =
globalThis._azleIc === undefined ? false : true;

if (globalThis._azleInsideCanister) {
const log = (...args: any[]) => {
const jsonStringifiedArgs = args
.map((arg) => JSON.stringify(arg, replacer, 4))
.join(' ');

ic.print(jsonStringifiedArgs);
};

globalThis.console = {
...globalThis.console,
log: (...args: any[]) => {
const jsonStringifiedArgs = args
.map((arg) => JSON.stringify(arg, replacer, 4))
.join(' ');

ic.print(jsonStringifiedArgs);
}
log,
error: log
};

const originalSetTimeout = setTimeout;
Expand All @@ -41,6 +44,7 @@ if (globalThis._azleInsideCanister) {
}

// TODO change this to throw once errors throw and show up properly
// TODO should this throw an error or just not do anything? At least a warning would be good right?
ic.trap(`setTimeout cannot be called with milliseconds above 0`);
};
}
Expand All @@ -57,8 +61,9 @@ globalThis._azleGuardFunctions = {};
// TODO the randomness is predictable
globalThis.crypto = {
...globalThis.crypto,
getRandomValues: (() => {
let array = new Uint8Array(32);
getRandomValues: ((array: Uint8Array) => {
// TODO the type is wrong of array
// TODO this could possibly be any kind of TypedArray

for (let i = 0; i < array.length; i++) {
array[i] = Math.floor(Math.random() * 256);
Expand All @@ -71,3 +76,6 @@ globalThis.crypto = {
globalThis.Buffer = Buffer;

globalThis.process = process;
globalThis.clearInterval = () => {}; // TODO should this throw an error or just not do anything? At least a warning would be good right?

globalThis.global = globalThis;

0 comments on commit be60909

Please sign in to comment.