forked from nodejs/undici
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fetch): allow setting base urls (nodejs#1631)
* feat(fetch): allow setting base url * add files :| * fix: set origin globally * fix: add docs
- Loading branch information
1 parent
0d53e5a
commit 1b32356
Showing
8 changed files
with
202 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict' | ||
|
||
// In case of breaking changes, increase the version | ||
// number to avoid conflicts. | ||
const globalOrigin = Symbol.for('undici.globalOrigin.1') | ||
|
||
function getGlobalOrigin () { | ||
return globalThis[globalOrigin] | ||
} | ||
|
||
function setGlobalOrigin (newOrigin) { | ||
if ( | ||
newOrigin !== undefined && | ||
typeof newOrigin !== 'string' && | ||
!(newOrigin instanceof URL) | ||
) { | ||
throw new Error('Invalid base url') | ||
} | ||
|
||
if (newOrigin === undefined) { | ||
Object.defineProperty(globalThis, globalOrigin, { | ||
value: undefined, | ||
writable: true, | ||
enumerable: false, | ||
configurable: false | ||
}) | ||
|
||
return | ||
} | ||
|
||
const parsedURL = new URL(newOrigin) | ||
|
||
if (parsedURL.protocol !== 'http:' && parsedURL.protocol !== 'https:') { | ||
throw new TypeError(`Only http & https urls are allowed, received ${parsedURL.protocol}`) | ||
} | ||
|
||
Object.defineProperty(globalThis, globalOrigin, { | ||
value: parsedURL, | ||
writable: true, | ||
enumerable: false, | ||
configurable: false | ||
}) | ||
} | ||
|
||
module.exports = { | ||
getGlobalOrigin, | ||
setGlobalOrigin | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
'use strict' | ||
|
||
const { test, afterEach } = require('tap') | ||
const { createServer } = require('http') | ||
const { once } = require('events') | ||
const { | ||
getGlobalOrigin, | ||
setGlobalOrigin, | ||
Response, | ||
Request, | ||
fetch | ||
} = require('../..') | ||
|
||
afterEach(() => setGlobalOrigin(undefined)) | ||
|
||
test('setGlobalOrigin & getGlobalOrigin', (t) => { | ||
t.equal(getGlobalOrigin(), undefined) | ||
|
||
setGlobalOrigin('http://localhost:3000') | ||
t.same(getGlobalOrigin(), new URL('http://localhost:3000')) | ||
|
||
setGlobalOrigin(undefined) | ||
t.equal(getGlobalOrigin(), undefined) | ||
|
||
setGlobalOrigin(new URL('http://localhost:3000')) | ||
t.same(getGlobalOrigin(), new URL('http://localhost:3000')) | ||
|
||
t.throws(() => { | ||
setGlobalOrigin('invalid.url') | ||
}, TypeError) | ||
|
||
t.throws(() => { | ||
setGlobalOrigin('wss://invalid.protocol') | ||
}, TypeError) | ||
|
||
t.throws(() => setGlobalOrigin(true)) | ||
|
||
t.end() | ||
}) | ||
|
||
test('Response.redirect', (t) => { | ||
t.throws(() => { | ||
Response.redirect('/relative/path', 302) | ||
}, TypeError('Failed to parse URL from /relative/path')) | ||
|
||
t.doesNotThrow(() => { | ||
setGlobalOrigin('http://localhost:3000') | ||
Response.redirect('/relative/path', 302) | ||
}) | ||
|
||
setGlobalOrigin('http://localhost:3000') | ||
const response = Response.redirect('/relative/path', 302) | ||
// See step #7 of https://fetch.spec.whatwg.org/#dom-response-redirect | ||
t.equal(response.headers.get('location'), 'http://localhost:3000/relative/path') | ||
|
||
t.end() | ||
}) | ||
|
||
test('new Request', (t) => { | ||
t.throws( | ||
() => new Request('/relative/path'), | ||
TypeError('Failed to parse URL from /relative/path') | ||
) | ||
|
||
t.doesNotThrow(() => { | ||
setGlobalOrigin('http://localhost:3000') | ||
// eslint-disable-next-line no-new | ||
new Request('/relative/path') | ||
}) | ||
|
||
setGlobalOrigin('http://localhost:3000') | ||
const request = new Request('/relative/path') | ||
t.equal(request.url, 'http://localhost:3000/relative/path') | ||
|
||
t.end() | ||
}) | ||
|
||
test('fetch', async (t) => { | ||
await t.rejects(async () => { | ||
await fetch('/relative/path') | ||
}, TypeError('Failed to parse URL from /relative/path')) | ||
|
||
t.test('Basic fetch', async (t) => { | ||
const server = createServer((req, res) => { | ||
t.equal(req.url, '/relative/path') | ||
res.end() | ||
}).listen(0) | ||
|
||
setGlobalOrigin(`http://localhost:${server.address().port}`) | ||
t.teardown(server.close.bind(server)) | ||
await once(server, 'listening') | ||
|
||
await t.resolves(fetch('/relative/path')) | ||
}) | ||
|
||
t.test('fetch return', async (t) => { | ||
const server = createServer((req, res) => { | ||
t.equal(req.url, '/relative/path') | ||
res.end() | ||
}).listen(0) | ||
|
||
setGlobalOrigin(`http://localhost:${server.address().port}`) | ||
t.teardown(server.close.bind(server)) | ||
await once(server, 'listening') | ||
|
||
const response = await fetch('/relative/path') | ||
|
||
t.equal(response.url, `http://localhost:${server.address().port}/relative/path`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export { | ||
setGlobalOrigin, | ||
getGlobalOrigin | ||
} | ||
|
||
declare function setGlobalOrigin(origin: string | URL | undefined): void; | ||
declare function getGlobalOrigin(): URL | undefined; |