|
| 1 | +import { encode, decode } from 'bs58'; |
| 2 | +import { |
| 3 | + CallServiceData, |
| 4 | + CallServiceHandler, |
| 5 | + CallServiceResult, |
| 6 | + CallServiceResultType, |
| 7 | + errorHandler, |
| 8 | + Middleware, |
| 9 | +} from './CallServiceHandler'; |
| 10 | + |
| 11 | +const makeDefaultClientHandler = (): CallServiceHandler => { |
| 12 | + const success = (resp: CallServiceResult, result: CallServiceResultType) => { |
| 13 | + resp.retCode = 0; |
| 14 | + resp.result = result; |
| 15 | + }; |
| 16 | + const error = (resp: CallServiceResult, errorMsg: string) => { |
| 17 | + resp.retCode = 1; |
| 18 | + resp.result = errorMsg; |
| 19 | + }; |
| 20 | + const mw: Middleware = (req: CallServiceData, resp: CallServiceResult, next: Function) => { |
| 21 | + if (req.serviceId === 'Op') { |
| 22 | + switch (req.fnName) { |
| 23 | + case 'noop': |
| 24 | + success(resp, {}); |
| 25 | + return; |
| 26 | + |
| 27 | + case 'array': |
| 28 | + success(resp, req.args); |
| 29 | + return; |
| 30 | + |
| 31 | + case 'identity': |
| 32 | + if (req.args.length > 1) { |
| 33 | + error(resp, `identity accepts up to 1 arguments, received ${req.args.length} arguments`); |
| 34 | + } else { |
| 35 | + success(resp, req.args.length === 0 ? {} : req.args[0]); |
| 36 | + } |
| 37 | + return; |
| 38 | + |
| 39 | + case 'concat': |
| 40 | + const incorrectArgIndices = req.args // |
| 41 | + .map((x, i) => [Array.isArray(x), i]) |
| 42 | + .filter(([isArray, _]) => !isArray) |
| 43 | + .map(([_, index]) => index); |
| 44 | + |
| 45 | + if (incorrectArgIndices.length > 0) { |
| 46 | + const str = incorrectArgIndices.join(', '); |
| 47 | + error(resp, `All arguments of 'concat' must be arrays: arguments ${str} are not`); |
| 48 | + } else { |
| 49 | + success(resp, [].concat.apply([], req.args)); |
| 50 | + } |
| 51 | + return; |
| 52 | + |
| 53 | + case 'string_to_b58': |
| 54 | + if (req.args.length !== 1) { |
| 55 | + error(resp, 'string_to_b58 accepts only one string argument'); |
| 56 | + } else { |
| 57 | + success(resp, encode(new TextEncoder().encode(req.args[0]))); |
| 58 | + } |
| 59 | + return; |
| 60 | + |
| 61 | + case 'string_from_b58': |
| 62 | + if (req.args.length !== 1) { |
| 63 | + error(resp, 'string_from_b58 accepts only one string argument'); |
| 64 | + } else { |
| 65 | + success(resp, new TextDecoder().decode(decode(req.args[0]))); |
| 66 | + } |
| 67 | + return; |
| 68 | + |
| 69 | + case 'bytes_to_b58': |
| 70 | + if (req.args.length !== 1 || !Array.isArray(req.args[0])) { |
| 71 | + error(resp, 'bytes_to_b58 accepts only single argument: array of numbers'); |
| 72 | + } else { |
| 73 | + const argumentArray = req.args[0] as number[]; |
| 74 | + success(resp, encode(new Uint8Array(argumentArray))); |
| 75 | + } |
| 76 | + return; |
| 77 | + |
| 78 | + case 'bytes_from_b58': |
| 79 | + if (req.args.length !== 1) { |
| 80 | + error(resp, 'bytes_from_b58 accepts only one string argument'); |
| 81 | + } else { |
| 82 | + success(resp, Array.from(decode(req.args[0]))); |
| 83 | + } |
| 84 | + return; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + next(); |
| 89 | + }; |
| 90 | + |
| 91 | + const res = new CallServiceHandler(); |
| 92 | + res.use(errorHandler); |
| 93 | + res.use(mw); |
| 94 | + return res; |
| 95 | +}; |
| 96 | + |
| 97 | +export default makeDefaultClientHandler; |
0 commit comments