Skip to content

Commit a5510d3

Browse files
committed
implementation ✨
1 parent 3da62a6 commit a5510d3

File tree

4 files changed

+70
-6
lines changed

4 files changed

+70
-6
lines changed

bin.js

+31-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const mri = require('mri')
55

6+
const encode = require('.')
67
const pkg = require('./package.json')
78

89
const argv = mri(process.argv.slice(2), {
@@ -16,7 +17,16 @@ const argv = mri(process.argv.slice(2), {
1617
if (argv.help || argv.h) {
1718
process.stdout.write(`
1819
Usage:
19-
as-data-uri
20+
as-data-uri [--url-encoded] [--mime mime-type] [--charset utf8]
21+
22+
Options:
23+
--url-encoded -u Url-encode instead of base64.
24+
--mime -m Specify a mime type. Default: text/plain.
25+
--charset -c Specify an optional charset (for text only).
26+
27+
Examples:
28+
echo -n 'hello world' | as-data-uri --mime text/plain --url-encoded
29+
cat picture.png | as-data-uri --mime image/png | pbcopy
2030
\n`)
2131
process.exit()
2232
}
@@ -33,4 +43,23 @@ const showError = (err) => {
3343
process.exit(err.code || 1)
3444
}
3545

36-
// todo
46+
const base64 = !(argv['url-encoded'] || argv.u)
47+
const mime = argv.mime || argv.m || null
48+
const charset = argv.charset || argv.c || null
49+
50+
// collect data from stdin
51+
let data = Buffer.alloc(0)
52+
process.stdin
53+
.on('data', (chunk) => {
54+
data = Buffer.concat([data, chunk])
55+
})
56+
57+
.once('error', showError)
58+
.once('end', () => {
59+
try {
60+
data = encode(data, mime, charset, base64)
61+
} catch (err) {
62+
return showError(err)
63+
}
64+
process.stdout.write(data)
65+
})

index.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
'use strict'
22

3-
// todo
3+
const {composeSync} = require('node-rfc2397')
4+
5+
const encode = (data, mime = null, charset = null, base64 = true) => {
6+
if (!Buffer.isBuffer(data)) data = Buffer.from(data)
7+
8+
const info = {data, base64: !!base64}
9+
if (mime !== null && mime !== undefined) info.mime = mime
10+
if (charset !== null && charset !== undefined) info.parameters = {charset}
11+
12+
return composeSync(info)
13+
}
14+
15+
module.exports = encode

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,7 @@
2727
"node": ">=6"
2828
},
2929
"dependencies": {
30+
"mri": "^1.1.0",
31+
"node-rfc2397": "^3.0.1"
3032
}
3133
}

readme.md

+24-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
**Command line tool to encode data into a [data URI](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs).**
44

5-
Alternatives:
5+
Similar to [`data-uri-to-file-cli`](https://github.com/ragingwind/data-uri-to-file-cli), but
66

7-
- [`data-uri-to-file-cli`](https://github.com/ragingwind/data-uri-to-file-cli)
7+
- only uses [stdio](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)) instead of read and writing files
8+
- supports url-encoded data URIs (e.g. `data:text/plain,hello%20world`)
9+
- supports an optional charset
810

911
[![npm version](https://img.shields.io/npm/v/as-data-uri-cli.svg)](https://www.npmjs.com/package/as-data-uri-cli)
1012
[![build status](https://api.travis-ci.org/derhuerst/as-data-uri-cli.svg?branch=master)](https://travis-ci.org/derhuerst/as-data-uri-cli)
@@ -14,8 +16,27 @@ Alternatives:
1416

1517
## Usage
1618

19+
```
20+
Usage:
21+
as-data-uri [--url-encoded] [--mime mime-type] [--charset utf8]
22+
23+
Options:
24+
--url-encoded -u Url-encode instead of base64.
25+
--mime -m Specify a mime type. Default: text/plain.
26+
--charset -c Specify an optional charset (for text only).
27+
28+
Examples:
29+
echo -n 'hello world' | as-data-uri --mime text/plain --url-encoded
30+
cat picture.png | as-data-uri --mime image/png | pbcopy
31+
```
32+
33+
If you have installed it:
34+
1735
```shell
18-
# todo
36+
echo -n 'Hello World!' | as-data-uri -m text/plain -u
37+
# data:text/plain,hello%20world
38+
cat foo.png | as-data-uri -m image/png
39+
# data:image/png;base64,iVBORw0K…CYII=
1940
```
2041

2142

0 commit comments

Comments
 (0)