summaryrefslogtreecommitdiff
path: root/node_modules/data-uri-to-buffer/index.js
diff options
context:
space:
mode:
authorakiyamn2023-09-24 23:22:21 +1000
committerakiyamn2023-09-24 23:22:21 +1000
commit4e87195739f2a5d9a05451b48773c8afdc680765 (patch)
tree9cba501844a4a11dcbdffc4050ed8189561c55ed /node_modules/data-uri-to-buffer/index.js
downloadprice-tracker-worker-4e87195739f2a5d9a05451b48773c8afdc680765.tar.gz
price-tracker-worker-4e87195739f2a5d9a05451b48773c8afdc680765.zip
Initial commit (by create-cloudflare CLI)
Diffstat (limited to 'node_modules/data-uri-to-buffer/index.js')
-rw-r--r--node_modules/data-uri-to-buffer/index.js70
1 files changed, 70 insertions, 0 deletions
diff --git a/node_modules/data-uri-to-buffer/index.js b/node_modules/data-uri-to-buffer/index.js
new file mode 100644
index 0000000..8f8ac76
--- /dev/null
+++ b/node_modules/data-uri-to-buffer/index.js
@@ -0,0 +1,70 @@
+'use strict';
+
+/**
+ * Module exports.
+ */
+
+module.exports = dataUriToBuffer;
+
+/**
+ * Returns a `Buffer` instance from the given data URI `uri`.
+ *
+ * @param {String} uri Data URI to turn into a Buffer instance
+ * @return {Buffer} Buffer instance from Data URI
+ * @api public
+ */
+
+function dataUriToBuffer(uri) {
+ if (!/^data\:/i.test(uri)) {
+ throw new TypeError(
+ '`uri` does not appear to be a Data URI (must begin with "data:")'
+ );
+ }
+
+ // strip newlines
+ uri = uri.replace(/\r?\n/g, '');
+
+ // split the URI up into the "metadata" and the "data" portions
+ var firstComma = uri.indexOf(',');
+ if (-1 === firstComma || firstComma <= 4) {
+ throw new TypeError('malformed data: URI');
+ }
+
+ // remove the "data:" scheme and parse the metadata
+ var meta = uri.substring(5, firstComma).split(';');
+
+ var type = meta[0] || 'text/plain';
+ var typeFull = type;
+ var base64 = false;
+ var charset = '';
+ for (var i = 1; i < meta.length; i++) {
+ if ('base64' == meta[i]) {
+ base64 = true;
+ } else {
+ typeFull += ';' + meta[i];
+ if (0 == meta[i].indexOf('charset=')) {
+ charset = meta[i].substring(8);
+ }
+ }
+ }
+ // defaults to US-ASCII only if type is not provided
+ if (!meta[0] && !charset.length) {
+ typeFull += ';charset=US-ASCII';
+ charset = 'US-ASCII';
+ }
+
+ // get the encoded data portion and decode URI-encoded chars
+ var data = unescape(uri.substring(firstComma + 1));
+
+ var encoding = base64 ? 'base64' : 'ascii';
+ var buffer = Buffer.from ? Buffer.from(data, encoding) : new Buffer(data, encoding);
+
+ // set `.type` and `.typeFull` properties to MIME type
+ buffer.type = type;
+ buffer.typeFull = typeFull;
+
+ // set the `.charset` property
+ buffer.charset = charset;
+
+ return buffer;
+}