Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@serwist/strategies

Package Overview
Dependencies
Maintainers
1
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@serwist/strategies - npm Package Compare versions

Comparing version 9.0.0-preview.12 to 9.0.0-preview.13

2

dist/CacheFirst.d.ts
import { Strategy } from "./Strategy.js";
import type { StrategyHandler } from "./StrategyHandler.js";
/**
* An implementation of the [cache first](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#cache-first-falling-back-to-network)
* An implementation of the [cache first](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#cache_first_falling_back_to_network)
* request strategy.

@@ -6,0 +6,0 @@ *

@@ -7,10 +7,10 @@ import { assert, Deferred, logger, getFriendlyURL, SerwistError, timeout, cacheMatchIgnoreParams, executeQuotaErrorCallbacks, privateCacheNames } from '@serwist/core/internal';

class StrategyHandler {
event;
request;
url;
event;
params;
_cacheKeys = {};
_strategy;
_handlerDeferred;
_extendLifetimePromises;
_handlerDeferred;
_plugins;

@@ -26,5 +26,15 @@ _pluginStateMap;

});
assert.isInstance(options.request, Request, {
moduleName: "@serwist/strategies",
className: "StrategyHandler",
funcName: "constructor",
paramName: "options.request"
});
}
Object.assign(this, options);
this.event = options.event;
this.request = options.request;
if (options.url) {
this.url = options.url;
this.params = options.params;
}
this._strategy = strategy;

@@ -313,7 +323,10 @@ this._handlerDeferred = new Deferred();

const request = typeof options.request === "string" ? new Request(options.request) : options.request;
const params = "params" in options ? options.params : undefined;
const handler = new StrategyHandler(this, {
const handler = new StrategyHandler(this, options.url ? {
event,
request,
params
url: options.url,
params: options.params
} : {
event,
request
});

@@ -320,0 +333,0 @@ const responseDone = this._getResponse(handler, request, event);

@@ -10,16 +10,17 @@ import type { StrategyOptions } from "./Strategy.js";

}
/**
* An implementation of the [network first](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#network-first-falling-back-to-cache)
* request strategy.
*
* By default, this strategy will cache responses with a 200 status code as
* well as [opaque responses](https://developer.chrome.com/docs/workbox/caching-resources-during-runtime/#opaque-responses).
* Opaque responses are are cross-origin requests where the response doesn't
* support [CORS](https://enable-cors.org/).
*
* If the network request fails, and there is no cache match, this will throw
* a `SerwistError` exception.
*/
export declare class NetworkFirst extends Strategy {
private readonly _networkTimeoutSeconds;
/**
* An implementation of the [network first](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#network-first-falling-back-to-cache)
* request strategy.
*
* By default, this strategy will cache responses with a 200 status code as
* well as [opaque responses](https://developer.chrome.com/docs/workbox/caching-resources-during-runtime/#opaque-responses).
* Opaque responses are are cross-origin requests where the response doesn't
* support [CORS](https://enable-cors.org/).
*
* If the network request fails, and there is no cache match, this will throw
* a `SerwistError` exception.
*
* @param options

@@ -26,0 +27,0 @@ * This option can be used to combat

@@ -10,12 +10,13 @@ import type { StrategyOptions } from "./Strategy.js";

}
/**
* An implementation of the [network only](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#network-only)
* request strategy.
*
* This class is useful if you want to take advantage of any Serwist plugin.
*
* If the network request fails, this will throw a `SerwistError` exception.
*/
export declare class NetworkOnly extends Strategy {
private readonly _networkTimeoutSeconds;
/**
* An implementation of the [network only](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#network-only)
* request strategy.
*
* This class is useful if you want to take advantage of any Serwist plugin.
*
* If the network request fails, this will throw a `SerwistError` exception.
*
* @param options

@@ -22,0 +23,0 @@ */

import type { StrategyOptions } from "./Strategy.js";
import { Strategy } from "./Strategy.js";
import type { StrategyHandler } from "./StrategyHandler.js";
/**
* An implementation of the
* [stale-while-revalidate](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#stale-while-revalidate)
* request strategy.
*
* Resources are requested from both the cache and the network in parallel.
* The strategy will respond with the cached version if available, otherwise
* wait for the network response. The cache is updated with the network response
* with each successful request.
*
* By default, this strategy will cache responses with a 200 status code as
* well as [opaque responses](https://developer.chrome.com/docs/workbox/caching-resources-during-runtime/#opaque-responses).
* Opaque responses are cross-origin requests where the response doesn't
* support [CORS](https://enable-cors.org/).
*
* If the network request fails, and there is no cache match, this will throw
* a `SerwistError` exception.
*/
export declare class StaleWhileRevalidate extends Strategy {
/**
* An implementation of the
* [stale-while-revalidate](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#stale-while-revalidate)
* request strategy.
*
* Resources are requested from both the cache and the network in parallel.
* The strategy will respond with the cached version if available, otherwise
* wait for the network response. The cache is updated with the network response
* with each successful request.
*
* By default, this strategy will cache responses with a 200 status code as
* well as [opaque responses](https://developer.chrome.com/docs/workbox/caching-resources-during-runtime/#opaque-responses).
* Opaque responses are cross-origin requests where the response doesn't
* support [CORS](https://enable-cors.org/).
*
* If the network request fails, and there is no cache match, this will throw
* a `SerwistError` exception.
*
* @param options

@@ -24,0 +25,0 @@ */

@@ -9,3 +9,3 @@ import type { HandlerCallbackOptions, RouteHandlerObject, SerwistPlugin } from "@serwist/core";

/**
* [Plugins](https://developers.google.com/web/tools/workbox/guides/using-plugins)
* [Plugins](https://developer.chrome.com/docs/workbox/using-plugins)
* to use in conjunction with this caching strategy.

@@ -12,0 +12,0 @@ */

@@ -1,2 +0,2 @@

import type { HandlerCallbackOptions, SerwistPlugin, SerwistPluginCallbackParam } from "@serwist/core";
import type { HandlerCallbackOptions, MapLikeObject, SerwistPlugin, SerwistPluginCallbackParam } from "@serwist/core";
import type { Strategy } from "./Strategy.js";

@@ -11,2 +11,6 @@ /**

/**
* The event associated with this request.
*/
event: ExtendableEvent;
/**
* The request the strategy is performing (passed to the strategy's

@@ -24,6 +28,2 @@ * `handle()` or `handleAll()` method).

/**
* The event associated with this request.
*/
event: ExtendableEvent;
/**
* A `param` value (if passed to the strategy's

@@ -35,7 +35,7 @@ * `handle()` or `handleAll()` method).

*/
params?: any;
params?: string[] | MapLikeObject;
private _cacheKeys;
private readonly _strategy;
private readonly _handlerDeferred;
private readonly _extendLifetimePromises;
private readonly _handlerDeferred;
private readonly _plugins;

@@ -53,3 +53,5 @@ private readonly _pluginStateMap;

*/
constructor(strategy: Strategy, options: HandlerCallbackOptions);
constructor(strategy: Strategy, options: HandlerCallbackOptions & {
request: HandlerCallbackOptions["request"] & Request;
});
/**

@@ -56,0 +58,0 @@ * Fetches a given request (and invokes any applicable plugin callback

{
"name": "@serwist/strategies",
"version": "9.0.0-preview.12",
"version": "9.0.0-preview.13",
"type": "module",

@@ -33,8 +33,8 @@ "description": "A service worker helper library implementing common caching strategies.",

"dependencies": {
"@serwist/core": "9.0.0-preview.12"
"@serwist/core": "9.0.0-preview.13"
},
"devDependencies": {
"rollup": "4.9.6",
"typescript": "5.4.0-dev.20240206",
"@serwist/constants": "9.0.0-preview.12"
"rollup": "4.12.0",
"typescript": "5.5.0-dev.20240304",
"@serwist/constants": "9.0.0-preview.13"
},

@@ -41,0 +41,0 @@ "peerDependencies": {

@@ -16,3 +16,3 @@ /*

/**
* An implementation of the [cache first](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#cache-first-falling-back-to-network)
* An implementation of the [cache first](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#cache_first_falling_back_to_network)
* request strategy.

@@ -19,0 +19,0 @@ *

@@ -24,17 +24,17 @@ /*

/**
* An implementation of the [network first](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#network-first-falling-back-to-cache)
* request strategy.
*
* By default, this strategy will cache responses with a 200 status code as
* well as [opaque responses](https://developer.chrome.com/docs/workbox/caching-resources-during-runtime/#opaque-responses).
* Opaque responses are are cross-origin requests where the response doesn't
* support [CORS](https://enable-cors.org/).
*
* If the network request fails, and there is no cache match, this will throw
* a `SerwistError` exception.
*/
export class NetworkFirst extends Strategy {
private readonly _networkTimeoutSeconds: number;
/**
* An implementation of the [network first](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#network-first-falling-back-to-cache)
* request strategy.
*
* By default, this strategy will cache responses with a 200 status code as
* well as [opaque responses](https://developer.chrome.com/docs/workbox/caching-resources-during-runtime/#opaque-responses).
* Opaque responses are are cross-origin requests where the response doesn't
* support [CORS](https://enable-cors.org/).
*
* If the network request fails, and there is no cache match, this will throw
* a `SerwistError` exception.
*
* @param options

@@ -41,0 +41,0 @@ * This option can be used to combat

@@ -23,2 +23,10 @@ /*

/**
* An implementation of the [network only](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#network-only)
* request strategy.
*
* This class is useful if you want to take advantage of any Serwist plugin.
*
* If the network request fails, this will throw a `SerwistError` exception.
*/
export class NetworkOnly extends Strategy {

@@ -28,9 +36,2 @@ private readonly _networkTimeoutSeconds: number;

/**
* An implementation of the [network only](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#network-only)
* request strategy.
*
* This class is useful if you want to take advantage of any Serwist plugin.
*
* If the network request fails, this will throw a `SerwistError` exception.
*
* @param options

@@ -37,0 +38,0 @@ */

@@ -17,21 +17,22 @@ /*

/**
* An implementation of the
* [stale-while-revalidate](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#stale-while-revalidate)
* request strategy.
*
* Resources are requested from both the cache and the network in parallel.
* The strategy will respond with the cached version if available, otherwise
* wait for the network response. The cache is updated with the network response
* with each successful request.
*
* By default, this strategy will cache responses with a 200 status code as
* well as [opaque responses](https://developer.chrome.com/docs/workbox/caching-resources-during-runtime/#opaque-responses).
* Opaque responses are cross-origin requests where the response doesn't
* support [CORS](https://enable-cors.org/).
*
* If the network request fails, and there is no cache match, this will throw
* a `SerwistError` exception.
*/
export class StaleWhileRevalidate extends Strategy {
/**
* An implementation of the
* [stale-while-revalidate](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#stale-while-revalidate)
* request strategy.
*
* Resources are requested from both the cache and the network in parallel.
* The strategy will respond with the cached version if available, otherwise
* wait for the network response. The cache is updated with the network response
* with each successful request.
*
* By default, this strategy will cache responses with a 200 status code as
* well as [opaque responses](https://developer.chrome.com/docs/workbox/caching-resources-during-runtime/#opaque-responses).
* Opaque responses are cross-origin requests where the response doesn't
* support [CORS](https://enable-cors.org/).
*
* If the network request fails, and there is no cache match, this will throw
* a `SerwistError` exception.
*
* @param options

@@ -38,0 +39,0 @@ */

@@ -20,3 +20,3 @@ /*

/**
* [Plugins](https://developers.google.com/web/tools/workbox/guides/using-plugins)
* [Plugins](https://developer.chrome.com/docs/workbox/using-plugins)
* to use in conjunction with this caching strategy.

@@ -115,5 +115,4 @@ */

const request = typeof options.request === "string" ? new Request(options.request) : options.request;
const params = "params" in options ? options.params : undefined;
const handler = new StrategyHandler(this, { event, request, params });
const handler = new StrategyHandler(this, options.url ? { event, request, url: options.url, params: options.params } : { event, request });

@@ -120,0 +119,0 @@ const responseDone = this._getResponse(handler, request, event);

@@ -35,6 +35,10 @@ /*

/**
* The event associated with this request.
*/
public event: ExtendableEvent;
/**
* The request the strategy is performing (passed to the strategy's
* `handle()` or `handleAll()` method).
*/
public request!: Request;
public request: Request;
/**

@@ -48,6 +52,2 @@ * A `URL` instance of `request.url` (if passed to the strategy's

/**
* The event associated with this request.
*/
public event: ExtendableEvent;
/**
* A `param` value (if passed to the strategy's

@@ -59,9 +59,8 @@ * `handle()` or `handleAll()` method).

*/
public params?: any;
public params?: string[] | MapLikeObject;
private _cacheKeys: Record<string, Request> = {};
private readonly _strategy: Strategy;
private readonly _handlerDeferred: Deferred<any>;
private readonly _extendLifetimePromises: Promise<any>[];
private readonly _handlerDeferred: Deferred<any>;
private readonly _plugins: SerwistPlugin[];

@@ -80,3 +79,8 @@ private readonly _pluginStateMap: Map<SerwistPlugin, MapLikeObject>;

*/
constructor(strategy: Strategy, options: HandlerCallbackOptions) {
constructor(
strategy: Strategy,
options: HandlerCallbackOptions & {
request: HandlerCallbackOptions["request"] & Request;
},
) {
if (process.env.NODE_ENV !== "production") {

@@ -89,7 +93,16 @@ assert!.isInstance(options.event, ExtendableEvent, {

});
assert!.isInstance(options.request, Request, {
moduleName: "@serwist/strategies",
className: "StrategyHandler",
funcName: "constructor",
paramName: "options.request",
});
}
Object.assign(this, options);
this.event = options.event;
this.request = options.request;
if (options.url) {
this.url = options.url;
this.params = options.params;
}
this._strategy = strategy;

@@ -96,0 +109,0 @@ this._handlerDeferred = new Deferred();

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc