|
| 1 | +import { EventEmitter } from 'node:events' |
| 2 | +import { Buffer } from 'node:buffer' |
| 3 | + |
| 4 | +const Crypto = globalThis.crypto |
| 5 | + |
| 6 | +let ids = 1 |
| 7 | +const tasks = new Set() |
| 8 | + |
| 9 | +const v4Seg = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])' |
| 10 | +const v4Str = `(${v4Seg}[.]){3}${v4Seg}` |
| 11 | +const IPv4Reg = new RegExp(`^${v4Str}$`) |
| 12 | + |
| 13 | +const v6Seg = '(?:[0-9a-fA-F]{1,4})' |
| 14 | +const IPv6Reg = new RegExp( |
| 15 | + '^(' + |
| 16 | + `(?:${v6Seg}:){7}(?:${v6Seg}|:)|` + |
| 17 | + `(?:${v6Seg}:){6}(?:${v4Str}|:${v6Seg}|:)|` + |
| 18 | + `(?:${v6Seg}:){5}(?::${v4Str}|(:${v6Seg}){1,2}|:)|` + |
| 19 | + `(?:${v6Seg}:){4}(?:(:${v6Seg}){0,1}:${v4Str}|(:${v6Seg}){1,3}|:)|` + |
| 20 | + `(?:${v6Seg}:){3}(?:(:${v6Seg}){0,2}:${v4Str}|(:${v6Seg}){1,4}|:)|` + |
| 21 | + `(?:${v6Seg}:){2}(?:(:${v6Seg}){0,3}:${v4Str}|(:${v6Seg}){1,5}|:)|` + |
| 22 | + `(?:${v6Seg}:){1}(?:(:${v6Seg}){0,4}:${v4Str}|(:${v6Seg}){1,6}|:)|` + |
| 23 | + `(?::((?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:))` + |
| 24 | + ')(%[0-9a-zA-Z-.:]{1,})?$' |
| 25 | +) |
| 26 | + |
| 27 | +const textEncoder = new TextEncoder() |
| 28 | +export const crypto = { |
| 29 | + randomBytes: l => Crypto.getRandomValues(Buffer.alloc(l)), |
| 30 | + pbkdf2Sync: async(password, salt, iterations, keylen) => |
| 31 | + Crypto.subtle.deriveBits( |
| 32 | + { |
| 33 | + name: 'PBKDF2', |
| 34 | + hash: 'SHA-256', |
| 35 | + salt, |
| 36 | + iterations |
| 37 | + }, |
| 38 | + await Crypto.subtle.importKey( |
| 39 | + 'raw', |
| 40 | + textEncoder.encode(password), |
| 41 | + 'PBKDF2', |
| 42 | + false, |
| 43 | + ['deriveBits'] |
| 44 | + ), |
| 45 | + keylen * 8, |
| 46 | + ['deriveBits'] |
| 47 | + ), |
| 48 | + createHash: type => ({ |
| 49 | + update: x => ({ |
| 50 | + digest: () => { |
| 51 | + if (type !== 'sha256') |
| 52 | + throw Error('createHash only supports sha256 on cloudflare.') |
| 53 | + if (!(x instanceof Uint8Array)) |
| 54 | + x = textEncoder.encode(x) |
| 55 | + return Crypto.subtle.digest('SHA-256', x) |
| 56 | + } |
| 57 | + }) |
| 58 | + }), |
| 59 | + createHmac: (type, key) => ({ |
| 60 | + update: x => ({ |
| 61 | + digest: async() => |
| 62 | + Buffer.from( |
| 63 | + await Crypto.subtle.sign( |
| 64 | + 'HMAC', |
| 65 | + await Crypto.subtle.importKey('raw', key, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']), |
| 66 | + textEncoder.encode(x) |
| 67 | + ) |
| 68 | + ) |
| 69 | + }) |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +export const process = { |
| 74 | + env: {} |
| 75 | +} |
| 76 | + |
| 77 | +export const os = { |
| 78 | + userInfo() { |
| 79 | + return { username: 'postgres' } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +export const fs = { |
| 84 | + readFile() { |
| 85 | + throw new Error('Reading files not supported on CloudFlare') |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +export const net = { |
| 90 | + isIP: (x) => RegExp.prototype.test.call(IPv4Reg, x) ? 4 : RegExp.prototype.test.call(IPv6Reg, x) ? 6 : 0, |
| 91 | + Socket |
| 92 | +} |
| 93 | + |
| 94 | +export { setImmediate, clearImmediate } |
| 95 | + |
| 96 | +export const tls = { |
| 97 | + connect({ socket: tcp, servername }) { |
| 98 | + tcp.writer.releaseLock() |
| 99 | + tcp.reader.releaseLock() |
| 100 | + tcp.readyState = 'upgrading' |
| 101 | + tcp.raw = tcp.raw.startTls({ servername }) |
| 102 | + tcp.raw.closed.then( |
| 103 | + () => tcp.emit('close'), |
| 104 | + (e) => tcp.emit('error', e) |
| 105 | + ) |
| 106 | + tcp.writer = tcp.raw.writable.getWriter() |
| 107 | + tcp.reader = tcp.raw.readable.getReader() |
| 108 | + |
| 109 | + tcp.writer.ready.then(() => { |
| 110 | + tcp.read() |
| 111 | + tcp.readyState = 'upgrade' |
| 112 | + }) |
| 113 | + return tcp |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +function Socket() { |
| 118 | + const tcp = Object.assign(new EventEmitter(), { |
| 119 | + readyState: 'open', |
| 120 | + raw: null, |
| 121 | + writer: null, |
| 122 | + reader: null, |
| 123 | + connect, |
| 124 | + write, |
| 125 | + end, |
| 126 | + destroy, |
| 127 | + read |
| 128 | + }) |
| 129 | + |
| 130 | + return tcp |
| 131 | + |
| 132 | + async function connect(port, host) { |
| 133 | + try { |
| 134 | + tcp.readyState = 'opening' |
| 135 | + const { connect } = await import('cloudflare:sockets') |
| 136 | + tcp.raw = connect(host + ':' + port, tcp.ssl ? { secureTransport: 'starttls' } : {}) |
| 137 | + tcp.raw.closed.then( |
| 138 | + () => { |
| 139 | + tcp.readyState !== 'upgrade' |
| 140 | + ? close() |
| 141 | + : ((tcp.readyState = 'open'), tcp.emit('secureConnect')) |
| 142 | + }, |
| 143 | + (e) => tcp.emit('error', e) |
| 144 | + ) |
| 145 | + tcp.writer = tcp.raw.writable.getWriter() |
| 146 | + tcp.reader = tcp.raw.readable.getReader() |
| 147 | + |
| 148 | + tcp.ssl ? readFirst() : read() |
| 149 | + tcp.writer.ready.then(() => { |
| 150 | + tcp.readyState = 'open' |
| 151 | + tcp.emit('connect') |
| 152 | + }) |
| 153 | + } catch (err) { |
| 154 | + error(err) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + function close() { |
| 159 | + if (tcp.readyState === 'closed') |
| 160 | + return |
| 161 | + |
| 162 | + tcp.readyState = 'closed' |
| 163 | + tcp.emit('close') |
| 164 | + } |
| 165 | + |
| 166 | + function write(data, cb) { |
| 167 | + tcp.writer.write(data).then(cb, error) |
| 168 | + return true |
| 169 | + } |
| 170 | + |
| 171 | + function end(data) { |
| 172 | + return data |
| 173 | + ? tcp.write(data, () => tcp.raw.close()) |
| 174 | + : tcp.raw.close() |
| 175 | + } |
| 176 | + |
| 177 | + function destroy() { |
| 178 | + tcp.destroyed = true |
| 179 | + tcp.end() |
| 180 | + } |
| 181 | + |
| 182 | + async function read() { |
| 183 | + try { |
| 184 | + let done |
| 185 | + , value |
| 186 | + while (({ done, value } = await tcp.reader.read(), !done)) |
| 187 | + tcp.emit('data', Buffer.from(value)) |
| 188 | + } catch (err) { |
| 189 | + error(err) |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + async function readFirst() { |
| 194 | + const { value } = await tcp.reader.read() |
| 195 | + tcp.emit('data', Buffer.from(value)) |
| 196 | + } |
| 197 | + |
| 198 | + function error(err) { |
| 199 | + tcp.emit('error', err) |
| 200 | + tcp.emit('close') |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +function setImmediate(fn) { |
| 205 | + const id = ids++ |
| 206 | + tasks.add(id) |
| 207 | + queueMicrotask(() => { |
| 208 | + if (tasks.has(id)) { |
| 209 | + fn() |
| 210 | + tasks.delete(id) |
| 211 | + } |
| 212 | + }) |
| 213 | + return id |
| 214 | +} |
| 215 | + |
| 216 | +function clearImmediate(id) { |
| 217 | + tasks.delete(id) |
| 218 | +} |
0 commit comments