Skip to content

Commit c0c1792

Browse files
authored
Merge pull request #46 from smartcontractkit/imports
2 parents 2867e8d + 540f3c3 commit c0c1792

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

.changeset/pretty-countries-look.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@chainlink/functions-toolkit': patch
3+
---
4+
5+
Added support for 3rd party imports in the simulator

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,17 @@ const result = await simulateScript({
572572
}
573573
```
574574

575+
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.
576+
577+
```
578+
const { format } = await import("npm:date-fns");
579+
return Functions.encodeString(format(new Date(), "yyyy-MM-dd"));
580+
```
581+
```
582+
const { escape } = await import("https://deno.land/std/regexp/mod.ts");
583+
return Functions.encodeString(escape("$hello*world?"));
584+
```
585+
575586
**_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.
576587

577588
**_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.

src/simulateScript/simulateScript.ts

-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ export const simulateScript = async ({
8686
const simulation = spawn('deno', [
8787
'run',
8888
'--no-prompt',
89-
'--no-npm',
90-
'--no-remote',
9189
`--v8-flags=--max-old-space-size=${maxMemoryUsageMb}`,
9290
'--allow-net',
9391
scriptPath,

test/unit/simulateScript.test.ts

+22-15
Original file line numberDiff line numberDiff line change
@@ -207,21 +207,6 @@ describe('simulateScript', () => {
207207
expect(result).toEqual(expected)
208208
})
209209

210-
it('should capture import error', async () => {
211-
const result = await simulateScript({
212-
source: 'const http = await import("https://deno.land/std/http/mod.ts");',
213-
maxExecutionTimeMs: 100,
214-
})
215-
216-
const expected = {
217-
capturedTerminalOutput: '',
218-
errorString:
219-
'A remote specifier was requested: "https://deno.land/std/http/mod.ts", but --no-remote is specified.',
220-
}
221-
222-
expect(result).toEqual(expected)
223-
})
224-
225210
it('should capture permissions error', async () => {
226211
const result = await simulateScript({
227212
source: "Deno.openSync('test.txt')",
@@ -296,6 +281,28 @@ describe('simulateScript', () => {
296281

297282
await expect(result).rejects.toThrow('bytesArgs param contains invalid hex string')
298283
})
284+
285+
it('should allow 3rd party imports', async () => {
286+
const result = await simulateScript({
287+
source:
288+
'const { escape } = await import("https://deno.land/std/regexp/mod.ts"); return Functions.encodeString(escape("$hello*world?"));',
289+
})
290+
291+
expect(result.responseBytesHexstring).toEqual(
292+
`0x${Buffer.from('\\$hello\\*world\\?').toString('hex')}`,
293+
)
294+
})
295+
296+
it('should allow NPM imports', async () => {
297+
const result = await simulateScript({
298+
source:
299+
'const { format } = await import("npm:date-fns"); return Functions.encodeString(format(new Date(), "yyyy-MM-dd"));',
300+
})
301+
302+
expect(Buffer.from(result.responseBytesHexstring?.slice(2) as string, 'hex').length).toEqual(
303+
10,
304+
)
305+
})
299306
})
300307
})
301308

0 commit comments

Comments
 (0)