From 4977956682eea1988af622e9ee83a040166e2a89 Mon Sep 17 00:00:00 2001
From: Kyiro <54126666+Kyiro@users.noreply.github.com>
Date: Sun, 7 Jan 2024 23:17:12 +0100
Subject: [PATCH 1/2] deno: move from std node polyfills to `node:` specifier
---
cjs/src/connection.js | 6 +-
deno/README.md | 4 +
deno/polyfills.js | 189 ----------------------------------------
deno/src/bytes.js | 2 +-
deno/src/connection.js | 21 ++---
deno/src/index.js | 6 +-
deno/src/large.js | 2 +-
deno/src/subscribe.js | 2 +-
deno/src/types.js | 2 +-
deno/tests/bootstrap.js | 2 +-
deno/tests/index.js | 10 +--
deno/tests/test.js | 4 +-
deno/types/index.d.ts | 6 +-
transpile.deno.js | 24 ++---
14 files changed, 39 insertions(+), 241 deletions(-)
delete mode 100644 deno/polyfills.js
diff --git a/cjs/src/connection.js b/cjs/src/connection.js
index 425e91cd..0e9d9c85 100644
--- a/cjs/src/connection.js
+++ b/cjs/src/connection.js
@@ -429,10 +429,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
lifeTimer.cancel()
connectTimer.cancel()
- if (socket.encrypted) {
- socket.removeAllListeners()
- socket = null
- }
+ socket.removeAllListeners()
+ socket = null
if (initial)
return reconnect()
diff --git a/deno/README.md b/deno/README.md
index 0fc569bb..0c792835 100644
--- a/deno/README.md
+++ b/deno/README.md
@@ -15,6 +15,10 @@
+### Installation
+```bash
+$ npm install postgres
+```
### Usage
Create your `sql` database instance
diff --git a/deno/polyfills.js b/deno/polyfills.js
deleted file mode 100644
index 71ee694d..00000000
--- a/deno/polyfills.js
+++ /dev/null
@@ -1,189 +0,0 @@
-/* global Deno */
-
-import { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'
-import { isIP } from 'https://deno.land/std@0.132.0/node/net.ts'
-
-const events = () => ({ data: [], error: [], drain: [], connect: [], secureConnect: [], close: [] })
-
-class Socket {
- constructor() {
- return createSocket()
- }
-}
-
-function createSocket() {
- let paused
- , resume
- , keepAlive
-
- const socket = {
- error,
- success,
- readyState: 'open',
- setKeepAlive: x => {
- keepAlive = x
- socket.raw && socket.raw.setKeepAlive && socket.raw.setKeepAlive(x)
- },
- connect: (port, hostname) => {
- socket.raw = null
- socket.readyState = 'connecting'
- typeof port === 'string'
- ? Deno.connect({ transport: 'unix', path: socket.path = port }).then(success, error)
- : Deno.connect({ transport: 'tcp', port: socket.port = port, hostname: socket.hostname = hostname || 'localhost' }).then(success, error) // eslint-disable-line
- return socket
- },
- pause: () => {
- paused = new Promise(r => resume = r)
- },
- resume: () => {
- resume && resume()
- paused = null
- },
- isPaused: () => !!paused,
- removeAllListeners: () => socket.events = events(),
- events: events(),
- raw: null,
- on: (x, fn) => socket.events[x].push(fn),
- once: (x, fn) => {
- if (x === 'data')
- socket.break = true
- const e = socket.events[x]
- e.push(once)
- once.once = fn
- function once(...args) {
- fn(...args)
- e.indexOf(once) > -1 && e.splice(e.indexOf(once), 1)
- }
- },
- removeListener: (x, fn) => {
- socket.events[x] = socket.events[x].filter(x => x !== fn && x.once !== fn)
- },
- write: (x, cb) => {
- socket.raw.write(x).then(l => {
- l < x.length
- ? socket.write(x.slice(l), cb)
- : (cb && cb(null))
- }).catch(err => {
- cb && cb()
- call(socket.events.error, err)
- })
- return false
- },
- destroy: () => close(),
- end: (x) => {
- x && socket.write(x)
- close()
- }
- }
-
- return socket
-
- async function success(raw) {
- if (socket.readyState !== 'connecting')
- return raw.close()
-
- const encrypted = socket.encrypted
- socket.raw = raw
- keepAlive != null && raw.setKeepAlive && raw.setKeepAlive(keepAlive)
- socket.readyState = 'open'
- socket.encrypted
- ? call(socket.events.secureConnect)
- : call(socket.events.connect)
-
- const b = new Uint8Array(1024)
- let result
-
- try {
- while ((result = socket.readyState === 'open' && await raw.read(b))) {
- call(socket.events.data, Buffer.from(b.subarray(0, result)))
- if (!encrypted && socket.break && (socket.break = false, b[0] === 83))
- return socket.break = false
- paused && await paused
- }
- } catch (e) {
- if (e instanceof Deno.errors.BadResource === false)
- error(e)
- }
-
- if (!socket.encrypted || encrypted)
- closed()
- }
-
- function close() {
- try {
- socket.raw && socket.raw.close()
- } catch (e) {
- if (e instanceof Deno.errors.BadResource === false)
- call(socket.events.error, e)
- }
- }
-
- function closed() {
- if (socket.readyState === 'closed')
- return
-
- socket.break = socket.encrypted = false
- socket.readyState = 'closed'
- call(socket.events.close)
- }
-
- function error(err) {
- call(socket.events.error, err)
- socket.raw
- ? close()
- : closed()
- }
-
- function call(xs, x) {
- xs.slice().forEach(fn => fn(x))
- }
-}
-
-export const net = {
- isIP,
- createServer() {
- const server = {
- address() {
- return { port: 9876 }
- },
- async listen() {
- server.raw = Deno.listen({ port: 9876, transport: 'tcp' })
- for await (const conn of server.raw)
- setTimeout(() => conn.close(), 500)
- },
- close() {
- server.raw.close()
- }
- }
- return server
- },
- Socket
-}
-
-export const tls = {
- connect({ socket, ...options }) {
- socket.encrypted = true
- socket.readyState = 'connecting'
- Deno.startTls(socket.raw, { hostname: socket.hostname, ...options })
- .then(socket.success, socket.error)
- socket.raw = null
- return socket
- }
-}
-
-let ids = 1
-const tasks = new Set()
-export const setImmediate = fn => {
- const id = ids++
- tasks.add(id)
- queueMicrotask(() => {
- if (tasks.has(id)) {
- fn()
- tasks.delete(id)
- }
- })
- return id
-}
-
-export const clearImmediate = id => tasks.delete(id)
-
diff --git a/deno/src/bytes.js b/deno/src/bytes.js
index fe9359db..48b6f983 100644
--- a/deno/src/bytes.js
+++ b/deno/src/bytes.js
@@ -1,4 +1,4 @@
-import { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'
+import { Buffer } from 'node:buffer'
const size = 256
let buffer = Buffer.allocUnsafe(size)
diff --git a/deno/src/connection.js b/deno/src/connection.js
index 334b9722..b1a43173 100644
--- a/deno/src/connection.js
+++ b/deno/src/connection.js
@@ -1,10 +1,9 @@
-import { HmacSha256 } from 'https://deno.land/std@0.132.0/hash/sha256.ts'
-import { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'
-import { setImmediate, clearImmediate } from '../polyfills.js'
-import { net } from '../polyfills.js'
-import { tls } from '../polyfills.js'
-import crypto from 'https://deno.land/std@0.132.0/node/crypto.ts'
-import Stream from 'https://deno.land/std@0.132.0/node/stream.ts'
+import { Buffer } from 'node:buffer'
+import { setImmediate, clearImmediate } from 'node:timers'
+import net from 'node:net'
+import tls from 'node:tls'
+import crypto from 'node:crypto'
+import Stream from 'node:stream'
import { stringify, handleValue, arrayParser, arraySerializer } from './types.js'
@@ -432,10 +431,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
lifeTimer.cancel()
connectTimer.cancel()
- if (socket.encrypted) {
- socket.removeAllListeners()
- socket = null
- }
+ socket.removeAllListeners()
+ socket = null
if (initial)
return reconnect()
@@ -1003,7 +1000,7 @@ function md5(x) {
}
function hmac(key, x) {
- return Buffer.from(new HmacSha256(key).update(x).digest())
+ return crypto.createHmac('sha256', key).update(x).digest()
}
function sha256(x) {
diff --git a/deno/src/index.js b/deno/src/index.js
index 3bbdf2ba..1ca950f8 100644
--- a/deno/src/index.js
+++ b/deno/src/index.js
@@ -1,6 +1,6 @@
-import process from 'https://deno.land/std@0.132.0/node/process.ts'
-import os from 'https://deno.land/std@0.132.0/node/os.ts'
-import fs from 'https://deno.land/std@0.132.0/node/fs.ts'
+import process from 'node:process'
+import os from 'node:os'
+import fs from 'node:fs'
import {
mergeUserTypes,
diff --git a/deno/src/large.js b/deno/src/large.js
index 1b9f42d2..8ae150dd 100644
--- a/deno/src/large.js
+++ b/deno/src/large.js
@@ -1,4 +1,4 @@
-import Stream from 'https://deno.land/std@0.132.0/node/stream.ts'
+import Stream from 'node:stream'
export default function largeObject(sql, oid, mode = 0x00020000 | 0x00040000) {
return new Promise(async(resolve, reject) => {
diff --git a/deno/src/subscribe.js b/deno/src/subscribe.js
index dbb9b971..1ab8b0be 100644
--- a/deno/src/subscribe.js
+++ b/deno/src/subscribe.js
@@ -1,4 +1,4 @@
-import { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'
+import { Buffer } from 'node:buffer'
const noop = () => { /* noop */ }
export default function Subscribe(postgres, options) {
diff --git a/deno/src/types.js b/deno/src/types.js
index ea0da6a2..aa2ead29 100644
--- a/deno/src/types.js
+++ b/deno/src/types.js
@@ -1,4 +1,4 @@
-import { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'
+import { Buffer } from 'node:buffer'
import { Query } from './query.js'
import { Errors } from './errors.js'
diff --git a/deno/tests/bootstrap.js b/deno/tests/bootstrap.js
index da416896..c75ddc40 100644
--- a/deno/tests/bootstrap.js
+++ b/deno/tests/bootstrap.js
@@ -1,4 +1,4 @@
-import { spawn } from 'https://deno.land/std@0.132.0/node/child_process.ts'
+import { spawn } from 'node:child_process'
await exec('dropdb', ['postgres_js_test'])
diff --git a/deno/tests/index.js b/deno/tests/index.js
index 90d1feeb..48cad2bd 100644
--- a/deno/tests/index.js
+++ b/deno/tests/index.js
@@ -1,11 +1,11 @@
-import { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'
-import process from 'https://deno.land/std@0.132.0/node/process.ts'
+import { Buffer } from 'node:buffer'
+import process from 'node:process'
import { exec } from './bootstrap.js'
import { t, nt, ot } from './test.js' // eslint-disable-line
-import { net } from '../polyfills.js'
-import fs from 'https://deno.land/std@0.132.0/node/fs.ts'
-import crypto from 'https://deno.land/std@0.132.0/node/crypto.ts'
+import net from 'node:net'
+import fs from 'node:fs'
+import crypto from 'node:crypto'
import postgres from '../src/index.js'
const delay = ms => new Promise(r => setTimeout(r, ms))
diff --git a/deno/tests/test.js b/deno/tests/test.js
index f61a253f..343eedf3 100644
--- a/deno/tests/test.js
+++ b/deno/tests/test.js
@@ -1,7 +1,7 @@
-import process from 'https://deno.land/std@0.132.0/node/process.ts'
+import process from 'node:process'
/* eslint no-console: 0 */
-import util from 'https://deno.land/std@0.132.0/node/util.ts'
+import util from 'node:util'
let done = 0
let only = false
diff --git a/deno/types/index.d.ts b/deno/types/index.d.ts
index 6f96fe97..79086f75 100644
--- a/deno/types/index.d.ts
+++ b/deno/types/index.d.ts
@@ -1,6 +1,6 @@
-import { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'
-import process from 'https://deno.land/std@0.132.0/node/process.ts'
-import { Readable, Writable } from 'https://deno.land/std@0.132.0/node/stream.ts'
+import { Buffer } from 'node:buffer'
+import process from 'node:process'
+import { Readable, Writable } from 'node:stream'
/**
* Establish a connection to a PostgreSQL server.
diff --git a/transpile.deno.js b/transpile.deno.js
index 923ac9af..63f8f99c 100644
--- a/transpile.deno.js
+++ b/transpile.deno.js
@@ -1,8 +1,7 @@
import fs from 'fs'
import path from 'path'
-const std = 'https://deno.land/std@0.132.0/'
- , empty = x => fs.readdirSync(x).forEach(f => fs.unlinkSync(path.join(x, f)))
+const empty = x => fs.readdirSync(x).forEach(f => fs.unlinkSync(path.join(x, f)))
, ensureEmpty = x => !fs.existsSync(x) ? fs.mkdirSync(x) : empty(x)
, root = 'deno'
, src = path.join(root, 'src')
@@ -59,34 +58,23 @@ function transpile(x, name, folder) {
}
const buffer = x.includes('Buffer')
- ? 'import { Buffer } from \'' + std + 'node/buffer.ts\'\n'
+ ? 'import { Buffer } from \'node:buffer\'\n'
: ''
const process = x.includes('process.')
- ? 'import process from \'' + std + 'node/process.ts\'\n'
+ ? 'import process from \'node:process\'\n'
: ''
const timers = x.includes('setImmediate')
- ? 'import { setImmediate, clearImmediate } from \'../polyfills.js\'\n'
+ ? 'import { setImmediate, clearImmediate } from \'node:timers\'\n'
: ''
- const hmac = x.includes('createHmac')
- ? 'import { HmacSha256 } from \'' + std + 'hash/sha256.ts\'\n'
- : ''
-
- return hmac + buffer + process + timers + x
- .replace(
- 'crypto.createHmac(\'sha256\', key).update(x).digest()',
- 'Buffer.from(new HmacSha256(key).update(x).digest())'
- )
+ return buffer + process + timers + x
.replace(
'query.writable.push({ chunk, callback })',
'(query.writable.push({ chunk }), callback())'
)
.replace('socket.setKeepAlive(true, 1000 * keep_alive)', 'socket.setKeepAlive(true)')
- .replace('node:stream', std + 'node/stream.ts')
- .replace('import net from \'net\'', 'import { net } from \'../polyfills.js\'')
- .replace('import tls from \'tls\'', 'import { tls } from \'../polyfills.js\'')
.replace('import { performance } from \'perf_hooks\'', '')
- .replace(/ from '([a-z_]+)'/g, ' from \'' + std + 'node/$1.ts\'')
+ .replace(/ from '([a-z_]+)'/g, ' from \'node:$1\'')
}
From 421e73ecf2d9d063b17e9f2c115836368ebe2a6b Mon Sep 17 00:00:00 2001
From: Kyiro <54126666+Kyiro@users.noreply.github.com>
Date: Mon, 8 Jan 2024 17:48:53 +0100
Subject: [PATCH 2/2] Fix README
---
deno/README.md | 4 ----
transpile.deno.js | 2 +-
2 files changed, 1 insertion(+), 5 deletions(-)
diff --git a/deno/README.md b/deno/README.md
index 0c792835..0fc569bb 100644
--- a/deno/README.md
+++ b/deno/README.md
@@ -15,10 +15,6 @@
-### Installation
-```bash
-$ npm install postgres
-```
### Usage
Create your `sql` database instance
diff --git a/transpile.deno.js b/transpile.deno.js
index 63f8f99c..d359bdc4 100644
--- a/transpile.deno.js
+++ b/transpile.deno.js
@@ -20,7 +20,7 @@ fs.writeFileSync(
fs.writeFileSync(
path.join(root, 'README.md'),
fs.readFileSync('README.md', 'utf8')
- .replace(/### Installation(\n.*){4}/, '')
+ .replace(/### Installation[\s\S]*?(?=\n##)/m, '')
.replace(
'import postgres from \'postgres\'',
'import postgres from \'https://deno.land/x/postgresjs/mod.js\''