Skip to content

Commit

Permalink
Merge pull request #8 from dojyorin/dev
Browse files Browse the repository at this point in the history
various updates.
  • Loading branch information
dojyorin authored Oct 27, 2022
2 parents 8ad81d5 + bf2b312 commit 1b847df
Show file tree
Hide file tree
Showing 19 changed files with 349 additions and 120 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Release
"on":
push:
tags: v[0-9].[0-9].[0-9]
jobs:
release:
name: 'Release: ${{github.ref_name}}'
runs-on: ubuntu-latest
steps:
- name: clone repository
uses: actions/checkout@v3
- name: dispatch release
uses: softprops/action-gh-release@v1
with:
generate_release_notes: true
13 changes: 9 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
name: Test
"on":
push:
branches-ignore:
- master
- topic/**
branches:
- dev
paths-ignore:
- .git*
- '**.md'
pull_request:
branches: master
branches:
- master
- dev
paths-ignore:
- .git*
- '**.md'
jobs:
test:
name: 'Test: ${{matrix.os}}'
Expand Down
24 changes: 24 additions & 0 deletions .github/workflows_json/release.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "Release",
"on": {
"push": {
"tags": "v[0-9].[0-9].[0-9]"
}
},
"jobs": {
"release": {
"name": "Release: ${{github.ref_name}}",
"runs-on": "ubuntu-latest",
"steps": [{
"name": "clone repository",
"uses": "actions/checkout@v3"
}, {
"name": "dispatch release",
"uses": "softprops/action-gh-release@v1",
"with": {
"generate_release_notes": true
}
}]
}
}
}
15 changes: 11 additions & 4 deletions .github/workflows_json/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@
"name": "Test",
"on": {
"push": {
"branches-ignore": [
"master",
"topic/**"
"branches": [
"dev"
],
"paths-ignore": [
".git*",
"**.md"
]
},
"pull_request": {
"branches": "master"
"branches": [
"master",
"dev"
],
"paths-ignore": [
".git*",
"**.md"
]
}
},
"jobs": {
Expand Down
File renamed without changes.
94 changes: 92 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,92 @@
# Utility collection for Deno
![](https://github.com/dojyorin/deno_bit_utility/actions/workflows/test.yaml/badge.svg?branch=master)
# **Simple Utility for Deno**
![Actions-Test](https://github.com/dojyorin/deno_simple_utility/actions/workflows/test.yaml/badge.svg)
![Actions-Release](https://github.com/dojyorin/deno_simple_utility/actions/workflows/release.yaml/badge.svg)

A handy utility collection.

# Example
**BASE64 Binary**

```ts
const file = await Deno.readFile("/path/to/binary.bin");

const encoded = base64Encode(file); // BASE64 encoded string.
const decoded = base64Decode(encoded); // Restored byte array.
```

**DEFLATE Compress**

```ts
const file = await Deno.readFile("/path/to/binary.bin");

const encoded = await deflateEncode(file); // DEFLATE compressed byte array.
const decoded = await deflateDecode(encoded); // Restored byte array.
```

**Extended Fetch API**

```ts
const json = await fetchExtend("https://path/to/get", "json"); // Response as JSON.
const bytes = await fetchExtend("https://path/to/get", "byte"); // Response as Uint8Array.
```

**Minipack Archive**

```ts
const files = [
new File([await Deno.readFile("/path/to/binary.bin")], "binary.bin")
];

const encoded = await minipackEncode(files); // Minipack archived byte array.
const decoded = await minipackDecode(encoded); // Restored file object array.
```

# Details
It's basically a thin wrapper around Deno's functions to improve usability, but some features are original to this module.

This section describes the original features of this module.

## Minipack
Minipack is a file archive format original to this module.

It's structure is inspired by the famous "tar" and is minimal as an archive.

Originally developed for browsers, the purpose was to aggregate multiple files input with the HTML File API into a single file.

Therefore, there is no concept of directory or filesystem, and it's feature by simple structure that stores only the file body, file name, and hash value for verification.

The actual binary structure looks like this:

|Index|Type|Title|Size (Byte)|
|:--|:--|:--|:--|
|1|Header|HashValue|32|
|2|Header|NameSize|1|
|3|Header|BodySize|4|
|4|Body|FileName|Max 255 (Defined in NameSize)|
|5|Body|FileBody|Max 4294967295 (Defined in BodySize)|

This is for one file and repeats for the number of files.

# API
## `Uint8Array base64Encode(data)`
- `data` ... The byte array.

## `Uint8Array base64Decode(data)`
- `data` ... The BASE64 code.

## `Promise<Uint8Array> deflateEncode(data)`
- `data` ... The byte array.

## `Promise<Uint8Array> deflateDecode(data)`
- `data` ... The deflate compressed byte array.

## `<FetchResponseType<T>> fetchExtend<T>(path, type, option)`
- `path` ... Target URL. Since the query string is ignored, please specify it in the `option.query` property instead of writing it directly in the URL.
- `type` ... The type you want to receive in the response.
- `option` ... Fetch option. `window` is removed from `RequestInit` and `query` is added to write the query string.

## `Promise<Uint8Array> minipackEncode(files)`
- `files` ... Array of file object.

## `Promise<File[]> minipackDecode(archive)`
- `data` ... The minipack archived byte array.
3 changes: 2 additions & 1 deletion deps.test.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export {assertEquals} from "https://deno.land/[email protected]/testing/asserts.ts";
export {assertEquals} from "https://deno.land/[email protected]/testing/asserts.ts";
export {serve} from "https://deno.land/[email protected]/http/mod.ts";
2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {type JsonValue} from "https://deno.land/std@0.159.0/encoding/json/stream.ts";
export {type JsonValue} from "https://deno.land/std@0.160.0/encoding/json/stream.ts";
2 changes: 1 addition & 1 deletion mod.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "./test/base64.test.ts";
import "./test/deflate.test.ts";
import "./test/fetch_extend.test.ts";
import "./test/fetch.test.ts";
import "./test/minipack.test.ts";
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./src/base64.ts";
export * from "./src/deflate.ts";
export * from "./src/fetch_extend.ts";
export * from "./src/fetch.ts";
export * from "./src/minipack.ts";
4 changes: 2 additions & 2 deletions src/base64.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Convert base64 code to byte array.
* Convert from BASE64 code to byte array.
* @param data The byte array.
**/
export function base64Encode(data:Uint8Array){
return btoa([...data].map(n => String.fromCharCode(n)).join(""));
}

/**
* Convert byte array to base64 code.
* Convert from byte array to BASE64 code.
* @param data The base64 code.
**/
export function base64Decode(data:string){
Expand Down
16 changes: 10 additions & 6 deletions src/deflate.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
async function transform(data:Uint8Array, ts:TransformStream<Uint8Array,Uint8Array>){
return new Uint8Array(await new Response(new Blob([data]).stream().pipeThrough(ts)).arrayBuffer());
}

/**
* Compresses raw binary in "deflate" format (RFC1951 compliant).
* It does not include header information like "gzip" (RFC1952) or "zlib" (RFC1950) as it does purely "compression only".
* @param data The byte buffer.
* @param data The byte array.
**/
export async function deflateEncode(data:ArrayBuffer){
return new Response(new Blob([data]).stream().pipeThrough(new CompressionStream("deflate-raw"))).arrayBuffer();
export async function deflateEncode(data:Uint8Array){
return await transform(data, new CompressionStream("deflate-raw"));
}

/**
* Decompress "deflate" format (RFC1951 compliant) binary.
* Binaries containing header information like "gzip" (RFC1952) or "zlib" (RFC1950) cannot be decompressed.
* @param data The byte buffer.
* @param data The byte array.
**/
export async function deflateDecode(data:ArrayBuffer){
return new Response(new Blob([data]).stream().pipeThrough(new DecompressionStream("deflate-raw"))).arrayBuffer();
export async function deflateDecode(data:Uint8Array){
return await transform(data, new DecompressionStream("deflate-raw"));
}
40 changes: 14 additions & 26 deletions src/fetch_extend.ts → src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ export interface FetchInit extends Omit<RequestInit, "window">{

interface FetchResponseMap{
"text": string;
"arraybuffer": ArrayBuffer;
"blob": Blob;
"json": JsonValue;
"response": Response;
"form": FormData;
"byte": Uint8Array;
"buffer": ArrayBuffer;
"blob": Blob;
"ok": boolean;
"response": Response;
}

/**
Expand Down Expand Up @@ -43,28 +45,14 @@ export async function fetchExtend<T extends FetchResponseLabel>(path:string, typ
});

switch(type){
case "response": {
return <FetchResponseType<T>>response;
}

case "ok": {
return <FetchResponseType<T>>response.ok;
}

case "arraybuffer": {
return <FetchResponseType<T>>await response.arrayBuffer();
}

case "blob": {
return <FetchResponseType<T>>await response.blob();
}

case "text": {
return <FetchResponseType<T>>await response.text();
}

case "json": {
return <FetchResponseType<T>>await response.json();
}
case "text": return <FetchResponseType<T>>await response.text();
case "json": return <FetchResponseType<T>>await response.json();
case "form": return <FetchResponseType<T>>await response.formData();
case "byte": return <FetchResponseType<T>>new Uint8Array(await response.arrayBuffer());
case "buffer": return <FetchResponseType<T>>await response.arrayBuffer();
case "blob": return <FetchResponseType<T>>await response.blob();
case "ok": return <FetchResponseType<T>>response.ok;
case "response": return <FetchResponseType<T>>response;
default: throw new Error();
}
}
Loading

0 comments on commit 1b847df

Please sign in to comment.