Skip to content

Store remote assets as zip #1527

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

Closed
Closed
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project are documented in this file by a CI job
that runs on every NPM release. The file follows the [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
format.

## Unreleased




## [v0.7.20] (2024-05-21)

### **Breaking Changes**
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"useWorkspaces": true,
"version": "0.7.20",
"version": "0.9.0",
"useNx": true
}
32 changes: 16 additions & 16 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion packages/docs/site/docs/13-contributing/02-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ npx @wp-now/wp-now start --port=80
There are a few ways to set up a local test domain, including editing your `hosts` file. If you're unsure how to do that, we suggest installing [Laravel Valet](https://laravel.com/docs/11.x/valet) and then running the following command:

```bash
valet proxy playground.test http://localhost:5400 --secure
valet proxy playground.test http://127.0.0.1:5400 --secure
```

Your dev server is now available on https://playground.test.
Expand Down
5 changes: 5 additions & 0 deletions packages/docs/site/docs/17-changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ All notable changes to this project are documented in this file by a CI job
that runs on every NPM release. The file follows the [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
format.

## Unreleased




## [v0.7.20] (2024-05-21)

### **Breaking Changes**
Expand Down
2 changes: 1 addition & 1 deletion packages/php-wasm/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@php-wasm/cli",
"version": "0.7.20",
"version": "0.9.0",
"description": "PHP.wasm CLI for node.js",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/php-wasm/compile/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ RUN set -euxo pipefail; \
# Without this, browsers will try the wasm:// protocol and never load the source map.
if [ "$EMSCRIPTEN_ENVIRONMENT" = "web" ]; then \
export PHP_VERSION_ESCAPED="${PHP_VERSION//./_}"; \
echo -n ' --source-map-base http://localhost:5400/@fs/Users/cloudnik/www/Automattic/core/plugins/playground/packages/php-wasm/web/public/${PHP_VERSION_ESCAPED}/' >> /root/.emcc-php-wasm-flags; \
echo -n ' --source-map-base http://127.0.0.1:5400/@fs/Users/cloudnik/www/Automattic/core/plugins/playground/packages/php-wasm/web/public/${PHP_VERSION_ESCAPED}/' >> /root/.emcc-php-wasm-flags; \
fi; \
else \
# Preserve symbol names in node.js build – the bundle size doesn't matter as much
Expand Down
2 changes: 1 addition & 1 deletion packages/php-wasm/fs-journal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@php-wasm/fs-journal",
"version": "0.7.20",
"version": "0.9.0",
"description": "Bindings to journal the PHP filesystem",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/php-wasm/logger/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@php-wasm/logger",
"version": "0.7.20",
"version": "0.9.0",
"description": "A logger for PHP-wasm clients like Playground and WP-now.",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/php-wasm/node-polyfills/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@php-wasm/node-polyfills",
"version": "0.7.20",
"version": "0.9.0",
"description": "PHP.wasm – polyfills for Node.js",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/php-wasm/node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@php-wasm/node",
"version": "0.7.20",
"version": "0.9.0",
"description": "PHP.wasm for Node.js",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/php-wasm/progress/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@php-wasm/progress",
"version": "0.7.20",
"version": "0.9.0",
"description": "PHP.wasm – loading progress monitoring",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/php-wasm/scopes/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@php-wasm/scopes",
"version": "0.7.20",
"version": "0.9.0",
"description": "PHP.wasm – scoped URLs utils",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/php-wasm/universal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@php-wasm/universal",
"version": "0.7.20",
"version": "0.9.0",
"description": "PHP.wasm – emscripten bindings for PHP",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/php-wasm/util/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@php-wasm/util",
"version": "0.7.20",
"version": "0.9.0",
"type": "commonjs",
"typedoc": {
"entryPoint": "./src/index.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/php-wasm/web-service-worker/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@php-wasm/web-service-worker",
"version": "0.7.20",
"version": "0.9.0",
"description": "PHP.wasm – service worker utils",
"repository": {
"type": "git",
Expand Down
35 changes: 34 additions & 1 deletion packages/php-wasm/web-service-worker/src/fetch-caching.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { logger } from '@php-wasm/logger';
import { decodeZip } from '@php-wasm/stream-compression';

const wpCacheKey = 'playground-cache';

Expand Down Expand Up @@ -34,6 +35,12 @@ export const getCache = async (key: string) => {
const validHostnames = ['playground.wordpress.net', '127.0.0.1', 'localhost'];

const isValidHostname = (url: URL) => {
if (url.pathname.startsWith('/website-server/@')) {
return false;
}
if (url.href.includes('?html-proxy')) {
return false;
}
return validHostnames.includes(url.hostname);
};

Expand All @@ -47,7 +54,7 @@ export const cachedFetch = async (request: Request): Promise<Response> => {
return new Response('Failed to fetch', { status: 500 });
}
}
const cacheKey = url.pathname;
const cacheKey = url.href;
const cache = await getCache(cacheKey);
if (cache) {
return cache;
Expand All @@ -67,3 +74,29 @@ export const precacheResources = async (): Promise<any> => {
.open(wpCacheKey)
.then((cache) => cache.addAll(precachedResources));
};

export const preloadStaticAssets = async () => {
cachedFetch(
new Request('/wp-nightly/wordpress-static.zip', {
method: 'GET',
headers: {
'Content-Type': 'application/zip',
},
})
).then(async (response) => {
const zipBytes = await response.arrayBuffer();
const zipStream = decodeZip(new Blob([zipBytes]).stream());
const files = [];
for await (const file of zipStream) {
files.push(file);
}
for (const file of files) {
const url = new URL(
file.name.replace('/wordpress-static', ''),
self.location.origin
);
const content = new Uint8Array(await file.arrayBuffer());
addCache(url.pathname, new Response(content));
}
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ declare const self: ServiceWorkerGlobalScope;

import { awaitReply, getNextRequestId } from './messaging';
import { getURLScope, isURLScoped, setURLScope } from '@php-wasm/scopes';
import { cachedFetch, precacheResources } from './fetch-caching';
import {
cachedFetch,
precacheResources,
preloadStaticAssets,
} from './fetch-caching';
import { logger } from '@php-wasm/logger';

/**
Expand All @@ -15,8 +19,10 @@ import { logger } from '@php-wasm/logger';
export function initializeServiceWorker(config: ServiceWorkerConfiguration) {
const { handleRequest = defaultRequestHandler } = config;

// Preload static assets from zip on install
self.addEventListener('install', (event) => {
try {
preloadStaticAssets();
const precachePromise = precacheResources();
event.waitUntil(precachePromise);
} catch (e) {
Expand All @@ -28,7 +34,7 @@ export function initializeServiceWorker(config: ServiceWorkerConfiguration) {
* The main method. It captures the requests and loop them back to the
* Worker Thread using the Loopback request
*/
self.addEventListener('fetch', (event) => {
self.addEventListener('fetch', async (event) => {
const url = new URL(event.request.url);

// Don't handle requests to the service worker script itself.
Expand Down Expand Up @@ -189,19 +195,6 @@ interface ServiceWorkerConfiguration {
handleRequest?: (event: FetchEvent) => Promise<Response> | undefined;
}

/**
* Copy a request with custom overrides.
*
* This function is only needed because Request properties
* are read-only. The only way to change e.g. a URL is to
* create an entirely new request:
*
* https://developer.mozilla.org/en-US/docs/Web/API/Request
*
* @param request
* @param overrides
* @returns The new request.
*/
export async function cloneRequest(
request: Request,
overrides: Record<string, any>
Expand Down
2 changes: 1 addition & 1 deletion packages/php-wasm/web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@php-wasm/web",
"version": "0.7.20",
"version": "0.9.0",
"description": "PHP.wasm for the web",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/blueprints/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wp-playground/blueprints",
"version": "0.7.20",
"version": "0.9.0",
"exports": {
".": {
"import": "./index.js",
Expand Down
Loading
Loading