mirror of
https://github.com/dawidd6/action-send-mail.git
synced 2026-11-21 21:06:12 +07:00
Co-authored-by: dawidd6 <9713907+dawidd6@users.noreply.github.com>
853 lines
32 KiB
JavaScript
853 lines
32 KiB
JavaScript
"use strict";
|
||
/* eslint no-console: 0 */
|
||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||
if (k2 === undefined) k2 = k;
|
||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||
}
|
||
Object.defineProperty(o, k2, desc);
|
||
}) : (function(o, m, k, k2) {
|
||
if (k2 === undefined) k2 = k;
|
||
o[k2] = m[k];
|
||
}));
|
||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||
}) : function(o, v) {
|
||
o["default"] = v;
|
||
});
|
||
var __importStar = (this && this.__importStar) || (function () {
|
||
var ownKeys = function(o) {
|
||
ownKeys = Object.getOwnPropertyNames || function (o) {
|
||
var ar = [];
|
||
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
||
return ar;
|
||
};
|
||
return ownKeys(o);
|
||
};
|
||
return function (mod) {
|
||
if (mod && mod.__esModule) return mod;
|
||
var result = {};
|
||
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
||
__setModuleDefault(result, mod);
|
||
return result;
|
||
};
|
||
})();
|
||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
};
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
exports.encodeXText = exports.assign = exports.parseDataURI = exports.callbackPromise = exports.getLogger = exports._logFunc = exports.parseConnectionUrl = exports.resolveHostname = exports.dnsCache = exports._readNetworkInterfaces = exports.networkInterfaces = exports._resetCacheCleanup = exports._lastCacheCleanup = exports.copyOwnKeys = exports.isProtoKey = void 0;
|
||
exports.resolveContent = resolveContent;
|
||
exports.recordStreamErrors = recordStreamErrors;
|
||
exports.destroyStream = destroyStream;
|
||
exports.unreadableStreamError = unreadableStreamError;
|
||
exports.resolveStream = resolveStream;
|
||
exports.normalizeDomain = normalizeDomain;
|
||
const urllib = __importStar(require("./url.js"));
|
||
const node_util_1 = __importDefault(require("node:util"));
|
||
const node_fs_1 = __importDefault(require("node:fs"));
|
||
const index_js_1 = __importDefault(require("../fetch/index.js"));
|
||
const errors = __importStar(require("../errors.js"));
|
||
const objects_js_1 = require("./objects.js");
|
||
Object.defineProperty(exports, "isProtoKey", { enumerable: true, get: function () { return objects_js_1.isProtoKey; } });
|
||
Object.defineProperty(exports, "copyOwnKeys", { enumerable: true, get: function () { return objects_js_1.copyOwnKeys; } });
|
||
const node_dns_1 = __importDefault(require("node:dns"));
|
||
const node_net_1 = __importDefault(require("node:net"));
|
||
const node_os_1 = __importDefault(require("node:os"));
|
||
const node_url_1 = __importDefault(require("node:url"));
|
||
const punycode = __importStar(require("../punycode/index.js"));
|
||
const node_stream_1 = require("node:stream");
|
||
const DNS_TTL = 5 * 60 * 1000;
|
||
const CACHE_CLEANUP_INTERVAL = 30 * 1000; // Minimum 30 seconds between cleanups
|
||
const MAX_CACHE_SIZE = 1000; // Maximum number of entries in cache
|
||
let lastCacheCleanup = 0;
|
||
/** @internal */
|
||
const _lastCacheCleanup = () => lastCacheCleanup;
|
||
exports._lastCacheCleanup = _lastCacheCleanup;
|
||
/** @internal */
|
||
const _resetCacheCleanup = () => {
|
||
lastCacheCleanup = 0;
|
||
};
|
||
exports._resetCacheCleanup = _resetCacheCleanup;
|
||
let networkInterfacesRead = false;
|
||
// Read on first use rather than at load, so that importing the module does not ask for
|
||
// the interface table (Deno prompts for --allow-sys on it)
|
||
/** @internal */
|
||
const _readNetworkInterfaces = () => {
|
||
if (!networkInterfacesRead) {
|
||
networkInterfacesRead = true;
|
||
try {
|
||
exports.networkInterfaces = node_os_1.default.networkInterfaces();
|
||
}
|
||
catch (_err) {
|
||
// fails on some systems
|
||
}
|
||
}
|
||
return exports.networkInterfaces;
|
||
};
|
||
exports._readNetworkInterfaces = _readNetworkInterfaces;
|
||
const isFamilySupported = (family, allowInternal) => {
|
||
const addresses = Object.values((0, exports._readNetworkInterfaces)() || {}).flat();
|
||
if (!addresses.length) {
|
||
// hope for the best. Runtimes without an interface table (Cloudflare
|
||
// Workers) report an empty object rather than throwing
|
||
return true;
|
||
}
|
||
return addresses.filter(i => !i.internal || allowInternal).some(i => i.family === 'IPv' + family || i.family === family);
|
||
};
|
||
const resolve = (family, hostname, options, callback) => {
|
||
options = options || {};
|
||
if (!isFamilySupported(family, options.allowInternalNetworkInterfaces)) {
|
||
callback(null, []);
|
||
return null;
|
||
}
|
||
const dnsResolver = node_dns_1.default.Resolver ? new node_dns_1.default.Resolver(options) : node_dns_1.default;
|
||
dnsResolver['resolve' + family](hostname, (err, addresses) => {
|
||
if (err) {
|
||
switch (err.code) {
|
||
case node_dns_1.default.NODATA:
|
||
case node_dns_1.default.NOTFOUND:
|
||
case node_dns_1.default.NOTIMP:
|
||
case node_dns_1.default.SERVFAIL:
|
||
case node_dns_1.default.CONNREFUSED:
|
||
case node_dns_1.default.REFUSED:
|
||
case 'EAI_AGAIN':
|
||
return callback(null, []);
|
||
}
|
||
return callback(err);
|
||
}
|
||
return callback(null, Array.isArray(addresses) ? addresses : [].concat(addresses || []));
|
||
});
|
||
return node_dns_1.default.Resolver ? dnsResolver : null;
|
||
};
|
||
exports.dnsCache = new Map();
|
||
const formatDNSValue = (value, extra) => {
|
||
if (!value) {
|
||
return Object.assign({}, extra || {});
|
||
}
|
||
const addresses = value.addresses || [];
|
||
// Select a random address from available addresses, or null if none
|
||
const host = addresses.length > 0 ? addresses[Math.floor(Math.random() * addresses.length)] : null;
|
||
return Object.assign({
|
||
host,
|
||
// Include all addresses for connection fallback support
|
||
_addresses: addresses
|
||
}, extra || {});
|
||
};
|
||
const resolveHostname = (options, callback) => {
|
||
options = options || {};
|
||
if (!options.host && options.servername) {
|
||
options.host = options.servername;
|
||
}
|
||
if (!options.host || node_net_1.default.isIP(options.host)) {
|
||
// nothing to do here
|
||
const value = {
|
||
addresses: [options.host]
|
||
};
|
||
return callback(null, formatDNSValue(value, {
|
||
servername: options.servername || false,
|
||
cached: false
|
||
}));
|
||
}
|
||
const host = options.host;
|
||
// The TLS server name belongs to the connection asking, not to the host it resolves. The
|
||
// cache is shared by every transport of the process and keyed by host alone, so a server
|
||
// name stored in it would be the one of whichever transport resolved the host first, and a
|
||
// later transport with its own tls.servername would present and verify that name instead
|
||
const servername = options.servername || host;
|
||
let cached;
|
||
if (exports.dnsCache.has(options.host)) {
|
||
cached = exports.dnsCache.get(options.host);
|
||
// Lazy cleanup with time throttling
|
||
const now = Date.now();
|
||
if (now - lastCacheCleanup > CACHE_CLEANUP_INTERVAL) {
|
||
lastCacheCleanup = now;
|
||
// Clean up expired entries
|
||
for (const [host, entry] of exports.dnsCache.entries()) {
|
||
if (entry.expires && entry.expires < now) {
|
||
exports.dnsCache.delete(host);
|
||
}
|
||
}
|
||
// If cache is still too large, remove oldest entries
|
||
if (exports.dnsCache.size > MAX_CACHE_SIZE) {
|
||
const toDelete = Math.floor(MAX_CACHE_SIZE * 0.1); // Remove 10% of entries
|
||
const keys = Array.from(exports.dnsCache.keys()).slice(0, toDelete);
|
||
keys.forEach(key => exports.dnsCache.delete(key));
|
||
}
|
||
}
|
||
if (!cached.expires || cached.expires >= now) {
|
||
return callback(null, formatDNSValue(cached.value, {
|
||
servername,
|
||
cached: true
|
||
}));
|
||
}
|
||
}
|
||
// The timeout limits the lookup as a whole. It is also handed to the resolver, but there
|
||
// it applies to every query attempt on its own, and the resolver retries a few times
|
||
let responded = false;
|
||
let deadline;
|
||
const respond = (err, result) => {
|
||
if (responded) {
|
||
return;
|
||
}
|
||
responded = true;
|
||
clearTimeout(deadline);
|
||
callback(err, result);
|
||
};
|
||
// a stale cached value is still better than no value at all
|
||
const respondCached = (error) => {
|
||
if (!cached) {
|
||
return false;
|
||
}
|
||
exports.dnsCache.set(host, {
|
||
value: cached.value,
|
||
expires: Date.now() + (options.dnsTtl || DNS_TTL)
|
||
});
|
||
respond(null, formatDNSValue(cached.value, {
|
||
servername,
|
||
cached: true,
|
||
error
|
||
}));
|
||
return true;
|
||
};
|
||
const resolvers = [];
|
||
const timeout = Number(options.timeout) || 0;
|
||
if (timeout > 0) {
|
||
deadline = setTimeout(() => {
|
||
for (const resolver of resolvers) {
|
||
if (typeof resolver.cancel === 'function') {
|
||
resolver.cancel();
|
||
}
|
||
}
|
||
const err = new Error('DNS lookup for ' + host + ' timed out');
|
||
err.code = node_dns_1.default.TIMEOUT;
|
||
if (!respondCached(err)) {
|
||
respond(err);
|
||
}
|
||
}, timeout);
|
||
}
|
||
// Resolve both IPv4 and IPv6 addresses for fallback support, at the same time
|
||
let ipv4Addresses = [];
|
||
let ipv6Addresses = [];
|
||
let ipv4Error = null;
|
||
let ipv6Error = null;
|
||
let pending = 2;
|
||
const onResolved = () => {
|
||
if (--pending || responded) {
|
||
return;
|
||
}
|
||
// Combine addresses: IPv4 first, then IPv6
|
||
const allAddresses = ipv4Addresses.concat(ipv6Addresses);
|
||
if (allAddresses.length) {
|
||
const value = {
|
||
addresses: allAddresses
|
||
};
|
||
exports.dnsCache.set(host, {
|
||
value,
|
||
expires: Date.now() + (options.dnsTtl || DNS_TTL)
|
||
});
|
||
return respond(null, formatDNSValue(value, {
|
||
servername,
|
||
cached: false
|
||
}));
|
||
}
|
||
// No addresses from resolve4/resolve6, try dns.lookup as fallback
|
||
if (ipv4Error && ipv6Error && respondCached(ipv4Error)) {
|
||
// Both resolvers had errors
|
||
return;
|
||
}
|
||
try {
|
||
node_dns_1.default.lookup(host, { all: true }, (err, addresses) => {
|
||
if (err) {
|
||
if (respondCached(err)) {
|
||
return;
|
||
}
|
||
return respond(err);
|
||
}
|
||
// Get all supported addresses from dns.lookup
|
||
const supportedAddresses = addresses ? addresses.filter(addr => isFamilySupported(addr.family)).map(addr => addr.address) : [];
|
||
if (addresses && addresses.length && !supportedAddresses.length) {
|
||
// there are addresses but none can be used
|
||
console.warn(`Failed to resolve IPv${addresses[0].family} addresses with current network`);
|
||
}
|
||
if (!supportedAddresses.length && cached) {
|
||
// nothing was found, fallback to cached value
|
||
return respond(null, formatDNSValue(cached.value, {
|
||
servername,
|
||
cached: true
|
||
}));
|
||
}
|
||
const value = {
|
||
addresses: supportedAddresses.length ? supportedAddresses : [host]
|
||
};
|
||
exports.dnsCache.set(host, {
|
||
value,
|
||
expires: Date.now() + (options.dnsTtl || DNS_TTL)
|
||
});
|
||
return respond(null, formatDNSValue(value, {
|
||
servername,
|
||
cached: false
|
||
}));
|
||
});
|
||
}
|
||
catch (lookupErr) {
|
||
if (respondCached(lookupErr)) {
|
||
return;
|
||
}
|
||
return respond(ipv4Error || ipv6Error || lookupErr);
|
||
}
|
||
};
|
||
for (const family of [4, 6]) {
|
||
const resolver = resolve(family, host, options, (err, addresses) => {
|
||
if (family === 4) {
|
||
ipv4Error = err;
|
||
ipv4Addresses = addresses || [];
|
||
}
|
||
else {
|
||
ipv6Error = err;
|
||
ipv6Addresses = addresses || [];
|
||
}
|
||
onResolved();
|
||
});
|
||
if (resolver) {
|
||
resolvers.push(resolver);
|
||
}
|
||
}
|
||
};
|
||
exports.resolveHostname = resolveHostname;
|
||
/**
|
||
* Parses connection url to a structured configuration object
|
||
*
|
||
* @param str Connection url
|
||
* @return Configuration object
|
||
*/
|
||
const parseConnectionUrl = (str) => {
|
||
str = str || '';
|
||
const options = {};
|
||
const url = urllib.parse(str, true);
|
||
switch (url.protocol) {
|
||
case 'smtp:':
|
||
options.secure = false;
|
||
break;
|
||
case 'smtps:':
|
||
options.secure = true;
|
||
break;
|
||
case 'direct:':
|
||
options.direct = true;
|
||
break;
|
||
}
|
||
if (!isNaN(url.port) && Number(url.port)) {
|
||
options.port = Number(url.port);
|
||
}
|
||
if (url.hostname) {
|
||
options.host = url.hostname;
|
||
}
|
||
if (url.username || url.password) {
|
||
options.auth = {
|
||
user: url.username || '',
|
||
pass: url.password || ''
|
||
};
|
||
}
|
||
Object.keys(url.query || {}).forEach(key => {
|
||
let obj = options;
|
||
let lKey = key;
|
||
let value = url.query[key];
|
||
if (!isNaN(value)) {
|
||
value = Number(value);
|
||
}
|
||
switch (value) {
|
||
case 'true':
|
||
value = true;
|
||
break;
|
||
case 'false':
|
||
value = false;
|
||
break;
|
||
}
|
||
// tls is nested object
|
||
if (key.indexOf('tls.') === 0) {
|
||
lKey = key.substr(4);
|
||
if (!options.tls) {
|
||
options.tls = {};
|
||
}
|
||
obj = options.tls;
|
||
}
|
||
else if (key.indexOf('.') >= 0) {
|
||
// ignore nested properties besides tls
|
||
return;
|
||
}
|
||
// `in` already keeps "__proto__" out, but only as a side effect of it being an
|
||
// Object.prototype member. Say it, so the protection survives a change to the check
|
||
if (!(0, objects_js_1.isProtoKey)(lKey) && !(lKey in obj)) {
|
||
obj[lKey] = value;
|
||
}
|
||
});
|
||
return options;
|
||
};
|
||
exports.parseConnectionUrl = parseConnectionUrl;
|
||
/** @internal */
|
||
const _logFunc = (logger, level, defaults, data, message, ...args) => {
|
||
const entry = Object.assign({}, defaults || {}, data || {});
|
||
delete entry.level;
|
||
let logLevel = level;
|
||
if (typeof logger[logLevel] !== 'function') {
|
||
// Provided logger does not implement this level. Fall back to a
|
||
// lower-severity handler instead of throwing.
|
||
logLevel = ['info', 'debug', 'log', 'trace', 'warn', 'error'].find(name => typeof logger[name] === 'function');
|
||
}
|
||
if (logLevel) {
|
||
logger[logLevel](entry, message, ...args);
|
||
}
|
||
};
|
||
exports._logFunc = _logFunc;
|
||
/**
|
||
* Returns a bunyan-compatible logger interface. Uses either provided logger or
|
||
* creates a default console logger
|
||
*
|
||
* @param [options] Options object that might include 'logger' value
|
||
* @param [defaults] Fields merged into every log entry, overridden by the fields of the entry itself
|
||
* @return bunyan compatible logger
|
||
*/
|
||
const getLogger = (options, defaults) => {
|
||
options = options || {};
|
||
const response = {};
|
||
const levels = ['trace', 'debug', 'info', 'warn', 'error', 'fatal'];
|
||
if (!options.logger) {
|
||
// use vanity logger
|
||
levels.forEach(level => {
|
||
response[level] = () => false;
|
||
});
|
||
return response;
|
||
}
|
||
const logger = options.logger === true ? createDefaultLogger(levels) : options.logger;
|
||
levels.forEach(level => {
|
||
response[level] = (...args) => {
|
||
// the bunyan forms: a data object first, or the message alone
|
||
const data = typeof args[0] === 'string' ? undefined : args.shift();
|
||
(0, exports._logFunc)(logger, level, defaults, data, ...args);
|
||
};
|
||
});
|
||
return response;
|
||
};
|
||
exports.getLogger = getLogger;
|
||
/**
|
||
* Wrapper for creating a callback that either resolves or rejects a promise
|
||
* based on input
|
||
*
|
||
* @param resolve Function to run if callback is called
|
||
* @param reject Function to run if callback ends with an error
|
||
*/
|
||
const callbackPromise = (resolve, reject) => function (...args) {
|
||
const err = args.shift();
|
||
if (err) {
|
||
reject(err);
|
||
}
|
||
else {
|
||
resolve(...args);
|
||
}
|
||
};
|
||
exports.callbackPromise = callbackPromise;
|
||
const parseDataURI = (uri) => {
|
||
if (typeof uri !== 'string') {
|
||
return null;
|
||
}
|
||
// Early return for non-data URIs to avoid unnecessary processing
|
||
if (!uri.startsWith('data:')) {
|
||
return null;
|
||
}
|
||
// Find the first comma safely - this prevents ReDoS
|
||
const commaPos = uri.indexOf(',');
|
||
if (commaPos === -1) {
|
||
return null;
|
||
}
|
||
const data = uri.substring(commaPos + 1);
|
||
const metaStr = uri.substring('data:'.length, commaPos);
|
||
let encoding;
|
||
const metaEntries = metaStr.split(';');
|
||
if (metaEntries.length > 0) {
|
||
const lastEntry = metaEntries[metaEntries.length - 1].toLowerCase().trim();
|
||
// Only recognize valid encoding types to prevent manipulation
|
||
if (['base64', 'utf8', 'utf-8'].includes(lastEntry) && lastEntry.indexOf('=') === -1) {
|
||
encoding = lastEntry;
|
||
metaEntries.pop();
|
||
}
|
||
}
|
||
const contentType = metaEntries.length > 0 ? metaEntries.shift() : 'application/octet-stream';
|
||
const params = {};
|
||
for (let i = 0; i < metaEntries.length; i++) {
|
||
const entry = metaEntries[i];
|
||
const sepPos = entry.indexOf('=');
|
||
if (sepPos > 0) {
|
||
// Ensure there's a key before the '='
|
||
const key = entry.substring(0, sepPos).trim();
|
||
const value = entry.substring(sepPos + 1).trim();
|
||
if (key && !(0, objects_js_1.isProtoKey)(key)) {
|
||
params[key] = value;
|
||
}
|
||
}
|
||
}
|
||
// Decode data based on encoding with proper error handling
|
||
let bufferData;
|
||
try {
|
||
if (encoding === 'base64') {
|
||
bufferData = Buffer.from(data, 'base64');
|
||
}
|
||
else {
|
||
try {
|
||
bufferData = Buffer.from(decodeURIComponent(data));
|
||
}
|
||
catch (_decodeError) {
|
||
bufferData = Buffer.from(data);
|
||
}
|
||
}
|
||
}
|
||
catch (_bufferError) {
|
||
bufferData = Buffer.alloc(0);
|
||
}
|
||
return {
|
||
data: bufferData,
|
||
encoding: encoding || null,
|
||
contentType: contentType || 'application/octet-stream',
|
||
params
|
||
};
|
||
};
|
||
exports.parseDataURI = parseDataURI;
|
||
function resolveContent(data, key, options, callback) {
|
||
// options is optional; support the legacy resolveContent(data, key, callback) signature
|
||
if (!callback && typeof options === 'function') {
|
||
callback = options;
|
||
options = false;
|
||
}
|
||
options = options || {};
|
||
let promise;
|
||
if (!callback) {
|
||
promise = new Promise((resolve, reject) => {
|
||
callback = (0, exports.callbackPromise)(resolve, reject);
|
||
});
|
||
}
|
||
resolveContentValue(data, key, options, callback);
|
||
return promise;
|
||
}
|
||
function resolveContentValue(data, key, options, callback) {
|
||
let content = (data && data[key] && data[key].content) || data[key];
|
||
const encoding = ((typeof data[key] === 'object' && data[key].encoding) || 'utf8')
|
||
.toString()
|
||
.toLowerCase()
|
||
.replace(/[-_\s]/g, '');
|
||
if (!content) {
|
||
return callback(null, content);
|
||
}
|
||
if (typeof content === 'object') {
|
||
if (typeof content.pipe === 'function') {
|
||
return resolveStream(content, (err, value) => {
|
||
if (err) {
|
||
return callback(err);
|
||
}
|
||
// we can't stream twice the same content, so we need
|
||
// to replace the stream object with the streaming result
|
||
if (data[key].content) {
|
||
data[key].content = value;
|
||
}
|
||
else {
|
||
data[key] = value;
|
||
}
|
||
callback(null, value);
|
||
});
|
||
}
|
||
else if (/^data:/i.test(content.path || content.href)) {
|
||
const parsedDataUri = (0, exports.parseDataURI)(content.path || content.href);
|
||
return callback(null, parsedDataUri && parsedDataUri.data ? parsedDataUri.data : Buffer.alloc(0));
|
||
}
|
||
else if (content.href || /^https?:\/\//i.test(content.path)) {
|
||
// An href is always a URL, and so is a path that looks like one. Let nmfetch
|
||
// decide whether it is fetchable, it validates the parsed URL. Testing the raw
|
||
// string here instead would let a file: href fall through to the "return as is"
|
||
// default below and travel on inside the resolved message.
|
||
const url = content.href || content.path;
|
||
if (options.disableUrlAccess) {
|
||
setImmediate(() => {
|
||
const err = new Error('Url access rejected for ' + url);
|
||
err.code = errors.EURLACCESS;
|
||
callback(err);
|
||
});
|
||
return;
|
||
}
|
||
return resolveStream((0, index_js_1.default)(url, { headers: content.httpHeaders, tls: content.tls }), callback);
|
||
}
|
||
else if (content.path) {
|
||
if (options.disableFileAccess) {
|
||
setImmediate(() => {
|
||
const err = new Error('File access rejected for ' + content.path);
|
||
err.code = errors.EFILEACCESS;
|
||
callback(err);
|
||
});
|
||
return;
|
||
}
|
||
return resolveStream(node_fs_1.default.createReadStream(content.path), callback);
|
||
}
|
||
}
|
||
if (typeof data[key].content === 'string' && !['utf8', 'usascii', 'ascii'].includes(encoding)) {
|
||
content = Buffer.from(data[key].content, encoding);
|
||
}
|
||
// default action, return as is
|
||
setImmediate(() => callback(null, content));
|
||
}
|
||
/**
|
||
* Copies properties from source objects to target objects
|
||
*/
|
||
const assign = function (...args) {
|
||
const target = args.shift() || {};
|
||
args.forEach(source => {
|
||
Object.keys(source || {}).forEach(key => {
|
||
if ((0, objects_js_1.isProtoKey)(key)) {
|
||
return;
|
||
}
|
||
if (['tls', 'auth'].includes(key) && source[key] && typeof source[key] === 'object') {
|
||
// tls and auth are special keys that need to be enumerated separately
|
||
// other objects are passed as is. Enumerating is a copy of user supplied
|
||
// keys just like the loop above, so it gets the same treatment
|
||
target[key] = (0, objects_js_1.copyOwnKeys)(target[key] || {}, source[key]);
|
||
}
|
||
else {
|
||
target[key] = source[key];
|
||
}
|
||
});
|
||
});
|
||
return target;
|
||
};
|
||
exports.assign = assign;
|
||
const encodeXText = (str) => {
|
||
// ! 0x21
|
||
// + 0x2B
|
||
// = 0x3D
|
||
// ~ 0x7E
|
||
if (!/[^\x21-\x2A\x2C-\x3C\x3E-\x7E]/.test(str)) {
|
||
return str;
|
||
}
|
||
const buf = Buffer.from(str);
|
||
let result = '';
|
||
for (let i = 0, len = buf.length; i < len; i++) {
|
||
const c = buf[i];
|
||
if (c < 0x21 || c > 0x7e || c === 0x2b || c === 0x3d) {
|
||
result += '+' + (c < 0x10 ? '0' : '') + c.toString(16).toUpperCase();
|
||
}
|
||
else {
|
||
result += String.fromCharCode(c);
|
||
}
|
||
}
|
||
return result;
|
||
};
|
||
exports.encodeXText = encodeXText;
|
||
// The first error a stream emitted, see recordStreamErrors
|
||
const streamErrors = new WeakMap();
|
||
/**
|
||
* Keeps the first error a content stream emits before it is read, so the error is reported
|
||
* when the stream is read instead of being thrown as unhandled. The listener stays attached
|
||
* for good, a stream that emits 'error' more than once never throws the later ones either
|
||
*
|
||
* @param stream Readable stream
|
||
*/
|
||
function recordStreamErrors(stream) {
|
||
stream.on('error', err => {
|
||
if (!streamErrors.has(stream)) {
|
||
streamErrors.set(stream, err);
|
||
}
|
||
});
|
||
}
|
||
/**
|
||
* Destroys a value if it is a readable stream that was not destroyed yet
|
||
*
|
||
* @param value Any content value
|
||
*/
|
||
function destroyStream(value) {
|
||
const stream = value;
|
||
if (stream && typeof stream.pipe === 'function' && typeof stream.destroy === 'function' && !stream.destroyed) {
|
||
stream.destroy();
|
||
}
|
||
}
|
||
/**
|
||
* Tells why a stream can not be read from start to end anymore. A stream that ended or was
|
||
* destroyed before anyone read it would never emit 'end' to a new reader, or, when piped, would
|
||
* end the destination right away and turn into an empty value without any error
|
||
*
|
||
* @param stream Readable stream
|
||
* @returns The error the stream failed with, an ESTREAM error, or null when it can be read
|
||
*/
|
||
function unreadableStreamError(stream) {
|
||
const err = streamErrors.get(stream) || stream.errored;
|
||
if (err) {
|
||
return err;
|
||
}
|
||
if (stream.readableEnded || stream.destroyed) {
|
||
const unreadable = new Error('Content stream was already read or destroyed');
|
||
unreadable.code = errors.ESTREAM;
|
||
return unreadable;
|
||
}
|
||
return null;
|
||
}
|
||
/**
|
||
* Streams a stream value into a Buffer
|
||
*
|
||
* @param stream Readable stream
|
||
* @param callback Callback function with (err, value)
|
||
*/
|
||
function resolveStream(stream, callback) {
|
||
let responded = false;
|
||
const respond = (err, value) => {
|
||
if (responded) {
|
||
return;
|
||
}
|
||
responded = true;
|
||
callback(err, value);
|
||
};
|
||
const unreadable = unreadableStreamError(stream);
|
||
if (unreadable) {
|
||
// absorbs a later 'error' from the stream, there is nobody left to report it to
|
||
stream.on('error', () => false);
|
||
setImmediate(() => respond(unreadable));
|
||
return;
|
||
}
|
||
const chunks = [];
|
||
let chunklen = 0;
|
||
stream.on('data', (chunk) => {
|
||
if (typeof chunk === 'string') {
|
||
chunk = Buffer.from(chunk);
|
||
}
|
||
chunks.push(chunk);
|
||
chunklen += chunk.length;
|
||
});
|
||
// finished() also reports a stream that is destroyed before 'end', which would otherwise
|
||
// leave the callback waiting forever
|
||
(0, node_stream_1.finished)(stream, { writable: false }, err => {
|
||
if (err) {
|
||
return respond(err);
|
||
}
|
||
let value;
|
||
try {
|
||
value = Buffer.concat(chunks, chunklen);
|
||
}
|
||
catch (E) {
|
||
return respond(E);
|
||
}
|
||
respond(null, value);
|
||
});
|
||
}
|
||
/**
|
||
* Generates a bunyan-like logger that prints to console
|
||
*
|
||
* @returns Bunyan logger instance
|
||
*/
|
||
function createDefaultLogger(levels) {
|
||
const levelMaxLen = levels.reduce((max, level) => Math.max(max, level.length), 0);
|
||
const levelNames = new Map();
|
||
levels.forEach(level => {
|
||
let levelName = level.toUpperCase();
|
||
if (levelName.length < levelMaxLen) {
|
||
levelName += ' '.repeat(levelMaxLen - levelName.length);
|
||
}
|
||
levelNames.set(level, levelName);
|
||
});
|
||
const print = (level, entry, message, ...args) => {
|
||
let prefix = '';
|
||
if (entry) {
|
||
if (entry.tnx === 'server') {
|
||
prefix = 'S: ';
|
||
}
|
||
else if (entry.tnx === 'client') {
|
||
prefix = 'C: ';
|
||
}
|
||
if (entry.sid) {
|
||
prefix = '[' + entry.sid + '] ' + prefix;
|
||
}
|
||
if (entry.cid) {
|
||
prefix = '[#' + entry.cid + '] ' + prefix;
|
||
}
|
||
}
|
||
message = node_util_1.default.format(message, ...args);
|
||
message.split(/\r?\n/).forEach((line) => {
|
||
console.log('[%s] %s %s', new Date().toISOString().substr(0, 19).replace(/T/, ' '), levelNames.get(level), prefix + line);
|
||
});
|
||
};
|
||
const logger = {};
|
||
levels.forEach(level => {
|
||
logger[level] = print.bind(null, level);
|
||
});
|
||
return logger;
|
||
}
|
||
// domainToASCII and domainToUnicode are WHATWG host parsers rather than plain IDNA
|
||
// mappers, so they do more than map: they cut the host at '/', '\\', '?' and '#', drop C0
|
||
// controls, and percent-decode. Handing them 'evil.example/mail.corp.example' returns the
|
||
// deliverable 'evil.example', which would turn a value the bundled codec leaves as
|
||
// unroutable garbage into mail for a domain the sender never named. None of these
|
||
// characters are legal in a domain, so keep them away from the mapper.
|
||
const URL_PARSER_UNSAFE = /[/\\?#%\x00-\x20\x7F]/;
|
||
// RFC 1035 bounds a label at 63 octets and a domain at 253. An A-label is never shorter than
|
||
// the code points it encodes, so anything past these bounds could not have become a valid
|
||
// domain whichever way it is encoded. The bundled codec is super-linear in the length of a
|
||
// label, a 60 KB label of distinct code points took seconds to encode, several times per
|
||
// address, so it is only handed what could be a domain
|
||
const MAX_DOMAIN_LENGTH = 253;
|
||
const MAX_LABEL_LENGTH = 63;
|
||
/**
|
||
* Checks a domain against the DNS length limits, counted in code points per label and in
|
||
* total. The label separators are the ones the bundled codec splits on.
|
||
*
|
||
* @param domain Domain to check
|
||
* @return true if a label or the domain as a whole is too long to be a valid domain
|
||
*/
|
||
function exceedsDomainLimits(domain) {
|
||
let total = 0;
|
||
let label = 0;
|
||
for (const char of domain) {
|
||
total++;
|
||
if (char === '.' || char === '。' || char === '.' || char === '。') {
|
||
label = 0;
|
||
}
|
||
else if (++label > MAX_LABEL_LENGTH) {
|
||
return true;
|
||
}
|
||
}
|
||
return total > MAX_DOMAIN_LENGTH;
|
||
}
|
||
/**
|
||
* Encodes a domain the way browsers, the WHATWG URL Standard and DNS facing resolvers do,
|
||
* which is with UTS-46 mapping applied before the Punycode step.
|
||
*
|
||
* The bundled codec is plain RFC 3492 and maps nothing, so it disagrees with every
|
||
* conformant parser on any domain holding a mapped or ignored code point. An invisible
|
||
* U+00AD in 'compa\u00ADny.com' encoded to 'xn--company-pka.com' where a validator reads
|
||
* 'company.com', which let an allow-listed domain be checked and a different one mailed.
|
||
*
|
||
* Anything the URL parser does not accept as a hostname, an address literal such as
|
||
* '[127.0.0.1]' included, comes back empty and falls through to the bundled codec, which
|
||
* leaves those as they were supplied apart from lowercasing them.
|
||
*
|
||
* The mapper gets the domain as supplied, since UTS-46 does its own case mapping and
|
||
* toLowerCase() differs from it: it turns U+1E9E into U+00DF where Node 20 and 22 map it
|
||
* to 'ss', and a word-final U+03A3 into U+03C2 where UTS-46 gives U+03C3. Lowercasing
|
||
* first sent 'user@stra\u1E9Ee.de', which a validator reads as strasse.de, to xn--strae-oqa.de.
|
||
*
|
||
* @param domain Domain to encode
|
||
* @param toUnicode Return the U-label form instead of the A-label form
|
||
* @return Encoded domain
|
||
*/
|
||
function normalizeDomain(domain, toUnicode) {
|
||
// domainToASCII and domainToUnicode landed in Node 7, the bundled codec covers Node 6
|
||
if (typeof node_url_1.default.domainToASCII === 'function' && typeof node_url_1.default.domainToUnicode === 'function' && !URL_PARSER_UNSAFE.test(domain)) {
|
||
// The U-label form is decoded from the A-label form rather than from the input:
|
||
// Deno's domainToASCII returns an empty string for a label it cannot decode, while
|
||
// its domainToUnicode returns the label with U+FFFD in place of the bad part
|
||
const ascii = node_url_1.default.domainToASCII(domain);
|
||
const mapped = ascii && toUnicode ? node_url_1.default.domainToUnicode(ascii) : ascii;
|
||
if (mapped) {
|
||
return mapped;
|
||
}
|
||
}
|
||
domain = domain.toLowerCase();
|
||
if (exceedsDomainLimits(domain)) {
|
||
// too long to be a domain whichever way it is encoded, keep it as supplied
|
||
return domain;
|
||
}
|
||
return toUnicode ? punycode.toUnicode(domain) : punycode.toASCII(domain);
|
||
}
|