Skip to content

Commit

Permalink
fix(cloudflare-worker-r2): exclude 'dom' from libs in example client …
Browse files Browse the repository at this point in the history
…tsconfig
  • Loading branch information
Klowner committed Nov 2, 2022
1 parent 51cc6b9 commit 6d116ad
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
35 changes: 34 additions & 1 deletion examples/cloudflare-worker-r2/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {TussleStorageService} from '@tussle/core';
import {TussleIncomingRequest, TussleStorageService} from '@tussle/core';
import {TussleCloudflareWorker} from '@tussle/middleware-cloudflareworker';
import {TussleStoragePatchFileCompleteResponse} from '@tussle/spec/interface/storage';
import {TussleStateMemory} from '@tussle/state-memory';
import {TussleStorageR2} from '@tussle/storage-r2';
import {R2UploadState} from "@tussle/storage-r2/lib/storage";
Expand All @@ -13,6 +14,24 @@ type UserParams = {
context: ExecutionContext;
}

async function cacheCompletedUploadResponse(
request: Request,
location: string,
offset: number,
) {
const url = new URL(request.url);
url.pathname = location;
console.log('CACHED ' + url.toString());
await caches.default.put(url.toString(), new Response(null, {
headers: {
'Upload-Offset': offset.toString(10),
'Upload-Length': offset.toString(10),
'Tus-Resumable': '1.0.0',
'Cache-Control': 'max-age=604800',
},
}));
}

const getTussleMiddleware = (() => {
let instance: TussleCloudflareWorker<UserParams>;
return (storage: TussleStorageService) => {
Expand All @@ -35,6 +54,11 @@ const getTussleMiddleware = (() => {
path,
};
},
"after-complete": async (ctx, params) => {
const { location, offset } = params;
await cacheCompletedUploadResponse(ctx.originalRequest, location, offset);
return params;
},
},
core: {
storage,
Expand All @@ -50,6 +74,15 @@ async function handleRequest(
bindings: Bindings,
context: ExecutionContext,
) {
if (request.method === 'HEAD') {
console.log('looking for cache', request.url);
const cache = await caches.default.match(request.url);
console.log({cache});
if (cache) {
return cache;
}
}

const storage = new TussleStorageR2({
stateService,
bucket: bindings.TUSSLE_BUCKET,
Expand Down
11 changes: 11 additions & 0 deletions examples/cloudflare-worker-r2/tsconfig.client.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"lib": ["esnext", "dom"]
},
"include": [
"types/**/*",
"src/client.ts"
],
"exclude": []
}
7 changes: 5 additions & 2 deletions examples/cloudflare-worker-r2/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": ["esnext", "dom"],
"lib": ["esnext"],
"types": ["@cloudflare/workers-types"],
"moduleResolution": "node",
"strict": true,
Expand All @@ -14,5 +14,8 @@
"include": [
"types/**/*",
"src/**/*"
]
],
"exclude": [
"src/**/client.ts"
]
}
1 change: 1 addition & 0 deletions examples/cloudflare-worker-r2/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ r2_buckets = [

[miniflare]
r2_persist = "./data/r2"
cache_persist = "./data/cache"
7 changes: 3 additions & 4 deletions packages/core/src/handlers/patch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {Observable, pipe, throwError} from 'rxjs';
import type {Tussle} from '../core';
import type {TussleIncomingRequest} from '@tussle/spec/interface/request';
import type {TussleStoragePatchFileResponse, TussleStoragePatchFileCompleteResponse} from '@tussle/spec/interface/storage';
import {of, from as observableFrom} from 'rxjs';
import type {TussleStoragePatchFileCompleteResponse, TussleStoragePatchFileResponse} from '@tussle/spec/interface/storage';
import {from as observableFrom, Observable, of, throwError} from 'rxjs';
import {map, switchMap} from 'rxjs/operators';
import type {Tussle} from '../core';

export default function handlePatch<T, P>(
_core: Tussle,
Expand Down

0 comments on commit 6d116ad

Please sign in to comment.