source: node_modules/pg/lib/stream.js@ 62b2964

finki-main main
Last change on this file since 62b2964 was 62b2964, checked in by Klimentina Efremova <klimentina08642@…>, 2 weeks ago

Project Handcraft Marketplace

  • Property mode set to 100644
File size: 2.1 KB
RevLine 
[62b2964]1const { getStream, getSecureStream } = getStreamFuncs()
2
3module.exports = {
4 /**
5 * Get a socket stream compatible with the current runtime environment.
6 * @returns {Duplex}
7 */
8 getStream,
9 /**
10 * Get a TLS secured socket, compatible with the current environment,
11 * using the socket and other settings given in `options`.
12 * @returns {Duplex}
13 */
14 getSecureStream,
15}
16
17/**
18 * The stream functions that work in Node.js
19 */
20function getNodejsStreamFuncs() {
21 function getStream(ssl) {
22 const net = require('net')
23 return new net.Socket()
24 }
25
26 function getSecureStream(options) {
27 const tls = require('tls')
28 return tls.connect(options)
29 }
30 return {
31 getStream,
32 getSecureStream,
33 }
34}
35
36/**
37 * The stream functions that work in Cloudflare Workers
38 */
39function getCloudflareStreamFuncs() {
40 function getStream(ssl) {
41 const { CloudflareSocket } = require('pg-cloudflare')
42 return new CloudflareSocket(ssl)
43 }
44
45 function getSecureStream(options) {
46 options.socket.startTls(options)
47 return options.socket
48 }
49 return {
50 getStream,
51 getSecureStream,
52 }
53}
54
55/**
56 * Are we running in a Cloudflare Worker?
57 *
58 * @returns true if the code is currently running inside a Cloudflare Worker.
59 */
60function isCloudflareRuntime() {
61 // Since 2022-03-21 the `global_navigator` compatibility flag is on for Cloudflare Workers
62 // which means that `navigator.userAgent` will be defined.
63 // eslint-disable-next-line no-undef
64 if (typeof navigator === 'object' && navigator !== null && typeof navigator.userAgent === 'string') {
65 // eslint-disable-next-line no-undef
66 return navigator.userAgent === 'Cloudflare-Workers'
67 }
68 // In case `navigator` or `navigator.userAgent` is not defined then try a more sneaky approach
69 if (typeof Response === 'function') {
70 const resp = new Response(null, { cf: { thing: true } })
71 if (typeof resp.cf === 'object' && resp.cf !== null && resp.cf.thing) {
72 return true
73 }
74 }
75 return false
76}
77
78function getStreamFuncs() {
79 if (isCloudflareRuntime()) {
80 return getCloudflareStreamFuncs()
81 }
82 return getNodejsStreamFuncs()
83}
Note: See TracBrowser for help on using the repository browser.