Code Monkey home page Code Monkey logo

data-uri's Introduction

DataURI

Swift Version Vapor Version Circle CI codebeat badge codecov Readme Score GitHub license

A pure Swift parser for Data URIs.

๐Ÿ“ฆ Installation

Update your Package.swift file.

.package(url: "https://github.com/nodes-vapor/data-uri.git", from: "2.0.0")

Getting started ๐Ÿš€

There are two options for decoding a Data URI. The first is using the String extension and the second is by using the DataURIParser directly.

The String method

This method is by far the easiest to use. All you need to do is call .dataURIDecoded() throws -> (data: Bytes, type: String) on any Data URI encoded String.

import Core //just for `Bytes.string`
import DataURI

let uri = "data:,Hello%2C%20World!"
let (data, type) = try uri.dataURIDecoded()
print(data.string) // "Hello, World!"
print(type) // "text/plain;charset=US-ASCII"

The DataURIParser method

Using the parser is a bit more involved as it returns all of its results as Bytes.

import Core //just for `Bytes.string`
import DataURI

let uri = "data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E"
let (data, type, metadata) = try DataURIParser.parse(uri: uri)
print(data.string) // "<h1>Hello, World!</h1>"
print(type.string) // "text/html"
print(metadata == nil) // "true"

๐Ÿ† Credits

This package is developed and maintained by the Vapor team at Nodes. The package owner for this project is Tom.

๐Ÿ“„ License

This package is open-sourced software licensed under the MIT license

data-uri's People

Contributors

brettrtoomey avatar steffendsommer avatar valen90 avatar bfolkens avatar heidipuk avatar siemensikkema avatar

Stargazers

eunpyo hong avatar Jason Ou avatar  avatar Kyle Howells avatar  avatar  avatar  avatar Haruhito Fuji avatar Isaiah J Turner avatar  avatar Christian Tietze avatar Casper Rasmussen avatar  avatar  avatar

Watchers

 avatar James Cloos avatar Rasmus Ebbesen avatar  avatar Casper Rasmussen avatar Tanner avatar Oliver Wang Hansen avatar logan avatar  avatar

data-uri's Issues

Remove dependencies

@BrettRToomey , It is two parts from Core that we use: An extension to Byte, which is found in "Core3.4.4/Bits/Byte-Control". We actually only use three cases, .comma, .percent and .semicolon. (We do however use a few more cases in the test)
The second is a function called convertFromData() -> Data, which can be found in "Core3.4.4/Core/LosslessDataConvertible". We use the version that extends the array.

Corrupt data returned when base64 encoded URI contains binary

When a data URI contains base64 encoded binary data (for example, a png image) the output is corrupt.

It appears that Parser decodes the base64 data successfully, but then attempts to convert the result into utf-8 encoded string which, in turn, corrupts the underlying data.

Support whitespaces in data

let (data, _) = try "data:text/plain;base64,\nAAECA3Rl\nc3QK".dataURIDecoded()
print(String(bytes: data, encoding: .utf8)!.debugDescription)

This code prints \nAAECA3Rl\nc3QK, while it should print \0\u{01}\u{02}\u{03}test\n

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.