toDataInfo()

in remote/data-info-manager.js [156:203]


    toDataInfo(target, recursive) {
        /** @type {Donuts.Remote.IDataInfo} */
        let dataInfo;

        if (target) {
            dataInfo = this.dataInfoMap.get(target);
        }

        dataInfo = dataInfo || { type: dataTypeOf(target) };
        recursive = !(recursive === false);

        if (dataInfo.id) {
            // Existing dataInfo.

        } else if (dataInfo.type === "node.buffer") {
            dataInfo.value = (target).toString("base64");

        } else if (utils.object.isSerializable(target)) {
            dataInfo.value = JSON.stringify(target,
                (key, value) => {
                    if (key === "") {
                        return value;
                    }

                    if (typeof value === "string") {
                        return `${"string"}:${value}`;
                    }

                    if (value && value.type === "Buffer" && Array.isArray(value.data)) {
                        return `${"node.buffer"}:${Buffer.from(value.data).toString("base64")}`;
                    }

                    return value;
                });

        } else if (recursive && dataInfo.type === "object") {
            dataInfo = this.toObjectDataInfo(target);

        } else if (dataInfo.type === "function") {
            dataInfo = this.toFunctionDataInfo(target);

        } else {
            throw new Error(`Unsupported DataType of the given target: ${dataInfo.type}`);
        }

        this.localRefs[dataInfo.id] = target;
        return dataInfo;
    }