Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Optimize file access times #51

Merged
merged 2 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions dist/drill.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! drill.js - v5.3.4 https://github.com/kirakiray/drill.js (c) 2018-2023 YAO
//! drill.js - v5.3.5 https://github.com/kirakiray/drill.js (c) 2018-2024 YAO
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
Expand Down Expand Up @@ -39,12 +39,16 @@
}

const caches = new Map();
const wrapFetch = async (url) => {
let fetchObj = caches.get(url);
const wrapFetch = async (url, params) => {
const d = new URL(url);

const reUrl = params.includes("-direct") ? url : `${d.origin}${d.pathname}`;

let fetchObj = caches.get(reUrl);

if (!fetchObj) {
fetchObj = fetch(url);
caches.set(url, fetchObj);
fetchObj = fetch(reUrl);
caches.set(reUrl, fetchObj);
}

const resp = await fetchObj;
Expand Down Expand Up @@ -106,11 +110,11 @@

use(["txt", "html", "htm"], async (ctx, next) => {
if (!ctx.result) {
const { url } = ctx;
const { url, params } = ctx;

let resp;
try {
resp = await wrapFetch(url);
resp = await wrapFetch(url, params);
} catch (error) {
throw wrapError(`Load ${url} failed`, error);
}
Expand All @@ -127,19 +131,19 @@

use("json", async (ctx, next) => {
if (!ctx.result) {
const { url } = ctx;
const { url, params } = ctx;

ctx.result = await wrapFetch(url).then((e) => e.json());
ctx.result = await wrapFetch(url, params).then((e) => e.json());
}

await next();
});

use("wasm", async (ctx, next) => {
if (!ctx.result) {
const { url } = ctx;
const { url, params } = ctx;

const data = await wrapFetch(url).then((e) => e.arrayBuffer());
const data = await wrapFetch(url, params).then((e) => e.arrayBuffer());

const module = await WebAssembly.compile(data);
const instance = new WebAssembly.Instance(module);
Expand All @@ -152,7 +156,7 @@

use("css", async (ctx, next) => {
if (!ctx.result) {
const { url, element } = ctx;
const { url, element, params } = ctx;

if (element) {
const link = document.createElement("link");
Expand All @@ -176,7 +180,7 @@
})
);
} else {
ctx.result = await wrapFetch(url).then((e) => e.text());
ctx.result = await wrapFetch(url, params).then((e) => e.text());
}
}

Expand Down
4 changes: 2 additions & 2 deletions dist/drill.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/drill.min.js.map

Large diffs are not rendered by default.

Loading
Loading