1- import { isFunction , isInteger , is , isArray } from '../util/is'
1+ import { isFunction , isInteger , isArray } from '../util/is'
22import createRequest from '../util/createRequest'
33import localProcedureCall from '../util/localProcedureCall'
4+ import converter from '../util/converter'
45import { NAME_REMOTE_FUNCTION } from '../const'
6+ import { getUniqueKey } from '../util/get'
57
6- export default function createNodeFactory ( DJSON ) {
7- const Func = DJSON . Function
8- const stringify = DJSON . stringify
9- const parse = DJSON . parse
10-
11- return function createNode ( ) {
8+ export default function createNodeFactory ( { encoders, decoders } ) {
9+ return function createNode ( {
10+ serialize = v => v ,
11+ deserialize = v => v
12+ } = { } ) {
1213 const requests = { }
1314 const local_functions_id = { }
1415 const local_functions_map = new Map ( )
@@ -35,7 +36,7 @@ export default function createNodeFactory(DJSON) {
3536 req . node = api
3637 req . destroy = ( ) => delete requests [ request_id ]
3738 requests [ request_id ] = req
38- api . send ( stringify ( data , stringifyReplacer ) )
39+ api . send ( serialize ( encode ( data ) ) )
3940 return req
4041 }
4142 Object . defineProperty ( f , 'name' , {
@@ -56,12 +57,11 @@ export default function createNodeFactory(DJSON) {
5657
5758 function message ( msg ) {
5859 // console.log(api.ENV, msg)
59- const tof = is ( msg )
60- if ( api . opened && tof == 'string' && msg [ 0 ] === '[' ) {
60+ if ( api . opened ) {
6161 try {
62- msg = parse ( msg , parseReplacer )
62+ msg = decode ( deserialize ( msg ) )
6363 } catch ( e ) {
64- // Invalid array to parse
64+ // Invalid array to deserialize or decode
6565 return false
6666 }
6767
@@ -78,10 +78,10 @@ export default function createNodeFactory(DJSON) {
7878 req . then ( value => {
7979 response . push ( 0 ) // no errors
8080 if ( value !== undefined ) response . push ( value )
81- api . send ( stringify ( response , stringifyReplacer ) )
81+ api . send ( serialize ( encode ( response ) ) )
8282 } ) . catch ( error => {
8383 response . push ( error ) // error
84- api . send ( stringify ( response , stringifyReplacer ) )
84+ api . send ( serialize ( encode ( response ) ) )
8585 } )
8686 args = isArray ( args ) ? args : [ ]
8787 args . push ( req )
@@ -109,23 +109,33 @@ export default function createNodeFactory(DJSON) {
109109 api . opened = false
110110 }
111111
112- function stringifyReplacer ( key , f ) {
113- if ( Func . isValidToStringify ( f ) && f . name !== NAME_REMOTE_FUNCTION ) {
114- const function_id = local_functions_map . has ( f )
115- ? local_functions_map . get ( f )
116- : registerLocalFunction ( f )
117- return Func . stringifyReplacer ( function_id )
112+ function encode ( object ) {
113+ const encodeFunction = ( { value } ) => {
114+ if ( isFunction ( value ) ) {
115+ if ( value . name === NAME_REMOTE_FUNCTION ) return null
116+ const function_id = local_functions_map . has ( value )
117+ ? local_functions_map . get ( value )
118+ : registerLocalFunction ( value )
119+ return { [ '$function' ] : function_id }
120+ }
121+ return value
118122 }
119- return f
123+ return converter ( object , encoders . concat ( encodeFunction ) )
120124 }
121125
122- function parseReplacer ( key , value ) {
123- if ( Func . isValidToParse ( value ) ) {
124- const function_id = value [ Func . key ]
125- const f = remote_functions_id [ function_id ]
126- return isFunction ( f ) ? f : createRemoteFunction ( function_id )
126+ function decode ( object ) {
127+ const decodeFunction = ( { value } ) => {
128+ if (
129+ getUniqueKey ( value ) === '$function' &&
130+ isInteger ( value [ '$function' ] )
131+ ) {
132+ const function_id = value [ '$function' ]
133+ const f = remote_functions_id [ function_id ]
134+ return isFunction ( f ) ? f : createRemoteFunction ( function_id )
135+ }
136+ return value
127137 }
128- return value
138+ return converter ( object , decoders . concat ( decodeFunction ) )
129139 }
130140
131141 const api = {
0 commit comments