Compare commits

..

3 Commits

Author SHA1 Message Date
5b9e0714ce Cache returns deep copies to prevent deletion mid-use
All checks were successful
Build / Build NPM Project (push) Successful in 1m13s
Build / Tag Version (push) Successful in 17s
Build / Publish Documentation (push) Successful in 54s
2025-02-27 08:36:30 -05:00
a3b34ef03f Fixed http no decode
All checks were successful
Build / Build NPM Project (push) Successful in 43s
Build / Tag Version (push) Successful in 8s
Build / Publish Documentation (push) Successful in 40s
2025-02-18 16:04:18 -05:00
f755d8f5b8 Fixed PathEvents filter
All checks were successful
Build / Build NPM Project (push) Successful in 38s
Build / Tag Version (push) Successful in 7s
Build / Publish Documentation (push) Successful in 35s
2025-02-04 22:17:31 -05:00
5 changed files with 36 additions and 17 deletions

View File

@ -9,6 +9,7 @@
import {PathEvent} from './dist/index.mjs';
console.log(PathEvent.filter(['payments/ztimson:cr', 'logs/momentum:c', 'data/Testing:r'], 'data'));
console.log(PathEvent.filter(['data/Submissions/Test:r'], 'data/Submissions/Test/test.html'));
</script>
</body>
</html>

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.23.10",
"version": "0.23.13",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",

View File

@ -1,3 +1,5 @@
import {deepCopy} from './objects.ts';
export type CacheOptions = {
/** Delete keys automatically after x amount of seconds */
ttl?: number;
@ -37,11 +39,11 @@ export class Cache<K extends string | number | symbol, T> {
return new Proxy(this, {
get: (target: this, prop: string | symbol) => {
if(prop in target) return (target as any)[prop];
return target.store[prop as K];
return deepCopy(target.store[prop as K]);
},
set: (target: this, prop: string | symbol, value: any) => {
if(prop in target) (target as any)[prop] = value;
else target.store[prop as K] = value;
else this.set(prop as K, value);
return true;
}
});
@ -58,7 +60,7 @@ export class Cache<K extends string | number | symbol, T> {
* @return {T[]} Array of items
*/
all(): T[] {
return Object.values(this.store);
return deepCopy(Object.values(this.store));
}
/**
@ -119,7 +121,7 @@ export class Cache<K extends string | number | symbol, T> {
* @return {T} Cached item
*/
get(key: K): T {
return this.store[key];
return deepCopy(this.store[key]);
}
/**
@ -137,7 +139,7 @@ export class Cache<K extends string | number | symbol, T> {
* @return {Record<K, T>}
*/
map(): Record<K, T> {
return structuredClone(this.store);
return deepCopy(this.store);
}
/**

View File

@ -22,6 +22,22 @@ export type HttpDefaults = {
url?: string;
}
class HttpResponse<T = any> extends Response {
data?: T
ok!: boolean;
redirected!: boolean;
type!: ResponseType;
url!: string;
constructor(resp: Response, stream: ReadableStream) {
super(stream, {headers: resp.headers, status: resp.status, statusText: resp.statusText});
this.ok = resp.ok;
this.redirected = resp.redirected;
this.type = resp.type;
this.url = resp.url;
}
}
export class Http {
private static interceptors: {[key: string]: HttpInterceptor} = {};
@ -101,18 +117,17 @@ export class Http {
push();
}).catch((error: any) => controller.error(error));
}
push();
}
});
resp.data = new Response(stream);
if(opts.decode == null || opts.decode) {
resp = new HttpResponse<T>(resp, stream);
if(opts.decode !== false) {
const content = resp.headers.get('Content-Type')?.toLowerCase();
if(content?.includes('form')) resp.data = <T>await resp.data.formData();
else if(content?.includes('json')) resp.data = <T>await resp.data.json();
else if(content?.includes('text')) resp.data = <T>await resp.data.text();
else if(content?.includes('application')) resp.data = <T>await resp.data.blob();
if(content?.includes('form')) resp.data = <T>await resp.formData();
else if(content?.includes('json')) resp.data = <T>await resp.json();
else if(content?.includes('text')) resp.data = <T>await resp.text();
else if(content?.includes('application')) resp.data = <T>await resp.blob();
}
if(resp.ok) res(resp);

View File

@ -142,8 +142,9 @@ export class PathEvent {
const parsedFind = makeArray(filter).map(pe => new PathEvent(pe));
return parsedTarget.filter(t => {
if(!t.fullPath && t.all) return true;
return !!parsedFind.find(f => t.fullPath.startsWith(f.fullPath)
&& (f.all || t.all || t.methods.intersection(f.methods).length));
return !!parsedFind.find(f =>
(t.fullPath.startsWith(f.fullPath) || f.fullPath.startsWith(t.fullPath)) &&
(f.all || t.all || t.methods.intersection(f.methods).length));
});
}