This commit is contained in:
2025-05-12 05:38:44 +09:00
parent dced21c3f8
commit 6d78bfa46e
8120 changed files with 1161564 additions and 0 deletions

View File

@ -0,0 +1,29 @@
'use strict';
const {isIP} = require('net');
const assert = require('assert');
const getHost = host => {
if (host[0] === '[') {
const idx = host.indexOf(']');
assert(idx !== -1);
return host.slice(1, idx);
}
const idx = host.indexOf(':');
if (idx === -1) {
return host;
}
return host.slice(0, idx);
};
module.exports = host => {
const servername = getHost(host);
if (isIP(servername)) {
return '';
}
return servername;
};

View File

@ -0,0 +1,20 @@
'use strict';
const checkType = (name, value, types) => {
const valid = types.some(type => {
const typeofType = typeof type;
if (typeofType === 'string') {
return typeof value === type;
}
return value instanceof type;
});
if (!valid) {
const names = types.map(type => typeof type === 'string' ? type : type.name);
throw new TypeError(`Expected '${name}' to be a type of ${names.join(' or ')}, got ${typeof value}`);
}
};
module.exports = checkType;

View File

@ -0,0 +1,33 @@
'use strict';
module.exports = stream => {
if (stream.listenerCount('error') !== 0) {
return stream;
}
stream.__destroy = stream._destroy;
stream._destroy = (...args) => {
const callback = args.pop();
stream.__destroy(...args, async error => {
await Promise.resolve();
callback(error);
});
};
const onError = error => {
// eslint-disable-next-line promise/prefer-await-to-then
Promise.resolve().then(() => {
stream.emit('error', error);
});
};
stream.once('error', onError);
// eslint-disable-next-line promise/prefer-await-to-then
Promise.resolve().then(() => {
stream.off('error', onError);
});
return stream;
};

51
book/node_modules/http2-wrapper/source/utils/errors.js generated vendored Normal file
View File

@ -0,0 +1,51 @@
'use strict';
/* istanbul ignore file: https://github.com/nodejs/node/blob/master/lib/internal/errors.js */
const makeError = (Base, key, getMessage) => {
module.exports[key] = class NodeError extends Base {
constructor(...args) {
super(typeof getMessage === 'string' ? getMessage : getMessage(args));
this.name = `${super.name} [${key}]`;
this.code = key;
}
};
};
makeError(TypeError, 'ERR_INVALID_ARG_TYPE', args => {
const type = args[0].includes('.') ? 'property' : 'argument';
let valid = args[1];
const isManyTypes = Array.isArray(valid);
if (isManyTypes) {
valid = `${valid.slice(0, -1).join(', ')} or ${valid.slice(-1)}`;
}
return `The "${args[0]}" ${type} must be ${isManyTypes ? 'one of' : 'of'} type ${valid}. Received ${typeof args[2]}`;
});
makeError(TypeError, 'ERR_INVALID_PROTOCOL', args =>
`Protocol "${args[0]}" not supported. Expected "${args[1]}"`
);
makeError(Error, 'ERR_HTTP_HEADERS_SENT', args =>
`Cannot ${args[0]} headers after they are sent to the client`
);
makeError(TypeError, 'ERR_INVALID_HTTP_TOKEN', args =>
`${args[0]} must be a valid HTTP token [${args[1]}]`
);
makeError(TypeError, 'ERR_HTTP_INVALID_HEADER_VALUE', args =>
`Invalid value "${args[0]} for header "${args[1]}"`
);
makeError(TypeError, 'ERR_INVALID_CHAR', args =>
`Invalid character in ${args[0]} [${args[1]}]`
);
makeError(
Error,
'ERR_HTTP2_NO_SOCKET_MANIPULATION',
'HTTP/2 sockets should not be directly manipulated (e.g. read and written)'
);

View File

@ -0,0 +1,13 @@
'use strict';
module.exports = header => {
switch (header) {
case ':method':
case ':scheme':
case ':authority':
case ':path':
return true;
default:
return false;
}
};

View File

@ -0,0 +1,8 @@
'use strict';
const stream = require('stream');
const tls = require('tls');
// Really awesome hack.
const JSStreamSocket = (new tls.TLSSocket(new stream.PassThrough()))._handle._parentWrap.constructor;
module.exports = JSStreamSocket;

View File

@ -0,0 +1,7 @@
'use strict';
module.exports = (from, to, events) => {
for (const event of events) {
from.on(event, (...args) => to.emit(event, ...args));
}
};

View File

@ -0,0 +1,102 @@
'use strict';
const {ERR_HTTP2_NO_SOCKET_MANIPULATION} = require('./errors.js');
/* istanbul ignore file */
/* https://github.com/nodejs/node/blob/6eec858f34a40ffa489c1ec54bb24da72a28c781/lib/internal/http2/compat.js#L195-L272 */
const proxySocketHandler = {
has(stream, property) {
// Replaced [kSocket] with .socket
const reference = stream.session === undefined ? stream : stream.session.socket;
return (property in stream) || (property in reference);
},
get(stream, property) {
switch (property) {
case 'on':
case 'once':
case 'end':
case 'emit':
case 'destroy':
return stream[property].bind(stream);
case 'writable':
case 'destroyed':
return stream[property];
case 'readable':
if (stream.destroyed) {
return false;
}
return stream.readable;
case 'setTimeout': {
const {session} = stream;
if (session !== undefined) {
return session.setTimeout.bind(session);
}
return stream.setTimeout.bind(stream);
}
case 'write':
case 'read':
case 'pause':
case 'resume':
throw new ERR_HTTP2_NO_SOCKET_MANIPULATION();
default: {
// Replaced [kSocket] with .socket
const reference = stream.session === undefined ? stream : stream.session.socket;
const value = reference[property];
return typeof value === 'function' ? value.bind(reference) : value;
}
}
},
getPrototypeOf(stream) {
if (stream.session !== undefined) {
// Replaced [kSocket] with .socket
return Reflect.getPrototypeOf(stream.session.socket);
}
return Reflect.getPrototypeOf(stream);
},
set(stream, property, value) {
switch (property) {
case 'writable':
case 'readable':
case 'destroyed':
case 'on':
case 'once':
case 'end':
case 'emit':
case 'destroy':
stream[property] = value;
return true;
case 'setTimeout': {
const {session} = stream;
if (session === undefined) {
stream.setTimeout = value;
} else {
session.setTimeout = value;
}
return true;
}
case 'write':
case 'read':
case 'pause':
case 'resume':
throw new ERR_HTTP2_NO_SOCKET_MANIPULATION();
default: {
// Replaced [kSocket] with .socket
const reference = stream.session === undefined ? stream : stream.session.socket;
reference[property] = value;
return true;
}
}
}
};
module.exports = proxySocketHandler;

View File

@ -0,0 +1,11 @@
'use strict';
const {ERR_INVALID_HTTP_TOKEN} = require('./errors.js');
const isRequestPseudoHeader = require('./is-request-pseudo-header.js');
const isValidHttpToken = /^[\^`\-\w!#$%&*+.|~]+$/;
module.exports = name => {
if (typeof name !== 'string' || (!isValidHttpToken.test(name) && !isRequestPseudoHeader(name))) {
throw new ERR_INVALID_HTTP_TOKEN('Header name', name);
}
};

View File

@ -0,0 +1,17 @@
'use strict';
const {
ERR_HTTP_INVALID_HEADER_VALUE,
ERR_INVALID_CHAR
} = require('./errors.js');
const isInvalidHeaderValue = /[^\t\u0020-\u007E\u0080-\u00FF]/;
module.exports = (name, value) => {
if (typeof value === 'undefined') {
throw new ERR_HTTP_INVALID_HEADER_VALUE(value, name);
}
if (isInvalidHeaderValue.test(value)) {
throw new ERR_INVALID_CHAR('header content', name);
}
};