Skip to content

Commit

Permalink
no hash for sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Sep 15, 2021
1 parent 8f0c6ee commit e5bc34e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 52 deletions.
24 changes: 1 addition & 23 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import typescript from '@rollup/plugin-typescript';
import { basename, join } from 'path';
import { createHash } from 'crypto';
import {
copySync,
emptyDir,
Expand Down Expand Up @@ -28,7 +27,6 @@ export default async function (cmdArgs) {
const reactBuildDir = join(rootDir, 'react');
const cacheDir = join(rootDir, '.cache');
const cache = {};
let sandboxHash = '';

const minOpts = {
compress: {
Expand Down Expand Up @@ -184,11 +182,6 @@ export default async function (cmdArgs) {

const sandboxCode = isDev ? '' : await getSandbox(false);

if (!isDev) {
sandboxHash = createHash('sha1').update(sandboxCode).digest('hex');
sandboxHash = sandboxHash.substr(0, 6).toLowerCase();
}

return {
input: join(srcLibDir, 'service-worker', 'index.ts'),
output,
Expand Down Expand Up @@ -219,11 +212,6 @@ export default async function (cmdArgs) {
if (id === '@sandbox') {
return `const Sandbox = ${JSON.stringify(sandboxCode)}; export default Sandbox;`;
}
if (id === '@sandbox-hash') {
return `const SandboxHash = ${JSON.stringify(
sandboxHash
)}; export default SandboxHash;`;
}
if (id === '@sandbox-debug') {
return `const SandboxDebug = ${JSON.stringify(
await getSandbox(true)
Expand Down Expand Up @@ -267,16 +255,6 @@ export default async function (cmdArgs) {
outputToFilesystem: false,
}),
{
resolveId(id) {
if (id.startsWith('@')) return id;
},
async load(id) {
if (id === '@sandbox-hash') {
return `const SandboxHash = ${JSON.stringify(
sandboxHash
)}; export default SandboxHash;`;
}
},
writeBundle() {
copySync(buildDir, testsBuildDir);
},
Expand Down Expand Up @@ -350,7 +328,7 @@ export default async function (cmdArgs) {
}

return {
input: join(srcReactDir, 'index.tsx'),
input: join(srcReactDir, 'index.ts'),
output: [
{
file: join(reactBuildDir, 'index.cjs'),
Expand Down
5 changes: 2 additions & 3 deletions src/lib/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { debug, PT_INITIALIZED_EVENT, PT_SCRIPT_TYPE } from '../utils';
import type { MainWindow } from '../types';
import SandboxHash from '@sandbox-hash';

(function (
doc: Document,
Expand All @@ -13,10 +12,10 @@ import SandboxHash from '@sandbox-hash';
function ready() {
if (!sandbox) {
sandbox = doc.createElement('iframe');
sandbox.dataset.partytown = 'sandbox';
sandbox.setAttribute('style', 'display:block;width:0;height:0;border:0;visibility:hidden');
sandbox.setAttribute('aria-hidden', 'true');
sandbox.src =
scope + 'partytown-sandbox' + (debug ? '.debug' : '-' + SandboxHash) + '?' + Date.now();
sandbox.src = scope + 'partytown-sandbox' + (debug ? '.debug?' : '?') + Date.now();
doc.body.appendChild(sandbox);
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/lib/modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ declare module '@sandbox' {
export default str;
}

declare module '@sandbox-hash' {
const str: string;
export default str;
}

declare module '@sandbox-debug' {
const str: string;
export default str;
Expand Down
24 changes: 8 additions & 16 deletions src/lib/service-worker/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
import { httpRequestFromWebWorker } from './sw-message';
import { CacheControl, ContentType, response } from './response';
import { debug, PT_PROXY_URL } from '../utils';
import { httpRequestFromWebWorker } from './sw-message';
import Sandbox from '@sandbox';
import SandboxHash from '@sandbox-hash';
import SandboxDebug from '@sandbox-debug';

export const onFetchServiceWorkerRequest = (self: ServiceWorkerGlobalScope, ev: FetchEvent) => {
const req = ev.request;
const pathname = new URL(req.url).pathname;

if (debug && pathname.endsWith('partytown-sandbox.debug')) {
ev.respondWith(
new Response(SandboxDebug, {
headers: { 'content-type': 'text/html', 'Cache-Control': 'no-store' },
})
);
} else if (!debug && pathname.endsWith('partytown-sandbox-' + SandboxHash)) {
ev.respondWith(
new Response(Sandbox, {
headers: {
'content-type': 'text/html',
'Cache-Control': 'max-age=31556952, immutable',
},
})
);
// debug version (sandbox and web worker are not inlined)
ev.respondWith(response(SandboxDebug, ContentType.HTML, CacheControl.NoStore));
} else if (!debug && pathname.endsWith('partytown-sandbox')) {
// sandbox and webworker, minified and inlined
ev.respondWith(response(Sandbox, ContentType.HTML, CacheControl.NoStore));
} else if (pathname.endsWith(PT_PROXY_URL)) {
// proxy request
ev.respondWith(httpRequestFromWebWorker(self, req));
}
};
18 changes: 18 additions & 0 deletions src/lib/service-worker/response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const response = (body: string, contentType: ContentType, cacheControl: CacheControl) =>
new Response(body, {
headers: {
'content-type': contentType === ContentType.JSON ? 'application/json' : 'text/html',
'Cache-Control':
cacheControl === CacheControl.Immutable ? 'max-age=31556952,immutable' : 'no-store',
},
});

export const enum ContentType {
HTML,
JSON,
}

export const enum CacheControl {
Immutable,
NoStore,
}
7 changes: 2 additions & 5 deletions src/lib/service-worker/sw-message.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { MainAccessRequest, MainAccessResponse } from '../types';
import { debug } from '../utils';
import { CacheControl, ContentType, response } from './response';

const resolves = new Map<number, MessageResolve>();

Expand Down Expand Up @@ -57,11 +58,7 @@ export const httpRequestFromWebWorker = (self: ServiceWorkerGlobalScope, req: Re
const accessReq: MainAccessRequest = await req.clone().json();
const responseData = await sendMessageToSandboxFromServiceWorker(self, accessReq);

resolve(
new Response(JSON.stringify(responseData), {
headers: { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' },
})
);
resolve(response(JSON.stringify(responseData), ContentType.JSON, CacheControl.Immutable));
});

type MessageResolve = [(data?: any) => void, any];

0 comments on commit e5bc34e

Please sign in to comment.