Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
azjezz committed Nov 19, 2019
0 parents commit 02fbf39
Show file tree
Hide file tree
Showing 31 changed files with 1,227 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.hack linguist-language=Hack

/tests export-ignore
/docs export-ignore
.travis.sh export-ignore
.travis.yml export-ignore
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
vendor/
*.hhast.parser-cache
5 changes: 5 additions & 0 deletions .hhconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
assume_php=false
safe_array = true
safe_vector_array = true
unsafe_rx = false
ignored_paths = [ "vendor/.+/tests/.+", "vendor/.+/bin/.+" ]
30 changes: 30 additions & 0 deletions .travis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/sh
set -ex
apt update -y
DEBIAN_FRONTEND=noninteractive apt install -y php-cli zip unzip
hhvm --version
php --version
if [ ! -e .git/refs/heads/master ]; then
# - Travis clones with `--branch`, then moves to a detached HEAD state
# - if we're on a detached HEAD, Composer uses master to resolve branch
# aliases.
# So, create the master branch :p
git branch master HEAD
fi

(
cd $(mktemp -d)
curl https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
)

if (hhvm --version | grep -q -- -dev); then
# Doesn't exist in master, but keep it here so that we can test release
# branches on nightlies too
rm -f composer.lock
fi

composer --version

composer install

composer check
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
sudo: false
dist: trusty
language: generic
services: docker
env:
- HHVM_VERSION=4.25-latest
- HHVM_VERSION=latest
- HHVM_VERSION=nightly
matrix:
allow_failures:
- env: HHVM_VERSION=nightly
install:
- docker pull hhvm/hhvm:$HHVM_VERSION
script:
- docker run --rm -w /var/source -v $(pwd):/var/source hhvm/hhvm:$HHVM_VERSION ./.travis.sh
notifications:
slack:
secure: fZ74Yt9xxelrIZiWBe74X6zCNV+RCbI2aD0EjB8P6d3ovIdyHc+JOe/AFDwUwU8tuKBYt1DpiMnw/rFQQwu2Y6CQnAjgGtG+ScCCVhy5OJvHqFTmMt/XMs9Hrgdylak3IofaI6D/4Du+E9ZMXHgXGVgjQQr0SNMsj1s70sSd97oiW4t4Kn5hxlAbZK7EWCs2BWwyTtVJD96UOEJrBK59lD0wQvfv0wSV948Wwnms70cPgO26Fa+pdBGsv4Ho475Dzu/y4JuO/kqMMzodZtMSm7FNDrppwqgX3qYkfGBI/foQ5IpBn5gGcG5w3RhZLXsNhLDrULcEHtF1Ptfo5PQGUArN8KPRf91Mju2CGICg3wy6GMEm+iXHdPWzUkCaQPhw4ty6ix+fm2ELatXW4BGGXANJzL6UNUGuQPh4Z2oVeX8zEFpUAA+PJRzd6FPYdQDdI3Xj8P445x/KQ+Mg4f2wCR/YKTkjWbkYKzqvjvssgrDGkbQfhXWAOr5/NgKj0/vRovT66Tra14UncjZdM6yHUOqeMq5KfDboEBXoj7+jZG1cQmtSErUoFF2CUyI/Jqva7symsJbjOYSVTKv6BAgoL+CncdLcTPGFLzavLiavkqp4Gd3ErWoeOWqFY0ZYByY4cLcnwLtL0TrY6Y9fdzFeJLmN6xhxTteuw3Ils66K/fw=
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018-2019 Saif Eddin Gmati

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<p align="center"><img src="https://avatars3.githubusercontent.com/u/45311177?s=200&v=4"></p>

<p align="center">
<a href="https://travis-ci.org/nuxed/asset"><img src="https://travis-ci.org/nuxed/asset.svg" alt="Build Status"></a>
<a href="https://packagist.org/packages/nuxed/asset"><img src="https://poser.pugx.org/nuxed/asset/d/total.svg" alt="Total Downloads"></a>
<a href="https://packagist.org/packages/nuxed/asset"><img src="https://poser.pugx.org/nuxed/asset/v/stable.svg" alt="Latest Stable Version"></a>
<a href="https://packagist.org/packages/nuxed/asset"><img src="https://poser.pugx.org/nuxed/asset/license.svg" alt="License"></a>
</p>

# Nuxed Asset

The Nuxed Asset component manages URL generation and versioning of web assets such as CSS stylesheets, JavaScript files and image files.

### Installation

This package can be installed with [Composer](https://getcomposer.org).

```console
$ composer require nuxed/asset
```

### Example

```hack
use namespace Nuxed\Asset;
use namespace Nuxed\Asset\VersionStrategy;
<<__EntryPoint>>
async function main(): Awaitable<void> {
// For example, the manifest file might look like this:
// {
// "main.js": "main.abc123.js",
// "css/styles.css": "css/styles.555abc.css"
// }
$versionStrategy = new VersionStrategy\JsonManifestVersionStrategy('/path/to/manifest.json');
$cdns = vec[
'https://asset-s1.example.com/',
'https://asset-s2.example.com/',
];
$package = new Asset\UrlPackage($cdns, $version);
// https://asset-s2.example.com/css/styles.555abc.css
echo await $package->getUrl('css/styles.css');
}
```

---

### Security

For information on reporting security vulnerabilities in Nuxed, see [SECURITY.md](SECURITY.md).

---

### License

Nuxed is open-sourced software licensed under the MIT-licensed.
7 changes: 7 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Security

If you discover a security vulnerability within Nuxed, please send an e-mail to Saif Eddin Gmati via [email protected].

Please withhold public disclosure until after we have addressed the vulnerability.

There are no hard and fast rules to determine if a bug is worth reporting as a security issue.
37 changes: 37 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "nuxed/asset",
"type": "library",
"description": "Nuxed Asset",
"license": "MIT",
"authors": [
{
"name": "azjezz",
"email": "[email protected]"
},
{
"name": "Nuxed Community",
"homepage": "https://github.com/nuxed/asset/graphs/contributors"
}
],
"require": {
"hhvm": "^4.25",
"hhvm/hsl": "^4.25",
"nuxed/filesystem": "^1.2",
"nuxed/json": "^1.0"
},
"require-dev": {
"facebook/fbexpect": "^2.7",
"hhvm/hacktest": "^2.0",
"hhvm/hhast": "^4.25"
},
"scripts": {
"check": [
"@type-check",
"@lint",
"@test"
],
"lint": "hhast-lint -v src/",
"test": "hacktest tests/",
"type-check": "hh_client src/"
}
}
9 changes: 9 additions & 0 deletions hh_autoload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"roots": [
"src/"
],
"devRoots": [
"tests/"
],
"devFailureHandler": "Facebook\\AutoloadMap\\HHClientFallbackHandler"
}
9 changes: 9 additions & 0 deletions hhast-lint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"roots": [ "src/" ],
"builtinLinters": "all",
"disableAllAutoFixes": false,
"disabledLinters": [
"Facebook\\HHAST\\Linters\\AsyncFunctionAndMethodLinter"
],
"overrides": []
}
19 changes: 19 additions & 0 deletions src/Nuxed/Asset/Context/Context.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Nuxed\Asset\Context;

class Context implements IContext {
public function __construct(private string $basePath, private bool $secure) {}

/**
* {@inheritdoc}
*/
public function getBasePath(): string {
return $this->basePath;
}

/**
* {@inheritdoc}
*/
public function isSecure(): bool {
return $this->secure;
}
}
17 changes: 17 additions & 0 deletions src/Nuxed/Asset/Context/IContext.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Nuxed\Asset\Context;

interface IContext {
/**
* Gets the base path.
*
* @return string The base path
*/
public function getBasePath(): string;

/**
* Checks whether the request is secure or not.
*
* @return bool true if the request is secure, false otherwise
*/
public function isSecure(): bool;
}
17 changes: 17 additions & 0 deletions src/Nuxed/Asset/Context/NullContext.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Nuxed\Asset\Context;

class NullContext implements IContext {
/**
* {@inheritdoc}
*/
public function getBasePath(): string {
return '';
}

/**
* {@inheritdoc}
*/
public function isSecure(): bool {
return false;
}
}
5 changes: 5 additions & 0 deletions src/Nuxed/Asset/Exception/IException.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Nuxed\Asset\Exception;

interface IException {
require extends \Exception;
}
6 changes: 6 additions & 0 deletions src/Nuxed/Asset/Exception/InvalidArgumentException.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Nuxed\Asset\Exception;

final class InvalidArgumentException
extends \InvalidArgumentException
implements IException {
}
4 changes: 4 additions & 0 deletions src/Nuxed/Asset/Exception/LogicException.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Nuxed\Asset\Exception;

final class LogicException extends \LogicException implements IException {
}
4 changes: 4 additions & 0 deletions src/Nuxed/Asset/Exception/RuntimeException.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Nuxed\Asset\Exception;

final class RuntimeException extends \RuntimeException implements IException {
}
13 changes: 13 additions & 0 deletions src/Nuxed/Asset/IPackage.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Nuxed\Asset;

interface IPackage {
/**
* Returns the asset version for an asset.
*/
public function getVersion(string $asset): Awaitable<string>;

/**
* Returns an absolute or root-relative public path.
*/
public function getUrl(string $path): Awaitable<string>;
}
44 changes: 44 additions & 0 deletions src/Nuxed/Asset/Package.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace Nuxed\Asset;

use namespace HH\Lib\Str;

/**
* Basic package that adds a version to asset URLs.
*/
class Package implements IPackage {
public function __construct(
private VersionStrategy\IVersionStrategy $versionStrategy,
private Context\IContext $context = new Context\NullContext(),
) {
}

/**
* {@inheritdoc}
*/
public async function getVersion(string $asset): Awaitable<string> {
return await $this->versionStrategy->getVersion($asset);
}

/**
* {@inheritdoc}
*/
public async function getUrl(string $path): Awaitable<string> {
if ($this->isAbsoluteUrl($path)) {
return $path;
}

return await $this->versionStrategy->applyVersion($path);
}

protected function getContext(): Context\IContext {
return $this->context;
}

protected function getVersionStrategy(): VersionStrategy\IVersionStrategy {
return $this->versionStrategy;
}

protected function isAbsoluteUrl(string $url): bool {
return Str\contains($url, '://') || Str\starts_with($url, '//');
}
}
Loading

0 comments on commit 02fbf39

Please sign in to comment.