diff --git a/cf/polyfills.js b/cf/polyfills.js index 53c5203d..7f12bf10 100644 --- a/cf/polyfills.js +++ b/cf/polyfills.js @@ -101,121 +101,9 @@ export const fs = { } } -export const net = { - isIP: (x) => IPv4Reg.test(x) ? 4 : IPv6Reg.test(x) ? 6 : 0, - Socket -} export { setImmediate, clearImmediate } -export const tls = { - connect({ socket: tcp, servername }) { - tcp.writer.releaseLock() - tcp.reader.releaseLock() - tcp.readyState = 'upgrading' - tcp.raw = tcp.raw.startTls({ servername }) - tcp.raw.closed.then( - () => tcp.emit('close'), - (e) => tcp.emit('error', e) - ) - tcp.writer = tcp.raw.writable.getWriter() - tcp.reader = tcp.raw.readable.getReader() - - tcp.writer.ready.then(() => { - tcp.read() - tcp.readyState = 'upgrade' - }) - return tcp - } -} - -function Socket() { - const tcp = Object.assign(new EventEmitter(), { - readyState: 'open', - raw: null, - writer: null, - reader: null, - connect, - write, - end, - destroy, - read - }) - - return tcp - - async function connect(port, host) { - try { - tcp.readyState = 'opening' - const { connect } = await import('cloudflare:sockets') - tcp.raw = connect(host + ':' + port, tcp.ssl ? { secureTransport: 'starttls' } : {}) - tcp.raw.closed.then( - () => { - tcp.readyState !== 'upgrade' - ? close() - : ((tcp.readyState = 'open'), tcp.emit('secureConnect')) - }, - (e) => tcp.emit('error', e) - ) - tcp.writer = tcp.raw.writable.getWriter() - tcp.reader = tcp.raw.readable.getReader() - - tcp.ssl ? readFirst() : read() - tcp.writer.ready.then(() => { - tcp.readyState = 'open' - tcp.emit('connect') - }) - } catch (err) { - error(err) - } - } - - function close() { - if (tcp.readyState === 'closed') - return - - tcp.readyState = 'closed' - tcp.emit('close') - } - - function write(data, cb) { - tcp.writer.write(data).then(cb, error) - return true - } - - function end(data) { - return data - ? tcp.write(data, () => tcp.raw.close()) - : tcp.raw.close() - } - - function destroy() { - tcp.destroyed = true - tcp.end() - } - - async function read() { - try { - let done - , value - while (({ done, value } = await tcp.reader.read(), !done)) - tcp.emit('data', Buffer.from(value)) - } catch (err) { - error(err) - } - } - - async function readFirst() { - const { value } = await tcp.reader.read() - tcp.emit('data', Buffer.from(value)) - } - - function error(err) { - tcp.emit('error', err) - tcp.emit('close') - } -} - function setImmediate(fn) { const id = ids++ tasks.add(id) @@ -231,3 +119,4 @@ function setImmediate(fn) { function clearImmediate(id) { tasks.delete(id) } + diff --git a/cf/src/connection.js b/cf/src/connection.js index 203af80d..1410f9fa 100644 --- a/cf/src/connection.js +++ b/cf/src/connection.js @@ -1,7 +1,7 @@ import { Buffer } from 'node:buffer' import { setImmediate, clearImmediate } from '../polyfills.js' -import { net } from '../polyfills.js' -import { tls } from '../polyfills.js' +import net from 'node:net' +import tls from 'node:tls' import { crypto } from '../polyfills.js' import Stream from 'node:stream' import { performance } from '../polyfills.js' diff --git a/transpile.cf.js b/transpile.cf.js index bbe4c500..27183770 100644 --- a/transpile.cf.js +++ b/transpile.cf.js @@ -29,8 +29,6 @@ function transpile(x) { : '' return process + buffer + timers + x - .replace('import net from \'net\'', 'import { net } from \'../polyfills.js\'') - .replace('import tls from \'tls\'', 'import { tls } from \'../polyfills.js\'') .replace('import crypto from \'crypto\'', 'import { crypto } from \'../polyfills.js\'') .replace('import os from \'os\'', 'import { os } from \'../polyfills.js\'') .replace('import fs from \'fs\'', 'import { fs } from \'../polyfills.js\'')