Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma-andex committed Mar 12, 2023
0 parents commit f359176
Show file tree
Hide file tree
Showing 12 changed files with 509 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/bower_components/
/node_modules/
/.pulp-cache/
/output/
/generated-docs/
/.psc-package/
/.psc*
/.purs*
/.psa*
/.spago
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) 2022 purescript-contrib

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.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# purescript-js-blob
Low-level bindings for the [`Blob` API](https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob).

**Note:** If you are using Node.js then you will need Node.js version >= v15.7.0, v14.18.0
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "purescript-js-blob",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "spago build",
"test": "spago -x test.dhall test"
},
"keywords": [],
"author": "",
"license": "MIT"
}
105 changes: 105 additions & 0 deletions packages.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{-
Welcome to your new Dhall package-set!
Below are instructions for how to edit this file for most use
cases, so that you don't need to know Dhall to use it.
## Use Cases
Most will want to do one or both of these options:
1. Override/Patch a package's dependency
2. Add a package not already in the default package set
This file will continue to work whether you use one or both options.
Instructions for each option are explained below.
### Overriding/Patching a package
Purpose:
- Change a package's dependency to a newer/older release than the
default package set's release
- Use your own modified version of some dependency that may
include new API, changed API, removed API by
using your custom git repo of the library rather than
the package set's repo
Syntax:
where `entityName` is one of the following:
- dependencies
- repo
- version
-------------------------------
let upstream = --
in upstream
with packageName.entityName = "new value"
-------------------------------
Example:
-------------------------------
let upstream = --
in upstream
with halogen.version = "master"
with halogen.repo = "https://example.com/path/to/git/repo.git"
with halogen-vdom.version = "v4.0.0"
with halogen-vdom.dependencies = [ "extra-dependency" ] # halogen-vdom.dependencies
-------------------------------
### Additions
Purpose:
- Add packages that aren't already included in the default package set
Syntax:
where `<version>` is:
- a tag (i.e. "v4.0.0")
- a branch (i.e. "master")
- commit hash (i.e. "701f3e44aafb1a6459281714858fadf2c4c2a977")
-------------------------------
let upstream = --
in upstream
with new-package-name =
{ dependencies =
[ "dependency1"
, "dependency2"
]
, repo =
"https://example.com/path/to/git/repo.git"
, version =
"<version>"
}
-------------------------------
Example:
-------------------------------
let upstream = --
in upstream
with benchotron =
{ dependencies =
[ "arrays"
, "exists"
, "profunctor"
, "strings"
, "quickcheck"
, "lcg"
, "transformers"
, "foldable-traversable"
, "exceptions"
, "node-fs"
, "node-buffer"
, "node-readline"
, "datetime"
, "now"
]
, repo =
"https://github.com/hdgarrood/purescript-benchotron.git"
, version =
"v7.0.0"
}
-------------------------------
-}
let upstream =
https://github.com/purescript/package-sets/releases/download/psc-0.15.7-20230310/packages.dhall
sha256:c30c50d19c9eb55516b0a8a1bd368a0754bde47365be36abadb489295d86d77c

in upstream
17 changes: 17 additions & 0 deletions spago.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{ name = "js-blob"
, dependencies =
[ "arrays"
, "console"
, "effect"
, "integers"
, "maybe"
, "media-types"
, "newtype"
, "nullable"
, "numbers"
, "prelude"
, "unsafe-coerce"
]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs" ]
}
65 changes: 65 additions & 0 deletions src/Js/Blob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
export function typeImpl(blob) {
return blob.type;
}

const fromSources = function (sources, options) {
if (options === null) {
return new Blob(sources);
} else {
return new Blob(sources, options);
}
};

export const fromStringsImpl = function (sources) {
return function (options) {
return fromSources(sources, options);
};
};

export function size(blob) {
return blob.size;
}

export function sliceImpl(contentType) {
return function (start) {
return function (end) {
return function (blob) {
if (contentType != null) {
return blob.slice(start, end, contentType);
} else {
return blob.slice(start, end);
}
};
};
};
}

export const text = function (blob) {
return function () {
return blob.text();
};
};

export const toArrayBuffer = function (blob) {
return function () {
return blob.arrayBuffer();
};
};

export const fromArrayBuffersImpl = function (sources) {
return function (options) {
return fromSources(sources, options);
};
};

export const fromBlobsImpl = function (sources) {
return function (options) {
return fromSources(sources, options);
};
};

export const fromDataViewImpl = function (sources) {
return function (options) {
return fromSources(sources, options);
};
};
151 changes: 151 additions & 0 deletions src/Js/Blob.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
module Js.Blob
( Blob
, BlobEnding(..)
, BlobOptions
, ByteIdx
, EndByte(..)
, StartByte(..)
, fromArrayBuffers
, fromBlobs
, fromDataView
, fromString
, fromStrings
, idxFromInt
, idxFromNumber
, size
, slice
, slice'
, text
, toArrayBuffer
, type_
) where

import Control.Applicative ((<#>))
import Data.Array.NonEmpty (NonEmptyArray)
import Data.Array.NonEmpty as NonEmptyArray
import Data.ArrayBuffer.Types (ArrayBuffer, DataView)
import Data.Int (toNumber)
import Data.Maybe (Maybe(..))
import Data.MediaType (MediaType(..))
import Data.Newtype (un)
import Data.Nullable (Nullable, toNullable)
import Data.Nullable as Nullable
import Data.Number (round)
import Effect (Effect)
import Prelude ((#), (==), (>>>))
import Promise (Promise)
import Unsafe.Coerce (unsafeCoerce)

foreign import data Blob :: Type

data BlobEnding = Transparent | Native

type BlobOptions =
{ "type" :: MediaType
, endings :: BlobEnding
}

type BlobOptionsImpl =
{ "type" :: String
, endings :: String
}

toBlobOptionsImpl :: BlobOptions -> BlobOptionsImpl
toBlobOptionsImpl { "type": mediaType, endings } =
{ "type": un MediaType mediaType
, endings: toEndings endings
}
where
toEndings Transparent = "transparent"
toEndings Native = "native"

foreign import fromStringsImpl :: Array String -> Nullable BlobOptionsImpl -> Blob

-- | Creates a String with the given Mediatype
-- | For example:
-- | ```
-- | myBlob = fromString (unsafeStringify { name: "Carl", age: 25 }) (MediaType "application/json")
-- | ```
fromString :: String -> Maybe BlobOptions -> Blob
fromString strs opts = fromStringsImpl [ strs ] (opts <#> toBlobOptionsImpl # toNullable)

-- | Creates a new Blob from one or more strings
fromStrings :: NonEmptyArray String -> Maybe BlobOptions -> Blob
fromStrings strs opts = fromStringsImpl (NonEmptyArray.toArray strs) (opts <#> toBlobOptionsImpl # toNullable)

foreign import typeImpl :: Blob -> String

-- | `MediaType` of the data contained in the `Blob`.
-- | Returns `Nothing` if the `MediaType` is unknown.
type_ :: Blob -> Maybe MediaType
type_ blob =
let
blobType = typeImpl blob
in
if blobType == "" then Nothing
else Just (MediaType blobType)

-- | The size (in bytes) of the data contained in the `Blob`.
foreign import size :: Blob -> Int

-- | An index into the Blob indicating the first byte to include in the new Blob.
-- | If you specify a negative value, it's treated as an offset from the end of the
-- | string toward the beginning. For example, -10 would be the 10th from last byte
-- | in the Blob. If you specify a value for start that is larger than the size
-- | of the source Blob, the returned Blob has size 0 and contains no data.
newtype StartByte = StartByte ByteIdx

-- | An index into the Blob indicating the first byte that will *not* be included
-- | in the new Blob (i.e. the byte exactly at this index is not included).
-- | If you specify a negative value, it's treated as an offset from the end of
-- | the string toward the beginning. For example, -10 would be the 10th from
-- | last byte in the Blob. The default value is size.
newtype EndByte = EndByte ByteIdx

foreign import data ByteIdx :: Type

-- | Creates `ByteIdx` from `Int` value
idxFromInt :: Int -> ByteIdx
idxFromInt = toNumber >>> unsafeCoerce

-- | Creates `ByteIdx` from `Number` value using `Math.round`.
idxFromNumber :: Number -> ByteIdx
idxFromNumber = round >>> unsafeCoerce

-- | Creates a new `Blob` object (with specified `MediaType`), containing the
-- | data in the specified range of bytes of the source Blob, by setting .
foreign import sliceImpl Nullable MediaType -> StartByte -> EndByte -> Blob -> Blob

-- | Creates a new `Blob` object containing the data in the specified range
-- | of bytes of the source Blob.
slice MediaType -> StartByte -> EndByte -> Blob -> Blob
slice mt = sliceImpl (Nullable.notNull mt)

-- | Creates a new `Blob` object containing the data in the specified range
-- | of bytes of the source Blob.
slice' StartByte -> EndByte -> Blob -> Blob
slice' = sliceImpl (Nullable.null)

-- | Returns a promise that fulfills with the contents of the Blob decoded as a UTF-8 string.
foreign import text :: Blob -> Effect (Promise String)

-- | Copies the data in the Blob to a new JS ArrayBuffer
foreign import toArrayBuffer :: Blob -> Effect (Promise ArrayBuffer)

foreign import fromArrayBuffersImpl :: NonEmptyArray ArrayBuffer -> Nullable BlobOptionsImpl -> Blob

-- | Creates a new Blob from one ore more `ArrayBuffer`s
fromArrayBuffers :: NonEmptyArray ArrayBuffer -> Maybe BlobOptions -> Blob
fromArrayBuffers strs opts = fromArrayBuffersImpl strs (opts <#> toBlobOptionsImpl # toNullable)

foreign import fromBlobsImpl :: NonEmptyArray Blob -> Nullable BlobOptionsImpl -> Blob

-- | Creates a new Blob from one ore more `Blob`s
fromBlobs :: NonEmptyArray Blob -> Maybe BlobOptions -> Blob
fromBlobs strs opts = fromBlobsImpl strs (opts <#> toBlobOptionsImpl # toNullable)

foreign import fromDataViewImpl :: NonEmptyArray DataView -> Nullable BlobOptionsImpl -> Blob

-- | Creates a new Blob from one ore more `DataView`s
fromDataView :: NonEmptyArray DataView -> Maybe BlobOptions -> Blob
fromDataView strs opts = fromDataViewImpl strs (opts <#> toBlobOptionsImpl # toNullable)
Loading

0 comments on commit f359176

Please sign in to comment.