arbase
Advanced tools
Comparing version 0.1.3 to 0.1.4
{ | ||
"name": "arbase", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Arbase is a tool to create object-based APIs on top of arweave in mere minutes", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -7,3 +7,3 @@ 'use strict' | ||
const { decodeAndValidate, decodeTxData } = require('./process') | ||
const { decodeAndValidate, decodeAndValidateList, ListEventType, decodeTxData } = require('./process') | ||
@@ -51,8 +51,8 @@ const queue = require('../queue')() | ||
switch (data.op) { | ||
case 'append': { | ||
switch (data.type) { | ||
case ListEventType.APPEND: { | ||
idMap[data.target] = data.push(data.target) | ||
break | ||
} | ||
case 'delete': { | ||
case ListEventType.DELETE: { | ||
delete data[idMap[data.target]] | ||
@@ -82,6 +82,4 @@ break | ||
const txLog = txs.reverse().map(() => queue(id, async () => { | ||
const fetched = await fetchTransaction(id) | ||
return validateListEntry(entry, listEntry, fetched) | ||
})) | ||
const txLog = txs.reverse().map(() => queue(id, async () => | ||
decodeAndValidateList(await fetchTransaction(id)))) | ||
@@ -88,0 +86,0 @@ for (let i = txLog.length; i > -1; i--) { |
@@ -7,2 +7,20 @@ 'use strict' | ||
const x = require('base-x') | ||
const b = x('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-') | ||
const protons = require('protons') | ||
const { ListEvent, ListEventType } = protons(` | ||
enum ListEventType { | ||
APPEND = 1; | ||
DELETE = 2; | ||
} | ||
message ListEvent { | ||
ListEventType type = 1; | ||
bytes blockId = 2; | ||
} | ||
`) | ||
async function decodeAndValidate (entry, data, half) { | ||
@@ -58,2 +76,33 @@ const decoded = entry.message.decode(data) | ||
const listValidator = Joi.object({ | ||
type: Joi.number().integer().required(), | ||
blockId: Joi.string().regex(/[a-zA-Z0-9_-]{43}/).required() | ||
}) | ||
async function decodeAndValidateList (data) { | ||
const decoded = ListEvent.decode(data) | ||
decoded.blockId = b.encode(decoded.blockId) | ||
const {error, value} = listValidator.validate(data) | ||
if (error) { | ||
throw error | ||
} | ||
return value | ||
} | ||
async function validateAndEncodeList (data) { | ||
const {error, value} = listValidator.validate(data) | ||
value.blockId = b.decode(value.blockId) | ||
if (error) { | ||
throw error | ||
} | ||
return ListEvent.encode(value) | ||
} | ||
function decodeTxData (tx) { | ||
@@ -70,4 +119,11 @@ return Buffer.from(tx.get('data', {decode: true})) | ||
validateAndEncode, | ||
decodeAndValidateList, | ||
validateAndEncodeList, | ||
ListEventType, | ||
decodeTxData, | ||
encodeTxData | ||
encodeTxData, | ||
b | ||
} |
@@ -36,6 +36,6 @@ 'use strict' | ||
// TODO: rewrite below | ||
async function listAppend (arweave, entry, id, targetList, targetId) { | ||
async function listAppend (arweave, entry, listEntry, id, targetId) { | ||
const tx = await createTx({ op: 'append', target: targetId }, arweave) | ||
tx.addTag('block', id) | ||
tx.addTag('child', targetList) | ||
tx.addTag('child', String(listEntry.id)) | ||
@@ -45,6 +45,6 @@ return tx | ||
async function listRemove (arweave, entry, id, targetList, targetId) { | ||
async function listRemove (arweave, entry, listEntry, id, targetId) { | ||
const tx = await createTx({ op: 'delete', target: targetId }, arweave) | ||
tx.addTag('block', id) | ||
tx.addTag('child', targetList) | ||
tx.addTag('child', String(listEntry.id)) | ||
@@ -51,0 +51,0 @@ return tx |
@@ -12,16 +12,4 @@ 'use strict' | ||
let baseMessage = ` | ||
let baseMessage = '' | ||
enum ListEventType { | ||
append = 1; | ||
delete = 2; | ||
} | ||
message ListEvent { | ||
ListEventType type = 1; | ||
bytes blockId = 2; | ||
} | ||
` | ||
// TODO: use pre-compiled protobufjs | ||
@@ -28,0 +16,0 @@ |
38313
996