This repository was archived by the owner on Jul 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Implement builtins #52
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ccdfed3
implement additional builtins
coder11 4902cac
some refactorings
coder11 a9a324d
implement some of builtins + tests
coder11 90a83c0
moar tests
coder11 c38dadf
implement bytes to b58 operations
coder11 56cca75
tidy up tests
coder11 e5beb78
fix identity usage
coder11 8d4253a
fix comment issues
coder11 cd6d212
fix tests
coder11 6fcdd28
Add additional checks
coder11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import each from 'jest-each'; | ||
| import { CallServiceData } from '../../internal/CallServiceHandler'; | ||
| import makeDefaultClientHandler from '../../internal/defaultClientHandler'; | ||
|
|
||
| describe('Tests for default handler', () => { | ||
| // prettier-ignore | ||
| each` | ||
| fnName | args | retCode | result | ||
| ${'identity'} | ${[]} | ${0} | ${{}} | ||
| ${'identity'} | ${[1]} | ${0} | ${1} | ||
| ${'identity'} | ${[1, 2]} | ${1} | ${'identity accepts up to 1 arguments, received 2 arguments'} | ||
|
|
||
| ${'noop'} | ${[1, 2]} | ${0} | ${{}} | ||
|
|
||
| ${'array'} | ${[1, 2, 3]} | ${0} | ${[1, 2, 3]} | ||
|
|
||
| ${'concat'} | ${[[1, 2], [3, 4], [5, 6]]} | ${0} | ${[1, 2, 3, 4, 5, 6]} | ||
| ${'concat'} | ${[[1, 2]]} | ${0} | ${[1, 2]} | ||
| ${'concat'} | ${[]} | ${0} | ${[]} | ||
| ${'concat'} | ${[1, [1, 2], 1]} | ${1} | ${"All arguments of 'concat' must be arrays: arguments 0, 2 are not"} | ||
|
|
||
| ${'string_to_b58'} | ${["test"]} | ${0} | ${"3yZe7d"} | ||
| ${'string_to_b58'} | ${["test", 1]} | ${1} | ${"string_to_b58 accepts only one string argument"} | ||
|
|
||
| ${'string_from_b58'} | ${["3yZe7d"]} | ${0} | ${"test"} | ||
| ${'string_from_b58'} | ${["3yZe7d", 1]} | ${1} | ${"string_from_b58 accepts only one string argument"} | ||
|
|
||
| ${'bytes_to_b58'} | ${[[116, 101, 115, 116]]} | ${0} | ${"3yZe7d"} | ||
| ${'bytes_to_b58'} | ${[[116, 101, 115, 116], 1]} | ${1} | ${"bytes_to_b58 accepts only single argument: array of numbers"} | ||
|
|
||
| ${'bytes_from_b58'} | ${["3yZe7d"]} | ${0} | ${[116, 101, 115, 116]} | ||
| ${'bytes_from_b58'} | ${["3yZe7d", 1]} | ${1} | ${"bytes_from_b58 accepts only one string argument"} | ||
|
|
||
| `.test( | ||
| // | ||
| '$fnName with $args expected retcode: $retCode and result: $result', | ||
| ({ fnName, args, retCode, result }) => { | ||
| // arrange | ||
| const req: CallServiceData = { | ||
| serviceId: 'Op', | ||
| fnName: fnName, | ||
| args: args, | ||
| tetraplets: [], | ||
| particleContext: { | ||
| particleId: 'some', | ||
| }, | ||
| }; | ||
|
|
||
| // act | ||
| const res = makeDefaultClientHandler().execute(req); | ||
|
|
||
| // assert | ||
| expect(res).toMatchObject({ | ||
| retCode: retCode, | ||
| result: result, | ||
| }); | ||
| const handler = makeDefaultClientHandler(); | ||
| }, | ||
| ); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { encode, decode } from 'bs58'; | ||
| import { | ||
| CallServiceData, | ||
| CallServiceHandler, | ||
| CallServiceResult, | ||
| CallServiceResultType, | ||
| errorHandler, | ||
| Middleware, | ||
| } from './CallServiceHandler'; | ||
|
|
||
| const makeDefaultClientHandler = (): CallServiceHandler => { | ||
| const success = (resp: CallServiceResult, result: CallServiceResultType) => { | ||
| resp.retCode = 0; | ||
| resp.result = result; | ||
| }; | ||
| const error = (resp: CallServiceResult, errorMsg: string) => { | ||
| resp.retCode = 1; | ||
| resp.result = errorMsg; | ||
| }; | ||
| const mw: Middleware = (req: CallServiceData, resp: CallServiceResult, next: Function) => { | ||
| if (req.serviceId === 'Op') { | ||
| switch (req.fnName) { | ||
| case 'noop': | ||
| success(resp, {}); | ||
| return; | ||
|
|
||
| case 'array': | ||
| success(resp, req.args); | ||
| return; | ||
|
|
||
| case 'identity': | ||
| if (req.args.length > 1) { | ||
| error(resp, `identity accepts up to 1 arguments, received ${req.args.length} arguments`); | ||
| } else { | ||
| success(resp, req.args.length === 0 ? {} : req.args[0]); | ||
| } | ||
| return; | ||
|
|
||
| case 'concat': | ||
| const incorrectArgIndices = req.args // | ||
| .map((x, i) => [Array.isArray(x), i]) | ||
| .filter(([isArray, _]) => !isArray) | ||
| .map(([_, index]) => index); | ||
|
|
||
| if (incorrectArgIndices.length > 0) { | ||
| const str = incorrectArgIndices.join(', '); | ||
| error(resp, `All arguments of 'concat' must be arrays: arguments ${str} are not`); | ||
| } else { | ||
| success(resp, [].concat.apply([], req.args)); | ||
| } | ||
| return; | ||
|
|
||
| case 'string_to_b58': | ||
| if (req.args.length !== 1) { | ||
| error(resp, 'string_to_b58 accepts only one string argument'); | ||
| } else { | ||
| success(resp, encode(new TextEncoder().encode(req.args[0]))); | ||
| } | ||
| return; | ||
|
|
||
| case 'string_from_b58': | ||
| if (req.args.length !== 1) { | ||
| error(resp, 'string_from_b58 accepts only one string argument'); | ||
| } else { | ||
| success(resp, new TextDecoder().decode(decode(req.args[0]))); | ||
| } | ||
| return; | ||
|
|
||
| case 'bytes_to_b58': | ||
| if (req.args.length !== 1 || !Array.isArray(req.args[0])) { | ||
| error(resp, 'bytes_to_b58 accepts only single argument: array of numbers'); | ||
| } else { | ||
| const argumentArray = req.args[0] as number[]; | ||
| success(resp, encode(new Uint8Array(argumentArray))); | ||
| } | ||
| return; | ||
|
|
||
| case 'bytes_from_b58': | ||
| if (req.args.length !== 1) { | ||
| error(resp, 'bytes_from_b58 accepts only one string argument'); | ||
| } else { | ||
| success(resp, Array.from(decode(req.args[0]))); | ||
| } | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| next(); | ||
| }; | ||
|
|
||
| const res = new CallServiceHandler(); | ||
| res.use(errorHandler); | ||
| res.use(mw); | ||
| return res; | ||
| }; | ||
|
|
||
| export default makeDefaultClientHandler; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow, cool :)