@composite-fetcher/core
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -11,8 +11,6 @@ // src/interfaces/index.ts | ||
plugins = []; | ||
requestIdCounter = 0; | ||
modifiedRequest; | ||
modifiedResponse; | ||
processedHooks = { | ||
preRequest: /* @__PURE__ */ new Set(), | ||
postRequest: /* @__PURE__ */ new Set() | ||
}; | ||
processedHooks = /* @__PURE__ */ new Map(); | ||
addPlugins(plugin) { | ||
@@ -25,4 +23,8 @@ if (Array.isArray(plugin)) { | ||
} | ||
hasBeenProcessed(plugin, hook) { | ||
return this.processedHooks[hook].has(plugin); | ||
hasBeenProcessed(requestId, plugin, hook) { | ||
if (!this.processedHooks.has(requestId)) { | ||
return false; | ||
} | ||
const hooksForRequest = this.processedHooks.get(requestId); | ||
return hooksForRequest && hooksForRequest[hook].has(plugin); | ||
} | ||
@@ -35,3 +37,3 @@ isPreRequestContext(context) { | ||
} | ||
processPlugins(hook, context, resolve) { | ||
processPlugins(requestId, hook, context, resolve) { | ||
let index = 0; | ||
@@ -50,3 +52,3 @@ const isPreRequestContext = this.isPreRequestContext(context); | ||
const plugin = this.plugins[index]; | ||
if (this.hasBeenProcessed(plugin, hook)) { | ||
if (this.hasBeenProcessed(requestId, plugin, hook)) { | ||
index++; | ||
@@ -57,3 +59,3 @@ next(); | ||
index++; | ||
this.processedHooks[hook].add(plugin); | ||
this.processedHooks.get(requestId)[hook].add(plugin); | ||
const timeout = setTimeout(() => { | ||
@@ -82,3 +84,3 @@ console.error(`Plugin timed out: ${plugin.constructor.name}`); | ||
} | ||
runPreRequestHooks(request) { | ||
runPreRequestHooks(requestId, request) { | ||
return new Promise((resolve) => { | ||
@@ -94,2 +96,3 @@ this.modifiedRequest = request.clone(); | ||
this.processPlugins( | ||
requestId, | ||
"preRequest" /* PRE_REQUEST */, | ||
@@ -101,3 +104,3 @@ context, | ||
} | ||
runPostRequestHooks(response, originalRequest) { | ||
runPostRequestHooks(requestId, response, originalRequest) { | ||
return new Promise((resolve) => { | ||
@@ -113,2 +116,3 @@ this.modifiedResponse = response.clone(); | ||
this.processPlugins( | ||
requestId, | ||
"postRequest" /* POST_REQUEST */, | ||
@@ -129,2 +133,13 @@ context, | ||
} | ||
generateNewRequestId() { | ||
const newId = this.requestIdCounter++; | ||
this.processedHooks.set(newId, { | ||
preRequest: /* @__PURE__ */ new Set(), | ||
postRequest: /* @__PURE__ */ new Set() | ||
}); | ||
return newId; | ||
} | ||
clearProcessedPlugins(requestId) { | ||
this.processedHooks.delete(requestId); | ||
} | ||
}; | ||
@@ -140,4 +155,6 @@ | ||
async fetch(input, init) { | ||
const requestId = this.pluginManager.generateNewRequestId(); | ||
const originalRequest = new Request(input, init); | ||
const modifiedRequest = await this.pluginManager.runPreRequestHooks( | ||
requestId, | ||
originalRequest.clone() | ||
@@ -150,5 +167,7 @@ ); | ||
const modifiedResponse = await this.pluginManager.runPostRequestHooks( | ||
requestId, | ||
response.clone(), | ||
originalRequest.clone() | ||
); | ||
this.pluginManager.clearProcessedPlugins(requestId); | ||
return modifiedResponse; | ||
@@ -155,0 +174,0 @@ } |
declare class PluginManager { | ||
private plugins; | ||
private requestIdCounter; | ||
private modifiedRequest; | ||
@@ -11,7 +12,9 @@ private modifiedResponse; | ||
private processPlugins; | ||
runPreRequestHooks(request: Request): Promise<Request | Response>; | ||
runPostRequestHooks(response: Response, originalRequest: Request): Promise<Response>; | ||
runPreRequestHooks(requestId: number, request: Request): Promise<Request | Response>; | ||
runPostRequestHooks(requestId: number, response: Response, originalRequest: Request): Promise<Response>; | ||
getModifiedRequest(): Request; | ||
getModifiedResponse(): Response; | ||
getPlugins(): Plugin[]; | ||
generateNewRequestId(): number; | ||
clearProcessedPlugins(requestId: number): void; | ||
} | ||
@@ -18,0 +21,0 @@ |
@@ -11,8 +11,6 @@ // src/interfaces/index.ts | ||
plugins = []; | ||
requestIdCounter = 0; | ||
modifiedRequest; | ||
modifiedResponse; | ||
processedHooks = { | ||
preRequest: /* @__PURE__ */ new Set(), | ||
postRequest: /* @__PURE__ */ new Set() | ||
}; | ||
processedHooks = /* @__PURE__ */ new Map(); | ||
addPlugins(plugin) { | ||
@@ -25,4 +23,8 @@ if (Array.isArray(plugin)) { | ||
} | ||
hasBeenProcessed(plugin, hook) { | ||
return this.processedHooks[hook].has(plugin); | ||
hasBeenProcessed(requestId, plugin, hook) { | ||
if (!this.processedHooks.has(requestId)) { | ||
return false; | ||
} | ||
const hooksForRequest = this.processedHooks.get(requestId); | ||
return hooksForRequest && hooksForRequest[hook].has(plugin); | ||
} | ||
@@ -35,3 +37,3 @@ isPreRequestContext(context) { | ||
} | ||
processPlugins(hook, context, resolve) { | ||
processPlugins(requestId, hook, context, resolve) { | ||
let index = 0; | ||
@@ -50,3 +52,3 @@ const isPreRequestContext = this.isPreRequestContext(context); | ||
const plugin = this.plugins[index]; | ||
if (this.hasBeenProcessed(plugin, hook)) { | ||
if (this.hasBeenProcessed(requestId, plugin, hook)) { | ||
index++; | ||
@@ -57,3 +59,3 @@ next(); | ||
index++; | ||
this.processedHooks[hook].add(plugin); | ||
this.processedHooks.get(requestId)[hook].add(plugin); | ||
const timeout = setTimeout(() => { | ||
@@ -82,3 +84,3 @@ console.error(`Plugin timed out: ${plugin.constructor.name}`); | ||
} | ||
runPreRequestHooks(request) { | ||
runPreRequestHooks(requestId, request) { | ||
return new Promise((resolve) => { | ||
@@ -94,2 +96,3 @@ this.modifiedRequest = request.clone(); | ||
this.processPlugins( | ||
requestId, | ||
"preRequest" /* PRE_REQUEST */, | ||
@@ -101,3 +104,3 @@ context, | ||
} | ||
runPostRequestHooks(response, originalRequest) { | ||
runPostRequestHooks(requestId, response, originalRequest) { | ||
return new Promise((resolve) => { | ||
@@ -113,2 +116,3 @@ this.modifiedResponse = response.clone(); | ||
this.processPlugins( | ||
requestId, | ||
"postRequest" /* POST_REQUEST */, | ||
@@ -129,2 +133,13 @@ context, | ||
} | ||
generateNewRequestId() { | ||
const newId = this.requestIdCounter++; | ||
this.processedHooks.set(newId, { | ||
preRequest: /* @__PURE__ */ new Set(), | ||
postRequest: /* @__PURE__ */ new Set() | ||
}); | ||
return newId; | ||
} | ||
clearProcessedPlugins(requestId) { | ||
this.processedHooks.delete(requestId); | ||
} | ||
}; | ||
@@ -140,4 +155,6 @@ | ||
async fetch(input, init) { | ||
const requestId = this.pluginManager.generateNewRequestId(); | ||
const originalRequest = new Request(input, init); | ||
const modifiedRequest = await this.pluginManager.runPreRequestHooks( | ||
requestId, | ||
originalRequest.clone() | ||
@@ -150,5 +167,7 @@ ); | ||
const modifiedResponse = await this.pluginManager.runPostRequestHooks( | ||
requestId, | ||
response.clone(), | ||
originalRequest.clone() | ||
); | ||
this.pluginManager.clearProcessedPlugins(requestId); | ||
return modifiedResponse; | ||
@@ -155,0 +174,0 @@ } |
{ | ||
"name": "@composite-fetcher/core", | ||
"description": "Fetcher core", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"author": "Teofanis Papadopulos", | ||
@@ -6,0 +6,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23640
617