diff options
Diffstat (limited to 'src/bun.js/node-tls.exports.js')
-rw-r--r-- | src/bun.js/node-tls.exports.js | 134 |
1 files changed, 99 insertions, 35 deletions
diff --git a/src/bun.js/node-tls.exports.js b/src/bun.js/node-tls.exports.js index a29020a3d..cbbab8e4e 100644 --- a/src/bun.js/node-tls.exports.js +++ b/src/bun.js/node-tls.exports.js @@ -1,9 +1,18 @@ +const { isTypedArray } = import.meta.require("util/types"); + function parseCertString() { throw Error("Not implemented"); } -function mapStringArray(item) { - return item.toString(); +function isValidTLSArray(obj) { + if (typeof obj === "string" || isTypedArray(obj) || obj instanceof ArrayBuffer || obj instanceof Blob) return true; + if (Array.isArray(obj)) { + for (var i = 0; i < obj.length; i++) { + if (typeof obj !== "string" && !isTypedArray(obj) && !(obj instanceof ArrayBuffer) && !(obj instanceof Blob)) + return false; + } + return true; + } } var InternalSecureContext = class SecureContext { @@ -12,32 +21,59 @@ var InternalSecureContext = class SecureContext { constructor(options) { const context = {}; if (options) { - if (options.key) { - context.key = (Array.isArray(options.key) ? options.key : [options.key]).map(mapStringArray); - } else context.key = undefined; - - if (options.passphrase) context.passphrase = options.passphrase; - else context.passphrase = undefined; + let key = options.key; + if (key) { + if (!isValidTLSArray(key)) { + throw new TypeError( + "key argument must be an string, Buffer, TypedArray, BunFile or an array containing string, Buffer, TypedArray or BunFile", + ); + } + this.key = key; + } + let cert = options.cert; + if (cert) { + if (!isValidTLSArray(cert)) { + throw new TypeError( + "cert argument must be an string, Buffer, TypedArray, BunFile or an array containing string, Buffer, TypedArray or BunFile", + ); + } + this.cert = cert; + } - if (options.cert) { - context.cert = (Array.isArray(options.cert) ? options.cert : [options.cert]).map(mapStringArray); - } else context.cert = undefined; + let ca = options.ca; + if (ca) { + if (!isValidTLSArray(ca)) { + throw new TypeError( + "ca argument must be an string, Buffer, TypedArray, BunFile or an array containing string, Buffer, TypedArray or BunFile", + ); + } + this.ca = ca; + } - if (options.ca) { - context.ca = (Array.isArray(options.ca) ? options.ca : [options.ca]).map(mapStringArray); - } else context.ca = undefined; + let passphrase = options.passphrase; + if (passphrase && typeof passphrase !== "string") { + throw new TypeError("passphrase argument must be an string"); + } + this.passphrase = passphrase; - const secureOptions = options.secureOptions || 0; + let servername = options.servername; + if (servername && typeof servername !== "string") { + throw new TypeError("servername argument must be an string"); + } + this.servername = servername; - if (secureOptions) context.secureOptions = secureOptions; - else context.secureOptions = undefined; + let secureOptions = options.secureOptions || 0; + if (secureOptions && typeof secureOptions !== "number") { + throw new TypeError("secureOptions argument must be an number"); + } + this.secureOptions = secureOptions; } this.context = context; } }; -function SecureContext() { - return new InternalSecureContext(); +function SecureContext(options) { + return new InternalSecureContext(options); } function createSecureContext(options) { @@ -139,6 +175,7 @@ class Server extends NetServer { secureOptions; _rejectUnauthorized; _requestCert; + servername; constructor(options, secureConnectionListener) { super(options, secureConnectionListener); @@ -159,25 +196,52 @@ class Server extends NetServer { options = options.context; } if (options) { - if (options.key) { - this.key = (Array.isArray(options.key) ? options.key : [options.key]).map(mapStringArray); - } else this.key = undefined; - - if (options.passphrase) this.passphrase = options.passphrase; - else this.passphrase = undefined; + let key = options.key; + if (key) { + if (!isValidTLSArray(key)) { + throw new TypeError( + "key argument must be an string, Buffer, TypedArray, BunFile or an array containing string, Buffer, TypedArray or BunFile", + ); + } + this.key = key; + } + let cert = options.cert; + if (cert) { + if (!isValidTLSArray(cert)) { + throw new TypeError( + "cert argument must be an string, Buffer, TypedArray, BunFile or an array containing string, Buffer, TypedArray or BunFile", + ); + } + this.cert = cert; + } - if (options.cert) { - this.cert = (Array.isArray(options.cert) ? options.cert : [options.cert]).map(mapStringArray); - } else this.cert = undefined; + let ca = options.ca; + if (ca) { + if (!isValidTLSArray(ca)) { + throw new TypeError( + "ca argument must be an string, Buffer, TypedArray, BunFile or an array containing string, Buffer, TypedArray or BunFile", + ); + } + this.ca = ca; + } - if (options.ca) { - this.ca = (Array.isArray(options.ca) ? options.ca : [options.ca]).map(mapStringArray); - } else this.ca = undefined; + let passphrase = options.passphrase; + if (passphrase && typeof passphrase !== "string") { + throw new TypeError("passphrase argument must be an string"); + } + this.passphrase = passphrase; - const secureOptions = options.secureOptions || 0; + let servername = options.servername; + if (servername && typeof servername !== "string") { + throw new TypeError("servername argument must be an string"); + } + this.servername = servername; - if (secureOptions) this.secureOptions = secureOptions; - else this.secureOptions = undefined; + let secureOptions = options.secureOptions || 0; + if (secureOptions && typeof secureOptions !== "number") { + throw new TypeError("secureOptions argument must be an number"); + } + this.secureOptions = secureOptions; const requestCert = options.requestCert || false; @@ -203,7 +267,7 @@ class Server extends NetServer { [buntls](port, host, isClient) { return [ { - serverName: host || "localhost", + serverName: this.servername || host || "localhost", key: this.key, cert: this.cert, ca: this.ca, |