|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import fetch from 'node-fetch' |
| 3 | + |
| 4 | +const STAMPS_ENDPOINT = 'stamps' |
| 5 | + |
| 6 | +async function http(url, config) { |
| 7 | + const response = await fetch(url, config) |
| 8 | + |
| 9 | + return await response.json() |
| 10 | +} |
| 11 | + |
| 12 | +async function getPostageBatch(url, postageBatchId) { |
| 13 | + return await http(`${url}/${STAMPS_ENDPOINT}/${postageBatchId}`, { |
| 14 | + method: 'get', |
| 15 | + }) |
| 16 | +} |
| 17 | + |
| 18 | +async function createPostageBatch(url, amount, depth) { |
| 19 | + const response = await http(`${url}/${STAMPS_ENDPOINT}/${amount}/${depth}`, { |
| 20 | + method: 'post', |
| 21 | + }) |
| 22 | + |
| 23 | + return response.batchID |
| 24 | +} |
| 25 | + |
| 26 | +export default async function postageSetup() { |
| 27 | + const beeDebugUrl = process.env.BEE_DEBUG_API_URL || 'http://127.0.0.1:1635' |
| 28 | + const beeDebugPeerUrl = process.env.BEE_PEER_DEBUG_API_URL || 'http://127.0.0.1:11635' |
| 29 | + |
| 30 | + try { |
| 31 | + if (process.env.BEE_POSTAGE) { |
| 32 | + try { |
| 33 | + if (!(await getPostageBatch(beeDebugUrl, process.env.BEE_POSTAGE)).usable) { |
| 34 | + delete process.env.BEE_POSTAGE |
| 35 | + console.log('BEE_POSTAGE stamp was found but is not usable') |
| 36 | + } else { |
| 37 | + console.log('Using configured BEE_POSTAGE stamp.') |
| 38 | + } |
| 39 | + } catch (e) { |
| 40 | + delete process.env.BEE_POSTAGE |
| 41 | + console.log('BEE_POSTAGE stamp was not found') |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if (process.env.BEE_PEER_POSTAGE) { |
| 46 | + try { |
| 47 | + if (!(await getPostageBatch(beeDebugPeerUrl, process.env.BEE_PEER_POSTAGE)).usable) { |
| 48 | + delete process.env.BEE_PEER_POSTAGE |
| 49 | + console.log('BEE_PEER_POSTAGE stamp was found but is not usable') |
| 50 | + } else { |
| 51 | + console.log('Using configured BEE_PEER_POSTAGE stamp.') |
| 52 | + } |
| 53 | + } catch (e) { |
| 54 | + delete process.env.BEE_PEER_POSTAGE |
| 55 | + console.log('BEE_PEER_POSTAGE stamp was not found') |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if (!process.env.BEE_POSTAGE || !process.env.BEE_PEER_POSTAGE) { |
| 60 | + console.log('Creating postage stamps...') |
| 61 | + |
| 62 | + const stampsOrder = [] |
| 63 | + |
| 64 | + if (!process.env.BEE_POSTAGE) { |
| 65 | + stampsOrder.push({ url: beeDebugUrl, env: 'BEE_POSTAGE' }) |
| 66 | + } |
| 67 | + |
| 68 | + if (!process.env.BEE_PEER_POSTAGE) { |
| 69 | + stampsOrder.push({ url: beeDebugPeerUrl, env: 'BEE_PEER_POSTAGE' }) |
| 70 | + } |
| 71 | + |
| 72 | + const stamps = await Promise.all(stampsOrder.map(async order => createPostageBatch(order.url, '1', 20))) |
| 73 | + |
| 74 | + for (let i = 0; i < stamps.length; i++) { |
| 75 | + process.env[stampsOrder[i].env] = stamps[i] |
| 76 | + console.log(`${stampsOrder[i].env}: ${stamps[i]}`) |
| 77 | + } |
| 78 | + |
| 79 | + console.log('Waiting for the stamps to be usable') |
| 80 | + let allUsable = true |
| 81 | + do { |
| 82 | + for (let i = 0; i < stamps.length; i++) { |
| 83 | + // eslint-disable-next-line max-depth |
| 84 | + try { |
| 85 | + // eslint-disable-next-line max-depth |
| 86 | + if (!(await getPostageBatch(stampsOrder[i].url, stamps[i])).usable) { |
| 87 | + allUsable = false |
| 88 | + break |
| 89 | + } else { |
| 90 | + allUsable = true |
| 91 | + } |
| 92 | + } catch (e) { |
| 93 | + allUsable = false |
| 94 | + break |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // eslint-disable-next-line no-loop-func |
| 99 | + await new Promise(resolve => setTimeout(() => resolve(), 1_000)) |
| 100 | + } while (!allUsable) |
| 101 | + console.log('Usable, yey!') |
| 102 | + } |
| 103 | + } catch (e) { |
| 104 | + // It is possible that for unit tests the Bee nodes does not run |
| 105 | + // so we are only logging errors and not leaving them to propagate |
| 106 | + console.error(e) |
| 107 | + } |
| 108 | +} |
0 commit comments