diff --git a/README.md b/README.md index 2a333eb..5038030 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,12 @@ const { protocol, host } = absoluteUrl(req, 'localhost:8004') const apiURL = `${protocol}//${host}/api/job.js` ``` +```js +import absoluteUrl from 'next-absolute-url' +const { protocol, host } = absoluteUrl(req, '', { https: true }) // protocol will be https +const apiURL = `${protocol}//${host}/api/job.js` +``` + or if you just want the full URL origin: ```js diff --git a/index.d.ts b/index.d.ts index dfd5c08..6cb781b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,6 +1,8 @@ /// import { IncomingMessage } from 'http'; -declare function absoluteUrl(req?: IncomingMessage, localhostAddress?: string): { +declare function absoluteUrl(req?: IncomingMessage, localhostAddress?: string, options?: { + https: boolean; +}): { protocol: string; host: string; origin: string; diff --git a/index.js b/index.js index c9fb3a8..f0dad08 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,27 @@ 'use strict' exports.__esModule = true -function absoluteUrl(req, localhostAddress) { +function absoluteUrl(req, localhostAddress, options) { if (localhostAddress === void 0) { localhostAddress = 'localhost:3000' } + if (options === void 0) { + options = { + https: false, + } + } var _a + var https = options.https var host = (((_a = req) === null || _a === void 0 ? void 0 : _a.headers) ? req.headers.host : window.location.host) || localhostAddress - var protocol = /^localhost(:\d+)?$/.test(host) ? 'http:' : 'https:' + var protocol = https + ? 'https:' // if NODE_ENV is production + : process.env.NODE_ENV == 'production' + ? 'https:' // if Customised Prameter Passed + : 'http:' if ( req && req.headers['x-forwarded-host'] && diff --git a/index.test.ts b/index.test.ts index 80b28b0..98ff4c7 100644 --- a/index.test.ts +++ b/index.test.ts @@ -42,14 +42,55 @@ describe('host is corrupted (is empty somehow)', () => { expect(protocol).toBe('http:') expect(host).toBe('localhost:10000') }) + test('protocol should be http for loopback', () => { + const req = { + headers: { + host: '127.0.0.1:3000', + }, + } as IncomingMessage + const { protocol, host, origin } = nextAbsoluteUrl(req) + expect(origin).toBe('http://127.0.0.1:3000') + expect(protocol).toBe('http:') + expect(host).toBe('127.0.0.1:3000') + }) + test('protocol should be http for ip', () => { + const req = { + headers: { + host: '192.168.0.1:3000', + }, + } as IncomingMessage + const { protocol, host, origin } = nextAbsoluteUrl(req) + expect(origin).toBe('http://192.168.0.1:3000') + expect(protocol).toBe('http:') + expect(host).toBe('192.168.0.1:3000') + }) + test('protocol should be https for ip , if user passed customised parameter https:true', () => { + const req = { + headers: { + host: '192.168.0.1:3000', + }, + } as IncomingMessage + const { protocol, host, origin } = nextAbsoluteUrl(req, '', { https: true }) + expect(origin).toBe('https://192.168.0.1:3000') + expect(protocol).toBe('https:') + expect(host).toBe('192.168.0.1:3000') + }) }) describe('host is remote', () => { + const OLD_ENV = process.env + beforeAll(() => { window.location.host = 'example.com' + jest.resetModules() // most important - it clears the cache + process.env = { ...OLD_ENV } // make a copy + }) + afterAll(() => { + process.env = OLD_ENV // restore old env }) test('no values', () => { + process.env.NODE_ENV = 'production' const { protocol, host, origin } = nextAbsoluteUrl() expect(origin).toBe('https://example.com') expect(protocol).toBe('https:') @@ -89,6 +130,29 @@ describe('host is remote', () => { expect(protocol).toBe('https:') expect(host).toBe('example.com') }) + test('protocol should be https for ip on production', () => { + const req = { + headers: { + host: '44.45.46.55', + }, + } as IncomingMessage + const { protocol, host, origin } = nextAbsoluteUrl(req) + expect(origin).toBe('https://44.45.46.55') + expect(protocol).toBe('https:') + expect(host).toBe('44.45.46.55') + }) + test('pass custom parameter for https, if NODE_ENV not passed Correctly', () => { + process.env.NODE_ENV = 'some wrong NODE_ENV' + const req = { + headers: { + host: '44.45.46.55', + }, + } as IncomingMessage + const { protocol, host, origin } = nextAbsoluteUrl(req, '', { https: true }) + expect(origin).toBe('https://44.45.46.55') + expect(protocol).toBe('https:') + expect(host).toBe('44.45.46.55') + }) }) describe('host is localhost', () => { @@ -139,7 +203,6 @@ describe('host is localhost', () => { }) }) - describe('behind a proxy', () => { test('should use the x-forwarded headers', () => { const req = { @@ -158,5 +221,4 @@ describe('behind a proxy', () => { expect(protocol).toBe('http:') expect(host).toBe('localhost:5000') }) - }) diff --git a/index.ts b/index.ts index 31cf5cd..69fff4c 100644 --- a/index.ts +++ b/index.ts @@ -2,11 +2,19 @@ import { IncomingMessage } from 'http' function absoluteUrl( req?: IncomingMessage, - localhostAddress = 'localhost:3000' + localhostAddress = 'localhost:3000', + options = { + https: false, + } ) { + let { https } = options let host = (req?.headers ? req.headers.host : window.location.host) || localhostAddress - let protocol = /^localhost(:\d+)?$/.test(host) ? 'http:' : 'https:' + let protocol = https + ? 'https:' // if NODE_ENV is production + : process.env.NODE_ENV == 'production' + ? 'https:' // if Customised Prameter Passed + : 'http:' if ( req && diff --git a/package.json b/package.json index debb013..5b58d6e 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "scripts": { "build": "tsc --declaration true index.ts && prettier --write index.ts index.js", "postbuild": "git add index.js index.d.ts", - "test": "jest" + "test": "jest", + "test-watch": "jest --watch" }, "git": { "pre-commit": [