arbase
Advanced tools
Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "arbase", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Arbase is a tool to create object-based APIs on top of arweave in mere minutes", | ||
@@ -30,3 +30,3 @@ "main": "src/index.js", | ||
"@hapi/boom": "^8.0.1", | ||
"@hapi/joi": "^16.1.4", | ||
"@hapi/joi": "^16.1.7", | ||
"arlang": "^0.1.3", | ||
@@ -33,0 +33,0 @@ "arweave": "^1.4.1", |
@@ -7,3 +7,3 @@ 'use strict' | ||
const {decodeAndValidate} = require('./process') | ||
const { decodeAndValidate, decodeTxData } = require('./process') | ||
@@ -21,6 +21,2 @@ const queue = require('../queue')() | ||
function fetchTransaction (arweave, id) { | ||
return arweave.transactions.get(id) | ||
} | ||
function validateEntry (entry, {data, tags}, isInitial) { | ||
@@ -72,2 +68,6 @@ // TODO: return false if invalid | ||
module.exports = (arweave) => { | ||
async function fetchTransaction (id) { | ||
return decodeTxData(await arweave.transactions.get(id)) | ||
} | ||
return { | ||
@@ -97,7 +97,7 @@ list: async (entry, listEntry, id, list) => { | ||
}, | ||
entry: async function fetchEntry (arweave, entry, id) { | ||
entry: async function fetchEntry (entry, id) { | ||
let obj | ||
try { | ||
obj = decodeAndValidate(entry, (await fetchTransaction(arweave, id)).data) | ||
obj = decodeAndValidate(entry, await fetchTransaction(id)) | ||
} catch (err) { | ||
@@ -112,2 +112,6 @@ if (err.type === 'TX_NOT_FOUND') { | ||
if (err.type === 'TX_PENDING') { | ||
throw Boom.notFound('Transaction is still pending (TODO fetch from arswarm)') | ||
} | ||
throw err | ||
@@ -128,3 +132,3 @@ } | ||
if (tx) { | ||
const {data} = await fetchTransaction(tx) | ||
const data = await fetchTransaction(tx) | ||
obj = joinOplog(obj, decodeAndValidate(entry, data, true)) | ||
@@ -131,0 +135,0 @@ } |
@@ -57,5 +57,15 @@ 'use strict' | ||
function decodeTxData (tx) { | ||
return Buffer.from(tx.get('data', {decode: true})) | ||
} | ||
function encodeTxData (data) { | ||
return new Uint8Array(data) | ||
} | ||
module.exports = { | ||
decodeAndValidate, | ||
validateAndEncode | ||
validateAndEncode, | ||
decodeTxData, | ||
encodeTxData | ||
} |
'use strict' | ||
const {validateAndEncode} = require('./process') | ||
const { validateAndEncode, encodeTxData } = require('./process') | ||
async function createTx (data, arweave) { | ||
return arweave.createTransaction({ | ||
data | ||
data: encodeTxData(data) | ||
}, arweave.jwk) | ||
@@ -14,3 +14,3 @@ } | ||
async function entryCreate (arweave, entry, val) { | ||
const tx = await createTx(validateAndEncode(entry, val), arweave) | ||
const tx = await createTx(await validateAndEncode(entry, val), arweave) | ||
@@ -21,3 +21,3 @@ return tx | ||
async function entryModify (arweave, entry, id, diff) { | ||
const tx = await createTx(validateAndEncode(entry, diff, true), arweave) | ||
const tx = await createTx(await validateAndEncode(entry, diff, true), arweave) | ||
tx.addTag('block', id) | ||
@@ -54,8 +54,17 @@ tx.addTag('child', '#') | ||
module.exports = { | ||
entryCreate, | ||
entryModify, | ||
entryDelete, | ||
listAppend, | ||
listRemove | ||
module.exports = (arweave) => { | ||
const out = { | ||
entryCreate, | ||
entryModify, | ||
entryDelete, | ||
listAppend, | ||
listRemove | ||
} | ||
for (const fnc in out) { // eslint-disable-line guard-for-in | ||
const o = out[fnc] | ||
out[fnc] = (...a) => o(arweave, ...a) | ||
} | ||
return out | ||
} |
@@ -30,3 +30,3 @@ 'use strict' | ||
if (attr.maxSize) { | ||
out[attribute] += `.length(${attr.maxSize})` | ||
out[attribute] += `.max(${attr.maxSize})` | ||
} | ||
@@ -55,3 +55,4 @@ | ||
if (attr.maxSize) { | ||
out += `.length(${attr.maxSize})` | ||
// TODO: rewrite props to be min, max instead | ||
out += `.max(${attr.maxSize})` | ||
} | ||
@@ -58,0 +59,0 @@ |
@@ -8,8 +8,8 @@ 'use strict' | ||
module.exports = async (src) => { | ||
module.exports = async (src, {signoff} = {}) => { | ||
const contents = await loader(src) | ||
validator(contents) | ||
const compiled = compiler({}, contents) | ||
const rendered = render(compiled) | ||
const rendered = render(compiled, {signoff}) | ||
return rendered | ||
} |
@@ -16,2 +16,8 @@ 'use strict' | ||
const signoffTable = { | ||
return: 'return data', | ||
eval: 'data', | ||
cjs: 'module.exports = data' | ||
} | ||
function str (data, table) { | ||
@@ -40,5 +46,5 @@ let keys = [] | ||
module.exports = (data) => { | ||
module.exports = (data, {signoff}) => { | ||
const out = str(data, mainTable) | ||
return template.replace("'$DATA'", out) | ||
return template.replace("'$DATA'", out).replace('signoff', signoffTable[signoff || 'cjs']) | ||
} |
@@ -38,2 +38,2 @@ 'use strict' | ||
module.exports = data | ||
signoff |
37419
969
Updated@hapi/joi@^16.1.7