-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the stream-compression package from wp-playground to php-wasm (#895
) Stream Compression introduced in #851 has no dependencies on WordPress and can be used in any JavaScript project. It also makes sense as a dependency for some `@php-wasm` packages. This commit, therefore, moves it from the `wp-playground` to the `php-wasm` npm namespace, making it reusable across the entire project. In addition, this adds a new `iterateFiles` function to the `@php-wasm/universal` package, which allows iterating over the files in the PHP filesystem. It uses the `stream-compression` package, which was some of the motivation for the move. This PR also ships eslint rules to keep the `stream-compression` package independent from the heavy `@php-wasm/web` and `@php-wasm/node` packages. This should enable using it in other project with a minimal dependency overhead of just `@php-wasm/util` and `@php-wasm/node-polyfills`. ## Testing instructions Since the package isn't used anywhere yet, only confirm if the CI checks pass.
- Loading branch information
Showing
49 changed files
with
202 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -161,5 +161,5 @@ | |
} | ||
} | ||
}, | ||
"tags": [] | ||
"tags": ["scope:php-binaries"] | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# php-wasm-stream-compression | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
|
||
## Building | ||
|
||
Run `nx build php-wasm-stream-compression` to build the library. | ||
|
||
## Running unit tests | ||
|
||
Run `nx test php-wasm-stream-compression` to execute the unit tests via [Jest](https://jestjs.io). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 7 additions & 7 deletions
14
...layground/stream-compression/project.json → .../php-wasm/stream-compression/project.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
{ | ||
"name": "playground-stream-compression", | ||
"name": "php-wasm-stream-compression", | ||
"$schema": "../../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "packages/playground/stream-compression/src", | ||
"sourceRoot": "packages/php-wasm/stream-compression/src", | ||
"projectType": "library", | ||
"targets": { | ||
"build": { | ||
"executor": "@nx/vite:build", | ||
"outputs": ["{options.outputPath}"], | ||
"options": { | ||
"outputPath": "dist/packages/playground/stream-compression" | ||
"outputPath": "dist/packages/php-wasm/stream-compression" | ||
} | ||
}, | ||
"test": { | ||
"executor": "@nx/vite:test", | ||
"outputs": ["{options.reportsDirectory}"], | ||
"options": { | ||
"passWithNoTests": true, | ||
"reportsDirectory": "../../../coverage/packages/playground/stream-compression" | ||
"reportsDirectory": "../../../coverage/packages/php-wasm/stream-compression" | ||
} | ||
}, | ||
"lint": { | ||
"executor": "@nx/linter:eslint", | ||
"outputs": ["{options.outputFile}"], | ||
"options": { | ||
"lintFilePatterns": [ | ||
"packages/playground/stream-compression/**/*.ts", | ||
"packages/playground/stream-compression/package.json" | ||
"packages/php-wasm/stream-compression/**/*.ts", | ||
"packages/php-wasm/stream-compression/package.json" | ||
] | ||
} | ||
} | ||
}, | ||
"tags": [] | ||
"tags": ["scope:independent-from-php-binaries"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
58 changes: 58 additions & 0 deletions
58
packages/php-wasm/stream-compression/src/utils/streamed-file.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { collectBytes } from './collect-bytes'; | ||
|
||
/** | ||
* Represents a file that is streamed and not fully | ||
* loaded into memory. | ||
*/ | ||
export class StreamedFile extends File { | ||
/** | ||
* Creates a new StreamedFile instance. | ||
* | ||
* @param readableStream The readable stream containing the file data. | ||
* @param name The name of the file. | ||
* @param type The MIME type of the file. | ||
*/ | ||
constructor( | ||
private readableStream: ReadableStream<Uint8Array>, | ||
name: string, | ||
type?: string | ||
) { | ||
super([], name, { type }); | ||
} | ||
|
||
/** | ||
* Overrides the slice() method of the File class. | ||
* | ||
* @returns A Blob representing a portion of the file. | ||
*/ | ||
override slice(): Blob { | ||
throw new Error('slice() is not possible on a StreamedFile'); | ||
} | ||
|
||
/** | ||
* Returns the readable stream associated with the file. | ||
* | ||
* @returns The readable stream. | ||
*/ | ||
override stream() { | ||
return this.readableStream; | ||
} | ||
|
||
/** | ||
* Loads the file data into memory and then returns it as a string. | ||
* | ||
* @returns File data as text. | ||
*/ | ||
override async text() { | ||
return new TextDecoder().decode(await this.arrayBuffer()); | ||
} | ||
|
||
/** | ||
* Loads the file data into memory and then returns it as an ArrayBuffer. | ||
* | ||
* @returns File data as an ArrayBuffer. | ||
*/ | ||
override async arrayBuffer() { | ||
return await collectBytes(this.stream()); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { joinPaths, normalizePath } from '@php-wasm/util'; | ||
import { StreamedFile } from '@php-wasm/stream-compression'; | ||
import { UniversalPHP } from './universal-php'; | ||
import { streamReadFileFromPHP } from './stream-read-file-from-php'; | ||
|
||
export type IteratePhpFilesOptions = { | ||
/** | ||
* Should yield paths relative to the root directory? | ||
* If false, all paths will be absolute. | ||
*/ | ||
relativePaths?: boolean; | ||
|
||
/** | ||
* A prefix to add to all paths. | ||
* Only used if `relativePaths` is true. | ||
*/ | ||
pathPrefix?: string; | ||
|
||
/** | ||
* A list of paths to exclude from the results. | ||
*/ | ||
exceptPaths?: string[]; | ||
}; | ||
|
||
/** | ||
* Iterates over all files in a php directory and its subdirectories. | ||
* | ||
* @param php - The PHP instance. | ||
* @param root - The root directory to start iterating from. | ||
* @param options - Optional configuration. | ||
* @returns All files found in the tree. | ||
*/ | ||
export async function* iteratePhpFiles( | ||
php: UniversalPHP, | ||
root: string, | ||
{ | ||
relativePaths = true, | ||
pathPrefix, | ||
exceptPaths = [], | ||
}: IteratePhpFilesOptions = {} | ||
): AsyncGenerator<File> { | ||
root = normalizePath(root); | ||
const stack: string[] = [root]; | ||
while (stack.length) { | ||
const currentParent = stack.pop(); | ||
if (!currentParent) { | ||
return; | ||
} | ||
const files = await php.listFiles(currentParent); | ||
for (const file of files) { | ||
const absPath = `${currentParent}/${file}`; | ||
if (exceptPaths.includes(absPath.substring(root.length + 1))) { | ||
continue; | ||
} | ||
const isDir = await php.isDir(absPath); | ||
if (isDir) { | ||
stack.push(absPath); | ||
} else { | ||
yield new StreamedFile( | ||
streamReadFileFromPHP(php, absPath), | ||
relativePaths | ||
? joinPaths( | ||
pathPrefix || '', | ||
absPath.substring(root.length + 1) | ||
) | ||
: absPath | ||
); | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/php-wasm/universal/src/lib/stream-read-file-from-php.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { UniversalPHP } from './universal-php'; | ||
|
||
/** | ||
* Reads a file from PHP filesystem using a stream. | ||
*/ | ||
export function streamReadFileFromPHP(php: UniversalPHP, path: string) { | ||
return new ReadableStream({ | ||
async pull(controller) { | ||
const buffer = await php.readFileAsBuffer(path); | ||
controller.enqueue(buffer); | ||
controller.close(); | ||
}, | ||
}); | ||
} |
4 changes: 2 additions & 2 deletions
4
...pression/src/utils/stream-write-to-php.ts → ...rsal/src/lib/write-files-stream-to-php.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,5 +38,5 @@ | |
} | ||
} | ||
}, | ||
"tags": [] | ||
"tags": ["scope:independent-from-php-binaries"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,5 +132,5 @@ | |
} | ||
} | ||
}, | ||
"tags": [] | ||
"tags": ["scope:php-binaries"] | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.