Skip to content

Commit

Permalink
Merge branch 'main' into chore/build-browser-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ernest-nowacki authored Dec 4, 2023
2 parents 8a803db + e98a9b0 commit 511d893
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @chainlink/functions-toolkit

## 0.2.7

### Patch Changes

- [#46](https://github.com/smartcontractkit/functions-toolkit/pull/46) [`d570bf8`](https://github.com/smartcontractkit/functions-toolkit/commit/d570bf8363a08056b442dc7a0437449152aa7dda) Thanks [@KuphJr](https://github.com/KuphJr)! - Added support for 3rd party imports in the simulator

## 0.2.6

### Patch Changes
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,17 @@ const result = await simulateScript({
}
```

Any 3rd party imports used in the JavaScript source code are loaded asynchronously at runtime. Therefore, to use 3rd party imports in the source code that is executed by the `simulateScript` function, you must use the async `import` function as shown in the examples below.

```
const { format } = await import("npm:date-fns");
return Functions.encodeString(format(new Date(), "yyyy-MM-dd"));
```
```
const { escape } = await import("https://deno.land/std/regexp/mod.ts");
return Functions.encodeString(escape("$hello*world?"));
```

**_NOTE:_** When running `simulateScript`, depending on your security settings, you may get a popup asking if you would like to accept incoming network connections. You can safely accept or ignore this popup and it should disappear when the simulation is complete.

**_NOTE:_** The `simulateScript` function is a debugging tool and hence is not a perfect representation of the actual Chainlink oracle execution environment. Therefore, it is important to make a Functions request on a supported testnet blockchain before mainnet usage.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@chainlink/functions-toolkit",
"private": false,
"version": "0.2.6",
"version": "0.2.7",
"description": "An NPM package with collection of functions that can be used for working with Chainlink Functions.",
"main": "./dist/index.js",
"scripts": {
Expand Down
2 changes: 0 additions & 2 deletions src/simulateScript/simulateScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ export const simulateScript = async ({
const simulation = spawn('deno', [
'run',
'--no-prompt',
'--no-npm',
'--no-remote',
`--v8-flags=--max-old-space-size=${maxMemoryUsageMb}`,
'--allow-net',
scriptPath,
Expand Down
37 changes: 22 additions & 15 deletions test/unit/simulateScript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,21 +207,6 @@ describe('simulateScript', () => {
expect(result).toEqual(expected)
})

it('should capture import error', async () => {
const result = await simulateScript({
source: 'const http = await import("https://deno.land/std/http/mod.ts");',
maxExecutionTimeMs: 100,
})

const expected = {
capturedTerminalOutput: '',
errorString:
'A remote specifier was requested: "https://deno.land/std/http/mod.ts", but --no-remote is specified.',
}

expect(result).toEqual(expected)
})

it('should capture permissions error', async () => {
const result = await simulateScript({
source: "Deno.openSync('test.txt')",
Expand Down Expand Up @@ -296,6 +281,28 @@ describe('simulateScript', () => {

await expect(result).rejects.toThrow('bytesArgs param contains invalid hex string')
})

it('should allow 3rd party imports', async () => {
const result = await simulateScript({
source:
'const { escape } = await import("https://deno.land/std/regexp/mod.ts"); return Functions.encodeString(escape("$hello*world?"));',
})

expect(result.responseBytesHexstring).toEqual(
`0x${Buffer.from('\\$hello\\*world\\?').toString('hex')}`,
)
})

it('should allow NPM imports', async () => {
const result = await simulateScript({
source:
'const { format } = await import("npm:date-fns"); return Functions.encodeString(format(new Date(), "yyyy-MM-dd"));',
})

expect(Buffer.from(result.responseBytesHexstring?.slice(2) as string, 'hex').length).toEqual(
10,
)
})
})
})

Expand Down

0 comments on commit 511d893

Please sign in to comment.