Comparing version 3.0.2 to 3.1.0
71
index.js
const debug = require('debug')('dat') | ||
const url = require('url') | ||
const https = require('https') | ||
const Emitter = require('events') | ||
const {stringify} = require('querystring') | ||
@@ -34,2 +35,4 @@ const memoryCache = require('./cache') | ||
var datDns = new Emitter() | ||
function resolveName (name, opts, cb) { | ||
@@ -80,3 +83,8 @@ if (typeof opts === 'function') { | ||
// parse the record | ||
res = parseDnsOverHttpsRecord(name, res.body) | ||
res = parseDnsOverHttpsRecord(datDns, name, res.body) | ||
datDns.emit('resolved', { | ||
method: 'dns-over-https', | ||
name, | ||
key: res.key | ||
}) | ||
debug('dns-over-http resolved', name, 'to', res.key) | ||
@@ -94,2 +102,7 @@ } catch (e) { | ||
debug('.well-known/dat lookup failed for name:', name, res.statusCode, res.err) | ||
datDns.emit('failed', { | ||
method: 'well-known', | ||
name, | ||
err: 'HTTP code ' + res.statusCode + ' ' + res.err | ||
}) | ||
mCache.set(name, false, 60) // cache the miss for a minute | ||
@@ -99,2 +112,7 @@ throw new Error('DNS record not found') | ||
debug('.well-known/dat lookup failed for name:', name, res.statusCode) | ||
datDns.emit('failed', { | ||
method: 'well-known', | ||
name, | ||
err: 'HTTP code ' + res.statusCode | ||
}) | ||
throw new Error('DNS record not found') | ||
@@ -104,3 +122,8 @@ } | ||
// parse the record | ||
res = parseWellknownDatRecord(name, res.body) | ||
res = parseWellknownDatRecord(datDns, name, res.body) | ||
datDns.emit('resolved', { | ||
method: 'well-known', | ||
name, | ||
key: res.key | ||
}) | ||
debug('.well-known/dat resolved', name, 'to', res.key) | ||
@@ -129,10 +152,10 @@ } | ||
function flushCache () { | ||
datDns.emit('cache-flushed') | ||
mCache.flush() | ||
} | ||
return { | ||
resolveName: resolveName, | ||
listCache: listCache, | ||
flushCache: flushCache | ||
} | ||
datDns.resolveName = resolveName | ||
datDns.listCache = listCache | ||
datDns.flushCache = flushCache | ||
return datDns | ||
} | ||
@@ -160,3 +183,3 @@ | ||
function parseDnsOverHttpsRecord (name, body) { | ||
function parseDnsOverHttpsRecord (datDns, name, body) { | ||
// decode to obj | ||
@@ -168,2 +191,7 @@ var record | ||
debug('dns-over-https failed', name, 'did not give a valid JSON response') | ||
datDns.emit('failed', { | ||
method: 'dns-over-https', | ||
name, | ||
err: 'Failed to parse JSON response' | ||
}) | ||
throw new Error('Invalid dns-over-https record, must provide json') | ||
@@ -176,2 +204,7 @@ } | ||
debug('dns-over-https failed', name, 'did not give any TXT answers') | ||
datDns.emit('failed', { | ||
method: 'dns-over-https', | ||
name, | ||
err: 'Did not give any TXT answers' | ||
}) | ||
throw new Error('Invalid dns-over-https record, no TXT answers given') | ||
@@ -195,2 +228,7 @@ } | ||
debug('dns-over-https failed', name, 'did not give any TXT datkey answers') | ||
datDns.emit('failed', { | ||
method: 'dns-over-https', | ||
name, | ||
err: 'Did not give any TXT datkey answers' | ||
}) | ||
throw new Error('Invalid dns-over-https record, no TXT datkey answer given') | ||
@@ -226,4 +264,9 @@ } | ||
function parseWellknownDatRecord (name, body) { | ||
function parseWellknownDatRecord (datDns, name, body) { | ||
if (!body || typeof body != 'string') { | ||
datDns.emit('failed', { | ||
method: 'well-known', | ||
name, | ||
err: 'Empty response' | ||
}) | ||
throw new Error('DNS record not found') | ||
@@ -240,2 +283,7 @@ } | ||
debug('.well-known/dat failed', name, 'Must be a dat://{key} url') | ||
datDns.emit('failed', { | ||
method: 'well-known', | ||
name, | ||
err: 'Record did not provide a valid dat://{key} url' | ||
}) | ||
throw new Error('Invalid .well-known/dat record, must provide a dat://{key} url') | ||
@@ -250,2 +298,7 @@ } | ||
} catch (e) { | ||
datDns.emit('failed', { | ||
method: 'well-known', | ||
name, | ||
err: 'Failed to parse TTL line, error: ' + e.toString() | ||
}) | ||
debug('.well-known/dat failed to parse TTL for %s, line: %s, error:', name, lines[1], e) | ||
@@ -252,0 +305,0 @@ } |
{ | ||
"name": "dat-dns", | ||
"version": "3.0.2", | ||
"version": "3.1.0", | ||
"description": "Issue DNS lookups for Dat archives using HTTPS requests to the target host.", | ||
@@ -26,3 +26,3 @@ "main": "index.js", | ||
"concat-stream": "^1.6.0", | ||
"debug": "^2.6.1" | ||
"debug": "^4.1.1" | ||
}, | ||
@@ -29,0 +29,0 @@ "devDependencies": { |
@@ -52,2 +52,7 @@ # dat-dns | ||
}) | ||
// emits some events, mainly useful for logging/debugging | ||
datDns.on('resolved', ({method, name, key}) => {...}) | ||
datDns.on('failed', ({method, name, err}) => {...}) | ||
datDns.on('cache-flushed', () => {...}) | ||
``` | ||
@@ -62,3 +67,3 @@ | ||
``` | ||
DATKEY={key} | ||
datkey={key} | ||
``` | ||
@@ -65,0 +70,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20286
550
77
+ Addeddebug@4.3.7(transitive)
+ Addedms@2.1.3(transitive)
- Removeddebug@2.6.9(transitive)
- Removedms@2.0.0(transitive)
Updateddebug@^4.1.1