Skip to content

Commit 9db10ae

Browse files
committed
Initial TS Support
Not sure why I try to upgrade so many big libraries to TS and ESM, it gets complicated quickly hehe. My goal is that I want to have something like JSZip, but build using only vanilla library features, and to be fully ESM compatible, as well as have first-party type definitions. Also, using a Promise-based API is key as well. https://stackoverflow.com/questions/14527820/does-html-5s-blobbuilder-still-work-in-google-chrome https://gist.github.com/eligrey/1079572/aeac96e31f03bb8aeecedd5e5a6a89ac92c106cc https://www.google.com/search?q=require%28%27setimmediate%27%29 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat https://stackoverflow.com/questions/29676635/convert-uint8array-to-array-in-javascript
1 parent 2ceb998 commit 9db10ae

File tree

5 files changed

+50
-24
lines changed

5 files changed

+50
-24
lines changed

lib/support.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ else {
2020
}
2121
catch (e) {
2222
try {
23+
// @ts-expect-error - Legacy `Blob` constructor fallback
2324
var Builder = self.BlobBuilder || self.WebKitBlobBuilder || self.MozBlobBuilder || self.MSBlobBuilder;
2425
var builder = new Builder();
2526
builder.append(buffer);

lib/utils.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var support = require("./support");
44
var base64 = require("./base64");
55
var nodejsUtils = require("./nodejsUtils");
66
var external = require("./external");
7+
// @ts-expect-error - Legacy fallback, it seems
78
require("setimmediate");
89

910

@@ -12,7 +13,7 @@ require("setimmediate");
1213
* array but may have > 255 char codes. Be sure to take only the first byte
1314
* and returns the byte array.
1415
* @param {String} str the string to transform.
15-
* @return {Array|Uint8Array} the string in a binary format.
16+
* @return {Array<number>|Uint8Array} the string in a binary format.
1617
*/
1718
function string2binary(str) {
1819
var result = null;
@@ -76,8 +77,8 @@ function identity(input) {
7677
/**
7778
* Fill in an array with a string.
7879
* @param {String} str the string to use.
79-
* @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to fill in (will be mutated).
80-
* @return {Array|ArrayBuffer|Uint8Array|Buffer} the updated array.
80+
* @param {Array<number>|Uint8Array|Buffer} array the array to fill in (will be mutated).
81+
* @return {Array<number>|Uint8Array|Buffer} the updated array.
8182
*/
8283
function stringToArrayLike(str, array) {
8384
for (var i = 0; i < str.length; ++i) {
@@ -95,24 +96,24 @@ var arrayToStringHelper = {
9596
/**
9697
* Transform an array of int into a string, chunk by chunk.
9798
* See the performances notes on arrayLikeToString.
98-
* @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to transform.
99-
* @param {String} type the type of the array.
100-
* @param {Integer} chunk the chunk size.
99+
* @param {Array<number>|Uint8Array|Buffer} array the array to transform.
100+
* @param {"array" | "nodebuffer" | "uint8array"} type the type of the array.
101+
* @param {number} chunk the chunk size.
101102
* @return {String} the resulting string.
102103
* @throws Error if the chunk is too big for the stack.
103104
*/
104105
stringifyByChunk: function(array, type, chunk) {
105106
var result = [], k = 0, len = array.length;
106107
// shortcut
107108
if (len <= chunk) {
108-
return String.fromCharCode.apply(null, array);
109+
return String.fromCharCode.apply(null, [].slice.call(array));
109110
}
110111
while (k < len) {
111112
if (type === "array" || type === "nodebuffer") {
112-
result.push(String.fromCharCode.apply(null, array.slice(k, Math.min(k + chunk, len))));
113+
result.push(String.fromCharCode.apply(null, [].slice.call(/** @type {number[] | Buffer} */ (array).slice(k, Math.min(k + chunk, len)))));
113114
}
114115
else {
115-
result.push(String.fromCharCode.apply(null, array.subarray(k, Math.min(k + chunk, len))));
116+
result.push(String.fromCharCode.apply(null, [].slice.call(/** @type {Uint8Array} */(array).subarray(k, Math.min(k + chunk, len)))));
116117
}
117118
k += chunk;
118119
}
@@ -122,7 +123,7 @@ var arrayToStringHelper = {
122123
* Call String.fromCharCode on every item in the array.
123124
* This is the naive implementation, which generate A LOT of intermediate string.
124125
* This should be used when everything else fail.
125-
* @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to transform.
126+
* @param {Array<number>|Uint8Array|Buffer} array the array to transform.
126127
* @return {String} the result.
127128
*/
128129
stringifyByChar: function(array){
@@ -138,7 +139,7 @@ var arrayToStringHelper = {
138139
*/
139140
uint8array : (function () {
140141
try {
141-
return support.uint8array && String.fromCharCode.apply(null, new Uint8Array(1)).length === 1;
142+
return support.uint8array && String.fromCharCode.apply(null, [].slice.call(new Uint8Array(1))).length === 1;
142143
} catch (e) {
143144
return false;
144145
}
@@ -148,7 +149,7 @@ var arrayToStringHelper = {
148149
*/
149150
nodebuffer : (function () {
150151
try {
151-
return support.nodebuffer && String.fromCharCode.apply(null, nodejsUtils.allocBuffer(1)).length === 1;
152+
return support.nodebuffer && String.fromCharCode.apply(null, [].slice.call(nodejsUtils.allocBuffer(1))).length === 1;
152153
} catch (e) {
153154
return false;
154155
}
@@ -158,7 +159,7 @@ var arrayToStringHelper = {
158159

159160
/**
160161
* Transform an array-like object to a string.
161-
* @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to transform.
162+
* @param {Array<number>|Uint8Array|Buffer} array the array to transform.
162163
* @return {String} the result.
163164
*/
164165
function arrayLikeToString(array) {
@@ -345,8 +346,8 @@ exports.resolve = function(path) {
345346
/**
346347
* Return the type of the input.
347348
* The type will be in a format valid for JSZip.utils.transformTo : string, array, uint8array, arraybuffer.
348-
* @param {Object} input the input to identify.
349-
* @return {String} the (lowercase) type of the input.
349+
* @param {string|object} input the input to identify.
350+
* @return {"string"|"array"|"nodebuffer"|"arraybuffer"|"uint8array"} the (lowercase) type of the input.
350351
*/
351352
exports.getTypeOf = function(input) {
352353
if (typeof input === "string") {
@@ -368,11 +369,12 @@ exports.getTypeOf = function(input) {
368369

369370
/**
370371
* Throw an exception if the type is not supported.
371-
* @param {String} type the type to check.
372+
* @param {string} type the type to check.
372373
* @throws {Error} an Error if the browser doesn't support the requested type.
374+
* @returns {asserts type is keyof support}
373375
*/
374376
exports.checkSupport = function(type) {
375-
var supported = support[type.toLowerCase()];
377+
var supported = support[/** @type {keyof support} */ (type.toLowerCase())];
376378
if (!supported) {
377379
throw new Error(type + " is not supported by this platform");
378380
}
@@ -398,8 +400,11 @@ exports.pretty = function(str) {
398400

399401
/**
400402
* Defer the call of a function.
401-
* @param {Function} callback the function to call asynchronously.
402-
* @param {Array} args the arguments to give to the callback.
403+
* @template {(this: This, ...args: any[]) => void} T
404+
* @template This
405+
* @param {T} callback the function to call asynchronously.
406+
* @param {Parameters<T>} args the arguments to give to the callback.
407+
* @param {This | null} self
403408
*/
404409
exports.delay = function(callback, args, self) {
405410
setImmediate(function () {

package-lock.json

Lines changed: 22 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"inflate"
4343
],
4444
"devDependencies": {
45+
"@types/readable-stream": "^4.0.10",
4546
"benchmark": "^2.1.4",
4647
"browserify": "~13.0.0",
4748
"eslint": "^8.18.0",

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
3838

3939
/* JavaScript Support */
40-
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
41-
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
40+
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
41+
"checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
4242
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
4343

4444
/* Emit */

0 commit comments

Comments
 (0)