Skip to content

fixed some error and added one new feature #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// <reference types="node" />
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;
Expand Down
14 changes: 12 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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'] &&
Expand Down
66 changes: 64 additions & 2 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:')
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -139,7 +203,6 @@ describe('host is localhost', () => {
})
})


describe('behind a proxy', () => {
test('should use the x-forwarded headers', () => {
const req = {
Expand All @@ -158,5 +221,4 @@ describe('behind a proxy', () => {
expect(protocol).toBe('http:')
expect(host).toBe('localhost:5000')
})

})
12 changes: 10 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Owner

@jakeburden jakeburden Sep 16, 2020

Choose a reason for hiding this comment

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

Question:

This seems to lock the protocol to https if process.env.NODE_ENV == 'production'.

In most cases I think this will be fine, but what if someone is testing their production build locally, over http?

I'm not sure if we should be changing things based on environment variables here. Thoughts?

? 'https:' // if Customised Prameter Passed
: 'http:'

if (
req &&
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down