Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ name: Build
on:
pull_request:
branches:
- main
- main
push:
branches:
- main
- main

jobs:
build:
name: Lint and tests
name: Checks and tests
runs-on: ubuntu-latest
strategy:
fail-fast: false
Expand All @@ -20,13 +20,13 @@ jobs:
- 22.14.0
- 22
- 24.0.0
- lts/* # currently 24
- lts/* # currently 24
- latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run lint
- run: npm test
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run check
- run: npm test
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Publish
on:
push:
tags:
- 'v*'
- "v*"

permissions:
id-token: write
Expand All @@ -19,6 +19,6 @@ jobs:
node-version: lts/*
registry-url: https://registry.npmjs.org
- run: npm ci
- run: npm run lint
- run: npm run check
- run: npm test
- run: npm publish
4 changes: 4 additions & 0 deletions .oxfmtrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"sortImports": true,
"ignorePatterns": ["**/*.html"]
}
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ const parseDataURL = require("data-urls");

const textExample = parseDataURL("data:,Hello%2C%20World!");
console.log(textExample.mimeType.toString()); // "text/plain;charset=US-ASCII"
console.log(textExample.body); // Uint8Array(13) [ 72, 101, 108, 108, 111, 44, … ]
console.log(textExample.body); // Uint8Array(13) [ 72, 101, 108, 108, 111, 44, … ]

const htmlExample = parseDataURL("data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E");
console.log(htmlExample.mimeType.toString()); // "text/html"
console.log(htmlExample.body); // Uint8Array(22) [ 60, 104, 49, 62, 72, 101, … ]

const pngExample = parseDataURL("data:image/png;base64,iVBORw0KGgoAAA" +
"ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4" +
"//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU" +
"5ErkJggg==");
console.log(htmlExample.body); // Uint8Array(22) [ 60, 104, 49, 62, 72, 101, … ]

const pngExample = parseDataURL(
"data:image/png;base64,iVBORw0KGgoAAA" +
"ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4" +
"//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU" +
"5ErkJggg==",
);
console.log(pngExample.mimeType.toString()); // "image/png"
console.log(pngExample.body); // Uint8Array(85) [ 137, 80, 78, 71, 13, 10, … ]
console.log(pngExample.body); // Uint8Array(85) [ 137, 80, 78, 71, 13, 10, … ]
```

## API
Expand Down
10 changes: 5 additions & 5 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ export default [
files: ["**/*.js"],
languageOptions: {
sourceType: "commonjs",
globals: globals.node
}
globals: globals.node,
},
},
{
files: ["**/*.mjs"],
languageOptions: {
sourceType: "module",
globals: globals.node
}
globals: globals.node,
},
},
...domenicConfig,
...domenicStylisticConfig
...domenicStylisticConfig,
];
12 changes: 8 additions & 4 deletions lib/parser.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
"use strict";
const { MIMEType } = require("whatwg-mimetype");
const { parseURL, serializeURL, percentDecodeString } = require("whatwg-url");
const { stripLeadingAndTrailingASCIIWhitespace, isomorphicDecode, forgivingBase64Decode } = require("./utils.js");
const {
stripLeadingAndTrailingASCIIWhitespace,
isomorphicDecode,
forgivingBase64Decode,
} = require("./utils.js");

module.exports = stringInput => {
module.exports = (stringInput) => {
const urlRecord = parseURL(stringInput);

if (urlRecord === null) {
Expand All @@ -13,7 +17,7 @@ module.exports = stringInput => {
return module.exports.fromURLRecord(urlRecord);
};

module.exports.fromURLRecord = urlRecord => {
module.exports.fromURLRecord = (urlRecord) => {
if (urlRecord.scheme !== "data") {
return null;
}
Expand Down Expand Up @@ -64,6 +68,6 @@ module.exports.fromURLRecord = urlRecord => {

return {
mimeType: mimeTypeRecord,
body
body,
};
};
10 changes: 5 additions & 5 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
"use strict";

exports.stripLeadingAndTrailingASCIIWhitespace = string => {
exports.stripLeadingAndTrailingASCIIWhitespace = (string) => {
return string.replace(/^[ \t\n\f\r]+/u, "").replace(/[ \t\n\f\r]+$/u, "");
};

exports.isomorphicDecode = input => {
return Array.from(input, byte => String.fromCodePoint(byte)).join("");
exports.isomorphicDecode = (input) => {
return Array.from(input, (byte) => String.fromCodePoint(byte)).join("");
};

exports.forgivingBase64Decode = data => {
exports.forgivingBase64Decode = (data) => {
let asString;
try {
asString = atob(data);
} catch {
return null;
}

return Uint8Array.from(asString, c => c.codePointAt(0));
return Uint8Array.from(asString, (c) => c.codePointAt(0));
};
Loading
Loading