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

@cloudflare/kv-asset-handler

Package Overview
Dependencies
Maintainers
0
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cloudflare/kv-asset-handler - npm Package Compare versions

Comparing version 0.3.3 to 0.3.4

5

dist/index.d.ts

@@ -0,4 +1,5 @@

/// <reference types="@cloudflare/workers-types" />
import { CacheControl, InternalError, MethodNotAllowedError, NotFoundError, Options } from "./types";
declare global {
var __STATIC_CONTENT: any, __STATIC_CONTENT_MANIFEST: string;
const __STATIC_CONTENT: KVNamespace | undefined, __STATIC_CONTENT_MANIFEST: string;
}

@@ -32,3 +33,3 @@ /**

request: Request;
waitUntil: (promise: Promise<any>) => void;
waitUntil: (promise: Promise<unknown>) => void;
};

@@ -35,0 +36,0 @@ declare const getAssetFromKV: (event: Evt, options?: Partial<Options>) => Promise<Response>;

37

dist/index.js
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InternalError = exports.NotFoundError = exports.MethodNotAllowedError = exports.serveSinglePageApp = exports.mapRequestToAsset = exports.getAssetFromKV = void 0;
const mime = require("mime");
const mime = __importStar(require("mime"));
const types_1 = require("./types");

@@ -127,3 +150,3 @@ Object.defineProperty(exports, "InternalError", { enumerable: true, get: function () { return types_1.InternalError; } });

let pathKey = pathname.replace(/^\/+/, ""); // remove prepended /
// @ts-ignore
// @ts-expect-error we should pick cf types here
const cache = caches.default;

@@ -144,3 +167,3 @@ let mimeType = mime.getType(pathKey) || options.defaultMimeType;

// TODO this excludes search params from cache, investigate ideal behavior
let cacheKey = new Request(`${parsedUrl.origin}/${pathKey}`, request);
const cacheKey = new Request(`${parsedUrl.origin}/${pathKey}`, request);
// if argument passed in for cacheControl is a function then

@@ -214,3 +237,3 @@ // evaluate that function. otherwise return the Object passed in

// fixes #165
let opts = {
const opts = {
headers: new Headers(response.headers),

@@ -257,5 +280,5 @@ status: 0,

if (response.status === 304) {
let etag = formatETag(response.headers.get("etag"));
let ifNoneMatch = cacheKey.headers.get("if-none-match");
let proxyCacheStatus = response.headers.get("CF-Cache-Status");
const etag = formatETag(response.headers.get("etag"));
const ifNoneMatch = cacheKey.headers.get("if-none-match");
const proxyCacheStatus = response.headers.get("CF-Cache-Status");
if (etag) {

@@ -262,0 +285,0 @@ if (ifNoneMatch && ifNoneMatch === etag && proxyCacheStatus === "MISS") {

@@ -1,4 +0,5 @@

export declare const getEvent: (request: Request) => any;
export declare const mockKV: (store: any) => {
get: (path: string) => any;
/// <reference types="@cloudflare/workers-types" />
export declare const getEvent: (request: Request) => Pick<FetchEvent, "request" | "waitUntil">;
export declare const mockKV: (kvStore: Record<string, string>) => {
get: (path: string) => string;
};

@@ -8,4 +9,4 @@ export declare const mockManifest: () => string;

default: {
match(key: any): Promise<any>;
put(key: any, val: Response): Promise<void>;
match(key: Request): Promise<Response>;
put(key: Request, val: Response): Promise<void>;
};

@@ -12,0 +13,0 @@ };

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sleep = exports.mockGlobalScope = exports.mockRequestScope = exports.mockCaches = exports.mockManifest = exports.mockKV = exports.getEvent = void 0;
const makeServiceWorkerEnv = require("service-worker-mock");
const service_worker_mock_1 = __importDefault(require("service-worker-mock"));
const HASH = "123HASHBROWN";
const getEvent = (request) => {
const waitUntil = async (callback) => {
await callback;
const waitUntil = async (maybePromise) => {
await maybePromise;
};

@@ -35,5 +38,5 @@ return {

};
const mockKV = (store) => {
const mockKV = (kvStore) => {
return {
get: (path) => store[path] || null,
get: (path) => kvStore[path] || null,
};

@@ -63,3 +66,3 @@ };

exports.mockManifest = mockManifest;
let cacheStore = new Map();
const cacheStore = new Map();
const mockCaches = () => {

@@ -69,3 +72,3 @@ return {

async match(key) {
let cacheKey = {
const cacheKey = {
url: key.url,

@@ -76,3 +79,3 @@ headers: {},

if (key.headers.has("if-none-match")) {
let makeStrongEtag = key.headers
const makeStrongEtag = key.headers
.get("if-none-match")

@@ -103,8 +106,10 @@ .replace("W/", "");

if (response.headers.has("content-range")) {
// @ts-expect-error overridding status in this mock
response.status = 206;
}
else {
// @ts-expect-error overridding status in this mock
response.status = 200;
}
let etag = response.headers.get("etag");
const etag = response.headers.get("etag");
if (etag && !etag.includes("W/")) {

@@ -117,7 +122,7 @@ response.headers.set("etag", `W/${etag}`);

async put(key, val) {
let headers = new Headers(val.headers);
let url = new URL(key.url);
let resWithBody = new Response(val.body, { headers, status: 200 });
let resNoBody = new Response(null, { headers, status: 304 });
let cacheKey = {
const headers = new Headers(val.headers);
const url = new URL(key.url);
const resWithBody = new Response(val.body, { headers, status: 200 });
const resNoBody = new Response(null, { headers, status: 304 });
const cacheKey = {
url: key.url,

@@ -139,6 +144,6 @@ headers: {

function mockRequestScope() {
Object.assign(global, makeServiceWorkerEnv());
Object.assign(global, { __STATIC_CONTENT_MANIFEST: (0, exports.mockManifest)() });
Object.assign(global, { __STATIC_CONTENT: (0, exports.mockKV)(store) });
Object.assign(global, { caches: (0, exports.mockCaches)() });
Object.assign(globalThis, (0, service_worker_mock_1.default)());
Object.assign(globalThis, { __STATIC_CONTENT_MANIFEST: (0, exports.mockManifest)() });
Object.assign(globalThis, { __STATIC_CONTENT: (0, exports.mockKV)(store) });
Object.assign(globalThis, { caches: (0, exports.mockCaches)() });
}

@@ -148,4 +153,4 @@ exports.mockRequestScope = mockRequestScope;

function mockGlobalScope() {
Object.assign(global, { __STATIC_CONTENT_MANIFEST: (0, exports.mockManifest)() });
Object.assign(global, { __STATIC_CONTENT: (0, exports.mockKV)(store) });
Object.assign(globalThis, { __STATIC_CONTENT_MANIFEST: (0, exports.mockManifest)() });
Object.assign(globalThis, { __STATIC_CONTENT: (0, exports.mockKV)(store) });
}

@@ -152,0 +157,0 @@ exports.mockGlobalScope = mockGlobalScope;

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

/// <reference types="@cloudflare/workers-types" />
export type CacheControl = {

@@ -9,3 +10,3 @@ browserTTL: number;

cacheControl: ((req: Request) => Partial<CacheControl>) | Partial<CacheControl>;
ASSET_NAMESPACE: any;
ASSET_NAMESPACE: KVNamespace;
ASSET_MANIFEST: AssetManifestType | string;

@@ -12,0 +13,0 @@ mapRequestToAsset?: (req: Request, options?: Partial<Options>) => Request;

@@ -12,2 +12,3 @@ "use strict";

}
status;
}

@@ -14,0 +15,0 @@ exports.KVError = KVError;

{
"name": "@cloudflare/kv-asset-handler",
"version": "0.3.3",
"version": "0.3.4",
"description": "Routes requests to KV assets",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/cloudflare/workers-sdk.git",
"directory": "packages/kv-asset-handler"
},
"keywords": [

@@ -19,2 +12,15 @@ "kv",

],
"homepage": "https://github.com/cloudflare/workers-sdk#readme",
"bugs": {
"url": "https://github.com/cloudflare/workers-sdk/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/cloudflare/workers-sdk.git",
"directory": "packages/kv-asset-handler"
},
"license": "MIT OR Apache-2.0",
"author": "wrangler@cloudflare.com",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [

@@ -26,8 +32,2 @@ "src",

],
"author": "wrangler@cloudflare.com",
"license": "MIT OR Apache-2.0",
"bugs": {
"url": "https://github.com/cloudflare/workers-sdk/issues"
},
"homepage": "https://github.com/cloudflare/workers-sdk#readme",
"dependencies": {

@@ -38,5 +38,6 @@ "mime": "^3.0.0"

"@ava/typescript": "^4.1.0",
"@cloudflare/workers-types": "^4.20240605.0",
"@cloudflare/workers-types": "^4.20240620.0",
"@types/mime": "^3.0.4",
"@types/node": "20.8.3",
"@types/service-worker-mock": "^2.0.1",
"ava": "^6.0.1",

@@ -48,2 +49,5 @@ "service-worker-mock": "^2.0.5"

},
"volta": {
"extends": "../../package.json"
},
"publishConfig": {

@@ -55,7 +59,6 @@ "access": "public"

},
"volta": {
"extends": "../../package.json"
},
"scripts": {
"build": "tsc -d",
"check:lint": "eslint .",
"check:type": "tsc",
"pretest": "npm run build",

@@ -62,0 +65,0 @@ "test": "ava dist/test/*.js --verbose",

import * as mime from "mime";
import {
AssetManifestType,
CacheControl,

@@ -10,5 +9,7 @@ InternalError,

} from "./types";
import type { AssetManifestType } from "./types";
declare global {
var __STATIC_CONTENT: any, __STATIC_CONTENT_MANIFEST: string;
const __STATIC_CONTENT: KVNamespace | undefined,
__STATIC_CONTENT_MANIFEST: string;
}

@@ -120,3 +121,3 @@

request: Request;
waitUntil: (promise: Promise<any>) => void;
waitUntil: (promise: Promise<unknown>) => void;
};

@@ -182,3 +183,3 @@

// @ts-ignore
// @ts-expect-error we should pick cf types here
const cache = caches.default;

@@ -201,3 +202,3 @@ let mimeType = mime.getType(pathKey) || options.defaultMimeType;

// TODO this excludes search params from cache, investigate ideal behavior
let cacheKey = new Request(`${parsedUrl.origin}/${pathKey}`, request);
const cacheKey = new Request(`${parsedUrl.origin}/${pathKey}`, request);

@@ -223,3 +224,3 @@ // if argument passed in for cacheControl is a function then

const formatETag = (
entityId: any = pathKey,
entityId: string = pathKey,
validatorType: string = options.defaultETag

@@ -282,3 +283,3 @@ ) => {

// fixes #165
let opts = {
const opts = {
headers: new Headers(response.headers),

@@ -331,5 +332,5 @@ status: 0,

if (response.status === 304) {
let etag = formatETag(response.headers.get("etag"));
let ifNoneMatch = cacheKey.headers.get("if-none-match");
let proxyCacheStatus = response.headers.get("CF-Cache-Status");
const etag = formatETag(response.headers.get("etag"));
const ifNoneMatch = cacheKey.headers.get("if-none-match");
const proxyCacheStatus = response.headers.get("CF-Cache-Status");
if (etag) {

@@ -336,0 +337,0 @@ if (ifNoneMatch && ifNoneMatch === etag && proxyCacheStatus === "MISS") {

@@ -1,8 +0,10 @@

const makeServiceWorkerEnv = require("service-worker-mock");
import makeServiceWorkerEnv from "service-worker-mock";
const HASH = "123HASHBROWN";
export const getEvent = (request: Request): any => {
const waitUntil = async (callback: any) => {
await callback;
export const getEvent = (
request: Request
): Pick<FetchEvent, "request" | "waitUntil"> => {
const waitUntil = async (maybePromise: unknown) => {
await maybePromise;
};

@@ -14,3 +16,3 @@ return {

};
const store: any = {
const store = {
"key1.123HASHBROWN.txt": "val1",

@@ -34,5 +36,5 @@ "key1.123HASHBROWN.png": "val1",

};
export const mockKV = (store: any) => {
export const mockKV = (kvStore: Record<string, string>) => {
return {
get: (path: string) => store[path] || null,
get: (path: string) => kvStore[path] || null,
};

@@ -62,6 +64,6 @@ };

let cacheStore: any = new Map();
const cacheStore = new Map<string, Response>();
interface CacheKey {
url: object;
headers: object;
url: string;
headers: HeadersInit;
}

@@ -71,4 +73,4 @@ export const mockCaches = () => {

default: {
async match(key: any) {
let cacheKey: CacheKey = {
async match(key: Request) {
const cacheKey: CacheKey = {
url: key.url,

@@ -79,3 +81,3 @@ headers: {},

if (key.headers.has("if-none-match")) {
let makeStrongEtag = key.headers
const makeStrongEtag = key.headers
.get("if-none-match")

@@ -110,7 +112,9 @@ .replace("W/", "");

if (response.headers.has("content-range")) {
// @ts-expect-error overridding status in this mock
response.status = 206;
} else {
// @ts-expect-error overridding status in this mock
response.status = 200;
}
let etag = response.headers.get("etag");
const etag = response.headers.get("etag");
if (etag && !etag.includes("W/")) {

@@ -122,8 +126,8 @@ response.headers.set("etag", `W/${etag}`);

},
async put(key: any, val: Response) {
let headers = new Headers(val.headers);
let url = new URL(key.url);
let resWithBody = new Response(val.body, { headers, status: 200 });
let resNoBody = new Response(null, { headers, status: 304 });
let cacheKey: CacheKey = {
async put(key: Request, val: Response) {
const headers = new Headers(val.headers);
const url = new URL(key.url);
const resWithBody = new Response(val.body, { headers, status: 200 });
const resNoBody = new Response(null, { headers, status: 304 });
const cacheKey: CacheKey = {
url: key.url,

@@ -145,6 +149,6 @@ headers: {

export function mockRequestScope() {
Object.assign(global, makeServiceWorkerEnv());
Object.assign(global, { __STATIC_CONTENT_MANIFEST: mockManifest() });
Object.assign(global, { __STATIC_CONTENT: mockKV(store) });
Object.assign(global, { caches: mockCaches() });
Object.assign(globalThis, makeServiceWorkerEnv());
Object.assign(globalThis, { __STATIC_CONTENT_MANIFEST: mockManifest() });
Object.assign(globalThis, { __STATIC_CONTENT: mockKV(store) });
Object.assign(globalThis, { caches: mockCaches() });
}

@@ -154,4 +158,4 @@

export function mockGlobalScope() {
Object.assign(global, { __STATIC_CONTENT_MANIFEST: mockManifest() });
Object.assign(global, { __STATIC_CONTENT: mockKV(store) });
Object.assign(globalThis, { __STATIC_CONTENT_MANIFEST: mockManifest() });
Object.assign(globalThis, { __STATIC_CONTENT: mockKV(store) });
}

@@ -158,0 +162,0 @@

@@ -13,3 +13,3 @@ export type CacheControl = {

| Partial<CacheControl>;
ASSET_NAMESPACE: any;
ASSET_NAMESPACE: KVNamespace;
ASSET_MANIFEST: AssetManifestType | string;

@@ -16,0 +16,0 @@ mapRequestToAsset?: (req: Request, options?: Partial<Options>) => Request;

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