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.

This commit is contained in:
SteveSandersonMS
2016-12-01 14:31:27 +00:00
parent 5e669d6e7a
commit b8913d29dd
4 changed files with 25 additions and 19 deletions

View File

@@ -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": {

View File

@@ -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<any> {
// Resolve relative URLs
if (baseUrl) {
@@ -70,16 +67,6 @@ export function fetch(url: string | Request, init?: RequestInit): Promise<any> {
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';

View File

@@ -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';

View File

@@ -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<any>) {
if (task && domain.active) {
@@ -57,6 +62,20 @@ export function run<T>(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;