From b8913d29dd864f459b4ed0d9c81db3a200bcd21b Mon Sep 17 00:00:00 2001 From: SteveSandersonMS Date: Thu, 1 Dec 2016 14:31:27 +0000 Subject: [PATCH] Update domain-task to export baseUrl from 'main' instead of only 'fetch'. Goal is to avoid Webpack detecting dependency on isomorphic-fetch unless you're actually using it. --- .../npm/domain-task/package.json | 2 +- .../npm/domain-task/src/fetch.ts | 21 ++++--------------- .../npm/domain-task/src/index.ts | 2 +- .../npm/domain-task/src/main.ts | 19 +++++++++++++++++ 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/Microsoft.AspNetCore.SpaServices/npm/domain-task/package.json b/src/Microsoft.AspNetCore.SpaServices/npm/domain-task/package.json index f00b165..a030ef5 100644 --- a/src/Microsoft.AspNetCore.SpaServices/npm/domain-task/package.json +++ b/src/Microsoft.AspNetCore.SpaServices/npm/domain-task/package.json @@ -1,6 +1,6 @@ { "name": "domain-task", - "version": "2.0.1", + "version": "2.0.2", "description": "Tracks outstanding operations for a logical thread of execution", "main": "index.js", "scripts": { diff --git a/src/Microsoft.AspNetCore.SpaServices/npm/domain-task/src/fetch.ts b/src/Microsoft.AspNetCore.SpaServices/npm/domain-task/src/fetch.ts index 3b2c93e..f83dd6d 100644 --- a/src/Microsoft.AspNetCore.SpaServices/npm/domain-task/src/fetch.ts +++ b/src/Microsoft.AspNetCore.SpaServices/npm/domain-task/src/fetch.ts @@ -1,13 +1,10 @@ import * as url from 'url'; import * as domain from 'domain'; import * as domainContext from 'domain-context'; +import { baseUrl } from './main'; const isomorphicFetch = require('isomorphic-fetch'); const isNode = typeof process === 'object' && process.versions && !!process.versions.node; -// Not using a symbol, because this may need to run in a version of Node.js that doesn't support them -const domainTaskStateKey = '__DOMAIN_TASK_INTERNAL_FETCH_BASEURL__DO_NOT_REFERENCE_THIS__'; -let noDomainBaseUrl: string; - function issueRequest(baseUrl: string, req: string | Request, init?: RequestInit): Promise { // Resolve relative URLs if (baseUrl) { @@ -70,16 +67,6 @@ export function fetch(url: string | Request, init?: RequestInit): Promise { return issueRequest(baseUrl(), url, init); } -export function baseUrl(url?: string): string { - if (url) { - if (domain.active) { - // There's an active domain (e.g., in Node.js), so associate the base URL with it - domainContext.set(domainTaskStateKey, url); - } else { - // There's no active domain (e.g., in browser), so there's just one shared base URL - noDomainBaseUrl = url; - } - } - - return domain.active ? domainContext.get(domainTaskStateKey) : noDomainBaseUrl; -} +// Re-exporting baseUrl from this module for back-compatibility only +// Newer code that wants to access baseUrl should use the version exported from the root of this package +export { baseUrl } from './main'; diff --git a/src/Microsoft.AspNetCore.SpaServices/npm/domain-task/src/index.ts b/src/Microsoft.AspNetCore.SpaServices/npm/domain-task/src/index.ts index 78f0c17..b6cb2ab 100644 --- a/src/Microsoft.AspNetCore.SpaServices/npm/domain-task/src/index.ts +++ b/src/Microsoft.AspNetCore.SpaServices/npm/domain-task/src/index.ts @@ -1,3 +1,3 @@ // This file determines the top-level package exports -export { addTask, run } from './main'; +export { addTask, run, baseUrl } from './main'; export { fetch } from './fetch'; diff --git a/src/Microsoft.AspNetCore.SpaServices/npm/domain-task/src/main.ts b/src/Microsoft.AspNetCore.SpaServices/npm/domain-task/src/main.ts index ee74d73..3dfc652 100644 --- a/src/Microsoft.AspNetCore.SpaServices/npm/domain-task/src/main.ts +++ b/src/Microsoft.AspNetCore.SpaServices/npm/domain-task/src/main.ts @@ -1,6 +1,11 @@ import * as domain from 'domain'; import * as domainContext from 'domain-context'; + +// Not using symbols, because this may need to run in a version of Node.js that doesn't support them const domainTasksStateKey = '__DOMAIN_TASKS'; +const domainTaskBaseUrlStateKey = '__DOMAIN_TASK_INTERNAL_FETCH_BASEURL__DO_NOT_REFERENCE_THIS__'; + +let noDomainBaseUrl: string; export function addTask(task: PromiseLike) { if (task && domain.active) { @@ -57,6 +62,20 @@ export function run(codeToRun: () => T, completionCallback: (error: any) => v return synchronousResult; } +export function baseUrl(url?: string): string { + if (url) { + if (domain.active) { + // There's an active domain (e.g., in Node.js), so associate the base URL with it + domainContext.set(domainTaskBaseUrlStateKey, url); + } else { + // There's no active domain (e.g., in browser), so there's just one shared base URL + noDomainBaseUrl = url; + } + } + + return domain.active ? domainContext.get(domainTaskBaseUrlStateKey) : noDomainBaseUrl; +} + interface DomainTasksState { numRemainingTasks: number; hasIssuedSuccessCallback: boolean;