Files: aa1510bda0bef9b7d7261249e14f2f553ca0ed32 / codeHandler.js
861 bytesRaw
1 | const Wasm = require('primea-wasm-container') |
2 | |
3 | const defaultHandler = { |
4 | test: (state) => { |
5 | return !state.code |
6 | }, |
7 | init: () => { |
8 | return require('./defaultAgent.js') |
9 | } |
10 | } |
11 | |
12 | const wasm = { |
13 | test: (state) => { |
14 | const code = Buffer.from(state.code) |
15 | return code && code.slice(0, 4).toString() === '\x00asm' |
16 | }, |
17 | init: (code) => { |
18 | return new Wasm(code) |
19 | } |
20 | } |
21 | |
22 | const javascript = { |
23 | test: (state) => { |
24 | return typeof state.code === 'function' |
25 | }, |
26 | init: (state) => { |
27 | return { |
28 | run: state.code |
29 | } |
30 | } |
31 | } |
32 | |
33 | let codeHandlers = exports.handlers = { |
34 | default: defaultHandler, |
35 | wasm: wasm, |
36 | javascript: javascript |
37 | } |
38 | |
39 | exports.init = (code) => { |
40 | for (let name in codeHandlers) { |
41 | try { |
42 | const handler = codeHandlers[name] |
43 | if (handler.test(code)) { |
44 | return handler.init(code) |
45 | } |
46 | } catch (e) {} |
47 | } |
48 | } |
49 |
Built with git-ssb-web