Skip to content

Commit

Permalink
feat: initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud committed Jul 23, 2024
0 parents commit 3f21eb7
Show file tree
Hide file tree
Showing 16 changed files with 605 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
; http://editorconfig.org

root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
40 changes: 40 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Release and Publish

on:
push:
branches:
- main
- alpha
- beta
- next

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20

- name: Install dependencies
run: npm install

- name: Lint files
run: npm run lint

- name: Generate type definitions
run: npm run types

- name: Run tests
run: npm run test

- name: Run semantic release
run: npx semantic-release
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
33 changes: 33 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Run Lint and Tests

on:
push:
branches-ignore:
- main
- alpha
- beta
- next

jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, macOS-latest, windows-latest]
node-version: [18, 20]
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- run: npm install

- run: npm run lint

- run: npm run types

- run: npm test
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
node_modules
package-lock.json
uploads
tmp
gcloud.json
node_modules/**/*
.DS_Store
tmp/**/*
.idea
.idea/**/*
*.iml
*.log
coverage
.vscode
.nyc_output/
.tap/
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package-lock=false
save-exact=true
13 changes: 13 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"singleQuote": true,
"trailingComma": "all",
"tabWidth": 4,
"overrides": [
{
"files": ["*.json", "*.yml"],
"options": {
"tabWidth": 2
}
}
]
}
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) 2020 Eik

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.
133 changes: 133 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# @eik/sink-memory

A Sink implementation that runs completely in memory, designed for automated tests.

## Usage

```sh
npm install @eik/sink-memory
```

```js
const { pipeline } = require('stream');
const express = require('express');
const Sink = require('@eik/sink-memory');

const app = express();
const sink = new Sink();

app.get('/file.js', async (req, res, next) => {
try {
const file = await sink.read('/path/to/file/file.js');
pipeline(file.stream, res, (error) => {
if (error) return next(error);
});
} catch (error) {
next(error);
}
});

app.listen(8000);
```

## API

The sink instance has the following API:

### .write(filePath, contentType)

Method for writing a file to in-memory storage.

This method takes the following arguments:

- `filePath` - String - Path to the file to be stored - Required.
- `contentType` - String - The content type of the file - Required.

Resolves with a writable stream.

```js
const { pipeline } = require('stream);
const fromStream = new SomeReadableStream();
const sink = new Sink({ ... });
try {
const file = await sink.write('/path/to/file/file.js', 'application/javascript');
pipeline(fromStream, file.stream, (error) => {
if (error) console.log(error);
});
} catch (error) {
console.log(error);
}
```
### .read(filePath)
Method for reading a file from storage.
This method takes the following arguments:
- `filePath` - String - Path to the file to be read - Required.
Resolves with a [ReadFile][read-file] object which holds metadata about
the file and a readable stream with the byte stream of the file on the
`.stream` property.
```js
const { pipeline } = require('stream);

const toStream = new SomeWritableStream();
const sink = new Sink({ ... });

try {
const file = await sink.read('/path/to/file/file.js');
pipeline(file.stream, toStream, (error) => {
if (error) console.log(error);
});
} catch (error) {
console.log(error);
}
```

### .delete(filePath)

Method for deleting a file in storage.

This method takes the following arguments:

- `filePath` - String - Path to the file to be deleted - Required.

Resolves if file is deleted and rejects if file could not be deleted.

```js
const sink = new Sink({ ... });

try {
await sink.delete('/path/to/file/file.js');
} catch (error) {
console.log(error);
}
```

### .exist(filePath)

Method for checking if a file exist in the storage.

This method takes the following arguments:

- `filePath` - String - Path to the file to be checked for existence - Required.

Resolves if file exists and rejects if file does not exist.

```js
const sink = new Sink({ ... });

try {
await sink.exist('/path/to/file/file.js');
} catch (error) {
console.log(error);
}
```

[eik]: https://github.com/eik-lib
[read-file]: https://github.com/eik-lib/common/blob/master/lib/classes/read-file.js
22 changes: 22 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import prettierConfig from 'eslint-config-prettier';
import prettierPlugin from 'eslint-plugin-prettier/recommended';
import globals from 'globals';
import js from '@eslint/js';

export default [
js.configs.recommended,
prettierConfig,
prettierPlugin,
{
languageOptions: {
globals: {
...globals.node,
...globals.browser,
global: true,
},
},
},
{
ignores: ['coverage/*', 'dist/*'],
},
];
Loading

0 comments on commit 3f21eb7

Please sign in to comment.