From d946dff18847041a394a6c771c6db2fb60d5b28b Mon Sep 17 00:00:00 2001 From: "Jake(Jaehak Song)" Date: Fri, 12 Sep 2025 17:57:44 +0900 Subject: [PATCH 1/4] Remove net/tls polyfills for Cloudflare compatibility Cloudflare Workers now provide built-in net and tls polyfills, making custom polyfills unnecessary and fixing memory leaks caused by the previous Socket polyfill implementation. --- cf/polyfills.js | 123 ------------------------------------------------ transpile.cf.js | 2 - 2 files changed, 125 deletions(-) diff --git a/cf/polyfills.js b/cf/polyfills.js index 53c5203d..58a3b81c 100644 --- a/cf/polyfills.js +++ b/cf/polyfills.js @@ -108,126 +108,3 @@ export const net = { 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) - queueMicrotask(() => { - if (tasks.has(id)) { - fn() - tasks.delete(id) - } - }) - return id -} - -function clearImmediate(id) { - tasks.delete(id) -} 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\'') From 20ee6f3031c8331263be5005a0f475b1d2ec916b Mon Sep 17 00:00:00 2001 From: "Jake(Jaehak Song)" Date: Fri, 12 Sep 2025 18:18:17 +0900 Subject: [PATCH 2/4] Restore setImmediate and clearImmediate functions --- cf/polyfills.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/cf/polyfills.js b/cf/polyfills.js index 58a3b81c..39be40f5 100644 --- a/cf/polyfills.js +++ b/cf/polyfills.js @@ -108,3 +108,19 @@ export const net = { export { setImmediate, clearImmediate } +function setImmediate(fn) { + const id = ids++ + tasks.add(id) + queueMicrotask(() => { + if (tasks.has(id)) { + fn() + tasks.delete(id) + } + }) + return id +} + +function clearImmediate(id) { + tasks.delete(id) +} + From 9e0206089d40799a6d951f75b5313fb90a75fea7 Mon Sep 17 00:00:00 2001 From: "Jake(Jaehak Song)" Date: Fri, 12 Sep 2025 18:33:49 +0900 Subject: [PATCH 3/4] build --- cf/src/connection.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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' From 97b0ea671e7f5004219e4e18ec6f902f9ab7c9f5 Mon Sep 17 00:00:00 2001 From: "Jake(Jaehak Song)" Date: Fri, 12 Sep 2025 18:41:18 +0900 Subject: [PATCH 4/4] Remove net polyfills for Cloudflare compatibility --- cf/polyfills.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cf/polyfills.js b/cf/polyfills.js index 39be40f5..7f12bf10 100644 --- a/cf/polyfills.js +++ b/cf/polyfills.js @@ -101,10 +101,6 @@ export const fs = { } } -export const net = { - isIP: (x) => IPv4Reg.test(x) ? 4 : IPv6Reg.test(x) ? 6 : 0, - Socket -} export { setImmediate, clearImmediate }