@leofcoin/faucet 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +4 -3
  2. package/src/server.js +12 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/faucet",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Operator-funded faucet for the Leofcoin Peach testnet",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -30,8 +30,9 @@
30
30
  "test": "node --test test/*.test.js"
31
31
  },
32
32
  "dependencies": {
33
- "@leofcoin/launch-chain": "^0.4.23",
34
- "@leofcoin/lib": "^1.3.12",
33
+ "@leofcoin/binary": "^1.1.0",
34
+ "@leofcoin/launch-chain": "^0.5.0",
35
+ "@leofcoin/lib": "^1.4.0",
35
36
  "@leofcoin/multi-wallet": "^3.1.9",
36
37
  "@leofcoin/utils": "^1.1.45",
37
38
  "@vandeurenglenn/wrtc": "^0.7.2"
package/src/server.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { encodeWire, decodeWire, BINARY_CONTENT_TYPE } from '@leofcoin/binary'
1
2
  import { createServer } from 'node:http'
2
3
  import { isIP } from 'node:net'
3
4
  import { FaucetError } from './service.js'
@@ -7,8 +8,8 @@ export function faucetServer(faucet, { origins = [], trustProxy = false } = {})
7
8
  const clients = new Map()
8
9
  return createServer(async (request, response) => {
9
10
  const send = (status, value) => {
10
- response.writeHead(status, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' })
11
- response.end(JSON.stringify(value))
11
+ response.writeHead(status, { 'Content-Type': BINARY_CONTENT_TYPE, 'Cache-Control': 'no-store' })
12
+ response.end(encodeWire('rpc', value))
12
13
  }
13
14
  try {
14
15
  const origin = request.headers.origin
@@ -46,18 +47,20 @@ export function faucetServer(faucet, { origins = [], trustProxy = false } = {})
46
47
  return
47
48
  }
48
49
  if (request.method !== 'POST' || path !== '/claims') throw new FaucetError(404, 'Not found')
49
- if (!request.headers['content-type']?.startsWith('application/json'))
50
- throw new FaucetError(415, 'Use application/json')
51
- let body = ''
50
+ if (request.headers['content-type']?.split(';')[0] !== BINARY_CONTENT_TYPE)
51
+ throw new FaucetError(415, 'Use application/octet-stream')
52
+ const chunks = []
53
+ let size = 0
52
54
  for await (const chunk of request) {
53
- body += chunk
54
- if (Buffer.byteLength(body) > 2048) throw new FaucetError(413, 'Request too large')
55
+ size += chunk.length
56
+ if (size > 2048) throw new FaucetError(413, 'Request too large')
57
+ chunks.push(chunk)
55
58
  }
56
59
  let value
57
60
  try {
58
- value = JSON.parse(body)
61
+ value = decodeWire('rpc', Buffer.concat(chunks), { maxBytes: 2048 })
59
62
  } catch {
60
- throw new FaucetError(400, 'Invalid JSON')
63
+ throw new FaucetError(400, 'Invalid binary request')
61
64
  }
62
65
  if (!value || typeof value.address !== 'string' || value.address.length > 100)
63
66
  throw new FaucetError(400, 'Recipient address required')