代码之家  ›  专栏  ›  技术社区  ›  Michael Paccione

缓冲区到字符串格式错误的编码

  •  0
  • Michael Paccione  · 技术社区  · 4 年前

    您好,我将JSON通过zlib进行压缩,将其作为缓冲区存储在平面文件数据库中,然后读取并发送出去。

    除了我的问题是数据是各种疯狂的字符。我试过了。toString()我试过Node的官方StringDecoder。我尝试了很多方法,但除了.toJSON作为JSON缓冲区可读之外,我似乎无法以任何格式获得它,而实际上我需要输出的最终JSON。

    思想?

    export const writeCollection = (index, timespan, data) => {
      zlib.gzip(JSON.stringify(data), (err, result) => {
        if (err) {
          console.log({ err });
        } else {
          const keyName = dbCollections.gzip[index].add({
            result
          });
          collectionKeys.gzip[index] = keyName;
          writeLogging(timespan, keyName, index, "gzip");
        }
      });
    
      zlib.brotliCompress(JSON.stringify(data), (err, result) => {
        if (err) {
          console.log({ err });
        } else {
          const keyName = dbCollections.brotli[index].add({
            result
          });
          collectionKeys.brotli[index] = keyName;
          writeLogging(timespan, keyName, index, "brotli");
        }
      });
    };
    

    从平面文件数据库读取

    export const readCollection = (index, encoding) => {
      const encodedRead = encoding.includes("br")
        ? dbCollections.brotli[index].all()
        : dbCollections.gzip[index].all();
      return encodedRead[0].result;
    };
    

    正在尝试转换为JSON

    export const testGetQuakeData = (req, res) => {
      const encoding = req.headers["accept-encoding"];
    
      try {
        const data = readCollection(0, encoding);
        console.log(data)
        const json = decoder.write(Buffer.from(data));
        console.log(json)
        // res.set({
        //   'Content-Type': 'application/json',
        //   'Content-Encoding': encoding.includes('br') ? 'br' : "gzip",
        // })
        res.send(json)
      } catch (err) {
        console.log({ err });
        res.status(500).send(err);
      }
    };
    
    0 回复  |  直到 4 年前
        1
  •  0
  •   Michael Paccione    4 年前

    忘了用ZLIB解开拉链!

     export const testGetQuakeData = (req, res) => {
      const encoding = req.headers["accept-encoding"];
    
      try {
        const data = readCollection(0, encoding);
        encoding.includes("br")
          ? zlib.brotliDecompress(data, (err, result) => {
              err ? res.status(500).send(err) : res.send(decoder.write(result));
            })
          : zlib.unzip(data, (err, result) => {
              err ? res.status(500).send(err) : res.send(decoder.write(result));
            });
      } catch (err) {
        console.log({ err });
        res.status(500).send(err);
      }
    };
    

    这是未来的产品功能!

    export const testGetCompressedQuakeData = (req, res) => {
      const encoding = req.headers["accept-encoding"];
      try {
        const data = readCollection(0, encoding);
        encoding.includes("br")
          ? res.writeHead(200, {
              "Content-Type": "application/json",
              "Content-Encoding": "br",
              "Content-Length": data.length,
            })
          : res.writeHead(200, {
              "Content-Type": "application/json",
              "Content-Encoding": "gzip",
              "Content-Length": data.length,
            })
        res.end(data)
      } catch (err) {
        console.log({ err });
        res.status(500).send(err);
      }
    };
    

    Brotli压缩获胜!

    推荐文章