@juzi/file-box 1.7.0

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.
Files changed (210) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +613 -0
  3. package/dist/cjs/package.json +3 -0
  4. package/dist/cjs/src/config.d.ts +4 -0
  5. package/dist/cjs/src/config.d.ts.map +1 -0
  6. package/dist/cjs/src/config.js +9 -0
  7. package/dist/cjs/src/config.js.map +1 -0
  8. package/dist/cjs/src/file-box.d.ts +209 -0
  9. package/dist/cjs/src/file-box.d.ts.map +1 -0
  10. package/dist/cjs/src/file-box.js +804 -0
  11. package/dist/cjs/src/file-box.js.map +1 -0
  12. package/dist/cjs/src/file-box.spec.d.ts +14 -0
  13. package/dist/cjs/src/file-box.spec.d.ts.map +1 -0
  14. package/dist/cjs/src/file-box.spec.js +393 -0
  15. package/dist/cjs/src/file-box.spec.js.map +1 -0
  16. package/dist/cjs/src/file-box.type.d.ts +103 -0
  17. package/dist/cjs/src/file-box.type.d.ts.map +1 -0
  18. package/dist/cjs/src/file-box.type.js +34 -0
  19. package/dist/cjs/src/file-box.type.js.map +1 -0
  20. package/dist/cjs/src/interface.d.ts +25 -0
  21. package/dist/cjs/src/interface.d.ts.map +1 -0
  22. package/dist/cjs/src/interface.js +3 -0
  23. package/dist/cjs/src/interface.js.map +1 -0
  24. package/dist/cjs/src/interface.spec.d.ts +3 -0
  25. package/dist/cjs/src/interface.spec.d.ts.map +1 -0
  26. package/dist/cjs/src/interface.spec.js +11 -0
  27. package/dist/cjs/src/interface.spec.js.map +1 -0
  28. package/dist/cjs/src/misc.d.ts +17 -0
  29. package/dist/cjs/src/misc.d.ts.map +1 -0
  30. package/dist/cjs/src/misc.js +137 -0
  31. package/dist/cjs/src/misc.js.map +1 -0
  32. package/dist/cjs/src/misc.spec.d.ts +3 -0
  33. package/dist/cjs/src/misc.spec.d.ts.map +1 -0
  34. package/dist/cjs/src/misc.spec.js +51 -0
  35. package/dist/cjs/src/misc.spec.js.map +1 -0
  36. package/dist/cjs/src/mod.d.ts +9 -0
  37. package/dist/cjs/src/mod.d.ts.map +1 -0
  38. package/dist/cjs/src/mod.js +12 -0
  39. package/dist/cjs/src/mod.js.map +1 -0
  40. package/dist/cjs/src/pure-functions/sized-chunk-transformer.d.ts +13 -0
  41. package/dist/cjs/src/pure-functions/sized-chunk-transformer.d.ts.map +1 -0
  42. package/dist/cjs/src/pure-functions/sized-chunk-transformer.js +45 -0
  43. package/dist/cjs/src/pure-functions/sized-chunk-transformer.js.map +1 -0
  44. package/dist/cjs/src/pure-functions/sized-chunk-transformer.spec.d.ts +3 -0
  45. package/dist/cjs/src/pure-functions/sized-chunk-transformer.spec.d.ts.map +1 -0
  46. package/dist/cjs/src/pure-functions/sized-chunk-transformer.spec.js +39 -0
  47. package/dist/cjs/src/pure-functions/sized-chunk-transformer.spec.js.map +1 -0
  48. package/dist/cjs/src/qrcode.d.ts +6 -0
  49. package/dist/cjs/src/qrcode.d.ts.map +1 -0
  50. package/dist/cjs/src/qrcode.js +35 -0
  51. package/dist/cjs/src/qrcode.js.map +1 -0
  52. package/dist/cjs/src/qrcode.spec.d.ts +3 -0
  53. package/dist/cjs/src/qrcode.spec.d.ts.map +1 -0
  54. package/dist/cjs/src/qrcode.spec.js +47 -0
  55. package/dist/cjs/src/qrcode.spec.js.map +1 -0
  56. package/dist/cjs/src/urn-registry/mod.d.ts +3 -0
  57. package/dist/cjs/src/urn-registry/mod.d.ts.map +1 -0
  58. package/dist/cjs/src/urn-registry/mod.js +6 -0
  59. package/dist/cjs/src/urn-registry/mod.js.map +1 -0
  60. package/dist/cjs/src/urn-registry/random-uuid.d.ts +4 -0
  61. package/dist/cjs/src/urn-registry/random-uuid.d.ts.map +1 -0
  62. package/dist/cjs/src/urn-registry/random-uuid.js +30 -0
  63. package/dist/cjs/src/urn-registry/random-uuid.js.map +1 -0
  64. package/dist/cjs/src/urn-registry/uniform-resource-name-registry.d.ts +76 -0
  65. package/dist/cjs/src/urn-registry/uniform-resource-name-registry.d.ts.map +1 -0
  66. package/dist/cjs/src/urn-registry/uniform-resource-name-registry.js +259 -0
  67. package/dist/cjs/src/urn-registry/uniform-resource-name-registry.js.map +1 -0
  68. package/dist/cjs/src/urn-registry/uniform-resource-name-registry.spec.d.ts +3 -0
  69. package/dist/cjs/src/urn-registry/uniform-resource-name-registry.spec.d.ts.map +1 -0
  70. package/dist/cjs/src/urn-registry/uniform-resource-name-registry.spec.js +81 -0
  71. package/dist/cjs/src/urn-registry/uniform-resource-name-registry.spec.js.map +1 -0
  72. package/dist/cjs/src/urn-registry/uuid-to-big-int.d.ts +8 -0
  73. package/dist/cjs/src/urn-registry/uuid-to-big-int.d.ts.map +1 -0
  74. package/dist/cjs/src/urn-registry/uuid-to-big-int.js +16 -0
  75. package/dist/cjs/src/urn-registry/uuid-to-big-int.js.map +1 -0
  76. package/dist/cjs/src/urn-registry/uuid-to-big-int.spec.d.ts +3 -0
  77. package/dist/cjs/src/urn-registry/uuid-to-big-int.spec.d.ts.map +1 -0
  78. package/dist/cjs/src/urn-registry/uuid-to-big-int.spec.js +17 -0
  79. package/dist/cjs/src/urn-registry/uuid-to-big-int.spec.js.map +1 -0
  80. package/dist/cjs/src/version.d.ts +5 -0
  81. package/dist/cjs/src/version.d.ts.map +1 -0
  82. package/dist/cjs/src/version.js +8 -0
  83. package/dist/cjs/src/version.js.map +1 -0
  84. package/dist/cjs/src/version.spec.d.ts +3 -0
  85. package/dist/cjs/src/version.spec.d.ts.map +1 -0
  86. package/dist/cjs/src/version.spec.js +9 -0
  87. package/dist/cjs/src/version.spec.js.map +1 -0
  88. package/dist/cjs/tests/integration.spec.d.ts +3 -0
  89. package/dist/cjs/tests/integration.spec.d.ts.map +1 -0
  90. package/dist/cjs/tests/integration.spec.js +8 -0
  91. package/dist/cjs/tests/integration.spec.js.map +1 -0
  92. package/dist/cjs/tests/network-timeout.spec.d.ts +3 -0
  93. package/dist/cjs/tests/network-timeout.spec.d.ts.map +1 -0
  94. package/dist/cjs/tests/network-timeout.spec.js +121 -0
  95. package/dist/cjs/tests/network-timeout.spec.js.map +1 -0
  96. package/dist/esm/src/config.d.ts +4 -0
  97. package/dist/esm/src/config.d.ts.map +1 -0
  98. package/dist/esm/src/config.js +5 -0
  99. package/dist/esm/src/config.js.map +1 -0
  100. package/dist/esm/src/file-box.d.ts +209 -0
  101. package/dist/esm/src/file-box.d.ts.map +1 -0
  102. package/dist/esm/src/file-box.js +775 -0
  103. package/dist/esm/src/file-box.js.map +1 -0
  104. package/dist/esm/src/file-box.spec.d.ts +14 -0
  105. package/dist/esm/src/file-box.spec.d.ts.map +1 -0
  106. package/dist/esm/src/file-box.spec.js +386 -0
  107. package/dist/esm/src/file-box.spec.js.map +1 -0
  108. package/dist/esm/src/file-box.type.d.ts +103 -0
  109. package/dist/esm/src/file-box.type.d.ts.map +1 -0
  110. package/dist/esm/src/file-box.type.js +31 -0
  111. package/dist/esm/src/file-box.type.js.map +1 -0
  112. package/dist/esm/src/interface.d.ts +25 -0
  113. package/dist/esm/src/interface.d.ts.map +1 -0
  114. package/dist/esm/src/interface.js +2 -0
  115. package/dist/esm/src/interface.js.map +1 -0
  116. package/dist/esm/src/interface.spec.d.ts +3 -0
  117. package/dist/esm/src/interface.spec.d.ts.map +1 -0
  118. package/dist/esm/src/interface.spec.js +9 -0
  119. package/dist/esm/src/interface.spec.js.map +1 -0
  120. package/dist/esm/src/misc.d.ts +17 -0
  121. package/dist/esm/src/misc.d.ts.map +1 -0
  122. package/dist/esm/src/misc.js +126 -0
  123. package/dist/esm/src/misc.js.map +1 -0
  124. package/dist/esm/src/misc.spec.d.ts +3 -0
  125. package/dist/esm/src/misc.spec.d.ts.map +1 -0
  126. package/dist/esm/src/misc.spec.js +49 -0
  127. package/dist/esm/src/misc.spec.js.map +1 -0
  128. package/dist/esm/src/mod.d.ts +9 -0
  129. package/dist/esm/src/mod.d.ts.map +1 -0
  130. package/dist/esm/src/mod.js +6 -0
  131. package/dist/esm/src/mod.js.map +1 -0
  132. package/dist/esm/src/pure-functions/sized-chunk-transformer.d.ts +13 -0
  133. package/dist/esm/src/pure-functions/sized-chunk-transformer.d.ts.map +1 -0
  134. package/dist/esm/src/pure-functions/sized-chunk-transformer.js +39 -0
  135. package/dist/esm/src/pure-functions/sized-chunk-transformer.js.map +1 -0
  136. package/dist/esm/src/pure-functions/sized-chunk-transformer.spec.d.ts +3 -0
  137. package/dist/esm/src/pure-functions/sized-chunk-transformer.spec.d.ts.map +1 -0
  138. package/dist/esm/src/pure-functions/sized-chunk-transformer.spec.js +37 -0
  139. package/dist/esm/src/pure-functions/sized-chunk-transformer.spec.js.map +1 -0
  140. package/dist/esm/src/qrcode.d.ts +6 -0
  141. package/dist/esm/src/qrcode.d.ts.map +1 -0
  142. package/dist/esm/src/qrcode.js +27 -0
  143. package/dist/esm/src/qrcode.js.map +1 -0
  144. package/dist/esm/src/qrcode.spec.d.ts +3 -0
  145. package/dist/esm/src/qrcode.spec.d.ts.map +1 -0
  146. package/dist/esm/src/qrcode.spec.js +45 -0
  147. package/dist/esm/src/qrcode.spec.js.map +1 -0
  148. package/dist/esm/src/urn-registry/mod.d.ts +3 -0
  149. package/dist/esm/src/urn-registry/mod.d.ts.map +1 -0
  150. package/dist/esm/src/urn-registry/mod.js +3 -0
  151. package/dist/esm/src/urn-registry/mod.js.map +1 -0
  152. package/dist/esm/src/urn-registry/random-uuid.d.ts +4 -0
  153. package/dist/esm/src/urn-registry/random-uuid.d.ts.map +1 -0
  154. package/dist/esm/src/urn-registry/random-uuid.js +4 -0
  155. package/dist/esm/src/urn-registry/random-uuid.js.map +1 -0
  156. package/dist/esm/src/urn-registry/uniform-resource-name-registry.d.ts +76 -0
  157. package/dist/esm/src/urn-registry/uniform-resource-name-registry.d.ts.map +1 -0
  158. package/dist/esm/src/urn-registry/uniform-resource-name-registry.js +253 -0
  159. package/dist/esm/src/urn-registry/uniform-resource-name-registry.js.map +1 -0
  160. package/dist/esm/src/urn-registry/uniform-resource-name-registry.spec.d.ts +3 -0
  161. package/dist/esm/src/urn-registry/uniform-resource-name-registry.spec.d.ts.map +1 -0
  162. package/dist/esm/src/urn-registry/uniform-resource-name-registry.spec.js +79 -0
  163. package/dist/esm/src/urn-registry/uniform-resource-name-registry.spec.js.map +1 -0
  164. package/dist/esm/src/urn-registry/uuid-to-big-int.d.ts +8 -0
  165. package/dist/esm/src/urn-registry/uuid-to-big-int.d.ts.map +1 -0
  166. package/dist/esm/src/urn-registry/uuid-to-big-int.js +13 -0
  167. package/dist/esm/src/urn-registry/uuid-to-big-int.js.map +1 -0
  168. package/dist/esm/src/urn-registry/uuid-to-big-int.spec.d.ts +3 -0
  169. package/dist/esm/src/urn-registry/uuid-to-big-int.spec.d.ts.map +1 -0
  170. package/dist/esm/src/urn-registry/uuid-to-big-int.spec.js +15 -0
  171. package/dist/esm/src/urn-registry/uuid-to-big-int.spec.js.map +1 -0
  172. package/dist/esm/src/version.d.ts +5 -0
  173. package/dist/esm/src/version.d.ts.map +1 -0
  174. package/dist/esm/src/version.js +5 -0
  175. package/dist/esm/src/version.js.map +1 -0
  176. package/dist/esm/src/version.spec.d.ts +3 -0
  177. package/dist/esm/src/version.spec.d.ts.map +1 -0
  178. package/dist/esm/src/version.spec.js +7 -0
  179. package/dist/esm/src/version.spec.js.map +1 -0
  180. package/dist/esm/tests/integration.spec.d.ts +3 -0
  181. package/dist/esm/tests/integration.spec.d.ts.map +1 -0
  182. package/dist/esm/tests/integration.spec.js +6 -0
  183. package/dist/esm/tests/integration.spec.js.map +1 -0
  184. package/dist/esm/tests/network-timeout.spec.d.ts +3 -0
  185. package/dist/esm/tests/network-timeout.spec.d.ts.map +1 -0
  186. package/dist/esm/tests/network-timeout.spec.js +119 -0
  187. package/dist/esm/tests/network-timeout.spec.js.map +1 -0
  188. package/package.json +85 -0
  189. package/src/config.ts +5 -0
  190. package/src/file-box.spec.ts +480 -0
  191. package/src/file-box.ts +1042 -0
  192. package/src/file-box.type.ts +145 -0
  193. package/src/interface.spec.ts +17 -0
  194. package/src/interface.ts +47 -0
  195. package/src/misc.spec.ts +70 -0
  196. package/src/misc.ts +157 -0
  197. package/src/mod.ts +33 -0
  198. package/src/pure-functions/sized-chunk-transformer.spec.ts +52 -0
  199. package/src/pure-functions/sized-chunk-transformer.ts +49 -0
  200. package/src/qrcode.spec.ts +55 -0
  201. package/src/qrcode.ts +38 -0
  202. package/src/typings.d.ts +1 -0
  203. package/src/urn-registry/mod.ts +7 -0
  204. package/src/urn-registry/random-uuid.ts +7 -0
  205. package/src/urn-registry/uniform-resource-name-registry.spec.ts +109 -0
  206. package/src/urn-registry/uniform-resource-name-registry.ts +342 -0
  207. package/src/urn-registry/uuid-to-big-int.spec.ts +19 -0
  208. package/src/urn-registry/uuid-to-big-int.ts +16 -0
  209. package/src/version.spec.ts +9 -0
  210. package/src/version.ts +4 -0
package/README.md ADDED
@@ -0,0 +1,613 @@
1
+ # file-box
2
+
3
+ [![NPM Version](https://badge.fury.io/js/file-box.svg)](https://badge.fury.io/js/file-box)
4
+ [![NPM](https://github.com/huan/file-box/workflows/NPM/badge.svg)](https://github.com/huan/file-box/actions?query=workflow%3ANPM)
5
+ [![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-blue.svg)](https://www.typescriptlang.org/)
6
+ [![ES Modules](https://img.shields.io/badge/ES-Modules-brightgreen)](https://github.com/Chatie/tsconfig/issues/16)
7
+
8
+ FileBox is a virtual container for packing a file data into it for future read, and easily transport between servers with the least payload, no mater than where it is (local path, remote url, or cloud storage).
9
+
10
+ ![File Box](https://huan.github.io/file-box/images/file-box-logo.jpg)
11
+
12
+ Currently the FileBox supports almost all kinds of the data input/output methods/formats:
13
+
14
+ | File Type | Pack Method | Unpack Method | Description |
15
+ | :--- | :--- | :--- | :--- |
16
+ | Local File | `fromFile()` | `toFile()` | Local file in file system |
17
+ | Remote URL | `fromUrl()` | `toUrl()`(TBW) | Remote file in a HTTP/HTTPS URL |
18
+ | Buffer | `fromBuffer()` | `toBuffer()` | JavaScript Buffer |
19
+ | Stream | `fromStream()` | `toStream()` | JavaScript Stream |
20
+ | Base64 | `fromBase64()` | `toBase64()` | Base64 data |
21
+ | DataURL | `fromDataURL()` | `toDataURL()` | DataURL data |
22
+ | QRCode | `fromQRCode()` | `toQRCode()` | QR Code Image Decode/Encode |
23
+ | UUID | `fromUuid()` | `toUuid()` | UUID by loader/saver helper functions |
24
+ | JSON | `fromJSON()` | `toJSON()` | Serialize/Deserialize FileBox |
25
+
26
+ ## Examples
27
+
28
+ The following example demos:
29
+
30
+ 1. Load local file
31
+ 1. Save URL to local file
32
+ 1. Convert buffer to stream
33
+ 1. Pack from Base64 then Unpack to DataURL
34
+
35
+ ```ts
36
+ import { FileBox } from 'file-box'
37
+
38
+ /**
39
+ * 0. Load local file
40
+ */
41
+ const fileBox0 = FileBox.fromFile('/tmp/file.jpg')
42
+
43
+ /**
44
+ * 1. Save URL to File
45
+ */
46
+ const fileBox1 = FileBox.fromUrl(
47
+ 'https://huan.github.io/file-box/images/file-box-logo.jpg',
48
+ 'logo.jpg',
49
+ )
50
+ fileBox1.toFile('/tmp/file-box-logo.jpg')
51
+
52
+ /**
53
+ * 2. Convert Buffer to Stream
54
+ */
55
+ import fs from 'fs'
56
+ const fileBox2 = FileBox.fromBuffer(
57
+ Buffer.from('world'),
58
+ 'hello.txt',
59
+ )
60
+ const writeStream = fs.createWriteStream('/tmp/hello.txt')
61
+ fileBox2.pipe(writeStream)
62
+
63
+ /**
64
+ * 3. Pack Base64, Unpack to DataURL
65
+ */
66
+ const fileBox3 = FileBox.fromBase64('d29ybGQK', 'hello.txt')
67
+ fileBox3.toDataURL()
68
+ .then(console.log)
69
+ // Output: data:text/plain;base64,d29ybGQK
70
+ ```
71
+
72
+ ## Known Issues
73
+
74
+ 1. TypeError [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped characters [#56](https://github.com/huan/file-box/issues/56)
75
+
76
+ ## API Reference
77
+
78
+ [API docs on Paka](https://paka.dev/npm/file-box)
79
+
80
+ ### 1. Load File in to Box
81
+
82
+ #### 1.1 `fromFile(filePath: string, name?: string, md5?: string): FileBox`
83
+
84
+ Alias: `fromLocal()`
85
+
86
+ About optional arguments:
87
+
88
+ name: filename, if not passed, will be parsed from file path, url, etc.
89
+
90
+ md5: file md5 code, only for checking purposes and will not be computed from file.
91
+
92
+ ```ts
93
+ const fileBox = FileBox.fromLocal('/tmp/test.txt')
94
+ ```
95
+
96
+ #### 1.2 `fromUrl(url: string, options?: {headers?: HTTP.OutgoingHttpHeaders, name?: string, size?: string, md5?: string}): FileBox`
97
+
98
+
99
+ Alias: `fromRemote()`
100
+
101
+ ```ts
102
+ const fileBox = FileBox.fromUrl(
103
+ 'https://huan.github.io/file-box/images/file-box-logo.jpg',
104
+ {name: 'logo.jpg'},
105
+ )
106
+ ```
107
+
108
+ #### 1.3 `fromStream(stream: Readable, name?: string, md5?: string): FileBox`
109
+
110
+ Will be named 'stream.dat' if no name is provided.
111
+
112
+ ```ts
113
+ const fileBox = FileBox.fromStream(res, '/tmp/download.zip')
114
+ ```
115
+
116
+ #### 1.4 `fromBuffer(buffer: Buffer, name?: string, md5?: string): FileBox`
117
+
118
+ Will be named 'buffer.dat' if no name is provided.
119
+
120
+ ```ts
121
+ const fileBox = FileBox.fromBuffer(buf, '/tmp/download.zip')
122
+ ```
123
+
124
+ #### 1.5 `FileBox.fromBase64(base64: string, name?: string, md5?: string): FileBox`
125
+
126
+ Decoded a base64 encoded file data. Will be named 'base64.dat' if no name is provided.
127
+
128
+
129
+ ```ts
130
+ const fileBox = FileBox.fromBase64('d29ybGQK', 'hello.txt')
131
+ fileBox.toFile()
132
+ ```
133
+
134
+ #### 1.6 `FileBox.fromDataURL(dataUrl: string, name?: string, md5?: string): FileBox`
135
+
136
+ Decoded a DataURL data. Will be named 'data-url.dat' if no name is provided.
137
+
138
+ ```ts
139
+ const fileBox = FileBox.fromDataURL('data:text/plain;base64,d29ybGQK', 'hello.txt')
140
+ fileBox.toFile()
141
+ ```
142
+
143
+ #### 1.7 `FileBox.fromJSON()`
144
+
145
+ Restore a `FileBox.toJSON()` text string back to a FileBox instance.
146
+
147
+ ```ts
148
+ const restoredFileBox = FileBox.fromJSON(jsonText)
149
+ ```
150
+
151
+ #### 1.8 `FileBox.fromQRCode(qrCodeValue: string, md5?: string)`
152
+
153
+ Get a FileBox instance that represent a QR Code value.
154
+
155
+ ```ts
156
+ const fileBox = FileBox.fromQRCode('https://github.com')
157
+ fileBox.toFile('qrcode.png')
158
+ ```
159
+
160
+ #### 1.9 `FileBox.fromUuid(uuid: string, options?: {name?: string, size?: string, md5?: string})`
161
+
162
+ Load a FileBox from a UUID. Will be named `${uuid}.dat` if no name is provided.
163
+
164
+ See: `FileBox.setUuidLoader()`
165
+
166
+ ### 2. Get File out from Box
167
+
168
+ ### 2.1 `toFile(name?: string): Promise<void>`
169
+
170
+ Save file to current work path(cwd) of the local file system with the default `name`.
171
+
172
+ if `name` specified with a full path, then will use the speficied file name instead.
173
+
174
+ ```ts
175
+ const fileBox = FileBox.fromRemote(
176
+ 'https://huan.github.io/file-box/images/file-box-logo.jpg',
177
+ )
178
+ await fileBox.toFile('/tmp/logo.jpg')
179
+ ```
180
+
181
+ #### 2.2 `toStream(): Readable`
182
+
183
+ Get the stream of file data.
184
+
185
+ ```ts
186
+ const fileBox = FileBox.fromRemote(
187
+ 'https://huan.github.io/file-box/images/file-box-logo.jpg',
188
+ )
189
+ const readableStream = fileBox.toStream()
190
+ ```
191
+
192
+ #### 2.3 `pipe(destination: Writable): Promise<void>`
193
+
194
+ Pipe to a writable stream.
195
+
196
+ ```ts
197
+ const fileBox = FileBox.fromRemote(
198
+ 'https://huan.github.io/file-box/images/file-box-logo.jpg',
199
+ )
200
+ const writableStream = fs.createWritable('/tmp/logo.jpg')
201
+ fileBox.pipe(writableStream)
202
+ ```
203
+
204
+ #### 2.4 `toBase64(): Promise<string>`
205
+
206
+ Get the base64 data of file.
207
+
208
+ ```ts
209
+ const fileBox = FileBox.fromRemote(
210
+ 'https://huan.github.io/file-box/images/file-box-logo.jpg',
211
+ )
212
+ const base64Text = await fileBox.toBase64()
213
+ console.log(base64Text) // Output: the base64 encoded data of the file
214
+ ```
215
+
216
+ #### 2.5 `toJSON(): string`
217
+
218
+ Get the `JSON.stringify`-ed text.
219
+
220
+ **Not Implement Yet: Working In Progress...**
221
+
222
+ ```ts
223
+ const fileBox = FileBox.fromRemote(
224
+ 'https://huan.github.io/file-box/images/file-box-logo.jpg',
225
+ )
226
+ const jsonText1 = fileBox.toJSON()
227
+ const jsonText2 = JSON.stringify(fileBox)
228
+ assert(jsonText1 === jsonText2)
229
+
230
+ console.log(jsonText1) // Output: the stringified data of the fileBox
231
+
232
+ const restoredFileBox = fileBox.fromJSON(jsonText1)
233
+ restoredFileBox.toFile('/tmp/file-box-logo.jpg')
234
+ ```
235
+
236
+ #### 2.6 `toDataURL(): Promise<string>`
237
+
238
+ Get the DataURL of the file.
239
+
240
+ ```ts
241
+ const fileBox = FileBox.fromFile('tests/fixtures/hello.txt')
242
+ const dataUrl = await fileBox.toDataURL()
243
+ console.log(dataUrl) // Output: data:text/plain;base64,d29ybGQK'
244
+ ```
245
+
246
+ #### 2.7 `toBuffer(): Promise<Buffer>`
247
+
248
+ Get the Buffer of the file.
249
+
250
+ ```ts
251
+ const fileBox = FileBox.fromFile('tests/fixtures/hello.txt')
252
+ const buffer = await fileBox.toBuffer()
253
+ console.log(buffer.toString()) // Output: world
254
+ ```
255
+
256
+ #### 2.8 `toQRCode(): Promise<string>`
257
+
258
+ Decode the QR Code value from the file.
259
+
260
+ ```ts
261
+ const fileBox = FileBox.fromFile('qrcode.jpg')
262
+ const qrCodeValue = await fileBox.toQRCode()
263
+ console.log(`QR Code decoded value is: "${qrCodeValue}"`)
264
+ // Output: QR Code decoded value is: "https://github.com"
265
+ ```
266
+
267
+ #### 2.9 `toUuid(): Promise<string>`
268
+
269
+ Save the FileBox to a UUID file and return the UUID.
270
+
271
+ See: `FileBox.setUuidSaver()`
272
+
273
+ #### 2.10 `toJSON(): string`
274
+
275
+ Encode a FileBox instance to JSON string so that we can transfer the FileBox on the wire.
276
+
277
+ ```ts
278
+ const fileBox = FileBox.fromBase64('RmlsZUJveEJhc2U2NAo=')
279
+
280
+ const jsonText = JSON.stringify(fileBox)
281
+ // the above code equals to the following line of code:
282
+ // const jsonText = fileBox.toJSON()
283
+
284
+ // we will get the serialized data for this FileBox:
285
+ console.log(jsonText)
286
+ // Output: {"name":"qrcode.png","metadata":{},"boxType":1,"base64":"RmlsZUJveEJhc2U2NAo="}
287
+
288
+ // restore our fleBox:
289
+ // const newFileBox = FileBox.fromJSON(jsonText)
290
+ ```
291
+
292
+ ##### Limitation
293
+
294
+ Because we want to enable the `JSON.stringify(fileBox)`, which will call `fileBox.toJSON()`, so the `toJSON()` can not be `async`, which means we can only support limited FileBoxType(s):
295
+
296
+ 1. FileBoxType.Base64
297
+ 1. FileBoxType.Url
298
+ 1. FileBoxType.QRCode
299
+
300
+ For other types like `FileBoxType.File`, `FileBoxType.Buffer`, `FileBoxType.Stream`, etc, we need to transform them to `FileBoxType.Base64` before we call `toJSON`:
301
+
302
+ ```ts
303
+ const fileBoxLazy = FileBox.fromFile('./test.txt')
304
+ const base64 = await fileBoxLazy.toBase64()
305
+
306
+ const fileBox = FileBox.fromBase64(base64, 'test.txt')
307
+ // fileBox will be serializable because it do not need async operations
308
+
309
+ const jsonText = JSON.stringify(fileBox)
310
+ console.log(jsonText)
311
+ ```
312
+
313
+ ### 3. Misc
314
+
315
+ #### 3.1 `name`
316
+
317
+ File name of the file in the box
318
+
319
+ ```ts
320
+ const fileBox = FileBox.fromRemote(
321
+ 'https://huan.github.io/file-box/images/file-box-logo.jpg',
322
+ )
323
+ console.log(fileBox.name) // Output: file-box-logo.jpg
324
+ ```
325
+
326
+ #### 3.2 `md5`
327
+
328
+ File md5 of the file in the box. The value is set by user, not computed from file.
329
+
330
+ ```ts
331
+ const fileBox = FileBox.fromUrl(
332
+ 'https://huan.github.io/file-box/images/file-box-logo.jpg',
333
+ {md5: 'computed-md5-string'}
334
+ )
335
+ console.log(fileBox.md5) // Output: computed-md5-string
336
+ ```
337
+
338
+ #### 3.3 `metadata: Metadata { [key: string]: any }`
339
+
340
+ Metadata for the file in the box. This value can only be assigned once, and will be immutable afterwards, all following assign or modify actions on `metadata` will throw errors
341
+
342
+ ```ts
343
+ const fileBox = FileBox.fromRemote(
344
+ 'https://huan.github.io/file-box/images/file-box-logo.jpg',
345
+ )
346
+ fileBox.metadata = {
347
+ author : 'huan',
348
+ githubRepo : 'https://github.com/huan/file-box',
349
+ }
350
+
351
+ console.log(fileBox.metadata) // Output: { author: 'huan', githubRepo: 'https://github.com/huan/file-box' }
352
+ fileBox.metadata.author = 'Tank' // Will throw exception
353
+ ```
354
+
355
+ #### 3.4 `version(): string`
356
+
357
+ Version of the FileBox
358
+
359
+ #### 3.5 `toJSON(): string`
360
+
361
+ Serialize FileBox metadata to JSON.
362
+
363
+ #### 3.6 `ready(): Promise<void>`
364
+
365
+ Update the necessary internal data and make everything ready for use.
366
+
367
+ #### 3.7 `syncRemoteName(): Promise<void>`
368
+
369
+ Sync the filename with the HTTP Response Header
370
+
371
+ HTTP Header Example:
372
+ > Content-Disposition: attachment; filename="filename.ext"
373
+
374
+ #### 3.8 `type: FileBoxType`
375
+
376
+ Return the type of the current FileBox instance.
377
+
378
+ The currently supported types are defined at [file-box-type.ts](https://github.com/huan/file-box/blob/master/src/file-box.type.ts#L15) as the following demonstrated:
379
+
380
+ ```ts
381
+ enum FileBoxType {
382
+ Unknown = 0,
383
+ Base64 = 1,
384
+ Url = 2,
385
+ QRCode = 3,
386
+ Buffer = 4,
387
+ File = 5,
388
+ Stream = 6,
389
+ Uuid = 7,
390
+ }
391
+ ```
392
+
393
+ #### 3.9 `FileBox.setUuidLoader(loader: UuidLoader): void`
394
+
395
+ Required by static method `FileBox.fromUuid()`
396
+
397
+ ```ts
398
+ class FileBoxUuid extends FileBox {}
399
+
400
+ const loader: UuidLoader = async (uuid: string) => {
401
+ const stream = new PassThrough()
402
+ stream.end('hello, world!')
403
+ return stream
404
+ })
405
+
406
+ FileBoxUuid.setUuidLoader(loader)
407
+ const fileBox = FileBoxUuid.fromUuid('12345678-1234-1234-1234-123456789012', 'test.txt')
408
+ await fileBox.toFile('test.txt')
409
+ ```
410
+
411
+ The `UuidLoader` is a function that takes a UUID and return a readable stream.
412
+
413
+ ```ts
414
+ type UuidLoader = (this: FileBox, uuid: string) => Readable
415
+ ```
416
+
417
+ #### 3.10 `FileBox.setUuidSaver(saver: UuidSaver): void`
418
+
419
+ Required by instance method `fileBox.toUuid()`
420
+
421
+ ```ts
422
+ class FileBoxUuid extends FileBox {}
423
+
424
+ const saver: UuidSaver = async (stream: Readable) => {
425
+ // save the stream and get uuid
426
+ return '12345678-1234-1234-1234-123456789012'
427
+ })
428
+
429
+ FileBoxUuid.setUuidSaver(saver)
430
+
431
+ const fileBox = FileBoxUuid.fromFile('test.txt')
432
+ const uuid = await fileBox.toUuid()
433
+ ```
434
+
435
+ The `UuidSaver` is a function that takes a readable stream and return a UUID promise.
436
+
437
+ ```ts
438
+ type UuidSaver = (this: FileBox, stream: Readable) => Promise<string>
439
+ ```
440
+
441
+ #### 3.11 `size`
442
+
443
+ The file box size in bytes. (`-1` means unknown)
444
+
445
+ It is not the size of the target (boxed) file itself.
446
+
447
+ For example:
448
+
449
+ ```ts
450
+ const fileBox = FileBox.fromUrl('http://example.com/image.png')
451
+ console.log(fileBox.size)
452
+ // > 20 <- this is the length of the URL string
453
+ ```
454
+
455
+ #### 3.12 `remoteSize`
456
+
457
+ The remote file size in bytes. (`-1` or `undefined` means unknown)
458
+
459
+ For example:
460
+
461
+ ```ts
462
+ const fileBox = FileBox.fromUrl('http://example.com/image.png')
463
+ await fileBox.ready()
464
+ console.log(fileBox.remoteSize)
465
+ // > 102400 <- this is the size of the remote image.png
466
+ ```
467
+
468
+ ## Features
469
+
470
+ 1. Present A File by Abstracting It's Meta Information that supports Reading & toJSON() API.
471
+ 1. Follow DOM File/BLOB Interface
472
+ 1. Present a file that could be: Local, Remote, Stream
473
+ 1. Lazy load
474
+ 1. Serializable
475
+ 1. Can be Transfered from server to server, server to browser.
476
+
477
+ ## Environments
478
+
479
+ Environment variables can be used to control some behavior.
480
+
481
+ - `FILEBOX_HTTP_TIMEOUT` [default=60000] Socket idle timeout when FileBox downloads data from URL. For example, when the network is temporarily interrupted, the request will be considered as failed after waiting for a specified time.
482
+
483
+ ## SCHEMAS
484
+
485
+ ### Url
486
+
487
+ [Node.js Documentation > URL Strings and URL Objects](https://nodejs.org/docs/latest/api/url.html#url_url_strings_and_url_objects)
488
+
489
+ ```asciiart
490
+ ┌─────────────────────────────────────────────────────────────────────────────────────────────┐
491
+ │ href │
492
+ ├──────────┬──┬─────────────────────┬─────────────────────┬───────────────────────────┬───────┤
493
+ │ protocol │ │ auth │ host │ path │ hash │
494
+ │ │ │ ├──────────────┬──────┼──────────┬────────────────┤ │
495
+ │ │ │ │ hostname │ port │ pathname │ search │ │
496
+ │ │ │ │ │ │ ├─┬──────────────┤ │
497
+ │ │ │ │ │ │ │ │ query │ │
498
+ " https: // user : pass @ sub.host.com : 8080 /p/a/t/h ? query=string #hash "
499
+ │ │ │ │ │ hostname │ port │ │ │ │
500
+ │ │ │ │ ├──────────────┴──────┤ │ │ │
501
+ │ protocol │ │ username │ password │ host │ │ │ │
502
+ ├──────────┴──┼──────────┴──────────┼─────────────────────┤ │ │ │
503
+ │ origin │ │ origin │ pathname │ search │ hash │
504
+ ├─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────────┴───────┤
505
+ │ href │
506
+ └─────────────────────────────────────────────────────────────────────────────────────────────┘
507
+ ```
508
+
509
+ ### Path
510
+
511
+ [Node.js Documentation > path.parse(path)](https://nodejs.org/api/path.html#path_path_parse_path)
512
+
513
+ ```asciiart
514
+ ┌─────────────────────┬────────────┐
515
+ │ dir │ base │
516
+ ├──────┬ ├──────┬─────┤
517
+ │ root │ │ name │ ext │
518
+ " / home/user/dir / file .txt "
519
+ └──────┴──────────────┴──────┴─────┘
520
+ ```
521
+
522
+ ## History
523
+
524
+ ### main v1.7 (Feb 18, 2023)
525
+
526
+ 1. Environment variables `FILEBOX_HTTP_TIMEOUT` can be set by user. see [Environments](#Environments). ([#80](https://github.com/huan/file-box/issues/80), by @[binsee](https://github.com/binsee))
527
+
528
+ ### main v1.5 (Jan 18, 2022)
529
+
530
+ 1. `fileBox.md5` can be set by user. This filed is for the receiver of the filebox to check, and it is not computed from the file.
531
+
532
+ ### main v1.4 (Nov 14, 2021)
533
+
534
+ 1. `fileBox.size` will be serialized to/from JSON, and present the `Content-Length` of the file. (`-1` means unknown)
535
+ 1. `mimeType` has been renamed to `mediaType`, and added to the `FileBoxInterface`
536
+
537
+ ### v1.0 (Oct 20, 2021)
538
+
539
+ 1. Suppert ES Module. ([#54](https://github.com/huan/file-box/issues/54))
540
+ 1. Add UUID boxType support: `FileBox.fromUuid()` and `FileBox.toUuid()`
541
+ 1. Add `size` property to return the size of the file. (`-1` means unknown)
542
+ 1. Add `remoteSize` property to present the remote size of the file (if applicable, `-1` means unknown)
543
+ 1. Add `UniformResourceNameRegistry` class for providing a production-ready basic UUID management tool.
544
+ 1. Add `FileBoxInterface`, `FileBox.isInstance()`, and `FileBox.isInterface()`
545
+
546
+ Breaking changes:
547
+
548
+ 1. `toJSON` format renamed `boxType` to `type`
549
+ 1. `type()` has been changed to `type`
550
+ 1. `version()` has been changed to `version`
551
+
552
+ ### v0.16 master
553
+
554
+ 1. Throw error when consume a stream twice to prevent data lost. ([#50](https://github.com/huan/file-box/issues/50))
555
+
556
+ ### v0.14 (Oct 2020)
557
+
558
+ 1. Add `fileBox.type()` to return the `FileBoxType` of a FileBox. ([wechaty/wechaty#1918](https://github.com/wechaty/wechaty/issues/1918#issuecomment-590146993))
559
+ 1. Change `Readable` to `stream.Readable` for better compatibility. (Jun 27, 2020)
560
+ 1. Add `chunkerTransformStream` to `toStream` ([#44](https://github.com/huan/file-box/issues/44))
561
+
562
+ ### v0.12 (Feb 2020)
563
+
564
+ Add support to `JSON.stringify()` ([#25](https://github.com/huan/file-box/issues/25)):
565
+
566
+ 1. `FileBox.fromJSON()` - Static method for deserialization
567
+ 1. `fileBox.toJSON()` - Instance method for serialization
568
+
569
+ ### v0.10 (Jan 2020)
570
+
571
+ 1. Add support to QR Code: `FileBox.fromQRCode()` and `FileBox.toQRCode()`
572
+ 1. Start using @chatie/tsconfig
573
+
574
+ ### v0.8 (Jun 2018)
575
+
576
+ 1. Add two new factory methods: `fromBase64()`, `fromDataURL()`
577
+ 1. Add `toBuffer()`, `toBase64()` and `toDataURL()` to get the Buffer and BASE64 encoded file data
578
+ 1. Add `metadata` property to store additional information. ([#3](https://github.com/huan/file-box/issues/3))
579
+
580
+ ### v0.4 (May 2018)
581
+
582
+ 1. Add `headers` option for `fromRemote()` method
583
+
584
+ ### v0.2 (Apr 2018)
585
+
586
+ Initial version.
587
+
588
+ ## See Also
589
+
590
+ * [File API - W3C Working Draft, 26 October 2017](https://www.w3.org/TR/FileAPI/)
591
+ * [MIME Sniffing - Living Standard — Last Updated 20 April 2018](https://mimesniff.spec.whatwg.org/#parsable-mime-type)
592
+ * [Using files from web applications](https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications)
593
+ * [Web technology for developers > Web APIs > File](https://developer.mozilla.org/en-US/docs/Web/API/File)
594
+ * [Web technology for developers > Web APIs > Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob)
595
+ * [Web technology for developers > Web APIs > FileReader](https://developer.mozilla.org/en-US/docs/Web/API/FileReader)
596
+ * [A simple HTTP Request & Response Service.](https://httpbin.org)
597
+ * [Hurl.it — Make HTTP Requests](https://www.hurl.it)
598
+
599
+ ## Thanks
600
+
601
+ This module is inspired by <https://github.com/gulpjs/vinyl> and <https://github.com/DefinitelyTyped/DefinitelyTyped/pull/12368> when I need a virtual File module for my [Chatie](https://github.com/Chatie) project.
602
+
603
+ ## Author
604
+
605
+ [Huan LI](https://github.com/huan) ([李卓桓](http://linkedin.com/in/zixia)), Microsoft AI MVP, zixia@zixia.net
606
+
607
+ [![Profile of Huan LI (李卓桓) on StackOverflow](https://stackexchange.com/users/flair/265499.png)](https://stackexchange.com/users/265499)
608
+
609
+ ## Copyright & License
610
+
611
+ * Docs released under Creative Commons
612
+ * Code released under the Apache-2.0 License
613
+ * Code & Docs © 2018 Huan LI \<zixia@zixia.net\>
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,4 @@
1
+ /// <reference path="../../../src/typings.d.ts" />
2
+ export { VERSION } from './version.js';
3
+ export declare const HTTP_TIMEOUT: number;
4
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../../src/config.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAEtC,eAAO,MAAM,YAAY,QACX,CAAA"}
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HTTP_TIMEOUT = exports.VERSION = void 0;
4
+ /// <reference path="./typings.d.ts" />
5
+ var version_js_1 = require("./version.js");
6
+ Object.defineProperty(exports, "VERSION", { enumerable: true, get: function () { return version_js_1.VERSION; } });
7
+ exports.HTTP_TIMEOUT = Number(process.env['FILEBOX_HTTP_TIMEOUT'])
8
+ || 60 * 1000;
9
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../../../src/config.ts"],"names":[],"mappings":";;;AAAA,uCAAuC;AACvC,2CAAsC;AAA7B,qGAAA,OAAO,OAAA;AAEH,QAAA,YAAY,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;OAClE,EAAE,GAAG,IAAI,CAAA"}