Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
megahertz committed Jan 14, 2022
0 parents commit 8ec7f8c
Show file tree
Hide file tree
Showing 16 changed files with 665 additions and 0 deletions.
61 changes: 61 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

module.exports = {
extends: 'airbnb-base',
env: {
es6: true,
node: true,
jasmine: true,
},

parserOptions: {
sourceType: 'script',
ecmaVersion: '2022',
},

settings: {
'import/core-modules': ['humile', 'electron'],
},

rules: {
'class-methods-use-this': 'off',
'comma-dangle': ['error', {
arrays: 'always-multiline',
objects: 'always-multiline',
imports: 'always-multiline',
exports: 'always-multiline',
functions: 'never',
}],
'import/no-extraneous-dependencies': ['error', {
devDependencies: ['**/*.spec.js'],
}],
'lines-between-class-members': ['error', 'always', {
exceptAfterSingleLine: true,
}],
'max-len': ['error', { code: 80 }],
'no-continue': 'off',
'no-multi-spaces': ['error', {
exceptions: {
AssignmentExpression: true,
AssignmentPattern: true,
CallExpression: true,
VariableDeclarator: true,
},
}],
'no-unused-expressions': 'off',
'no-unused-vars': ['warn', {
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
vars: 'all',
args: 'after-used',
ignoreRestSiblings: true,
}],
'no-use-before-define': 'off',
'no-restricted-syntax': 'off',
'object-curly-newline': 'off',
'prefer-destructuring': 'off',
'prefer-spread': 'off',
'prefer-template': 'off',
strict: ['error', 'global'],
},
};
21 changes: 21 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Tests

on:
- pull_request
- push

env:
CI: 1

jobs:
main:
runs-on: ${{ ubuntu-latest }}

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16

- run: npm install
- run: npm run test:full
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
node_modules
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2022 Alexey Prokhorov

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.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# s3u
[![Tests](https://github.com/megahertz/s3u/workflows/Tests/badge.svg)](https://github.com/megahertz/s3u/actions?query=workflow%3ATests)
[![npm version](https://badge.fury.io/js/s3u.svg)](https://badge.fury.io/js/s3u)

## Description

S3 URL manipulation helper

### Key features

- Support different S3 providers
- Simple and lightweight
- No dependencies

## Installation

Install with [npm](https://npmjs.org/package/s3u):

npm install --save s3u

## Usage

```js
const { S3Url } = require('s3u');

const url = S3Url.fromUrl('https://mybucket.s3.amazonaws.com/');

url.key = 'My file.txt';

console.log(url.toString());
// https://mybucket.s3.amazonaws.com/My+file.txt
```

## License

Licensed under MIT.
42 changes: 42 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "s3u",
"version": "0.0.0",
"description": "S3 URL manipulation helper",
"main": "src/index.js",
"scripts": {
"lint": "eslint 'src/**/*.js'",
"test": "humile",
"test:full": "npm run test && npm run lint && tsc --noEmit",
"postversion": "git push && git push --tags",
"prepack": "npm run test:full",
"preversion": "npm run test:full"
},
"repository": "megahertz/s3u",
"files": [
"src/*",
"!__specs__"
],
"keywords": [
"s3",
"aws",
"url",
"link",
"stackpath",
"spaces"
],
"author": "Alexey Prokhorov",
"license": "MIT",
"bugs": "https://github.com/megahertz/s3u/issues",
"homepage": "https://github.com/megahertz/s3u#readme",
"engines": {
"node": ">=12.0"
},
"typings": "src/index.d.ts",
"devDependencies": {
"eslint": "*",
"eslint-config-airbnb-base": "*",
"eslint-plugin-import": "*",
"humile": "*",
"typescript": "*"
}
}
86 changes: 86 additions & 0 deletions src/S3Parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'use strict';

const S3Url = require('./S3Url');

class S3Parser {
providers = [];
fallbackProvider = null;

constructor({ providers = [], fallbackProvider = null }) {
providers.forEach((provider) => this.addProvider(provider));
this.fallbackProvider = fallbackProvider;
}

addProvider(provider) {
if (typeof provider.matchHostName !== 'function') {
throw new Error('Provider should implement matchHostName method');
}

if (typeof provider.parseUrl !== 'function') {
throw new Error('Provider should implement parseUrl method');
}

this.providers.push(provider);
}

getProvidersByHostName(hostName) {
return this.providers.filter((p) => p.matchHostName(hostName));
}

getProviderById(providerId) {
if (!providerId) {
return null;
}

return this.providers.find((p) => p.id === providerId);
}

getProvidersForParse({ url, providerId = null }) {
if (providerId) {
const provider = this.getProviderById(providerId);
if (provider) {
return [provider];
}
}

try {
const urlObj = new URL(url);

if (urlObj.protocol === 's3') {
return [this.getProviderById('amazonaws.com')];
}

return this.getProvidersByHostName(urlObj.hostname);
} catch {
return [];
}
}

parseUrl({ url, providerId = null }) {
const providers = this.getProvidersForParse({ url, providerId });

for (const provider of providers) {
try {
const s3Url = provider.parseUrl({ url });
if (s3Url) {
return s3Url;
}
} catch (e) {
// Can't parse, try another provider
}
}

try {
const s3Url = this.fallbackProvider?.parseUrl(url);
if (s3Url) {
return s3Url;
}
} catch (e) {
return new S3Url({ error: e, sourceUrl: url });
}

return new S3Url({ sourceUrl: url });
}
}

module.exports = S3Parser;
Loading

0 comments on commit 8ec7f8c

Please sign in to comment.