From 80e512eadf369e4ff504fa701a17e37c3ead7557 Mon Sep 17 00:00:00 2001 From: Jack Frain Date: Wed, 6 Nov 2024 14:48:07 -0500 Subject: [PATCH] fix(weavedrive): store headers as utf8 encoded --- extensions/weavedrive/src/index.js | 6 +++--- extensions/weavedrive/test/index.test.js | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/extensions/weavedrive/src/index.js b/extensions/weavedrive/src/index.js index 4f17b0d5..f6b73198 100644 --- a/extensions/weavedrive/src/index.js +++ b/extensions/weavedrive/src/index.js @@ -105,7 +105,7 @@ module.exports = function weaveDrive(mod, FS) { var bytesLength = result.length; - var node = FS.createDataFile('/', 'block/' + id, result, true, false); + var node = FS.createDataFile('/', 'block/' + id, Buffer.from(result, 'utf-8'), true, false); var stream = FS.open('/block/' + id, 'r'); return stream; @@ -135,7 +135,7 @@ module.exports = function weaveDrive(mod, FS) { //.then(x => (console.error(x), x)) .then(x => JSON.stringify(x)); - var node = FS.createDataFile('/', 'tx/' + id, result, true, false); + var node = FS.createDataFile('/', 'tx/' + id, Buffer.from(result, 'utf-8'), true, false); var stream = FS.open('/tx/' + id, 'r'); return stream; }, @@ -220,7 +220,7 @@ module.exports = function weaveDrive(mod, FS) { if (result === 'No results') { return result } - FS.createDataFile('/', 'tx2/' + id, result, true, false); + FS.createDataFile('/', 'tx2/' + id, Buffer.from(result, 'utf-8'), true, false); var stream = FS.open('/tx2/' + id, 'r'); return stream; diff --git a/extensions/weavedrive/test/index.test.js b/extensions/weavedrive/test/index.test.js index d4a731b5..e48aa76b 100644 --- a/extensions/weavedrive/test/index.test.js +++ b/extensions/weavedrive/test/index.test.js @@ -356,6 +356,24 @@ return result assert.equal(data.block.height, 1290333) }) +test('data item with chinese characters', async () => { + const chineseCharacterTx = 'Xg82mcAfQNgJetk_kSKqMkwYDfoMJLGw8fdheU3evWk' + const handle = await AoLoader(wasm, options) + const result = await handle(memory, { + ...Msg, + Data: ` + local data = require('WeaveDrive').getDataItem('${chineseCharacterTx}') + local json = require('json') + local decoded = json.decode(data) + return data + ` + }, { Process, Module }) + memory = result.Memory + const data = JSON.parse(result.Output.data) + const description = data.tags.find(tag => tag.name === 'Description')?.value + assert.equal(description, "Smartweave是不是要转到AO了?\n本文档介绍原子资产SmartWeave合约的标准接口。原子资产是SmartWeave合约及其在发布到Arweave的同一事务中初始化的数据。SmartWeave合约和数据共享一个交易ID。") +}) + test('read data item tx, no result', async () => { const handle = await AoLoader(wasm, options) const result = await handle(memory, {