Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/lnd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { toPositiveNumber } from '@/lib/format'
import { authenticatedLndGrpc } from '@/lib/lnd'
import { getIdentity, getHeight, getWalletInfo, getNode, getPayment, parsePaymentRequest } from 'ln-service'
import { datePivot } from '@/lib/time'
import { LND_PATHFINDING_TIMEOUT_MS } from '@/lib/constants'
import { LND_PATHFINDING_TIMEOUT_MS, __DEV__ } from '@/lib/constants'

const lnd = global.lnd || authenticatedLndGrpc({
cert: process.env.LND_CERT,
macaroon: process.env.LND_MACAROON,
socket: process.env.LND_SOCKET
}).lnd

if (process.env.NODE_ENV === 'development') global.lnd = lnd
if (__DEV__) global.lnd = lnd

// Check LND GRPC connection
getWalletInfo({ lnd }, (err, result) => {
Expand Down
3 changes: 2 additions & 1 deletion api/models/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import createPrisma from '@/lib/create-prisma'
import { __DEV__ } from '@/lib/constants'

const prisma = global.prisma || (() => {
console.log('initing prisma')
Expand All @@ -9,6 +10,6 @@ const prisma = global.prisma || (() => {
})
})()

if (process.env.NODE_ENV === 'development') global.prisma = prisma
if (__DEV__) global.prisma = prisma

export default prisma
8 changes: 4 additions & 4 deletions api/s3/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AWS from 'aws-sdk'
import { MEDIA_URL } from '@/lib/constants'
import { __DEV__, MEDIA_URL } from '@/lib/constants'

const bucketRegion = 'us-east-1'
const Bucket = process.env.NEXT_PUBLIC_AWS_UPLOAD_BUCKET
Expand All @@ -10,13 +10,13 @@ AWS.config.update({

const config = {
apiVersion: '2006-03-01',
s3ForcePathStyle: process.env.NODE_ENV === 'development'
s3ForcePathStyle: __DEV__
}

export function createPresignedPost ({ key, type, size }) {
// for local development, we use the NEXT_PUBLIC_MEDIA_URL which
// is reachable from the host machine
if (process.env.NODE_ENV === 'development') {
if (__DEV__) {
config.endpoint = process.env.NEXT_PUBLIC_MEDIA_URL
}

Expand All @@ -39,7 +39,7 @@ export function createPresignedPost ({ key, type, size }) {
export async function deleteObjects (keys) {
// for local development, we use the MEDIA_URL which
// is reachable from the container network
if (process.env.NODE_ENV === 'development') {
if (__DEV__) {
config.endpoint = MEDIA_URL
}

Expand Down
3 changes: 2 additions & 1 deletion capture/media-check.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { filetypemime } from 'magic-bytes.js'
import { __DEV__ } from '@/lib/constants'

const TIMEOUT_HEAD = 2000
const TIMEOUT_GET = 10000
Expand Down Expand Up @@ -81,7 +82,7 @@ export default async function mediaCheck (req, res) {
try {
// in development, the capture container can't reach the public media url,
// so we need to replace it with its docker equivalent, e.g. http://s3:4566/uploads
if (url.startsWith(process.env.NEXT_PUBLIC_MEDIA_URL) && process.env.NODE_ENV === 'development') {
if (url.startsWith(process.env.NEXT_PUBLIC_MEDIA_URL) && __DEV__) {
url = url.replace(process.env.NEXT_PUBLIC_MEDIA_URL, process.env.MEDIA_URL_DOCKER)
}

Expand Down
4 changes: 2 additions & 2 deletions lib/apollo.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApolloClient, InMemoryCache, HttpLink, makeVar, split, from } from '@apollo/client'
import { BatchHttpLink } from '@apollo/client/link/batch-http'
import { decodeCursor, LIMIT } from './cursor'
import { COMMENTS_LIMIT, SSR } from './constants'
import { COMMENTS_LIMIT, SSR, __DEV__ } from './constants'
import { RetryLink } from '@apollo/client/link/retry'
import { isMutationOperation, isQueryOperation } from '@apollo/client/utilities'

Expand Down Expand Up @@ -59,7 +59,7 @@ function getClient (uri) {
return new ApolloClient({
link,
ssrMode: SSR,
connectToDevTools: process.env.NODE_ENV !== 'production',
connectToDevTools: __DEV__,
cache: new InMemoryCache({
freezeResults: true,
// https://github.com/apollographql/apollo-client/issues/7648
Expand Down
3 changes: 2 additions & 1 deletion lib/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { datePivot } from '@/lib/time'
import * as cookie from 'cookie'
import { NodeNextRequest } from 'next/dist/server/base-http/node'
import { encode as encodeJWT, decode as decodeJWT } from 'next-auth/jwt'
import { __PROD__ } from '@/lib/constants'

const b64Encode = obj => Buffer.from(JSON.stringify(obj)).toString('base64')
const b64Decode = s => JSON.parse(Buffer.from(s, 'base64'))

export const HTTPS = process.env.NODE_ENV === 'production'
export const HTTPS = __PROD__

const secureCookie = (name) =>
HTTPS
Expand Down
4 changes: 2 additions & 2 deletions lib/cln.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import crypto from 'crypto'
import { getAgent } from '@/lib/proxy'
import { assertContentTypeJson, assertResponseOk } from './url'
import { fetchWithTimeout, FetchTimeoutError } from './fetch'
import { WALLET_CREATE_INVOICE_TIMEOUT_MS } from './constants'
import { WALLET_CREATE_INVOICE_TIMEOUT_MS, __DEV__ } from './constants'

export const createInvoice = async ({ msats, description, expiry }, { socket, rune, cert }, { signal }) => {
const agent = getAgent({ hostname: socket, cert })
Expand Down Expand Up @@ -54,7 +54,7 @@ export const sendPayment = async (bolt11, { socket, rune }, { signal }) => {
// https://docs.corelightning.org/reference/pay
const url = new URL(
'/v1/pay',
process.env.NODE_ENV === 'development' ? `http://${socket}` : `https://${socket}`)
__DEV__ ? `http://${socket}` : `https://${socket}`)
const method = 'POST'
const res = await fetchWithTimeout(url, {
method,
Expand Down
6 changes: 6 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,9 @@ export const BECH32_CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l'

export const HALLOWEEN_IMMUNITY_HOURS = 8
export const HALLOWEEN_INACTIVITY_TIMEOUT_HOURS = 36

/** true if `process.env.NODE_ENV` is set to `development` */
export const __DEV__ = process.env.NODE_ENV === 'development'

/** true if `process.env.NODE_ENV` is set to `production` */
export const __PROD__ = process.env.NODE_ENV === 'production'
Comment on lines +221 to +225
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using /** ... */ enables intellisense:

2025-11-03-163918_1920x1080_scrot

4 changes: 2 additions & 2 deletions lib/lnurl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createHash } from 'crypto'
import { bech32 } from 'bech32'
import { lnAddrSchema } from './validate'
import { FetchTimeoutError } from '@/lib/fetch'
import { WALLET_CREATE_INVOICE_TIMEOUT_MS } from './constants'
import { WALLET_CREATE_INVOICE_TIMEOUT_MS, __DEV__ } from './constants'
import { assertContentTypeJson, assertResponseOk, ResponseAssertError } from '@/lib/url'

export function encodeLNUrl (url) {
Expand Down Expand Up @@ -32,7 +32,7 @@ export async function lnAddrOptions (addr, { signal } = {}) {
await lnAddrSchema().fields.addr.validate(addr)
const [name, domain] = addr.split('@')
let protocol = 'https'
if (process.env.NODE_ENV === 'development') {
if (__DEV__) {
// support HTTP and HTTPS during development
protocol = process.env.NEXT_PUBLIC_URL.split('://')[0]
}
Expand Down
3 changes: 2 additions & 1 deletion lib/proxy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import http from 'http'
import https from 'https'
import { TOR_REGEXP } from '@/lib/url'
import { __DEV__ } from '@/lib/constants'

// from https://github.com/delvedor/hpagent

Expand Down Expand Up @@ -137,7 +138,7 @@ export function getAgent ({ hostname, cert }) {
return agent
}

if (process.env.NODE_ENV === 'development' && !cert) {
if (__DEV__ && !cert) {
return new http.Agent()
}

Expand Down
4 changes: 3 additions & 1 deletion lib/sndev.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { __PROD__ } from '@/lib/constants'

export function isServiceEnabled (service) {
if (process.env.NODE_ENV !== 'development') return true
if (__PROD__) return true

const services = (process.env.COMPOSE_PROFILES ?? '').split(',')
return services.includes(service)
Expand Down
5 changes: 3 additions & 2 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
MIN_POLL_NUM_CHOICES, MAX_FORWARDS, BOOST_MULT, MAX_TERRITORY_DESC_LENGTH, POST_TYPES,
TERRITORY_BILLING_TYPES, MAX_COMMENT_TEXT_LENGTH, MAX_POST_TEXT_LENGTH, MIN_TITLE_LENGTH, BOUNTY_MIN, BOUNTY_MAX,
RESERVED_SUB_NAMES,
BOOST_MAX
BOOST_MAX,
__DEV__
} from './constants'
import { SUPPORTED_CURRENCIES } from './currency'
import { NOSTR_MAX_RELAY_NUM, NOSTR_PUBKEY_BECH32, NOSTR_PUBKEY_HEX } from './nostr'
Expand Down Expand Up @@ -45,7 +46,7 @@ const nameValidator = string()
const intValidator = number().typeError('must be a number').integer('must be whole')
const floatValidator = number().typeError('must be a number')

export const lightningAddressValidator = process.env.NODE_ENV === 'development'
export const lightningAddressValidator = __DEV__
? string().or(
[string().matches(/^[\w_]+@localhost:\d+$/), string().matches(/^[\w_]+@app:\d+$/), string().email()],
'address is no good')
Expand Down
4 changes: 2 additions & 2 deletions lib/webPush.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import webPush from 'web-push'
import removeMd from 'remove-markdown'
import { COMMENT_DEPTH_LIMIT, FOUND_BLURBS, LOST_BLURBS } from './constants'
import { COMMENT_DEPTH_LIMIT, FOUND_BLURBS, LOST_BLURBS, __PROD__ } from './constants'
import { msatsToSats, numWithUnits } from './format'
import models from '@/api/models'
import { isMuted } from '@/lib/user'
import { Prisma } from '@prisma/client'

const webPushEnabled = process.env.NODE_ENV === 'production' ||
const webPushEnabled = __PROD__ ||
(process.env.VAPID_MAILTO && process.env.NEXT_PUBLIC_VAPID_PUBKEY && process.env.VAPID_PRIVKEY)

function log (...args) {
Expand Down
9 changes: 4 additions & 5 deletions middleware.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NextResponse, URLPattern } from 'next/server'
import { __DEV__ } from '@/lib/constants'

const referrerPattern = new URLPattern({ pathname: ':pathname(*)/r/:referrer([\\w_]+)' })
const itemPattern = new URLPattern({ pathname: '/items/:id(\\d+){/:other(\\w+)}?' })
Expand Down Expand Up @@ -87,14 +88,12 @@ function referrerMiddleware (request) {
export function middleware (request) {
const resp = referrerMiddleware(request)

const isDev = process.env.NODE_ENV === 'development'

const nonce = Buffer.from(crypto.randomUUID()).toString('base64')
// we want to load media from other localhost ports during development
const devSrc = isDev ? ' localhost:* http: ws:' : ''
const devSrc = __DEV__ ? ' localhost:* http: ws:' : ''
// unsafe-eval is required during development due to react-refresh.js
// see https://github.com/vercel/next.js/issues/14221
const devScriptSrc = isDev ? " 'unsafe-eval'" : ''
const devScriptSrc = __DEV__ ? " 'unsafe-eval'" : ''

const cspHeader = [
// if something is not explicitly allowed, we don't allow it.
Expand All @@ -117,7 +116,7 @@ export function middleware (request) {
// blocks injection of <base> tags
"base-uri 'none'",
// tell user agents to replace HTTP with HTTPS
isDev ? '' : 'upgrade-insecure-requests',
__DEV__ ? '' : 'upgrade-insecure-requests',
// prevents any domain from framing the content (defense against clickjacking attacks)
"frame-ancestors 'none'"
].join('; ')
Expand Down
12 changes: 6 additions & 6 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { InjectManifest } = require('workbox-webpack-plugin')
const CopyPlugin = require('copy-webpack-plugin')
const webpack = require('webpack')

let isProd = process.env.NODE_ENV === 'production'
let __PROD__ = process.env.NODE_ENV === 'production'
const corsHeaders = [
{
key: 'Access-Control-Allow-Origin',
Expand All @@ -29,14 +29,14 @@ const getGitCommit = (env) => {

let commitHash
try {
if (isProd) {
if (__PROD__) {
try {
commitHash = getGitCommit('aws')
} catch (e) {
// maybe we're running prod build locally
commitHash = getGitCommit()
// if above line worked, we're running locally and should not use prod config which configurates CDN
isProd = false
__PROD__ = false
}
} else {
commitHash = getGitCommit()
Expand All @@ -50,7 +50,7 @@ module.exports = withPlausibleProxy()({
env: {
NEXT_PUBLIC_COMMIT_HASH: commitHash,
NEXT_PUBLIC_LND_CONNECT_ADDRESS: process.env.LND_CONNECT_ADDRESS,
NEXT_PUBLIC_ASSET_PREFIX: isProd ? 'https://a.stacker.news' : '',
NEXT_PUBLIC_ASSET_PREFIX: __PROD__ ? 'https://a.stacker.news' : '',
// in prod, we build in /var/app/staging and then cp and deploy in /var/app/current
// so we need to resolve the relative path to the lightning module
LIGHTNING_MODULE_PATH: require('path').relative(process.cwd(), require.resolve('lightning'))
Expand All @@ -64,8 +64,8 @@ module.exports = withPlausibleProxy()({
productionBrowserSourceMaps: true,
generateBuildId: commitHash ? async () => commitHash : undefined,
// Use the CDN in production and localhost for development.
assetPrefix: isProd ? 'https://a.stacker.news' : undefined,
crossOrigin: isProd ? 'anonymous' : undefined,
assetPrefix: __PROD__ ? 'https://a.stacker.news' : undefined,
crossOrigin: __PROD__ ? 'anonymous' : undefined,
async headers () {
return [
{
Expand Down
4 changes: 2 additions & 2 deletions pages/api/lnurlp/[username]/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getPublicKey } from 'nostr'
import models from '@/api/models'
import { lnurlPayMetadataString } from '@/lib/lnurl'
import { LNURLP_COMMENT_MAX_LENGTH, PROXY_RECEIVE_FEE_PERCENT } from '@/lib/constants'
import { LNURLP_COMMENT_MAX_LENGTH, PROXY_RECEIVE_FEE_PERCENT, __DEV__ } from '@/lib/constants'

export default async ({ query: { username } }, res) => {
const user = await models.user.findUnique({ where: { name: username } })
Expand All @@ -14,7 +14,7 @@ export default async ({ query: { username } }, res) => {
minSendable += minSendable * PROXY_RECEIVE_FEE_PERCENT / 100n
}

const url = process.env.NODE_ENV === 'development' ? process.env.SELF_URL : process.env.NEXT_PUBLIC_URL
const url = __DEV__ ? process.env.SELF_URL : process.env.NEXT_PUBLIC_URL
return res.status(200).json({
callback: `${url}/api/lnurlp/${username}/pay`, // The URL from LN SERVICE which will accept the pay request parameters
minSendable: Number(minSendable), // Min amount LN SERVICE is willing to receive, can not be less than 1 or more than `maxSendable`
Expand Down
3 changes: 2 additions & 1 deletion scripts/imgproxy.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const { __PROD__ } = require('@/lib/constants')
const { PrismaClient, Prisma } = require('@prisma/client')

const prisma = new PrismaClient()

const imgProxyEnabled = process.env.NODE_ENV === 'production' ||
const imgProxyEnabled = __PROD__ ||
(process.env.NEXT_PUBLIC_IMGPROXY_URL && process.env.IMGPROXY_SALT && process.env.IMGPROXY_KEY)

if (!imgProxyEnabled) {
Expand Down
3 changes: 2 additions & 1 deletion wallets/lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { TOR_REGEXP } from '@/lib/url'
import { lightningAddressValidator } from '@/lib/validate'
import { decodeBech32 as clinkDecodeBech32, OfferPriceType } from '@shocknet/clink-sdk'
import { string, array } from 'yup'
import { __DEV__ } from '@/lib/constants'

export const externalLightningAddressValidator = lightningAddressValidator
.test({
Expand Down Expand Up @@ -168,7 +169,7 @@ export const bip39Validator = ({ min = 12, max = 24 } = {}) =>
export const certValidator = () => string().hexOrBase64()

export const urlValidator = (...args) =>
process.env.NODE_ENV === 'development'
__DEV__
? string()
.or([
string().matches(/^(http:\/\/)?localhost:\d+$/),
Expand Down
4 changes: 2 additions & 2 deletions wallets/server/protocols/lnbits.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WALLET_CREATE_INVOICE_TIMEOUT_MS } from '@/lib/constants'
import { __DEV__, WALLET_CREATE_INVOICE_TIMEOUT_MS } from '@/lib/constants'
import { FetchTimeoutError } from '@/lib/fetch'
import { msatsToSats } from '@/lib/format'
import { getAgent } from '@/lib/proxy'
Expand Down Expand Up @@ -32,7 +32,7 @@ export async function createInvoice (
let hostname = url.replace(/^https?:\/\//, '').replace(/\/+$/, '')
const agent = getAgent({ hostname })

if (process.env.NODE_ENV !== 'production' && hostname.startsWith('localhost:')) {
if (__DEV__ && hostname.startsWith('localhost:')) {
// to make it possible to attach LNbits for receives during local dev
hostname = hostname === `localhost:${process.env.LNBITS_WEB_PORT}` ? 'lnbits:5000' : 'lnbits-v1:5000'
}
Expand Down
5 changes: 3 additions & 2 deletions worker/imgproxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { isJob } from '@/lib/item'
import path from 'node:path'
import { decodeProxyUrl } from '@/lib/url'
import { fetchWithTimeout } from '@/lib/fetch'
import { __PROD__ } from '@/lib/constants'

const imgProxyEnabled = process.env.NODE_ENV === 'production' ||
const imgProxyEnabled = __PROD__ ||
(process.env.NEXT_PUBLIC_IMGPROXY_URL && process.env.IMGPROXY_SALT && process.env.IMGPROXY_KEY)

if (!imgProxyEnabled) {
Expand All @@ -32,7 +33,7 @@ const imageUrlMatchers = [
u => u.host === 'i.imgur.com'
]
const exclude = [
u => process.env.NODE_ENV === 'production' && u.protocol !== 'https:',
u => __PROD__ && u.protocol !== 'https:',
u => u.host.endsWith('.onion') || u.host.endsWith('.b32.ip') || u.host.endsWith('.loki'),
u => ['twitter.com', 'x.com', 'nitter.it', 'nitter.at', 'xcancel.com'].some(h => h === u.host),
u => u.host === 'stacker.news',
Expand Down
3 changes: 3 additions & 0 deletions worker/loadenv.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
import { loadEnvConfig } from '@next/env'

// this should probably not import __DEV__ from @/lib/constants
// because we are initializing the environment variables for the worker here
loadEnvConfig('.', process.env.NODE_ENV === 'development')
Loading