onchain-utility 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Base64.js ADDED
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ 'use strict';
10
+
11
+ /**
12
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
13
+ *
14
+ * This source code is licensed under the MIT license found in the
15
+ * LICENSE file in the root directory of this source tree.
16
+ *
17
+ */
18
+ /* eslint-disable @typescript-eslint/no-explicit-any */
19
+
20
+ function generateUUID() {
21
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
22
+ const r = Math.random() * 16 | 0;
23
+ const v = c === 'x' ? r : r & 0x3 | 0x8;
24
+ return v.toString(16);
25
+ });
26
+ }
27
+
28
+ /** 生成UUID */
29
+ function generateSecureUUID() {
30
+ if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
31
+ const buffer = new Uint8Array(16);
32
+ crypto.getRandomValues(buffer);
33
+ buffer[6] = buffer[6] & 0x0f | 0x40;
34
+ buffer[8] = buffer[8] & 0x3f | 0x80;
35
+ return Array.from(buffer).map((b, i) => {
36
+ return (i === 4 || i === 6 || i === 8 || i === 10 ? '-' : '') + b.toString(16).padStart(2, '0');
37
+ }).join('');
38
+ }
39
+ return generateUUID();
40
+ }
41
+
42
+ /**
43
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
44
+ *
45
+ * This source code is licensed under the MIT license found in the
46
+ * LICENSE file in the root directory of this source tree.
47
+ *
48
+ */
49
+
50
+ function getBase64Regular() {
51
+ return /data:(.+?)\/(.+?);base64,(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?/g;
52
+ }
53
+ function base64ToMimeType(base64Src) {
54
+ const regular = new RegExp(`^data:(.*?)/(.*?);base64`);
55
+ const [, type, suffix] = base64Src.match(regular) || [];
56
+ return {
57
+ mimeType: `${type}/${suffix}`,
58
+ suffix,
59
+ type
60
+ };
61
+ }
62
+
63
+ /** base64 转 文件 */
64
+ function base64ToFile({
65
+ type,
66
+ text,
67
+ suffix,
68
+ filename
69
+ }) {
70
+ const mimeType = type && suffix ? `${type}/${suffix}` : base64ToMimeType(text).mimeType;
71
+ const base64WithoutPrefix = text.split(';base64,').pop();
72
+ const byteCharacters = atob(base64WithoutPrefix);
73
+ const byteArrays = [];
74
+ for (let offset = 0; offset < byteCharacters.length; offset += 512) {
75
+ const slice = byteCharacters.slice(offset, offset + 512);
76
+ const byteNumbers = new Array(slice.length);
77
+ for (let i = 0; i < slice.length; i++) {
78
+ byteNumbers[i] = slice.charCodeAt(i);
79
+ }
80
+ const byteArray = new Uint8Array(byteNumbers);
81
+ byteArrays.push(byteArray);
82
+ }
83
+ const blob = new Blob(byteArrays, {
84
+ type: mimeType
85
+ });
86
+ return new File([blob], `${filename || generateSecureUUID()}.${suffix}`, {
87
+ type: mimeType
88
+ });
89
+ }
90
+
91
+ exports.base64ToFile = base64ToFile;
92
+ exports.base64ToMimeType = base64ToMimeType;
93
+ exports.getBase64Regular = getBase64Regular;
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ /**
10
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
11
+ *
12
+ * This source code is licensed under the MIT license found in the
13
+ * LICENSE file in the root directory of this source tree.
14
+ *
15
+ */
16
+ /* eslint-disable @typescript-eslint/no-explicit-any */
17
+
18
+ function generateUUID() {
19
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
20
+ const r = Math.random() * 16 | 0;
21
+ const v = c === 'x' ? r : r & 0x3 | 0x8;
22
+ return v.toString(16);
23
+ });
24
+ }
25
+
26
+ /** 生成UUID */
27
+ function generateSecureUUID() {
28
+ if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
29
+ const buffer = new Uint8Array(16);
30
+ crypto.getRandomValues(buffer);
31
+ buffer[6] = buffer[6] & 0x0f | 0x40;
32
+ buffer[8] = buffer[8] & 0x3f | 0x80;
33
+ return Array.from(buffer).map((b, i) => {
34
+ return (i === 4 || i === 6 || i === 8 || i === 10 ? '-' : '') + b.toString(16).padStart(2, '0');
35
+ }).join('');
36
+ }
37
+ return generateUUID();
38
+ }
39
+
40
+ /**
41
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
42
+ *
43
+ * This source code is licensed under the MIT license found in the
44
+ * LICENSE file in the root directory of this source tree.
45
+ *
46
+ */
47
+
48
+ function getBase64Regular() {
49
+ return /data:(.+?)\/(.+?);base64,(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?/g;
50
+ }
51
+ function base64ToMimeType(base64Src) {
52
+ const regular = new RegExp(`^data:(.*?)/(.*?);base64`);
53
+ const [, type, suffix] = base64Src.match(regular) || [];
54
+ return {
55
+ mimeType: `${type}/${suffix}`,
56
+ suffix,
57
+ type
58
+ };
59
+ }
60
+
61
+ /** base64 转 文件 */
62
+ function base64ToFile({
63
+ type,
64
+ text,
65
+ suffix,
66
+ filename
67
+ }) {
68
+ const mimeType = type && suffix ? `${type}/${suffix}` : base64ToMimeType(text).mimeType;
69
+ const base64WithoutPrefix = text.split(';base64,').pop();
70
+ const byteCharacters = atob(base64WithoutPrefix);
71
+ const byteArrays = [];
72
+ for (let offset = 0; offset < byteCharacters.length; offset += 512) {
73
+ const slice = byteCharacters.slice(offset, offset + 512);
74
+ const byteNumbers = new Array(slice.length);
75
+ for (let i = 0; i < slice.length; i++) {
76
+ byteNumbers[i] = slice.charCodeAt(i);
77
+ }
78
+ const byteArray = new Uint8Array(byteNumbers);
79
+ byteArrays.push(byteArray);
80
+ }
81
+ const blob = new Blob(byteArrays, {
82
+ type: mimeType
83
+ });
84
+ return new File([blob], `${filename || generateSecureUUID()}.${suffix}`, {
85
+ type: mimeType
86
+ });
87
+ }
88
+
89
+ export { base64ToFile, base64ToMimeType, getBase64Regular };