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

miniflare

Package Overview
Dependencies
Maintainers
1
Versions
452
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

miniflare - npm Package Compare versions

Comparing version 0.0.0-46a91e7e7 to 0.0.0-48eeff467

596

dist/src/workers/assets/assets.worker.js

@@ -1,113 +0,543 @@

// ../workers-shared/dist/asset-worker.mjs
import { WorkerEntrypoint as T } from "cloudflare:workers";
var i = class {
// ../workers-shared/asset-worker/src/index.ts
import { WorkerEntrypoint } from "cloudflare:workers";
// ../workers-shared/asset-worker/src/assets-manifest.ts
var AssetsManifest = class {
data;
constructor(e) {
this.data = e;
constructor(data) {
this.data = data;
}
async get(e) {
let n = await g(e), s = p(new Uint8Array(this.data, 20), n);
return s ? S(s) : null;
async get(pathname) {
let pathHash = await hashPath(pathname), entry = binarySearch(
new Uint8Array(this.data, 20),
pathHash
);
return entry ? contentHashToKey(entry) : null;
}
}, g = async (t) => {
let n = new TextEncoder().encode(t), s = await crypto.subtle.digest("SHA-256", n.buffer);
return new Uint8Array(s, 0, 16);
}, p = (t, e) => {
if (t.byteLength === 0)
}, hashPath = async (path) => {
let data = new TextEncoder().encode(path), hashBuffer = await crypto.subtle.digest("SHA-256", data.buffer);
return new Uint8Array(hashBuffer, 0, 16);
}, binarySearch = (arr, searchValue) => {
if (arr.byteLength === 0)
return !1;
let n = t.byteOffset + (t.byteLength / 40 >> 1) * 40, s = new Uint8Array(t.buffer, n, 16);
if (s.byteLength !== e.byteLength)
throw new TypeError("Search value and current value are of different lengths");
let r = H(e, s);
if (r < 0) {
let a = t.byteOffset, o = n - t.byteOffset;
return p(new Uint8Array(t.buffer, a, o), e);
} else if (r > 0) {
let a = n + 40, o = t.buffer.byteLength - n - 40;
return p(new Uint8Array(t.buffer, a, o), e);
let offset = arr.byteOffset + (arr.byteLength / 40 >> 1) * 40, current = new Uint8Array(arr.buffer, offset, 16);
if (current.byteLength !== searchValue.byteLength)
throw new TypeError(
"Search value and current value are of different lengths"
);
let cmp = compare(searchValue, current);
if (cmp < 0) {
let nextOffset = arr.byteOffset, nextLength = offset - arr.byteOffset;
return binarySearch(
new Uint8Array(arr.buffer, nextOffset, nextLength),
searchValue
);
} else if (cmp > 0) {
let nextOffset = offset + 40, nextLength = arr.buffer.byteLength - offset - 40;
return binarySearch(
new Uint8Array(arr.buffer, nextOffset, nextLength),
searchValue
);
} else
return new Uint8Array(t.buffer, n, 40);
}, H = (t, e) => {
if (t.byteLength < e.byteLength)
return new Uint8Array(arr.buffer, offset, 40);
}, compare = (a, b) => {
if (a.byteLength < b.byteLength)
return -1;
if (t.byteLength > e.byteLength)
if (a.byteLength > b.byteLength)
return 1;
for (let [n, s] of t.entries()) {
if (s < e[n])
for (let [i, v] of a.entries()) {
if (v < b[i])
return -1;
if (s > e[n])
if (v > b[i])
return 1;
}
return 0;
}, S = (t) => [...new Uint8Array(t, t.byteOffset + 16).slice(0, 16)].map((n) => n.toString(16).padStart(2, "0")).join(""), c = class extends Response {
constructor(e, n) {
super(e, { ...n, status: 200 });
}, contentHashToKey = (buffer) => [...buffer.slice(
16,
16 + 16
)].map((b) => b.toString(16).padStart(2, "0")).join("");
// ../workers-shared/asset-worker/src/configuration.ts
var applyConfigurationDefaults = (configuration) => ({
html_handling: configuration?.html_handling ?? "auto-trailing-slash",
not_found_handling: configuration?.not_found_handling ?? "none"
});
// ../workers-shared/asset-worker/src/responses.ts
var OkResponse = class extends Response {
constructor(body, init) {
super(body, {
...init,
status: 200
});
}
}, u = class extends Response {
constructor(...[e, n]) {
super(e, { ...n, status: 404, statusText: "Not Found" });
}, NotFoundResponse = class extends Response {
constructor(...[body, init]) {
super(body, {
...init,
status: 404,
statusText: "Not Found"
});
}
}, f = class extends Response {
constructor(...[e, n]) {
super(e, { ...n, status: 405, statusText: "Method Not Allowed" });
}, MethodNotAllowedResponse = class extends Response {
constructor(...[body, init]) {
super(body, {
...init,
status: 405,
statusText: "Method Not Allowed"
});
}
}, d = class extends Response {
constructor(e, n) {
super(void 0, { ...n, status: 500 });
}, InternalServerErrorResponse = class extends Response {
constructor(err, init) {
super(null, {
...init,
status: 500
});
}
}, h = "public, max-age=0, must-revalidate";
function y(t, e) {
let n = new Headers(t);
for (let [s, r] of e)
n.set(s, r);
return n;
}, NotModifiedResponse = class extends Response {
constructor(...[_body, init]) {
super(null, {
...init,
status: 304,
statusText: "Not Modified"
});
}
}, TemporaryRedirectResponse = class extends Response {
constructor(location, init) {
super(null, {
...init,
status: 307,
statusText: "Temporary Redirect",
headers: {
...init?.headers,
Location: location
}
});
}
};
// ../workers-shared/asset-worker/src/constants.ts
var CACHE_CONTROL_BROWSER = "public, max-age=0, must-revalidate";
// ../workers-shared/asset-worker/src/utils/headers.ts
function getHeaders(eTag, contentType, request) {
let headers = new Headers({
"Content-Type": contentType,
ETag: `"${eTag}"`
});
return isCacheable(request) && headers.append("Cache-Control", CACHE_CONTROL_BROWSER), headers;
}
function A(t, e, n) {
let s = e?.contentType ?? "application/octet-stream";
s.startsWith("text/") && !s.includes("charset") && (s = `${s}; charset=utf-8`);
let r = new Headers({ "Access-Control-Allow-Origin": "*", "Content-Type": s, "Referrer-Policy": "strict-origin-when-cross-origin", "X-Content-Type-Options": "nosniff", ETag: `${t}` });
return m(n) && r.append("Cache-Control", h), r;
function isCacheable(request) {
return !request.headers.has("Authorization") && !request.headers.has("Range");
}
function m(t) {
return !t.headers.has("authorization") && !t.headers.has("range");
}
async function E(t, e, n = 1) {
let s = 0;
for (; s <= n; )
// ../workers-shared/asset-worker/src/handler.ts
var handleRequest = async (request, configuration, exists, getByETag) => {
let { pathname, search } = new URL(request.url), intent = await getIntent(pathname, configuration, exists);
if (!intent)
return new NotFoundResponse();
let method = request.method.toUpperCase();
if (!["GET", "HEAD"].includes(method))
return new MethodNotAllowedResponse();
if (intent.redirect)
return new TemporaryRedirectResponse(intent.redirect + search);
if (!intent.asset)
return new InternalServerErrorResponse(new Error("Unknown action"));
let asset = await getByETag(intent.asset.eTag), headers = getHeaders(intent.asset.eTag, asset.contentType, request), strongETag = `"${intent.asset.eTag}"`, weakETag = `W/${strongETag}`, ifNoneMatch = request.headers.get("If-None-Match") || "";
if ([weakETag, strongETag].includes(ifNoneMatch))
return new NotModifiedResponse(null, { headers });
let body = method === "HEAD" ? null : asset.readableStream;
switch (intent.asset.status) {
case 404:
return new NotFoundResponse(body, { headers });
case 200:
return new OkResponse(body, { headers });
}
}, getIntent = async (pathname, configuration, exists, skipRedirects = !1) => {
switch (configuration.html_handling) {
case "auto-trailing-slash":
return htmlHandlingAutoTrailingSlash(
pathname,
configuration,
exists,
skipRedirects
);
case "force-trailing-slash":
return htmlHandlingForceTrailingSlash(
pathname,
configuration,
exists,
skipRedirects
);
case "drop-trailing-slash":
return htmlHandlingDropTrailingSlash(
pathname,
configuration,
exists,
skipRedirects
);
case "none":
return htmlHandlingNone(pathname, configuration, exists);
}
}, htmlHandlingAutoTrailingSlash = async (pathname, configuration, exists, skipRedirects) => {
let redirectResult = null, eTagResult = null, exactETag = await exists(pathname);
if (pathname.endsWith("/index")) {
if (exactETag)
return { asset: { eTag: exactETag, status: 200 }, redirect: null };
if (redirectResult = await safeRedirect(
`${pathname}.html`,
pathname.slice(0, -5),
configuration,
exists,
skipRedirects
))
return redirectResult;
if (redirectResult = await safeRedirect(
`${pathname.slice(0, -6)}.html`,
pathname.slice(0, -6),
configuration,
exists,
skipRedirects
))
return redirectResult;
} else if (pathname.endsWith("/index.html")) {
if (redirectResult = await safeRedirect(
pathname,
pathname.slice(0, -10),
configuration,
exists,
skipRedirects
))
return redirectResult;
if (redirectResult = await safeRedirect(
`${pathname.slice(0, -11)}.html`,
pathname.slice(0, -11),
configuration,
exists,
skipRedirects
))
return redirectResult;
} else if (pathname.endsWith("/")) {
if (eTagResult = await exists(`${pathname}index.html`))
return { asset: { eTag: eTagResult, status: 200 }, redirect: null };
if (redirectResult = await safeRedirect(
`${pathname.slice(0, -1)}.html`,
pathname.slice(0, -1),
configuration,
exists,
skipRedirects
))
return redirectResult;
} else if (pathname.endsWith(".html")) {
if (redirectResult = await safeRedirect(
pathname,
pathname.slice(0, -5),
configuration,
exists,
skipRedirects
))
return redirectResult;
if (redirectResult = await safeRedirect(
`${pathname.slice(0, -5)}/index.html`,
`${pathname.slice(0, -5)}/`,
configuration,
exists,
skipRedirects
))
return redirectResult;
}
return exactETag ? { asset: { eTag: exactETag, status: 200 }, redirect: null } : (eTagResult = await exists(`${pathname}.html`)) ? { asset: { eTag: eTagResult, status: 200 }, redirect: null } : (redirectResult = await safeRedirect(
`${pathname}/index.html`,
`${pathname}/`,
configuration,
exists,
skipRedirects
)) ? redirectResult : notFound(pathname, configuration, exists);
}, htmlHandlingForceTrailingSlash = async (pathname, configuration, exists, skipRedirects) => {
let redirectResult = null, eTagResult = null, exactETag = await exists(pathname);
if (pathname.endsWith("/index")) {
if (exactETag)
return { asset: { eTag: exactETag, status: 200 }, redirect: null };
if (redirectResult = await safeRedirect(
`${pathname}.html`,
pathname.slice(0, -5),
configuration,
exists,
skipRedirects
))
return redirectResult;
if (redirectResult = await safeRedirect(
`${pathname.slice(0, -6)}.html`,
pathname.slice(0, -5),
configuration,
exists,
skipRedirects
))
return redirectResult;
} else if (pathname.endsWith("/index.html")) {
if (redirectResult = await safeRedirect(
pathname,
pathname.slice(0, -10),
configuration,
exists,
skipRedirects
))
return redirectResult;
if (redirectResult = await safeRedirect(
`${pathname.slice(0, -11)}.html`,
pathname.slice(0, -10),
configuration,
exists,
skipRedirects
))
return redirectResult;
} else if (pathname.endsWith("/")) {
if (eTagResult = await exists(`${pathname}index.html`))
return { asset: { eTag: eTagResult, status: 200 }, redirect: null };
if (eTagResult = await exists(`${pathname.slice(0, -1)}.html`))
return { asset: { eTag: eTagResult, status: 200 }, redirect: null };
} else if (pathname.endsWith(".html")) {
if (redirectResult = await safeRedirect(
pathname,
`${pathname.slice(0, -5)}/`,
configuration,
exists,
skipRedirects
))
return redirectResult;
if (exactETag)
return { asset: { eTag: exactETag, status: 200 }, redirect: null };
if (redirectResult = await safeRedirect(
`${pathname.slice(0, -5)}/index.html`,
`${pathname.slice(0, -5)}/`,
configuration,
exists,
skipRedirects
))
return redirectResult;
}
return exactETag ? { asset: { eTag: exactETag, status: 200 }, redirect: null } : (redirectResult = await safeRedirect(
`${pathname}.html`,
`${pathname}/`,
configuration,
exists,
skipRedirects
)) || (redirectResult = await safeRedirect(
`${pathname}/index.html`,
`${pathname}/`,
configuration,
exists,
skipRedirects
)) ? redirectResult : notFound(pathname, configuration, exists);
}, htmlHandlingDropTrailingSlash = async (pathname, configuration, exists, skipRedirects) => {
let redirectResult = null, eTagResult = null, exactETag = await exists(pathname);
if (pathname.endsWith("/index")) {
if (exactETag)
return { asset: { eTag: exactETag, status: 200 }, redirect: null };
if (pathname === "/index") {
if (redirectResult = await safeRedirect(
"/index.html",
"/",
configuration,
exists,
skipRedirects
))
return redirectResult;
} else {
if (redirectResult = await safeRedirect(
`${pathname.slice(0, -6)}.html`,
pathname.slice(0, -6),
configuration,
exists,
skipRedirects
))
return redirectResult;
if (redirectResult = await safeRedirect(
`${pathname}.html`,
pathname.slice(0, -6),
configuration,
exists,
skipRedirects
))
return redirectResult;
}
} else if (pathname.endsWith("/index.html"))
if (pathname === "/index.html") {
if (redirectResult = await safeRedirect(
"/index.html",
"/",
configuration,
exists,
skipRedirects
))
return redirectResult;
} else {
if (redirectResult = await safeRedirect(
pathname,
pathname.slice(0, -11),
configuration,
exists,
skipRedirects
))
return redirectResult;
if (exactETag)
return { asset: { eTag: exactETag, status: 200 }, redirect: null };
if (redirectResult = await safeRedirect(
`${pathname.slice(0, -11)}.html`,
pathname.slice(0, -11),
configuration,
exists,
skipRedirects
))
return redirectResult;
}
else if (pathname.endsWith("/"))
if (pathname === "/") {
if (eTagResult = await exists("/index.html"))
return { asset: { eTag: eTagResult, status: 200 }, redirect: null };
} else {
if (redirectResult = await safeRedirect(
`${pathname.slice(0, -1)}.html`,
pathname.slice(0, -1),
configuration,
exists,
skipRedirects
))
return redirectResult;
if (redirectResult = await safeRedirect(
`${pathname.slice(0, -1)}/index.html`,
pathname.slice(0, -1),
configuration,
exists,
skipRedirects
))
return redirectResult;
}
else if (pathname.endsWith(".html")) {
if (redirectResult = await safeRedirect(
pathname,
pathname.slice(0, -5),
configuration,
exists,
skipRedirects
))
return redirectResult;
if (redirectResult = await safeRedirect(
`${pathname.slice(0, -5)}/index.html`,
pathname.slice(0, -5),
configuration,
exists,
skipRedirects
))
return redirectResult;
}
return exactETag ? { asset: { eTag: exactETag, status: 200 }, redirect: null } : (eTagResult = await exists(`${pathname}.html`)) ? { asset: { eTag: eTagResult, status: 200 }, redirect: null } : (eTagResult = await exists(`${pathname}/index.html`)) ? { asset: { eTag: eTagResult, status: 200 }, redirect: null } : notFound(pathname, configuration, exists);
}, htmlHandlingNone = async (pathname, configuration, exists) => {
let exactETag = await exists(pathname);
return exactETag ? { asset: { eTag: exactETag, status: 200 }, redirect: null } : notFound(pathname, configuration, exists);
}, notFound = async (pathname, configuration, exists) => {
switch (configuration.not_found_handling) {
case "single-page-application": {
let eTag = await exists("/index.html");
return eTag ? { asset: { eTag, status: 200 }, redirect: null } : null;
}
case "404-page": {
let cwd = pathname;
for (; cwd; ) {
cwd = cwd.slice(0, cwd.lastIndexOf("/"));
let eTag = await exists(`${cwd}/404.html`);
if (eTag)
return { asset: { eTag, status: 404 }, redirect: null };
}
return null;
}
case "none":
default:
return null;
}
}, safeRedirect = async (file, destination, configuration, exists, skip) => {
if (skip)
return null;
if (!await exists(destination)) {
let intent = await getIntent(destination, configuration, exists, !0);
if (intent?.asset && intent.asset.eTag === await exists(file))
return {
asset: null,
redirect: destination
};
}
return null;
};
// ../workers-shared/asset-worker/src/utils/kv.ts
async function getAssetWithMetadataFromKV(assetsKVNamespace, assetKey, retries = 1) {
let attempts = 0;
for (; attempts <= retries; )
try {
return await t.getWithMetadata(e, { type: "stream", cacheTtl: 31536e3 });
return await assetsKVNamespace.getWithMetadata(assetKey, {
type: "stream",
cacheTtl: 31536e3
// 1 year
});
} catch {
if (s >= n)
throw new Error(`Requested asset ${e} could not be fetched from KV namespace.`);
await new Promise((a) => setTimeout(a, Math.pow(2, s++) * 1e3));
if (attempts >= retries)
throw new Error(
`Requested asset ${assetKey} could not be fetched from KV namespace.`
);
await new Promise(
(resolvePromise) => setTimeout(resolvePromise, Math.pow(2, attempts++) * 1e3)
);
}
}
var l = class extends T {
async fetch(t) {
// ../workers-shared/asset-worker/src/index.ts
var src_default = class extends WorkerEntrypoint {
async fetch(request) {
try {
return this.handleRequest(t);
} catch (e) {
return new d(e);
return handleRequest(
request,
applyConfigurationDefaults(this.env.CONFIG),
this.exists.bind(this),
this.getByETag.bind(this)
);
} catch (err) {
return new InternalServerErrorResponse(err);
}
}
async handleRequest(t) {
let e = await this.getAssetEntry(t);
if (!e)
return new u();
if (t.method.toLowerCase() !== "get")
return new f();
let n = await E(this.env.ASSETS_KV_NAMESPACE, e);
if (!n || !n.value)
throw new Error(`Requested asset ${e} exists in the asset manifest but not in the KV namespace.`);
let { value: s, metadata: r } = n, a = A(e, r, t), o = y(t.headers, a);
return new c(s, { headers: o });
async unstable_canFetch(request) {
let url = new URL(request.url), method = request.method.toUpperCase(), intent = await getIntent(
url.pathname,
{
...applyConfigurationDefaults(this.env.CONFIG),
not_found_handling: "none"
},
this.exists.bind(this)
);
return intent && ["GET", "HEAD"].includes(method) ? new MethodNotAllowedResponse() : intent !== null;
}
async getAssetEntry(t) {
let e = new URL(t.url);
return await new i(this.env.ASSETS_MANIFEST).get(e.pathname);
async getByETag(eTag) {
let asset = await getAssetWithMetadataFromKV(
this.env.ASSETS_KV_NAMESPACE,
eTag
);
if (!asset || !asset.value)
throw new Error(
`Requested asset ${eTag} exists in the asset manifest but not in the KV namespace.`
);
return {
readableStream: asset.value,
contentType: asset.metadata?.contentType ?? "application/octet-stream"
};
}
async getByPathname(pathname) {
let eTag = await this.exists(pathname);
return eTag ? this.getByETag(eTag) : null;
}
async exists(pathname) {
return await new AssetsManifest(this.env.ASSETS_MANIFEST).get(pathname);
}
};
// src/workers/assets/assets.worker.ts
var assets_worker_default = l;
var assets_worker_default = src_default;
export {

@@ -114,0 +544,0 @@ assets_worker_default as default

14

dist/src/workers/assets/router.worker.js

@@ -1,9 +0,11 @@

// ../workers-shared/dist/router-worker.mjs
var s = { async fetch(e, t) {
let R = e.clone(), r = await t.ASSET_WORKER.fetch(e);
return r.status === 404 && t.CONFIG.hasUserWorker ? await t.USER_WORKER.fetch(R) : r;
} };
// ../workers-shared/router-worker/src/index.ts
var src_default = {
async fetch(request, env) {
let maybeSecondRequest = request.clone();
return env.CONFIG.has_user_worker ? await env.ASSET_WORKER.unstable_canFetch(request) ? await env.ASSET_WORKER.fetch(maybeSecondRequest) : env.USER_WORKER.fetch(maybeSecondRequest) : await env.ASSET_WORKER.fetch(request);
}
};
// src/workers/assets/router.worker.ts
var router_worker_default = s;
var router_worker_default = src_default;
export {

@@ -10,0 +12,0 @@ router_worker_default as default

{
"name": "miniflare",
"version": "0.0.0-46a91e7e7",
"version": "0.0.0-48eeff467",
"description": "Fun, full-featured, fully-local simulator for Cloudflare Workers",

@@ -41,3 +41,3 @@ "keywords": [

"undici": "^5.28.4",
"workerd": "1.20240821.1",
"workerd": "1.20240909.0",
"ws": "^8.17.1",

@@ -49,3 +49,3 @@ "youch": "^3.2.2",

"@ava/typescript": "^4.1.0",
"@cloudflare/workers-types": "^4.20240821.1",
"@cloudflare/workers-types": "^4.20240909.0",
"@microsoft/api-extractor": "^7.47.0",

@@ -84,3 +84,3 @@ "@types/debug": "^4.1.7",

"@cloudflare/kv-asset-handler": "0.3.4",
"@cloudflare/workers-shared": "0.0.0-46a91e7e7"
"@cloudflare/workers-shared": "0.0.0-48eeff467"
},

@@ -87,0 +87,0 @@ "engines": {

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

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