json-readable
v0.0.1
Published
## Usage
Downloads
4
Readme
JSON Readable
Usage
File System
import JSONReadable, { JsonReadableFragment } from "json-readable";
import https from 'https'
import fs from 'fs'
import { Base64Encode } from 'base64-stream';
function get(url: string): Promise<JsonReadableFragment> {
return new Promise<JsonReadableFragment>(resolve => {
https.get(
url,
res => {
const contentLength = Number(res.headers['content-length']);
/**
* Content-Length + Base64Encode calc
*/
const size =
Math.ceil((contentLength * 4) / 3) +
((3 - (contentLength % 3)) % 3);
resolve(
res.pipe(new Base64Encode())
.pipe(new JsonReadableFragment(size))
)
}
)
})
}
void (async () => {
new JSONReadable({
files: [
{
name: 'Bulbasaur.png',
content: get('https://e7.pngegg.com/pngimages/72/948/png-clipart-bulbasaur-pokemon-diamond-and-pearl-pokemon-go-pokedex-ivysaur-bulbasaur-pixel-vertebrate-grass.png')
},
{
name: 'Squirtle.png',
content: get('https://e7.pngegg.com/pngimages/700/421/png-clipart-pokemon-squirtle-squirtle-pokemon-games-pokemon.png')
},
{
name: 'Charmander.png',
content: get('https://e7.pngegg.com/pngimages/9/929/png-clipart-pokemon-charmander-pikachu-pokemon-x-and-y-charmander-charizard-charmander-food-orange.png')
},
]
}).pipe(fs.createWriteStream('output.json'))
})()
HTTP / HTTPS
import JsonReadable, { JsonReadableFragment } from "json-readable";
import https from 'https'
import { Base64Encode } from 'base64-stream';
function get(url: string): Promise<JsonReadableFragment> {
return new Promise<JsonReadableFragment>(resolve => {
https.get(
url,
res => {
const contentLength = Number(res.headers['content-length']);
/**
* Content-Length + Base64Encode calc
*/
const size =
Math.ceil((contentLength * 4) / 3) +
((3 - (contentLength % 3)) % 3);
resolve(
res.pipe(new Base64Encode())
.pipe(new JsonReadableFragment(size))
)
}
)
})
}
void (async () => {
const jsonReadable = new JsonReadable({
files: [
{
name: 'Bulbasaur.png',
content: get('https://e7.pngegg.com/pngimages/72/948/png-clipart-bulbasaur-pokemon-diamond-and-pearl-pokemon-go-pokedex-ivysaur-bulbasaur-pixel-vertebrate-grass.png')
},
{
name: 'Squirtle.png',
content: get('https://e7.pngegg.com/pngimages/700/421/png-clipart-pokemon-squirtle-squirtle-pokemon-games-pokemon.png')
},
{
name: 'Charmander.png',
content: get('https://e7.pngegg.com/pngimages/9/929/png-clipart-pokemon-charmander-pikachu-pokemon-x-and-y-charmander-charizard-charmander-food-orange.png')
},
]
})
const req = https.request({
method: 'POST',
host: '<hostname>',
headers: {
'Content-Type': 'application/json',
'Content-Length': await jsonReadable.getContentLength()
}
})
jsonReadable.pipe(req)
})()
Axios
import JsonReadable, { JsonReadableFragment } from "json-readable";
import { Base64Encode } from 'base64-stream';
import axios from 'axios'
import { Readable } from "stream";
async function get(url: string): Promise<JsonReadableFragment> {
const response = await axios.get<Readable>(url, { responseType: 'stream' })
const contentLength = Number(response.headers['Content-Length']);
const size = Math.ceil((contentLength * 4) / 3) + ((3 - (contentLength % 3)) % 3);
return response.data.pipe(new Base64Encode())
.pipe(new JsonReadableFragment(size))
}
void (async () => {
const jsonReadable = new JsonReadable({
files: [
{
name: 'Bulbasaur.png',
content: get('https://e7.pngegg.com/pngimages/72/948/png-clipart-bulbasaur-pokemon-diamond-and-pearl-pokemon-go-pokedex-ivysaur-bulbasaur-pixel-vertebrate-grass.png')
},
{
name: 'Squirtle.png',
content: get('https://e7.pngegg.com/pngimages/700/421/png-clipart-pokemon-squirtle-squirtle-pokemon-games-pokemon.png')
},
{
name: 'Charmander.png',
content: get('https://e7.pngegg.com/pngimages/9/929/png-clipart-pokemon-charmander-pikachu-pokemon-x-and-y-charmander-charizard-charmander-food-orange.png')
},
]
})
await axios.post('<url>', jsonReadable, {
headers: {
'Content-Type': 'application/json',
'Content-Length': await jsonReadable.getContentLength()
}
})
})()