Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: metadata padding #37

Merged
merged 9 commits into from
Sep 22, 2022
20 changes: 18 additions & 2 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {

const PATH_SEPARATOR = '/'
const PATH_SEPARATOR_BYTE = 47
const PADDING_BYTE = 0x0a

type ForkMapping = { [key: number]: MantarayFork }
type RecursiveSaveReturnType = { reference: Reference; changed: boolean }
Expand Down Expand Up @@ -99,10 +100,25 @@ export class MantarayFork {
const metadataBytes = new TextEncoder().encode(jsonString)

// pad JSON bytes if necessary -> the encryptDecrypt handles if the data has no key length
const metadataSizeWithSize = metadataBytes.length + nodeForkSizes.metadata
agazso marked this conversation as resolved.
Show resolved Hide resolved
let padding = new Uint8Array(0)
if (metadataSizeWithSize < nodeHeaderSizes.obfuscationKey) {
agazso marked this conversation as resolved.
Show resolved Hide resolved
const paddingLength = nodeHeaderSizes.obfuscationKey - metadataSizeWithSize
padding = new Uint8Array(paddingLength)
for (let i = 0; i < padding.length; i++) {
padding[i] = PADDING_BYTE
}
} else if (metadataSizeWithSize > nodeHeaderSizes.obfuscationKey) {
const paddingLength = nodeHeaderSizes.obfuscationKey - metadataSizeWithSize%nodeHeaderSizes.obfuscationKey
padding = new Uint8Array(paddingLength)
for (let i = 0; i < padding.length; i++) {
padding[i] = PADDING_BYTE
}
}

const metadataBytesSize = toBigEndianFromUint16(metadataBytes.length)
const metadataBytesSize = toBigEndianFromUint16(metadataBytes.length + padding.length)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any sense to add padding length to the metadataByteSize.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the goal was to replicate the implementation in Bee and this is the exact same thing.


return new Uint8Array([...data, ...metadataBytesSize, ...metadataBytes])
return new Uint8Array([...data, ...metadataBytesSize, ...metadataBytes, ...padding])
}

return data
Expand Down
40 changes: 39 additions & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,44 @@ const beeTestPageManifestData = async (): Promise<Uint8Array> => {
return bee.downloadData(contentHash) //only download its manifest
}

it('should generate the same content hash as Bee', async () => {
const contentHash = await bee.uploadFilesFromDirectory(process.env.BEE_POSTAGE, join(__dirname, 'testpage'), {
pin: true,
indexDocument: 'index.html',
})
const testPage = join(__dirname, 'testpage')
const indexHtmlBytes = FS.readFileSync(join(testPage, 'index.html'))
const imageBytes = FS.readFileSync(join(testPage, 'img', 'icon.png'))
const textBytes = FS.readFileSync(join(testPage, 'img', 'icon.png.txt'))
const [indexReference, imageReference, textReference] = await Promise.all([
bee.uploadData(process.env.BEE_POSTAGE, indexHtmlBytes),
bee.uploadData(process.env.BEE_POSTAGE, imageBytes),
bee.uploadData(process.env.BEE_POSTAGE, textBytes),
])
const utf8ToBytes = (value: string): Uint8Array => {
return new TextEncoder().encode(value)
}
const iNode = new MantarayNode()
iNode.addFork(utf8ToBytes('index.html'), hexToBytes(indexReference), {
'Content-Type': 'text/html; charset=utf-8',
Filename: 'index.html',
})
iNode.addFork(utf8ToBytes('img/icon.png.txt'), hexToBytes(textReference), {
'Content-Type': 'text/plain; charset=utf-8',
Filename: 'icon.png.txt',
})
iNode.addFork(utf8ToBytes('img/icon.png'), hexToBytes(imageReference), {
'Content-Type': 'image/png',
Filename: 'icon.png',
})
iNode.addFork(utf8ToBytes('/'), new Uint8Array(32) as Reference, {
'website-index-document': 'index.html',
})
const iNodeRef = await iNode.save(saveFunction)

expect(iNodeRef).toEqual(hexToBytes(contentHash))
})

it('should serialize/deserialize the same as Bee', async () => {
const data = await beeTestPageManifestData()
const node = new MantarayNode()
Expand Down Expand Up @@ -70,7 +108,7 @@ it('should construct manifests of testpage folder', async () => {
Filename: 'index.html',
})
iNode.addFork(utf8ToBytes('img/icon.png.txt'), hexToBytes(textReference), {
'Content-Type': '', // FIXME: The bee node assigns empty string to Content Type in this case
'Content-Type': 'text/plain; charset=utf-8',
Filename: 'icon.png.txt',
})
iNode.addFork(utf8ToBytes('img/icon.png'), hexToBytes(imageReference), {
Expand Down